是否可以使用 std::文件系统获取根名称列表?



我知道如何获取某些路径的根名称,并且有std::filesystem::d irectory_itirator用于遍历目录。

但是如何获取带有根名称的列表,如果不可能,那为什么?

看看最近开源的Microsoft实现std::filesystem,更具体地说是辅助函数_Find_root_name_end

// This is the place in the generic grammar where library implementations have the most freedom.
// Below are example Windows paths, and what we've decided to do with them:
// * X:DriveRelative, X:DosAbsolute
//   We parse X: as root-name, if and only if  is present we consider that root-directory
// * RootRelative
//   We parse no root-name, and  as root-directory
// * \servershare
//   We parse \server as root-name,  as root-directory, and share as the first element in relative-path.
//   Technically, Windows considers all of \servershare the logical "root", but for purposes
//   of decomposition we want those split, so that path(R"(\servershare)").replace_filename("other_share")
//   is \serverother_share
// * \?device
// * ??device
// * \.device
//   CreateFile appears to treat these as the same thing; we will set the first three characters as root-name
//   and the first  as root-directory. Support for these prefixes varies by particular Windows version, but
//   for the purposes of path decomposition we don't need to worry about that.
// * \?UNCservershare
//   MSDN explicitly documents the \?UNC syntax as a special case. What actually happens is that the device
//   Mup, or "Multiple UNC provider", owns the path \?UNC in the NT namespace, and is responsible for the
//   network file access. When the user says \servershare, CreateFile translates that into
//   \?UNCservershare to get the remote server access behavior. Because NT treats this like any other
//   device, we have chosen to treat this as the \? case above.

这突出了一些问题:

  • Windows 中的驱动器号可以来来去去,因此"有效根列表"将因调用而异
  • \servershare的情况需要"有效根列表"来枚举基本上整个互联网,因为\1.2.3.4是一个有效的主机。

最新更新