/**---------------------------------------------------------------------------- \brief \param \return \code \endcode -----------------------------------------------------------------------------*/ DTSTATUS FileIoHelper::FIOReadFromFile( IN LARGE_INTEGER Offset, IN DWORD Size, IN OUT PUCHAR Buffer ) { _ASSERTE(NULL != Buffer); if (NULL == Buffer) return DTS_INVALID_PARAMETER; UCHAR* p = NULL; DTSTATUS status = FIOReference(TRUE, Offset, Size, p); if (TRUE != DT_SUCCEEDED(status)) { printf("FIOReference() failed. status=0x%08x", status); return status; } __try { RtlCopyMemory(Buffer, p, Size); } __except (EXCEPTION_EXECUTE_HANDLER) { printf("exception. code=0x%08x", GetExceptionCode()); status = DTS_EXCEPTION_RAISED; } FIOUnreference(); return status; }
/**---------------------------------------------------------------------------- \brief \param \return \code \endcode -----------------------------------------------------------------------------*/ DTSTATUS FileIoHelper::FIOWriteToFile( IN LARGE_INTEGER Offset, IN DWORD Size, IN PUCHAR Buffer ) { _ASSERTE(NULL != Buffer); _ASSERTE(0 != Size); _ASSERTE(NULL != Buffer); if (NULL == Buffer || 0 == Size || NULL == Buffer) return DTS_INVALID_PARAMETER; UCHAR* p = NULL; DTSTATUS status = FIOReference(FALSE, Offset, Size, p); if (TRUE != DT_SUCCEEDED(status)) { return status; } __try { RtlCopyMemory(p, Buffer, Size); } __except (EXCEPTION_EXECUTE_HANDLER) { status = DTS_EXCEPTION_RAISED; } FIOUnreference(); return status; }
int main(){ StopWatch sw = StopWatch(); create_very_big_file(L"make.bin", 4096); //create_very_big_file 함수 내부에서 LARGE_INTEGER 다룰때 LL 추가 sw.Start(); LARGE_INTEGER file_size_read = { 0 }; LARGE_INTEGER file_offset = { 0 }; unsigned char buf[BUF_SIZE]; FileIoHelper Fi_read= FileIoHelper(); FileIoHelper Fi_write = FileIoHelper(); Fi_read.FIOpenForRead(L"make.bin"); file_size_read.QuadPart = (1024 * 1024) * 4096LL; Fi_write.FIOCreateFile(L"copy.bin", file_size_read); while (DT_SUCCEEDED(Fi_read.FIOReadFromFile(file_offset, BUF_SIZE, buf))){ Fi_write.FIOWriteToFile(file_offset, BUF_SIZE, buf); file_offset.QuadPart += BUF_SIZE; //printf("%d", file_offset.QuadPart); } Fi_read.FIOClose(); Fi_write.FIOClose(); sw.Stop(); printf("using memory map via FileIoHelperClass %f , bufsize = %d \n\n", sw.GetDurationSecond(),BUF_SIZE); sw.Start(); file_copy_using_read_write(L"make.bin", L"copy.bin2"); sw.Stop(); printf("using read write %f\n", sw.GetDurationSecond()); system("pause"); return 0; }
/**---------------------------------------------------------------------------- \brief 파일 IO 를 위해 파일을 오픈한다. \param \return \code \endcode -----------------------------------------------------------------------------*/ DTSTATUS FileIoHelper::FIOpenForRead( IN std::wstring FilePath ) { if (TRUE == Initialized()) { FIOClose(); } mReadOnly = TRUE; if (TRUE != is_file_existsW(FilePath.c_str())) { printf( "no file exists. file=%ws", FilePath.c_str() ); return DTS_NO_FILE_EXIST; } #pragma warning(disable: 4127) DTSTATUS status = DTS_WINAPI_FAILED; do { mFileHandle = CreateFileW( FilePath.c_str(), GENERIC_READ, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); if (INVALID_HANDLE_VALUE == mFileHandle) { printf( "CreateFile(%ws) failed, gle=0x%08x", FilePath.c_str(), GetLastError() ); break; } // check file size // if (TRUE != GetFileSizeEx(mFileHandle, &mFileSize)) { printf( "%ws, can not get file size, gle=0x%08x", FilePath.c_str(), GetLastError() ); break; } mFileMap = CreateFileMapping( mFileHandle, NULL, PAGE_READONLY, 0, 0, NULL ); if (NULL == mFileMap) { printf( "CreateFileMapping(%ws) failed, gle=0x%08x", FilePath.c_str(), GetLastError() ); break; } status = DTS_SUCCESS; } while (FALSE); #pragma warning(default: 4127) if (TRUE != DT_SUCCEEDED(status)) { if (INVALID_HANDLE_VALUE != mFileHandle) { CloseHandle(mFileHandle); mFileHandle = INVALID_HANDLE_VALUE; } if (NULL != mFileMap) CloseHandle(mFileMap); } return status; }
/**---------------------------------------------------------------------------- \brief FileSize 바이트 짜리 파일을 생성한다. \param \return \code \endcode -----------------------------------------------------------------------------*/ DTSTATUS FileIoHelper::FIOCreateFile( IN std::wstring FilePath, IN LARGE_INTEGER FileSize ) { if (TRUE == Initialized()) { FIOClose(); } if (FileSize.QuadPart == 0) return DTS_INVALID_PARAMETER; mReadOnly = FALSE; #pragma warning(disable: 4127) DTSTATUS status = DTS_WINAPI_FAILED; do { mFileSize = FileSize; mFileHandle = CreateFileW( FilePath.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, // write 도중 다른 프로세스에서 읽기가 가능 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); if (INVALID_HANDLE_VALUE == mFileHandle) { printf( "CreateFile(%ws) failed, gle=0x%08x", FilePath.c_str(), GetLastError() ); break; } // increase file size // if (TRUE != SetFilePointerEx(mFileHandle, mFileSize, NULL, FILE_BEGIN)) { printf( "SetFilePointerEx( move to %I64d ) failed, gle=0x%08x", FileSize.QuadPart, GetLastError() ); break; } if (TRUE != SetEndOfFile(mFileHandle)) { printf("SetEndOfFile( ) failed, gle=0x%08x", GetLastError()); break; } mFileMap = CreateFileMapping( mFileHandle, NULL, PAGE_READWRITE, 0, 0, NULL ); if (NULL == mFileMap) { printf( "CreateFileMapping(%ws) failed, gle=0x%08x", FilePath.c_str(), GetLastError() ); break; } status = DTS_SUCCESS; } while (FALSE); #pragma warning(default: 4127) if (TRUE != DT_SUCCEEDED(status)) { if (INVALID_HANDLE_VALUE != mFileHandle) { CloseHandle(mFileHandle); mFileHandle = INVALID_HANDLE_VALUE; } if (NULL != mFileMap) CloseHandle(mFileMap); } return status; }