Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does boost::filesystem::remove_all(path) work?

Tags:

c++

boost

I am trying to remove all directories, subdirectories and the contained files from a specific path using boost::filesystem::remove_all(path). I also want to display an error message in case a file is open in another program. Does boost::filesystem::remove_all(path) throw an exception in this case?

Or is there another way I can achieve this?

like image 710
AlexandraC Avatar asked Jan 16 '14 14:01

AlexandraC


1 Answers

this does not fit in a comment so I'm posting as an answer

Just look in the source: http://www.boost.org/doc/libs/1_55_0/libs/filesystem/src/operations.cpp

  BOOST_FILESYSTEM_DECL
  boost::uintmax_t remove_all(const path& p, error_code* ec)
  {
    error_code tmp_ec;
    file_type type = query_file_type(p, &tmp_ec);
    if (error(type == status_error, tmp_ec, p, ec,
      "boost::filesystem::remove_all"))
      return 0;

    return (type != status_error && type != file_not_found) // exists
      ? remove_all_aux(p, type, ec)
      : 0;
  }

remove_all_aux is defined few lines above and so is remove_file_or_directory, remove_file, remove_directory and so forth and so on. The primitive operations are:

# if defined(BOOST_POSIX_API)
... 
#   define BOOST_REMOVE_DIRECTORY(P)(::rmdir(P)== 0)
#   define BOOST_DELETE_FILE(P)(::unlink(P)== 0)
...
# else  // BOOST_WINDOWS_API
...
#   define BOOST_REMOVE_DIRECTORY(P)(::RemoveDirectoryW(P)!= 0)
#   define BOOST_DELETE_FILE(P)(::DeleteFileW(P)!= 0)
...
# endif

The behavior of removing a locked file will be whatever your platform will provide.

like image 146
Remus Rusanu Avatar answered Sep 28 '22 16:09

Remus Rusanu