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