0

So, I am using a simple code like

boost::system::error_code ec;
if (!boost::filesystem::create_directory(out_path.parent_path(), ec)) {
    std::cerr << "couldn't create directory\n";
    std::cerr << ec.message() << "\n";
    return -1;
}

I am trying to create directory passing absolute file path as out_path. The result is, I get into if branch, and the ec.message() returns Success. I have all rights over the out_path directory, read, write, execute whatsoever.

I am completely pissed of. I can create the directory with the same very path right from bash, but I cannot using the c++ code I have written. There is no fail however, when I pass relative pass as out_path, like "temp", and directory gets created from where the c++ program is run.

Help me please, I really don't understand whats goin on here :(

Andy Silver
  • 155
  • 3
  • 8
  • "I have all rights over the out_path directory" I'm confused, is out_path existed? – Danh Oct 26 '16 at 14:44
  • And why don't you use create_directories instead of create_directory? – Danh Oct 26 '16 at 14:49
  • The `out_path` doesn't exist, but `out_path.parent_path()` does. I also tried to use `create_directories` with same result UPD `out_path.parent_path().parent_path()` exists, `out_path.parent_path()` does not – Andy Silver Oct 26 '16 at 14:56
  • then out_path.parent_path() is existed, why do want to create it again? – Danh Oct 26 '16 at 14:57
  • Sorry, I am imprecise. It also doesn't exist. Say, I have /A/B/C, then out_path is /A/B/C/D/file. I want to create D under /A/B/C, which exists. – Andy Silver Oct 26 '16 at 14:58
  • Ok, i have figured something out I am trying to create directory using `boost::filesystem::path`, and it fails. I hardcode the very same path as `char *`, and I succeed. Weird. – Andy Silver Oct 26 '16 at 15:16
  • Have you tried to print that path – Danh Oct 26 '16 at 15:24
  • You know what? My path contains subfolder named 0. This means, I have path like .../0/... and somewhy it turns out the path is trimmed to that very 0! I don't know whether it is a std::string problem or boost, but what I see now is that even though `out_path.c_str()` is identical to `char *` variant when printed, they have different strlen! – Andy Silver Oct 26 '16 at 15:25
  • They have different strlen, that's the problem. Try to hex-dump it, I doubt that there's some Unicode value in your path. – Danh Oct 26 '16 at 15:27
  • Oh, my bad. I indeed used extra parent_path()! Danh, excuse me so much for taking your time! – Andy Silver Oct 26 '16 at 15:39

1 Answers1

3

When create_directory returns false it means it did not create the directory. The most likely reason for that is that the directory already exists! In that case it would return false, but not set the error_code.

The code in your question assumes if the function returns false then an error happened, but that's wrong. False just means it didn't create it.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • ok, so it basically doesn't create the directory and gives you no reason why? Only STL designers geniuses make such APIs. – FunkyKowal Apr 01 '20 at 12:51
  • 2
    The design comes from Boost, not the STL, and not the C++ standard library, so direct your snark elsewhere please. – Jonathan Wakely Apr 02 '20 at 15:47