#include <time.h>
#include <sys/types.h>
#include <sys/param.h>
+#include <glob.h>
+#include <fnmatch.h>
#include "log.h"
#include "util.h"
* bootup. */
enum {
+ /* These ones take file names */
CREATE_FILE = 'f',
TRUNCATE_FILE = 'F',
CREATE_DIRECTORY = 'd',
TRUNCATE_DIRECTORY = 'D',
+
+ /* These ones take globs */
IGNORE_PATH = 'x',
REMOVE_PATH = 'r',
RECURSIVE_REMOVE_PATH = 'R'
bool age_set:1;
} Item;
-static Hashmap *items = NULL;
+static Hashmap *items = NULL, *globs = NULL;
static bool arg_create = false;
static bool arg_clean = false;
#define MAX_DEPTH 256
+static bool needs_glob(int t) {
+ return t == IGNORE_PATH || t == REMOVE_PATH || t == RECURSIVE_REMOVE_PATH;
+}
+
+static struct Item* find_glob(Hashmap *h, const char *match) {
+ Item *j;
+ Iterator i;
+
+ HASHMAP_FOREACH(j, h, i)
+ if (fnmatch(j->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
+ return j;
+
+ return NULL;
+}
+
static int dir_cleanup(
const char *p,
DIR *d,
if (hashmap_get(items, sub_path))
continue;
+ if (find_glob(globs, sub_path))
+ continue;
+
if (S_ISDIR(s.st_mode)) {
if (mountpoint &&
DIR *sub_dir;
int q;
- sub_dir = xopendirat(dirfd(d), dent->d_name);
+ sub_dir = xopendirat(dirfd(d), dent->d_name, O_NOFOLLOW);
if (sub_dir == NULL) {
if (errno != ENOENT) {
log_error("opendir(%s/%s) failed: %m", p, dent->d_name);
return r;
}
-static int remove_item(Item *i) {
+static int remove_item(Item *i, const char *instance) {
int r;
assert(i);
break;
case REMOVE_PATH:
- if (remove(i->path) < 0 && errno != ENOENT) {
- log_error("remove(%s): %m", i->path);
+ if (remove(instance) < 0 && errno != ENOENT) {
+ log_error("remove(%s): %m", instance);
return -errno;
}
case TRUNCATE_DIRECTORY:
case RECURSIVE_REMOVE_PATH:
- if ((r = rm_rf(i->path, false, i->type == RECURSIVE_REMOVE_PATH)) < 0 &&
+ if ((r = rm_rf(instance, false, i->type == RECURSIVE_REMOVE_PATH)) < 0 &&
r != -ENOENT) {
- log_error("rm_rf(%s): %s", i->path, strerror(-r));
+ log_error("rm_rf(%s): %s", instance, strerror(-r));
return r;
}
return 0;
}
+static int remove_item_glob(Item *i) {
+ assert(i);
+
+ switch (i->type) {
+
+ case CREATE_FILE:
+ case TRUNCATE_FILE:
+ case CREATE_DIRECTORY:
+ case IGNORE_PATH:
+ break;
+
+ case REMOVE_PATH:
+ case TRUNCATE_DIRECTORY:
+ case RECURSIVE_REMOVE_PATH: {
+ int r = 0, k;
+ glob_t g;
+ char **fn;
+
+ zero(g);
+
+ errno = 0;
+ if ((k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g)) != 0) {
+
+ if (k != GLOB_NOMATCH) {
+ if (errno != 0)
+ errno = EIO;
+
+ log_error("glob(%s) failed: %m", i->path);
+ return -errno;
+ }
+ }
+
+ STRV_FOREACH(fn, g.gl_pathv)
+ if ((k = remove_item(i, *fn)) < 0)
+ r = k;
+
+ globfree(&g);
+ return r;
+ }
+ }
+
+ return 0;
+}
+
static int process_item(Item *i) {
int r, q, p;
assert(i);
r = arg_create ? create_item(i) : 0;
- q = arg_remove ? remove_item(i) : 0;
+ q = arg_remove ? remove_item_glob(i) : 0;
p = arg_clean ? clean_item(i) : 0;
if (r < 0)
static int parse_line(const char *fname, unsigned line, const char *buffer, const char *prefix) {
Item *i;
char *mode = NULL, *user = NULL, *group = NULL, *age = NULL;
- int r, n;
+ int r;
assert(fname);
assert(line >= 1);
return -ENOMEM;
}
- if ((n = sscanf(buffer,
- "%c "
- "%ms "
- "%ms "
- "%ms "
- "%ms "
- "%ms",
- &i->type,
- &i->path,
- &mode,
- &user,
- &group,
- &age)) < 2) {
+ if (sscanf(buffer,
+ "%c "
+ "%ms "
+ "%ms "
+ "%ms "
+ "%ms "
+ "%ms",
+ &i->type,
+ &i->path,
+ &mode,
+ &user,
+ &group,
+ &age) < 2) {
log_error("[%s:%u] Syntax error.", fname, line);
r = -EIO;
goto finish;
i->age_set = true;
}
- if ((r = hashmap_put(items, i->path, i)) < 0) {
+ if ((r = hashmap_put(needs_glob(i->type) ? globs : items, i->path, i)) < 0) {
+ if (r == -EEXIST) {
+ log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
+ r = 0;
+ goto finish;
+ }
+
log_error("Failed to insert item %s: %s", i->path, strerror(-r));
goto finish;
}
label_init();
- if (!(items = hashmap_new(string_hash_func, string_compare_func))) {
+ items = hashmap_new(string_hash_func, string_compare_func);
+ globs = hashmap_new(string_hash_func, string_compare_func);
+
+ if (!items || !globs) {
log_error("Out of memory");
r = EXIT_FAILURE;
goto finish;
free(de);
+ HASHMAP_FOREACH(i, globs, iterator)
+ if (process_item(i) < 0)
+ r = EXIT_FAILURE;
+
HASHMAP_FOREACH(i, items, iterator)
if (process_item(i) < 0)
r = EXIT_FAILURE;
item_free(i);
hashmap_free(items);
+ hashmap_free(globs);
label_finish();