@ -207,118 +207,6 @@ inline Status ftruncate(const std::string& filename, HANDLE hFile,
return status ;
}
class WinRandomRWFile : public RandomRWFile {
const std : : string filename_ ;
HANDLE hFile_ ;
bool pending_fsync_ ;
public :
WinRandomRWFile ( const std : : string & fname , HANDLE hFile ,
const EnvOptions & options )
: filename_ ( fname ) , hFile_ ( hFile ) , pending_fsync_ ( false ) {
assert ( ! options . use_mmap_writes & & ! options . use_mmap_reads ) ;
}
~ WinRandomRWFile ( ) {
if ( hFile_ ! = INVALID_HANDLE_VALUE & & hFile_ ! = NULL ) {
: : CloseHandle ( hFile_ ) ;
}
}
virtual Status Write ( uint64_t offset , const Slice & data ) override {
const char * src = data . data ( ) ;
size_t left = data . size ( ) ;
pending_fsync_ = true ;
SSIZE_T done = 0 ;
{
IOSTATS_TIMER_GUARD ( write_nanos ) ;
done = pwrite ( hFile_ , src , left , offset ) ;
}
if ( done < 0 ) {
return IOErrorFromWindowsError ( " pwrite failed to: " + filename_ ,
GetLastError ( ) ) ;
}
IOSTATS_ADD ( bytes_written , done ) ;
left - = done ;
src + = done ;
offset + = done ;
return Status : : OK ( ) ;
}
virtual Status Read ( uint64_t offset , size_t n , Slice * result ,
char * scratch ) const override {
Status s ;
SSIZE_T r = - 1 ;
char * ptr = scratch ;
size_t left = n ;
while ( left > 0 ) {
{
IOSTATS_TIMER_GUARD ( read_nanos ) ;
r = pread ( hFile_ , ptr , n , offset ) ;
}
if ( r < = 0 ) {
break ;
}
ptr + = r ;
offset + = r ;
left - = r ;
}
IOSTATS_ADD_IF_POSITIVE ( bytes_read , n - left ) ;
* result = Slice ( scratch , ( r < 0 ) ? 0 : r ) ;
if ( r < 0 ) {
s = IOErrorFromWindowsError ( " pread failed from: " + filename_ ,
GetLastError ( ) ) ;
}
return s ;
}
virtual Status Close ( ) override {
Status s = Status : : OK ( ) ;
if ( hFile_ ! = INVALID_HANDLE_VALUE & & : : CloseHandle ( hFile_ ) = = FALSE ) {
s = IOErrorFromWindowsError ( " Failed to close file: " + filename_ ,
GetLastError ( ) ) ;
}
hFile_ = INVALID_HANDLE_VALUE ;
return s ;
}
virtual Status Sync ( ) override {
if ( pending_fsync_ & & fsync ( hFile_ ) < 0 ) {
return IOErrorFromWindowsError (
" Failed to Sync() buffers for: " + filename_ , GetLastError ( ) ) ;
}
pending_fsync_ = false ;
return Status : : OK ( ) ;
}
virtual Status Fsync ( ) override {
if ( pending_fsync_ & & fsync ( hFile_ ) < 0 ) {
return IOErrorFromWindowsError ( " Failed to Fsync() for: " + filename_ ,
GetLastError ( ) ) ;
}
pending_fsync_ = false ;
return Status : : OK ( ) ;
}
virtual Status Allocate ( off_t offset , off_t len ) override {
IOSTATS_TIMER_GUARD ( allocate_nanos ) ;
return fallocate ( filename_ , hFile_ , len ) ;
}
} ;
// mmap() based random-access
class WinMmapReadableFile : public RandomAccessFile {
const std : : string fileName_ ;
@ -1616,38 +1504,6 @@ class WinEnv : public Env {
return s ;
}
virtual Status NewRandomRWFile ( const std : : string & fname ,
std : : unique_ptr < RandomRWFile > * result ,
const EnvOptions & options ) override {
result - > reset ( ) ;
// no support for mmap yet (same as POSIX env)
if ( options . use_mmap_writes | | options . use_mmap_reads ) {
return Status : : NotSupported ( " No support for mmap read/write yet " ) ;
}
Status s ;
HANDLE hFile = 0 ;
{
IOSTATS_TIMER_GUARD ( open_nanos ) ;
hFile = CreateFileA ( fname . c_str ( ) , GENERIC_READ | GENERIC_WRITE ,
FILE_SHARE_READ , NULL ,
OPEN_ALWAYS , // Posix env specifies O_CREAT, it will
// open existing file or create new
FILE_ATTRIBUTE_NORMAL , NULL ) ;
}
if ( INVALID_HANDLE_VALUE = = hFile ) {
auto lastError = GetLastError ( ) ;
s = IOErrorFromWindowsError (
" Failed to Open/Create NewRandomRWFile " + fname , lastError ) ;
} else {
result - > reset ( new WinRandomRWFile ( fname , hFile , options ) ) ;
}
return s ;
}
virtual Status NewDirectory ( const std : : string & name ,
std : : unique_ptr < Directory > * result ) override {
Status s ;