diff options
| author | Joel Klinghed <the_jk@yahoo.com> | 2015-06-03 23:38:36 +0200 |
|---|---|---|
| committer | Joel Klinghed <the_jk@yahoo.com> | 2015-06-03 23:39:29 +0200 |
| commit | 052a162715449252bb6126c41dd1700b1440c394 (patch) | |
| tree | d4277bae097d8b2e476d4fab2f6e065995b651e2 /src/fsutils.cc | |
| parent | fec5364398c0cdb59db056c9530730a15c9fba9e (diff) | |
Improve db_path
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 |
