aboutsummaryrefslogtreecommitdiff
path: root/tools/atree/fs.cpp
blob: 15d6092155e7ee969fc6e23ac7d7a2076b8c47e2 (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "fs.h"
#include "files.h"
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <string>
#include <vector>
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
#include <host/CopyFile.h>

using namespace std;

static bool
is_dir(const string& path)
{
    int err;
    struct stat st;
    err = stat(path.c_str(), &st);
    return err != 0 || S_ISDIR(st.st_mode);
}

static int
remove_file(const string& path)
{
    int err = unlink(path.c_str());
    if (err != 0) {
        fprintf(stderr, "error deleting file %s (%s)\n", path.c_str(),
                strerror(errno));
        return errno;
    }
    return 0;
}

int
remove_recursively(const string& path)
{
    int err;

    if (is_dir(path)) {
        DIR *d = opendir(path.c_str());
        if (d == NULL) {
            fprintf(stderr, "error getting directory contents %s (%s)\n",
                    path.c_str(), strerror(errno));
            return errno;
        }

        vector<string> files;
        vector<string> dirs;

        struct dirent *ent;
        while (NULL != (ent = readdir(d))) {
            if (0 == strcmp(".", ent->d_name)
                    || 0 == strcmp("..", ent->d_name)) {
                continue;
            }
            string full = path;
            full += '/';
            full += ent->d_name;
#ifdef HAVE_DIRENT_D_TYPE
            bool is_directory = (ent->d_type == DT_DIR);
#else
	    	// If dirent.d_type is missing, then use stat instead
			struct stat stat_buf;
			stat(full.c_str(), &stat_buf);
			bool is_directory = S_ISDIR(stat_buf.st_mode);
#endif
            if (is_directory) {
                dirs.push_back(full);
            } else {
                files.push_back(full);
            }
        }
        closedir(d);

        for (vector<string>::iterator it=files.begin(); it!=files.end(); it++) {
            err = remove_file(*it);
            if (err != 0) {
                return err;
            }
        }

        for (vector<string>::iterator it=dirs.begin(); it!=dirs.end(); it++) {
            err = remove_recursively(*it);
            if (err != 0) {
                return err;
            }
        }

        err = rmdir(path.c_str());
        if (err != 0) {
            fprintf(stderr, "error deleting directory %s (%s)\n", path.c_str(),
                    strerror(errno));
            return errno;
        }
        return 0;
    } else {
        return remove_file(path);
    }
}

int
mkdir_recursively(const string& path)
{
    int err;
    size_t pos = 0;
    while (true) {
        pos = path.find('/', pos);
        string p = path.substr(0, pos);
        struct stat st;
        err = stat(p.c_str(), &st);
        if (err != 0) {
            err = mkdir(p.c_str(), 0770);
            if (err != 0) {
                fprintf(stderr, "can't create directory %s (%s)\n",
                        path.c_str(), strerror(errno));
                return errno;
            }
        }
        else if (!S_ISDIR(st.st_mode)) {
            fprintf(stderr, "can't create directory %s because %s is a file.\n",
                        path.c_str(), p.c_str());
            return 1;
        }
        pos++;
        if (p == path) {
            return 0;
        }
    }
}

int
copy_file(const string& src, const string& dst)
{
    int err;

    err = copyFile(src.c_str(), dst.c_str(),
                    COPY_NO_DEREFERENCE | COPY_FORCE | COPY_PERMISSIONS);
    return err;
}