/src/systemd/src/basic/fs-util.h
Line | Count | Source (jump to first uncovered line) |
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 | | #include "user-util.h" |
17 | | |
18 | | #define MODE_INVALID ((mode_t) -1) |
19 | | |
20 | | /* The following macros add 1 when converting things, since 0 is a valid mode, while the pointer |
21 | | * NULL is special */ |
22 | | #define PTR_TO_MODE(p) ((mode_t) ((uintptr_t) (p)-1)) |
23 | | #define MODE_TO_PTR(u) ((void *) ((uintptr_t) (u)+1)) |
24 | | |
25 | | int unlink_noerrno(const char *path); |
26 | | |
27 | | int rmdir_parents(const char *path, const char *stop); |
28 | | |
29 | | int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath); |
30 | | |
31 | | int readlinkat_malloc(int fd, const char *p, char **ret); |
32 | | int readlink_malloc(const char *p, char **r); |
33 | | int readlink_value(const char *p, char **ret); |
34 | | int readlink_and_make_absolute(const char *p, char **r); |
35 | | |
36 | | int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid); |
37 | | int fchmod_and_chown_with_fallback(int fd, const char *path, mode_t mode, uid_t uid, gid_t gid); |
38 | 0 | static inline int fchmod_and_chown(int fd, mode_t mode, uid_t uid, gid_t gid) { |
39 | 0 | return fchmod_and_chown_with_fallback(fd, NULL, mode, uid, gid); /* no fallback */ |
40 | 0 | } |
41 | | |
42 | | int fchmod_umask(int fd, mode_t mode); |
43 | | int fchmod_opath(int fd, mode_t m); |
44 | | |
45 | | int futimens_opath(int fd, const struct timespec ts[2]); |
46 | | |
47 | | int fd_warn_permissions(const char *path, int fd); |
48 | | int stat_warn_permissions(const char *path, const struct stat *st); |
49 | | |
50 | | #define laccess(path, mode) \ |
51 | | RET_NERRNO(faccessat(AT_FDCWD, (path), (mode), AT_SYMLINK_NOFOLLOW)) |
52 | | |
53 | | int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode); |
54 | | |
55 | 0 | static inline int touch(const char *path) { |
56 | 0 | return touch_file(path, false, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID); |
57 | 0 | } |
58 | | |
59 | | int symlink_idempotent(const char *from, const char *to, bool make_relative); |
60 | | |
61 | | int symlinkat_atomic_full(const char *from, int atfd, const char *to, bool make_relative); |
62 | 0 | static inline int symlink_atomic(const char *from, const char *to) { |
63 | 0 | return symlinkat_atomic_full(from, AT_FDCWD, to, false); |
64 | 0 | } |
65 | | |
66 | | int mknodat_atomic(int atfd, const char *path, mode_t mode, dev_t dev); |
67 | 0 | static inline int mknod_atomic(const char *path, mode_t mode, dev_t dev) { |
68 | 0 | return mknodat_atomic(AT_FDCWD, path, mode, dev); |
69 | 0 | } |
70 | | |
71 | | int mkfifoat_atomic(int dir_fd, const char *path, mode_t mode); |
72 | 0 | static inline int mkfifo_atomic(const char *path, mode_t mode) { |
73 | 0 | return mkfifoat_atomic(AT_FDCWD, path, mode); |
74 | 0 | } |
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 | 0 | static inline char *rmdir_and_free(char *p) { |
85 | 0 | PROTECT_ERRNO; |
86 | 0 |
|
87 | 0 | if (!p) |
88 | 0 | return NULL; |
89 | 0 |
|
90 | 0 | (void) rmdir(p); |
91 | 0 | return mfree(p); |
92 | 0 | } |
93 | | DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rmdir_and_free); |
94 | | |
95 | 0 | static inline char* unlink_and_free(char *p) { |
96 | 0 | if (!p) |
97 | 0 | return NULL; |
98 | 0 |
|
99 | 0 | (void) unlink_noerrno(p); |
100 | 0 | return mfree(p); |
101 | 0 | } |
102 | | DEFINE_TRIVIAL_CLEANUP_FUNC(char*, unlink_and_free); |
103 | | |
104 | | int access_fd(int fd, int mode); |
105 | | |
106 | | void unlink_tempfilep(char (*p)[]); |
107 | | |
108 | | typedef enum UnlinkDeallocateFlags { |
109 | | UNLINK_REMOVEDIR = 1 << 0, |
110 | | UNLINK_ERASE = 1 << 1, |
111 | | } UnlinkDeallocateFlags; |
112 | | |
113 | | int unlinkat_deallocate(int fd, const char *name, UnlinkDeallocateFlags flags); |
114 | | |
115 | | int open_parent(const char *path, int flags, mode_t mode); |
116 | | |
117 | | int conservative_renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath); |
118 | 0 | static inline int conservative_rename(const char *oldpath, const char *newpath) { |
119 | 0 | return conservative_renameat(AT_FDCWD, oldpath, AT_FDCWD, newpath); |
120 | 0 | } |
121 | | |
122 | | int posix_fallocate_loop(int fd, uint64_t offset, uint64_t size); |
123 | | |
124 | | int parse_cifs_service(const char *s, char **ret_host, char **ret_service, char **ret_path); |
125 | | |
126 | | int open_mkdir_at(int dirfd, const char *path, int flags, mode_t mode); |
127 | | |
128 | | int openat_report_new(int dirfd, const char *pathname, int flags, mode_t mode, bool *ret_newly_created); |