From d5ea842403f7cc5598fd5f58f74835395e84d052 Mon Sep 17 00:00:00 2001 From: Eric Warmenhoven Date: Sat, 21 Jun 2025 23:33:39 -0400 Subject: [PATCH] apple: speed up cloud sync --- griffin/griffin.c | 2 ++ libretro-common/include/lrc_hash.h | 13 +++++++++++++ libretro-common/vfs/vfs_implementation.c | 12 ++++++++---- tasks/task_cloudsync.c | 21 ++++++++++++--------- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/griffin/griffin.c b/griffin/griffin.c index 10e75c3ddf..9b0bdeadc4 100644 --- a/griffin/griffin.c +++ b/griffin/griffin.c @@ -232,7 +232,9 @@ ACHIEVEMENTS /*============================================================ MD5 ============================================================ */ +#ifndef __APPLE__ #include "../libretro-common/utils/md5.c" +#endif /*============================================================ CHEATS diff --git a/libretro-common/include/lrc_hash.h b/libretro-common/include/lrc_hash.h index 7856e1349e..d97225d66a 100644 --- a/libretro-common/include/lrc_hash.h +++ b/libretro-common/include/lrc_hash.h @@ -35,6 +35,10 @@ #include +#ifdef __APPLE__ +#include +#endif + RETRO_BEGIN_DECLS /** @@ -51,6 +55,13 @@ int sha1_calculate(const char *path, char *result); uint32_t djb2_calculate(const char *str); +#ifdef __APPLE__ +typedef CC_MD5_CTX MD5_CTX; +#define MD5_Init CC_MD5_Init +#define MD5_Update CC_MD5_Update +#define MD5_Final CC_MD5_Final +#else + /* Any 32-bit or wider unsigned integer data type will do */ typedef unsigned int MD5_u32plus; @@ -90,6 +101,8 @@ void MD5_Init(MD5_CTX *ctx); void MD5_Update(MD5_CTX *ctx, const void *data, unsigned long size); void MD5_Final(unsigned char *result, MD5_CTX *ctx); +#endif + RETRO_END_DECLS #endif diff --git a/libretro-common/vfs/vfs_implementation.c b/libretro-common/vfs/vfs_implementation.c index efbbf508c5..e6698ac577 100644 --- a/libretro-common/vfs/vfs_implementation.c +++ b/libretro-common/vfs/vfs_implementation.c @@ -225,6 +225,8 @@ int64_t retro_vfs_file_seek_internal( libretro_vfs_implementation_file *stream, int64_t offset, int whence) { + int64_t val; + if (!stream) return -1; @@ -280,10 +282,10 @@ int64_t retro_vfs_file_seek_internal( } #endif - if (lseek(stream->fd, (off_t)offset, whence) < 0) + if ((val = lseek(stream->fd, (off_t)offset, whence)) < 0) return -1; - return 0; + return val; } /** @@ -637,6 +639,8 @@ int64_t retro_vfs_file_truncate_impl(libretro_vfs_implementation_file *stream, i int64_t retro_vfs_file_tell_impl(libretro_vfs_implementation_file *stream) { + int64_t val; + if (!stream) return -1; @@ -662,10 +666,10 @@ int64_t retro_vfs_file_tell_impl(libretro_vfs_implementation_file *stream) RETRO_VFS_FILE_ACCESS_HINT_FREQUENT_ACCESS) return stream->mappos; #endif - if (lseek(stream->fd, 0, SEEK_CUR) < 0) + if ((val = lseek(stream->fd, 0, SEEK_CUR)) < 0) return -1; - return 0; + return val; } int64_t retro_vfs_file_seek_impl(libretro_vfs_implementation_file *stream, diff --git a/tasks/task_cloudsync.c b/tasks/task_cloudsync.c index b369aee659..e0c92fd2c0 100644 --- a/tasks/task_cloudsync.c +++ b/tasks/task_cloudsync.c @@ -491,19 +491,22 @@ static char *task_cloud_sync_md5_rfile(RFILE *file) char *hash = malloc(33); unsigned char buf[4096]; unsigned char digest[16]; + libretro_vfs_implementation_file *hfile = filestream_get_vfs_handle(file); if (!hash) return NULL; MD5_Init(&md5); - do - { - rv = (int)filestream_read(file, buf, sizeof(buf)); - if (rv > 0) - MD5_Update(&md5, buf, rv); - } while (rv > 0); - + if (hfile && hfile->mapped) + MD5_Update(&md5, hfile->mapped, hfile->size); + else + do + { + rv = (int)filestream_read(file, buf, sizeof(buf)); + if (rv > 0) + MD5_Update(&md5, buf, rv); + } while (rv > 0); MD5_Final(digest, &md5); snprintf(hash, 33, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", @@ -748,7 +751,7 @@ static void task_cloud_sync_upload_current_file(task_cloud_sync_state_t *sync_st } file = filestream_open(filename, - RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE); + RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_FREQUENT_ACCESS); if (!file) return; @@ -803,7 +806,7 @@ static void task_cloud_sync_check_server_current(task_cloud_sync_state_t *sync_s } file = filestream_open(filename, - RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE); + RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_FREQUENT_ACCESS); if (!file) return;