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

不问归期 提交于 2020-01-01 14:40:13

问题


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:

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

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.


回答1:


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.



来源:https://stackoverflow.com/questions/39095594/windows-api-to-access-case-sensitive-paths-bash-on-ubuntu-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!