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