1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <dirent.h>
5 #include <fcntl.h>
6 #include <limits.h>
7 #include <stdbool.h>
8 #include <stdint.h>
9 #include <sys/stat.h>
10 #include <sys/types.h>
11 #include <unistd.h>
12
13 #include "alloc-util.h"
14 #include "errno-util.h"
15 #include "time-util.h"
16
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
24 int unlink_noerrno(const char *path);
25
26 int rmdir_parents(const char *path, const char *stop);
27
28 int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
29
30 int readlinkat_malloc(int fd, const char *p, char **ret);
31 int readlink_malloc(const char *p, char **r);
32 int readlink_value(const char *p, char **ret);
33 int readlink_and_make_absolute(const char *p, char **r);
34
35 int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
36 int fchmod_and_chown_with_fallback(int fd, const char *path, mode_t mode, uid_t uid, gid_t gid);
fchmod_and_chown(int fd,mode_t mode,uid_t uid,gid_t gid)37 static 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 }
40
41 int fchmod_umask(int fd, mode_t mode);
42 int fchmod_opath(int fd, mode_t m);
43
44 int futimens_opath(int fd, const struct timespec ts[2]);
45
46 int fd_warn_permissions(const char *path, int fd);
47 int stat_warn_permissions(const char *path, const struct stat *st);
48
49 #define laccess(path, mode) \
50 RET_NERRNO(faccessat(AT_FDCWD, (path), (mode), AT_SYMLINK_NOFOLLOW))
51
52 int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode);
53 int touch(const char *path);
54
55 int symlink_idempotent(const char *from, const char *to, bool make_relative);
56
57 int symlink_atomic(const char *from, const char *to);
58 int mknod_atomic(const char *path, mode_t mode, dev_t dev);
59 int mkfifo_atomic(const char *path, mode_t mode);
60 int mkfifoat_atomic(int dir_fd, const char *path, mode_t mode);
61
62 int get_files_in_directory(const char *path, char ***list);
63
64 int tmp_dir(const char **ret);
65 int var_tmp_dir(const char **ret);
66
67 int unlink_or_warn(const char *filename);
68
69 /* Useful for usage with _cleanup_(), removes a directory and frees the pointer */
rmdir_and_free(char * p)70 static inline char *rmdir_and_free(char *p) {
71 PROTECT_ERRNO;
72
73 if (!p)
74 return NULL;
75
76 (void) rmdir(p);
77 return mfree(p);
78 }
79 DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rmdir_and_free);
80
unlink_and_free(char * p)81 static inline char* unlink_and_free(char *p) {
82 if (!p)
83 return NULL;
84
85 (void) unlink_noerrno(p);
86 return mfree(p);
87 }
88 DEFINE_TRIVIAL_CLEANUP_FUNC(char*, unlink_and_free);
89
90 int access_fd(int fd, int mode);
91
92 void unlink_tempfilep(char (*p)[]);
93
94 typedef enum UnlinkDeallocateFlags {
95 UNLINK_REMOVEDIR = 1 << 0,
96 UNLINK_ERASE = 1 << 1,
97 } UnlinkDeallocateFlags;
98
99 int unlinkat_deallocate(int fd, const char *name, UnlinkDeallocateFlags flags);
100
101 int open_parent(const char *path, int flags, mode_t mode);
102
103 int conservative_renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
conservative_rename(const char * oldpath,const char * newpath)104 static inline int conservative_rename(const char *oldpath, const char *newpath) {
105 return conservative_renameat(AT_FDCWD, oldpath, AT_FDCWD, newpath);
106 }
107
108 int posix_fallocate_loop(int fd, uint64_t offset, uint64_t size);
109
110 int parse_cifs_service(const char *s, char **ret_host, char **ret_service, char **ret_path);
111
112 int open_mkdir_at(int dirfd, const char *path, int flags, mode_t mode);
113
114 int openat_report_new(int dirfd, const char *pathname, int flags, mode_t mode, bool *ret_newly_created);
115