1 //===- llvm/Support/Unix/PathV2.cpp - Unix Path Implementation --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Unix specific implementation of the PathV2 API.
12 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
15 //=== WARNING: Implementation here must contain only generic UNIX code that
16 //=== is guaranteed to work on *all* UNIX variants.
17 //===----------------------------------------------------------------------===//
33 /// This class automatically closes the given file descriptor when it goes out
34 /// of scope. You can take back explicit ownership of the file descriptor by
35 /// calling take(). The destructor does not verify that close was successful.
36 /// Therefore, never allow this class to call close on a file descriptor that
37 /// has been read from or written to.
41 AutoFD(int fd) : FileDescriptor(fd) {}
43 if (FileDescriptor >= 0)
44 ::close(FileDescriptor);
48 int ret = FileDescriptor;
53 operator int() const {return FileDescriptor;}
56 error_code TempDir(SmallVectorImpl<char> &result) {
57 // FIXME: Don't use TMPDIR if program is SUID or SGID enabled.
59 (dir = std::getenv("TMPDIR" )) ||
60 (dir = std::getenv("TMP" )) ||
61 (dir = std::getenv("TEMP" )) ||
62 (dir = std::getenv("TEMPDIR")) ||
70 result.append(d.begin(), d.end());
79 error_code current_path(SmallVectorImpl<char> &result) {
80 result.reserve(MAXPATHLEN);
83 if (::getcwd(result.data(), result.capacity()) == 0) {
84 // See if there was a real error.
85 if (errno != errc::not_enough_memory)
86 return error_code(errno, system_category());
87 // Otherwise there just wasn't enough space.
88 result.reserve(result.capacity() * 2);
93 result.set_size(strlen(result.data()));
97 error_code copy_file(const Twine &from, const Twine &to, copy_option copt) {
99 SmallString<128> from_storage;
100 SmallString<128> to_storage;
101 StringRef f = from.toNullTerminatedStringRef(from_storage);
102 StringRef t = to.toNullTerminatedStringRef(to_storage);
104 const size_t buf_sz = 32768;
106 int from_file = -1, to_file = -1;
109 if ((from_file = ::open(f.begin(), O_RDONLY)) < 0)
110 return error_code(errno, system_category());
111 AutoFD from_fd(from_file);
114 struct stat from_stat;
115 if (::stat(f.begin(), &from_stat) != 0)
116 return error_code(errno, system_category());
119 int to_flags = O_CREAT | O_WRONLY;
120 if (copt == copy_option::fail_if_exists)
124 if ((to_file = ::open(t.begin(), to_flags, from_stat.st_mode)) < 0)
125 return error_code(errno, system_category());
126 AutoFD to_fd(to_file);
129 ssize_t sz, sz_read = 1, sz_write;
130 while (sz_read > 0 &&
131 (sz_read = ::read(from_fd, buffer, buf_sz)) > 0) {
132 // Allow for partial writes - see Advanced Unix Programming (2nd Ed.),
133 // Marc Rochkind, Addison-Wesley, 2004, page 94
136 if ((sz = ::write(to_fd, buffer + sz_write, sz_read - sz_write)) < 0) {
137 sz_read = sz; // cause read loop termination.
141 } while (sz_write < sz_read);
144 // After all the file operations above the return value of close actually
146 if (::close(from_fd.take()) < 0) sz_read = -1;
147 if (::close(to_fd.take()) < 0) sz_read = -1;
151 return error_code(errno, system_category());
156 error_code create_directory(const Twine &path, bool &existed) {
157 SmallString<128> path_storage;
158 StringRef p = path.toNullTerminatedStringRef(path_storage);
160 if (::mkdir(p.begin(), S_IRWXU | S_IRWXG) == -1) {
161 if (errno != errc::file_exists)
162 return error_code(errno, system_category());
170 error_code create_hard_link(const Twine &to, const Twine &from) {
172 SmallString<128> from_storage;
173 SmallString<128> to_storage;
174 StringRef f = from.toNullTerminatedStringRef(from_storage);
175 StringRef t = to.toNullTerminatedStringRef(to_storage);
177 if (::link(t.begin(), f.begin()) == -1)
178 return error_code(errno, system_category());
183 error_code create_symlink(const Twine &to, const Twine &from) {
185 SmallString<128> from_storage;
186 SmallString<128> to_storage;
187 StringRef f = from.toNullTerminatedStringRef(from_storage);
188 StringRef t = to.toNullTerminatedStringRef(to_storage);
190 if (::symlink(t.begin(), f.begin()) == -1)
191 return error_code(errno, system_category());
196 error_code remove(const Twine &path, bool &existed) {
197 SmallString<128> path_storage;
198 StringRef p = path.toNullTerminatedStringRef(path_storage);
200 if (::remove(p.begin()) == -1) {
201 if (errno != errc::no_such_file_or_directory)
202 return error_code(errno, system_category());
210 error_code rename(const Twine &from, const Twine &to) {
212 SmallString<128> from_storage;
213 SmallString<128> to_storage;
214 StringRef f = from.toNullTerminatedStringRef(from_storage);
215 StringRef t = to.toNullTerminatedStringRef(to_storage);
217 if (::rename(f.begin(), t.begin()) == -1)
218 return error_code(errno, system_category());
223 error_code resize_file(const Twine &path, uint64_t size) {
224 SmallString<128> path_storage;
225 StringRef p = path.toNullTerminatedStringRef(path_storage);
227 if (::truncate(p.begin(), size) == -1)
228 return error_code(errno, system_category());
233 error_code exists(const Twine &path, bool &result) {
234 SmallString<128> path_storage;
235 StringRef p = path.toNullTerminatedStringRef(path_storage);
238 if (::stat(p.begin(), &status) == -1) {
239 if (errno != errc::no_such_file_or_directory)
240 return error_code(errno, system_category());
248 error_code equivalent(const Twine &A, const Twine &B, bool &result) {
250 SmallString<128> a_storage;
251 SmallString<128> b_storage;
252 StringRef a = A.toNullTerminatedStringRef(a_storage);
253 StringRef b = B.toNullTerminatedStringRef(b_storage);
255 struct stat stat_a, stat_b;
256 int error_b = ::stat(b.begin(), &stat_b);
257 int error_a = ::stat(a.begin(), &stat_a);
259 // If both are invalid, it's an error. If only one is, the result is false.
260 if (error_a != 0 || error_b != 0) {
261 if (error_a == error_b)
262 return error_code(errno, system_category());
266 stat_a.st_dev == stat_b.st_dev &&
267 stat_a.st_ino == stat_b.st_ino;
273 error_code file_size(const Twine &path, uint64_t &result) {
274 SmallString<128> path_storage;
275 StringRef p = path.toNullTerminatedStringRef(path_storage);
278 if (::stat(p.begin(), &status) == -1)
279 return error_code(errno, system_category());
280 if (!S_ISREG(status.st_mode))
281 return make_error_code(errc::operation_not_permitted);
283 result = status.st_size;
287 error_code status(const Twine &path, file_status &result) {
288 SmallString<128> path_storage;
289 StringRef p = path.toNullTerminatedStringRef(path_storage);
292 if (::stat(p.begin(), &status) != 0) {
293 error_code ec(errno, system_category());
294 if (ec == errc::no_such_file_or_directory)
295 result = file_status(file_type::file_not_found);
297 result = file_status(file_type::status_error);
301 if (S_ISDIR(status.st_mode))
302 result = file_status(file_type::directory_file);
303 else if (S_ISREG(status.st_mode))
304 result = file_status(file_type::regular_file);
305 else if (S_ISBLK(status.st_mode))
306 result = file_status(file_type::block_file);
307 else if (S_ISCHR(status.st_mode))
308 result = file_status(file_type::character_file);
309 else if (S_ISFIFO(status.st_mode))
310 result = file_status(file_type::fifo_file);
311 else if (S_ISSOCK(status.st_mode))
312 result = file_status(file_type::socket_file);
314 result = file_status(file_type::type_unknown);
319 error_code unique_file(const Twine &model, int &result_fd,
320 SmallVectorImpl<char> &result_path) {
321 SmallString<128> Model;
322 model.toVector(Model);
326 // Make model absolute by prepending a temp directory if it's not already.
327 bool absolute = path::is_absolute(Twine(Model));
329 SmallString<128> TDir;
330 if (error_code ec = TempDir(TDir)) return ec;
331 path::append(TDir, Twine(Model));
335 // Replace '%' with random chars. From here on, DO NOT modify model. It may be
336 // needed if the randomly chosen path already exists.
337 SmallString<128> RandomPath;
338 RandomPath.reserve(Model.size() + 1);
339 ::srand(::time(NULL));
342 // This is opened here instead of above to make it easier to track when to
343 // close it. Collisions should be rare enough for the possible extra syscalls
345 FILE *RandomSource = ::fopen("/dev/urandom", "r");
346 RandomPath.set_size(0);
347 for (SmallVectorImpl<char>::const_iterator i = Model.begin(),
348 e = Model.end(); i != e; ++i) {
352 val = fgetc(RandomSource);
355 RandomPath.push_back("0123456789abcdef"[val & 15]);
357 RandomPath.push_back(*i);
361 ::fclose(RandomSource);
363 // Try to open + create the file.
365 int RandomFD = ::open(RandomPath.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600);
366 if (RandomFD == -1) {
367 // If the file existed, try again, otherwise, error.
368 if (errno == errc::file_exists)
369 goto retry_random_path;
370 // The path prefix doesn't exist.
371 if (errno == errc::no_such_file_or_directory) {
372 StringRef p(RandomPath.begin(), RandomPath.size());
373 SmallString<64> dir_to_create;
374 for (path::const_iterator i = path::begin(p),
375 e = --path::end(p); i != e; ++i) {
376 path::append(dir_to_create, *i);
378 if (error_code ec = exists(Twine(dir_to_create), Exists)) return ec;
380 // Don't try to create network paths.
381 if (i->size() > 2 && (*i)[0] == '/' &&
384 return make_error_code(errc::no_such_file_or_directory);
385 if (::mkdir(dir_to_create.c_str(), 0700) == -1)
386 return error_code(errno, system_category());
389 goto rety_open_create;
391 return error_code(errno, system_category());
394 // Make the path absolute.
395 char real_path_buff[PATH_MAX + 1];
396 if (realpath(RandomPath.c_str(), real_path_buff) == NULL) {
399 ::unlink(RandomPath.c_str());
400 return error_code(error, system_category());
404 StringRef d(real_path_buff);
405 result_path.append(d.begin(), d.end());
407 result_fd = RandomFD;
411 } // end namespace fs
412 } // end namespace sys
413 } // end namespace llvm