xref: /freebsd/contrib/llvm-project/llvm/lib/Support/Unix/Path.inc (revision 6966ac055c3b7a39266fb982493330df7a097997)
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 on older FreeBSD.  Fall
205  // back to finding the ELF auxiliary vectors after the processes'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  StringRef curproc("/proc/curproc/file");
227  char exe_path[PATH_MAX];
228  if (sys::fs::exists(curproc)) {
229    ssize_t len = readlink(curproc.str().c_str(), 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__)
242  char exe_path[MAXPATHLEN];
243  StringRef 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.str().c_str(), 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<space_info> 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 = llvm::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#if defined(_DIRENT_HAVE_D_TYPE) && defined(DTTOIF)
872  return typeForMode(DTTOIF(Entry->d_type));
873#else
874  // Other platforms such as Solaris require a stat() to get the type.
875  return file_type::type_unknown;
876#endif
877}
878
879std::error_code detail::directory_iterator_increment(detail::DirIterState &It) {
880  errno = 0;
881  dirent *CurDir = ::readdir(reinterpret_cast<DIR *>(It.IterationHandle));
882  if (CurDir == nullptr && errno != 0) {
883    return std::error_code(errno, std::generic_category());
884  } else if (CurDir != nullptr) {
885    StringRef Name(CurDir->d_name);
886    if ((Name.size() == 1 && Name[0] == '.') ||
887        (Name.size() == 2 && Name[0] == '.' && Name[1] == '.'))
888      return directory_iterator_increment(It);
889    It.CurrentEntry.replace_filename(Name, direntType(CurDir));
890  } else
891    return directory_iterator_destruct(It);
892
893  return std::error_code();
894}
895
896ErrorOr<basic_file_status> directory_entry::status() const {
897  file_status s;
898  if (auto EC = fs::status(Path, s, FollowSymlinks))
899    return EC;
900  return s;
901}
902
903#if !defined(F_GETPATH)
904static bool hasProcSelfFD() {
905  // If we have a /proc filesystem mounted, we can quickly establish the
906  // real name of the file with readlink
907  static const bool Result = (::access("/proc/self/fd", R_OK) == 0);
908  return Result;
909}
910#endif
911
912static int nativeOpenFlags(CreationDisposition Disp, OpenFlags Flags,
913                           FileAccess Access) {
914  int Result = 0;
915  if (Access == FA_Read)
916    Result |= O_RDONLY;
917  else if (Access == FA_Write)
918    Result |= O_WRONLY;
919  else if (Access == (FA_Read | FA_Write))
920    Result |= O_RDWR;
921
922  // This is for compatibility with old code that assumed F_Append implied
923  // would open an existing file.  See Windows/Path.inc for a longer comment.
924  if (Flags & F_Append)
925    Disp = CD_OpenAlways;
926
927  if (Disp == CD_CreateNew) {
928    Result |= O_CREAT; // Create if it doesn't exist.
929    Result |= O_EXCL;  // Fail if it does.
930  } else if (Disp == CD_CreateAlways) {
931    Result |= O_CREAT; // Create if it doesn't exist.
932    Result |= O_TRUNC; // Truncate if it does.
933  } else if (Disp == CD_OpenAlways) {
934    Result |= O_CREAT; // Create if it doesn't exist.
935  } else if (Disp == CD_OpenExisting) {
936    // Nothing special, just don't add O_CREAT and we get these semantics.
937  }
938
939  if (Flags & F_Append)
940    Result |= O_APPEND;
941
942#ifdef O_CLOEXEC
943  if (!(Flags & OF_ChildInherit))
944    Result |= O_CLOEXEC;
945#endif
946
947  return Result;
948}
949
950std::error_code openFile(const Twine &Name, int &ResultFD,
951                         CreationDisposition Disp, FileAccess Access,
952                         OpenFlags Flags, unsigned Mode) {
953  int OpenFlags = nativeOpenFlags(Disp, Flags, Access);
954
955  SmallString<128> Storage;
956  StringRef P = Name.toNullTerminatedStringRef(Storage);
957  // Call ::open in a lambda to avoid overload resolution in RetryAfterSignal
958  // when open is overloaded, such as in Bionic.
959  auto Open = [&]() { return ::open(P.begin(), OpenFlags, Mode); };
960  if ((ResultFD = sys::RetryAfterSignal(-1, Open)) < 0)
961    return std::error_code(errno, std::generic_category());
962#ifndef O_CLOEXEC
963  if (!(Flags & OF_ChildInherit)) {
964    int r = fcntl(ResultFD, F_SETFD, FD_CLOEXEC);
965    (void)r;
966    assert(r == 0 && "fcntl(F_SETFD, FD_CLOEXEC) failed");
967  }
968#endif
969  return std::error_code();
970}
971
972Expected<int> openNativeFile(const Twine &Name, CreationDisposition Disp,
973                             FileAccess Access, OpenFlags Flags,
974                             unsigned Mode) {
975
976  int FD;
977  std::error_code EC = openFile(Name, FD, Disp, Access, Flags, Mode);
978  if (EC)
979    return errorCodeToError(EC);
980  return FD;
981}
982
983std::error_code openFileForRead(const Twine &Name, int &ResultFD,
984                                OpenFlags Flags,
985                                SmallVectorImpl<char> *RealPath) {
986  std::error_code EC =
987      openFile(Name, ResultFD, CD_OpenExisting, FA_Read, Flags, 0666);
988  if (EC)
989    return EC;
990
991  // Attempt to get the real name of the file, if the user asked
992  if(!RealPath)
993    return std::error_code();
994  RealPath->clear();
995#if defined(F_GETPATH)
996  // When F_GETPATH is availble, it is the quickest way to get
997  // the real path name.
998  char Buffer[MAXPATHLEN];
999  if (::fcntl(ResultFD, F_GETPATH, Buffer) != -1)
1000    RealPath->append(Buffer, Buffer + strlen(Buffer));
1001#else
1002  char Buffer[PATH_MAX];
1003  if (hasProcSelfFD()) {
1004    char ProcPath[64];
1005    snprintf(ProcPath, sizeof(ProcPath), "/proc/self/fd/%d", ResultFD);
1006    ssize_t CharCount = ::readlink(ProcPath, Buffer, sizeof(Buffer));
1007    if (CharCount > 0)
1008      RealPath->append(Buffer, Buffer + CharCount);
1009  } else {
1010    SmallString<128> Storage;
1011    StringRef P = Name.toNullTerminatedStringRef(Storage);
1012
1013    // Use ::realpath to get the real path name
1014    if (::realpath(P.begin(), Buffer) != nullptr)
1015      RealPath->append(Buffer, Buffer + strlen(Buffer));
1016  }
1017#endif
1018  return std::error_code();
1019}
1020
1021Expected<file_t> openNativeFileForRead(const Twine &Name, OpenFlags Flags,
1022                                       SmallVectorImpl<char> *RealPath) {
1023  file_t ResultFD;
1024  std::error_code EC = openFileForRead(Name, ResultFD, Flags, RealPath);
1025  if (EC)
1026    return errorCodeToError(EC);
1027  return ResultFD;
1028}
1029
1030file_t getStdinHandle() { return 0; }
1031file_t getStdoutHandle() { return 1; }
1032file_t getStderrHandle() { return 2; }
1033
1034std::error_code readNativeFile(file_t FD, MutableArrayRef<char> Buf,
1035                               size_t *BytesRead) {
1036  *BytesRead = sys::RetryAfterSignal(-1, ::read, FD, Buf.data(), Buf.size());
1037  if (ssize_t(*BytesRead) == -1)
1038    return std::error_code(errno, std::generic_category());
1039  return std::error_code();
1040}
1041
1042std::error_code readNativeFileSlice(file_t FD, MutableArrayRef<char> Buf,
1043                                    size_t Offset) {
1044  char *BufPtr = Buf.data();
1045  size_t BytesLeft = Buf.size();
1046
1047#ifndef HAVE_PREAD
1048  // If we don't have pread, seek to Offset.
1049  if (lseek(FD, Offset, SEEK_SET) == -1)
1050    return std::error_code(errno, std::generic_category());
1051#endif
1052
1053  while (BytesLeft) {
1054#ifdef HAVE_PREAD
1055    ssize_t NumRead = sys::RetryAfterSignal(-1, ::pread, FD, BufPtr, BytesLeft,
1056                                            Buf.size() - BytesLeft + Offset);
1057#else
1058    ssize_t NumRead = sys::RetryAfterSignal(-1, ::read, FD, BufPtr, BytesLeft);
1059#endif
1060    if (NumRead == -1) {
1061      // Error while reading.
1062      return std::error_code(errno, std::generic_category());
1063    }
1064    if (NumRead == 0) {
1065      memset(BufPtr, 0, BytesLeft); // zero-initialize rest of the buffer.
1066      break;
1067    }
1068    BytesLeft -= NumRead;
1069    BufPtr += NumRead;
1070  }
1071  return std::error_code();
1072}
1073
1074std::error_code closeFile(file_t &F) {
1075  file_t TmpF = F;
1076  F = kInvalidFile;
1077  return Process::SafelyCloseFileDescriptor(TmpF);
1078}
1079
1080template <typename T>
1081static std::error_code remove_directories_impl(const T &Entry,
1082                                               bool IgnoreErrors) {
1083  std::error_code EC;
1084  directory_iterator Begin(Entry, EC, false);
1085  directory_iterator End;
1086  while (Begin != End) {
1087    auto &Item = *Begin;
1088    ErrorOr<basic_file_status> st = Item.status();
1089    if (!st && !IgnoreErrors)
1090      return st.getError();
1091
1092    if (is_directory(*st)) {
1093      EC = remove_directories_impl(Item, IgnoreErrors);
1094      if (EC && !IgnoreErrors)
1095        return EC;
1096    }
1097
1098    EC = fs::remove(Item.path(), true);
1099    if (EC && !IgnoreErrors)
1100      return EC;
1101
1102    Begin.increment(EC);
1103    if (EC && !IgnoreErrors)
1104      return EC;
1105  }
1106  return std::error_code();
1107}
1108
1109std::error_code remove_directories(const Twine &path, bool IgnoreErrors) {
1110  auto EC = remove_directories_impl(path, IgnoreErrors);
1111  if (EC && !IgnoreErrors)
1112    return EC;
1113  EC = fs::remove(path, true);
1114  if (EC && !IgnoreErrors)
1115    return EC;
1116  return std::error_code();
1117}
1118
1119std::error_code real_path(const Twine &path, SmallVectorImpl<char> &dest,
1120                          bool expand_tilde) {
1121  dest.clear();
1122  if (path.isTriviallyEmpty())
1123    return std::error_code();
1124
1125  if (expand_tilde) {
1126    SmallString<128> Storage;
1127    path.toVector(Storage);
1128    expandTildeExpr(Storage);
1129    return real_path(Storage, dest, false);
1130  }
1131
1132  SmallString<128> Storage;
1133  StringRef P = path.toNullTerminatedStringRef(Storage);
1134  char Buffer[PATH_MAX];
1135  if (::realpath(P.begin(), Buffer) == nullptr)
1136    return std::error_code(errno, std::generic_category());
1137  dest.append(Buffer, Buffer + strlen(Buffer));
1138  return std::error_code();
1139}
1140
1141} // end namespace fs
1142
1143namespace path {
1144
1145bool home_directory(SmallVectorImpl<char> &result) {
1146  char *RequestedDir = getenv("HOME");
1147  if (!RequestedDir) {
1148    struct passwd *pw = getpwuid(getuid());
1149    if (pw && pw->pw_dir)
1150      RequestedDir = pw->pw_dir;
1151  }
1152  if (!RequestedDir)
1153    return false;
1154
1155  result.clear();
1156  result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
1157  return true;
1158}
1159
1160static bool getDarwinConfDir(bool TempDir, SmallVectorImpl<char> &Result) {
1161  #if defined(_CS_DARWIN_USER_TEMP_DIR) && defined(_CS_DARWIN_USER_CACHE_DIR)
1162  // On Darwin, use DARWIN_USER_TEMP_DIR or DARWIN_USER_CACHE_DIR.
1163  // macros defined in <unistd.h> on darwin >= 9
1164  int ConfName = TempDir ? _CS_DARWIN_USER_TEMP_DIR
1165                         : _CS_DARWIN_USER_CACHE_DIR;
1166  size_t ConfLen = confstr(ConfName, nullptr, 0);
1167  if (ConfLen > 0) {
1168    do {
1169      Result.resize(ConfLen);
1170      ConfLen = confstr(ConfName, Result.data(), Result.size());
1171    } while (ConfLen > 0 && ConfLen != Result.size());
1172
1173    if (ConfLen > 0) {
1174      assert(Result.back() == 0);
1175      Result.pop_back();
1176      return true;
1177    }
1178
1179    Result.clear();
1180  }
1181  #endif
1182  return false;
1183}
1184
1185static const char *getEnvTempDir() {
1186  // Check whether the temporary directory is specified by an environment
1187  // variable.
1188  const char *EnvironmentVariables[] = {"TMPDIR", "TMP", "TEMP", "TEMPDIR"};
1189  for (const char *Env : EnvironmentVariables) {
1190    if (const char *Dir = std::getenv(Env))
1191      return Dir;
1192  }
1193
1194  return nullptr;
1195}
1196
1197static const char *getDefaultTempDir(bool ErasedOnReboot) {
1198#ifdef P_tmpdir
1199  if ((bool)P_tmpdir)
1200    return P_tmpdir;
1201#endif
1202
1203  if (ErasedOnReboot)
1204    return "/tmp";
1205  return "/var/tmp";
1206}
1207
1208void system_temp_directory(bool ErasedOnReboot, SmallVectorImpl<char> &Result) {
1209  Result.clear();
1210
1211  if (ErasedOnReboot) {
1212    // There is no env variable for the cache directory.
1213    if (const char *RequestedDir = getEnvTempDir()) {
1214      Result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
1215      return;
1216    }
1217  }
1218
1219  if (getDarwinConfDir(ErasedOnReboot, Result))
1220    return;
1221
1222  const char *RequestedDir = getDefaultTempDir(ErasedOnReboot);
1223  Result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
1224}
1225
1226} // end namespace path
1227
1228namespace fs {
1229
1230#ifdef __APPLE__
1231/// This implementation tries to perform an APFS CoW clone of the file,
1232/// which can be much faster and uses less space.
1233/// Unfortunately fcopyfile(3) does not support COPYFILE_CLONE, so the
1234/// file descriptor variant of this function still uses the default
1235/// implementation.
1236std::error_code copy_file(const Twine &From, const Twine &To) {
1237  uint32_t Flag = COPYFILE_DATA;
1238#if __has_builtin(__builtin_available) && defined(COPYFILE_CLONE)
1239  if (__builtin_available(macos 10.12, *)) {
1240    bool IsSymlink;
1241    if (std::error_code Error = is_symlink_file(From, IsSymlink))
1242      return Error;
1243    // COPYFILE_CLONE clones the symlink instead of following it
1244    // and returns EEXISTS if the target file already exists.
1245    if (!IsSymlink && !exists(To))
1246      Flag = COPYFILE_CLONE;
1247  }
1248#endif
1249  int Status =
1250      copyfile(From.str().c_str(), To.str().c_str(), /* State */ NULL, Flag);
1251
1252  if (Status == 0)
1253    return std::error_code();
1254  return std::error_code(errno, std::generic_category());
1255}
1256#endif // __APPLE__
1257
1258} // end namespace fs
1259
1260} // end namespace sys
1261} // end namespace llvm
1262