Graceful fallback to readonly mode if RDB can't be opened readwrite (#15569)

* Graceful fallback to readonly mode if RDB can't be opened readwrite
* let RA open db readonly
This commit is contained in:
Joe Osborn 2023-08-10 11:04:47 -07:00 committed by GitHub
parent d0d1321ac1
commit 679083a08b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 26 deletions

View File

@ -717,7 +717,7 @@ static int database_cursor_open(libretrodb_t *db,
const char *error = NULL; const char *error = NULL;
libretrodb_query_t *q = NULL; libretrodb_query_t *q = NULL;
if ((libretrodb_open(path, db)) != 0) if ((libretrodb_open(path, db, false)) != 0)
return -1; return -1;
if (query) if (query)

View File

@ -54,6 +54,7 @@ struct libretrodb
{ {
RFILE *fd; RFILE *fd;
char *path; char *path;
bool can_write;
uint64_t root; uint64_t root;
uint64_t count; uint64_t count;
uint64_t first_index_offset; uint64_t first_index_offset;
@ -180,14 +181,14 @@ void libretrodb_close(libretrodb_t *db)
db->fd = NULL; 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_header_t header;
libretrodb_metadata_t md; libretrodb_metadata_t md;
RFILE *fd = filestream_open(path, 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); RETRO_VFS_FILE_ACCESS_HINT_NONE);
db->can_write = write;
if (!fd) if (!fd)
return -1; return -1;
@ -456,9 +457,14 @@ int libretrodb_create_index(libretrodb_t *db,
uint64_t item_count = 0; uint64_t item_count = 0;
int rval = -1; int rval = -1;
if (libretrodb_find_index(db, name, &idx) >= 0) { if (libretrodb_find_index(db, name, &idx) >= 0)
{
return 1; return 1;
} }
if (!db->can_write)
{
return -1;
}
tree = bintree_new(node_compare, &field_size); tree = bintree_new(node_compare, &field_size);

View File

@ -49,7 +49,7 @@ int libretrodb_create(RFILE *fd, libretrodb_value_provider value_provider, void
void libretrodb_close(libretrodb_t *db); 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, int libretrodb_create_index(libretrodb_t *db, const char *name,
const char *field_name); const char *field_name);

View File

@ -60,7 +60,7 @@ int main(int argc, char ** argv)
if (!db || !cur) if (!db || !cur)
goto error; 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); printf("Could not open db file '%s'\n", path);
goto error; goto error;

View File

@ -583,7 +583,7 @@ explore_state_t *menu_explore_build_list(const char *directory_playlist,
ext_path[3] = 'b'; ext_path[3] = 'b';
} }
if (libretrodb_open(tmp, newrdb.handle) != 0) if (libretrodb_open(tmp, newrdb.handle, false) != 0)
{ {
/* Invalid RDB file */ /* Invalid RDB file */
libretrodb_free(newrdb.handle); libretrodb_free(newrdb.handle);