1 //===-- FileCache.cpp -------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Host/FileCache.h"
12 #include "lldb/Host/File.h"
15 using namespace lldb_private;
17 FileCache *FileCache::m_instance = nullptr;
19 FileCache &FileCache::GetInstance() {
20 if (m_instance == nullptr)
21 m_instance = new FileCache();
26 lldb::user_id_t FileCache::OpenFile(const FileSpec &file_spec, uint32_t flags,
27 uint32_t mode, Error &error) {
28 std::string path(file_spec.GetPath());
30 error.SetErrorString("empty path");
33 FileSP file_sp(new File());
34 error = file_sp->Open(path.c_str(), flags, mode);
35 if (file_sp->IsValid() == false)
37 lldb::user_id_t fd = file_sp->GetDescriptor();
38 m_cache[fd] = file_sp;
42 bool FileCache::CloseFile(lldb::user_id_t fd, Error &error) {
43 if (fd == UINT64_MAX) {
44 error.SetErrorString("invalid file descriptor");
47 FDToFileMap::iterator pos = m_cache.find(fd);
48 if (pos == m_cache.end()) {
49 error.SetErrorStringWithFormat("invalid host file descriptor %" PRIu64, fd);
52 FileSP file_sp = pos->second;
54 error.SetErrorString("invalid host backing file");
57 error = file_sp->Close();
59 return error.Success();
62 uint64_t FileCache::WriteFile(lldb::user_id_t fd, uint64_t offset,
63 const void *src, uint64_t src_len, Error &error) {
64 if (fd == UINT64_MAX) {
65 error.SetErrorString("invalid file descriptor");
68 FDToFileMap::iterator pos = m_cache.find(fd);
69 if (pos == m_cache.end()) {
70 error.SetErrorStringWithFormat("invalid host file descriptor %" PRIu64, fd);
73 FileSP file_sp = pos->second;
75 error.SetErrorString("invalid host backing file");
78 if (static_cast<uint64_t>(file_sp->SeekFromStart(offset, &error)) != offset ||
81 size_t bytes_written = src_len;
82 error = file_sp->Write(src, bytes_written);
88 uint64_t FileCache::ReadFile(lldb::user_id_t fd, uint64_t offset, void *dst,
89 uint64_t dst_len, Error &error) {
90 if (fd == UINT64_MAX) {
91 error.SetErrorString("invalid file descriptor");
94 FDToFileMap::iterator pos = m_cache.find(fd);
95 if (pos == m_cache.end()) {
96 error.SetErrorStringWithFormat("invalid host file descriptor %" PRIu64, fd);
99 FileSP file_sp = pos->second;
101 error.SetErrorString("invalid host backing file");
104 if (static_cast<uint64_t>(file_sp->SeekFromStart(offset, &error)) != offset ||
107 size_t bytes_read = dst_len;
108 error = file_sp->Read(dst, bytes_read);