NOINLINE bln Files::MoveObjectTo( const FilePath &sourcePnn, const FilePath &targetPnn, bln is_replace, CError *error ) { FilePath source = sourcePnn, target = targetPnn; bln result = false; CError retError; BOOL wapiResult; DWORD flags = MOVEFILE_COPY_ALLOWED | MOVEFILE_WRITE_THROUGH; if( is_replace ) { flags |= MOVEFILE_REPLACE_EXISTING; } source.MakeAbsolute(); target.MakeAbsolute(); if( !source.IsValid() || !target.IsValid() || source == target ) { retError = Error::InvalidArgument(); goto toRet; } wapiResult = ::MoveFileExW( source.PlatformPath(), target.PlatformPath(), flags ); if( wapiResult != TRUE ) { retError = Error::UnknownError(); goto toRet; } result = true; toRet: DSA( error, retError ); return result; }
NOINLINE bln Files::RemoveFile( const FilePath &pnn, CError *po_error ) { CError retError; bln funcResult = false; if( !Files::IsFile( pnn, &retError ) ) // will check the pnn { if( retError == Error::Ok() ) { retError = Error::InvalidArgument(); } goto toExit; } funcResult = ::DeleteFileW( pnn.PlatformPath() ) != 0; if( funcResult == false ) { switch( ::GetLastError() ) { case ERROR_ACCESS_DENIED: retError = Error::NoAccess(); break; case ERROR_FILE_NOT_FOUND: retError = Error::NoAccess(); break; default: retError = Error::UnknownError(); } } toExit: DSA( po_error, retError ); return funcResult; }
bln Files::CopyObjectTo( const FilePath &sourcePnn, const FilePath &targetPnn, bln is_replace, CError *error ) { CError retError; BOOL result = FALSE; if( !sourcePnn.IsValid() || !targetPnn.IsValid() ) { retError = Error::InvalidArgument(); goto toExit; } // TODO: Windows 7, Windows Server 2008 R2, Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP: Security resource properties for the existing file are not copied to the new file until Windows 8 and Windows Server 2012. result = ::CopyFileW( sourcePnn.PlatformPath(), targetPnn.PlatformPath(), is_replace ? FALSE : TRUE ); if( result == FALSE ) { retError = StdLib_FileError(); } toExit: DSA( error, retError ); return result == TRUE; }
bln FileCFILEStream::Open( const FilePath &path, FileOpenMode::mode_t openMode, FileProcMode::mode_t procMode, FileCacheMode::mode_t cacheMode, fileError *error ) { this->Close(); bln is_fileExists = Files::IsExists( path ); if( openMode == FileOpenMode::OpenExisting ) { if( !is_fileExists ) { DSA( error, Error::DoesNotExist() ); return false; } } if( openMode == FileOpenMode::CreateNew ) { if( is_fileExists ) { DSA( error, Error::AlreadyExists() ); return false; } } if( openMode == FileOpenMode::CreateAlways ) { if( !(procMode & FileProcMode::Write) ) { DSA( error, fileError( Error::InvalidArgument(), "FileOpenMode::CreateAlways cannot be used without FileProcMode::Write" ) ); return false; } } TCStr < pathChar > procModeStr; if( (procMode & FileProcMode::Read) && (procMode & FileProcMode::Write) ) { procModeStr += is_fileExists ? PLATFORM_PATH( "r+" ) : PLATFORM_PATH( "w+" ); } else if( procMode & FileProcMode::Read ) { procModeStr += PLATFORM_PATH( "r" ); } else { ASSUME( procMode & FileProcMode::Write ); procModeStr += PLATFORM_PATH( "w" ); } procModeStr += PLATFORM_PATH( "b" ); _file = fopen( path.PlatformPath(), procModeStr.CStr() ); if( !_file ) { DSA( error, fileError( Error::UnknownError(), "fopen has failed" ) ); return false; } if( (cacheMode & FileCacheMode::DisableSystemWriteCache) && (procMode & FileProcMode::Write) ) { if( setvbuf( (FILE *)_file, 0, _IONBF, 0 ) != 0 ) { DSA( error, fileError( Error::UnknownError(), "setvbuf has failed, cannot disable caching" ) ); fclose( (FILE *)_file ); _file = 0; return false; } } _procMode = procMode; _openMode = openMode; _cacheMode = cacheMode; _offsetToStart = 0; _bufferSize = 0; _customBufferPtr = 0; if( FileProcMode::WriteAppend & procMode ) { _offsetToStart = ftell( (FILE *)_file ); if( _offsetToStart == -1 ) { DSA( error, fileError( Error::UnknownError(), "ftell has failed" ) ); return false; } } DSA( error, Error::Ok() ); return true; }
NOINLINE bln Files::RemoveFolder( const FilePath &path, CError *po_error ) // potentially recursive { uiw len; CWStr buf; WIN32_FIND_DATAW o_find; HANDLE h_find; CError o_error; bln funcResult = false; if( !Files::IsFolder( path, &o_error ) ) // will check the pnn { if( o_error == Error::Ok() ) { o_error = Error::InvalidArgument(); } goto toExit; } buf = path.PlatformPath(); len = buf.Size() + 1; buf += L"\\*"; h_find = ::FindFirstFileW( buf.CStr(), &o_find ); if( h_find == INVALID_HANDLE_VALUE ) { goto toExit; } do { if( !::wcscmp( o_find.cFileName, L"." ) || !::wcscmp( o_find.cFileName, L".." ) ) { continue; } buf.Resize( len ); buf += o_find.cFileName; if( o_find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) { if( !RemoveFolder( buf.CStr(), po_error ) ) { ::FindClose( h_find ); goto toExit; } } else if( !RemoveFile( buf.CStr(), po_error ) ) { ::FindClose( h_find ); goto toExit; } } while( ::FindNextFileW( h_find, &o_find ) ); ::FindClose( h_find ); funcResult = ::RemoveDirectoryW( path.PlatformPath() ) != 0; if( !funcResult ) { o_error = Error::UnknownError(); } toExit: DSA( po_error, o_error ); return funcResult; }