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.