Merge pull request #4076 from akallabeth/SetFilePointer_fix

Set file pointer fix
This commit is contained in:
MartinHaimberger 2017-08-09 10:35:37 +02:00 committed by GitHub
commit 80ed23779f
2 changed files with 49 additions and 10 deletions

View File

@ -385,16 +385,13 @@ BOOL drive_file_free(DRIVE_FILE* file)
BOOL drive_file_seek(DRIVE_FILE* file, UINT64 Offset) BOOL drive_file_seek(DRIVE_FILE* file, UINT64 Offset)
{ {
LONG lDistHigh; LARGE_INTEGER loffset;
DWORD dwPtrLow;
if (!file) if (!file)
return FALSE; return FALSE;
lDistHigh = Offset >> 32; loffset.QuadPart = Offset;
DEBUG_WSTR("Seek %s", file->fullpath); return SetFilePointerEx(file->file_handle, loffset, NULL, FILE_BEGIN);
dwPtrLow = SetFilePointer(file->file_handle, Offset & 0xFFFFFFFF, &lDistHigh, FILE_BEGIN);
return dwPtrLow != INVALID_SET_FILE_POINTER;
} }
BOOL drive_file_read(DRIVE_FILE* file, BYTE* buffer, UINT32* Length) BOOL drive_file_read(DRIVE_FILE* file, BYTE* buffer, UINT32* Length)
@ -623,8 +620,7 @@ BOOL drive_file_set_information(DRIVE_FILE* file, UINT32 FsInformationClass, UIN
liSize.QuadPart = size & 0xFFFFFFFF; liSize.QuadPart = size & 0xFFFFFFFF;
if (SetFilePointer(file->file_handle, liSize.LowPart, &liSize.HighPart, if (!SetFilePointerEx(file->file_handle, liSize, NULL, FILE_BEGIN))
FILE_BEGIN) == INVALID_SET_FILE_POINTER)
{ {
WLog_ERR(TAG, "Unable to truncate %s to %d (%"PRId32")", file->fullpath, size, GetLastError()); WLog_ERR(TAG, "Unable to truncate %s to %d (%"PRId32")", file->fullpath, size, GetLastError());
return FALSE; return FALSE;

View File

@ -122,12 +122,19 @@ static DWORD FileSetFilePointer(HANDLE hFile, LONG lDistanceToMove,
PLONG lpDistanceToMoveHigh, DWORD dwMoveMethod) PLONG lpDistanceToMoveHigh, DWORD dwMoveMethod)
{ {
WINPR_FILE* pFile = (WINPR_FILE*) hFile; WINPR_FILE* pFile = (WINPR_FILE*) hFile;
long offset = lDistanceToMove; INT64 offset;
int whence; int whence;
if (!hFile) if (!hFile)
return INVALID_SET_FILE_POINTER; return INVALID_SET_FILE_POINTER;
if (lpDistanceToMoveHigh)
{
offset = (*lpDistanceToMoveHigh << 32) | (UINT32)lDistanceToMove;
}
else
offset = lDistanceToMove;
switch(dwMoveMethod) switch(dwMoveMethod)
{ {
case FILE_BEGIN: case FILE_BEGIN:
@ -153,6 +160,42 @@ static DWORD FileSetFilePointer(HANDLE hFile, LONG lDistanceToMove,
return ftell(pFile->fp); return ftell(pFile->fp);
} }
static BOOL FileSetFilePointerEx(HANDLE hFile, LARGE_INTEGER liDistanceToMove, PLARGE_INTEGER lpNewFilePointer, DWORD dwMoveMethod)
{
WINPR_FILE* pFile = (WINPR_FILE*) hFile;
int whence;
if (!hFile)
return FALSE;
switch(dwMoveMethod)
{
case FILE_BEGIN:
whence = SEEK_SET;
break;
case FILE_END:
whence = SEEK_END;
break;
case FILE_CURRENT:
whence = SEEK_CUR;
break;
default:
return FALSE;
}
if (fseek(pFile->fp, liDistanceToMove.QuadPart, whence))
{
WLog_ERR(TAG, "fseek(%s) failed with %s [0x%08X]", pFile->lpFileName,
strerror(errno), errno);
return FALSE;
}
if (lpNewFilePointer)
lpNewFilePointer->QuadPart = ftell(pFile->fp);
return TRUE;
}
static BOOL FileRead(PVOID Object, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, static BOOL FileRead(PVOID Object, LPVOID lpBuffer, DWORD nNumberOfBytesToRead,
LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped) LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped)
{ {
@ -550,7 +593,7 @@ static HANDLE_OPS fileOps = {
NULL, /* FlushFileBuffers */ NULL, /* FlushFileBuffers */
FileSetEndOfFile, FileSetEndOfFile,
FileSetFilePointer, FileSetFilePointer,
NULL, /* SetFilePointerEx */ FileSetFilePointerEx,
NULL, /* FileLockFile */ NULL, /* FileLockFile */
FileLockFileEx, FileLockFileEx,
FileUnlockFile, FileUnlockFile,