]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/fs-util.h
basic: spit out chase_symlinks() from fs-util.[ch] → chase-symlinks.[ch]
[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"
f4f15635
LP
15#include "time-util.h"
16
961189af
YW
17#define MODE_INVALID ((mode_t) -1)
18
19/* The following macros add 1 when converting things, since 0 is a valid mode, while the pointer
20 * NULL is special */
21#define PTR_TO_MODE(p) ((mode_t) ((uintptr_t) (p)-1))
22#define MODE_TO_PTR(u) ((void *) ((uintptr_t) (u)+1))
23
f4f15635
LP
24int unlink_noerrno(const char *path);
25
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
LP
34
35int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
0520564d
ZJS
36int fchmod_and_chown_with_fallback(int fd, const char *path, mode_t mode, uid_t uid, gid_t gid);
37static inline int fchmod_and_chown(int fd, mode_t mode, uid_t uid, gid_t gid) {
38 return fchmod_and_chown_with_fallback(fd, NULL, mode, uid, gid); /* no fallback */
39}
f4f15635
LP
40
41int fchmod_umask(int fd, mode_t mode);
4dfaa528 42int fchmod_opath(int fd, mode_t m);
f4f15635 43
f25bff5e
LP
44int futimens_opath(int fd, const struct timespec ts[2]);
45
f4f15635 46int fd_warn_permissions(const char *path, int fd);
22ed4a6d 47int stat_warn_permissions(const char *path, const struct stat *st);
f4f15635 48
41979f59
LP
49#define laccess(path, mode) \
50 (faccessat(AT_FDCWD, (path), (mode), AT_SYMLINK_NOFOLLOW) < 0 ? -errno : 0)
f4f15635
LP
51
52int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode);
53int touch(const char *path);
54
6c9c51e5 55int symlink_idempotent(const char *from, const char *to, bool make_relative);
f4f15635
LP
56
57int symlink_atomic(const char *from, const char *to);
58int mknod_atomic(const char *path, mode_t mode, dev_t dev);
59int mkfifo_atomic(const char *path, mode_t mode);
4fe3828c 60int mkfifoat_atomic(int dir_fd, const char *path, mode_t mode);
f4f15635
LP
61
62int get_files_in_directory(const char *path, char ***list);
77601719 63
992e8f22
LP
64int tmp_dir(const char **ret);
65int var_tmp_dir(const char **ret);
34a8f081 66
af229d7a
ZJS
67int unlink_or_warn(const char *filename);
68
676bcb0f 69/* Useful for usage with _cleanup_(), removes a directory and frees the pointer */
63ec26a4 70static inline char *rmdir_and_free(char *p) {
dfd14786 71 PROTECT_ERRNO;
63ec26a4
LP
72
73 if (!p)
74 return NULL;
75
676bcb0f 76 (void) rmdir(p);
57ac6959 77 return mfree(p);
676bcb0f
LP
78}
79DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rmdir_and_free);
f5b84de2 80
63ec26a4
LP
81static inline char* unlink_and_free(char *p) {
82 if (!p)
83 return NULL;
84
dfd14786 85 (void) unlink_noerrno(p);
57ac6959 86 return mfree(p);
f5b84de2
LP
87}
88DEFINE_TRIVIAL_CLEANUP_FUNC(char*, unlink_and_free);
57a4359e
LP
89
90int access_fd(int fd, int mode);
43767d9d 91
627d2bac 92void unlink_tempfilep(char (*p)[]);
053e0626
LP
93
94typedef enum UnlinkDeallocateFlags {
95 UNLINK_REMOVEDIR = 1 << 0,
96 UNLINK_ERASE = 1 << 1,
97} UnlinkDeallocateFlags;
98
99int unlinkat_deallocate(int fd, const char *name, UnlinkDeallocateFlags flags);
11b29a96 100
ef8becfa 101int open_parent(const char *path, int flags, mode_t mode);
ed9c0851 102
10195179
YW
103int conservative_renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
104static inline int conservative_rename(const char *oldpath, const char *newpath) {
105 return conservative_renameat(AT_FDCWD, oldpath, AT_FDCWD, newpath);
106}
4c54768c
IZ
107
108int posix_fallocate_loop(int fd, uint64_t offset, uint64_t size);