|
|
|
@ -1,6 +1,8 @@
|
|
|
|
#include "byte_stream.h"
|
|
|
|
#include "byte_stream.h"
|
|
|
|
#include "assert.h"
|
|
|
|
#include "assert.h"
|
|
|
|
|
|
|
|
#include "file_system.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
#include "string_util.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdio>
|
|
|
|
@ -958,16 +960,14 @@ std::unique_ptr<ByteStream> ByteStream_OpenFileStream(const char* fileName, u32
|
|
|
|
|
|
|
|
|
|
|
|
// fill in random characters
|
|
|
|
// fill in random characters
|
|
|
|
_mktemp_s(temporaryFileName, fileNameLength + 8);
|
|
|
|
_mktemp_s(temporaryFileName, fileNameLength + 8);
|
|
|
|
|
|
|
|
const std::wstring wideTemporaryFileName(StringUtil::UTF8StringToWideString(temporaryFileName));
|
|
|
|
// open the file
|
|
|
|
|
|
|
|
errno_t err;
|
|
|
|
|
|
|
|
FILE* pTemporaryFile;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// massive hack here
|
|
|
|
// massive hack here
|
|
|
|
DWORD desiredAccess = GENERIC_WRITE;
|
|
|
|
DWORD desiredAccess = GENERIC_WRITE;
|
|
|
|
if (openMode & BYTESTREAM_OPEN_READ)
|
|
|
|
if (openMode & BYTESTREAM_OPEN_READ)
|
|
|
|
desiredAccess |= GENERIC_READ;
|
|
|
|
desiredAccess |= GENERIC_READ;
|
|
|
|
HANDLE hFile = CreateFileA(temporaryFileName, desiredAccess, FILE_SHARE_DELETE, NULL, CREATE_NEW, 0, NULL);
|
|
|
|
HANDLE hFile =
|
|
|
|
|
|
|
|
CreateFileW(wideTemporaryFileName.c_str(), desiredAccess, FILE_SHARE_DELETE, NULL, CREATE_NEW, 0, NULL);
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
return nullptr;
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
|
|
|
|
@ -976,16 +976,16 @@ std::unique_ptr<ByteStream> ByteStream_OpenFileStream(const char* fileName, u32
|
|
|
|
if (fd < 0)
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
CloseHandle(hFile);
|
|
|
|
CloseHandle(hFile);
|
|
|
|
DeleteFileA(temporaryFileName);
|
|
|
|
DeleteFileW(wideTemporaryFileName.c_str());
|
|
|
|
return nullptr;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// convert to a stream
|
|
|
|
// convert to a stream
|
|
|
|
pTemporaryFile = _fdopen(fd, modeString);
|
|
|
|
FILE* pTemporaryFile = _fdopen(fd, modeString);
|
|
|
|
if (pTemporaryFile == nullptr)
|
|
|
|
if (!pTemporaryFile)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_close(fd);
|
|
|
|
_close(fd);
|
|
|
|
DeleteFileA(temporaryFileName);
|
|
|
|
DeleteFileW(wideTemporaryFileName.c_str());
|
|
|
|
return nullptr;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -996,9 +996,8 @@ std::unique_ptr<ByteStream> ByteStream_OpenFileStream(const char* fileName, u32
|
|
|
|
// do we need to copy the existing file into this one?
|
|
|
|
// do we need to copy the existing file into this one?
|
|
|
|
if (!(openMode & BYTESTREAM_OPEN_TRUNCATE))
|
|
|
|
if (!(openMode & BYTESTREAM_OPEN_TRUNCATE))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
FILE* pOriginalFile;
|
|
|
|
FILE* pOriginalFile = FileSystem::OpenCFile(fileName, "rb");
|
|
|
|
err = fopen_s(&pOriginalFile, fileName, "rb");
|
|
|
|
if (!pOriginalFile)
|
|
|
|
if (err != 0 || pOriginalFile == nullptr)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// this will delete the temporary file
|
|
|
|
// this will delete the temporary file
|
|
|
|
pStream->Discard();
|
|
|
|
pStream->Discard();
|
|
|
|
@ -1031,9 +1030,8 @@ std::unique_ptr<ByteStream> ByteStream_OpenFileStream(const char* fileName, u32
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// forward through
|
|
|
|
// forward through
|
|
|
|
FILE* pFile;
|
|
|
|
FILE* pFile = FileSystem::OpenCFile(fileName, modeString);
|
|
|
|
errno_t err = fopen_s(&pFile, fileName, modeString);
|
|
|
|
if (!pFile)
|
|
|
|
if (err != 0 || pFile == NULL)
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
|
|
|
|
return std::make_unique<FileByteStream>(pFile);
|
|
|
|
return std::make_unique<FileByteStream>(pFile);
|
|
|
|
|