]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/fs-util.h
stat-util: unify code that checks whether something is a regular file
[thirdparty/systemd.git] / src / basic / fs-util.h
CommitLineData
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
34int unlink_noerrno(const char *path);
35
36int rmdir_parents(const char *path, const char *stop);
37
38int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
39
40int readlinkat_malloc(int fd, const char *p, char **ret);
41int readlink_malloc(const char *p, char **r);
42int readlink_value(const char *p, char **ret);
43int readlink_and_make_absolute(const char *p, char **r);
f4f15635
LP
44
45int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
f4f15635
LP
46
47int fchmod_umask(int fd, mode_t mode);
48
49int fd_warn_permissions(const char *path, int fd);
50
51#define laccess(path, mode) faccessat(AT_FDCWD, (path), (mode), AT_SYMLINK_NOFOLLOW)
52
53int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode);
54int touch(const char *path);
55
56int symlink_idempotent(const char *from, const char *to);
57
58int symlink_atomic(const char *from, const char *to);
59int mknod_atomic(const char *path, mode_t mode, dev_t dev);
60int mkfifo_atomic(const char *path, mode_t mode);
61
62int get_files_in_directory(const char *path, char ***list);
77601719 63
992e8f22
LP
64int tmp_dir(const char **ret);
65int var_tmp_dir(const char **ret);
34a8f081 66
af229d7a
ZJS
67int 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
76union inotify_event_buffer {
77 struct inotify_event ev;
78 uint8_t raw[INOTIFY_EVENT_MAX];
79};
430fbf8e
LP
80
81int inotify_add_watch_fd(int fd, int what, uint32_t mask);
d944dc95 82
c4f4fce7 83enum {
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 */
c4f4fce7
LP
89};
90
91int chase_symlinks(const char *path_with_prefix, const char *root, unsigned flags, char **ret);
676bcb0f
LP
92
93/* Useful for usage with _cleanup_(), removes a directory and frees the pointer */
94static inline void rmdir_and_free(char *p) {
dfd14786 95 PROTECT_ERRNO;
676bcb0f
LP
96 (void) rmdir(p);
97 free(p);
98}
99DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rmdir_and_free);
f5b84de2
LP
100
101static inline void unlink_and_free(char *p) {
dfd14786 102 (void) unlink_noerrno(p);
f5b84de2
LP
103 free(p);
104}
105DEFINE_TRIVIAL_CLEANUP_FUNC(char*, unlink_and_free);
57a4359e
LP
106
107int access_fd(int fd, int mode);
43767d9d
LP
108
109int unlinkat_deallocate(int fd, const char *name, int flags);