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