Coverage Report

Created: 2022-01-25 06:29

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Host/common/FileCache.cpp
Line
Count
Source (jump to first uncovered line)
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
9
#include "lldb/Host/FileCache.h"
10
11
#include "lldb/Host/File.h"
12
#include "lldb/Host/FileSystem.h"
13
14
using namespace lldb;
15
using namespace lldb_private;
16
17
FileCache *FileCache::m_instance = nullptr;
18
19
0
FileCache &FileCache::GetInstance() {
20
0
  if (m_instance == nullptr)
21
0
    m_instance = new FileCache();
22
23
0
  return *m_instance;
24
0
}
25
26
lldb::user_id_t FileCache::OpenFile(const FileSpec &file_spec,
27
                                    File::OpenOptions flags, uint32_t mode,
28
0
                                    Status &error) {
29
0
  if (!file_spec) {
30
0
    error.SetErrorString("empty path");
31
0
    return UINT64_MAX;
32
0
  }
33
0
  auto file = FileSystem::Instance().Open(file_spec, flags, mode);
34
0
  if (!file) {
35
0
    error = file.takeError();
36
0
    return UINT64_MAX;
37
0
  }
38
0
  lldb::user_id_t fd = file.get()->GetDescriptor();
39
0
  m_cache[fd] = std::move(file.get());
40
0
  return fd;
41
0
}
42
43
0
bool FileCache::CloseFile(lldb::user_id_t fd, Status &error) {
44
0
  if (fd == UINT64_MAX) {
45
0
    error.SetErrorString("invalid file descriptor");
46
0
    return false;
47
0
  }
48
0
  FDToFileMap::iterator pos = m_cache.find(fd);
49
0
  if (pos == m_cache.end()) {
50
0
    error.SetErrorStringWithFormat("invalid host file descriptor %" PRIu64, fd);
51
0
    return false;
52
0
  }
53
0
  FileUP &file_up = pos->second;
54
0
  if (!file_up) {
55
0
    error.SetErrorString("invalid host backing file");
56
0
    return false;
57
0
  }
58
0
  error = file_up->Close();
59
0
  m_cache.erase(pos);
60
0
  return error.Success();
61
0
}
62
63
uint64_t FileCache::WriteFile(lldb::user_id_t fd, uint64_t offset,
64
                              const void *src, uint64_t src_len,
65
0
                              Status &error) {
66
0
  if (fd == UINT64_MAX) {
67
0
    error.SetErrorString("invalid file descriptor");
68
0
    return UINT64_MAX;
69
0
  }
70
0
  FDToFileMap::iterator pos = m_cache.find(fd);
71
0
  if (pos == m_cache.end()) {
72
0
    error.SetErrorStringWithFormat("invalid host file descriptor %" PRIu64, fd);
73
0
    return false;
74
0
  }
75
0
  FileUP &file_up = pos->second;
76
0
  if (!file_up) {
77
0
    error.SetErrorString("invalid host backing file");
78
0
    return UINT64_MAX;
79
0
  }
80
0
  if (static_cast<uint64_t>(file_up->SeekFromStart(offset, &error)) != offset ||
81
0
      error.Fail())
82
0
    return UINT64_MAX;
83
0
  size_t bytes_written = src_len;
84
0
  error = file_up->Write(src, bytes_written);
85
0
  if (error.Fail())
86
0
    return UINT64_MAX;
87
0
  return bytes_written;
88
0
}
89
90
uint64_t FileCache::ReadFile(lldb::user_id_t fd, uint64_t offset, void *dst,
91
0
                             uint64_t dst_len, Status &error) {
92
0
  if (fd == UINT64_MAX) {
93
0
    error.SetErrorString("invalid file descriptor");
94
0
    return UINT64_MAX;
95
0
  }
96
0
  FDToFileMap::iterator pos = m_cache.find(fd);
97
0
  if (pos == m_cache.end()) {
98
0
    error.SetErrorStringWithFormat("invalid host file descriptor %" PRIu64, fd);
99
0
    return false;
100
0
  }
101
0
  FileUP &file_up = pos->second;
102
0
  if (!file_up) {
103
0
    error.SetErrorString("invalid host backing file");
104
0
    return UINT64_MAX;
105
0
  }
106
0
  if (static_cast<uint64_t>(file_up->SeekFromStart(offset, &error)) != offset ||
107
0
      error.Fail())
108
0
    return UINT64_MAX;
109
0
  size_t bytes_read = dst_len;
110
0
  error = file_up->Read(dst, bytes_read);
111
0
  if (error.Fail())
112
0
    return UINT64_MAX;
113
0
  return bytes_read;
114
0
}