简体   繁体   中英

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资源管理器显示文件和目录

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 dbl单击目录时出错

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM