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());
66 return make_error_code(errc::success);
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);
79 if (::getcwd(result.data(), result.size()) == 0)
80 return error_code(errno, system_category());
82 result.set_size(strlen(result.data()));
83 return make_error_code(errc::success);
86 } // end namespace path
90 error_code copy_file(const Twine &from, const Twine &to, copy_option copt) {
92 SmallString<128> from_storage;
93 SmallString<128> to_storage;
94 StringRef f = from.toNullTerminatedStringRef(from_storage);
95 StringRef t = to.toNullTerminatedStringRef(to_storage);
97 const size_t buf_sz = 32768;
99 int from_file = -1, to_file = -1;
102 if ((from_file = ::open(f.begin(), O_RDONLY)) < 0)
103 return error_code(errno, system_category());
104 AutoFD from_fd(from_file);
107 struct stat from_stat;
108 if (::stat(f.begin(), &from_stat) != 0)
109 return error_code(errno, system_category());
112 int to_flags = O_CREAT | O_WRONLY;
113 if (copt == copy_option::fail_if_exists)
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);
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
129 if ((sz = ::write(to_fd, buffer + sz_write, sz_read - sz_write)) < 0) {
130 sz_read = sz; // cause read loop termination.
134 } while (sz_write < sz_read);
137 // After all the file operations above the return value of close actually
139 if (::close(from_fd.take()) < 0) sz_read = -1;
140 if (::close(to_fd.take()) < 0) sz_read = -1;
144 return error_code(errno, system_category());
146 return make_error_code(errc::success);
149 error_code create_directory(const Twine &path, bool &existed) {
150 SmallString<128> path_storage;
151 StringRef p = path.toNullTerminatedStringRef(path_storage);
153 if (::mkdir(p.begin(), 0700) == -1) {
155 return error_code(errno, system_category());
160 return make_error_code(errc::success);
163 error_code create_hard_link(const Twine &to, const Twine &from) {
165 SmallString<128> from_storage;
166 SmallString<128> to_storage;
167 StringRef f = from.toNullTerminatedStringRef(from_storage);
168 StringRef t = to.toNullTerminatedStringRef(to_storage);
170 if (::link(t.begin(), f.begin()) == -1)
171 return error_code(errno, system_category());
173 return make_error_code(errc::success);
176 error_code create_symlink(const Twine &to, const Twine &from) {
178 SmallString<128> from_storage;
179 SmallString<128> to_storage;
180 StringRef f = from.toNullTerminatedStringRef(from_storage);
181 StringRef t = to.toNullTerminatedStringRef(to_storage);
183 if (::symlink(t.begin(), f.begin()) == -1)
184 return error_code(errno, system_category());
186 return make_error_code(errc::success);
189 error_code remove(const Twine &path, bool &existed) {
190 SmallString<128> path_storage;
191 StringRef p = path.toNullTerminatedStringRef(path_storage);
193 if (::remove(p.begin()) == -1) {
195 return error_code(errno, system_category());
200 return make_error_code(errc::success);
203 error_code rename(const Twine &from, const Twine &to) {
205 SmallString<128> from_storage;
206 SmallString<128> to_storage;
207 StringRef f = from.toNullTerminatedStringRef(from_storage);
208 StringRef t = to.toNullTerminatedStringRef(to_storage);
210 if (::rename(f.begin(), t.begin()) == -1)
211 return error_code(errno, system_category());
213 return make_error_code(errc::success);
216 error_code resize_file(const Twine &path, uint64_t size) {
217 SmallString<128> path_storage;
218 StringRef p = path.toNullTerminatedStringRef(path_storage);
220 if (::truncate(p.begin(), size) == -1)
221 return error_code(errno, system_category());
223 return make_error_code(errc::success);
226 error_code exists(const Twine &path, bool &result) {
227 SmallString<128> path_storage;
228 StringRef p = path.toNullTerminatedStringRef(path_storage);
231 if (::stat(p.begin(), &status) == -1) {
233 return error_code(errno, system_category());
238 return make_error_code(errc::success);
241 error_code equivalent(const Twine &A, const Twine &B, bool &result) {
243 SmallString<128> a_storage;
244 SmallString<128> b_storage;
245 StringRef a = A.toNullTerminatedStringRef(a_storage);
246 StringRef b = B.toNullTerminatedStringRef(b_storage);
248 struct stat stat_a, stat_b;
249 int error_b = ::stat(b.begin(), &stat_b);
250 int error_a = ::stat(a.begin(), &stat_a);
252 // If both are invalid, it's an error. If only one is, the result is false.
253 if (error_a != 0 || error_b != 0) {
254 if (error_a == error_b)
255 return error_code(errno, system_category());
259 stat_a.st_dev == stat_b.st_dev &&
260 stat_a.st_ino == stat_b.st_ino;
263 return make_error_code(errc::success);
266 error_code file_size(const Twine &path, uint64_t &result) {
267 SmallString<128> path_storage;
268 StringRef p = path.toNullTerminatedStringRef(path_storage);
271 if (::stat(p.begin(), &status) == -1)
272 return error_code(errno, system_category());
273 if (!S_ISREG(status.st_mode))
274 return error_code(EPERM, system_category());
276 result = status.st_size;
277 return make_error_code(errc::success);
280 error_code unique_file(const Twine &model, int &result_fd,
281 SmallVectorImpl<char> &result_path) {
282 SmallString<128> Model;
283 model.toVector(Model);
287 // Make model absolute by prepending a temp directory if it's not already.
289 if (error_code ec = path::is_absolute(Twine(Model), absolute)) return ec;
291 SmallString<128> TDir;
292 if (error_code ec = TempDir(TDir)) return ec;
293 if (error_code ec = path::append(TDir, Twine(Model))) return ec;
297 // Replace '%' with random chars. From here on, DO NOT modify model. It may be
298 // needed if the randomly chosen path already exists.
299 SmallString<128> RandomPath;
300 RandomPath.reserve(Model.size() + 1);
301 ::srand(::time(NULL));
304 // This is opened here instead of above to make it easier to track when to
305 // close it. Collisions should be rare enough for the possible extra syscalls
307 FILE *RandomSource = ::fopen("/dev/urandom", "r");
308 RandomPath.set_size(0);
309 for (SmallVectorImpl<char>::const_iterator i = Model.begin(),
310 e = Model.end(); i != e; ++i) {
314 val = fgetc(RandomSource);
317 RandomPath.push_back("0123456789abcdef"[val & 15]);
319 RandomPath.push_back(*i);
323 ::fclose(RandomSource);
325 // Try to open + create the file.
327 int RandomFD = ::open(RandomPath.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600);
328 if (RandomFD == -1) {
329 // If the file existed, try again, otherwise, error.
331 goto retry_random_path;
332 // The path prefix doesn't exist.
333 if (errno == ENOENT) {
334 StringRef p(RandomPath.begin(), RandomPath.size());
335 SmallString<64> dir_to_create;
336 for (path::const_iterator i = path::begin(p),
337 e = --path::end(p); i != e; ++i) {
338 if (error_code ec = path::append(dir_to_create, *i)) return ec;
340 if (error_code ec = exists(Twine(dir_to_create), Exists)) return ec;
342 // Don't try to create network paths.
343 if (i->size() > 2 && (*i)[0] == '/' &&
346 return error_code(ENOENT, system_category());
347 if (::mkdir(dir_to_create.c_str(), 0700) == -1)
348 return error_code(errno, system_category());
351 goto rety_open_create;
353 return error_code(errno, system_category());
356 // Make the path absolute.
357 char real_path_buff[PATH_MAX + 1];
358 if (realpath(RandomPath.c_str(), real_path_buff) == NULL) {
360 ::unlink(RandomPath.c_str());
361 return error_code(errno, system_category());
364 result_path.set_size(0);
365 StringRef d(real_path_buff);
366 result_path.append(d.begin(), d.end());
368 result_fd = RandomFD;
369 return make_error_code(errc::success);
372 } // end namespace fs
373 } // end namespace sys
374 } // end namespace llvm