Route all file I/O through libretro VFS; remove non-VFS codepaths

byte_stream: rebuild FileByteStream on RFILE* + the libretro-common rf*
shim (rfread/rfwrite/rfseek/rftell/filestream_flush/filestream_error/
rfclose) instead of FILE*. This collapses the Win32/POSIX _fseeki64 vs
fseeko #if seek branches into one portable 64-bit path. ByteStream_OpenFileStream
is reduced from two platform-specific implementations (Win32 CreateFileW/
_open_osfhandle/_fdopen + POSIX mkstemp/fdopen) to a single VFS factory using
rfopen(); the atomic-update path opens its temp file via VFS and keeps the
rename()/remove() commit/discard logic (filesystem ops, not file I/O). Drop the
now-unused Win32 io.h/share.h/direct.h and encodings/utf.h includes.

file_system: remove FileSystem::OpenCFile() entirely -- it was a raw
fopen/_wfopen path whose only caller (byte_stream) now uses VFS, leaving it
dead.

gl/program: write the bad_shader_*.txt compile-failure dump through a VFS
ByteStream instead of std::ofstream. system: drop an unused <fstream> include.

libchdr: convert the core_stdio_* backend to filestream_* (drop the
USE_LIBRETRO_VFS toggle and the fseeko64/_fseeki64 macro ladder); core_file
argp is an RFILE*, and chd_open_file()/chd_read_header_file() take RFILE*.
coretypes.h includes <streams/file_stream.h> unconditionally. (miniz stays --
it is the active zlib backend here since CHDR_SYSTEM_ZLIB is unset.)

dr_flac: remove every DR_FLAC_NO_STDIO-gated stdio path (open_file/_w,
with_metadata/_w, open_file_and_read_pcm_frames_*, the stdio read/seek/tell
callbacks, and the stdio/Ogg-file close handling) and delete the
DR_FLAC_NO_STDIO macro; the decoder is callback/memory-only by default.
This commit is contained in:
libretroadmin 2026-06-25 12:54:37 +00:00 • committed by libretroadmin
commit db6941773a
10 changed files with 57 additions and 1188 deletions

View file

@ -83,9 +83,6 @@ Build Options
=============
#define these options before including this file.
#define DR_FLAC_NO_STDIO
Disable `drflac_open_file()` and family.
#define DR_FLAC_NO_OGG
Disables support for Ogg/FLAC streams.
@ -102,7 +99,7 @@ Build Options
Disables SIMD optimizations (SSE on x86/x64 architectures, NEON on ARM architectures). Use this if you are having compatibility issues with your compiler.
#define DR_FLAC_NO_WCHAR
Disables all functions ending with `_w`. Use this if your compiler does not provide wchar.h. Not required if DR_FLAC_NO_STDIO is also defined.
Disables all functions ending with `_w`. Use this if your compiler does not provide wchar.h.
@ -991,81 +988,6 @@ DRFLAC_API drflac_bool32 drflac_seek_to_pcm_frame(drflac* pFlac, drflac_uint64 p
#ifndef DR_FLAC_NO_STDIO
/*
Opens a FLAC decoder from the file at the given path.
Parameters
----------
pFileName (in)
The path of the file to open, either absolute or relative to the current directory.
pAllocationCallbacks (in, optional)
A pointer to application defined callbacks for managing memory allocations.
Return Value
------------
A pointer to an object representing the decoder.
Remarks
-------
Close the decoder with drflac_close().
Remarks
-------
This will hold a handle to the file until the decoder is closed with drflac_close(). Some platforms will restrict the number of files a process can have open
at any given time, so keep this mind if you have many decoders open at the same time.
See Also
--------
drflac_open_file_with_metadata()
drflac_open()
drflac_close()
*/
DRFLAC_API drflac* drflac_open_file(const char* pFileName, const drflac_allocation_callbacks* pAllocationCallbacks);
DRFLAC_API drflac* drflac_open_file_w(const wchar_t* pFileName, const drflac_allocation_callbacks* pAllocationCallbacks);
/*
Opens a FLAC decoder from the file at the given path and notifies the caller of the metadata chunks (album art, etc.)
Parameters
----------
pFileName (in)
The path of the file to open, either absolute or relative to the current directory.
pAllocationCallbacks (in, optional)
A pointer to application defined callbacks for managing memory allocations.
onMeta (in)
The callback to fire for each metadata block.
pUserData (in)
A pointer to the user data to pass to the metadata callback.
pAllocationCallbacks (in)
A pointer to application defined callbacks for managing memory allocations.
Remarks
-------
Look at the documentation for drflac_open_with_metadata() for more information on how metadata is handled.
See Also
--------
drflac_open_with_metadata()
drflac_open()
drflac_close()
*/
DRFLAC_API drflac* drflac_open_file_with_metadata(const char* pFileName, drflac_meta_proc onMeta, void* pUserData, const drflac_allocation_callbacks* pAllocationCallbacks);
DRFLAC_API drflac* drflac_open_file_with_metadata_w(const wchar_t* pFileName, drflac_meta_proc onMeta, void* pUserData, const drflac_allocation_callbacks* pAllocationCallbacks);
#endif
/*
Opens a FLAC decoder from a pre-allocated block of memory
@ -1159,16 +1081,6 @@ DRFLAC_API drflac_int16* drflac_open_and_read_pcm_frames_s16(drflac_read_proc on
/* Same as drflac_open_and_read_pcm_frames_s32(), except returns 32-bit floating-point samples. */
DRFLAC_API float* drflac_open_and_read_pcm_frames_f32(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_tell_proc onTell, void* pUserData, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks);
#ifndef DR_FLAC_NO_STDIO
/* Same as drflac_open_and_read_pcm_frames_s32() except opens the decoder from a file. */
DRFLAC_API drflac_int32* drflac_open_file_and_read_pcm_frames_s32(const char* filename, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks);
/* Same as drflac_open_file_and_read_pcm_frames_s32(), except returns signed 16-bit integer samples. */
DRFLAC_API drflac_int16* drflac_open_file_and_read_pcm_frames_s16(const char* filename, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks);
/* Same as drflac_open_file_and_read_pcm_frames_s32(), except returns 32-bit floating-point samples. */
DRFLAC_API float* drflac_open_file_and_read_pcm_frames_f32(const char* filename, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks);
#endif
/* Same as drflac_open_and_read_pcm_frames_s32() except opens the decoder from a block of memory. */
DRFLAC_API drflac_int32* drflac_open_memory_and_read_pcm_frames_s32(const void* data, size_t dataSize, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks);
@ -8210,697 +8122,6 @@ static drflac* drflac_open_with_metadata_private(drflac_read_proc onRead, drflac
#ifndef DR_FLAC_NO_STDIO
#include <stdio.h>
#ifndef DR_FLAC_NO_WCHAR
#include <wchar.h> /* For wcslen(), wcsrtombs() */
#endif
/* Errno */
/* drflac_result_from_errno() is only used for fopen() and wfopen() so putting it inside DR_WAV_NO_STDIO for now. If something else needs this later we can move it out. */
#include <errno.h>
static drflac_result drflac_result_from_errno(int e)
{
switch (e)
{
case 0: return DRFLAC_SUCCESS;
#ifdef EPERM
case EPERM: return DRFLAC_INVALID_OPERATION;
#endif
#ifdef ENOENT
case ENOENT: return DRFLAC_DOES_NOT_EXIST;
#endif
#ifdef ESRCH
case ESRCH: return DRFLAC_DOES_NOT_EXIST;
#endif
#ifdef EINTR
case EINTR: return DRFLAC_INTERRUPT;
#endif
#ifdef EIO
case EIO: return DRFLAC_IO_ERROR;
#endif
#ifdef ENXIO
case ENXIO: return DRFLAC_DOES_NOT_EXIST;
#endif
#ifdef E2BIG
case E2BIG: return DRFLAC_INVALID_ARGS;
#endif
#ifdef ENOEXEC
case ENOEXEC: return DRFLAC_INVALID_FILE;
#endif
#ifdef EBADF
case EBADF: return DRFLAC_INVALID_FILE;
#endif
#ifdef ECHILD
case ECHILD: return DRFLAC_ERROR;
#endif
#ifdef EAGAIN
case EAGAIN: return DRFLAC_UNAVAILABLE;
#endif
#ifdef ENOMEM
case ENOMEM: return DRFLAC_OUT_OF_MEMORY;
#endif
#ifdef EACCES
case EACCES: return DRFLAC_ACCESS_DENIED;
#endif
#ifdef EFAULT
case EFAULT: return DRFLAC_BAD_ADDRESS;
#endif
#ifdef ENOTBLK
case ENOTBLK: return DRFLAC_ERROR;
#endif
#ifdef EBUSY
case EBUSY: return DRFLAC_BUSY;
#endif
#ifdef EEXIST
case EEXIST: return DRFLAC_ALREADY_EXISTS;
#endif
#ifdef EXDEV
case EXDEV: return DRFLAC_ERROR;
#endif
#ifdef ENODEV
case ENODEV: return DRFLAC_DOES_NOT_EXIST;
#endif
#ifdef ENOTDIR
case ENOTDIR: return DRFLAC_NOT_DIRECTORY;
#endif
#ifdef EISDIR
case EISDIR: return DRFLAC_IS_DIRECTORY;
#endif
#ifdef EINVAL
case EINVAL: return DRFLAC_INVALID_ARGS;
#endif
#ifdef ENFILE
case ENFILE: return DRFLAC_TOO_MANY_OPEN_FILES;
#endif
#ifdef EMFILE
case EMFILE: return DRFLAC_TOO_MANY_OPEN_FILES;
#endif
#ifdef ENOTTY
case ENOTTY: return DRFLAC_INVALID_OPERATION;
#endif
#ifdef ETXTBSY
case ETXTBSY: return DRFLAC_BUSY;
#endif
#ifdef EFBIG
case EFBIG: return DRFLAC_TOO_BIG;
#endif
#ifdef ENOSPC
case ENOSPC: return DRFLAC_NO_SPACE;
#endif
#ifdef ESPIPE
case ESPIPE: return DRFLAC_BAD_SEEK;
#endif
#ifdef EROFS
case EROFS: return DRFLAC_ACCESS_DENIED;
#endif
#ifdef EMLINK
case EMLINK: return DRFLAC_TOO_MANY_LINKS;
#endif
#ifdef EPIPE
case EPIPE: return DRFLAC_BAD_PIPE;
#endif
#ifdef EDOM
case EDOM: return DRFLAC_OUT_OF_RANGE;
#endif
#ifdef ERANGE
case ERANGE: return DRFLAC_OUT_OF_RANGE;
#endif
#ifdef EDEADLK
case EDEADLK: return DRFLAC_DEADLOCK;
#endif
#ifdef ENAMETOOLONG
case ENAMETOOLONG: return DRFLAC_PATH_TOO_LONG;
#endif
#ifdef ENOLCK
case ENOLCK: return DRFLAC_ERROR;
#endif
#ifdef ENOSYS
case ENOSYS: return DRFLAC_NOT_IMPLEMENTED;
#endif
#if defined(ENOTEMPTY) && ENOTEMPTY != EEXIST /* In AIX, ENOTEMPTY and EEXIST use the same value. */
case ENOTEMPTY: return DRFLAC_DIRECTORY_NOT_EMPTY;
#endif
#ifdef ELOOP
case ELOOP: return DRFLAC_TOO_MANY_LINKS;
#endif
#ifdef ENOMSG
case ENOMSG: return DRFLAC_NO_MESSAGE;
#endif
#ifdef EIDRM
case EIDRM: return DRFLAC_ERROR;
#endif
#ifdef ECHRNG
case ECHRNG: return DRFLAC_ERROR;
#endif
#ifdef EL2NSYNC
case EL2NSYNC: return DRFLAC_ERROR;
#endif
#ifdef EL3HLT
case EL3HLT: return DRFLAC_ERROR;
#endif
#ifdef EL3RST
case EL3RST: return DRFLAC_ERROR;
#endif
#ifdef ELNRNG
case ELNRNG: return DRFLAC_OUT_OF_RANGE;
#endif
#ifdef EUNATCH
case EUNATCH: return DRFLAC_ERROR;
#endif
#ifdef ENOCSI
case ENOCSI: return DRFLAC_ERROR;
#endif
#ifdef EL2HLT
case EL2HLT: return DRFLAC_ERROR;
#endif
#ifdef EBADE
case EBADE: return DRFLAC_ERROR;
#endif
#ifdef EBADR
case EBADR: return DRFLAC_ERROR;
#endif
#ifdef EXFULL
case EXFULL: return DRFLAC_ERROR;
#endif
#ifdef ENOANO
case ENOANO: return DRFLAC_ERROR;
#endif
#ifdef EBADRQC
case EBADRQC: return DRFLAC_ERROR;
#endif
#ifdef EBADSLT
case EBADSLT: return DRFLAC_ERROR;
#endif
#ifdef EBFONT
case EBFONT: return DRFLAC_INVALID_FILE;
#endif
#ifdef ENOSTR
case ENOSTR: return DRFLAC_ERROR;
#endif
#ifdef ENODATA
case ENODATA: return DRFLAC_NO_DATA_AVAILABLE;
#endif
#ifdef ETIME
case ETIME: return DRFLAC_TIMEOUT;
#endif
#ifdef ENOSR
case ENOSR: return DRFLAC_NO_DATA_AVAILABLE;
#endif
#ifdef ENONET
case ENONET: return DRFLAC_NO_NETWORK;
#endif
#ifdef ENOPKG
case ENOPKG: return DRFLAC_ERROR;
#endif
#ifdef EREMOTE
case EREMOTE: return DRFLAC_ERROR;
#endif
#ifdef ENOLINK
case ENOLINK: return DRFLAC_ERROR;
#endif
#ifdef EADV
case EADV: return DRFLAC_ERROR;
#endif
#ifdef ESRMNT
case ESRMNT: return DRFLAC_ERROR;
#endif
#ifdef ECOMM
case ECOMM: return DRFLAC_ERROR;
#endif
#ifdef EPROTO
case EPROTO: return DRFLAC_ERROR;
#endif
#ifdef EMULTIHOP
case EMULTIHOP: return DRFLAC_ERROR;
#endif
#ifdef EDOTDOT
case EDOTDOT: return DRFLAC_ERROR;
#endif
#ifdef EBADMSG
case EBADMSG: return DRFLAC_BAD_MESSAGE;
#endif
#ifdef EOVERFLOW
case EOVERFLOW: return DRFLAC_TOO_BIG;
#endif
#ifdef ENOTUNIQ
case ENOTUNIQ: return DRFLAC_NOT_UNIQUE;
#endif
#ifdef EBADFD
case EBADFD: return DRFLAC_ERROR;
#endif
#ifdef EREMCHG
case EREMCHG: return DRFLAC_ERROR;
#endif
#ifdef ELIBACC
case ELIBACC: return DRFLAC_ACCESS_DENIED;
#endif
#ifdef ELIBBAD
case ELIBBAD: return DRFLAC_INVALID_FILE;
#endif
#ifdef ELIBSCN
case ELIBSCN: return DRFLAC_INVALID_FILE;
#endif
#ifdef ELIBMAX
case ELIBMAX: return DRFLAC_ERROR;
#endif
#ifdef ELIBEXEC
case ELIBEXEC: return DRFLAC_ERROR;
#endif
#ifdef EILSEQ
case EILSEQ: return DRFLAC_INVALID_DATA;
#endif
#ifdef ERESTART
case ERESTART: return DRFLAC_ERROR;
#endif
#ifdef ESTRPIPE
case ESTRPIPE: return DRFLAC_ERROR;
#endif
#ifdef EUSERS
case EUSERS: return DRFLAC_ERROR;
#endif
#ifdef ENOTSOCK
case ENOTSOCK: return DRFLAC_NOT_SOCKET;
#endif
#ifdef EDESTADDRREQ
case EDESTADDRREQ: return DRFLAC_NO_ADDRESS;
#endif
#ifdef EMSGSIZE
case EMSGSIZE: return DRFLAC_TOO_BIG;
#endif
#ifdef EPROTOTYPE
case EPROTOTYPE: return DRFLAC_BAD_PROTOCOL;
#endif
#ifdef ENOPROTOOPT
case ENOPROTOOPT: return DRFLAC_PROTOCOL_UNAVAILABLE;
#endif
#ifdef EPROTONOSUPPORT
case EPROTONOSUPPORT: return DRFLAC_PROTOCOL_NOT_SUPPORTED;
#endif
#ifdef ESOCKTNOSUPPORT
case ESOCKTNOSUPPORT: return DRFLAC_SOCKET_NOT_SUPPORTED;
#endif
#ifdef EOPNOTSUPP
case EOPNOTSUPP: return DRFLAC_INVALID_OPERATION;
#endif
#ifdef EPFNOSUPPORT
case EPFNOSUPPORT: return DRFLAC_PROTOCOL_FAMILY_NOT_SUPPORTED;
#endif
#ifdef EAFNOSUPPORT
case EAFNOSUPPORT: return DRFLAC_ADDRESS_FAMILY_NOT_SUPPORTED;
#endif
#ifdef EADDRINUSE
case EADDRINUSE: return DRFLAC_ALREADY_IN_USE;
#endif
#ifdef EADDRNOTAVAIL
case EADDRNOTAVAIL: return DRFLAC_ERROR;
#endif
#ifdef ENETDOWN
case ENETDOWN: return DRFLAC_NO_NETWORK;
#endif
#ifdef ENETUNREACH
case ENETUNREACH: return DRFLAC_NO_NETWORK;
#endif
#ifdef ENETRESET
case ENETRESET: return DRFLAC_NO_NETWORK;
#endif
#ifdef ECONNABORTED
case ECONNABORTED: return DRFLAC_NO_NETWORK;
#endif
#ifdef ECONNRESET
case ECONNRESET: return DRFLAC_CONNECTION_RESET;
#endif
#ifdef ENOBUFS
case ENOBUFS: return DRFLAC_NO_SPACE;
#endif
#ifdef EISCONN
case EISCONN: return DRFLAC_ALREADY_CONNECTED;
#endif
#ifdef ENOTCONN
case ENOTCONN: return DRFLAC_NOT_CONNECTED;
#endif
#ifdef ESHUTDOWN
case ESHUTDOWN: return DRFLAC_ERROR;
#endif
#ifdef ETOOMANYREFS
case ETOOMANYREFS: return DRFLAC_ERROR;
#endif
#ifdef ETIMEDOUT
case ETIMEDOUT: return DRFLAC_TIMEOUT;
#endif
#ifdef ECONNREFUSED
case ECONNREFUSED: return DRFLAC_CONNECTION_REFUSED;
#endif
#ifdef EHOSTDOWN
case EHOSTDOWN: return DRFLAC_NO_HOST;
#endif
#ifdef EHOSTUNREACH
case EHOSTUNREACH: return DRFLAC_NO_HOST;
#endif
#ifdef EALREADY
case EALREADY: return DRFLAC_IN_PROGRESS;
#endif
#ifdef EINPROGRESS
case EINPROGRESS: return DRFLAC_IN_PROGRESS;
#endif
#ifdef ESTALE
case ESTALE: return DRFLAC_INVALID_FILE;
#endif
#ifdef EUCLEAN
case EUCLEAN: return DRFLAC_ERROR;
#endif
#ifdef ENOTNAM
case ENOTNAM: return DRFLAC_ERROR;
#endif
#ifdef ENAVAIL
case ENAVAIL: return DRFLAC_ERROR;
#endif
#ifdef EISNAM
case EISNAM: return DRFLAC_ERROR;
#endif
#ifdef EREMOTEIO
case EREMOTEIO: return DRFLAC_IO_ERROR;
#endif
#ifdef EDQUOT
case EDQUOT: return DRFLAC_NO_SPACE;
#endif
#ifdef ENOMEDIUM
case ENOMEDIUM: return DRFLAC_DOES_NOT_EXIST;
#endif
#ifdef EMEDIUMTYPE
case EMEDIUMTYPE: return DRFLAC_ERROR;
#endif
#ifdef ECANCELED
case ECANCELED: return DRFLAC_CANCELLED;
#endif
#ifdef ENOKEY
case ENOKEY: return DRFLAC_ERROR;
#endif
#ifdef EKEYEXPIRED
case EKEYEXPIRED: return DRFLAC_ERROR;
#endif
#ifdef EKEYREVOKED
case EKEYREVOKED: return DRFLAC_ERROR;
#endif
#ifdef EKEYREJECTED
case EKEYREJECTED: return DRFLAC_ERROR;
#endif
#ifdef EOWNERDEAD
case EOWNERDEAD: return DRFLAC_ERROR;
#endif
#ifdef ENOTRECOVERABLE
case ENOTRECOVERABLE: return DRFLAC_ERROR;
#endif
#ifdef ERFKILL
case ERFKILL: return DRFLAC_ERROR;
#endif
#ifdef EHWPOISON
case EHWPOISON: return DRFLAC_ERROR;
#endif
default: return DRFLAC_ERROR;
}
}
/* End Errno */
/* fopen */
static drflac_result drflac_fopen(FILE** ppFile, const char* pFilePath, const char* pOpenMode)
{
#if defined(_MSC_VER) && _MSC_VER >= 1400
errno_t err;
#endif
if (ppFile != NULL) {
*ppFile = NULL; /* Safety. */
}
if (pFilePath == NULL || pOpenMode == NULL || ppFile == NULL) {
return DRFLAC_INVALID_ARGS;
}
#if defined(_MSC_VER) && _MSC_VER >= 1400
err = fopen_s(ppFile, pFilePath, pOpenMode);
if (err != 0) {
return drflac_result_from_errno(err);
}
#else
#if defined(_WIN32) || defined(__APPLE__)
*ppFile = fopen(pFilePath, pOpenMode);
#else
#if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64 && defined(_LARGEFILE64_SOURCE)
*ppFile = fopen64(pFilePath, pOpenMode);
#else
*ppFile = fopen(pFilePath, pOpenMode);
#endif
#endif
if (*ppFile == NULL) {
drflac_result result = drflac_result_from_errno(errno);
if (result == DRFLAC_SUCCESS) {
result = DRFLAC_ERROR; /* Just a safety check to make sure we never ever return success when pFile == NULL. */
}
return result;
}
#endif
return DRFLAC_SUCCESS;
}
/*
_wfopen() isn't always available in all compilation environments.
* Windows only.
* MSVC seems to support it universally as far back as VC6 from what I can tell (haven't checked further back).
* MinGW-64 (both 32- and 64-bit) seems to support it.
* MinGW wraps it in !defined(__STRICT_ANSI__).
* OpenWatcom wraps it in !defined(_NO_EXT_KEYS).
This can be reviewed as compatibility issues arise. The preference is to use _wfopen_s() and _wfopen() as opposed to the wcsrtombs()
fallback, so if you notice your compiler not detecting this properly I'm happy to look at adding support.
*/
#if defined(_WIN32)
#if defined(_MSC_VER) || defined(__MINGW64__) || (!defined(__STRICT_ANSI__) && !defined(_NO_EXT_KEYS))
#define DRFLAC_HAS_WFOPEN
#endif
#endif
#ifndef DR_FLAC_NO_WCHAR
static drflac_result drflac_wfopen(FILE** ppFile, const wchar_t* pFilePath, const wchar_t* pOpenMode, const drflac_allocation_callbacks* pAllocationCallbacks)
{
if (ppFile != NULL) {
*ppFile = NULL; /* Safety. */
}
if (pFilePath == NULL || pOpenMode == NULL || ppFile == NULL) {
return DRFLAC_INVALID_ARGS;
}
#if defined(DRFLAC_HAS_WFOPEN)
{
/* Use _wfopen() on Windows. */
#if defined(_MSC_VER) && _MSC_VER >= 1400
errno_t err = _wfopen_s(ppFile, pFilePath, pOpenMode);
if (err != 0) {
return drflac_result_from_errno(err);
}
#else
*ppFile = _wfopen(pFilePath, pOpenMode);
if (*ppFile == NULL) {
return drflac_result_from_errno(errno);
}
#endif
(void)pAllocationCallbacks;
}
#else
/*
Use fopen() on anything other than Windows. Requires a conversion. This is annoying because
fopen() is locale specific. The only real way I can think of to do this is with wcsrtombs(). Note
that wcstombs() is apparently not thread-safe because it uses a static global mbstate_t object for
maintaining state. I've checked this with -std=c89 and it works, but if somebody get's a compiler
error I'll look into improving compatibility.
*/
/*
Some compilers don't support wchar_t or wcsrtombs() which we're using below. In this case we just
need to abort with an error. If you encounter a compiler lacking such support, add it to this list
and submit a bug report and it'll be added to the library upstream.
*/
#if defined(__DJGPP__)
{
/* Nothing to do here. This will fall through to the error check below. */
}
#else
{
mbstate_t mbs;
size_t lenMB;
const wchar_t* pFilePathTemp = pFilePath;
char* pFilePathMB = NULL;
char pOpenModeMB[32] = {0};
/* Get the length first. */
DRFLAC_ZERO_OBJECT(&mbs);
lenMB = wcsrtombs(NULL, &pFilePathTemp, 0, &mbs);
if (lenMB == (size_t)-1) {
return drflac_result_from_errno(errno);
}
pFilePathMB = (char*)drflac__malloc_from_callbacks(lenMB + 1, pAllocationCallbacks);
if (pFilePathMB == NULL) {
return DRFLAC_OUT_OF_MEMORY;
}
pFilePathTemp = pFilePath;
DRFLAC_ZERO_OBJECT(&mbs);
wcsrtombs(pFilePathMB, &pFilePathTemp, lenMB + 1, &mbs);
/* The open mode should always consist of ASCII characters so we should be able to do a trivial conversion. */
{
size_t i = 0;
for (;;) {
if (pOpenMode[i] == 0) {
pOpenModeMB[i] = '\0';
break;
}
pOpenModeMB[i] = (char)pOpenMode[i];
i += 1;
}
}
*ppFile = fopen(pFilePathMB, pOpenModeMB);
drflac__free_from_callbacks(pFilePathMB, pAllocationCallbacks);
}
#endif
if (*ppFile == NULL) {
return DRFLAC_ERROR;
}
#endif
return DRFLAC_SUCCESS;
}
#endif
/* End fopen */
static size_t drflac__on_read_stdio(void* pUserData, void* bufferOut, size_t bytesToRead)
{
return fread(bufferOut, 1, bytesToRead, (FILE*)pUserData);
}
static drflac_bool32 drflac__on_seek_stdio(void* pUserData, int offset, drflac_seek_origin origin)
{
int whence = SEEK_SET;
if (origin == DRFLAC_SEEK_CUR) {
whence = SEEK_CUR;
} else if (origin == DRFLAC_SEEK_END) {
whence = SEEK_END;
}
return fseek((FILE*)pUserData, offset, whence) == 0;
}
static drflac_bool32 drflac__on_tell_stdio(void* pUserData, drflac_int64* pCursor)
{
FILE* pFileStdio = (FILE*)pUserData;
drflac_int64 result;
/* These were all validated at a higher level. */
DRFLAC_ASSERT(pFileStdio != NULL);
DRFLAC_ASSERT(pCursor != NULL);
#if defined(_WIN32) && !defined(NXDK)
#if defined(_MSC_VER) && _MSC_VER > 1200
result = _ftelli64(pFileStdio);
#else
result = ftell(pFileStdio);
#endif
#else
result = ftell(pFileStdio);
#endif
*pCursor = result;
return DRFLAC_TRUE;
}
DRFLAC_API drflac* drflac_open_file(const char* pFileName, const drflac_allocation_callbacks* pAllocationCallbacks)
{
drflac* pFlac;
FILE* pFile;
if (drflac_fopen(&pFile, pFileName, "rb") != DRFLAC_SUCCESS) {
return NULL;
}
pFlac = drflac_open(drflac__on_read_stdio, drflac__on_seek_stdio, drflac__on_tell_stdio, (void*)pFile, pAllocationCallbacks);
if (pFlac == NULL) {
fclose(pFile);
return NULL;
}
return pFlac;
}
#ifndef DR_FLAC_NO_WCHAR
DRFLAC_API drflac* drflac_open_file_w(const wchar_t* pFileName, const drflac_allocation_callbacks* pAllocationCallbacks)
{
drflac* pFlac;
FILE* pFile;
if (drflac_wfopen(&pFile, pFileName, L"rb", pAllocationCallbacks) != DRFLAC_SUCCESS) {
return NULL;
}
pFlac = drflac_open(drflac__on_read_stdio, drflac__on_seek_stdio, drflac__on_tell_stdio, (void*)pFile, pAllocationCallbacks);
if (pFlac == NULL) {
fclose(pFile);
return NULL;
}
return pFlac;
}
#endif
DRFLAC_API drflac* drflac_open_file_with_metadata(const char* pFileName, drflac_meta_proc onMeta, void* pUserData, const drflac_allocation_callbacks* pAllocationCallbacks)
{
drflac* pFlac;
FILE* pFile;
if (drflac_fopen(&pFile, pFileName, "rb") != DRFLAC_SUCCESS) {
return NULL;
}
pFlac = drflac_open_with_metadata_private(drflac__on_read_stdio, drflac__on_seek_stdio, drflac__on_tell_stdio, onMeta, drflac_container_unknown, (void*)pFile, pUserData, pAllocationCallbacks);
if (pFlac == NULL) {
fclose(pFile);
return pFlac;
}
return pFlac;
}
#ifndef DR_FLAC_NO_WCHAR
DRFLAC_API drflac* drflac_open_file_with_metadata_w(const wchar_t* pFileName, drflac_meta_proc onMeta, void* pUserData, const drflac_allocation_callbacks* pAllocationCallbacks)
{
drflac* pFlac;
FILE* pFile;
if (drflac_wfopen(&pFile, pFileName, L"rb", pAllocationCallbacks) != DRFLAC_SUCCESS) {
return NULL;
}
pFlac = drflac_open_with_metadata_private(drflac__on_read_stdio, drflac__on_seek_stdio, drflac__on_tell_stdio, onMeta, drflac_container_unknown, (void*)pFile, pUserData, pAllocationCallbacks);
if (pFlac == NULL) {
fclose(pFile);
return pFlac;
}
return pFlac;
}
#endif
#endif /* DR_FLAC_NO_STDIO */
static size_t drflac__on_read_memory(void* pUserData, void* bufferOut, size_t bytesToRead)
{
@ -9054,27 +8275,6 @@ DRFLAC_API void drflac_close(drflac* pFlac)
return;
}
#ifndef DR_FLAC_NO_STDIO
/*
If we opened the file with drflac_open_file() we will want to close the file handle. We can know whether or not drflac_open_file()
was used by looking at the callbacks.
*/
if (pFlac->bs.onRead == drflac__on_read_stdio) {
fclose((FILE*)pFlac->bs.pUserData);
}
#ifndef DR_FLAC_NO_OGG
/* Need to clean up Ogg streams a bit differently due to the way the bit streaming is chained. */
if (pFlac->container == drflac_container_ogg) {
drflac_oggbs* oggbs = (drflac_oggbs*)pFlac->_oggbs;
DRFLAC_ASSERT(pFlac->bs.onRead == drflac__on_read_ogg);
if (oggbs->onRead == drflac__on_read_stdio) {
fclose((FILE*)oggbs->pUserData);
}
}
#endif
#endif
drflac__free_from_callbacks(pFlac, &pFlac->allocationCallbacks);
}
@ -11928,73 +11128,6 @@ DRFLAC_API float* drflac_open_and_read_pcm_frames_f32(drflac_read_proc onRead, d
return drflac__full_read_and_close_f32(pFlac, channelsOut, sampleRateOut, totalPCMFrameCountOut);
}
#ifndef DR_FLAC_NO_STDIO
DRFLAC_API drflac_int32* drflac_open_file_and_read_pcm_frames_s32(const char* filename, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks)
{
drflac* pFlac;
if (sampleRate) {
*sampleRate = 0;
}
if (channels) {
*channels = 0;
}
if (totalPCMFrameCount) {
*totalPCMFrameCount = 0;
}
pFlac = drflac_open_file(filename, pAllocationCallbacks);
if (pFlac == NULL) {
return NULL;
}
return drflac__full_read_and_close_s32(pFlac, channels, sampleRate, totalPCMFrameCount);
}
DRFLAC_API drflac_int16* drflac_open_file_and_read_pcm_frames_s16(const char* filename, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks)
{
drflac* pFlac;
if (sampleRate) {
*sampleRate = 0;
}
if (channels) {
*channels = 0;
}
if (totalPCMFrameCount) {
*totalPCMFrameCount = 0;
}
pFlac = drflac_open_file(filename, pAllocationCallbacks);
if (pFlac == NULL) {
return NULL;
}
return drflac__full_read_and_close_s16(pFlac, channels, sampleRate, totalPCMFrameCount);
}
DRFLAC_API float* drflac_open_file_and_read_pcm_frames_f32(const char* filename, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks)
{
drflac* pFlac;
if (sampleRate) {
*sampleRate = 0;
}
if (channels) {
*channels = 0;
}
if (totalPCMFrameCount) {
*totalPCMFrameCount = 0;
}
pFlac = drflac_open_file(filename, pAllocationCallbacks);
if (pFlac == NULL) {
return NULL;
}
return drflac__full_read_and_close_f32(pFlac, channels, sampleRate, totalPCMFrameCount);
}
#endif
DRFLAC_API drflac_int32* drflac_open_memory_and_read_pcm_frames_s32(const void* data, size_t dataSize, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks)
{

View file

@ -381,7 +381,7 @@ typedef chd_error (*chd_codec_interface_decompress)(void *codec, const uint8_t *
/* open an existing CHD file */
CHD_EXPORT chd_error chd_open_core_file_callbacks(const core_file_callbacks *callbacks, const void *user_data, int mode, chd_file *parent, chd_file **chd);
CHD_EXPORT chd_error chd_open_core_file(core_file *file, int mode, chd_file *parent, chd_file **chd); /* Legacy; use chd_open_core_file_callbacks instead! */
CHD_EXPORT chd_error chd_open_file(FILE *file, int mode, chd_file *parent, chd_file **chd);
CHD_EXPORT chd_error chd_open_file(RFILE *file, int mode, chd_file *parent, chd_file **chd);
CHD_EXPORT chd_error chd_open(const char *filename, int mode, chd_file *parent, chd_file **chd);
/* precache underlying file */
@ -406,7 +406,7 @@ CHD_EXPORT const chd_header *chd_get_header(chd_file *chd);
/* read CHD header data from file into the pointed struct */
CHD_EXPORT chd_error chd_read_header_core_file_callbacks(const core_file_callbacks *callback, const void *user_data, chd_header *header);
CHD_EXPORT chd_error chd_read_header_core_file(core_file *file, chd_header *header); /* Legacy; use chd_read_header_core_file_callbacks instead! */
CHD_EXPORT chd_error chd_read_header_file(FILE *file, chd_header *header);
CHD_EXPORT chd_error chd_read_header_file(RFILE *file, chd_header *header);
CHD_EXPORT chd_error chd_read_header(const char *filename, chd_header *header);

View file

@ -2,11 +2,7 @@
#define __CORETYPES_H__
#include <stdint.h>
#include <stdio.h>
#ifdef USE_LIBRETRO_VFS
#include <streams/file_stream_transforms.h>
#endif
#include <streams/file_stream.h>
#include "macros.h"

View file

@ -858,7 +858,7 @@ static const core_file_callbacks core_legacy = {
chd_open_file - open a CHD file for access
-------------------------------------------------*/
CHD_EXPORT chd_error chd_open_file(FILE *file, int mode, chd_file *parent, chd_file **chd) {
CHD_EXPORT chd_error chd_open_file(RFILE *file, int mode, chd_file *parent, chd_file **chd) {
return chd_open_core_file_callbacks(&core_stdio_nonowner, file, mode, parent, chd);
}
@ -1368,7 +1368,7 @@ CHD_EXPORT chd_error chd_read_header_core_file(core_file *file, chd_header *head
from file into the pointed struct
-------------------------------------------------*/
CHD_EXPORT chd_error chd_read_header_file(FILE *file, chd_header *header)
CHD_EXPORT chd_error chd_read_header_file(RFILE *file, chd_header *header)
{
return chd_read_header_core_file_callbacks(&core_stdio_nonowner, file, header);
}
@ -2107,7 +2107,9 @@ static chd_error metadata_find_entry(chd_file *chd, uint32_t metatag, uint32_t m
core_stdio_fopen - core_file wrapper over fopen
-------------------------------------------------*/
static void *core_stdio_fopen(char const *path) {
return fopen(path, "rb");
return filestream_open(path,
RETRO_VFS_FILE_ACCESS_READ,
RETRO_VFS_FILE_ACCESS_HINT_NONE);
}
/*-------------------------------------------------
@ -2115,45 +2117,28 @@ static void *core_stdio_fopen(char const *path) {
getting file size with stdio
-------------------------------------------------*/
static uint64_t core_stdio_fsize(void *file) {
#if defined USE_LIBRETRO_VFS
#define core_stdio_fseek_impl fseek
#define core_stdio_ftell_impl ftell
#elif defined(__WIN32__) || defined(_WIN32) || defined(WIN32) || defined(__WIN64__)
#define core_stdio_fseek_impl _fseeki64
#define core_stdio_ftell_impl _ftelli64
#elif defined(_LARGEFILE_SOURCE) && defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
#define core_stdio_fseek_impl fseeko64
#define core_stdio_ftell_impl ftello64
#elif defined(__PS3__) && !defined(__PSL1GHT__) || defined(__SWITCH__) || defined(__vita__)
#define core_stdio_fseek_impl(x,y,z) fseek(x,(off_t)y,z)
#define core_stdio_ftell_impl(x) (off_t)ftell(x)
#else
#define core_stdio_fseek_impl fseeko
#define core_stdio_ftell_impl ftello
#endif
FILE *fp;
uint64_t p, rv;
fp = (FILE*)file;
RFILE *fp = (RFILE*)file;
int64_t p, rv;
p = core_stdio_ftell_impl(fp);
core_stdio_fseek_impl(fp, 0, SEEK_END);
rv = core_stdio_ftell_impl(fp);
core_stdio_fseek_impl(fp, p, SEEK_SET);
return rv;
p = filestream_tell(fp);
filestream_seek(fp, 0, SEEK_END);
rv = filestream_tell(fp);
filestream_seek(fp, p, SEEK_SET);
return (uint64_t)rv;
}
/*-------------------------------------------------
core_stdio_fread - core_file wrapper over fread
-------------------------------------------------*/
static size_t core_stdio_fread(void *ptr, size_t size, size_t nmemb, void *file) {
return fread(ptr, size, nmemb, (FILE*)file);
return (size_t)filestream_read((RFILE*)file, ptr, size * nmemb) / (size ? size : 1);
}
/*-------------------------------------------------
core_stdio_fclose - core_file wrapper over fclose
-------------------------------------------------*/
static int core_stdio_fclose(void *file) {
return fclose((FILE*)file);
return filestream_close((RFILE*)file);
}
/*-------------------------------------------------
@ -2169,7 +2154,7 @@ static int core_stdio_fclose_nonowner(void *file) {
core_stdio_fseek - core_file wrapper over fclose
-------------------------------------------------*/
static int core_stdio_fseek(void* file, int64_t offset, int whence) {
return core_stdio_fseek_impl((FILE*)file, offset, whence);
return (filestream_seek((RFILE*)file, offset, whence) < 0) ? -1 : 0;
}
/*-------------------------------------------------

View file

@ -13,7 +13,6 @@
#include "../include/libchdr/flac.h"
#include "../include/libchdr/macros.h"
#define DR_FLAC_IMPLEMENTATION
#define DR_FLAC_NO_STDIO
#include "../include/dr_libs/dr_flac.h"
/***************************************************************************

View file

@ -11,9 +11,6 @@
#if defined(_WIN32)
#include "windows_headers.h"
#include <direct.h>
#include <io.h>
#include <share.h>
#include <malloc.h>
#else
#include <sys/stat.h>
@ -24,21 +21,20 @@
#endif
#include <file/file_path.h>
#include <encodings/utf.h>
class FileByteStream : public ByteStream
{
public:
FileByteStream(FILE* pFile) : m_pFile(pFile) { }
FileByteStream(RFILE* pFile) : m_pFile(pFile) { }
virtual ~FileByteStream() override { fclose(m_pFile); }
virtual ~FileByteStream() override { rfclose(m_pFile); }
bool ReadByte(uint8_t* pDestByte) override
{
if (m_errorState)
return false;
if (fread(pDestByte, 1, 1, m_pFile) != 1)
if (rfread(pDestByte, 1, 1, m_pFile) != 1)
{
m_errorState = true;
return false;
@ -52,8 +48,8 @@ public:
if (m_errorState)
return 0;
uint32_t readCount = (uint32_t)fread(pDestination, 1, ByteCount, m_pFile);
if (readCount != ByteCount && ferror(m_pFile) != 0)
uint32_t readCount = (uint32_t)rfread(pDestination, 1, ByteCount, m_pFile);
if (readCount != ByteCount && filestream_error(m_pFile) != 0)
m_errorState = true;
return readCount;
@ -83,7 +79,7 @@ public:
if (m_errorState)
return false;
if (fwrite(&SourceByte, 1, 1, m_pFile) != 1)
if (rfwrite(&SourceByte, 1, 1, m_pFile) != 1)
{
m_errorState = true;
return false;
@ -97,7 +93,7 @@ public:
if (m_errorState)
return 0;
uint32_t writeCount = (uint32_t)fwrite(pSource, 1, ByteCount, m_pFile);
uint32_t writeCount = (uint32_t)rfwrite(pSource, 1, ByteCount, m_pFile);
if (writeCount != ByteCount)
m_errorState = true;
@ -123,13 +119,12 @@ public:
return true;
}
#if defined(_WIN32)
bool SeekAbsolute(uint64_t Offset) override
{
if (m_errorState)
return false;
if (_fseeki64(m_pFile, Offset, SEEK_SET) != 0)
if (rfseek(m_pFile, static_cast<int64_t>(Offset), SEEK_SET) != 0)
{
m_errorState = true;
return false;
@ -138,49 +133,23 @@ public:
return true;
}
uint64_t GetPosition() const override { return _ftelli64(m_pFile); }
uint64_t GetPosition() const override { return static_cast<uint64_t>(rftell(m_pFile)); }
uint64_t GetSize() const override
{
int64_t OldPos = _ftelli64(m_pFile);
_fseeki64(m_pFile, 0, SEEK_END);
int64_t Size = _ftelli64(m_pFile);
_fseeki64(m_pFile, OldPos, SEEK_SET);
int64_t OldPos = rftell(m_pFile);
rfseek(m_pFile, 0, SEEK_END);
int64_t Size = rftell(m_pFile);
rfseek(m_pFile, OldPos, SEEK_SET);
return (uint64_t)Size;
}
#else
bool SeekAbsolute(uint64_t Offset) override
{
if (m_errorState)
return false;
if (fseeko(m_pFile, static_cast<off_t>(Offset), SEEK_SET) != 0)
{
m_errorState = true;
return false;
}
return true;
}
uint64_t GetPosition() const override { return static_cast<uint64_t>(ftello(m_pFile)); }
uint64_t GetSize() const override
{
off_t OldPos = ftello(m_pFile);
fseeko(m_pFile, 0, SEEK_END);
off_t Size = ftello(m_pFile);
fseeko(m_pFile, OldPos, SEEK_SET);
return (uint64_t)Size;
}
#endif
bool Flush() override
{
if (m_errorState)
return false;
if (fflush(m_pFile) != 0)
if (filestream_flush(m_pFile) != 0)
{
m_errorState = true;
return false;
@ -194,13 +163,13 @@ public:
virtual bool Discard() override { return false; }
protected:
FILE* m_pFile;
RFILE* m_pFile;
};
class AtomicUpdatedFileByteStream final : public FileByteStream
{
public:
AtomicUpdatedFileByteStream(FILE* pFile, std::string originalFileName, std::string temporaryFileName)
AtomicUpdatedFileByteStream(RFILE* pFile, std::string originalFileName, std::string temporaryFileName)
: FileByteStream(pFile), m_committed(false), m_discarded(false), m_originalFileName(std::move(originalFileName)),
m_temporaryFileName(std::move(temporaryFileName))
{
@ -210,22 +179,15 @@ public:
{
if (m_discarded)
{
#if defined(_WIN32)
// delete the temporary file
wchar_t *a = utf8_to_utf16_string_alloc(m_temporaryFileName.c_str());
if (!DeleteFileW(a)) { }
free(a);
#else
// delete the temporary file
if (remove(m_temporaryFileName.c_str()) < 0) { }
#endif
}
else if (!m_committed)
{
Commit();
}
// fclose called by FileByteStream destructor
// rfclose called by FileByteStream destructor
}
bool Commit() override
@ -233,25 +195,13 @@ public:
if (m_committed)
return Flush();
fflush(m_pFile);
filestream_flush(m_pFile);
#if defined(_WIN32)
// move the atomic file name to the original file name
wchar_t *a = utf8_to_utf16_string_alloc(m_temporaryFileName.c_str());
wchar_t *b = utf8_to_utf16_string_alloc(m_originalFileName.c_str());
if (!MoveFileExW(a, b, MOVEFILE_REPLACE_EXISTING))
m_discarded = true;
else
m_committed = true;
free(a);
free(b);
#else
// move the atomic file name to the original file name
if (rename(m_temporaryFileName.c_str(), m_originalFileName.c_str()) < 0)
m_discarded = true;
else
m_committed = true;
#endif
return (!m_discarded);
}
@ -634,152 +584,6 @@ void GrowableMemoryByteStream::Grow(uint32_t MinimumGrowth)
ResizeMemory(NewSize);
}
#if defined(_WIN32)
std::unique_ptr<ByteStream> ByteStream_OpenFileStream(const char* fileName, uint32_t openMode)
{
if ((openMode & (BYTESTREAM_OPEN_CREATE | BYTESTREAM_OPEN_WRITE)) == BYTESTREAM_OPEN_WRITE)
{
// if opening with write but not create, the path must exist.
if (!path_is_valid(fileName))
return nullptr;
}
char modeString[16];
uint32_t modeStringLength = 0;
if (openMode & BYTESTREAM_OPEN_WRITE)
{
// if the file exists, use r+, otherwise w+
// HACK: if we're not truncating, and the file exists (we want to only update it), we still have to use r+
if (!path_is_valid(fileName))
{
modeString[modeStringLength++] = 'w';
if (openMode & BYTESTREAM_OPEN_READ)
modeString[modeStringLength++] = '+';
}
else
{
modeString[modeStringLength++] = 'r';
modeString[modeStringLength++] = '+';
}
modeString[modeStringLength++] = 'b';
}
else if (openMode & BYTESTREAM_OPEN_READ)
{
modeString[modeStringLength++] = 'r';
modeString[modeStringLength++] = 'b';
}
// doesn't work with _fdopen
if (!(openMode & BYTESTREAM_OPEN_ATOMIC_UPDATE))
{
if (openMode & BYTESTREAM_OPEN_STREAMED)
modeString[modeStringLength++] = 'S';
}
modeString[modeStringLength] = 0;
if (openMode & BYTESTREAM_OPEN_ATOMIC_UPDATE)
{
// generate the temporary file name
uint32_t fileNameLength = static_cast<uint32_t>(std::strlen(fileName));
char* temporaryFileName = (char*)alloca(fileNameLength + 8);
std::snprintf(temporaryFileName, fileNameLength + 8, "%s.XXXXXX", fileName);
// fill in random characters
_mktemp_s(temporaryFileName, fileNameLength + 8);
wchar_t *wideTemporaryFileName = utf8_to_utf16_string_alloc(temporaryFileName);
// massive hack here
DWORD desiredAccess = GENERIC_WRITE;
if (openMode & BYTESTREAM_OPEN_READ)
desiredAccess |= GENERIC_READ;
HANDLE hFile =
CreateFileW(wideTemporaryFileName, desiredAccess, FILE_SHARE_DELETE, NULL, CREATE_NEW, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
free(wideTemporaryFileName);
return nullptr;
}
// get fd from this
int fd = _open_osfhandle(reinterpret_cast<intptr_t>(hFile), 0);
if (fd < 0)
{
CloseHandle(hFile);
DeleteFileW(wideTemporaryFileName);
free(wideTemporaryFileName);
return nullptr;
}
// convert to a stream
FILE* pTemporaryFile = _fdopen(fd, modeString);
if (!pTemporaryFile)
{
_close(fd);
DeleteFileW(wideTemporaryFileName);
free(wideTemporaryFileName);
return nullptr;
}
// create the stream pointer
std::unique_ptr<AtomicUpdatedFileByteStream> pStream =
std::make_unique<AtomicUpdatedFileByteStream>(pTemporaryFile, fileName, temporaryFileName);
// do we need to copy the existing file into this one?
if (!(openMode & BYTESTREAM_OPEN_TRUNCATE))
{
RFILE* pOriginalFile = FileSystem::OpenRFile(fileName, "rb");
if (!pOriginalFile)
{
// this will delete the temporary file
pStream->Discard();
free(wideTemporaryFileName);
return nullptr;
}
static const size_t BUFFERSIZE = 4096;
uint8_t buffer[BUFFERSIZE];
while (!rfeof(pOriginalFile))
{
size_t nBytes = rfread(buffer, BUFFERSIZE, sizeof(uint8_t), pOriginalFile);
if (nBytes == 0)
break;
if (pStream->Write(buffer, (uint32_t)nBytes) != (uint32_t)nBytes)
{
pStream->Discard();
rfclose(pOriginalFile);
free(wideTemporaryFileName);
return nullptr;
}
}
// close original file
rfclose(pOriginalFile);
}
free(wideTemporaryFileName);
// return pointer
return pStream;
}
else
{
// forward through
FILE* pFile = FileSystem::OpenCFile(fileName, modeString);
if (!pFile)
return nullptr;
return std::make_unique<FileByteStream>(pFile);
}
}
#else
std::unique_ptr<ByteStream> ByteStream_OpenFileStream(const char* fileName, uint32_t openMode)
{
if ((openMode & (BYTESTREAM_OPEN_CREATE | BYTESTREAM_OPEN_WRITE)) == BYTESTREAM_OPEN_WRITE)
@ -817,21 +621,11 @@ std::unique_ptr<ByteStream> ByteStream_OpenFileStream(const char* fileName, uint
// generate the temporary file name
const uint32_t fileNameLength = static_cast<uint32_t>(std::strlen(fileName));
char* temporaryFileName = (char*)alloca(fileNameLength + 8);
std::snprintf(temporaryFileName, fileNameLength + 8, "%s.XXXXXX", fileName);
std::snprintf(temporaryFileName, fileNameLength + 8, "%s.tmp", fileName);
std::FILE* pTemporaryFile;
// fill in random characters
#ifdef HAVE_MKSTEMP
int fd = mkstemp(temporaryFileName);
if (fd == -1)
return nullptr;
pTemporaryFile = fdopen(fd, modeString);
#else
if (mktemp(temporaryFileName) == nullptr)
return nullptr;
pTemporaryFile = std::fopen(temporaryFileName, modeString);
#endif
if (pTemporaryFile == nullptr)
// open the temporary file through libretro VFS
RFILE* pTemporaryFile = rfopen(temporaryFileName, modeString);
if (!pTemporaryFile)
return nullptr;
// create the stream pointer
@ -845,7 +639,7 @@ std::unique_ptr<ByteStream> ByteStream_OpenFileStream(const char* fileName, uint
if (!pOriginalFile)
{
// this will delete the temporary file
pStream->SetErrorState();
pStream->Discard();
return nullptr;
}
@ -853,13 +647,13 @@ std::unique_ptr<ByteStream> ByteStream_OpenFileStream(const char* fileName, uint
uint8_t buffer[BUFFERSIZE];
while (!rfeof(pOriginalFile))
{
size_t nBytes = rfread(buffer, BUFFERSIZE, sizeof(uint8_t), pOriginalFile);
if (nBytes == 0)
int64_t nBytes = rfread(buffer, sizeof(uint8_t), BUFFERSIZE, pOriginalFile);
if (nBytes <= 0)
break;
if (pStream->Write(buffer, (uint32_t)nBytes) != (uint32_t)nBytes)
{
pStream->SetErrorState();
pStream->Discard();
rfclose(pOriginalFile);
return nullptr;
}
@ -874,7 +668,7 @@ std::unique_ptr<ByteStream> ByteStream_OpenFileStream(const char* fileName, uint
}
else
{
std::FILE* pFile = std::fopen(fileName, modeString);
RFILE* pFile = rfopen(fileName, modeString);
if (!pFile)
return nullptr;
@ -882,8 +676,6 @@ std::unique_ptr<ByteStream> ByteStream_OpenFileStream(const char* fileName, uint
}
}
#endif
std::unique_ptr<MemoryByteStream> ByteStream_CreateMemoryStream(void* pMemory, uint32_t Size)
{
return std::make_unique<MemoryByteStream>(pMemory, Size);

View file

@ -250,41 +250,6 @@ std::unique_ptr<ByteStream> OpenFile(const char* FileName, uint32_t Flags)
return ByteStream_OpenFileStream(FileName, Flags);
}
std::FILE* OpenCFile(const char* filename, const char* mode)
{
#ifdef _WIN32
int filename_len = static_cast<int>(std::strlen(filename));
int mode_len = static_cast<int>(std::strlen(mode));
int wlen = MultiByteToWideChar(CP_UTF8, 0, filename, filename_len, nullptr, 0);
int wmodelen = MultiByteToWideChar(CP_UTF8, 0, mode, mode_len, nullptr, 0);
if (wlen > 0 && wmodelen > 0)
{
wchar_t* wfilename = static_cast<wchar_t*>(alloca(sizeof(wchar_t) * (wlen + 1)));
wchar_t* wmode = static_cast<wchar_t*>(alloca(sizeof(wchar_t) * (wmodelen + 1)));
wlen = MultiByteToWideChar(CP_UTF8, 0, filename, filename_len, wfilename, wlen);
wmodelen = MultiByteToWideChar(CP_UTF8, 0, mode, mode_len, wmode, wmodelen);
if (wlen > 0 && wmodelen > 0)
{
wfilename[wlen] = 0;
wmode[wmodelen] = 0;
std::FILE* fp;
if (_wfopen_s(&fp, wfilename, wmode) != 0)
return nullptr;
return fp;
}
}
std::FILE* fp;
if (fopen_s(&fp, filename, mode) != 0)
return nullptr;
return fp;
#else
return std::fopen(filename, mode);
#endif
}
std::optional<std::vector<uint8_t>> ReadBinaryFile(const char* filename)
{

View file

@ -61,8 +61,6 @@ bool FindFiles(const char* Path, const char* Pattern, uint32_t Flags, FindResult
// open files
std::unique_ptr<ByteStream> OpenFile(const char* FileName, uint32_t Flags);
std::FILE* OpenCFile(const char* filename, const char* mode);
std::optional<std::vector<uint8_t>> ReadBinaryFile(const char* filename);
bool WriteBinaryFile(const char* filename, const void* data, size_t data_length);

View file

@ -1,8 +1,8 @@
#include "program.h"
#include "../byte_stream.h"
#include "../log.h"
#include "../string_util.h"
#include <array>
#include <fstream>
Log_SetChannel(GL);
namespace GL {
@ -57,14 +57,16 @@ GLuint Program::CompileShader(GLenum type, const std::string_view source)
{
Log_ErrorPrintf("Shader failed to compile:\n%s", info_log.c_str());
std::ofstream ofs(StringUtil::StdStringFromFormat("bad_shader_%u.txt", s_next_bad_shader_id++).c_str(),
std::ofstream::out | std::ofstream::binary);
if (ofs.is_open())
std::unique_ptr<ByteStream> ofs = ByteStream_OpenFileStream(
StringUtil::StdStringFromFormat("bad_shader_%u.txt", s_next_bad_shader_id++).c_str(),
BYTESTREAM_OPEN_CREATE | BYTESTREAM_OPEN_WRITE | BYTESTREAM_OPEN_TRUNCATE);
if (ofs)
{
ofs.write(sources[0], source_lengths[0]);
ofs << "\n\nCompile failed, info log:\n";
ofs << info_log;
ofs.close();
ofs->Write(sources[0], static_cast<uint32_t>(source_lengths[0]));
static const char tail[] = "\n\nCompile failed, info log:\n";
ofs->Write(tail, sizeof(tail) - 1);
ofs->Write(info_log.c_str(), static_cast<uint32_t>(info_log.size()));
ofs->Commit();
}
glDeleteShader(id);

View file

@ -40,7 +40,6 @@
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <limits>
#include <thread>