blob: b5cf724c5ff862eb256034fdda11eed6f88e537f [file] [log] [blame]
Michael J. Spencerdffde992010-11-29 22:28:51 +00001//===- 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. Spencerbee0c382010-12-01 19:32:01 +000020#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. Spencer3cb84ef2010-12-03 01:21:28 +000026#if HAVE_STDIO_H
27#include <stdio.h>
Michael J. Spencerbee0c382010-12-01 19:32:01 +000028#endif
29
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +000030using namespace llvm;
31
Michael J. Spencerbee0c382010-12-01 19:32:01 +000032namespace {
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. Spencer3cb84ef2010-12-03 01:21:28 +000050
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());
Michael J. Spencer9d425e72010-12-04 18:45:32 +000066 return success;
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +000067 }
Michael J. Spencerbee0c382010-12-01 19:32:01 +000068}
Michael J. Spencerdffde992010-11-29 22:28:51 +000069
70namespace llvm {
71namespace sys {
72namespace path {
73
74error_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()));
Michael J. Spencer9d425e72010-12-04 18:45:32 +000083 return success;
Michael J. Spencerdffde992010-11-29 22:28:51 +000084}
85
86} // end namespace path
Michael J. Spencerbee0c382010-12-01 19:32:01 +000087
88namespace fs{
89
90error_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. Spencer871498e2010-12-01 20:37:42 +000094 StringRef f = from.toNullTerminatedStringRef(from_storage);
95 StringRef t = to.toNullTerminatedStringRef(to_storage);
Michael J. Spencerbee0c382010-12-01 19:32:01 +000096
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
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000146 return success;
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000147}
148
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000149error_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) {
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000154 if (errno != errc::file_exists)
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000155 return error_code(errno, system_category());
156 existed = true;
157 } else
158 existed = false;
159
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000160 return success;
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000161}
162
Michael J. Spencerd7b305f2010-12-03 05:58:41 +0000163error_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
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000173 return success;
Michael J. Spencerd7b305f2010-12-03 05:58:41 +0000174}
175
Michael J. Spencer9b391c52010-12-03 07:41:25 +0000176error_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
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000186 return success;
Michael J. Spencer9b391c52010-12-03 07:41:25 +0000187}
188
Michael J. Spencer106aa732010-12-03 17:53:43 +0000189error_code remove(const Twine &path, bool &existed) {
190 SmallString<128> path_storage;
191 StringRef p = path.toNullTerminatedStringRef(path_storage);
192
193 if (::remove(p.begin()) == -1) {
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000194 if (errno != errc::no_such_file_or_directory)
Michael J. Spencer106aa732010-12-03 17:53:43 +0000195 return error_code(errno, system_category());
196 existed = false;
197 } else
198 existed = true;
199
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000200 return success;
Michael J. Spencer106aa732010-12-03 17:53:43 +0000201}
202
Michael J. Spencera50b98c2010-12-03 17:53:55 +0000203error_code rename(const Twine &from, const Twine &to) {
204 // Get arguments.
205 SmallString<128> from_storage;
206 SmallString<128> to_storage;
207 StringRef f = from.toNullTerminatedStringRef(from_storage);
208 StringRef t = to.toNullTerminatedStringRef(to_storage);
209
210 if (::rename(f.begin(), t.begin()) == -1)
211 return error_code(errno, system_category());
212
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000213 return success;
Michael J. Spencera50b98c2010-12-03 17:53:55 +0000214}
215
Michael J. Spencer3920d3b2010-12-03 17:54:07 +0000216error_code resize_file(const Twine &path, uint64_t size) {
217 SmallString<128> path_storage;
218 StringRef p = path.toNullTerminatedStringRef(path_storage);
219
220 if (::truncate(p.begin(), size) == -1)
221 return error_code(errno, system_category());
222
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000223 return success;
Michael J. Spencer3920d3b2010-12-03 17:54:07 +0000224}
225
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000226error_code exists(const Twine &path, bool &result) {
227 SmallString<128> path_storage;
228 StringRef p = path.toNullTerminatedStringRef(path_storage);
229
230 struct stat status;
231 if (::stat(p.begin(), &status) == -1) {
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000232 if (errno != errc::no_such_file_or_directory)
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000233 return error_code(errno, system_category());
234 result = false;
235 } else
236 result = true;
237
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000238 return success;
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000239}
240
Michael J. Spencerb531f452010-12-03 18:49:13 +0000241error_code equivalent(const Twine &A, const Twine &B, bool &result) {
242 // Get arguments.
243 SmallString<128> a_storage;
244 SmallString<128> b_storage;
245 StringRef a = A.toNullTerminatedStringRef(a_storage);
246 StringRef b = B.toNullTerminatedStringRef(b_storage);
247
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);
251
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());
256 result = false;
257 } else {
258 result =
259 stat_a.st_dev == stat_b.st_dev &&
260 stat_a.st_ino == stat_b.st_ino;
261 }
262
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000263 return success;
Michael J. Spencerb531f452010-12-03 18:49:13 +0000264}
265
Michael J. Spencer01a87c42010-12-04 00:31:48 +0000266error_code file_size(const Twine &path, uint64_t &result) {
267 SmallString<128> path_storage;
268 StringRef p = path.toNullTerminatedStringRef(path_storage);
269
270 struct stat status;
271 if (::stat(p.begin(), &status) == -1)
272 return error_code(errno, system_category());
273 if (!S_ISREG(status.st_mode))
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000274 return make_error_code(errc::operation_not_permitted);
Michael J. Spencer01a87c42010-12-04 00:31:48 +0000275
276 result = status.st_size;
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000277 return success;
Michael J. Spencer01a87c42010-12-04 00:31:48 +0000278}
279
Michael J. Spencer470ae132010-12-04 00:32:40 +0000280error_code status(const Twine &path, file_status &result) {
281 SmallString<128> path_storage;
282 StringRef p = path.toNullTerminatedStringRef(path_storage);
283
284 struct stat status;
285 if (::stat(p.begin(), &status) != 0) {
286 error_code ec(errno, system_category());
287 if (ec == errc::no_such_file_or_directory)
288 result = file_status(file_type::file_not_found);
289 else
290 result = file_status(file_type::status_error);
291 return ec;
292 }
293
294 if (S_ISDIR(status.st_mode))
295 result = file_status(file_type::directory_file);
296 else if (S_ISREG(status.st_mode))
297 result = file_status(file_type::regular_file);
298 else if (S_ISBLK(status.st_mode))
299 result = file_status(file_type::block_file);
300 else if (S_ISCHR(status.st_mode))
301 result = file_status(file_type::character_file);
302 else if (S_ISFIFO(status.st_mode))
303 result = file_status(file_type::fifo_file);
304 else if (S_ISSOCK(status.st_mode))
305 result = file_status(file_type::socket_file);
306 else
307 result = file_status(file_type::type_unknown);
308
309 return success;
310}
311
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000312error_code unique_file(const Twine &model, int &result_fd,
313 SmallVectorImpl<char> &result_path) {
314 SmallString<128> Model;
315 model.toVector(Model);
316 // Null terminate.
317 Model.c_str();
318
319 // Make model absolute by prepending a temp directory if it's not already.
320 bool absolute;
321 if (error_code ec = path::is_absolute(Twine(Model), absolute)) return ec;
322 if (!absolute) {
323 SmallString<128> TDir;
324 if (error_code ec = TempDir(TDir)) return ec;
325 if (error_code ec = path::append(TDir, Twine(Model))) return ec;
326 Model.swap(TDir);
327 }
328
329 // Replace '%' with random chars. From here on, DO NOT modify model. It may be
330 // needed if the randomly chosen path already exists.
331 SmallString<128> RandomPath;
332 RandomPath.reserve(Model.size() + 1);
333 ::srand(::time(NULL));
334
335retry_random_path:
336 // This is opened here instead of above to make it easier to track when to
337 // close it. Collisions should be rare enough for the possible extra syscalls
338 // not to matter.
339 FILE *RandomSource = ::fopen("/dev/urandom", "r");
340 RandomPath.set_size(0);
341 for (SmallVectorImpl<char>::const_iterator i = Model.begin(),
342 e = Model.end(); i != e; ++i) {
343 if (*i == '%') {
344 char val = 0;
345 if (RandomSource)
346 val = fgetc(RandomSource);
347 else
348 val = ::rand();
349 RandomPath.push_back("0123456789abcdef"[val & 15]);
350 } else
351 RandomPath.push_back(*i);
352 }
353
354 if (RandomSource)
355 ::fclose(RandomSource);
356
357 // Try to open + create the file.
358rety_open_create:
359 int RandomFD = ::open(RandomPath.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600);
360 if (RandomFD == -1) {
361 // If the file existed, try again, otherwise, error.
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000362 if (errno == errc::file_exists)
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000363 goto retry_random_path;
364 // The path prefix doesn't exist.
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000365 if (errno == errc::no_such_file_or_directory) {
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000366 StringRef p(RandomPath.begin(), RandomPath.size());
367 SmallString<64> dir_to_create;
368 for (path::const_iterator i = path::begin(p),
369 e = --path::end(p); i != e; ++i) {
370 if (error_code ec = path::append(dir_to_create, *i)) return ec;
371 bool Exists;
372 if (error_code ec = exists(Twine(dir_to_create), Exists)) return ec;
373 if (!Exists) {
374 // Don't try to create network paths.
375 if (i->size() > 2 && (*i)[0] == '/' &&
376 (*i)[1] == '/' &&
377 (*i)[2] != '/')
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000378 return make_error_code(errc::no_such_file_or_directory);
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000379 if (::mkdir(dir_to_create.c_str(), 0700) == -1)
380 return error_code(errno, system_category());
381 }
382 }
383 goto rety_open_create;
384 }
385 return error_code(errno, system_category());
386 }
387
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000388 // Make the path absolute.
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000389 char real_path_buff[PATH_MAX + 1];
390 if (realpath(RandomPath.c_str(), real_path_buff) == NULL) {
391 ::close(RandomFD);
392 ::unlink(RandomPath.c_str());
393 return error_code(errno, system_category());
394 }
395
396 result_path.set_size(0);
397 StringRef d(real_path_buff);
398 result_path.append(d.begin(), d.end());
399
400 result_fd = RandomFD;
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000401 return success;
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000402}
403
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000404} // end namespace fs
Michael J. Spencerdffde992010-11-29 22:28:51 +0000405} // end namespace sys
406} // end namespace llvm