Error 206 The filename or extension is too long

We welcome any suggestions for new features or improvements in Altap Salamander. Please post one suggestion per report.
Stefan von Allmen

Error 206 The filename or extension is too long

Post by Stefan von Allmen » 26 Feb 2006, 01:27

I would like to bring something to your attention which might help to improve Servant Salamander.

With ordinary path names, Windows API is limited to a maximum path length of 260 characters. You might however avoid this limit by prepending \\?\ to the fully qualified path name for local files (i.e \\?\C:\WINDOWS\Win.ini) or replace the initial \\ by \\?\UNC\ for an UNC path (i.e. \\?\UNC\Server\Share\File.ext). By doing so, the maximum path length is at around 32000 characters

This can circumvent the error mentioned in the subject.

Jan Rysavy
ALTAP Staff
ALTAP Staff
Posts: 5196
Joined: 08 Dec 2005, 06:34
Location: Novy Bor, Czech Republic
Contact:

Post by Jan Rysavy » 26 Feb 2006, 08:20

Moderator: moved to Feature Requests.

thehappyhippo
Posts: 1
Joined: 06 Feb 2006, 14:52

Post by thehappyhippo » 28 Feb 2006, 23:07

I support this request. I got no error message, but the limitation occures by subdirectories with a path length larger than 260 characters. This is often the case on network shares (e.g Sama export of UNIX volumes).

I have written a small program that demonstrates the problem. This program creates 20 subdirectories "a" and an file (filexx.txt) in each directory. Then the directories are renamed upwards to a 20 character long name. At the top level the program tries to open the file at the lowest level directory (file19.txt) with CreateFileW and with the \\?\ prefix.

With SS and Explorer you should not able to reach a directory lower then level 10 (file10.txt).

The program started with d renames the directories to the short names, so you can delete it.

Here is the code:

Code: Select all

/*
  Demonstrates usage of CreateFile with paths longer than MAX_PATH
  SS should implement this feature on W2K and newer Windows versions
  
  The auhtor does not accept any WARRANTY for direct or indirect damages caused by this program.
  Use it at your own risk!
  
  see also http://msdn.microsoft.com/library/en-us/fileio/fs/createfile.asp
*/
#include <windows.h>
#include <tchar.h>
#include <stdio.h>

#define PATHLENGTH 20*20+30

void printErrorAndExit(const char *msg, DWORD err)
{
	LPSTR lpMsgBuf;
	if(FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
			          FORMAT_MESSAGE_FROM_SYSTEM | 
					  FORMAT_MESSAGE_IGNORE_INSERTS,
						NULL,
						err,
					  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
					  (LPSTR) &lpMsgBuf,
					  0,
					  NULL ))
	{
		fprintf(stderr,"%s : %s\n",msg,lpMsgBuf);
		LocalFree(lpMsgBuf);
	}
	else
	{
		fprintf(stderr,"Error at FormatMesage: %d\n",err=GetLastError());
	}
	//exit(err);
}



int main(int argc, char *argv[])
{
    int i;
    wchar_t tmp[100];
    wchar_t *path;
    wchar_t *upath;
    wchar_t *curdir;
    HANDLE f;
    
    if(argc==1)
    {
        fprintf(stderr,"%s\nusage: c(reate)|d(delete)\n",argv[0]);
        exit(1);
    }
    else
        if(*argv[1]=='c')
        {
            for(i=0;i<20;i++)
            {
                DWORD written;
                if(!CreateDirectoryW(L"a",NULL)) //create new dir a
                    printErrorAndExit("CreateDirectory",GetLastError());
                if(!SetCurrentDirectoryW(L"a")) //change to a
                    printErrorAndExit("SetCurrentDirectory",GetLastError());
                swprintf(tmp,L"file%2d.txt",i); //create file
                f=CreateFileW(tmp,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
                WriteFile(f,tmp,10*sizeof(wchar_t),&written,NULL);
                CloseHandle(f);
            }
    
            for(i=0;i<20;i++) //rename Directories recursiv
            {
                SetCurrentDirectoryW(L"..");//go upwards
                if(!MoveFileW(L"a",L"01234567890123456789")) /* rename Directory to 20 long name */
                    printErrorAndExit("MoveFile",GetLastError());
            }
            /* now 20*20+19*Backslash + Basedirectory >> MAX_PATH */
            
            DWORD len=0;
            len=GetCurrentDirectoryW(len,NULL); //get Basedirectory
            curdir=new wchar_t[len+1]; 
            path=new wchar_t[PATHLENGTH+len+10];
            GetCurrentDirectoryW(len,curdir);
            wprintf(L"%s\n",curdir);
            
            //now the file10.txt exceeds MAX_PATH
            for(i=0;i<20;i++)
            {
                if(i==0) //build the complete path to file19.txt
                    swprintf(path,L"%s\\01234567890123456789\\",curdir);
                else
                    wcscat(path,L"01234567890123456789\\");
            }
            wcscat(path,L"file19.txt"); // add file name 
            wprintf(L"Try to open %s\n",path);
            // try to open the normal way, should not work !!!
            if(INVALID_HANDLE_VALUE==CreateFileW(path,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL))
                printErrorAndExit("CreateFile 1",GetLastError());
                        
            upath=new wchar_t[wcslen(path)+4];
            swprintf(upath,L"\\\\?\\%s",path); // use UNICODE path prefix, works also for networkshares
                       
            wprintf(L"Try to open %s\n",upath);
            // open with unicode prefix, allows 32k path length
            if(INVALID_HANDLE_VALUE==CreateFileW(upath,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL))
                printErrorAndExit("CreateFile 2",GetLastError());
            // should work
            delete curdir;
            delete upath;
            delete path;
        }
        else if(*argv[1]=='d') // rename the directories to short names
        { //rename to a
            for(i=0;i<20;i++)
            {
                if(!MoveFileW(L"01234567890123456789",L"a"))
                    printErrorAndExit("MoveFile 1",GetLastError());
                if(!SetCurrentDirectoryW(L"a"))
                     printErrorAndExit("SetCurrentDirectory 1",GetLastError());
            }
            //now you can delete the directories
        }  
        
    return 0;        
}

Petr Solin
ALTAP Staff
ALTAP Staff
Posts: 1110
Joined: 08 Dec 2005, 09:13
Location: Novy Bor, Czech Republic
Contact:

Post by Petr Solin » 01 Mar 2006, 10:58

I know about this NTFS feature, but at least for now I think it would not be reasonable to support it, because most of applications can't work with such extra-long file names (neither Windows Explorer - at least under WinXP). Moreover not all Win32 API functions support this extra-long path format, so it will probably lead us to some unexpected troubles.

I have added support for working with too long file names on not too long pathes (less than 260 characters) to current working version of Salamander. It's based on dos-file-names (it works only if path length plus 12 characters (8.3 names) is less than 260 characters). Windows Explorer under WinXP uses the same thing.


therube
Posts: 627
Joined: 14 Dec 2006, 06:22

Re: Error 206 The filename or extension is too long

Post by therube » 04 Sep 2010, 01:30

To take advantage of auto path shrinking, you do need to not turn off short name generation.
And then it goes to link to an article, which shows four possible values.
And if you search you will find other articles, which shows two possible values.

So what settings you can use depends on OS ...

And then some more searching turns up (older articles):
If you have not installed any 16-bit applications on a Windows NT-based computer, you can turn off automatic short (8-character name, 3-character extension) file name generation to speed up file and folder access on your computer running Windows NT.

http://support.microsoft.com/kb/210638
Which kind of implies that if you have pre-existing 16-bit apps installed, it would bugger up such apps, as presumably they do store SFN in the Windows Registry?


So things may not be as simple as that.


Not to mention if I turned off 8.3, when I fire up Norton Commander 5.0 (DOS), what would I see ;-)?
WinXP Pro SP3 or Win7 x86 | SS 2.54

User avatar
SvA
Posts: 461
Joined: 29 Mar 2006, 02:41
Location: DE

Re: Error 206 The filename or extension is too long

Post by SvA » 05 Sep 2010, 16:50

Therube, you misread:
To take advantage of auto path shrinking, you do need to not turn off short name generation.
8dot3names are required for this "auto path shrinking" to work.

therube
Posts: 627
Joined: 14 Dec 2006, 06:22

Re: Error 206 The filename or extension is too long

Post by therube » 06 Sep 2010, 13:52

Heh. You're right. (And I read that a number of times because it was confusing to me.)
WinXP Pro SP3 or Win7 x86 | SS 2.54

roman2
Posts: 106
Joined: 07 Aug 2006, 11:11

Re: Error 206 The filename or extension is too long

Post by roman2 » 18 Apr 2011, 21:56

I have to resort to using Windows Explorer when dealing with very long paths. When will this be addressed?

Post Reply