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;
20 FileCache::GetInstance()
22 if (m_instance == nullptr)
23 m_instance = new FileCache();
29 FileCache::OpenFile(const FileSpec &file_spec, uint32_t flags, uint32_t mode, Error &error)
31 std::string path(file_spec.GetPath());
34 error.SetErrorString("empty path");
37 FileSP file_sp(new File());
38 error = file_sp->Open(path.c_str(), flags, mode);
39 if (file_sp->IsValid() == false)
41 lldb::user_id_t fd = file_sp->GetDescriptor();
42 m_cache[fd] = file_sp;
47 FileCache::CloseFile(lldb::user_id_t fd, Error &error)
51 error.SetErrorString("invalid file descriptor");
54 FDToFileMap::iterator pos = m_cache.find(fd);
55 if (pos == m_cache.end())
57 error.SetErrorStringWithFormat("invalid host file descriptor %" PRIu64, fd);
60 FileSP file_sp = pos->second;
63 error.SetErrorString("invalid host backing file");
66 error = file_sp->Close();
68 return error.Success();
72 FileCache::WriteFile(lldb::user_id_t fd, uint64_t offset, const void *src, uint64_t src_len, Error &error)
76 error.SetErrorString("invalid file descriptor");
79 FDToFileMap::iterator pos = m_cache.find(fd);
80 if (pos == m_cache.end())
82 error.SetErrorStringWithFormat("invalid host file descriptor %" PRIu64, fd);
85 FileSP file_sp = pos->second;
88 error.SetErrorString("invalid host backing file");
91 if (static_cast<uint64_t>(file_sp->SeekFromStart(offset, &error)) != offset || error.Fail())
93 size_t bytes_written = src_len;
94 error = file_sp->Write(src, bytes_written);
101 FileCache::ReadFile(lldb::user_id_t fd, uint64_t offset, void *dst, uint64_t dst_len, Error &error)
103 if (fd == UINT64_MAX)
105 error.SetErrorString("invalid file descriptor");
108 FDToFileMap::iterator pos = m_cache.find(fd);
109 if (pos == m_cache.end())
111 error.SetErrorStringWithFormat("invalid host file descriptor %" PRIu64, fd);
114 FileSP file_sp = pos->second;
117 error.SetErrorString("invalid host backing file");
120 if (static_cast<uint64_t>(file_sp->SeekFromStart(offset, &error)) != offset || error.Fail())
122 size_t bytes_read = dst_len;
123 error = file_sp->Read(dst, bytes_read);