forked from ryd/chaosvpn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.c
439 lines (370 loc) · 10.3 KB
/
fs.c
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <dirent.h>
#include "chaosvpn.h"
#define NOERR (0)
/* Note the function definition -- this CANNOT take a const char for the path!
* Returns 0 on success, -1 on error. errno should be set.
* On success, path is returned intact. On failure, path is undefined.
*/
int fs_mkdir_p( char *path, mode_t mode )
{
int err,pos,i;
err = mkdir( path, mode );
if( (err != NOERR) && errno == ENOENT ) {
/* find the last occurance of '/' */
for( i = 0, pos = 0; path[ i ] != '\0'; i++ ) {
if( path[ i ] == '/' )
pos = i;
}
/* return -1 if '/' is not found, or found only at position 1
* ("/" is not a valid mkdir) */
if( pos == 0 )
return -1;
path[ pos ] = '\0';
err = fs_mkdir_p( path, mode );
if( err != NOERR )
return err;
path[ pos ] = '/';
err = mkdir( path, mode );
}
return err;
}
static bool
fs_ensure_suffix(struct string* s)
{
if (string_get(s)[s->length - 1] == '/') return true;
if (!string_putc(s, '/')) return false;
return string_ensurez(s);
}
bool
fs_get_cwd(struct string* s)
{
char stackpath[256];
char* allocpath;
size_t bta;
int retval;
if (getcwd(stackpath, sizeof(stackpath)) != NULL) {
if (!string_concat(s, stackpath)) return false;
if (string_get(s)[s->length - 1] == '/') return true;
return string_putc(s, '/');
}
if (errno != ERANGE) return false;
for (bta = 4096; bta < 65536; bta+=4096) {
allocpath = malloc(bta);
if (allocpath == NULL) { return false; }
if (getcwd(allocpath, bta) != NULL) {
retval = string_concat(s, allocpath);
free(allocpath);
if (!retval) return false;
if (string_get(s)[s->length - 1] == '/') return true;
return string_putc(s, '/');
}
free(allocpath);
if (errno != ERANGE) return false;
}
return true;
}
static bool
fs_cp_file(const char *src, const char *dst)
{
int fh_source;
int fh_destination;
struct stat stat_source;
ssize_t filesize;
ssize_t readbytes;
ssize_t writtenbytes;
char buf[65536];
bool retval = false;
if (stat(src, &stat_source)) return false;
fh_source = open(src, O_RDONLY);
if (fh_source == -1) return false;
fh_destination = open(dst, O_CREAT | O_WRONLY, stat_source.st_mode & 07777);
if (fh_destination == -1) {
goto bail_out_close_source;
}
filesize = stat_source.st_size;
while (filesize > 0) {
readbytes = read(fh_source, &buf, sizeof(buf));
writtenbytes = write(fh_destination, &buf, readbytes);
if (writtenbytes != readbytes) {
goto bail_out_close_dest;
}
filesize -= writtenbytes;
}
retval = true;
bail_out_close_dest:
(void)close(fh_destination);
bail_out_close_source:
(void)close(fh_source);
return retval;
}
static bool
handledir(struct string* src, struct string* dst)
{
DIR* dir;
struct dirent* dirent;
size_t srcdirlen;
size_t dstdirlen;
struct stat st;
struct timeval tv[2];
bool retval = false;
if (!string_ensurez(src)) return false;
if (!string_ensurez(dst)) return false;
//log_debug("fs_cp_r: handledir(%s, %s)", string_get(src), string_get(dst));
if (stat(string_get(src), &st)) {
log_warn("fs_cp_r: stat(%s) failed: %s", string_get(src), strerror(errno));
return false;
}
(void)mkdir(string_get(dst), st.st_mode & 07777);
if (chdir(string_get(src))) {
log_warn("fs_cp_r: chdir(%s) failed: %s", string_get(src), strerror(errno));
return false;
}
dir = opendir(string_get(src));
if (!dir) {
log_warn("fs_cp_r: opendir(%s) failed: %s", string_get(src), strerror(errno));
return false;
}
tv[0].tv_usec = 0;
tv[1].tv_usec = 0;
while ((dirent = readdir(dir))) {
if (stat(dirent->d_name, &st)) continue;
if ((*dirent->d_name == '.') &&
((dirent->d_name[1] == 0) ||
((dirent->d_name[1] == '.') &&
(dirent->d_name[2] == 0)))) continue;
tv[0].tv_sec = st.st_atime;
tv[1].tv_sec = st.st_mtime;
//log_debug("handle %s %s", string_get(src), dirent->d_name);
if (S_ISDIR(st.st_mode)) {
srcdirlen = src->length;
dstdirlen = dst->length;
if (!string_concat(src, dirent->d_name)) goto bail_out;
if (!fs_ensure_suffix(src)) goto bail_out;
if (!string_concat(dst, dirent->d_name)) goto bail_out;
if (!fs_ensure_suffix(dst)) goto bail_out;
if (!handledir(src, dst)) goto bail_out;
#ifndef WIN32
if (utimes(string_get(dst), tv)) {
log_warn("fs_cp_r: warning: utimes failed for %s", string_get(dst));
}
#endif
src->length = srcdirlen;
dst->length = dstdirlen;
(void)string_ensurez(src);
(void)string_ensurez(dst);
if (chdir(string_get(src))) {
log_warn("fs_cp_r: chdir(%s) failed: %s", string_get(src), strerror(errno));
goto bail_out;
}
} else if (S_ISREG(st.st_mode)) {
dstdirlen = dst->length;
if (!string_concat(dst, dirent->d_name)) goto bail_out;
if (!string_ensurez(dst)) goto bail_out;
if (!fs_cp_file(dirent->d_name, string_get(dst))) {
log_warn("fs_cp_r: copy %s to %s failed: %s", dirent->d_name, string_get(dst), strerror(errno));
goto bail_out;
}
#ifndef WIN32
if (utimes(string_get(dst), tv)) {
log_warn("fs_cp_r: warning: utimes failed for %s", string_get(dst));
}
#endif
dst->length = dstdirlen;
}
}
retval = true;
/* fallthrough */
bail_out:
(void)closedir(dir);
return retval;
}
bool
fs_cp_r(char* src, char* dest)
{
struct string source;
struct string destination;
struct string curwd;
bool retval = false;
(void)string_init(&source, 512, 512);
(void)string_init(&destination, 512, 512);
(void)string_init(&curwd, 512, 512);
if (!fs_get_cwd(&curwd)) goto nrcwd_bail_out;
if (*src != '/') if (!fs_get_cwd(&source)) goto bail_out;
if (!string_concat(&source, src)) goto bail_out;
if (!fs_ensure_suffix(&source)) goto bail_out;
if (*dest != '/') if (!fs_get_cwd(&destination)) goto bail_out;
if (!string_concat(&destination, dest)) goto bail_out;
if (!fs_ensure_suffix(&destination)) goto bail_out;
retval = handledir(&source, &destination);
/* fallthrough */
bail_out:
if (!string_ensurez(&curwd)) goto nrcwd_bail_out;
if (chdir(string_get(&curwd))) {
log_err("fs_cp_r: couldn't restore old cwd %s\n", string_get(&curwd));
}
nrcwd_bail_out:
string_free(&source);
string_free(&destination);
string_free(&curwd);
return retval;
}
/* unlinks all regular files in a specified directory */
/* does not use recursion! does not touch symlinks! */
bool
fs_empty_dir(char* dest)
{
struct string dst;
size_t dstdirlen;
struct string curwd;
DIR* dir;
struct dirent* dirent;
struct stat st;
bool retval = false;
(void)string_init(&dst, 512, 512);
(void)string_init(&curwd, 512, 512);
if (!fs_get_cwd(&curwd)) goto nrcwd_bail_out;
if (*dest != '/') if (!fs_get_cwd(&dst)) goto bail_out;
if (!string_concat(&dst, dest)) goto bail_out;
if (!string_ensurez(&dst)) goto bail_out;
if (stat(string_get(&dst), &st)) {
retval = true; /* non-existance is not an error reason! */
goto bail_out;
}
if (!S_ISDIR(st.st_mode)) goto bail_out;
if (!fs_ensure_suffix(&dst)) goto bail_out;
if (chdir(string_get(&dst))) goto bail_out;
dir = opendir(string_get(&dst));
if (!dir) goto bail_out;
while ((dirent = readdir(dir))) {
if (stat(dirent->d_name, &st)) continue;
if ((*dirent->d_name == '.') &&
((dirent->d_name[1] == 0) ||
((dirent->d_name[1] == '.') &&
(dirent->d_name[2] == 0)))) continue;
if (S_ISREG(st.st_mode)) {
dstdirlen = dst.length;
if (!string_concat(&dst, dirent->d_name)) goto bail_out_closedir;
if (unlink(string_get(&dst))) {
log_err("fs_empty_dir: failed to unlink %s: %s\n", string_get(&dst), strerror(errno));
}
dst.length = dstdirlen;
}
}
retval = true;
/* fallthrough */
bail_out_closedir:
(void)closedir(dir);
bail_out:
if (!string_ensurez(&curwd)) goto nrcwd_bail_out;
if (chdir(string_get(&curwd))) {
log_err("fs_empty_dir: couldn't restore old cwd %s\n", string_get(&curwd));
}
nrcwd_bail_out:
string_free(&dst);
string_free(&curwd);
return retval;
}
bool
fs_writecontents(const char *fn,
const char *cnt,
const size_t len,
const int mode)
{
int fh;
size_t bw;
fh = open(fn, O_CREAT | O_WRONLY | O_TRUNC, mode);
if (fh == -1) {
return false;
}
/* ABABAB: should throw proper error here */
bw = write(fh, cnt, len);
(void)close(fh);
if (len != bw) return false;
return true;
}
bool
fs_writecontents_safe(const char *dir,
const char *fn,
const char *cnt,
const int len,
const int mode)
{
char *buf = NULL, *ptr = NULL;
bool res;
unsigned int dlen, reqlen;
dlen = strlen(dir) + 1;
/* ABABAB: code never executed: if(!dlen) return 1; */
reqlen = dlen + strlen(fn) + 1; /* Gives us two extra bytes at end when one is required */
/* ABABAB: code never executed: if(reqlen < dlen) return 1; */
if (NULL == (ptr = malloc((size_t)reqlen))) return 1;
buf = ptr; /* for the write below */
strcpy(ptr, dir);
*(ptr + dlen - 1) = '/';
*(ptr + dlen) = '\0';
strcat(ptr, fn);
for(ptr=buf+dlen;'\0' != *ptr;ptr++) if('/' == *ptr) *ptr='_';
res = fs_writecontents(buf, cnt, len, mode);
free(buf);
return res;
}
/* read file into struct string */
/* note: not NULL terminated! binary clean */
bool
fs_read_file(struct string *buffer, char *fname)
{
bool retval = false;
FILE *fp;
char chunk[4096];
size_t read_size;
fp = fopen(fname, "r");
if (fp==NULL) return false;
while ((read_size = fread(&chunk, 1, sizeof(chunk), fp)) > 0) {
if (!string_concatb(buffer, chunk, read_size)) goto bail_out;
}
if (!ferror(fp))
retval = true;
bail_out:
fclose(fp);
return retval;
}
/* read everything from fd into struct string */
/* note: not NULL terminated! binary clean */
bool
fs_read_fd(struct string *buffer, FILE *fd)
{
bool retval = false;
char chunk[4096];
ssize_t read_size;
while ((read_size = fread(&chunk, 1, sizeof(chunk), fd)) > 0) {
if (!string_concatb(buffer, chunk, read_size)) goto bail_out;
}
retval = true;
bail_out:
return retval;
}
/* execute cmd, return stdout output in struct string *outputbuffer */
bool
fs_backticks_exec(const char *cmd, struct string *outputbuffer)
{
bool retval = false;
FILE *pfd;
pfd = popen(cmd, "r");
if (!pfd) {
log_err("fs_backticks_exec: pipe() failed\n");
goto bail_out;
}
retval = fs_read_fd(outputbuffer, pfd);
fclose(pfd);
bail_out:
return retval;
}