]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/journal/catalog.c
util-lib: split our string related calls from util.[ch] into its own file string...
[thirdparty/systemd.git] / src / journal / catalog.c
index e505a05305fe241e49069e7e99b31a312ddf4f10..32b851aea38e8ab6fee136abeabc41de601d43e8 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#include <errno.h>
 #include <fcntl.h>
+#include <locale.h>
 #include <stdio.h>
-#include <unistd.h>
-#include <errno.h>
 #include <string.h>
 #include <sys/mman.h>
-#include <locale.h>
+#include <unistd.h>
 
-#include "util.h"
-#include "log.h"
-#include "sparse-endian.h"
 #include "sd-id128.h"
-#include "hashmap.h"
-#include "strv.h"
-#include "strbuf.h"
+
+#include "catalog.h"
 #include "conf-files.h"
+#include "hashmap.h"
+#include "log.h"
 #include "mkdir.h"
-#include "catalog.h"
 #include "siphash24.h"
+#include "sparse-endian.h"
+#include "strbuf.h"
+#include "string-util.h"
+#include "strv.h"
+#include "util.h"
 
 const char * const catalog_file_dirs[] = {
         "/usr/local/lib/systemd/catalog/",
@@ -62,21 +64,11 @@ typedef struct CatalogItem {
         le64_t offset;
 } CatalogItem;
 
-static unsigned long catalog_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) {
+static void catalog_hash_func(const void *p, struct siphash *state) {
         const CatalogItem *i = p;
-        uint64_t u;
-        size_t l, sz;
-        void *v;
 
-        l = strlen(i->language);
-        sz = sizeof(i->id) + l;
-        v = alloca(sz);
-
-        memcpy(mempcpy(v, &i->id, sizeof(i->id)), i->language, l);
-
-        siphash24((uint8_t*) &u, v, sz, hash_key);
-
-        return (unsigned long) u;
+        siphash24_compress(&i->id, sizeof(i->id), state);
+        siphash24_compress(i->language, strlen(i->language), state);
 }
 
 static int catalog_compare_func(const void *a, const void *b) {
@@ -263,8 +255,7 @@ int catalog_import_file(Hashmap *h, struct strbuf *sb, const char *path) {
                                         if (r < 0)
                                                 return r;
 
-                                        free(lang);
-                                        lang = NULL;
+                                        lang = mfree(lang);
                                 }
 
                                 if (with_language) {
@@ -371,25 +362,23 @@ static long write_catalog(const char *database, Hashmap *h, struct strbuf *sb,
                 goto error;
         }
 
-        fflush(w);
-
-        if (ferror(w)) {
-                log_error("%s: failed to write database.", p);
+        r = fflush_and_check(w);
+        if (r < 0) {
+                log_error_errno(r, "%s: failed to write database: %m", p);
                 goto error;
         }
 
         fchmod(fileno(w), 0644);
 
         if (rename(p, database) < 0) {
-                log_error_errno(errno, "rename (%s -> %s) failed: %m", p, database);
-                r = -errno;
+                r = log_error_errno(errno, "rename (%s -> %s) failed: %m", p, database);
                 goto error;
         }
 
         return ftell(w);
 
 error:
-        unlink(p);
+        (void) unlink(p);
         return r;
 }
 
@@ -422,8 +411,7 @@ int catalog_update(const char* database, const char* root, const char* const* di
                 log_debug("Reading file '%s'", *f);
                 r = catalog_import_file(h, sb, *f);
                 if (r < 0) {
-                        log_error("Failed to import file '%s': %s.",
-                                  *f, strerror(-r));
+                        log_error_errno(r, "Failed to import file '%s': %m", *f);
                         goto finish;
                 }
         }
@@ -558,7 +546,7 @@ static const char *find_id(void *p, sd_id128_t id) {
 int catalog_get(const char* database, sd_id128_t id, char **_text) {
         _cleanup_close_ int fd = -1;
         void *p = NULL;
-        struct stat st;
+        struct stat st = {};
         char *text = NULL;
         int r;
         const char *s;
@@ -679,8 +667,7 @@ int catalog_list_items(FILE *f, const char *database, bool oneline, char **items
 
                 k = sd_id128_from_string(*item, &id);
                 if (k < 0) {
-                        log_error_errno(k, "Failed to parse id128 '%s': %m",
-                                        *item);
+                        log_error_errno(k, "Failed to parse id128 '%s': %m", *item);
                         if (r == 0)
                                 r = k;
                         continue;
@@ -688,9 +675,8 @@ int catalog_list_items(FILE *f, const char *database, bool oneline, char **items
 
                 k = catalog_get(database, id, &msg);
                 if (k < 0) {
-                        log_full(k == -ENOENT ? LOG_NOTICE : LOG_ERR,
-                                 "Failed to retrieve catalog entry for '%s': %s",
-                                  *item, strerror(-k));
+                        log_full_errno(k == -ENOENT ? LOG_NOTICE : LOG_ERR, k,
+                                       "Failed to retrieve catalog entry for '%s': %m", *item);
                         if (r == 0)
                                 r = k;
                         continue;