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
+62
-7
@@ -26,6 +26,7 @@
|
||||
#include <dirent.h>
|
||||
#include <endian.h>
|
||||
#include <fcntl.h>
|
||||
#include <fnmatch.h>
|
||||
#include <ftw.h>
|
||||
#include <getopt.h>
|
||||
#include <glob.h>
|
||||
@@ -351,6 +352,49 @@ static int parse_gid(const char *group)
|
||||
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 */
|
||||
static time_t clean_age;
|
||||
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)
|
||||
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);
|
||||
|
||||
return 0;
|
||||
@@ -747,6 +794,11 @@ static void tmpfiles(char *line)
|
||||
}
|
||||
if (excluded(path))
|
||||
return;
|
||||
if (collect) {
|
||||
if (type[0] == 'x' || type[0] == 'X')
|
||||
keep_add(path, type[0] == 'X');
|
||||
return;
|
||||
}
|
||||
|
||||
token = strtok(NULL, "\t ");
|
||||
if (token) {
|
||||
@@ -805,7 +857,7 @@ static void tmpfiles(char *line)
|
||||
break;
|
||||
case 'X':
|
||||
case 'x':
|
||||
/* handled by clean_flag (--clean) */
|
||||
/* see kept(), used with --clean */
|
||||
break;
|
||||
case 'Z':
|
||||
case 'z':
|
||||
@@ -833,10 +885,6 @@ static void tmpfiles(char *line)
|
||||
clean_now = time(NULL);
|
||||
nftw(path, do_clean, 20, FTW_DEPTH | FTW_PHYS | FTW_MOUNT);
|
||||
break;
|
||||
case 'x':
|
||||
case 'X':
|
||||
/* exclusion patterns - not yet implemented */
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -1037,7 +1085,7 @@ static void tmpfiles(char *line)
|
||||
break;
|
||||
case 'X':
|
||||
case 'x':
|
||||
/* handled by clean_flag (--clean) */
|
||||
/* see kept(), used with --clean */
|
||||
break;
|
||||
case 'Z':
|
||||
opts = "-R";
|
||||
@@ -1235,6 +1283,13 @@ int main(int argc, char *argv[])
|
||||
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 */
|
||||
if (create_flag && (clean_flag || remove_flag)) {
|
||||
create_flag = 0;
|
||||
|
||||
Reference in new issue
Block a user