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