c++filesystemsc++17

Is possible to get list of root names with std::filesystem?


I know how to get root name for some path and there is std::filesystem::directory_iterator for walking through over directory.

But how to get list with root names and if it's impossible then why?


Solution

  • Take a look at the recently-opensourced Microsoft implementation of std::filesystem, more specifically the helper function _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
            // * \\server\share
            //   We parse \\server as root-name, \ as root-directory, and share as the first element in relative-path.
            //   Technically, Windows considers all of \\server\share the logical "root", but for purposes
            //   of decomposition we want those split, so that path(R"(\\server\share)").replace_filename("other_share")
            //   is \\server\other_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.
            // * \\?\UNC\server\share
            //   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 \\server\share, CreateFile translates that into
            //   \\?\UNC\server\share 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.
    

    This highlights a number of issues: