daandemeyer / rpms / systemd

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