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" |
Daniel Dunbar | 3f0fa19 | 2012-05-05 16:36:24 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Process.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 |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 27 | #ifdef HAVE_SYS_MMAN_H |
| 28 | #include <sys/mman.h> |
| 29 | #endif |
Michael J. Spencer | 5271486 | 2011-01-05 16:38:57 +0000 | [diff] [blame] | 30 | #if HAVE_DIRENT_H |
| 31 | # include <dirent.h> |
| 32 | # define NAMLEN(dirent) strlen((dirent)->d_name) |
| 33 | #else |
| 34 | # define dirent direct |
| 35 | # define NAMLEN(dirent) (dirent)->d_namlen |
| 36 | # if HAVE_SYS_NDIR_H |
| 37 | # include <sys/ndir.h> |
| 38 | # endif |
| 39 | # if HAVE_SYS_DIR_H |
| 40 | # include <sys/dir.h> |
| 41 | # endif |
| 42 | # if HAVE_NDIR_H |
| 43 | # include <ndir.h> |
| 44 | # endif |
| 45 | #endif |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 46 | #if HAVE_STDIO_H |
| 47 | #include <stdio.h> |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 48 | #endif |
Bill Wendling | bdaa57f | 2011-09-14 21:49:42 +0000 | [diff] [blame] | 49 | #if HAVE_LIMITS_H |
| 50 | #include <limits.h> |
| 51 | #endif |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 52 | |
Rafael Espindola | 4601c46 | 2013-06-26 05:25:44 +0000 | [diff] [blame] | 53 | #ifdef __APPLE__ |
| 54 | #include <mach-o/dyld.h> |
| 55 | #endif |
| 56 | |
Joerg Sonnenberger | c069730 | 2012-08-10 10:56:09 +0000 | [diff] [blame] | 57 | // Both stdio.h and cstdio are included via different pathes and |
| 58 | // stdcxx's cstdio doesn't include stdio.h, so it doesn't #undef the macros |
| 59 | // either. |
| 60 | #undef ferror |
| 61 | #undef feof |
| 62 | |
Sylvestre Ledru | 14ada94 | 2012-04-11 15:35:36 +0000 | [diff] [blame] | 63 | // For GNU Hurd |
| 64 | #if defined(__GNU__) && !defined(PATH_MAX) |
| 65 | # define PATH_MAX 4096 |
| 66 | #endif |
| 67 | |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 68 | using namespace llvm; |
| 69 | |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 70 | namespace { |
Michael J. Spencer | 5529c57 | 2010-12-07 01:23:08 +0000 | [diff] [blame] | 71 | /// This class automatically closes the given file descriptor when it goes out |
| 72 | /// of scope. You can take back explicit ownership of the file descriptor by |
| 73 | /// calling take(). The destructor does not verify that close was successful. |
| 74 | /// Therefore, never allow this class to call close on a file descriptor that |
| 75 | /// has been read from or written to. |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 76 | struct AutoFD { |
| 77 | int FileDescriptor; |
| 78 | |
| 79 | AutoFD(int fd) : FileDescriptor(fd) {} |
| 80 | ~AutoFD() { |
| 81 | if (FileDescriptor >= 0) |
| 82 | ::close(FileDescriptor); |
| 83 | } |
| 84 | |
| 85 | int take() { |
| 86 | int ret = FileDescriptor; |
| 87 | FileDescriptor = -1; |
| 88 | return ret; |
| 89 | } |
| 90 | |
| 91 | operator int() const {return FileDescriptor;} |
| 92 | }; |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 93 | |
| 94 | error_code TempDir(SmallVectorImpl<char> &result) { |
| 95 | // FIXME: Don't use TMPDIR if program is SUID or SGID enabled. |
| 96 | const char *dir = 0; |
| 97 | (dir = std::getenv("TMPDIR" )) || |
| 98 | (dir = std::getenv("TMP" )) || |
| 99 | (dir = std::getenv("TEMP" )) || |
| 100 | (dir = std::getenv("TEMPDIR")) || |
| 101 | #ifdef P_tmpdir |
| 102 | (dir = P_tmpdir) || |
| 103 | #endif |
| 104 | (dir = "/tmp"); |
| 105 | |
Michael J. Spencer | 98c7a11 | 2010-12-07 01:23:19 +0000 | [diff] [blame] | 106 | result.clear(); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 107 | StringRef d(dir); |
| 108 | result.append(d.begin(), d.end()); |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 109 | return error_code::success(); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 110 | } |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 111 | } |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 112 | |
| 113 | namespace llvm { |
| 114 | namespace sys { |
Michael J. Spencer | 20daa28 | 2010-12-07 01:22:31 +0000 | [diff] [blame] | 115 | namespace fs { |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 116 | #if defined(__FreeBSD__) || defined (__NetBSD__) || defined(__Bitrig__) || \ |
| 117 | defined(__OpenBSD__) || defined(__minix) || defined(__FreeBSD_kernel__) || \ |
| 118 | defined(__linux__) || defined(__CYGWIN__) |
| 119 | static int |
| 120 | test_dir(char buf[PATH_MAX], char ret[PATH_MAX], |
| 121 | const char *dir, const char *bin) |
| 122 | { |
| 123 | struct stat sb; |
| 124 | |
| 125 | snprintf(buf, PATH_MAX, "%s/%s", dir, bin); |
| 126 | if (realpath(buf, ret) == NULL) |
| 127 | return (1); |
| 128 | if (stat(buf, &sb) != 0) |
| 129 | return (1); |
| 130 | |
| 131 | return (0); |
| 132 | } |
| 133 | |
| 134 | static char * |
| 135 | getprogpath(char ret[PATH_MAX], const char *bin) |
| 136 | { |
| 137 | char *pv, *s, *t, buf[PATH_MAX]; |
| 138 | |
| 139 | /* First approach: absolute path. */ |
| 140 | if (bin[0] == '/') { |
| 141 | if (test_dir(buf, ret, "/", bin) == 0) |
| 142 | return (ret); |
| 143 | return (NULL); |
| 144 | } |
| 145 | |
| 146 | /* Second approach: relative path. */ |
| 147 | if (strchr(bin, '/') != NULL) { |
| 148 | if (getcwd(buf, PATH_MAX) == NULL) |
| 149 | return (NULL); |
| 150 | if (test_dir(buf, ret, buf, bin) == 0) |
| 151 | return (ret); |
| 152 | return (NULL); |
| 153 | } |
| 154 | |
| 155 | /* Third approach: $PATH */ |
| 156 | if ((pv = getenv("PATH")) == NULL) |
| 157 | return (NULL); |
| 158 | s = pv = strdup(pv); |
| 159 | if (pv == NULL) |
| 160 | return (NULL); |
| 161 | while ((t = strsep(&s, ":")) != NULL) { |
| 162 | if (test_dir(buf, ret, t, bin) == 0) { |
| 163 | free(pv); |
| 164 | return (ret); |
| 165 | } |
| 166 | } |
| 167 | free(pv); |
| 168 | return (NULL); |
| 169 | } |
| 170 | #endif // __FreeBSD__ || __NetBSD__ || __FreeBSD_kernel__ |
| 171 | |
| 172 | /// GetMainExecutable - Return the path to the main executable, given the |
| 173 | /// value of argv[0] from program startup. |
| 174 | std::string getMainExecutable(const char *argv0, void *MainAddr) { |
| 175 | #if defined(__APPLE__) |
| 176 | // On OS X the executable path is saved to the stack by dyld. Reading it |
| 177 | // from there is much faster than calling dladdr, especially for large |
| 178 | // binaries with symbols. |
| 179 | char exe_path[MAXPATHLEN]; |
| 180 | uint32_t size = sizeof(exe_path); |
| 181 | if (_NSGetExecutablePath(exe_path, &size) == 0) { |
| 182 | char link_path[MAXPATHLEN]; |
| 183 | if (realpath(exe_path, link_path)) |
Rafael Espindola | 4601c46 | 2013-06-26 05:25:44 +0000 | [diff] [blame] | 184 | return link_path; |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 185 | } |
| 186 | #elif defined(__FreeBSD__) || defined (__NetBSD__) || defined(__Bitrig__) || \ |
| 187 | defined(__OpenBSD__) || defined(__minix) || defined(__FreeBSD_kernel__) |
| 188 | char exe_path[PATH_MAX]; |
| 189 | |
| 190 | if (getprogpath(exe_path, argv0) != NULL) |
Rafael Espindola | 2c6f4fe | 2013-06-26 06:10:32 +0000 | [diff] [blame] | 191 | return exe_path; |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 192 | #elif defined(__linux__) || defined(__CYGWIN__) |
| 193 | char exe_path[MAXPATHLEN]; |
| 194 | StringRef aPath("/proc/self/exe"); |
| 195 | if (sys::fs::exists(aPath)) { |
| 196 | // /proc is not always mounted under Linux (chroot for example). |
| 197 | ssize_t len = readlink(aPath.str().c_str(), exe_path, sizeof(exe_path)); |
| 198 | if (len >= 0) |
| 199 | return StringRef(exe_path, len); |
| 200 | } else { |
| 201 | // Fall back to the classical detection. |
| 202 | if (getprogpath(exe_path, argv0) != NULL) |
| 203 | return exe_path; |
| 204 | } |
| 205 | #elif defined(HAVE_DLFCN_H) |
| 206 | // Use dladdr to get executable path if available. |
| 207 | Dl_info DLInfo; |
| 208 | int err = dladdr(MainAddr, &DLInfo); |
| 209 | if (err == 0) |
Rafael Espindola | 2c6f4fe | 2013-06-26 06:10:32 +0000 | [diff] [blame] | 210 | return ""; |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 211 | |
| 212 | // If the filename is a symlink, we need to resolve and return the location of |
| 213 | // the actual executable. |
| 214 | char link_path[MAXPATHLEN]; |
| 215 | if (realpath(DLInfo.dli_fname, link_path)) |
Rafael Espindola | 2c6f4fe | 2013-06-26 06:10:32 +0000 | [diff] [blame] | 216 | return link_path; |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 217 | #else |
| 218 | #error GetMainExecutable is not implemented on this host yet. |
| 219 | #endif |
| 220 | return ""; |
| 221 | } |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 222 | |
Rafael Espindola | be3ede7 | 2013-06-20 21:51:49 +0000 | [diff] [blame] | 223 | TimeValue file_status::getLastModificationTime() const { |
Rafael Espindola | db5d8fe | 2013-06-20 18:42:04 +0000 | [diff] [blame] | 224 | TimeValue Ret; |
| 225 | Ret.fromEpochTime(fs_st_mtime); |
| 226 | return Ret; |
| 227 | } |
| 228 | |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 229 | error_code current_path(SmallVectorImpl<char> &result) { |
Sylvestre Ledru | 14ada94 | 2012-04-11 15:35:36 +0000 | [diff] [blame] | 230 | #ifdef MAXPATHLEN |
Andrew Trick | a4ec5b2 | 2011-03-24 16:43:37 +0000 | [diff] [blame] | 231 | result.reserve(MAXPATHLEN); |
Sylvestre Ledru | 14ada94 | 2012-04-11 15:35:36 +0000 | [diff] [blame] | 232 | #else |
| 233 | // For GNU Hurd |
| 234 | result.reserve(1024); |
| 235 | #endif |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 236 | |
Michael J. Spencer | 20daa28 | 2010-12-07 01:22:31 +0000 | [diff] [blame] | 237 | while (true) { |
| 238 | if (::getcwd(result.data(), result.capacity()) == 0) { |
| 239 | // See if there was a real error. |
| 240 | if (errno != errc::not_enough_memory) |
| 241 | return error_code(errno, system_category()); |
| 242 | // Otherwise there just wasn't enough space. |
| 243 | result.reserve(result.capacity() * 2); |
| 244 | } else |
| 245 | break; |
| 246 | } |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 247 | |
| 248 | result.set_size(strlen(result.data())); |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 249 | return error_code::success(); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 252 | error_code copy_file(const Twine &from, const Twine &to, copy_option copt) { |
| 253 | // Get arguments. |
| 254 | SmallString<128> from_storage; |
| 255 | SmallString<128> to_storage; |
Michael J. Spencer | 795adf5 | 2010-12-01 20:37:42 +0000 | [diff] [blame] | 256 | StringRef f = from.toNullTerminatedStringRef(from_storage); |
| 257 | StringRef t = to.toNullTerminatedStringRef(to_storage); |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 258 | |
| 259 | const size_t buf_sz = 32768; |
| 260 | char buffer[buf_sz]; |
| 261 | int from_file = -1, to_file = -1; |
| 262 | |
| 263 | // Open from. |
| 264 | if ((from_file = ::open(f.begin(), O_RDONLY)) < 0) |
| 265 | return error_code(errno, system_category()); |
| 266 | AutoFD from_fd(from_file); |
| 267 | |
| 268 | // Stat from. |
| 269 | struct stat from_stat; |
| 270 | if (::stat(f.begin(), &from_stat) != 0) |
| 271 | return error_code(errno, system_category()); |
| 272 | |
| 273 | // Setup to flags. |
| 274 | int to_flags = O_CREAT | O_WRONLY; |
| 275 | if (copt == copy_option::fail_if_exists) |
| 276 | to_flags |= O_EXCL; |
| 277 | |
| 278 | // Open to. |
| 279 | if ((to_file = ::open(t.begin(), to_flags, from_stat.st_mode)) < 0) |
| 280 | return error_code(errno, system_category()); |
| 281 | AutoFD to_fd(to_file); |
| 282 | |
| 283 | // Copy! |
| 284 | ssize_t sz, sz_read = 1, sz_write; |
| 285 | while (sz_read > 0 && |
| 286 | (sz_read = ::read(from_fd, buffer, buf_sz)) > 0) { |
| 287 | // Allow for partial writes - see Advanced Unix Programming (2nd Ed.), |
| 288 | // Marc Rochkind, Addison-Wesley, 2004, page 94 |
| 289 | sz_write = 0; |
| 290 | do { |
| 291 | if ((sz = ::write(to_fd, buffer + sz_write, sz_read - sz_write)) < 0) { |
| 292 | sz_read = sz; // cause read loop termination. |
| 293 | break; // error. |
| 294 | } |
| 295 | sz_write += sz; |
| 296 | } while (sz_write < sz_read); |
| 297 | } |
| 298 | |
| 299 | // After all the file operations above the return value of close actually |
| 300 | // matters. |
| 301 | if (::close(from_fd.take()) < 0) sz_read = -1; |
| 302 | if (::close(to_fd.take()) < 0) sz_read = -1; |
| 303 | |
| 304 | // Check for errors. |
| 305 | if (sz_read < 0) |
| 306 | return error_code(errno, system_category()); |
| 307 | |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 308 | return error_code::success(); |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Michael J. Spencer | 31e310c | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 311 | error_code create_directory(const Twine &path, bool &existed) { |
| 312 | SmallString<128> path_storage; |
| 313 | StringRef p = path.toNullTerminatedStringRef(path_storage); |
| 314 | |
Michael J. Spencer | e5755be | 2010-12-07 01:23:29 +0000 | [diff] [blame] | 315 | if (::mkdir(p.begin(), S_IRWXU | S_IRWXG) == -1) { |
Michael J. Spencer | 66a1f86 | 2010-12-04 18:45:32 +0000 | [diff] [blame] | 316 | if (errno != errc::file_exists) |
Michael J. Spencer | 31e310c | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 317 | return error_code(errno, system_category()); |
| 318 | existed = true; |
| 319 | } else |
| 320 | existed = false; |
| 321 | |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 322 | return error_code::success(); |
Michael J. Spencer | 31e310c | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Michael J. Spencer | e0c4560 | 2010-12-03 05:58:41 +0000 | [diff] [blame] | 325 | error_code create_hard_link(const Twine &to, const Twine &from) { |
| 326 | // Get arguments. |
| 327 | SmallString<128> from_storage; |
| 328 | SmallString<128> to_storage; |
| 329 | StringRef f = from.toNullTerminatedStringRef(from_storage); |
| 330 | StringRef t = to.toNullTerminatedStringRef(to_storage); |
| 331 | |
| 332 | if (::link(t.begin(), f.begin()) == -1) |
| 333 | return error_code(errno, system_category()); |
| 334 | |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 335 | return error_code::success(); |
Michael J. Spencer | e0c4560 | 2010-12-03 05:58:41 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Michael J. Spencer | 7ee6d5d | 2010-12-03 07:41:25 +0000 | [diff] [blame] | 338 | error_code create_symlink(const Twine &to, const Twine &from) { |
| 339 | // Get arguments. |
| 340 | SmallString<128> from_storage; |
| 341 | SmallString<128> to_storage; |
| 342 | StringRef f = from.toNullTerminatedStringRef(from_storage); |
| 343 | StringRef t = to.toNullTerminatedStringRef(to_storage); |
| 344 | |
| 345 | if (::symlink(t.begin(), f.begin()) == -1) |
| 346 | return error_code(errno, system_category()); |
| 347 | |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 348 | return error_code::success(); |
Michael J. Spencer | 7ee6d5d | 2010-12-03 07:41:25 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Michael J. Spencer | 6e74e11 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 351 | error_code remove(const Twine &path, bool &existed) { |
| 352 | SmallString<128> path_storage; |
| 353 | StringRef p = path.toNullTerminatedStringRef(path_storage); |
| 354 | |
Rafael Espindola | 8cd62b0 | 2013-06-17 20:35:51 +0000 | [diff] [blame] | 355 | struct stat buf; |
| 356 | if (stat(p.begin(), &buf) != 0) { |
| 357 | if (errno != errc::no_such_file_or_directory) |
| 358 | return error_code(errno, system_category()); |
| 359 | existed = false; |
| 360 | return error_code::success(); |
| 361 | } |
| 362 | |
| 363 | // Note: this check catches strange situations. In all cases, LLVM should |
| 364 | // only be involved in the creation and deletion of regular files. This |
| 365 | // check ensures that what we're trying to erase is a regular file. It |
| 366 | // effectively prevents LLVM from erasing things like /dev/null, any block |
| 367 | // special file, or other things that aren't "regular" files. |
| 368 | if (!S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)) |
| 369 | return make_error_code(errc::operation_not_permitted); |
| 370 | |
Michael J. Spencer | 6e74e11 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 371 | if (::remove(p.begin()) == -1) { |
Michael J. Spencer | 66a1f86 | 2010-12-04 18:45:32 +0000 | [diff] [blame] | 372 | if (errno != errc::no_such_file_or_directory) |
Michael J. Spencer | 6e74e11 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 373 | return error_code(errno, system_category()); |
| 374 | existed = false; |
| 375 | } else |
| 376 | existed = true; |
| 377 | |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 378 | return error_code::success(); |
Michael J. Spencer | 6e74e11 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Michael J. Spencer | 409f556 | 2010-12-03 17:53:55 +0000 | [diff] [blame] | 381 | error_code rename(const Twine &from, const Twine &to) { |
| 382 | // Get arguments. |
| 383 | SmallString<128> from_storage; |
| 384 | SmallString<128> to_storage; |
| 385 | StringRef f = from.toNullTerminatedStringRef(from_storage); |
| 386 | StringRef t = to.toNullTerminatedStringRef(to_storage); |
| 387 | |
Michael J. Spencer | ec202ee | 2011-01-16 22:18:41 +0000 | [diff] [blame] | 388 | if (::rename(f.begin(), t.begin()) == -1) { |
| 389 | // If it's a cross device link, copy then delete, otherwise return the error |
| 390 | if (errno == EXDEV) { |
| 391 | if (error_code ec = copy_file(from, to, copy_option::overwrite_if_exists)) |
| 392 | return ec; |
| 393 | bool Existed; |
| 394 | if (error_code ec = remove(from, Existed)) |
| 395 | return ec; |
| 396 | } else |
| 397 | return error_code(errno, system_category()); |
| 398 | } |
Michael J. Spencer | 409f556 | 2010-12-03 17:53:55 +0000 | [diff] [blame] | 399 | |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 400 | return error_code::success(); |
Michael J. Spencer | 409f556 | 2010-12-03 17:53:55 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Michael J. Spencer | c20a032 | 2010-12-03 17:54:07 +0000 | [diff] [blame] | 403 | error_code resize_file(const Twine &path, uint64_t size) { |
| 404 | SmallString<128> path_storage; |
| 405 | StringRef p = path.toNullTerminatedStringRef(path_storage); |
| 406 | |
| 407 | if (::truncate(p.begin(), size) == -1) |
| 408 | return error_code(errno, system_category()); |
| 409 | |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 410 | return error_code::success(); |
Michael J. Spencer | c20a032 | 2010-12-03 17:54:07 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 413 | error_code exists(const Twine &path, bool &result) { |
| 414 | SmallString<128> path_storage; |
| 415 | StringRef p = path.toNullTerminatedStringRef(path_storage); |
| 416 | |
Benjamin Kramer | 172f808 | 2012-06-02 16:28:09 +0000 | [diff] [blame] | 417 | if (::access(p.begin(), F_OK) == -1) { |
Michael J. Spencer | 66a1f86 | 2010-12-04 18:45:32 +0000 | [diff] [blame] | 418 | if (errno != errc::no_such_file_or_directory) |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 419 | return error_code(errno, system_category()); |
| 420 | result = false; |
| 421 | } else |
| 422 | result = true; |
| 423 | |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 424 | return error_code::success(); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Rafael Espindola | a1280c1 | 2013-06-18 20:56:38 +0000 | [diff] [blame] | 427 | bool can_write(const Twine &Path) { |
| 428 | SmallString<128> PathStorage; |
| 429 | StringRef P = Path.toNullTerminatedStringRef(PathStorage); |
| 430 | return 0 == access(P.begin(), W_OK); |
| 431 | } |
| 432 | |
Rafael Espindola | b0a5c96 | 2013-06-14 19:38:45 +0000 | [diff] [blame] | 433 | bool can_execute(const Twine &Path) { |
| 434 | SmallString<128> PathStorage; |
| 435 | StringRef P = Path.toNullTerminatedStringRef(PathStorage); |
| 436 | |
Manuel Klimek | 52772bf | 2013-06-17 10:48:34 +0000 | [diff] [blame] | 437 | if (0 != access(P.begin(), R_OK | X_OK)) |
| 438 | return false; |
| 439 | struct stat buf; |
| 440 | if (0 != stat(P.begin(), &buf)) |
| 441 | return false; |
| 442 | if (!S_ISREG(buf.st_mode)) |
| 443 | return false; |
| 444 | return true; |
Rafael Espindola | b0a5c96 | 2013-06-14 19:38:45 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 447 | bool equivalent(file_status A, file_status B) { |
| 448 | assert(status_known(A) && status_known(B)); |
Sylvestre Ledru | 3099f4bd | 2012-04-23 16:37:23 +0000 | [diff] [blame] | 449 | return A.fs_st_dev == B.fs_st_dev && |
| 450 | A.fs_st_ino == B.fs_st_ino; |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Michael J. Spencer | 376d387 | 2010-12-03 18:49:13 +0000 | [diff] [blame] | 453 | error_code equivalent(const Twine &A, const Twine &B, bool &result) { |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 454 | file_status fsA, fsB; |
| 455 | if (error_code ec = status(A, fsA)) return ec; |
| 456 | if (error_code ec = status(B, fsB)) return ec; |
| 457 | result = equivalent(fsA, fsB); |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 458 | return error_code::success(); |
Michael J. Spencer | 376d387 | 2010-12-03 18:49:13 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Michael J. Spencer | 818ab4a | 2010-12-04 00:31:48 +0000 | [diff] [blame] | 461 | error_code file_size(const Twine &path, uint64_t &result) { |
| 462 | SmallString<128> path_storage; |
| 463 | StringRef p = path.toNullTerminatedStringRef(path_storage); |
| 464 | |
| 465 | struct stat status; |
| 466 | if (::stat(p.begin(), &status) == -1) |
| 467 | return error_code(errno, system_category()); |
| 468 | if (!S_ISREG(status.st_mode)) |
Michael J. Spencer | 66a1f86 | 2010-12-04 18:45:32 +0000 | [diff] [blame] | 469 | return make_error_code(errc::operation_not_permitted); |
Michael J. Spencer | 818ab4a | 2010-12-04 00:31:48 +0000 | [diff] [blame] | 470 | |
| 471 | result = status.st_size; |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 472 | return error_code::success(); |
Michael J. Spencer | 818ab4a | 2010-12-04 00:31:48 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Rafael Espindola | 7cf7c51 | 2013-06-20 15:06:35 +0000 | [diff] [blame] | 475 | error_code getUniqueID(const Twine Path, uint64_t &Result) { |
Rafael Espindola | 45e6c24 | 2013-06-18 19:34:49 +0000 | [diff] [blame] | 476 | SmallString<128> Storage; |
| 477 | StringRef P = Path.toNullTerminatedStringRef(Storage); |
| 478 | |
| 479 | struct stat Status; |
| 480 | if (::stat(P.begin(), &Status) != 0) |
| 481 | return error_code(errno, system_category()); |
| 482 | |
| 483 | Result = Status.st_ino; |
| 484 | return error_code::success(); |
| 485 | } |
| 486 | |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 487 | error_code status(const Twine &path, file_status &result) { |
| 488 | SmallString<128> path_storage; |
| 489 | StringRef p = path.toNullTerminatedStringRef(path_storage); |
| 490 | |
| 491 | struct stat status; |
| 492 | if (::stat(p.begin(), &status) != 0) { |
| 493 | error_code ec(errno, system_category()); |
| 494 | if (ec == errc::no_such_file_or_directory) |
| 495 | result = file_status(file_type::file_not_found); |
| 496 | else |
| 497 | result = file_status(file_type::status_error); |
| 498 | return ec; |
| 499 | } |
| 500 | |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 501 | perms prms = static_cast<perms>(status.st_mode & perms_mask); |
| 502 | |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 503 | if (S_ISDIR(status.st_mode)) |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 504 | result = file_status(file_type::directory_file, prms); |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 505 | else if (S_ISREG(status.st_mode)) |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 506 | result = file_status(file_type::regular_file, prms); |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 507 | else if (S_ISBLK(status.st_mode)) |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 508 | result = file_status(file_type::block_file, prms); |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 509 | else if (S_ISCHR(status.st_mode)) |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 510 | result = file_status(file_type::character_file, prms); |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 511 | else if (S_ISFIFO(status.st_mode)) |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 512 | result = file_status(file_type::fifo_file, prms); |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 513 | else if (S_ISSOCK(status.st_mode)) |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 514 | result = file_status(file_type::socket_file, prms); |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 515 | else |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 516 | result = file_status(file_type::type_unknown, prms); |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 517 | |
Sylvestre Ledru | 3099f4bd | 2012-04-23 16:37:23 +0000 | [diff] [blame] | 518 | result.fs_st_dev = status.st_dev; |
| 519 | result.fs_st_ino = status.st_ino; |
Rafael Espindola | db5d8fe | 2013-06-20 18:42:04 +0000 | [diff] [blame] | 520 | result.fs_st_mtime = status.st_mtime; |
Rafael Espindola | e34d6a5 | 2013-06-20 22:02:10 +0000 | [diff] [blame] | 521 | result.fs_st_uid = status.st_uid; |
| 522 | result.fs_st_gid = status.st_gid; |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 523 | |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 524 | return error_code::success(); |
Michael J. Spencer | db5576a | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 527 | // Modifies permissions on a file. |
| 528 | error_code permissions(const Twine &path, perms prms) { |
| 529 | if ((prms & add_perms) && (prms & remove_perms)) |
| 530 | llvm_unreachable("add_perms and remove_perms are mutually exclusive"); |
| 531 | |
| 532 | // Get current permissions |
Rafael Espindola | 1efb69c | 2013-06-20 22:07:53 +0000 | [diff] [blame] | 533 | // FIXME: We only need this stat for add_perms and remove_perms. |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 534 | file_status info; |
| 535 | if (error_code ec = status(path, info)) { |
| 536 | return ec; |
| 537 | } |
| 538 | |
| 539 | // Set updated permissions. |
| 540 | SmallString<128> path_storage; |
| 541 | StringRef p = path.toNullTerminatedStringRef(path_storage); |
| 542 | perms permsToSet; |
| 543 | if (prms & add_perms) { |
| 544 | permsToSet = (info.permissions() | prms) & perms_mask; |
| 545 | } else if (prms & remove_perms) { |
| 546 | permsToSet = (info.permissions() & ~prms) & perms_mask; |
| 547 | } else { |
| 548 | permsToSet = prms & perms_mask; |
| 549 | } |
| 550 | if (::chmod(p.begin(), static_cast<mode_t>(permsToSet))) { |
| 551 | return error_code(errno, system_category()); |
| 552 | } |
| 553 | |
| 554 | return error_code::success(); |
| 555 | } |
| 556 | |
Rafael Espindola | 4a3365c | 2013-06-20 20:56:14 +0000 | [diff] [blame] | 557 | error_code setLastModificationAndAccessTime(int FD, TimeValue Time) { |
| 558 | timeval Times[2]; |
| 559 | Times[0].tv_sec = Time.toPosixTime(); |
| 560 | Times[0].tv_usec = 0; |
| 561 | Times[1] = Times[0]; |
| 562 | if (::futimes(FD, Times)) |
| 563 | return error_code(errno, system_category()); |
| 564 | return error_code::success(); |
| 565 | } |
| 566 | |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 567 | error_code unique_file(const Twine &model, int &result_fd, |
Eric Christopher | b6148ed | 2012-05-11 00:07:44 +0000 | [diff] [blame] | 568 | SmallVectorImpl<char> &result_path, |
| 569 | bool makeAbsolute, unsigned mode) { |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 570 | SmallString<128> Model; |
| 571 | model.toVector(Model); |
| 572 | // Null terminate. |
| 573 | Model.c_str(); |
| 574 | |
Argyrios Kyrtzidis | 348937d | 2011-07-28 00:29:20 +0000 | [diff] [blame] | 575 | if (makeAbsolute) { |
| 576 | // Make model absolute by prepending a temp directory if it's not already. |
| 577 | bool absolute = path::is_absolute(Twine(Model)); |
| 578 | if (!absolute) { |
| 579 | SmallString<128> TDir; |
| 580 | if (error_code ec = TempDir(TDir)) return ec; |
| 581 | path::append(TDir, Twine(Model)); |
| 582 | Model.swap(TDir); |
| 583 | } |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Daniel Dunbar | 58ed0c6 | 2012-05-05 16:39:22 +0000 | [diff] [blame] | 586 | // From here on, DO NOT modify model. It may be needed if the randomly chosen |
| 587 | // path already exists. |
Daniel Dunbar | 3f0fa19 | 2012-05-05 16:36:24 +0000 | [diff] [blame] | 588 | SmallString<128> RandomPath = Model; |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 589 | |
| 590 | retry_random_path: |
Daniel Dunbar | 58ed0c6 | 2012-05-05 16:39:22 +0000 | [diff] [blame] | 591 | // Replace '%' with random chars. |
Daniel Dunbar | 3f0fa19 | 2012-05-05 16:36:24 +0000 | [diff] [blame] | 592 | for (unsigned i = 0, e = Model.size(); i != e; ++i) { |
| 593 | if (Model[i] == '%') |
| 594 | RandomPath[i] = "0123456789abcdef"[sys::Process::GetRandomNumber() & 15]; |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Argyrios Kyrtzidis | eed2dc5 | 2013-02-28 00:38:19 +0000 | [diff] [blame] | 597 | // Make sure we don't fall into an infinite loop by constantly trying |
| 598 | // to create the parent path. |
| 599 | bool TriedToCreateParent = false; |
| 600 | |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 601 | // Try to open + create the file. |
| 602 | rety_open_create: |
Eric Christopher | b6148ed | 2012-05-11 00:07:44 +0000 | [diff] [blame] | 603 | int RandomFD = ::open(RandomPath.c_str(), O_RDWR | O_CREAT | O_EXCL, mode); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 604 | if (RandomFD == -1) { |
Douglas Gregor | 95585ab | 2013-01-10 01:58:46 +0000 | [diff] [blame] | 605 | int SavedErrno = errno; |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 606 | // If the file existed, try again, otherwise, error. |
Douglas Gregor | 95585ab | 2013-01-10 01:58:46 +0000 | [diff] [blame] | 607 | if (SavedErrno == errc::file_exists) |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 608 | goto retry_random_path; |
Daniel Dunbar | 61d59f2 | 2012-11-15 20:24:52 +0000 | [diff] [blame] | 609 | // If path prefix doesn't exist, try to create it. |
Douglas Gregor | 6bd4d8c | 2013-04-05 20:48:36 +0000 | [diff] [blame] | 610 | if (SavedErrno == errc::no_such_file_or_directory && !TriedToCreateParent) { |
Argyrios Kyrtzidis | eed2dc5 | 2013-02-28 00:38:19 +0000 | [diff] [blame] | 611 | TriedToCreateParent = true; |
Daniel Dunbar | 61d59f2 | 2012-11-15 20:24:52 +0000 | [diff] [blame] | 612 | StringRef p(RandomPath); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 613 | SmallString<64> dir_to_create; |
| 614 | for (path::const_iterator i = path::begin(p), |
| 615 | e = --path::end(p); i != e; ++i) { |
Michael J. Spencer | 1e090f0 | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 616 | path::append(dir_to_create, *i); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 617 | bool Exists; |
| 618 | if (error_code ec = exists(Twine(dir_to_create), Exists)) return ec; |
| 619 | if (!Exists) { |
| 620 | // Don't try to create network paths. |
| 621 | if (i->size() > 2 && (*i)[0] == '/' && |
| 622 | (*i)[1] == '/' && |
| 623 | (*i)[2] != '/') |
Michael J. Spencer | 66a1f86 | 2010-12-04 18:45:32 +0000 | [diff] [blame] | 624 | return make_error_code(errc::no_such_file_or_directory); |
Douglas Gregor | 95585ab | 2013-01-10 01:58:46 +0000 | [diff] [blame] | 625 | if (::mkdir(dir_to_create.c_str(), 0700) == -1 && |
| 626 | errno != errc::file_exists) |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 627 | return error_code(errno, system_category()); |
| 628 | } |
| 629 | } |
| 630 | goto rety_open_create; |
| 631 | } |
Douglas Gregor | 95585ab | 2013-01-10 01:58:46 +0000 | [diff] [blame] | 632 | |
| 633 | return error_code(SavedErrno, system_category()); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Michael J. Spencer | 66a1f86 | 2010-12-04 18:45:32 +0000 | [diff] [blame] | 636 | // Make the path absolute. |
Andrew Trick | a4ec5b2 | 2011-03-24 16:43:37 +0000 | [diff] [blame] | 637 | char real_path_buff[PATH_MAX + 1]; |
| 638 | if (realpath(RandomPath.c_str(), real_path_buff) == NULL) { |
| 639 | int error = errno; |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 640 | ::close(RandomFD); |
| 641 | ::unlink(RandomPath.c_str()); |
Andrew Trick | a4ec5b2 | 2011-03-24 16:43:37 +0000 | [diff] [blame] | 642 | return error_code(error, system_category()); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Andrew Trick | a4ec5b2 | 2011-03-24 16:43:37 +0000 | [diff] [blame] | 645 | result_path.clear(); |
| 646 | StringRef d(real_path_buff); |
| 647 | result_path.append(d.begin(), d.end()); |
| 648 | |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 649 | result_fd = RandomFD; |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 650 | return error_code::success(); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 653 | error_code mapped_file_region::init(int FD, bool CloseFD, uint64_t Offset) { |
| 654 | AutoFD ScopedFD(FD); |
| 655 | if (!CloseFD) |
| 656 | ScopedFD.take(); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 657 | |
| 658 | // Figure out how large the file is. |
| 659 | struct stat FileInfo; |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 660 | if (fstat(FD, &FileInfo) == -1) |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 661 | return error_code(errno, system_category()); |
| 662 | uint64_t FileSize = FileInfo.st_size; |
| 663 | |
| 664 | if (Size == 0) |
| 665 | Size = FileSize; |
| 666 | else if (FileSize < Size) { |
| 667 | // We need to grow the file. |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 668 | if (ftruncate(FD, Size) == -1) |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 669 | return error_code(errno, system_category()); |
| 670 | } |
| 671 | |
| 672 | int flags = (Mode == readwrite) ? MAP_SHARED : MAP_PRIVATE; |
| 673 | int prot = (Mode == readonly) ? PROT_READ : (PROT_READ | PROT_WRITE); |
| 674 | #ifdef MAP_FILE |
| 675 | flags |= MAP_FILE; |
| 676 | #endif |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 677 | Mapping = ::mmap(0, Size, prot, flags, FD, Offset); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 678 | if (Mapping == MAP_FAILED) |
| 679 | return error_code(errno, system_category()); |
| 680 | return error_code::success(); |
| 681 | } |
| 682 | |
| 683 | mapped_file_region::mapped_file_region(const Twine &path, |
| 684 | mapmode mode, |
| 685 | uint64_t length, |
| 686 | uint64_t offset, |
| 687 | error_code &ec) |
| 688 | : Mode(mode) |
| 689 | , Size(length) |
| 690 | , Mapping() { |
| 691 | // Make sure that the requested size fits within SIZE_T. |
| 692 | if (length > std::numeric_limits<size_t>::max()) { |
| 693 | ec = make_error_code(errc::invalid_argument); |
| 694 | return; |
| 695 | } |
| 696 | |
| 697 | SmallString<128> path_storage; |
| 698 | StringRef name = path.toNullTerminatedStringRef(path_storage); |
| 699 | int oflags = (mode == readonly) ? O_RDONLY : O_RDWR; |
| 700 | int ofd = ::open(name.begin(), oflags); |
| 701 | if (ofd == -1) { |
| 702 | ec = error_code(errno, system_category()); |
| 703 | return; |
| 704 | } |
| 705 | |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 706 | ec = init(ofd, true, offset); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 707 | if (ec) |
| 708 | Mapping = 0; |
| 709 | } |
| 710 | |
| 711 | mapped_file_region::mapped_file_region(int fd, |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 712 | bool closefd, |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 713 | mapmode mode, |
| 714 | uint64_t length, |
| 715 | uint64_t offset, |
| 716 | error_code &ec) |
| 717 | : Mode(mode) |
| 718 | , Size(length) |
| 719 | , Mapping() { |
| 720 | // Make sure that the requested size fits within SIZE_T. |
| 721 | if (length > std::numeric_limits<size_t>::max()) { |
| 722 | ec = make_error_code(errc::invalid_argument); |
| 723 | return; |
| 724 | } |
| 725 | |
Michael J. Spencer | 42ad29f | 2013-03-14 00:20:10 +0000 | [diff] [blame] | 726 | ec = init(fd, closefd, offset); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 727 | if (ec) |
| 728 | Mapping = 0; |
| 729 | } |
| 730 | |
| 731 | mapped_file_region::~mapped_file_region() { |
| 732 | if (Mapping) |
| 733 | ::munmap(Mapping, Size); |
| 734 | } |
| 735 | |
Chandler Carruth | f12e3a6 | 2012-11-30 11:45:22 +0000 | [diff] [blame] | 736 | #if LLVM_HAS_RVALUE_REFERENCES |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 737 | mapped_file_region::mapped_file_region(mapped_file_region &&other) |
| 738 | : Mode(other.Mode), Size(other.Size), Mapping(other.Mapping) { |
| 739 | other.Mapping = 0; |
| 740 | } |
| 741 | #endif |
| 742 | |
| 743 | mapped_file_region::mapmode mapped_file_region::flags() const { |
| 744 | assert(Mapping && "Mapping failed but used anyway!"); |
| 745 | return Mode; |
| 746 | } |
| 747 | |
| 748 | uint64_t mapped_file_region::size() const { |
| 749 | assert(Mapping && "Mapping failed but used anyway!"); |
| 750 | return Size; |
| 751 | } |
| 752 | |
| 753 | char *mapped_file_region::data() const { |
| 754 | assert(Mapping && "Mapping failed but used anyway!"); |
| 755 | assert(Mode != readonly && "Cannot get non const data for readonly mapping!"); |
| 756 | return reinterpret_cast<char*>(Mapping); |
| 757 | } |
| 758 | |
| 759 | const char *mapped_file_region::const_data() const { |
| 760 | assert(Mapping && "Mapping failed but used anyway!"); |
| 761 | return reinterpret_cast<const char*>(Mapping); |
| 762 | } |
| 763 | |
| 764 | int mapped_file_region::alignment() { |
Chandler Carruth | acd64be | 2012-12-31 23:31:56 +0000 | [diff] [blame] | 765 | return process::get_self()->page_size(); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 768 | error_code detail::directory_iterator_construct(detail::DirIterState &it, |
| 769 | StringRef path){ |
Michael J. Spencer | 5271486 | 2011-01-05 16:38:57 +0000 | [diff] [blame] | 770 | SmallString<128> path_null(path); |
| 771 | DIR *directory = ::opendir(path_null.c_str()); |
| 772 | if (directory == 0) |
| 773 | return error_code(errno, system_category()); |
| 774 | |
| 775 | it.IterationHandle = reinterpret_cast<intptr_t>(directory); |
| 776 | // Add something for replace_filename to replace. |
| 777 | path::append(path_null, "."); |
| 778 | it.CurrentEntry = directory_entry(path_null.str()); |
| 779 | return directory_iterator_increment(it); |
| 780 | } |
| 781 | |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 782 | error_code detail::directory_iterator_destruct(detail::DirIterState &it) { |
Michael J. Spencer | 5271486 | 2011-01-05 16:38:57 +0000 | [diff] [blame] | 783 | if (it.IterationHandle) |
| 784 | ::closedir(reinterpret_cast<DIR *>(it.IterationHandle)); |
| 785 | it.IterationHandle = 0; |
| 786 | it.CurrentEntry = directory_entry(); |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 787 | return error_code::success(); |
Michael J. Spencer | 5271486 | 2011-01-05 16:38:57 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 790 | error_code detail::directory_iterator_increment(detail::DirIterState &it) { |
Michael J. Spencer | 5271486 | 2011-01-05 16:38:57 +0000 | [diff] [blame] | 791 | errno = 0; |
| 792 | dirent *cur_dir = ::readdir(reinterpret_cast<DIR *>(it.IterationHandle)); |
| 793 | if (cur_dir == 0 && errno != 0) { |
| 794 | return error_code(errno, system_category()); |
| 795 | } else if (cur_dir != 0) { |
| 796 | StringRef name(cur_dir->d_name, NAMLEN(cur_dir)); |
| 797 | if ((name.size() == 1 && name[0] == '.') || |
| 798 | (name.size() == 2 && name[0] == '.' && name[1] == '.')) |
| 799 | return directory_iterator_increment(it); |
| 800 | it.CurrentEntry.replace_filename(name); |
| 801 | } else |
| 802 | return directory_iterator_destruct(it); |
| 803 | |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 804 | return error_code::success(); |
Michael J. Spencer | 5271486 | 2011-01-05 16:38:57 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Michael J. Spencer | ee1699c | 2011-01-15 18:52:33 +0000 | [diff] [blame] | 807 | error_code get_magic(const Twine &path, uint32_t len, |
| 808 | SmallVectorImpl<char> &result) { |
| 809 | SmallString<128> PathStorage; |
| 810 | StringRef Path = path.toNullTerminatedStringRef(PathStorage); |
| 811 | result.set_size(0); |
| 812 | |
| 813 | // Open path. |
| 814 | std::FILE *file = std::fopen(Path.data(), "rb"); |
| 815 | if (file == 0) |
| 816 | return error_code(errno, system_category()); |
| 817 | |
| 818 | // Reserve storage. |
| 819 | result.reserve(len); |
| 820 | |
| 821 | // Read magic! |
| 822 | size_t size = std::fread(result.data(), 1, len, file); |
| 823 | if (std::ferror(file) != 0) { |
| 824 | std::fclose(file); |
| 825 | return error_code(errno, system_category()); |
Evgeniy Stepanov | 6eb4484 | 2013-06-20 15:56:05 +0000 | [diff] [blame] | 826 | } else if (size != len) { |
Michael J. Spencer | ee1699c | 2011-01-15 18:52:33 +0000 | [diff] [blame] | 827 | if (std::feof(file) != 0) { |
| 828 | std::fclose(file); |
| 829 | result.set_size(size); |
| 830 | return make_error_code(errc::value_too_large); |
| 831 | } |
| 832 | } |
| 833 | std::fclose(file); |
Evgeniy Stepanov | 6eb4484 | 2013-06-20 15:56:05 +0000 | [diff] [blame] | 834 | result.set_size(size); |
David Blaikie | 18544b9 | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 835 | return error_code::success(); |
Michael J. Spencer | ee1699c | 2011-01-15 18:52:33 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 838 | error_code map_file_pages(const Twine &path, off_t file_offset, size_t size, |
| 839 | bool map_writable, void *&result) { |
| 840 | SmallString<128> path_storage; |
| 841 | StringRef name = path.toNullTerminatedStringRef(path_storage); |
| 842 | int oflags = map_writable ? O_RDWR : O_RDONLY; |
| 843 | int ofd = ::open(name.begin(), oflags); |
| 844 | if ( ofd == -1 ) |
| 845 | return error_code(errno, system_category()); |
| 846 | AutoFD fd(ofd); |
| 847 | int flags = map_writable ? MAP_SHARED : MAP_PRIVATE; |
| 848 | int prot = map_writable ? (PROT_READ|PROT_WRITE) : PROT_READ; |
| 849 | #ifdef MAP_FILE |
| 850 | flags |= MAP_FILE; |
| 851 | #endif |
| 852 | result = ::mmap(0, size, prot, flags, fd, file_offset); |
| 853 | if (result == MAP_FAILED) { |
| 854 | return error_code(errno, system_category()); |
| 855 | } |
| 856 | |
| 857 | return error_code::success(); |
| 858 | } |
| 859 | |
| 860 | error_code unmap_file_pages(void *base, size_t size) { |
| 861 | if ( ::munmap(base, size) == -1 ) |
| 862 | return error_code(errno, system_category()); |
| 863 | |
| 864 | return error_code::success(); |
| 865 | } |
| 866 | |
| 867 | |
Michael J. Spencer | 9fc1d9d | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 868 | } // end namespace fs |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 869 | } // end namespace sys |
| 870 | } // end namespace llvm |