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