Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 1 | //===- llvm/Support/Unix/PathV2.cpp - Unix Path Implementation --*- C++ -*-===// |
| 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 | // |
| 10 | // This file implements the Unix specific implementation of the PathV2 API. |
| 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" |
Michael J. Spencer | bee0c38 | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 20 | #if HAVE_SYS_STAT_H |
| 21 | #include <sys/stat.h> |
| 22 | #endif |
| 23 | #if HAVE_FCNTL_H |
| 24 | #include <fcntl.h> |
| 25 | #endif |
Michael J. Spencer | da7c1ca | 2011-01-05 16:38:57 +0000 | [diff] [blame] | 26 | #if HAVE_DIRENT_H |
| 27 | # include <dirent.h> |
| 28 | # define NAMLEN(dirent) strlen((dirent)->d_name) |
| 29 | #else |
| 30 | # define dirent direct |
| 31 | # define NAMLEN(dirent) (dirent)->d_namlen |
| 32 | # if HAVE_SYS_NDIR_H |
| 33 | # include <sys/ndir.h> |
| 34 | # endif |
| 35 | # if HAVE_SYS_DIR_H |
| 36 | # include <sys/dir.h> |
| 37 | # endif |
| 38 | # if HAVE_NDIR_H |
| 39 | # include <ndir.h> |
| 40 | # endif |
| 41 | #endif |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 42 | #if HAVE_STDIO_H |
| 43 | #include <stdio.h> |
Michael J. Spencer | bee0c38 | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 44 | #endif |
Bill Wendling | 544e412 | 2011-09-14 21:49:42 +0000 | [diff] [blame] | 45 | #if HAVE_LIMITS_H |
| 46 | #include <limits.h> |
| 47 | #endif |
Michael J. Spencer | bee0c38 | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 48 | |
Sylvestre Ledru | 6fc30c2 | 2012-04-11 15:35:36 +0000 | [diff] [blame] | 49 | // For GNU Hurd |
| 50 | #if defined(__GNU__) && !defined(PATH_MAX) |
| 51 | # define PATH_MAX 4096 |
| 52 | #endif |
| 53 | |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 54 | using namespace llvm; |
| 55 | |
Michael J. Spencer | bee0c38 | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 56 | namespace { |
Michael J. Spencer | ad8a14f | 2010-12-07 01:23:08 +0000 | [diff] [blame] | 57 | /// This class automatically closes the given file descriptor when it goes out |
| 58 | /// of scope. You can take back explicit ownership of the file descriptor by |
| 59 | /// calling take(). The destructor does not verify that close was successful. |
| 60 | /// Therefore, never allow this class to call close on a file descriptor that |
| 61 | /// has been read from or written to. |
Michael J. Spencer | bee0c38 | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 62 | struct AutoFD { |
| 63 | int FileDescriptor; |
| 64 | |
| 65 | AutoFD(int fd) : FileDescriptor(fd) {} |
| 66 | ~AutoFD() { |
| 67 | if (FileDescriptor >= 0) |
| 68 | ::close(FileDescriptor); |
| 69 | } |
| 70 | |
| 71 | int take() { |
| 72 | int ret = FileDescriptor; |
| 73 | FileDescriptor = -1; |
| 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | operator int() const {return FileDescriptor;} |
| 78 | }; |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 79 | |
| 80 | error_code TempDir(SmallVectorImpl<char> &result) { |
| 81 | // FIXME: Don't use TMPDIR if program is SUID or SGID enabled. |
| 82 | const char *dir = 0; |
| 83 | (dir = std::getenv("TMPDIR" )) || |
| 84 | (dir = std::getenv("TMP" )) || |
| 85 | (dir = std::getenv("TEMP" )) || |
| 86 | (dir = std::getenv("TEMPDIR")) || |
| 87 | #ifdef P_tmpdir |
| 88 | (dir = P_tmpdir) || |
| 89 | #endif |
| 90 | (dir = "/tmp"); |
| 91 | |
Michael J. Spencer | fbd1bbd | 2010-12-07 01:23:19 +0000 | [diff] [blame] | 92 | result.clear(); |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 93 | StringRef d(dir); |
| 94 | result.append(d.begin(), d.end()); |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 95 | return error_code::success(); |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 96 | } |
Michael J. Spencer | bee0c38 | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 97 | } |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 98 | |
| 99 | namespace llvm { |
| 100 | namespace sys { |
Michael J. Spencer | 1522fce | 2010-12-07 01:22:31 +0000 | [diff] [blame] | 101 | namespace fs { |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 102 | |
| 103 | error_code current_path(SmallVectorImpl<char> &result) { |
Sylvestre Ledru | 6fc30c2 | 2012-04-11 15:35:36 +0000 | [diff] [blame] | 104 | #ifdef MAXPATHLEN |
Andrew Trick | 0a828fd | 2011-03-24 16:43:37 +0000 | [diff] [blame] | 105 | result.reserve(MAXPATHLEN); |
Sylvestre Ledru | 6fc30c2 | 2012-04-11 15:35:36 +0000 | [diff] [blame] | 106 | #else |
| 107 | // For GNU Hurd |
| 108 | result.reserve(1024); |
| 109 | #endif |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 110 | |
Michael J. Spencer | 1522fce | 2010-12-07 01:22:31 +0000 | [diff] [blame] | 111 | while (true) { |
| 112 | if (::getcwd(result.data(), result.capacity()) == 0) { |
| 113 | // See if there was a real error. |
| 114 | if (errno != errc::not_enough_memory) |
| 115 | return error_code(errno, system_category()); |
| 116 | // Otherwise there just wasn't enough space. |
| 117 | result.reserve(result.capacity() * 2); |
| 118 | } else |
| 119 | break; |
| 120 | } |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 121 | |
| 122 | result.set_size(strlen(result.data())); |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 123 | return error_code::success(); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Michael J. Spencer | bee0c38 | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 126 | error_code copy_file(const Twine &from, const Twine &to, copy_option copt) { |
| 127 | // Get arguments. |
| 128 | SmallString<128> from_storage; |
| 129 | SmallString<128> to_storage; |
Michael J. Spencer | 871498e | 2010-12-01 20:37:42 +0000 | [diff] [blame] | 130 | StringRef f = from.toNullTerminatedStringRef(from_storage); |
| 131 | StringRef t = to.toNullTerminatedStringRef(to_storage); |
Michael J. Spencer | bee0c38 | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 132 | |
| 133 | const size_t buf_sz = 32768; |
| 134 | char buffer[buf_sz]; |
| 135 | int from_file = -1, to_file = -1; |
| 136 | |
| 137 | // Open from. |
| 138 | if ((from_file = ::open(f.begin(), O_RDONLY)) < 0) |
| 139 | return error_code(errno, system_category()); |
| 140 | AutoFD from_fd(from_file); |
| 141 | |
| 142 | // Stat from. |
| 143 | struct stat from_stat; |
| 144 | if (::stat(f.begin(), &from_stat) != 0) |
| 145 | return error_code(errno, system_category()); |
| 146 | |
| 147 | // Setup to flags. |
| 148 | int to_flags = O_CREAT | O_WRONLY; |
| 149 | if (copt == copy_option::fail_if_exists) |
| 150 | to_flags |= O_EXCL; |
| 151 | |
| 152 | // Open to. |
| 153 | if ((to_file = ::open(t.begin(), to_flags, from_stat.st_mode)) < 0) |
| 154 | return error_code(errno, system_category()); |
| 155 | AutoFD to_fd(to_file); |
| 156 | |
| 157 | // Copy! |
| 158 | ssize_t sz, sz_read = 1, sz_write; |
| 159 | while (sz_read > 0 && |
| 160 | (sz_read = ::read(from_fd, buffer, buf_sz)) > 0) { |
| 161 | // Allow for partial writes - see Advanced Unix Programming (2nd Ed.), |
| 162 | // Marc Rochkind, Addison-Wesley, 2004, page 94 |
| 163 | sz_write = 0; |
| 164 | do { |
| 165 | if ((sz = ::write(to_fd, buffer + sz_write, sz_read - sz_write)) < 0) { |
| 166 | sz_read = sz; // cause read loop termination. |
| 167 | break; // error. |
| 168 | } |
| 169 | sz_write += sz; |
| 170 | } while (sz_write < sz_read); |
| 171 | } |
| 172 | |
| 173 | // After all the file operations above the return value of close actually |
| 174 | // matters. |
| 175 | if (::close(from_fd.take()) < 0) sz_read = -1; |
| 176 | if (::close(to_fd.take()) < 0) sz_read = -1; |
| 177 | |
| 178 | // Check for errors. |
| 179 | if (sz_read < 0) |
| 180 | return error_code(errno, system_category()); |
| 181 | |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 182 | return error_code::success(); |
Michael J. Spencer | bee0c38 | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Michael J. Spencer | b83769f | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 185 | error_code create_directory(const Twine &path, bool &existed) { |
| 186 | SmallString<128> path_storage; |
| 187 | StringRef p = path.toNullTerminatedStringRef(path_storage); |
| 188 | |
Michael J. Spencer | 79c3c3a | 2010-12-07 01:23:29 +0000 | [diff] [blame] | 189 | if (::mkdir(p.begin(), S_IRWXU | S_IRWXG) == -1) { |
Michael J. Spencer | 9d425e7 | 2010-12-04 18:45:32 +0000 | [diff] [blame] | 190 | if (errno != errc::file_exists) |
Michael J. Spencer | b83769f | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 191 | return error_code(errno, system_category()); |
| 192 | existed = true; |
| 193 | } else |
| 194 | existed = false; |
| 195 | |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 196 | return error_code::success(); |
Michael J. Spencer | b83769f | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Michael J. Spencer | d7b305f | 2010-12-03 05:58:41 +0000 | [diff] [blame] | 199 | error_code create_hard_link(const Twine &to, const Twine &from) { |
| 200 | // Get arguments. |
| 201 | SmallString<128> from_storage; |
| 202 | SmallString<128> to_storage; |
| 203 | StringRef f = from.toNullTerminatedStringRef(from_storage); |
| 204 | StringRef t = to.toNullTerminatedStringRef(to_storage); |
| 205 | |
| 206 | if (::link(t.begin(), f.begin()) == -1) |
| 207 | return error_code(errno, system_category()); |
| 208 | |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 209 | return error_code::success(); |
Michael J. Spencer | d7b305f | 2010-12-03 05:58:41 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Michael J. Spencer | 9b391c5 | 2010-12-03 07:41:25 +0000 | [diff] [blame] | 212 | error_code create_symlink(const Twine &to, const Twine &from) { |
| 213 | // Get arguments. |
| 214 | SmallString<128> from_storage; |
| 215 | SmallString<128> to_storage; |
| 216 | StringRef f = from.toNullTerminatedStringRef(from_storage); |
| 217 | StringRef t = to.toNullTerminatedStringRef(to_storage); |
| 218 | |
| 219 | if (::symlink(t.begin(), f.begin()) == -1) |
| 220 | return error_code(errno, system_category()); |
| 221 | |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 222 | return error_code::success(); |
Michael J. Spencer | 9b391c5 | 2010-12-03 07:41:25 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Michael J. Spencer | 106aa73 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 225 | error_code remove(const Twine &path, bool &existed) { |
| 226 | SmallString<128> path_storage; |
| 227 | StringRef p = path.toNullTerminatedStringRef(path_storage); |
| 228 | |
| 229 | if (::remove(p.begin()) == -1) { |
Michael J. Spencer | 9d425e7 | 2010-12-04 18:45:32 +0000 | [diff] [blame] | 230 | if (errno != errc::no_such_file_or_directory) |
Michael J. Spencer | 106aa73 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 231 | return error_code(errno, system_category()); |
| 232 | existed = false; |
| 233 | } else |
| 234 | existed = true; |
| 235 | |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 236 | return error_code::success(); |
Michael J. Spencer | 106aa73 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Michael J. Spencer | a50b98c | 2010-12-03 17:53:55 +0000 | [diff] [blame] | 239 | error_code rename(const Twine &from, const Twine &to) { |
| 240 | // Get arguments. |
| 241 | SmallString<128> from_storage; |
| 242 | SmallString<128> to_storage; |
| 243 | StringRef f = from.toNullTerminatedStringRef(from_storage); |
| 244 | StringRef t = to.toNullTerminatedStringRef(to_storage); |
| 245 | |
Michael J. Spencer | 283d49c | 2011-01-16 22:18:41 +0000 | [diff] [blame] | 246 | if (::rename(f.begin(), t.begin()) == -1) { |
| 247 | // If it's a cross device link, copy then delete, otherwise return the error |
| 248 | if (errno == EXDEV) { |
| 249 | if (error_code ec = copy_file(from, to, copy_option::overwrite_if_exists)) |
| 250 | return ec; |
| 251 | bool Existed; |
| 252 | if (error_code ec = remove(from, Existed)) |
| 253 | return ec; |
| 254 | } else |
| 255 | return error_code(errno, system_category()); |
| 256 | } |
Michael J. Spencer | a50b98c | 2010-12-03 17:53:55 +0000 | [diff] [blame] | 257 | |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 258 | return error_code::success(); |
Michael J. Spencer | a50b98c | 2010-12-03 17:53:55 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Michael J. Spencer | 3920d3b | 2010-12-03 17:54:07 +0000 | [diff] [blame] | 261 | error_code resize_file(const Twine &path, uint64_t size) { |
| 262 | SmallString<128> path_storage; |
| 263 | StringRef p = path.toNullTerminatedStringRef(path_storage); |
| 264 | |
| 265 | if (::truncate(p.begin(), size) == -1) |
| 266 | return error_code(errno, system_category()); |
| 267 | |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 268 | return error_code::success(); |
Michael J. Spencer | 3920d3b | 2010-12-03 17:54:07 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 271 | error_code exists(const Twine &path, bool &result) { |
| 272 | SmallString<128> path_storage; |
| 273 | StringRef p = path.toNullTerminatedStringRef(path_storage); |
| 274 | |
| 275 | struct stat status; |
| 276 | if (::stat(p.begin(), &status) == -1) { |
Michael J. Spencer | 9d425e7 | 2010-12-04 18:45:32 +0000 | [diff] [blame] | 277 | if (errno != errc::no_such_file_or_directory) |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 278 | return error_code(errno, system_category()); |
| 279 | result = false; |
| 280 | } else |
| 281 | result = true; |
| 282 | |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 283 | return error_code::success(); |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Michael J. Spencer | d45fbe6 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 286 | bool equivalent(file_status A, file_status B) { |
| 287 | assert(status_known(A) && status_known(B)); |
Sylvestre Ledru | 9dc06bd | 2012-04-23 16:37:23 +0000 | [diff] [blame^] | 288 | return A.fs_st_dev == B.fs_st_dev && |
| 289 | A.fs_st_ino == B.fs_st_ino; |
Michael J. Spencer | d45fbe6 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Michael J. Spencer | b531f45 | 2010-12-03 18:49:13 +0000 | [diff] [blame] | 292 | error_code equivalent(const Twine &A, const Twine &B, bool &result) { |
Michael J. Spencer | d45fbe6 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 293 | file_status fsA, fsB; |
| 294 | if (error_code ec = status(A, fsA)) return ec; |
| 295 | if (error_code ec = status(B, fsB)) return ec; |
| 296 | result = equivalent(fsA, fsB); |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 297 | return error_code::success(); |
Michael J. Spencer | b531f45 | 2010-12-03 18:49:13 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Michael J. Spencer | 01a87c4 | 2010-12-04 00:31:48 +0000 | [diff] [blame] | 300 | error_code file_size(const Twine &path, uint64_t &result) { |
| 301 | SmallString<128> path_storage; |
| 302 | StringRef p = path.toNullTerminatedStringRef(path_storage); |
| 303 | |
| 304 | struct stat status; |
| 305 | if (::stat(p.begin(), &status) == -1) |
| 306 | return error_code(errno, system_category()); |
| 307 | if (!S_ISREG(status.st_mode)) |
Michael J. Spencer | 9d425e7 | 2010-12-04 18:45:32 +0000 | [diff] [blame] | 308 | return make_error_code(errc::operation_not_permitted); |
Michael J. Spencer | 01a87c4 | 2010-12-04 00:31:48 +0000 | [diff] [blame] | 309 | |
| 310 | result = status.st_size; |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 311 | return error_code::success(); |
Michael J. Spencer | 01a87c4 | 2010-12-04 00:31:48 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Michael J. Spencer | 470ae13 | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 314 | error_code status(const Twine &path, file_status &result) { |
| 315 | SmallString<128> path_storage; |
| 316 | StringRef p = path.toNullTerminatedStringRef(path_storage); |
| 317 | |
| 318 | struct stat status; |
| 319 | if (::stat(p.begin(), &status) != 0) { |
| 320 | error_code ec(errno, system_category()); |
| 321 | if (ec == errc::no_such_file_or_directory) |
| 322 | result = file_status(file_type::file_not_found); |
| 323 | else |
| 324 | result = file_status(file_type::status_error); |
| 325 | return ec; |
| 326 | } |
| 327 | |
| 328 | if (S_ISDIR(status.st_mode)) |
| 329 | result = file_status(file_type::directory_file); |
| 330 | else if (S_ISREG(status.st_mode)) |
| 331 | result = file_status(file_type::regular_file); |
| 332 | else if (S_ISBLK(status.st_mode)) |
| 333 | result = file_status(file_type::block_file); |
| 334 | else if (S_ISCHR(status.st_mode)) |
| 335 | result = file_status(file_type::character_file); |
| 336 | else if (S_ISFIFO(status.st_mode)) |
| 337 | result = file_status(file_type::fifo_file); |
| 338 | else if (S_ISSOCK(status.st_mode)) |
| 339 | result = file_status(file_type::socket_file); |
| 340 | else |
| 341 | result = file_status(file_type::type_unknown); |
| 342 | |
Sylvestre Ledru | 9dc06bd | 2012-04-23 16:37:23 +0000 | [diff] [blame^] | 343 | result.fs_st_dev = status.st_dev; |
| 344 | result.fs_st_ino = status.st_ino; |
Michael J. Spencer | d45fbe6 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 345 | |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 346 | return error_code::success(); |
Michael J. Spencer | 470ae13 | 2010-12-04 00:32:40 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 349 | error_code unique_file(const Twine &model, int &result_fd, |
Argyrios Kyrtzidis | 814450a | 2011-07-28 00:29:20 +0000 | [diff] [blame] | 350 | SmallVectorImpl<char> &result_path, |
| 351 | bool makeAbsolute) { |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 352 | SmallString<128> Model; |
| 353 | model.toVector(Model); |
| 354 | // Null terminate. |
| 355 | Model.c_str(); |
| 356 | |
Argyrios Kyrtzidis | 814450a | 2011-07-28 00:29:20 +0000 | [diff] [blame] | 357 | if (makeAbsolute) { |
| 358 | // Make model absolute by prepending a temp directory if it's not already. |
| 359 | bool absolute = path::is_absolute(Twine(Model)); |
| 360 | if (!absolute) { |
| 361 | SmallString<128> TDir; |
| 362 | if (error_code ec = TempDir(TDir)) return ec; |
| 363 | path::append(TDir, Twine(Model)); |
| 364 | Model.swap(TDir); |
| 365 | } |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | // Replace '%' with random chars. From here on, DO NOT modify model. It may be |
| 369 | // needed if the randomly chosen path already exists. |
| 370 | SmallString<128> RandomPath; |
| 371 | RandomPath.reserve(Model.size() + 1); |
| 372 | ::srand(::time(NULL)); |
| 373 | |
| 374 | retry_random_path: |
| 375 | // This is opened here instead of above to make it easier to track when to |
| 376 | // close it. Collisions should be rare enough for the possible extra syscalls |
| 377 | // not to matter. |
| 378 | FILE *RandomSource = ::fopen("/dev/urandom", "r"); |
| 379 | RandomPath.set_size(0); |
| 380 | for (SmallVectorImpl<char>::const_iterator i = Model.begin(), |
| 381 | e = Model.end(); i != e; ++i) { |
| 382 | if (*i == '%') { |
| 383 | char val = 0; |
| 384 | if (RandomSource) |
| 385 | val = fgetc(RandomSource); |
| 386 | else |
| 387 | val = ::rand(); |
| 388 | RandomPath.push_back("0123456789abcdef"[val & 15]); |
| 389 | } else |
| 390 | RandomPath.push_back(*i); |
| 391 | } |
| 392 | |
| 393 | if (RandomSource) |
| 394 | ::fclose(RandomSource); |
| 395 | |
| 396 | // Try to open + create the file. |
| 397 | rety_open_create: |
| 398 | int RandomFD = ::open(RandomPath.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600); |
| 399 | if (RandomFD == -1) { |
| 400 | // If the file existed, try again, otherwise, error. |
Michael J. Spencer | 9d425e7 | 2010-12-04 18:45:32 +0000 | [diff] [blame] | 401 | if (errno == errc::file_exists) |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 402 | goto retry_random_path; |
| 403 | // The path prefix doesn't exist. |
Michael J. Spencer | 9d425e7 | 2010-12-04 18:45:32 +0000 | [diff] [blame] | 404 | if (errno == errc::no_such_file_or_directory) { |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 405 | StringRef p(RandomPath.begin(), RandomPath.size()); |
| 406 | SmallString<64> dir_to_create; |
| 407 | for (path::const_iterator i = path::begin(p), |
| 408 | e = --path::end(p); i != e; ++i) { |
Michael J. Spencer | 936671b | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 409 | path::append(dir_to_create, *i); |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 410 | bool Exists; |
| 411 | if (error_code ec = exists(Twine(dir_to_create), Exists)) return ec; |
| 412 | if (!Exists) { |
| 413 | // Don't try to create network paths. |
| 414 | if (i->size() > 2 && (*i)[0] == '/' && |
| 415 | (*i)[1] == '/' && |
| 416 | (*i)[2] != '/') |
Michael J. Spencer | 9d425e7 | 2010-12-04 18:45:32 +0000 | [diff] [blame] | 417 | return make_error_code(errc::no_such_file_or_directory); |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 418 | if (::mkdir(dir_to_create.c_str(), 0700) == -1) |
| 419 | return error_code(errno, system_category()); |
| 420 | } |
| 421 | } |
| 422 | goto rety_open_create; |
| 423 | } |
| 424 | return error_code(errno, system_category()); |
| 425 | } |
| 426 | |
Michael J. Spencer | 9d425e7 | 2010-12-04 18:45:32 +0000 | [diff] [blame] | 427 | // Make the path absolute. |
Andrew Trick | 0a828fd | 2011-03-24 16:43:37 +0000 | [diff] [blame] | 428 | char real_path_buff[PATH_MAX + 1]; |
| 429 | if (realpath(RandomPath.c_str(), real_path_buff) == NULL) { |
| 430 | int error = errno; |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 431 | ::close(RandomFD); |
| 432 | ::unlink(RandomPath.c_str()); |
Andrew Trick | 0a828fd | 2011-03-24 16:43:37 +0000 | [diff] [blame] | 433 | return error_code(error, system_category()); |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Andrew Trick | 0a828fd | 2011-03-24 16:43:37 +0000 | [diff] [blame] | 436 | result_path.clear(); |
| 437 | StringRef d(real_path_buff); |
| 438 | result_path.append(d.begin(), d.end()); |
| 439 | |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 440 | result_fd = RandomFD; |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 441 | return error_code::success(); |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Michael J. Spencer | a81ac8f | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 444 | error_code detail::directory_iterator_construct(detail::DirIterState &it, |
| 445 | StringRef path){ |
Michael J. Spencer | da7c1ca | 2011-01-05 16:38:57 +0000 | [diff] [blame] | 446 | SmallString<128> path_null(path); |
| 447 | DIR *directory = ::opendir(path_null.c_str()); |
| 448 | if (directory == 0) |
| 449 | return error_code(errno, system_category()); |
| 450 | |
| 451 | it.IterationHandle = reinterpret_cast<intptr_t>(directory); |
| 452 | // Add something for replace_filename to replace. |
| 453 | path::append(path_null, "."); |
| 454 | it.CurrentEntry = directory_entry(path_null.str()); |
| 455 | return directory_iterator_increment(it); |
| 456 | } |
| 457 | |
Michael J. Spencer | a81ac8f | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 458 | error_code detail::directory_iterator_destruct(detail::DirIterState &it) { |
Michael J. Spencer | da7c1ca | 2011-01-05 16:38:57 +0000 | [diff] [blame] | 459 | if (it.IterationHandle) |
| 460 | ::closedir(reinterpret_cast<DIR *>(it.IterationHandle)); |
| 461 | it.IterationHandle = 0; |
| 462 | it.CurrentEntry = directory_entry(); |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 463 | return error_code::success(); |
Michael J. Spencer | da7c1ca | 2011-01-05 16:38:57 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Michael J. Spencer | a81ac8f | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 466 | error_code detail::directory_iterator_increment(detail::DirIterState &it) { |
Michael J. Spencer | da7c1ca | 2011-01-05 16:38:57 +0000 | [diff] [blame] | 467 | errno = 0; |
| 468 | dirent *cur_dir = ::readdir(reinterpret_cast<DIR *>(it.IterationHandle)); |
| 469 | if (cur_dir == 0 && errno != 0) { |
| 470 | return error_code(errno, system_category()); |
| 471 | } else if (cur_dir != 0) { |
| 472 | StringRef name(cur_dir->d_name, NAMLEN(cur_dir)); |
| 473 | if ((name.size() == 1 && name[0] == '.') || |
| 474 | (name.size() == 2 && name[0] == '.' && name[1] == '.')) |
| 475 | return directory_iterator_increment(it); |
| 476 | it.CurrentEntry.replace_filename(name); |
| 477 | } else |
| 478 | return directory_iterator_destruct(it); |
| 479 | |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 480 | return error_code::success(); |
Michael J. Spencer | da7c1ca | 2011-01-05 16:38:57 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Michael J. Spencer | d6cdf1d | 2011-01-15 18:52:33 +0000 | [diff] [blame] | 483 | error_code get_magic(const Twine &path, uint32_t len, |
| 484 | SmallVectorImpl<char> &result) { |
| 485 | SmallString<128> PathStorage; |
| 486 | StringRef Path = path.toNullTerminatedStringRef(PathStorage); |
| 487 | result.set_size(0); |
| 488 | |
| 489 | // Open path. |
| 490 | std::FILE *file = std::fopen(Path.data(), "rb"); |
| 491 | if (file == 0) |
| 492 | return error_code(errno, system_category()); |
| 493 | |
| 494 | // Reserve storage. |
| 495 | result.reserve(len); |
| 496 | |
| 497 | // Read magic! |
| 498 | size_t size = std::fread(result.data(), 1, len, file); |
| 499 | if (std::ferror(file) != 0) { |
| 500 | std::fclose(file); |
| 501 | return error_code(errno, system_category()); |
| 502 | } else if (size != result.size()) { |
| 503 | if (std::feof(file) != 0) { |
| 504 | std::fclose(file); |
| 505 | result.set_size(size); |
| 506 | return make_error_code(errc::value_too_large); |
| 507 | } |
| 508 | } |
| 509 | std::fclose(file); |
| 510 | result.set_size(len); |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 511 | return error_code::success(); |
Michael J. Spencer | d6cdf1d | 2011-01-15 18:52:33 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Michael J. Spencer | bee0c38 | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 514 | } // end namespace fs |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 515 | } // end namespace sys |
| 516 | } // end namespace llvm |