@@ -106,7 +106,7 @@ class Win32RandomAccessFile : public RandomAccessFile
106
106
class Win32WritableFile : public WritableFile
107
107
{
108
108
public:
109
- Win32WritableFile (const std::string& fname);
109
+ Win32WritableFile (const std::string& fname, bool append );
110
110
~Win32WritableFile ();
111
111
112
112
virtual Status Append (const Slice& data);
@@ -158,6 +158,8 @@ class Win32Env : public Env
158
158
RandomAccessFile** result);
159
159
virtual Status NewWritableFile (const std::string& fname,
160
160
WritableFile** result);
161
+ virtual Status NewAppendableFile (const std::string& fname,
162
+ WritableFile** result);
161
163
162
164
virtual bool FileExists (const std::string& fname);
163
165
@@ -423,17 +425,23 @@ void Win32RandomAccessFile::_CleanUp()
423
425
}
424
426
}
425
427
426
- Win32WritableFile::Win32WritableFile (const std::string& fname)
428
+ Win32WritableFile::Win32WritableFile (const std::string& fname, bool append )
427
429
: filename_(fname)
428
430
{
429
431
std::wstring path;
430
432
ToWidePath (fname, path);
431
- DWORD Flag = PathFileExistsW (path.c_str ()) ? OPEN_EXISTING : CREATE_ALWAYS;
433
+ // NewAppendableFile: append to an existing file, or create a new one
434
+ // if none exists - this is OPEN_ALWAYS behavior, with
435
+ // FILE_APPEND_DATA to avoid having to manually position the file
436
+ // pointer at the end of the file.
437
+ // NewWritableFile: create a new file, delete if it exists - this is
438
+ // CREATE_ALWAYS behavior. This file is used for writing only so
439
+ // use GENERIC_WRITE.
432
440
_hFile = CreateFileW (path.c_str (),
433
- GENERIC_READ | GENERIC_WRITE,
441
+ append ? FILE_APPEND_DATA : GENERIC_WRITE,
434
442
FILE_SHARE_READ|FILE_SHARE_DELETE|FILE_SHARE_WRITE,
435
443
NULL ,
436
- Flag ,
444
+ append ? OPEN_ALWAYS : CREATE_ALWAYS ,
437
445
FILE_ATTRIBUTE_NORMAL,
438
446
NULL );
439
447
// CreateFileW returns INVALID_HANDLE_VALUE in case of error, always check isEnable() before use
@@ -823,7 +831,9 @@ Status Win32Env::NewLogger( const std::string& fname, Logger** result )
823
831
{
824
832
Status sRet ;
825
833
std::string path = fname;
826
- Win32WritableFile* pMapFile = new Win32WritableFile (ModifyPath (path));
834
+ // Logs are opened with write semantics, not with append semantics
835
+ // (see PosixEnv::NewLogger)
836
+ Win32WritableFile* pMapFile = new Win32WritableFile (ModifyPath (path), false );
827
837
if (!pMapFile->isEnable ()){
828
838
delete pMapFile;
829
839
*result = NULL ;
@@ -837,7 +847,20 @@ Status Win32Env::NewWritableFile( const std::string& fname, WritableFile** resul
837
847
{
838
848
Status sRet ;
839
849
std::string path = fname;
840
- Win32WritableFile* pFile = new Win32WritableFile (ModifyPath (path));
850
+ Win32WritableFile* pFile = new Win32WritableFile (ModifyPath (path), false );
851
+ if (!pFile->isEnable ()){
852
+ *result = NULL ;
853
+ sRet = Status::IOError (fname,Win32::GetLastErrSz ());
854
+ }else
855
+ *result = pFile;
856
+ return sRet ;
857
+ }
858
+
859
+ Status Win32Env::NewAppendableFile ( const std::string& fname, WritableFile** result )
860
+ {
861
+ Status sRet ;
862
+ std::string path = fname;
863
+ Win32WritableFile* pFile = new Win32WritableFile (ModifyPath (path), true );
841
864
if (!pFile->isEnable ()){
842
865
*result = NULL ;
843
866
sRet = Status::IOError (fname,Win32::GetLastErrSz ());
0 commit comments