]>
git.proxmox.com Git - ceph.git/blob - 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).
6 #include "util/file_util.h"
11 #include "rocksdb/env.h"
12 #include "util/sst_file_manager_impl.h"
13 #include "util/file_reader_writer.h"
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
;
22 std::unique_ptr
<SequentialFileReader
> src_reader
;
23 std::unique_ptr
<WritableFileWriter
> dest_writer
;
26 std::unique_ptr
<SequentialFile
> srcfile
;
27 s
= env
->NewSequentialFile(source
, &srcfile
, soptions
);
31 std::unique_ptr
<WritableFile
> destfile
;
32 s
= env
->NewWritableFile(destination
, &destfile
, soptions
);
38 // default argument means copy everything
39 s
= env
->GetFileSize(source
, &size
);
44 src_reader
.reset(new SequentialFileReader(std::move(srcfile
), source
));
46 new WritableFileWriter(std::move(destfile
), destination
, soptions
));
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
);
57 if (slice
.size() == 0) {
58 return Status::Corruption("file too small");
60 s
= dest_writer
->Append(slice
);
66 return dest_writer
->Sync(use_fsync
);
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
;
74 std::unique_ptr
<WritableFileWriter
> dest_writer
;
76 std::unique_ptr
<WritableFile
> destfile
;
77 s
= env
->NewWritableFile(destination
, &destfile
, soptions
);
82 new WritableFileWriter(std::move(destfile
), destination
, soptions
));
83 s
= dest_writer
->Append(Slice(contents
));
87 return dest_writer
->Sync(use_fsync
);
90 Status
DeleteDBFile(const ImmutableDBOptions
* db_options
,
91 const std::string
& fname
, const std::string
& dir_to_sync
,
92 const bool force_bg
) {
94 SstFileManagerImpl
* sfm
=
95 static_cast<SstFileManagerImpl
*>(db_options
->sst_file_manager
.get());
97 return sfm
->ScheduleFileDeletion(fname
, dir_to_sync
, force_bg
);
99 return db_options
->env
->DeleteFile(fname
);
104 // SstFileManager is not supported in ROCKSDB_LITE
105 // Delete file immediately
106 return db_options
->env
->DeleteFile(fname
);
110 } // namespace rocksdb