1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
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.)
37 #include <limits.h> /* PATH_MAX */
39 #include <sys/types.h>
40 #include <sys/socket.h>
49 #if defined(__DragonFly__) || \
50 defined(__FreeBSD__) || \
51 defined(__FreeBSD_kernel__) || \
52 defined(__OpenBSD__) || \
54 # define HAVE_PREADV 1
56 # define HAVE_PREADV 0
59 #if defined(__linux__)
60 # include "sys/utsname.h"
63 #if defined(__linux__) || defined(__sun)
64 # include <sys/sendfile.h>
65 # include <sys/sysmacros.h>
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)
75 #if defined(_AIX) && !defined(_AIX71)
79 #if defined(__APPLE__) || \
80 defined(__DragonFly__) || \
81 defined(__FreeBSD__) || \
82 defined(__FreeBSD_kernel__) || \
83 defined(__OpenBSD__) || \
85 # include <sys/param.h>
86 # include <sys/mount.h>
87 #elif defined(__sun) || \
89 defined(__NetBSD__) || \
90 defined(__HAIKU__) || \
92 # include <sys/statvfs.h>
94 # include <sys/statfs.h>
97 #if defined(_AIX) && _XOPEN_SOURCE <= 600
98 extern char *mkdtemp(char *template); /* See issue #740 on AIX < 7 */
101 #define INIT(subtype) \
105 UV_REQ_INIT(req, UV_FS); \
106 req->fs_type = UV_FS_ ## subtype; \
111 req->new_path = NULL; \
119 assert(path != NULL); \
123 req->path = uv__strdup(path); \
124 if (req->path == NULL) \
134 req->new_path = new_path; \
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) \
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); \
153 uv__req_register(loop, req); \
154 uv__work_submit(loop, \
162 uv__fs_work(&req->work_req); \
163 return req->result; \
169 static int uv__fs_close(int fd) {
172 rc = uv__close_nocancel(fd);
174 if (errno == EINTR || errno == EINPROGRESS)
175 rc = 0; /* The close is in progress, not an error. */
181 static ssize_t uv__fs_fsync(uv_fs_t* req) {
182 #if defined(__APPLE__)
183 /* Apple's fdatasync and fsync explicitly do NOT flush the drive write cache
184 * to the drive platters. This is in contrast to Linux's fdatasync and fsync
185 * which do, according to recent man pages. F_FULLFSYNC is Apple's equivalent
186 * for flushing buffered data to permanent storage. If F_FULLFSYNC is not
187 * supported by the file system we fall back to F_BARRIERFSYNC or fsync().
188 * This is the same approach taken by sqlite, except sqlite does not issue
189 * an F_BARRIERFSYNC call.
193 r = fcntl(req->file, F_FULLFSYNC);
195 r = fcntl(req->file, 85 /* F_BARRIERFSYNC */); /* fsync + barrier */
197 r = fsync(req->file);
200 return fsync(req->file);
205 static ssize_t uv__fs_fdatasync(uv_fs_t* req) {
206 #if defined(__linux__) || defined(__sun) || defined(__NetBSD__)
207 return fdatasync(req->file);
208 #elif defined(__APPLE__)
209 /* See the comment in uv__fs_fsync. */
210 return uv__fs_fsync(req);
212 return fsync(req->file);
217 UV_UNUSED(static struct timespec uv__fs_to_timespec(double time)) {
220 ts.tv_nsec = (time - ts.tv_sec) * 1e9;
222 /* TODO(bnoordhuis) Remove this. utimesat() has nanosecond resolution but we
223 * stick to microsecond resolution for the sake of consistency with other
224 * platforms. I'm the original author of this compatibility hack but I'm
225 * less convinced it's useful nowadays.
227 ts.tv_nsec -= ts.tv_nsec % 1000;
229 if (ts.tv_nsec < 0) {
236 UV_UNUSED(static struct timeval uv__fs_to_timeval(double time)) {
239 tv.tv_usec = (time - tv.tv_sec) * 1e6;
240 if (tv.tv_usec < 0) {
247 static ssize_t uv__fs_futime(uv_fs_t* req) {
248 #if defined(__linux__) \
250 || defined(__HAIKU__) \
252 struct timespec ts[2];
253 ts[0] = uv__fs_to_timespec(req->atime);
254 ts[1] = uv__fs_to_timespec(req->mtime);
255 return futimens(req->file, ts);
256 #elif defined(__APPLE__) \
257 || defined(__DragonFly__) \
258 || defined(__FreeBSD__) \
259 || defined(__FreeBSD_kernel__) \
260 || defined(__NetBSD__) \
261 || defined(__OpenBSD__) \
263 struct timeval tv[2];
264 tv[0] = uv__fs_to_timeval(req->atime);
265 tv[1] = uv__fs_to_timeval(req->mtime);
267 return futimesat(req->file, NULL, tv);
269 return futimes(req->file, tv);
271 #elif defined(__MVS__)
273 memset(&atr, 0, sizeof(atr));
274 atr.att_mtimechg = 1;
275 atr.att_atimechg = 1;
276 atr.att_mtime = req->mtime;
277 atr.att_atime = req->atime;
278 return __fchattr(req->file, &atr, sizeof(atr));
285 #if (defined(__sun) || defined(__hpux)) && (_XOPEN_SOURCE < 600 || defined(CMAKE_BOOTSTRAP))
286 static char* uv__mkdtemp(char *template)
288 if (!mktemp(template) || mkdir(template, 0700))
293 #define uv__mkdtemp mkdtemp
296 static ssize_t uv__fs_mkdtemp(uv_fs_t* req) {
297 return uv__mkdtemp((char*) req->path) ? 0 : -1;
301 static int (*uv__mkostemp)(char*, int);
304 static void uv__mkostemp_initonce(void) {
305 /* z/os doesn't have RTLD_DEFAULT but that's okay
306 * because it doesn't have mkostemp(O_CLOEXEC) either.
309 uv__mkostemp = (int (*)(char*, int)) dlsym(RTLD_DEFAULT, "mkostemp");
311 /* We don't care about errors, but we do want to clean them up.
312 * If there has been no error, then dlerror() will just return
316 #endif /* RTLD_DEFAULT */
320 static int uv__fs_mkstemp(uv_fs_t* req) {
321 static uv_once_t once = UV_ONCE_INIT;
324 static int no_cloexec_support;
326 static const char pattern[] = "XXXXXX";
327 static const size_t pattern_size = sizeof(pattern) - 1;
331 path = (char*) req->path;
332 path_length = strlen(path);
334 /* EINVAL can be returned for 2 reasons:
335 1. The template's last 6 characters were not XXXXXX
336 2. open() didn't support O_CLOEXEC
337 We want to avoid going to the fallback path in case
338 of 1, so it's manually checked before. */
339 if (path_length < pattern_size ||
340 strcmp(path + path_length - pattern_size, pattern)) {
346 uv_once(&once, uv__mkostemp_initonce);
349 if (uv__load_relaxed(&no_cloexec_support) == 0 && uv__mkostemp != NULL) {
350 r = uv__mkostemp(path, O_CLOEXEC);
355 /* If mkostemp() returns EINVAL, it means the kernel doesn't
356 support O_CLOEXEC, so we just fallback to mkstemp() below. */
360 /* We set the static variable so that next calls don't even
361 try to use mkostemp. */
362 uv__store_relaxed(&no_cloexec_support, 1);
364 #endif /* O_CLOEXEC */
367 uv_rwlock_rdlock(&req->loop->cloexec_lock);
371 /* In case of failure `uv__cloexec` will leave error in `errno`,
372 * so it is enough to just set `r` to `-1`.
374 if (r >= 0 && uv__cloexec(r, 1) != 0) {
382 uv_rwlock_rdunlock(&req->loop->cloexec_lock);
391 static ssize_t uv__fs_open(uv_fs_t* req) {
393 return open(req->path, req->flags | O_CLOEXEC, req->mode);
394 #else /* O_CLOEXEC */
398 uv_rwlock_rdlock(&req->loop->cloexec_lock);
400 r = open(req->path, req->flags, req->mode);
402 /* In case of failure `uv__cloexec` will leave error in `errno`,
403 * so it is enough to just set `r` to `-1`.
405 if (r >= 0 && uv__cloexec(r, 1) != 0) {
413 uv_rwlock_rdunlock(&req->loop->cloexec_lock);
416 #endif /* O_CLOEXEC */
421 static ssize_t uv__fs_preadv(uv_file fd,
440 rc = pread(fd, buf->base + pos, buf->len - pos, off + result);
441 while (rc == -1 && errno == EINTR);
446 if (rc == -1 && result == 0)
447 return UV__ERR(errno);
450 break; /* We read some data so return that, ignore the error. */
470 static ssize_t uv__fs_read(uv_fs_t* req) {
471 #if defined(__linux__)
472 static int no_preadv;
477 iovmax = uv__getiovmax();
478 if (req->nbufs > iovmax)
483 result = read(req->file, req->bufs[0].base, req->bufs[0].len);
485 result = readv(req->file, (struct iovec*) req->bufs, req->nbufs);
487 if (req->nbufs == 1) {
488 result = pread(req->file, req->bufs[0].base, req->bufs[0].len, req->off);
493 result = preadv(req->file, (struct iovec*) req->bufs, req->nbufs, req->off);
495 # if defined(__linux__)
496 if (uv__load_relaxed(&no_preadv)) retry:
499 result = uv__fs_preadv(req->file, req->bufs, req->nbufs, req->off);
501 # if defined(__linux__)
503 result = uv__preadv(req->file,
504 (struct iovec*)req->bufs,
507 if (result == -1 && errno == ENOSYS) {
508 uv__store_relaxed(&no_preadv, 1);
517 /* Early cleanup of bufs allocation, since we're done with it. */
518 if (req->bufs != req->bufsml)
525 /* PASE returns EOPNOTSUPP when reading a directory, convert to EISDIR */
526 if (result == -1 && errno == EOPNOTSUPP) {
529 rc = fstat(req->file, &buf);
530 if (rc == 0 && S_ISDIR(buf.st_mode)) {
540 #if defined(__APPLE__) && !defined(MAC_OS_X_VERSION_10_8)
541 #define UV_CONST_DIRENT uv__dirent_t
543 #define UV_CONST_DIRENT const uv__dirent_t
547 static int uv__fs_scandir_filter(UV_CONST_DIRENT* dent) {
548 return strcmp(dent->d_name, ".") != 0 && strcmp(dent->d_name, "..") != 0;
552 static int uv__fs_scandir_sort(UV_CONST_DIRENT** a, UV_CONST_DIRENT** b) {
553 return strcmp((*a)->d_name, (*b)->d_name);
557 static ssize_t uv__fs_scandir(uv_fs_t* req) {
558 uv__dirent_t** dents;
562 n = scandir(req->path, &dents, uv__fs_scandir_filter, uv__fs_scandir_sort);
564 /* NOTE: We will use nbufs as an index field */
568 /* OS X still needs to deallocate some memory.
569 * Memory was allocated using the system allocator, so use free() here.
573 } else if (n == -1) {
582 static int uv__fs_opendir(uv_fs_t* req) {
585 dir = uv__malloc(sizeof(*dir));
589 dir->dir = opendir(req->path);
590 if (dir->dir == NULL)
602 static int uv__fs_readdir(uv_fs_t* req) {
606 unsigned int dirent_idx;
612 while (dirent_idx < dir->nentries) {
613 /* readdir() returns NULL on end of directory, as well as on error. errno
614 is used to differentiate between the two conditions. */
616 res = readdir(dir->dir);
624 if (strcmp(res->d_name, ".") == 0 || strcmp(res->d_name, "..") == 0)
627 dirent = &dir->dirents[dirent_idx];
628 dirent->name = uv__strdup(res->d_name);
630 if (dirent->name == NULL)
633 dirent->type = uv__fs_get_dirent_type(res);
640 for (i = 0; i < dirent_idx; ++i) {
641 uv__free((char*) dir->dirents[i].name);
642 dir->dirents[i].name = NULL;
648 static int uv__fs_closedir(uv_fs_t* req) {
653 if (dir->dir != NULL) {
663 static int uv__fs_statfs(uv_fs_t* req) {
664 uv_statfs_t* stat_fs;
665 #if defined(__sun) || \
666 defined(__MVS__) || \
667 defined(__NetBSD__) || \
668 defined(__HAIKU__) || \
672 if (0 != statvfs(req->path, &buf))
676 if (0 != statfs(req->path, &buf))
677 #endif /* defined(__sun) */
680 stat_fs = uv__malloc(sizeof(*stat_fs));
681 if (stat_fs == NULL) {
686 #if defined(__sun) || \
687 defined(__MVS__) || \
688 defined(__OpenBSD__) || \
689 defined(__NetBSD__) || \
690 defined(__HAIKU__) || \
692 stat_fs->f_type = 0; /* f_type is not supported. */
694 stat_fs->f_type = buf.f_type;
696 stat_fs->f_bsize = buf.f_bsize;
697 stat_fs->f_blocks = buf.f_blocks;
698 stat_fs->f_bfree = buf.f_bfree;
699 stat_fs->f_bavail = buf.f_bavail;
700 stat_fs->f_files = buf.f_files;
701 stat_fs->f_ffree = buf.f_ffree;
706 static ssize_t uv__fs_pathmax_size(const char* path) {
709 pathmax = pathconf(path, _PC_PATH_MAX);
712 pathmax = UV__PATH_MAX;
717 static ssize_t uv__fs_readlink(uv_fs_t* req) {
722 #if defined(_POSIX_PATH_MAX) || defined(PATH_MAX)
723 maxlen = uv__fs_pathmax_size(req->path);
725 /* We may not have a real PATH_MAX. Read size of link. */
728 ret = lstat(req->path, &st);
731 if (!S_ISLNK(st.st_mode)) {
738 /* According to readlink(2) lstat can report st_size == 0
739 for some symlinks, such as those in /proc or /sys. */
741 maxlen = uv__fs_pathmax_size(req->path);
744 buf = uv__malloc(maxlen);
752 len = os390_readlink(req->path, buf, maxlen);
754 len = readlink(req->path, buf, maxlen);
762 /* Uncommon case: resize to make room for the trailing nul byte. */
764 buf = uv__reallocf(buf, len + 1);
776 static ssize_t uv__fs_realpath(uv_fs_t* req) {
779 #if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L
780 buf = realpath(req->path, NULL);
786 len = uv__fs_pathmax_size(req->path);
787 buf = uv__malloc(len + 1);
794 if (realpath(req->path, buf) == NULL) {
805 static ssize_t uv__fs_sendfile_emul(uv_fs_t* req) {
819 len = req->bufsml[0].len;
825 /* Here are the rules regarding errors:
827 * 1. Read errors are reported only if nsent==0, otherwise we return nsent.
828 * The user needs to know that some data has already been sent, to stop
829 * them from sending it twice.
831 * 2. Write errors are always reported. Write errors are bad because they
832 * mean data loss: we've read data but now we can't write it out.
834 * We try to use pread() and fall back to regular read() if the source fd
835 * doesn't support positional reads, for example when it's a pipe fd.
837 * If we get EAGAIN when writing to the target fd, we poll() on it until
838 * it becomes writable again.
840 * FIXME: If we get a write error when use_pread==1, it should be safe to
841 * return the number of sent bytes instead of an error because pread()
842 * is, in theory, idempotent. However, special files in /dev or /proc
843 * may support pread() but not necessarily return the same data on
846 * FIXME: There is no way now to signal that we managed to send *some* data
847 * before a write error.
849 for (nsent = 0; (size_t) nsent < len; ) {
850 buflen = len - nsent;
852 if (buflen > sizeof(buf))
853 buflen = sizeof(buf);
857 nread = pread(in_fd, buf, buflen, offset);
859 nread = read(in_fd, buf, buflen);
860 while (nread == -1 && errno == EINTR);
866 if (use_pread && nsent == 0 && (errno == EIO || errno == ESPIPE)) {
877 for (nwritten = 0; nwritten < nread; ) {
879 n = write(out_fd, buf + nwritten, nread - nwritten);
880 while (n == -1 && errno == EINTR);
887 if (errno != EAGAIN && errno != EWOULDBLOCK) {
893 pfd.events = POLLOUT;
897 n = poll(&pfd, 1, -1);
898 while (n == -1 && errno == EINTR);
900 if (n == -1 || (pfd.revents & ~POLLOUT) != 0) {
920 static unsigned uv__kernel_version(void) {
921 static unsigned cached_version;
928 version = uv__load_relaxed(&cached_version);
935 if (3 != sscanf(u.release, "%u.%u.%u", &major, &minor, &patch))
938 version = major * 65536 + minor * 256 + patch;
939 uv__store_relaxed(&cached_version, version);
945 /* Pre-4.20 kernels have a bug where CephFS uses the RADOS copy-from command
946 * in copy_file_range() when it shouldn't. There is no workaround except to
947 * fall back to a regular copy.
949 static int uv__is_buggy_cephfs(int fd) {
952 if (-1 == fstatfs(fd, &s))
955 if (s.f_type != /* CephFS */ 0xC36400)
958 return uv__kernel_version() < /* 4.20.0 */ 0x041400;
962 static int uv__is_cifs_or_smb(int fd) {
965 if (-1 == fstatfs(fd, &s))
968 switch ((unsigned) s.f_type) {
969 case 0x0000517Bu: /* SMB */
970 case 0xFE534D42u: /* SMB2 */
971 case 0xFF534D42u: /* CIFS */
979 static ssize_t uv__fs_try_copy_file_range(int in_fd, off_t* off,
980 int out_fd, size_t len) {
981 static int no_copy_file_range_support;
984 if (uv__load_relaxed(&no_copy_file_range_support)) {
989 r = uv__fs_copy_file_range(in_fd, off, out_fd, NULL, len, 0);
996 /* Pre-4.20 kernels have a bug where CephFS uses the RADOS
997 * copy-from command when it shouldn't.
999 if (uv__is_buggy_cephfs(in_fd))
1000 errno = ENOSYS; /* Use fallback. */
1003 uv__store_relaxed(&no_copy_file_range_support, 1);
1006 /* It's been reported that CIFS spuriously fails.
1007 * Consider it a transient error.
1009 if (uv__is_cifs_or_smb(out_fd))
1010 errno = ENOSYS; /* Use fallback. */
1014 /* ENOTSUP - it could work on another file system type.
1015 * EXDEV - it will not work when in_fd and out_fd are not on the same
1016 * mounted filesystem (pre Linux 5.3)
1018 errno = ENOSYS; /* Use fallback. */
1025 #endif /* __linux__ */
1028 static ssize_t uv__fs_sendfile(uv_fs_t* req) {
1035 #if defined(__linux__) || defined(__sun)
1043 len = req->bufsml[0].len;
1046 r = uv__fs_try_copy_file_range(in_fd, &off, out_fd, len);
1047 try_sendfile = (r == -1 && errno == ENOSYS);
1053 r = sendfile(out_fd, in_fd, &off, len);
1055 /* sendfile() on SunOS returns EINVAL if the target fd is not a socket but
1056 * it still writes out data. Fortunately, we can detect it by checking if
1057 * the offset has been updated.
1059 if (r != -1 || off > req->off) {
1065 if (errno == EINVAL ||
1067 errno == ENOTSOCK ||
1070 return uv__fs_sendfile_emul(req);
1075 #elif defined(__APPLE__) || \
1076 defined(__DragonFly__) || \
1077 defined(__FreeBSD__) || \
1078 defined(__FreeBSD_kernel__)
1083 /* sendfile() on FreeBSD and Darwin returns EAGAIN if the target fd is in
1084 * non-blocking mode and not all data could be written. If a non-zero
1085 * number of bytes have been sent, we don't consider it an error.
1088 #if defined(__FreeBSD__) || defined(__DragonFly__)
1089 #if defined(__FreeBSD__)
1093 r = uv__fs_copy_file_range(in_fd, &off, out_fd, NULL, req->bufsml[0].len, 0);
1101 r = sendfile(in_fd, out_fd, req->off, req->bufsml[0].len, NULL, &len, 0);
1102 #elif defined(__FreeBSD_kernel__)
1104 r = bsd_sendfile(in_fd,
1112 /* The darwin sendfile takes len as an input for the length to send,
1113 * so make sure to initialize it with the caller's value. */
1114 len = req->bufsml[0].len;
1115 r = sendfile(in_fd, out_fd, req->off, &len, NULL, 0);
1119 * The man page for sendfile(2) on DragonFly states that `len` contains
1120 * a meaningful value ONLY in case of EAGAIN and EINTR.
1121 * Nothing is said about it's value in case of other errors, so better
1122 * not depend on the potential wrong assumption that is was not modified
1125 if (r == 0 || ((errno == EAGAIN || errno == EINTR) && len != 0)) {
1127 return (ssize_t) len;
1130 if (errno == EINVAL ||
1132 errno == ENOTSOCK ||
1135 return uv__fs_sendfile_emul(req);
1141 /* Squelch compiler warnings. */
1145 return uv__fs_sendfile_emul(req);
1150 static ssize_t uv__fs_utime(uv_fs_t* req) {
1151 #if defined(__linux__) \
1152 || defined(_AIX71) \
1154 || defined(__HAIKU__)
1155 struct timespec ts[2];
1156 ts[0] = uv__fs_to_timespec(req->atime);
1157 ts[1] = uv__fs_to_timespec(req->mtime);
1158 return utimensat(AT_FDCWD, req->path, ts, 0);
1159 #elif defined(__APPLE__) \
1160 || defined(__DragonFly__) \
1161 || defined(__FreeBSD__) \
1162 || defined(__FreeBSD_kernel__) \
1163 || defined(__NetBSD__) \
1164 || defined(__OpenBSD__)
1165 struct timeval tv[2];
1166 tv[0] = uv__fs_to_timeval(req->atime);
1167 tv[1] = uv__fs_to_timeval(req->mtime);
1168 return utimes(req->path, tv);
1169 #elif defined(_AIX) \
1172 buf.actime = req->atime;
1173 buf.modtime = req->mtime;
1174 return utime(req->path, &buf);
1175 #elif defined(__MVS__)
1177 memset(&atr, 0, sizeof(atr));
1178 atr.att_mtimechg = 1;
1179 atr.att_atimechg = 1;
1180 atr.att_mtime = req->mtime;
1181 atr.att_atime = req->atime;
1182 return __lchattr((char*) req->path, &atr, sizeof(atr));
1190 static ssize_t uv__fs_lutime(uv_fs_t* req) {
1191 #if defined(__linux__) || \
1192 defined(_AIX71) || \
1194 defined(__HAIKU__) || \
1195 defined(__GNU__) || \
1196 defined(__OpenBSD__)
1197 struct timespec ts[2];
1198 ts[0] = uv__fs_to_timespec(req->atime);
1199 ts[1] = uv__fs_to_timespec(req->mtime);
1200 return utimensat(AT_FDCWD, req->path, ts, AT_SYMLINK_NOFOLLOW);
1201 #elif defined(__APPLE__) || \
1202 defined(__DragonFly__) || \
1203 defined(__FreeBSD__) || \
1204 defined(__FreeBSD_kernel__) || \
1206 struct timeval tv[2];
1207 tv[0] = uv__fs_to_timeval(req->atime);
1208 tv[1] = uv__fs_to_timeval(req->mtime);
1209 return lutimes(req->path, tv);
1217 static ssize_t uv__fs_write(uv_fs_t* req) {
1218 #if defined(__linux__)
1219 static int no_pwritev;
1223 /* Serialize writes on OS X, concurrent write() and pwrite() calls result in
1224 * data loss. We can't use a per-file descriptor lock, the descriptor may be
1227 #if defined(__APPLE__)
1228 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
1230 if (pthread_mutex_lock(&lock))
1235 if (req->nbufs == 1)
1236 r = write(req->file, req->bufs[0].base, req->bufs[0].len);
1238 r = writev(req->file, (struct iovec*) req->bufs, req->nbufs);
1240 if (req->nbufs == 1) {
1241 r = pwrite(req->file, req->bufs[0].base, req->bufs[0].len, req->off);
1245 r = pwritev(req->file, (struct iovec*) req->bufs, req->nbufs, req->off);
1247 # if defined(__linux__)
1248 if (no_pwritev) retry:
1251 r = pwrite(req->file, req->bufs[0].base, req->bufs[0].len, req->off);
1253 # if defined(__linux__)
1255 r = uv__pwritev(req->file,
1256 (struct iovec*) req->bufs,
1259 if (r == -1 && errno == ENOSYS) {
1269 #if defined(__APPLE__)
1270 if (pthread_mutex_unlock(&lock))
1277 static ssize_t uv__fs_copyfile(uv_fs_t* req) {
1281 struct stat src_statsbuf;
1282 struct stat dst_statsbuf;
1286 off_t bytes_to_send;
1288 off_t bytes_written;
1294 /* Open the source file. */
1295 srcfd = uv_fs_open(NULL, &fs_req, req->path, O_RDONLY, 0, NULL);
1296 uv_fs_req_cleanup(&fs_req);
1301 /* Get the source file's mode. */
1302 if (fstat(srcfd, &src_statsbuf)) {
1303 err = UV__ERR(errno);
1307 dst_flags = O_WRONLY | O_CREAT;
1309 if (req->flags & UV_FS_COPYFILE_EXCL)
1310 dst_flags |= O_EXCL;
1312 /* Open the destination file. */
1313 dstfd = uv_fs_open(NULL,
1317 src_statsbuf.st_mode,
1319 uv_fs_req_cleanup(&fs_req);
1326 /* If the file is not being opened exclusively, verify that the source and
1327 destination are not the same file. If they are the same, bail out early. */
1328 if ((req->flags & UV_FS_COPYFILE_EXCL) == 0) {
1329 /* Get the destination file's mode. */
1330 if (fstat(dstfd, &dst_statsbuf)) {
1331 err = UV__ERR(errno);
1335 /* Check if srcfd and dstfd refer to the same file */
1336 if (src_statsbuf.st_dev == dst_statsbuf.st_dev &&
1337 src_statsbuf.st_ino == dst_statsbuf.st_ino) {
1341 /* Truncate the file in case the destination already existed. */
1342 if (ftruncate(dstfd, 0) != 0) {
1343 err = UV__ERR(errno);
1348 if (fchmod(dstfd, src_statsbuf.st_mode) == -1) {
1349 err = UV__ERR(errno);
1351 /* fchmod() on CIFS shares always fails with EPERM unless the share is
1352 * mounted with "noperm". As fchmod() is a meaningless operation on such
1353 * shares anyway, detect that condition and squelch the error.
1355 if (err != UV_EPERM)
1358 if (!uv__is_cifs_or_smb(dstfd))
1362 #else /* !__linux__ */
1364 #endif /* !__linux__ */
1368 if (req->flags & UV_FS_COPYFILE_FICLONE ||
1369 req->flags & UV_FS_COPYFILE_FICLONE_FORCE) {
1370 if (ioctl(dstfd, FICLONE, srcfd) == 0) {
1371 /* ioctl() with FICLONE succeeded. */
1374 /* If an error occurred and force was set, return the error to the caller;
1375 * fall back to sendfile() when force was not set. */
1376 if (req->flags & UV_FS_COPYFILE_FICLONE_FORCE) {
1377 err = UV__ERR(errno);
1382 if (req->flags & UV_FS_COPYFILE_FICLONE_FORCE) {
1388 bytes_to_send = src_statsbuf.st_size;
1390 while (bytes_to_send != 0) {
1391 bytes_chunk = SSIZE_MAX;
1392 if (bytes_to_send < (off_t) bytes_chunk)
1393 bytes_chunk = bytes_to_send;
1394 uv_fs_sendfile(NULL, &fs_req, dstfd, srcfd, in_offset, bytes_chunk, NULL);
1395 bytes_written = fs_req.result;
1396 uv_fs_req_cleanup(&fs_req);
1398 if (bytes_written < 0) {
1399 err = bytes_written;
1403 bytes_to_send -= bytes_written;
1404 in_offset += bytes_written;
1413 /* Close the source file. */
1414 err = uv__close_nocheckstdio(srcfd);
1416 /* Don't overwrite any existing errors. */
1417 if (err != 0 && result == 0)
1420 /* Close the destination file if it is open. */
1422 err = uv__close_nocheckstdio(dstfd);
1424 /* Don't overwrite any existing errors. */
1425 if (err != 0 && result == 0)
1428 /* Remove the destination file if something went wrong. */
1430 uv_fs_unlink(NULL, &fs_req, req->new_path, NULL);
1431 /* Ignore the unlink return value, as an error already happened. */
1432 uv_fs_req_cleanup(&fs_req);
1439 errno = UV__ERR(result);
1443 static void uv__to_stat(struct stat* src, uv_stat_t* dst) {
1444 dst->st_dev = src->st_dev;
1445 dst->st_mode = src->st_mode;
1446 dst->st_nlink = src->st_nlink;
1447 dst->st_uid = src->st_uid;
1448 dst->st_gid = src->st_gid;
1449 dst->st_rdev = src->st_rdev;
1450 dst->st_ino = src->st_ino;
1451 dst->st_size = src->st_size;
1452 dst->st_blksize = src->st_blksize;
1453 dst->st_blocks = src->st_blocks;
1455 #if defined(__APPLE__)
1456 dst->st_atim.tv_sec = src->st_atimespec.tv_sec;
1457 dst->st_atim.tv_nsec = src->st_atimespec.tv_nsec;
1458 dst->st_mtim.tv_sec = src->st_mtimespec.tv_sec;
1459 dst->st_mtim.tv_nsec = src->st_mtimespec.tv_nsec;
1460 dst->st_ctim.tv_sec = src->st_ctimespec.tv_sec;
1461 dst->st_ctim.tv_nsec = src->st_ctimespec.tv_nsec;
1462 dst->st_birthtim.tv_sec = src->st_birthtimespec.tv_sec;
1463 dst->st_birthtim.tv_nsec = src->st_birthtimespec.tv_nsec;
1464 dst->st_flags = src->st_flags;
1465 dst->st_gen = src->st_gen;
1466 #elif defined(__ANDROID__)
1467 dst->st_atim.tv_sec = src->st_atime;
1468 dst->st_atim.tv_nsec = src->st_atimensec;
1469 dst->st_mtim.tv_sec = src->st_mtime;
1470 dst->st_mtim.tv_nsec = src->st_mtimensec;
1471 dst->st_ctim.tv_sec = src->st_ctime;
1472 dst->st_ctim.tv_nsec = src->st_ctimensec;
1473 dst->st_birthtim.tv_sec = src->st_ctime;
1474 dst->st_birthtim.tv_nsec = src->st_ctimensec;
1477 #elif !defined(_AIX) && \
1478 !defined(__MVS__) && ( \
1479 defined(__DragonFly__) || \
1480 defined(__FreeBSD__) || \
1481 defined(__OpenBSD__) || \
1482 defined(__NetBSD__) || \
1483 defined(_GNU_SOURCE) || \
1484 defined(_BSD_SOURCE) || \
1485 defined(_SVID_SOURCE) || \
1486 defined(_XOPEN_SOURCE) || \
1487 defined(_DEFAULT_SOURCE))
1488 dst->st_atim.tv_sec = src->st_atim.tv_sec;
1489 dst->st_atim.tv_nsec = src->st_atim.tv_nsec;
1490 dst->st_mtim.tv_sec = src->st_mtim.tv_sec;
1491 dst->st_mtim.tv_nsec = src->st_mtim.tv_nsec;
1492 dst->st_ctim.tv_sec = src->st_ctim.tv_sec;
1493 dst->st_ctim.tv_nsec = src->st_ctim.tv_nsec;
1494 # if defined(__FreeBSD__) || \
1496 dst->st_birthtim.tv_sec = src->st_birthtim.tv_sec;
1497 dst->st_birthtim.tv_nsec = src->st_birthtim.tv_nsec;
1498 dst->st_flags = src->st_flags;
1499 dst->st_gen = src->st_gen;
1501 dst->st_birthtim.tv_sec = src->st_ctim.tv_sec;
1502 dst->st_birthtim.tv_nsec = src->st_ctim.tv_nsec;
1507 dst->st_atim.tv_sec = src->st_atime;
1508 dst->st_atim.tv_nsec = 0;
1509 dst->st_mtim.tv_sec = src->st_mtime;
1510 dst->st_mtim.tv_nsec = 0;
1511 dst->st_ctim.tv_sec = src->st_ctime;
1512 dst->st_ctim.tv_nsec = 0;
1513 dst->st_birthtim.tv_sec = src->st_ctime;
1514 dst->st_birthtim.tv_nsec = 0;
1521 static int uv__fs_statx(int fd,
1526 STATIC_ASSERT(UV_ENOSYS != -1);
1528 static int no_statx;
1529 struct uv__statx statxbuf;
1535 if (uv__load_relaxed(&no_statx))
1539 flags = 0; /* AT_STATX_SYNC_AS_STAT */
1540 mode = 0xFFF; /* STATX_BASIC_STATS + STATX_BTIME */
1544 flags |= 0x1000; /* AT_EMPTY_PATH */
1548 flags |= AT_SYMLINK_NOFOLLOW;
1550 rc = uv__statx(dirfd, path, flags, mode, &statxbuf);
1556 /* EPERM happens when a seccomp filter rejects the system call.
1557 * Has been observed with libseccomp < 2.3.3 and docker < 18.04.
1558 * EOPNOTSUPP is used on DVS exported filesystems
1560 if (errno != EINVAL && errno != EPERM && errno != ENOSYS && errno != EOPNOTSUPP)
1564 /* Normally on success, zero is returned and On error, -1 is returned.
1565 * Observed on S390 RHEL running in a docker container with statx not
1566 * implemented, rc might return 1 with 0 set as the error code in which
1567 * case we return ENOSYS.
1569 uv__store_relaxed(&no_statx, 1);
1573 buf->st_dev = makedev(statxbuf.stx_dev_major, statxbuf.stx_dev_minor);
1574 buf->st_mode = statxbuf.stx_mode;
1575 buf->st_nlink = statxbuf.stx_nlink;
1576 buf->st_uid = statxbuf.stx_uid;
1577 buf->st_gid = statxbuf.stx_gid;
1578 buf->st_rdev = makedev(statxbuf.stx_rdev_major, statxbuf.stx_rdev_minor);
1579 buf->st_ino = statxbuf.stx_ino;
1580 buf->st_size = statxbuf.stx_size;
1581 buf->st_blksize = statxbuf.stx_blksize;
1582 buf->st_blocks = statxbuf.stx_blocks;
1583 buf->st_atim.tv_sec = statxbuf.stx_atime.tv_sec;
1584 buf->st_atim.tv_nsec = statxbuf.stx_atime.tv_nsec;
1585 buf->st_mtim.tv_sec = statxbuf.stx_mtime.tv_sec;
1586 buf->st_mtim.tv_nsec = statxbuf.stx_mtime.tv_nsec;
1587 buf->st_ctim.tv_sec = statxbuf.stx_ctime.tv_sec;
1588 buf->st_ctim.tv_nsec = statxbuf.stx_ctime.tv_nsec;
1589 buf->st_birthtim.tv_sec = statxbuf.stx_btime.tv_sec;
1590 buf->st_birthtim.tv_nsec = statxbuf.stx_btime.tv_nsec;
1597 #endif /* __linux__ */
1601 static int uv__fs_stat(const char *path, uv_stat_t *buf) {
1605 ret = uv__fs_statx(-1, path, /* is_fstat */ 0, /* is_lstat */ 0, buf);
1606 if (ret != UV_ENOSYS)
1609 ret = stat(path, &pbuf);
1611 uv__to_stat(&pbuf, buf);
1617 static int uv__fs_lstat(const char *path, uv_stat_t *buf) {
1621 ret = uv__fs_statx(-1, path, /* is_fstat */ 0, /* is_lstat */ 1, buf);
1622 if (ret != UV_ENOSYS)
1625 ret = lstat(path, &pbuf);
1627 uv__to_stat(&pbuf, buf);
1633 static int uv__fs_fstat(int fd, uv_stat_t *buf) {
1637 ret = uv__fs_statx(fd, "", /* is_fstat */ 1, /* is_lstat */ 0, buf);
1638 if (ret != UV_ENOSYS)
1641 ret = fstat(fd, &pbuf);
1643 uv__to_stat(&pbuf, buf);
1648 static size_t uv__fs_buf_offset(uv_buf_t* bufs, size_t size) {
1650 /* Figure out which bufs are done */
1651 for (offset = 0; size > 0 && bufs[offset].len <= size; ++offset)
1652 size -= bufs[offset].len;
1654 /* Fix a partial read/write */
1656 bufs[offset].base += size;
1657 bufs[offset].len -= size;
1662 static ssize_t uv__fs_write_all(uv_fs_t* req) {
1663 unsigned int iovmax;
1669 iovmax = uv__getiovmax();
1676 if (req->nbufs > iovmax)
1677 req->nbufs = iovmax;
1680 result = uv__fs_write(req);
1681 while (result < 0 && errno == EINTR);
1692 req->nbufs = uv__fs_buf_offset(req->bufs, result);
1693 req->bufs += req->nbufs;
1694 nbufs -= req->nbufs;
1698 if (bufs != req->bufsml)
1708 static void uv__fs_work(struct uv__work* w) {
1713 req = container_of(w, uv_fs_t, work_req);
1714 retry_on_eintr = !(req->fs_type == UV_FS_CLOSE ||
1715 req->fs_type == UV_FS_READ);
1720 #define X(type, action) \
1721 case UV_FS_ ## type: \
1725 switch (req->fs_type) {
1726 X(ACCESS, access(req->path, req->flags));
1727 X(CHMOD, chmod(req->path, req->mode));
1728 X(CHOWN, chown(req->path, req->uid, req->gid));
1729 X(CLOSE, uv__fs_close(req->file));
1730 X(COPYFILE, uv__fs_copyfile(req));
1731 X(FCHMOD, fchmod(req->file, req->mode));
1732 X(FCHOWN, fchown(req->file, req->uid, req->gid));
1733 X(LCHOWN, lchown(req->path, req->uid, req->gid));
1734 X(FDATASYNC, uv__fs_fdatasync(req));
1735 X(FSTAT, uv__fs_fstat(req->file, &req->statbuf));
1736 X(FSYNC, uv__fs_fsync(req));
1737 X(FTRUNCATE, ftruncate(req->file, req->off));
1738 X(FUTIME, uv__fs_futime(req));
1739 X(LUTIME, uv__fs_lutime(req));
1740 X(LSTAT, uv__fs_lstat(req->path, &req->statbuf));
1741 X(LINK, link(req->path, req->new_path));
1742 X(MKDIR, mkdir(req->path, req->mode));
1743 X(MKDTEMP, uv__fs_mkdtemp(req));
1744 X(MKSTEMP, uv__fs_mkstemp(req));
1745 X(OPEN, uv__fs_open(req));
1746 X(READ, uv__fs_read(req));
1747 X(SCANDIR, uv__fs_scandir(req));
1748 X(OPENDIR, uv__fs_opendir(req));
1749 X(READDIR, uv__fs_readdir(req));
1750 X(CLOSEDIR, uv__fs_closedir(req));
1751 X(READLINK, uv__fs_readlink(req));
1752 X(REALPATH, uv__fs_realpath(req));
1753 X(RENAME, rename(req->path, req->new_path));
1754 X(RMDIR, rmdir(req->path));
1755 X(SENDFILE, uv__fs_sendfile(req));
1756 X(STAT, uv__fs_stat(req->path, &req->statbuf));
1757 X(STATFS, uv__fs_statfs(req));
1758 X(SYMLINK, symlink(req->path, req->new_path));
1759 X(UNLINK, unlink(req->path));
1760 X(UTIME, uv__fs_utime(req));
1761 X(WRITE, uv__fs_write_all(req));
1765 } while (r == -1 && errno == EINTR && retry_on_eintr);
1768 req->result = UV__ERR(errno);
1772 if (r == 0 && (req->fs_type == UV_FS_STAT ||
1773 req->fs_type == UV_FS_FSTAT ||
1774 req->fs_type == UV_FS_LSTAT)) {
1775 req->ptr = &req->statbuf;
1780 static void uv__fs_done(struct uv__work* w, int status) {
1783 req = container_of(w, uv_fs_t, work_req);
1784 uv__req_unregister(req->loop, req);
1786 if (status == UV_ECANCELED) {
1787 assert(req->result == 0);
1788 req->result = UV_ECANCELED;
1795 int uv_fs_access(uv_loop_t* loop,
1807 int uv_fs_chmod(uv_loop_t* loop,
1819 int uv_fs_chown(uv_loop_t* loop,
1833 int uv_fs_close(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
1840 int uv_fs_fchmod(uv_loop_t* loop,
1852 int uv_fs_fchown(uv_loop_t* loop,
1866 int uv_fs_lchown(uv_loop_t* loop,
1880 int uv_fs_fdatasync(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
1887 int uv_fs_fstat(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
1894 int uv_fs_fsync(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
1901 int uv_fs_ftruncate(uv_loop_t* loop,
1913 int uv_fs_futime(uv_loop_t* loop,
1926 int uv_fs_lutime(uv_loop_t* loop,
1940 int uv_fs_lstat(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
1947 int uv_fs_link(uv_loop_t* loop,
1950 const char* new_path,
1958 int uv_fs_mkdir(uv_loop_t* loop,
1970 int uv_fs_mkdtemp(uv_loop_t* loop,
1975 req->path = uv__strdup(tpl);
1976 if (req->path == NULL)
1982 int uv_fs_mkstemp(uv_loop_t* loop,
1987 req->path = uv__strdup(tpl);
1988 if (req->path == NULL)
1994 int uv_fs_open(uv_loop_t* loop,
2008 int uv_fs_read(uv_loop_t* loop, uv_fs_t* req,
2010 const uv_buf_t bufs[],
2016 if (bufs == NULL || nbufs == 0)
2022 req->bufs = req->bufsml;
2023 if (nbufs > ARRAY_SIZE(req->bufsml))
2024 req->bufs = uv__malloc(nbufs * sizeof(*bufs));
2026 if (req->bufs == NULL)
2029 memcpy(req->bufs, bufs, nbufs * sizeof(*bufs));
2036 int uv_fs_scandir(uv_loop_t* loop,
2047 int uv_fs_opendir(uv_loop_t* loop,
2056 int uv_fs_readdir(uv_loop_t* loop,
2062 if (dir == NULL || dir->dir == NULL || dir->dirents == NULL)
2069 int uv_fs_closedir(uv_loop_t* loop,
2082 int uv_fs_readlink(uv_loop_t* loop,
2092 int uv_fs_realpath(uv_loop_t* loop,
2102 int uv_fs_rename(uv_loop_t* loop,
2105 const char* new_path,
2113 int uv_fs_rmdir(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
2120 int uv_fs_sendfile(uv_loop_t* loop,
2128 req->flags = in_fd; /* hack */
2131 req->bufsml[0].len = len;
2136 int uv_fs_stat(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
2143 int uv_fs_symlink(uv_loop_t* loop,
2146 const char* new_path,
2156 int uv_fs_unlink(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
2163 int uv_fs_utime(uv_loop_t* loop,
2177 int uv_fs_write(uv_loop_t* loop,
2180 const uv_buf_t bufs[],
2186 if (bufs == NULL || nbufs == 0)
2192 req->bufs = req->bufsml;
2193 if (nbufs > ARRAY_SIZE(req->bufsml))
2194 req->bufs = uv__malloc(nbufs * sizeof(*bufs));
2196 if (req->bufs == NULL)
2199 memcpy(req->bufs, bufs, nbufs * sizeof(*bufs));
2206 void uv_fs_req_cleanup(uv_fs_t* req) {
2210 /* Only necessary for asychronous requests, i.e., requests with a callback.
2211 * Synchronous ones don't copy their arguments and have req->path and
2212 * req->new_path pointing to user-owned memory. UV_FS_MKDTEMP and
2213 * UV_FS_MKSTEMP are the exception to the rule, they always allocate memory.
2215 if (req->path != NULL &&
2217 req->fs_type == UV_FS_MKDTEMP || req->fs_type == UV_FS_MKSTEMP))
2218 uv__free((void*) req->path); /* Memory is shared with req->new_path. */
2221 req->new_path = NULL;
2223 if (req->fs_type == UV_FS_READDIR && req->ptr != NULL)
2224 uv__fs_readdir_cleanup(req);
2226 if (req->fs_type == UV_FS_SCANDIR && req->ptr != NULL)
2227 uv__fs_scandir_cleanup(req);
2229 if (req->bufs != req->bufsml)
2230 uv__free(req->bufs);
2233 if (req->fs_type != UV_FS_OPENDIR && req->ptr != &req->statbuf)
2239 int uv_fs_copyfile(uv_loop_t* loop,
2242 const char* new_path,
2247 if (flags & ~(UV_FS_COPYFILE_EXCL |
2248 UV_FS_COPYFILE_FICLONE |
2249 UV_FS_COPYFILE_FICLONE_FORCE)) {
2259 int uv_fs_statfs(uv_loop_t* loop,
2268 int uv_fs_get_system_error(const uv_fs_t* req) {
2269 return -req->result;