HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/tmpfiles/tmpfiles.c
Git blob: c4caa0b857918b2141bedc1762e40c4e54e030a4
Lines 3001–4000 of 5,453
if (r < 0)
return log_error_errno(r, "Failed to remove contents of \"%s/%s\": %m", parent_name ?: "...", name);
r = unlinkat_harder(parent_fd, name, AT_REMOVEDIR, REMOVE_CHMOD | REMOVE_CHMOD_RESTORE);
} else
r = unlinkat_harder(parent_fd, name, 0, REMOVE_CHMOD | REMOVE_CHMOD_RESTORE);
if (r < 0)
return log_error_errno(r, "Failed to remove \"%s/%s\": %m", parent_name ?: "...", name);
}
/* This is covered by the log_notice "Wrong file type...".
* It is logged earlier because it gives context to other error messages that might follow. */
return -ENOENT;
}
/* If child_mode is non-zero, rm_if_wrong_type_safe will be executed for the last path component. */
static int mkdir_parents_rm_if_wrong_type(mode_t child_mode, const char *path) {
_cleanup_close_ int parent_fd = -EBADF;
struct stat parent_st;
size_t path_len;
int r;
assert(path);
assert((child_mode & ~S_IFMT) == 0);
path_len = strlen(path);
if (!is_path(path))
/* rm_if_wrong_type_safe already logs errors. */
return rm_if_wrong_type_safe(child_mode, AT_FDCWD, NULL, path, AT_SYMLINK_NOFOLLOW);
if (child_mode != 0 && endswith(path, "/"))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Trailing path separators are only allowed if child_mode is not set; got \"%s\"", path);
/* Get the parent_fd and stat. */
parent_fd = openat(AT_FDCWD, path_is_absolute(path) ? "/" : ".", O_NOCTTY | O_CLOEXEC | O_DIRECTORY);
if (parent_fd < 0)
return log_error_errno(errno, "Failed to open root: %m");
if (fstat(parent_fd, &parent_st) < 0)
return log_error_errno(errno, "Failed to stat root: %m");
/* Check every parent directory in the path, except the last component */
for (const char *e = path;;) {
_cleanup_close_ int next_fd = -EBADF;
char t[path_len + 1];
const char *s;
/* Find the start of the next path component. */
s = e + strspn(e, "/");
/* Find the end of the next path component. */
e = s + strcspn(s, "/");
/* Copy the path component to t so it can be a null terminated string. */
*mempcpy_typesafe(t, s, e - s) = 0;
/* Is this the last component? If so, then check the type */
if (*e == 0)
return rm_if_wrong_type_safe(child_mode, parent_fd, &parent_st, t, AT_SYMLINK_NOFOLLOW);
r = rm_if_wrong_type_safe(S_IFDIR, parent_fd, &parent_st, t, 0);
/* Remove dangling symlinks. */
if (r == -ENOENT)
r = rm_if_wrong_type_safe(S_IFDIR, parent_fd, &parent_st, t, AT_SYMLINK_NOFOLLOW);
if (r == -ENOENT) {
if (!arg_dry_run) {
WITH_UMASK(0000)
r = mkdirat_label(parent_fd, t, 0755, arg_label_context);
if (r < 0) {
_cleanup_free_ char *parent_name = NULL;
(void) fd_get_path(parent_fd, &parent_name);
return log_error_errno(r, "Failed to mkdir \"%s\" at \"%s\": %m", t, strnull(parent_name));
}
}
} else if (r < 0)
/* rm_if_wrong_type_safe already logs errors. */
return r;
next_fd = RET_NERRNO(openat(parent_fd, t, O_NOCTTY | O_CLOEXEC | O_DIRECTORY));
if (next_fd < 0) {
_cleanup_free_ char *parent_name = NULL;
(void) fd_get_path(parent_fd, &parent_name);
return log_error_errno(next_fd, "Failed to open \"%s\" at \"%s\": %m", t, strnull(parent_name));
}
r = RET_NERRNO(fstat(next_fd, &parent_st));
if (r < 0) {
_cleanup_free_ char *parent_name = NULL;
(void) fd_get_path(parent_fd, &parent_name);
return log_error_errno(r, "Failed to stat \"%s\" at \"%s\": %m", t, strnull(parent_name));
}
close_and_replace(parent_fd, next_fd);
}
}
static int mkdir_parents_item(Item *i, mode_t child_mode) {
int r;
if (i->try_replace) {
r = mkdir_parents_rm_if_wrong_type(child_mode, i->path);
if (r < 0 && r != -ENOENT)
return r;
} else
WITH_UMASK(0000)
if (!arg_dry_run)
(void) mkdirat_parents_label(AT_FDCWD, i->path, 0755, arg_label_context);
return 0;
}
static int create_item(Context *c, Item *i) {
int r;
assert(c);
assert(i);
log_debug("Running create action for entry %c %s", (char) i->type, i->path);
switch (i->type) {
case IGNORE_PATH:
case IGNORE_DIRECTORY_PATH:
case REMOVE_PATH:
case RECURSIVE_REMOVE_PATH:
return 0;
case TRUNCATE_FILE:
case CREATE_FILE:
r = mkdir_parents_item(i, S_IFREG);
if (r < 0)
return r;
if ((i->type == CREATE_FILE && i->append_or_force) || i->type == TRUNCATE_FILE)
r = truncate_file(c, i, i->path);
else
r = create_file(c, i, i->path);
if (r < 0)
return r;
break;
case COPY_FILES:
r = mkdir_parents_item(i, 0);
if (r < 0)
return r;
r = copy_files(c, i);
if (r < 0)
return r;
break;
case WRITE_FILE:
r = glob_item(c, i, write_one_file);
if (r < 0)
return r;
break;
case CREATE_DIRECTORY:
case TRUNCATE_DIRECTORY:
r = mkdir_parents_item(i, S_IFDIR);
if (r < 0)
return r;
r = create_directory(c, i, i->path);
if (r < 0)
return r;
break;
case CREATE_SUBVOLUME:
case CREATE_SUBVOLUME_INHERIT_QUOTA:
case CREATE_SUBVOLUME_NEW_QUOTA:
r = mkdir_parents_item(i, S_IFDIR);
if (r < 0)
return r;
r = create_subvolume(c, i, i->path);
if (r < 0)
return r;
break;
case EMPTY_DIRECTORY:
r = glob_item(c, i, empty_directory);
if (r < 0)
return r;
break;
case CREATE_FIFO:
r = mkdir_parents_item(i, S_IFIFO);
if (r < 0)
return r;
r = create_fifo(c, i);
if (r < 0)
return r;
break;
case CREATE_SYMLINK:
r = mkdir_parents_item(i, S_IFLNK);
if (r < 0)
return r;
r = create_symlink(c, i);
if (r < 0)
return r;
break;
case CREATE_BLOCK_DEVICE:
case CREATE_CHAR_DEVICE:
if (have_effective_cap(CAP_MKNOD) <= 0) {
/* In a container we lack CAP_MKNOD. We shouldn't attempt to create the device node in that
* case to avoid noise, and we don't support virtualized devices in containers anyway. */
log_debug("We lack CAP_MKNOD, skipping creation of device node %s.", i->path);
return 0;
}
r = mkdir_parents_item(i, i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR);
if (r < 0)
return r;
r = create_device(c, i, i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR);
if (r < 0)
return r;
break;
case ADJUST_MODE:
case RELABEL_PATH:
r = glob_item(c, i, path_set_perms);
if (r < 0)
return r;
break;
case RECURSIVE_RELABEL_PATH:
r = glob_item_recursively(c, i, fd_set_perms);
if (r < 0)
return r;
break;
case SET_XATTR:
r = glob_item(c, i, path_set_xattrs);
if (r < 0)
return r;
break;
case RECURSIVE_SET_XATTR:
r = glob_item_recursively(c, i, fd_set_xattrs);
if (r < 0)
return r;
break;
case SET_ACL:
r = glob_item(c, i, path_set_acls);
if (r < 0)
return r;
break;
case RECURSIVE_SET_ACL:
r = glob_item_recursively(c, i, fd_set_acls);
if (r < 0)
return r;
break;
case SET_FCAPS:
r = glob_item(c, i, path_set_caps);
if (r < 0)
return r;
break;
case RECURSIVE_SET_FCAPS:
r = glob_item_recursively(c, i, fd_set_caps);
if (r < 0)
return r;
break;
case SET_ATTRIBUTE:
r = glob_item(c, i, path_set_attribute);
if (r < 0)
return r;
break;
case RECURSIVE_SET_ATTRIBUTE:
r = glob_item_recursively(c, i, fd_set_attribute);
if (r < 0)
return r;
break;
}
return 0;
}
static int remove_recursive(
Context *c,
Item *i,
const char *instance,
bool remove_instance) {
_cleanup_closedir_ DIR *d = NULL;
struct statx sx;
bool mountpoint;
int r;
r = opendir_and_stat(instance, &d, &sx, &mountpoint);
if (r < 0)
return r;
if (r == 0) {
if (remove_instance) {
log_action("Would remove", "Removing", "%s file \"%s\".", instance);
if (!arg_dry_run &&
remove(instance) < 0 &&
errno != ENOENT)
return log_error_errno(errno, "rm %s: %m", instance);
}
return 0;
}
r = dir_cleanup(c, i, instance, d,
/* self_atime_nsec= */ NSEC_INFINITY,
/* self_mtime_nsec= */ NSEC_INFINITY,
/* cutoff_nsec= */ NSEC_INFINITY,
sx.stx_dev_major, sx.stx_dev_minor,
mountpoint,
MAX_DEPTH,
/* keep_this_level= */ false,
/* age_by_file= */ 0,
/* age_by_dir= */ 0);
if (r < 0)
return r;
if (remove_instance) {
log_action("Would remove", "Removing", "%s directory \"%s\".", instance);
if (!arg_dry_run) {
r = RET_NERRNO(rmdir(instance));
if (r < 0) {
bool fatal = !IN_SET(r, -ENOENT, -ENOTEMPTY);
log_full_errno(fatal ? LOG_ERR : LOG_DEBUG, r, "Failed to remove %s: %m", instance);
if (fatal)
return r;
}
}
}
return 0;
}
static int purge_item_instance(Context *c, Item *i, const char *instance, CreationMode creation) {
return remove_recursive(c, i, instance, /* remove_instance= */ true);
}
static int purge_item(Context *c, Item *i) {
assert(i);
if (!needs_purge(i->type))
return 0;
if (!i->purge)
return 0;
log_debug("Running purge action for entry %c %s", (char) i->type, i->path);
if (needs_glob(i->type))
return glob_item(c, i, purge_item_instance);
return purge_item_instance(c, i, i->path, CREATION_EXISTING);
}
static int remove_item_instance(
Context *c,
Item *i,
const char *instance,
CreationMode creation) {
assert(c);
assert(i);
switch (i->type) {
case REMOVE_PATH:
log_action("Would remove", "Removing", "%s \"%s\".", instance);
if (!arg_dry_run &&
remove(instance) < 0 &&
errno != ENOENT)
return log_error_errno(errno, "rm %s: %m", instance);
return 0;
case RECURSIVE_REMOVE_PATH:
return remove_recursive(c, i, instance, /* remove_instance= */ true);
default:
assert_not_reached();
}
}
static bool item_instance_needs_cleanup(
Item *i,
const char *instance,
const struct statx *sx) {
assert(i);
assert(instance);
assert(sx);
if (!i->age_set)
return false;
usec_t n = now(CLOCK_REALTIME);
if (n < i->age)
return false;
usec_t cutoff = n - i->age;
nsec_t atime_nsec = FLAGS_SET(sx->stx_mask, STATX_ATIME) ? statx_timestamp_load_nsec(&sx->stx_atime) : NSEC_INFINITY;
nsec_t mtime_nsec = FLAGS_SET(sx->stx_mask, STATX_MTIME) ? statx_timestamp_load_nsec(&sx->stx_mtime) : NSEC_INFINITY;
nsec_t ctime_nsec = FLAGS_SET(sx->stx_mask, STATX_CTIME) ? statx_timestamp_load_nsec(&sx->stx_ctime) : NSEC_INFINITY;
nsec_t btime_nsec = FLAGS_SET(sx->stx_mask, STATX_BTIME) ? statx_timestamp_load_nsec(&sx->stx_btime) : NSEC_INFINITY;
bool is_dir = S_ISDIR(sx->stx_mode);
return needs_cleanup(atime_nsec, btime_nsec, ctime_nsec, mtime_nsec,
cutoff * NSEC_PER_USEC, instance,
is_dir ? i->age_by_dir : i->age_by_file, is_dir);
}
static int item_instance_open_parent(
Item *i,
const char *instance,
char **ret_name,
bool *ret_must_be_directory) {
int r;
assert(i);
assert(instance);
assert(ret_name);
assert(ret_must_be_directory);
_cleanup_free_ char *name = NULL;
r = path_extract_filename(instance, &name);
if (r < 0)
return log_error_errno(r, "Failed to extract filename from path '%s': %m", instance);
_cleanup_close_ int parent_fd = path_open_parent_safe(instance, i->allow_failure);
if (parent_fd < 0)
return parent_fd;
*ret_name = TAKE_PTR(name);
*ret_must_be_directory = r == O_DIRECTORY;
return TAKE_FD(parent_fd);
}
static int item_instance_open_opath(
int parent_fd,
const char *name,
const char *instance,
int *ret_fd,
struct statx *ret_sx) {
int r;
assert(parent_fd >= 0);
assert(name);
assert(instance);
assert(ret_fd);
assert(ret_sx);
_cleanup_close_ int fd = RET_NERRNO(openat(parent_fd, name, O_PATH|O_CLOEXEC|O_NOFOLLOW));
if (IN_SET(fd, -ENOENT, -ENOTDIR))
return 0;
if (fd < 0)
return log_error_errno(fd, "Failed to open \"%s\": %m", instance);
struct statx sx;
r = xstatx_full(fd,
/* path= */ NULL,
AT_EMPTY_PATH,
/* xstatx_flags= */ 0,
STATX_TYPE|STATX_MODE,
STATX_ATIME|STATX_MTIME|STATX_CTIME|STATX_BTIME,
/* mandatory_attributes= */ 0,
&sx);
if (r < 0)
return log_error_errno(r, "statx(%s) failed: %m", instance);
*ret_fd = TAKE_FD(fd);
*ret_sx = sx;
return 1;
}
static int item_instance_still_current(
int fd,
int parent_fd,
const char *name,
const char *instance) {
int r;
assert(fd >= 0);
assert(parent_fd >= 0);
assert(name);
assert(instance);
r = inode_same_at(fd, /* filea= */ NULL,
parent_fd, name,
AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW|AT_NO_AUTOMOUNT);
if (IN_SET(r, -ENOENT, -ENOTDIR))
return 0;
if (r < 0) {
log_debug_errno(r, "Failed to verify that \"%s\" still refers to the same inode, skipping: %m", instance);
return 0;
}
if (r == 0) {
log_debug("Skipping \"%s\": inode changed.", instance);
return 0;
}
return 1;
}
static int item_instance_fd_still_current(
int fd,
int other_fd,
const char *instance) {
int r;
assert(fd >= 0);
assert(other_fd >= 0);
assert(instance);
r = fd_inode_same(fd, other_fd);
if (r < 0) {
log_debug_errno(r, "Failed to verify that \"%s\" still refers to the same inode, skipping: %m", instance);
return 0;
}
if (r == 0) {
log_debug("Skipping \"%s\": inode changed.", instance);
return 0;
}
return 1;
}
static int item_instance_open_directory_and_lock(
int parent_fd,
const char *name,
int fd,
const char *instance,
DIR **ret) {
_cleanup_closedir_ DIR *d = NULL;
int r;
assert(parent_fd >= 0);
assert(name);
assert(fd >= 0);
assert(instance);
assert(ret);
d = xopendirat_nomod(parent_fd, name);
if (!d) {
if (IN_SET(errno, ENOENT, ENOTDIR, ELOOP))
return 0;
return log_error_errno(errno, "Failed to open directory \"%s\": %m", instance);
}
r = item_instance_fd_still_current(fd, dirfd(d), instance);
if (r <= 0)
return r;
if (!arg_dry_run &&
flock(dirfd(d), LOCK_EX|LOCK_NB) < 0) {
log_debug_errno(errno, "Couldn't acquire exclusive BSD lock on directory \"%s\", skipping: %m", instance);
return 0;
}
*ret = TAKE_PTR(d);
return 1;
}
static int item_instance_remove_empty_directory(
int parent_fd,
const char *name,
const char *instance) {
int r;
assert(parent_fd >= 0);
assert(name);
assert(instance);
if (arg_dry_run)
return 0;
r = RET_NERRNO(unlinkat(parent_fd, name, AT_REMOVEDIR));
if (r < 0) {
bool fatal = !IN_SET(r, -ENOENT, -ENOTEMPTY, -EBUSY);
log_full_errno(fatal ? LOG_ERR : LOG_DEBUG, r, "Failed to remove %s: %m", instance);
if (fatal)
return r;
}
return 0;
}
static int item_instance_remove_directory_if_empty(
int parent_fd,
const char *name,
DIR *d,
const char *instance,
bool assume_empty) {
int r;
assert(parent_fd >= 0);
assert(name);
assert(d);
assert(instance);
if (!assume_empty) {
r = dir_is_empty_at(dirfd(d), /* path= */ NULL, /* ignore_hidden_or_backup= */ false);
if (IN_SET(r, -ENOENT, -ENOTDIR))
return 0;
if (r < 0)
return log_error_errno(r, "Failed to determine whether \"%s\" is empty: %m", instance);
if (r == 0)
return 0;
}
r = item_instance_still_current(dirfd(d), parent_fd, name, instance);
if (r <= 0)
return r;
log_action("Would remove", "Removing", "%s directory \"%s\".", instance);
r = item_instance_remove_empty_directory(parent_fd, name, instance);
if (r < 0)
return r;
return 0;
}
static int clean_remove_item_instance_at(
Item *i,
int parent_fd,
const char *name,
int fd,
const char *instance,
const struct statx *sx,
bool must_be_directory) {
int r;
assert(i);
assert(parent_fd >= 0);
assert(name);
assert(fd >= 0);
assert(instance);
assert(sx);
if (must_be_directory && !S_ISDIR(sx->stx_mode))
return log_error_errno(SYNTHETIC_ERRNO(ENOTDIR), "\"%s\" is not a directory.", instance);
if (!item_instance_needs_cleanup(i, instance, sx))
return 0;
if (S_ISDIR(sx->stx_mode)) {
_cleanup_closedir_ DIR *d = NULL;
r = item_instance_open_directory_and_lock(parent_fd, name, fd, instance, &d);
if (r <= 0)
return r;
return item_instance_remove_directory_if_empty(parent_fd, name, d, instance, /* assume_empty= */ false);
}
_cleanup_close_ int lock_fd = -EBADF;
if (!arg_dry_run) {
lock_fd = xopenat(parent_fd, name, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME|O_NONBLOCK|O_NOCTTY);
if (lock_fd < 0 && !IN_SET(lock_fd, -ENOENT, -ELOOP))
log_warning_errno(lock_fd, "Opening file \"%s\" failed, proceeding without lock: %m", instance);
if (lock_fd >= 0) {
r = item_instance_fd_still_current(fd, lock_fd, instance);
if (r <= 0)
return r;
if (flock(lock_fd, LOCK_EX|LOCK_NB) < 0 && errno == EAGAIN) {
log_debug_errno(errno, "Couldn't acquire shared BSD lock on file \"%s\", skipping: %m", instance);
return 0;
}
}
r = item_instance_still_current(fd, parent_fd, name, instance);
if (r <= 0)
return r;
}
if (i->type == RECURSIVE_REMOVE_PATH)
log_action("Would remove", "Removing", "%s file \"%s\".", instance);
else
log_action("Would remove", "Removing", "%s \"%s\".", instance);
if (!arg_dry_run &&
unlinkat(parent_fd, name, 0) < 0 &&
errno != ENOENT)
log_warning_errno(errno, "Failed to remove \"%s\", ignoring: %m", instance);
return 0;
}
static int clean_remove_item_instance(
Context *c,
Item *i,
const char *instance,
CreationMode creation) {
int r;
assert(c);
assert(i);
assert(i->age_set);
assert(instance);
_cleanup_free_ char *name = NULL;
bool must_be_directory;
_cleanup_close_ int parent_fd = item_instance_open_parent(i, instance, &name, &must_be_directory);
if (parent_fd < 0)
return parent_fd;
_cleanup_close_ int fd = -EBADF;
struct statx sx;
r = item_instance_open_opath(parent_fd, name, instance, &fd, &sx);
if (r <= 0)
return r;
return clean_remove_item_instance_at(i, parent_fd, name, fd, instance, &sx, must_be_directory);
}
static int remove_item(Context *c, Item *i) {
assert(c);
assert(i);
log_debug("Running remove action for entry %c %s", (char) i->type, i->path);
switch (i->type) {
case TRUNCATE_DIRECTORY:
return remove_recursive(c, i, i->path, /* remove_instance= */ false);
case REMOVE_PATH:
case RECURSIVE_REMOVE_PATH:
return glob_item(c, i, remove_item_instance);
default:
return 0;
}
}
static char *age_by_to_string(AgeBy ab, bool is_dir) {
static const char ab_map[] = { 'a', 'b', 'c', 'm' };
size_t j = 0;
char *ret;
ret = new(char, ELEMENTSOF(ab_map) + 1);
if (!ret)
return NULL;
for (size_t i = 0; i < ELEMENTSOF(ab_map); i++)
if (BIT_SET(ab, i))
ret[j++] = is_dir ? ascii_toupper(ab_map[i]) : ab_map[i];
ret[j] = 0;
return ret;
}
static int clean_item_instance_from_dir(
Context *c,
Item *i,
const char *instance,
DIR *d,
const struct statx *sx,
bool mountpoint) {
assert(c);
assert(i);
assert(i->age_set);
assert(instance);
assert(d);
assert(sx);
if (!i->age_set)
return 0;
usec_t n = now(CLOCK_REALTIME);
if (n < i->age)
return 0;
usec_t cutoff = n - i->age;
nsec_t atime_nsec = FLAGS_SET(sx->stx_mask, STATX_ATIME) ? statx_timestamp_load_nsec(&sx->stx_atime) : NSEC_INFINITY;
nsec_t mtime_nsec = FLAGS_SET(sx->stx_mask, STATX_MTIME) ? statx_timestamp_load_nsec(&sx->stx_mtime) : NSEC_INFINITY;
if (DEBUG_LOGGING) {
_cleanup_free_ char *ab_f = NULL, *ab_d = NULL;
ab_f = age_by_to_string(i->age_by_file, false);
if (!ab_f)
return log_oom();
ab_d = age_by_to_string(i->age_by_dir, true);
if (!ab_d)
return log_oom();
log_debug("Cleanup threshold for %s \"%s\" is %s; age-by: %s%s",
mountpoint ? "mount point" : "directory",
instance,
FORMAT_TIMESTAMP_STYLE(cutoff, TIMESTAMP_US),
ab_f, ab_d);
}
return dir_cleanup(c, i, instance, d,
atime_nsec,
mtime_nsec,
cutoff * NSEC_PER_USEC,
sx->stx_dev_major, sx->stx_dev_minor,
mountpoint,
MAX_DEPTH, i->keep_first_level,
i->age_by_file, i->age_by_dir);
}
static int clean_item_instance(
Context *c,
Item *i,
const char *instance,
CreationMode creation) {
int r;
assert(c);
assert(i);
assert(i->age_set);
assert(instance);
_cleanup_closedir_ DIR *d = NULL;
struct statx sx;
bool mountpoint;
r = opendir_and_stat(instance, &d, &sx, &mountpoint);
if (r <= 0)
return r;
return clean_item_instance_from_dir(c, i, instance, d, &sx, mountpoint);
}
static int clean_recursive_remove_item_instance(
Context *c,
Item *i,
const char *instance,
CreationMode creation) {
int r;
assert(c);
assert(i);
assert(i->age_set);
assert(instance);
_cleanup_free_ char *name = NULL;
bool must_be_directory;
_cleanup_close_ int parent_fd = item_instance_open_parent(i, instance, &name, &must_be_directory);
if (parent_fd < 0)
return parent_fd;
_cleanup_close_ int fd = -EBADF;
struct statx sx;
r = item_instance_open_opath(parent_fd, name, instance, &fd, &sx);
if (r <= 0)
return r;
if (!S_ISDIR(sx.stx_mode))
return clean_remove_item_instance_at(i, parent_fd, name, fd, instance, &sx, must_be_directory);
_cleanup_closedir_ DIR *d = NULL;
r = item_instance_open_directory_and_lock(parent_fd, name, fd, instance, &d);
if (r <= 0)
return r;
struct statx dir_sx;
r = xstatx_full(dirfd(d),
/* path= */ NULL,
AT_EMPTY_PATH,
/* xstatx_flags= */ 0,
STATX_TYPE|STATX_MODE|STATX_INO,
STATX_ATIME|STATX_MTIME|STATX_CTIME|STATX_BTIME,
STATX_ATTR_MOUNT_ROOT,
&dir_sx);
if (r < 0)
return log_error_errno(r, "statx(%s) failed: %m", instance);
bool cleanup_needed = item_instance_needs_cleanup(i, instance, &dir_sx);
r = clean_item_instance_from_dir(c, i, instance, d, &dir_sx, FLAGS_SET(dir_sx.stx_attributes, STATX_ATTR_MOUNT_ROOT));
if (r < 0)
return r;
if (i->keep_first_level)
return 0;
if (!cleanup_needed)
return 0;
return item_instance_remove_directory_if_empty(parent_fd, name, d, instance, /* assume_empty= */ arg_dry_run);
}
static int clean_item(Context *c, Item *i) {
assert(c);
assert(i);
if (!i->age_set)
return 0;
log_debug("Running clean action for entry %c %s", (char) i->type, i->path);
switch (i->type) {
case CREATE_DIRECTORY:
case TRUNCATE_DIRECTORY:
case CREATE_SUBVOLUME:
case CREATE_SUBVOLUME_INHERIT_QUOTA:
case CREATE_SUBVOLUME_NEW_QUOTA:
case COPY_FILES:
return clean_item_instance(c, i, i->path, CREATION_EXISTING);
case EMPTY_DIRECTORY:
case IGNORE_PATH:
case IGNORE_DIRECTORY_PATH:
return glob_item(c, i, clean_item_instance);
case REMOVE_PATH:
return glob_item(c, i, clean_remove_item_instance);
case RECURSIVE_REMOVE_PATH:
return glob_item(c, i, clean_recursive_remove_item_instance);
default:
return 0;
}
}
static int process_item(
Context *c,
Item *i,
OperationMask operation) {
OperationMask todo;
_cleanup_free_ char *_path = NULL;
const char *path;
int r;
assert(c);
assert(i);
todo = operation & ~i->done;
if (todo == 0) /* Everything already done? */
return 0;
i->done |= operation;
path = i->path;
if (string_is_glob(path)) {
/* We can't easily check whether a glob matches any autofs path, so let's do the check only
* for the non-glob part. */
r = glob_non_glob_prefix(path, &_path);
if (r < 0 && r != -ENOENT)
return log_debug_errno(r, "Failed to deglob path: %m");
if (r >= 0)
path = _path;
}
r = chase(path, arg_root, CHASE_NO_AUTOFS|CHASE_NONEXISTENT|CHASE_WARN, NULL, NULL);
if (r == -EREMOTE) {
log_notice_errno(r, "Skipping %s", i->path); /* We log the configured path, to not confuse the user. */
return 0;
}
if (r < 0)
log_debug_errno(r, "Failed to determine whether '%s' is below autofs, ignoring: %m", i->path);
r = FLAGS_SET(operation, OPERATION_CREATE) ? create_item(c, i) : 0;
/* Failure can only be tolerated for create */
if (i->allow_failure)
r = 0;
RET_GATHER(r, FLAGS_SET(operation, OPERATION_REMOVE) ? remove_item(c, i) : 0);
RET_GATHER(r, FLAGS_SET(operation, OPERATION_CLEAN) ? clean_item(c, i) : 0);
RET_GATHER(r, FLAGS_SET(operation, OPERATION_PURGE) ? purge_item(c, i) : 0);
return r;