Debian Codegraph @

src/basic/fs-util.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include "basic-forward.h"
5 #include "lock-util.h"
6
7 /* The following macros add 1 when converting things, since 0 is a valid mode, while the pointer
8 * NULL is special */
9 static inline mode_t PTR_TO_MODE(void *p) {
10 return p ? (mode_t) ((uintptr_t) p - 1) : MODE_INVALID;
11 }
12 static inline void* MODE_TO_PTR(mode_t m) {
13 return m == MODE_INVALID ? NULL : (void *) ((uintptr_t) m + 1);
14 }
15
16 int rmdir_parents(const char *path, const char *stop);
17
18 int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
19
20 int readlinkat_malloc(int fd, const char *p, char **ret);
21 static inline int readlink_malloc(const char *p, char **ret) {
22 return readlinkat_malloc(AT_FDCWD, p, ret);
23 }
24 int readlink_value(const char *p, char **ret);
25 int readlink_and_make_absolute(const char *p, char **ret);
26
27 int chmod_and_chown_at(int dir_fd, const char *path, mode_t mode, uid_t uid, gid_t gid);
28 static inline int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) {
29 return chmod_and_chown_at(AT_FDCWD, path, mode, uid, gid);
30 }
31 int fchmod_and_chown_with_fallback(int fd, const char *path, mode_t mode, uid_t uid, gid_t gid);
32 static inline int fchmod_and_chown(int fd, mode_t mode, uid_t uid, gid_t gid) {
33 return fchmod_and_chown_with_fallback(fd, NULL, mode, uid, gid); /* no fallback */
34 }
35
36 int fchmod_umask(int fd, mode_t mode);
37 int fchmod_opath(int fd, mode_t m);
38
39 int futimens_opath(int fd, const struct timespec ts[2]);
40
41 int fd_warn_permissions(const char *path, int fd);
42 int stat_warn_permissions(const char *path, const struct stat *st);
43
44 int access_nofollow(const char *path, int mode);
45 int access_fd(int fd, int mode);
46
47 int touch_fd(int fd, usec_t stamp);
48 int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode);
49 int touch(const char *path);
50
51 int symlinkat_idempotent(const char *target, int atfd, const char *linkpath, bool make_relative);
52 static inline int symlink_idempotent(const char *target, const char *linkpath, bool make_relative) {
53 return symlinkat_idempotent(target, AT_FDCWD, linkpath, make_relative);
54 }
55
56 typedef enum SymlinkFlags {
57 SYMLINK_MAKE_RELATIVE = 1 << 0,
58 SYMLINK_LABEL = 1 << 1,
59 } SymlinkFlags;
60
61 int symlinkat_atomic_full(const char *target, int atfd, const char *linkpath, SymlinkFlags flags);
62 static inline int symlink_atomic(const char *target, const char *linkpath) {
63 return symlinkat_atomic_full(target, AT_FDCWD, linkpath, 0);
64 }
65
66 int mknodat_atomic(int atfd, const char *path, mode_t mode, dev_t dev);
67 static inline int mknod_atomic(const char *path, mode_t mode, dev_t dev) {
68 return mknodat_atomic(AT_FDCWD, path, mode, dev);
69 }
70
71 int mkfifoat_atomic(int dir_fd, const char *path, mode_t mode);
72 static inline int mkfifo_atomic(const char *path, mode_t mode) {
73 return mkfifoat_atomic(AT_FDCWD, path, mode);
74 }
75
76 int get_files_in_directory(const char *path, char ***list);
77
78 int tmp_dir(const char **ret);
79 int var_tmp_dir(const char **ret);
80
81 int unlink_or_warn(const char *filename);
82
83 /* Useful for usage with _cleanup_(), removes a directory and frees the pointer */
84 char *rmdir_and_free(char *p);
85 DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rmdir_and_free);
86
87 char* unlink_and_free(char *p);
88 DEFINE_TRIVIAL_CLEANUP_FUNC(char*, unlink_and_free);
89
90 typedef enum UnlinkDeallocateFlags {
91 UNLINK_REMOVEDIR = 1 << 0,
92 UNLINK_ERASE = 1 << 1,
93 } UnlinkDeallocateFlags;
94
95 int unlinkat_deallocate(int fd, const char *name, UnlinkDeallocateFlags flags);
96
97 int open_parent_at(int dir_fd, const char *path, int flags, mode_t mode);
98 static inline int open_parent(const char *path, int flags, mode_t mode) {
99 return open_parent_at(AT_FDCWD, path, flags, mode);
100 }
101
102 int conservative_renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
103 static inline int conservative_rename(const char *oldpath, const char *newpath) {
104 return conservative_renameat(AT_FDCWD, oldpath, AT_FDCWD, newpath);
105 }
106
107 int posix_fallocate_loop(int fd, uint64_t offset, uint64_t size);
108
109 int parse_cifs_service(const char *s, char **ret_host, char **ret_service, char **ret_path);
110
111 typedef enum XOpenFlags {
112 XO_LABEL = 1 << 0, /* When creating: relabel */
113 XO_SUBVOLUME = 1 << 1, /* When creating as directory: make it a subvolume */
114 XO_NOCOW = 1 << 2, /* Enable NOCOW mode after opening */
115 XO_REGULAR = 1 << 3, /* Fail if the inode is not a regular file */
116 XO_SOCKET = 1 << 4, /* Fail if the inode is not a socket */
117 XO_TRIGGER_AUTOMOUNT = 1 << 5, /* Trigger automounts via open_tree(). Requires O_PATH. */
118 XO_AUTO_RW_RO = 1 << 6, /* Open in O_RDWR mode if possible, O_RDONLY if not */
119 } XOpenFlags;
120
121 int open_mkdir_at_full(int dirfd, const char *path, int flags, XOpenFlags xopen_flags, mode_t mode);
122 static inline int open_mkdir_at(int dirfd, const char *path, int flags, mode_t mode) {
123 return open_mkdir_at_full(dirfd, path, flags, 0, mode);
124 }
125 static inline int open_mkdir(const char *path, int flags, mode_t mode) {
126 return open_mkdir_at_full(AT_FDCWD, path, flags, 0, mode);
127 }
128
129 int openat_report_new(int dirfd, const char *pathname, int flags, mode_t mode, bool *ret_newly_created);
130
131 int xopenat_full(int dir_fd, const char *path, int open_flags, XOpenFlags xopen_flags, mode_t mode);
132 static inline int xopenat(int dir_fd, const char *path, int open_flags) {
133 return xopenat_full(dir_fd, path, open_flags, 0, MODE_INVALID);
134 }
135
136 int xopenat_lock_full(int dir_fd, const char *path, int open_flags, XOpenFlags xopen_flags, mode_t mode, LockType locktype, int operation);
137 static inline int xopenat_lock(int dir_fd, const char *path, int open_flags, LockType locktype, int operation) {
138 return xopenat_lock_full(dir_fd, path, open_flags, 0, 0, locktype, operation);
139 }
140
141 int link_fd(int fd, int newdirfd, const char *newpath);
142
143 int linkat_replace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
144
145 static inline int at_flags_normalize_nofollow(int flags) {
146 if (FLAGS_SET(flags, AT_SYMLINK_FOLLOW)) {
147 assert(!FLAGS_SET(flags, AT_SYMLINK_NOFOLLOW));
148 flags &= ~AT_SYMLINK_FOLLOW;
149 } else
150 flags |= AT_SYMLINK_NOFOLLOW;
151 return flags;
152 }
153
154 static inline int at_flags_normalize_follow(int flags) {
155 if (FLAGS_SET(flags, AT_SYMLINK_NOFOLLOW)) {
156 assert(!FLAGS_SET(flags, AT_SYMLINK_FOLLOW));
157 flags &= ~AT_SYMLINK_NOFOLLOW;
158 } else
159 flags |= AT_SYMLINK_FOLLOW;
160 return flags;
161 }