1 //===- llvm/System/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 //===----------------------------------------------------------------------===//
19 #include "llvm/ADT/SmallVector.h"
27 #ifdef HAVE_SYS_MMAN_H
30 #ifdef HAVE_SYS_STAT_H
41 # define NAMLEN(dirent) strlen((dirent)->d_name)
43 # define dirent direct
44 # define NAMLEN(dirent) (dirent)->d_namlen
46 # include <sys/ndir.h>
61 #include <mach-o/dyld.h>
64 // Put in a hack for Cygwin which falsely reports that the mkdtemp function
65 // is available when it is not.
71 inline bool lastIsSlash(const std::string& path) {
72 return !path.empty() && path[path.length() - 1] == '/';
80 const char sys::PathSeparator = ':';
82 Path::Path(const std::string& p)
85 Path::Path(const char *StrStart, unsigned StrLen)
86 : path(StrStart, StrLen) {}
89 Path::operator=(const std::string &that) {
95 Path::isValid() const {
96 // Check some obvious things
99 return path.length() < MAXPATHLEN;
103 Path::isAbsolute(const char *NameStart, unsigned NameLen) {
107 return NameStart[0] == '/';
111 Path::isAbsolute() const {
114 return path[0] == '/';
117 void Path::makeAbsolute() {
121 Path CWD = Path::GetCurrentDirectory();
122 assert(CWD.isAbsolute() && "GetCurrentDirectory returned relative path!");
124 CWD.appendComponent(path);
130 Path::GetRootDirectory() {
137 Path::GetTemporaryDirectory(std::string *ErrMsg) {
138 #if defined(HAVE_MKDTEMP)
139 // The best way is with mkdtemp but that's not available on many systems,
140 // Linux and FreeBSD have it. Others probably won't.
141 char pathname[MAXPATHLEN];
142 strcpy(pathname,"/tmp/llvm_XXXXXX");
143 if (0 == mkdtemp(pathname)) {
145 std::string(pathname) + ": can't create temporary directory");
149 result.set(pathname);
150 assert(result.isValid() && "mkdtemp didn't create a valid pathname!");
152 #elif defined(HAVE_MKSTEMP)
153 // If no mkdtemp is available, mkstemp can be used to create a temporary file
154 // which is then removed and created as a directory. We prefer this over
155 // mktemp because of mktemp's inherent security and threading risks. We still
156 // have a slight race condition from the time the temporary file is created to
157 // the time it is re-created as a directoy.
158 char pathname[MAXPATHLEN];
159 strcpy(pathname, "/tmp/llvm_XXXXXX");
161 if (-1 == (fd = mkstemp(pathname))) {
163 std::string(pathname) + ": can't create temporary directory");
167 ::unlink(pathname); // start race condition, ignore errors
168 if (-1 == ::mkdir(pathname, S_IRWXU)) { // end race condition
170 std::string(pathname) + ": can't create temporary directory");
174 result.set(pathname);
175 assert(result.isValid() && "mkstemp didn't create a valid pathname!");
177 #elif defined(HAVE_MKTEMP)
178 // If a system doesn't have mkdtemp(3) or mkstemp(3) but it does have
179 // mktemp(3) then we'll assume that system (e.g. AIX) has a reasonable
180 // implementation of mktemp(3) and doesn't follow BSD 4.3's lead of replacing
181 // the XXXXXX with the pid of the process and a letter. That leads to only
182 // twenty six temporary files that can be generated.
183 char pathname[MAXPATHLEN];
184 strcpy(pathname, "/tmp/llvm_XXXXXX");
185 char *TmpName = ::mktemp(pathname);
188 std::string(TmpName) + ": can't create unique directory name");
191 if (-1 == ::mkdir(TmpName, S_IRWXU)) {
193 std::string(TmpName) + ": can't create temporary directory");
198 assert(result.isValid() && "mktemp didn't create a valid pathname!");
201 // This is the worst case implementation. tempnam(3) leaks memory unless its
202 // on an SVID2 (or later) system. On BSD 4.3 it leaks. tmpnam(3) has thread
203 // issues. The mktemp(3) function doesn't have enough variability in the
204 // temporary name generated. So, we provide our own implementation that
205 // increments an integer from a random number seeded by the current time. This
206 // should be sufficiently unique that we don't have many collisions between
207 // processes. Generally LLVM processes don't run very long and don't use very
208 // many temporary files so this shouldn't be a big issue for LLVM.
209 static time_t num = ::time(0);
210 char pathname[MAXPATHLEN];
213 sprintf(pathname, "/tmp/llvm_%010u", unsigned(num));
214 } while ( 0 == access(pathname, F_OK ) );
215 if (-1 == ::mkdir(pathname, S_IRWXU)) {
217 std::string(pathname) + ": can't create temporary directory");
221 result.set(pathname);
222 assert(result.isValid() && "mkstemp didn't create a valid pathname!");
228 Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
229 #ifdef LTDL_SHLIBPATH_VAR
230 char* env_var = getenv(LTDL_SHLIBPATH_VAR);
232 getPathList(env_var,Paths);
235 // FIXME: Should this look at LD_LIBRARY_PATH too?
236 Paths.push_back(sys::Path("/usr/local/lib/"));
237 Paths.push_back(sys::Path("/usr/X11R6/lib/"));
238 Paths.push_back(sys::Path("/usr/lib/"));
239 Paths.push_back(sys::Path("/lib/"));
243 Path::GetBitcodeLibraryPaths(std::vector<sys::Path>& Paths) {
244 char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
246 getPathList(env_var,Paths);
251 if (tmpPath.set(LLVM_LIBDIR))
252 if (tmpPath.canRead())
253 Paths.push_back(tmpPath);
256 GetSystemLibraryPaths(Paths);
260 Path::GetLLVMDefaultConfigDir() {
261 return Path("/etc/llvm/");
265 Path::GetUserHomeDirectory() {
266 const char* home = getenv("HOME");
269 if (result.set(home))
272 return GetRootDirectory();
276 Path::GetCurrentDirectory() {
277 char pathname[MAXPATHLEN];
278 if (!getcwd(pathname,MAXPATHLEN)) {
279 assert (false && "Could not query current working directory.");
283 return Path(pathname);
288 test_dir(char buf[PATH_MAX], char ret[PATH_MAX],
289 const char *dir, const char *bin)
293 snprintf(buf, PATH_MAX, "%s//%s", dir, bin);
294 if (realpath(buf, ret) == NULL)
296 if (stat(buf, &sb) != 0)
303 getprogpath(char ret[PATH_MAX], const char *bin)
305 char *pv, *s, *t, buf[PATH_MAX];
307 /* First approach: absolute path. */
309 if (test_dir(buf, ret, "/", bin) == 0)
314 /* Second approach: relative path. */
315 if (strchr(bin, '/') != NULL) {
316 if (getcwd(buf, PATH_MAX) == NULL)
318 if (test_dir(buf, ret, buf, bin) == 0)
323 /* Third approach: $PATH */
324 if ((pv = getenv("PATH")) == NULL)
329 while ((t = strsep(&s, ":")) != NULL) {
330 if (test_dir(buf, ret, t, bin) == 0) {
338 #endif // __FreeBSD__
340 /// GetMainExecutable - Return the path to the main executable, given the
341 /// value of argv[0] from program startup.
342 Path Path::GetMainExecutable(const char *argv0, void *MainAddr) {
343 #if defined(__APPLE__)
344 // On OS X the executable path is saved to the stack by dyld. Reading it
345 // from there is much faster than calling dladdr, especially for large
346 // binaries with symbols.
347 char exe_path[MAXPATHLEN];
348 uint32_t size = sizeof(exe_path);
349 if (_NSGetExecutablePath(exe_path, &size) == 0) {
350 char link_path[MAXPATHLEN];
351 if (realpath(exe_path, link_path))
352 return Path(std::string(link_path));
354 #elif defined(__FreeBSD__)
355 char exe_path[PATH_MAX];
357 if (getprogpath(exe_path, argv0) != NULL)
358 return Path(std::string(exe_path));
359 #elif defined(__linux__) || defined(__CYGWIN__)
360 char exe_path[MAXPATHLEN];
361 ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path));
363 return Path(std::string(exe_path, len));
364 #elif defined(HAVE_DLFCN_H)
365 // Use dladdr to get executable path if available.
367 int err = dladdr(MainAddr, &DLInfo);
371 // If the filename is a symlink, we need to resolve and return the location of
372 // the actual executable.
373 char link_path[MAXPATHLEN];
374 if (realpath(DLInfo.dli_fname, link_path))
375 return Path(std::string(link_path));
381 std::string Path::getDirname() const {
382 return getDirnameCharSep(path, '/');
386 Path::getBasename() const {
387 // Find the last slash
388 std::string::size_type slash = path.rfind('/');
389 if (slash == std::string::npos)
394 std::string::size_type dot = path.rfind('.');
395 if (dot == std::string::npos || dot < slash)
396 return path.substr(slash);
398 return path.substr(slash, dot - slash);
402 Path::getSuffix() const {
403 // Find the last slash
404 std::string::size_type slash = path.rfind('/');
405 if (slash == std::string::npos)
410 std::string::size_type dot = path.rfind('.');
411 if (dot == std::string::npos || dot < slash)
412 return std::string();
414 return path.substr(dot + 1);
417 bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
418 assert(len < 1024 && "Request for magic string too long");
419 SmallVector<char, 128> Buf;
421 char* buf = Buf.data();
422 int fd = ::open(path.c_str(), O_RDONLY);
425 ssize_t bytes_read = ::read(fd, buf, len);
427 if (ssize_t(len) != bytes_read) {
431 Magic.assign(buf,len);
436 Path::exists() const {
437 return 0 == access(path.c_str(), F_OK );
441 Path::isDirectory() const {
443 if (0 != stat(path.c_str(), &buf))
445 return buf.st_mode & S_IFDIR ? true : false;
449 Path::canRead() const {
450 return 0 == access(path.c_str(), R_OK);
454 Path::canWrite() const {
455 return 0 == access(path.c_str(), W_OK);
459 Path::isRegularFile() const {
460 // Get the status so we can determine if its a file or directory
463 if (0 != stat(path.c_str(), &buf))
466 if (S_ISREG(buf.st_mode))
473 Path::canExecute() const {
474 if (0 != access(path.c_str(), R_OK | X_OK ))
477 if (0 != stat(path.c_str(), &buf))
479 if (!S_ISREG(buf.st_mode))
485 Path::getLast() const {
486 // Find the last slash
487 size_t pos = path.rfind('/');
489 // Handle the corner cases
490 if (pos == std::string::npos)
493 // If the last character is a slash
494 if (pos == path.length()-1) {
495 // Find the second to last slash
496 size_t pos2 = path.rfind('/', pos-1);
497 if (pos2 == std::string::npos)
498 return path.substr(0,pos);
500 return path.substr(pos2+1,pos-pos2-1);
502 // Return everything after the last slash
503 return path.substr(pos+1);
507 PathWithStatus::getFileStatus(bool update, std::string *ErrStr) const {
508 if (!fsIsValid || update) {
510 if (0 != stat(path.c_str(), &buf)) {
511 MakeErrMsg(ErrStr, path + ": can't get status of file");
514 status.fileSize = buf.st_size;
515 status.modTime.fromEpochTime(buf.st_mtime);
516 status.mode = buf.st_mode;
517 status.user = buf.st_uid;
518 status.group = buf.st_gid;
519 status.uniqueID = uint64_t(buf.st_ino);
520 status.isDir = S_ISDIR(buf.st_mode);
521 status.isFile = S_ISREG(buf.st_mode);
527 static bool AddPermissionBits(const Path &File, int bits) {
528 // Get the umask value from the operating system. We want to use it
529 // when changing the file's permissions. Since calling umask() sets
530 // the umask and returns its old value, we must call it a second
531 // time to reset it to the user's preference.
532 int mask = umask(0777); // The arg. to umask is arbitrary.
533 umask(mask); // Restore the umask.
535 // Get the file's current mode.
537 if (0 != stat(File.c_str(), &buf))
539 // Change the file to have whichever permissions bits from 'bits'
540 // that the umask would not disable.
541 if ((chmod(File.c_str(), (buf.st_mode | (bits & ~mask)))) == -1)
546 bool Path::makeReadableOnDisk(std::string* ErrMsg) {
547 if (!AddPermissionBits(*this, 0444))
548 return MakeErrMsg(ErrMsg, path + ": can't make file readable");
552 bool Path::makeWriteableOnDisk(std::string* ErrMsg) {
553 if (!AddPermissionBits(*this, 0222))
554 return MakeErrMsg(ErrMsg, path + ": can't make file writable");
558 bool Path::makeExecutableOnDisk(std::string* ErrMsg) {
559 if (!AddPermissionBits(*this, 0111))
560 return MakeErrMsg(ErrMsg, path + ": can't make file executable");
565 Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const {
566 DIR* direntries = ::opendir(path.c_str());
568 return MakeErrMsg(ErrMsg, path + ": can't open directory");
570 std::string dirPath = path;
571 if (!lastIsSlash(dirPath))
575 struct dirent* de = ::readdir(direntries);
576 for ( ; de != 0; de = ::readdir(direntries)) {
577 if (de->d_name[0] != '.') {
578 Path aPath(dirPath + (const char*)de->d_name);
580 if (0 != lstat(aPath.path.c_str(), &st)) {
581 if (S_ISLNK(st.st_mode))
582 continue; // dangling symlink -- ignore
583 return MakeErrMsg(ErrMsg,
584 aPath.path + ": can't determine file object type");
586 result.insert(aPath);
590 closedir(direntries);
595 Path::set(const std::string& a_path) {
598 std::string save(path);
608 Path::appendComponent(const std::string& name) {
611 std::string save(path);
612 if (!lastIsSlash(path))
623 Path::eraseComponent() {
624 size_t slashpos = path.rfind('/',path.size());
625 if (slashpos == 0 || slashpos == std::string::npos) {
629 if (slashpos == path.size() - 1)
630 slashpos = path.rfind('/',slashpos-1);
631 if (slashpos == std::string::npos) {
635 path.erase(slashpos);
640 Path::appendSuffix(const std::string& suffix) {
641 std::string save(path);
652 Path::eraseSuffix() {
653 std::string save = path;
654 size_t dotpos = path.rfind('.',path.size());
655 size_t slashpos = path.rfind('/',path.size());
656 if (dotpos != std::string::npos) {
657 if (slashpos == std::string::npos || dotpos > slashpos+1) {
658 path.erase(dotpos, path.size()-dotpos);
667 static bool createDirectoryHelper(char* beg, char* end, bool create_parents) {
669 if (access(beg, R_OK | W_OK) == 0)
672 if (create_parents) {
676 for (; c != beg; --c)
679 // Recurse to handling the parent directory.
681 bool x = createDirectoryHelper(beg, c, create_parents);
684 // Return if we encountered an error.
692 return mkdir(beg, S_IRWXU | S_IRWXG) != 0;
696 Path::createDirectoryOnDisk( bool create_parents, std::string* ErrMsg ) {
697 // Get a writeable copy of the path name
698 char pathname[MAXPATHLEN];
699 path.copy(pathname,MAXPATHLEN);
701 // Null-terminate the last component
702 size_t lastchar = path.length() - 1 ;
704 if (pathname[lastchar] != '/')
707 pathname[lastchar] = 0;
709 if (createDirectoryHelper(pathname, pathname+lastchar, create_parents))
710 return MakeErrMsg(ErrMsg,
711 std::string(pathname) + ": can't create directory");
717 Path::createFileOnDisk(std::string* ErrMsg) {
719 int fd = ::creat(path.c_str(), S_IRUSR | S_IWUSR);
721 return MakeErrMsg(ErrMsg, path + ": can't create file");
727 Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) {
728 // Make this into a unique file name
729 if (makeUnique( reuse_current, ErrMsg ))
733 int fd = ::open(path.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0666);
735 return MakeErrMsg(ErrMsg, path + ": can't create temporary file");
741 Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const {
742 // Get the status so we can determine if its a file or directory
744 if (0 != stat(path.c_str(), &buf)) {
745 MakeErrMsg(ErrStr, path + ": can't get status of file");
749 // Note: this check catches strange situations. In all cases, LLVM should
750 // only be involved in the creation and deletion of regular files. This
751 // check ensures that what we're trying to erase is a regular file. It
752 // effectively prevents LLVM from erasing things like /dev/null, any block
753 // special file, or other things that aren't "regular" files.
754 if (S_ISREG(buf.st_mode)) {
755 if (unlink(path.c_str()) != 0)
756 return MakeErrMsg(ErrStr, path + ": can't destroy file");
760 if (!S_ISDIR(buf.st_mode)) {
761 if (ErrStr) *ErrStr = "not a file or directory";
765 if (remove_contents) {
766 // Recursively descend the directory to remove its contents.
767 std::string cmd = "/bin/rm -rf " + path;
768 if (system(cmd.c_str()) != 0) {
769 MakeErrMsg(ErrStr, path + ": failed to recursively remove directory.");
775 // Otherwise, try to just remove the one directory.
776 char pathname[MAXPATHLEN];
777 path.copy(pathname, MAXPATHLEN);
778 size_t lastchar = path.length() - 1;
779 if (pathname[lastchar] == '/')
780 pathname[lastchar] = 0;
782 pathname[lastchar+1] = 0;
784 if (rmdir(pathname) != 0)
785 return MakeErrMsg(ErrStr,
786 std::string(pathname) + ": can't erase directory");
791 Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) {
792 if (0 != ::rename(path.c_str(), newName.c_str()))
793 return MakeErrMsg(ErrMsg, std::string("can't rename '") + path + "' as '" +
794 newName.str() + "'");
799 Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrStr) const {
801 utb.actime = si.modTime.toPosixTime();
802 utb.modtime = utb.actime;
803 if (0 != ::utime(path.c_str(),&utb))
804 return MakeErrMsg(ErrStr, path + ": can't set file modification time");
805 if (0 != ::chmod(path.c_str(),si.mode))
806 return MakeErrMsg(ErrStr, path + ": can't set mode");
811 sys::CopyFile(const sys::Path &Dest, const sys::Path &Src, std::string* ErrMsg){
814 inFile = ::open(Src.c_str(), O_RDONLY);
816 return MakeErrMsg(ErrMsg, Src.str() +
817 ": can't open source file to copy");
819 outFile = ::open(Dest.c_str(), O_WRONLY|O_CREAT, 0666);
822 return MakeErrMsg(ErrMsg, Dest.str() +
823 ": can't create destination file for copy");
826 char Buffer[16*1024];
827 while (ssize_t Amt = ::read(inFile, Buffer, 16*1024)) {
829 if (errno != EINTR && errno != EAGAIN) {
832 return MakeErrMsg(ErrMsg, Src.str()+": can't read source file");
835 char *BufPtr = Buffer;
837 ssize_t AmtWritten = ::write(outFile, BufPtr, Amt);
838 if (AmtWritten == -1) {
839 if (errno != EINTR && errno != EAGAIN) {
842 return MakeErrMsg(ErrMsg, Dest.str() +
843 ": can't write destination file");
847 BufPtr += AmtWritten;
858 Path::makeUnique(bool reuse_current, std::string* ErrMsg) {
859 if (reuse_current && !exists())
860 return false; // File doesn't exist already, just use it!
862 // Append an XXXXXX pattern to the end of the file for use with mkstemp,
863 // mktemp or our own implementation.
864 SmallVector<char, 128> Buf;
865 Buf.resize(path.size()+8);
866 char *FNBuffer = Buf.data();
867 path.copy(FNBuffer,path.size());
869 strcpy(FNBuffer+path.size(), "/XXXXXX");
871 strcpy(FNBuffer+path.size(), "-XXXXXX");
873 #if defined(HAVE_MKSTEMP)
875 if ((TempFD = mkstemp(FNBuffer)) == -1)
876 return MakeErrMsg(ErrMsg, path + ": can't make unique filename");
878 // We don't need to hold the temp file descriptor... we will trust that no one
879 // will overwrite/delete the file before we can open it again.
884 #elif defined(HAVE_MKTEMP)
885 // If we don't have mkstemp, use the old and obsolete mktemp function.
886 if (mktemp(FNBuffer) == 0)
887 return MakeErrMsg(ErrMsg, path + ": can't make unique filename");
892 // Okay, looks like we have to do it all by our lonesome.
893 static unsigned FCounter = 0;
894 unsigned offset = path.size() + 1;
895 while ( FCounter < 999999 && exists()) {
896 sprintf(FNBuffer+offset,"%06u",++FCounter);
899 if (FCounter > 999999)
900 return MakeErrMsg(ErrMsg,
901 path + ": can't make unique filename: too many files");
906 const char *Path::MapInFilePages(int FD, uint64_t FileSize) {
907 int Flags = MAP_PRIVATE;
911 void *BasePtr = ::mmap(0, FileSize, PROT_READ, Flags, FD, 0);
912 if (BasePtr == MAP_FAILED)
914 return (const char*)BasePtr;
917 void Path::UnMapFilePages(const char *BasePtr, uint64_t FileSize) {
918 ::munmap((void*)BasePtr, FileSize);
921 } // end llvm namespace