In C++20, is it possible to check the legality of a path using std::filesystem
under Windows?
By “legality”, I mean whether the path contains any “forbidden” characters, is too long, or is otherwise syntactically incorrect?
Windows 11, Visual Studio 2022, C++20.
5
The only reliable way to determine the validity of a path is to try opening it (or its parent directory, if you don’t know if the target entity exists). The reason is that within a path, there can exist reparse points (redirections) of different types; they can take you to a filesystem with a different set of rules. I.e., the path valid in a straightforward analysis may become invalid in the real application.
Thus, when you attempt to open the entity referenced by a path, your request travels through different filesystem filters, can get redirected elsewhere (and/or sent across the network), reach the filesystem and get redirected again; and on each step, the check may succeed or fail.
2