(VFS/libretro-common) Cleanups

This commit is contained in:
libretroadmin 2023-08-20 04:29:42 +02:00
parent 8debf4b88e
commit a9733ab418
1 changed files with 106 additions and 125 deletions

View File

@ -301,7 +301,7 @@ libretro_vfs_implementation_file *retro_vfs_file_open_impl(
{ {
int flags = 0; int flags = 0;
const char *mode_str = NULL; const char *mode_str = NULL;
libretro_vfs_implementation_file *stream = libretro_vfs_implementation_file *stream =
(libretro_vfs_implementation_file*) (libretro_vfs_implementation_file*)
malloc(sizeof(*stream)); malloc(sizeof(*stream));
@ -452,14 +452,14 @@ libretro_vfs_implementation_file *retro_vfs_file_open_impl(
* *
* https://www.freebsd.org/cgi/man.cgi?query=setvbuf&apropos=0&sektion=0&manpath=FreeBSD+11.1-RELEASE&arch=default&format=html * https://www.freebsd.org/cgi/man.cgi?query=setvbuf&apropos=0&sektion=0&manpath=FreeBSD+11.1-RELEASE&arch=default&format=html
* *
* If the size argument is not zero but buf is NULL, * If the size argument is not zero but buf is NULL,
* a buffer of the given size will be allocated immediately, and * a buffer of the given size will be allocated immediately, and
* released on close. This is an extension to ANSI C. * released on close. This is an extension to ANSI C.
* *
* Since C89 does not support specifying a NULL buffer * Since C89 does not support specifying a NULL buffer
* with a non-zero size, we create and track our own buffer for it. * with a non-zero size, we create and track our own buffer for it.
*/ */
/* TODO: this is only useful for a few platforms, /* TODO: this is only useful for a few platforms,
* find which and add ifdef */ * find which and add ifdef */
#if defined(_3DS) #if defined(_3DS)
if (stream->scheme != VFS_SCHEME_CDROM) if (stream->scheme != VFS_SCHEME_CDROM)
@ -648,7 +648,7 @@ int64_t retro_vfs_file_tell_impl(libretro_vfs_implementation_file *stream)
#ifdef HAVE_MMAP #ifdef HAVE_MMAP
/* Need to check stream->mapped because this function /* Need to check stream->mapped because this function
* is called in filestream_open() */ * is called in filestream_open() */
if (stream->mapped && stream->hints & if (stream->mapped && stream->hints &
RETRO_VFS_FILE_ACCESS_HINT_FREQUENT_ACCESS) RETRO_VFS_FILE_ACCESS_HINT_FREQUENT_ACCESS)
return stream->mappos; return stream->mappos;
#endif #endif
@ -740,33 +740,31 @@ int retro_vfs_file_remove_impl(const char *path)
{ {
#if defined(_WIN32) && !defined(_XBOX) #if defined(_WIN32) && !defined(_XBOX)
/* Win32 (no Xbox) */ /* Win32 (no Xbox) */
#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0500
char *path_local = NULL;
#else
wchar_t *path_wide = NULL;
#endif
if (!path || !*path) if (!path || !*path)
return -1; return -1;
{
#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0500 #if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0500
if ((path_local = utf8_to_local_string_alloc(path))) char *path_local = NULL;
{ if ((path_local = utf8_to_local_string_alloc(path)))
int ret = remove(path_local); {
free(path_local); int ret = remove(path_local);
free(path_local);
if (ret == 0) if (ret == 0)
return 0; return 0;
} }
#else #else
if ((path_wide = utf8_to_utf16_string_alloc(path))) wchar_t *path_wide = NULL;
{ if ((path_wide = utf8_to_utf16_string_alloc(path)))
int ret = _wremove(path_wide); {
free(path_wide); int ret = _wremove(path_wide);
free(path_wide);
if (ret == 0) if (ret == 0)
return 0; return 0;
} }
#endif #endif
}
#else #else
if (remove(path) == 0) if (remove(path) == 0)
return 0; return 0;
@ -842,132 +840,115 @@ const char *retro_vfs_file_get_path_impl(
int retro_vfs_stat_impl(const char *path, int32_t *size) int retro_vfs_stat_impl(const char *path, int32_t *size)
{ {
bool is_dir = false; int ret = RETRO_VFS_STAT_IS_VALID;
bool is_character_special = false;
if (!path || !*path)
return 0;
{
#if defined(VITA) #if defined(VITA)
/* Vita / PSP */ /* Vita / PSP */
SceIoStat buf; SceIoStat stat_buf;
int dir_ret; int dir_ret;
char *tmp = NULL; char *tmp = strdup(path);
size_t len = 0; size_t len = strlen(tmp);
if (tmp[len-1] == '/')
tmp[len-1] = '\0';
if (!path || !*path) dir_ret = sceIoGetstat(tmp, &stat_buf);
return 0; free(tmp);
if (dir_ret < 0)
return 0;
tmp = strdup(path); if (size)
len = strlen(tmp); *size = (int32_t)stat_buf.st_size;
if (tmp[len-1] == '/')
tmp[len-1] = '\0';
dir_ret = sceIoGetstat(tmp, &buf); if (FIO_S_ISDIR(stat_buf.st_mode))
free(tmp); ret |= RETRO_VFS_STAT_IS_DIRECTORY;
if (dir_ret < 0)
return 0;
if (size)
*size = (int32_t)buf.st_size;
is_dir = FIO_S_ISDIR(buf.st_mode);
#elif defined(__PSL1GHT__) || defined(__PS3__) #elif defined(__PSL1GHT__) || defined(__PS3__)
/* Lowlevel Lv2 */ /* Lowlevel Lv2 */
sysFSStat buf; sysFSStat stat_buf;
if (!path || !*path) if (sysFsStat(path, &buf) < 0)
return 0; return 0;
if (sysFsStat(path, &buf) < 0)
return 0;
if (size) if (size)
*size = (int32_t)buf.st_size; *size = (int32_t)stat_buf.st_size;
is_dir = ((buf.st_mode & S_IFMT) == S_IFDIR); if ((stat_buf.st_mode & S_IFMT) == S_IFDIR)
ret |= RETRO_VFS_STAT_IS_DIRECTORY;
#elif defined(_WIN32) #elif defined(_WIN32)
/* Windows */ /* Windows */
DWORD file_info; struct _stat stat_buf;
struct _stat buf;
#if defined(LEGACY_WIN32) #if defined(LEGACY_WIN32)
char *path_local = NULL; char *path_local = utf8_to_local_string_alloc(path);
DWORD file_info = GetFileAttributes(path_local);
if (!string_is_empty(path_local))
_stat(path_local, &stat_buf);
if (path_local)
free(path_local);
#else #else
wchar_t *path_wide = NULL; wchar_t *path_wide = utf8_to_utf16_string_alloc(path);
DWORD file_info = GetFileAttributesW(path_wide);
_wstat(path_wide, &stat_buf);
if (path_wide)
free(path_wide);
#endif #endif
if (file_info == INVALID_FILE_ATTRIBUTES)
return 0;
if (!path || !*path) if (size)
return 0; *size = (int32_t)stat_buf.st_size;
#if defined(LEGACY_WIN32)
path_local = utf8_to_local_string_alloc(path);
file_info = GetFileAttributes(path_local);
if (!string_is_empty(path_local))
_stat(path_local, &buf);
if (path_local)
free(path_local);
#else
path_wide = utf8_to_utf16_string_alloc(path);
file_info = GetFileAttributesW(path_wide);
_wstat(path_wide, &buf);
if (path_wide)
free(path_wide);
#endif
if (file_info == INVALID_FILE_ATTRIBUTES)
return 0;
if (size)
*size = (int32_t)buf.st_size;
is_dir = (file_info & FILE_ATTRIBUTE_DIRECTORY);
if (file_info & FILE_ATTRIBUTE_DIRECTORY)
ret |= RETRO_VFS_STAT_IS_DIRECTORY;
#elif defined(GEKKO) #elif defined(GEKKO)
/* On GEKKO platforms, paths cannot have /* On GEKKO platforms, paths cannot have
* trailing slashes - we must therefore * trailing slashes - we must therefore
* remove them */ * remove them */
char *path_buf = NULL; size_t len;
int stat_ret = -1; char *path_buf = NULL;
struct stat stat_buf; struct stat stat_buf;
size_t len;
if (string_is_empty(path)) if (!(path_buf = strdup(path)))
return 0; return 0;
if (!(path_buf = strdup(path))) if ((len = strlen(path_buf)) > 0)
return 0; if (path_buf[len - 1] == '/')
path_buf[len - 1] = '\0';
if ((len = strlen(path_buf)) > 0) free(path_buf);
if (path_buf[len - 1] == '/')
path_buf[len - 1] = '\0';
stat_ret = stat(path_buf, &stat_buf); if (stat(path_buf, &stat_buf) < 0)
free(path_buf); return 0;
if (stat_ret < 0) if (size)
return 0; *size = (int32_t)stat_buf.st_size;
if (size)
*size = (int32_t)stat_buf.st_size;
is_dir = S_ISDIR(stat_buf.st_mode);
is_character_special = S_ISCHR(stat_buf.st_mode);
if (S_ISDIR(stat_buf.st_mode))
ret |= RETRO_VFS_STAT_IS_DIRECTORY;
if (S_ISCHR(stat_buf.st_mode))
ret |= RETRO_VFS_STAT_IS_CHARACTER_SPECIAL;
#else #else
/* Every other platform */ /* Every other platform */
struct stat buf; struct stat stat_buf;
if (!path || !*path) if (stat(path, &stat_buf) < 0)
return 0; return 0;
if (stat(path, &buf) < 0)
return 0;
if (size) if (size)
*size = (int32_t)buf.st_size; *size = (int32_t)stat_buf.st_size;
is_dir = S_ISDIR(buf.st_mode); if (S_ISDIR(stat_buf.st_mode))
is_character_special = S_ISCHR(buf.st_mode); ret |= RETRO_VFS_STAT_IS_DIRECTORY;
if (S_ISCHR(stat_buf.st_mode))
ret |= RETRO_VFS_STAT_IS_CHARACTER_SPECIAL;
#endif #endif
return RETRO_VFS_STAT_IS_VALID | (is_dir ? RETRO_VFS_STAT_IS_DIRECTORY : 0) | (is_character_special ? RETRO_VFS_STAT_IS_CHARACTER_SPECIAL : 0); }
return ret;
} }
#if defined(VITA) #if defined(VITA)