Coverage Report

Created: 2023-09-30 09:22

/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
3
FileCache &FileCache::GetInstance() {
20
3
  if (m_instance == nullptr)
21
1
    m_instance = new FileCache();
22
23
3
  return *m_instance;
24
3
}
25
26
lldb::user_id_t FileCache::OpenFile(const FileSpec &file_spec,
27
                                    File::OpenOptions flags, uint32_t mode,
28
1
                                    Status &error) {
29
1
  if (!file_spec) {
30
0
    error.SetErrorString("empty path");
31
0
    return UINT64_MAX;
32
0
  }
33
1
  auto file = FileSystem::Instance().Open(file_spec, flags, mode);
34
1
  if (!file) {
35
0
    error = file.takeError();
36
0
    return UINT64_MAX;
37
0
  }
38
1
  lldb::user_id_t fd = file.get()->GetDescriptor();
39
1
  m_cache[fd] = std::move(file.get());
40
1
  return fd;
41
1
}
42
43
1
bool FileCache::CloseFile(lldb::user_id_t fd, Status &error) {
44
1
  if (fd == UINT64_MAX) {
45
0
    error.SetErrorString("invalid file descriptor");
46
0
    return false;
47
0
  }
48
1
  FDToFileMap::iterator pos = m_cache.find(fd);
49
1
  if (pos == m_cache.end()) {
50
0
    error.SetErrorStringWithFormat("invalid host file descriptor %" PRIu64, fd);
51
0
    return false;
52
0
  }
53
1
  FileUP &file_up = pos->second;
54
1
  if (!file_up) {
55
0
    error.SetErrorString("invalid host backing file");
56
0
    return false;
57
0
  }
58
1
  error = file_up->Close();
59
1
  m_cache.erase(pos);
60
1
  return error.Success();
61
1
}
62
63
uint64_t FileCache::WriteFile(lldb::user_id_t fd, uint64_t offset,
64
                              const void *src, uint64_t src_len,
65
1
                              Status &error) {
66
1
  if (fd == UINT64_MAX) {
67
0
    error.SetErrorString("invalid file descriptor");
68
0
    return UINT64_MAX;
69
0
  }
70
1
  FDToFileMap::iterator pos = m_cache.find(fd);
71
1
  if (pos == m_cache.end()) {
72
0
    error.SetErrorStringWithFormat("invalid host file descriptor %" PRIu64, fd);
73
0
    return false;
74
0
  }
75
1
  FileUP &file_up = pos->second;
76
1
  if (!file_up) {
77
0
    error.SetErrorString("invalid host backing file");
78
0
    return UINT64_MAX;
79
0
  }
80
1
  if (static_cast<uint64_t>(file_up->SeekFromStart(offset, &error)) != offset ||
81
1
      error.Fail())
82
0
    return UINT64_MAX;
83
1
  size_t bytes_written = src_len;
84
1
  error = file_up->Write(src, bytes_written);
85
1
  if (error.Fail())
86
0
    return UINT64_MAX;
87
1
  return bytes_written;
88
1
}
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
}