X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fjournal%2Fcatalog.c;h=dacf5c50a197cb608e07877f63421e222706846c;hb=3ac251b81a41295a90c89c164f0d72ce6de651aa;hp=639e2d1b1bef7a6b21438b5dedcdf9921e19ac14;hpb=83f6936a018b08880670838756e0f4e9ea98b4a7;p=elogind.git diff --git a/src/journal/catalog.c b/src/journal/catalog.c index 639e2d1b1..dacf5c50a 100644 --- a/src/journal/catalog.c +++ b/src/journal/catalog.c @@ -269,6 +269,8 @@ static int import_file(Hashmap *h, struct strbuf *sb, const char *path) { return 0; } +#define CATALOG_DATABASE CATALOG_PATH "/database" + int catalog_update(void) { _cleanup_strv_free_ char **files = NULL; _cleanup_fclose_ FILE *w = NULL; @@ -294,7 +296,7 @@ int catalog_update(void) { goto finish; } - r = conf_files_list_strv(&files, ".catalog", (const char **) conf_file_dirs); + r = conf_files_list_strv(&files, ".catalog", NULL, (const char **) conf_file_dirs); if (r < 0) { log_error("Failed to get catalog files: %s", strerror(-r)); goto finish; @@ -309,7 +311,8 @@ int catalog_update(void) { log_info("No items in catalog."); r = 0; goto finish; - } + } else + log_debug("Found %u items in catalog.", hashmap_size(h)); strbuf_complete(sb); @@ -328,11 +331,16 @@ int catalog_update(void) { assert(n == hashmap_size(h)); qsort(items, n, sizeof(CatalogItem), catalog_compare_func); - mkdir_p("/var/lib/systemd/catalog", 0775); + r = mkdir_p(CATALOG_PATH, 0775); + if (r < 0) { + log_error("Recursive mkdir %s: %s", CATALOG_PATH, strerror(-r)); + goto finish; + } - r = fopen_temporary("/var/lib/systemd/catalog/database", &w, &p); + r = fopen_temporary(CATALOG_DATABASE, &w, &p); if (r < 0) { - log_error("Failed to open database for writing: %s", strerror(-r)); + log_error("Failed to open database for writing: %s: %s", + CATALOG_DATABASE, strerror(-r)); goto finish; } @@ -344,37 +352,40 @@ int catalog_update(void) { k = fwrite(&header, 1, sizeof(header), w); if (k != sizeof(header)) { - log_error("Failed to write 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("Failed to write database."); + log_error("%s: failed to write database.", p); goto finish; } k = fwrite(sb->buf, 1, sb->len, w); if (k != sb->len) { - log_error("Failed to write strings."); + log_error("%s: failed to write strings.", p); goto finish; } fflush(w); if (ferror(w)) { - log_error("Failed to write database."); + log_error("%s: failed to write database.", p); goto finish; } fchmod(fileno(w), 0644); - if (rename(p, "/var/lib/systemd/catalog/database") < 0) { - log_error("rename() failed: %m"); + if (rename(p, CATALOG_DATABASE) < 0) { + log_error("rename (%s -> %s) failed: %m", p, CATALOG_DATABASE); r = -errno; goto finish; } + log_debug("%s: wrote %u items, with %zu bytes of strings, %ld total size.", + CATALOG_DATABASE, n, sb->len, ftell(w)); + free(p); p = NULL; @@ -402,7 +413,7 @@ static int open_mmap(int *_fd, struct stat *_st, void **_p) { assert(_st); assert(_p); - fd = open("/var/lib/systemd/catalog/database", O_RDONLY|O_CLOEXEC); + fd = open(CATALOG_DATABASE, O_RDONLY|O_CLOEXEC); if (fd < 0) return -errno; @@ -540,7 +551,23 @@ static char *find_header(const char *s, const char *header) { } } -int catalog_list(FILE *f) { +static void dump_catalog_entry(FILE *f, sd_id128_t id, const char *s, bool oneline) { + if (oneline) { + _cleanup_free_ char *subject = NULL, *defined_by = NULL; + + subject = find_header(s, "Subject:"); + defined_by = find_header(s, "Defined-By:"); + + fprintf(f, SD_ID128_FORMAT_STR " %s: %s\n", + SD_ID128_FORMAT_VAL(id), + strna(defined_by), strna(subject)); + } else + fprintf(f, "-- " SD_ID128_FORMAT_STR "\n%s\n", + SD_ID128_FORMAT_VAL(id), s); +} + + +int catalog_list(FILE *f, bool oneline) { _cleanup_close_ int fd = -1; void *p = NULL; struct stat st; @@ -560,17 +587,13 @@ int catalog_list(FILE *f) { for (n = 0; n < le64toh(h->n_items); n++) { const char *s; - _cleanup_free_ char *subject = NULL, *defined_by = NULL; if (last_id_set && sd_id128_equal(last_id, items[n].id)) continue; assert_se(s = find_id(p, items[n].id)); - subject = find_header(s, "Subject:"); - defined_by = find_header(s, "Defined-By:"); - - fprintf(f, SD_ID128_FORMAT_STR " %s: %s\n", SD_ID128_FORMAT_VAL(items[n].id), strna(defined_by), strna(subject)); + dump_catalog_entry(f, items[n].id, s, oneline); last_id_set = true; last_id = items[n].id; @@ -580,3 +603,37 @@ int catalog_list(FILE *f) { return 0; } + +int catalog_list_items(FILE *f, bool oneline, char **items) { + char **item; + int r = 0; + + STRV_FOREACH(item, items) { + sd_id128_t id; + int k; + char _cleanup_free_ *msg = NULL; + + k = sd_id128_from_string(*item, &id); + if (k < 0) { + log_error("Failed to parse id128 '%s': %s", + *item, strerror(-r)); + if (r < 0) + r = k; + continue; + } + + k = catalog_get(id, &msg); + if (k < 0) { + log_full(k == -ENOENT ? LOG_NOTICE : LOG_ERR, + "Failed to retrieve catalog entry for '%s': %s", + *item, strerror(-r)); + if (r < 0) + r = k; + continue; + } + + dump_catalog_entry(f, id, msg, oneline); + } + + return r; +}