- r = mkdir_p(CATALOG_PATH, 0775);
- if (r < 0) {
- log_error("Recursive mkdir %s: %s", CATALOG_PATH, strerror(-r));
- goto finish;
- }
-
- r = fopen_temporary(CATALOG_DATABASE, &w, &p);
- if (r < 0) {
- log_error("Failed to open database for writing: %s: %s",
- CATALOG_DATABASE, strerror(-r));
- goto finish;
- }
-
- zero(header);
- memcpy(header.signature, CATALOG_SIGNATURE, sizeof(header.signature));
- header.header_size = htole64(ALIGN_TO(sizeof(CatalogHeader), 8));
- header.catalog_item_size = htole64(sizeof(CatalogItem));
- header.n_items = htole64(hashmap_size(h));
-
- k = fwrite(&header, 1, sizeof(header), w);
- if (k != sizeof(header)) {
- log_error("%s: failed to write header.", p);
- goto finish;
- }
-
- k = fwrite(items, 1, n * sizeof(CatalogItem), w);
- if (k != n * sizeof(CatalogItem)) {
- log_error("%s: failed to write database.", p);
- goto finish;
- }
-
- k = fwrite(sb->buf, 1, sb->len, w);
- if (k != sb->len) {
- log_error("%s: failed to write strings.", p);
- goto finish;
- }
-
- fflush(w);
-
- if (ferror(w)) {
- log_error("%s: failed to write database.", p);
- goto finish;
- }
-
- fchmod(fileno(w), 0644);
-
- if (rename(p, CATALOG_DATABASE) < 0) {
- log_error("rename (%s -> %s) failed: %m", p, CATALOG_DATABASE);
- r = -errno;
- goto finish;
- }
-
- log_info("%s: wrote %u items, with %zu bytes of strings, %zu total size.",
- CATALOG_DATABASE, n, sb->len, ftell(w));
-
- free(p);
- p = NULL;
+ r = write_catalog(database, h, sb, items, n);
+ if (r < 0)
+ log_error("Failed to write %s: %s", database, strerror(-r));
+ else
+ log_debug("%s: wrote %u items, with %zu bytes of strings, %ld total size.",
+ database, n, sb->len, r);