Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion database_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
50 changes: 28 additions & 22 deletions libretro-db/libretrodb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -180,16 +181,16 @@ 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 = write;
if (!fd)
return -1;
return -1;

if (!string_is_empty(db->path))
free(db->path);
Expand Down Expand Up @@ -456,9 +457,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);

Expand Down
2 changes: 1 addition & 1 deletion libretro-db/libretrodb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion libretro-db/libretrodb_tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion menu/menu_explore.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading