]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/chattr-util.h
man: fix formatting in file-hierarchy
[thirdparty/systemd.git] / src / basic / chattr-util.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <fcntl.h>
5 #include <linux/fs.h>
6 #include <stdbool.h>
7 #include <stddef.h>
8
9 #include "missing_fs.h"
10
11 /* The chattr() flags to apply when creating a new file *before* writing to it. In particular, flags such as
12 * FS_NOCOW_FL don't work if applied a-posteriori. All other flags are fine (or even necessary, think
13 * FS_IMMUTABLE_FL!) to apply after writing to the files. */
14 #define CHATTR_EARLY_FL \
15 (FS_NOATIME_FL | \
16 FS_COMPR_FL | \
17 FS_NOCOW_FL | \
18 FS_NOCOMP_FL | \
19 FS_PROJINHERIT_FL)
20
21 #define CHATTR_ALL_FL \
22 (FS_NOATIME_FL | \
23 FS_SYNC_FL | \
24 FS_DIRSYNC_FL | \
25 FS_APPEND_FL | \
26 FS_COMPR_FL | \
27 FS_NODUMP_FL | \
28 FS_EXTENT_FL | \
29 FS_IMMUTABLE_FL | \
30 FS_JOURNAL_DATA_FL | \
31 FS_SECRM_FL | \
32 FS_UNRM_FL | \
33 FS_NOTAIL_FL | \
34 FS_TOPDIR_FL | \
35 FS_NOCOW_FL | \
36 FS_PROJINHERIT_FL)
37
38 typedef enum ChattrApplyFlags {
39 CHATTR_FALLBACK_BITWISE = 1 << 0,
40 CHATTR_WARN_UNSUPPORTED_FLAGS = 1 << 1,
41 } ChattrApplyFlags;
42
43 int chattr_full(int dir_fd, const char *path, unsigned value, unsigned mask, unsigned *ret_previous, unsigned *ret_final, ChattrApplyFlags flags);
44 static inline int chattr_at(int dir_fd, const char *path, unsigned value, unsigned mask, unsigned *previous) {
45 return chattr_full(dir_fd, path, value, mask, previous, NULL, 0);
46 }
47 static inline int chattr_fd(int fd, unsigned value, unsigned mask, unsigned *previous) {
48 return chattr_full(fd, NULL, value, mask, previous, NULL, 0);
49 }
50 static inline int chattr_path(const char *path, unsigned value, unsigned mask, unsigned *previous) {
51 return chattr_full(AT_FDCWD, path, value, mask, previous, NULL, 0);
52 }
53
54 int read_attr_fd(int fd, unsigned *ret);
55 int read_attr_at(int dir_fd, const char *path, unsigned *ret);
56
57 /* Combination of chattr flags, that should be appropriate for secrets stored on disk: Secure Remove +
58 * Exclusion from Dumping + Synchronous Writing (i.e. not caching in memory) + In-Place Updating (i.e. not
59 * spurious copies). */
60 #define CHATTR_SECRET_FLAGS (FS_SECRM_FL|FS_NODUMP_FL|FS_SYNC_FL|FS_NOCOW_FL)
61
62 static inline int chattr_secret(int fd, ChattrApplyFlags flags) {
63 return chattr_full(fd, NULL, CHATTR_SECRET_FLAGS, CHATTR_SECRET_FLAGS, NULL, NULL, flags|CHATTR_FALLBACK_BITWISE);
64 }