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 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Rafael Espindola | f1fc382 | 2013-06-26 19:33:03 +0000 | [diff] [blame] | 10 | // This file implements the Unix specific implementation of the Path API. |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | //=== WARNING: Implementation here must contain only generic UNIX code that |
| 16 | //=== is guaranteed to work on *all* UNIX variants. |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #include "Unix.h" |
Reid Kleckner | 37f69de | 2013-07-26 16:54:23 +0000 | [diff] [blame] | 20 | #include <limits.h> |
| 21 | #include <stdio.h> |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 22 | #if HAVE_SYS_STAT_H |
| 23 | #include <sys/stat.h> |
| 24 | #endif |
| 25 | #if HAVE_FCNTL_H |
| 26 | #include <fcntl.h> |
| 27 | #endif |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 28 | #ifdef HAVE_SYS_MMAN_H |
| 29 | #include <sys/mman.h> |
| 30 | #endif |
Michael J. Spencer | 5271486 | 2011-01-05 16:38:57 +0000 | [diff] [blame] | 31 | #if HAVE_DIRENT_H |
| 32 | # include <dirent.h> |
| 33 | # define NAMLEN(dirent) strlen((dirent)->d_name) |
| 34 | #else |
| 35 | # define dirent direct |
| 36 | # define NAMLEN(dirent) (dirent)->d_namlen |
| 37 | # if HAVE_SYS_NDIR_H |
| 38 | # include <sys/ndir.h> |
| 39 | # endif |
| 40 | # if HAVE_SYS_DIR_H |
| 41 | # include <sys/dir.h> |
| 42 | # endif |
| 43 | # if HAVE_NDIR_H |
| 44 | # include <ndir.h> |
| 45 | # endif |
| 46 | #endif |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 47 | |
Rafael Espindola | 4601c46 | 2013-06-26 05:25:44 +0000 | [diff] [blame] | 48 | #ifdef __APPLE__ |
| 49 | #include <mach-o/dyld.h> |
| 50 | #endif |
| 51 | |
Joerg Sonnenberger | c069730 | 2012-08-10 10:56:09 +0000 | [diff] [blame] | 52 | // Both stdio.h and cstdio are included via different pathes and |
| 53 | // stdcxx's cstdio doesn't include stdio.h, so it doesn't #undef the macros |
| 54 | // either. |
| 55 | #undef ferror |
| 56 | #undef feof |
| 57 | |
Sylvestre Ledru | 14ada94 | 2012-04-11 15:35:36 +0000 | [diff] [blame] | 58 | // For GNU Hurd |
| 59 | #if defined(__GNU__) && !defined(PATH_MAX) |
| 60 | # define PATH_MAX 4096 |
| 61 | #endif |
| 62 | |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 63 | using namespace llvm; |
| 64 | |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 65 | namespace { |
Michael J. Spencer | 5529c57 | 2010-12-07 01:23:08 +0000 | [diff] [blame] | 66 | /// This class automatically closes the given file descriptor when it goes out |
| 67 | /// of scope. You can take back explicit ownership of the file descriptor by |
| 68 | /// calling take(). The destructor does not verify that close was successful. |
| 69 | /// Therefore, never allow this class to call close on a file descriptor that |
| 70 | /// has been read from or written to. |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 71 | struct AutoFD { |
| 72 | int FileDescriptor; |
| 73 | |
| 74 | AutoFD(int fd) : FileDescriptor(fd) {} |
| 75 | ~AutoFD() { |
| 76 | if (FileDescriptor >= 0) |
| 77 | ::close(FileDescriptor); |
| 78 | } |
| 79 | |
| 80 | int take() { |
| 81 | int ret = FileDescriptor; |
| 82 | FileDescriptor = -1; |
| 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | operator int() const {return FileDescriptor;} |
| 87 | }; |
Rafael Espindola | 37b012d | 2014-02-23 15:16:03 +0000 | [diff] [blame] | 88 | } |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 89 | |
Rafael Espindola | 37b012d | 2014-02-23 15:16:03 +0000 | [diff] [blame] | 90 | static error_code TempDir(SmallVectorImpl<char> &result) { |
| 91 | // FIXME: Don't use TMPDIR if program is SUID or SGID enabled. |
| 92 | const char *dir = 0; |
| 93 | (dir = std::getenv("TMPDIR")) || (dir = std::getenv("TMP")) || |
| 94 | (dir = std::getenv("TEMP")) || (dir = std::getenv("TEMPDIR")) || |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 95 | #ifdef P_tmpdir |
Rafael Espindola | 37b012d | 2014-02-23 15:16:03 +0000 | [diff] [blame] | 96 | (dir = P_tmpdir) || |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 97 | #endif |
Rafael Espindola | 37b012d | 2014-02-23 15:16:03 +0000 | [diff] [blame] | 98 | (dir = "/tmp"); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 99 | |
Rafael Espindola | 37b012d | 2014-02-23 15:16:03 +0000 | [diff] [blame] | 100 | result.clear(); |
| 101 | StringRef d(dir); |
| 102 | result.append(d.begin(), d.end()); |
| 103 | return error_code::success(); |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 104 | } |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 105 | |
| 106 | namespace llvm { |
| 107 | namespace sys { |
Michael J. Spencer | 20daa28 | 2010-12-07 01:22:31 +0000 | [diff] [blame] | 108 | namespace fs { |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 109 | #if defined(__FreeBSD__) || defined (__NetBSD__) || defined(__Bitrig__) || \ |
| 110 | defined(__OpenBSD__) || defined(__minix) || defined(__FreeBSD_kernel__) || \ |
Rafael Espindola | aca9739 | 2013-10-31 14:35:00 +0000 | [diff] [blame] | 111 | defined(__linux__) || defined(__CYGWIN__) || defined(__DragonFly__) |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +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) |
| 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 | |
Sylvestre Ledru | 21e67472 | 2013-12-09 16:27:00 +0000 | [diff] [blame] | 118 | snprintf(fullpath, PATH_MAX, "%s/%s", dir, bin); |
| 119 | if (realpath(fullpath, ret) == NULL) |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 120 | return (1); |
Sylvestre Ledru | 21e67472 | 2013-12-09 16:27:00 +0000 | [diff] [blame] | 121 | if (stat(fullpath, &sb) != 0) |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 122 | return (1); |
| 123 | |
| 124 | return (0); |
| 125 | } |
| 126 | |
| 127 | static char * |
| 128 | getprogpath(char ret[PATH_MAX], const char *bin) |
| 129 | { |
Sylvestre Ledru | 21e67472 | 2013-12-09 16:27:00 +0000 | [diff] [blame] | 130 | char *pv, *s, *t; |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 131 | |
| 132 | /* First approach: absolute path. */ |
| 133 | if (bin[0] == '/') { |
Sylvestre Ledru | 21e67472 | 2013-12-09 16:27:00 +0000 | [diff] [blame] | 134 | if (test_dir(ret, "/", bin) == 0) |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 135 | return (ret); |
| 136 | return (NULL); |
| 137 | } |
| 138 | |
| 139 | /* Second approach: relative path. */ |
| 140 | if (strchr(bin, '/') != NULL) { |
Sylvestre Ledru | 21e67472 | 2013-12-09 16:27:00 +0000 | [diff] [blame] | 141 | char cwd[PATH_MAX]; |
| 142 | if (getcwd(cwd, PATH_MAX) == NULL) |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 143 | return (NULL); |
Sylvestre Ledru | 21e67472 | 2013-12-09 16:27:00 +0000 | [diff] [blame] | 144 | if (test_dir(ret, cwd, bin) == 0) |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 145 | return (ret); |
| 146 | return (NULL); |
| 147 | } |
| 148 | |
| 149 | /* Third approach: $PATH */ |
| 150 | if ((pv = getenv("PATH")) == NULL) |
| 151 | return (NULL); |
| 152 | s = pv = strdup(pv); |
| 153 | if (pv == NULL) |
| 154 | return (NULL); |
| 155 | while ((t = strsep(&s, ":")) != NULL) { |
Sylvestre Ledru | 21e67472 | 2013-12-09 16:27:00 +0000 | [diff] [blame] | 156 | if (test_dir(ret, t, bin) == 0) { |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 157 | free(pv); |
| 158 | return (ret); |
| 159 | } |
| 160 | } |
| 161 | free(pv); |
| 162 | return (NULL); |
| 163 | } |
| 164 | #endif // __FreeBSD__ || __NetBSD__ || __FreeBSD_kernel__ |
| 165 | |
| 166 | /// GetMainExecutable - Return the path to the main executable, given the |
| 167 | /// value of argv[0] from program startup. |
| 168 | std::string getMainExecutable(const char *argv0, void *MainAddr) { |
| 169 | #if defined(__APPLE__) |
| 170 | // On OS X the executable path is saved to the stack by dyld. Reading it |
| 171 | // from there is much faster than calling dladdr, especially for large |
| 172 | // binaries with symbols. |
| 173 | char exe_path[MAXPATHLEN]; |
| 174 | uint32_t size = sizeof(exe_path); |
| 175 | if (_NSGetExecutablePath(exe_path, &size) == 0) { |
| 176 | char link_path[MAXPATHLEN]; |
| 177 | if (realpath(exe_path, link_path)) |
Rafael Espindola | 4601c46 | 2013-06-26 05:25:44 +0000 | [diff] [blame] | 178 | return link_path; |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 179 | } |
| 180 | #elif defined(__FreeBSD__) || defined (__NetBSD__) || defined(__Bitrig__) || \ |
Rafael Espindola | aca9739 | 2013-10-31 14:35:00 +0000 | [diff] [blame] | 181 | defined(__OpenBSD__) || defined(__minix) || defined(__DragonFly__) || \ |
| 182 | defined(__FreeBSD_kernel__) |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 183 | char exe_path[PATH_MAX]; |
| 184 | |
| 185 | if (getprogpath(exe_path, argv0) != NULL) |
Rafael Espindola | 2c6f4fe | 2013-06-26 06:10:32 +0000 | [diff] [blame] | 186 | return exe_path; |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 187 | #elif defined(__linux__) || defined(__CYGWIN__) |
| 188 | char exe_path[MAXPATHLEN]; |
| 189 | StringRef aPath("/proc/self/exe"); |
| 190 | if (sys::fs::exists(aPath)) { |
| 191 | // /proc is not always mounted under Linux (chroot for example). |
| 192 | ssize_t len = readlink(aPath.str().c_str(), exe_path, sizeof(exe_path)); |
| 193 | if (len >= 0) |
| 194 | return StringRef(exe_path, len); |
| 195 | } else { |
| 196 | // Fall back to the classical detection. |
| 197 | if (getprogpath(exe_path, argv0) != NULL) |
| 198 | return exe_path; |
| 199 | } |
| 200 | #elif defined(HAVE_DLFCN_H) |
| 201 | // Use dladdr to get executable path if available. |
| 202 | Dl_info DLInfo; |
| 203 | int err = dladdr(MainAddr, &DLInfo); |
| 204 | if (err == 0) |
Rafael Espindola | 2c6f4fe | 2013-06-26 06:10:32 +0000 | [diff] [blame] | 205 | return ""; |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 206 | |
| 207 | // If the filename is a symlink, we need to resolve and return the location of |
| 208 | // the actual executable. |
| 209 | char link_path[MAXPATHLEN]; |
| 210 | if (realpath(DLInfo.dli_fname, link_path)) |
Rafael Espindola | 2c6f4fe | 2013-06-26 06:10:32 +0000 | [diff] [blame] | 211 | return link_path; |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 212 | #else |
| 213 | #error GetMainExecutable is not implemented on this host yet. |
| 214 | #endif |
| 215 | return ""; |
| 216 | } |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 217 | |
Rafael Espindola | be3ede7 | 2013-06-20 21:51:49 +0000 | [diff] [blame] | 218 | TimeValue file_status::getLastModificationTime() const { |
Rafael Espindola | db5d8fe | 2013-06-20 18:42:04 +0000 | [diff] [blame] | 219 | TimeValue Ret; |
| 220 | Ret.fromEpochTime(fs_st_mtime); |
| 221 | return Ret; |
| 222 | } |
| 223 | |
Rafael Espindola | d123099 | 2013-07-29 21:55:38 +0000 | [diff] [blame] | 224 | UniqueID file_status::getUniqueID() const { |
Rafael Espindola | 7f822a9 | 2013-07-29 21:26:49 +0000 | [diff] [blame] | 225 | return UniqueID(fs_st_dev, fs_st_ino); |
| 226 | } |
| 227 | |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 228 | error_code current_path(SmallVectorImpl<char> &result) { |
Rafael Espindola | 6ee1638 | 2013-08-10 00:50:57 +0000 | [diff] [blame] | 229 | result.clear(); |
| 230 | |
| 231 | const char *pwd = ::getenv("PWD"); |
| 232 | llvm::sys::fs::file_status PWDStatus, DotStatus; |
| 233 | if (pwd && llvm::sys::path::is_absolute(pwd) && |
| 234 | !llvm::sys::fs::status(pwd, PWDStatus) && |
| 235 | !llvm::sys::fs::status(".", DotStatus) && |
| 236 | PWDStatus.getUniqueID() == DotStatus.getUniqueID()) { |
| 237 | result.append(pwd, pwd + strlen(pwd)); |
| 238 | return error_code::success(); |
| 239 | } |
| 240 | |
Sylvestre Ledru | 14ada94 | 2012-04-11 15:35:36 +0000 | [diff] [blame] | 241 | #ifdef MAXPATHLEN |
Andrew Trick | a4ec5b2 | 2011-03-24 16:43:37 +0000 | [diff] [blame] | 242 | result.reserve(MAXPATHLEN); |
Sylvestre Ledru | 14ada94 | 2012-04-11 15:35:36 +0000 | [diff] [blame] | 243 | #else |
| 244 | // For GNU Hurd |
| 245 | result.reserve(1024); |
| 246 | #endif |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 247 | |
Michael J. Spencer | 20daa28 | 2010-12-07 01:22:31 +0000 | [diff] [blame] | 248 | while (true) { |
| 249 | if (::getcwd(result.data(), result.capacity()) == 0) { |
| 250 | // See if there was a real error. |
| 251 | if (errno != errc::not_enough_memory) |
| 252 | return error_code(errno, system_category()); |
| 253 | // Otherwise there just wasn't enough space. |
| 254 | result.reserve(result.capacity() * 2); |
| 255 | } else |
| 256 | break; |
| 257 | } |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 258 | |
| 259 | result.set_size(strlen(result.data())); |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 260 | return error_code::success(); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 263 | error_code create_directory(const Twine &path, bool IgnoreExisting) { |
Michael J. Spencer | 31e310c | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 264 | SmallString<128> path_storage; |
| 265 | StringRef p = path.toNullTerminatedStringRef(path_storage); |
| 266 | |
Michael J. Spencer | e5755be | 2010-12-07 01:23:29 +0000 | [diff] [blame] | 267 | if (::mkdir(p.begin(), S_IRWXU | S_IRWXG) == -1) { |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 268 | if (errno != errc::file_exists || !IgnoreExisting) |
Michael J. Spencer | 31e310c | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 269 | return error_code(errno, system_category()); |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 270 | } |
Michael J. Spencer | 31e310c | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 271 | |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 272 | return error_code::success(); |
Michael J. Spencer | 31e310c | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Saleem Abdulrasool | afc50b3ed | 2014-03-11 22:05:42 +0000 | [diff] [blame] | 275 | error_code normalize_separators(SmallVectorImpl<char> &Path) { |
| 276 | for (auto PI = Path.begin(), PE = Path.end(); PI < PE; ++PI) { |
| 277 | if (*PI == '\\') { |
| 278 | auto PN = PI + 1; |
| 279 | if (PN < PE && *PN == '\\') |
| 280 | ++PI; // increment once, the for loop will move over the escaped slash |
| 281 | else |
| 282 | *PI = '/'; |
| 283 | } |
| 284 | } |
| 285 | return error_code::success(); |
| 286 | } |
| 287 | |
Rafael Espindola | 83f858e | 2014-03-11 18:40:24 +0000 | [diff] [blame] | 288 | // Note that we are using symbolic link because hard links are not supported by |
| 289 | // all filesystems (SMB doesn't). |
| 290 | error_code create_link(const Twine &to, const Twine &from) { |
Michael J. Spencer | e0c4560 | 2010-12-03 05:58:41 +0000 | [diff] [blame] | 291 | // Get arguments. |
| 292 | SmallString<128> from_storage; |
| 293 | SmallString<128> to_storage; |
| 294 | StringRef f = from.toNullTerminatedStringRef(from_storage); |
| 295 | StringRef t = to.toNullTerminatedStringRef(to_storage); |
| 296 | |
Rafael Espindola | 83f858e | 2014-03-11 18:40:24 +0000 | [diff] [blame] | 297 | if (::symlink(t.begin(), f.begin()) == -1) |
Michael J. Spencer | e0c4560 | 2010-12-03 05:58:41 +0000 | [diff] [blame] | 298 | return error_code(errno, system_category()); |
| 299 | |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 300 | return error_code::success(); |
Michael J. Spencer | e0c4560 | 2010-12-03 05:58:41 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 303 | error_code remove(const Twine &path, bool IgnoreNonExisting) { |
Michael J. Spencer | 6e74e11 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 304 | SmallString<128> path_storage; |
| 305 | StringRef p = path.toNullTerminatedStringRef(path_storage); |
| 306 | |
Rafael Espindola | 8cd62b0 | 2013-06-17 20:35:51 +0000 | [diff] [blame] | 307 | struct stat buf; |
Argyrios Kyrtzidis | 3757569 | 2014-03-21 01:25:37 +0000 | [diff] [blame^] | 308 | if (lstat(p.begin(), &buf) != 0) { |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 309 | if (errno != errc::no_such_file_or_directory || !IgnoreNonExisting) |
Rafael Espindola | 8cd62b0 | 2013-06-17 20:35:51 +0000 | [diff] [blame] | 310 | return error_code(errno, system_category()); |
Rafael Espindola | 8cd62b0 | 2013-06-17 20:35:51 +0000 | [diff] [blame] | 311 | return error_code::success(); |
| 312 | } |
| 313 | |
| 314 | // Note: this check catches strange situations. In all cases, LLVM should |
| 315 | // only be involved in the creation and deletion of regular files. This |
| 316 | // check ensures that what we're trying to erase is a regular file. It |
| 317 | // effectively prevents LLVM from erasing things like /dev/null, any block |
| 318 | // special file, or other things that aren't "regular" files. |
Argyrios Kyrtzidis | 3757569 | 2014-03-21 01:25:37 +0000 | [diff] [blame^] | 319 | if (!S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode) && !S_ISLNK(buf.st_mode)) |
Rafael Espindola | 8cd62b0 | 2013-06-17 20:35:51 +0000 | [diff] [blame] | 320 | return make_error_code(errc::operation_not_permitted); |
| 321 | |
Michael J. Spencer | 6e74e11 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 322 | if (::remove(p.begin()) == -1) { |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 323 | if (errno != errc::no_such_file_or_directory || !IgnoreNonExisting) |
Michael J. Spencer | 6e74e11 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 324 | return error_code(errno, system_category()); |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 325 | } |
Michael J. Spencer | 6e74e11 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 326 | |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 327 | return error_code::success(); |
Michael J. Spencer | 6e74e11 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Michael J. Spencer | 409f556 | 2010-12-03 17:53:55 +0000 | [diff] [blame] | 330 | error_code rename(const Twine &from, const Twine &to) { |
| 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 | b6fea4c | 2013-07-17 03:33:41 +0000 | [diff] [blame] | 337 | if (::rename(f.begin(), t.begin()) == -1) |
| 338 | return error_code(errno, system_category()); |
Michael J. Spencer | 409f556 | 2010-12-03 17:53:55 +0000 | [diff] [blame] | 339 | |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 340 | return error_code::success(); |
Michael J. Spencer | 409f556 | 2010-12-03 17:53:55 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Michael J. Spencer | c20a032 | 2010-12-03 17:54:07 +0000 | [diff] [blame] | 343 | error_code resize_file(const Twine &path, uint64_t size) { |
| 344 | SmallString<128> path_storage; |
| 345 | StringRef p = path.toNullTerminatedStringRef(path_storage); |
| 346 | |
| 347 | if (::truncate(p.begin(), size) == -1) |
| 348 | return error_code(errno, system_category()); |
| 349 | |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 350 | return error_code::success(); |
Michael J. Spencer | c20a032 | 2010-12-03 17:54:07 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 353 | error_code exists(const Twine &path, bool &result) { |
| 354 | SmallString<128> path_storage; |
| 355 | StringRef p = path.toNullTerminatedStringRef(path_storage); |
| 356 | |
Benjamin Kramer | 172f808 | 2012-06-02 16:28:09 +0000 | [diff] [blame] | 357 | if (::access(p.begin(), F_OK) == -1) { |
Michael J. Spencer | 66a1f86 | 2010-12-04 18:45:32 +0000 | [diff] [blame] | 358 | if (errno != errc::no_such_file_or_directory) |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 359 | return error_code(errno, system_category()); |
| 360 | result = false; |
| 361 | } else |
| 362 | result = true; |
| 363 | |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 364 | return error_code::success(); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Rafael Espindola | a1280c1 | 2013-06-18 20:56:38 +0000 | [diff] [blame] | 367 | bool can_write(const Twine &Path) { |
| 368 | SmallString<128> PathStorage; |
| 369 | StringRef P = Path.toNullTerminatedStringRef(PathStorage); |
| 370 | return 0 == access(P.begin(), W_OK); |
| 371 | } |
| 372 | |
Rafael Espindola | b0a5c96 | 2013-06-14 19:38:45 +0000 | [diff] [blame] | 373 | bool can_execute(const Twine &Path) { |
| 374 | SmallString<128> PathStorage; |
| 375 | StringRef P = Path.toNullTerminatedStringRef(PathStorage); |
| 376 | |
Manuel Klimek | 52772bf | 2013-06-17 10:48:34 +0000 | [diff] [blame] | 377 | if (0 != access(P.begin(), R_OK | X_OK)) |
| 378 | return false; |
| 379 | struct stat buf; |
| 380 | if (0 != stat(P.begin(), &buf)) |
| 381 | return false; |
| 382 | if (!S_ISREG(buf.st_mode)) |
| 383 | return false; |
| 384 | return true; |
Rafael Espindola | b0a5c96 | 2013-06-14 19:38:45 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 387 | bool equivalent(file_status A, file_status B) { |
| 388 | assert(status_known(A) && status_known(B)); |
Sylvestre Ledru | 3099f4bd | 2012-04-23 16:37:23 +0000 | [diff] [blame] | 389 | return A.fs_st_dev == B.fs_st_dev && |
| 390 | A.fs_st_ino == B.fs_st_ino; |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Michael J. Spencer | 376d387 | 2010-12-03 18:49:13 +0000 | [diff] [blame] | 393 | error_code equivalent(const Twine &A, const Twine &B, bool &result) { |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 394 | file_status fsA, fsB; |
| 395 | if (error_code ec = status(A, fsA)) return ec; |
| 396 | if (error_code ec = status(B, fsB)) return ec; |
| 397 | result = equivalent(fsA, fsB); |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 398 | return error_code::success(); |
Michael J. Spencer | 376d387 | 2010-12-03 18:49:13 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 401 | static error_code fillStatus(int StatRet, const struct stat &Status, |
| 402 | file_status &Result) { |
| 403 | if (StatRet != 0) { |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 404 | error_code ec(errno, system_category()); |
| 405 | if (ec == errc::no_such_file_or_directory) |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 406 | Result = file_status(file_type::file_not_found); |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 407 | else |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 408 | Result = file_status(file_type::status_error); |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 409 | return ec; |
| 410 | } |
| 411 | |
Rafael Espindola | 9da91a0 | 2013-07-16 02:55:33 +0000 | [diff] [blame] | 412 | file_type Type = file_type::type_unknown; |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 413 | |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 414 | if (S_ISDIR(Status.st_mode)) |
Rafael Espindola | 9da91a0 | 2013-07-16 02:55:33 +0000 | [diff] [blame] | 415 | Type = file_type::directory_file; |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 416 | else if (S_ISREG(Status.st_mode)) |
Rafael Espindola | 9da91a0 | 2013-07-16 02:55:33 +0000 | [diff] [blame] | 417 | Type = file_type::regular_file; |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 418 | else if (S_ISBLK(Status.st_mode)) |
Rafael Espindola | 9da91a0 | 2013-07-16 02:55:33 +0000 | [diff] [blame] | 419 | Type = file_type::block_file; |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 420 | else if (S_ISCHR(Status.st_mode)) |
Rafael Espindola | 9da91a0 | 2013-07-16 02:55:33 +0000 | [diff] [blame] | 421 | Type = file_type::character_file; |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 422 | else if (S_ISFIFO(Status.st_mode)) |
Rafael Espindola | 9da91a0 | 2013-07-16 02:55:33 +0000 | [diff] [blame] | 423 | Type = file_type::fifo_file; |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 424 | else if (S_ISSOCK(Status.st_mode)) |
Rafael Espindola | 9da91a0 | 2013-07-16 02:55:33 +0000 | [diff] [blame] | 425 | Type = file_type::socket_file; |
| 426 | |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 427 | perms Perms = static_cast<perms>(Status.st_mode); |
| 428 | Result = |
| 429 | file_status(Type, Perms, Status.st_dev, Status.st_ino, Status.st_mtime, |
| 430 | Status.st_uid, Status.st_gid, Status.st_size); |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 431 | |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 432 | return error_code::success(); |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Rafael Espindola | 77021c9 | 2013-07-16 03:20:13 +0000 | [diff] [blame] | 435 | error_code status(const Twine &Path, file_status &Result) { |
| 436 | SmallString<128> PathStorage; |
| 437 | StringRef P = Path.toNullTerminatedStringRef(PathStorage); |
| 438 | |
| 439 | struct stat Status; |
| 440 | int StatRet = ::stat(P.begin(), &Status); |
| 441 | return fillStatus(StatRet, Status, Result); |
| 442 | } |
| 443 | |
| 444 | error_code status(int FD, file_status &Result) { |
| 445 | struct stat Status; |
| 446 | int StatRet = ::fstat(FD, &Status); |
| 447 | return fillStatus(StatRet, Status, Result); |
| 448 | } |
| 449 | |
Rafael Espindola | 4a3365c | 2013-06-20 20:56:14 +0000 | [diff] [blame] | 450 | error_code setLastModificationAndAccessTime(int FD, TimeValue Time) { |
Eric Christopher | a24dc7f | 2013-07-04 01:10:38 +0000 | [diff] [blame] | 451 | #if defined(HAVE_FUTIMENS) |
| 452 | timespec Times[2]; |
Dmitri Gribenko | 70e6585 | 2014-02-11 09:11:18 +0000 | [diff] [blame] | 453 | Times[0].tv_sec = Time.toEpochTime(); |
Eric Christopher | a24dc7f | 2013-07-04 01:10:38 +0000 | [diff] [blame] | 454 | Times[0].tv_nsec = 0; |
| 455 | Times[1] = Times[0]; |
| 456 | if (::futimens(FD, Times)) |
Alp Toker | b30f01e | 2013-12-11 15:42:33 +0000 | [diff] [blame] | 457 | return error_code(errno, system_category()); |
| 458 | return error_code::success(); |
Eric Christopher | a24dc7f | 2013-07-04 01:10:38 +0000 | [diff] [blame] | 459 | #elif defined(HAVE_FUTIMES) |
Rafael Espindola | 4a3365c | 2013-06-20 20:56:14 +0000 | [diff] [blame] | 460 | timeval Times[2]; |
Dmitri Gribenko | 70e6585 | 2014-02-11 09:11:18 +0000 | [diff] [blame] | 461 | Times[0].tv_sec = Time.toEpochTime(); |
Rafael Espindola | 4a3365c | 2013-06-20 20:56:14 +0000 | [diff] [blame] | 462 | Times[0].tv_usec = 0; |
| 463 | Times[1] = Times[0]; |
| 464 | if (::futimes(FD, Times)) |
| 465 | return error_code(errno, system_category()); |
| 466 | return error_code::success(); |
Alp Toker | b30f01e | 2013-12-11 15:42:33 +0000 | [diff] [blame] | 467 | #else |
| 468 | #warning Missing futimes() and futimens() |
| 469 | return make_error_code(errc::not_supported); |
| 470 | #endif |
Rafael Espindola | 4a3365c | 2013-06-20 20:56:14 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 473 | error_code mapped_file_region::init(int FD, bool CloseFD, uint64_t Offset) { |
| 474 | AutoFD ScopedFD(FD); |
| 475 | if (!CloseFD) |
| 476 | ScopedFD.take(); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 477 | |
| 478 | // Figure out how large the file is. |
| 479 | struct stat FileInfo; |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 480 | if (fstat(FD, &FileInfo) == -1) |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 481 | return error_code(errno, system_category()); |
| 482 | uint64_t FileSize = FileInfo.st_size; |
| 483 | |
| 484 | if (Size == 0) |
| 485 | Size = FileSize; |
| 486 | else if (FileSize < Size) { |
| 487 | // We need to grow the file. |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 488 | if (ftruncate(FD, Size) == -1) |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 489 | return error_code(errno, system_category()); |
| 490 | } |
| 491 | |
| 492 | int flags = (Mode == readwrite) ? MAP_SHARED : MAP_PRIVATE; |
| 493 | int prot = (Mode == readonly) ? PROT_READ : (PROT_READ | PROT_WRITE); |
| 494 | #ifdef MAP_FILE |
| 495 | flags |= MAP_FILE; |
| 496 | #endif |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 497 | Mapping = ::mmap(0, Size, prot, flags, FD, Offset); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 498 | if (Mapping == MAP_FAILED) |
| 499 | return error_code(errno, system_category()); |
| 500 | return error_code::success(); |
| 501 | } |
| 502 | |
| 503 | mapped_file_region::mapped_file_region(const Twine &path, |
| 504 | mapmode mode, |
| 505 | uint64_t length, |
| 506 | uint64_t offset, |
| 507 | error_code &ec) |
| 508 | : Mode(mode) |
| 509 | , Size(length) |
| 510 | , Mapping() { |
| 511 | // Make sure that the requested size fits within SIZE_T. |
| 512 | if (length > std::numeric_limits<size_t>::max()) { |
| 513 | ec = make_error_code(errc::invalid_argument); |
| 514 | return; |
| 515 | } |
| 516 | |
| 517 | SmallString<128> path_storage; |
| 518 | StringRef name = path.toNullTerminatedStringRef(path_storage); |
| 519 | int oflags = (mode == readonly) ? O_RDONLY : O_RDWR; |
| 520 | int ofd = ::open(name.begin(), oflags); |
| 521 | if (ofd == -1) { |
| 522 | ec = error_code(errno, system_category()); |
| 523 | return; |
| 524 | } |
| 525 | |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 526 | ec = init(ofd, true, offset); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 527 | if (ec) |
| 528 | Mapping = 0; |
| 529 | } |
| 530 | |
| 531 | mapped_file_region::mapped_file_region(int fd, |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 532 | bool closefd, |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 533 | mapmode mode, |
| 534 | uint64_t length, |
| 535 | uint64_t offset, |
| 536 | error_code &ec) |
| 537 | : Mode(mode) |
| 538 | , Size(length) |
| 539 | , Mapping() { |
| 540 | // Make sure that the requested size fits within SIZE_T. |
| 541 | if (length > std::numeric_limits<size_t>::max()) { |
| 542 | ec = make_error_code(errc::invalid_argument); |
| 543 | return; |
| 544 | } |
| 545 | |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 546 | ec = init(fd, closefd, offset); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 547 | if (ec) |
| 548 | Mapping = 0; |
| 549 | } |
| 550 | |
| 551 | mapped_file_region::~mapped_file_region() { |
| 552 | if (Mapping) |
| 553 | ::munmap(Mapping, Size); |
| 554 | } |
| 555 | |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 556 | mapped_file_region::mapped_file_region(mapped_file_region &&other) |
| 557 | : Mode(other.Mode), Size(other.Size), Mapping(other.Mapping) { |
| 558 | other.Mapping = 0; |
| 559 | } |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 560 | |
| 561 | mapped_file_region::mapmode mapped_file_region::flags() const { |
| 562 | assert(Mapping && "Mapping failed but used anyway!"); |
| 563 | return Mode; |
| 564 | } |
| 565 | |
| 566 | uint64_t mapped_file_region::size() const { |
| 567 | assert(Mapping && "Mapping failed but used anyway!"); |
| 568 | return Size; |
| 569 | } |
| 570 | |
| 571 | char *mapped_file_region::data() const { |
| 572 | assert(Mapping && "Mapping failed but used anyway!"); |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 573 | assert(Mode != readonly && "Cannot get non-const data for readonly mapping!"); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 574 | return reinterpret_cast<char*>(Mapping); |
| 575 | } |
| 576 | |
| 577 | const char *mapped_file_region::const_data() const { |
| 578 | assert(Mapping && "Mapping failed but used anyway!"); |
| 579 | return reinterpret_cast<const char*>(Mapping); |
| 580 | } |
| 581 | |
| 582 | int mapped_file_region::alignment() { |
Chandler Carruth | acd64be | 2012-12-31 23:31:56 +0000 | [diff] [blame] | 583 | return process::get_self()->page_size(); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 586 | error_code detail::directory_iterator_construct(detail::DirIterState &it, |
| 587 | StringRef path){ |
Michael J. Spencer | 5271486 | 2011-01-05 16:38:57 +0000 | [diff] [blame] | 588 | SmallString<128> path_null(path); |
| 589 | DIR *directory = ::opendir(path_null.c_str()); |
| 590 | if (directory == 0) |
| 591 | return error_code(errno, system_category()); |
| 592 | |
| 593 | it.IterationHandle = reinterpret_cast<intptr_t>(directory); |
| 594 | // Add something for replace_filename to replace. |
| 595 | path::append(path_null, "."); |
| 596 | it.CurrentEntry = directory_entry(path_null.str()); |
| 597 | return directory_iterator_increment(it); |
| 598 | } |
| 599 | |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 600 | error_code detail::directory_iterator_destruct(detail::DirIterState &it) { |
Michael J. Spencer | 5271486 | 2011-01-05 16:38:57 +0000 | [diff] [blame] | 601 | if (it.IterationHandle) |
| 602 | ::closedir(reinterpret_cast<DIR *>(it.IterationHandle)); |
| 603 | it.IterationHandle = 0; |
| 604 | it.CurrentEntry = directory_entry(); |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 605 | return error_code::success(); |
Michael J. Spencer | 5271486 | 2011-01-05 16:38:57 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 608 | error_code detail::directory_iterator_increment(detail::DirIterState &it) { |
Michael J. Spencer | 5271486 | 2011-01-05 16:38:57 +0000 | [diff] [blame] | 609 | errno = 0; |
| 610 | dirent *cur_dir = ::readdir(reinterpret_cast<DIR *>(it.IterationHandle)); |
| 611 | if (cur_dir == 0 && errno != 0) { |
| 612 | return error_code(errno, system_category()); |
| 613 | } else if (cur_dir != 0) { |
| 614 | StringRef name(cur_dir->d_name, NAMLEN(cur_dir)); |
| 615 | if ((name.size() == 1 && name[0] == '.') || |
| 616 | (name.size() == 2 && name[0] == '.' && name[1] == '.')) |
| 617 | return directory_iterator_increment(it); |
| 618 | it.CurrentEntry.replace_filename(name); |
| 619 | } else |
| 620 | return directory_iterator_destruct(it); |
| 621 | |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 622 | return error_code::success(); |
Michael J. Spencer | 5271486 | 2011-01-05 16:38:57 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Michael J. Spencer | ee1699c | 2011-01-15 18:52:33 +0000 | [diff] [blame] | 625 | error_code get_magic(const Twine &path, uint32_t len, |
| 626 | SmallVectorImpl<char> &result) { |
| 627 | SmallString<128> PathStorage; |
| 628 | StringRef Path = path.toNullTerminatedStringRef(PathStorage); |
| 629 | result.set_size(0); |
| 630 | |
| 631 | // Open path. |
| 632 | std::FILE *file = std::fopen(Path.data(), "rb"); |
| 633 | if (file == 0) |
| 634 | return error_code(errno, system_category()); |
| 635 | |
| 636 | // Reserve storage. |
| 637 | result.reserve(len); |
| 638 | |
| 639 | // Read magic! |
| 640 | size_t size = std::fread(result.data(), 1, len, file); |
| 641 | if (std::ferror(file) != 0) { |
| 642 | std::fclose(file); |
| 643 | return error_code(errno, system_category()); |
Evgeniy Stepanov | 6eb4484 | 2013-06-20 15:56:05 +0000 | [diff] [blame] | 644 | } else if (size != len) { |
Michael J. Spencer | ee1699c | 2011-01-15 18:52:33 +0000 | [diff] [blame] | 645 | if (std::feof(file) != 0) { |
| 646 | std::fclose(file); |
| 647 | result.set_size(size); |
| 648 | return make_error_code(errc::value_too_large); |
| 649 | } |
| 650 | } |
| 651 | std::fclose(file); |
Evgeniy Stepanov | 6eb4484 | 2013-06-20 15:56:05 +0000 | [diff] [blame] | 652 | result.set_size(size); |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 653 | return error_code::success(); |
Michael J. Spencer | ee1699c | 2011-01-15 18:52:33 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 656 | error_code map_file_pages(const Twine &path, off_t file_offset, size_t size, |
| 657 | bool map_writable, void *&result) { |
| 658 | SmallString<128> path_storage; |
| 659 | StringRef name = path.toNullTerminatedStringRef(path_storage); |
| 660 | int oflags = map_writable ? O_RDWR : O_RDONLY; |
| 661 | int ofd = ::open(name.begin(), oflags); |
| 662 | if ( ofd == -1 ) |
| 663 | return error_code(errno, system_category()); |
| 664 | AutoFD fd(ofd); |
| 665 | int flags = map_writable ? MAP_SHARED : MAP_PRIVATE; |
| 666 | int prot = map_writable ? (PROT_READ|PROT_WRITE) : PROT_READ; |
| 667 | #ifdef MAP_FILE |
| 668 | flags |= MAP_FILE; |
| 669 | #endif |
| 670 | result = ::mmap(0, size, prot, flags, fd, file_offset); |
| 671 | if (result == MAP_FAILED) { |
| 672 | return error_code(errno, system_category()); |
| 673 | } |
| 674 | |
| 675 | return error_code::success(); |
| 676 | } |
| 677 | |
| 678 | error_code unmap_file_pages(void *base, size_t size) { |
| 679 | if ( ::munmap(base, size) == -1 ) |
| 680 | return error_code(errno, system_category()); |
| 681 | |
| 682 | return error_code::success(); |
| 683 | } |
| 684 | |
Rafael Espindola | a0d9b6b | 2013-07-17 14:58:25 +0000 | [diff] [blame] | 685 | error_code openFileForRead(const Twine &Name, int &ResultFD) { |
| 686 | SmallString<128> Storage; |
| 687 | StringRef P = Name.toNullTerminatedStringRef(Storage); |
| 688 | while ((ResultFD = open(P.begin(), O_RDONLY)) < 0) { |
| 689 | if (errno != EINTR) |
| 690 | return error_code(errno, system_category()); |
| 691 | } |
| 692 | return error_code::success(); |
| 693 | } |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 694 | |
Rafael Espindola | 67080ce | 2013-07-19 15:02:03 +0000 | [diff] [blame] | 695 | error_code openFileForWrite(const Twine &Name, int &ResultFD, |
| 696 | sys::fs::OpenFlags Flags, unsigned Mode) { |
| 697 | // Verify that we don't have both "append" and "excl". |
| 698 | assert((!(Flags & sys::fs::F_Excl) || !(Flags & sys::fs::F_Append)) && |
| 699 | "Cannot specify both 'excl' and 'append' file creation flags!"); |
| 700 | |
Rafael Espindola | 7a0b640 | 2014-02-24 03:07:41 +0000 | [diff] [blame] | 701 | int OpenFlags = O_CREAT; |
| 702 | |
| 703 | if (Flags & F_RW) |
| 704 | OpenFlags |= O_RDWR; |
| 705 | else |
| 706 | OpenFlags |= O_WRONLY; |
Rafael Espindola | 67080ce | 2013-07-19 15:02:03 +0000 | [diff] [blame] | 707 | |
| 708 | if (Flags & F_Append) |
| 709 | OpenFlags |= O_APPEND; |
| 710 | else |
| 711 | OpenFlags |= O_TRUNC; |
| 712 | |
| 713 | if (Flags & F_Excl) |
| 714 | OpenFlags |= O_EXCL; |
| 715 | |
| 716 | SmallString<128> Storage; |
| 717 | StringRef P = Name.toNullTerminatedStringRef(Storage); |
| 718 | while ((ResultFD = open(P.begin(), OpenFlags, Mode)) < 0) { |
| 719 | if (errno != EINTR) |
| 720 | return error_code(errno, system_category()); |
| 721 | } |
| 722 | return error_code::success(); |
| 723 | } |
| 724 | |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 725 | } // end namespace fs |
Peter Collingbourne | f7d4101 | 2014-01-31 23:46:06 +0000 | [diff] [blame] | 726 | |
| 727 | namespace path { |
| 728 | |
| 729 | bool home_directory(SmallVectorImpl<char> &result) { |
| 730 | if (char *RequestedDir = getenv("HOME")) { |
| 731 | result.clear(); |
| 732 | result.append(RequestedDir, RequestedDir + strlen(RequestedDir)); |
| 733 | return true; |
| 734 | } |
| 735 | |
| 736 | return false; |
| 737 | } |
| 738 | |
| 739 | } // end namespace path |
| 740 | |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 741 | } // end namespace sys |
| 742 | } // end namespace llvm |