1

I would like to have a best prectice for this subject. I have to do some path magic prior to actually doing something.

using namespace boost::filesystem;
path p = "apple"; // apple 
p /= "pear";      // apple/pear
p /= "..";        // apple/pear/..                      // apple
p /= "chicken";   // apple/pear/../chicken              // apple/chichken
p /= "..";        // apple/pear/../chicken/..           // apple
p /= "..";        // apple/pear/../chicken/../..        // .
p /= "..";        // apple/pear/../chicken/../../..     // ..
p /= "cow";       // apple/pear/../chicken/../../../cow // ../cow
path s = "\\tmp\\file857463.tmp"; // s.is_relative() is true on windows

In every line after the first // you can see what I get when I print it. After the second // you can see what I would like to see (if differs)

What intermediate step can/should I do if I would like to see this behavior, considering that I surely do not want to touch the filesystem. After the final form appears I would like to test that the reference points inside the current directory or not (still without accessing the FS).

I would like to detect upwards jumps like \ (windows) or \\ (windows) or / on Linux. Quote from manual: On Windows, path("/foo").is_absolute() returns false. Which is IMHO very scientific, but useless. The main goal for me is to prevent upwards references without accessing characters by hand and without boost accessing the FS. (If I have to touch any characters manually I rather write my own path utility all together.)

I failed to find a robust concept for detecting drive root references. I tried boost documentation and Google too.

Mat
  • 202,337
  • 40
  • 393
  • 406
Notinlist
  • 16,144
  • 10
  • 57
  • 99

1 Answers1

1

Try to use canonical: http://www.boost.org/doc/libs/1_50_0/libs/filesystem/doc/reference.html#canonical

Pawel Zubrycki
  • 2,703
  • 17
  • 26
  • Overview: Converts p, which must exist, to an absolute path that has no symbolic link, dot, or dot-dot elements. I mentioned mutiple times that I do not want to touch the actual FS. – Notinlist Aug 08 '12 at 09:29
  • In what way it touches FS? What do you mean by "not touching FS"? And why -1 for someone who tried to help? Do you even know it has different purpose? – Pawel Zubrycki Aug 08 '12 at 09:58
  • You may use canonical with function from answer to http://stackoverflow.com/a/10167550/527807 – Pawel Zubrycki Aug 08 '12 at 10:02
  • How is it ensures that the path has no symbolic links without touching the FS? – Notinlist Aug 08 '12 at 10:47