AmendHub

Download:

jcs

/

subtext

/

amendments

/

185

db: Create and check for folder directories, sort boards/folders


jcs made amendment 185 over 2 years ago
--- db.c Wed Jun 29 09:35:09 2022 +++ db.c Thu Jul 7 17:30:23 2022 @@ -401,6 +401,8 @@ db_cache_boards(struct db *tdb) { Str255 db_filename, board_filename; size_t n, size; + unsigned long *ids; + short error; struct bile_object *obj; char *data = NULL; @@ -417,7 +419,8 @@ db_cache_boards(struct db *tdb) panic("getpath failed on %s", PtoCstr(tdb->bile->filename)); PtoCstr(db_filename); - tdb->nboards = bile_count_by_type(tdb->bile, DB_BOARD_RTYPE); + tdb->nboards = bile_sorted_ids_by_type(tdb->bile, DB_BOARD_RTYPE, + &ids); if (!tdb->nboards) return; tdb->boards = xcalloc(tdb->nboards, sizeof(struct board)); @@ -427,10 +430,10 @@ db_cache_boards(struct db *tdb) * 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); + obj = bile_find(tdb->bile, DB_BOARD_RTYPE, ids[n]); if (obj == NULL) break; - + size = bile_read_alloc(tdb->bile, DB_BOARD_RTYPE, obj->id, &data); bile_unmarshall_object(tdb->bile, board_object_fields, @@ -439,6 +442,8 @@ db_cache_boards(struct db *tdb) free(data); } + free(ids); + for (n = 0; n < tdb->nboards; n++) { snprintf((char *)board_filename, sizeof(board_filename), "%s:%ld.%s", db_filename, tdb->boards[n].id, BOARD_FILENAME_EXT); @@ -514,8 +519,9 @@ db_board_create(struct db *tdb, struct board *board) void db_cache_folders(struct db *tdb) { - Str255 db_filename, folder_filename; + Str255 db_filename, folder_filename, folder_dir; size_t n, size; + unsigned long *ids; struct bile_object *obj; char *data = NULL; @@ -532,7 +538,8 @@ db_cache_folders(struct db *tdb) panic("getpath failed on %s", PtoCstr(tdb->bile->filename)); PtoCstr(db_filename); - tdb->nfolders = bile_count_by_type(tdb->bile, DB_FOLDER_RTYPE); + tdb->nfolders = bile_sorted_ids_by_type(tdb->bile, DB_FOLDER_RTYPE, + &ids); if (!tdb->nfolders) return; tdb->folders = xcalloc(tdb->nfolders, sizeof(struct folder)); @@ -542,7 +549,7 @@ db_cache_folders(struct db *tdb) * fix/recreate each bile, which may change their order in the map */ for (n = 0; n < tdb->nfolders; n++) { - obj = bile_get_nth_of_type(tdb->bile, n, DB_FOLDER_RTYPE); + obj = bile_find(tdb->bile, DB_FOLDER_RTYPE, ids[n]); if (obj == NULL) break; @@ -553,6 +560,8 @@ db_cache_folders(struct db *tdb) free(data); } + free(ids); + for (n = 0; n < tdb->nfolders; n++) { snprintf((char *)folder_filename, sizeof(folder_filename), "%s:%ld.%s", db_filename, tdb->folders[n].id, @@ -586,15 +595,24 @@ db_cache_folders(struct db *tdb) panic("Failed to create folder file %s: %d", folder_filename, bile_error(NULL)); } + + for (n = 0; n < tdb->nfolders; n++) { + /* make sure directory exists */ + memcpy(folder_dir, tdb->folders[n].path, sizeof(folder_dir)); + CtoPstr(folder_dir); + if (!FIsDir(folder_dir)) + panic("Folder %ld path %s does not exist", + tdb->folders[n].id, tdb->folders[n].path); + } } struct bile * db_folder_create(struct db *tdb, struct folder *folder) { - Str255 db_filename, folder_filename; + Str255 db_filename, folder_filename, folder_dir; struct bile *folder_bile; size_t size; - short ret; + short ret, newid, error; char *data; ret = bile_marshall_object(tdb->bile, folder_object_fields, @@ -623,6 +641,14 @@ db_folder_create(struct db *tdb, struct folder *folder if (folder_bile == NULL) panic("Failed creating new folder bile at %s: %d", PtoCstr(folder_filename), bile_error(NULL)); + + memcpy(folder_dir, folder->path, sizeof(folder_dir)); + CtoPstr(folder_dir); + if (!FIsDir(folder_dir)) { + error = DirCreate(tdb->bile->vrefnum, 0, folder_dir, &newid); + if (error) + warn("Failed creating %s: %d", folder->path, error); + } return folder_bile; }