]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/fs-util.h
man/systemd-sysext: list ephemeral/ephemeral-import in the list of options
[thirdparty/systemd.git] / src / basic / fs-util.h
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
f4f15635
LP
2#pragma once
3
0c15577a 4#include "forward.h"
2646b86d 5#include "lock-util.h"
961189af
YW
6
7/* The following macros add 1 when converting things, since 0 is a valid mode, while the pointer
8 * NULL is special */
9#define PTR_TO_MODE(p) ((mode_t) ((uintptr_t) (p)-1))
10#define MODE_TO_PTR(u) ((void *) ((uintptr_t) (u)+1))
11
f4f15635
LP
12int rmdir_parents(const char *path, const char *stop);
13
14int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
15
16int readlinkat_malloc(int fd, const char *p, char **ret);
28b9c823
YW
17static inline int readlink_malloc(const char *p, char **ret) {
18 return readlinkat_malloc(AT_FDCWD, p, ret);
19}
f4f15635 20int readlink_value(const char *p, char **ret);
28b9c823 21int readlink_and_make_absolute(const char *p, char **ret);
f4f15635 22
55451417
DDM
23int chmod_and_chown_at(int dir_fd, const char *path, mode_t mode, uid_t uid, gid_t gid);
24static inline int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) {
25 return chmod_and_chown_at(AT_FDCWD, path, mode, uid, gid);
26}
0520564d
ZJS
27int fchmod_and_chown_with_fallback(int fd, const char *path, mode_t mode, uid_t uid, gid_t gid);
28static inline int fchmod_and_chown(int fd, mode_t mode, uid_t uid, gid_t gid) {
29 return fchmod_and_chown_with_fallback(fd, NULL, mode, uid, gid); /* no fallback */
30}
f4f15635
LP
31
32int fchmod_umask(int fd, mode_t mode);
4dfaa528 33int fchmod_opath(int fd, mode_t m);
f4f15635 34
f25bff5e
LP
35int futimens_opath(int fd, const struct timespec ts[2]);
36
f4f15635 37int fd_warn_permissions(const char *path, int fd);
22ed4a6d 38int stat_warn_permissions(const char *path, const struct stat *st);
f4f15635 39
65a76659 40int access_nofollow(const char *path, int mode);
f4f15635 41
2ee6fa55 42int touch_fd(int fd, usec_t stamp);
f4f15635 43int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode);
fda65211 44int touch(const char *path);
f4f15635 45
3e187621
AV
46int symlinkat_idempotent(const char *from, int atfd, const char *to, bool make_relative);
47static inline int symlink_idempotent(const char *from, const char *to, bool make_relative) {
48 return symlinkat_idempotent(from, AT_FDCWD, to, make_relative);
49}
f4f15635 50
9ea5a6e7
LP
51typedef enum SymlinkFlags {
52 SYMLINK_MAKE_RELATIVE = 1 << 0,
53 SYMLINK_LABEL = 1 << 1,
54} SymlinkFlags;
55
56int symlinkat_atomic_full(const char *from, int atfd, const char *to, SymlinkFlags flags);
590d8100 57static inline int symlink_atomic(const char *from, const char *to) {
9ea5a6e7 58 return symlinkat_atomic_full(from, AT_FDCWD, to, 0);
590d8100 59}
497ca785
LP
60
61int mknodat_atomic(int atfd, const char *path, mode_t mode, dev_t dev);
62static inline int mknod_atomic(const char *path, mode_t mode, dev_t dev) {
63 return mknodat_atomic(AT_FDCWD, path, mode, dev);
64}
65
4fe3828c 66int mkfifoat_atomic(int dir_fd, const char *path, mode_t mode);
4f477796
LP
67static inline int mkfifo_atomic(const char *path, mode_t mode) {
68 return mkfifoat_atomic(AT_FDCWD, path, mode);
69}
f4f15635
LP
70
71int get_files_in_directory(const char *path, char ***list);
77601719 72
992e8f22
LP
73int tmp_dir(const char **ret);
74int var_tmp_dir(const char **ret);
34a8f081 75
af229d7a
ZJS
76int unlink_or_warn(const char *filename);
77
676bcb0f 78/* Useful for usage with _cleanup_(), removes a directory and frees the pointer */
fda65211 79char *rmdir_and_free(char *p);
676bcb0f 80DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rmdir_and_free);
f5b84de2 81
0c15577a 82char* unlink_and_free(char *p);
f5b84de2 83DEFINE_TRIVIAL_CLEANUP_FUNC(char*, unlink_and_free);
57a4359e
LP
84
85int access_fd(int fd, int mode);
43767d9d 86
053e0626
LP
87typedef enum UnlinkDeallocateFlags {
88 UNLINK_REMOVEDIR = 1 << 0,
89 UNLINK_ERASE = 1 << 1,
90} UnlinkDeallocateFlags;
91
92int unlinkat_deallocate(int fd, const char *name, UnlinkDeallocateFlags flags);
11b29a96 93
14460a8a
DDM
94int open_parent_at(int dir_fd, const char *path, int flags, mode_t mode);
95static inline int open_parent(const char *path, int flags, mode_t mode) {
96 return open_parent_at(AT_FDCWD, path, flags, mode);
97}
ed9c0851 98
10195179
YW
99int conservative_renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
100static inline int conservative_rename(const char *oldpath, const char *newpath) {
101 return conservative_renameat(AT_FDCWD, oldpath, AT_FDCWD, newpath);
102}
4c54768c
IZ
103
104int posix_fallocate_loop(int fd, uint64_t offset, uint64_t size);
68def5a9
LP
105
106int parse_cifs_service(const char *s, char **ret_host, char **ret_service, char **ret_path);
c73094f3 107
420d2e31 108typedef enum XOpenFlags {
dffafa47
LP
109 XO_LABEL = 1 << 0, /* When creating: relabel */
110 XO_SUBVOLUME = 1 << 1, /* When creating as directory: make it a subvolume */
111 XO_NOCOW = 1 << 2, /* Enable NOCOW mode after opening */
112 XO_REGULAR = 1 << 3, /* Fail if the inode is not a regular file */
420d2e31 113} XOpenFlags;
2646b86d 114
4be62f82
CG
115int open_mkdir_at_full(int dirfd, const char *path, int flags, XOpenFlags xopen_flags, mode_t mode);
116static inline int open_mkdir_at(int dirfd, const char *path, int flags, mode_t mode) {
117 return open_mkdir_at_full(dirfd, path, flags, 0, mode);
118}
c29778a1
LP
119static inline int open_mkdir(const char *path, int flags, mode_t mode) {
120 return open_mkdir_at_full(AT_FDCWD, path, flags, 0, mode);
121}
4be62f82
CG
122
123int openat_report_new(int dirfd, const char *pathname, int flags, mode_t mode, bool *ret_newly_created);
124
e40b11be
YW
125int xopenat_full(int dir_fd, const char *path, int open_flags, XOpenFlags xopen_flags, mode_t mode);
126static inline int xopenat(int dir_fd, const char *path, int open_flags) {
127 return xopenat_full(dir_fd, path, open_flags, 0, 0);
128}
420d2e31 129
e40b11be
YW
130int xopenat_lock_full(int dir_fd, const char *path, int open_flags, XOpenFlags xopen_flags, mode_t mode, LockType locktype, int operation);
131static inline int xopenat_lock(int dir_fd, const char *path, int open_flags, LockType locktype, int operation) {
132 return xopenat_lock_full(dir_fd, path, open_flags, 0, 0, locktype, operation);
133}
0b8e36f0
LP
134
135int link_fd(int fd, int newdirfd, const char *newpath);
1f27e7b7
LP
136
137int linkat_replace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
6981203f
MY
138
139static inline int at_flags_normalize_nofollow(int flags) {
140 if (FLAGS_SET(flags, AT_SYMLINK_FOLLOW)) {
141 assert(!FLAGS_SET(flags, AT_SYMLINK_NOFOLLOW));
142 flags &= ~AT_SYMLINK_FOLLOW;
143 } else
144 flags |= AT_SYMLINK_NOFOLLOW;
145 return flags;
146}
4b11087b
LP
147
148static inline int at_flags_normalize_follow(int flags) {
149 if (FLAGS_SET(flags, AT_SYMLINK_NOFOLLOW)) {
150 assert(!FLAGS_SET(flags, AT_SYMLINK_FOLLOW));
151 flags &= ~AT_SYMLINK_NOFOLLOW;
152 } else
153 flags |= AT_SYMLINK_FOLLOW;
154 return flags;
155}