Rafael Espindola | f1fc382 | 2013-06-26 19:33:03 +0000 | [diff] [blame] | 1 | //===- llvm/Support/Unix/Path.inc - Unix Path Implementation ----*- C++ -*-===// |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Rafael Espindola | f1fc382 | 2013-06-26 19:33:03 +0000 | [diff] [blame] | 9 | // This file implements the Unix specific implementation of the Path API. |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 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" |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 19 | #include <limits.h> |
| 20 | #include <stdio.h> |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 21 | #if HAVE_SYS_STAT_H |
| 22 | #include <sys/stat.h> |
| 23 | #endif |
| 24 | #if HAVE_FCNTL_H |
| 25 | #include <fcntl.h> |
| 26 | #endif |
Taewook Oh | d915327 | 2016-06-13 15:54:56 +0000 | [diff] [blame] | 27 | #ifdef HAVE_UNISTD_H |
| 28 | #include <unistd.h> |
| 29 | #endif |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 30 | #ifdef HAVE_SYS_MMAN_H |
| 31 | #include <sys/mman.h> |
| 32 | #endif |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 33 | |
Nico Weber | f3db8e3 | 2018-04-02 17:17:29 +0000 | [diff] [blame] | 34 | #include <dirent.h> |
Zachary Turner | e48ace6 | 2017-03-10 17:39:21 +0000 | [diff] [blame] | 35 | #include <pwd.h> |
| 36 | |
Rafael Espindola | 4601c46 | 2013-06-26 05:25:44 +0000 | [diff] [blame] | 37 | #ifdef __APPLE__ |
| 38 | #include <mach-o/dyld.h> |
Taewook Oh | d915327 | 2016-06-13 15:54:56 +0000 | [diff] [blame] | 39 | #include <sys/attr.h> |
Adrian Prantl | fac7875 | 2019-04-18 21:22:50 +0000 | [diff] [blame] | 40 | #include <copyfile.h> |
Ed Maste | a0a38b8 | 2019-11-13 13:24:23 -0500 | [diff] [blame] | 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> |
| 47 | extern char **environ; |
| 48 | #endif |
David Carlier | b79eee4 | 2018-11-10 01:01:03 +0000 | [diff] [blame] | 49 | #elif defined(__DragonFly__) |
| 50 | #include <sys/mount.h> |
Rafael Espindola | 4601c46 | 2013-06-26 05:25:44 +0000 | [diff] [blame] | 51 | #endif |
| 52 | |
Simon Pilgrim | f2fbf43 | 2016-11-20 13:47:59 +0000 | [diff] [blame] | 53 | // Both stdio.h and cstdio are included via different paths and |
Joerg Sonnenberger | c069730 | 2012-08-10 10:56:09 +0000 | [diff] [blame] | 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 | |
Sylvestre Ledru | 14ada94 | 2012-04-11 15:35:36 +0000 | [diff] [blame] | 59 | // For GNU Hurd |
| 60 | #if defined(__GNU__) && !defined(PATH_MAX) |
| 61 | # define PATH_MAX 4096 |
Sylvestre Ledru | f4719c4 | 2018-10-23 07:13:47 +0000 | [diff] [blame] | 62 | # define MAXPATHLEN 4096 |
Sylvestre Ledru | 14ada94 | 2012-04-11 15:35:36 +0000 | [diff] [blame] | 63 | #endif |
| 64 | |
Mehdi Amini | e2d8f1b | 2016-04-01 00:18:08 +0000 | [diff] [blame] | 65 | #include <sys/types.h> |
Zachary Turner | 392ed9d | 2017-02-21 20:55:47 +0000 | [diff] [blame] | 66 | #if !defined(__APPLE__) && !defined(__OpenBSD__) && !defined(__FreeBSD__) && \ |
Hubert Tong | 2416885 | 2019-03-29 23:32:47 +0000 | [diff] [blame] | 67 | !defined(__linux__) && !defined(__FreeBSD_kernel__) && !defined(_AIX) |
Mehdi Amini | e2d8f1b | 2016-04-01 00:18:08 +0000 | [diff] [blame] | 68 | #include <sys/statvfs.h> |
| 69 | #define STATVFS statvfs |
Zachary Turner | 392ed9d | 2017-02-21 20:55:47 +0000 | [diff] [blame] | 70 | #define FSTATVFS fstatvfs |
Mehdi Amini | e2d8f1b | 2016-04-01 00:18:08 +0000 | [diff] [blame] | 71 | #define STATVFS_F_FRSIZE(vfs) vfs.f_frsize |
| 72 | #else |
Zachary Turner | 392ed9d | 2017-02-21 20:55:47 +0000 | [diff] [blame] | 73 | #if defined(__OpenBSD__) || defined(__FreeBSD__) |
Mehdi Amini | 3ba84ca | 2016-04-08 16:45:05 +0000 | [diff] [blame] | 74 | #include <sys/mount.h> |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 75 | #include <sys/param.h> |
Zachary Turner | 392ed9d | 2017-02-21 20:55:47 +0000 | [diff] [blame] | 76 | #elif defined(__linux__) |
Michal Gorny | 5ddd2a5 | 2017-02-22 18:09:15 +0000 | [diff] [blame] | 77 | #if defined(HAVE_LINUX_MAGIC_H) |
Zachary Turner | 392ed9d | 2017-02-21 20:55:47 +0000 | [diff] [blame] | 78 | #include <linux/magic.h> |
Michal Gorny | 5ddd2a5 | 2017-02-22 18:09:15 +0000 | [diff] [blame] | 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 |
Mehdi Amini | e2d8f1b | 2016-04-01 00:18:08 +0000 | [diff] [blame] | 87 | #include <sys/vfs.h> |
Hubert Tong | 2416885 | 2019-03-29 23:32:47 +0000 | [diff] [blame] | 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. |
| 94 | typedef uint_t uint; |
| 95 | #include <sys/vmount.h> |
Mehdi Amini | e2d8f1b | 2016-04-01 00:18:08 +0000 | [diff] [blame] | 96 | #else |
| 97 | #include <sys/mount.h> |
| 98 | #endif |
| 99 | #define STATVFS statfs |
Zachary Turner | 392ed9d | 2017-02-21 20:55:47 +0000 | [diff] [blame] | 100 | #define FSTATVFS fstatfs |
Mehdi Amini | e2d8f1b | 2016-04-01 00:18:08 +0000 | [diff] [blame] | 101 | #define STATVFS_F_FRSIZE(vfs) static_cast<uint64_t>(vfs.f_bsize) |
| 102 | #endif |
| 103 | |
David Carlier | b79eee4 | 2018-11-10 01:01:03 +0000 | [diff] [blame] | 104 | #if defined(__NetBSD__) || defined(__DragonFly__) || defined(__GNU__) |
Zachary Turner | 392ed9d | 2017-02-21 20:55:47 +0000 | [diff] [blame] | 105 | #define STATVFS_F_FLAG(vfs) (vfs).f_flag |
| 106 | #else |
| 107 | #define STATVFS_F_FLAG(vfs) (vfs).f_flags |
| 108 | #endif |
Mehdi Amini | e2d8f1b | 2016-04-01 00:18:08 +0000 | [diff] [blame] | 109 | |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 110 | using namespace llvm; |
| 111 | |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 112 | namespace llvm { |
| 113 | namespace sys { |
Michael J. Spencer | 20daa28 | 2010-12-07 01:22:31 +0000 | [diff] [blame] | 114 | namespace fs { |
Zachary Turner | 63db25b | 2018-06-04 19:38:11 +0000 | [diff] [blame] | 115 | |
| 116 | const file_t kInvalidFile = -1; |
| 117 | |
Michal Gorny | 2d8be64 | 2019-03-04 04:53:50 +0000 | [diff] [blame] | 118 | #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \ |
Erich Keane | d8f61f8 | 2017-07-21 22:48:47 +0000 | [diff] [blame] | 119 | defined(__minix) || defined(__FreeBSD_kernel__) || defined(__linux__) || \ |
Sylvestre Ledru | f4719c4 | 2018-10-23 07:13:47 +0000 | [diff] [blame] | 120 | defined(__CYGWIN__) || defined(__DragonFly__) || defined(_AIX) || defined(__GNU__) |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 121 | static int |
Sylvestre Ledru | 21e67472 | 2013-12-09 16:27:00 +0000 | [diff] [blame] | 122 | test_dir(char ret[PATH_MAX], const char *dir, const char *bin) |
Mehdi Amini | e2d8f1b | 2016-04-01 00:18:08 +0000 | [diff] [blame] | 123 | { |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 124 | struct stat sb; |
Sylvestre Ledru | 21e67472 | 2013-12-09 16:27:00 +0000 | [diff] [blame] | 125 | char fullpath[PATH_MAX]; |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 126 | |
Jonas Hahnfeld | e59746f | 2019-03-13 10:37:56 +0000 | [diff] [blame] | 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; |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 132 | if (!realpath(fullpath, ret)) |
| 133 | return 1; |
Sylvestre Ledru | 21e67472 | 2013-12-09 16:27:00 +0000 | [diff] [blame] | 134 | if (stat(fullpath, &sb) != 0) |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 135 | return 1; |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 136 | |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 137 | return 0; |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 140 | static char * |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 141 | getprogpath(char ret[PATH_MAX], const char *bin) |
| 142 | { |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 143 | /* First approach: absolute path. */ |
| 144 | if (bin[0] == '/') { |
Sylvestre Ledru | 21e67472 | 2013-12-09 16:27:00 +0000 | [diff] [blame] | 145 | if (test_dir(ret, "/", bin) == 0) |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 146 | return ret; |
| 147 | return nullptr; |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /* Second approach: relative path. */ |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 151 | if (strchr(bin, '/')) { |
Sylvestre Ledru | 21e67472 | 2013-12-09 16:27:00 +0000 | [diff] [blame] | 152 | char cwd[PATH_MAX]; |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 153 | if (!getcwd(cwd, PATH_MAX)) |
| 154 | return nullptr; |
Sylvestre Ledru | 21e67472 | 2013-12-09 16:27:00 +0000 | [diff] [blame] | 155 | if (test_dir(ret, cwd, bin) == 0) |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 156 | return ret; |
| 157 | return nullptr; |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /* Third approach: $PATH */ |
Xing Xue | 2dee094 | 2019-05-16 14:02:13 +0000 | [diff] [blame] | 161 | char *pv; |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 162 | if ((pv = getenv("PATH")) == nullptr) |
| 163 | return nullptr; |
Xing Xue | 2dee094 | 2019-05-16 14:02:13 +0000 | [diff] [blame] | 164 | char *s = strdup(pv); |
| 165 | if (!s) |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 166 | return nullptr; |
Xing Xue | 2dee094 | 2019-05-16 14:02:13 +0000 | [diff] [blame] | 167 | char *state; |
| 168 | for (char *t = strtok_r(s, ":", &state); t != nullptr; |
| 169 | t = strtok_r(nullptr, ":", &state)) { |
Sylvestre Ledru | 21e67472 | 2013-12-09 16:27:00 +0000 | [diff] [blame] | 170 | if (test_dir(ret, t, bin) == 0) { |
Xing Xue | 2dee094 | 2019-05-16 14:02:13 +0000 | [diff] [blame] | 171 | free(s); |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 172 | return ret; |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
Xing Xue | 2dee094 | 2019-05-16 14:02:13 +0000 | [diff] [blame] | 175 | free(s); |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 176 | return nullptr; |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 177 | } |
Dimitry Andric | ebc8779 | 2017-05-17 19:33:10 +0000 | [diff] [blame] | 178 | #endif // __FreeBSD__ || __NetBSD__ || __FreeBSD_kernel__ |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 179 | |
| 180 | /// GetMainExecutable - Return the path to the main executable, given the |
| 181 | /// value of argv[0] from program startup. |
| 182 | std::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)) |
Rafael Espindola | 4601c46 | 2013-06-26 05:25:44 +0000 | [diff] [blame] | 192 | return link_path; |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 193 | } |
Ed Maste | a0a38b8 | 2019-11-13 13:24:23 -0500 | [diff] [blame] | 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 |
Ed Maste | a0a38b8 | 2019-11-13 13:24:23 -0500 | [diff] [blame] | 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) |
Nico Weber | 1919317 | 2019-08-07 17:00:19 +0000 | [diff] [blame] | 226 | const char *curproc = "/proc/curproc/file"; |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 227 | char exe_path[PATH_MAX]; |
David Chisnall | 714a442 | 2019-04-29 09:24:51 +0000 | [diff] [blame] | 228 | if (sys::fs::exists(curproc)) { |
Nico Weber | 1919317 | 2019-08-07 17:00:19 +0000 | [diff] [blame] | 229 | ssize_t len = readlink(curproc, exe_path, sizeof(exe_path)); |
David Chisnall | 714a442 | 2019-04-29 09:24:51 +0000 | [diff] [blame] | 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] |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 239 | if (getprogpath(exe_path, argv0) != NULL) |
Rafael Espindola | 2c6f4fe | 2013-06-26 06:10:32 +0000 | [diff] [blame] | 240 | return exe_path; |
kristina | f426712 | 2019-11-01 17:27:24 +0000 | [diff] [blame] | 241 | #elif defined(__linux__) || defined(__CYGWIN__) || defined(__gnu_hurd__) |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 242 | char exe_path[MAXPATHLEN]; |
Nico Weber | 1919317 | 2019-08-07 17:00:19 +0000 | [diff] [blame] | 243 | const char *aPath = "/proc/self/exe"; |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 244 | if (sys::fs::exists(aPath)) { |
Reid Kleckner | 1500eff | 2018-10-23 23:35:43 +0000 | [diff] [blame] | 245 | // /proc is not always mounted under Linux (chroot for example). |
Nico Weber | 1919317 | 2019-08-07 17:00:19 +0000 | [diff] [blame] | 246 | ssize_t len = readlink(aPath, exe_path, sizeof(exe_path)); |
Reid Kleckner | 1500eff | 2018-10-23 23:35:43 +0000 | [diff] [blame] | 247 | if (len < 0) |
| 248 | return ""; |
| 249 | |
| 250 | // Null terminate the string for realpath. readlink never null |
| 251 | // terminates its output. |
Reid Kleckner | 5fa1e35 | 2018-10-23 23:44:44 +0000 | [diff] [blame] | 252 | len = std::min(len, ssize_t(sizeof(exe_path) - 1)); |
Reid Kleckner | 1500eff | 2018-10-23 23:35:43 +0000 | [diff] [blame] | 253 | exe_path[len] = '\0'; |
| 254 | |
Reid Kleckner | 5fa1e35 | 2018-10-23 23:44:44 +0000 | [diff] [blame] | 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. |
Reid Kleckner | 1500eff | 2018-10-23 23:35:43 +0000 | [diff] [blame] | 259 | #if _POSIX_VERSION >= 200112 || defined(__GLIBC__) |
Sam McCall | edf904e | 2019-07-02 15:42:37 +0000 | [diff] [blame] | 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 | } |
Reid Kleckner | 1500eff | 2018-10-23 23:35:43 +0000 | [diff] [blame] | 265 | #else |
| 266 | char real_path[MAXPATHLEN]; |
Sam McCall | edf904e | 2019-07-02 15:42:37 +0000 | [diff] [blame] | 267 | if (realpath(exe_path, real_path)) |
| 268 | return std::string(real_path); |
Reid Kleckner | 1500eff | 2018-10-23 23:35:43 +0000 | [diff] [blame] | 269 | #endif |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 270 | } |
Sam McCall | edf904e | 2019-07-02 15:42:37 +0000 | [diff] [blame] | 271 | // Fall back to the classical detection. |
| 272 | if (getprogpath(exe_path, argv0)) |
| 273 | return exe_path; |
Omair Javaid | f5d560bc84 | 2017-02-02 01:17:49 +0000 | [diff] [blame] | 274 | #elif defined(HAVE_DLFCN_H) && defined(HAVE_DLADDR) |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 275 | // Use dladdr to get executable path if available. |
| 276 | Dl_info DLInfo; |
| 277 | int err = dladdr(MainAddr, &DLInfo); |
| 278 | if (err == 0) |
Rafael Espindola | 2c6f4fe | 2013-06-26 06:10:32 +0000 | [diff] [blame] | 279 | return ""; |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 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)) |
Rafael Espindola | 2c6f4fe | 2013-06-26 06:10:32 +0000 | [diff] [blame] | 285 | return link_path; |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 286 | #else |
| 287 | #error GetMainExecutable is not implemented on this host yet. |
| 288 | #endif |
| 289 | return ""; |
| 290 | } |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 291 | |
Peter Collingbourne | 0dfdb44 | 2017-10-10 22:19:46 +0000 | [diff] [blame] | 292 | TimePoint<> basic_file_status::getLastAccessedTime() const { |
Argyrios Kyrtzidis | 5167c13 | 2018-11-26 00:03:39 +0000 | [diff] [blame] | 293 | return toTimePoint(fs_st_atime, fs_st_atime_nsec); |
Mehdi Amini | 1e39ef3 | 2016-03-25 07:30:21 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Peter Collingbourne | 0dfdb44 | 2017-10-10 22:19:46 +0000 | [diff] [blame] | 296 | TimePoint<> basic_file_status::getLastModificationTime() const { |
Argyrios Kyrtzidis | 5167c13 | 2018-11-26 00:03:39 +0000 | [diff] [blame] | 297 | return toTimePoint(fs_st_mtime, fs_st_mtime_nsec); |
Rafael Espindola | db5d8fe | 2013-06-20 18:42:04 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Rafael Espindola | d123099 | 2013-07-29 21:55:38 +0000 | [diff] [blame] | 300 | UniqueID file_status::getUniqueID() const { |
Rafael Espindola | 7f822a9 | 2013-07-29 21:26:49 +0000 | [diff] [blame] | 301 | return UniqueID(fs_st_dev, fs_st_ino); |
| 302 | } |
| 303 | |
Zachary Turner | 5821a3b | 2017-03-20 23:55:20 +0000 | [diff] [blame] | 304 | uint32_t file_status::getLinkCount() const { |
| 305 | return fs_st_nlinks; |
| 306 | } |
| 307 | |
Mehdi Amini | e2d8f1b | 2016-04-01 00:18:08 +0000 | [diff] [blame] | 308 | ErrorOr<space_info> disk_space(const Twine &Path) { |
| 309 | struct STATVFS Vfs; |
Hubert Tong | 2416885 | 2019-03-29 23:32:47 +0000 | [diff] [blame] | 310 | if (::STATVFS(const_cast<char *>(Path.str().c_str()), &Vfs)) |
Mehdi Amini | e2d8f1b | 2016-04-01 00:18:08 +0000 | [diff] [blame] | 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 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 320 | std::error_code current_path(SmallVectorImpl<char> &result) { |
Rafael Espindola | 6ee1638 | 2013-08-10 00:50:57 +0000 | [diff] [blame] | 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)); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 330 | return std::error_code(); |
Rafael Espindola | 6ee1638 | 2013-08-10 00:50:57 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Sylvestre Ledru | 14ada94 | 2012-04-11 15:35:36 +0000 | [diff] [blame] | 333 | #ifdef MAXPATHLEN |
Andrew Trick | a4ec5b2 | 2011-03-24 16:43:37 +0000 | [diff] [blame] | 334 | result.reserve(MAXPATHLEN); |
Sylvestre Ledru | 14ada94 | 2012-04-11 15:35:36 +0000 | [diff] [blame] | 335 | #else |
| 336 | // For GNU Hurd |
| 337 | result.reserve(1024); |
| 338 | #endif |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 339 | |
Michael J. Spencer | 20daa28 | 2010-12-07 01:22:31 +0000 | [diff] [blame] | 340 | while (true) { |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 341 | if (::getcwd(result.data(), result.capacity()) == nullptr) { |
Michael J. Spencer | 20daa28 | 2010-12-07 01:22:31 +0000 | [diff] [blame] | 342 | // See if there was a real error. |
Rafael Espindola | 882ce87b | 2014-05-31 02:29:28 +0000 | [diff] [blame] | 343 | if (errno != ENOMEM) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 344 | return std::error_code(errno, std::generic_category()); |
Michael J. Spencer | 20daa28 | 2010-12-07 01:22:31 +0000 | [diff] [blame] | 345 | // Otherwise there just wasn't enough space. |
| 346 | result.reserve(result.capacity() * 2); |
| 347 | } else |
| 348 | break; |
| 349 | } |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 350 | |
| 351 | result.set_size(strlen(result.data())); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 352 | return std::error_code(); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Pavel Labath | 2f09609 | 2017-01-24 10:32:03 +0000 | [diff] [blame] | 355 | std::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 | |
Frederic Riss | 6b9396c | 2015-08-06 21:04:55 +0000 | [diff] [blame] | 365 | std::error_code create_directory(const Twine &path, bool IgnoreExisting, |
| 366 | perms Perms) { |
Michael J. Spencer | 31e310c | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 367 | SmallString<128> path_storage; |
| 368 | StringRef p = path.toNullTerminatedStringRef(path_storage); |
| 369 | |
Frederic Riss | 6b9396c | 2015-08-06 21:04:55 +0000 | [diff] [blame] | 370 | if (::mkdir(p.begin(), Perms) == -1) { |
Rafael Espindola | 882ce87b | 2014-05-31 02:29:28 +0000 | [diff] [blame] | 371 | if (errno != EEXIST || !IgnoreExisting) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 372 | return std::error_code(errno, std::generic_category()); |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 373 | } |
Michael J. Spencer | 31e310c | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 374 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 375 | return std::error_code(); |
Michael J. Spencer | 31e310c | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Rafael Espindola | 83f858e | 2014-03-11 18:40:24 +0000 | [diff] [blame] | 378 | // Note that we are using symbolic link because hard links are not supported by |
| 379 | // all filesystems (SMB doesn't). |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 380 | std::error_code create_link(const Twine &to, const Twine &from) { |
Michael J. Spencer | e0c4560 | 2010-12-03 05:58:41 +0000 | [diff] [blame] | 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 | |
Rafael Espindola | 83f858e | 2014-03-11 18:40:24 +0000 | [diff] [blame] | 387 | if (::symlink(t.begin(), f.begin()) == -1) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 388 | return std::error_code(errno, std::generic_category()); |
Michael J. Spencer | e0c4560 | 2010-12-03 05:58:41 +0000 | [diff] [blame] | 389 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 390 | return std::error_code(); |
Michael J. Spencer | e0c4560 | 2010-12-03 05:58:41 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Mehdi Amini | 8e13bc4 | 2016-12-14 04:56:42 +0000 | [diff] [blame] | 393 | std::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 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 406 | std::error_code remove(const Twine &path, bool IgnoreNonExisting) { |
Michael J. Spencer | 6e74e11 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 407 | SmallString<128> path_storage; |
| 408 | StringRef p = path.toNullTerminatedStringRef(path_storage); |
| 409 | |
Rafael Espindola | 8cd62b0 | 2013-06-17 20:35:51 +0000 | [diff] [blame] | 410 | struct stat buf; |
Argyrios Kyrtzidis | 3757569 | 2014-03-21 01:25:37 +0000 | [diff] [blame] | 411 | if (lstat(p.begin(), &buf) != 0) { |
Rafael Espindola | 882ce87b | 2014-05-31 02:29:28 +0000 | [diff] [blame] | 412 | if (errno != ENOENT || !IgnoreNonExisting) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 413 | return std::error_code(errno, std::generic_category()); |
| 414 | return std::error_code(); |
Rafael Espindola | 8cd62b0 | 2013-06-17 20:35:51 +0000 | [diff] [blame] | 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. |
Argyrios Kyrtzidis | 3757569 | 2014-03-21 01:25:37 +0000 | [diff] [blame] | 422 | if (!S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode) && !S_ISLNK(buf.st_mode)) |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 423 | return make_error_code(errc::operation_not_permitted); |
Rafael Espindola | 8cd62b0 | 2013-06-17 20:35:51 +0000 | [diff] [blame] | 424 | |
Michael J. Spencer | 6e74e11 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 425 | if (::remove(p.begin()) == -1) { |
Rafael Espindola | 882ce87b | 2014-05-31 02:29:28 +0000 | [diff] [blame] | 426 | if (errno != ENOENT || !IgnoreNonExisting) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 427 | return std::error_code(errno, std::generic_category()); |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 428 | } |
Michael J. Spencer | 6e74e11 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 429 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 430 | return std::error_code(); |
Michael J. Spencer | 6e74e11 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Zachary Turner | 392ed9d | 2017-02-21 20:55:47 +0000 | [diff] [blame] | 433 | static bool is_local_impl(struct STATVFS &Vfs) { |
Sylvestre Ledru | f4719c4 | 2018-10-23 07:13:47 +0000 | [diff] [blame] | 434 | #if defined(__linux__) || defined(__GNU__) |
Michal Gorny | 5ddd2a5 | 2017-02-22 18:09:15 +0000 | [diff] [blame] | 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 |
Zachary Turner | 6bc2dac | 2017-02-21 21:13:10 +0000 | [diff] [blame] | 441 | #ifndef CIFS_MAGIC_NUMBER |
| 442 | #define CIFS_MAGIC_NUMBER 0xFF534D42 |
| 443 | #endif |
Sylvestre Ledru | f4719c4 | 2018-10-23 07:13:47 +0000 | [diff] [blame] | 444 | #ifdef __GNU__ |
| 445 | switch ((uint32_t)Vfs.__f_type) { |
| 446 | #else |
Zachary Turner | 392ed9d | 2017-02-21 20:55:47 +0000 | [diff] [blame] | 447 | switch ((uint32_t)Vfs.f_type) { |
Sylvestre Ledru | f4719c4 | 2018-10-23 07:13:47 +0000 | [diff] [blame] | 448 | #endif |
Zachary Turner | 392ed9d | 2017-02-21 20:55:47 +0000 | [diff] [blame] | 449 | case NFS_SUPER_MAGIC: |
| 450 | case SMB_SUPER_MAGIC: |
| 451 | case CIFS_MAGIC_NUMBER: |
| 452 | return false; |
| 453 | default: |
| 454 | return true; |
| 455 | } |
Nuno Lopes | 7a1bbd4 | 2017-03-09 13:43:31 +0000 | [diff] [blame] | 456 | #elif defined(__CYGWIN__) |
| 457 | // Cygwin doesn't expose this information; would need to use Win32 API. |
| 458 | return false; |
Petr Hosek | 87f1343 | 2018-05-03 01:38:49 +0000 | [diff] [blame] | 459 | #elif defined(__Fuchsia__) |
| 460 | // Fuchsia doesn't yet support remote filesystem mounts. |
| 461 | return true; |
Keno Fischer | 5f4ae7c | 2019-06-23 00:29:59 +0000 | [diff] [blame] | 462 | #elif defined(__EMSCRIPTEN__) |
| 463 | // Emscripten doesn't currently support remote filesystem mounts. |
| 464 | return true; |
Tim Northover | 43d64b0 | 2018-07-18 13:42:18 +0000 | [diff] [blame] | 465 | #elif defined(__HAIKU__) |
| 466 | // Haiku doesn't expose this information. |
| 467 | return false; |
Kamil Rytarowski | 07c81b1 | 2017-06-01 12:57:00 +0000 | [diff] [blame] | 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"); |
Hubert Tong | 2416885 | 2019-03-29 23:32:47 +0000 | [diff] [blame] | 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--) { |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 481 | Buf = std::make_unique<char[]>(BufSize); |
Hubert Tong | 2416885 | 2019-03-29 23:32:47 +0000 | [diff] [blame] | 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; |
Zachary Turner | 392ed9d | 2017-02-21 20:55:47 +0000 | [diff] [blame] | 507 | #else |
| 508 | return !!(STATVFS_F_FLAG(Vfs) & MNT_LOCAL); |
| 509 | #endif |
| 510 | } |
| 511 | |
| 512 | std::error_code is_local(const Twine &Path, bool &Result) { |
| 513 | struct STATVFS Vfs; |
Hubert Tong | 2416885 | 2019-03-29 23:32:47 +0000 | [diff] [blame] | 514 | if (::STATVFS(const_cast<char *>(Path.str().c_str()), &Vfs)) |
Zachary Turner | 392ed9d | 2017-02-21 20:55:47 +0000 | [diff] [blame] | 515 | return std::error_code(errno, std::generic_category()); |
| 516 | |
| 517 | Result = is_local_impl(Vfs); |
| 518 | return std::error_code(); |
| 519 | } |
| 520 | |
| 521 | std::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 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 530 | std::error_code rename(const Twine &from, const Twine &to) { |
Michael J. Spencer | 409f556 | 2010-12-03 17:53:55 +0000 | [diff] [blame] | 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 | |
Rafael Espindola | b6fea4c | 2013-07-17 03:33:41 +0000 | [diff] [blame] | 537 | if (::rename(f.begin(), t.begin()) == -1) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 538 | return std::error_code(errno, std::generic_category()); |
Michael J. Spencer | 409f556 | 2010-12-03 17:53:55 +0000 | [diff] [blame] | 539 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 540 | return std::error_code(); |
Michael J. Spencer | 409f556 | 2010-12-03 17:53:55 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Rafael Espindola | 59aaa6c | 2014-12-12 17:55:12 +0000 | [diff] [blame] | 543 | std::error_code resize_file(int FD, uint64_t Size) { |
Rafael Espindola | 3816c53 | 2016-07-19 20:19:56 +0000 | [diff] [blame] | 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. |
Joerg Sonnenberger | ecfb876 | 2017-05-05 17:55:58 +0000 | [diff] [blame] | 547 | if (int Err = ::posix_fallocate(FD, 0, Size)) { |
Hubert Tong | 7f8b3bf | 2019-04-04 00:40:34 +0000 | [diff] [blame] | 548 | #ifdef _AIX |
| 549 | constexpr int NotSupportedError = ENOTSUP; |
| 550 | #else |
| 551 | constexpr int NotSupportedError = EOPNOTSUPP; |
| 552 | #endif |
| 553 | if (Err != EINVAL && Err != NotSupportedError) |
Joerg Sonnenberger | ecfb876 | 2017-05-05 17:55:58 +0000 | [diff] [blame] | 554 | return std::error_code(Err, std::generic_category()); |
| 555 | } |
| 556 | #endif |
Rafael Espindola | 3816c53 | 2016-07-19 20:19:56 +0000 | [diff] [blame] | 557 | // Use ftruncate as a fallback. It may or may not allocate space. At least on |
| 558 | // OS X with HFS+ it does. |
Rafael Espindola | 59aaa6c | 2014-12-12 17:55:12 +0000 | [diff] [blame] | 559 | if (::ftruncate(FD, Size) == -1) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 560 | return std::error_code(errno, std::generic_category()); |
Michael J. Spencer | c20a032 | 2010-12-03 17:54:07 +0000 | [diff] [blame] | 561 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 562 | return std::error_code(); |
Michael J. Spencer | c20a032 | 2010-12-03 17:54:07 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 565 | static int convertAccessMode(AccessMode Mode) { |
Rafael Espindola | 281f23a | 2014-09-11 20:30:02 +0000 | [diff] [blame] | 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 | } |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 576 | |
Rafael Espindola | 281f23a | 2014-09-11 20:30:02 +0000 | [diff] [blame] | 577 | std::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 | } |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 592 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 593 | return std::error_code(); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Reid Kleckner | 89d4b1a | 2015-09-10 23:28:06 +0000 | [diff] [blame] | 596 | bool can_execute(const Twine &Path) { |
| 597 | return !access(Path, AccessMode::Execute); |
| 598 | } |
| 599 | |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 600 | bool equivalent(file_status A, file_status B) { |
| 601 | assert(status_known(A) && status_known(B)); |
Sylvestre Ledru | 3099f4bd | 2012-04-23 16:37:23 +0000 | [diff] [blame] | 602 | return A.fs_st_dev == B.fs_st_dev && |
| 603 | A.fs_st_ino == B.fs_st_ino; |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 606 | std::error_code equivalent(const Twine &A, const Twine &B, bool &result) { |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 607 | file_status fsA, fsB; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 608 | if (std::error_code ec = status(A, fsA)) |
| 609 | return ec; |
| 610 | if (std::error_code ec = status(B, fsB)) |
| 611 | return ec; |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 612 | result = equivalent(fsA, fsB); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 613 | return std::error_code(); |
Michael J. Spencer | 376d387 | 2010-12-03 18:49:13 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Zachary Turner | e48ace6 | 2017-03-10 17:39:21 +0000 | [diff] [blame] | 616 | static 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(); |
Zachary Turner | 1875334 | 2017-03-16 22:34:18 +0000 | [diff] [blame] | 622 | StringRef Expr = |
| 623 | PathStr.take_until([](char c) { return path::is_separator(c); }); |
Zachary Turner | e48ace6 | 2017-03-10 17:39:21 +0000 | [diff] [blame] | 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 | |
Jonas Devlieghere | b23f430 | 2018-11-13 18:23:32 +0000 | [diff] [blame] | 656 | |
| 657 | void 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 | |
Kristina Brooks | 3a55d1e | 2018-09-12 22:08:10 +0000 | [diff] [blame] | 668 | static 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 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 686 | static std::error_code fillStatus(int StatRet, const struct stat &Status, |
Fangrui Song | 69d6418 | 2018-06-10 04:53:14 +0000 | [diff] [blame] | 687 | file_status &Result) { |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 688 | if (StatRet != 0) { |
Kristina Brooks | 3a55d1e | 2018-09-12 22:08:10 +0000 | [diff] [blame] | 689 | std::error_code EC(errno, std::generic_category()); |
| 690 | if (EC == errc::no_such_file_or_directory) |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 691 | Result = file_status(file_type::file_not_found); |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 692 | else |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 693 | Result = file_status(file_type::status_error); |
Kristina Brooks | 3a55d1e | 2018-09-12 22:08:10 +0000 | [diff] [blame] | 694 | return EC; |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Argyrios Kyrtzidis | 5167c13 | 2018-11-26 00:03:39 +0000 | [diff] [blame] | 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 | |
James Henderson | 566fdf4 | 2017-03-16 11:22:09 +0000 | [diff] [blame] | 708 | perms Perms = static_cast<perms>(Status.st_mode) & all_perms; |
Kristina Brooks | 3a55d1e | 2018-09-12 22:08:10 +0000 | [diff] [blame] | 709 | Result = file_status(typeForMode(Status.st_mode), Perms, Status.st_dev, |
Argyrios Kyrtzidis | 5167c13 | 2018-11-26 00:03:39 +0000 | [diff] [blame] | 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); |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 713 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 714 | return std::error_code(); |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Zachary Turner | 82dd542 | 2017-03-07 16:10:10 +0000 | [diff] [blame] | 717 | std::error_code status(const Twine &Path, file_status &Result, bool Follow) { |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 718 | SmallString<128> PathStorage; |
| 719 | StringRef P = Path.toNullTerminatedStringRef(PathStorage); |
| 720 | |
| 721 | struct stat Status; |
Zachary Turner | 82dd542 | 2017-03-07 16:10:10 +0000 | [diff] [blame] | 722 | int StatRet = (Follow ? ::stat : ::lstat)(P.begin(), &Status); |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 723 | return fillStatus(StatRet, Status, Result); |
| 724 | } |
| 725 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 726 | std::error_code status(int FD, file_status &Result) { |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 727 | struct stat Status; |
| 728 | int StatRet = ::fstat(FD, &Status); |
Aaron Ballman | 345012d | 2017-03-13 12:24:51 +0000 | [diff] [blame] | 729 | return fillStatus(StatRet, Status, Result); |
| 730 | } |
| 731 | |
Alex Brachet | 3b715d6 | 2019-06-28 03:21:00 +0000 | [diff] [blame] | 732 | unsigned 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 | |
Fangrui Song | 6dc5962 | 2019-07-11 10:17:59 +0000 | [diff] [blame] | 740 | std::error_code setPermissions(const Twine &Path, perms Permissions) { |
James Henderson | 566fdf4 | 2017-03-16 11:22:09 +0000 | [diff] [blame] | 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 | |
Fangrui Song | 6dc5962 | 2019-07-11 10:17:59 +0000 | [diff] [blame] | 749 | std::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 | |
Jordan Rupprecht | 97ea485 | 2018-08-13 23:03:45 +0000 | [diff] [blame] | 755 | std::error_code setLastAccessAndModificationTime(int FD, TimePoint<> AccessTime, |
| 756 | TimePoint<> ModificationTime) { |
Aaron Ballman | 345012d | 2017-03-13 12:24:51 +0000 | [diff] [blame] | 757 | #if defined(HAVE_FUTIMENS) |
| 758 | timespec Times[2]; |
Jordan Rupprecht | 97ea485 | 2018-08-13 23:03:45 +0000 | [diff] [blame] | 759 | Times[0] = sys::toTimeSpec(AccessTime); |
| 760 | Times[1] = sys::toTimeSpec(ModificationTime); |
Eric Christopher | a24dc7f | 2013-07-04 01:10:38 +0000 | [diff] [blame] | 761 | if (::futimens(FD, Times)) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 762 | return std::error_code(errno, std::generic_category()); |
| 763 | return std::error_code(); |
Eric Christopher | a24dc7f | 2013-07-04 01:10:38 +0000 | [diff] [blame] | 764 | #elif defined(HAVE_FUTIMES) |
Rafael Espindola | 4a3365c | 2013-06-20 20:56:14 +0000 | [diff] [blame] | 765 | timeval Times[2]; |
Jordan Rupprecht | 97ea485 | 2018-08-13 23:03:45 +0000 | [diff] [blame] | 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)); |
Rafael Espindola | 4a3365c | 2013-06-20 20:56:14 +0000 | [diff] [blame] | 771 | if (::futimes(FD, Times)) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 772 | return std::error_code(errno, std::generic_category()); |
| 773 | return std::error_code(); |
Alp Toker | b30f01e | 2013-12-11 15:42:33 +0000 | [diff] [blame] | 774 | #else |
| 775 | #warning Missing futimes() and futimens() |
Alp Toker | b792a01 | 2014-06-30 18:57:04 +0000 | [diff] [blame] | 776 | return make_error_code(errc::function_not_supported); |
Alp Toker | b30f01e | 2013-12-11 15:42:33 +0000 | [diff] [blame] | 777 | #endif |
Rafael Espindola | 4a3365c | 2013-06-20 20:56:14 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Rafael Espindola | 7eb1f18 | 2014-12-11 20:12:55 +0000 | [diff] [blame] | 780 | std::error_code mapped_file_region::init(int FD, uint64_t Offset, |
| 781 | mapmode Mode) { |
Rafael Espindola | c69f13b | 2014-12-12 18:13:23 +0000 | [diff] [blame] | 782 | assert(Size != 0); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 783 | |
| 784 | int flags = (Mode == readwrite) ? MAP_SHARED : MAP_PRIVATE; |
| 785 | int prot = (Mode == readonly) ? PROT_READ : (PROT_READ | PROT_WRITE); |
Zachary Turner | 842972b | 2017-02-22 21:24:06 +0000 | [diff] [blame] | 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__) |
Zachary Turner | 392ed9d | 2017-02-21 20:55:47 +0000 | [diff] [blame] | 805 | |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 806 | Mapping = ::mmap(nullptr, Size, prot, flags, FD, Offset); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 807 | if (Mapping == MAP_FAILED) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 808 | return std::error_code(errno, std::generic_category()); |
| 809 | return std::error_code(); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 810 | } |
| 811 | |
Roman Lebedev | 7c98367 | 2017-09-27 15:59:16 +0000 | [diff] [blame] | 812 | mapped_file_region::mapped_file_region(int fd, mapmode mode, size_t length, |
Rafael Espindola | 7eb1f18 | 2014-12-11 20:12:55 +0000 | [diff] [blame] | 813 | uint64_t offset, std::error_code &ec) |
Peter Collingbourne | 881ba10 | 2018-06-13 18:03:14 +0000 | [diff] [blame] | 814 | : Size(length), Mapping(), Mode(mode) { |
Zachary Turner | 2061ad2 | 2018-02-15 18:46:59 +0000 | [diff] [blame] | 815 | (void)Mode; |
Rafael Espindola | 7eb1f18 | 2014-12-11 20:12:55 +0000 | [diff] [blame] | 816 | ec = init(fd, offset, mode); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 817 | if (ec) |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 818 | Mapping = nullptr; |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | mapped_file_region::~mapped_file_region() { |
| 822 | if (Mapping) |
| 823 | ::munmap(Mapping, Size); |
| 824 | } |
| 825 | |
Roman Lebedev | 21b013e | 2017-09-27 16:08:33 +0000 | [diff] [blame] | 826 | size_t mapped_file_region::size() const { |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 827 | assert(Mapping && "Mapping failed but used anyway!"); |
| 828 | return Size; |
| 829 | } |
| 830 | |
| 831 | char *mapped_file_region::data() const { |
| 832 | assert(Mapping && "Mapping failed but used anyway!"); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 833 | return reinterpret_cast<char*>(Mapping); |
| 834 | } |
| 835 | |
| 836 | const char *mapped_file_region::const_data() const { |
| 837 | assert(Mapping && "Mapping failed but used anyway!"); |
| 838 | return reinterpret_cast<const char*>(Mapping); |
| 839 | } |
| 840 | |
| 841 | int mapped_file_region::alignment() { |
Lang Hames | e4b4ab6 | 2019-05-08 02:11:07 +0000 | [diff] [blame] | 842 | return Process::getPageSizeEstimate(); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 845 | std::error_code detail::directory_iterator_construct(detail::DirIterState &it, |
Zachary Turner | 260bda3 | 2017-03-08 22:49:32 +0000 | [diff] [blame] | 846 | StringRef path, |
| 847 | bool follow_symlinks) { |
Michael J. Spencer | 5271486 | 2011-01-05 16:38:57 +0000 | [diff] [blame] | 848 | SmallString<128> path_null(path); |
| 849 | DIR *directory = ::opendir(path_null.c_str()); |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 850 | if (!directory) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 851 | return std::error_code(errno, std::generic_category()); |
Michael J. Spencer | 5271486 | 2011-01-05 16:38:57 +0000 | [diff] [blame] | 852 | |
| 853 | it.IterationHandle = reinterpret_cast<intptr_t>(directory); |
| 854 | // Add something for replace_filename to replace. |
| 855 | path::append(path_null, "."); |
Zachary Turner | 260bda3 | 2017-03-08 22:49:32 +0000 | [diff] [blame] | 856 | it.CurrentEntry = directory_entry(path_null.str(), follow_symlinks); |
Michael J. Spencer | 5271486 | 2011-01-05 16:38:57 +0000 | [diff] [blame] | 857 | return directory_iterator_increment(it); |
| 858 | } |
| 859 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 860 | std::error_code detail::directory_iterator_destruct(detail::DirIterState &it) { |
Michael J. Spencer | 5271486 | 2011-01-05 16:38:57 +0000 | [diff] [blame] | 861 | if (it.IterationHandle) |
| 862 | ::closedir(reinterpret_cast<DIR *>(it.IterationHandle)); |
| 863 | it.IterationHandle = 0; |
| 864 | it.CurrentEntry = directory_entry(); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 865 | return std::error_code(); |
Michael J. Spencer | 5271486 | 2011-01-05 16:38:57 +0000 | [diff] [blame] | 866 | } |
| 867 | |
Kristina Brooks | 3a55d1e | 2018-09-12 22:08:10 +0000 | [diff] [blame] | 868 | static 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. |
Jordan Rose | 887d31c | 2019-07-18 20:05:11 +0000 | [diff] [blame] | 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) |
Kristina Brooks | 3a55d1e | 2018-09-12 22:08:10 +0000 | [diff] [blame] | 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 | |
| 882 | std::error_code detail::directory_iterator_increment(detail::DirIterState &It) { |
Michael J. Spencer | 5271486 | 2011-01-05 16:38:57 +0000 | [diff] [blame] | 883 | errno = 0; |
Kristina Brooks | 3a55d1e | 2018-09-12 22:08:10 +0000 | [diff] [blame] | 884 | dirent *CurDir = ::readdir(reinterpret_cast<DIR *>(It.IterationHandle)); |
| 885 | if (CurDir == nullptr && errno != 0) { |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 886 | return std::error_code(errno, std::generic_category()); |
Kristina Brooks | 3a55d1e | 2018-09-12 22:08:10 +0000 | [diff] [blame] | 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)); |
Michael J. Spencer | 5271486 | 2011-01-05 16:38:57 +0000 | [diff] [blame] | 893 | } else |
Kristina Brooks | 3a55d1e | 2018-09-12 22:08:10 +0000 | [diff] [blame] | 894 | return directory_iterator_destruct(It); |
Michael J. Spencer | 5271486 | 2011-01-05 16:38:57 +0000 | [diff] [blame] | 895 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 896 | return std::error_code(); |
Michael J. Spencer | 5271486 | 2011-01-05 16:38:57 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Peter Collingbourne | 0dfdb44 | 2017-10-10 22:19:46 +0000 | [diff] [blame] | 899 | ErrorOr<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 | |
Taewook Oh | d915327 | 2016-06-13 15:54:56 +0000 | [diff] [blame] | 906 | #if !defined(F_GETPATH) |
| 907 | static 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 | |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 915 | static 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 | |
Fangrui Song | d9b948b | 2019-08-05 05:43:48 +0000 | [diff] [blame] | 925 | // This is for compatibility with old code that assumed OF_Append implied |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 926 | // would open an existing file. See Windows/Path.inc for a longer comment. |
Fangrui Song | d9b948b | 2019-08-05 05:43:48 +0000 | [diff] [blame] | 927 | if (Flags & OF_Append) |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 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 | |
Fangrui Song | d9b948b | 2019-08-05 05:43:48 +0000 | [diff] [blame] | 942 | if (Flags & OF_Append) |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 943 | Result |= O_APPEND; |
| 944 | |
| 945 | #ifdef O_CLOEXEC |
Zachary Turner | 6edfecb | 2018-06-08 15:15:56 +0000 | [diff] [blame] | 946 | if (!(Flags & OF_ChildInherit)) |
| 947 | Result |= O_CLOEXEC; |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 948 | #endif |
| 949 | |
| 950 | return Result; |
| 951 | } |
| 952 | |
Zachary Turner | 9d2cfa6 | 2018-06-07 23:25:13 +0000 | [diff] [blame] | 953 | std::error_code openFile(const Twine &Name, int &ResultFD, |
| 954 | CreationDisposition Disp, FileAccess Access, |
| 955 | OpenFlags Flags, unsigned Mode) { |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 956 | int OpenFlags = nativeOpenFlags(Disp, Flags, Access); |
| 957 | |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 958 | SmallString<128> Storage; |
| 959 | StringRef P = Name.toNullTerminatedStringRef(Storage); |
Hans Wennborg | 0a0e411 | 2018-08-27 15:55:39 +0000 | [diff] [blame] | 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) |
Pavel Labath | fe09f50 | 2017-06-29 13:15:31 +0000 | [diff] [blame] | 964 | return std::error_code(errno, std::generic_category()); |
Pavel Labath | f726dfa | 2017-01-24 10:57:01 +0000 | [diff] [blame] | 965 | #ifndef O_CLOEXEC |
Zachary Turner | 6edfecb | 2018-06-08 15:15:56 +0000 | [diff] [blame] | 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 | } |
Pavel Labath | f726dfa | 2017-01-24 10:57:01 +0000 | [diff] [blame] | 971 | #endif |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 972 | return std::error_code(); |
| 973 | } |
| 974 | |
Zachary Turner | 9d2cfa6 | 2018-06-07 23:25:13 +0000 | [diff] [blame] | 975 | Expected<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 | |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 986 | std::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 | |
Taewook Oh | d915327 | 2016-06-13 15:54:56 +0000 | [diff] [blame] | 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 { |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 1013 | SmallString<128> Storage; |
| 1014 | StringRef P = Name.toNullTerminatedStringRef(Storage); |
| 1015 | |
Taewook Oh | d915327 | 2016-06-13 15:54:56 +0000 | [diff] [blame] | 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 |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1021 | return std::error_code(); |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 1022 | } |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 1023 | |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 1024 | Expected<file_t> openNativeFileForRead(const Twine &Name, OpenFlags Flags, |
Zachary Turner | 63db25b | 2018-06-04 19:38:11 +0000 | [diff] [blame] | 1025 | SmallVectorImpl<char> *RealPath) { |
| 1026 | file_t ResultFD; |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 1027 | std::error_code EC = openFileForRead(Name, ResultFD, Flags, RealPath); |
Zachary Turner | 63db25b | 2018-06-04 19:38:11 +0000 | [diff] [blame] | 1028 | if (EC) |
| 1029 | return errorCodeToError(EC); |
| 1030 | return ResultFD; |
| 1031 | } |
| 1032 | |
Reid Kleckner | cc418a3 | 2019-07-10 00:34:13 +0000 | [diff] [blame] | 1033 | file_t getStdinHandle() { return 0; } |
| 1034 | file_t getStdoutHandle() { return 1; } |
| 1035 | file_t getStderrHandle() { return 2; } |
| 1036 | |
Pavel Labath | 1b30ea2 | 2019-08-22 08:13:30 +0000 | [diff] [blame] | 1037 | Expected<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; |
Reid Kleckner | cc418a3 | 2019-07-10 00:34:13 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
Pavel Labath | 1b30ea2 | 2019-08-22 08:13:30 +0000 | [diff] [blame] | 1045 | Expected<size_t> readNativeFileSlice(file_t FD, MutableArrayRef<char> Buf, |
| 1046 | uint64_t Offset) { |
Reid Kleckner | cc418a3 | 2019-07-10 00:34:13 +0000 | [diff] [blame] | 1047 | #ifdef HAVE_PREAD |
Pavel Labath | 1b30ea2 | 2019-08-22 08:13:30 +0000 | [diff] [blame] | 1048 | ssize_t NumRead = |
| 1049 | sys::RetryAfterSignal(-1, ::pread, FD, Buf.data(), Buf.size(), Offset); |
Reid Kleckner | cc418a3 | 2019-07-10 00:34:13 +0000 | [diff] [blame] | 1050 | #else |
Pavel Labath | 1b30ea2 | 2019-08-22 08:13:30 +0000 | [diff] [blame] | 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()); |
Reid Kleckner | cc418a3 | 2019-07-10 00:34:13 +0000 | [diff] [blame] | 1055 | #endif |
Pavel Labath | 1b30ea2 | 2019-08-22 08:13:30 +0000 | [diff] [blame] | 1056 | if (NumRead == -1) |
| 1057 | return errorCodeToError(std::error_code(errno, std::generic_category())); |
| 1058 | return NumRead; |
Reid Kleckner | cc418a3 | 2019-07-10 00:34:13 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | std::error_code closeFile(file_t &F) { |
| 1062 | file_t TmpF = F; |
Zachary Turner | 63db25b | 2018-06-04 19:38:11 +0000 | [diff] [blame] | 1063 | F = kInvalidFile; |
Reid Kleckner | cc418a3 | 2019-07-10 00:34:13 +0000 | [diff] [blame] | 1064 | return Process::SafelyCloseFileDescriptor(TmpF); |
Zachary Turner | 63db25b | 2018-06-04 19:38:11 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Zachary Turner | 260bda3 | 2017-03-08 22:49:32 +0000 | [diff] [blame] | 1067 | template <typename T> |
| 1068 | static 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; |
Peter Collingbourne | 0dfdb44 | 2017-10-10 22:19:46 +0000 | [diff] [blame] | 1075 | ErrorOr<basic_file_status> st = Item.status(); |
| 1076 | if (!st && !IgnoreErrors) |
| 1077 | return st.getError(); |
Zachary Turner | 260bda3 | 2017-03-08 22:49:32 +0000 | [diff] [blame] | 1078 | |
Peter Collingbourne | 0dfdb44 | 2017-10-10 22:19:46 +0000 | [diff] [blame] | 1079 | if (is_directory(*st)) { |
Zachary Turner | 260bda3 | 2017-03-08 22:49:32 +0000 | [diff] [blame] | 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 | |
| 1096 | std::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 | |
Zachary Turner | e48ace6 | 2017-03-10 17:39:21 +0000 | [diff] [blame] | 1106 | std::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 | |
Davide Italiano | 4762c06 | 2018-01-09 17:27:45 +0000 | [diff] [blame] | 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)); |
Zachary Turner | e48ace6 | 2017-03-10 17:39:21 +0000 | [diff] [blame] | 1125 | return std::error_code(); |
| 1126 | } |
| 1127 | |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 1128 | } // end namespace fs |
Peter Collingbourne | f7d4101 | 2014-01-31 23:46:06 +0000 | [diff] [blame] | 1129 | |
| 1130 | namespace path { |
| 1131 | |
| 1132 | bool home_directory(SmallVectorImpl<char> &result) { |
Zachary Turner | a3cf70b | 2017-03-22 15:24:59 +0000 | [diff] [blame] | 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; |
Peter Collingbourne | f7d4101 | 2014-01-31 23:46:06 +0000 | [diff] [blame] | 1138 | } |
Zachary Turner | a3cf70b | 2017-03-22 15:24:59 +0000 | [diff] [blame] | 1139 | if (!RequestedDir) |
| 1140 | return false; |
| 1141 | |
| 1142 | result.clear(); |
| 1143 | result.append(RequestedDir, RequestedDir + strlen(RequestedDir)); |
| 1144 | return true; |
Peter Collingbourne | f7d4101 | 2014-01-31 23:46:06 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 1147 | static bool getDarwinConfDir(bool TempDir, SmallVectorImpl<char> &Result) { |
Pawel Bylica | 0e97e5c | 2015-11-02 09:49:17 +0000 | [diff] [blame] | 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 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 1172 | static const char *getEnvTempDir() { |
Rafael Espindola | 016a6d5 | 2014-08-26 14:47:52 +0000 | [diff] [blame] | 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 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 1184 | static const char *getDefaultTempDir(bool ErasedOnReboot) { |
Rafael Espindola | 016a6d5 | 2014-08-26 14:47:52 +0000 | [diff] [blame] | 1185 | #ifdef P_tmpdir |
Benjamin Kramer | 870d951 | 2014-08-27 11:47:52 +0000 | [diff] [blame] | 1186 | if ((bool)P_tmpdir) |
Rafael Espindola | 016a6d5 | 2014-08-26 14:47:52 +0000 | [diff] [blame] | 1187 | return P_tmpdir; |
| 1188 | #endif |
| 1189 | |
| 1190 | if (ErasedOnReboot) |
| 1191 | return "/tmp"; |
| 1192 | return "/var/tmp"; |
| 1193 | } |
| 1194 | |
| 1195 | void 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 | |
Pawel Bylica | 0e97e5c | 2015-11-02 09:49:17 +0000 | [diff] [blame] | 1206 | if (getDarwinConfDir(ErasedOnReboot, Result)) |
| 1207 | return; |
Rafael Espindola | 016a6d5 | 2014-08-26 14:47:52 +0000 | [diff] [blame] | 1208 | |
| 1209 | const char *RequestedDir = getDefaultTempDir(ErasedOnReboot); |
| 1210 | Result.append(RequestedDir, RequestedDir + strlen(RequestedDir)); |
| 1211 | } |
| 1212 | |
Peter Collingbourne | f7d4101 | 2014-01-31 23:46:06 +0000 | [diff] [blame] | 1213 | } // end namespace path |
| 1214 | |
Adrian Prantl | fac7875 | 2019-04-18 21:22:50 +0000 | [diff] [blame] | 1215 | namespace 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. |
Adrian Prantl | c90ff5e | 2019-04-24 19:08:43 +0000 | [diff] [blame] | 1220 | /// Unfortunately fcopyfile(3) does not support COPYFILE_CLONE, so the |
| 1221 | /// file descriptor variant of this function still uses the default |
| 1222 | /// implementation. |
Adrian Prantl | fac7875 | 2019-04-18 21:22:50 +0000 | [diff] [blame] | 1223 | std::error_code copy_file(const Twine &From, const Twine &To) { |
| 1224 | uint32_t Flag = COPYFILE_DATA; |
Yi Kong | 1755abe | 2019-07-26 05:17:14 +0000 | [diff] [blame] | 1225 | #if __has_builtin(__builtin_available) && defined(COPYFILE_CLONE) |
Adrian Prantl | fac7875 | 2019-04-18 21:22:50 +0000 | [diff] [blame] | 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 | } |
Adrian Prantl | ccdefb2 | 2019-05-07 17:10:27 +0000 | [diff] [blame] | 1235 | #endif |
Adrian Prantl | fac7875 | 2019-04-18 21:22:50 +0000 | [diff] [blame] | 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 | } |
Adrian Prantl | c90ff5e | 2019-04-24 19:08:43 +0000 | [diff] [blame] | 1243 | #endif // __APPLE__ |
Adrian Prantl | fac7875 | 2019-04-18 21:22:50 +0000 | [diff] [blame] | 1244 | |
| 1245 | } // end namespace fs |
| 1246 | |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 1247 | } // end namespace sys |
| 1248 | } // end namespace llvm |