Path: blob/main/contrib/llvm-project/lldb/source/Host/common/FileCache.cpp
39606 views
//===-- FileCache.cpp -----------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/Host/FileCache.h"910#include "lldb/Host/File.h"11#include "lldb/Host/FileSystem.h"1213using namespace lldb;14using namespace lldb_private;1516FileCache *FileCache::m_instance = nullptr;1718FileCache &FileCache::GetInstance() {19if (m_instance == nullptr)20m_instance = new FileCache();2122return *m_instance;23}2425lldb::user_id_t FileCache::OpenFile(const FileSpec &file_spec,26File::OpenOptions flags, uint32_t mode,27Status &error) {28if (!file_spec) {29error.SetErrorString("empty path");30return UINT64_MAX;31}32auto file = FileSystem::Instance().Open(file_spec, flags, mode);33if (!file) {34error = file.takeError();35return UINT64_MAX;36}37lldb::user_id_t fd = file.get()->GetDescriptor();38m_cache[fd] = std::move(file.get());39return fd;40}4142bool FileCache::CloseFile(lldb::user_id_t fd, Status &error) {43if (fd == UINT64_MAX) {44error.SetErrorString("invalid file descriptor");45return false;46}47FDToFileMap::iterator pos = m_cache.find(fd);48if (pos == m_cache.end()) {49error.SetErrorStringWithFormat("invalid host file descriptor %" PRIu64, fd);50return false;51}52FileUP &file_up = pos->second;53if (!file_up) {54error.SetErrorString("invalid host backing file");55return false;56}57error = file_up->Close();58m_cache.erase(pos);59return error.Success();60}6162uint64_t FileCache::WriteFile(lldb::user_id_t fd, uint64_t offset,63const void *src, uint64_t src_len,64Status &error) {65if (fd == UINT64_MAX) {66error.SetErrorString("invalid file descriptor");67return UINT64_MAX;68}69FDToFileMap::iterator pos = m_cache.find(fd);70if (pos == m_cache.end()) {71error.SetErrorStringWithFormat("invalid host file descriptor %" PRIu64, fd);72return false;73}74FileUP &file_up = pos->second;75if (!file_up) {76error.SetErrorString("invalid host backing file");77return UINT64_MAX;78}79if (static_cast<uint64_t>(file_up->SeekFromStart(offset, &error)) != offset ||80error.Fail())81return UINT64_MAX;82size_t bytes_written = src_len;83error = file_up->Write(src, bytes_written);84if (error.Fail())85return UINT64_MAX;86return bytes_written;87}8889uint64_t FileCache::ReadFile(lldb::user_id_t fd, uint64_t offset, void *dst,90uint64_t dst_len, Status &error) {91if (fd == UINT64_MAX) {92error.SetErrorString("invalid file descriptor");93return UINT64_MAX;94}95FDToFileMap::iterator pos = m_cache.find(fd);96if (pos == m_cache.end()) {97error.SetErrorStringWithFormat("invalid host file descriptor %" PRIu64, fd);98return false;99}100FileUP &file_up = pos->second;101if (!file_up) {102error.SetErrorString("invalid host backing file");103return UINT64_MAX;104}105if (static_cast<uint64_t>(file_up->SeekFromStart(offset, &error)) != offset ||106error.Fail())107return UINT64_MAX;108size_t bytes_read = dst_len;109error = file_up->Read(dst, bytes_read);110if (error.Fail())111return UINT64_MAX;112return bytes_read;113}114115116