Replace Status with IOStatus in CopyFile and CreateFile (#6916)

Summary:
Replace Status with IOStatus in CopyFile and CreateFile.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/6916

Test Plan: pass make asan_check

Reviewed By: cheng-chang

Differential Revision: D21843775

Pulled By: zhichao-cao

fbshipit-source-id: 524d4a0fcf47f0941b923da0346e0de71607f5f6
main
Zhichao Cao 4 years ago committed by Facebook GitHub Bot
parent bfc9737aca
commit 556972e964
  1. 57
      file/file_util.cc
  2. 10
      file/file_util.h

@ -17,30 +17,31 @@
namespace ROCKSDB_NAMESPACE { namespace ROCKSDB_NAMESPACE {
// Utility function to copy a file up to a specified length // Utility function to copy a file up to a specified length
Status CopyFile(FileSystem* fs, const std::string& source, IOStatus CopyFile(FileSystem* fs, const std::string& source,
const std::string& destination, uint64_t size, bool use_fsync) { const std::string& destination, uint64_t size,
bool use_fsync) {
const FileOptions soptions; const FileOptions soptions;
Status s; IOStatus io_s;
std::unique_ptr<SequentialFileReader> src_reader; std::unique_ptr<SequentialFileReader> src_reader;
std::unique_ptr<WritableFileWriter> dest_writer; std::unique_ptr<WritableFileWriter> dest_writer;
{ {
std::unique_ptr<FSSequentialFile> srcfile; std::unique_ptr<FSSequentialFile> srcfile;
s = fs->NewSequentialFile(source, soptions, &srcfile, nullptr); io_s = fs->NewSequentialFile(source, soptions, &srcfile, nullptr);
if (!s.ok()) { if (!io_s.ok()) {
return s; return io_s;
} }
std::unique_ptr<FSWritableFile> destfile; std::unique_ptr<FSWritableFile> destfile;
s = fs->NewWritableFile(destination, soptions, &destfile, nullptr); io_s = fs->NewWritableFile(destination, soptions, &destfile, nullptr);
if (!s.ok()) { if (!io_s.ok()) {
return s; return io_s;
} }
if (size == 0) { if (size == 0) {
// default argument means copy everything // default argument means copy everything
s = fs->GetFileSize(source, IOOptions(), &size, nullptr); io_s = fs->GetFileSize(source, IOOptions(), &size, nullptr);
if (!s.ok()) { if (!io_s.ok()) {
return s; return io_s;
} }
} }
src_reader.reset(new SequentialFileReader(std::move(srcfile), source)); src_reader.reset(new SequentialFileReader(std::move(srcfile), source));
@ -52,16 +53,16 @@ Status CopyFile(FileSystem* fs, const std::string& source,
Slice slice; Slice slice;
while (size > 0) { while (size > 0) {
size_t bytes_to_read = std::min(sizeof(buffer), static_cast<size_t>(size)); size_t bytes_to_read = std::min(sizeof(buffer), static_cast<size_t>(size));
s = src_reader->Read(bytes_to_read, &slice, buffer); io_s = status_to_io_status(src_reader->Read(bytes_to_read, &slice, buffer));
if (!s.ok()) { if (!io_s.ok()) {
return s; return io_s;
} }
if (slice.size() == 0) { if (slice.size() == 0) {
return Status::Corruption("file too small"); return IOStatus::Corruption("file too small");
} }
s = dest_writer->Append(slice); io_s = dest_writer->Append(slice);
if (!s.ok()) { if (!io_s.ok()) {
return s; return io_s;
} }
size -= slice.size(); size -= slice.size();
} }
@ -69,22 +70,22 @@ Status CopyFile(FileSystem* fs, const std::string& source,
} }
// Utility function to create a file with the provided contents // Utility function to create a file with the provided contents
Status CreateFile(FileSystem* fs, const std::string& destination, IOStatus CreateFile(FileSystem* fs, const std::string& destination,
const std::string& contents, bool use_fsync) { const std::string& contents, bool use_fsync) {
const EnvOptions soptions; const EnvOptions soptions;
Status s; IOStatus io_s;
std::unique_ptr<WritableFileWriter> dest_writer; std::unique_ptr<WritableFileWriter> dest_writer;
std::unique_ptr<FSWritableFile> destfile; std::unique_ptr<FSWritableFile> destfile;
s = fs->NewWritableFile(destination, soptions, &destfile, nullptr); io_s = fs->NewWritableFile(destination, soptions, &destfile, nullptr);
if (!s.ok()) { if (!io_s.ok()) {
return s; return io_s;
} }
dest_writer.reset( dest_writer.reset(
new WritableFileWriter(std::move(destfile), destination, soptions)); new WritableFileWriter(std::move(destfile), destination, soptions));
s = dest_writer->Append(Slice(contents)); io_s = dest_writer->Append(Slice(contents));
if (!s.ok()) { if (!io_s.ok()) {
return s; return io_s;
} }
return dest_writer->Sync(use_fsync); return dest_writer->Sync(use_fsync);
} }

@ -16,12 +16,12 @@
namespace ROCKSDB_NAMESPACE { namespace ROCKSDB_NAMESPACE {
// use_fsync maps to options.use_fsync, which determines the way that // use_fsync maps to options.use_fsync, which determines the way that
// the file is synced after copying. // the file is synced after copying.
extern Status CopyFile(FileSystem* fs, const std::string& source, extern IOStatus CopyFile(FileSystem* fs, const std::string& source,
const std::string& destination, uint64_t size, const std::string& destination, uint64_t size,
bool use_fsync); bool use_fsync);
extern Status CreateFile(FileSystem* fs, const std::string& destination, extern IOStatus CreateFile(FileSystem* fs, const std::string& destination,
const std::string& contents, bool use_fsync); const std::string& contents, bool use_fsync);
extern Status DeleteDBFile(const ImmutableDBOptions* db_options, extern Status DeleteDBFile(const ImmutableDBOptions* db_options,
const std::string& fname, const std::string& fname,

Loading…
Cancel
Save