LLDB mainline
FileSystemPosix.cpp
Go to the documentation of this file.
1//===-- FileSystemPosix.cpp -----------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
11// C includes
12#include <dirent.h>
13#include <fcntl.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"
27
28#include "llvm/Support/Errno.h"
29#include "llvm/Support/FileSystem.h"
30
31using namespace lldb;
32using namespace lldb_private;
33
34const char *FileSystem::DEV_NULL = "/dev/null";
35
38 if (::symlink(dst.GetPath().c_str(), src.GetPath().c_str()) == -1)
39 return Status::FromErrno();
40 return error;
41}
42
45 char buf[PATH_MAX];
46 ssize_t count = ::readlink(src.GetPath().c_str(), buf, sizeof(buf) - 1);
47 if (count < 0)
48 return Status::FromErrno();
49
50 buf[count] = '\0'; // Success
51 dst.SetFile(buf, FileSpec::Style::native);
52
53 return error;
54}
55
57 char resolved_path[PATH_MAX];
58 if (!src.GetPath(resolved_path, sizeof(resolved_path))) {
60 "Couldn't get the canonical path for %s", src.GetPath().c_str());
61 }
62
63 char real_path[PATH_MAX + 1];
64 if (realpath(resolved_path, real_path) == nullptr) {
65 return Status::FromErrno();
66 }
67
68 dst = FileSpec(real_path);
69
70 return Status();
71}
72
73FILE *FileSystem::Fopen(const char *path, const char *mode) {
74 return llvm::sys::RetryAfterSignal(nullptr, ::fopen, path, mode);
75}
76
77int FileSystem::Open(const char *path, int flags, int mode) {
78 // Call ::open in a lambda to avoid overload resolution in RetryAfterSignal
79 // when open is overloaded, such as in Bionic.
80 auto lambda = [&]() { return ::open(path, flags, mode); };
81 return llvm::sys::RetryAfterSignal(-1, lambda);
82}
static llvm::raw_ostream & error(Stream &strm)
A file utility class.
Definition: FileSpec.h:56
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
Definition: FileSpec.cpp:174
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:367
static const char * DEV_NULL
Definition: FileSystem.h:32
Status Symlink(const FileSpec &src, const FileSpec &dst)
Status ResolveSymbolicLink(const FileSpec &src, FileSpec &dst)
int Open(const char *path, int flags, int mode=0600)
Wraps ::open in a platform-independent way.
Status Readlink(const FileSpec &src, FileSpec &dst)
FILE * Fopen(const char *path, const char *mode)
Wraps ::fopen in a platform-independent way.
An error handling class.
Definition: Status.h:115
static Status FromErrno()
Set the current error to errno.
Definition: Status.cpp:276
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition: Status.cpp:106
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
#define PATH_MAX