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