Move GetEXESuffix to the one place it is used.
[oota-llvm.git] / lib / Support / Unix / Path.inc
1 //===- llvm/Support/Unix/Path.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 portion of the Path class.
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 #if HAVE_SYS_STAT_H
21 #include <sys/stat.h>
22 #endif
23 #if HAVE_FCNTL_H
24 #include <fcntl.h>
25 #endif
26 #ifdef HAVE_SYS_MMAN_H
27 #include <sys/mman.h>
28 #endif
29 #ifdef HAVE_SYS_STAT_H
30 #include <sys/stat.h>
31 #endif
32 #if HAVE_UTIME_H
33 #include <utime.h>
34 #endif
35 #if HAVE_TIME_H
36 #include <time.h>
37 #endif
38 #if HAVE_DIRENT_H
39 # include <dirent.h>
40 # define NAMLEN(dirent) strlen((dirent)->d_name)
41 #else
42 # define dirent direct
43 # define NAMLEN(dirent) (dirent)->d_namlen
44 # if HAVE_SYS_NDIR_H
45 #  include <sys/ndir.h>
46 # endif
47 # if HAVE_SYS_DIR_H
48 #  include <sys/dir.h>
49 # endif
50 # if HAVE_NDIR_H
51 #  include <ndir.h>
52 # endif
53 #endif
54
55 #if HAVE_DLFCN_H
56 #include <dlfcn.h>
57 #endif
58
59 #ifdef __APPLE__
60 #include <mach-o/dyld.h>
61 #endif
62
63 // For GNU Hurd
64 #if defined(__GNU__) && !defined(MAXPATHLEN)
65 # define MAXPATHLEN 4096
66 #endif
67
68 // Put in a hack for Cygwin which falsely reports that the mkdtemp function
69 // is available when it is not.
70 #ifdef __CYGWIN__
71 # undef HAVE_MKDTEMP
72 #endif
73
74 namespace {
75 inline bool lastIsSlash(const std::string& path) {
76   return !path.empty() && path[path.length() - 1] == '/';
77 }
78
79 }
80
81 namespace llvm {
82 using namespace sys;
83
84 Path::Path(StringRef p)
85   : path(p) {}
86
87 Path::Path(const char *StrStart, unsigned StrLen)
88   : path(StrStart, StrLen) {}
89
90 Path&
91 Path::operator=(StringRef that) {
92   path.assign(that.data(), that.size());
93   return *this;
94 }
95
96 bool
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
102   // ENAMETOLONG.
103   return !path.empty();
104 }
105
106 Path
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)) {
113     MakeErrMsg(ErrMsg,
114                std::string(pathname) + ": can't create temporary directory");
115     return Path();
116   }
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";
125   int fd = 0;
126   if (-1 == (fd = mkstemp(pathname))) {
127     MakeErrMsg(ErrMsg,
128       std::string(pathname) + ": can't create temporary directory");
129     return Path();
130   }
131   ::close(fd);
132   ::unlink(pathname); // start race condition, ignore errors
133   if (-1 == ::mkdir(pathname, S_IRWXU)) { // end race condition
134     MakeErrMsg(ErrMsg,
135       std::string(pathname) + ": can't create temporary directory");
136     return Path();
137   }
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);
147   if (TmpName == 0) {
148     MakeErrMsg(ErrMsg,
149       std::string(TmpName) + ": can't create unique directory name");
150     return Path();
151   }
152   if (-1 == ::mkdir(TmpName, S_IRWXU)) {
153     MakeErrMsg(ErrMsg,
154         std::string(TmpName) + ": can't create temporary directory");
155     return Path();
156   }
157   return Path(TmpName);
158 #else
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];
169   do {
170     num++;
171     sprintf(pathname, "/tmp/llvm_%010u", unsigned(num));
172   } while ( 0 == access(pathname, F_OK ) );
173   if (-1 == ::mkdir(pathname, S_IRWXU)) {
174     MakeErrMsg(ErrMsg,
175       std::string(pathname) + ": can't create temporary directory");
176     return Path();
177   }
178   return Path(pathname);
179 #endif
180 }
181
182 Path
183 Path::GetCurrentDirectory() {
184   char pathname[MAXPATHLEN];
185   if (!getcwd(pathname, MAXPATHLEN)) {
186     assert(false && "Could not query current working directory.");
187     return Path();
188   }
189
190   return Path(pathname);
191 }
192
193 #if defined(__FreeBSD__) || defined (__NetBSD__) || defined(__Bitrig__) || \
194     defined(__OpenBSD__) || defined(__minix) || defined(__FreeBSD_kernel__) || \
195     defined(__linux__) || defined(__CYGWIN__)
196 static int
197 test_dir(char buf[PATH_MAX], char ret[PATH_MAX],
198     const char *dir, const char *bin)
199 {
200   struct stat sb;
201
202   snprintf(buf, PATH_MAX, "%s/%s", dir, bin);
203   if (realpath(buf, ret) == NULL)
204     return (1);
205   if (stat(buf, &sb) != 0)
206     return (1);
207
208   return (0);
209 }
210
211 static char *
212 getprogpath(char ret[PATH_MAX], const char *bin)
213 {
214   char *pv, *s, *t, buf[PATH_MAX];
215
216   /* First approach: absolute path. */
217   if (bin[0] == '/') {
218     if (test_dir(buf, ret, "/", bin) == 0)
219       return (ret);
220     return (NULL);
221   }
222
223   /* Second approach: relative path. */
224   if (strchr(bin, '/') != NULL) {
225     if (getcwd(buf, PATH_MAX) == NULL)
226       return (NULL);
227     if (test_dir(buf, ret, buf, bin) == 0)
228       return (ret);
229     return (NULL);
230   }
231
232   /* Third approach: $PATH */
233   if ((pv = getenv("PATH")) == NULL)
234     return (NULL);
235   s = pv = strdup(pv);
236   if (pv == NULL)
237     return (NULL);
238   while ((t = strsep(&s, ":")) != NULL) {
239     if (test_dir(buf, ret, t, bin) == 0) {
240       free(pv);
241       return (ret);
242     }
243   }
244   free(pv);
245   return (NULL);
246 }
247 #endif // __FreeBSD__ || __NetBSD__ || __FreeBSD_kernel__
248
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);
262   }
263 #elif defined(__FreeBSD__) || defined (__NetBSD__) || defined(__Bitrig__) || \
264       defined(__OpenBSD__) || defined(__minix) || defined(__FreeBSD_kernel__)
265   char exe_path[PATH_MAX];
266
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));
275       if (len >= 0)
276           return Path(StringRef(exe_path, len));
277   } else {
278       // Fall back to the classical detection.
279       if (getprogpath(exe_path, argv0) != NULL)
280           return Path(exe_path);
281   }
282 #elif defined(HAVE_DLFCN_H)
283   // Use dladdr to get executable path if available.
284   Dl_info DLInfo;
285   int err = dladdr(MainAddr, &DLInfo);
286   if (err == 0)
287     return Path();
288
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);
294 #else
295 #error GetMainExecutable is not implemented on this host yet.
296 #endif
297   return Path();
298 }
299
300 bool
301 Path::exists() const {
302   return 0 == access(path.c_str(), F_OK );
303 }
304
305 bool
306 Path::isDirectory() const {
307   struct stat buf;
308   if (0 != stat(path.c_str(), &buf))
309     return false;
310   return ((buf.st_mode & S_IFMT) == S_IFDIR) ? true : false;
311 }
312
313 bool
314 Path::isSymLink() const {
315   struct stat buf;
316   if (0 != lstat(path.c_str(), &buf))
317     return false;
318   return S_ISLNK(buf.st_mode);
319 }
320
321 bool
322 Path::isRegularFile() const {
323   // Get the status so we can determine if it's a file or directory
324   struct stat buf;
325
326   if (0 != stat(path.c_str(), &buf))
327     return false;
328
329   if (S_ISREG(buf.st_mode))
330     return true;
331
332   return false;
333 }
334
335 const FileStatus *
336 PathWithStatus::getFileStatus(bool update, std::string *ErrStr) const {
337   if (!fsIsValid || update) {
338     struct stat buf;
339     if (0 != stat(path.c_str(), &buf)) {
340       MakeErrMsg(ErrStr, path + ": can't get status of file");
341       return 0;
342     }
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);
350     fsIsValid = true;
351   }
352   return &status;
353 }
354
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.
362
363   // Get the file's current mode.
364   struct stat buf;
365   if (0 != stat(File.c_str(), &buf))
366     return false;
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)
370       return false;
371   return true;
372 }
373
374 bool Path::makeReadableOnDisk(std::string* ErrMsg) {
375   if (!AddPermissionBits(*this, 0444))
376     return MakeErrMsg(ErrMsg, path + ": can't make file readable");
377   return false;
378 }
379
380 bool Path::makeWriteableOnDisk(std::string* ErrMsg) {
381   if (!AddPermissionBits(*this, 0222))
382     return MakeErrMsg(ErrMsg, path + ": can't make file writable");
383   return false;
384 }
385
386 bool
387 Path::set(StringRef a_path) {
388   if (a_path.empty())
389     return false;
390   path = a_path;
391   return true;
392 }
393
394 bool
395 Path::appendComponent(StringRef name) {
396   if (name.empty())
397     return false;
398   if (!lastIsSlash(path))
399     path += '/';
400   path += name;
401   return true;
402 }
403
404 bool
405 Path::eraseComponent() {
406   size_t slashpos = path.rfind('/',path.size());
407   if (slashpos == 0 || slashpos == std::string::npos) {
408     path.erase();
409     return true;
410   }
411   if (slashpos == path.size() - 1)
412     slashpos = path.rfind('/',slashpos-1);
413   if (slashpos == std::string::npos) {
414     path.erase();
415     return true;
416   }
417   path.erase(slashpos);
418   return true;
419 }
420
421 bool
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);
428       return true;
429     }
430   }
431   return false;
432 }
433
434 static bool createDirectoryHelper(char* beg, char* end, bool create_parents) {
435
436   if (access(beg, R_OK | W_OK) == 0)
437     return false;
438
439   if (create_parents) {
440
441     char* c = end;
442
443     for (; c != beg; --c)
444       if (*c == '/') {
445
446         // Recurse to handling the parent directory.
447         *c = '\0';
448         bool x = createDirectoryHelper(beg, c, create_parents);
449         *c = '/';
450
451         // Return if we encountered an error.
452         if (x)
453           return true;
454
455         break;
456       }
457   }
458
459   return mkdir(beg, S_IRWXU | S_IRWXG) != 0;
460 }
461
462 bool
463 Path::createDirectoryOnDisk( bool create_parents, std::string* ErrMsg ) {
464   // Get a writeable copy of the path name
465   std::string pathname(path);
466
467   // Null-terminate the last component
468   size_t lastchar = path.length() - 1 ;
469
470   if (pathname[lastchar] != '/')
471     ++lastchar;
472
473   pathname[lastchar] = '\0';
474
475   if (createDirectoryHelper(&pathname[0], &pathname[lastchar], create_parents))
476     return MakeErrMsg(ErrMsg, pathname + ": can't create directory");
477
478   return false;
479 }
480
481 bool
482 Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) {
483   // Make this into a unique file name
484   if (makeUnique( reuse_current, ErrMsg ))
485     return true;
486
487   // create the file
488   int fd = ::open(path.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0666);
489   if (fd < 0)
490     return MakeErrMsg(ErrMsg, path + ": can't create temporary file");
491   ::close(fd);
492   return false;
493 }
494
495 bool
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.
498   struct stat buf;
499   if (0 != stat(path.c_str(), &buf)) {
500     MakeErrMsg(ErrStr, path + ": can't get status of file");
501     return true;
502   }
503
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");
512     return false;
513   }
514
515   if (!S_ISDIR(buf.st_mode)) {
516     if (ErrStr) *ErrStr = "not a file or directory";
517     return true;
518   }
519
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.");
525       return true;
526     }
527     return false;
528   }
529
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';
535   else
536     pathname[lastchar+1] = '\0';
537
538   if (rmdir(pathname.c_str()) != 0)
539     return MakeErrMsg(ErrStr, pathname + ": can't erase directory");
540   return false;
541 }
542
543 bool
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() + "'");
548   return false;
549 }
550
551 bool
552 Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrStr) const {
553   struct utimbuf utb;
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");
560   return false;
561 }
562
563 bool
564 Path::makeUnique(bool reuse_current, std::string* ErrMsg) {
565   bool Exists;
566   if (reuse_current && (fs::exists(path, Exists) || !Exists))
567     return false; // File doesn't exist already, just use it!
568
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());
577   bool isdir;
578   if (!fs::is_directory(path, isdir) && isdir)
579     strcpy(FNBuffer+path.size(), "/XXXXXX");
580   else
581     strcpy(FNBuffer+path.size(), "-XXXXXX");
582
583 #if defined(HAVE_MKSTEMP)
584   int TempFD;
585   if ((TempFD = mkstemp(FNBuffer)) == -1)
586     return MakeErrMsg(ErrMsg, path + ": can't make unique filename");
587
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.
590   close(TempFD);
591
592   // Save the name
593   path = FNBuffer;
594
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");
601
602   // Save the name
603   path = FNBuffer;
604 #else
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");
610   do {
611     if (++FCounter > 0xFFFFFF) {
612       return MakeErrMsg(ErrMsg,
613         path + ": can't make unique filename: too many files");
614     }
615     sprintf(pos, "%06X", FCounter);
616     path = FNBuffer;
617   } while (exists());
618   // POSSIBLE SECURITY BUG: An attacker can easily guess the name and exploit
619   // LLVM.
620 #endif
621   return false;
622 }
623 } // end llvm namespace