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 | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 26 | #if HAVE_STDIO_H |
| 27 | #include <stdio.h> |
Michael J. Spencer | bee0c38 | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 28 | #endif |
| 29 | |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
Michael J. Spencer | bee0c38 | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 32 | namespace { |
| 33 | struct AutoFD { |
| 34 | int FileDescriptor; |
| 35 | |
| 36 | AutoFD(int fd) : FileDescriptor(fd) {} |
| 37 | ~AutoFD() { |
| 38 | if (FileDescriptor >= 0) |
| 39 | ::close(FileDescriptor); |
| 40 | } |
| 41 | |
| 42 | int take() { |
| 43 | int ret = FileDescriptor; |
| 44 | FileDescriptor = -1; |
| 45 | return ret; |
| 46 | } |
| 47 | |
| 48 | operator int() const {return FileDescriptor;} |
| 49 | }; |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 50 | |
| 51 | error_code TempDir(SmallVectorImpl<char> &result) { |
| 52 | // FIXME: Don't use TMPDIR if program is SUID or SGID enabled. |
| 53 | const char *dir = 0; |
| 54 | (dir = std::getenv("TMPDIR" )) || |
| 55 | (dir = std::getenv("TMP" )) || |
| 56 | (dir = std::getenv("TEMP" )) || |
| 57 | (dir = std::getenv("TEMPDIR")) || |
| 58 | #ifdef P_tmpdir |
| 59 | (dir = P_tmpdir) || |
| 60 | #endif |
| 61 | (dir = "/tmp"); |
| 62 | |
| 63 | result.set_size(0); |
| 64 | StringRef d(dir); |
| 65 | result.append(d.begin(), d.end()); |
| 66 | return make_error_code(errc::success); |
| 67 | } |
Michael J. Spencer | bee0c38 | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 68 | } |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 69 | |
| 70 | namespace llvm { |
| 71 | namespace sys { |
| 72 | namespace path { |
| 73 | |
| 74 | error_code current_path(SmallVectorImpl<char> &result) { |
| 75 | long size = ::pathconf(".", _PC_PATH_MAX); |
| 76 | result.reserve(size + 1); |
| 77 | result.set_size(size + 1); |
| 78 | |
| 79 | if (::getcwd(result.data(), result.size()) == 0) |
| 80 | return error_code(errno, system_category()); |
| 81 | |
| 82 | result.set_size(strlen(result.data())); |
| 83 | return make_error_code(errc::success); |
| 84 | } |
| 85 | |
| 86 | } // end namespace path |
Michael J. Spencer | bee0c38 | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 87 | |
| 88 | namespace fs{ |
| 89 | |
| 90 | error_code copy_file(const Twine &from, const Twine &to, copy_option copt) { |
| 91 | // Get arguments. |
| 92 | SmallString<128> from_storage; |
| 93 | SmallString<128> to_storage; |
Michael J. Spencer | 871498e | 2010-12-01 20:37:42 +0000 | [diff] [blame] | 94 | StringRef f = from.toNullTerminatedStringRef(from_storage); |
| 95 | StringRef t = to.toNullTerminatedStringRef(to_storage); |
Michael J. Spencer | bee0c38 | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 96 | |
| 97 | const size_t buf_sz = 32768; |
| 98 | char buffer[buf_sz]; |
| 99 | int from_file = -1, to_file = -1; |
| 100 | |
| 101 | // Open from. |
| 102 | if ((from_file = ::open(f.begin(), O_RDONLY)) < 0) |
| 103 | return error_code(errno, system_category()); |
| 104 | AutoFD from_fd(from_file); |
| 105 | |
| 106 | // Stat from. |
| 107 | struct stat from_stat; |
| 108 | if (::stat(f.begin(), &from_stat) != 0) |
| 109 | return error_code(errno, system_category()); |
| 110 | |
| 111 | // Setup to flags. |
| 112 | int to_flags = O_CREAT | O_WRONLY; |
| 113 | if (copt == copy_option::fail_if_exists) |
| 114 | to_flags |= O_EXCL; |
| 115 | |
| 116 | // Open to. |
| 117 | if ((to_file = ::open(t.begin(), to_flags, from_stat.st_mode)) < 0) |
| 118 | return error_code(errno, system_category()); |
| 119 | AutoFD to_fd(to_file); |
| 120 | |
| 121 | // Copy! |
| 122 | ssize_t sz, sz_read = 1, sz_write; |
| 123 | while (sz_read > 0 && |
| 124 | (sz_read = ::read(from_fd, buffer, buf_sz)) > 0) { |
| 125 | // Allow for partial writes - see Advanced Unix Programming (2nd Ed.), |
| 126 | // Marc Rochkind, Addison-Wesley, 2004, page 94 |
| 127 | sz_write = 0; |
| 128 | do { |
| 129 | if ((sz = ::write(to_fd, buffer + sz_write, sz_read - sz_write)) < 0) { |
| 130 | sz_read = sz; // cause read loop termination. |
| 131 | break; // error. |
| 132 | } |
| 133 | sz_write += sz; |
| 134 | } while (sz_write < sz_read); |
| 135 | } |
| 136 | |
| 137 | // After all the file operations above the return value of close actually |
| 138 | // matters. |
| 139 | if (::close(from_fd.take()) < 0) sz_read = -1; |
| 140 | if (::close(to_fd.take()) < 0) sz_read = -1; |
| 141 | |
| 142 | // Check for errors. |
| 143 | if (sz_read < 0) |
| 144 | return error_code(errno, system_category()); |
| 145 | |
| 146 | return make_error_code(errc::success); |
| 147 | } |
| 148 | |
Michael J. Spencer | b83769f | 2010-12-03 05:42:11 +0000 | [diff] [blame] | 149 | error_code create_directory(const Twine &path, bool &existed) { |
| 150 | SmallString<128> path_storage; |
| 151 | StringRef p = path.toNullTerminatedStringRef(path_storage); |
| 152 | |
| 153 | if (::mkdir(p.begin(), 0700) == -1) { |
| 154 | if (errno != EEXIST) |
| 155 | return error_code(errno, system_category()); |
| 156 | existed = true; |
| 157 | } else |
| 158 | existed = false; |
| 159 | |
| 160 | return make_error_code(errc::success); |
| 161 | } |
| 162 | |
Michael J. Spencer | d7b305f | 2010-12-03 05:58:41 +0000 | [diff] [blame] | 163 | error_code create_hard_link(const Twine &to, const Twine &from) { |
| 164 | // Get arguments. |
| 165 | SmallString<128> from_storage; |
| 166 | SmallString<128> to_storage; |
| 167 | StringRef f = from.toNullTerminatedStringRef(from_storage); |
| 168 | StringRef t = to.toNullTerminatedStringRef(to_storage); |
| 169 | |
| 170 | if (::link(t.begin(), f.begin()) == -1) |
| 171 | return error_code(errno, system_category()); |
| 172 | |
| 173 | return make_error_code(errc::success); |
| 174 | } |
| 175 | |
Michael J. Spencer | 9b391c5 | 2010-12-03 07:41:25 +0000 | [diff] [blame^] | 176 | error_code create_symlink(const Twine &to, const Twine &from) { |
| 177 | // Get arguments. |
| 178 | SmallString<128> from_storage; |
| 179 | SmallString<128> to_storage; |
| 180 | StringRef f = from.toNullTerminatedStringRef(from_storage); |
| 181 | StringRef t = to.toNullTerminatedStringRef(to_storage); |
| 182 | |
| 183 | if (::symlink(t.begin(), f.begin()) == -1) |
| 184 | return error_code(errno, system_category()); |
| 185 | |
| 186 | return make_error_code(errc::success); |
| 187 | } |
| 188 | |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 189 | error_code exists(const Twine &path, bool &result) { |
| 190 | SmallString<128> path_storage; |
| 191 | StringRef p = path.toNullTerminatedStringRef(path_storage); |
| 192 | |
| 193 | struct stat status; |
| 194 | if (::stat(p.begin(), &status) == -1) { |
| 195 | if (errno != ENOENT) |
| 196 | return error_code(errno, system_category()); |
| 197 | result = false; |
| 198 | } else |
| 199 | result = true; |
| 200 | |
| 201 | return make_error_code(errc::success); |
| 202 | } |
| 203 | |
| 204 | error_code unique_file(const Twine &model, int &result_fd, |
| 205 | SmallVectorImpl<char> &result_path) { |
| 206 | SmallString<128> Model; |
| 207 | model.toVector(Model); |
| 208 | // Null terminate. |
| 209 | Model.c_str(); |
| 210 | |
| 211 | // Make model absolute by prepending a temp directory if it's not already. |
| 212 | bool absolute; |
| 213 | if (error_code ec = path::is_absolute(Twine(Model), absolute)) return ec; |
| 214 | if (!absolute) { |
| 215 | SmallString<128> TDir; |
| 216 | if (error_code ec = TempDir(TDir)) return ec; |
| 217 | if (error_code ec = path::append(TDir, Twine(Model))) return ec; |
| 218 | Model.swap(TDir); |
| 219 | } |
| 220 | |
| 221 | // Replace '%' with random chars. From here on, DO NOT modify model. It may be |
| 222 | // needed if the randomly chosen path already exists. |
| 223 | SmallString<128> RandomPath; |
| 224 | RandomPath.reserve(Model.size() + 1); |
| 225 | ::srand(::time(NULL)); |
| 226 | |
| 227 | retry_random_path: |
| 228 | // This is opened here instead of above to make it easier to track when to |
| 229 | // close it. Collisions should be rare enough for the possible extra syscalls |
| 230 | // not to matter. |
| 231 | FILE *RandomSource = ::fopen("/dev/urandom", "r"); |
| 232 | RandomPath.set_size(0); |
| 233 | for (SmallVectorImpl<char>::const_iterator i = Model.begin(), |
| 234 | e = Model.end(); i != e; ++i) { |
| 235 | if (*i == '%') { |
| 236 | char val = 0; |
| 237 | if (RandomSource) |
| 238 | val = fgetc(RandomSource); |
| 239 | else |
| 240 | val = ::rand(); |
| 241 | RandomPath.push_back("0123456789abcdef"[val & 15]); |
| 242 | } else |
| 243 | RandomPath.push_back(*i); |
| 244 | } |
| 245 | |
| 246 | if (RandomSource) |
| 247 | ::fclose(RandomSource); |
| 248 | |
| 249 | // Try to open + create the file. |
| 250 | rety_open_create: |
| 251 | int RandomFD = ::open(RandomPath.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600); |
| 252 | if (RandomFD == -1) { |
| 253 | // If the file existed, try again, otherwise, error. |
| 254 | if (errno == EEXIST) |
| 255 | goto retry_random_path; |
| 256 | // The path prefix doesn't exist. |
| 257 | if (errno == ENOENT) { |
| 258 | StringRef p(RandomPath.begin(), RandomPath.size()); |
| 259 | SmallString<64> dir_to_create; |
| 260 | for (path::const_iterator i = path::begin(p), |
| 261 | e = --path::end(p); i != e; ++i) { |
| 262 | if (error_code ec = path::append(dir_to_create, *i)) return ec; |
| 263 | bool Exists; |
| 264 | if (error_code ec = exists(Twine(dir_to_create), Exists)) return ec; |
| 265 | if (!Exists) { |
| 266 | // Don't try to create network paths. |
| 267 | if (i->size() > 2 && (*i)[0] == '/' && |
| 268 | (*i)[1] == '/' && |
| 269 | (*i)[2] != '/') |
| 270 | return error_code(ENOENT, system_category()); |
| 271 | if (::mkdir(dir_to_create.c_str(), 0700) == -1) |
| 272 | return error_code(errno, system_category()); |
| 273 | } |
| 274 | } |
| 275 | goto rety_open_create; |
| 276 | } |
| 277 | return error_code(errno, system_category()); |
| 278 | } |
| 279 | |
| 280 | // Make the path absolute. |
| 281 | char real_path_buff[PATH_MAX + 1]; |
| 282 | if (realpath(RandomPath.c_str(), real_path_buff) == NULL) { |
| 283 | ::close(RandomFD); |
| 284 | ::unlink(RandomPath.c_str()); |
| 285 | return error_code(errno, system_category()); |
| 286 | } |
| 287 | |
| 288 | result_path.set_size(0); |
| 289 | StringRef d(real_path_buff); |
| 290 | result_path.append(d.begin(), d.end()); |
| 291 | |
| 292 | result_fd = RandomFD; |
| 293 | return make_error_code(errc::success); |
| 294 | } |
| 295 | |
Michael J. Spencer | bee0c38 | 2010-12-01 19:32:01 +0000 | [diff] [blame] | 296 | } // end namespace fs |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 297 | } // end namespace sys |
| 298 | } // end namespace llvm |