803fb7
From f2d7881cf56b2d1448b9e09c46c076a14a05011d Mon Sep 17 00:00:00 2001
803fb7
From: Jan Synacek <jsynacek@redhat.com>
803fb7
Date: Tue, 11 Apr 2017 11:20:36 +0200
803fb7
Subject: [PATCH] tmpfiles: add new 'e' action which cleans up a dir without
803fb7
 creating it
803fb7
803fb7
I wanted to add a config line that would empty a directory
803fb7
without creating it if doesn't exist. Existing actions don't allow
803fb7
this.
803fb7
803fb7
v2: properly add 'e' to needs_glob() and takes_ownership()
803fb7
803fb7
(cherry picked from commit df8dee85da5fa41e95dd7f536e67fcc6940a6488)
803fb7
Resolves: #1225739
803fb7
---
de8967
 man/tmpfiles.d.xml      |  9 +++++++-
de8967
 src/tmpfiles/tmpfiles.c | 51 ++++++++++++-----------------------------
803fb7
 2 files changed, 23 insertions(+), 37 deletions(-)
803fb7
803fb7
diff --git a/man/tmpfiles.d.xml b/man/tmpfiles.d.xml
803fb7
index fc1fe13ac..fc9db622e 100644
803fb7
--- a/man/tmpfiles.d.xml
803fb7
+++ b/man/tmpfiles.d.xml
803fb7
@@ -161,6 +161,13 @@
803fb7
           <listitem><para>Create or empty a directory.</para></listitem>
803fb7
         </varlistentry>
803fb7
 
803fb7
+        <varlistentry>
803fb7
+          <term><varname>e</varname></term>
803fb7
+          <listitem><para>Clean directory contents based on the age argument.
803fb7
+          Lines of this type accept shell-style globs in
803fb7
+          place of normal path names.</para></listitem>
803fb7
+        </varlistentry>
803fb7
+
803fb7
         <varlistentry>
803fb7
           <term><varname>v</varname></term>
803fb7
           <listitem><para>Create a subvolume if the path does not
803fb7
@@ -467,7 +474,7 @@
803fb7
 
803fb7
       <para>The age field only applies to lines
803fb7
       starting with <varname>d</varname>,
803fb7
-      <varname>D</varname>, and
803fb7
+      <varname>D</varname>, <varname>e</varname> and
803fb7
       <varname>x</varname>. If omitted or set to
803fb7
       <literal>-</literal>, no automatic clean-up is
803fb7
       done.</para>
803fb7
diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c
803fb7
index bda89df5b..df7676b57 100644
803fb7
--- a/src/tmpfiles/tmpfiles.c
803fb7
+++ b/src/tmpfiles/tmpfiles.c
803fb7
@@ -79,6 +79,7 @@ typedef enum ItemType {
803fb7
 
803fb7
         /* These ones take globs */
803fb7
         WRITE_FILE = 'w',
803fb7
+        EMPTY_DIRECTORY = 'e',
803fb7
         SET_XATTR = 't',
803fb7
         RECURSIVE_SET_XATTR = 'T',
803fb7
         SET_ACL = 'a',
803fb7
@@ -150,6 +151,7 @@ static bool needs_glob(ItemType t) {
803fb7
                       IGNORE_PATH,
803fb7
                       IGNORE_DIRECTORY_PATH,
803fb7
                       REMOVE_PATH,
803fb7
+                      EMPTY_DIRECTORY,
803fb7
                       RECURSIVE_REMOVE_PATH,
803fb7
                       ADJUST_MODE,
803fb7
                       RELABEL_PATH,
803fb7
@@ -165,6 +167,7 @@ static bool takes_ownership(ItemType t) {
803fb7
                       CREATE_FILE,
803fb7
                       TRUNCATE_FILE,
803fb7
                       CREATE_DIRECTORY,
803fb7
+                      EMPTY_DIRECTORY,
803fb7
                       TRUNCATE_DIRECTORY,
803fb7
                       CREATE_SUBVOLUME,
803fb7
                       CREATE_FIFO,
803fb7
@@ -1059,6 +1062,9 @@ static int create_item(Item *i) {
803fb7
 
803fb7
                 log_debug("%s directory \"%s\".", creation_mode_verb_to_string(creation), i->path);
803fb7
 
803fb7
+                /* fall through */
803fb7
+
803fb7
+        case EMPTY_DIRECTORY:
803fb7
                 r = path_set_perms(i, i->path);
803fb7
                 if (r < 0)
803fb7
                         return r;
803fb7
@@ -1285,43 +1291,19 @@ static int remove_item_instance(Item *i, const char *instance) {
803fb7
 }
803fb7
 
803fb7
 static int remove_item(Item *i) {
803fb7
-        int r = 0;
803fb7
-
803fb7
         assert(i);
803fb7
 
803fb7
         log_debug("Running remove action for entry %c %s", (char) i->type, i->path);
803fb7
 
803fb7
         switch (i->type) {
803fb7
-
803fb7
-        case CREATE_FILE:
803fb7
-        case TRUNCATE_FILE:
803fb7
-        case CREATE_DIRECTORY:
803fb7
-        case CREATE_SUBVOLUME:
803fb7
-        case CREATE_FIFO:
803fb7
-        case CREATE_SYMLINK:
803fb7
-        case CREATE_CHAR_DEVICE:
803fb7
-        case CREATE_BLOCK_DEVICE:
803fb7
-        case IGNORE_PATH:
803fb7
-        case IGNORE_DIRECTORY_PATH:
803fb7
-        case ADJUST_MODE:
803fb7
-        case RELABEL_PATH:
803fb7
-        case RECURSIVE_RELABEL_PATH:
803fb7
-        case WRITE_FILE:
803fb7
-        case COPY_FILES:
803fb7
-        case SET_XATTR:
803fb7
-        case RECURSIVE_SET_XATTR:
803fb7
-        case SET_ACL:
803fb7
-        case RECURSIVE_SET_ACL:
803fb7
-                break;
803fb7
-
803fb7
         case REMOVE_PATH:
803fb7
         case TRUNCATE_DIRECTORY:
803fb7
         case RECURSIVE_REMOVE_PATH:
803fb7
-                r = glob_item(i, remove_item_instance, false);
803fb7
-                break;
803fb7
-        }
803fb7
+                return glob_item(i, remove_item_instance, false);
803fb7
 
803fb7
-        return r;
803fb7
+        default:
803fb7
+                return 0;
803fb7
+        }
803fb7
 }
803fb7
 
803fb7
 static int clean_item_instance(Item *i, const char* instance) {
803fb7
@@ -1377,8 +1359,6 @@ static int clean_item_instance(Item *i, const char* instance) {
803fb7
 }
803fb7
 
803fb7
 static int clean_item(Item *i) {
803fb7
-        int r = 0;
803fb7
-
803fb7
         assert(i);
803fb7
 
803fb7
         log_debug("Running clean action for entry %c %s", (char) i->type, i->path);
803fb7
@@ -1386,19 +1366,17 @@ static int clean_item(Item *i) {
803fb7
         switch (i->type) {
803fb7
         case CREATE_DIRECTORY:
803fb7
         case CREATE_SUBVOLUME:
803fb7
+        case EMPTY_DIRECTORY:
803fb7
         case TRUNCATE_DIRECTORY:
803fb7
         case IGNORE_PATH:
803fb7
         case COPY_FILES:
803fb7
                 clean_item_instance(i, i->path);
803fb7
-                break;
803fb7
+                return 0;
803fb7
         case IGNORE_DIRECTORY_PATH:
803fb7
-                r = glob_item(i, clean_item_instance, false);
803fb7
-                break;
803fb7
+                return glob_item(i, clean_item_instance, false);
803fb7
         default:
803fb7
-                break;
803fb7
+                return 0;
803fb7
         }
803fb7
-
803fb7
-        return r;
803fb7
 }
803fb7
 
803fb7
 static int process_item_array(ItemArray *array);
803fb7
@@ -1642,6 +1620,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
803fb7
         case TRUNCATE_FILE:
803fb7
         case CREATE_DIRECTORY:
803fb7
         case CREATE_SUBVOLUME:
803fb7
+        case EMPTY_DIRECTORY:
803fb7
         case TRUNCATE_DIRECTORY:
803fb7
         case CREATE_FIFO:
803fb7
         case IGNORE_PATH: