Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::filesystem exists() on directory path fails, but is_directory() is ok

Tags:

I'm getting path to current directory with boost filesystem, then checking if the directory exists.

is_directory() is ok, but exists() fails on the same path, am I missing something?

Example code (boost 1.35):

#include <boost/filesystem/operations.hpp> #include <boost/filesystem/path.hpp>  namespace fs = boost::filesystem;  // the path is full path /home/my/somedirectory     fs::path data_dir(fs::current_path());  fs::is_directory(data_dir)  // true, directory exists  fs::exists(data_dir)   // false exists(status(data_dir))  // false 

EDIT:

#include <boost/filesystem/operations.hpp> #include <boost/filesystem/path.hpp> namespace fs = boost::filesystem;  fs::path data_dir(fs::current_path()); // data_dir == "/home/myusername/proj/test"  if (fs::is_directory(data_dir))             // true - is directory if (fs::is_directory(fs::status(data_dir))) // true - it is still a directory 

Fun part:

if (fs::exists(fs::status(data_dir)))       // true - directory exists if (fs::exists( data_dir ))                 // true - directory exists 

BUT:

if (!fs::exists(fs::status(data_dir)))      // false - directory still exists if (!fs::exists( data_dir ))                // true  - directory does not exists 
like image 742
stefanB Avatar asked Feb 04 '10 23:02

stefanB


1 Answers

The following is from the Boost source code:

inline bool is_directory( file_status f ) { return f.type() == directory_file; } inline bool exists( file_status f )       { return f.type() != status_unknown && f.type() != file_not_found; } 

As you can see, if is_directory returns true then exists must return true as well. Maybe the problem is elsewhere in your code - please post a minimal compilable example that shows the problem.

You may also want to try using the same file_status object for both calls, to see if maybe the output status was changing.

like image 130
interjay Avatar answered Oct 24 '22 15:10

interjay