| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "shared-forward.h" |
| 5 |
|
| 6 |
typedef enum RemoveFlags { |
| 7 |
REMOVE_ONLY_DIRECTORIES = 1 << 0, /* Only remove empty directories, no files */ |
| 8 |
REMOVE_ROOT = 1 << 1, /* Remove the specified directory itself too, not just the contents of it */ |
| 9 |
REMOVE_PHYSICAL = 1 << 2, /* If not set, only removes files on tmpfs, never physical file systems */ |
| 10 |
REMOVE_SUBVOLUME = 1 << 3, /* Drop btrfs subvolumes in the tree too */ |
| 11 |
REMOVE_MISSING_OK = 1 << 4, /* If the top-level directory is missing, ignore the ENOENT for it */ |
| 12 |
REMOVE_CHMOD = 1 << 5, /* chmod() for write access if we cannot delete or access something */ |
| 13 |
REMOVE_CHMOD_RESTORE = 1 << 6, /* Restore the old mode before returning */ |
| 14 |
REMOVE_SYNCFS = 1 << 7, /* syncfs() the root of the specified directory after removing everything in it */ |
| 15 |
} RemoveFlags; |
| 16 |
|
| 17 |
int unlinkat_harder(int dfd, const char *filename, int unlink_flags, RemoveFlags remove_flags); |
| 18 |
int fstatat_harder(int dfd, |
| 19 |
const char *filename, |
| 20 |
struct stat *ret, |
| 21 |
int fstatat_flags, |
| 22 |
RemoveFlags remove_flags); |
| 23 |
|
| 24 |
/* Note: directory file descriptors passed to the functions below must be |
| 25 |
* positioned at the beginning. If the fd was already used for reading, rewind it. */ |
| 26 |
int rm_rf_children(int fd, RemoveFlags flags, const struct stat *root_dev); |
| 27 |
int rm_rf_child(int fd, const char *name, RemoveFlags flags); |
| 28 |
int rm_rf_at(int dir_fd, const char *path, RemoveFlags flags); |
| 29 |
static inline int rm_rf(const char *path, RemoveFlags flags) { |
| 30 |
return rm_rf_at(AT_FDCWD, path, flags); |
| 31 |
} |
| 32 |
|
| 33 |
/* Useful for using with _cleanup_(), destroys a directory on a temporary file system. */ |
| 34 |
const char* rm_rf_safe(const char *p); |
| 35 |
DEFINE_TRIVIAL_CLEANUP_FUNC(const char*, rm_rf_safe); |
| 36 |
|
| 37 |
/* Similar as above, but allow to destroy a directory on a physical file system, and also frees the pointer. */ |
| 38 |
char* rm_rf_physical_and_free(char *p); |
| 39 |
DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rm_rf_physical_and_free); |
| 40 |
|
| 41 |
/* Similar as above, but also has magic btrfs subvolume powers. */ |
| 42 |
char* rm_rf_subvolume_and_free(char *p); |
| 43 |
DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rm_rf_subvolume_and_free); |