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 //===----------------------------------------------------------------------===//
36 AutoFD(int fd) : FileDescriptor(fd) {}
38 if (FileDescriptor >= 0)
39 ::close(FileDescriptor);
43 int ret = FileDescriptor;
48 operator int() const {return FileDescriptor;}
51 error_code TempDir(SmallVectorImpl<char> &result) {
52 // FIXME: Don't use TMPDIR if program is SUID or SGID enabled.
54 (dir = std::getenv("TMPDIR" )) ||
55 (dir = std::getenv("TMP" )) ||
56 (dir = std::getenv("TEMP" )) ||
57 (dir = std::getenv("TEMPDIR")) ||
65 result.append(d.begin(), d.end());
74 error_code current_path(SmallVectorImpl<char> &result) {
75 result.reserve(MAXPATHLEN);
78 if (::getcwd(result.data(), result.capacity()) == 0) {
79 // See if there was a real error.
80 if (errno != errc::not_enough_memory)
81 return error_code(errno, system_category());
82 // Otherwise there just wasn't enough space.
83 result.reserve(result.capacity() * 2);
88 result.set_size(strlen(result.data()));
92 error_code copy_file(const Twine &from, const Twine &to, copy_option copt) {
94 SmallString<128> from_storage;
95 SmallString<128> to_storage;
96 StringRef f = from.toNullTerminatedStringRef(from_storage);
97 StringRef t = to.toNullTerminatedStringRef(to_storage);
99 const size_t buf_sz = 32768;
101 int from_file = -1, to_file = -1;
104 if ((from_file = ::open(f.begin(), O_RDONLY)) < 0)
105 return error_code(errno, system_category());
106 AutoFD from_fd(from_file);
109 struct stat from_stat;
110 if (::stat(f.begin(), &from_stat) != 0)
111 return error_code(errno, system_category());
114 int to_flags = O_CREAT | O_WRONLY;
115 if (copt == copy_option::fail_if_exists)
119 if ((to_file = ::open(t.begin(), to_flags, from_stat.st_mode)) < 0)
120 return error_code(errno, system_category());
121 AutoFD to_fd(to_file);
124 ssize_t sz, sz_read = 1, sz_write;
125 while (sz_read > 0 &&
126 (sz_read = ::read(from_fd, buffer, buf_sz)) > 0) {
127 // Allow for partial writes - see Advanced Unix Programming (2nd Ed.),
128 // Marc Rochkind, Addison-Wesley, 2004, page 94
131 if ((sz = ::write(to_fd, buffer + sz_write, sz_read - sz_write)) < 0) {
132 sz_read = sz; // cause read loop termination.
136 } while (sz_write < sz_read);
139 // After all the file operations above the return value of close actually
141 if (::close(from_fd.take()) < 0) sz_read = -1;
142 if (::close(to_fd.take()) < 0) sz_read = -1;
146 return error_code(errno, system_category());
151 error_code create_directory(const Twine &path, bool &existed) {
152 SmallString<128> path_storage;
153 StringRef p = path.toNullTerminatedStringRef(path_storage);
155 if (::mkdir(p.begin(), 0700) == -1) {
156 if (errno != errc::file_exists)
157 return error_code(errno, system_category());
165 error_code create_hard_link(const Twine &to, const Twine &from) {
167 SmallString<128> from_storage;
168 SmallString<128> to_storage;
169 StringRef f = from.toNullTerminatedStringRef(from_storage);
170 StringRef t = to.toNullTerminatedStringRef(to_storage);
172 if (::link(t.begin(), f.begin()) == -1)
173 return error_code(errno, system_category());
178 error_code create_symlink(const Twine &to, const Twine &from) {
180 SmallString<128> from_storage;
181 SmallString<128> to_storage;
182 StringRef f = from.toNullTerminatedStringRef(from_storage);
183 StringRef t = to.toNullTerminatedStringRef(to_storage);
185 if (::symlink(t.begin(), f.begin()) == -1)
186 return error_code(errno, system_category());
191 error_code remove(const Twine &path, bool &existed) {
192 SmallString<128> path_storage;
193 StringRef p = path.toNullTerminatedStringRef(path_storage);
195 if (::remove(p.begin()) == -1) {
196 if (errno != errc::no_such_file_or_directory)
197 return error_code(errno, system_category());
205 error_code rename(const Twine &from, const Twine &to) {
207 SmallString<128> from_storage;
208 SmallString<128> to_storage;
209 StringRef f = from.toNullTerminatedStringRef(from_storage);
210 StringRef t = to.toNullTerminatedStringRef(to_storage);
212 if (::rename(f.begin(), t.begin()) == -1)
213 return error_code(errno, system_category());
218 error_code resize_file(const Twine &path, uint64_t size) {
219 SmallString<128> path_storage;
220 StringRef p = path.toNullTerminatedStringRef(path_storage);
222 if (::truncate(p.begin(), size) == -1)
223 return error_code(errno, system_category());
228 error_code exists(const Twine &path, bool &result) {
229 SmallString<128> path_storage;
230 StringRef p = path.toNullTerminatedStringRef(path_storage);
233 if (::stat(p.begin(), &status) == -1) {
234 if (errno != errc::no_such_file_or_directory)
235 return error_code(errno, system_category());
243 error_code equivalent(const Twine &A, const Twine &B, bool &result) {
245 SmallString<128> a_storage;
246 SmallString<128> b_storage;
247 StringRef a = A.toNullTerminatedStringRef(a_storage);
248 StringRef b = B.toNullTerminatedStringRef(b_storage);
250 struct stat stat_a, stat_b;
251 int error_b = ::stat(b.begin(), &stat_b);
252 int error_a = ::stat(a.begin(), &stat_a);
254 // If both are invalid, it's an error. If only one is, the result is false.
255 if (error_a != 0 || error_b != 0) {
256 if (error_a == error_b)
257 return error_code(errno, system_category());
261 stat_a.st_dev == stat_b.st_dev &&
262 stat_a.st_ino == stat_b.st_ino;
268 error_code file_size(const Twine &path, uint64_t &result) {
269 SmallString<128> path_storage;
270 StringRef p = path.toNullTerminatedStringRef(path_storage);
273 if (::stat(p.begin(), &status) == -1)
274 return error_code(errno, system_category());
275 if (!S_ISREG(status.st_mode))
276 return make_error_code(errc::operation_not_permitted);
278 result = status.st_size;
282 error_code status(const Twine &path, file_status &result) {
283 SmallString<128> path_storage;
284 StringRef p = path.toNullTerminatedStringRef(path_storage);
287 if (::stat(p.begin(), &status) != 0) {
288 error_code ec(errno, system_category());
289 if (ec == errc::no_such_file_or_directory)
290 result = file_status(file_type::file_not_found);
292 result = file_status(file_type::status_error);
296 if (S_ISDIR(status.st_mode))
297 result = file_status(file_type::directory_file);
298 else if (S_ISREG(status.st_mode))
299 result = file_status(file_type::regular_file);
300 else if (S_ISBLK(status.st_mode))
301 result = file_status(file_type::block_file);
302 else if (S_ISCHR(status.st_mode))
303 result = file_status(file_type::character_file);
304 else if (S_ISFIFO(status.st_mode))
305 result = file_status(file_type::fifo_file);
306 else if (S_ISSOCK(status.st_mode))
307 result = file_status(file_type::socket_file);
309 result = file_status(file_type::type_unknown);
314 error_code unique_file(const Twine &model, int &result_fd,
315 SmallVectorImpl<char> &result_path) {
316 SmallString<128> Model;
317 model.toVector(Model);
321 // Make model absolute by prepending a temp directory if it's not already.
323 if (error_code ec = path::is_absolute(Twine(Model), absolute)) return ec;
325 SmallString<128> TDir;
326 if (error_code ec = TempDir(TDir)) return ec;
327 if (error_code ec = path::append(TDir, Twine(Model))) return ec;
331 // Replace '%' with random chars. From here on, DO NOT modify model. It may be
332 // needed if the randomly chosen path already exists.
333 SmallString<128> RandomPath;
334 RandomPath.reserve(Model.size() + 1);
335 ::srand(::time(NULL));
338 // This is opened here instead of above to make it easier to track when to
339 // close it. Collisions should be rare enough for the possible extra syscalls
341 FILE *RandomSource = ::fopen("/dev/urandom", "r");
342 RandomPath.set_size(0);
343 for (SmallVectorImpl<char>::const_iterator i = Model.begin(),
344 e = Model.end(); i != e; ++i) {
348 val = fgetc(RandomSource);
351 RandomPath.push_back("0123456789abcdef"[val & 15]);
353 RandomPath.push_back(*i);
357 ::fclose(RandomSource);
359 // Try to open + create the file.
361 int RandomFD = ::open(RandomPath.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600);
362 if (RandomFD == -1) {
363 // If the file existed, try again, otherwise, error.
364 if (errno == errc::file_exists)
365 goto retry_random_path;
366 // The path prefix doesn't exist.
367 if (errno == errc::no_such_file_or_directory) {
368 StringRef p(RandomPath.begin(), RandomPath.size());
369 SmallString<64> dir_to_create;
370 for (path::const_iterator i = path::begin(p),
371 e = --path::end(p); i != e; ++i) {
372 if (error_code ec = path::append(dir_to_create, *i)) return ec;
374 if (error_code ec = exists(Twine(dir_to_create), Exists)) return ec;
376 // Don't try to create network paths.
377 if (i->size() > 2 && (*i)[0] == '/' &&
380 return make_error_code(errc::no_such_file_or_directory);
381 if (::mkdir(dir_to_create.c_str(), 0700) == -1)
382 return error_code(errno, system_category());
385 goto rety_open_create;
387 return error_code(errno, system_category());
390 // Make the path absolute.
391 char real_path_buff[PATH_MAX + 1];
392 if (realpath(RandomPath.c_str(), real_path_buff) == NULL) {
394 ::unlink(RandomPath.c_str());
395 return error_code(errno, system_category());
398 result_path.set_size(0);
399 StringRef d(real_path_buff);
400 result_path.append(d.begin(), d.end());
402 result_fd = RandomFD;
406 } // end namespace fs
407 } // end namespace sys
408 } // end namespace llvm