Harald Hoyer fe20ad
From 84601ab5c6e6c17bd489423b693b85377b9b1be9 Mon Sep 17 00:00:00 2001
Harald Hoyer fe20ad
From: Dave Reisner <dreisner@archlinux.org>
Harald Hoyer fe20ad
Date: Wed, 24 Jul 2013 11:19:24 -0400
Harald Hoyer fe20ad
Subject: [PATCH] tmpfiles: introduce --exclude-prefix
Harald Hoyer fe20ad
Harald Hoyer fe20ad
The opposite of --prefix, allows specifying path prefixes which should
Harald Hoyer fe20ad
be skipped when processing rules.
Harald Hoyer fe20ad
---
Harald Hoyer fe20ad
 man/systemd-tmpfiles.xml                    |  7 +++++
Harald Hoyer fe20ad
 shell-completion/systemd-zsh-completion.zsh |  1 +
Harald Hoyer fe20ad
 src/tmpfiles/tmpfiles.c                     | 44 ++++++++++++++++++-----------
Harald Hoyer fe20ad
 3 files changed, 36 insertions(+), 16 deletions(-)
Harald Hoyer fe20ad
Harald Hoyer fe20ad
diff --git a/man/systemd-tmpfiles.xml b/man/systemd-tmpfiles.xml
Harald Hoyer fe20ad
index b0f2d9c..403592d 100644
Harald Hoyer fe20ad
--- a/man/systemd-tmpfiles.xml
Harald Hoyer fe20ad
+++ b/man/systemd-tmpfiles.xml
Harald Hoyer fe20ad
@@ -124,6 +124,13 @@
Harald Hoyer fe20ad
                                 prefix. This option can be specified
Harald Hoyer fe20ad
                                 multiple times.</para></listitem>
Harald Hoyer fe20ad
                         </varlistentry>
Harald Hoyer fe20ad
+                        <varlistentry>
Harald Hoyer fe20ad
+                                <term><option>--exclude-prefix=PATH</option></term>
Harald Hoyer fe20ad
+                                <listitem><para>Ignore rules that
Harald Hoyer fe20ad
+                                apply to paths with the specified
Harald Hoyer fe20ad
+                                prefix. This option can be specified
Harald Hoyer fe20ad
+                                multiple times.</para></listitem>
Harald Hoyer fe20ad
+                        </varlistentry>
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
                         <varlistentry>
Harald Hoyer fe20ad
diff --git a/shell-completion/systemd-zsh-completion.zsh b/shell-completion/systemd-zsh-completion.zsh
Harald Hoyer fe20ad
index c85e00e..7aebbcd 100644
Harald Hoyer fe20ad
--- a/shell-completion/systemd-zsh-completion.zsh
Harald Hoyer fe20ad
+++ b/shell-completion/systemd-zsh-completion.zsh
Harald Hoyer fe20ad
@@ -249,6 +249,7 @@ _ctls()
Harald Hoyer fe20ad
                 '--clean[Clean up all files and directories with an age parameter configured.]' \
Harald Hoyer fe20ad
                 '--remove[All files and directories marked with r, R in the configuration files are removed.]' \
Harald Hoyer fe20ad
                 '--prefix=[Only apply rules that apply to paths with the specified prefix.]' \
Harald Hoyer fe20ad
+                '--exclude-prefix=[Ignore rules that apply to paths with the specified prefix.]' \
Harald Hoyer fe20ad
                 '--help[Prints a short help text and exits.]' \
Harald Hoyer fe20ad
                 '*::files:_files'
Harald Hoyer fe20ad
         ;;
Harald Hoyer fe20ad
diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c
Harald Hoyer fe20ad
index cb15133..5eca82a 100644
Harald Hoyer fe20ad
--- a/src/tmpfiles/tmpfiles.c
Harald Hoyer fe20ad
+++ b/src/tmpfiles/tmpfiles.c
Harald Hoyer fe20ad
@@ -106,6 +106,7 @@ static bool arg_clean = false;
Harald Hoyer fe20ad
 static bool arg_remove = false;
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
 static char **include_prefixes = NULL;
Harald Hoyer fe20ad
+static char **exclude_prefixes = NULL;
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
 static const char conf_file_dirs[] =
Harald Hoyer fe20ad
         "/etc/tmpfiles.d\0"
Harald Hoyer fe20ad
@@ -1021,16 +1022,19 @@ static bool item_equal(Item *a, Item *b) {
Harald Hoyer fe20ad
 static bool should_include_path(const char *path) {
Harald Hoyer fe20ad
         char **prefix;
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
-        /* no explicit paths specified for inclusion, so everything is valid */
Harald Hoyer fe20ad
-        if (strv_length(include_prefixes) == 0)
Harald Hoyer fe20ad
-                return true;
Harald Hoyer fe20ad
+        STRV_FOREACH(prefix, exclude_prefixes) {
Harald Hoyer fe20ad
+                if (path_startswith(path, *prefix))
Harald Hoyer fe20ad
+                        return false;
Harald Hoyer fe20ad
+        }
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
         STRV_FOREACH(prefix, include_prefixes) {
Harald Hoyer fe20ad
                 if (path_startswith(path, *prefix))
Harald Hoyer fe20ad
                         return true;
Harald Hoyer fe20ad
         }
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
-        return false;
Harald Hoyer fe20ad
+        /* no matches, so we should include this path only if we
Harald Hoyer fe20ad
+         * have no whitelist at all */
Harald Hoyer fe20ad
+        return strv_length(include_prefixes) == 0;
Harald Hoyer fe20ad
 }
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
 static int parse_line(const char *fname, unsigned line, const char *buffer) {
Harald Hoyer fe20ad
@@ -1219,11 +1223,12 @@ static int help(void) {
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
         printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
Harald Hoyer fe20ad
                "Creates, deletes and cleans up volatile and temporary files and directories.\n\n"
Harald Hoyer fe20ad
-               "  -h --help             Show this help\n"
Harald Hoyer fe20ad
-               "     --create           Create marked files/directories\n"
Harald Hoyer fe20ad
-               "     --clean            Clean up marked directories\n"
Harald Hoyer fe20ad
-               "     --remove           Remove marked files/directories\n"
Harald Hoyer fe20ad
-               "     --prefix=PATH      Only apply rules that apply to paths with the specified prefix\n",
Harald Hoyer fe20ad
+               "  -h --help                 Show this help\n"
Harald Hoyer fe20ad
+               "     --create               Create marked files/directories\n"
Harald Hoyer fe20ad
+               "     --clean                Clean up marked directories\n"
Harald Hoyer fe20ad
+               "     --remove               Remove marked files/directories\n"
Harald Hoyer fe20ad
+               "     --prefix=PATH          Only apply rules that apply to paths with the specified prefix\n"
Harald Hoyer fe20ad
+               "     --exclude-prefix=PATH  Ignore rules that apply to paths with the specified prefix\n",
Harald Hoyer fe20ad
                program_invocation_short_name);
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
         return 0;
Harald Hoyer fe20ad
@@ -1235,16 +1240,18 @@ static int parse_argv(int argc, char *argv[]) {
Harald Hoyer fe20ad
                 ARG_CREATE,
Harald Hoyer fe20ad
                 ARG_CLEAN,
Harald Hoyer fe20ad
                 ARG_REMOVE,
Harald Hoyer fe20ad
-                ARG_PREFIX
Harald Hoyer fe20ad
+                ARG_PREFIX,
Harald Hoyer fe20ad
+                ARG_EXCLUDE_PREFIX,
Harald Hoyer fe20ad
         };
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
         static const struct option options[] = {
Harald Hoyer fe20ad
-                { "help",      no_argument,       NULL, 'h'           },
Harald Hoyer fe20ad
-                { "create",    no_argument,       NULL, ARG_CREATE    },
Harald Hoyer fe20ad
-                { "clean",     no_argument,       NULL, ARG_CLEAN     },
Harald Hoyer fe20ad
-                { "remove",    no_argument,       NULL, ARG_REMOVE    },
Harald Hoyer fe20ad
-                { "prefix",    required_argument, NULL, ARG_PREFIX    },
Harald Hoyer fe20ad
-                { NULL,        0,                 NULL, 0             }
Harald Hoyer fe20ad
+                { "help",           no_argument,         NULL, 'h'                },
Harald Hoyer fe20ad
+                { "create",         no_argument,         NULL, ARG_CREATE         },
Harald Hoyer fe20ad
+                { "clean",          no_argument,         NULL, ARG_CLEAN          },
Harald Hoyer fe20ad
+                { "remove",         no_argument,         NULL, ARG_REMOVE         },
Harald Hoyer fe20ad
+                { "prefix",         required_argument,   NULL, ARG_PREFIX         },
Harald Hoyer fe20ad
+                { "exclude-prefix", required_argument,   NULL, ARG_EXCLUDE_PREFIX },
Harald Hoyer fe20ad
+                { NULL,             0,                   NULL, 0                  }
Harald Hoyer fe20ad
         };
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
         int c;
Harald Hoyer fe20ad
@@ -1277,6 +1284,11 @@ static int parse_argv(int argc, char *argv[]) {
Harald Hoyer fe20ad
                                 return log_oom();
Harald Hoyer fe20ad
                         break;
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
+                case ARG_EXCLUDE_PREFIX:
Harald Hoyer fe20ad
+                        if (strv_extend(&exclude_prefixes, optarg) < 0)
Harald Hoyer fe20ad
+                                return log_oom();
Harald Hoyer fe20ad
+                        break;
Harald Hoyer fe20ad
+
Harald Hoyer fe20ad
                 case '?':
Harald Hoyer fe20ad
                         return -EINVAL;
Harald Hoyer fe20ad