/src/keystone/llvm/lib/Support/Unix/Path.inc
Line | Count | Source (jump to first uncovered line) |
1 | | //===- llvm/Support/Unix/Path.inc - 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 Path 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" |
20 | | #include <limits.h> |
21 | | #include <stdio.h> |
22 | | #if HAVE_SYS_STAT_H |
23 | | #include <sys/stat.h> |
24 | | #endif |
25 | | #if HAVE_FCNTL_H |
26 | | #include <fcntl.h> |
27 | | #endif |
28 | | #ifdef HAVE_SYS_MMAN_H |
29 | | #include <sys/mman.h> |
30 | | #endif |
31 | | #if HAVE_DIRENT_H |
32 | | # include <dirent.h> |
33 | 0 | # define NAMLEN(dirent) strlen((dirent)->d_name) |
34 | | #else |
35 | | # define dirent direct |
36 | | # define NAMLEN(dirent) (dirent)->d_namlen |
37 | | # if HAVE_SYS_NDIR_H |
38 | | # include <sys/ndir.h> |
39 | | # endif |
40 | | # if HAVE_SYS_DIR_H |
41 | | # include <sys/dir.h> |
42 | | # endif |
43 | | # if HAVE_NDIR_H |
44 | | # include <ndir.h> |
45 | | # endif |
46 | | #endif |
47 | | |
48 | | #ifdef __APPLE__ |
49 | | #include <mach-o/dyld.h> |
50 | | #endif |
51 | | |
52 | | // Both stdio.h and cstdio are included via different pathes and |
53 | | // stdcxx's cstdio doesn't include stdio.h, so it doesn't #undef the macros |
54 | | // either. |
55 | | #undef ferror |
56 | | #undef feof |
57 | | |
58 | | // For GNU Hurd |
59 | | #if defined(__GNU__) && !defined(PATH_MAX) |
60 | | # define PATH_MAX 4096 |
61 | | #endif |
62 | | |
63 | | using namespace llvm_ks; |
64 | | |
65 | | namespace llvm_ks { |
66 | | namespace sys { |
67 | | namespace fs { |
68 | | #if defined(__FreeBSD__) || defined (__NetBSD__) || defined(__Bitrig__) || \ |
69 | | defined(__OpenBSD__) || defined(__minix) || defined(__FreeBSD_kernel__) || \ |
70 | | defined(__linux__) || defined(__CYGWIN__) || defined(__DragonFly__) |
71 | | static int |
72 | | test_dir(char ret[PATH_MAX], const char *dir, const char *bin) |
73 | 0 | { |
74 | 0 | struct stat sb; |
75 | 0 | char fullpath[PATH_MAX]; |
76 | |
|
77 | 0 | snprintf(fullpath, PATH_MAX, "%s/%s", dir, bin); |
78 | 0 | if (!realpath(fullpath, ret)) |
79 | 0 | return 1; |
80 | 0 | if (stat(fullpath, &sb) != 0) |
81 | 0 | return 1; |
82 | | |
83 | 0 | return 0; |
84 | 0 | } |
85 | | |
86 | | static char * |
87 | | getprogpath(char ret[PATH_MAX], const char *bin) |
88 | 0 | { |
89 | 0 | char *pv, *s, *t; |
90 | | |
91 | | /* First approach: absolute path. */ |
92 | 0 | if (bin[0] == '/') { |
93 | 0 | if (test_dir(ret, "/", bin) == 0) |
94 | 0 | return ret; |
95 | 0 | return nullptr; |
96 | 0 | } |
97 | | |
98 | | /* Second approach: relative path. */ |
99 | 0 | if (strchr(bin, '/')) { |
100 | 0 | char cwd[PATH_MAX]; |
101 | 0 | if (!getcwd(cwd, PATH_MAX)) |
102 | 0 | return nullptr; |
103 | 0 | if (test_dir(ret, cwd, bin) == 0) |
104 | 0 | return ret; |
105 | 0 | return nullptr; |
106 | 0 | } |
107 | | |
108 | | /* Third approach: $PATH */ |
109 | 0 | if ((pv = getenv("PATH")) == nullptr) |
110 | 0 | return nullptr; |
111 | 0 | s = pv = strdup(pv); |
112 | 0 | if (!pv) |
113 | 0 | return nullptr; |
114 | 0 | while ((t = strsep(&s, ":")) != nullptr) { |
115 | 0 | if (test_dir(ret, t, bin) == 0) { |
116 | 0 | free(pv); |
117 | 0 | return ret; |
118 | 0 | } |
119 | 0 | } |
120 | 0 | free(pv); |
121 | 0 | return nullptr; |
122 | 0 | } |
123 | | #endif // __FreeBSD__ || __NetBSD__ || __FreeBSD_kernel__ |
124 | | |
125 | | /// GetMainExecutable - Return the path to the main executable, given the |
126 | | /// value of argv[0] from program startup. |
127 | 0 | std::string getMainExecutable(const char *argv0, void *MainAddr) { |
128 | | #if defined(__APPLE__) |
129 | | // On OS X the executable path is saved to the stack by dyld. Reading it |
130 | | // from there is much faster than calling dladdr, especially for large |
131 | | // binaries with symbols. |
132 | | char exe_path[MAXPATHLEN]; |
133 | | uint32_t size = sizeof(exe_path); |
134 | | if (_NSGetExecutablePath(exe_path, &size) == 0) { |
135 | | char link_path[MAXPATHLEN]; |
136 | | if (realpath(exe_path, link_path)) |
137 | | return link_path; |
138 | | } |
139 | | #elif defined(__FreeBSD__) || defined (__NetBSD__) || defined(__Bitrig__) || \ |
140 | | defined(__OpenBSD__) || defined(__minix) || defined(__DragonFly__) || \ |
141 | | defined(__FreeBSD_kernel__) |
142 | | char exe_path[PATH_MAX]; |
143 | | |
144 | | if (getprogpath(exe_path, argv0) != NULL) |
145 | | return exe_path; |
146 | | #elif defined(__linux__) || defined(__CYGWIN__) |
147 | 0 | char exe_path[MAXPATHLEN]; |
148 | 0 | StringRef aPath("/proc/self/exe"); |
149 | 0 | if (sys::fs::exists(aPath)) { |
150 | | // /proc is not always mounted under Linux (chroot for example). |
151 | 0 | ssize_t len = readlink(aPath.str().c_str(), exe_path, sizeof(exe_path)); |
152 | 0 | if (len >= 0) |
153 | 0 | return std::string(exe_path, len); |
154 | 0 | } else { |
155 | | // Fall back to the classical detection. |
156 | 0 | if (getprogpath(exe_path, argv0)) |
157 | 0 | return exe_path; |
158 | 0 | } |
159 | | #elif defined(HAVE_DLFCN_H) |
160 | | // Use dladdr to get executable path if available. |
161 | | Dl_info DLInfo; |
162 | | int err = dladdr(MainAddr, &DLInfo); |
163 | | if (err == 0) |
164 | | return ""; |
165 | | |
166 | | // If the filename is a symlink, we need to resolve and return the location of |
167 | | // the actual executable. |
168 | | char link_path[MAXPATHLEN]; |
169 | | if (realpath(DLInfo.dli_fname, link_path)) |
170 | | return link_path; |
171 | | #else |
172 | | #error GetMainExecutable is not implemented on this host yet. |
173 | | #endif |
174 | 0 | return ""; |
175 | 0 | } |
176 | | |
177 | 222k | UniqueID file_status::getUniqueID() const { |
178 | 222k | return UniqueID(fs_st_dev, fs_st_ino); |
179 | 222k | } |
180 | | |
181 | 111k | std::error_code current_path(SmallVectorImpl<char> &result) { |
182 | 111k | result.clear(); |
183 | | |
184 | 111k | const char *pwd = ::getenv("PWD"); |
185 | 111k | llvm_ks::sys::fs::file_status PWDStatus, DotStatus; |
186 | 111k | if (pwd && llvm_ks::sys::path::is_absolute(pwd) && |
187 | 111k | !llvm_ks::sys::fs::status(pwd, PWDStatus) && |
188 | 111k | !llvm_ks::sys::fs::status(".", DotStatus) && |
189 | 111k | PWDStatus.getUniqueID() == DotStatus.getUniqueID()) { |
190 | 111k | result.append(pwd, pwd + strlen(pwd)); |
191 | 111k | return std::error_code(); |
192 | 111k | } |
193 | | |
194 | 0 | #ifdef MAXPATHLEN |
195 | 0 | result.reserve(MAXPATHLEN); |
196 | | #else |
197 | | // For GNU Hurd |
198 | | result.reserve(1024); |
199 | | #endif |
200 | |
|
201 | 0 | while (true) { |
202 | 0 | if (::getcwd(result.data(), result.capacity()) == nullptr) { |
203 | | // See if there was a real error. |
204 | 0 | if (errno != ENOMEM) |
205 | 0 | return std::error_code(errno, std::generic_category()); |
206 | | // Otherwise there just wasn't enough space. |
207 | 0 | result.reserve(result.capacity() * 2); |
208 | 0 | } else |
209 | 0 | break; |
210 | 0 | } |
211 | | |
212 | 0 | result.set_size(strlen(result.data())); |
213 | 0 | return std::error_code(); |
214 | 0 | } |
215 | | |
216 | | std::error_code create_directory(const Twine &path, bool IgnoreExisting, |
217 | 0 | perms Perms) { |
218 | 0 | SmallString<128> path_storage; |
219 | 0 | StringRef p = path.toNullTerminatedStringRef(path_storage); |
220 | |
|
221 | 0 | if (::mkdir(p.begin(), Perms) == -1) { |
222 | 0 | if (errno != EEXIST || !IgnoreExisting) |
223 | 0 | return std::error_code(errno, std::generic_category()); |
224 | 0 | } |
225 | | |
226 | 0 | return std::error_code(); |
227 | 0 | } |
228 | | |
229 | | // Note that we are using symbolic link because hard links are not supported by |
230 | | // all filesystems (SMB doesn't). |
231 | 0 | std::error_code create_link(const Twine &to, const Twine &from) { |
232 | | // Get arguments. |
233 | 0 | SmallString<128> from_storage; |
234 | 0 | SmallString<128> to_storage; |
235 | 0 | StringRef f = from.toNullTerminatedStringRef(from_storage); |
236 | 0 | StringRef t = to.toNullTerminatedStringRef(to_storage); |
237 | |
|
238 | 0 | if (::symlink(t.begin(), f.begin()) == -1) |
239 | 0 | return std::error_code(errno, std::generic_category()); |
240 | | |
241 | 0 | return std::error_code(); |
242 | 0 | } |
243 | | |
244 | 0 | std::error_code remove(const Twine &path, bool IgnoreNonExisting) { |
245 | 0 | SmallString<128> path_storage; |
246 | 0 | StringRef p = path.toNullTerminatedStringRef(path_storage); |
247 | |
|
248 | 0 | struct stat buf; |
249 | 0 | if (lstat(p.begin(), &buf) != 0) { |
250 | 0 | if (errno != ENOENT || !IgnoreNonExisting) |
251 | 0 | return std::error_code(errno, std::generic_category()); |
252 | 0 | return std::error_code(); |
253 | 0 | } |
254 | | |
255 | | // Note: this check catches strange situations. In all cases, LLVM should |
256 | | // only be involved in the creation and deletion of regular files. This |
257 | | // check ensures that what we're trying to erase is a regular file. It |
258 | | // effectively prevents LLVM from erasing things like /dev/null, any block |
259 | | // special file, or other things that aren't "regular" files. |
260 | 0 | if (!S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode) && !S_ISLNK(buf.st_mode)) |
261 | 0 | return make_error_code(errc::operation_not_permitted); |
262 | | |
263 | 0 | if (::remove(p.begin()) == -1) { |
264 | 0 | if (errno != ENOENT || !IgnoreNonExisting) |
265 | 0 | return std::error_code(errno, std::generic_category()); |
266 | 0 | } |
267 | | |
268 | 0 | return std::error_code(); |
269 | 0 | } |
270 | | |
271 | 0 | std::error_code rename(const Twine &from, const Twine &to) { |
272 | | // Get arguments. |
273 | 0 | SmallString<128> from_storage; |
274 | 0 | SmallString<128> to_storage; |
275 | 0 | StringRef f = from.toNullTerminatedStringRef(from_storage); |
276 | 0 | StringRef t = to.toNullTerminatedStringRef(to_storage); |
277 | |
|
278 | 0 | if (::rename(f.begin(), t.begin()) == -1) |
279 | 0 | return std::error_code(errno, std::generic_category()); |
280 | | |
281 | 0 | return std::error_code(); |
282 | 0 | } |
283 | | |
284 | 0 | std::error_code resize_file(int FD, uint64_t Size) { |
285 | 0 | if (::ftruncate(FD, Size) == -1) |
286 | 0 | return std::error_code(errno, std::generic_category()); |
287 | | |
288 | 0 | return std::error_code(); |
289 | 0 | } |
290 | | |
291 | 0 | static int convertAccessMode(AccessMode Mode) { |
292 | 0 | switch (Mode) { |
293 | 0 | case AccessMode::Exist: |
294 | 0 | return F_OK; |
295 | 0 | case AccessMode::Write: |
296 | 0 | return W_OK; |
297 | 0 | case AccessMode::Execute: |
298 | 0 | return R_OK | X_OK; // scripts also need R_OK. |
299 | 0 | } |
300 | 0 | llvm_unreachable("invalid enum"); |
301 | 0 | } |
302 | | |
303 | 0 | std::error_code access(const Twine &Path, AccessMode Mode) { |
304 | 0 | SmallString<128> PathStorage; |
305 | 0 | StringRef P = Path.toNullTerminatedStringRef(PathStorage); |
306 | |
|
307 | 0 | if (::access(P.begin(), convertAccessMode(Mode)) == -1) |
308 | 0 | return std::error_code(errno, std::generic_category()); |
309 | | |
310 | 0 | if (Mode == AccessMode::Execute) { |
311 | | // Don't say that directories are executable. |
312 | 0 | struct stat buf; |
313 | 0 | if (0 != stat(P.begin(), &buf)) |
314 | 0 | return errc::permission_denied; |
315 | 0 | if (!S_ISREG(buf.st_mode)) |
316 | 0 | return errc::permission_denied; |
317 | 0 | } |
318 | | |
319 | 0 | return std::error_code(); |
320 | 0 | } |
321 | | |
322 | 0 | bool can_execute(const Twine &Path) { |
323 | 0 | return !access(Path, AccessMode::Execute); |
324 | 0 | } |
325 | | |
326 | 0 | bool equivalent(file_status A, file_status B) { |
327 | 0 | assert(status_known(A) && status_known(B)); |
328 | 0 | return A.fs_st_dev == B.fs_st_dev && |
329 | 0 | A.fs_st_ino == B.fs_st_ino; |
330 | 0 | } |
331 | | |
332 | 0 | std::error_code equivalent(const Twine &A, const Twine &B, bool &result) { |
333 | 0 | file_status fsA, fsB; |
334 | 0 | if (std::error_code ec = status(A, fsA)) |
335 | 0 | return ec; |
336 | 0 | if (std::error_code ec = status(B, fsB)) |
337 | 0 | return ec; |
338 | 0 | result = equivalent(fsA, fsB); |
339 | 0 | return std::error_code(); |
340 | 0 | } |
341 | | |
342 | | static std::error_code fillStatus(int StatRet, const struct stat &Status, |
343 | 222k | file_status &Result) { |
344 | 222k | if (StatRet != 0) { |
345 | 0 | std::error_code ec(errno, std::generic_category()); |
346 | 0 | if (ec == errc::no_such_file_or_directory) |
347 | 0 | Result = file_status(file_type::file_not_found); |
348 | 0 | else |
349 | 0 | Result = file_status(file_type::status_error); |
350 | 0 | return ec; |
351 | 0 | } |
352 | | |
353 | 222k | file_type Type = file_type::type_unknown; |
354 | | |
355 | 222k | if (S_ISDIR(Status.st_mode)) |
356 | 222k | Type = file_type::directory_file; |
357 | 0 | else if (S_ISREG(Status.st_mode)) |
358 | 0 | Type = file_type::regular_file; |
359 | 0 | else if (S_ISBLK(Status.st_mode)) |
360 | 0 | Type = file_type::block_file; |
361 | 0 | else if (S_ISCHR(Status.st_mode)) |
362 | 0 | Type = file_type::character_file; |
363 | 0 | else if (S_ISFIFO(Status.st_mode)) |
364 | 0 | Type = file_type::fifo_file; |
365 | 0 | else if (S_ISSOCK(Status.st_mode)) |
366 | 0 | Type = file_type::socket_file; |
367 | | |
368 | 222k | perms Perms = static_cast<perms>(Status.st_mode); |
369 | 222k | Result = |
370 | 222k | file_status(Type, Perms, Status.st_dev, Status.st_ino, Status.st_mtime, |
371 | 222k | Status.st_uid, Status.st_gid, Status.st_size); |
372 | | |
373 | 222k | return std::error_code(); |
374 | 222k | } |
375 | | |
376 | 222k | std::error_code status(const Twine &Path, file_status &Result) { |
377 | 222k | SmallString<128> PathStorage; |
378 | 222k | StringRef P = Path.toNullTerminatedStringRef(PathStorage); |
379 | | |
380 | 222k | struct stat Status; |
381 | 222k | int StatRet = ::stat(P.begin(), &Status); |
382 | 222k | return fillStatus(StatRet, Status, Result); |
383 | 222k | } |
384 | | |
385 | 2 | std::error_code status(int FD, file_status &Result) { |
386 | 2 | struct stat Status; |
387 | 2 | int StatRet = ::fstat(FD, &Status); |
388 | 2 | return fillStatus(StatRet, Status, Result); |
389 | 2 | } |
390 | | |
391 | | std::error_code mapped_file_region::init(int FD, uint64_t Offset, |
392 | 0 | mapmode Mode) { |
393 | 0 | assert(Size != 0); |
394 | | |
395 | 0 | int flags = (Mode == readwrite) ? MAP_SHARED : MAP_PRIVATE; |
396 | 0 | int prot = (Mode == readonly) ? PROT_READ : (PROT_READ | PROT_WRITE); |
397 | 0 | Mapping = ::mmap(nullptr, Size, prot, flags, FD, Offset); |
398 | 0 | if (Mapping == MAP_FAILED) |
399 | 0 | return std::error_code(errno, std::generic_category()); |
400 | 0 | return std::error_code(); |
401 | 0 | } |
402 | | |
403 | | mapped_file_region::mapped_file_region(int fd, mapmode mode, uint64_t length, |
404 | | uint64_t offset, std::error_code &ec) |
405 | 0 | : Size(length), Mapping() { |
406 | | // Make sure that the requested size fits within SIZE_T. |
407 | 0 | if (length > std::numeric_limits<size_t>::max()) { |
408 | 0 | ec = make_error_code(errc::invalid_argument); |
409 | 0 | return; |
410 | 0 | } |
411 | | |
412 | 0 | ec = init(fd, offset, mode); |
413 | 0 | if (ec) |
414 | 0 | Mapping = nullptr; |
415 | 0 | } |
416 | | |
417 | 0 | mapped_file_region::~mapped_file_region() { |
418 | 0 | if (Mapping) |
419 | 0 | ::munmap(Mapping, Size); |
420 | 0 | } |
421 | | |
422 | 0 | uint64_t mapped_file_region::size() const { |
423 | 0 | assert(Mapping && "Mapping failed but used anyway!"); |
424 | 0 | return Size; |
425 | 0 | } |
426 | | |
427 | 0 | char *mapped_file_region::data() const { |
428 | 0 | assert(Mapping && "Mapping failed but used anyway!"); |
429 | 0 | return reinterpret_cast<char*>(Mapping); |
430 | 0 | } |
431 | | |
432 | 0 | const char *mapped_file_region::const_data() const { |
433 | 0 | assert(Mapping && "Mapping failed but used anyway!"); |
434 | 0 | return reinterpret_cast<const char*>(Mapping); |
435 | 0 | } |
436 | | |
437 | 0 | int mapped_file_region::alignment() { |
438 | 0 | return 4096; |
439 | 0 | } |
440 | | |
441 | | std::error_code detail::directory_iterator_construct(detail::DirIterState &it, |
442 | 0 | StringRef path){ |
443 | 0 | SmallString<128> path_null(path); |
444 | 0 | DIR *directory = ::opendir(path_null.c_str()); |
445 | 0 | if (!directory) |
446 | 0 | return std::error_code(errno, std::generic_category()); |
447 | | |
448 | 0 | it.IterationHandle = reinterpret_cast<intptr_t>(directory); |
449 | | // Add something for replace_filename to replace. |
450 | 0 | path::append(path_null, "."); |
451 | 0 | it.CurrentEntry = directory_entry(path_null.str()); |
452 | 0 | return directory_iterator_increment(it); |
453 | 0 | } |
454 | | |
455 | 0 | std::error_code detail::directory_iterator_destruct(detail::DirIterState &it) { |
456 | 0 | if (it.IterationHandle) |
457 | 0 | ::closedir(reinterpret_cast<DIR *>(it.IterationHandle)); |
458 | 0 | it.IterationHandle = 0; |
459 | 0 | it.CurrentEntry = directory_entry(); |
460 | 0 | return std::error_code(); |
461 | 0 | } |
462 | | |
463 | 0 | std::error_code detail::directory_iterator_increment(detail::DirIterState &it) { |
464 | 0 | errno = 0; |
465 | 0 | dirent *cur_dir = ::readdir(reinterpret_cast<DIR *>(it.IterationHandle)); |
466 | 0 | if (cur_dir == nullptr && errno != 0) { |
467 | 0 | return std::error_code(errno, std::generic_category()); |
468 | 0 | } else if (cur_dir != nullptr) { |
469 | 0 | StringRef name(cur_dir->d_name, NAMLEN(cur_dir)); |
470 | 0 | if ((name.size() == 1 && name[0] == '.') || |
471 | 0 | (name.size() == 2 && name[0] == '.' && name[1] == '.')) |
472 | 0 | return directory_iterator_increment(it); |
473 | 0 | it.CurrentEntry.replace_filename(name); |
474 | 0 | } else |
475 | 0 | return directory_iterator_destruct(it); |
476 | | |
477 | 0 | return std::error_code(); |
478 | 0 | } |
479 | | |
480 | 36 | std::error_code openFileForRead(const Twine &Name, int &ResultFD) { |
481 | 36 | SmallString<128> Storage; |
482 | 36 | StringRef P = Name.toNullTerminatedStringRef(Storage); |
483 | 36 | while ((ResultFD = open(P.begin(), O_RDONLY)) < 0) { |
484 | 34 | if (errno != EINTR) |
485 | 34 | return std::error_code(errno, std::generic_category()); |
486 | 34 | } |
487 | 2 | return std::error_code(); |
488 | 36 | } |
489 | | |
490 | | std::error_code openFileForWrite(const Twine &Name, int &ResultFD, |
491 | 0 | sys::fs::OpenFlags Flags, unsigned Mode) { |
492 | | // Verify that we don't have both "append" and "excl". |
493 | 0 | assert((!(Flags & sys::fs::F_Excl) || !(Flags & sys::fs::F_Append)) && |
494 | 0 | "Cannot specify both 'excl' and 'append' file creation flags!"); |
495 | | |
496 | 0 | int OpenFlags = O_CREAT; |
497 | |
|
498 | 0 | if (Flags & F_RW) |
499 | 0 | OpenFlags |= O_RDWR; |
500 | 0 | else |
501 | 0 | OpenFlags |= O_WRONLY; |
502 | |
|
503 | 0 | if (Flags & F_Append) |
504 | 0 | OpenFlags |= O_APPEND; |
505 | 0 | else |
506 | 0 | OpenFlags |= O_TRUNC; |
507 | |
|
508 | 0 | if (Flags & F_Excl) |
509 | 0 | OpenFlags |= O_EXCL; |
510 | |
|
511 | 0 | SmallString<128> Storage; |
512 | 0 | StringRef P = Name.toNullTerminatedStringRef(Storage); |
513 | 0 | while ((ResultFD = open(P.begin(), OpenFlags, Mode)) < 0) { |
514 | 0 | if (errno != EINTR) |
515 | 0 | return std::error_code(errno, std::generic_category()); |
516 | 0 | } |
517 | 0 | return std::error_code(); |
518 | 0 | } |
519 | | |
520 | | } // end namespace fs |
521 | | |
522 | | namespace path { |
523 | | |
524 | 0 | bool home_directory(SmallVectorImpl<char> &result) { |
525 | 0 | if (char *RequestedDir = getenv("HOME")) { |
526 | 0 | result.clear(); |
527 | 0 | result.append(RequestedDir, RequestedDir + strlen(RequestedDir)); |
528 | 0 | return true; |
529 | 0 | } |
530 | | |
531 | 0 | return false; |
532 | 0 | } |
533 | | |
534 | 0 | static bool getDarwinConfDir(bool TempDir, SmallVectorImpl<char> &Result) { |
535 | | #if defined(_CS_DARWIN_USER_TEMP_DIR) && defined(_CS_DARWIN_USER_CACHE_DIR) |
536 | | // On Darwin, use DARWIN_USER_TEMP_DIR or DARWIN_USER_CACHE_DIR. |
537 | | // macros defined in <unistd.h> on darwin >= 9 |
538 | | int ConfName = TempDir ? _CS_DARWIN_USER_TEMP_DIR |
539 | | : _CS_DARWIN_USER_CACHE_DIR; |
540 | | size_t ConfLen = confstr(ConfName, nullptr, 0); |
541 | | if (ConfLen > 0) { |
542 | | do { |
543 | | Result.resize(ConfLen); |
544 | | ConfLen = confstr(ConfName, Result.data(), Result.size()); |
545 | | } while (ConfLen > 0 && ConfLen != Result.size()); |
546 | | |
547 | | if (ConfLen > 0) { |
548 | | assert(Result.back() == 0); |
549 | | Result.pop_back(); |
550 | | return true; |
551 | | } |
552 | | |
553 | | Result.clear(); |
554 | | } |
555 | | #endif |
556 | 0 | return false; |
557 | 0 | } |
558 | | |
559 | 0 | static bool getUserCacheDir(SmallVectorImpl<char> &Result) { |
560 | | // First try using XDS_CACHE_HOME env variable, |
561 | | // as specified in XDG Base Directory Specification at |
562 | | // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html |
563 | 0 | if (const char *XdsCacheDir = std::getenv("XDS_CACHE_HOME")) { |
564 | 0 | Result.clear(); |
565 | 0 | Result.append(XdsCacheDir, XdsCacheDir + strlen(XdsCacheDir)); |
566 | 0 | return true; |
567 | 0 | } |
568 | | |
569 | | // Try Darwin configuration query |
570 | 0 | if (getDarwinConfDir(false, Result)) |
571 | 0 | return true; |
572 | | |
573 | | // Use "$HOME/.cache" if $HOME is available |
574 | 0 | if (home_directory(Result)) { |
575 | 0 | append(Result, ".cache"); |
576 | 0 | return true; |
577 | 0 | } |
578 | | |
579 | 0 | return false; |
580 | 0 | } |
581 | | |
582 | 0 | static const char *getEnvTempDir() { |
583 | | // Check whether the temporary directory is specified by an environment |
584 | | // variable. |
585 | 0 | const char *EnvironmentVariables[] = {"TMPDIR", "TMP", "TEMP", "TEMPDIR"}; |
586 | 0 | for (const char *Env : EnvironmentVariables) { |
587 | 0 | if (const char *Dir = std::getenv(Env)) |
588 | 0 | return Dir; |
589 | 0 | } |
590 | | |
591 | 0 | return nullptr; |
592 | 0 | } |
593 | | |
594 | 0 | static const char *getDefaultTempDir(bool ErasedOnReboot) { |
595 | 0 | #ifdef P_tmpdir |
596 | 0 | if ((bool)P_tmpdir) |
597 | 0 | return P_tmpdir; |
598 | 0 | #endif |
599 | | |
600 | 0 | if (ErasedOnReboot) |
601 | 0 | return "/tmp"; |
602 | 0 | return "/var/tmp"; |
603 | 0 | } |
604 | | |
605 | 0 | void system_temp_directory(bool ErasedOnReboot, SmallVectorImpl<char> &Result) { |
606 | 0 | Result.clear(); |
607 | |
|
608 | 0 | if (ErasedOnReboot) { |
609 | | // There is no env variable for the cache directory. |
610 | 0 | if (const char *RequestedDir = getEnvTempDir()) { |
611 | 0 | Result.append(RequestedDir, RequestedDir + strlen(RequestedDir)); |
612 | 0 | return; |
613 | 0 | } |
614 | 0 | } |
615 | | |
616 | 0 | if (getDarwinConfDir(ErasedOnReboot, Result)) |
617 | 0 | return; |
618 | | |
619 | 0 | const char *RequestedDir = getDefaultTempDir(ErasedOnReboot); |
620 | 0 | Result.append(RequestedDir, RequestedDir + strlen(RequestedDir)); |
621 | 0 | } |
622 | | |
623 | | } // end namespace path |
624 | | |
625 | | } // end namespace sys |
626 | | } // end namespace llvm_ks |