forked from rethinkdb/rethinkdb_rebirth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaths.cc
223 lines (195 loc) · 6.35 KB
/
paths.cc
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "paths.hpp"
#include <fcntl.h>
#include <limits.h>
#include <unistd.h>
#ifdef _WIN32
#include "windows.hpp"
#include <io.h> // NOLINT
#include <direct.h> // NOLINT
#ifndef __MINGW32__
#include <filesystem>
#endif
#else // _WIN32
#include <ftw.h>
#endif // _WIN32
#include "arch/io/disk.hpp"
#include "clustering/administration/main/directory_lock.hpp"
#include "logger.hpp"
#ifdef _MSC_VER
int remove_directory_helper(const char *path) {
logNTC("In recursion: removing file '%s'\n", path);
DWORD attrs = GetFileAttributes(path);
if (GetLastError() == ERROR_FILE_NOT_FOUND) {
logWRN("Trying to delete non-existent file '%s'", path);
return 0;
} else {
guarantee_winerr(attrs != INVALID_FILE_ATTRIBUTES, "GetFileAttributes failed");
}
BOOL res;
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
res = RemoveDirectory(path);
} else {
res = DeleteFile(path);
}
guarantee_winerr(res, "failed to remove: '%s': %s", path, winerr_string(GetLastError()).c_str());
return 0;
}
#else
int remove_directory_helper(const char *path, UNUSED const struct stat *, UNUSED int, UNUSED struct FTW *) {
logNTC("In recursion: removing file '%s'\n", path);
int res = ::remove(path);
guarantee_err(res == 0, "Fatal error: failed to delete '%s'.", path);
return 0;
}
#endif
void remove_directory_recursive(const char *dirpath) {
#ifdef _MSC_VER
using namespace std::tr2; // NOLINT
std::function<void(sys::path)> go = [&go](sys::path dir){
for (auto it : sys::directory_iterator(dir)) {
if (sys::is_directory(it.status())) {
go(it.path());
} else {
remove_directory_helper(it.path().string().c_str());
}
}
remove_directory_helper(dir.string().c_str());
};
go(dirpath);
#else
// max_openfd is ignored on OS X (which claims the parameter
// specifies the maximum traversal depth) and used by Linux to
// limit the number of file descriptors that are open (by opening
// and closing directories extra times if it needs to go deeper
// than that).
const int max_openfd = 128;
logNTC("Recursively removing directory %s\n", dirpath);
int res = nftw(dirpath, remove_directory_helper, max_openfd, FTW_PHYS | FTW_MOUNT | FTW_DEPTH);
guarantee_err(res == 0 || get_errno() == ENOENT, "Trouble while traversing and destroying temporary directory %s.", dirpath);
#endif
}
base_path_t::base_path_t(const std::string &_path) : path_(_path) { }
void base_path_t::make_absolute() {
#ifdef _WIN32
char absolute_path[MAX_PATH];
DWORD size = GetFullPathName(path_.c_str(), sizeof(absolute_path), absolute_path, nullptr);
guarantee_winerr(size != 0, "GetFullPathName failed");
if (size < sizeof(absolute_path)) {
path_.assign(absolute_path);
return;
}
std::string long_absolute_path;
long_absolute_path.resize(size);
DWORD new_size = GetFullPathName(path_.c_str(), size, &long_absolute_path[0], nullptr);
guarantee_winerr(size != 0, "GetFullPathName failed");
guarantee(new_size < size, "GetFullPathName: name too long");
path_ = std::move(long_absolute_path);
#else
char absolute_path[PATH_MAX];
char *res = realpath(path_.c_str(), absolute_path);
guarantee_err(res != nullptr, "Failed to determine absolute path for '%s'", path_.c_str());
path_.assign(absolute_path);
#endif
}
const std::string& base_path_t::path() const {
guarantee(!path_.empty());
return path_;
}
std::string temporary_directory_path(const base_path_t& base_path) {
return base_path.path() + "/tmp";
}
bool is_rw_directory(const base_path_t& path) {
#ifdef _WIN32
if (_access(path.path().c_str(), 06 /* read and write */) != 0)
return false;
#else
if (access(path.path().c_str(), R_OK | F_OK | W_OK) != 0)
return false;
#endif
struct stat details;
if (stat(path.path().c_str(), &details) != 0)
return false;
return (details.st_mode & S_IFDIR) > 0;
}
void recreate_temporary_directory(const base_path_t& base_path) {
const base_path_t path(temporary_directory_path(base_path));
if (is_rw_directory(path) && check_dir_emptiness(path))
return;
remove_directory_recursive(path.path().c_str());
int res;
#ifdef _WIN32
res = _mkdir(path.path().c_str());
#else
do {
res = mkdir(path.path().c_str(), 0755);
} while (res == -1 && get_errno() == EINTR);
#endif
guarantee_err(res == 0, "mkdir of temporary directory %s failed",
path.path().c_str());
// Call fsync() on the parent directory to guarantee that the newly
// created directory's directory entry is persisted to disk.
warn_fsync_parent_directory(path.path().c_str());
}
bool blocking_read_file(const char *path, std::string *contents_out) {
#ifdef _WIN32
HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_ALWAYS, 0, nullptr);
if (hFile == INVALID_HANDLE_VALUE) return false;
LARGE_INTEGER fileSize;
BOOL res = GetFileSizeEx(hFile, &fileSize);
if (!res) {
CloseHandle(hFile);
return false;
}
DWORD remaining = fileSize.QuadPart;
std::string ret;
ret.resize(remaining);
size_t index = 0;
while (remaining > 0) {
DWORD consumed;
res = ReadFile(hFile, &ret[index], remaining, &consumed, nullptr);
if (!res) {
CloseHandle(hFile);
return false;
}
remaining -= consumed;
index += consumed;
}
CloseHandle(hFile);
*contents_out = std::move(ret);
return true;
#else
scoped_fd_t fd;
{
int res;
do {
res = open(path, O_RDONLY);
} while (res == -1 && get_errno() == EINTR);
if (res == -1) {
return false;
}
fd.reset(res);
}
std::string ret;
char buf[4096];
for (;;) {
ssize_t res;
do {
res = read(fd.get(), buf, sizeof(buf));
} while (res == -1 && get_errno() == EINTR);
if (res == -1) {
return false;
}
if (res == 0) {
*contents_out = std::move(ret);
return true;
}
ret.append(buf, buf + res);
}
#endif
}
std::string blocking_read_file(const char *path) {
std::string ret;
bool success = blocking_read_file(path, &ret);
guarantee(success);
return ret;
}