From cfb120f7371da3ae4e1c64fbb383eb96c5426233 Mon Sep 17 00:00:00 2001 From: Andrew Kryczka Date: Fri, 3 Nov 2017 11:48:12 -0700 Subject: [PATCH] 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 --- util/file_util.cc | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/util/file_util.cc b/util/file_util.cc index c6323b35b..80376b6df 100644 --- a/util/file_util.cc +++ b/util/file_util.cc @@ -24,39 +24,39 @@ Status CopyFile(Env* env, const std::string& source, { unique_ptr srcfile; - s = env->NewSequentialFile(source, &srcfile, soptions); - unique_ptr destfile; - if (s.ok()) { + s = env->NewSequentialFile(source, &srcfile, soptions); + if (!s.ok()) { + return s; + } + unique_ptr destfile; s = env->NewWritableFile(destination, &destfile, soptions); - } else { - return s; - } + if (!s.ok()) { + return s; + } - if (size == 0) { - // default argument means copy everything - if (s.ok()) { + if (size == 0) { + // default argument means copy everything s = env->GetFileSize(source, &size); - } else { - return s; + if (!s.ok()) { + return s; + } } - } - src_reader.reset(new SequentialFileReader(std::move(srcfile))); - dest_writer.reset(new WritableFileWriter(std::move(destfile), soptions)); + src_reader.reset(new SequentialFileReader(std::move(srcfile))); + dest_writer.reset(new WritableFileWriter(std::move(destfile), soptions)); } char buffer[4096]; Slice slice; while (size > 0) { size_t bytes_to_read = std::min(sizeof(buffer), static_cast(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) { - return Status::Corruption("file too small"); - } - s = dest_writer->Append(slice); + if (slice.size() == 0) { + return Status::Corruption("file too small"); } + s = dest_writer->Append(slice); if (!s.ok()) { return s; }