]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/rm-rf.h
license: LGPL-2.1+ -> LGPL-2.1-or-later
[thirdparty/systemd.git] / src / basic / rm-rf.h
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
c6878637
LP
2#pragma once
3
c6878637
LP
4#include <sys/stat.h>
5
2b2fec7d 6#include "errno-util.h"
dfd14786 7
c6878637 8typedef enum RemoveFlags {
c0228b4f
LP
9 REMOVE_ONLY_DIRECTORIES = 1 << 0, /* Only remove empty directories, no files */
10 REMOVE_ROOT = 1 << 1, /* Remove the specified directory itself too, not just the contents of it */
11 REMOVE_PHYSICAL = 1 << 2, /* If not set, only removes files on tmpfs, never physical file systems */
12 REMOVE_SUBVOLUME = 1 << 3, /* Drop btrfs subvolumes in the tree too */
13 REMOVE_MISSING_OK = 1 << 4, /* If the top-level directory is missing, ignore the ENOENT for it */
2899fb02 14 REMOVE_CHMOD = 1 << 5, /* chmod() for write access if we cannot delete something */
c6878637
LP
15} RemoveFlags;
16
17int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev);
18int rm_rf(const char *path, RemoveFlags flags);
d2120590
LP
19
20/* Useful for usage with _cleanup_(), destroys a directory and frees the pointer */
f942504e 21static inline void rm_rf_physical_and_free(char *p) {
dfd14786 22 PROTECT_ERRNO;
f942504e 23 (void) rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL);
d2120590
LP
24 free(p);
25}
f942504e 26DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rm_rf_physical_and_free);
1d7579c4
LP
27
28/* Similar as above, but also has magic btrfs subvolume powers */
29static inline void rm_rf_subvolume_and_free(char *p) {
30 PROTECT_ERRNO;
31 (void) rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME);
32 free(p);
33}
34DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rm_rf_subvolume_and_free);