3

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.

tmr232
  • 1,171
  • 14
  • 23
  • 1
    OpenFileById() would work I guess. (Though you can't use it to delete files.) Otherwise, the kernel API (for device drivers) allows you to ask for case sensitivity, though support has to first be turned on via a registry setting. I would *guess* that installing the Ubuntu-on-Windows component will turn on kernel support for case sensitivity. You'd probably be better off giving up and manipulating these files via the Ubuntu component. :-) – Harry Johnston Aug 24 '16 at 00:05
  • The Windows 10 Creators Update allows you to launch Windows applications from the WSL shell. However, you cannot launch applications from directories that are inaccessible to Windows. It might be possible once the registry flag is set, but not without it (haven't tested yet). – tmr232 Apr 18 '17 at 15:07

1 Answers1

4

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.

Community
  • 1
  • 1
tmr232
  • 1,171
  • 14
  • 23
  • 2
    So the registry value *wasn't* already set to permit case sensitivity? That's interesting; I wonder how the Ubuntu-on-Windows supports it? – Harry Johnston Aug 24 '16 at 21:08
  • 1
    I guess kernel drivers can do more than user mode code ;) I'll look into it when I have the time. – tmr232 Aug 25 '16 at 08:05
  • @HarryJohnston [MS explained it like this](https://blogs.msdn.microsoft.com/wsl/2016/06/15/wsl-file-system-support/) "Case sensitivity is handled by Windows itself. As mentioned earlier, Windows and NTFS actually support case sensitive operations, so VolFs simply requests the Object Manager to treat paths as case sensitive regardless of the global registry key controlling this behavior." so it's not using that registry key anyway – phuclv Apr 09 '17 at 02:31
  • 2
    @LưuVĩnhPhúc, yeah, but if you pass the same request in from user-mode, it only works if the registry key is set. My guess would be that this check is only done for user-mode requests, not requests from the kernel. Alternatively, the flag might be treated differently because it is coming from a file system driver. Interesting read, though. – Harry Johnston Apr 09 '17 at 21:18