• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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# 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
63using namespace llvm;
64
65namespace {
66  /// This class automatically closes the given file descriptor when it goes out
67  /// of scope. You can take back explicit ownership of the file descriptor by
68  /// calling take(). The destructor does not verify that close was successful.
69  /// Therefore, never allow this class to call close on a file descriptor that
70  /// has been read from or written to.
71  struct AutoFD {
72    int FileDescriptor;
73
74    AutoFD(int fd) : FileDescriptor(fd) {}
75    ~AutoFD() {
76      if (FileDescriptor >= 0)
77        ::close(FileDescriptor);
78    }
79
80    int take() {
81      int ret = FileDescriptor;
82      FileDescriptor = -1;
83      return ret;
84    }
85
86    operator int() const {return FileDescriptor;}
87  };
88}
89
90static std::error_code TempDir(SmallVectorImpl<char> &result) {
91  // FIXME: Don't use TMPDIR if program is SUID or SGID enabled.
92  const char *dir = nullptr;
93  (dir = std::getenv("TMPDIR")) || (dir = std::getenv("TMP")) ||
94      (dir = std::getenv("TEMP")) || (dir = std::getenv("TEMPDIR")) ||
95#ifdef P_tmpdir
96      (dir = P_tmpdir) ||
97#endif
98      (dir = "/tmp");
99
100  result.clear();
101  StringRef d(dir);
102  result.append(d.begin(), d.end());
103  return std::error_code();
104}
105
106namespace llvm {
107namespace sys  {
108namespace fs {
109#if defined(__FreeBSD__) || defined (__NetBSD__) || defined(__Bitrig__) || \
110    defined(__OpenBSD__) || defined(__minix) || defined(__FreeBSD_kernel__) || \
111    defined(__linux__) || defined(__CYGWIN__) || defined(__DragonFly__)
112static int
113test_dir(char ret[PATH_MAX], const char *dir, const char *bin)
114{
115  struct stat sb;
116  char fullpath[PATH_MAX];
117
118  snprintf(fullpath, PATH_MAX, "%s/%s", dir, bin);
119  if (realpath(fullpath, ret) == NULL)
120    return (1);
121  if (stat(fullpath, &sb) != 0)
122    return (1);
123
124  return (0);
125}
126
127static char *
128getprogpath(char ret[PATH_MAX], const char *bin)
129{
130  char *pv, *s, *t;
131
132  /* First approach: absolute path. */
133  if (bin[0] == '/') {
134    if (test_dir(ret, "/", bin) == 0)
135      return (ret);
136    return (NULL);
137  }
138
139  /* Second approach: relative path. */
140  if (strchr(bin, '/') != NULL) {
141    char cwd[PATH_MAX];
142    if (getcwd(cwd, PATH_MAX) == NULL)
143      return (NULL);
144    if (test_dir(ret, cwd, bin) == 0)
145      return (ret);
146    return (NULL);
147  }
148
149  /* Third approach: $PATH */
150  if ((pv = getenv("PATH")) == NULL)
151    return (NULL);
152  s = pv = strdup(pv);
153  if (pv == NULL)
154    return (NULL);
155  while ((t = strsep(&s, ":")) != NULL) {
156    if (test_dir(ret, t, bin) == 0) {
157      free(pv);
158      return (ret);
159    }
160  }
161  free(pv);
162  return (NULL);
163}
164#endif // __FreeBSD__ || __NetBSD__ || __FreeBSD_kernel__
165
166/// GetMainExecutable - Return the path to the main executable, given the
167/// value of argv[0] from program startup.
168std::string getMainExecutable(const char *argv0, void *MainAddr) {
169#if defined(__APPLE__)
170  // On OS X the executable path is saved to the stack by dyld. Reading it
171  // from there is much faster than calling dladdr, especially for large
172  // binaries with symbols.
173  char exe_path[MAXPATHLEN];
174  uint32_t size = sizeof(exe_path);
175  if (_NSGetExecutablePath(exe_path, &size) == 0) {
176    char link_path[MAXPATHLEN];
177    if (realpath(exe_path, link_path))
178      return link_path;
179  }
180#elif defined(__FreeBSD__) || defined (__NetBSD__) || defined(__Bitrig__) || \
181      defined(__OpenBSD__) || defined(__minix) || defined(__DragonFly__) || \
182      defined(__FreeBSD_kernel__)
183  char exe_path[PATH_MAX];
184
185  if (getprogpath(exe_path, argv0) != NULL)
186    return exe_path;
187#elif defined(__linux__) || defined(__CYGWIN__)
188  char exe_path[MAXPATHLEN];
189  StringRef aPath("/proc/self/exe");
190  if (sys::fs::exists(aPath)) {
191      // /proc is not always mounted under Linux (chroot for example).
192      ssize_t len = readlink(aPath.str().c_str(), exe_path, sizeof(exe_path));
193      if (len >= 0)
194          return StringRef(exe_path, len);
195  } else {
196      // Fall back to the classical detection.
197      if (getprogpath(exe_path, argv0) != NULL)
198          return exe_path;
199  }
200#elif defined(HAVE_DLFCN_H)
201  // Use dladdr to get executable path if available.
202  Dl_info DLInfo;
203  int err = dladdr(MainAddr, &DLInfo);
204  if (err == 0)
205    return "";
206
207  // If the filename is a symlink, we need to resolve and return the location of
208  // the actual executable.
209  char link_path[MAXPATHLEN];
210  if (realpath(DLInfo.dli_fname, link_path))
211    return link_path;
212#else
213#error GetMainExecutable is not implemented on this host yet.
214#endif
215  return "";
216}
217
218TimeValue file_status::getLastModificationTime() const {
219  TimeValue Ret;
220  Ret.fromEpochTime(fs_st_mtime);
221  return Ret;
222}
223
224UniqueID file_status::getUniqueID() const {
225  return UniqueID(fs_st_dev, fs_st_ino);
226}
227
228std::error_code current_path(SmallVectorImpl<char> &result) {
229  result.clear();
230
231  const char *pwd = ::getenv("PWD");
232  llvm::sys::fs::file_status PWDStatus, DotStatus;
233  if (pwd && llvm::sys::path::is_absolute(pwd) &&
234      !llvm::sys::fs::status(pwd, PWDStatus) &&
235      !llvm::sys::fs::status(".", DotStatus) &&
236      PWDStatus.getUniqueID() == DotStatus.getUniqueID()) {
237    result.append(pwd, pwd + strlen(pwd));
238    return std::error_code();
239  }
240
241#ifdef MAXPATHLEN
242  result.reserve(MAXPATHLEN);
243#else
244// For GNU Hurd
245  result.reserve(1024);
246#endif
247
248  while (true) {
249    if (::getcwd(result.data(), result.capacity()) == nullptr) {
250      // See if there was a real error.
251      if (errno != ENOMEM)
252        return std::error_code(errno, std::generic_category());
253      // Otherwise there just wasn't enough space.
254      result.reserve(result.capacity() * 2);
255    } else
256      break;
257  }
258
259  result.set_size(strlen(result.data()));
260  return std::error_code();
261}
262
263std::error_code create_directory(const Twine &path, bool IgnoreExisting) {
264  SmallString<128> path_storage;
265  StringRef p = path.toNullTerminatedStringRef(path_storage);
266
267  if (::mkdir(p.begin(), S_IRWXU | S_IRWXG) == -1) {
268    if (errno != EEXIST || !IgnoreExisting)
269      return std::error_code(errno, std::generic_category());
270  }
271
272  return std::error_code();
273}
274
275std::error_code normalize_separators(SmallVectorImpl<char> &Path) {
276  for (auto PI = Path.begin(), PE = Path.end(); PI < PE; ++PI) {
277    if (*PI == '\\') {
278      auto PN = PI + 1;
279      if (PN < PE && *PN == '\\')
280        ++PI; // increment once, the for loop will move over the escaped slash
281      else
282        *PI = '/';
283    }
284  }
285  return std::error_code();
286}
287
288// Note that we are using symbolic link because hard links are not supported by
289// all filesystems (SMB doesn't).
290std::error_code create_link(const Twine &to, const Twine &from) {
291  // Get arguments.
292  SmallString<128> from_storage;
293  SmallString<128> to_storage;
294  StringRef f = from.toNullTerminatedStringRef(from_storage);
295  StringRef t = to.toNullTerminatedStringRef(to_storage);
296
297  if (::symlink(t.begin(), f.begin()) == -1)
298    return std::error_code(errno, std::generic_category());
299
300  return std::error_code();
301}
302
303std::error_code remove(const Twine &path, bool IgnoreNonExisting) {
304  SmallString<128> path_storage;
305  StringRef p = path.toNullTerminatedStringRef(path_storage);
306
307  struct stat buf;
308  if (lstat(p.begin(), &buf) != 0) {
309    if (errno != ENOENT || !IgnoreNonExisting)
310      return std::error_code(errno, std::generic_category());
311    return std::error_code();
312  }
313
314  // Note: this check catches strange situations. In all cases, LLVM should
315  // only be involved in the creation and deletion of regular files.  This
316  // check ensures that what we're trying to erase is a regular file. It
317  // effectively prevents LLVM from erasing things like /dev/null, any block
318  // special file, or other things that aren't "regular" files.
319  if (!S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode) && !S_ISLNK(buf.st_mode))
320    return make_error_code(errc::operation_not_permitted);
321
322  if (::remove(p.begin()) == -1) {
323    if (errno != ENOENT || !IgnoreNonExisting)
324      return std::error_code(errno, std::generic_category());
325  }
326
327  return std::error_code();
328}
329
330std::error_code rename(const Twine &from, const Twine &to) {
331  // Get arguments.
332  SmallString<128> from_storage;
333  SmallString<128> to_storage;
334  StringRef f = from.toNullTerminatedStringRef(from_storage);
335  StringRef t = to.toNullTerminatedStringRef(to_storage);
336
337  if (::rename(f.begin(), t.begin()) == -1)
338    return std::error_code(errno, std::generic_category());
339
340  return std::error_code();
341}
342
343std::error_code resize_file(const Twine &path, uint64_t size) {
344  SmallString<128> path_storage;
345  StringRef p = path.toNullTerminatedStringRef(path_storage);
346
347  if (::truncate(p.begin(), size) == -1)
348    return std::error_code(errno, std::generic_category());
349
350  return std::error_code();
351}
352
353std::error_code exists(const Twine &path, bool &result) {
354  SmallString<128> path_storage;
355  StringRef p = path.toNullTerminatedStringRef(path_storage);
356
357  if (::access(p.begin(), F_OK) == -1) {
358    if (errno != ENOENT)
359      return std::error_code(errno, std::generic_category());
360    result = false;
361  } else
362    result = true;
363
364  return std::error_code();
365}
366
367bool can_write(const Twine &Path) {
368  SmallString<128> PathStorage;
369  StringRef P = Path.toNullTerminatedStringRef(PathStorage);
370  return 0 == access(P.begin(), W_OK);
371}
372
373bool can_execute(const Twine &Path) {
374  SmallString<128> PathStorage;
375  StringRef P = Path.toNullTerminatedStringRef(PathStorage);
376
377  if (0 != access(P.begin(), R_OK | X_OK))
378    return false;
379  struct stat buf;
380  if (0 != stat(P.begin(), &buf))
381    return false;
382  if (!S_ISREG(buf.st_mode))
383    return false;
384  return true;
385}
386
387bool equivalent(file_status A, file_status B) {
388  assert(status_known(A) && status_known(B));
389  return A.fs_st_dev == B.fs_st_dev &&
390         A.fs_st_ino == B.fs_st_ino;
391}
392
393std::error_code equivalent(const Twine &A, const Twine &B, bool &result) {
394  file_status fsA, fsB;
395  if (std::error_code ec = status(A, fsA))
396    return ec;
397  if (std::error_code ec = status(B, fsB))
398    return ec;
399  result = equivalent(fsA, fsB);
400  return std::error_code();
401}
402
403static std::error_code fillStatus(int StatRet, const struct stat &Status,
404                             file_status &Result) {
405  if (StatRet != 0) {
406    std::error_code ec(errno, std::generic_category());
407    if (ec == errc::no_such_file_or_directory)
408      Result = file_status(file_type::file_not_found);
409    else
410      Result = file_status(file_type::status_error);
411    return ec;
412  }
413
414  file_type Type = file_type::type_unknown;
415
416  if (S_ISDIR(Status.st_mode))
417    Type = file_type::directory_file;
418  else if (S_ISREG(Status.st_mode))
419    Type = file_type::regular_file;
420  else if (S_ISBLK(Status.st_mode))
421    Type = file_type::block_file;
422  else if (S_ISCHR(Status.st_mode))
423    Type = file_type::character_file;
424  else if (S_ISFIFO(Status.st_mode))
425    Type = file_type::fifo_file;
426  else if (S_ISSOCK(Status.st_mode))
427    Type = file_type::socket_file;
428
429  perms Perms = static_cast<perms>(Status.st_mode);
430  Result =
431      file_status(Type, Perms, Status.st_dev, Status.st_ino, Status.st_mtime,
432                  Status.st_uid, Status.st_gid, Status.st_size);
433
434  return std::error_code();
435}
436
437std::error_code status(const Twine &Path, file_status &Result) {
438  SmallString<128> PathStorage;
439  StringRef P = Path.toNullTerminatedStringRef(PathStorage);
440
441  struct stat Status;
442  int StatRet = ::stat(P.begin(), &Status);
443  return fillStatus(StatRet, Status, Result);
444}
445
446std::error_code status(int FD, file_status &Result) {
447  struct stat Status;
448  int StatRet = ::fstat(FD, &Status);
449  return fillStatus(StatRet, Status, Result);
450}
451
452std::error_code setLastModificationAndAccessTime(int FD, TimeValue Time) {
453#if defined(HAVE_FUTIMENS)
454  timespec Times[2];
455  Times[0].tv_sec = Time.toEpochTime();
456  Times[0].tv_nsec = 0;
457  Times[1] = Times[0];
458  if (::futimens(FD, Times))
459    return std::error_code(errno, std::generic_category());
460  return std::error_code();
461#elif defined(HAVE_FUTIMES)
462  timeval Times[2];
463  Times[0].tv_sec = Time.toEpochTime();
464  Times[0].tv_usec = 0;
465  Times[1] = Times[0];
466  if (::futimes(FD, Times))
467    return std::error_code(errno, std::generic_category());
468  return std::error_code();
469#else
470#warning Missing futimes() and futimens()
471  return make_error_code(errc::function_not_supported);
472#endif
473}
474
475std::error_code mapped_file_region::init(int FD, bool CloseFD, uint64_t Offset) {
476  AutoFD ScopedFD(FD);
477  if (!CloseFD)
478    ScopedFD.take();
479
480  // Figure out how large the file is.
481  struct stat FileInfo;
482  if (fstat(FD, &FileInfo) == -1)
483    return std::error_code(errno, std::generic_category());
484  uint64_t FileSize = FileInfo.st_size;
485
486  if (Size == 0)
487    Size = FileSize;
488  else if (FileSize < Size) {
489    // We need to grow the file.
490    if (ftruncate(FD, Size) == -1)
491      return std::error_code(errno, std::generic_category());
492  }
493
494  int flags = (Mode == readwrite) ? MAP_SHARED : MAP_PRIVATE;
495  int prot = (Mode == readonly) ? PROT_READ : (PROT_READ | PROT_WRITE);
496#ifdef MAP_FILE
497  flags |= MAP_FILE;
498#endif
499  Mapping = ::mmap(nullptr, Size, prot, flags, FD, Offset);
500  if (Mapping == MAP_FAILED)
501    return std::error_code(errno, std::generic_category());
502  return std::error_code();
503}
504
505mapped_file_region::mapped_file_region(const Twine &path,
506                                       mapmode mode,
507                                       uint64_t length,
508                                       uint64_t offset,
509                                       std::error_code &ec)
510  : Mode(mode)
511  , Size(length)
512  , Mapping() {
513  // Make sure that the requested size fits within SIZE_T.
514  if (length > std::numeric_limits<size_t>::max()) {
515    ec = make_error_code(errc::invalid_argument);
516    return;
517  }
518
519  SmallString<128> path_storage;
520  StringRef name = path.toNullTerminatedStringRef(path_storage);
521  int oflags = (mode == readonly) ? O_RDONLY : O_RDWR;
522  int ofd = ::open(name.begin(), oflags);
523  if (ofd == -1) {
524    ec = std::error_code(errno, std::generic_category());
525    return;
526  }
527
528  ec = init(ofd, true, offset);
529  if (ec)
530    Mapping = nullptr;
531}
532
533mapped_file_region::mapped_file_region(int fd,
534                                       bool closefd,
535                                       mapmode mode,
536                                       uint64_t length,
537                                       uint64_t offset,
538                                       std::error_code &ec)
539  : Mode(mode)
540  , Size(length)
541  , Mapping() {
542  // Make sure that the requested size fits within SIZE_T.
543  if (length > std::numeric_limits<size_t>::max()) {
544    ec = make_error_code(errc::invalid_argument);
545    return;
546  }
547
548  ec = init(fd, closefd, offset);
549  if (ec)
550    Mapping = nullptr;
551}
552
553mapped_file_region::~mapped_file_region() {
554  if (Mapping)
555    ::munmap(Mapping, Size);
556}
557
558mapped_file_region::mapped_file_region(mapped_file_region &&other)
559  : Mode(other.Mode), Size(other.Size), Mapping(other.Mapping) {
560  other.Mapping = nullptr;
561}
562
563mapped_file_region::mapmode mapped_file_region::flags() const {
564  assert(Mapping && "Mapping failed but used anyway!");
565  return Mode;
566}
567
568uint64_t mapped_file_region::size() const {
569  assert(Mapping && "Mapping failed but used anyway!");
570  return Size;
571}
572
573char *mapped_file_region::data() const {
574  assert(Mapping && "Mapping failed but used anyway!");
575  assert(Mode != readonly && "Cannot get non-const data for readonly mapping!");
576  return reinterpret_cast<char*>(Mapping);
577}
578
579const char *mapped_file_region::const_data() const {
580  assert(Mapping && "Mapping failed but used anyway!");
581  return reinterpret_cast<const char*>(Mapping);
582}
583
584int mapped_file_region::alignment() {
585  return process::get_self()->page_size();
586}
587
588std::error_code detail::directory_iterator_construct(detail::DirIterState &it,
589                                                StringRef path){
590  SmallString<128> path_null(path);
591  DIR *directory = ::opendir(path_null.c_str());
592  if (!directory)
593    return std::error_code(errno, std::generic_category());
594
595  it.IterationHandle = reinterpret_cast<intptr_t>(directory);
596  // Add something for replace_filename to replace.
597  path::append(path_null, ".");
598  it.CurrentEntry = directory_entry(path_null.str());
599  return directory_iterator_increment(it);
600}
601
602std::error_code detail::directory_iterator_destruct(detail::DirIterState &it) {
603  if (it.IterationHandle)
604    ::closedir(reinterpret_cast<DIR *>(it.IterationHandle));
605  it.IterationHandle = 0;
606  it.CurrentEntry = directory_entry();
607  return std::error_code();
608}
609
610std::error_code detail::directory_iterator_increment(detail::DirIterState &it) {
611  errno = 0;
612  dirent *cur_dir = ::readdir(reinterpret_cast<DIR *>(it.IterationHandle));
613  if (cur_dir == nullptr && errno != 0) {
614    return std::error_code(errno, std::generic_category());
615  } else if (cur_dir != nullptr) {
616    StringRef name(cur_dir->d_name, NAMLEN(cur_dir));
617    if ((name.size() == 1 && name[0] == '.') ||
618        (name.size() == 2 && name[0] == '.' && name[1] == '.'))
619      return directory_iterator_increment(it);
620    it.CurrentEntry.replace_filename(name);
621  } else
622    return directory_iterator_destruct(it);
623
624  return std::error_code();
625}
626
627std::error_code openFileForRead(const Twine &Name, int &ResultFD) {
628  SmallString<128> Storage;
629  StringRef P = Name.toNullTerminatedStringRef(Storage);
630  while ((ResultFD = open(P.begin(), O_RDONLY)) < 0) {
631    if (errno != EINTR)
632      return std::error_code(errno, std::generic_category());
633  }
634  return std::error_code();
635}
636
637std::error_code openFileForWrite(const Twine &Name, int &ResultFD,
638                            sys::fs::OpenFlags Flags, unsigned Mode) {
639  // Verify that we don't have both "append" and "excl".
640  assert((!(Flags & sys::fs::F_Excl) || !(Flags & sys::fs::F_Append)) &&
641         "Cannot specify both 'excl' and 'append' file creation flags!");
642
643  int OpenFlags = O_CREAT;
644
645  if (Flags & F_RW)
646    OpenFlags |= O_RDWR;
647  else
648    OpenFlags |= O_WRONLY;
649
650  if (Flags & F_Append)
651    OpenFlags |= O_APPEND;
652  else
653    OpenFlags |= O_TRUNC;
654
655  if (Flags & F_Excl)
656    OpenFlags |= O_EXCL;
657
658  SmallString<128> Storage;
659  StringRef P = Name.toNullTerminatedStringRef(Storage);
660  while ((ResultFD = open(P.begin(), OpenFlags, Mode)) < 0) {
661    if (errno != EINTR)
662      return std::error_code(errno, std::generic_category());
663  }
664  return std::error_code();
665}
666
667} // end namespace fs
668
669namespace path {
670
671bool home_directory(SmallVectorImpl<char> &result) {
672  if (char *RequestedDir = getenv("HOME")) {
673    result.clear();
674    result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
675    return true;
676  }
677
678  return false;
679}
680
681} // end namespace path
682
683} // end namespace sys
684} // end namespace llvm
685