blob: 5f75a838e682168c0aef66d6e1c9ca54bc79ad84 [file] [log] [blame]
Yu Watanabedb9ecf02020-11-09 13:23:58 +09001/* SPDX-License-Identifier: LGPL-2.1-or-later */
Lennart Poetteringc6878632015-04-04 11:52:57 +02002#pragma once
3
Lennart Poetteringc6878632015-04-04 11:52:57 +02004#include <sys/stat.h>
5
Lennart Poettering2b2fec72019-03-14 12:24:39 +01006#include "errno-util.h"
Lennart Poetteringdfd14782018-01-10 17:21:15 +01007
Lennart Poetteringc6878632015-04-04 11:52:57 +02008typedef enum RemoveFlags {
Lennart Poetteringc0228b42019-03-29 16:13:03 +01009 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 Webb6b08fdf2021-06-28 11:07:59 -050014 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 Poetteringc6878632015-04-04 11:52:57 +020016} RemoveFlags;
17
Allen Webb6b08fdf2021-06-28 11:07:59 -050018int unlinkat_harder(int dfd, const char *filename, int unlink_flags, RemoveFlags remove_flags);
19int fstatat_harder(int dfd,
20 const char *filename,
21 struct stat *ret,
22 int fstatat_flags,
23 RemoveFlags remove_flags);
24
Lennart Poetteringc6878632015-04-04 11:52:57 +020025int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev);
26int rm_rf(const char *path, RemoveFlags flags);
Lennart Poetteringd2120592016-04-08 18:54:05 +020027
28/* Useful for usage with _cleanup_(), destroys a directory and frees the pointer */
Evgeny Vereshchaginf9425042016-05-20 16:08:24 +030029static inline void rm_rf_physical_and_free(char *p) {
Lennart Poetteringdfd14782018-01-10 17:21:15 +010030 PROTECT_ERRNO;
Evgeny Vereshchaginf9425042016-05-20 16:08:24 +030031 (void) rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL);
Lennart Poetteringd2120592016-04-08 18:54:05 +020032 free(p);
33}
Evgeny Vereshchaginf9425042016-05-20 16:08:24 +030034DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rm_rf_physical_and_free);
Lennart Poettering1d7579c2018-10-10 21:20:08 +020035
36/* Similar as above, but also has magic btrfs subvolume powers */
37static 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}
42DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rm_rf_subvolume_and_free);