]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/util/file_util.cc
import 14.2.4 nautilus point release
[ceph.git] / ceph / src / rocksdb / util / file_util.cc
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
5 //
6 #include "util/file_util.h"
7
8 #include <string>
9 #include <algorithm>
10
11 #include "rocksdb/env.h"
12 #include "util/sst_file_manager_impl.h"
13 #include "util/file_reader_writer.h"
14
15 namespace rocksdb {
16
17 // Utility function to copy a file up to a specified length
18 Status CopyFile(Env* env, const std::string& source,
19 const std::string& destination, uint64_t size, bool use_fsync) {
20 const EnvOptions soptions;
21 Status s;
22 std::unique_ptr<SequentialFileReader> src_reader;
23 std::unique_ptr<WritableFileWriter> dest_writer;
24
25 {
26 std::unique_ptr<SequentialFile> srcfile;
27 s = env->NewSequentialFile(source, &srcfile, soptions);
28 if (!s.ok()) {
29 return s;
30 }
31 std::unique_ptr<WritableFile> destfile;
32 s = env->NewWritableFile(destination, &destfile, soptions);
33 if (!s.ok()) {
34 return s;
35 }
36
37 if (size == 0) {
38 // default argument means copy everything
39 s = env->GetFileSize(source, &size);
40 if (!s.ok()) {
41 return s;
42 }
43 }
44 src_reader.reset(new SequentialFileReader(std::move(srcfile), source));
45 dest_writer.reset(
46 new WritableFileWriter(std::move(destfile), destination, soptions));
47 }
48
49 char buffer[4096];
50 Slice slice;
51 while (size > 0) {
52 size_t bytes_to_read = std::min(sizeof(buffer), static_cast<size_t>(size));
53 s = src_reader->Read(bytes_to_read, &slice, buffer);
54 if (!s.ok()) {
55 return s;
56 }
57 if (slice.size() == 0) {
58 return Status::Corruption("file too small");
59 }
60 s = dest_writer->Append(slice);
61 if (!s.ok()) {
62 return s;
63 }
64 size -= slice.size();
65 }
66 return dest_writer->Sync(use_fsync);
67 }
68
69 // Utility function to create a file with the provided contents
70 Status CreateFile(Env* env, const std::string& destination,
71 const std::string& contents, bool use_fsync) {
72 const EnvOptions soptions;
73 Status s;
74 std::unique_ptr<WritableFileWriter> dest_writer;
75
76 std::unique_ptr<WritableFile> destfile;
77 s = env->NewWritableFile(destination, &destfile, soptions);
78 if (!s.ok()) {
79 return s;
80 }
81 dest_writer.reset(
82 new WritableFileWriter(std::move(destfile), destination, soptions));
83 s = dest_writer->Append(Slice(contents));
84 if (!s.ok()) {
85 return s;
86 }
87 return dest_writer->Sync(use_fsync);
88 }
89
90 Status DeleteDBFile(const ImmutableDBOptions* db_options,
91 const std::string& fname, const std::string& dir_to_sync,
92 const bool force_bg) {
93 #ifndef ROCKSDB_LITE
94 SstFileManagerImpl* sfm =
95 static_cast<SstFileManagerImpl*>(db_options->sst_file_manager.get());
96 if (sfm) {
97 return sfm->ScheduleFileDeletion(fname, dir_to_sync, force_bg);
98 } else {
99 return db_options->env->DeleteFile(fname);
100 }
101 #else
102 (void)dir_to_sync;
103 (void)force_bg;
104 // SstFileManager is not supported in ROCKSDB_LITE
105 // Delete file immediately
106 return db_options->env->DeleteFile(fname);
107 #endif
108 }
109
110 } // namespace rocksdb