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