mirror of
https://github.com/troglobit/finit.git
synced 2026-11-23 20:11:44 +00:00
tmpfiles: keep what x and X entries name from being cleaned
The x and X entries were read and ignored, so a path that a package
had marked to be left alone was cleaned like the rest:
d /var/tmp - - - 30d
x /var/tmp/keep
tmpfiles --clean t.conf
This removed what was old in /var/tmp/keep.
Gather the x and X entries of all files in a pass before the cleaning,
they may come after the entry they apply to, or in another file. x
keeps the path and what is in it, X the path alone. A directory that
is not empty afterwards is no longer warned about.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
3 files changed
+77
-7
No files matched your search
@@ -15,6 +15,8 @@ All relevant changes are documented in this file.
|
|||||||
skipped unless the new `--boot` option is given. Finit gives it at
|
skipped unless the new `--boot` option is given. Finit gives it at
|
||||||
boot, and now also `--remove`, so `r`, `R`, and `D` entries take effect
|
boot, and now also `--remove`, so `r`, `R`, and `D` entries take effect
|
||||||
at boot and no longer need a separate call
|
at boot and no longer need a separate call
|
||||||
|
- tmpfiles: the `x` and `X` entries now keep paths from being cleaned by
|
||||||
|
`tmpfiles --clean`, before they were read and ignored
|
||||||
- tmpfiles: add the `a`, `a+`, `A`, and `A+` entries for POSIX ACLs, used
|
- tmpfiles: add the `a`, `a+`, `A`, and `A+` entries for POSIX ACLs, used
|
||||||
by for example the tpm2-tss package. Finit sets them itself, no need
|
by for example the tpm2-tss package. Finit sets them itself, no need
|
||||||
for libacl or `setfacl`
|
for libacl or `setfacl`
|
||||||
|
|||||||
+62
-7
@@ -26,6 +26,7 @@
|
|||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <endian.h>
|
#include <endian.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <fnmatch.h>
|
||||||
#include <ftw.h>
|
#include <ftw.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include <glob.h>
|
#include <glob.h>
|
||||||
@@ -351,6 +352,49 @@ static int parse_gid(const char *group)
|
|||||||
return val < 0 ? 0 : val;
|
return val < 0 ? 0 : val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* What the x and X entries keep from being cleaned */
|
||||||
|
struct keep {
|
||||||
|
char *pattern;
|
||||||
|
int self; /* X, the path but not what is in it */
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct keep *keeps;
|
||||||
|
static int num_keeps;
|
||||||
|
static int collect; /* Pass that only gathers the above */
|
||||||
|
|
||||||
|
static void keep_add(const char *pattern, int self)
|
||||||
|
{
|
||||||
|
struct keep *k;
|
||||||
|
|
||||||
|
k = realloc(keeps, (num_keeps + 1) * sizeof(*k));
|
||||||
|
if (!k)
|
||||||
|
return;
|
||||||
|
|
||||||
|
keeps = k;
|
||||||
|
k = &keeps[num_keeps];
|
||||||
|
k->pattern = strdup(pattern);
|
||||||
|
k->self = self;
|
||||||
|
if (!k->pattern)
|
||||||
|
return;
|
||||||
|
|
||||||
|
/* The paths from nftw() have no slash at the end */
|
||||||
|
for (size_t len = strlen(k->pattern); len > 1 && k->pattern[len - 1] == '/'; len--)
|
||||||
|
k->pattern[len - 1] = 0;
|
||||||
|
num_keeps++;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int kept(const char *path)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < num_keeps; i++) {
|
||||||
|
struct keep *k = &keeps[i];
|
||||||
|
|
||||||
|
if (!fnmatch(k->pattern, path, FNM_PATHNAME | (k->self ? 0 : FNM_LEADING_DIR)))
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/* Globals for do_clean() callback - nftw doesn't support user data */
|
/* Globals for do_clean() callback - nftw doesn't support user data */
|
||||||
static time_t clean_age;
|
static time_t clean_age;
|
||||||
static time_t clean_now;
|
static time_t clean_now;
|
||||||
@@ -380,7 +424,10 @@ static int do_clean(const char *fpath, const struct stat *sb, int tflag, struct
|
|||||||
if (!is_dir && clean_now - sb->st_ctime < clean_age)
|
if (!is_dir && clean_now - sb->st_ctime < clean_age)
|
||||||
return 0; /* ctime is too new, keep it (files only) */
|
return 0; /* ctime is too new, keep it (files only) */
|
||||||
|
|
||||||
if (remove(fpath) && errno != ENOENT && errno != EBUSY)
|
if (kept(fpath))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (remove(fpath) && errno != ENOENT && errno != EBUSY && errno != ENOTEMPTY)
|
||||||
warn("Failed cleaning %s", fpath);
|
warn("Failed cleaning %s", fpath);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -747,6 +794,11 @@ static void tmpfiles(char *line)
|
|||||||
}
|
}
|
||||||
if (excluded(path))
|
if (excluded(path))
|
||||||
return;
|
return;
|
||||||
|
if (collect) {
|
||||||
|
if (type[0] == 'x' || type[0] == 'X')
|
||||||
|
keep_add(path, type[0] == 'X');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
token = strtok(NULL, "\t ");
|
token = strtok(NULL, "\t ");
|
||||||
if (token) {
|
if (token) {
|
||||||
@@ -805,7 +857,7 @@ static void tmpfiles(char *line)
|
|||||||
break;
|
break;
|
||||||
case 'X':
|
case 'X':
|
||||||
case 'x':
|
case 'x':
|
||||||
/* handled by clean_flag (--clean) */
|
/* see kept(), used with --clean */
|
||||||
break;
|
break;
|
||||||
case 'Z':
|
case 'Z':
|
||||||
case 'z':
|
case 'z':
|
||||||
@@ -833,10 +885,6 @@ static void tmpfiles(char *line)
|
|||||||
clean_now = time(NULL);
|
clean_now = time(NULL);
|
||||||
nftw(path, do_clean, 20, FTW_DEPTH | FTW_PHYS | FTW_MOUNT);
|
nftw(path, do_clean, 20, FTW_DEPTH | FTW_PHYS | FTW_MOUNT);
|
||||||
break;
|
break;
|
||||||
case 'x':
|
|
||||||
case 'X':
|
|
||||||
/* exclusion patterns - not yet implemented */
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -1037,7 +1085,7 @@ static void tmpfiles(char *line)
|
|||||||
break;
|
break;
|
||||||
case 'X':
|
case 'X':
|
||||||
case 'x':
|
case 'x':
|
||||||
/* handled by clean_flag (--clean) */
|
/* see kept(), used with --clean */
|
||||||
break;
|
break;
|
||||||
case 'Z':
|
case 'Z':
|
||||||
opts = "-R";
|
opts = "-R";
|
||||||
@@ -1235,6 +1283,13 @@ int main(int argc, char *argv[])
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Cleaning needs every x and X entry, also those in files not yet read */
|
||||||
|
if (clean_flag) {
|
||||||
|
collect = quiet = 1;
|
||||||
|
process(argc - optind, &argv[optind]);
|
||||||
|
collect = quiet = 0;
|
||||||
|
}
|
||||||
|
|
||||||
/* Remove and clean first, or an r entry takes what an f entry above it made */
|
/* Remove and clean first, or an r entry takes what an f entry above it made */
|
||||||
if (create_flag && (clean_flag || remove_flag)) {
|
if (create_flag && (clean_flag || remove_flag)) {
|
||||||
create_flag = 0;
|
create_flag = 0;
|
||||||
|
|||||||
@@ -40,3 +40,16 @@ CONF
|
|||||||
assert_left $DIR/young "file "
|
assert_left $DIR/young "file "
|
||||||
assert_left $DIR/old "mnt "
|
assert_left $DIR/old "mnt "
|
||||||
assert_left $DIR/old/mnt "file "
|
assert_left $DIR/old/mnt "file "
|
||||||
|
|
||||||
|
sep
|
||||||
|
say 'x keeps a path and what is in it, X only the path itself'
|
||||||
|
run "mkdir -p $DIR/keep/all $DIR/keep/dir; touch $DIR/keep/file $DIR/keep/all/file $DIR/keep/dir/file"
|
||||||
|
sleep 2
|
||||||
|
clean <<CONF
|
||||||
|
d $DIR/keep - - - 1s
|
||||||
|
x $DIR/keep/a*
|
||||||
|
X $DIR/keep/dir/
|
||||||
|
CONF
|
||||||
|
assert_left $DIR/keep "all dir "
|
||||||
|
assert_left $DIR/keep/all "file "
|
||||||
|
assert_left $DIR/keep/dir ""
|
||||||
Reference in new issue
Block a user