AmendHub

jcs

/

subtext

/

amendments

/

129

db: Fix db_cache_boards when we have to recreate a file


jcs made amendment 129 5 months ago
--- db.c Tue Jun 7 17:00:56 2022 +++ db.c Thu Jun 9 10:13:04 2022 @@ -368,41 +368,53 @@ db_cache_boards(struct db *tdb) return; tdb->boards = xcalloc(tdb->nboards, sizeof(struct board)); + /* + * Read ids first so we have a consistent order, then try to open or + * fix/recreate each bile, which may change their order in the map + */ for (n = 0; n < tdb->nboards; n++) { obj = bile_get_nth_of_type(tdb->bile, n, DB_BOARD_RTYPE); if (obj == NULL) break; - + size = bile_read_alloc(tdb->bile, DB_BOARD_RTYPE, obj->id, &data); bile_unmarshall_object(tdb->bile, board_object_fields, nboard_object_fields, data, size, (char *)(&tdb->boards[n]), true); free(data); - + } + + for (n = 0; n < tdb->nboards; n++) { snprintf((char *)board_filename, sizeof(board_filename), - "%s:%ld.brd", db_filename, obj->id); + "%s:%ld.brd", db_filename, tdb->boards[n].id); CtoPstr(board_filename); - tdb->boards[n].bile = bile_open(board_filename, tdb->bile->vrefnum); + tdb->boards[n].bile = bile_open(board_filename, + tdb->bile->vrefnum); if (tdb->boards[n].bile != NULL) continue; - if (ask("Attempt recovery with backup map?")) { + PtoCstr(board_filename); + + if (bile_error(NULL) != fnfErr && + ask("Attempt recovery of %s with backup map?", board_filename)) { + CtoPstr(board_filename); tdb->boards[n].bile = bile_open_recover_map(board_filename, tdb->bile->vrefnum); if (tdb->boards[n].bile == NULL) panic("Failed to recover board file %s: %d", PtoCstr(board_filename), bile_error(NULL)); - } else { - if (ask("Failed to open board bile %s (error %d), recreate it?", - PtoCstr(board_filename), bile_error(NULL)) == false) - exit(0); - CtoPstr(board_filename); - tdb->boards[n].bile = db_board_create(tdb, &tdb->boards[n]); - if (tdb->boards[n].bile == NULL) - panic("Failed to create board file %s: %d", - PtoCstr(board_filename), bile_error(NULL)); + continue; } + + if (ask("Failed to open board bile %s (error %d), recreate it?", + board_filename, bile_error(NULL)) == false) + exit(0); + + tdb->boards[n].bile = db_board_create(tdb, &tdb->boards[n]); + if (tdb->boards[n].bile == NULL) + panic("Failed to create board file %s: %d", + board_filename, bile_error(NULL)); } } @@ -439,8 +451,8 @@ db_board_create(struct db *tdb, struct board *board) board_bile = bile_create(board_filename, tdb->bile->vrefnum, SUBTEXT_CREATOR, DB_BOARD_RTYPE); if (board_bile == NULL) - panic("failed creating new board bile at %s: %d", - PtoCstr(board_filename), bile_error(tdb->bile)); + panic("Failed creating new board bile at %s: %d", + PtoCstr(board_filename), bile_error(NULL)); return board_bile; }