@ -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 ;
IO Status 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 IO Status: : 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 ,
IO Status 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 ;
IO Status 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 ) ;
}
}