chiark / gitweb /
fs-util: add calls that combine chase_symlinks() and open()/opendir() in one
[elogind.git] / src / basic / fs-util.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5   This file is part of systemd.
6
7   Copyright 2010 Lennart Poettering
8
9   systemd is free software; you can redistribute it and/or modify it
10   under the terms of the GNU Lesser General Public License as published by
11   the Free Software Foundation; either version 2.1 of the License, or
12   (at your option) any later version.
13
14   systemd is distributed in the hope that it will be useful, but
15   WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   Lesser General Public License for more details.
18
19   You should have received a copy of the GNU Lesser General Public License
20   along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23 //#include <dirent.h>
24 #include <fcntl.h>
25 #include <limits.h>
26 #include <stdbool.h>
27 #include <stdint.h>
28 #include <sys/inotify.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31
32 #include "time-util.h"
33 #include "util.h"
34
35 int unlink_noerrno(const char *path);
36
37 #if 0 /// UNNEEDED by elogind
38 int rmdir_parents(const char *path, const char *stop);
39
40 int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
41 #endif // 0
42
43 int readlinkat_malloc(int fd, const char *p, char **ret);
44 int readlink_malloc(const char *p, char **r);
45 #if 0 /// UNNEEDED by elogind
46 int readlink_value(const char *p, char **ret);
47 #endif // 0
48 int readlink_and_make_absolute(const char *p, char **r);
49 #if 0 /// UNNEEDED by elogind
50 #endif // 0
51
52 int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
53
54 int fchmod_umask(int fd, mode_t mode);
55
56 int fd_warn_permissions(const char *path, int fd);
57
58 #ifdef __GLIBC__
59 #define laccess(path, mode) faccessat(AT_FDCWD, (path), (mode), AT_SYMLINK_NOFOLLOW)
60 #else
61 #define laccess(path, mode) faccessat(AT_FDCWD, (path), (mode), 0)
62 #endif // __GLIBC__
63
64 int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode);
65 int touch(const char *path);
66
67 #if 0 /// UNNEEDED by elogind
68 int symlink_idempotent(const char *from, const char *to);
69
70 int symlink_atomic(const char *from, const char *to);
71 int mknod_atomic(const char *path, mode_t mode, dev_t dev);
72 int mkfifo_atomic(const char *path, mode_t mode);
73 #endif // 0
74
75 int get_files_in_directory(const char *path, char ***list);
76
77 int tmp_dir(const char **ret);
78 #if 0 /// UNNEEDED by elogind
79 int var_tmp_dir(const char **ret);
80 #endif // 0
81
82 int unlink_or_warn(const char *filename);
83
84 #if 0 /// UNNEEDED by elogind
85 #define INOTIFY_EVENT_MAX (sizeof(struct inotify_event) + NAME_MAX + 1)
86
87 #define FOREACH_INOTIFY_EVENT(e, buffer, sz) \
88         for ((e) = &buffer.ev;                                \
89              (uint8_t*) (e) < (uint8_t*) (buffer.raw) + (sz); \
90              (e) = (struct inotify_event*) ((uint8_t*) (e) + sizeof(struct inotify_event) + (e)->len))
91
92 union inotify_event_buffer {
93         struct inotify_event ev;
94         uint8_t raw[INOTIFY_EVENT_MAX];
95 };
96
97 int inotify_add_watch_fd(int fd, int what, uint32_t mask);
98
99 #endif // 0
100 enum {
101         CHASE_PREFIX_ROOT = 1U << 0,   /* If set, the specified path will be prefixed by the specified root before beginning the iteration */
102         CHASE_NONEXISTENT = 1U << 1,   /* If set, it's OK if the path doesn't actually exist. */
103         CHASE_NO_AUTOFS   = 1U << 2,   /* If set, return -EREMOTE if autofs mount point found */
104         CHASE_SAFE        = 1U << 3,   /* If set, return EPERM if we ever traverse from unprivileged to privileged files or directories */
105         CHASE_OPEN        = 1U << 4,   /* If set, return an O_PATH object to the final component */
106         CHASE_TRAIL_SLASH = 1U << 5,   /* If set, any trailing slash will be preserved */
107 };
108
109 int chase_symlinks(const char *path_with_prefix, const char *root, unsigned flags, char **ret);
110
111 int chase_symlinks_and_open(const char *path, const char *root, unsigned chase_flags, int open_flags, char **ret_path);
112 int chase_symlinks_and_opendir(const char *path, const char *root, unsigned chase_flags, char **ret_path, DIR **ret_dir);
113
114 /* Useful for usage with _cleanup_(), removes a directory and frees the pointer */
115 static inline void rmdir_and_free(char *p) {
116         PROTECT_ERRNO;
117         (void) rmdir(p);
118         free(p);
119 }
120 DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rmdir_and_free);
121
122 #if 0 /// UNNEEDED by elogind
123 static inline void unlink_and_free(char *p) {
124         (void) unlink_noerrno(p);
125         free(p);
126 }
127 DEFINE_TRIVIAL_CLEANUP_FUNC(char*, unlink_and_free);
128 #endif // 0
129
130 int access_fd(int fd, int mode);
131
132 int unlinkat_deallocate(int fd, const char *name, int flags);
133
134 int fsync_directory_of_file(int fd);