From d41827ec7e79f7cfd4d5e3d62417103a2ee6f288 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Fri, 26 Apr 2019 05:53:59 +0200 Subject: [PATCH] (VFS implementation) Small cleanups --- libretro-common/vfs/vfs_implementation.c | 182 +++++++++++------------ 1 file changed, 85 insertions(+), 97 deletions(-) diff --git a/libretro-common/vfs/vfs_implementation.c b/libretro-common/vfs/vfs_implementation.c index 2b650cef50..939d8bd326 100644 --- a/libretro-common/vfs/vfs_implementation.c +++ b/libretro-common/vfs/vfs_implementation.c @@ -213,7 +213,7 @@ struct libretro_vfs_implementation_file int64_t retro_vfs_file_seek_internal(libretro_vfs_implementation_file *stream, int64_t offset, int whence) { if (!stream) - goto error; + return -1; if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0) { @@ -226,7 +226,7 @@ int64_t retro_vfs_file_seek_internal(libretro_vfs_implementation_file *stream, i int64_t ret = fileXioLseek(fileno(stream->fp), (off_t)offset, whence); /* fileXioLseek could return positive numbers */ if (ret > 0) - ret = 0; + return 0; return ret; #elif defined(ORBIS) int ret = orbisLseek(stream->fd, offset, whence); @@ -250,22 +250,22 @@ int64_t retro_vfs_file_seek_internal(libretro_vfs_implementation_file *stream, i { case SEEK_SET: if (offset < 0) - goto error; + return -1; stream->mappos = offset; break; case SEEK_CUR: - if ((offset < 0 && stream->mappos + offset > stream->mappos) || + if ( (offset < 0 && stream->mappos + offset > stream->mappos) || (offset > 0 && stream->mappos + offset < stream->mappos)) - goto error; + return -1; stream->mappos += offset; break; case SEEK_END: if (stream->mapsize + offset < stream->mapsize) - goto error; + return -1; stream->mappos = stream->mapsize + offset; break; @@ -275,12 +275,9 @@ int64_t retro_vfs_file_seek_internal(libretro_vfs_implementation_file *stream, i #endif if (lseek(stream->fd, offset, whence) < 0) - goto error; + return -1; return 0; - -error: - return -1; } /** @@ -293,20 +290,25 @@ error: * Returns a pointer to an RFILE if opened successfully, otherwise NULL. **/ -libretro_vfs_implementation_file *retro_vfs_file_open_impl(const char *path, unsigned mode, unsigned hints) +libretro_vfs_implementation_file *retro_vfs_file_open_impl( + const char *path, unsigned mode, unsigned hints) { int flags = 0; const char *mode_str = NULL; - libretro_vfs_implementation_file *stream = (libretro_vfs_implementation_file*)calloc(1, sizeof(*stream)); + libretro_vfs_implementation_file *stream = (libretro_vfs_implementation_file*) + calloc(1, sizeof(*stream)); #ifdef VFS_FRONTEND const char *dumb_prefix = "vfsonly://"; - int dumb_prefix_len = (int)strlen(dumb_prefix); + size_t dumb_prefix_siz = strlen(dumb_prefix); + int dumb_prefix_len = (int)dumb_prefix_siz; int path_len = (int)strlen(path); if (path_len >= dumb_prefix_len) + { if (!memcmp(path, dumb_prefix, dumb_prefix_len)) - path += strlen(dumb_prefix); + path += dumb_prefix_siz; + } #endif if (!stream) @@ -388,7 +390,7 @@ libretro_vfs_implementation_file *retro_vfs_file_open_impl(const char *path, uns { #ifdef ORBIS int fd = orbisOpen(path, flags, 0644); - if( fd < 0) + if (fd < 0) { stream->fd = -1; goto error; @@ -432,7 +434,7 @@ libretro_vfs_implementation_file *retro_vfs_file_open_impl(const char *path, uns free(path_wide); #endif #else - stream->fd = open(path, flags, 0); + stream->fd = open(path, flags, 0); #endif if (stream->fd == -1) @@ -498,7 +500,7 @@ int retro_vfs_file_close_impl(libretro_vfs_implementation_file *stream) { #ifdef ORBIS orbisClose(stream->fd); - stream->fd=-1; + stream->fd = -1; #else close(stream->fd); #endif @@ -524,9 +526,9 @@ int retro_vfs_file_error_impl(libretro_vfs_implementation_file *stream) int64_t retro_vfs_file_size_impl(libretro_vfs_implementation_file *stream) { - if (!stream) - return 0; - return stream->size; + if (stream) + return stream->size; + return 0; } int64_t retro_vfs_file_truncate_impl(libretro_vfs_implementation_file *stream, int64_t length) @@ -535,10 +537,10 @@ int64_t retro_vfs_file_truncate_impl(libretro_vfs_implementation_file *stream, i return -1; #ifdef _WIN32 - if(_chsize(_fileno(stream->fp), length) != 0) + if (_chsize(_fileno(stream->fp), length) != 0) return -1; #elif !defined(VITA) && !defined(PSP) && !defined(PS2) && !defined(ORBIS) && (!defined(SWITCH) || defined(HAVE_LIBNX)) - if(ftruncate(fileno(stream->fp), length) != 0) + if (ftruncate(fileno(stream->fp), length) != 0) return -1; #endif @@ -554,7 +556,7 @@ int64_t retro_vfs_file_tell_impl(libretro_vfs_implementation_file *stream) { #ifdef ORBIS int64_t ret = orbisLseek(stream->fd, 0, SEEK_CUR); - if(ret < 0) + if (ret < 0) return -1; return ret; #else @@ -602,13 +604,12 @@ int64_t retro_vfs_file_read_impl(libretro_vfs_implementation_file *stream, void *s, uint64_t len) { if (!stream || !s) - goto error; + return -1; if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0) { #ifdef ORBIS - int64_t ret = orbisRead(stream->fd, s, (size_t)len); - if( ret < 0) + if (orbisRead(stream->fd, s, (size_t)len) < 0) return -1; return 0; #else @@ -619,7 +620,7 @@ int64_t retro_vfs_file_read_impl(libretro_vfs_implementation_file *stream, if (stream->hints & RETRO_VFS_FILE_ACCESS_HINT_FREQUENT_ACCESS) { if (stream->mappos > stream->mapsize) - goto error; + return -1; if (stream->mappos + len > stream->mapsize) len = stream->mapsize - stream->mappos; @@ -632,35 +633,29 @@ int64_t retro_vfs_file_read_impl(libretro_vfs_implementation_file *stream, #endif return read(stream->fd, s, (size_t)len); - -error: - return -1; } int64_t retro_vfs_file_write_impl(libretro_vfs_implementation_file *stream, const void *s, uint64_t len) { if (!stream) - goto error; + return -1; if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0) { #ifdef ORBIS - int64_t ret = orbisWrite(stream->fd, s, (size_t)len); - if( ret < 0) + if (orbisWrite(stream->fd, s, (size_t)len) < 0) return -1; return 0; #else return fwrite(s, 1, (size_t)len, stream->fp); #endif } + #ifdef HAVE_MMAP if (stream->hints & RETRO_VFS_FILE_ACCESS_HINT_FREQUENT_ACCESS) - goto error; + return -1; #endif return write(stream->fd, s, (size_t)len); - -error: - return -1; } int retro_vfs_file_flush_impl(libretro_vfs_implementation_file *stream) @@ -670,21 +665,23 @@ int retro_vfs_file_flush_impl(libretro_vfs_implementation_file *stream) #ifdef ORBIS return 0; #else - return fflush(stream->fp)==0 ? 0 : -1; + return fflush(stream->fp) == 0 ? 0 : -1; #endif } int retro_vfs_file_remove_impl(const char *path) { +#if defined(_WIN32) && !defined(_XBOX) +#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0500 char *path_local = NULL; +#else wchar_t *path_wide = NULL; +#endif +#endif if (!path || !*path) return -1; - (void)path_local; - (void)path_wide; - #if defined(_WIN32) && !defined(_XBOX) #if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0500 path_local = utf8_to_local_string_alloc(path); @@ -723,66 +720,59 @@ int retro_vfs_file_remove_impl(const char *path) int retro_vfs_file_rename_impl(const char *old_path, const char *new_path) { + int ret = -1; +#if defined(_WIN32) && !defined(_XBOX) +#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0500 char *old_path_local = NULL; - char *new_path_local = NULL; +#else wchar_t *old_path_wide = NULL; - wchar_t *new_path_wide = NULL; +#endif +#endif if (!old_path || !*old_path || !new_path || !*new_path) return -1; - (void)old_path_local; - (void)new_path_local; - (void)old_path_wide; - (void)new_path_wide; - #if defined(_WIN32) && !defined(_XBOX) #if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0500 old_path_local = utf8_to_local_string_alloc(old_path); - new_path_local = utf8_to_local_string_alloc(new_path); if (old_path_local) { + char *new_path_local = utf8_to_local_string_alloc(new_path); + if (new_path_local) { - int ret = rename(old_path_local, new_path_local); - free(old_path_local); + if (rename(old_path_local, new_path_local) == 0) + ret = 0; free(new_path_local); - return ret==0 ? 0 : -1; } free(old_path_local); } - - if (new_path_local) - free(new_path_local); #else old_path_wide = utf8_to_utf16_string_alloc(old_path); - new_path_wide = utf8_to_utf16_string_alloc(new_path); if (old_path_wide) { + wchar_t *new_path_wide = utf8_to_utf16_string_alloc(new_path); + if (new_path_wide) { - int ret = _wrename(old_path_wide, new_path_wide); - free(old_path_wide); + if (_wrename(old_path_wide, new_path_wide) == 0) + ret = 0; free(new_path_wide); - return ret==0 ? 0 : -1; } free(old_path_wide); } - - if (new_path_wide) - free(new_path_wide); #endif - return -1; + return ret; #else #ifdef ORBIS /* stub for now */ return 0; #else - return rename(old_path, new_path)==0 ? 0 : -1; + return rename(old_path, new_path) == 0 ? 0 : -1; #endif #endif } @@ -803,17 +793,16 @@ int retro_vfs_stat_impl(const char *path, int32_t *size) #endif #if defined(VITA) || defined(PSP) SceIoStat buf; + int stat_ret; char *tmp = strdup(path); size_t len = strlen(tmp); if (tmp[len-1] == '/') tmp[len-1] = '\0'; - if (sceIoGetstat(tmp, &buf) < 0) - { - free(tmp); - return 0; - } + stat_ret = sceIoGetstat(tmp, &buf); free(tmp); + if (stat_ret < 0) + return 0; #elif defined(PS2) iox_stat_t buf; char *tmp = strdup(path); @@ -830,16 +819,15 @@ int retro_vfs_stat_impl(const char *path, int32_t *size) #elif defined(_WIN32) DWORD file_info; struct _stat buf; +#if defined(LEGACY_WIN32) char *path_local = NULL; +#else wchar_t *path_wide = NULL; +#endif if (!path || !*path) return 0; - (void)path_wide; - (void)path_local; - (void)file_info; - #if defined(LEGACY_WIN32) path_local = utf8_to_local_string_alloc(path); file_info = GetFileAttributes(path_local); @@ -914,7 +902,7 @@ int retro_vfs_mkdir_impl(const char *dir) { #if defined(_WIN32) #ifdef LEGACY_WIN32 - int ret = _mkdir(dir); + int ret = _mkdir(dir); #else wchar_t *dirW = utf8_to_utf16_string_alloc(dir); int ret = -1; @@ -995,15 +983,18 @@ static bool dirent_check_error(libretro_vfs_implementation_dir *rdir) libretro_vfs_implementation_dir *retro_vfs_opendir_impl(const char *name, bool include_hidden) { #if defined(_WIN32) - char path_buf[1024]; - char *path_local = NULL; - wchar_t *path_wide = NULL; unsigned path_len; + char path_buf[1024]; +#if defined(LEGACY_WIN32) + char *path_local = NULL; +#else + wchar_t *path_wide = NULL; +#endif #endif libretro_vfs_implementation_dir *rdir; /*Reject null or empty string paths*/ - if (!name||(*name==0)) + if (!name || (*name == 0)) return NULL; /*Allocate RDIR struct. Tidied later with retro_closedir*/ @@ -1014,11 +1005,8 @@ libretro_vfs_implementation_dir *retro_vfs_opendir_impl(const char *name, bool i rdir->orig_path = strdup(name); #if defined(_WIN32) - (void)path_wide; - (void)path_local; - - path_buf[0] = '\0'; - path_len = strlen(name); + path_buf[0] = '\0'; + path_len = strlen(name); /* Non-NT platforms don't like extra slashes in the path */ if (name[path_len - 1] == '\\') @@ -1027,33 +1015,33 @@ libretro_vfs_implementation_dir *retro_vfs_opendir_impl(const char *name, bool i snprintf(path_buf, sizeof(path_buf), "%s\\*", name); #if defined(LEGACY_WIN32) - path_local = utf8_to_local_string_alloc(path_buf); - rdir->directory = FindFirstFile(path_local, &rdir->entry); + path_local = utf8_to_local_string_alloc(path_buf); + rdir->directory = FindFirstFile(path_local, &rdir->entry); if (path_local) free(path_local); #else - path_wide = utf8_to_utf16_string_alloc(path_buf); - rdir->directory = FindFirstFileW(path_wide, &rdir->entry); + path_wide = utf8_to_utf16_string_alloc(path_buf); + rdir->directory = FindFirstFileW(path_wide, &rdir->entry); if (path_wide) free(path_wide); #endif #elif defined(VITA) || defined(PSP) - rdir->directory = sceIoDopen(name); + rdir->directory = sceIoDopen(name); #elif defined(PS2) - rdir->directory = ps2fileXioDopen(name); + rdir->directory = ps2fileXioDopen(name); #elif defined(_3DS) - rdir->directory = !string_is_empty(name) ? opendir(name) : NULL; - rdir->entry = NULL; + rdir->directory = !string_is_empty(name) ? opendir(name) : NULL; + rdir->entry = NULL; #elif defined(__CELLOS_LV2__) - rdir->error = cellFsOpendir(name, &rdir->directory); + rdir->error = cellFsOpendir(name, &rdir->directory); #elif defined(ORBIS) - rdir->directory = orbisDopen(name); + rdir->directory = orbisDopen(name); #else - rdir->directory = opendir(name); - rdir->entry = NULL; + rdir->directory = opendir(name); + rdir->entry = NULL; #endif #ifdef _WIN32 @@ -1073,7 +1061,7 @@ libretro_vfs_implementation_dir *retro_vfs_opendir_impl(const char *name, bool i bool retro_vfs_readdir_impl(libretro_vfs_implementation_dir *rdir) { #if defined(_WIN32) - if(rdir->next) + if (rdir->next) #if defined(LEGACY_WIN32) return (FindNextFile(rdir->directory, &rdir->entry) != 0); #else @@ -1149,7 +1137,7 @@ bool retro_vfs_dirent_is_dir_impl(libretro_vfs_implementation_dir *rdir) return (entry->d_type == CELL_FS_TYPE_DIRECTORY); #elif defined(ORBIS) const struct dirent *entry = &rdir->entry; - if (entry->d_type==DT_DIR) + if (entry->d_type == DT_DIR) return true; if (!(entry->d_type == DT_UNKNOWN || entry->d_type == DT_LNK)) return false;