LLDB mainline
common/FileSystem.cpp
Go to the documentation of this file.
1//===-- FileSystem.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
14
15#include "llvm/Support/Errc.h"
16#include "llvm/Support/Errno.h"
17#include "llvm/Support/Error.h"
18#include "llvm/Support/FileSystem.h"
19#include "llvm/Support/Path.h"
20#include "llvm/Support/Program.h"
21#include "llvm/Support/Threading.h"
22
23#include <cerrno>
24#include <climits>
25#include <cstdarg>
26#include <cstdio>
27#include <fcntl.h>
28
29#ifdef _WIN32
31#else
32#include <sys/ioctl.h>
33#include <sys/stat.h>
34#include <termios.h>
35#include <unistd.h>
36#endif
37
38#include <algorithm>
39#include <fstream>
40#include <optional>
41#include <vector>
42
43using namespace lldb;
44using namespace lldb_private;
45using namespace llvm;
46
48
50 lldbassert(!InstanceImpl() && "Already initialized.");
51 InstanceImpl().emplace();
52}
53
54void FileSystem::Initialize(IntrusiveRefCntPtr<vfs::FileSystem> fs) {
55 lldbassert(!InstanceImpl() && "Already initialized.");
56 InstanceImpl().emplace(fs);
57}
58
60 lldbassert(InstanceImpl() && "Already terminated.");
61 InstanceImpl().reset();
62}
63
64std::optional<FileSystem> &FileSystem::InstanceImpl() {
65 static std::optional<FileSystem> g_fs;
66 return g_fs;
67}
68
69vfs::directory_iterator FileSystem::DirBegin(const FileSpec &file_spec,
70 std::error_code &ec) {
71 if (!file_spec) {
72 ec = std::error_code(static_cast<int>(errc::no_such_file_or_directory),
73 std::system_category());
74 return {};
75 }
76 return DirBegin(file_spec.GetPath(), ec);
77}
78
79vfs::directory_iterator FileSystem::DirBegin(const Twine &dir,
80 std::error_code &ec) {
81 return m_fs->dir_begin(dir, ec);
82}
83
84llvm::ErrorOr<vfs::Status>
85FileSystem::GetStatus(const FileSpec &file_spec) const {
86 if (!file_spec)
87 return std::error_code(static_cast<int>(errc::no_such_file_or_directory),
88 std::system_category());
89 return GetStatus(file_spec.GetPath());
90}
91
92llvm::ErrorOr<vfs::Status> FileSystem::GetStatus(const Twine &path) const {
93 return m_fs->status(path);
94}
95
96sys::TimePoint<>
98 if (!file_spec)
99 return sys::TimePoint<>();
100 return GetModificationTime(file_spec.GetPath());
101}
102
103sys::TimePoint<> FileSystem::GetModificationTime(const Twine &path) const {
104 ErrorOr<vfs::Status> status = m_fs->status(path);
105 if (!status)
106 return sys::TimePoint<>();
107 return status->getLastModificationTime();
108}
109
110uint64_t FileSystem::GetByteSize(const FileSpec &file_spec) const {
111 if (!file_spec)
112 return 0;
113 return GetByteSize(file_spec.GetPath());
114}
115
116uint64_t FileSystem::GetByteSize(const Twine &path) const {
117 ErrorOr<vfs::Status> status = m_fs->status(path);
118 if (!status)
119 return 0;
120 return status->getSize();
121}
122
124 return GetPermissions(file_spec.GetPath());
125}
126
128 std::error_code &ec) const {
129 if (!file_spec)
130 return sys::fs::perms::perms_not_known;
131 return GetPermissions(file_spec.GetPath(), ec);
132}
133
134uint32_t FileSystem::GetPermissions(const Twine &path) const {
135 std::error_code ec;
136 return GetPermissions(path, ec);
137}
138
139uint32_t FileSystem::GetPermissions(const Twine &path,
140 std::error_code &ec) const {
141 ErrorOr<vfs::Status> status = m_fs->status(path);
142 if (!status) {
143 ec = status.getError();
144 return sys::fs::perms::perms_not_known;
145 }
146 return status->getPermissions();
147}
148
149bool FileSystem::Exists(const Twine &path) const { return m_fs->exists(path); }
150
151bool FileSystem::Exists(const FileSpec &file_spec) const {
152 return file_spec && Exists(file_spec.GetPath());
153}
154
155bool FileSystem::Readable(const Twine &path) const {
156 return GetPermissions(path) & sys::fs::perms::all_read;
157}
158
159bool FileSystem::Readable(const FileSpec &file_spec) const {
160 return file_spec && Readable(file_spec.GetPath());
161}
162
163bool FileSystem::IsDirectory(const Twine &path) const {
164 ErrorOr<vfs::Status> status = m_fs->status(path);
165 if (!status)
166 return false;
167 return status->isDirectory();
168}
169
170bool FileSystem::IsDirectory(const FileSpec &file_spec) const {
171 return file_spec && IsDirectory(file_spec.GetPath());
172}
173
174bool FileSystem::IsLocal(const Twine &path) const {
175 bool b = false;
176 m_fs->isLocal(path, b);
177 return b;
178}
179
180bool FileSystem::IsLocal(const FileSpec &file_spec) const {
181 return file_spec && IsLocal(file_spec.GetPath());
182}
183
184void FileSystem::EnumerateDirectory(Twine path, bool find_directories,
185 bool find_files, bool find_other,
186 EnumerateDirectoryCallbackType callback,
187 void *callback_baton) {
188 std::error_code EC;
189 vfs::recursive_directory_iterator Iter(*m_fs, path, EC);
190 vfs::recursive_directory_iterator End;
191 for (; Iter != End && !EC; Iter.increment(EC)) {
192 const auto &Item = *Iter;
193 ErrorOr<vfs::Status> Status = m_fs->status(Item.path());
194 if (!Status)
195 break;
196 if (!find_files && Status->isRegularFile())
197 continue;
198 if (!find_directories && Status->isDirectory())
199 continue;
200 if (!find_other && Status->isOther())
201 continue;
202
203 auto Result = callback(callback_baton, Status->getType(), Item.path());
204 if (Result == eEnumerateDirectoryResultQuit)
205 return;
206 if (Result == eEnumerateDirectoryResultNext) {
207 // Default behavior is to recurse. Opt out if the callback doesn't want
208 // this behavior.
209 Iter.no_push();
210 }
211 }
212}
213
215 return m_fs->makeAbsolute(path);
216}
217
218std::error_code FileSystem::MakeAbsolute(FileSpec &file_spec) const {
219 SmallString<128> path;
220 file_spec.GetPath(path, false);
221
222 auto EC = MakeAbsolute(path);
223 if (EC)
224 return EC;
225
226 FileSpec new_file_spec(path, file_spec.GetPathStyle());
227 file_spec = new_file_spec;
228 return {};
229}
230
231std::error_code FileSystem::GetRealPath(const Twine &path,
232 SmallVectorImpl<char> &output) const {
233 return m_fs->getRealPath(path, output);
234}
235
237 if (path.empty())
238 return;
239
240 // Resolve tilde in path.
241 SmallString<128> resolved(path.begin(), path.end());
243 Resolver.ResolveFullPath(llvm::StringRef(path.begin(), path.size()),
244 resolved);
245
246 // Try making the path absolute if it exists.
247 SmallString<128> absolute(resolved.begin(), resolved.end());
248 MakeAbsolute(absolute);
249
250 path.clear();
251 if (Exists(absolute)) {
252 path.append(absolute.begin(), absolute.end());
253 } else {
254 path.append(resolved.begin(), resolved.end());
255 }
256}
257
259 if (!file_spec)
260 return;
261
262 // Extract path from the FileSpec.
263 SmallString<128> path;
264 file_spec.GetPath(path);
265
266 // Resolve the path.
267 Resolve(path);
268
269 // Update the FileSpec with the resolved path.
270 if (file_spec.GetFilename().IsEmpty())
271 file_spec.SetDirectory(path);
272 else
273 file_spec.SetPath(path);
274 file_spec.SetIsResolved(true);
275}
276
277template <typename T>
278static std::unique_ptr<T> GetMemoryBuffer(const llvm::Twine &path,
279 uint64_t size, uint64_t offset,
280 bool is_volatile) {
281 std::unique_ptr<T> buffer;
282 if (size == 0) {
283 auto buffer_or_error = T::getFile(path, is_volatile);
284 if (!buffer_or_error)
285 return nullptr;
286 buffer = std::move(*buffer_or_error);
287 } else {
288 auto buffer_or_error = T::getFileSlice(path, size, offset, is_volatile);
289 if (!buffer_or_error)
290 return nullptr;
291 buffer = std::move(*buffer_or_error);
292 }
293 return buffer;
294}
295
296std::shared_ptr<WritableDataBuffer>
297FileSystem::CreateWritableDataBuffer(const llvm::Twine &path, uint64_t size,
298 uint64_t offset) {
299 const bool is_volatile = !IsLocal(path);
300 auto buffer = GetMemoryBuffer<llvm::WritableMemoryBuffer>(path, size, offset,
301 is_volatile);
302 if (!buffer)
303 return {};
304 return std::shared_ptr<WritableDataBufferLLVM>(
305 new WritableDataBufferLLVM(std::move(buffer)));
306}
307
308std::shared_ptr<DataBuffer>
309FileSystem::CreateDataBuffer(const llvm::Twine &path, uint64_t size,
310 uint64_t offset) {
311 const bool is_volatile = !IsLocal(path);
312 auto buffer =
313 GetMemoryBuffer<llvm::MemoryBuffer>(path, size, offset, is_volatile);
314 if (!buffer)
315 return {};
316 return std::shared_ptr<DataBufferLLVM>(new DataBufferLLVM(std::move(buffer)));
317}
318
319std::shared_ptr<WritableDataBuffer>
320FileSystem::CreateWritableDataBuffer(const FileSpec &file_spec, uint64_t size,
321 uint64_t offset) {
322 return CreateWritableDataBuffer(file_spec.GetPath(), size, offset);
323}
324
325std::shared_ptr<DataBuffer>
326FileSystem::CreateDataBuffer(const FileSpec &file_spec, uint64_t size,
327 uint64_t offset) {
328 return CreateDataBuffer(file_spec.GetPath(), size, offset);
329}
330
332 // If the directory is set there's nothing to do.
333 ConstString directory = file_spec.GetDirectory();
334 if (directory)
335 return false;
336
337 // We cannot look for a file if there's no file name.
338 ConstString filename = file_spec.GetFilename();
339 if (!filename)
340 return false;
341
342 // Search for the file on the host.
343 const std::string filename_str(filename.GetCString());
344 llvm::ErrorOr<std::string> error_or_path =
345 llvm::sys::findProgramByName(filename_str);
346 if (!error_or_path)
347 return false;
348
349 // findProgramByName returns "." if it can't find the file.
350 llvm::StringRef path = *error_or_path;
351 llvm::StringRef parent = llvm::sys::path::parent_path(path);
352 if (parent.empty() || parent == ".")
353 return false;
354
355 // Make sure that the result exists.
356 FileSpec result(*error_or_path);
357 if (!Exists(result))
358 return false;
359
360 file_spec = result;
361 return true;
362}
363
365 if (!m_home_directory.empty()) {
366 path.assign(m_home_directory.begin(), m_home_directory.end());
367 return true;
368 }
369 return llvm::sys::path::home_directory(path);
370}
371
373 SmallString<128> home_dir;
374 if (!GetHomeDirectory(home_dir))
375 return false;
376 file_spec.SetPath(home_dir);
377 return true;
378}
379
380static int OpenWithFS(const FileSystem &fs, const char *path, int flags,
381 int mode) {
382 return const_cast<FileSystem &>(fs).Open(path, flags, mode);
383}
384
385static int GetOpenFlags(File::OpenOptions options) {
386 int open_flags = 0;
392 open_flags |= O_RDWR;
393 else
394 open_flags |= O_WRONLY;
395
396 if (options & File::eOpenOptionAppend)
397 open_flags |= O_APPEND;
398
399 if (options & File::eOpenOptionTruncate)
400 open_flags |= O_TRUNC;
401
402 if (options & File::eOpenOptionCanCreate)
403 open_flags |= O_CREAT;
404
406 open_flags |= O_CREAT | O_EXCL;
407 } else if (rw == File::eOpenOptionReadOnly) {
408 open_flags |= O_RDONLY;
409
410#ifndef _WIN32
412 open_flags |= O_NOFOLLOW;
413#endif
414 }
415
416#ifndef _WIN32
417 if (options & File::eOpenOptionNonBlocking)
418 open_flags |= O_NONBLOCK;
419 if (options & File::eOpenOptionCloseOnExec)
420 open_flags |= O_CLOEXEC;
421#else
422 open_flags |= O_BINARY;
423#endif
424
425 return open_flags;
426}
427
428static mode_t GetOpenMode(uint32_t permissions) {
429 mode_t mode = 0;
430 if (permissions & lldb::eFilePermissionsUserRead)
431 mode |= S_IRUSR;
432 if (permissions & lldb::eFilePermissionsUserWrite)
433 mode |= S_IWUSR;
434 if (permissions & lldb::eFilePermissionsUserExecute)
435 mode |= S_IXUSR;
436 if (permissions & lldb::eFilePermissionsGroupRead)
437 mode |= S_IRGRP;
438 if (permissions & lldb::eFilePermissionsGroupWrite)
439 mode |= S_IWGRP;
440 if (permissions & lldb::eFilePermissionsGroupExecute)
441 mode |= S_IXGRP;
442 if (permissions & lldb::eFilePermissionsWorldRead)
443 mode |= S_IROTH;
444 if (permissions & lldb::eFilePermissionsWorldWrite)
445 mode |= S_IWOTH;
446 if (permissions & lldb::eFilePermissionsWorldExecute)
447 mode |= S_IXOTH;
448 return mode;
449}
450
451Expected<FileUP> FileSystem::Open(const FileSpec &file_spec,
452 File::OpenOptions options,
453 uint32_t permissions, bool should_close_fd) {
454 const int open_flags = GetOpenFlags(options);
455 const mode_t open_mode =
456 (open_flags & O_CREAT) ? GetOpenMode(permissions) : 0;
457
458 auto path = file_spec.GetPath();
459
460 int descriptor = llvm::sys::RetryAfterSignal(
461 -1, OpenWithFS, *this, path.c_str(), open_flags, open_mode);
462
463 if (!File::DescriptorIsValid(descriptor))
464 return llvm::errorCodeToError(
465 std::error_code(errno, std::system_category()));
466
467 auto file = std::unique_ptr<File>(
468 new NativeFile(descriptor, options, should_close_fd));
469 assert(file->IsValid());
470 return std::move(file);
471}
472
473void FileSystem::SetHomeDirectory(std::string home_directory) {
474 m_home_directory = std::move(home_directory);
475}
476
478 return RemoveFile(file_spec.GetPath());
479}
480
481Status FileSystem::RemoveFile(const llvm::Twine &path) {
482 return Status(llvm::sys::fs::remove(path));
483}
#define lldbassert(x)
Definition: LLDBAssert.h:15
A uniqued constant string class.
Definition: ConstString.h:39
bool IsEmpty() const
Test for empty string.
Definition: ConstString.h:303
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:215
A file utility class.
Definition: FileSpec.h:56
void SetDirectory(ConstString directory)
Directory string set accessor.
Definition: FileSpec.cpp:334
const ConstString & GetFilename() const
Filename string const get accessor.
Definition: FileSpec.h:240
void SetIsResolved(bool is_resolved)
Set if the file path has been resolved or not.
Definition: FileSpec.h:393
const ConstString & GetDirectory() const
Directory string const get accessor.
Definition: FileSpec.h:223
void SetPath(llvm::StringRef p)
Temporary helper for FileSystem change.
Definition: FileSpec.h:279
Style GetPathStyle() const
Definition: FileSpec.cpp:332
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:366
uint64_t GetByteSize(const FileSpec &file_spec) const
Returns the on-disk size of the given file in bytes.
void Resolve(llvm::SmallVectorImpl< char > &path)
Resolve path to make it canonical.
bool GetHomeDirectory(llvm::SmallVectorImpl< char > &path) const
Get the user home directory.
void EnumerateDirectory(llvm::Twine path, bool find_directories, bool find_files, bool find_other, EnumerateDirectoryCallbackType callback, void *callback_baton)
@ eEnumerateDirectoryResultNext
Enumerate next entry in the current directory.
Definition: FileSystem.h:168
@ eEnumerateDirectoryResultQuit
Stop directory enumerations at any level.
Definition: FileSystem.h:173
llvm::sys::TimePoint GetModificationTime(const FileSpec &file_spec) const
Returns the modification time of the given file.
std::string m_home_directory
Definition: FileSystem.h:200
bool ResolveExecutableLocation(FileSpec &file_spec)
Call into the Host to see if it can help find the file.
bool Exists(const FileSpec &file_spec) const
Returns whether the given file exists.
std::error_code GetRealPath(const llvm::Twine &path, llvm::SmallVectorImpl< char > &output) const
void SetHomeDirectory(std::string home_directory)
static std::optional< FileSystem > & InstanceImpl()
uint32_t GetPermissions(const FileSpec &file_spec) const
Return the current permissions of the given file.
llvm::vfs::directory_iterator DirBegin(const FileSpec &file_spec, std::error_code &ec)
Get a directory iterator.
std::error_code MakeAbsolute(llvm::SmallVectorImpl< char > &path) const
Make the given file path absolute.
int Open(const char *path, int flags, int mode)
Wraps ::open in a platform-independent way.
llvm::ErrorOr< llvm::vfs::Status > GetStatus(const FileSpec &file_spec) const
Returns the Status object for the given file.
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > m_fs
Definition: FileSystem.h:199
std::shared_ptr< WritableDataBuffer > CreateWritableDataBuffer(const llvm::Twine &path, uint64_t size=0, uint64_t offset=0)
bool Readable(const FileSpec &file_spec) const
Returns whether the given file is readable.
bool IsDirectory(const FileSpec &file_spec) const
Returns whether the given path is a directory.
static FileSystem & Instance()
Status RemoveFile(const FileSpec &file_spec)
Remove a single file.
bool IsLocal(const FileSpec &file_spec) const
Returns whether the given path is local to the file system.
std::shared_ptr< DataBuffer > CreateDataBuffer(const llvm::Twine &path, uint64_t size=0, uint64_t offset=0)
Create memory buffer from path.
static bool DescriptorIsValid(int descriptor)
Definition: File.h:71
@ eOpenOptionReadOnly
Definition: File.h:51
@ eOpenOptionCanCreateNewOnly
Definition: File.h:58
@ eOpenOptionReadWrite
Definition: File.h:53
@ eOpenOptionWriteOnly
Definition: File.h:52
@ eOpenOptionAppend
Definition: File.h:54
@ eOpenOptionCanCreate
Definition: File.h:56
@ eOpenOptionCloseOnExec
Definition: File.h:63
@ eOpenOptionDontFollowSymlinks
Definition: File.h:62
@ eOpenOptionTruncate
Definition: File.h:57
@ eOpenOptionNonBlocking
Definition: File.h:61
An error handling class.
Definition: Status.h:44
bool ResolveFullPath(llvm::StringRef Expr, llvm::SmallVectorImpl< char > &Output)
Resolve an entire path that begins with a tilde expression, replacing the username portion with the m...
static int GetOpenFlags(File::OpenOptions options)
static mode_t GetOpenMode(uint32_t permissions)
static std::unique_ptr< T > GetMemoryBuffer(const llvm::Twine &path, uint64_t size, uint64_t offset, bool is_volatile)
static int OpenWithFS(const FileSystem &fs, const char *path, int flags, int mode)
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15
Definition: Debugger.h:52
#define S_IXGRP
#define S_IROTH
#define O_NONBLOCK
#define S_IXOTH
#define S_IRGRP
#define S_IWOTH
#define S_IRUSR
#define S_IWUSR
#define S_IWGRP
#define S_IXUSR