Coverage Report

Created: 2024-10-29 12:10

/root/bitcoin/src/leveldb/util/env.cc
Line
Count
Source (jump to first uncovered line)
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
2
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
0
SequentialFile::~SequentialFile() = default;
16
17
0
RandomAccessFile::~RandomAccessFile() = default;
18
19
4
WritableFile::~WritableFile() = default;
20
21
2
Logger::~Logger() = default;
22
23
2
FileLock::~FileLock() = default;
24
25
0
void Log(Logger* info_log, const char* format, ...) {
26
0
  if (info_log != nullptr) {
27
0
    va_list ap;
28
0
    va_start(ap, format);
29
0
    info_log->Logv(format, ap);
30
0
    va_end(ap);
31
0
  }
32
0
}
33
34
static Status DoWriteStringToFile(Env* env, const Slice& data,
35
0
                                  const std::string& fname, bool should_sync) {
36
0
  WritableFile* file;
37
0
  Status s = env->NewWritableFile(fname, &file);
38
0
  if (!s.ok()) {
39
0
    return s;
40
0
  }
41
0
  s = file->Append(data);
42
0
  if (s.ok() && should_sync) {
43
0
    s = file->Sync();
44
0
  }
45
0
  if (s.ok()) {
46
0
    s = file->Close();
47
0
  }
48
0
  delete file;  // Will auto-close if we did not close above
49
0
  if (!s.ok()) {
50
0
    env->DeleteFile(fname);
51
0
  }
52
0
  return s;
53
0
}
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
0
                             const std::string& fname) {
62
0
  return DoWriteStringToFile(env, data, fname, true);
63
0
}
64
65
0
Status ReadFileToString(Env* env, const std::string& fname, std::string* data) {
66
0
  data->clear();
67
0
  SequentialFile* file;
68
0
  Status s = env->NewSequentialFile(fname, &file);
69
0
  if (!s.ok()) {
70
0
    return s;
71
0
  }
72
0
  static const int kBufferSize = 8192;
73
0
  char* space = new char[kBufferSize];
74
0
  while (true) {
75
0
    Slice fragment;
76
0
    s = file->Read(kBufferSize, &fragment, space);
77
0
    if (!s.ok()) {
78
0
      break;
79
0
    }
80
0
    data->append(fragment.data(), fragment.size());
81
0
    if (fragment.empty()) {
82
0
      break;
83
0
    }
84
0
  }
85
0
  delete[] space;
86
0
  delete file;
87
0
  return s;
88
0
}
89
90
EnvWrapper::~EnvWrapper() {}
91
92
}  // namespace leveldb