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