13 #ifndef __STOUT_OS_POSIX_RMDIR_HPP__
14 #define __STOUT_OS_POSIX_RMDIR_HPP__
20 #include <glog/logging.h>
42 #ifndef __sun // FTS is not available on Solaris.
44 const std::string& directory,
45 bool recursive =
true,
46 bool removeRoot =
true,
47 bool continueOnError =
false)
49 unsigned int errorCount = 0;
52 if (::
rmdir(directory.c_str()) < 0) {
63 char*
paths[] = {
const_cast<char*
>(directory.c_str()),
nullptr};
67 FTS* tree = fts_open(paths, (FTS_NOCHDIR | FTS_PHYSICAL),
nullptr);
68 if (tree ==
nullptr) {
73 while ((node = fts_read(tree)) !=
nullptr) {
74 switch (node->fts_info) {
78 if (!removeRoot && node->fts_level == FTS_ROOTLEVEL) {
82 if (::
rmdir(node->fts_path) < 0 && errno != ENOENT) {
83 if (continueOnError) {
84 LOG(ERROR) <<
"Failed to delete directory "
103 if (::unlink(node->fts_path) < 0 && errno != ENOENT) {
104 if (continueOnError) {
105 LOG(ERROR) <<
"Failed to delete path "
127 if (fts_close(tree) < 0) {
132 if (errorCount > 0) {
133 return Error(
"Failed to delete " +
stringify(errorCount) +
" paths");
143 #endif // __STOUT_OS_POSIX_RMDIR_HPP__
std::string strerror(int errno_)
A thread-safe version of strerror.
Definition: strerror.hpp:30
Try< Nothing > rmdir(const std::string &directory, bool recursive=true, bool removeRoot=true, bool continueOnError=false)
Definition: rmdir.hpp:43
bool exists(const std::string &path)
Definition: exists.hpp:26
Definition: nothing.hpp:16
Definition: errorbase.hpp:35
std::string paths()
Definition: os.hpp:136
Definition: errorbase.hpp:49
std::string join(const std::string &path1, const std::string &path2, const char _separator=os::PATH_SEPARATOR)
Definition: path.hpp:56
std::string error(const std::string &msg, uint32_t code)
std::string stringify(int flags)