Skip to content

<filesystem>: lexically_relative() can not handle long path with prefix \\?\ #2256

@Trafo

Description

@Trafo

Describe the bug
I would expect that this line of code is true:
std::filesystem::path("\\\\?\\C:\\a\\d").lexically_relative("\\\\?\\C:\\a") == "d"
But it is not, because an empty string will be returned. The reason for that is, that inside of lexically_relative() a check _Relative_path_contains_root_name() will be executed, but this is always true. The reason is how the filesystem is handling the root name as explained in _Find_root_name_end(). There is pointed out, that in my case \\?\ is the root name. But at the same time, the path contains C:\ which is a valid root name as well. Because of that, we have the weird behaving, that:
std::filesystem::path("\\\\?\\C:\\a\\d").relative_path() == "C:\\a\\d") which is not a relative path and causes the misbehaving of lexically_relative, because it can find another root_name inside of the "relative" path.

Command-line test case

#include <iostream>
#include <filesystem>
#include <cassert>
namespace fs = std::filesystem;
 
int main()
{
    assert(fs::path("C:\\a\\d").lexically_relative("C:\\a") == "d");
}

Expected behavior
That \\?\C:\ will be treated as root path and not \\?\

STL version

  • 16.11.3

Additional context
In another issue #1921 StephanTLavavej pointed out, that \\?\ only should be used by expert users and I agree. But in case I want to use the new Desktop-Bridge I had to use the new manifest appxmanifest, which not has a property anymore longPathAware. So I don't know how my app gives the consence of supporting that. (Is there maybe a better way to opt-in to the long path app wise) And I don't find any documentation for this use case, besides https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=cmd. And so my only solution to solve this issue is, of converting the paths with the prefix \\?\. But this from my point of view is not well handled by the std::filesystem lib.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfilesystemC++17 filesystemfixedSomething works now, yay!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions