Given the following code,
namespace fs = boost::filesystem;
fs::path parentPath("/home/user1/");
fs::path childPath("/home/user1/Downloads/Books");
std::cout << childPath.lexically_relative(parentPath) << '\n';
I expect the output to be "Downloads/Books"
, but instead it is "../Downloads/Books"
. If I change the parentPath
to "/home/user1"
(removing the trailing separator), I do get my expected output.
In my real code, the paths often times come in as strings from data. In my context, a trailing slash at the end is unintentional and should be ignored for the purposes of any logic. Basically, in my program "/home/user1"
is identical to "/home/user1/"
. However the logic of boost::filesystem::path
doesn't reflect this assertion.
So my question is two fold:
Why does the trailing separator affect the behavior? I just can't quite understand why it steps up a directory with ".."
in this case.
How can I make path
ignore or normalize trailing slashes globally in my program? For example, "sanitizing" my path strings before I construct path
objects would be too much of a maintenance burden, so I need something more inherent if possible.
Here's a live sample I've been using for testing.
I found out shortly after posting this question that this is a bug, reported here. I thought initially that I may have been doing something wrong. Even though my question doesn't make much sense at this point, I didn't want to just delete it in case others run into the same issue.