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