Ticket #6813 (new Bugs)

Opened 5 years ago

Last modified 5 years ago

canonical function converts root-directory separator from '\' to '/' on Windows

Reported by: Alex Goldberg <alex.goldberg@…> Owned by: bemandawes
Milestone: To Be Determined Component: filesystem
Version: Boost 1.49.0 Severity: Problem
Keywords: canonical Cc:


boost::filesystem::canonical reverses the path separator on rooted paths

Consider the following line:

boost::filesystem::path convertedPath = boost::filesystem::canonical("C:\\Foo\\Bar\\..\\Bar\\Baz");

On Windows, convertedPath is set to "C:/Foo\Bar\Baz", where I would expect it to be set to "C:\Foo\Bar\Baz"

The documentation states: "Returns: A canonical path that refers to the same file system object as absolute(p,base)."

If the input path has no symbolic links, 'dot' directories, or 'dot-dot' directories, then I would expect the output to match a call to boost::filesystem::absolute.

For example:

boost::filesystem::path canonicalPath = boost::filesystem::canonical("Bar\\Baz", "C:\\Foo");
boost::filesystem::path absolutePath = boost::filesystem::absolute("Bar\\Baz", "C:\\Foo");

canonicalPath is set to "C:/Foo\Bar\Baz", while absolutePath is set to "C:\Foo\Bar\Baz".

Looking at the implementation, this is related to the issue in ticket 5989 ( I agree with the sentiments in that ticket, but if the behavior of the path iterator will not be changing, then I think the canonical function should be updated to preseve path consistency in Windows.


Change History

comment:1 Changed 5 years ago by Aaron Barany <akb825@…>

This is also a general problem if you try to iterate over a path and reconstruct it with operator/. For example, if you have the following code:

boost::filesystem::path path = "C:\\Foo\\Bar\\Baz";
boost::filesystem::path finalPath;
for (boost::filesystem::path::iterator iter = path.begin(); iter != path.end(); ++iter)
   finalPath /= *iter;

The result for finalPath is going to be "C:/Foo\Bar\Baz". One way to solve this problem with keeping the iterator behavior the same is to always convert path separators to the preferred separator when appending paths.


Add a comment

Modify Ticket

Change Properties
<Author field>
as new

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.