Coverage Report

Created: 2025-05-14 12:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/leveldb/util/env.cc
Line
Count
Source
1
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style license that can be
3
// found in the LICENSE file. See the AUTHORS file for names of contributors.
4
5
#include "leveldb/env.h"
6
7
namespace leveldb {
8
9
5.34k
Env::~Env() = default;
10
11
0
Status Env::NewAppendableFile(const std::string& fname, WritableFile** result) {
12
0
  return Status::NotSupported("NewAppendableFile", fname);
13
0
}
14
15
10.6k
SequentialFile::~SequentialFile() = default;
16
17
201
RandomAccessFile::~RandomAccessFile() = default;
18
19
27.1k
WritableFile::~WritableFile() = default;
20
21
5.34k
Logger::~Logger() = default;
22
23
5.34k
FileLock::~FileLock() = default;
24
25
5.74k
void Log(Logger* info_log, const char* format, ...) {
26
5.74k
  if (info_log != nullptr) {
27
5.74k
    va_list ap;
28
5.74k
    va_start(ap, format);
29
5.74k
    info_log->Logv(format, ap);
30
5.74k
    va_end(ap);
31
5.74k
  }
32
5.74k
}
33
34
static Status DoWriteStringToFile(Env* env, const Slice& data,
35
10.6k
                                  const std::string& fname, bool should_sync) {
36
10.6k
  WritableFile* file;
37
10.6k
  Status s = env->NewWritableFile(fname, &file);
38
10.6k
  if (!s.ok()) {
39
0
    return s;
40
0
  }
41
10.6k
  s = file->Append(data);
42
10.6k
  if (s.ok() && should_sync) {
43
10.6k
    s = file->Sync();
44
10.6k
  }
45
10.6k
  if (s.ok()) {
46
10.6k
    s = file->Close();
47
10.6k
  }
48
10.6k
  delete file;  // Will auto-close if we did not close above
49
10.6k
  if (!s.ok()) {
50
0
    env->DeleteFile(fname);
51
0
  }
52
10.6k
  return s;
53
10.6k
}
54
55
Status WriteStringToFile(Env* env, const Slice& data,
56
0
                         const std::string& fname) {
57
0
  return DoWriteStringToFile(env, data, fname, false);
58
0
}
59
60
Status WriteStringToFileSync(Env* env, const Slice& data,
61
10.6k
                             const std::string& fname) {
62
10.6k
  return DoWriteStringToFile(env, data, fname, true);
63
10.6k
}
64
65
5.34k
Status ReadFileToString(Env* env, const std::string& fname, std::string* data) {
66
5.34k
  data->clear();
67
5.34k
  SequentialFile* file;
68
5.34k
  Status s = env->NewSequentialFile(fname, &file);
69
5.34k
  if (!s.ok()) {
70
0
    return s;
71
0
  }
72
5.34k
  static const int kBufferSize = 8192;
73
5.34k
  char* space = new char[kBufferSize];
74
10.6k
  while (true) {
75
10.6k
    Slice fragment;
76
10.6k
    s = file->Read(kBufferSize, &fragment, space);
77
10.6k
    if (!s.ok()) {
78
0
      break;
79
0
    }
80
10.6k
    data->append(fragment.data(), fragment.size());
81
10.6k
    if (fragment.empty()) {
82
5.34k
      break;
83
5.34k
    }
84
10.6k
  }
85
5.34k
  delete[] space;
86
5.34k
  delete file;
87
5.34k
  return s;
88
5.34k
}
89
90
EnvWrapper::~EnvWrapper() {}
91
92
}  // namespace leveldb