summaryrefslogtreecommitdiff
path: root/src/fsutils.cc
blob: 6058a182fedec77ba07ddc7414e5f267cb7aabad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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