LLDB mainline
FileCache.cpp
Go to the documentation of this file.
1//===-- FileCache.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#include "lldb/Host/File.h"
13
14using namespace lldb;
15using namespace lldb_private;
16
18
20 if (m_instance == nullptr)
21 m_instance = new FileCache();
22
23 return *m_instance;
24}
25
27 File::OpenOptions flags, uint32_t mode,
28 Status &error) {
29 if (!file_spec) {
30 error.SetErrorString("empty path");
31 return UINT64_MAX;
32 }
33 auto file = FileSystem::Instance().Open(file_spec, flags, mode);
34 if (!file) {
35 error = file.takeError();
36 return UINT64_MAX;
37 }
38 lldb::user_id_t fd = file.get()->GetDescriptor();
39 m_cache[fd] = std::move(file.get());
40 return fd;
41}
42
44 if (fd == UINT64_MAX) {
45 error.SetErrorString("invalid file descriptor");
46 return false;
47 }
48 FDToFileMap::iterator pos = m_cache.find(fd);
49 if (pos == m_cache.end()) {
50 error.SetErrorStringWithFormat("invalid host file descriptor %" PRIu64, fd);
51 return false;
52 }
53 FileUP &file_up = pos->second;
54 if (!file_up) {
55 error.SetErrorString("invalid host backing file");
56 return false;
57 }
58 error = file_up->Close();
59 m_cache.erase(pos);
60 return error.Success();
61}
62
63uint64_t FileCache::WriteFile(lldb::user_id_t fd, uint64_t offset,
64 const void *src, uint64_t src_len,
65 Status &error) {
66 if (fd == UINT64_MAX) {
67 error.SetErrorString("invalid file descriptor");
68 return UINT64_MAX;
69 }
70 FDToFileMap::iterator pos = m_cache.find(fd);
71 if (pos == m_cache.end()) {
72 error.SetErrorStringWithFormat("invalid host file descriptor %" PRIu64, fd);
73 return false;
74 }
75 FileUP &file_up = pos->second;
76 if (!file_up) {
77 error.SetErrorString("invalid host backing file");
78 return UINT64_MAX;
79 }
80 if (static_cast<uint64_t>(file_up->SeekFromStart(offset, &error)) != offset ||
81 error.Fail())
82 return UINT64_MAX;
83 size_t bytes_written = src_len;
84 error = file_up->Write(src, bytes_written);
85 if (error.Fail())
86 return UINT64_MAX;
87 return bytes_written;
88}
89
90uint64_t FileCache::ReadFile(lldb::user_id_t fd, uint64_t offset, void *dst,
91 uint64_t dst_len, Status &error) {
92 if (fd == UINT64_MAX) {
93 error.SetErrorString("invalid file descriptor");
94 return UINT64_MAX;
95 }
96 FDToFileMap::iterator pos = m_cache.find(fd);
97 if (pos == m_cache.end()) {
98 error.SetErrorStringWithFormat("invalid host file descriptor %" PRIu64, fd);
99 return false;
100 }
101 FileUP &file_up = pos->second;
102 if (!file_up) {
103 error.SetErrorString("invalid host backing file");
104 return UINT64_MAX;
105 }
106 if (static_cast<uint64_t>(file_up->SeekFromStart(offset, &error)) != offset ||
107 error.Fail())
108 return UINT64_MAX;
109 size_t bytes_read = dst_len;
110 error = file_up->Read(dst, bytes_read);
111 if (error.Fail())
112 return UINT64_MAX;
113 return bytes_read;
114}
static llvm::raw_ostream & error(Stream &strm)
bool CloseFile(lldb::user_id_t fd, Status &error)
Definition: FileCache.cpp:43
uint64_t ReadFile(lldb::user_id_t fd, uint64_t offset, void *dst, uint64_t dst_len, Status &error)
Definition: FileCache.cpp:90
lldb::user_id_t OpenFile(const FileSpec &file_spec, File::OpenOptions flags, uint32_t mode, Status &error)
Definition: FileCache.cpp:26
FDToFileMap m_cache
Definition: FileCache.h:43
uint64_t WriteFile(lldb::user_id_t fd, uint64_t offset, const void *src, uint64_t src_len, Status &error)
Definition: FileCache.cpp:63
static FileCache * m_instance
Definition: FileCache.h:41
static FileCache & GetInstance()
Definition: FileCache.cpp:19
A file utility class.
Definition: FileSpec.h:56
int Open(const char *path, int flags, int mode)
Wraps ::open in a platform-independent way.
static FileSystem & Instance()
An error handling class.
Definition: Status.h:44
#define UINT64_MAX
Definition: lldb-defines.h:23
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
uint64_t user_id_t
Definition: lldb-types.h:84