AmendHub

jcs

/

subtext

/

amendments

/

297

db: Properly close board and folder biles at shutdown

This was causing subsequent runs under non-MultiFinder to fail to
open files with opWrErr.
Also when we fail to open a bile and recovery fails, fall through to
letting the user recreate the file as we do if the user opts not to
recover the file.

jcs made amendment 297 2 months ago
--- db.c Sun Nov 13 10:08:35 2022 +++ db.c Wed Nov 30 13:29:58 2022 @@ -244,12 +244,32 @@ db_init(Str255 path, short vrefnum, struct bile *bile) void db_close(struct db *tdb) { + short n; + bile_close(tdb->bile); xfree(&tdb->bile); + if (tdb->sessions_bile != NULL) { bile_close(tdb->sessions_bile); xfree(&tdb->sessions_bile); } + + if (tdb->boards) { + for (n = 0; n < tdb->nboards; n++) { + if (tdb->boards[n].bile) + bile_close(tdb->boards[n].bile); + } + xfree(&tdb->boards); + } + + if (tdb->folders) { + for (n = 0; n < tdb->nfolders; n++) { + if (tdb->folders[n].bile) + bile_close(tdb->folders[n].bile); + } + xfree(&tdb->folders); + } + xfree(&tdb); } @@ -613,17 +633,17 @@ db_cache_boards(struct db *tdb) 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)); - continue; + if (tdb->boards[n].bile != NULL) + continue; + warn("Failed to recover board file %s: %d", + PtoCstr(board_filename), bile_error(NULL)); } 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]); + + tdb->boards[n].bile = db_board_create(tdb, &tdb->boards[n], true); if (tdb->boards[n].bile == NULL) panic("Failed to create board file %s: %d", board_filename, bile_error(NULL)); @@ -631,7 +651,7 @@ db_cache_boards(struct db *tdb) } struct bile * -db_board_create(struct db *tdb, struct board *board) +db_board_create(struct db *tdb, struct board *board, bool delete_first) { Str255 db_filename, board_filename; struct bile *board_bile; @@ -660,6 +680,9 @@ db_board_create(struct db *tdb, struct board *board) db_filename, board->id, BOARD_FILENAME_EXT); CtoPstr(board_filename); + if (delete_first) + FSDelete(board_filename, tdb->bile->vrefnum); + board_bile = bile_create(board_filename, tdb->bile->vrefnum, SUBTEXT_CREATOR, DB_BOARD_RTYPE); if (board_bile == NULL) @@ -736,19 +759,20 @@ db_cache_folders(struct db *tdb) CtoPstr(folder_filename); tdb->folders[n].bile = bile_open_recover_map(folder_filename, tdb->bile->vrefnum); - if (tdb->folders[n].bile == NULL) - panic("Failed to recover folder file %s: %d", - PtoCstr(folder_filename), bile_error(NULL)); - continue; + if (tdb->folders[n].bile != NULL) + continue; + warn("Failed to recover folder bile %s: %d", + PtoCstr(folder_filename), bile_error(NULL)); } if (ask("Failed to open folder bile %s (error %d), recreate it?", folder_filename, bile_error(NULL)) == false) exit(0); - tdb->folders[n].bile = db_folder_create(tdb, &tdb->folders[n]); + tdb->folders[n].bile = db_folder_create(tdb, &tdb->folders[n], + true); if (tdb->folders[n].bile == NULL) - panic("Failed to create folder file %s: %d", + panic("Failed to create folder bile %s: %d", folder_filename, bile_error(NULL)); } @@ -763,7 +787,7 @@ db_cache_folders(struct db *tdb) } struct bile * -db_folder_create(struct db *tdb, struct folder *folder) +db_folder_create(struct db *tdb, struct folder *folder, bool delete_first) { Str255 db_filename, folder_filename, folder_dir; struct bile *folder_bile; @@ -792,6 +816,9 @@ db_folder_create(struct db *tdb, struct folder *folder "%s:%ld.%s", db_filename, folder->id, FOLDER_FILENAME_EXT); CtoPstr(folder_filename); + if (delete_first) + FSDelete(folder_filename, tdb->bile->vrefnum); + folder_bile = bile_create(folder_filename, tdb->bile->vrefnum, SUBTEXT_CREATOR, DB_FOLDER_RTYPE); if (folder_bile == NULL) --- db.h Wed Nov 9 09:56:01 2022 +++ db.h Wed Nov 30 13:28:52 2022 @@ -107,8 +107,10 @@ struct db * db_create(void); void db_close(struct db *tdb); void db_config_save(struct db *tdb); void db_cache_boards(struct db *tdb); -struct bile * db_board_create(struct db *tdb, struct board *board); +struct bile * db_board_create(struct db *tdb, struct board *board, + bool delete_first); void db_cache_folders(struct db *tdb); -struct bile * db_folder_create(struct db *tdb, struct folder *folder); +struct bile * db_folder_create(struct db *tdb, struct folder *folder, + bool delete_first); #endif --- sysop.c Wed Sep 14 11:09:13 2022 +++ sysop.c Wed Nov 30 13:29:04 2022 @@ -171,7 +171,7 @@ sysop_edit_boards(struct session *s) continue; } - new_board_bile = db_board_create(db, new_board); + new_board_bile = db_board_create(db, new_board, false); bile_close(new_board_bile); db_cache_boards(db); @@ -294,7 +294,7 @@ sysop_edit_folders(struct session *s) continue; } - new_folder_bile = db_folder_create(db, new_folder); + new_folder_bile = db_folder_create(db, new_folder, false); bile_close(new_folder_bile); db_cache_folders(db);