]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
f4f15635 LP |
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 | ||
f4f15635 | 23 | #include <fcntl.h> |
71d35b6b | 24 | #include <limits.h> |
11c3a366 TA |
25 | #include <stdbool.h> |
26 | #include <stdint.h> | |
77601719 LP |
27 | #include <sys/inotify.h> |
28 | #include <sys/types.h> | |
f4f15635 LP |
29 | #include <unistd.h> |
30 | ||
31 | #include "time-util.h" | |
dfd14786 | 32 | #include "util.h" |
f4f15635 LP |
33 | |
34 | int unlink_noerrno(const char *path); | |
35 | ||
36 | int rmdir_parents(const char *path, const char *stop); | |
37 | ||
38 | int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath); | |
39 | ||
40 | int readlinkat_malloc(int fd, const char *p, char **ret); | |
41 | int readlink_malloc(const char *p, char **r); | |
42 | int readlink_value(const char *p, char **ret); | |
43 | int readlink_and_make_absolute(const char *p, char **r); | |
f4f15635 LP |
44 | |
45 | int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid); | |
f4f15635 LP |
46 | |
47 | int fchmod_umask(int fd, mode_t mode); | |
48 | ||
49 | int fd_warn_permissions(const char *path, int fd); | |
50 | ||
51 | #define laccess(path, mode) 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 | int touch(const char *path); | |
55 | ||
56 | int symlink_idempotent(const char *from, const char *to); | |
57 | ||
58 | int symlink_atomic(const char *from, const char *to); | |
59 | int mknod_atomic(const char *path, mode_t mode, dev_t dev); | |
60 | int mkfifo_atomic(const char *path, mode_t mode); | |
61 | ||
62 | int get_files_in_directory(const char *path, char ***list); | |
77601719 | 63 | |
992e8f22 LP |
64 | int tmp_dir(const char **ret); |
65 | int var_tmp_dir(const char **ret); | |
34a8f081 | 66 | |
af229d7a ZJS |
67 | int unlink_or_warn(const char *filename); |
68 | ||
77601719 LP |
69 | #define INOTIFY_EVENT_MAX (sizeof(struct inotify_event) + NAME_MAX + 1) |
70 | ||
71 | #define FOREACH_INOTIFY_EVENT(e, buffer, sz) \ | |
72 | for ((e) = &buffer.ev; \ | |
73 | (uint8_t*) (e) < (uint8_t*) (buffer.raw) + (sz); \ | |
74 | (e) = (struct inotify_event*) ((uint8_t*) (e) + sizeof(struct inotify_event) + (e)->len)) | |
75 | ||
76 | union inotify_event_buffer { | |
77 | struct inotify_event ev; | |
78 | uint8_t raw[INOTIFY_EVENT_MAX]; | |
79 | }; | |
430fbf8e LP |
80 | |
81 | int inotify_add_watch_fd(int fd, int what, uint32_t mask); | |
d944dc95 | 82 | |
c4f4fce7 | 83 | enum { |
f14f1806 LP |
84 | CHASE_PREFIX_ROOT = 1U << 0, /* If set, the specified path will be prefixed by the specified root before beginning the iteration */ |
85 | CHASE_NONEXISTENT = 1U << 1, /* If set, it's OK if the path doesn't actually exist. */ | |
86 | CHASE_NO_AUTOFS = 1U << 2, /* If set, return -EREMOTE if autofs mount point found */ | |
87 | CHASE_SAFE = 1U << 3, /* If set, return EPERM if we ever traverse from unprivileged to privileged files or directories */ | |
1ed34d75 | 88 | CHASE_OPEN = 1U << 4, /* If set, return an O_PATH object to the final component */ |
62570f6f | 89 | CHASE_TRAIL_SLASH = 1U << 5, /* If set, any trailing slash will be preserved */ |
c4f4fce7 LP |
90 | }; |
91 | ||
92 | int chase_symlinks(const char *path_with_prefix, const char *root, unsigned flags, char **ret); | |
676bcb0f LP |
93 | |
94 | /* Useful for usage with _cleanup_(), removes a directory and frees the pointer */ | |
95 | static inline void rmdir_and_free(char *p) { | |
dfd14786 | 96 | PROTECT_ERRNO; |
676bcb0f LP |
97 | (void) rmdir(p); |
98 | free(p); | |
99 | } | |
100 | DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rmdir_and_free); | |
f5b84de2 LP |
101 | |
102 | static inline void unlink_and_free(char *p) { | |
dfd14786 | 103 | (void) unlink_noerrno(p); |
f5b84de2 LP |
104 | free(p); |
105 | } | |
106 | DEFINE_TRIVIAL_CLEANUP_FUNC(char*, unlink_and_free); | |
57a4359e LP |
107 | |
108 | int access_fd(int fd, int mode); | |
43767d9d LP |
109 | |
110 | int unlinkat_deallocate(int fd, const char *name, int flags); | |
11b29a96 LP |
111 | |
112 | int fsync_directory_of_file(int fd); |