Path.inc revision 360784
1//===- llvm/Support/Unix/Path.inc - Unix Path Implementation ----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Unix specific implementation of the Path API.
10//
11//===----------------------------------------------------------------------===//
12
13//===----------------------------------------------------------------------===//
14//=== WARNING: Implementation here must contain only generic UNIX code that
15//===          is guaranteed to work on *all* UNIX variants.
16//===----------------------------------------------------------------------===//
17
18#include "Unix.h"
19#include <limits.h>
20#include <stdio.h>
21#if HAVE_SYS_STAT_H
22#include <sys/stat.h>
23#endif
24#if HAVE_FCNTL_H
25#include <fcntl.h>
26#endif
27#ifdef HAVE_UNISTD_H
28#include <unistd.h>
29#endif
30#ifdef HAVE_SYS_MMAN_H
31#include <sys/mman.h>
32#endif
33
34#include <dirent.h>
35#include <pwd.h>
36
37#ifdef __APPLE__
38#include <mach-o/dyld.h>
39#include <sys/attr.h>
40#include <copyfile.h>
41#elif defined(__FreeBSD__)
42#include <osreldate.h>
43#if __FreeBSD_version >= 1300057
44#include <sys/auxv.h>
45#else
46#include <machine/elf.h>
47extern char **environ;
48#endif
49#elif defined(__DragonFly__)
50#include <sys/mount.h>
51#endif
52
53// Both stdio.h and cstdio are included via different paths and
54// stdcxx's cstdio doesn't include stdio.h, so it doesn't #undef the macros
55// either.
56#undef ferror
57#undef feof
58
59// For GNU Hurd
60#if defined(__GNU__) && !defined(PATH_MAX)
61# define PATH_MAX 4096
62# define MAXPATHLEN 4096
63#endif
64
65#include <sys/types.h>
66#if !defined(__APPLE__) && !defined(__OpenBSD__) && !defined(__FreeBSD__) &&   \
67    !defined(__linux__) && !defined(__FreeBSD_kernel__) && !defined(_AIX)
68#include <sys/statvfs.h>
69#define STATVFS statvfs
70#define FSTATVFS fstatvfs
71#define STATVFS_F_FRSIZE(vfs) vfs.f_frsize
72#else
73#if defined(__OpenBSD__) || defined(__FreeBSD__)
74#include <sys/mount.h>
75#include <sys/param.h>
76#elif defined(__linux__)
77#if defined(HAVE_LINUX_MAGIC_H)
78#include <linux/magic.h>
79#else
80#if defined(HAVE_LINUX_NFS_FS_H)
81#include <linux/nfs_fs.h>
82#endif
83#if defined(HAVE_LINUX_SMB_H)
84#include <linux/smb.h>
85#endif
86#endif
87#include <sys/vfs.h>
88#elif defined(_AIX)
89#include <sys/statfs.h>
90
91// <sys/vmount.h> depends on `uint` to be a typedef from <sys/types.h> to
92// `uint_t`; however, <sys/types.h> does not always declare `uint`. We provide
93// the typedef prior to including <sys/vmount.h> to work around this issue.
94typedef uint_t uint;
95#include <sys/vmount.h>
96#else
97#include <sys/mount.h>
98#endif
99#define STATVFS statfs
100#define FSTATVFS fstatfs
101#define STATVFS_F_FRSIZE(vfs) static_cast<uint64_t>(vfs.f_bsize)
102#endif
103
104#if defined(__NetBSD__) || defined(__DragonFly__) || defined(__GNU__)
105#define STATVFS_F_FLAG(vfs) (vfs).f_flag
106#else
107#define STATVFS_F_FLAG(vfs) (vfs).f_flags
108#endif
109
110using namespace llvm;
111
112namespace llvm {
113namespace sys  {
114namespace fs {
115
116const file_t kInvalidFile = -1;
117
118#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) ||     \
119    defined(__minix) || defined(__FreeBSD_kernel__) || defined(__linux__) ||   \
120    defined(__CYGWIN__) || defined(__DragonFly__) || defined(_AIX) || defined(__GNU__)
121static int
122test_dir(char ret[PATH_MAX], const char *dir, const char *bin)
123{
124  struct stat sb;
125  char fullpath[PATH_MAX];
126
127  int chars = snprintf(fullpath, PATH_MAX, "%s/%s", dir, bin);
128  // We cannot write PATH_MAX characters because the string will be terminated
129  // with a null character. Fail if truncation happened.
130  if (chars >= PATH_MAX)
131    return 1;
132  if (!realpath(fullpath, ret))
133    return 1;
134  if (stat(fullpath, &sb) != 0)
135    return 1;
136
137  return 0;
138}
139
140static char *
141getprogpath(char ret[PATH_MAX], const char *bin)
142{
143  /* First approach: absolute path. */
144  if (bin[0] == '/') {
145    if (test_dir(ret, "/", bin) == 0)
146      return ret;
147    return nullptr;
148  }
149
150  /* Second approach: relative path. */
151  if (strchr(bin, '/')) {
152    char cwd[PATH_MAX];
153    if (!getcwd(cwd, PATH_MAX))
154      return nullptr;
155    if (test_dir(ret, cwd, bin) == 0)
156      return ret;
157    return nullptr;
158  }
159
160  /* Third approach: $PATH */
161  char *pv;
162  if ((pv = getenv("PATH")) == nullptr)
163    return nullptr;
164  char *s = strdup(pv);
165  if (!s)
166    return nullptr;
167  char *state;
168  for (char *t = strtok_r(s, ":", &state); t != nullptr;
169       t = strtok_r(nullptr, ":", &state)) {
170    if (test_dir(ret, t, bin) == 0) {
171      free(s);
172      return ret;
173    }
174  }
175  free(s);
176  return nullptr;
177}
178#endif // __FreeBSD__ || __NetBSD__ || __FreeBSD_kernel__
179
180/// GetMainExecutable - Return the path to the main executable, given the
181/// value of argv[0] from program startup.
182std::string getMainExecutable(const char *argv0, void *MainAddr) {
183#if defined(__APPLE__)
184  // On OS X the executable path is saved to the stack by dyld. Reading it
185  // from there is much faster than calling dladdr, especially for large
186  // binaries with symbols.
187  char exe_path[MAXPATHLEN];
188  uint32_t size = sizeof(exe_path);
189  if (_NSGetExecutablePath(exe_path, &size) == 0) {
190    char link_path[MAXPATHLEN];
191    if (realpath(exe_path, link_path))
192      return link_path;
193  }
194#elif defined(__FreeBSD__)
195  // On FreeBSD if the exec path specified in ELF auxiliary vectors is
196  // preferred, if available.  /proc/curproc/file and the KERN_PROC_PATHNAME
197  // sysctl may not return the desired path if there are multiple hardlinks
198  // to the file.
199  char exe_path[PATH_MAX];
200#if __FreeBSD_version >= 1300057
201  if (elf_aux_info(AT_EXECPATH, exe_path, sizeof(exe_path)) == 0)
202    return exe_path;
203#else
204  // elf_aux_info(AT_EXECPATH, ... is not available in all supported versions,
205  // fall back to finding the ELF auxiliary vectors after the process's
206  // environment.
207  char **p = ::environ;
208  while (*p++ != 0)
209    ;
210  // Iterate through auxiliary vectors for AT_EXECPATH.
211  for (;;) {
212    switch (*(uintptr_t *)p++) {
213    case AT_EXECPATH:
214      return *p;
215    case AT_NULL:
216      break;
217    }
218    p++;
219  }
220#endif
221  // Fall back to argv[0] if auxiliary vectors are not available.
222  if (getprogpath(exe_path, argv0) != NULL)
223    return exe_path;
224#elif defined(__NetBSD__) || defined(__OpenBSD__) || defined(__minix) ||       \
225    defined(__DragonFly__) || defined(__FreeBSD_kernel__) || defined(_AIX)
226  const char *curproc = "/proc/curproc/file";
227  char exe_path[PATH_MAX];
228  if (sys::fs::exists(curproc)) {
229    ssize_t len = readlink(curproc, exe_path, sizeof(exe_path));
230    if (len > 0) {
231      // Null terminate the string for realpath. readlink never null
232      // terminates its output.
233      len = std::min(len, ssize_t(sizeof(exe_path) - 1));
234      exe_path[len] = '\0';
235      return exe_path;
236    }
237  }
238  // If we don't have procfs mounted, fall back to argv[0]
239  if (getprogpath(exe_path, argv0) != NULL)
240    return exe_path;
241#elif defined(__linux__) || defined(__CYGWIN__) || defined(__gnu_hurd__)
242  char exe_path[MAXPATHLEN];
243  const char *aPath = "/proc/self/exe";
244  if (sys::fs::exists(aPath)) {
245    // /proc is not always mounted under Linux (chroot for example).
246    ssize_t len = readlink(aPath, exe_path, sizeof(exe_path));
247    if (len < 0)
248      return "";
249
250    // Null terminate the string for realpath. readlink never null
251    // terminates its output.
252    len = std::min(len, ssize_t(sizeof(exe_path) - 1));
253    exe_path[len] = '\0';
254
255    // On Linux, /proc/self/exe always looks through symlinks. However, on
256    // GNU/Hurd, /proc/self/exe is a symlink to the path that was used to start
257    // the program, and not the eventual binary file. Therefore, call realpath
258    // so this behaves the same on all platforms.
259#if _POSIX_VERSION >= 200112 || defined(__GLIBC__)
260    if (char *real_path = realpath(exe_path, NULL)) {
261      std::string ret = std::string(real_path);
262      free(real_path);
263      return ret;
264    }
265#else
266    char real_path[MAXPATHLEN];
267    if (realpath(exe_path, real_path))
268      return std::string(real_path);
269#endif
270  }
271  // Fall back to the classical detection.
272  if (getprogpath(exe_path, argv0))
273    return exe_path;
274#elif defined(HAVE_DLFCN_H) && defined(HAVE_DLADDR)
275  // Use dladdr to get executable path if available.
276  Dl_info DLInfo;
277  int err = dladdr(MainAddr, &DLInfo);
278  if (err == 0)
279    return "";
280
281  // If the filename is a symlink, we need to resolve and return the location of
282  // the actual executable.
283  char link_path[MAXPATHLEN];
284  if (realpath(DLInfo.dli_fname, link_path))
285    return link_path;
286#else
287#error GetMainExecutable is not implemented on this host yet.
288#endif
289  return "";
290}
291
292TimePoint<> basic_file_status::getLastAccessedTime() const {
293  return toTimePoint(fs_st_atime, fs_st_atime_nsec);
294}
295
296TimePoint<> basic_file_status::getLastModificationTime() const {
297  return toTimePoint(fs_st_mtime, fs_st_mtime_nsec);
298}
299
300UniqueID file_status::getUniqueID() const {
301  return UniqueID(fs_st_dev, fs_st_ino);
302}
303
304uint32_t file_status::getLinkCount() const {
305  return fs_st_nlinks;
306}
307
308ErrorOr disk_space(const Twine &Path) {
309  struct STATVFS Vfs;
310  if (::STATVFS(const_cast<char *>(Path.str().c_str()), &Vfs))
311    return std::error_code(errno, std::generic_category());
312  auto FrSize = STATVFS_F_FRSIZE(Vfs);
313  space_info SpaceInfo;
314  SpaceInfo.capacity = static_cast<uint64_t>(Vfs.f_blocks) * FrSize;
315  SpaceInfo.free = static_cast<uint64_t>(Vfs.f_bfree) * FrSize;
316  SpaceInfo.available = static_cast<uint64_t>(Vfs.f_bavail) * FrSize;
317  return SpaceInfo;
318}
319
320std::error_code current_path(SmallVectorImpl<char> &result) {
321  result.clear();
322
323  const char *pwd = ::getenv("PWD");
324  llvm::sys::fs::file_status PWDStatus, DotStatus;
325  if (pwd && llvm::sys::path::is_absolute(pwd) &&
326      !llvm::sys::fs::status(pwd, PWDStatus) &&
327      !llvm::sys::fs::status(".", DotStatus) &&
328      PWDStatus.getUniqueID() == DotStatus.getUniqueID()) {
329    result.append(pwd, pwd + strlen(pwd));
330    return std::error_code();
331  }
332
333#ifdef MAXPATHLEN
334  result.reserve(MAXPATHLEN);
335#else
336// For GNU Hurd
337  result.reserve(1024);
338#endif
339
340  while (true) {
341    if (::getcwd(result.data(), result.capacity()) == nullptr) {
342      // See if there was a real error.
343      if (errno != ENOMEM)
344        return std::error_code(errno, std::generic_category());
345      // Otherwise there just wasn't enough space.
346      result.reserve(result.capacity() * 2);
347    } else
348      break;
349  }
350
351  result.set_size(strlen(result.data()));
352  return std::error_code();
353}
354
355std::error_code set_current_path(const Twine &path) {
356  SmallString<128> path_storage;
357  StringRef p = path.toNullTerminatedStringRef(path_storage);
358
359  if (::chdir(p.begin()) == -1)
360    return std::error_code(errno, std::generic_category());
361
362  return std::error_code();
363}
364
365std::error_code create_directory(const Twine &path, bool IgnoreExisting,
366                                 perms Perms) {
367  SmallString<128> path_storage;
368  StringRef p = path.toNullTerminatedStringRef(path_storage);
369
370  if (::mkdir(p.begin(), Perms) == -1) {
371    if (errno != EEXIST || !IgnoreExisting)
372      return std::error_code(errno, std::generic_category());
373  }
374
375  return std::error_code();
376}
377
378// Note that we are using symbolic link because hard links are not supported by
379// all filesystems (SMB doesn't).
380std::error_code create_link(const Twine &to, const Twine &from) {
381  // Get arguments.
382  SmallString<128> from_storage;
383  SmallString<128> to_storage;
384  StringRef f = from.toNullTerminatedStringRef(from_storage);
385  StringRef t = to.toNullTerminatedStringRef(to_storage);
386
387  if (::symlink(t.begin(), f.begin()) == -1)
388    return std::error_code(errno, std::generic_category());
389
390  return std::error_code();
391}
392
393std::error_code create_hard_link(const Twine &to, const Twine &from) {
394  // Get arguments.
395  SmallString<128> from_storage;
396  SmallString<128> to_storage;
397  StringRef f = from.toNullTerminatedStringRef(from_storage);
398  StringRef t = to.toNullTerminatedStringRef(to_storage);
399
400  if (::link(t.begin(), f.begin()) == -1)
401    return std::error_code(errno, std::generic_category());
402
403  return std::error_code();
404}
405
406std::error_code remove(const Twine &path, bool IgnoreNonExisting) {
407  SmallString<128> path_storage;
408  StringRef p = path.toNullTerminatedStringRef(path_storage);
409
410  struct stat buf;
411  if (lstat(p.begin(), &buf) != 0) {
412    if (errno != ENOENT || !IgnoreNonExisting)
413      return std::error_code(errno, std::generic_category());
414    return std::error_code();
415  }
416
417  // Note: this check catches strange situations. In all cases, LLVM should
418  // only be involved in the creation and deletion of regular files.  This
419  // check ensures that what we're trying to erase is a regular file. It
420  // effectively prevents LLVM from erasing things like /dev/null, any block
421  // special file, or other things that aren't "regular" files.
422  if (!S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode) && !S_ISLNK(buf.st_mode))
423    return make_error_code(errc::operation_not_permitted);
424
425  if (::remove(p.begin()) == -1) {
426    if (errno != ENOENT || !IgnoreNonExisting)
427      return std::error_code(errno, std::generic_category());
428  }
429
430  return std::error_code();
431}
432
433static bool is_local_impl(struct STATVFS &Vfs) {
434#if defined(__linux__) || defined(__GNU__)
435#ifndef NFS_SUPER_MAGIC
436#define NFS_SUPER_MAGIC 0x6969
437#endif
438#ifndef SMB_SUPER_MAGIC
439#define SMB_SUPER_MAGIC 0x517B
440#endif
441#ifndef CIFS_MAGIC_NUMBER
442#define CIFS_MAGIC_NUMBER 0xFF534D42
443#endif
444#ifdef __GNU__
445  switch ((uint32_t)Vfs.__f_type) {
446#else
447  switch ((uint32_t)Vfs.f_type) {
448#endif
449  case NFS_SUPER_MAGIC:
450  case SMB_SUPER_MAGIC:
451  case CIFS_MAGIC_NUMBER:
452    return false;
453  default:
454    return true;
455  }
456#elif defined(__CYGWIN__)
457  // Cygwin doesn't expose this information; would need to use Win32 API.
458  return false;
459#elif defined(__Fuchsia__)
460  // Fuchsia doesn't yet support remote filesystem mounts.
461  return true;
462#elif defined(__EMSCRIPTEN__)
463  // Emscripten doesn't currently support remote filesystem mounts.
464  return true;
465#elif defined(__HAIKU__)
466  // Haiku doesn't expose this information.
467  return false;
468#elif defined(__sun)
469  // statvfs::f_basetype contains a null-terminated FSType name of the mounted target
470  StringRef fstype(Vfs.f_basetype);
471  // NFS is the only non-local fstype??
472  return !fstype.equals("nfs");
473#elif defined(_AIX)
474  // Call mntctl; try more than twice in case of timing issues with a concurrent
475  // mount.
476  int Ret;
477  size_t BufSize = 2048u;
478  std::unique_ptr<char[]> Buf;
479  int Tries = 3;
480  while (Tries--) {
481    Buf = std::make_unique<char[]>(BufSize);
482    Ret = mntctl(MCTL_QUERY, BufSize, Buf.get());
483    if (Ret != 0)
484      break;
485    BufSize = *reinterpret_cast<unsigned int *>(Buf.get());
486    Buf.reset();
487  }
488
489  if (Ret == -1)
490    // There was an error; "remote" is the conservative answer.
491    return false;
492
493  // Look for the correct vmount entry.
494  char *CurObjPtr = Buf.get();
495  while (Ret--) {
496    struct vmount *Vp = reinterpret_cast<struct vmount *>(CurObjPtr);
497    static_assert(sizeof(Vfs.f_fsid) == sizeof(Vp->vmt_fsid),
498                  "fsid length mismatch");
499    if (memcmp(&Vfs.f_fsid, &Vp->vmt_fsid, sizeof Vfs.f_fsid) == 0)
500      return (Vp->vmt_flags & MNT_REMOTE) == 0;
501
502    CurObjPtr += Vp->vmt_length;
503  }
504
505  // vmount entry not found; "remote" is the conservative answer.
506  return false;
507#else
508  return !!(STATVFS_F_FLAG(Vfs) & MNT_LOCAL);
509#endif
510}
511
512std::error_code is_local(const Twine &Path, bool &Result) {
513  struct STATVFS Vfs;
514  if (::STATVFS(const_cast<char *>(Path.str().c_str()), &Vfs))
515    return std::error_code(errno, std::generic_category());
516
517  Result = is_local_impl(Vfs);
518  return std::error_code();
519}
520
521std::error_code is_local(int FD, bool &Result) {
522  struct STATVFS Vfs;
523  if (::FSTATVFS(FD, &Vfs))
524    return std::error_code(errno, std::generic_category());
525
526  Result = is_local_impl(Vfs);
527  return std::error_code();
528}
529
530std::error_code rename(const Twine &from, const Twine &to) {
531  // Get arguments.
532  SmallString<128> from_storage;
533  SmallString<128> to_storage;
534  StringRef f = from.toNullTerminatedStringRef(from_storage);
535  StringRef t = to.toNullTerminatedStringRef(to_storage);
536
537  if (::rename(f.begin(), t.begin()) == -1)
538    return std::error_code(errno, std::generic_category());
539
540  return std::error_code();
541}
542
543std::error_code resize_file(int FD, uint64_t Size) {
544#if defined(HAVE_POSIX_FALLOCATE)
545  // If we have posix_fallocate use it. Unlike ftruncate it always allocates
546  // space, so we get an error if the disk is full.
547  if (int Err = ::posix_fallocate(FD, 0, Size)) {
548#ifdef _AIX
549    constexpr int NotSupportedError = ENOTSUP;
550#else
551    constexpr int NotSupportedError = EOPNOTSUPP;
552#endif
553    if (Err != EINVAL && Err != NotSupportedError)
554      return std::error_code(Err, std::generic_category());
555  }
556#endif
557  // Use ftruncate as a fallback. It may or may not allocate space. At least on
558  // OS X with HFS+ it does.
559  if (::ftruncate(FD, Size) == -1)
560    return std::error_code(errno, std::generic_category());
561
562  return std::error_code();
563}
564
565static int convertAccessMode(AccessMode Mode) {
566  switch (Mode) {
567  case AccessMode::Exist:
568    return F_OK;
569  case AccessMode::Write:
570    return W_OK;
571  case AccessMode::Execute:
572    return R_OK | X_OK; // scripts also need R_OK.
573  }
574  llvm_unreachable("invalid enum");
575}
576
577std::error_code access(const Twine &Path, AccessMode Mode) {
578  SmallString<128> PathStorage;
579  StringRef P = Path.toNullTerminatedStringRef(PathStorage);
580
581  if (::access(P.begin(), convertAccessMode(Mode)) == -1)
582    return std::error_code(errno, std::generic_category());
583
584  if (Mode == AccessMode::Execute) {
585    // Don't say that directories are executable.
586    struct stat buf;
587    if (0 != stat(P.begin(), &buf))
588      return errc::permission_denied;
589    if (!S_ISREG(buf.st_mode))
590      return errc::permission_denied;
591  }
592
593  return std::error_code();
594}
595
596bool can_execute(const Twine &Path) {
597  return !access(Path, AccessMode::Execute);
598}
599
600bool equivalent(file_status A, file_status B) {
601  assert(status_known(A) && status_known(B));
602  return A.fs_st_dev == B.fs_st_dev &&
603         A.fs_st_ino == B.fs_st_ino;
604}
605
606std::error_code equivalent(const Twine &A, const Twine &B, bool &result) {
607  file_status fsA, fsB;
608  if (std::error_code ec = status(A, fsA))
609    return ec;
610  if (std::error_code ec = status(B, fsB))
611    return ec;
612  result = equivalent(fsA, fsB);
613  return std::error_code();
614}
615
616static void expandTildeExpr(SmallVectorImpl<char> &Path) {
617  StringRef PathStr(Path.begin(), Path.size());
618  if (PathStr.empty() || !PathStr.startswith("~"))
619    return;
620
621  PathStr = PathStr.drop_front();
622  StringRef Expr =
623      PathStr.take_until([](char c) { return path::is_separator(c); });
624  StringRef Remainder = PathStr.substr(Expr.size() + 1);
625  SmallString<128> Storage;
626  if (Expr.empty()) {
627    // This is just ~/..., resolve it to the current user's home dir.
628    if (!path::home_directory(Storage)) {
629      // For some reason we couldn't get the home directory.  Just exit.
630      return;
631    }
632
633    // Overwrite the first character and insert the rest.
634    Path[0] = Storage[0];
635    Path.insert(Path.begin() + 1, Storage.begin() + 1, Storage.end());
636    return;
637  }
638
639  // This is a string of the form ~username/, look up this user's entry in the
640  // password database.
641  struct passwd *Entry = nullptr;
642  std::string User = Expr.str();
643  Entry = ::getpwnam(User.c_str());
644
645  if (!Entry) {
646    // Unable to look up the entry, just return back the original path.
647    return;
648  }
649
650  Storage = Remainder;
651  Path.clear();
652  Path.append(Entry->pw_dir, Entry->pw_dir + strlen(Entry->pw_dir));
653  llvm::sys::path::append(Path, Storage);
654}
655
656
657void expand_tilde(const Twine &path, SmallVectorImpl<char> &dest) {
658  dest.clear();
659  if (path.isTriviallyEmpty())
660    return;
661
662  path.toVector(dest);
663  expandTildeExpr(dest);
664
665  return;
666}
667
668static file_type typeForMode(mode_t Mode) {
669  if (S_ISDIR(Mode))
670    return file_type::directory_file;
671  else if (S_ISREG(Mode))
672    return file_type::regular_file;
673  else if (S_ISBLK(Mode))
674    return file_type::block_file;
675  else if (S_ISCHR(Mode))
676    return file_type::character_file;
677  else if (S_ISFIFO(Mode))
678    return file_type::fifo_file;
679  else if (S_ISSOCK(Mode))
680    return file_type::socket_file;
681  else if (S_ISLNK(Mode))
682    return file_type::symlink_file;
683  return file_type::type_unknown;
684}
685
686static std::error_code fillStatus(int StatRet, const struct stat &Status,
687                                  file_status &Result) {
688  if (StatRet != 0) {
689    std::error_code EC(errno, std::generic_category());
690    if (EC == errc::no_such_file_or_directory)
691      Result = file_status(file_type::file_not_found);
692    else
693      Result = file_status(file_type::status_error);
694    return EC;
695  }
696
697  uint32_t atime_nsec, mtime_nsec;
698#if defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
699  atime_nsec = Status.st_atimespec.tv_nsec;
700  mtime_nsec = Status.st_mtimespec.tv_nsec;
701#elif defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
702  atime_nsec = Status.st_atim.tv_nsec;
703  mtime_nsec = Status.st_mtim.tv_nsec;
704#else
705  atime_nsec = mtime_nsec = 0;
706#endif
707
708  perms Perms = static_cast<perms>(Status.st_mode) & all_perms;
709  Result = file_status(typeForMode(Status.st_mode), Perms, Status.st_dev,
710                       Status.st_nlink, Status.st_ino,
711                       Status.st_atime, atime_nsec, Status.st_mtime, mtime_nsec,
712                       Status.st_uid, Status.st_gid, Status.st_size);
713
714  return std::error_code();
715}
716
717std::error_code status(const Twine &Path, file_status &Result, bool Follow) {
718  SmallString<128> PathStorage;
719  StringRef P = Path.toNullTerminatedStringRef(PathStorage);
720
721  struct stat Status;
722  int StatRet = (Follow ? ::stat : ::lstat)(P.begin(), &Status);
723  return fillStatus(StatRet, Status, Result);
724}
725
726std::error_code status(int FD, file_status &Result) {
727  struct stat Status;
728  int StatRet = ::fstat(FD, &Status);
729  return fillStatus(StatRet, Status, Result);
730}
731
732unsigned getUmask() {
733  // Chose arbitary new mask and reset the umask to the old mask.
734  // umask(2) never fails so ignore the return of the second call.
735  unsigned Mask = ::umask(0);
736  (void) ::umask(Mask);
737  return Mask;
738}
739
740std::error_code setPermissions(const Twine &Path, perms Permissions) {
741  SmallString<128> PathStorage;
742  StringRef P = Path.toNullTerminatedStringRef(PathStorage);
743
744  if (::chmod(P.begin(), Permissions))
745    return std::error_code(errno, std::generic_category());
746  return std::error_code();
747}
748
749std::error_code setPermissions(int FD, perms Permissions) {
750  if (::fchmod(FD, Permissions))
751    return std::error_code(errno, std::generic_category());
752  return std::error_code();
753}
754
755std::error_code setLastAccessAndModificationTime(int FD, TimePoint<> AccessTime,
756                                                 TimePoint<> ModificationTime) {
757#if defined(HAVE_FUTIMENS)
758  timespec Times[2];
759  Times[0] = sys::toTimeSpec(AccessTime);
760  Times[1] = sys::toTimeSpec(ModificationTime);
761  if (::futimens(FD, Times))
762    return std::error_code(errno, std::generic_category());
763  return std::error_code();
764#elif defined(HAVE_FUTIMES)
765  timeval Times[2];
766  Times[0] = sys::toTimeVal(
767      std::chrono::time_point_cast<std::chrono::microseconds>(AccessTime));
768  Times[1] =
769      sys::toTimeVal(std::chrono::time_point_cast<std::chrono::microseconds>(
770          ModificationTime));
771  if (::futimes(FD, Times))
772    return std::error_code(errno, std::generic_category());
773  return std::error_code();
774#else
775#warning Missing futimes() and futimens()
776  return make_error_code(errc::function_not_supported);
777#endif
778}
779
780std::error_code mapped_file_region::init(int FD, uint64_t Offset,
781                                         mapmode Mode) {
782  assert(Size != 0);
783
784  int flags = (Mode == readwrite) ? MAP_SHARED : MAP_PRIVATE;
785  int prot = (Mode == readonly) ? PROT_READ : (PROT_READ | PROT_WRITE);
786#if defined(__APPLE__)
787  //----------------------------------------------------------------------
788  // Newer versions of MacOSX have a flag that will allow us to read from
789  // binaries whose code signature is invalid without crashing by using
790  // the MAP_RESILIENT_CODESIGN flag. Also if a file from removable media
791  // is mapped we can avoid crashing and return zeroes to any pages we try
792  // to read if the media becomes unavailable by using the
793  // MAP_RESILIENT_MEDIA flag.  These flags are only usable when mapping
794  // with PROT_READ, so take care not to specify them otherwise.
795  //----------------------------------------------------------------------
796  if (Mode == readonly) {
797#if defined(MAP_RESILIENT_CODESIGN)
798    flags |= MAP_RESILIENT_CODESIGN;
799#endif
800#if defined(MAP_RESILIENT_MEDIA)
801    flags |= MAP_RESILIENT_MEDIA;
802#endif
803  }
804#endif // #if defined (__APPLE__)
805
806  Mapping = ::mmap(nullptr, Size, prot, flags, FD, Offset);
807  if (Mapping == MAP_FAILED)
808    return std::error_code(errno, std::generic_category());
809  return std::error_code();
810}
811
812mapped_file_region::mapped_file_region(int fd, mapmode mode, size_t length,
813                                       uint64_t offset, std::error_code &ec)
814    : Size(length), Mapping(), Mode(mode) {
815  (void)Mode;
816  ec = init(fd, offset, mode);
817  if (ec)
818    Mapping = nullptr;
819}
820
821mapped_file_region::~mapped_file_region() {
822  if (Mapping)
823    ::munmap(Mapping, Size);
824}
825
826size_t mapped_file_region::size() const {
827  assert(Mapping && "Mapping failed but used anyway!");
828  return Size;
829}
830
831char *mapped_file_region::data() const {
832  assert(Mapping && "Mapping failed but used anyway!");
833  return reinterpret_cast<char*>(Mapping);
834}
835
836const char *mapped_file_region::const_data() const {
837  assert(Mapping && "Mapping failed but used anyway!");
838  return reinterpret_cast<const char*>(Mapping);
839}
840
841int mapped_file_region::alignment() {
842  return Process::getPageSizeEstimate();
843}
844
845std::error_code detail::directory_iterator_construct(detail::DirIterState &it,
846                                                     StringRef path,
847                                                     bool follow_symlinks) {
848  SmallString<128> path_null(path);
849  DIR *directory = ::opendir(path_null.c_str());
850  if (!directory)
851    return std::error_code(errno, std::generic_category());
852
853  it.IterationHandle = reinterpret_cast<intptr_t>(directory);
854  // Add something for replace_filename to replace.
855  path::append(path_null, ".");
856  it.CurrentEntry = directory_entry(path_null.str(), follow_symlinks);
857  return directory_iterator_increment(it);
858}
859
860std::error_code detail::directory_iterator_destruct(detail::DirIterState &it) {
861  if (it.IterationHandle)
862    ::closedir(reinterpret_cast<DIR *>(it.IterationHandle));
863  it.IterationHandle = 0;
864  it.CurrentEntry = directory_entry();
865  return std::error_code();
866}
867
868static file_type direntType(dirent* Entry) {
869  // Most platforms provide the file type in the dirent: Linux/BSD/Mac.
870  // The DTTOIF macro lets us reuse our status -> type conversion.
871  // Note that while glibc provides a macro to see if this is supported,
872  // _DIRENT_HAVE_D_TYPE, it's not defined on BSD/Mac, so we test for the
873  // d_type-to-mode_t conversion macro instead.
874#if defined(DTTOIF)
875  return typeForMode(DTTOIF(Entry->d_type));
876#else
877  // Other platforms such as Solaris require a stat() to get the type.
878  return file_type::type_unknown;
879#endif
880}
881
882std::error_code detail::directory_iterator_increment(detail::DirIterState &It) {
883  errno = 0;
884  dirent *CurDir = ::readdir(reinterpret_cast<DIR *>(It.IterationHandle));
885  if (CurDir == nullptr && errno != 0) {
886    return std::error_code(errno, std::generic_category());
887  } else if (CurDir != nullptr) {
888    StringRef Name(CurDir->d_name);
889    if ((Name.size() == 1 && Name[0] == '.') ||
890        (Name.size() == 2 && Name[0] == '.' && Name[1] == '.'))
891      return directory_iterator_increment(It);
892    It.CurrentEntry.replace_filename(Name, direntType(CurDir));
893  } else
894    return directory_iterator_destruct(It);
895
896  return std::error_code();
897}
898
899ErrorOr<basic_file_status> directory_entry::status() const {
900  file_status s;
901  if (auto EC = fs::status(Path, s, FollowSymlinks))
902    return EC;
903  return s;
904}
905
906#if !defined(F_GETPATH)
907static bool hasProcSelfFD() {
908  // If we have a /proc filesystem mounted, we can quickly establish the
909  // real name of the file with readlink
910  static const bool Result = (::access("/proc/self/fd", R_OK) == 0);
911  return Result;
912}
913#endif
914
915static int nativeOpenFlags(CreationDisposition Disp, OpenFlags Flags,
916                           FileAccess Access) {
917  int Result = 0;
918  if (Access == FA_Read)
919    Result |= O_RDONLY;
920  else if (Access == FA_Write)
921    Result |= O_WRONLY;
922  else if (Access == (FA_Read | FA_Write))
923    Result |= O_RDWR;
924
925  // This is for compatibility with old code that assumed OF_Append implied
926  // would open an existing file.  See Windows/Path.inc for a longer comment.
927  if (Flags & OF_Append)
928    Disp = CD_OpenAlways;
929
930  if (Disp == CD_CreateNew) {
931    Result |= O_CREAT; // Create if it doesn't exist.
932    Result |= O_EXCL;  // Fail if it does.
933  } else if (Disp == CD_CreateAlways) {
934    Result |= O_CREAT; // Create if it doesn't exist.
935    Result |= O_TRUNC; // Truncate if it does.
936  } else if (Disp == CD_OpenAlways) {
937    Result |= O_CREAT; // Create if it doesn't exist.
938  } else if (Disp == CD_OpenExisting) {
939    // Nothing special, just don't add O_CREAT and we get these semantics.
940  }
941
942  if (Flags & OF_Append)
943    Result |= O_APPEND;
944
945#ifdef O_CLOEXEC
946  if (!(Flags & OF_ChildInherit))
947    Result |= O_CLOEXEC;
948#endif
949
950  return Result;
951}
952
953std::error_code openFile(const Twine &Name, int &ResultFD,
954                         CreationDisposition Disp, FileAccess Access,
955                         OpenFlags Flags, unsigned Mode) {
956  int OpenFlags = nativeOpenFlags(Disp, Flags, Access);
957
958  SmallString<128> Storage;
959  StringRef P = Name.toNullTerminatedStringRef(Storage);
960  // Call ::open in a lambda to avoid overload resolution in RetryAfterSignal
961  // when open is overloaded, such as in Bionic.
962  auto Open = [&]() { return ::open(P.begin(), OpenFlags, Mode); };
963  if ((ResultFD = sys::RetryAfterSignal(-1, Open)) < 0)
964    return std::error_code(errno, std::generic_category());
965#ifndef O_CLOEXEC
966  if (!(Flags & OF_ChildInherit)) {
967    int r = fcntl(ResultFD, F_SETFD, FD_CLOEXEC);
968    (void)r;
969    assert(r == 0 && "fcntl(F_SETFD, FD_CLOEXEC) failed");
970  }
971#endif
972  return std::error_code();
973}
974
975Expected<int> openNativeFile(const Twine &Name, CreationDisposition Disp,
976                             FileAccess Access, OpenFlags Flags,
977                             unsigned Mode) {
978
979  int FD;
980  std::error_code EC = openFile(Name, FD, Disp, Access, Flags, Mode);
981  if (EC)
982    return errorCodeToError(EC);
983  return FD;
984}
985
986std::error_code openFileForRead(const Twine &Name, int &ResultFD,
987                                OpenFlags Flags,
988                                SmallVectorImpl<char> *RealPath) {
989  std::error_code EC =
990      openFile(Name, ResultFD, CD_OpenExisting, FA_Read, Flags, 0666);
991  if (EC)
992    return EC;
993
994  // Attempt to get the real name of the file, if the user asked
995  if(!RealPath)
996    return std::error_code();
997  RealPath->clear();
998#if defined(F_GETPATH)
999  // When F_GETPATH is availble, it is the quickest way to get
1000  // the real path name.
1001  char Buffer[MAXPATHLEN];
1002  if (::fcntl(ResultFD, F_GETPATH, Buffer) != -1)
1003    RealPath->append(Buffer, Buffer + strlen(Buffer));
1004#else
1005  char Buffer[PATH_MAX];
1006  if (hasProcSelfFD()) {
1007    char ProcPath[64];
1008    snprintf(ProcPath, sizeof(ProcPath), "/proc/self/fd/%d", ResultFD);
1009    ssize_t CharCount = ::readlink(ProcPath, Buffer, sizeof(Buffer));
1010    if (CharCount > 0)
1011      RealPath->append(Buffer, Buffer + CharCount);
1012  } else {
1013    SmallString<128> Storage;
1014    StringRef P = Name.toNullTerminatedStringRef(Storage);
1015
1016    // Use ::realpath to get the real path name
1017    if (::realpath(P.begin(), Buffer) != nullptr)
1018      RealPath->append(Buffer, Buffer + strlen(Buffer));
1019  }
1020#endif
1021  return std::error_code();
1022}
1023
1024Expected<file_t> openNativeFileForRead(const Twine &Name, OpenFlags Flags,
1025                                       SmallVectorImpl<char> *RealPath) {
1026  file_t ResultFD;
1027  std::error_code EC = openFileForRead(Name, ResultFD, Flags, RealPath);
1028  if (EC)
1029    return errorCodeToError(EC);
1030  return ResultFD;
1031}
1032
1033file_t getStdinHandle() { return 0; }
1034file_t getStdoutHandle() { return 1; }
1035file_t getStderrHandle() { return 2; }
1036
1037Expected<size_t> readNativeFile(file_t FD, MutableArrayRef<char> Buf) {
1038  ssize_t NumRead =
1039      sys::RetryAfterSignal(-1, ::read, FD, Buf.data(), Buf.size());
1040  if (ssize_t(NumRead) == -1)
1041    return errorCodeToError(std::error_code(errno, std::generic_category()));
1042  return NumRead;
1043}
1044
1045Expected<size_t> readNativeFileSlice(file_t FD, MutableArrayRef<char> Buf,
1046                                     uint64_t Offset) {
1047#ifdef HAVE_PREAD
1048  ssize_t NumRead =
1049      sys::RetryAfterSignal(-1, ::pread, FD, Buf.data(), Buf.size(), Offset);
1050#else
1051  if (lseek(FD, Offset, SEEK_SET) == -1)
1052    return errorCodeToError(std::error_code(errno, std::generic_category()));
1053  ssize_t NumRead =
1054      sys::RetryAfterSignal(-1, ::read, FD, Buf.data(), Buf.size());
1055#endif
1056  if (NumRead == -1)
1057    return errorCodeToError(std::error_code(errno, std::generic_category()));
1058  return NumRead;
1059}
1060
1061std::error_code closeFile(file_t &F) {
1062  file_t TmpF = F;
1063  F = kInvalidFile;
1064  return Process::SafelyCloseFileDescriptor(TmpF);
1065}
1066
1067template <typename T>
1068static std::error_code remove_directories_impl(const T &Entry,
1069                                               bool IgnoreErrors) {
1070  std::error_code EC;
1071  directory_iterator Begin(Entry, EC, false);
1072  directory_iterator End;
1073  while (Begin != End) {
1074    auto &Item = *Begin;
1075    ErrorOr<basic_file_status> st = Item.status();
1076    if (!st && !IgnoreErrors)
1077      return st.getError();
1078
1079    if (is_directory(*st)) {
1080      EC = remove_directories_impl(Item, IgnoreErrors);
1081      if (EC && !IgnoreErrors)
1082        return EC;
1083    }
1084
1085    EC = fs::remove(Item.path(), true);
1086    if (EC && !IgnoreErrors)
1087      return EC;
1088
1089    Begin.increment(EC);
1090    if (EC && !IgnoreErrors)
1091      return EC;
1092  }
1093  return std::error_code();
1094}
1095
1096std::error_code remove_directories(const Twine &path, bool IgnoreErrors) {
1097  auto EC = remove_directories_impl(path, IgnoreErrors);
1098  if (EC && !IgnoreErrors)
1099    return EC;
1100  EC = fs::remove(path, true);
1101  if (EC && !IgnoreErrors)
1102    return EC;
1103  return std::error_code();
1104}
1105
1106std::error_code real_path(const Twine &path, SmallVectorImpl<char> &dest,
1107                          bool expand_tilde) {
1108  dest.clear();
1109  if (path.isTriviallyEmpty())
1110    return std::error_code();
1111
1112  if (expand_tilde) {
1113    SmallString<128> Storage;
1114    path.toVector(Storage);
1115    expandTildeExpr(Storage);
1116    return real_path(Storage, dest, false);
1117  }
1118
1119  SmallString<128> Storage;
1120  StringRef P = path.toNullTerminatedStringRef(Storage);
1121  char Buffer[PATH_MAX];
1122  if (::realpath(P.begin(), Buffer) == nullptr)
1123    return std::error_code(errno, std::generic_category());
1124  dest.append(Buffer, Buffer + strlen(Buffer));
1125  return std::error_code();
1126}
1127
1128} // end namespace fs
1129
1130namespace path {
1131
1132bool home_directory(SmallVectorImpl<char> &result) {
1133  char *RequestedDir = getenv("HOME");
1134  if (!RequestedDir) {
1135    struct passwd *pw = getpwuid(getuid());
1136    if (pw && pw->pw_dir)
1137      RequestedDir = pw->pw_dir;
1138  }
1139  if (!RequestedDir)
1140    return false;
1141
1142  result.clear();
1143  result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
1144  return true;
1145}
1146
1147static bool getDarwinConfDir(bool TempDir, SmallVectorImpl<char> &Result) {
1148  #if defined(_CS_DARWIN_USER_TEMP_DIR) && defined(_CS_DARWIN_USER_CACHE_DIR)
1149  // On Darwin, use DARWIN_USER_TEMP_DIR or DARWIN_USER_CACHE_DIR.
1150  // macros defined in <unistd.h> on darwin >= 9
1151  int ConfName = TempDir ? _CS_DARWIN_USER_TEMP_DIR
1152                         : _CS_DARWIN_USER_CACHE_DIR;
1153  size_t ConfLen = confstr(ConfName, nullptr, 0);
1154  if (ConfLen > 0) {
1155    do {
1156      Result.resize(ConfLen);
1157      ConfLen = confstr(ConfName, Result.data(), Result.size());
1158    } while (ConfLen > 0 && ConfLen != Result.size());
1159
1160    if (ConfLen > 0) {
1161      assert(Result.back() == 0);
1162      Result.pop_back();
1163      return true;
1164    }
1165
1166    Result.clear();
1167  }
1168  #endif
1169  return false;
1170}
1171
1172static const char *getEnvTempDir() {
1173  // Check whether the temporary directory is specified by an environment
1174  // variable.
1175  const char *EnvironmentVariables[] = {"TMPDIR", "TMP", "TEMP", "TEMPDIR"};
1176  for (const char *Env : EnvironmentVariables) {
1177    if (const char *Dir = std::getenv(Env))
1178      return Dir;
1179  }
1180
1181  return nullptr;
1182}
1183
1184static const char *getDefaultTempDir(bool ErasedOnReboot) {
1185#ifdef P_tmpdir
1186  if ((bool)P_tmpdir)
1187    return P_tmpdir;
1188#endif
1189
1190  if (ErasedOnReboot)
1191    return "/tmp";
1192  return "/var/tmp";
1193}
1194
1195void system_temp_directory(bool ErasedOnReboot, SmallVectorImpl<char> &Result) {
1196  Result.clear();
1197
1198  if (ErasedOnReboot) {
1199    // There is no env variable for the cache directory.
1200    if (const char *RequestedDir = getEnvTempDir()) {
1201      Result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
1202      return;
1203    }
1204  }
1205
1206  if (getDarwinConfDir(ErasedOnReboot, Result))
1207    return;
1208
1209  const char *RequestedDir = getDefaultTempDir(ErasedOnReboot);
1210  Result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
1211}
1212
1213} // end namespace path
1214
1215namespace fs {
1216
1217#ifdef __APPLE__
1218/// This implementation tries to perform an APFS CoW clone of the file,
1219/// which can be much faster and uses less space.
1220/// Unfortunately fcopyfile(3) does not support COPYFILE_CLONE, so the
1221/// file descriptor variant of this function still uses the default
1222/// implementation.
1223std::error_code copy_file(const Twine &From, const Twine &To) {
1224  uint32_t Flag = COPYFILE_DATA;
1225#if __has_builtin(__builtin_available) && defined(COPYFILE_CLONE)
1226  if (__builtin_available(macos 10.12, *)) {
1227    bool IsSymlink;
1228    if (std::error_code Error = is_symlink_file(From, IsSymlink))
1229      return Error;
1230    // COPYFILE_CLONE clones the symlink instead of following it
1231    // and returns EEXISTS if the target file already exists.
1232    if (!IsSymlink && !exists(To))
1233      Flag = COPYFILE_CLONE;
1234  }
1235#endif
1236  int Status =
1237      copyfile(From.str().c_str(), To.str().c_str(), /* State */ NULL, Flag);
1238
1239  if (Status == 0)
1240    return std::error_code();
1241  return std::error_code(errno, std::generic_category());
1242}
1243#endif // __APPLE__
1244
1245} // end namespace fs
1246
1247} // end namespace sys
1248} // end namespace llvm
1249