A basic file truncation function suitable for this test.
| 52 | |
| 53 | // A basic file truncation function suitable for this test. |
| 54 | Status 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 | |
| 85 | struct FileState { |
| 86 | std::string filename_; |
no test coverage detected