Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows API to access case-sensitive paths (Bash-on-Ubuntu-on-Windows)

Bash-on-Ubuntu-on-Windows supports case-sensitive file paths. This means that I can create two files or directories with names only differing in capitalization. I have issues accessing those files, though.

Running

bash -c "touch Magic ; mkdir magic ; echo Secret! > magic/secret"

Creates a file names Magic, a directory named magic and a file names secret in that directory.

bash -c "ls -lR" yields

.:
total 0
drwxrwxrwx 2 root root 0 Aug 23 10:37 magic
-rwxrwxrwx 1 root root 0 Aug 23 10:37 Magic

./magic:
total 0
-rwxrwxrwx 1 root root 8 Aug 23 10:37 secret

(I am not sure why I get root, as it is not the default user, but that does not seem relevant to my question.)

Windows Explorer shows: Windows Explorer shows file and directory

Now, while bash can easily access the magic/secret file in the directory, Windows seems to treat both the directory and the file as one and the same. So double-clicking the directory I get a "directory name invalid" error Error dbl-clicking directory

Same goes for using cd, as I get The directory name is invalid. printed out.

Are there any APIs that allow me to access those case-sensitive paths, or create them? It seems that regular Windows APIs ignore character case completely when accessing existing files.

like image 726
tmr232 Avatar asked Sep 18 '25 21:09

tmr232


1 Answers

Case-sensitive paths can be used on Windows with NTFS, but it requires a bit of extra work.

First, case-sensitivity must be enabled system-wide. This is done by setting the HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\kernel\ dword:ObCaseInsensitive registry value to 0, then restarting the system. I found this part here.

Once case-sensitivity is enabled, it is possible to use CreateFile to with case-sensitive paths. To do that, you have to pass the FILE_FLAG_POSIX_SEMANTICS as part of the dwFlagsAndAttributes parameter. From msdn:

Access will occur according to POSIX rules. This includes allowing multiple files with names, differing only in case, for file systems that support that naming.

I found this part in this answer.

By setting the registry setting and the CreateFile flag, I was able to access case-sensitive paths.

like image 175
tmr232 Avatar answered Sep 21 '25 19:09

tmr232