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