0c1623acdc7b35f30c45b4ead60b6e0cdc9cac21
[oota-llvm.git] / lib / Support / Unix / Path.inc
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 "llvm/Support/Process.h"
21 #if HAVE_SYS_STAT_H
22 #include <sys/stat.h>
23 #endif
24 #if HAVE_FCNTL_H
25 #include <fcntl.h>
26 #endif
27 #ifdef HAVE_SYS_MMAN_H
28 #include <sys/mman.h>
29 #endif
30 #if HAVE_DIRENT_H
31 # include <dirent.h>
32 # define NAMLEN(dirent) strlen((dirent)->d_name)
33 #else
34 # define dirent direct
35 # define NAMLEN(dirent) (dirent)->d_namlen
36 # if HAVE_SYS_NDIR_H
37 #  include <sys/ndir.h>
38 # endif
39 # if HAVE_SYS_DIR_H
40 #  include <sys/dir.h>
41 # endif
42 # if HAVE_NDIR_H
43 #  include <ndir.h>
44 # endif
45 #endif
46 #if HAVE_STDIO_H
47 #include <stdio.h>
48 #endif
49 #if HAVE_LIMITS_H
50 #include <limits.h>
51 #endif
52
53 #ifdef __APPLE__
54 #include <mach-o/dyld.h>
55 #endif
56
57 // Both stdio.h and cstdio are included via different pathes and
58 // stdcxx's cstdio doesn't include stdio.h, so it doesn't #undef the macros
59 // either.
60 #undef ferror
61 #undef feof
62
63 // For GNU Hurd
64 #if defined(__GNU__) && !defined(PATH_MAX)
65 # define PATH_MAX 4096
66 #endif
67
68 using namespace llvm;
69
70 namespace {
71   /// This class automatically closes the given file descriptor when it goes out
72   /// of scope. You can take back explicit ownership of the file descriptor by
73   /// calling take(). The destructor does not verify that close was successful.
74   /// Therefore, never allow this class to call close on a file descriptor that
75   /// has been read from or written to.
76   struct AutoFD {
77     int FileDescriptor;
78
79     AutoFD(int fd) : FileDescriptor(fd) {}
80     ~AutoFD() {
81       if (FileDescriptor >= 0)
82         ::close(FileDescriptor);
83     }
84
85     int take() {
86       int ret = FileDescriptor;
87       FileDescriptor = -1;
88       return ret;
89     }
90
91     operator int() const {return FileDescriptor;}
92   };
93
94   error_code TempDir(SmallVectorImpl<char> &result) {
95     // FIXME: Don't use TMPDIR if program is SUID or SGID enabled.
96     const char *dir = 0;
97     (dir = std::getenv("TMPDIR" )) ||
98     (dir = std::getenv("TMP"    )) ||
99     (dir = std::getenv("TEMP"   )) ||
100     (dir = std::getenv("TEMPDIR")) ||
101 #ifdef P_tmpdir
102     (dir = P_tmpdir) ||
103 #endif
104     (dir = "/tmp");
105
106     result.clear();
107     StringRef d(dir);
108     result.append(d.begin(), d.end());
109     return error_code::success();
110   }
111 }
112
113 static error_code createUniqueEntity(const Twine &Model, int &ResultFD,
114                                      SmallVectorImpl<char> &ResultPath,
115                                      bool MakeAbsolute, unsigned Mode,
116                                      FSEntity Type) {
117   SmallString<128> ModelStorage;
118   Model.toVector(ModelStorage);
119
120   if (MakeAbsolute) {
121     // Make model absolute by prepending a temp directory if it's not already.
122     bool absolute = sys::path::is_absolute(Twine(ModelStorage));
123     if (!absolute) {
124       SmallString<128> TDir;
125       if (error_code ec = TempDir(TDir)) return ec;
126       sys::path::append(TDir, Twine(ModelStorage));
127       ModelStorage.swap(TDir);
128     }
129   }
130
131   // From here on, DO NOT modify model. It may be needed if the randomly chosen
132   // path already exists.
133   ResultPath = ModelStorage;
134   // Null terminate.
135   ResultPath.push_back(0);
136   ResultPath.pop_back();
137
138 retry_random_path:
139   // Replace '%' with random chars.
140   for (unsigned i = 0, e = ModelStorage.size(); i != e; ++i) {
141     if (ModelStorage[i] == '%')
142       ResultPath[i] = "0123456789abcdef"[sys::Process::GetRandomNumber() & 15];
143   }
144
145   // Try to open + create the file.
146   switch (Type) {
147   case FS_File: {
148     int RandomFD = ::open(ResultPath.begin(), O_RDWR | O_CREAT | O_EXCL, Mode);
149     if (RandomFD == -1) {
150       int SavedErrno = errno;
151       // If the file existed, try again, otherwise, error.
152       if (SavedErrno == errc::file_exists)
153         goto retry_random_path;
154       return error_code(SavedErrno, system_category());
155     }
156
157     ResultFD = RandomFD;
158     return error_code::success();
159   }
160
161   case FS_Name: {
162     bool Exists;
163     error_code EC = sys::fs::exists(ResultPath.begin(), Exists);
164     if (EC)
165       return EC;
166     if (Exists)
167       goto retry_random_path;
168     return error_code::success();
169   }
170
171   case FS_Dir: {
172     bool Existed;
173     error_code EC = sys::fs::create_directory(ResultPath.begin(), Existed);
174     if (EC)
175       return EC;
176     if (Existed)
177       goto retry_random_path;
178     return error_code::success();
179   }
180   }
181   llvm_unreachable("Invalid Type");
182 }
183
184 namespace llvm {
185 namespace sys  {
186 namespace fs {
187 #if defined(__FreeBSD__) || defined (__NetBSD__) || defined(__Bitrig__) || \
188     defined(__OpenBSD__) || defined(__minix) || defined(__FreeBSD_kernel__) || \
189     defined(__linux__) || defined(__CYGWIN__)
190 static int
191 test_dir(char buf[PATH_MAX], char ret[PATH_MAX],
192     const char *dir, const char *bin)
193 {
194   struct stat sb;
195
196   snprintf(buf, PATH_MAX, "%s/%s", dir, bin);
197   if (realpath(buf, ret) == NULL)
198     return (1);
199   if (stat(buf, &sb) != 0)
200     return (1);
201
202   return (0);
203 }
204
205 static char *
206 getprogpath(char ret[PATH_MAX], const char *bin)
207 {
208   char *pv, *s, *t, buf[PATH_MAX];
209
210   /* First approach: absolute path. */
211   if (bin[0] == '/') {
212     if (test_dir(buf, ret, "/", bin) == 0)
213       return (ret);
214     return (NULL);
215   }
216
217   /* Second approach: relative path. */
218   if (strchr(bin, '/') != NULL) {
219     if (getcwd(buf, PATH_MAX) == NULL)
220       return (NULL);
221     if (test_dir(buf, ret, buf, bin) == 0)
222       return (ret);
223     return (NULL);
224   }
225
226   /* Third approach: $PATH */
227   if ((pv = getenv("PATH")) == NULL)
228     return (NULL);
229   s = pv = strdup(pv);
230   if (pv == NULL)
231     return (NULL);
232   while ((t = strsep(&s, ":")) != NULL) {
233     if (test_dir(buf, ret, t, bin) == 0) {
234       free(pv);
235       return (ret);
236     }
237   }
238   free(pv);
239   return (NULL);
240 }
241 #endif // __FreeBSD__ || __NetBSD__ || __FreeBSD_kernel__
242
243 /// GetMainExecutable - Return the path to the main executable, given the
244 /// value of argv[0] from program startup.
245 std::string getMainExecutable(const char *argv0, void *MainAddr) {
246 #if defined(__APPLE__)
247   // On OS X the executable path is saved to the stack by dyld. Reading it
248   // from there is much faster than calling dladdr, especially for large
249   // binaries with symbols.
250   char exe_path[MAXPATHLEN];
251   uint32_t size = sizeof(exe_path);
252   if (_NSGetExecutablePath(exe_path, &size) == 0) {
253     char link_path[MAXPATHLEN];
254     if (realpath(exe_path, link_path))
255       return link_path;
256   }
257 #elif defined(__FreeBSD__) || defined (__NetBSD__) || defined(__Bitrig__) || \
258       defined(__OpenBSD__) || defined(__minix) || defined(__FreeBSD_kernel__)
259   char exe_path[PATH_MAX];
260
261   if (getprogpath(exe_path, argv0) != NULL)
262     return exe_path;
263 #elif defined(__linux__) || defined(__CYGWIN__)
264   char exe_path[MAXPATHLEN];
265   StringRef aPath("/proc/self/exe");
266   if (sys::fs::exists(aPath)) {
267       // /proc is not always mounted under Linux (chroot for example).
268       ssize_t len = readlink(aPath.str().c_str(), exe_path, sizeof(exe_path));
269       if (len >= 0)
270           return StringRef(exe_path, len);
271   } else {
272       // Fall back to the classical detection.
273       if (getprogpath(exe_path, argv0) != NULL)
274           return exe_path;
275   }
276 #elif defined(HAVE_DLFCN_H)
277   // Use dladdr to get executable path if available.
278   Dl_info DLInfo;
279   int err = dladdr(MainAddr, &DLInfo);
280   if (err == 0)
281     return "";
282
283   // If the filename is a symlink, we need to resolve and return the location of
284   // the actual executable.
285   char link_path[MAXPATHLEN];
286   if (realpath(DLInfo.dli_fname, link_path))
287     return link_path;
288 #else
289 #error GetMainExecutable is not implemented on this host yet.
290 #endif
291   return "";
292 }
293
294 TimeValue file_status::getLastModificationTime() const {
295   TimeValue Ret;
296   Ret.fromEpochTime(fs_st_mtime);
297   return Ret;
298 }
299
300 error_code current_path(SmallVectorImpl<char> &result) {
301 #ifdef MAXPATHLEN
302   result.reserve(MAXPATHLEN);
303 #else
304 // For GNU Hurd
305   result.reserve(1024);
306 #endif
307
308   while (true) {
309     if (::getcwd(result.data(), result.capacity()) == 0) {
310       // See if there was a real error.
311       if (errno != errc::not_enough_memory)
312         return error_code(errno, system_category());
313       // Otherwise there just wasn't enough space.
314       result.reserve(result.capacity() * 2);
315     } else
316       break;
317   }
318
319   result.set_size(strlen(result.data()));
320   return error_code::success();
321 }
322
323 error_code copy_file(const Twine &from, const Twine &to, copy_option copt) {
324  // Get arguments.
325   SmallString<128> from_storage;
326   SmallString<128> to_storage;
327   StringRef f = from.toNullTerminatedStringRef(from_storage);
328   StringRef t = to.toNullTerminatedStringRef(to_storage);
329
330   const size_t buf_sz = 32768;
331   char buffer[buf_sz];
332   int from_file = -1, to_file = -1;
333
334   // Open from.
335   if ((from_file = ::open(f.begin(), O_RDONLY)) < 0)
336     return error_code(errno, system_category());
337   AutoFD from_fd(from_file);
338
339   // Stat from.
340   struct stat from_stat;
341   if (::stat(f.begin(), &from_stat) != 0)
342     return error_code(errno, system_category());
343
344   // Setup to flags.
345   int to_flags = O_CREAT | O_WRONLY;
346   if (copt == copy_option::fail_if_exists)
347     to_flags |= O_EXCL;
348
349   // Open to.
350   if ((to_file = ::open(t.begin(), to_flags, from_stat.st_mode)) < 0)
351     return error_code(errno, system_category());
352   AutoFD to_fd(to_file);
353
354   // Copy!
355   ssize_t sz, sz_read = 1, sz_write;
356   while (sz_read > 0 &&
357          (sz_read = ::read(from_fd, buffer, buf_sz)) > 0) {
358     // Allow for partial writes - see Advanced Unix Programming (2nd Ed.),
359     // Marc Rochkind, Addison-Wesley, 2004, page 94
360     sz_write = 0;
361     do {
362       if ((sz = ::write(to_fd, buffer + sz_write, sz_read - sz_write)) < 0) {
363         sz_read = sz;  // cause read loop termination.
364         break;         // error.
365       }
366       sz_write += sz;
367     } while (sz_write < sz_read);
368   }
369
370   // After all the file operations above the return value of close actually
371   // matters.
372   if (::close(from_fd.take()) < 0) sz_read = -1;
373   if (::close(to_fd.take()) < 0) sz_read = -1;
374
375   // Check for errors.
376   if (sz_read < 0)
377     return error_code(errno, system_category());
378
379   return error_code::success();
380 }
381
382 error_code create_directory(const Twine &path, bool &existed) {
383   SmallString<128> path_storage;
384   StringRef p = path.toNullTerminatedStringRef(path_storage);
385
386   if (::mkdir(p.begin(), S_IRWXU | S_IRWXG) == -1) {
387     if (errno != errc::file_exists)
388       return error_code(errno, system_category());
389     existed = true;
390   } else
391     existed = false;
392
393   return error_code::success();
394 }
395
396 error_code create_hard_link(const Twine &to, const Twine &from) {
397   // Get arguments.
398   SmallString<128> from_storage;
399   SmallString<128> to_storage;
400   StringRef f = from.toNullTerminatedStringRef(from_storage);
401   StringRef t = to.toNullTerminatedStringRef(to_storage);
402
403   if (::link(t.begin(), f.begin()) == -1)
404     return error_code(errno, system_category());
405
406   return error_code::success();
407 }
408
409 error_code create_symlink(const Twine &to, const Twine &from) {
410   // Get arguments.
411   SmallString<128> from_storage;
412   SmallString<128> to_storage;
413   StringRef f = from.toNullTerminatedStringRef(from_storage);
414   StringRef t = to.toNullTerminatedStringRef(to_storage);
415
416   if (::symlink(t.begin(), f.begin()) == -1)
417     return error_code(errno, system_category());
418
419   return error_code::success();
420 }
421
422 error_code remove(const Twine &path, bool &existed) {
423   SmallString<128> path_storage;
424   StringRef p = path.toNullTerminatedStringRef(path_storage);
425
426   struct stat buf;
427   if (stat(p.begin(), &buf) != 0) {
428     if (errno != errc::no_such_file_or_directory)
429       return error_code(errno, system_category());
430     existed = false;
431     return error_code::success();
432   }
433
434   // Note: this check catches strange situations. In all cases, LLVM should
435   // only be involved in the creation and deletion of regular files.  This
436   // check ensures that what we're trying to erase is a regular file. It
437   // effectively prevents LLVM from erasing things like /dev/null, any block
438   // special file, or other things that aren't "regular" files.
439   if (!S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode))
440     return make_error_code(errc::operation_not_permitted);
441
442   if (::remove(p.begin()) == -1) {
443     if (errno != errc::no_such_file_or_directory)
444       return error_code(errno, system_category());
445     existed = false;
446   } else
447     existed = true;
448
449   return error_code::success();
450 }
451
452 error_code rename(const Twine &from, const Twine &to) {
453   // Get arguments.
454   SmallString<128> from_storage;
455   SmallString<128> to_storage;
456   StringRef f = from.toNullTerminatedStringRef(from_storage);
457   StringRef t = to.toNullTerminatedStringRef(to_storage);
458
459   if (::rename(f.begin(), t.begin()) == -1)
460     return error_code(errno, system_category());
461
462   return error_code::success();
463 }
464
465 error_code resize_file(const Twine &path, uint64_t size) {
466   SmallString<128> path_storage;
467   StringRef p = path.toNullTerminatedStringRef(path_storage);
468
469   if (::truncate(p.begin(), size) == -1)
470     return error_code(errno, system_category());
471
472   return error_code::success();
473 }
474
475 error_code exists(const Twine &path, bool &result) {
476   SmallString<128> path_storage;
477   StringRef p = path.toNullTerminatedStringRef(path_storage);
478
479   if (::access(p.begin(), F_OK) == -1) {
480     if (errno != errc::no_such_file_or_directory)
481       return error_code(errno, system_category());
482     result = false;
483   } else
484     result = true;
485
486   return error_code::success();
487 }
488
489 bool can_write(const Twine &Path) {
490   SmallString<128> PathStorage;
491   StringRef P = Path.toNullTerminatedStringRef(PathStorage);
492   return 0 == access(P.begin(), W_OK);
493 }
494
495 bool can_execute(const Twine &Path) {
496   SmallString<128> PathStorage;
497   StringRef P = Path.toNullTerminatedStringRef(PathStorage);
498
499   if (0 != access(P.begin(), R_OK | X_OK))
500     return false;
501   struct stat buf;
502   if (0 != stat(P.begin(), &buf))
503     return false;
504   if (!S_ISREG(buf.st_mode))
505     return false;
506   return true;
507 }
508
509 bool equivalent(file_status A, file_status B) {
510   assert(status_known(A) && status_known(B));
511   return A.fs_st_dev == B.fs_st_dev &&
512          A.fs_st_ino == B.fs_st_ino;
513 }
514
515 error_code equivalent(const Twine &A, const Twine &B, bool &result) {
516   file_status fsA, fsB;
517   if (error_code ec = status(A, fsA)) return ec;
518   if (error_code ec = status(B, fsB)) return ec;
519   result = equivalent(fsA, fsB);
520   return error_code::success();
521 }
522
523 error_code getUniqueID(const Twine Path, uint64_t &Result) {
524   SmallString<128> Storage;
525   StringRef P = Path.toNullTerminatedStringRef(Storage);
526
527   struct stat Status;
528   if (::stat(P.begin(), &Status) != 0)
529     return error_code(errno, system_category());
530
531   Result = Status.st_ino;
532   return error_code::success();
533 }
534
535 static error_code fillStatus(int StatRet, const struct stat &Status,
536                              file_status &Result) {
537   if (StatRet != 0) {
538     error_code ec(errno, system_category());
539     if (ec == errc::no_such_file_or_directory)
540       Result = file_status(file_type::file_not_found);
541     else
542       Result = file_status(file_type::status_error);
543     return ec;
544   }
545
546   file_type Type = file_type::type_unknown;
547
548   if (S_ISDIR(Status.st_mode))
549     Type = file_type::directory_file;
550   else if (S_ISREG(Status.st_mode))
551     Type = file_type::regular_file;
552   else if (S_ISBLK(Status.st_mode))
553     Type = file_type::block_file;
554   else if (S_ISCHR(Status.st_mode))
555     Type = file_type::character_file;
556   else if (S_ISFIFO(Status.st_mode))
557     Type = file_type::fifo_file;
558   else if (S_ISSOCK(Status.st_mode))
559     Type = file_type::socket_file;
560
561   perms Perms = static_cast<perms>(Status.st_mode);
562   Result =
563       file_status(Type, Perms, Status.st_dev, Status.st_ino, Status.st_mtime,
564                   Status.st_uid, Status.st_gid, Status.st_size);
565
566   return error_code::success();
567 }
568
569 error_code status(const Twine &Path, file_status &Result) {
570   SmallString<128> PathStorage;
571   StringRef P = Path.toNullTerminatedStringRef(PathStorage);
572
573   struct stat Status;
574   int StatRet = ::stat(P.begin(), &Status);
575   return fillStatus(StatRet, Status, Result);
576 }
577
578 error_code status(int FD, file_status &Result) {
579   struct stat Status;
580   int StatRet = ::fstat(FD, &Status);
581   return fillStatus(StatRet, Status, Result);
582 }
583
584 error_code setLastModificationAndAccessTime(int FD, TimeValue Time) {
585 #if defined(HAVE_FUTIMENS)
586   timespec Times[2];
587   Times[0].tv_sec = Time.toPosixTime();
588   Times[0].tv_nsec = 0;
589   Times[1] = Times[0];
590   if (::futimens(FD, Times))
591 #elif defined(HAVE_FUTIMES)
592   timeval Times[2];
593   Times[0].tv_sec = Time.toPosixTime();
594   Times[0].tv_usec = 0;
595   Times[1] = Times[0];
596   if (::futimes(FD, Times))
597 #else
598 #error Missing futimes() and futimens()
599 #endif
600     return error_code(errno, system_category());
601   return error_code::success();
602 }
603
604 error_code mapped_file_region::init(int FD, bool CloseFD, uint64_t Offset) {
605   AutoFD ScopedFD(FD);
606   if (!CloseFD)
607     ScopedFD.take();
608
609   // Figure out how large the file is.
610   struct stat FileInfo;
611   if (fstat(FD, &FileInfo) == -1)
612     return error_code(errno, system_category());
613   uint64_t FileSize = FileInfo.st_size;
614
615   if (Size == 0)
616     Size = FileSize;
617   else if (FileSize < Size) {
618     // We need to grow the file.
619     if (ftruncate(FD, Size) == -1)
620       return error_code(errno, system_category());
621   }
622
623   int flags = (Mode == readwrite) ? MAP_SHARED : MAP_PRIVATE;
624   int prot = (Mode == readonly) ? PROT_READ : (PROT_READ | PROT_WRITE);
625 #ifdef MAP_FILE
626   flags |= MAP_FILE;
627 #endif
628   Mapping = ::mmap(0, Size, prot, flags, FD, Offset);
629   if (Mapping == MAP_FAILED)
630     return error_code(errno, system_category());
631   return error_code::success();
632 }
633
634 mapped_file_region::mapped_file_region(const Twine &path,
635                                        mapmode mode,
636                                        uint64_t length,
637                                        uint64_t offset,
638                                        error_code &ec)
639   : Mode(mode)
640   , Size(length)
641   , Mapping() {
642   // Make sure that the requested size fits within SIZE_T.
643   if (length > std::numeric_limits<size_t>::max()) {
644     ec = make_error_code(errc::invalid_argument);
645     return;
646   }
647
648   SmallString<128> path_storage;
649   StringRef name = path.toNullTerminatedStringRef(path_storage);
650   int oflags = (mode == readonly) ? O_RDONLY : O_RDWR;
651   int ofd = ::open(name.begin(), oflags);
652   if (ofd == -1) {
653     ec = error_code(errno, system_category());
654     return;
655   }
656
657   ec = init(ofd, true, offset);
658   if (ec)
659     Mapping = 0;
660 }
661
662 mapped_file_region::mapped_file_region(int fd,
663                                        bool closefd,
664                                        mapmode mode,
665                                        uint64_t length,
666                                        uint64_t offset,
667                                        error_code &ec)
668   : Mode(mode)
669   , Size(length)
670   , Mapping() {
671   // Make sure that the requested size fits within SIZE_T.
672   if (length > std::numeric_limits<size_t>::max()) {
673     ec = make_error_code(errc::invalid_argument);
674     return;
675   }
676
677   ec = init(fd, closefd, offset);
678   if (ec)
679     Mapping = 0;
680 }
681
682 mapped_file_region::~mapped_file_region() {
683   if (Mapping)
684     ::munmap(Mapping, Size);
685 }
686
687 #if LLVM_HAS_RVALUE_REFERENCES
688 mapped_file_region::mapped_file_region(mapped_file_region &&other)
689   : Mode(other.Mode), Size(other.Size), Mapping(other.Mapping) {
690   other.Mapping = 0;
691 }
692 #endif
693
694 mapped_file_region::mapmode mapped_file_region::flags() const {
695   assert(Mapping && "Mapping failed but used anyway!");
696   return Mode;
697 }
698
699 uint64_t mapped_file_region::size() const {
700   assert(Mapping && "Mapping failed but used anyway!");
701   return Size;
702 }
703
704 char *mapped_file_region::data() const {
705   assert(Mapping && "Mapping failed but used anyway!");
706   assert(Mode != readonly && "Cannot get non const data for readonly mapping!");
707   return reinterpret_cast<char*>(Mapping);
708 }
709
710 const char *mapped_file_region::const_data() const {
711   assert(Mapping && "Mapping failed but used anyway!");
712   return reinterpret_cast<const char*>(Mapping);
713 }
714
715 int mapped_file_region::alignment() {
716   return process::get_self()->page_size();
717 }
718
719 error_code detail::directory_iterator_construct(detail::DirIterState &it,
720                                                 StringRef path){
721   SmallString<128> path_null(path);
722   DIR *directory = ::opendir(path_null.c_str());
723   if (directory == 0)
724     return error_code(errno, system_category());
725
726   it.IterationHandle = reinterpret_cast<intptr_t>(directory);
727   // Add something for replace_filename to replace.
728   path::append(path_null, ".");
729   it.CurrentEntry = directory_entry(path_null.str());
730   return directory_iterator_increment(it);
731 }
732
733 error_code detail::directory_iterator_destruct(detail::DirIterState &it) {
734   if (it.IterationHandle)
735     ::closedir(reinterpret_cast<DIR *>(it.IterationHandle));
736   it.IterationHandle = 0;
737   it.CurrentEntry = directory_entry();
738   return error_code::success();
739 }
740
741 error_code detail::directory_iterator_increment(detail::DirIterState &it) {
742   errno = 0;
743   dirent *cur_dir = ::readdir(reinterpret_cast<DIR *>(it.IterationHandle));
744   if (cur_dir == 0 && errno != 0) {
745     return error_code(errno, system_category());
746   } else if (cur_dir != 0) {
747     StringRef name(cur_dir->d_name, NAMLEN(cur_dir));
748     if ((name.size() == 1 && name[0] == '.') ||
749         (name.size() == 2 && name[0] == '.' && name[1] == '.'))
750       return directory_iterator_increment(it);
751     it.CurrentEntry.replace_filename(name);
752   } else
753     return directory_iterator_destruct(it);
754
755   return error_code::success();
756 }
757
758 error_code get_magic(const Twine &path, uint32_t len,
759                      SmallVectorImpl<char> &result) {
760   SmallString<128> PathStorage;
761   StringRef Path = path.toNullTerminatedStringRef(PathStorage);
762   result.set_size(0);
763
764   // Open path.
765   std::FILE *file = std::fopen(Path.data(), "rb");
766   if (file == 0)
767     return error_code(errno, system_category());
768
769   // Reserve storage.
770   result.reserve(len);
771
772   // Read magic!
773   size_t size = std::fread(result.data(), 1, len, file);
774   if (std::ferror(file) != 0) {
775     std::fclose(file);
776     return error_code(errno, system_category());
777   } else if (size != len) {
778     if (std::feof(file) != 0) {
779       std::fclose(file);
780       result.set_size(size);
781       return make_error_code(errc::value_too_large);
782     }
783   }
784   std::fclose(file);
785   result.set_size(size);
786   return error_code::success();
787 }
788
789 error_code map_file_pages(const Twine &path, off_t file_offset, size_t size,  
790                                             bool map_writable, void *&result) {
791   SmallString<128> path_storage;
792   StringRef name = path.toNullTerminatedStringRef(path_storage);
793   int oflags = map_writable ? O_RDWR : O_RDONLY;
794   int ofd = ::open(name.begin(), oflags);
795   if ( ofd == -1 )
796     return error_code(errno, system_category());
797   AutoFD fd(ofd);
798   int flags = map_writable ? MAP_SHARED : MAP_PRIVATE;
799   int prot = map_writable ? (PROT_READ|PROT_WRITE) : PROT_READ;
800 #ifdef MAP_FILE
801   flags |= MAP_FILE;
802 #endif
803   result = ::mmap(0, size, prot, flags, fd, file_offset);
804   if (result == MAP_FAILED) {
805     return error_code(errno, system_category());
806   }
807   
808   return error_code::success();
809 }
810
811 error_code unmap_file_pages(void *base, size_t size) {
812   if ( ::munmap(base, size) == -1 )
813     return error_code(errno, system_category());
814    
815   return error_code::success();
816 }
817
818
819 } // end namespace fs
820 } // end namespace sys
821 } // end namespace llvm