fix CopyFile status checks

Summary:
copied from internal diff D6156261
Closes https://github.com/facebook/rocksdb/pull/3124

Differential Revision: D6230167

Pulled By: ajkr

fbshipit-source-id: 17926bb1152d607556364e3aacfec0ef3c115748
main
Andrew Kryczka 7 years ago committed by Facebook Github Bot
parent d956169563
commit cfb120f737
  1. 42
      util/file_util.cc

@ -24,39 +24,39 @@ Status CopyFile(Env* env, const std::string& source,
{ {
unique_ptr<SequentialFile> srcfile; unique_ptr<SequentialFile> srcfile;
s = env->NewSequentialFile(source, &srcfile, soptions); s = env->NewSequentialFile(source, &srcfile, soptions);
unique_ptr<WritableFile> destfile; if (!s.ok()) {
if (s.ok()) { return s;
}
unique_ptr<WritableFile> destfile;
s = env->NewWritableFile(destination, &destfile, soptions); s = env->NewWritableFile(destination, &destfile, soptions);
} else { if (!s.ok()) {
return s; return s;
} }
if (size == 0) { if (size == 0) {
// default argument means copy everything // default argument means copy everything
if (s.ok()) {
s = env->GetFileSize(source, &size); s = env->GetFileSize(source, &size);
} else { if (!s.ok()) {
return s; return s;
}
} }
} src_reader.reset(new SequentialFileReader(std::move(srcfile)));
src_reader.reset(new SequentialFileReader(std::move(srcfile))); dest_writer.reset(new WritableFileWriter(std::move(destfile), soptions));
dest_writer.reset(new WritableFileWriter(std::move(destfile), soptions));
} }
char buffer[4096]; char buffer[4096];
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));
if (s.ok()) { s = src_reader->Read(bytes_to_read, &slice, buffer);
s = src_reader->Read(bytes_to_read, &slice, buffer); if (!s.ok()) {
return s;
} }
if (s.ok()) { if (slice.size() == 0) {
if (slice.size() == 0) { return Status::Corruption("file too small");
return Status::Corruption("file too small");
}
s = dest_writer->Append(slice);
} }
s = dest_writer->Append(slice);
if (!s.ok()) { if (!s.ok()) {
return s; return s;
} }

Loading…
Cancel
Save