From 052a162715449252bb6126c41dd1700b1440c394 Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Wed, 3 Jun 2015 23:38:36 +0200 Subject: Improve db_path --- src/fsutils.cc | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/fsutils.cc (limited to 'src/fsutils.cc') 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 +#include +#include +#include +#include + +#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(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 -- cgit v1.2.3-70-g09d2