aboutsummaryrefslogtreecommitdiff
path: root/dep/CascLib/src/common/FileStream.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'dep/CascLib/src/common/FileStream.cpp')
-rw-r--r--dep/CascLib/src/common/FileStream.cpp94
1 files changed, 52 insertions, 42 deletions
diff --git a/dep/CascLib/src/common/FileStream.cpp b/dep/CascLib/src/common/FileStream.cpp
index 350b0540cf4..cb0090f8dfb 100644
--- a/dep/CascLib/src/common/FileStream.cpp
+++ b/dep/CascLib/src/common/FileStream.cpp
@@ -75,7 +75,7 @@ static bool BaseFile_Create(TFileStream * pStream)
if(handle == -1)
{
pStream->Base.File.hFile = INVALID_HANDLE_VALUE;
- SetLastError(errno);
+ SetCascError(errno);
return false;
}
@@ -128,14 +128,14 @@ static bool BaseFile_Open(TFileStream * pStream, LPCTSTR szFileName, DWORD dwStr
handle = open(szFileName, oflag | O_LARGEFILE);
if(handle == -1)
{
- SetLastError(errno);
+ SetCascError(errno);
return false;
}
// Get the file size
if(fstat64(handle, &fileinfo) == -1)
{
- SetLastError(errno);
+ SetCascError(errno);
close(handle);
return false;
}
@@ -185,8 +185,11 @@ static bool BaseFile_Read(
Overlapped.OffsetHigh = (DWORD)(ByteOffset >> 32);
Overlapped.Offset = (DWORD)ByteOffset;
Overlapped.hEvent = NULL;
- if (!ReadFile(pStream->Base.File.hFile, pvBuffer, dwBytesToRead, &dwBytesRead, &Overlapped))
+ if(!ReadFile(pStream->Base.File.hFile, pvBuffer, dwBytesToRead, &dwBytesRead, &Overlapped))
+ {
+ CascUnlock(pStream->Lock);
return false;
+ }
}
}
#endif
@@ -201,7 +204,8 @@ static bool BaseFile_Read(
{
if (lseek64((intptr_t)pStream->Base.File.hFile, (off64_t)(ByteOffset), SEEK_SET) == (off64_t)-1)
{
- SetLastError(errno);
+ CascUnlock(pStream->Lock);
+ SetCascError(errno);
return false;
}
pStream->Base.File.FilePos = ByteOffset;
@@ -213,7 +217,8 @@ static bool BaseFile_Read(
bytes_read = read((intptr_t)pStream->Base.File.hFile, pvBuffer, (size_t)dwBytesToRead);
if (bytes_read == -1)
{
- SetLastError(errno);
+ CascUnlock(pStream->Lock);
+ SetCascError(errno);
return false;
}
@@ -239,7 +244,7 @@ static bool BaseFile_Read(
}
else
{
- SetLastError(ERROR_HANDLE_EOF);
+ SetCascError(ERROR_HANDLE_EOF);
}
}
@@ -280,8 +285,11 @@ static bool BaseFile_Write(TFileStream * pStream, ULONGLONG * pByteOffset, const
Overlapped.OffsetHigh = (DWORD)(ByteOffset >> 32);
Overlapped.Offset = (DWORD)ByteOffset;
Overlapped.hEvent = NULL;
- if (!WriteFile(pStream->Base.File.hFile, pvBuffer, dwBytesToWrite, &dwBytesWritten, &Overlapped))
+ if(!WriteFile(pStream->Base.File.hFile, pvBuffer, dwBytesToWrite, &dwBytesWritten, &Overlapped))
+ {
+ CascUnlock(pStream->Lock);
return false;
+ }
}
}
#endif
@@ -296,7 +304,8 @@ static bool BaseFile_Write(TFileStream * pStream, ULONGLONG * pByteOffset, const
{
if (lseek64((intptr_t)pStream->Base.File.hFile, (off64_t)(ByteOffset), SEEK_SET) == (off64_t)-1)
{
- SetLastError(errno);
+ CascUnlock(pStream->Lock);
+ SetCascError(errno);
return false;
}
pStream->Base.File.FilePos = ByteOffset;
@@ -306,7 +315,8 @@ static bool BaseFile_Write(TFileStream * pStream, ULONGLONG * pByteOffset, const
bytes_written = write((intptr_t)pStream->Base.File.hFile, pvBuffer, (size_t)dwBytesToWrite);
if (bytes_written == -1)
{
- SetLastError(errno);
+ CascUnlock(pStream->Lock);
+ SetCascError(errno);
return false;
}
@@ -324,7 +334,7 @@ static bool BaseFile_Write(TFileStream * pStream, ULONGLONG * pByteOffset, const
CascUnlock(pStream->Lock);
if(dwBytesWritten != dwBytesToWrite)
- SetLastError(ERROR_DISK_FULL);
+ SetCascError(ERROR_DISK_FULL);
return (dwBytesWritten == dwBytesToWrite);
}
@@ -343,7 +353,7 @@ static bool BaseFile_Resize(TFileStream * pStream, ULONGLONG NewFileSize)
// Set the position at the new file size
dwNewPos = SetFilePointer(pStream->Base.File.hFile, (LONG)NewFileSize, &FileSizeHi, FILE_BEGIN);
- if(dwNewPos == INVALID_SET_FILE_POINTER && GetLastError() != ERROR_SUCCESS)
+ if(dwNewPos == INVALID_SET_FILE_POINTER && GetCascError() != ERROR_SUCCESS)
return false;
// Set the current file pointer as the end of the file
@@ -363,7 +373,7 @@ static bool BaseFile_Resize(TFileStream * pStream, ULONGLONG NewFileSize)
{
if(ftruncate64((intptr_t)pStream->Base.File.hFile, (off64_t)NewFileSize) == -1)
{
- SetLastError(errno);
+ SetCascError(errno);
return false;
}
@@ -403,7 +413,7 @@ static bool BaseFile_Replace(TFileStream * pStream, TFileStream * pNewStream)
// "rename" on Linux also works if the target file exists
if(rename(pNewStream->szFileName, pStream->szFileName) == -1)
{
- SetLastError(errno);
+ SetCascError(errno);
return false;
}
@@ -533,7 +543,7 @@ static bool BaseMap_Open(TFileStream * pStream, LPCTSTR szFileName, DWORD dwStre
// Did the mapping fail?
if(bResult == false)
{
- SetLastError(errno);
+ SetCascError(errno);
return false;
}
#endif
@@ -638,7 +648,7 @@ static bool BaseHttp_Open(TFileStream * pStream, LPCTSTR szFileName, DWORD dwStr
// Don't download if we are not connected to the internet
if(!InternetGetConnectedState(&dwTemp, 0))
- dwErrCode = GetLastError();
+ dwErrCode = GetCascError();
// Initiate the connection to the internet
if(dwErrCode == ERROR_SUCCESS)
@@ -649,7 +659,7 @@ static bool BaseHttp_Open(TFileStream * pStream, LPCTSTR szFileName, DWORD dwStr
NULL,
0);
if(pStream->Base.Http.hInternet == NULL)
- dwErrCode = GetLastError();
+ dwErrCode = GetCascError();
}
// Connect to the server
@@ -669,7 +679,7 @@ static bool BaseHttp_Open(TFileStream * pStream, LPCTSTR szFileName, DWORD dwStr
dwFlags,
0);
if(pStream->Base.Http.hConnect == NULL)
- dwErrCode = GetLastError();
+ dwErrCode = GetCascError();
}
// Now try to query the file size
@@ -727,7 +737,7 @@ static bool BaseHttp_Open(TFileStream * pStream, LPCTSTR szFileName, DWORD dwStr
if(bFileAvailable == false)
{
pStream->BaseClose(pStream);
- SetLastError(dwErrCode);
+ SetCascError(dwErrCode);
return false;
}
@@ -736,7 +746,7 @@ static bool BaseHttp_Open(TFileStream * pStream, LPCTSTR szFileName, DWORD dwStr
#else
// Not supported
- SetLastError(ERROR_NOT_SUPPORTED);
+ SetCascError(ERROR_NOT_SUPPORTED);
pStream = pStream;
return false;
@@ -804,7 +814,7 @@ static bool BaseHttp_Read(
// If the number of bytes read doesn't match the required amount, return false
if(dwTotalBytesRead != dwBytesToRead)
- SetLastError(ERROR_HANDLE_EOF);
+ SetCascError(ERROR_HANDLE_EOF);
return (dwTotalBytesRead == dwBytesToRead);
#else
@@ -814,7 +824,7 @@ static bool BaseHttp_Read(
pByteOffset = pByteOffset;
pvBuffer = pvBuffer;
dwBytesToRead = dwBytesToRead;
- SetLastError(ERROR_NOT_SUPPORTED);
+ SetCascError(ERROR_NOT_SUPPORTED);
return false;
#endif
@@ -888,7 +898,7 @@ static bool BlockStream_Read(
EndOffset = ByteOffset + dwBytesToRead;
if(EndOffset > pStream->StreamSize)
{
- SetLastError(ERROR_HANDLE_EOF);
+ SetCascError(ERROR_HANDLE_EOF);
return false;
}
@@ -905,7 +915,7 @@ static bool BlockStream_Read(
TransferBuffer = BlockBuffer = CASC_ALLOC<BYTE>(BlockCount * BlockSize);
if(TransferBuffer == NULL)
{
- SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+ SetCascError(ERROR_NOT_ENOUGH_MEMORY);
return false;
}
@@ -983,7 +993,7 @@ static bool BlockStream_Read(
else
{
// If the block read failed, set the last error
- SetLastError(ERROR_FILE_INCOMPLETE);
+ SetCascError(ERROR_FILE_INCOMPLETE);
}
// Call the callback to indicate we are done
@@ -1058,7 +1068,7 @@ static TFileStream * AllocateFileStream(
// Don't allow another master file in the string
if(_tcschr(szNextFile + 1, _T('*')) != NULL)
{
- SetLastError(ERROR_INVALID_PARAMETER);
+ SetCascError(ERROR_INVALID_PARAMETER);
return NULL;
}
@@ -1392,7 +1402,7 @@ static TFileStream * FlatStream_Open(LPCTSTR szFileName, DWORD dwStreamFlags)
pStream = (TBlockStream *)AllocateFileStream(szFileName, sizeof(TBlockStream), dwStreamFlags);
if(pStream == NULL)
{
- SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+ SetCascError(ERROR_NOT_ENOUGH_MEMORY);
return NULL;
}
@@ -1402,7 +1412,7 @@ static TFileStream * FlatStream_Open(LPCTSTR szFileName, DWORD dwStreamFlags)
if(!FlatStream_CreateMirror(pStream))
{
FileStream_Close(pStream);
- SetLastError(ERROR_FILE_NOT_FOUND);
+ SetCascError(ERROR_FILE_NOT_FOUND);
return NULL;
}
}
@@ -1819,7 +1829,7 @@ static TFileStream * PartStream_Open(LPCTSTR szFileName, DWORD dwStreamFlags)
if(!PartStream_CreateMirror(pStream))
{
FileStream_Close(pStream);
- SetLastError(ERROR_FILE_NOT_FOUND);
+ SetCascError(ERROR_FILE_NOT_FOUND);
return NULL;
}
}
@@ -1836,7 +1846,7 @@ static TFileStream * PartStream_Open(LPCTSTR szFileName, DWORD dwStreamFlags)
if(!PartStream_LoadBitmap(pStream))
{
FileStream_Close(pStream);
- SetLastError(ERROR_BAD_FORMAT);
+ SetCascError(ERROR_BAD_FORMAT);
return NULL;
}
}
@@ -2149,7 +2159,7 @@ static TFileStream * EncrStream_Open(LPCTSTR szFileName, DWORD dwStreamFlags)
// Cleanup the stream and return
FileStream_Close(pStream);
- SetLastError(ERROR_FILE_ENCRYPTED);
+ SetCascError(ERROR_FILE_ENCRYPTED);
return NULL;
}
@@ -2293,7 +2303,7 @@ static TFileStream * Block4Stream_Open(LPCTSTR szFileName, DWORD dwStreamFlags)
NewBaseArray = CASC_ALLOC<TBaseProviderData>(dwBaseFiles + 1);
if(NewBaseArray == NULL)
{
- SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+ SetCascError(ERROR_NOT_ENOUGH_MEMORY);
return NULL;
}
@@ -2348,7 +2358,7 @@ static TFileStream * Block4Stream_Open(LPCTSTR szFileName, DWORD dwStreamFlags)
if(dwBaseFiles == 0)
{
FileStream_Close(pStream);
- SetLastError(ERROR_FILE_NOT_FOUND);
+ SetCascError(ERROR_FILE_NOT_FOUND);
pStream = NULL;
}
@@ -2384,7 +2394,7 @@ TFileStream * FileStream_CreateFile(
// We only support creation of flat, local file
if((dwStreamFlags & (STREAM_PROVIDERS_MASK)) != (STREAM_PROVIDER_FLAT | BASE_PROVIDER_FILE))
{
- SetLastError(ERROR_NOT_SUPPORTED);
+ SetCascError(ERROR_NOT_SUPPORTED);
return NULL;
}
@@ -2457,7 +2467,7 @@ TFileStream * FileStream_OpenFile(
return Block4Stream_Open(szFileName, dwStreamFlags);
default:
- SetLastError(ERROR_INVALID_PARAMETER);
+ SetCascError(ERROR_INVALID_PARAMETER);
return NULL;
}
}
@@ -2569,7 +2579,7 @@ bool FileStream_SetCallback(TFileStream * pStream, STREAM_DOWNLOAD_CALLBACK pfnC
if(pStream->BlockRead == NULL)
{
- SetLastError(ERROR_NOT_SUPPORTED);
+ SetCascError(ERROR_NOT_SUPPORTED);
return false;
}
@@ -2594,8 +2604,8 @@ bool FileStream_SetCallback(TFileStream * pStream, STREAM_DOWNLOAD_CALLBACK pfnC
*
* \returns
* - If the function reads the required amount of bytes, it returns true.
- * - If the function reads less than required bytes, it returns false and GetLastError() returns ERROR_HANDLE_EOF
- * - If the function fails, it reads false and GetLastError() returns an error code different from ERROR_HANDLE_EOF
+ * - If the function reads less than required bytes, it returns false and GetCascError() returns ERROR_HANDLE_EOF
+ * - If the function fails, it reads false and GetCascError() returns an error code different from ERROR_HANDLE_EOF
*/
bool FileStream_Read(TFileStream * pStream, ULONGLONG * pByteOffset, void * pvBuffer, DWORD dwBytesToRead)
{
@@ -2619,7 +2629,7 @@ bool FileStream_Write(TFileStream * pStream, ULONGLONG * pByteOffset, const void
{
if(pStream->dwFlags & STREAM_FLAG_READ_ONLY)
{
- SetLastError(ERROR_ACCESS_DENIED);
+ SetCascError(ERROR_ACCESS_DENIED);
return false;
}
@@ -2649,7 +2659,7 @@ bool FileStream_SetSize(TFileStream * pStream, ULONGLONG NewFileSize)
{
if(pStream->dwFlags & STREAM_FLAG_READ_ONLY)
{
- SetLastError(ERROR_ACCESS_DENIED);
+ SetCascError(ERROR_ACCESS_DENIED);
return false;
}
@@ -2709,14 +2719,14 @@ bool FileStream_Replace(TFileStream * pStream, TFileStream * pNewStream)
// Only supported on flat files
if((pStream->dwFlags & STREAM_PROVIDERS_MASK) != (STREAM_PROVIDER_FLAT | BASE_PROVIDER_FILE))
{
- SetLastError(ERROR_NOT_SUPPORTED);
+ SetCascError(ERROR_NOT_SUPPORTED);
return false;
}
// Not supported on read-only streams
if(pStream->dwFlags & STREAM_FLAG_READ_ONLY)
{
- SetLastError(ERROR_ACCESS_DENIED);
+ SetCascError(ERROR_ACCESS_DENIED);
return false;
}