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