MCPcopy Create free account
hub / github.com/ElementsProject/elements / Truncate

Function Truncate

src/leveldb/db/fault_injection_test.cc:54–83  ·  view source on GitHub ↗

A basic file truncation function suitable for this test.

Source from the content-addressed store, hash-verified

52
53// A basic file truncation function suitable for this test.
54Status Truncate(const std::string& filename, uint64_t length) {
55 leveldb::Env* env = leveldb::Env::Default();
56
57 SequentialFile* orig_file;
58 Status s = env->NewSequentialFile(filename, &orig_file);
59 if (!s.ok()) return s;
60
61 char* scratch = new char[length];
62 leveldb::Slice result;
63 s = orig_file->Read(length, &result, scratch);
64 delete orig_file;
65 if (s.ok()) {
66 std::string tmp_name = GetDirName(filename) + "/truncate.tmp";
67 WritableFile* tmp_file;
68 s = env->NewWritableFile(tmp_name, &tmp_file);
69 if (s.ok()) {
70 s = tmp_file->Append(result);
71 delete tmp_file;
72 if (s.ok()) {
73 s = env->RenameFile(tmp_name, filename);
74 } else {
75 env->DeleteFile(tmp_name);
76 }
77 }
78 }
79
80 delete[] scratch;
81
82 return s;
83}
84
85struct FileState {
86 std::string filename_;

Callers 2

~FileStateMethod · 0.85
DropUnsyncedDataMethod · 0.85

Calls 7

GetDirNameFunction · 0.85
NewSequentialFileMethod · 0.45
ReadMethod · 0.45
NewWritableFileMethod · 0.45
AppendMethod · 0.45
RenameFileMethod · 0.45
DeleteFileMethod · 0.45

Tested by

no test coverage detected