forked from luigoalma/ctrcdnfetch
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDirectoryManagement.cpp
More file actions
330 lines (238 loc) · 5.64 KB
/
DirectoryManagement.cpp
File metadata and controls
330 lines (238 loc) · 5.64 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#define _DEFAULT_SOURCE
#ifndef _FILE_OFFSET_BITS
#define _FILE_OFFSET_BITS 64
#endif
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <cstring>
#include "DirectoryManagement.hpp"
int Utils::DirectoryManagement::MakeDirectory(const char* path, bool recursive)
{
if (!path) return errno = EFAULT;
struct stat buffer;
memset(&buffer, 0, sizeof(struct stat));
errno = 0;
if (stat(path, &buffer) == 0)
{
if ((buffer.st_mode & S_IFMT) != S_IFDIR) errno = ENOTDIR;
if (!errno) return 0;
}
if (recursive)
{
char* copypath = (char *)calloc(strlen(path) + 1, 1);
if (!copypath) return errno = ENOMEM;
strcpy(copypath, path);
char* workstr = copypath;
std::string progressivepath("");
if (path[0] == '/')
progressivepath += '/';
char* save = NULL;
char* str = workstr;
for (;; str = NULL)
{
char* part = strtok_r(str, "/", &save);
if (!part) break; //we finished.
progressivepath += part;
if (stat(progressivepath.c_str(), &buffer) != 0)
{
if (errno != ENOENT) break;
if (mkdir(progressivepath.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) break;
errno = 0; //success? clear
}
else
{
if ((buffer.st_mode & S_IFMT) != S_IFDIR)
{
errno = ENOTDIR;
break;
}
}
progressivepath += '/';
}
free(copypath);
}
else if (errno == ENOENT)
{
if (mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) errno = 0;
}
return errno;
}
int Utils::DirectoryManagement::RemoveDirectory(const char* path, bool recursive)
{
if (!path) return errno = EFAULT;
int ret = 0;
char* fixedpath = NULL;
if ((ret = FixUpPath(fixedpath, path, false)) != 0) return errno;
struct stat buffer;
if (lstat(fixedpath, &buffer) != 0) return errno;
if ((buffer.st_mode & S_IFMT) != S_IFDIR) return errno = ENOTDIR;
if (!recursive) //just try delete given path, as is
{
errno = 0;
if (rmdir(fixedpath) != 0)
{
ret = errno;
}
free(fixedpath);
return ret;
}
try {
std::vector<RecursiveRmdirHelper> v;
v.push_back(RecursiveRmdirHelper(fixedpath));
free(fixedpath);
fixedpath = nullptr;
while (!v.empty())
{
while (v.back().index < v.back().path.size())
{
if (v.back().path[v.back().index].GetType() == 0) //folder
{
size_t tmp = v.back().index++;
v.push_back(RecursiveRmdirHelper(v.back().path + v.back().path[tmp]));
continue;
}
else unlink((v.back().path + v.back().path[v.back().index]).c_str()); //file or other
v.back().index++;
}
if (rmdir(v.back().path.GetPath())) ret = errno;
v.pop_back();
}
}
catch (std::length_error& e)
{
ret = ENOMEM;
}
catch (std::bad_alloc& e)
{
ret = ENOMEM;
}
catch (...)
{
ret = ECANCELED;
}
free(fixedpath); //only needed if got an exception before the free above on the try block
return errno = ret;
}
int Utils::DirectoryManagement::DirectoryListing(DirFileList& output, const char* path)
{
if (!path) return errno = EFAULT;
DIR* dp;
struct dirent* ep;
struct stat buffer;
memset(&buffer, 0, sizeof(struct stat));
int ret = 0;
if (stat(path, &buffer) != 0) return errno;
if ((buffer.st_mode & S_IFMT) != S_IFDIR) return errno = ENOTDIR;
if ((dp = opendir(path)) == NULL) return errno;
output.Clear();
while ((ep = readdir(dp)))
{
if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0) continue;
DirFileList::entry* dir_entry = new(std::nothrow) DirFileList::entry();
if (!dir_entry)
{
ret = ENOMEM;
break;
}
size_t length = strlen(ep->d_name) + 1;
dir_entry->name = (char *)malloc(length);
if (!dir_entry->name)
{
delete dir_entry;
ret = ENOMEM;
break;
}
strcpy(dir_entry->name, ep->d_name);
try
{
std::string pathcheck(path);
if (pathcheck.back() != '/') pathcheck += '/';
pathcheck += dir_entry->name;
if (lstat(pathcheck.c_str(), &buffer) != 0) break;
}
catch (...)
{
delete dir_entry;
ret = ENOMEM;
break;
}
dir_entry->type = ((buffer.st_mode & S_IFMT) == S_IFDIR ? 0 : ((buffer.st_mode & S_IFMT) == S_IFREG ? 1 : 2));
try
{
output.entries.push_back(dir_entry);
}
catch (...)
{
delete dir_entry;
ret = ENOMEM;
break;
}
}
if (!ret)
{
char* copy_path = NULL;
if (!(ret = FixUpPath(copy_path, path))) output.original_path = copy_path;
}
if (ret) output.Clear();
closedir(dp);
return errno = ret;
}
int Utils::DirectoryManagement::PathIsDirectory(const char* path)
{
if (!path) return errno = EFAULT;
struct stat buffer;
memset(&buffer, 0, sizeof(struct stat));
errno = 0;
if (stat(path, &buffer) != 0) return errno;
return ((buffer.st_mode & S_IFMT) != S_IFDIR) ? errno = ENOTDIR : errno;
}
int Utils::DirectoryManagement::FixUpPath(char*& out, const char* path, bool ending_delimitor)
{
if (!path) return errno = EFAULT;
std::string fixedpath("");
if (path[0] == '/' || path[0] == '\\') fixedpath += '/';
char* copypath = (char *)malloc(strlen(path) + 1);
if (!copypath) return errno = ENOMEM;
strcpy(copypath, path);
int ret = 0;
char* tmp = NULL;
try
{
char* save = NULL;
char* str = copypath;
for (;; str = NULL)
{
char* part = strtok_r(str, "/", &save);
if (!part) break; //we finished.
fixedpath += part;
fixedpath += '/';
}
if (!ending_delimitor) fixedpath.pop_back();
tmp = (char *)malloc(fixedpath.size() + 1);
if (!tmp)
{
free(copypath);
return errno = ENOMEM;
}
fixedpath.copy(tmp, fixedpath.size());
out = tmp;
}
catch (std::length_error& e)
{
ret = ENOMEM;
free(tmp);
}
catch (std::bad_alloc& e)
{
ret = ENOMEM;
free(tmp);
}
catch (...)
{
ret = ECANCELED;
free(tmp);
}
free(copypath);
return errno = ret;
}