Opened 11 years ago

Last modified 4 years ago

#6813 new Bugs

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

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

Description

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 (https://svn.boost.org/trac/boost/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 (2)

comment:1 by Aaron Barany <akb825@…>, 11 years ago

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.

comment:2 by anonymous, 4 years ago

Is there any update on it?

Note: See TracTickets for help on using tickets.