forked from ares-emulator/ares
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirectory.hpp
282 lines (252 loc) · 9.57 KB
/
directory.hpp
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#pragma once
#include <nall/file.hpp>
#include <nall/function.hpp>
#include <nall/inode.hpp>
#include <nall/intrinsics.hpp>
#include <nall/merge-sort.hpp>
#include <nall/string.hpp>
#include <nall/vector.hpp>
#if defined(PLATFORM_WINDOWS)
#include <nall/windows/utf8.hpp>
#else
#include <dirent.h>
#include <stdio.h>
#include <sys/types.h>
#endif
namespace nall {
struct directory : inode {
directory() = delete;
static auto copy(const string& source, const string& target) -> bool; //recursive
static auto create(const string& pathname, u32 permissions = 0755) -> bool; //recursive
static auto remove(const string& pathname) -> bool; //recursive
static auto exists(const string& pathname) -> bool;
static auto folders(const string& pathname, const string& pattern = "*") -> vector<string> {
auto folders = directory::ufolders(pathname, pattern);
folders.sort();
for(auto& folder : folders) folder.append("/"); //must append after sorting
return folders;
}
static auto files(const string& pathname, const string& pattern = "*") -> vector<string> {
auto files = directory::ufiles(pathname, pattern);
files.sort();
return files;
}
static auto contents(const string& pathname, const string& pattern = "*") -> vector<string> {
auto folders = directory::ufolders(pathname); //pattern search of contents should only filter files
folders.sort();
for(auto& folder : folders) folder.append("/"); //must append after sorting
auto files = directory::ufiles(pathname, pattern);
files.sort();
for(auto& file : files) folders.append(file);
return folders;
}
static auto ifolders(const string& pathname, const string& pattern = "*") -> vector<string> {
auto folders = ufolders(pathname, pattern);
folders.isort();
for(auto& folder : folders) folder.append("/"); //must append after sorting
return folders;
}
static auto ifiles(const string& pathname, const string& pattern = "*") -> vector<string> {
auto files = ufiles(pathname, pattern);
files.isort();
return files;
}
static auto icontents(const string& pathname, const string& pattern = "*") -> vector<string> {
auto folders = directory::ufolders(pathname); //pattern search of contents should only filter files
folders.isort();
for(auto& folder : folders) folder.append("/"); //must append after sorting
auto files = directory::ufiles(pathname, pattern);
files.isort();
for(auto& file : files) folders.append(file);
return folders;
}
static auto rcontents(const string& pathname, const string& pattern = "*") -> vector<string> {
vector<string> contents;
function<void (const string&, const string&, const string&)>
recurse = [&](const string& basename, const string& pathname, const string& pattern) {
for(auto& folder : directory::ufolders(pathname)) {
contents.append(string{pathname, folder, "/"}.trimLeft(basename, 1L));
recurse(basename, {pathname, folder, "/"}, pattern);
}
for(auto& file : directory::ufiles(pathname, pattern)) {
contents.append(string{pathname, file}.trimLeft(basename, 1L));
}
};
for(auto& folder : directory::ufolders(pathname)) {
contents.append({folder, "/"});
recurse(pathname, {pathname, folder, "/"}, pattern);
}
for(auto& file : directory::ufiles(pathname, pattern)) {
contents.append(file);
}
contents.sort();
return contents;
}
static auto ircontents(const string& pathname, const string& pattern = "*") -> vector<string> {
vector<string> contents;
function<void (const string&, const string&, const string&)>
recurse = [&](const string& basename, const string& pathname, const string& pattern) {
for(auto& folder : directory::ufolders(pathname)) {
contents.append(string{pathname, folder, "/"}.trimLeft(basename, 1L));
recurse(basename, {pathname, folder, "/"}, pattern);
}
for(auto& file : directory::ufiles(pathname, pattern)) {
contents.append(string{pathname, file}.trimLeft(basename, 1L));
}
};
for(auto& folder : directory::ufolders(pathname)) {
contents.append({folder, "/"});
recurse(pathname, {pathname, folder, "/"}, pattern);
}
for(auto& file : directory::ufiles(pathname, pattern)) {
contents.append(file);
}
contents.isort();
return contents;
}
static auto rfolders(const string& pathname, const string& pattern = "*") -> vector<string> {
vector<string> folders;
for(auto& folder : rcontents(pathname, pattern)) {
if(directory::exists({pathname, folder})) folders.append(folder);
}
return folders;
}
static auto irfolders(const string& pathname, const string& pattern = "*") -> vector<string> {
vector<string> folders;
for(auto& folder : ircontents(pathname, pattern)) {
if(directory::exists({pathname, folder})) folders.append(folder);
}
return folders;
}
static auto rfiles(const string& pathname, const string& pattern = "*") -> vector<string> {
vector<string> files;
for(auto& file : rcontents(pathname, pattern)) {
if(file::exists({pathname, file})) files.append(file);
}
return files;
}
static auto irfiles(const string& pathname, const string& pattern = "*") -> vector<string> {
vector<string> files;
for(auto& file : ircontents(pathname, pattern)) {
if(file::exists({pathname, file})) files.append(file);
}
return files;
}
private:
//internal functions; these return unsorted lists
static auto ufolders(const string& pathname, const string& pattern = "*") -> vector<string>;
static auto ufiles(const string& pathname, const string& pattern = "*") -> vector<string>;
};
inline auto directory::copy(const string& source, const string& target) -> bool {
bool result = true;
if(!directory::exists(source)) return result = false;
if(!directory::create(target)) return result = false;
for(auto& name : directory::folders(source)) {
if(!directory::copy({source, name}, {target, name})) result = false;
}
for(auto& name : directory::files(source)) {
if(!file::copy({source, name}, {target, name})) result = false;
}
return result;
}
#if defined(PLATFORM_WINDOWS)
inline auto directory::create(const string& pathname, u32 permissions) -> bool {
string path;
auto list = string{pathname}.transform("\\", "/").trimRight("/").split("/");
bool result = true;
for(auto& part : list) {
path.append(part, "/");
if(directory::exists(path)) continue;
result &= (_wmkdir(utf16_t(path)) == 0);
}
return result;
}
inline auto directory::remove(const string& pathname) -> bool {
if(!pathname || pathname == "/" || pathname.match("?:") || pathname.match("?:/")) return false; //safeguard
string separator = pathname.endsWith("/") || pathname.endsWith("\\") ? "" : "/";
auto list = directory::contents(pathname);
for(auto& name : list) {
if(name.endsWith("/")) directory::remove({pathname, separator, name});
else file::remove({pathname, separator, name});
}
return _wrmdir(utf16_t(pathname)) == 0;
}
#else
inline auto directoryIsFolder(DIR* dp, struct dirent* ep) -> bool {
if(ep->d_type == DT_DIR) return true;
if(ep->d_type == DT_LNK || ep->d_type == DT_UNKNOWN) {
//symbolic links must be resolved to determine type
struct stat sp = {0};
fstatat(dirfd(dp), ep->d_name, &sp, 0);
return S_ISDIR(sp.st_mode);
}
return false;
}
inline auto directory::create(const string& pathname, u32 permissions) -> bool {
string path;
auto list = string{pathname}.trimRight("/").split("/");
bool result = true;
for(auto& part : list) {
path.append(part, "/");
if(directory::exists(path)) continue;
result &= (mkdir(path, permissions) == 0);
}
return result;
}
inline auto directory::remove(const string& pathname) -> bool {
if(!pathname || pathname == "/") return false; //safeguard
string separator = pathname.endsWith("/") ? "" : "/";
auto list = directory::contents(pathname);
for(auto& name : list) {
if(name.endsWith("/")) directory::remove({pathname, separator, name});
else file::remove({pathname, separator, name});
}
return rmdir(pathname) == 0;
}
inline auto directory::exists(const string& pathname) -> bool {
if(!pathname) return false;
struct stat data;
if(stat(pathname, &data) != 0) return false;
return S_ISDIR(data.st_mode);
}
inline auto directory::ufolders(const string& pathname, const string& pattern) -> vector<string> {
if(!pathname) return vector<string>{"/"};
vector<string> list;
DIR* dp;
struct dirent* ep;
dp = opendir(pathname);
if(dp) {
while(ep = readdir(dp)) {
if(!strcmp(ep->d_name, ".")) continue;
if(!strcmp(ep->d_name, "..")) continue;
if(!directoryIsFolder(dp, ep)) continue;
string name{ep->d_name};
if(name.match(pattern)) list.append(std::move(name));
}
closedir(dp);
}
return list;
}
inline auto directory::ufiles(const string& pathname, const string& pattern) -> vector<string> {
if(!pathname) return {};
vector<string> list;
DIR* dp;
struct dirent* ep;
dp = opendir(pathname);
if(dp) {
while(ep = readdir(dp)) {
if(!strcmp(ep->d_name, ".")) continue;
if(!strcmp(ep->d_name, "..")) continue;
if(directoryIsFolder(dp, ep)) continue;
string name{ep->d_name};
if(name.match(pattern)) list.append(std::move(name));
}
closedir(dp);
}
return list;
}
#endif
}
#if defined(NALL_HEADER_ONLY)
#include <nall/directory.cpp>
#endif