]> git.proxmox.com Git - ceph.git/blame - ceph/src/rocksdb/file/file_util.cc
buildsys: switch source download to quincy
[ceph.git] / ceph / src / rocksdb / file / file_util.cc
CommitLineData
7c673cae 1// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
11fdf7f2
TL
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).
7c673cae 5//
f67539c2 6#include "file/file_util.h"
7c673cae
FG
7
8#include <string>
9#include <algorithm>
10
f67539c2
TL
11#include "file/random_access_file_reader.h"
12#include "file/sequence_file_reader.h"
13#include "file/sst_file_manager_impl.h"
14#include "file/writable_file_writer.h"
7c673cae 15#include "rocksdb/env.h"
7c673cae 16
f67539c2 17namespace ROCKSDB_NAMESPACE {
7c673cae
FG
18
19// Utility function to copy a file up to a specified length
f67539c2 20Status CopyFile(FileSystem* fs, const std::string& source,
7c673cae 21 const std::string& destination, uint64_t size, bool use_fsync) {
f67539c2 22 const FileOptions soptions;
7c673cae 23 Status s;
494da23a
TL
24 std::unique_ptr<SequentialFileReader> src_reader;
25 std::unique_ptr<WritableFileWriter> dest_writer;
7c673cae
FG
26
27 {
f67539c2
TL
28 std::unique_ptr<FSSequentialFile> srcfile;
29 s = fs->NewSequentialFile(source, soptions, &srcfile, nullptr);
11fdf7f2
TL
30 if (!s.ok()) {
31 return s;
32 }
f67539c2
TL
33 std::unique_ptr<FSWritableFile> destfile;
34 s = fs->NewWritableFile(destination, soptions, &destfile, nullptr);
11fdf7f2
TL
35 if (!s.ok()) {
36 return s;
37 }
7c673cae 38
11fdf7f2
TL
39 if (size == 0) {
40 // default argument means copy everything
f67539c2 41 s = fs->GetFileSize(source, IOOptions(), &size, nullptr);
11fdf7f2
TL
42 if (!s.ok()) {
43 return s;
44 }
7c673cae 45 }
11fdf7f2
TL
46 src_reader.reset(new SequentialFileReader(std::move(srcfile), source));
47 dest_writer.reset(
48 new WritableFileWriter(std::move(destfile), destination, soptions));
7c673cae
FG
49 }
50
51 char buffer[4096];
52 Slice slice;
53 while (size > 0) {
54 size_t bytes_to_read = std::min(sizeof(buffer), static_cast<size_t>(size));
11fdf7f2
TL
55 s = src_reader->Read(bytes_to_read, &slice, buffer);
56 if (!s.ok()) {
57 return s;
7c673cae 58 }
11fdf7f2
TL
59 if (slice.size() == 0) {
60 return Status::Corruption("file too small");
7c673cae 61 }
11fdf7f2 62 s = dest_writer->Append(slice);
7c673cae
FG
63 if (!s.ok()) {
64 return s;
65 }
66 size -= slice.size();
67 }
11fdf7f2 68 return dest_writer->Sync(use_fsync);
7c673cae
FG
69}
70
71// Utility function to create a file with the provided contents
f67539c2 72Status CreateFile(FileSystem* fs, const std::string& destination,
11fdf7f2 73 const std::string& contents, bool use_fsync) {
7c673cae
FG
74 const EnvOptions soptions;
75 Status s;
494da23a 76 std::unique_ptr<WritableFileWriter> dest_writer;
7c673cae 77
f67539c2
TL
78 std::unique_ptr<FSWritableFile> destfile;
79 s = fs->NewWritableFile(destination, soptions, &destfile, nullptr);
7c673cae
FG
80 if (!s.ok()) {
81 return s;
82 }
11fdf7f2
TL
83 dest_writer.reset(
84 new WritableFileWriter(std::move(destfile), destination, soptions));
85 s = dest_writer->Append(Slice(contents));
86 if (!s.ok()) {
87 return s;
88 }
89 return dest_writer->Sync(use_fsync);
7c673cae
FG
90}
91
494da23a 92Status DeleteDBFile(const ImmutableDBOptions* db_options,
f67539c2
TL
93 const std::string& fname, const std::string& dir_to_sync,
94 const bool force_bg, const bool force_fg) {
7c673cae 95#ifndef ROCKSDB_LITE
494da23a 96 SstFileManagerImpl* sfm =
7c673cae 97 static_cast<SstFileManagerImpl*>(db_options->sst_file_manager.get());
f67539c2 98 if (sfm && !force_fg) {
494da23a 99 return sfm->ScheduleFileDeletion(fname, dir_to_sync, force_bg);
7c673cae
FG
100 } else {
101 return db_options->env->DeleteFile(fname);
102 }
103#else
11fdf7f2 104 (void)dir_to_sync;
494da23a 105 (void)force_bg;
f67539c2 106 (void)force_fg;
7c673cae 107 // SstFileManager is not supported in ROCKSDB_LITE
494da23a 108 // Delete file immediately
7c673cae
FG
109 return db_options->env->DeleteFile(fname);
110#endif
111}
112
f67539c2
TL
113bool IsWalDirSameAsDBPath(const ImmutableDBOptions* db_options) {
114 bool same = false;
115 assert(!db_options->db_paths.empty());
116 Status s = db_options->env->AreFilesSame(db_options->wal_dir,
117 db_options->db_paths[0].path, &same);
118 if (s.IsNotSupported()) {
119 same = db_options->wal_dir == db_options->db_paths[0].path;
120 }
121 return same;
122}
123
124} // namespace ROCKSDB_NAMESPACE