]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/shared/hwdb-util.c
machined: downgrade warning if we cannot drop ref to systemd unit if disconnected...
[thirdparty/systemd.git] / src / shared / hwdb-util.c
index a2fbcd7078d3a9ad75a26c4d2ad1583cc4750c44..d96902c7f26fe737eb1f38f6340c55dcad9c5384 100644 (file)
@@ -6,6 +6,7 @@
 
 #include "alloc-util.h"
 #include "conf-files.h"
+#include "env-util.h"
 #include "fd-util.h"
 #include "fileio.h"
 #include "fs-util.h"
@@ -193,7 +194,7 @@ static int trie_insert(struct trie *trie, struct trie_node *node, const char *se
 
         for (size_t i = 0;; i++) {
                 size_t p;
-                uint8_t c;
+                char c;
                 struct trie_node *child;
 
                 for (p = 0; (c = trie->strings->buf[node->prefix_off + p]); p++) {
@@ -408,7 +409,7 @@ static int trie_store(struct trie *trie, const char *filename, bool compat) {
                 return -errno;
         fwrite(&h, sizeof(struct trie_header_f), 1, f);
 
-        r = flink_tmpfile(f, filename_tmp, filename, /* replace= */ true);
+        r = flink_tmpfile(f, filename_tmp, filename, LINK_TMPFILE_REPLACE|LINK_TMPFILE_SYNC);
         if (r < 0)
                 return r;
 
@@ -483,7 +484,7 @@ static int import_file(struct trie *trie, const char *filename, uint16_t file_pr
                 if (r == 0)
                         break;
 
-                line_number ++;
+                line_number++;
 
                 /* comment line */
                 if (line[0] == '#')
@@ -587,6 +588,10 @@ int hwdb_update(const char *root, const char *hwdb_bin_dir, bool strict, bool co
          * source. If true, then hwdb.bin will be created without the information. systemd-hwdb command
          * should set the argument false, and 'udevadm hwdb' command should set it true. */
 
+        hwdb_bin = path_join(root, hwdb_bin_dir ?: "/etc/udev", "hwdb.bin");
+        if (!hwdb_bin)
+                return -ENOMEM;
+
         trie = new0(struct trie, 1);
         if (!trie)
                 return -ENOMEM;
@@ -607,6 +612,18 @@ int hwdb_update(const char *root, const char *hwdb_bin_dir, bool strict, bool co
         if (err < 0)
                 return log_error_errno(err, "Failed to enumerate hwdb files: %m");
 
+        if (strv_isempty(files)) {
+                if (unlink(hwdb_bin) < 0) {
+                        if (errno != ENOENT)
+                                return log_error_errno(errno, "Failed to remove compiled hwdb database %s: %m", hwdb_bin);
+
+                        log_info("No hwdb files found, skipping.");
+                } else
+                        log_info("No hwdb files found, compiled hwdb database %s removed.", hwdb_bin);
+
+                return 0;
+        }
+
         STRV_FOREACH(f, files) {
                 log_debug("Reading file \"%s\"", *f);
                 err = import_file(trie, *f, file_priority++, compat);
@@ -630,10 +647,6 @@ int hwdb_update(const char *root, const char *hwdb_bin_dir, bool strict, bool co
         log_debug("strings dedup'ed: %8zu bytes (%8zu)",
                   trie->strings->dedup_len, trie->strings->dedup_count);
 
-        hwdb_bin = path_join(root, hwdb_bin_dir ?: "/etc/udev", "hwdb.bin");
-        if (!hwdb_bin)
-                return -ENOMEM;
-
         (void) mkdir_parents_label(hwdb_bin, 0755);
         err = trie_store(trie, hwdb_bin, compat);
         if (err < 0)
@@ -698,3 +711,16 @@ bool hwdb_should_reload(sd_hwdb *hwdb) {
                 return true;
         return false;
 }
+
+int hwdb_bypass(void) {
+        int r;
+
+        r = getenv_bool("SYSTEMD_HWDB_UPDATE_BYPASS");
+        if (r < 0 && r != -ENXIO)
+                log_debug_errno(r, "Failed to parse $SYSTEMD_HWDB_UPDATE_BYPASS, assuming no.");
+        if (r <= 0)
+                return false;
+
+        log_debug("$SYSTEMD_HWDB_UPDATE_BYPASS is enabled, skipping execution.");
+        return true;
+}