1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 * IN THE SOFTWARE.
20 */
21
22 /* Caveat emptor: this file deviates from the libuv convention of returning
23 * negated errno codes. Most uv_fs_*() functions map directly to the system
24 * call of the same name. For more complex wrappers, it's easier to just
25 * return -1 with errno set. The dispatcher in uv__fs_work() takes care of
26 * getting the errno to the right place (req->result or as the return value.)
27 */
28
29 #include "uv.h"
30 #include "internal.h"
31
32 #include <errno.h>
33 #include <dlfcn.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <limits.h> /* PATH_MAX */
38
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <sys/stat.h>
42 #include <sys/time.h>
43 #include <sys/uio.h>
44 #include <pthread.h>
45 #include <unistd.h>
46 #include <fcntl.h>
47 #include <poll.h>
48
49 #if defined(__DragonFly__) || \
50 defined(__FreeBSD__) || \
51 defined(__FreeBSD_kernel__) || \
52 defined(__OpenBSD__) || \
53 defined(__NetBSD__)
54 # define HAVE_PREADV 1
55 #else
56 # define HAVE_PREADV 0
57 #endif
58
59 #if defined(__linux__)
60 # include "sys/utsname.h"
61 #endif
62
63 #if defined(__linux__) || defined(__sun)
64 # include <sys/sendfile.h>
65 # include <sys/sysmacros.h>
66 #endif
67
68 #if defined(__APPLE__)
69 # include <sys/sysctl.h>
70 #elif defined(__linux__) && !defined(FICLONE)
71 # include <sys/ioctl.h>
72 # define FICLONE _IOW(0x94, 9, int)
73 #endif
74
75 #if defined(_AIX) && !defined(_AIX71)
76 # include <utime.h>
77 #endif
78
79 #if defined(__APPLE__) || \
80 defined(__DragonFly__) || \
81 defined(__FreeBSD__) || \
82 defined(__FreeBSD_kernel__) || \
83 defined(__OpenBSD__) || \
84 defined(__NetBSD__)
85 # include <sys/param.h>
86 # include <sys/mount.h>
87 #elif defined(__sun) || \
88 defined(__MVS__) || \
89 defined(__NetBSD__) || \
90 defined(__HAIKU__) || \
91 defined(__QNX__)
92 # include <sys/statvfs.h>
93 #else
94 # include <sys/statfs.h>
95 #endif
96
97 #if defined(_AIX) && _XOPEN_SOURCE <= 600
98 extern char *mkdtemp(char *template); /* See issue #740 on AIX < 7 */
99 #endif
100
101 #define INIT(subtype) \
102 do { \
103 if (req == NULL) \
104 return UV_EINVAL; \
105 UV_REQ_INIT(req, UV_FS); \
106 req->fs_type = UV_FS_ ## subtype; \
107 req->result = 0; \
108 req->ptr = NULL; \
109 req->loop = loop; \
110 req->path = NULL; \
111 req->new_path = NULL; \
112 req->bufs = NULL; \
113 req->cb = cb; \
114 } \
115 while (0)
116
117 #define PATH \
118 do { \
119 assert(path != NULL); \
120 if (cb == NULL) { \
121 req->path = path; \
122 } else { \
123 req->path = uv__strdup(path); \
124 if (req->path == NULL) \
125 return UV_ENOMEM; \
126 } \
127 } \
128 while (0)
129
130 #define PATH2 \
131 do { \
132 if (cb == NULL) { \
133 req->path = path; \
134 req->new_path = new_path; \
135 } else { \
136 size_t path_len; \
137 size_t new_path_len; \
138 path_len = strlen(path) + 1; \
139 new_path_len = strlen(new_path) + 1; \
140 req->path = uv__malloc(path_len + new_path_len); \
141 if (req->path == NULL) \
142 return UV_ENOMEM; \
143 req->new_path = req->path + path_len; \
144 memcpy((void*) req->path, path, path_len); \
145 memcpy((void*) req->new_path, new_path, new_path_len); \
146 } \
147 } \
148 while (0)
149
150 #ifdef USE_FFRT
151 #define POST \
152 do { \
153 if (cb != NULL) { \
154 uv__req_register(loop, req); \
155 uv__work_submit(loop, \
156 (uv_req_t*)req, \
157 &req->work_req, \
158 UV__WORK_FAST_IO, \
159 uv__fs_work, \
160 uv__fs_done); \
161 return 0; \
162 } \
163 else { \
164 uv__fs_work(&req->work_req); \
165 return req->result; \
166 } \
167 } \
168 while (0)
169 #else
170 #define POST \
171 do { \
172 if (cb != NULL) { \
173 uv__req_register(loop, req); \
174 uv__work_submit(loop, \
175 &req->work_req, \
176 UV__WORK_FAST_IO, \
177 uv__fs_work, \
178 uv__fs_done); \
179 return 0; \
180 } \
181 else { \
182 uv__fs_work(&req->work_req); \
183 return req->result; \
184 } \
185 } \
186 while (0)
187 #endif
188
189
uv__fs_close(int fd)190 static int uv__fs_close(int fd) {
191 int rc;
192
193 rc = uv__close_nocancel(fd);
194 if (rc == -1)
195 if (errno == EINTR || errno == EINPROGRESS)
196 rc = 0; /* The close is in progress, not an error. */
197
198 return rc;
199 }
200
201
uv__fs_fsync(uv_fs_t * req)202 static ssize_t uv__fs_fsync(uv_fs_t* req) {
203 #if defined(__APPLE__)
204 /* Apple's fdatasync and fsync explicitly do NOT flush the drive write cache
205 * to the drive platters. This is in contrast to Linux's fdatasync and fsync
206 * which do, according to recent man pages. F_FULLFSYNC is Apple's equivalent
207 * for flushing buffered data to permanent storage. If F_FULLFSYNC is not
208 * supported by the file system we fall back to F_BARRIERFSYNC or fsync().
209 * This is the same approach taken by sqlite, except sqlite does not issue
210 * an F_BARRIERFSYNC call.
211 */
212 int r;
213
214 r = fcntl(req->file, F_FULLFSYNC);
215 if (r != 0)
216 r = fcntl(req->file, 85 /* F_BARRIERFSYNC */); /* fsync + barrier */
217 if (r != 0)
218 r = fsync(req->file);
219 return r;
220 #else
221 return fsync(req->file);
222 #endif
223 }
224
225
uv__fs_fdatasync(uv_fs_t * req)226 static ssize_t uv__fs_fdatasync(uv_fs_t* req) {
227 #if defined(__linux__) || defined(__sun) || defined(__NetBSD__)
228 return fdatasync(req->file);
229 #elif defined(__APPLE__)
230 /* See the comment in uv__fs_fsync. */
231 return uv__fs_fsync(req);
232 #else
233 return fsync(req->file);
234 #endif
235 }
236
237
UV_UNUSED(static struct timespec uv__fs_to_timespec (double time))238 UV_UNUSED(static struct timespec uv__fs_to_timespec(double time)) {
239 struct timespec ts;
240 ts.tv_sec = time;
241 ts.tv_nsec = (time - ts.tv_sec) * 1e9;
242
243 /* TODO(bnoordhuis) Remove this. utimesat() has nanosecond resolution but we
244 * stick to microsecond resolution for the sake of consistency with other
245 * platforms. I'm the original author of this compatibility hack but I'm
246 * less convinced it's useful nowadays.
247 */
248 ts.tv_nsec -= ts.tv_nsec % 1000;
249
250 if (ts.tv_nsec < 0) {
251 ts.tv_nsec += 1e9;
252 ts.tv_sec -= 1;
253 }
254 return ts;
255 }
256
UV_UNUSED(static struct timeval uv__fs_to_timeval (double time))257 UV_UNUSED(static struct timeval uv__fs_to_timeval(double time)) {
258 struct timeval tv;
259 tv.tv_sec = time;
260 tv.tv_usec = (time - tv.tv_sec) * 1e6;
261 if (tv.tv_usec < 0) {
262 tv.tv_usec += 1e6;
263 tv.tv_sec -= 1;
264 }
265 return tv;
266 }
267
uv__fs_futime(uv_fs_t * req)268 static ssize_t uv__fs_futime(uv_fs_t* req) {
269 #if defined(__linux__) \
270 || defined(_AIX71) \
271 || defined(__HAIKU__) \
272 || defined(__GNU__)
273 struct timespec ts[2];
274 ts[0] = uv__fs_to_timespec(req->atime);
275 ts[1] = uv__fs_to_timespec(req->mtime);
276 return futimens(req->file, ts);
277 #elif defined(__APPLE__) \
278 || defined(__DragonFly__) \
279 || defined(__FreeBSD__) \
280 || defined(__FreeBSD_kernel__) \
281 || defined(__NetBSD__) \
282 || defined(__OpenBSD__) \
283 || defined(__sun)
284 struct timeval tv[2];
285 tv[0] = uv__fs_to_timeval(req->atime);
286 tv[1] = uv__fs_to_timeval(req->mtime);
287 # if defined(__sun)
288 return futimesat(req->file, NULL, tv);
289 # else
290 return futimes(req->file, tv);
291 # endif
292 #elif defined(__MVS__)
293 attrib_t atr;
294 memset(&atr, 0, sizeof(atr));
295 atr.att_mtimechg = 1;
296 atr.att_atimechg = 1;
297 atr.att_mtime = req->mtime;
298 atr.att_atime = req->atime;
299 return __fchattr(req->file, &atr, sizeof(atr));
300 #else
301 errno = ENOSYS;
302 return -1;
303 #endif
304 }
305
306
uv__fs_mkdtemp(uv_fs_t * req)307 static ssize_t uv__fs_mkdtemp(uv_fs_t* req) {
308 return mkdtemp((char*) req->path) ? 0 : -1;
309 }
310
311
312 static int (*uv__mkostemp)(char*, int);
313
314
uv__mkostemp_initonce(void)315 static void uv__mkostemp_initonce(void) {
316 /* z/os doesn't have RTLD_DEFAULT but that's okay
317 * because it doesn't have mkostemp(O_CLOEXEC) either.
318 */
319 #ifdef RTLD_DEFAULT
320 uv__mkostemp = (int (*)(char*, int)) dlsym(RTLD_DEFAULT, "mkostemp");
321
322 /* We don't care about errors, but we do want to clean them up.
323 * If there has been no error, then dlerror() will just return
324 * NULL.
325 */
326 dlerror();
327 #endif /* RTLD_DEFAULT */
328 }
329
330
uv__fs_mkstemp(uv_fs_t * req)331 static int uv__fs_mkstemp(uv_fs_t* req) {
332 static uv_once_t once = UV_ONCE_INIT;
333 int r;
334 #ifdef O_CLOEXEC
335 static int no_cloexec_support;
336 #endif
337 static const char pattern[] = "XXXXXX";
338 static const size_t pattern_size = sizeof(pattern) - 1;
339 char* path;
340 size_t path_length;
341
342 path = (char*) req->path;
343 path_length = strlen(path);
344
345 /* EINVAL can be returned for 2 reasons:
346 1. The template's last 6 characters were not XXXXXX
347 2. open() didn't support O_CLOEXEC
348 We want to avoid going to the fallback path in case
349 of 1, so it's manually checked before. */
350 if (path_length < pattern_size ||
351 strcmp(path + path_length - pattern_size, pattern)) {
352 errno = EINVAL;
353 r = -1;
354 goto clobber;
355 }
356
357 uv_once(&once, uv__mkostemp_initonce);
358
359 #ifdef O_CLOEXEC
360 if (uv__load_relaxed(&no_cloexec_support) == 0 && uv__mkostemp != NULL) {
361 r = uv__mkostemp(path, O_CLOEXEC);
362
363 if (r >= 0)
364 return r;
365
366 /* If mkostemp() returns EINVAL, it means the kernel doesn't
367 support O_CLOEXEC, so we just fallback to mkstemp() below. */
368 if (errno != EINVAL)
369 goto clobber;
370
371 /* We set the static variable so that next calls don't even
372 try to use mkostemp. */
373 uv__store_relaxed(&no_cloexec_support, 1);
374 }
375 #endif /* O_CLOEXEC */
376
377 if (req->cb != NULL)
378 uv_rwlock_rdlock(&req->loop->cloexec_lock);
379
380 r = mkstemp(path);
381
382 /* In case of failure `uv__cloexec` will leave error in `errno`,
383 * so it is enough to just set `r` to `-1`.
384 */
385 if (r >= 0 && uv__cloexec(r, 1) != 0) {
386 r = uv__close(r);
387 if (r != 0)
388 abort();
389 r = -1;
390 }
391
392 if (req->cb != NULL)
393 uv_rwlock_rdunlock(&req->loop->cloexec_lock);
394
395 clobber:
396 if (r < 0)
397 path[0] = '\0';
398 return r;
399 }
400
401
uv__fs_open(uv_fs_t * req)402 static ssize_t uv__fs_open(uv_fs_t* req) {
403 #ifdef O_CLOEXEC
404 return open(req->path, req->flags | O_CLOEXEC, req->mode);
405 #else /* O_CLOEXEC */
406 int r;
407
408 if (req->cb != NULL)
409 uv_rwlock_rdlock(&req->loop->cloexec_lock);
410
411 r = open(req->path, req->flags, req->mode);
412
413 /* In case of failure `uv__cloexec` will leave error in `errno`,
414 * so it is enough to just set `r` to `-1`.
415 */
416 if (r >= 0 && uv__cloexec(r, 1) != 0) {
417 r = uv__close(r);
418 if (r != 0)
419 abort();
420 r = -1;
421 }
422
423 if (req->cb != NULL)
424 uv_rwlock_rdunlock(&req->loop->cloexec_lock);
425
426 return r;
427 #endif /* O_CLOEXEC */
428 }
429
430
431 #if !HAVE_PREADV
uv__fs_preadv(uv_file fd,uv_buf_t * bufs,unsigned int nbufs,off_t off)432 static ssize_t uv__fs_preadv(uv_file fd,
433 uv_buf_t* bufs,
434 unsigned int nbufs,
435 off_t off) {
436 uv_buf_t* buf;
437 uv_buf_t* end;
438 ssize_t result;
439 ssize_t rc;
440 size_t pos;
441
442 assert(nbufs > 0);
443
444 result = 0;
445 pos = 0;
446 buf = bufs + 0;
447 end = bufs + nbufs;
448
449 for (;;) {
450 do
451 rc = pread(fd, buf->base + pos, buf->len - pos, off + result);
452 while (rc == -1 && errno == EINTR);
453
454 if (rc == 0)
455 break;
456
457 if (rc == -1 && result == 0)
458 return UV__ERR(errno);
459
460 if (rc == -1)
461 break; /* We read some data so return that, ignore the error. */
462
463 pos += rc;
464 result += rc;
465
466 if (pos < buf->len)
467 continue;
468
469 pos = 0;
470 buf += 1;
471
472 if (buf == end)
473 break;
474 }
475
476 return result;
477 }
478 #endif
479
480
uv__fs_read(uv_fs_t * req)481 static ssize_t uv__fs_read(uv_fs_t* req) {
482 #if defined(__linux__)
483 static int no_preadv;
484 #endif
485 unsigned int iovmax;
486 ssize_t result;
487
488 iovmax = uv__getiovmax();
489 if (req->nbufs > iovmax)
490 req->nbufs = iovmax;
491
492 if (req->off < 0) {
493 if (req->nbufs == 1)
494 result = read(req->file, req->bufs[0].base, req->bufs[0].len);
495 else
496 result = readv(req->file, (struct iovec*) req->bufs, req->nbufs);
497 } else {
498 if (req->nbufs == 1) {
499 result = pread(req->file, req->bufs[0].base, req->bufs[0].len, req->off);
500 goto done;
501 }
502
503 #if HAVE_PREADV
504 result = preadv(req->file, (struct iovec*) req->bufs, req->nbufs, req->off);
505 #else
506 # if defined(__linux__)
507 if (uv__load_relaxed(&no_preadv)) retry:
508 # endif
509 {
510 result = uv__fs_preadv(req->file, req->bufs, req->nbufs, req->off);
511 }
512 # if defined(__linux__)
513 else {
514 result = uv__preadv(req->file,
515 (struct iovec*)req->bufs,
516 req->nbufs,
517 req->off);
518 if (result == -1 && errno == ENOSYS) {
519 uv__store_relaxed(&no_preadv, 1);
520 goto retry;
521 }
522 }
523 # endif
524 #endif
525 }
526
527 done:
528 /* Early cleanup of bufs allocation, since we're done with it. */
529 if (req->bufs != req->bufsml)
530 uv__free(req->bufs);
531
532 req->bufs = NULL;
533 req->nbufs = 0;
534
535 #ifdef __PASE__
536 /* PASE returns EOPNOTSUPP when reading a directory, convert to EISDIR */
537 if (result == -1 && errno == EOPNOTSUPP) {
538 struct stat buf;
539 ssize_t rc;
540 rc = fstat(req->file, &buf);
541 if (rc == 0 && S_ISDIR(buf.st_mode)) {
542 errno = EISDIR;
543 }
544 }
545 #endif
546
547 return result;
548 }
549
550
551 #if defined(__APPLE__) && !defined(MAC_OS_X_VERSION_10_8)
552 #define UV_CONST_DIRENT uv__dirent_t
553 #else
554 #define UV_CONST_DIRENT const uv__dirent_t
555 #endif
556
557
uv__fs_scandir_filter(UV_CONST_DIRENT * dent)558 static int uv__fs_scandir_filter(UV_CONST_DIRENT* dent) {
559 return strcmp(dent->d_name, ".") != 0 && strcmp(dent->d_name, "..") != 0;
560 }
561
562
uv__fs_scandir_sort(UV_CONST_DIRENT ** a,UV_CONST_DIRENT ** b)563 static int uv__fs_scandir_sort(UV_CONST_DIRENT** a, UV_CONST_DIRENT** b) {
564 return strcmp((*a)->d_name, (*b)->d_name);
565 }
566
567
uv__fs_scandir(uv_fs_t * req)568 static ssize_t uv__fs_scandir(uv_fs_t* req) {
569 uv__dirent_t** dents;
570 int n;
571
572 dents = NULL;
573 n = scandir(req->path, &dents, uv__fs_scandir_filter, uv__fs_scandir_sort);
574
575 /* NOTE: We will use nbufs as an index field */
576 req->nbufs = 0;
577
578 if (n == 0) {
579 /* OS X still needs to deallocate some memory.
580 * Memory was allocated using the system allocator, so use free() here.
581 */
582 free(dents);
583 dents = NULL;
584 } else if (n == -1) {
585 return n;
586 }
587
588 req->ptr = dents;
589
590 return n;
591 }
592
uv__fs_opendir(uv_fs_t * req)593 static int uv__fs_opendir(uv_fs_t* req) {
594 uv_dir_t* dir;
595
596 dir = uv__malloc(sizeof(*dir));
597 if (dir == NULL)
598 goto error;
599
600 dir->dir = opendir(req->path);
601 if (dir->dir == NULL)
602 goto error;
603
604 req->ptr = dir;
605 return 0;
606
607 error:
608 uv__free(dir);
609 req->ptr = NULL;
610 return -1;
611 }
612
uv__fs_readdir(uv_fs_t * req)613 static int uv__fs_readdir(uv_fs_t* req) {
614 uv_dir_t* dir;
615 uv_dirent_t* dirent;
616 struct dirent* res;
617 unsigned int dirent_idx;
618 unsigned int i;
619
620 dir = req->ptr;
621 dirent_idx = 0;
622
623 while (dirent_idx < dir->nentries) {
624 /* readdir() returns NULL on end of directory, as well as on error. errno
625 is used to differentiate between the two conditions. */
626 errno = 0;
627 res = readdir(dir->dir);
628
629 if (res == NULL) {
630 if (errno != 0)
631 goto error;
632 break;
633 }
634
635 if (strcmp(res->d_name, ".") == 0 || strcmp(res->d_name, "..") == 0)
636 continue;
637
638 dirent = &dir->dirents[dirent_idx];
639 dirent->name = uv__strdup(res->d_name);
640
641 if (dirent->name == NULL)
642 goto error;
643
644 dirent->type = uv__fs_get_dirent_type(res);
645 ++dirent_idx;
646 }
647
648 return dirent_idx;
649
650 error:
651 for (i = 0; i < dirent_idx; ++i) {
652 uv__free((char*) dir->dirents[i].name);
653 dir->dirents[i].name = NULL;
654 }
655
656 return -1;
657 }
658
uv__fs_closedir(uv_fs_t * req)659 static int uv__fs_closedir(uv_fs_t* req) {
660 uv_dir_t* dir;
661
662 dir = req->ptr;
663
664 if (dir->dir != NULL) {
665 closedir(dir->dir);
666 dir->dir = NULL;
667 }
668
669 uv__free(req->ptr);
670 req->ptr = NULL;
671 return 0;
672 }
673
uv__fs_statfs(uv_fs_t * req)674 static int uv__fs_statfs(uv_fs_t* req) {
675 uv_statfs_t* stat_fs;
676 #if defined(__sun) || \
677 defined(__MVS__) || \
678 defined(__NetBSD__) || \
679 defined(__HAIKU__) || \
680 defined(__QNX__)
681 struct statvfs buf;
682
683 if (0 != statvfs(req->path, &buf))
684 #else
685 struct statfs buf;
686
687 if (0 != statfs(req->path, &buf))
688 #endif /* defined(__sun) */
689 return -1;
690
691 stat_fs = uv__malloc(sizeof(*stat_fs));
692 if (stat_fs == NULL) {
693 errno = ENOMEM;
694 return -1;
695 }
696
697 #if defined(__sun) || \
698 defined(__MVS__) || \
699 defined(__OpenBSD__) || \
700 defined(__NetBSD__) || \
701 defined(__HAIKU__) || \
702 defined(__QNX__)
703 stat_fs->f_type = 0; /* f_type is not supported. */
704 #else
705 stat_fs->f_type = buf.f_type;
706 #endif
707 stat_fs->f_bsize = buf.f_bsize;
708 stat_fs->f_blocks = buf.f_blocks;
709 stat_fs->f_bfree = buf.f_bfree;
710 stat_fs->f_bavail = buf.f_bavail;
711 stat_fs->f_files = buf.f_files;
712 stat_fs->f_ffree = buf.f_ffree;
713 req->ptr = stat_fs;
714 return 0;
715 }
716
uv__fs_pathmax_size(const char * path)717 static ssize_t uv__fs_pathmax_size(const char* path) {
718 ssize_t pathmax;
719
720 pathmax = pathconf(path, _PC_PATH_MAX);
721
722 if (pathmax == -1)
723 pathmax = UV__PATH_MAX;
724
725 return pathmax;
726 }
727
uv__fs_readlink(uv_fs_t * req)728 static ssize_t uv__fs_readlink(uv_fs_t* req) {
729 ssize_t maxlen;
730 ssize_t len;
731 char* buf;
732
733 #if defined(_POSIX_PATH_MAX) || defined(PATH_MAX)
734 maxlen = uv__fs_pathmax_size(req->path);
735 #else
736 /* We may not have a real PATH_MAX. Read size of link. */
737 struct stat st;
738 int ret;
739 ret = lstat(req->path, &st);
740 if (ret != 0)
741 return -1;
742 if (!S_ISLNK(st.st_mode)) {
743 errno = EINVAL;
744 return -1;
745 }
746
747 maxlen = st.st_size;
748
749 /* According to readlink(2) lstat can report st_size == 0
750 for some symlinks, such as those in /proc or /sys. */
751 if (maxlen == 0)
752 maxlen = uv__fs_pathmax_size(req->path);
753 #endif
754
755 buf = uv__malloc(maxlen);
756
757 if (buf == NULL) {
758 errno = ENOMEM;
759 return -1;
760 }
761
762 #if defined(__MVS__)
763 len = os390_readlink(req->path, buf, maxlen);
764 #else
765 len = readlink(req->path, buf, maxlen);
766 #endif
767
768 if (len == -1) {
769 uv__free(buf);
770 return -1;
771 }
772
773 /* Uncommon case: resize to make room for the trailing nul byte. */
774 if (len == maxlen) {
775 buf = uv__reallocf(buf, len + 1);
776
777 if (buf == NULL)
778 return -1;
779 }
780
781 buf[len] = '\0';
782 req->ptr = buf;
783
784 return 0;
785 }
786
uv__fs_realpath(uv_fs_t * req)787 static ssize_t uv__fs_realpath(uv_fs_t* req) {
788 char* buf;
789
790 #if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L
791 buf = realpath(req->path, NULL);
792 if (buf == NULL)
793 return -1;
794 #else
795 ssize_t len;
796
797 len = uv__fs_pathmax_size(req->path);
798 buf = uv__malloc(len + 1);
799
800 if (buf == NULL) {
801 errno = ENOMEM;
802 return -1;
803 }
804
805 if (realpath(req->path, buf) == NULL) {
806 uv__free(buf);
807 return -1;
808 }
809 #endif
810
811 req->ptr = buf;
812
813 return 0;
814 }
815
uv__fs_sendfile_emul(uv_fs_t * req)816 static ssize_t uv__fs_sendfile_emul(uv_fs_t* req) {
817 struct pollfd pfd;
818 int use_pread;
819 off_t offset;
820 ssize_t nsent;
821 ssize_t nread;
822 ssize_t nwritten;
823 size_t buflen;
824 size_t len;
825 ssize_t n;
826 int in_fd;
827 int out_fd;
828 char buf[8192];
829
830 len = req->bufsml[0].len;
831 in_fd = req->flags;
832 out_fd = req->file;
833 offset = req->off;
834 use_pread = 1;
835
836 /* Here are the rules regarding errors:
837 *
838 * 1. Read errors are reported only if nsent==0, otherwise we return nsent.
839 * The user needs to know that some data has already been sent, to stop
840 * them from sending it twice.
841 *
842 * 2. Write errors are always reported. Write errors are bad because they
843 * mean data loss: we've read data but now we can't write it out.
844 *
845 * We try to use pread() and fall back to regular read() if the source fd
846 * doesn't support positional reads, for example when it's a pipe fd.
847 *
848 * If we get EAGAIN when writing to the target fd, we poll() on it until
849 * it becomes writable again.
850 *
851 * FIXME: If we get a write error when use_pread==1, it should be safe to
852 * return the number of sent bytes instead of an error because pread()
853 * is, in theory, idempotent. However, special files in /dev or /proc
854 * may support pread() but not necessarily return the same data on
855 * successive reads.
856 *
857 * FIXME: There is no way now to signal that we managed to send *some* data
858 * before a write error.
859 */
860 for (nsent = 0; (size_t) nsent < len; ) {
861 buflen = len - nsent;
862
863 if (buflen > sizeof(buf))
864 buflen = sizeof(buf);
865
866 do
867 if (use_pread)
868 nread = pread(in_fd, buf, buflen, offset);
869 else
870 nread = read(in_fd, buf, buflen);
871 while (nread == -1 && errno == EINTR);
872
873 if (nread == 0)
874 goto out;
875
876 if (nread == -1) {
877 if (use_pread && nsent == 0 && (errno == EIO || errno == ESPIPE)) {
878 use_pread = 0;
879 continue;
880 }
881
882 if (nsent == 0)
883 nsent = -1;
884
885 goto out;
886 }
887
888 for (nwritten = 0; nwritten < nread; ) {
889 do
890 n = write(out_fd, buf + nwritten, nread - nwritten);
891 while (n == -1 && errno == EINTR);
892
893 if (n != -1) {
894 nwritten += n;
895 continue;
896 }
897
898 if (errno != EAGAIN && errno != EWOULDBLOCK) {
899 nsent = -1;
900 goto out;
901 }
902
903 pfd.fd = out_fd;
904 pfd.events = POLLOUT;
905 pfd.revents = 0;
906
907 do
908 n = poll(&pfd, 1, -1);
909 while (n == -1 && errno == EINTR);
910
911 if (n == -1 || (pfd.revents & ~POLLOUT) != 0) {
912 errno = EIO;
913 nsent = -1;
914 goto out;
915 }
916 }
917
918 offset += nread;
919 nsent += nread;
920 }
921
922 out:
923 if (nsent != -1)
924 req->off = offset;
925
926 return nsent;
927 }
928
929
930 #ifdef __linux__
uv__kernel_version(void)931 static unsigned uv__kernel_version(void) {
932 static unsigned cached_version;
933 struct utsname u;
934 unsigned version;
935 unsigned major;
936 unsigned minor;
937 unsigned patch;
938
939 version = uv__load_relaxed(&cached_version);
940 if (version != 0)
941 return version;
942
943 if (-1 == uname(&u))
944 return 0;
945
946 if (3 != sscanf(u.release, "%u.%u.%u", &major, &minor, &patch))
947 return 0;
948
949 version = major * 65536 + minor * 256 + patch;
950 uv__store_relaxed(&cached_version, version);
951
952 return version;
953 }
954
955
956 /* Pre-4.20 kernels have a bug where CephFS uses the RADOS copy-from command
957 * in copy_file_range() when it shouldn't. There is no workaround except to
958 * fall back to a regular copy.
959 */
uv__is_buggy_cephfs(int fd)960 static int uv__is_buggy_cephfs(int fd) {
961 struct statfs s;
962
963 if (-1 == fstatfs(fd, &s))
964 return 0;
965
966 if (s.f_type != /* CephFS */ 0xC36400)
967 return 0;
968
969 return uv__kernel_version() < /* 4.20.0 */ 0x041400;
970 }
971
972
uv__is_cifs_or_smb(int fd)973 static int uv__is_cifs_or_smb(int fd) {
974 struct statfs s;
975
976 if (-1 == fstatfs(fd, &s))
977 return 0;
978
979 switch ((unsigned) s.f_type) {
980 case 0x0000517Bu: /* SMB */
981 case 0xFE534D42u: /* SMB2 */
982 case 0xFF534D42u: /* CIFS */
983 return 1;
984 }
985
986 return 0;
987 }
988
989
uv__fs_try_copy_file_range(int in_fd,off_t * off,int out_fd,size_t len)990 static ssize_t uv__fs_try_copy_file_range(int in_fd, off_t* off,
991 int out_fd, size_t len) {
992 static int no_copy_file_range_support;
993 ssize_t r;
994
995 if (uv__load_relaxed(&no_copy_file_range_support)) {
996 errno = ENOSYS;
997 return -1;
998 }
999
1000 r = uv__fs_copy_file_range(in_fd, off, out_fd, NULL, len, 0);
1001
1002 if (r != -1)
1003 return r;
1004
1005 switch (errno) {
1006 case EACCES:
1007 /* Pre-4.20 kernels have a bug where CephFS uses the RADOS
1008 * copy-from command when it shouldn't.
1009 */
1010 if (uv__is_buggy_cephfs(in_fd))
1011 errno = ENOSYS; /* Use fallback. */
1012 break;
1013 case ENOSYS:
1014 uv__store_relaxed(&no_copy_file_range_support, 1);
1015 break;
1016 case EPERM:
1017 /* It's been reported that CIFS spuriously fails.
1018 * Consider it a transient error.
1019 */
1020 if (uv__is_cifs_or_smb(out_fd))
1021 errno = ENOSYS; /* Use fallback. */
1022 break;
1023 case ENOTSUP:
1024 case EXDEV:
1025 /* ENOTSUP - it could work on another file system type.
1026 * EXDEV - it will not work when in_fd and out_fd are not on the same
1027 * mounted filesystem (pre Linux 5.3)
1028 */
1029 errno = ENOSYS; /* Use fallback. */
1030 break;
1031 }
1032
1033 return -1;
1034 }
1035
1036 #endif /* __linux__ */
1037
1038
uv__fs_sendfile(uv_fs_t * req)1039 static ssize_t uv__fs_sendfile(uv_fs_t* req) {
1040 int in_fd;
1041 int out_fd;
1042
1043 in_fd = req->flags;
1044 out_fd = req->file;
1045
1046 #if defined(__linux__) || defined(__sun)
1047 {
1048 off_t off;
1049 ssize_t r;
1050 size_t len;
1051 int try_sendfile;
1052
1053 off = req->off;
1054 len = req->bufsml[0].len;
1055 try_sendfile = 1;
1056
1057 #ifdef __linux__
1058 r = uv__fs_try_copy_file_range(in_fd, &off, out_fd, len);
1059 try_sendfile = (r == -1 && errno == ENOSYS);
1060 #endif
1061
1062 if (try_sendfile)
1063 r = sendfile(out_fd, in_fd, &off, len);
1064
1065 /* sendfile() on SunOS returns EINVAL if the target fd is not a socket but
1066 * it still writes out data. Fortunately, we can detect it by checking if
1067 * the offset has been updated.
1068 */
1069 if (r != -1 || off > req->off) {
1070 r = off - req->off;
1071 req->off = off;
1072 return r;
1073 }
1074
1075 if (errno == EINVAL ||
1076 errno == EIO ||
1077 errno == ENOTSOCK ||
1078 errno == EXDEV) {
1079 errno = 0;
1080 return uv__fs_sendfile_emul(req);
1081 }
1082
1083 return -1;
1084 }
1085 #elif defined(__APPLE__) || \
1086 defined(__DragonFly__) || \
1087 defined(__FreeBSD__) || \
1088 defined(__FreeBSD_kernel__)
1089 {
1090 off_t len;
1091 ssize_t r;
1092
1093 /* sendfile() on FreeBSD and Darwin returns EAGAIN if the target fd is in
1094 * non-blocking mode and not all data could be written. If a non-zero
1095 * number of bytes have been sent, we don't consider it an error.
1096 */
1097
1098 #if defined(__FreeBSD__) || defined(__DragonFly__)
1099 #if defined(__FreeBSD__)
1100 off_t off;
1101
1102 off = req->off;
1103 r = uv__fs_copy_file_range(in_fd, &off, out_fd, NULL, req->bufsml[0].len, 0);
1104 if (r >= 0) {
1105 r = off - req->off;
1106 req->off = off;
1107 return r;
1108 }
1109 #endif
1110 len = 0;
1111 r = sendfile(in_fd, out_fd, req->off, req->bufsml[0].len, NULL, &len, 0);
1112 #elif defined(__FreeBSD_kernel__)
1113 len = 0;
1114 r = bsd_sendfile(in_fd,
1115 out_fd,
1116 req->off,
1117 req->bufsml[0].len,
1118 NULL,
1119 &len,
1120 0);
1121 #else
1122 /* The darwin sendfile takes len as an input for the length to send,
1123 * so make sure to initialize it with the caller's value. */
1124 len = req->bufsml[0].len;
1125 r = sendfile(in_fd, out_fd, req->off, &len, NULL, 0);
1126 #endif
1127
1128 /*
1129 * The man page for sendfile(2) on DragonFly states that `len` contains
1130 * a meaningful value ONLY in case of EAGAIN and EINTR.
1131 * Nothing is said about it's value in case of other errors, so better
1132 * not depend on the potential wrong assumption that is was not modified
1133 * by the syscall.
1134 */
1135 if (r == 0 || ((errno == EAGAIN || errno == EINTR) && len != 0)) {
1136 req->off += len;
1137 return (ssize_t) len;
1138 }
1139
1140 if (errno == EINVAL ||
1141 errno == EIO ||
1142 errno == ENOTSOCK ||
1143 errno == EXDEV) {
1144 errno = 0;
1145 return uv__fs_sendfile_emul(req);
1146 }
1147
1148 return -1;
1149 }
1150 #else
1151 /* Squelch compiler warnings. */
1152 (void) &in_fd;
1153 (void) &out_fd;
1154
1155 return uv__fs_sendfile_emul(req);
1156 #endif
1157 }
1158
1159
uv__fs_utime(uv_fs_t * req)1160 static ssize_t uv__fs_utime(uv_fs_t* req) {
1161 #if defined(__linux__) \
1162 || defined(_AIX71) \
1163 || defined(__sun) \
1164 || defined(__HAIKU__)
1165 struct timespec ts[2];
1166 ts[0] = uv__fs_to_timespec(req->atime);
1167 ts[1] = uv__fs_to_timespec(req->mtime);
1168 return utimensat(AT_FDCWD, req->path, ts, 0);
1169 #elif defined(__APPLE__) \
1170 || defined(__DragonFly__) \
1171 || defined(__FreeBSD__) \
1172 || defined(__FreeBSD_kernel__) \
1173 || defined(__NetBSD__) \
1174 || defined(__OpenBSD__)
1175 struct timeval tv[2];
1176 tv[0] = uv__fs_to_timeval(req->atime);
1177 tv[1] = uv__fs_to_timeval(req->mtime);
1178 return utimes(req->path, tv);
1179 #elif defined(_AIX) \
1180 && !defined(_AIX71)
1181 struct utimbuf buf;
1182 buf.actime = req->atime;
1183 buf.modtime = req->mtime;
1184 return utime(req->path, &buf);
1185 #elif defined(__MVS__)
1186 attrib_t atr;
1187 memset(&atr, 0, sizeof(atr));
1188 atr.att_mtimechg = 1;
1189 atr.att_atimechg = 1;
1190 atr.att_mtime = req->mtime;
1191 atr.att_atime = req->atime;
1192 return __lchattr((char*) req->path, &atr, sizeof(atr));
1193 #else
1194 errno = ENOSYS;
1195 return -1;
1196 #endif
1197 }
1198
1199
uv__fs_lutime(uv_fs_t * req)1200 static ssize_t uv__fs_lutime(uv_fs_t* req) {
1201 #if defined(__linux__) || \
1202 defined(_AIX71) || \
1203 defined(__sun) || \
1204 defined(__HAIKU__) || \
1205 defined(__GNU__) || \
1206 defined(__OpenBSD__)
1207 struct timespec ts[2];
1208 ts[0] = uv__fs_to_timespec(req->atime);
1209 ts[1] = uv__fs_to_timespec(req->mtime);
1210 return utimensat(AT_FDCWD, req->path, ts, AT_SYMLINK_NOFOLLOW);
1211 #elif defined(__APPLE__) || \
1212 defined(__DragonFly__) || \
1213 defined(__FreeBSD__) || \
1214 defined(__FreeBSD_kernel__) || \
1215 defined(__NetBSD__)
1216 struct timeval tv[2];
1217 tv[0] = uv__fs_to_timeval(req->atime);
1218 tv[1] = uv__fs_to_timeval(req->mtime);
1219 return lutimes(req->path, tv);
1220 #else
1221 errno = ENOSYS;
1222 return -1;
1223 #endif
1224 }
1225
1226
uv__fs_write(uv_fs_t * req)1227 static ssize_t uv__fs_write(uv_fs_t* req) {
1228 #if defined(__linux__)
1229 static int no_pwritev;
1230 #endif
1231 ssize_t r;
1232
1233 /* Serialize writes on OS X, concurrent write() and pwrite() calls result in
1234 * data loss. We can't use a per-file descriptor lock, the descriptor may be
1235 * a dup().
1236 */
1237 #if defined(__APPLE__)
1238 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
1239
1240 if (pthread_mutex_lock(&lock))
1241 abort();
1242 #endif
1243
1244 if (req->off < 0) {
1245 if (req->nbufs == 1)
1246 r = write(req->file, req->bufs[0].base, req->bufs[0].len);
1247 else
1248 r = writev(req->file, (struct iovec*) req->bufs, req->nbufs);
1249 } else {
1250 if (req->nbufs == 1) {
1251 r = pwrite(req->file, req->bufs[0].base, req->bufs[0].len, req->off);
1252 goto done;
1253 }
1254 #if HAVE_PREADV
1255 r = pwritev(req->file, (struct iovec*) req->bufs, req->nbufs, req->off);
1256 #else
1257 # if defined(__linux__)
1258 if (no_pwritev) retry:
1259 # endif
1260 {
1261 r = pwrite(req->file, req->bufs[0].base, req->bufs[0].len, req->off);
1262 }
1263 # if defined(__linux__)
1264 else {
1265 r = uv__pwritev(req->file,
1266 (struct iovec*) req->bufs,
1267 req->nbufs,
1268 req->off);
1269 if (r == -1 && errno == ENOSYS) {
1270 no_pwritev = 1;
1271 goto retry;
1272 }
1273 }
1274 # endif
1275 #endif
1276 }
1277
1278 done:
1279 #if defined(__APPLE__)
1280 if (pthread_mutex_unlock(&lock))
1281 abort();
1282 #endif
1283
1284 return r;
1285 }
1286
uv__fs_copyfile(uv_fs_t * req)1287 static ssize_t uv__fs_copyfile(uv_fs_t* req) {
1288 uv_fs_t fs_req;
1289 uv_file srcfd;
1290 uv_file dstfd;
1291 struct stat src_statsbuf;
1292 struct stat dst_statsbuf;
1293 int dst_flags;
1294 int result;
1295 int err;
1296 off_t bytes_to_send;
1297 off_t in_offset;
1298 off_t bytes_written;
1299 size_t bytes_chunk;
1300
1301 dstfd = -1;
1302 err = 0;
1303
1304 /* Open the source file. */
1305 srcfd = uv_fs_open(NULL, &fs_req, req->path, O_RDONLY, 0, NULL);
1306 uv_fs_req_cleanup(&fs_req);
1307
1308 if (srcfd < 0)
1309 return srcfd;
1310
1311 /* Get the source file's mode. */
1312 if (fstat(srcfd, &src_statsbuf)) {
1313 err = UV__ERR(errno);
1314 goto out;
1315 }
1316
1317 dst_flags = O_WRONLY | O_CREAT;
1318
1319 if (req->flags & UV_FS_COPYFILE_EXCL)
1320 dst_flags |= O_EXCL;
1321
1322 /* Open the destination file. */
1323 dstfd = uv_fs_open(NULL,
1324 &fs_req,
1325 req->new_path,
1326 dst_flags,
1327 src_statsbuf.st_mode,
1328 NULL);
1329 uv_fs_req_cleanup(&fs_req);
1330
1331 if (dstfd < 0) {
1332 err = dstfd;
1333 goto out;
1334 }
1335
1336 /* If the file is not being opened exclusively, verify that the source and
1337 destination are not the same file. If they are the same, bail out early. */
1338 if ((req->flags & UV_FS_COPYFILE_EXCL) == 0) {
1339 /* Get the destination file's mode. */
1340 if (fstat(dstfd, &dst_statsbuf)) {
1341 err = UV__ERR(errno);
1342 goto out;
1343 }
1344
1345 /* Check if srcfd and dstfd refer to the same file */
1346 if (src_statsbuf.st_dev == dst_statsbuf.st_dev &&
1347 src_statsbuf.st_ino == dst_statsbuf.st_ino) {
1348 goto out;
1349 }
1350
1351 /* Truncate the file in case the destination already existed. */
1352 if (ftruncate(dstfd, 0) != 0) {
1353 err = UV__ERR(errno);
1354 goto out;
1355 }
1356 }
1357
1358 if (fchmod(dstfd, src_statsbuf.st_mode) == -1) {
1359 err = UV__ERR(errno);
1360 #ifdef __linux__
1361 /* fchmod() on CIFS shares always fails with EPERM unless the share is
1362 * mounted with "noperm". As fchmod() is a meaningless operation on such
1363 * shares anyway, detect that condition and squelch the error.
1364 */
1365 if (err != UV_EPERM)
1366 goto out;
1367
1368 if (!uv__is_cifs_or_smb(dstfd))
1369 goto out;
1370
1371 err = 0;
1372 #else /* !__linux__ */
1373 goto out;
1374 #endif /* !__linux__ */
1375 }
1376
1377 #ifdef FICLONE
1378 if (req->flags & UV_FS_COPYFILE_FICLONE ||
1379 req->flags & UV_FS_COPYFILE_FICLONE_FORCE) {
1380 if (ioctl(dstfd, FICLONE, srcfd) == 0) {
1381 /* ioctl() with FICLONE succeeded. */
1382 goto out;
1383 }
1384 /* If an error occurred and force was set, return the error to the caller;
1385 * fall back to sendfile() when force was not set. */
1386 if (req->flags & UV_FS_COPYFILE_FICLONE_FORCE) {
1387 err = UV__ERR(errno);
1388 goto out;
1389 }
1390 }
1391 #else
1392 if (req->flags & UV_FS_COPYFILE_FICLONE_FORCE) {
1393 err = UV_ENOSYS;
1394 goto out;
1395 }
1396 #endif
1397
1398 bytes_to_send = src_statsbuf.st_size;
1399 in_offset = 0;
1400 while (bytes_to_send != 0) {
1401 bytes_chunk = SSIZE_MAX;
1402 if (bytes_to_send < (off_t) bytes_chunk)
1403 bytes_chunk = bytes_to_send;
1404 uv_fs_sendfile(NULL, &fs_req, dstfd, srcfd, in_offset, bytes_chunk, NULL);
1405 bytes_written = fs_req.result;
1406 uv_fs_req_cleanup(&fs_req);
1407
1408 if (bytes_written < 0) {
1409 err = bytes_written;
1410 break;
1411 }
1412
1413 bytes_to_send -= bytes_written;
1414 in_offset += bytes_written;
1415 }
1416
1417 out:
1418 if (err < 0)
1419 result = err;
1420 else
1421 result = 0;
1422
1423 /* Close the source file. */
1424 err = uv__close_nocheckstdio(srcfd);
1425
1426 /* Don't overwrite any existing errors. */
1427 if (err != 0 && result == 0)
1428 result = err;
1429
1430 /* Close the destination file if it is open. */
1431 if (dstfd >= 0) {
1432 err = uv__close_nocheckstdio(dstfd);
1433
1434 /* Don't overwrite any existing errors. */
1435 if (err != 0 && result == 0)
1436 result = err;
1437
1438 /* Remove the destination file if something went wrong. */
1439 if (result != 0) {
1440 uv_fs_unlink(NULL, &fs_req, req->new_path, NULL);
1441 /* Ignore the unlink return value, as an error already happened. */
1442 uv_fs_req_cleanup(&fs_req);
1443 }
1444 }
1445
1446 if (result == 0)
1447 return 0;
1448
1449 errno = UV__ERR(result);
1450 return -1;
1451 }
1452
uv__to_stat(struct stat * src,uv_stat_t * dst)1453 static void uv__to_stat(struct stat* src, uv_stat_t* dst) {
1454 dst->st_dev = src->st_dev;
1455 dst->st_mode = src->st_mode;
1456 dst->st_nlink = src->st_nlink;
1457 dst->st_uid = src->st_uid;
1458 dst->st_gid = src->st_gid;
1459 dst->st_rdev = src->st_rdev;
1460 dst->st_ino = src->st_ino;
1461 dst->st_size = src->st_size;
1462 dst->st_blksize = src->st_blksize;
1463 dst->st_blocks = src->st_blocks;
1464
1465 #if defined(__APPLE__)
1466 dst->st_atim.tv_sec = src->st_atimespec.tv_sec;
1467 dst->st_atim.tv_nsec = src->st_atimespec.tv_nsec;
1468 dst->st_mtim.tv_sec = src->st_mtimespec.tv_sec;
1469 dst->st_mtim.tv_nsec = src->st_mtimespec.tv_nsec;
1470 dst->st_ctim.tv_sec = src->st_ctimespec.tv_sec;
1471 dst->st_ctim.tv_nsec = src->st_ctimespec.tv_nsec;
1472 dst->st_birthtim.tv_sec = src->st_birthtimespec.tv_sec;
1473 dst->st_birthtim.tv_nsec = src->st_birthtimespec.tv_nsec;
1474 dst->st_flags = src->st_flags;
1475 dst->st_gen = src->st_gen;
1476 #elif defined(__ANDROID__)
1477 dst->st_atim.tv_sec = src->st_atime;
1478 dst->st_atim.tv_nsec = src->st_atimensec;
1479 dst->st_mtim.tv_sec = src->st_mtime;
1480 dst->st_mtim.tv_nsec = src->st_mtimensec;
1481 dst->st_ctim.tv_sec = src->st_ctime;
1482 dst->st_ctim.tv_nsec = src->st_ctimensec;
1483 dst->st_birthtim.tv_sec = src->st_ctime;
1484 dst->st_birthtim.tv_nsec = src->st_ctimensec;
1485 dst->st_flags = 0;
1486 dst->st_gen = 0;
1487 #elif !defined(_AIX) && \
1488 !defined(__MVS__) && ( \
1489 defined(__DragonFly__) || \
1490 defined(__FreeBSD__) || \
1491 defined(__OpenBSD__) || \
1492 defined(__NetBSD__) || \
1493 defined(_GNU_SOURCE) || \
1494 defined(_BSD_SOURCE) || \
1495 defined(_SVID_SOURCE) || \
1496 defined(_XOPEN_SOURCE) || \
1497 defined(_DEFAULT_SOURCE))
1498 dst->st_atim.tv_sec = src->st_atim.tv_sec;
1499 dst->st_atim.tv_nsec = src->st_atim.tv_nsec;
1500 dst->st_mtim.tv_sec = src->st_mtim.tv_sec;
1501 dst->st_mtim.tv_nsec = src->st_mtim.tv_nsec;
1502 dst->st_ctim.tv_sec = src->st_ctim.tv_sec;
1503 dst->st_ctim.tv_nsec = src->st_ctim.tv_nsec;
1504 # if defined(__FreeBSD__) || \
1505 defined(__NetBSD__)
1506 dst->st_birthtim.tv_sec = src->st_birthtim.tv_sec;
1507 dst->st_birthtim.tv_nsec = src->st_birthtim.tv_nsec;
1508 dst->st_flags = src->st_flags;
1509 dst->st_gen = src->st_gen;
1510 # else
1511 dst->st_birthtim.tv_sec = src->st_ctim.tv_sec;
1512 dst->st_birthtim.tv_nsec = src->st_ctim.tv_nsec;
1513 dst->st_flags = 0;
1514 dst->st_gen = 0;
1515 # endif
1516 #else
1517 dst->st_atim.tv_sec = src->st_atime;
1518 dst->st_atim.tv_nsec = 0;
1519 dst->st_mtim.tv_sec = src->st_mtime;
1520 dst->st_mtim.tv_nsec = 0;
1521 dst->st_ctim.tv_sec = src->st_ctime;
1522 dst->st_ctim.tv_nsec = 0;
1523 dst->st_birthtim.tv_sec = src->st_ctime;
1524 dst->st_birthtim.tv_nsec = 0;
1525 dst->st_flags = 0;
1526 dst->st_gen = 0;
1527 #endif
1528 }
1529
1530
uv__fs_statx(int fd,const char * path,int is_fstat,int is_lstat,uv_stat_t * buf)1531 static int uv__fs_statx(int fd,
1532 const char* path,
1533 int is_fstat,
1534 int is_lstat,
1535 uv_stat_t* buf) {
1536 STATIC_ASSERT(UV_ENOSYS != -1);
1537 #ifdef __linux__
1538 static int no_statx;
1539 struct uv__statx statxbuf;
1540 int dirfd;
1541 int flags;
1542 int mode;
1543 int rc;
1544
1545 if (uv__load_relaxed(&no_statx))
1546 return UV_ENOSYS;
1547
1548 dirfd = AT_FDCWD;
1549 flags = 0; /* AT_STATX_SYNC_AS_STAT */
1550 mode = 0xFFF; /* STATX_BASIC_STATS + STATX_BTIME */
1551
1552 if (is_fstat) {
1553 dirfd = fd;
1554 flags |= 0x1000; /* AT_EMPTY_PATH */
1555 }
1556
1557 if (is_lstat)
1558 flags |= AT_SYMLINK_NOFOLLOW;
1559
1560 rc = uv__statx(dirfd, path, flags, mode, &statxbuf);
1561
1562 switch (rc) {
1563 case 0:
1564 break;
1565 case -1:
1566 /* EPERM happens when a seccomp filter rejects the system call.
1567 * Has been observed with libseccomp < 2.3.3 and docker < 18.04.
1568 * EOPNOTSUPP is used on DVS exported filesystems
1569 */
1570 if (errno != EINVAL && errno != EPERM && errno != ENOSYS && errno != EOPNOTSUPP)
1571 return -1;
1572 /* Fall through. */
1573 default:
1574 /* Normally on success, zero is returned and On error, -1 is returned.
1575 * Observed on S390 RHEL running in a docker container with statx not
1576 * implemented, rc might return 1 with 0 set as the error code in which
1577 * case we return ENOSYS.
1578 */
1579 uv__store_relaxed(&no_statx, 1);
1580 return UV_ENOSYS;
1581 }
1582
1583 buf->st_dev = makedev(statxbuf.stx_dev_major, statxbuf.stx_dev_minor);
1584 buf->st_mode = statxbuf.stx_mode;
1585 buf->st_nlink = statxbuf.stx_nlink;
1586 buf->st_uid = statxbuf.stx_uid;
1587 buf->st_gid = statxbuf.stx_gid;
1588 buf->st_rdev = makedev(statxbuf.stx_rdev_major, statxbuf.stx_rdev_minor);
1589 buf->st_ino = statxbuf.stx_ino;
1590 buf->st_size = statxbuf.stx_size;
1591 buf->st_blksize = statxbuf.stx_blksize;
1592 buf->st_blocks = statxbuf.stx_blocks;
1593 buf->st_atim.tv_sec = statxbuf.stx_atime.tv_sec;
1594 buf->st_atim.tv_nsec = statxbuf.stx_atime.tv_nsec;
1595 buf->st_mtim.tv_sec = statxbuf.stx_mtime.tv_sec;
1596 buf->st_mtim.tv_nsec = statxbuf.stx_mtime.tv_nsec;
1597 buf->st_ctim.tv_sec = statxbuf.stx_ctime.tv_sec;
1598 buf->st_ctim.tv_nsec = statxbuf.stx_ctime.tv_nsec;
1599 buf->st_birthtim.tv_sec = statxbuf.stx_btime.tv_sec;
1600 buf->st_birthtim.tv_nsec = statxbuf.stx_btime.tv_nsec;
1601 buf->st_flags = 0;
1602 buf->st_gen = 0;
1603
1604 return 0;
1605 #else
1606 return UV_ENOSYS;
1607 #endif /* __linux__ */
1608 }
1609
1610
uv__fs_stat(const char * path,uv_stat_t * buf)1611 static int uv__fs_stat(const char *path, uv_stat_t *buf) {
1612 struct stat pbuf;
1613 int ret;
1614
1615 ret = uv__fs_statx(-1, path, /* is_fstat */ 0, /* is_lstat */ 0, buf);
1616 if (ret != UV_ENOSYS)
1617 return ret;
1618
1619 ret = stat(path, &pbuf);
1620 if (ret == 0)
1621 uv__to_stat(&pbuf, buf);
1622
1623 return ret;
1624 }
1625
1626
uv__fs_lstat(const char * path,uv_stat_t * buf)1627 static int uv__fs_lstat(const char *path, uv_stat_t *buf) {
1628 struct stat pbuf;
1629 int ret;
1630
1631 ret = uv__fs_statx(-1, path, /* is_fstat */ 0, /* is_lstat */ 1, buf);
1632 if (ret != UV_ENOSYS)
1633 return ret;
1634
1635 ret = lstat(path, &pbuf);
1636 if (ret == 0)
1637 uv__to_stat(&pbuf, buf);
1638
1639 return ret;
1640 }
1641
1642
uv__fs_fstat(int fd,uv_stat_t * buf)1643 static int uv__fs_fstat(int fd, uv_stat_t *buf) {
1644 struct stat pbuf;
1645 int ret;
1646
1647 ret = uv__fs_statx(fd, "", /* is_fstat */ 1, /* is_lstat */ 0, buf);
1648 if (ret != UV_ENOSYS)
1649 return ret;
1650
1651 ret = fstat(fd, &pbuf);
1652 if (ret == 0)
1653 uv__to_stat(&pbuf, buf);
1654
1655 return ret;
1656 }
1657
uv__fs_buf_offset(uv_buf_t * bufs,size_t size)1658 static size_t uv__fs_buf_offset(uv_buf_t* bufs, size_t size) {
1659 size_t offset;
1660 /* Figure out which bufs are done */
1661 for (offset = 0; size > 0 && bufs[offset].len <= size; ++offset)
1662 size -= bufs[offset].len;
1663
1664 /* Fix a partial read/write */
1665 if (size > 0) {
1666 bufs[offset].base += size;
1667 bufs[offset].len -= size;
1668 }
1669 return offset;
1670 }
1671
uv__fs_write_all(uv_fs_t * req)1672 static ssize_t uv__fs_write_all(uv_fs_t* req) {
1673 unsigned int iovmax;
1674 unsigned int nbufs;
1675 uv_buf_t* bufs;
1676 ssize_t total;
1677 ssize_t result;
1678
1679 iovmax = uv__getiovmax();
1680 nbufs = req->nbufs;
1681 bufs = req->bufs;
1682 total = 0;
1683
1684 while (nbufs > 0) {
1685 req->nbufs = nbufs;
1686 if (req->nbufs > iovmax)
1687 req->nbufs = iovmax;
1688
1689 do
1690 result = uv__fs_write(req);
1691 while (result < 0 && errno == EINTR);
1692
1693 if (result <= 0) {
1694 if (total == 0)
1695 total = result;
1696 break;
1697 }
1698
1699 if (req->off >= 0)
1700 req->off += result;
1701
1702 req->nbufs = uv__fs_buf_offset(req->bufs, result);
1703 req->bufs += req->nbufs;
1704 nbufs -= req->nbufs;
1705 total += result;
1706 }
1707
1708 if (bufs != req->bufsml)
1709 uv__free(bufs);
1710
1711 req->bufs = NULL;
1712 req->nbufs = 0;
1713
1714 return total;
1715 }
1716
1717
uv__fs_work(struct uv__work * w)1718 static void uv__fs_work(struct uv__work* w) {
1719 int retry_on_eintr;
1720 uv_fs_t* req;
1721 ssize_t r;
1722
1723 req = container_of(w, uv_fs_t, work_req);
1724 retry_on_eintr = !(req->fs_type == UV_FS_CLOSE ||
1725 req->fs_type == UV_FS_READ);
1726
1727 do {
1728 errno = 0;
1729
1730 #define X(type, action) \
1731 case UV_FS_ ## type: \
1732 r = action; \
1733 break;
1734
1735 switch (req->fs_type) {
1736 X(ACCESS, access(req->path, req->flags));
1737 X(CHMOD, chmod(req->path, req->mode));
1738 X(CHOWN, chown(req->path, req->uid, req->gid));
1739 X(CLOSE, uv__fs_close(req->file));
1740 X(COPYFILE, uv__fs_copyfile(req));
1741 X(FCHMOD, fchmod(req->file, req->mode));
1742 X(FCHOWN, fchown(req->file, req->uid, req->gid));
1743 X(LCHOWN, lchown(req->path, req->uid, req->gid));
1744 X(FDATASYNC, uv__fs_fdatasync(req));
1745 X(FSTAT, uv__fs_fstat(req->file, &req->statbuf));
1746 X(FSYNC, uv__fs_fsync(req));
1747 X(FTRUNCATE, ftruncate(req->file, req->off));
1748 X(FUTIME, uv__fs_futime(req));
1749 X(LUTIME, uv__fs_lutime(req));
1750 X(LSTAT, uv__fs_lstat(req->path, &req->statbuf));
1751 X(LINK, link(req->path, req->new_path));
1752 X(MKDIR, mkdir(req->path, req->mode));
1753 X(MKDTEMP, uv__fs_mkdtemp(req));
1754 X(MKSTEMP, uv__fs_mkstemp(req));
1755 X(OPEN, uv__fs_open(req));
1756 X(READ, uv__fs_read(req));
1757 X(SCANDIR, uv__fs_scandir(req));
1758 X(OPENDIR, uv__fs_opendir(req));
1759 X(READDIR, uv__fs_readdir(req));
1760 X(CLOSEDIR, uv__fs_closedir(req));
1761 X(READLINK, uv__fs_readlink(req));
1762 X(REALPATH, uv__fs_realpath(req));
1763 X(RENAME, rename(req->path, req->new_path));
1764 X(RMDIR, rmdir(req->path));
1765 X(SENDFILE, uv__fs_sendfile(req));
1766 X(STAT, uv__fs_stat(req->path, &req->statbuf));
1767 X(STATFS, uv__fs_statfs(req));
1768 X(SYMLINK, symlink(req->path, req->new_path));
1769 X(UNLINK, unlink(req->path));
1770 X(UTIME, uv__fs_utime(req));
1771 X(WRITE, uv__fs_write_all(req));
1772 default: abort();
1773 }
1774 #undef X
1775 } while (r == -1 && errno == EINTR && retry_on_eintr);
1776
1777 if (r == -1)
1778 req->result = UV__ERR(errno);
1779 else
1780 req->result = r;
1781
1782 if (r == 0 && (req->fs_type == UV_FS_STAT ||
1783 req->fs_type == UV_FS_FSTAT ||
1784 req->fs_type == UV_FS_LSTAT)) {
1785 req->ptr = &req->statbuf;
1786 }
1787 }
1788
1789
uv__fs_done(struct uv__work * w,int status)1790 static void uv__fs_done(struct uv__work* w, int status) {
1791 uv_fs_t* req;
1792
1793 req = container_of(w, uv_fs_t, work_req);
1794 uv__req_unregister(req->loop, req);
1795
1796 if (status == UV_ECANCELED) {
1797 assert(req->result == 0);
1798 req->result = UV_ECANCELED;
1799 }
1800
1801 req->cb(req);
1802 }
1803
1804
uv_fs_access(uv_loop_t * loop,uv_fs_t * req,const char * path,int flags,uv_fs_cb cb)1805 int uv_fs_access(uv_loop_t* loop,
1806 uv_fs_t* req,
1807 const char* path,
1808 int flags,
1809 uv_fs_cb cb) {
1810 INIT(ACCESS);
1811 PATH;
1812 req->flags = flags;
1813 POST;
1814 }
1815
1816
uv_fs_chmod(uv_loop_t * loop,uv_fs_t * req,const char * path,int mode,uv_fs_cb cb)1817 int uv_fs_chmod(uv_loop_t* loop,
1818 uv_fs_t* req,
1819 const char* path,
1820 int mode,
1821 uv_fs_cb cb) {
1822 INIT(CHMOD);
1823 PATH;
1824 req->mode = mode;
1825 POST;
1826 }
1827
1828
uv_fs_chown(uv_loop_t * loop,uv_fs_t * req,const char * path,uv_uid_t uid,uv_gid_t gid,uv_fs_cb cb)1829 int uv_fs_chown(uv_loop_t* loop,
1830 uv_fs_t* req,
1831 const char* path,
1832 uv_uid_t uid,
1833 uv_gid_t gid,
1834 uv_fs_cb cb) {
1835 INIT(CHOWN);
1836 PATH;
1837 req->uid = uid;
1838 req->gid = gid;
1839 POST;
1840 }
1841
1842
uv_fs_close(uv_loop_t * loop,uv_fs_t * req,uv_file file,uv_fs_cb cb)1843 int uv_fs_close(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
1844 INIT(CLOSE);
1845 req->file = file;
1846 POST;
1847 }
1848
1849
uv_fs_fchmod(uv_loop_t * loop,uv_fs_t * req,uv_file file,int mode,uv_fs_cb cb)1850 int uv_fs_fchmod(uv_loop_t* loop,
1851 uv_fs_t* req,
1852 uv_file file,
1853 int mode,
1854 uv_fs_cb cb) {
1855 INIT(FCHMOD);
1856 req->file = file;
1857 req->mode = mode;
1858 POST;
1859 }
1860
1861
uv_fs_fchown(uv_loop_t * loop,uv_fs_t * req,uv_file file,uv_uid_t uid,uv_gid_t gid,uv_fs_cb cb)1862 int uv_fs_fchown(uv_loop_t* loop,
1863 uv_fs_t* req,
1864 uv_file file,
1865 uv_uid_t uid,
1866 uv_gid_t gid,
1867 uv_fs_cb cb) {
1868 INIT(FCHOWN);
1869 req->file = file;
1870 req->uid = uid;
1871 req->gid = gid;
1872 POST;
1873 }
1874
1875
uv_fs_lchown(uv_loop_t * loop,uv_fs_t * req,const char * path,uv_uid_t uid,uv_gid_t gid,uv_fs_cb cb)1876 int uv_fs_lchown(uv_loop_t* loop,
1877 uv_fs_t* req,
1878 const char* path,
1879 uv_uid_t uid,
1880 uv_gid_t gid,
1881 uv_fs_cb cb) {
1882 INIT(LCHOWN);
1883 PATH;
1884 req->uid = uid;
1885 req->gid = gid;
1886 POST;
1887 }
1888
1889
uv_fs_fdatasync(uv_loop_t * loop,uv_fs_t * req,uv_file file,uv_fs_cb cb)1890 int uv_fs_fdatasync(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
1891 INIT(FDATASYNC);
1892 req->file = file;
1893 POST;
1894 }
1895
1896
uv_fs_fstat(uv_loop_t * loop,uv_fs_t * req,uv_file file,uv_fs_cb cb)1897 int uv_fs_fstat(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
1898 INIT(FSTAT);
1899 req->file = file;
1900 POST;
1901 }
1902
1903
uv_fs_fsync(uv_loop_t * loop,uv_fs_t * req,uv_file file,uv_fs_cb cb)1904 int uv_fs_fsync(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
1905 INIT(FSYNC);
1906 req->file = file;
1907 POST;
1908 }
1909
1910
uv_fs_ftruncate(uv_loop_t * loop,uv_fs_t * req,uv_file file,int64_t off,uv_fs_cb cb)1911 int uv_fs_ftruncate(uv_loop_t* loop,
1912 uv_fs_t* req,
1913 uv_file file,
1914 int64_t off,
1915 uv_fs_cb cb) {
1916 INIT(FTRUNCATE);
1917 req->file = file;
1918 req->off = off;
1919 POST;
1920 }
1921
1922
uv_fs_futime(uv_loop_t * loop,uv_fs_t * req,uv_file file,double atime,double mtime,uv_fs_cb cb)1923 int uv_fs_futime(uv_loop_t* loop,
1924 uv_fs_t* req,
1925 uv_file file,
1926 double atime,
1927 double mtime,
1928 uv_fs_cb cb) {
1929 INIT(FUTIME);
1930 req->file = file;
1931 req->atime = atime;
1932 req->mtime = mtime;
1933 POST;
1934 }
1935
uv_fs_lutime(uv_loop_t * loop,uv_fs_t * req,const char * path,double atime,double mtime,uv_fs_cb cb)1936 int uv_fs_lutime(uv_loop_t* loop,
1937 uv_fs_t* req,
1938 const char* path,
1939 double atime,
1940 double mtime,
1941 uv_fs_cb cb) {
1942 INIT(LUTIME);
1943 PATH;
1944 req->atime = atime;
1945 req->mtime = mtime;
1946 POST;
1947 }
1948
1949
uv_fs_lstat(uv_loop_t * loop,uv_fs_t * req,const char * path,uv_fs_cb cb)1950 int uv_fs_lstat(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
1951 INIT(LSTAT);
1952 PATH;
1953 POST;
1954 }
1955
1956
uv_fs_link(uv_loop_t * loop,uv_fs_t * req,const char * path,const char * new_path,uv_fs_cb cb)1957 int uv_fs_link(uv_loop_t* loop,
1958 uv_fs_t* req,
1959 const char* path,
1960 const char* new_path,
1961 uv_fs_cb cb) {
1962 INIT(LINK);
1963 PATH2;
1964 POST;
1965 }
1966
1967
uv_fs_mkdir(uv_loop_t * loop,uv_fs_t * req,const char * path,int mode,uv_fs_cb cb)1968 int uv_fs_mkdir(uv_loop_t* loop,
1969 uv_fs_t* req,
1970 const char* path,
1971 int mode,
1972 uv_fs_cb cb) {
1973 INIT(MKDIR);
1974 PATH;
1975 req->mode = mode;
1976 POST;
1977 }
1978
1979
uv_fs_mkdtemp(uv_loop_t * loop,uv_fs_t * req,const char * tpl,uv_fs_cb cb)1980 int uv_fs_mkdtemp(uv_loop_t* loop,
1981 uv_fs_t* req,
1982 const char* tpl,
1983 uv_fs_cb cb) {
1984 INIT(MKDTEMP);
1985 req->path = uv__strdup(tpl);
1986 if (req->path == NULL)
1987 return UV_ENOMEM;
1988 POST;
1989 }
1990
1991
uv_fs_mkstemp(uv_loop_t * loop,uv_fs_t * req,const char * tpl,uv_fs_cb cb)1992 int uv_fs_mkstemp(uv_loop_t* loop,
1993 uv_fs_t* req,
1994 const char* tpl,
1995 uv_fs_cb cb) {
1996 INIT(MKSTEMP);
1997 req->path = uv__strdup(tpl);
1998 if (req->path == NULL)
1999 return UV_ENOMEM;
2000 POST;
2001 }
2002
2003
uv_fs_open(uv_loop_t * loop,uv_fs_t * req,const char * path,int flags,int mode,uv_fs_cb cb)2004 int uv_fs_open(uv_loop_t* loop,
2005 uv_fs_t* req,
2006 const char* path,
2007 int flags,
2008 int mode,
2009 uv_fs_cb cb) {
2010 INIT(OPEN);
2011 PATH;
2012 req->flags = flags;
2013 req->mode = mode;
2014 POST;
2015 }
2016
2017
uv_fs_read(uv_loop_t * loop,uv_fs_t * req,uv_file file,const uv_buf_t bufs[],unsigned int nbufs,int64_t off,uv_fs_cb cb)2018 int uv_fs_read(uv_loop_t* loop, uv_fs_t* req,
2019 uv_file file,
2020 const uv_buf_t bufs[],
2021 unsigned int nbufs,
2022 int64_t off,
2023 uv_fs_cb cb) {
2024 INIT(READ);
2025
2026 if (bufs == NULL || nbufs == 0)
2027 return UV_EINVAL;
2028
2029 req->file = file;
2030
2031 req->nbufs = nbufs;
2032 req->bufs = req->bufsml;
2033 if (nbufs > ARRAY_SIZE(req->bufsml))
2034 req->bufs = uv__malloc(nbufs * sizeof(*bufs));
2035
2036 if (req->bufs == NULL)
2037 return UV_ENOMEM;
2038
2039 memcpy(req->bufs, bufs, nbufs * sizeof(*bufs));
2040
2041 req->off = off;
2042 POST;
2043 }
2044
2045
uv_fs_scandir(uv_loop_t * loop,uv_fs_t * req,const char * path,int flags,uv_fs_cb cb)2046 int uv_fs_scandir(uv_loop_t* loop,
2047 uv_fs_t* req,
2048 const char* path,
2049 int flags,
2050 uv_fs_cb cb) {
2051 INIT(SCANDIR);
2052 PATH;
2053 req->flags = flags;
2054 POST;
2055 }
2056
uv_fs_opendir(uv_loop_t * loop,uv_fs_t * req,const char * path,uv_fs_cb cb)2057 int uv_fs_opendir(uv_loop_t* loop,
2058 uv_fs_t* req,
2059 const char* path,
2060 uv_fs_cb cb) {
2061 INIT(OPENDIR);
2062 PATH;
2063 POST;
2064 }
2065
uv_fs_readdir(uv_loop_t * loop,uv_fs_t * req,uv_dir_t * dir,uv_fs_cb cb)2066 int uv_fs_readdir(uv_loop_t* loop,
2067 uv_fs_t* req,
2068 uv_dir_t* dir,
2069 uv_fs_cb cb) {
2070 INIT(READDIR);
2071
2072 if (dir == NULL || dir->dir == NULL || dir->dirents == NULL)
2073 return UV_EINVAL;
2074
2075 req->ptr = dir;
2076 POST;
2077 }
2078
uv_fs_closedir(uv_loop_t * loop,uv_fs_t * req,uv_dir_t * dir,uv_fs_cb cb)2079 int uv_fs_closedir(uv_loop_t* loop,
2080 uv_fs_t* req,
2081 uv_dir_t* dir,
2082 uv_fs_cb cb) {
2083 INIT(CLOSEDIR);
2084
2085 if (dir == NULL)
2086 return UV_EINVAL;
2087
2088 req->ptr = dir;
2089 POST;
2090 }
2091
uv_fs_readlink(uv_loop_t * loop,uv_fs_t * req,const char * path,uv_fs_cb cb)2092 int uv_fs_readlink(uv_loop_t* loop,
2093 uv_fs_t* req,
2094 const char* path,
2095 uv_fs_cb cb) {
2096 INIT(READLINK);
2097 PATH;
2098 POST;
2099 }
2100
2101
uv_fs_realpath(uv_loop_t * loop,uv_fs_t * req,const char * path,uv_fs_cb cb)2102 int uv_fs_realpath(uv_loop_t* loop,
2103 uv_fs_t* req,
2104 const char * path,
2105 uv_fs_cb cb) {
2106 INIT(REALPATH);
2107 PATH;
2108 POST;
2109 }
2110
2111
uv_fs_rename(uv_loop_t * loop,uv_fs_t * req,const char * path,const char * new_path,uv_fs_cb cb)2112 int uv_fs_rename(uv_loop_t* loop,
2113 uv_fs_t* req,
2114 const char* path,
2115 const char* new_path,
2116 uv_fs_cb cb) {
2117 INIT(RENAME);
2118 PATH2;
2119 POST;
2120 }
2121
2122
uv_fs_rmdir(uv_loop_t * loop,uv_fs_t * req,const char * path,uv_fs_cb cb)2123 int uv_fs_rmdir(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
2124 INIT(RMDIR);
2125 PATH;
2126 POST;
2127 }
2128
2129
uv_fs_sendfile(uv_loop_t * loop,uv_fs_t * req,uv_file out_fd,uv_file in_fd,int64_t off,size_t len,uv_fs_cb cb)2130 int uv_fs_sendfile(uv_loop_t* loop,
2131 uv_fs_t* req,
2132 uv_file out_fd,
2133 uv_file in_fd,
2134 int64_t off,
2135 size_t len,
2136 uv_fs_cb cb) {
2137 INIT(SENDFILE);
2138 req->flags = in_fd; /* hack */
2139 req->file = out_fd;
2140 req->off = off;
2141 req->bufsml[0].len = len;
2142 POST;
2143 }
2144
2145
uv_fs_stat(uv_loop_t * loop,uv_fs_t * req,const char * path,uv_fs_cb cb)2146 int uv_fs_stat(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
2147 INIT(STAT);
2148 PATH;
2149 POST;
2150 }
2151
2152
uv_fs_symlink(uv_loop_t * loop,uv_fs_t * req,const char * path,const char * new_path,int flags,uv_fs_cb cb)2153 int uv_fs_symlink(uv_loop_t* loop,
2154 uv_fs_t* req,
2155 const char* path,
2156 const char* new_path,
2157 int flags,
2158 uv_fs_cb cb) {
2159 INIT(SYMLINK);
2160 PATH2;
2161 req->flags = flags;
2162 POST;
2163 }
2164
2165
uv_fs_unlink(uv_loop_t * loop,uv_fs_t * req,const char * path,uv_fs_cb cb)2166 int uv_fs_unlink(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
2167 INIT(UNLINK);
2168 PATH;
2169 POST;
2170 }
2171
2172
uv_fs_utime(uv_loop_t * loop,uv_fs_t * req,const char * path,double atime,double mtime,uv_fs_cb cb)2173 int uv_fs_utime(uv_loop_t* loop,
2174 uv_fs_t* req,
2175 const char* path,
2176 double atime,
2177 double mtime,
2178 uv_fs_cb cb) {
2179 INIT(UTIME);
2180 PATH;
2181 req->atime = atime;
2182 req->mtime = mtime;
2183 POST;
2184 }
2185
2186
uv_fs_write(uv_loop_t * loop,uv_fs_t * req,uv_file file,const uv_buf_t bufs[],unsigned int nbufs,int64_t off,uv_fs_cb cb)2187 int uv_fs_write(uv_loop_t* loop,
2188 uv_fs_t* req,
2189 uv_file file,
2190 const uv_buf_t bufs[],
2191 unsigned int nbufs,
2192 int64_t off,
2193 uv_fs_cb cb) {
2194 INIT(WRITE);
2195
2196 if (bufs == NULL || nbufs == 0)
2197 return UV_EINVAL;
2198
2199 req->file = file;
2200
2201 req->nbufs = nbufs;
2202 req->bufs = req->bufsml;
2203 if (nbufs > ARRAY_SIZE(req->bufsml))
2204 req->bufs = uv__malloc(nbufs * sizeof(*bufs));
2205
2206 if (req->bufs == NULL)
2207 return UV_ENOMEM;
2208
2209 memcpy(req->bufs, bufs, nbufs * sizeof(*bufs));
2210
2211 req->off = off;
2212 POST;
2213 }
2214
2215
uv_fs_req_cleanup(uv_fs_t * req)2216 void uv_fs_req_cleanup(uv_fs_t* req) {
2217 if (req == NULL)
2218 return;
2219
2220 /* Only necessary for asychronous requests, i.e., requests with a callback.
2221 * Synchronous ones don't copy their arguments and have req->path and
2222 * req->new_path pointing to user-owned memory. UV_FS_MKDTEMP and
2223 * UV_FS_MKSTEMP are the exception to the rule, they always allocate memory.
2224 */
2225 if (req->path != NULL &&
2226 (req->cb != NULL ||
2227 req->fs_type == UV_FS_MKDTEMP || req->fs_type == UV_FS_MKSTEMP))
2228 uv__free((void*) req->path); /* Memory is shared with req->new_path. */
2229
2230 req->path = NULL;
2231 req->new_path = NULL;
2232
2233 if (req->fs_type == UV_FS_READDIR && req->ptr != NULL)
2234 uv__fs_readdir_cleanup(req);
2235
2236 if (req->fs_type == UV_FS_SCANDIR && req->ptr != NULL)
2237 uv__fs_scandir_cleanup(req);
2238
2239 if (req->bufs != req->bufsml)
2240 uv__free(req->bufs);
2241 req->bufs = NULL;
2242
2243 if (req->fs_type != UV_FS_OPENDIR && req->ptr != &req->statbuf)
2244 uv__free(req->ptr);
2245 req->ptr = NULL;
2246 }
2247
2248
uv_fs_copyfile(uv_loop_t * loop,uv_fs_t * req,const char * path,const char * new_path,int flags,uv_fs_cb cb)2249 int uv_fs_copyfile(uv_loop_t* loop,
2250 uv_fs_t* req,
2251 const char* path,
2252 const char* new_path,
2253 int flags,
2254 uv_fs_cb cb) {
2255 INIT(COPYFILE);
2256
2257 if (flags & ~(UV_FS_COPYFILE_EXCL |
2258 UV_FS_COPYFILE_FICLONE |
2259 UV_FS_COPYFILE_FICLONE_FORCE)) {
2260 return UV_EINVAL;
2261 }
2262
2263 PATH2;
2264 req->flags = flags;
2265 POST;
2266 }
2267
2268
uv_fs_statfs(uv_loop_t * loop,uv_fs_t * req,const char * path,uv_fs_cb cb)2269 int uv_fs_statfs(uv_loop_t* loop,
2270 uv_fs_t* req,
2271 const char* path,
2272 uv_fs_cb cb) {
2273 INIT(STATFS);
2274 PATH;
2275 POST;
2276 }
2277
uv_fs_get_system_error(const uv_fs_t * req)2278 int uv_fs_get_system_error(const uv_fs_t* req) {
2279 return -req->result;
2280 }
2281