From 857d601d5d576c4d513b764ecf50b888a8f0bed4 Mon Sep 17 00:00:00 2001 From: "Joseph C. Osborn" Date: Wed, 9 Aug 2023 08:44:45 -0700 Subject: [PATCH 1/3] Graceful fallback to readonly mode if RDB can't be opened readwrite --- libretro-db/libretrodb.c | 54 +++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/libretro-db/libretrodb.c b/libretro-db/libretrodb.c index 3113959d37f..42f8066deec 100644 --- a/libretro-db/libretrodb.c +++ b/libretro-db/libretrodb.c @@ -46,45 +46,46 @@ struct node_iter_ctx { - libretrodb_t *db; - libretrodb_index_t *idx; + libretrodb_t *db; + libretrodb_index_t *idx; }; struct libretrodb { - RFILE *fd; + RFILE *fd; char *path; - uint64_t root; - uint64_t count; - uint64_t first_index_offset; + bool can_write; + uint64_t root; + uint64_t count; + uint64_t first_index_offset; }; struct libretrodb_index { - char name[50]; - uint64_t key_size; - uint64_t next; - uint64_t count; + char name[50]; + uint64_t key_size; + uint64_t next; + uint64_t count; }; typedef struct libretrodb_metadata { - uint64_t count; + uint64_t count; } libretrodb_metadata_t; typedef struct libretrodb_header { - char magic_number[sizeof(MAGIC_NUMBER)]; - uint64_t metadata_offset; + char magic_number[sizeof(MAGIC_NUMBER)]; + uint64_t metadata_offset; } libretrodb_header_t; struct libretrodb_cursor { RFILE *fd; - libretrodb_query_t *query; - libretrodb_t *db; - int is_valid; - int eof; + libretrodb_query_t *query; + libretrodb_t *db; + int is_valid; + int eof; }; static int libretrodb_validate_document(const struct rmsgpack_dom_value *doc) @@ -187,9 +188,17 @@ int libretrodb_open(const char *path, libretrodb_t *db) RFILE *fd = filestream_open(path, RETRO_VFS_FILE_ACCESS_READ_WRITE | RETRO_VFS_FILE_ACCESS_UPDATE_EXISTING, RETRO_VFS_FILE_ACCESS_HINT_NONE); - + db->can_write = true; if (!fd) - return -1; + { + /* Try to reopen in readonly mode */ + fd = filestream_open(path, + RETRO_VFS_FILE_ACCESS_READ, + RETRO_VFS_FILE_ACCESS_HINT_NONE); + db->can_write = false; + } + if (!fd) + return -1; if (!string_is_empty(db->path)) free(db->path); @@ -456,9 +465,14 @@ int libretrodb_create_index(libretrodb_t *db, uint64_t item_count = 0; int rval = -1; - if (libretrodb_find_index(db, name, &idx) >= 0) { + if (libretrodb_find_index(db, name, &idx) >= 0) + { return 1; } + if (!db->can_write) + { + return -1; + } tree = bintree_new(node_compare, &field_size); From 2d2fb0b0aa909a674658ced2f1b1fa9b2a2f8db4 Mon Sep 17 00:00:00 2001 From: Joe Osborn Date: Wed, 9 Aug 2023 19:53:07 -0700 Subject: [PATCH 2/3] let RA open db readonly --- database_info.c | 2 +- libretro-db/libretrodb.c | 14 +++----------- libretro-db/libretrodb.h | 2 +- libretro-db/libretrodb_tool.c | 2 +- 4 files changed, 6 insertions(+), 14 deletions(-) diff --git a/database_info.c b/database_info.c index 564aeeac1f7..7a462801ce7 100644 --- a/database_info.c +++ b/database_info.c @@ -717,7 +717,7 @@ static int database_cursor_open(libretrodb_t *db, const char *error = NULL; libretrodb_query_t *q = NULL; - if ((libretrodb_open(path, db)) != 0) + if ((libretrodb_open(path, db, false)) != 0) return -1; if (query) diff --git a/libretro-db/libretrodb.c b/libretro-db/libretrodb.c index 42f8066deec..43122c42e3a 100644 --- a/libretro-db/libretrodb.c +++ b/libretro-db/libretrodb.c @@ -181,22 +181,14 @@ void libretrodb_close(libretrodb_t *db) db->fd = NULL; } -int libretrodb_open(const char *path, libretrodb_t *db) +int libretrodb_open(const char *path, libretrodb_t *db, bool write) { libretrodb_header_t header; libretrodb_metadata_t md; RFILE *fd = filestream_open(path, - RETRO_VFS_FILE_ACCESS_READ_WRITE | RETRO_VFS_FILE_ACCESS_UPDATE_EXISTING, + write ? RETRO_VFS_FILE_ACCESS_READ_WRITE | RETRO_VFS_FILE_ACCESS_UPDATE_EXISTING : RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE); - db->can_write = true; - if (!fd) - { - /* Try to reopen in readonly mode */ - fd = filestream_open(path, - RETRO_VFS_FILE_ACCESS_READ, - RETRO_VFS_FILE_ACCESS_HINT_NONE); - db->can_write = false; - } + db->can_write = write; if (!fd) return -1; diff --git a/libretro-db/libretrodb.h b/libretro-db/libretrodb.h index 5b8775e33a6..a2c8619f8c2 100644 --- a/libretro-db/libretrodb.h +++ b/libretro-db/libretrodb.h @@ -49,7 +49,7 @@ int libretrodb_create(RFILE *fd, libretrodb_value_provider value_provider, void void libretrodb_close(libretrodb_t *db); -int libretrodb_open(const char *path, libretrodb_t *db); +int libretrodb_open(const char *path, libretrodb_t *db, bool write); int libretrodb_create_index(libretrodb_t *db, const char *name, const char *field_name); diff --git a/libretro-db/libretrodb_tool.c b/libretro-db/libretrodb_tool.c index 95738065eb5..a90d66d4a9d 100644 --- a/libretro-db/libretrodb_tool.c +++ b/libretro-db/libretrodb_tool.c @@ -60,7 +60,7 @@ int main(int argc, char ** argv) if (!db || !cur) goto error; - if ((rv = libretrodb_open(path, db)) != 0) + if ((rv = libretrodb_open(path, db, true)) != 0) { printf("Could not open db file '%s'\n", path); goto error; From 2b7b765e1e8219648b129d2d26daa1721a128115 Mon Sep 17 00:00:00 2001 From: Joe Osborn Date: Wed, 9 Aug 2023 20:04:53 -0700 Subject: [PATCH 3/3] fix stray open --- menu/menu_explore.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/menu/menu_explore.c b/menu/menu_explore.c index 507cb01ee73..07da868369e 100644 --- a/menu/menu_explore.c +++ b/menu/menu_explore.c @@ -583,7 +583,7 @@ explore_state_t *menu_explore_build_list(const char *directory_playlist, ext_path[3] = 'b'; } - if (libretrodb_open(tmp, newrdb.handle) != 0) + if (libretrodb_open(tmp, newrdb.handle, false) != 0) { /* Invalid RDB file */ libretrodb_free(newrdb.handle);