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