a52f674
From 8193cf614082dc3ed42895bbae963cf5d134e191 Mon Sep 17 00:00:00 2001
b12ff75
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
b12ff75
Date: Sun, 22 Dec 2013 22:53:23 -0500
b12ff75
Subject: [PATCH] delta: if prefix is specified, only show overrides there
b12ff75
b12ff75
systemd-delta /run/systemd/system will show all unit overrides
b12ff75
in /run, etc.
a52f674
a52f674
(cherry picked from commit 6096dfd6167f8520be3a359c1fd0e6f2e76a4dc1)
b12ff75
---
b12ff75
 man/systemd-delta.xml | 80 +++++++++++++++++++++++++++++++++++++++++----------
b12ff75
 src/delta/delta.c     | 71 ++++++++++++++++++++++++++++-----------------
b12ff75
 2 files changed, 109 insertions(+), 42 deletions(-)
b12ff75
b12ff75
diff --git a/man/systemd-delta.xml b/man/systemd-delta.xml
b12ff75
index 413ebd8..4d3ab78 100644
b12ff75
--- a/man/systemd-delta.xml
b12ff75
+++ b/man/systemd-delta.xml
b12ff75
@@ -49,7 +49,9 @@
b12ff75
 
b12ff75
         <refsynopsisdiv>
b12ff75
                 <cmdsynopsis>
b12ff75
-                        <command>systemd-delta <arg choice="opt" rep="repeat">OPTIONS</arg> <arg choice="opt" rep="repeat">SUFFIX</arg></command>
b12ff75
+                        <command>systemd-delta</command>
b12ff75
+			<arg choice="opt" rep="repeat">OPTIONS</arg>
b12ff75
+			<arg choice="opt" rep="repeat"><replaceable>PREFIX</replaceable><optional>/<replaceable>SUFFIX</replaceable></optional>|<replaceable>SUFFIX</replaceable></arg>
b12ff75
                 </cmdsynopsis>
b12ff75
         </refsynopsisdiv>
b12ff75
 
b12ff75
@@ -57,18 +59,46 @@
b12ff75
                 <title>Description</title>
b12ff75
 
b12ff75
                 <para><command>systemd-delta</command> may be used to
b12ff75
-                identify and compare configuration files in
b12ff75
-                <filename>/etc</filename> that override default
b12ff75
-                counterparts in <filename>/usr</filename>. The command
b12ff75
-                line argument can be one or more name of a subdirectories of
b12ff75
-                <filename>/etc</filename> or
b12ff75
-                <filename>/usr/lib</filename> to compare, such as
b12ff75
-                <filename>tmpfiles.d</filename>, <filename>sysctl.d</filename> or
b12ff75
-                <filename>systemd/system</filename>.</para>
b12ff75
-
b12ff75
-                <para>When no argument is specified a number of
b12ff75
-                well-known subdirectories are searched for overridden
b12ff75
-                files.</para>
b12ff75
+                identify and compare configuration files that override
b12ff75
+                other configuration files. Files in
b12ff75
+                <filename>/etc</filename> have highest priority, files
b12ff75
+                in <filename>/run</filename> have the second highest
b12ff75
+                priority, ..., files in <filename>/lib</filename> have
b12ff75
+                lowest priority. Files in a directory with higher
b12ff75
+                priority override files with the same name in
b12ff75
+                directories of lower priority. In addition, certain
b12ff75
+                configuration files can have <literal>.d</literal>
b12ff75
+                directories which contain "drop-in" files with
b12ff75
+                configuration snippets which augment the main
b12ff75
+                configuration file. "Drop-in" files can be overriden
b12ff75
+                in the same way by placing files with the same name in
b12ff75
+                a directory of higher priority (except that in case of
b12ff75
+                "drop-in" files, both the "drop-in" file name and the
b12ff75
+                name of the containing directory, which corresponds to
b12ff75
+                the name of the main configuration file, must match).
b12ff75
+                For a fuller explanation, see
b12ff75
+                <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
b12ff75
+		</para>
b12ff75
+
b12ff75
+		<para>The command line argument will be split into a
b12ff75
+		prefix and a suffix. Either is optional. The prefix
b12ff75
+		must be one of the directories containing
b12ff75
+		configuration files (<filename>/etc</filename>,
b12ff75
+		<filename>/run</filename>,
b12ff75
+		<filename>/usr/lib</filename>, ...). If it is given,
b12ff75
+		only overriding files contained in this directory will
b12ff75
+		be shown. Otherwise, all overriding files will be
b12ff75
+		shown. The suffix must be a name of a subdirectory
b12ff75
+		containing configuration files like
b12ff75
+		<filename>tmpfiles.d</filename>,
b12ff75
+		<filename>sysctl.d</filename> or
b12ff75
+		<filename>systemd/system</filename>. If it is given,
b12ff75
+		only configuration files in this subdirectory (across
b12ff75
+		all configuration paths) will be analyzed. Otherwise,
b12ff75
+		all configuration files will be analyzed. If the
b12ff75
+		commandline argument is not given at all, all
b12ff75
+		configuration files will be analyzed. See below for
b12ff75
+		some examples.</para>
b12ff75
         </refsect1>
b12ff75
 
b12ff75
         <refsect1>
b12ff75
@@ -168,9 +198,28 @@
b12ff75
                         </varlistentry>
b12ff75
 
b12ff75
                 </variablelist>
b12ff75
-
b12ff75
         </refsect1>
b12ff75
 
b12ff75
+	<refsect1>
b12ff75
+		<title>Examples</title>
b12ff75
+
b12ff75
+		<para>To see all local configuration:</para>
b12ff75
+		<programlisting>systemd-delta
b12ff75
+		</programlisting>
b12ff75
+
b12ff75
+		<para>To see all runtime configuration:</para>
b12ff75
+		<programlisting>systemd-delta /run
b12ff75
+		</programlisting>
b12ff75
+
b12ff75
+		<para>To see all system unit configuration changes:</para>
b12ff75
+		<programlisting>systemd-delta systemd/system
b12ff75
+		</programlisting>
b12ff75
+
b12ff75
+		<para>To see all runtime "drop-in" changes for system units:</para>
b12ff75
+		<programlisting>systemd-delta --type=extended /run/systemd/system
b12ff75
+		</programlisting>
b12ff75
+	</refsect1>
b12ff75
+
b12ff75
         <refsect1>
b12ff75
                 <title>Exit status</title>
b12ff75
 
b12ff75
@@ -181,7 +230,8 @@
b12ff75
         <refsect1>
b12ff75
                 <title>See Also</title>
b12ff75
                 <para>
b12ff75
-                        <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>
b12ff75
+                        <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
b12ff75
+                        <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>
b12ff75
                 </para>
b12ff75
         </refsect1>
b12ff75
 
b12ff75
diff --git a/src/delta/delta.c b/src/delta/delta.c
b12ff75
index e97f2e9..ae658f9 100644
b12ff75
--- a/src/delta/delta.c
b12ff75
+++ b/src/delta/delta.c
b12ff75
@@ -4,6 +4,7 @@
b12ff75
   This file is part of systemd.
b12ff75
 
b12ff75
   Copyright 2012 Lennart Poettering
b12ff75
+  Copyright 2013 Zbigniew Jędrzejewski-Szmek
b12ff75
 
b12ff75
   systemd is free software; you can redistribute it and/or modify it
b12ff75
   under the terms of the GNU Lesser General Public License as published by
b12ff75
@@ -346,7 +347,7 @@ static int enumerate_dir(Hashmap *top, Hashmap *bottom, Hashmap *drops, const ch
b12ff75
         }
b12ff75
 }
b12ff75
 
b12ff75
-static int process_suffix(const char *suffix) {
b12ff75
+static int process_suffix(const char *suffix, const char *onlyprefix) {
b12ff75
         const char *p;
b12ff75
         char *f;
b12ff75
         Hashmap *top, *bottom, *drops;
b12ff75
@@ -391,20 +392,23 @@ static int process_suffix(const char *suffix) {
b12ff75
                 o = hashmap_get(bottom, key);
b12ff75
                 assert(o);
b12ff75
 
b12ff75
-                if (path_equal(o, f))
b12ff75
-                        notify_override_unchanged(f);
b12ff75
-                else {
b12ff75
-                        k = found_override(f, o);
b12ff75
-                        if (k < 0)
b12ff75
-                                r = k;
b12ff75
-                        else
b12ff75
-                                n_found += k;
b12ff75
+                if (!onlyprefix || startswith(o, onlyprefix)) {
b12ff75
+                        if (path_equal(o, f)) {
b12ff75
+                                notify_override_unchanged(f);
b12ff75
+                        } else {
b12ff75
+                                k = found_override(f, o);
b12ff75
+                                if (k < 0)
b12ff75
+                                        r = k;
b12ff75
+                                else
b12ff75
+                                        n_found += k;
b12ff75
+                        }
b12ff75
                 }
b12ff75
 
b12ff75
                 h = hashmap_get(drops, key);
b12ff75
                 if (h)
b12ff75
                         HASHMAP_FOREACH(o, h, j)
b12ff75
-                                n_found += notify_override_extended(f, o);
b12ff75
+                                if (!onlyprefix || startswith(o, onlyprefix))
b12ff75
+                                        n_found += notify_override_extended(f, o);
b12ff75
         }
b12ff75
 
b12ff75
 finish:
b12ff75
@@ -423,24 +427,41 @@ finish:
b12ff75
         return r < 0 ? r : n_found;
b12ff75
 }
b12ff75
 
b12ff75
-static int process_suffix_chop(const char *suffix) {
b12ff75
+static int process_suffixes(const char *onlyprefix) {
b12ff75
+        const char *n;
b12ff75
+        int n_found = 0, r;
b12ff75
+
b12ff75
+        NULSTR_FOREACH(n, suffixes) {
b12ff75
+                r = process_suffix(n, onlyprefix);
b12ff75
+                if (r < 0)
b12ff75
+                        return r;
b12ff75
+                else
b12ff75
+                        n_found += r;
b12ff75
+        }
b12ff75
+        return n_found;
b12ff75
+}
b12ff75
+
b12ff75
+static int process_suffix_chop(const char *arg) {
b12ff75
         const char *p;
b12ff75
 
b12ff75
-        assert(suffix);
b12ff75
+        assert(arg);
b12ff75
 
b12ff75
-        if (!path_is_absolute(suffix))
b12ff75
-                return process_suffix(suffix);
b12ff75
+        if (!path_is_absolute(arg))
b12ff75
+                return process_suffix(arg, NULL);
b12ff75
 
b12ff75
         /* Strip prefix from the suffix */
b12ff75
         NULSTR_FOREACH(p, prefixes) {
b12ff75
-                if (startswith(suffix, p)) {
b12ff75
-                        suffix += strlen(p);
b12ff75
+                const char *suffix = startswith(arg, p);
b12ff75
+                if (suffix) {
b12ff75
                         suffix += strspn(suffix, "/");
b12ff75
-                        return process_suffix(suffix);
b12ff75
+                        if (*suffix)
b12ff75
+                                return process_suffix(suffix, NULL);
b12ff75
+                        else
b12ff75
+                                return process_suffixes(arg);
b12ff75
                 }
b12ff75
         }
b12ff75
 
b12ff75
-        log_error("Invalid suffix specification %s.", suffix);
b12ff75
+        log_error("Invalid suffix specification %s.", arg);
b12ff75
         return -EINVAL;
b12ff75
 }
b12ff75
 
b12ff75
@@ -594,15 +615,11 @@ int main(int argc, char *argv[]) {
b12ff75
                 }
b12ff75
 
b12ff75
         } else {
b12ff75
-                const char *n;
b12ff75
-
b12ff75
-                NULSTR_FOREACH(n, suffixes) {
b12ff75
-                        k = process_suffix(n);
b12ff75
-                        if (k < 0)
b12ff75
-                                r = k;
b12ff75
-                        else
b12ff75
-                                n_found += k;
b12ff75
-                }
b12ff75
+                k = process_suffixes(NULL);
b12ff75
+                if (k < 0)
b12ff75
+                        r = k;
b12ff75
+                else
b12ff75
+                        n_found += k;
b12ff75
         }
b12ff75
 
b12ff75
         if (r >= 0)