Yu Watanabe | db9ecf0 | 2020-11-09 13:23:58 +0900 | [diff] [blame] | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
Lennart Poettering | c687863 | 2015-04-04 11:52:57 +0200 | [diff] [blame] | 2 | #pragma once |
| 3 | |
Lennart Poettering | c687863 | 2015-04-04 11:52:57 +0200 | [diff] [blame] | 4 | #include <sys/stat.h> |
| 5 | |
Lennart Poettering | 2b2fec7 | 2019-03-14 12:24:39 +0100 | [diff] [blame] | 6 | #include "errno-util.h" |
Lennart Poettering | dfd1478 | 2018-01-10 17:21:15 +0100 | [diff] [blame] | 7 | |
Lennart Poettering | c687863 | 2015-04-04 11:52:57 +0200 | [diff] [blame] | 8 | typedef enum RemoveFlags { |
Lennart Poettering | c0228b4 | 2019-03-29 16:13:03 +0100 | [diff] [blame] | 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 */ |
Allen Webb | 6b08fdf | 2021-06-28 11:07:59 -0500 | [diff] [blame^] | 14 | REMOVE_CHMOD = 1 << 5, /* chmod() for write access if we cannot delete or access something */ |
| 15 | REMOVE_CHMOD_RESTORE = 1 << 6, /* Restore the old mode before returning */ |
Lennart Poettering | c687863 | 2015-04-04 11:52:57 +0200 | [diff] [blame] | 16 | } RemoveFlags; |
| 17 | |
Allen Webb | 6b08fdf | 2021-06-28 11:07:59 -0500 | [diff] [blame^] | 18 | int unlinkat_harder(int dfd, const char *filename, int unlink_flags, RemoveFlags remove_flags); |
| 19 | int fstatat_harder(int dfd, |
| 20 | const char *filename, |
| 21 | struct stat *ret, |
| 22 | int fstatat_flags, |
| 23 | RemoveFlags remove_flags); |
| 24 | |
Lennart Poettering | c687863 | 2015-04-04 11:52:57 +0200 | [diff] [blame] | 25 | int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev); |
| 26 | int rm_rf(const char *path, RemoveFlags flags); |
Lennart Poettering | d212059 | 2016-04-08 18:54:05 +0200 | [diff] [blame] | 27 | |
| 28 | /* Useful for usage with _cleanup_(), destroys a directory and frees the pointer */ |
Evgeny Vereshchagin | f942504 | 2016-05-20 16:08:24 +0300 | [diff] [blame] | 29 | static inline void rm_rf_physical_and_free(char *p) { |
Lennart Poettering | dfd1478 | 2018-01-10 17:21:15 +0100 | [diff] [blame] | 30 | PROTECT_ERRNO; |
Evgeny Vereshchagin | f942504 | 2016-05-20 16:08:24 +0300 | [diff] [blame] | 31 | (void) rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL); |
Lennart Poettering | d212059 | 2016-04-08 18:54:05 +0200 | [diff] [blame] | 32 | free(p); |
| 33 | } |
Evgeny Vereshchagin | f942504 | 2016-05-20 16:08:24 +0300 | [diff] [blame] | 34 | DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rm_rf_physical_and_free); |
Lennart Poettering | 1d7579c | 2018-10-10 21:20:08 +0200 | [diff] [blame] | 35 | |
| 36 | /* Similar as above, but also has magic btrfs subvolume powers */ |
| 37 | static inline void rm_rf_subvolume_and_free(char *p) { |
| 38 | PROTECT_ERRNO; |
| 39 | (void) rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME); |
| 40 | free(p); |
| 41 | } |
| 42 | DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rm_rf_subvolume_and_free); |