This repository has been archived by the owner on May 4, 2018. It is now read-only.
/
fs.c
652 lines (507 loc) · 14.5 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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "uv.h"
#include "internal.h"
#include "eio.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <utime.h>
#define ARGS1(a) (a)
#define ARGS2(a,b) (a), (b)
#define ARGS3(a,b,c) (a), (b), (c)
#define ARGS4(a,b,c,d) (a), (b), (c), (d)
#define WRAP_EIO(type, eiofunc, func, args) \
uv_fs_req_init(loop, req, type, path, cb); \
if (cb) { \
/* async */ \
uv_ref(loop); \
req->eio = eiofunc(args, EIO_PRI_DEFAULT, uv__fs_after, req); \
if (!req->eio) { \
uv_err_new(loop, ENOMEM); \
return -1; \
} \
} else { \
/* sync */ \
req->result = func(args); \
if (req->result) { \
uv_err_new(loop, errno); \
return -1; \
} \
} \
return 0;
static void uv_fs_req_init(uv_loop_t* loop, uv_fs_t* req, uv_fs_type fs_type,
char* path, uv_fs_cb cb) {
/* Make sure the thread pool is initialized. */
uv_eio_init(loop);
uv__req_init((uv_req_t*) req);
req->type = UV_FS;
req->loop = loop;
req->fs_type = fs_type;
req->cb = cb;
req->result = 0;
req->ptr = NULL;
req->path = path ? strdup(path) : NULL;
req->errorno = 0;
req->eio = NULL;
}
void uv_fs_req_cleanup(uv_fs_t* req) {
free(req->path);
req->path = NULL;
switch (req->fs_type) {
case UV_FS_READDIR:
assert(req->ptr);
free(req->ptr);
req->ptr = NULL;
break;
case UV_FS_STAT:
case UV_FS_LSTAT:
req->ptr = NULL;
break;
default:
break;
}
}
static int uv__fs_after(eio_req* eio) {
char* name;
int namelen;
int buflen = 0;
uv_fs_t* req = eio->data;
int i;
assert(req->cb);
req->result = req->eio->result;
req->errorno = req->eio->errorno;
switch (req->fs_type) {
case UV_FS_READDIR:
/*
* XXX This is pretty bad.
* We alloc and copy the large null terminated string list from libeio.
* This is done because libeio is going to free eio->ptr2 after this
* callback. We must keep it until uv_fs_req_cleanup. If we get rid of
* libeio this can be avoided.
*/
buflen = 0;
name = req->eio->ptr2;
for (i = 0; i < req->result; i++) {
namelen = strlen(name);
buflen += namelen + 1;
/* TODO check ENOMEM */
name += namelen;
assert(*name == '\0');
name++;
}
req->ptr = malloc(buflen);
memcpy(req->ptr, req->eio->ptr2, buflen);
break;
case UV_FS_STAT:
case UV_FS_LSTAT:
case UV_FS_FSTAT:
req->ptr = req->eio->ptr2;
break;
case UV_FS_READLINK:
if (req->result == -1) {
req->ptr = NULL;
} else {
assert(req->result > 0);
if ((name = realloc(req->eio->ptr2, req->result + 1)) == NULL) {
/* Not enough memory. Reuse buffer, chop off last byte. */
name = req->eio->ptr2;
req->result--;
}
name[req->result] = '\0';
req->ptr = name;
req->result = 0;
}
break;
default:
break;
}
uv_unref(req->loop);
req->eio = NULL; /* Freed by libeio */
req->cb(req);
return 0;
}
int uv_fs_close(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
char* path = NULL;
WRAP_EIO(UV_FS_CLOSE, eio_close, close, ARGS1(file));
}
int uv_fs_open(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags,
int mode, uv_fs_cb cb) {
uv_fs_req_init(loop, req, UV_FS_OPEN, path, cb);
if (cb) {
/* async */
uv_ref(loop);
req->eio = eio_open(path, flags, mode, EIO_PRI_DEFAULT, uv__fs_after, req);
if (!req->eio) {
uv_err_new(loop, ENOMEM);
return -1;
}
} else {
/* sync */
req->result = open(path, flags, mode);
if (req->result < 0) {
uv_err_new(loop, errno);
return -1;
}
uv__cloexec(req->result, 1);
}
return 0;
}
int uv_fs_read(uv_loop_t* loop, uv_fs_t* req, uv_file fd, void* buf,
size_t length, off_t offset, uv_fs_cb cb) {
uv_fs_req_init(loop, req, UV_FS_READ, NULL, cb);
if (cb) {
/* async */
uv_ref(loop);
req->eio = eio_read(fd, buf, length, offset, EIO_PRI_DEFAULT,
uv__fs_after, req);
if (!req->eio) {
uv_err_new(loop, ENOMEM);
return -1;
}
} else {
/* sync */
req->result = offset < 0 ?
read(fd, buf, length) :
pread(fd, buf, length, offset);
if (req->result < 0) {
uv_err_new(loop, errno);
return -1;
}
}
return 0;
}
int uv_fs_unlink(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
WRAP_EIO(UV_FS_UNLINK, eio_unlink, unlink, ARGS1(path))
}
int uv_fs_write(uv_loop_t* loop, uv_fs_t* req, uv_file file, void* buf,
size_t length, off_t offset, uv_fs_cb cb) {
uv_fs_req_init(loop, req, UV_FS_WRITE, NULL, cb);
if (cb) {
/* async */
uv_ref(loop);
req->eio = eio_write(file, buf, length, offset, EIO_PRI_DEFAULT,
uv__fs_after, req);
if (!req->eio) {
uv_err_new(loop, ENOMEM);
return -1;
}
} else {
/* sync */
req->result = offset < 0 ?
write(file, buf, length) :
pwrite(file, buf, length, offset);
if (req->result < 0) {
uv_err_new(loop, errno);
return -1;
}
}
return 0;
}
int uv_fs_mkdir(uv_loop_t* loop, uv_fs_t* req, const char* path, int mode,
uv_fs_cb cb) {
WRAP_EIO(UV_FS_MKDIR, eio_mkdir, mkdir, ARGS2(path, mode))
}
int uv_fs_rmdir(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
WRAP_EIO(UV_FS_RMDIR, eio_rmdir, rmdir, ARGS1(path))
}
int uv_fs_readdir(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags,
uv_fs_cb cb) {
int r;
struct dirent* entry;
size_t size = 0;
size_t d_namlen = 0;
uv_fs_req_init(loop, req, UV_FS_READDIR, path, cb);
if (cb) {
/* async */
uv_ref(loop);
req->eio = eio_readdir(path, flags, EIO_PRI_DEFAULT, uv__fs_after, req);
if (!req->eio) {
uv_err_new(loop, ENOMEM);
return -1;
}
} else {
/* sync */
DIR* dir = opendir(path);
if (!dir) {
uv_err_new(loop, errno);
req->result = -1;
return -1;
}
/* req->result stores number of entries */
req->result = 0;
while ((entry = readdir(dir))) {
d_namlen = strlen(entry->d_name);
/* Skip . and .. */
if ((d_namlen == 1 && entry->d_name[0] == '.') ||
(d_namlen == 2 && entry->d_name[0] == '.' &&
entry->d_name[1] == '.')) {
continue;
}
req->ptr = realloc(req->ptr, size + d_namlen + 1);
/* TODO check ENOMEM */
memcpy((char*)req->ptr + size, entry->d_name, d_namlen);
size += d_namlen;
((char*)req->ptr)[size] = '\0';
size++;
req->result++;
}
r = closedir(dir);
if (r) {
uv_err_new(loop, errno);
req->result = -1;
return -1;
}
}
return 0;
}
int uv_fs_stat(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
char* pathdup = path;
int pathlen;
uv_fs_req_init(loop, req, UV_FS_STAT, path, cb);
/* TODO do this without duplicating the string. */
/* TODO security */
pathdup = strdup(path);
pathlen = strlen(path);
if (pathlen > 0 && path[pathlen - 1] == '\\') {
/* TODO do not modify input string */
pathdup[pathlen - 1] = '\0';
}
if (cb) {
/* async */
uv_ref(loop);
req->eio = eio_stat(pathdup, EIO_PRI_DEFAULT, uv__fs_after, req);
free(pathdup);
if (!req->eio) {
uv_err_new(loop, ENOMEM);
return -1;
}
} else {
/* sync */
req->result = stat(pathdup, &req->statbuf);
free(pathdup);
if (req->result < 0) {
uv_err_new(loop, errno);
return -1;
}
req->ptr = &req->statbuf;
}
return 0;
}
int uv_fs_fstat(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
uv_fs_req_init(loop, req, UV_FS_FSTAT, NULL, cb);
if (cb) {
/* async */
uv_ref(loop);
req->eio = eio_fstat(file, EIO_PRI_DEFAULT, uv__fs_after, req);
if (!req->eio) {
uv_err_new(loop, ENOMEM);
return -1;
}
} else {
/* sync */
req->result = fstat(file, &req->statbuf);
if (req->result < 0) {
uv_err_new(loop, errno);
return -1;
}
req->ptr = &req->statbuf;
}
return 0;
}
int uv_fs_rename(uv_loop_t* loop, uv_fs_t* req, const char* path, const char* new_path,
uv_fs_cb cb) {
WRAP_EIO(UV_FS_RENAME, eio_rename, rename, ARGS2(path, new_path))
}
int uv_fs_fsync(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
char* path = NULL;
WRAP_EIO(UV_FS_FSYNC, eio_fsync, fsync, ARGS1(file))
}
int uv_fs_fdatasync(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
char* path = NULL;
WRAP_EIO(UV_FS_FDATASYNC, eio_fdatasync, fdatasync, ARGS1(file))
}
int uv_fs_ftruncate(uv_loop_t* loop, uv_fs_t* req, uv_file file, off_t offset,
uv_fs_cb cb) {
char* path = NULL;
WRAP_EIO(UV_FS_FTRUNCATE, eio_ftruncate, ftruncate, ARGS2(file, offset))
}
int uv_fs_sendfile(uv_loop_t* loop, uv_fs_t* req, uv_file out_fd, uv_file in_fd,
off_t in_offset, size_t length, uv_fs_cb cb) {
char* path = NULL;
WRAP_EIO(UV_FS_SENDFILE, eio_sendfile, eio_sendfile_sync,
ARGS4(out_fd, in_fd, in_offset, length))
}
int uv_fs_chmod(uv_loop_t* loop, uv_fs_t* req, const char* path, int mode,
uv_fs_cb cb) {
WRAP_EIO(UV_FS_CHMOD, eio_chmod, chmod, ARGS2(path, mode))
}
static int _utime(const char* path, double atime, double mtime) {
struct utimbuf buf;
buf.actime = atime;
buf.modtime = mtime;
return utime(path, &buf);
}
int uv_fs_utime(uv_loop_t* loop, uv_fs_t* req, const char* path, double atime,
double mtime, uv_fs_cb cb) {
WRAP_EIO(UV_FS_UTIME, eio_utime, _utime, ARGS3(path, atime, mtime))
}
int uv_fs_futime(uv_loop_t* loop, uv_fs_t* req, uv_file file, double atime,
double mtime, uv_fs_cb cb) {
assert(0 && "implement me");
return -1;
}
int uv_fs_lstat(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
char* pathdup = path;
int pathlen;
uv_fs_req_init(loop, req, UV_FS_LSTAT, path, cb);
/* TODO do this without duplicating the string. */
/* TODO security */
pathdup = strdup(path);
pathlen = strlen(path);
if (pathlen > 0 && path[pathlen - 1] == '\\') {
/* TODO do not modify input string */
pathdup[pathlen - 1] = '\0';
}
if (cb) {
/* async */
uv_ref(loop);
req->eio = eio_lstat(pathdup, EIO_PRI_DEFAULT, uv__fs_after, req);
free(pathdup);
if (!req->eio) {
uv_err_new(loop, ENOMEM);
return -1;
}
} else {
/* sync */
req->result = lstat(pathdup, &req->statbuf);
free(pathdup);
if (req->result < 0) {
uv_err_new(loop, errno);
return -1;
}
req->ptr = &req->statbuf;
}
return 0;
}
int uv_fs_link(uv_loop_t* loop, uv_fs_t* req, const char* path,
const char* new_path, uv_fs_cb cb) {
WRAP_EIO(UV_FS_LINK, eio_link, link, ARGS2(path, new_path))
}
int uv_fs_symlink(uv_loop_t* loop, uv_fs_t* req, const char* path,
const char* new_path, int flags, uv_fs_cb cb) {
WRAP_EIO(UV_FS_SYMLINK, eio_symlink, symlink, ARGS2(path, new_path))
}
int uv_fs_readlink(uv_loop_t* loop, uv_fs_t* req, const char* path,
uv_fs_cb cb) {
size_t size;
int status;
char* buf;
status = -1;
uv_fs_req_init(loop, req, UV_FS_READLINK, path, cb);
if (cb) {
if ((req->eio = eio_readlink(path, EIO_PRI_DEFAULT, uv__fs_after, req))) {
uv_ref(loop);
return 0;
} else {
uv_err_new(loop, ENOMEM);
return -1;
}
} else {
/* pathconf(_PC_PATH_MAX) may return -1 to signify that path
* lengths have no upper limit or aren't suitable for malloc'ing.
*/
if ((size = pathconf(path, _PC_PATH_MAX)) == -1) {
#if defined(PATH_MAX)
size = PATH_MAX;
#else
size = 4096;
#endif
}
if ((buf = malloc(size + 1)) == NULL) {
uv_err_new(loop, ENOMEM);
return -1;
}
if ((size = readlink(path, buf, size)) == -1) {
req->errorno = errno;
req->result = -1;
free(buf);
} else {
/* Cannot conceivably fail since it shrinks the buffer. */
buf = realloc(buf, size + 1);
buf[size] = '\0';
req->result = 0;
req->ptr = buf;
}
return 0;
}
assert(0 && "unreachable");
}
int uv_fs_fchmod(uv_loop_t* loop, uv_fs_t* req, uv_file file, int mode,
uv_fs_cb cb) {
char* path = NULL;
WRAP_EIO(UV_FS_FCHMOD, eio_fchmod, fchmod, ARGS2(file, mode))
}
int uv_fs_chown(uv_loop_t* loop, uv_fs_t* req, const char* path, int uid,
int gid, uv_fs_cb cb) {
WRAP_EIO(UV_FS_CHOWN, eio_chown, chown, ARGS3(path, uid, gid))
}
int uv_fs_fchown(uv_loop_t* loop, uv_fs_t* req, uv_file file, int uid, int gid,
uv_fs_cb cb) {
char* path = NULL;
WRAP_EIO(UV_FS_FCHOWN, eio_fchown, fchown, ARGS3(file, uid, gid))
}
static void uv__work(eio_req* eio) {
uv_work_t* req = eio->data;
if (req->work_cb) {
req->work_cb(req);
}
}
static int uv__after_work(eio_req *eio) {
uv_work_t* req = eio->data;
uv_unref(req->loop);
if (req->after_work_cb) {
req->after_work_cb(req);
}
return 0;
}
int uv_queue_work(uv_loop_t* loop, uv_work_t* req, uv_work_cb work_cb,
uv_after_work_cb after_work_cb) {
void* data = req->data;
uv_eio_init(loop);
uv__req_init((uv_req_t*) req);
uv_ref(loop);
req->loop = loop;
req->data = data;
req->work_cb = work_cb;
req->after_work_cb = after_work_cb;
req->eio = eio_custom(uv__work, EIO_PRI_DEFAULT, uv__after_work, req);
if (!req->eio) {
uv_err_new(loop, ENOMEM);
return -1;
}
return 0;
}