5

When trying to create a folder in a mapped network drive with boost filesystem create_directories function, I get random errors of type: "The system cannot find the path specified.". By random I mean that sometimes I get the error and sometimes I don't.

And yes, I have checked:

  • It is a valid path.
  • It is not a too long path. In fact, I use windows extended path as \?\PATH.
  • The network drive works perfectly, and it is inside our company local network (Gigabit ethernet).
  • I have write permissions.
  • There are not unicode characters.

Below the code (adapted to make it more simple).

#include "boost/filesystem.hpp"
#include <string>
#include <chrono>
#include <thread>
#include <iostream>

#define MAX_RETRIES 10
#define RETRY_TIME 5000 //in millisecond

namespace fs = boost::filesystem;

bool createDirectory(const std::string& folderPath)
{        
    //If the function does not succeed to create the directory in first place, it will retry after RETRY_TIME miliseconds a maximum number of MAX_RETRIES.
    for (unsigned int i=0; i<MAX_RETRIES; i++)
    {
        try
        {
            if (fs::create_directories(fs::path(folderPath)))
            {   
                std::cerr << "Folder created successfully!" << std::endl;             
                return true;
            }
        }
        catch (fs::filesystem_error const & e)
        {
                std::cerr << "File operation problem with files: " << e.what() <<". Try number: "<<i+1<<std::endl;
        }

        if (i==MAX_RETRIES-1)
        {
            return false;
        }

        std::this_thread::sleep_for(std::chrono::milliseconds(RETRY_TIME)); 

    }
    return true;
}

In this example I am trying to create the folder:

Z:\PRIVATE_FOLDERS\XXX\tests\YYY\ZZZZZ\MMMMMMM\CCCCCC\@mmmm1\@ccccc1\@eeeeeeeee2

(Z is a network mapped drive):

And here is the output. Sometimes it works the first time

Folder created successfully!

Sometimes it fails several consecutive times until it works. Magic?

File operation problem with files: "boost::filesystem::create_directories: The system cannot find the path specified: "\\?\Z:\PRIVATE_FOLDERS\XXX\tests\YYY\ZZZZZ\MMMMMMM\CCCCCC\@mmmm1\@ccccc1\@eeeeeeeee2"". try number: 1
File operation problem with files: "boost::filesystem::create_directories: The system cannot find the path specified: "\\?\Z:\PRIVATE_FOLDERS\XXX\tests\YYY\ZZZZZ\MMMMMMM\CCCCCC\@mmmm1\@ccccc1\@eeeeeeeee2"". try number: 2
File operation problem with files: "boost::filesystem::create_directories: The system cannot find the path specified: "\\?\Z:\PRIVATE_FOLDERS\XXX\tests\YYY\ZZZZZ\MMMMMMM\CCCCCC\@mmmm1\@ccccc1\@eeeeeeeee2"". try number: 3
Folder created successfully!

The problem of this implementation is that, a priori there is no way to differentiate if the input path is a network drive, or it is a wrong path or does not have write permissions. Therefore I need always to spend the MAX_RETRIES until it finally fails, with the consequent performance penalty.

Notes:

  • I am on Windows 10
  • Boost version is 1.60 built on VS2015 downloaded from official webpage.
  • I use intel compiler 17 for the rest of the code.
sah
  • 71
  • 7
  • Have you seen https://stackoverflow.com/q/12186955/85371 - specifically about mixing debug/release builds (or, by extension: builds using different flags) – sehe May 21 '18 at 19:37
  • Similar things have been encountered on OSX, it appears: https://stackoverflow.com/questions/29423879/boost-filesystem-create-directories-mangles-directory-name – sehe May 21 '18 at 19:38
  • Thank you for the links. I have taken a look at them but I still think it is a different thing. I am using a release version of boost. In addition create_directories calls work sometimes and sometimes not. And this only happens with a network drive. I thought that maybe it has anything to do with certain timeout setup, but I didn't find anything similar to that in boost documentation. – sah May 22 '18 at 08:44
  • Just check that you're linking the same versions of runtime library (and other dependencies, such as Boost System). Are you also using [WSL](https://learn.microsoft.com/en-us/windows/wsl/install-win10)? There has been a reported bug there: https://stackoverflow.com/questions/50206890/intermittent-random-file-not-found-errors-under-windows-subsystem-for-linux – sehe May 22 '18 at 09:44
  • Thanks @sehe, I am almost sure 99.99% that I use the same runtime versions, since we download the whole boost headers + built dlls bundle from boost website, but I'll check again. With respect WLS I don't use it and btw the reported bug is about not finding headers while building, and my program builds perfectly but fails at run-time when trying to create a folder in a network drive. And it doesn't fail always!. I guess it must be something else. – sah May 22 '18 at 13:20
  • Yup. But a concurrency bug in the filesystem driver obviously affects your program too, not just the compiler. (I wasn't saying you have the same error, I was pointing at issues with WLS) – sehe May 22 '18 at 14:50
  • I confirm that the boost versions of headers and runtimes of all the dependencies are ok. BTW it is boost 1.60 instead of 1.52 (I already edited the post). I am not using WLS. – sah May 23 '18 at 10:04

0 Answers0