daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
65878a
From 5bb09b7611b2bb5df0df432df20c84ca741cad24 Mon Sep 17 00:00:00 2001
65878a
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
65878a
Date: Sun, 22 Dec 2013 19:45:02 -0500
65878a
Subject: [PATCH] delta: fix delta for drop-ins
65878a
65878a
Also, fix highlighting, add more debug statements, make const tables
65878a
static and global, run path_kill_slashes only at entry.
65878a
65878a
Conflicts:
65878a
	src/delta/delta.c
65878a
---
65878a
 src/delta/delta.c | 174 +++++++++++++++++++++++++-----------------------------
65878a
 1 file changed, 79 insertions(+), 95 deletions(-)
65878a
65878a
diff --git a/src/delta/delta.c b/src/delta/delta.c
65878a
index c2550d6..06c7431 100644
65878a
--- a/src/delta/delta.c
65878a
+++ b/src/delta/delta.c
65878a
@@ -33,6 +33,34 @@
65878a
 #include "build.h"
65878a
 #include "strv.h"
65878a
 
65878a
+static const char prefixes[] =
65878a
+        "/etc\0"
65878a
+        "/run\0"
65878a
+        "/usr/local/lib\0"
65878a
+        "/usr/local/share\0"
65878a
+        "/usr/lib\0"
65878a
+        "/usr/share\0"
65878a
+#ifdef HAVE_SPLIT_USR
65878a
+        "/lib\0"
65878a
+#endif
65878a
+        ;
65878a
+
65878a
+static const char suffixes[] =
65878a
+        "sysctl.d\0"
65878a
+        "tmpfiles.d\0"
65878a
+        "modules-load.d\0"
65878a
+        "binfmt.d\0"
65878a
+        "systemd/system\0"
65878a
+        "systemd/user\0"
65878a
+        "systemd/system-preset\0"
65878a
+        "systemd/user-preset\0"
65878a
+        "udev/rules.d\0"
65878a
+        "modprobe.d\0";
65878a
+
65878a
+static const char have_dropins[] =
65878a
+        "systemd/system\0"
65878a
+        "systemd/user\0";
65878a
+
65878a
 static bool arg_no_pager = false;
65878a
 static int arg_diff = -1;
65878a
 
65878a
@@ -48,6 +76,14 @@ static enum {
65878a
         (SHOW_MASKED | SHOW_EQUIVALENT | SHOW_REDIRECTED | SHOW_OVERRIDDEN | SHOW_EXTENDED)
65878a
 } arg_flags = 0;
65878a
 
65878a
+static void pager_open_if_enabled(void) {
65878a
+
65878a
+        if (arg_no_pager)
65878a
+                return;
65878a
+
65878a
+        pager_open(false);
65878a
+}
65878a
+
65878a
 static int equivalent(const char *a, const char *b) {
65878a
         _cleanup_free_ char *x = NULL, *y = NULL;
65878a
 
65878a
@@ -76,7 +112,7 @@ static int notify_override_equivalent(const char *top, const char *bottom) {
65878a
                 return 0;
65878a
 
65878a
         printf("%s%s%s %s → %s\n",
65878a
-               ansi_highlight_green(), "[EQUIVALENT]", ansi_highlight(), top, bottom);
65878a
+               ansi_highlight_green(), "[EQUIVALENT]", ansi_highlight_off(), top, bottom);
65878a
         return 1;
65878a
 }
65878a
 
65878a
@@ -160,24 +196,26 @@ static int found_override(const char *top, const char *bottom) {
65878a
 }
65878a
 
65878a
 static int enumerate_dir_d(Hashmap *top, Hashmap *bottom, Hashmap *drops, const char *toppath, const char *drop) {
65878a
-        _cleanup_free_ char *conf = NULL;
65878a
+        _cleanup_free_ char *unit = NULL;
65878a
         _cleanup_free_ char *path = NULL;
65878a
         _cleanup_strv_free_ char **list = NULL;
65878a
         char **file;
65878a
         char *c;
65878a
         int r;
65878a
 
65878a
+        assert(!endswith(drop, "/"));
65878a
+
65878a
         path = strjoin(toppath, "/", drop, NULL);
65878a
         if (!path)
65878a
                 return -ENOMEM;
65878a
 
65878a
-        path_kill_slashes(path);
65878a
+        log_debug("Looking at %s", path);
65878a
 
65878a
-        conf = strdup(drop);
65878a
-        if (!conf)
65878a
+        unit = strdup(drop);
65878a
+        if (!unit)
65878a
                 return -ENOMEM;
65878a
 
65878a
-        c = strrchr(conf, '.');
65878a
+        c = strrchr(unit, '.');
65878a
         if (!c)
65878a
                 return -EINVAL;
65878a
         *c = 0;
65878a
@@ -200,35 +238,21 @@ static int enumerate_dir_d(Hashmap *top, Hashmap *bottom, Hashmap *drops, const
65878a
                 p = strjoin(path, "/", *file, NULL);
65878a
                 if (!p)
65878a
                         return -ENOMEM;
65878a
+                d = p + strlen(toppath) + 1;
65878a
 
65878a
-                path_kill_slashes(p);
65878a
-
65878a
-                d = strrchr(p, '/');
65878a
-                if (!d || d == p) {
65878a
-                        free(p);
65878a
-                        return -EINVAL;
65878a
-                }
65878a
-                d--;
65878a
-                d = strrchr(p, '/');
65878a
-
65878a
-                if (!d || d == p) {
65878a
-                        free(p);
65878a
-                        return -EINVAL;
65878a
-                }
65878a
-
65878a
+                log_debug("Adding at top: %s → %s", d, p);
65878a
                 k = hashmap_put(top, d, p);
65878a
                 if (k >= 0) {
65878a
                         p = strdup(p);
65878a
                         if (!p)
65878a
                                 return -ENOMEM;
65878a
-                        d = strrchr(p, '/');
65878a
-                        d--;
65878a
-                        d = strrchr(p, '/');
65878a
+                        d = p + strlen(toppath) + 1;
65878a
                 } else if (k != -EEXIST) {
65878a
                         free(p);
65878a
                         return k;
65878a
                 }
65878a
 
65878a
+                log_debug("Adding at bottom: %s → %s", d, p);
65878a
                 free(hashmap_remove(bottom, d));
65878a
                 k = hashmap_put(bottom, d, p);
65878a
                 if (k < 0) {
65878a
@@ -236,14 +260,14 @@ static int enumerate_dir_d(Hashmap *top, Hashmap *bottom, Hashmap *drops, const
65878a
                         return k;
65878a
                 }
65878a
 
65878a
-                h = hashmap_get(drops, conf);
65878a
+                h = hashmap_get(drops, unit);
65878a
                 if (!h) {
65878a
                         h = hashmap_new(string_hash_func, string_compare_func);
65878a
                         if (!h)
65878a
                                 return -ENOMEM;
65878a
-                        hashmap_put(drops, conf, h);
65878a
-                        conf = strdup(conf);
65878a
-                        if (!conf)
65878a
+                        hashmap_put(drops, unit, h);
65878a
+                        unit = strdup(unit);
65878a
+                        if (!unit)
65878a
                                 return -ENOMEM;
65878a
                 }
65878a
 
65878a
@@ -251,7 +275,8 @@ static int enumerate_dir_d(Hashmap *top, Hashmap *bottom, Hashmap *drops, const
65878a
                 if (!p)
65878a
                         return -ENOMEM;
65878a
 
65878a
-                k = hashmap_put(h, path_get_file_name(p), p);
65878a
+                log_debug("Adding to drops: %s → %s → %s", unit, basename(p), p);
65878a
+                k = hashmap_put(h, basename(p), p);
65878a
                 if (k < 0) {
65878a
                         free(p);
65878a
                         if (k != -EEXIST)
65878a
@@ -269,12 +294,14 @@ static int enumerate_dir(Hashmap *top, Hashmap *bottom, Hashmap *drops, const ch
65878a
         assert(drops);
65878a
         assert(path);
65878a
 
65878a
+        log_debug("Looking at %s", path);
65878a
+
65878a
         d = opendir(path);
65878a
         if (!d) {
65878a
                 if (errno == ENOENT)
65878a
                         return 0;
65878a
 
65878a
-                log_error("Failed to enumerate %s: %m", path);
65878a
+                log_error("Failed to open %s: %m", path);
65878a
                 return -errno;
65878a
         }
65878a
 
65878a
@@ -285,11 +312,8 @@ static int enumerate_dir(Hashmap *top, Hashmap *bottom, Hashmap *drops, const ch
65878a
 
65878a
                 errno = 0;
65878a
                 de = readdir(d);
65878a
-                if (!de && errno != 0)
65878a
-                        return -errno;
65878a
-
65878a
                 if (!de)
65878a
-                        break;
65878a
+                        return -errno;
65878a
 
65878a
                 dirent_ensure_type(d, de);
65878a
 
65878a
@@ -303,9 +327,8 @@ static int enumerate_dir(Hashmap *top, Hashmap *bottom, Hashmap *drops, const ch
65878a
                 if (!p)
65878a
                         return -ENOMEM;
65878a
 
65878a
-                path_kill_slashes(p);
65878a
-
65878a
-                k = hashmap_put(top, path_get_file_name(p), p);
65878a
+                log_debug("Adding at top: %s → %s", basename(p), p);
65878a
+                k = hashmap_put(top, basename(p), p);
65878a
                 if (k >= 0) {
65878a
                         p = strdup(p);
65878a
                         if (!p)
65878a
@@ -315,44 +338,37 @@ static int enumerate_dir(Hashmap *top, Hashmap *bottom, Hashmap *drops, const ch
65878a
                         return k;
65878a
                 }
65878a
 
65878a
-                free(hashmap_remove(bottom, path_get_file_name(p)));
65878a
-                k = hashmap_put(bottom, path_get_file_name(p), p);
65878a
+                log_debug("Adding at bottom: %s → %s", basename(p), p);
65878a
+                free(hashmap_remove(bottom, basename(p)));
65878a
+                k = hashmap_put(bottom, basename(p), p);
65878a
                 if (k < 0) {
65878a
                         free(p);
65878a
                         return k;
65878a
                 }
65878a
         }
65878a
-
65878a
-        return 0;
65878a
 }
65878a
 
65878a
-static int process_suffix(const char *prefixes, const char *suffix, bool dropins) {
65878a
+static int process_suffix(const char *suffix) {
65878a
         const char *p;
65878a
         char *f;
65878a
-        Hashmap *top, *bottom=NULL, *drops=NULL;
65878a
+        Hashmap *top, *bottom, *drops;
65878a
         Hashmap *h;
65878a
         char *key;
65878a
         int r = 0, k;
65878a
         Iterator i, j;
65878a
         int n_found = 0;
65878a
+        bool dropins;
65878a
 
65878a
-        assert(prefixes);
65878a
         assert(suffix);
65878a
+        assert(!startswith(suffix, "/"));
65878a
+        assert(!strstr(suffix, "//"));
65878a
 
65878a
-        top = hashmap_new(string_hash_func, string_compare_func);
65878a
-        if (!top) {
65878a
-                r = -ENOMEM;
65878a
-                goto finish;
65878a
-        }
65878a
+        dropins = nulstr_contains(have_dropins, suffix);
65878a
 
65878a
+        top = hashmap_new(string_hash_func, string_compare_func);
65878a
         bottom = hashmap_new(string_hash_func, string_compare_func);
65878a
-        if (!bottom) {
65878a
-                r = -ENOMEM;
65878a
-                goto finish;
65878a
-        }
65878a
-
65878a
         drops = hashmap_new(string_hash_func, string_compare_func);
65878a
-        if (!drops) {
65878a
+        if (!top || !bottom || !drops) {
65878a
                 r = -ENOMEM;
65878a
                 goto finish;
65878a
         }
65878a
@@ -367,10 +383,8 @@ static int process_suffix(const char *prefixes, const char *suffix, bool dropins
65878a
                 }
65878a
 
65878a
                 k = enumerate_dir(top, bottom, drops, t, dropins);
65878a
-                if (k < 0)
65878a
+                if (r == 0)
65878a
                         r = k;
65878a
-
65878a
-                log_debug("Looking at %s", t);
65878a
         }
65878a
 
65878a
         HASHMAP_FOREACH_KEY(f, key, top, i) {
65878a
@@ -411,21 +425,20 @@ finish:
65878a
         return r < 0 ? r : n_found;
65878a
 }
65878a
 
65878a
-static int process_suffix_chop(const char *prefixes, const char *suffix, const char *have_dropins) {
65878a
+static int process_suffix_chop(const char *suffix) {
65878a
         const char *p;
65878a
 
65878a
-        assert(prefixes);
65878a
         assert(suffix);
65878a
 
65878a
         if (!path_is_absolute(suffix))
65878a
-                return process_suffix(prefixes, suffix, nulstr_contains(have_dropins, suffix));
65878a
+                return process_suffix(suffix);
65878a
 
65878a
         /* Strip prefix from the suffix */
65878a
         NULSTR_FOREACH(p, prefixes) {
65878a
                 if (startswith(suffix, p)) {
65878a
                         suffix += strlen(p);
65878a
                         suffix += strspn(suffix, "/");
65878a
-                        return process_suffix(prefixes, suffix, nulstr_contains(have_dropins, suffix));
65878a
+                        return process_suffix(suffix);
65878a
                 }
65878a
         }
65878a
 
65878a
@@ -550,35 +563,6 @@ static int parse_argv(int argc, char *argv[]) {
65878a
 }
65878a
 
65878a
 int main(int argc, char *argv[]) {
65878a
-
65878a
-        const char prefixes[] =
65878a
-                "/etc\0"
65878a
-                "/run\0"
65878a
-                "/usr/local/lib\0"
65878a
-                "/usr/local/share\0"
65878a
-                "/usr/lib\0"
65878a
-                "/usr/share\0"
65878a
-#ifdef HAVE_SPLIT_USR
65878a
-                "/lib\0"
65878a
-#endif
65878a
-                ;
65878a
-
65878a
-        const char suffixes[] =
65878a
-                "sysctl.d\0"
65878a
-                "tmpfiles.d\0"
65878a
-                "modules-load.d\0"
65878a
-                "binfmt.d\0"
65878a
-                "systemd/system\0"
65878a
-                "systemd/user\0"
65878a
-                "systemd/system-preset\0"
65878a
-                "systemd/user-preset\0"
65878a
-                "udev/rules.d\0"
65878a
-                "modprobe.d\0";
65878a
-
65878a
-        const char have_dropins[] =
65878a
-                "systemd/system\0"
65878a
-                "systemd/user\0";
65878a
-
65878a
         int r = 0, k;
65878a
         int n_found = 0;
65878a
 
65878a
@@ -597,14 +581,14 @@ int main(int argc, char *argv[]) {
65878a
         else if (arg_diff)
65878a
                 arg_flags |= SHOW_OVERRIDDEN;
65878a
 
65878a
-        if (!arg_no_pager)
65878a
-                pager_open(false);
65878a
+        pager_open_if_enabled();
65878a
 
65878a
         if (optind < argc) {
65878a
                 int i;
65878a
 
65878a
                 for (i = optind; i < argc; i++) {
65878a
-                        k = process_suffix_chop(prefixes, argv[i], have_dropins);
65878a
+                        path_kill_slashes(argv[i]);
65878a
+                        k = process_suffix_chop(argv[i]);
65878a
                         if (k < 0)
65878a
                                 r = k;
65878a
                         else
65878a
@@ -615,7 +599,7 @@ int main(int argc, char *argv[]) {
65878a
                 const char *n;
65878a
 
65878a
                 NULSTR_FOREACH(n, suffixes) {
65878a
-                        k = process_suffix(prefixes, n, nulstr_contains(have_dropins, n));
65878a
+                        k = process_suffix(n);
65878a
                         if (k < 0)
65878a
                                 r = k;
65878a
                         else