1 //===-- FileSystem.cpp ------------------------------------------*- 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 #include "lldb/Host/FileSystem.h"
11 
12 // C includes
13 #include <dirent.h>
14 #include <sys/mount.h>
15 #include <sys/param.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 #include <unistd.h>
19 #ifdef __linux__
20 #include <linux/magic.h>
21 #include <sys/mount.h>
22 #include <sys/statfs.h>
23 #endif
24 #if defined(__NetBSD__)
25 #include <sys/statvfs.h>
26 #endif
27 
28 // lldb Includes
29 #include "lldb/Host/Host.h"
30 #include "lldb/Utility/Status.h"
31 #include "lldb/Utility/StreamString.h"
32 
33 #include "llvm/Support/FileSystem.h"
34 
35 using namespace lldb;
36 using namespace lldb_private;
37 
38 const char *FileSystem::DEV_NULL = "/dev/null";
39 
40 Status FileSystem::Symlink(const FileSpec &src, const FileSpec &dst) {
41   Status error;
42   if (::symlink(dst.GetCString(), src.GetCString()) == -1)
43     error.SetErrorToErrno();
44   return error;
45 }
46 
47 Status FileSystem::Readlink(const FileSpec &src, FileSpec &dst) {
48   Status error;
49   char buf[PATH_MAX];
50   ssize_t count = ::readlink(src.GetCString(), buf, sizeof(buf) - 1);
51   if (count < 0)
52     error.SetErrorToErrno();
53   else {
54     buf[count] = '\0'; // Success
55     dst.SetFile(buf, false);
56   }
57   return error;
58 }
59 
60 Status FileSystem::ResolveSymbolicLink(const FileSpec &src, FileSpec &dst) {
61   char resolved_path[PATH_MAX];
62   if (!src.GetPath(resolved_path, sizeof(resolved_path))) {
63     return Status("Couldn't get the canonical path for %s", src.GetCString());
64   }
65 
66   char real_path[PATH_MAX + 1];
67   if (realpath(resolved_path, real_path) == nullptr) {
68     Status err;
69     err.SetErrorToErrno();
70     return err;
71   }
72 
73   dst = FileSpec(real_path, false);
74 
75   return Status();
76 }
77 
78 FILE *FileSystem::Fopen(const char *path, const char *mode) {
79   return ::fopen(path, mode);
80 }
81