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