1 //===- llvm/Support/Unix/Path.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 portion of the Path class.
12 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
15 //=== WARNING: Implementation here must contain only generic UNIX code that
16 //=== is guaranteed to work on *all* UNIX variants.
17 //===----------------------------------------------------------------------===//
26 #ifdef HAVE_SYS_MMAN_H
29 #ifdef HAVE_SYS_STAT_H
40 # define NAMLEN(dirent) strlen((dirent)->d_name)
42 # define dirent direct
43 # define NAMLEN(dirent) (dirent)->d_namlen
45 # include <sys/ndir.h>
60 #include <mach-o/dyld.h>
64 #if defined(__GNU__) && !defined(MAXPATHLEN)
65 # define MAXPATHLEN 4096
68 // Put in a hack for Cygwin which falsely reports that the mkdtemp function
69 // is available when it is not.
75 inline bool lastIsSlash(const std::string& path) {
76 return !path.empty() && path[path.length() - 1] == '/';
84 Path::Path(StringRef p)
87 Path::Path(const char *StrStart, unsigned StrLen)
88 : path(StrStart, StrLen) {}
91 Path::operator=(StringRef that) {
92 path.assign(that.data(), that.size());
97 Path::isValid() const {
98 // Empty paths are considered invalid here.
99 // This code doesn't check MAXPATHLEN because there's no need. Nothing in
100 // LLVM manipulates Paths with fixed-sizes arrays, and if the OS can't
101 // handle names longer than some limit, it'll report this on demand using
103 return !path.empty();
107 Path::GetTemporaryDirectory(std::string *ErrMsg) {
108 #if defined(HAVE_MKDTEMP)
109 // The best way is with mkdtemp but that's not available on many systems,
110 // Linux and FreeBSD have it. Others probably won't.
111 char pathname[] = "/tmp/llvm_XXXXXX";
112 if (0 == mkdtemp(pathname)) {
114 std::string(pathname) + ": can't create temporary directory");
117 return Path(pathname);
118 #elif defined(HAVE_MKSTEMP)
119 // If no mkdtemp is available, mkstemp can be used to create a temporary file
120 // which is then removed and created as a directory. We prefer this over
121 // mktemp because of mktemp's inherent security and threading risks. We still
122 // have a slight race condition from the time the temporary file is created to
123 // the time it is re-created as a directoy.
124 char pathname[] = "/tmp/llvm_XXXXXX";
126 if (-1 == (fd = mkstemp(pathname))) {
128 std::string(pathname) + ": can't create temporary directory");
132 ::unlink(pathname); // start race condition, ignore errors
133 if (-1 == ::mkdir(pathname, S_IRWXU)) { // end race condition
135 std::string(pathname) + ": can't create temporary directory");
138 return Path(pathname);
139 #elif defined(HAVE_MKTEMP)
140 // If a system doesn't have mkdtemp(3) or mkstemp(3) but it does have
141 // mktemp(3) then we'll assume that system (e.g. AIX) has a reasonable
142 // implementation of mktemp(3) and doesn't follow BSD 4.3's lead of replacing
143 // the XXXXXX with the pid of the process and a letter. That leads to only
144 // twenty six temporary files that can be generated.
145 char pathname[] = "/tmp/llvm_XXXXXX";
146 char *TmpName = ::mktemp(pathname);
149 std::string(TmpName) + ": can't create unique directory name");
152 if (-1 == ::mkdir(TmpName, S_IRWXU)) {
154 std::string(TmpName) + ": can't create temporary directory");
157 return Path(TmpName);
159 // This is the worst case implementation. tempnam(3) leaks memory unless its
160 // on an SVID2 (or later) system. On BSD 4.3 it leaks. tmpnam(3) has thread
161 // issues. The mktemp(3) function doesn't have enough variability in the
162 // temporary name generated. So, we provide our own implementation that
163 // increments an integer from a random number seeded by the current time. This
164 // should be sufficiently unique that we don't have many collisions between
165 // processes. Generally LLVM processes don't run very long and don't use very
166 // many temporary files so this shouldn't be a big issue for LLVM.
167 static time_t num = ::time(0);
168 char pathname[MAXPATHLEN];
171 sprintf(pathname, "/tmp/llvm_%010u", unsigned(num));
172 } while ( 0 == access(pathname, F_OK ) );
173 if (-1 == ::mkdir(pathname, S_IRWXU)) {
175 std::string(pathname) + ": can't create temporary directory");
178 return Path(pathname);
183 Path::GetCurrentDirectory() {
184 char pathname[MAXPATHLEN];
185 if (!getcwd(pathname, MAXPATHLEN)) {
186 assert(false && "Could not query current working directory.");
190 return Path(pathname);
193 #if defined(__FreeBSD__) || defined (__NetBSD__) || defined(__Bitrig__) || \
194 defined(__OpenBSD__) || defined(__minix) || defined(__FreeBSD_kernel__) || \
195 defined(__linux__) || defined(__CYGWIN__)
197 test_dir(char buf[PATH_MAX], char ret[PATH_MAX],
198 const char *dir, const char *bin)
202 snprintf(buf, PATH_MAX, "%s/%s", dir, bin);
203 if (realpath(buf, ret) == NULL)
205 if (stat(buf, &sb) != 0)
212 getprogpath(char ret[PATH_MAX], const char *bin)
214 char *pv, *s, *t, buf[PATH_MAX];
216 /* First approach: absolute path. */
218 if (test_dir(buf, ret, "/", bin) == 0)
223 /* Second approach: relative path. */
224 if (strchr(bin, '/') != NULL) {
225 if (getcwd(buf, PATH_MAX) == NULL)
227 if (test_dir(buf, ret, buf, bin) == 0)
232 /* Third approach: $PATH */
233 if ((pv = getenv("PATH")) == NULL)
238 while ((t = strsep(&s, ":")) != NULL) {
239 if (test_dir(buf, ret, t, bin) == 0) {
247 #endif // __FreeBSD__ || __NetBSD__ || __FreeBSD_kernel__
249 /// GetMainExecutable - Return the path to the main executable, given the
250 /// value of argv[0] from program startup.
251 Path Path::GetMainExecutable(const char *argv0, void *MainAddr) {
252 #if defined(__APPLE__)
253 // On OS X the executable path is saved to the stack by dyld. Reading it
254 // from there is much faster than calling dladdr, especially for large
255 // binaries with symbols.
256 char exe_path[MAXPATHLEN];
257 uint32_t size = sizeof(exe_path);
258 if (_NSGetExecutablePath(exe_path, &size) == 0) {
259 char link_path[MAXPATHLEN];
260 if (realpath(exe_path, link_path))
261 return Path(link_path);
263 #elif defined(__FreeBSD__) || defined (__NetBSD__) || defined(__Bitrig__) || \
264 defined(__OpenBSD__) || defined(__minix) || defined(__FreeBSD_kernel__)
265 char exe_path[PATH_MAX];
267 if (getprogpath(exe_path, argv0) != NULL)
268 return Path(exe_path);
269 #elif defined(__linux__) || defined(__CYGWIN__)
270 char exe_path[MAXPATHLEN];
271 StringRef aPath("/proc/self/exe");
272 if (sys::fs::exists(aPath)) {
273 // /proc is not always mounted under Linux (chroot for example).
274 ssize_t len = readlink(aPath.str().c_str(), exe_path, sizeof(exe_path));
276 return Path(StringRef(exe_path, len));
278 // Fall back to the classical detection.
279 if (getprogpath(exe_path, argv0) != NULL)
280 return Path(exe_path);
282 #elif defined(HAVE_DLFCN_H)
283 // Use dladdr to get executable path if available.
285 int err = dladdr(MainAddr, &DLInfo);
289 // If the filename is a symlink, we need to resolve and return the location of
290 // the actual executable.
291 char link_path[MAXPATHLEN];
292 if (realpath(DLInfo.dli_fname, link_path))
293 return Path(link_path);
295 #error GetMainExecutable is not implemented on this host yet.
301 Path::exists() const {
302 return 0 == access(path.c_str(), F_OK );
306 Path::isDirectory() const {
308 if (0 != stat(path.c_str(), &buf))
310 return ((buf.st_mode & S_IFMT) == S_IFDIR) ? true : false;
314 Path::isSymLink() const {
316 if (0 != lstat(path.c_str(), &buf))
318 return S_ISLNK(buf.st_mode);
322 Path::isRegularFile() const {
323 // Get the status so we can determine if it's a file or directory
326 if (0 != stat(path.c_str(), &buf))
329 if (S_ISREG(buf.st_mode))
336 PathWithStatus::getFileStatus(bool update, std::string *ErrStr) const {
337 if (!fsIsValid || update) {
339 if (0 != stat(path.c_str(), &buf)) {
340 MakeErrMsg(ErrStr, path + ": can't get status of file");
343 status.fileSize = buf.st_size;
344 status.modTime.fromEpochTime(buf.st_mtime);
345 status.mode = buf.st_mode;
346 status.user = buf.st_uid;
347 status.group = buf.st_gid;
348 status.isDir = S_ISDIR(buf.st_mode);
349 status.isFile = S_ISREG(buf.st_mode);
355 static bool AddPermissionBits(const Path &File, int bits) {
356 // Get the umask value from the operating system. We want to use it
357 // when changing the file's permissions. Since calling umask() sets
358 // the umask and returns its old value, we must call it a second
359 // time to reset it to the user's preference.
360 int mask = umask(0777); // The arg. to umask is arbitrary.
361 umask(mask); // Restore the umask.
363 // Get the file's current mode.
365 if (0 != stat(File.c_str(), &buf))
367 // Change the file to have whichever permissions bits from 'bits'
368 // that the umask would not disable.
369 if ((chmod(File.c_str(), (buf.st_mode | (bits & ~mask)))) == -1)
374 bool Path::makeReadableOnDisk(std::string* ErrMsg) {
375 if (!AddPermissionBits(*this, 0444))
376 return MakeErrMsg(ErrMsg, path + ": can't make file readable");
380 bool Path::makeWriteableOnDisk(std::string* ErrMsg) {
381 if (!AddPermissionBits(*this, 0222))
382 return MakeErrMsg(ErrMsg, path + ": can't make file writable");
387 Path::set(StringRef a_path) {
395 Path::appendComponent(StringRef name) {
398 if (!lastIsSlash(path))
405 Path::eraseComponent() {
406 size_t slashpos = path.rfind('/',path.size());
407 if (slashpos == 0 || slashpos == std::string::npos) {
411 if (slashpos == path.size() - 1)
412 slashpos = path.rfind('/',slashpos-1);
413 if (slashpos == std::string::npos) {
417 path.erase(slashpos);
422 Path::eraseSuffix() {
423 size_t dotpos = path.rfind('.',path.size());
424 size_t slashpos = path.rfind('/',path.size());
425 if (dotpos != std::string::npos) {
426 if (slashpos == std::string::npos || dotpos > slashpos+1) {
427 path.erase(dotpos, path.size()-dotpos);
434 static bool createDirectoryHelper(char* beg, char* end, bool create_parents) {
436 if (access(beg, R_OK | W_OK) == 0)
439 if (create_parents) {
443 for (; c != beg; --c)
446 // Recurse to handling the parent directory.
448 bool x = createDirectoryHelper(beg, c, create_parents);
451 // Return if we encountered an error.
459 return mkdir(beg, S_IRWXU | S_IRWXG) != 0;
463 Path::createDirectoryOnDisk( bool create_parents, std::string* ErrMsg ) {
464 // Get a writeable copy of the path name
465 std::string pathname(path);
467 // Null-terminate the last component
468 size_t lastchar = path.length() - 1 ;
470 if (pathname[lastchar] != '/')
473 pathname[lastchar] = '\0';
475 if (createDirectoryHelper(&pathname[0], &pathname[lastchar], create_parents))
476 return MakeErrMsg(ErrMsg, pathname + ": can't create directory");
482 Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) {
483 // Make this into a unique file name
484 if (makeUnique( reuse_current, ErrMsg ))
488 int fd = ::open(path.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0666);
490 return MakeErrMsg(ErrMsg, path + ": can't create temporary file");
496 Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const {
497 // Get the status so we can determine if it's a file or directory.
499 if (0 != stat(path.c_str(), &buf)) {
500 MakeErrMsg(ErrStr, path + ": can't get status of file");
504 // Note: this check catches strange situations. In all cases, LLVM should
505 // only be involved in the creation and deletion of regular files. This
506 // check ensures that what we're trying to erase is a regular file. It
507 // effectively prevents LLVM from erasing things like /dev/null, any block
508 // special file, or other things that aren't "regular" files.
509 if (S_ISREG(buf.st_mode)) {
510 if (unlink(path.c_str()) != 0)
511 return MakeErrMsg(ErrStr, path + ": can't destroy file");
515 if (!S_ISDIR(buf.st_mode)) {
516 if (ErrStr) *ErrStr = "not a file or directory";
520 if (remove_contents) {
521 // Recursively descend the directory to remove its contents.
522 std::string cmd = "/bin/rm -rf " + path;
523 if (system(cmd.c_str()) != 0) {
524 MakeErrMsg(ErrStr, path + ": failed to recursively remove directory.");
530 // Otherwise, try to just remove the one directory.
531 std::string pathname(path);
532 size_t lastchar = path.length() - 1;
533 if (pathname[lastchar] == '/')
534 pathname[lastchar] = '\0';
536 pathname[lastchar+1] = '\0';
538 if (rmdir(pathname.c_str()) != 0)
539 return MakeErrMsg(ErrStr, pathname + ": can't erase directory");
544 Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) {
545 if (0 != ::rename(path.c_str(), newName.c_str()))
546 return MakeErrMsg(ErrMsg, std::string("can't rename '") + path + "' as '" +
547 newName.str() + "'");
552 Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrStr) const {
554 utb.actime = si.modTime.toPosixTime();
555 utb.modtime = utb.actime;
556 if (0 != ::utime(path.c_str(),&utb))
557 return MakeErrMsg(ErrStr, path + ": can't set file modification time");
558 if (0 != ::chmod(path.c_str(),si.mode))
559 return MakeErrMsg(ErrStr, path + ": can't set mode");
564 Path::makeUnique(bool reuse_current, std::string* ErrMsg) {
566 if (reuse_current && (fs::exists(path, Exists) || !Exists))
567 return false; // File doesn't exist already, just use it!
569 // Append an XXXXXX pattern to the end of the file for use with mkstemp,
570 // mktemp or our own implementation.
571 // This uses std::vector instead of SmallVector to avoid a dependence on
572 // libSupport. And performance isn't critical here.
573 std::vector<char> Buf;
574 Buf.resize(path.size()+8);
575 char *FNBuffer = &Buf[0];
576 path.copy(FNBuffer,path.size());
578 if (!fs::is_directory(path, isdir) && isdir)
579 strcpy(FNBuffer+path.size(), "/XXXXXX");
581 strcpy(FNBuffer+path.size(), "-XXXXXX");
583 #if defined(HAVE_MKSTEMP)
585 if ((TempFD = mkstemp(FNBuffer)) == -1)
586 return MakeErrMsg(ErrMsg, path + ": can't make unique filename");
588 // We don't need to hold the temp file descriptor... we will trust that no one
589 // will overwrite/delete the file before we can open it again.
595 // By default mkstemp sets the mode to 0600, so update mode bits now.
596 AddPermissionBits (*this, 0666);
597 #elif defined(HAVE_MKTEMP)
598 // If we don't have mkstemp, use the old and obsolete mktemp function.
599 if (mktemp(FNBuffer) == 0)
600 return MakeErrMsg(ErrMsg, path + ": can't make unique filename");
605 // Okay, looks like we have to do it all by our lonesome.
606 static unsigned FCounter = 0;
607 // Try to initialize with unique value.
608 if (FCounter == 0) FCounter = ((unsigned)getpid() & 0xFFFF) << 8;
609 char* pos = strstr(FNBuffer, "XXXXXX");
611 if (++FCounter > 0xFFFFFF) {
612 return MakeErrMsg(ErrMsg,
613 path + ": can't make unique filename: too many files");
615 sprintf(pos, "%06X", FCounter);
618 // POSSIBLE SECURITY BUG: An attacker can easily guess the name and exploit
623 } // end llvm namespace