]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/fs-util.h
Add SPDX license identifiers to source files under the LGPL
[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"
32
33int unlink_noerrno(const char *path);
34
35int rmdir_parents(const char *path, const char *stop);
36
37int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
38
39int readlinkat_malloc(int fd, const char *p, char **ret);
40int readlink_malloc(const char *p, char **r);
41int readlink_value(const char *p, char **ret);
42int readlink_and_make_absolute(const char *p, char **r);
e1873695 43int readlink_and_canonicalize(const char *p, const char *root, char **r);
0ec0deaa 44int readlink_and_make_absolute_root(const char *root, const char *path, char **ret);
f4f15635
LP
45
46int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
f4f15635
LP
47
48int fchmod_umask(int fd, mode_t mode);
49
50int fd_warn_permissions(const char *path, int fd);
51
52#define laccess(path, mode) faccessat(AT_FDCWD, (path), (mode), AT_SYMLINK_NOFOLLOW)
53
54int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode);
55int touch(const char *path);
56
57int symlink_idempotent(const char *from, const char *to);
58
59int symlink_atomic(const char *from, const char *to);
60int mknod_atomic(const char *path, mode_t mode, dev_t dev);
61int mkfifo_atomic(const char *path, mode_t mode);
62
63int get_files_in_directory(const char *path, char ***list);
77601719 64
992e8f22
LP
65int tmp_dir(const char **ret);
66int var_tmp_dir(const char **ret);
34a8f081 67
77601719
LP
68#define INOTIFY_EVENT_MAX (sizeof(struct inotify_event) + NAME_MAX + 1)
69
70#define FOREACH_INOTIFY_EVENT(e, buffer, sz) \
71 for ((e) = &buffer.ev; \
72 (uint8_t*) (e) < (uint8_t*) (buffer.raw) + (sz); \
73 (e) = (struct inotify_event*) ((uint8_t*) (e) + sizeof(struct inotify_event) + (e)->len))
74
75union inotify_event_buffer {
76 struct inotify_event ev;
77 uint8_t raw[INOTIFY_EVENT_MAX];
78};
430fbf8e
LP
79
80int inotify_add_watch_fd(int fd, int what, uint32_t mask);
d944dc95 81
c4f4fce7
LP
82enum {
83 CHASE_PREFIX_ROOT = 1, /* If set, the specified path will be prefixed by the specified root before beginning the iteration */
cb638b5e 84 CHASE_NONEXISTENT = 2, /* If set, it's OK if the path doesn't actually exist. */
655f2da0 85 CHASE_NO_AUTOFS = 4, /* If set, return -EREMOTE if autofs mount point found */
c4f4fce7
LP
86};
87
88int chase_symlinks(const char *path_with_prefix, const char *root, unsigned flags, char **ret);
676bcb0f
LP
89
90/* Useful for usage with _cleanup_(), removes a directory and frees the pointer */
91static inline void rmdir_and_free(char *p) {
92 (void) rmdir(p);
93 free(p);
94}
95DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rmdir_and_free);
f5b84de2
LP
96
97static inline void unlink_and_free(char *p) {
98 (void) unlink(p);
99 free(p);
100}
101DEFINE_TRIVIAL_CLEANUP_FUNC(char*, unlink_and_free);
57a4359e
LP
102
103int access_fd(int fd, int mode);