Maximum Possible File Name Length in Windows Kernel

谁说我不能喝 提交于 2019-12-12 11:13:00

问题


I was wondering, what is the longest possible name length allowed by the Windows kernel?

E.g.: I know the kernel uses UNICODE_STRING structures to hold all object paths, and since the byte length of a wide-character string is stored inside a USHORT, that allows for a maximum path length of 2^15 - 1 characters. Is there a similar, hard restriction on a file name (rather than path)? (I don't care if NTFS or FAT32 imposes a particular restriction; I'm looking for the longest possible theoretically allowed name in the kernel, assuming no additional file system or shell restrictions.)

(Edit: For those wondering why this even matters, consider that normally, traversing a directory is achieved by FindFirstFile/FindNextFile calls, one call per file. Given the function named NtQueryDirectoryFile, which is the underlying system call and which returns multiple file names per call, it's actually possible to take advantage of this maximum-length restriction on the path to make an extremely-fast directory traverser that uses solely the stack as a buffer. Now I'm trying to extend that concept, and I need to know the maximum size of a file name.)


回答1:


The maximum length of a path is 32,767 characters whereby each path component (directory or file) can have a maximum length of 255 characters (to be more exact, the value returned in the lpMaximumComponentLength parameter of the GetVolumeInformation function).

This is documented on MSDN.




回答2:


Ah, I found this page myself that guarantees that file names can't be longer than 255 characters:

  • A pathname MUST be no more than 32,760 characters in length.
    ...
  • Each pathname component MUST be no more than 255 characters in length.

Which makes me wonder:

Why does Windows use ULONGs for file name lengths, when it uses USHORTs for path lengths?!

If anyone knows why this is, please post/comment! I'm rather curious. :)



来源:https://stackoverflow.com/questions/4624057/maximum-possible-file-name-length-in-windows-kernel

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