1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 #include <sys/types.h>
13 #include "alloc-util.h"
14 #include "errno-util.h"
15 #include "lock-util.h"
16 #include "time-util.h"
17 #include "user-util.h"
19 #define MODE_INVALID ((mode_t) -1)
21 /* The following macros add 1 when converting things, since 0 is a valid mode, while the pointer
23 #define PTR_TO_MODE(p) ((mode_t) ((uintptr_t) (p)-1))
24 #define MODE_TO_PTR(u) ((void *) ((uintptr_t) (u)+1))
26 int rmdir_parents(const char *path
, const char *stop
);
28 int rename_noreplace(int olddirfd
, const char *oldpath
, int newdirfd
, const char *newpath
);
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
);
35 int chmod_and_chown_at(int dir_fd
, const char *path
, mode_t mode
, uid_t uid
, gid_t gid
);
36 static 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
);
39 int fchmod_and_chown_with_fallback(int fd
, const char *path
, mode_t mode
, uid_t uid
, gid_t gid
);
40 static 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 */
44 int fchmod_umask(int fd
, mode_t mode
);
45 int fchmod_opath(int fd
, mode_t m
);
47 int futimens_opath(int fd
, const struct timespec ts
[2]);
49 int fd_warn_permissions(const char *path
, int fd
);
50 int stat_warn_permissions(const char *path
, const struct stat
*st
);
52 #define laccess(path, mode) \
53 RET_NERRNO(faccessat(AT_FDCWD, (path), (mode), AT_SYMLINK_NOFOLLOW))
55 int touch_file(const char *path
, bool parents
, usec_t stamp
, uid_t uid
, gid_t gid
, mode_t mode
);
57 static inline int touch(const char *path
) {
58 return touch_file(path
, false, USEC_INFINITY
, UID_INVALID
, GID_INVALID
, MODE_INVALID
);
61 int symlink_idempotent(const char *from
, const char *to
, bool make_relative
);
63 int symlinkat_atomic_full(const char *from
, int atfd
, const char *to
, bool make_relative
);
64 static inline int symlink_atomic(const char *from
, const char *to
) {
65 return symlinkat_atomic_full(from
, AT_FDCWD
, to
, false);
68 int mknodat_atomic(int atfd
, const char *path
, mode_t mode
, dev_t dev
);
69 static inline int mknod_atomic(const char *path
, mode_t mode
, dev_t dev
) {
70 return mknodat_atomic(AT_FDCWD
, path
, mode
, dev
);
73 int mkfifoat_atomic(int dir_fd
, const char *path
, mode_t mode
);
74 static inline int mkfifo_atomic(const char *path
, mode_t mode
) {
75 return mkfifoat_atomic(AT_FDCWD
, path
, mode
);
78 int get_files_in_directory(const char *path
, char ***list
);
80 int tmp_dir(const char **ret
);
81 int var_tmp_dir(const char **ret
);
83 int unlink_or_warn(const char *filename
);
85 /* Useful for usage with _cleanup_(), removes a directory and frees the pointer */
86 static inline char *rmdir_and_free(char *p
) {
95 DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rmdir_and_free
);
97 static inline char* unlink_and_free(char *p
) {
104 DEFINE_TRIVIAL_CLEANUP_FUNC(char*, unlink_and_free
);
106 int access_fd(int fd
, int mode
);
108 void unlink_tempfilep(char (*p
)[]);
110 typedef enum UnlinkDeallocateFlags
{
111 UNLINK_REMOVEDIR
= 1 << 0,
112 UNLINK_ERASE
= 1 << 1,
113 } UnlinkDeallocateFlags
;
115 int unlinkat_deallocate(int fd
, const char *name
, UnlinkDeallocateFlags flags
);
117 int open_parent_at(int dir_fd
, const char *path
, int flags
, mode_t mode
);
118 static inline int open_parent(const char *path
, int flags
, mode_t mode
) {
119 return open_parent_at(AT_FDCWD
, path
, flags
, mode
);
122 int conservative_renameat(int olddirfd
, const char *oldpath
, int newdirfd
, const char *newpath
);
123 static inline int conservative_rename(const char *oldpath
, const char *newpath
) {
124 return conservative_renameat(AT_FDCWD
, oldpath
, AT_FDCWD
, newpath
);
127 int posix_fallocate_loop(int fd
, uint64_t offset
, uint64_t size
);
129 int parse_cifs_service(const char *s
, char **ret_host
, char **ret_service
, char **ret_path
);
131 int open_mkdir_at(int dirfd
, const char *path
, int flags
, mode_t mode
);
133 int openat_report_new(int dirfd
, const char *pathname
, int flags
, mode_t mode
, bool *ret_newly_created
);
135 typedef enum XOpenFlags
{
137 XO_SUBVOLUME
= 1 << 1,
140 int xopenat(int dir_fd
, const char *path
, int open_flags
, XOpenFlags xopen_flags
, mode_t mode
);
142 int xopenat_lock(int dir_fd
, const char *path
, int open_flags
, XOpenFlags xopen_flags
, mode_t mode
, LockType locktype
, int operation
);