diff options
Diffstat (limited to 'src/fsutils.cc')
| -rw-r--r-- | src/fsutils.cc | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/fsutils.cc b/src/fsutils.cc new file mode 100644 index 0000000..6058a18 --- /dev/null +++ b/src/fsutils.cc @@ -0,0 +1,34 @@ +#include "common.hh" + +#include <sys/stat.h> +#include <sys/types.h> +#include <cerrno> +#include <cstring> +#include <libgen.h> + +#include "fsutils.hh" + +namespace stuff { + +bool isdir(const std::string& path) { + struct stat buf; + if (stat(path.c_str(), &buf)) { + return false; + } + return S_ISDIR(buf.st_mode); +} + +bool mkdir_p(const std::string& path) { + if (mkdir(path.c_str(), 0777)) { + if (errno == EEXIST) return isdir(path); + char* dir = dirname(const_cast<char*>(path.c_str())); + if (strcmp(dir, ".") == 0 || strcmp(dir, "/") == 0) return false; + if (!mkdir_p(dir)) return false; + if (mkdir(path.c_str(), 0777)) { + return errno == EEXIST && isdir(path); + } + } + return true; +} + +} // namespace |
