]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/fs-util.h
Merge pull request #31777 from keszybz/unit-retitling-and-comments
[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
21c692e9 4#include <dirent.h>
f4f15635 5#include <fcntl.h>
71d35b6b 6#include <limits.h>
11c3a366
TA
7#include <stdbool.h>
8#include <stdint.h>
56e3c958 9#include <sys/stat.h>
77601719 10#include <sys/types.h>
f4f15635
LP
11#include <unistd.h>
12
57ac6959 13#include "alloc-util.h"
2b2fec7d 14#include "errno-util.h"
2646b86d 15#include "lock-util.h"
f4f15635 16#include "time-util.h"
86b4e141 17#include "user-util.h"
f4f15635 18
961189af
YW
19#define MODE_INVALID ((mode_t) -1)
20
21/* The following macros add 1 when converting things, since 0 is a valid mode, while the pointer
22 * NULL is special */
23#define PTR_TO_MODE(p) ((mode_t) ((uintptr_t) (p)-1))
24#define MODE_TO_PTR(u) ((void *) ((uintptr_t) (u)+1))
25
f4f15635
LP
26int rmdir_parents(const char *path, const char *stop);
27
28int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
29
30int readlinkat_malloc(int fd, const char *p, char **ret);
31int readlink_malloc(const char *p, char **r);
32int readlink_value(const char *p, char **ret);
33int readlink_and_make_absolute(const char *p, char **r);
f4f15635 34
55451417
DDM
35int chmod_and_chown_at(int dir_fd, const char *path, mode_t mode, uid_t uid, gid_t gid);
36static inline int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) {
37 return chmod_and_chown_at(AT_FDCWD, path, mode, uid, gid);
38}
0520564d
ZJS
39int fchmod_and_chown_with_fallback(int fd, const char *path, mode_t mode, uid_t uid, gid_t gid);
40static inline int fchmod_and_chown(int fd, mode_t mode, uid_t uid, gid_t gid) {
41 return fchmod_and_chown_with_fallback(fd, NULL, mode, uid, gid); /* no fallback */
42}
f4f15635
LP
43
44int fchmod_umask(int fd, mode_t mode);
4dfaa528 45int fchmod_opath(int fd, mode_t m);
f4f15635 46
f25bff5e
LP
47int futimens_opath(int fd, const struct timespec ts[2]);
48
f4f15635 49int fd_warn_permissions(const char *path, int fd);
22ed4a6d 50int stat_warn_permissions(const char *path, const struct stat *st);
f4f15635 51
41979f59 52#define laccess(path, mode) \
7c248223 53 RET_NERRNO(faccessat(AT_FDCWD, (path), (mode), AT_SYMLINK_NOFOLLOW))
f4f15635
LP
54
55int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode);
86b4e141
LP
56
57static inline int touch(const char *path) {
58 return touch_file(path, false, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID);
59}
f4f15635 60
6c9c51e5 61int symlink_idempotent(const char *from, const char *to, bool make_relative);
f4f15635 62
da9dd029 63int symlinkat_atomic_full(const char *from, int atfd, const char *to, bool make_relative);
590d8100 64static inline int symlink_atomic(const char *from, const char *to) {
da9dd029 65 return symlinkat_atomic_full(from, AT_FDCWD, to, false);
590d8100 66}
497ca785
LP
67
68int mknodat_atomic(int atfd, const char *path, mode_t mode, dev_t dev);
69static inline int mknod_atomic(const char *path, mode_t mode, dev_t dev) {
70 return mknodat_atomic(AT_FDCWD, path, mode, dev);
71}
72
4fe3828c 73int mkfifoat_atomic(int dir_fd, const char *path, mode_t mode);
4f477796
LP
74static inline int mkfifo_atomic(const char *path, mode_t mode) {
75 return mkfifoat_atomic(AT_FDCWD, path, mode);
76}
f4f15635
LP
77
78int get_files_in_directory(const char *path, char ***list);
77601719 79
992e8f22
LP
80int tmp_dir(const char **ret);
81int var_tmp_dir(const char **ret);
34a8f081 82
af229d7a
ZJS
83int unlink_or_warn(const char *filename);
84
676bcb0f 85/* Useful for usage with _cleanup_(), removes a directory and frees the pointer */
63ec26a4 86static inline char *rmdir_and_free(char *p) {
dfd14786 87 PROTECT_ERRNO;
63ec26a4
LP
88
89 if (!p)
90 return NULL;
91
676bcb0f 92 (void) rmdir(p);
57ac6959 93 return mfree(p);
676bcb0f
LP
94}
95DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rmdir_and_free);
f5b84de2 96
63ec26a4
LP
97static inline char* unlink_and_free(char *p) {
98 if (!p)
99 return NULL;
100
39eb3ffa 101 (void) unlink(p);
57ac6959 102 return mfree(p);
f5b84de2
LP
103}
104DEFINE_TRIVIAL_CLEANUP_FUNC(char*, unlink_and_free);
57a4359e
LP
105
106int access_fd(int fd, int mode);
43767d9d 107
627d2bac 108void unlink_tempfilep(char (*p)[]);
053e0626
LP
109
110typedef enum UnlinkDeallocateFlags {
111 UNLINK_REMOVEDIR = 1 << 0,
112 UNLINK_ERASE = 1 << 1,
113} UnlinkDeallocateFlags;
114
115int unlinkat_deallocate(int fd, const char *name, UnlinkDeallocateFlags flags);
11b29a96 116
14460a8a
DDM
117int open_parent_at(int dir_fd, const char *path, int flags, mode_t mode);
118static inline int open_parent(const char *path, int flags, mode_t mode) {
119 return open_parent_at(AT_FDCWD, path, flags, mode);
120}
ed9c0851 121
10195179
YW
122int conservative_renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
123static inline int conservative_rename(const char *oldpath, const char *newpath) {
124 return conservative_renameat(AT_FDCWD, oldpath, AT_FDCWD, newpath);
125}
4c54768c
IZ
126
127int posix_fallocate_loop(int fd, uint64_t offset, uint64_t size);
68def5a9
LP
128
129int parse_cifs_service(const char *s, char **ret_host, char **ret_service, char **ret_path);
c73094f3
LP
130
131int open_mkdir_at(int dirfd, const char *path, int flags, mode_t mode);
ca8503f1
LP
132
133int openat_report_new(int dirfd, const char *pathname, int flags, mode_t mode, bool *ret_newly_created);
7486f9c3 134
420d2e31 135typedef enum XOpenFlags {
bc6a6130
DDM
136 XO_LABEL = 1 << 0,
137 XO_SUBVOLUME = 1 << 1,
420d2e31 138} XOpenFlags;
2646b86d 139
e40b11be
YW
140int xopenat_full(int dir_fd, const char *path, int open_flags, XOpenFlags xopen_flags, mode_t mode);
141static inline int xopenat(int dir_fd, const char *path, int open_flags) {
142 return xopenat_full(dir_fd, path, open_flags, 0, 0);
143}
420d2e31 144
e40b11be
YW
145int xopenat_lock_full(int dir_fd, const char *path, int open_flags, XOpenFlags xopen_flags, mode_t mode, LockType locktype, int operation);
146static inline int xopenat_lock(int dir_fd, const char *path, int open_flags, LockType locktype, int operation) {
147 return xopenat_lock_full(dir_fd, path, open_flags, 0, 0, locktype, operation);
148}
0b8e36f0
LP
149
150int link_fd(int fd, int newdirfd, const char *newpath);
1f27e7b7
LP
151
152int linkat_replace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);