CreateFile non-error on filename with trailing backslash inside a junction point.

J

jkloth

When using the Windows API CreateFile to access a file created within a junction point, it does *NOT* fail when an existing filename ends with a backslash. This is opposite of using "resolved" filename (still with backslash). To demonstrate:


<in cmd.exe>

mkdir %TEMP%\real

mklink /j %TEMP%\junc %TEMP%\real


// create the test file

h = CreateFile("%TEMP%\\real\\file", GENERIC_WRITE, NULL, 0, 0, CREATE_NEW, FILE_ATTRIBUTES_NORMAL, 0);

assert(h != INVALID_HANDLE_VALUE);

CloseHandle(h);


// opening from source works

h = CreateFile("%TEMP%\\real\\file", GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);

assert(h != INVALID_HANDLE_VALUE);

CloseHandle(h);


// opening from junction works

h = CreateFile("%TEMP%\\junc\\file", GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);

assert(h != INVALID_HANDLE_VALUE);

CloseHandle(h);


// trailing slash fails from source (correct)

h = CreateFile("%TEMP%\\real\\file\\", GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);

assert(h == INVALID_HANDLE_VALUE);


// trailing slash succeeds! (incorrect)

h = CreateFile("%TEMP%\\junc\\file\\", GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);

assert(h == INVALID_HANDLE_VALUE); // bombs


Is this expected behavior for CreateFile()? If yes, then which function should be used in its stead to achieve errors with invalid filenames? I know that this can be worked around by checking the last character for the slash, but that shouldn't be necessary, when in the "normal" (non-junction) case, It Just Works.


Point of note, cmd.exe internal commands (dir, type) correctly fail when given the filename with the trailing slash.


Insight please!

Continue reading...
 
Back
Top Bottom