2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2015-10-26 21:16:26 +01:00
|
|
|
#pragma once
|
|
|
|
|
2018-03-26 14:15:43 +02:00
|
|
|
#include <dirent.h>
|
2015-10-26 21:16:26 +01:00
|
|
|
#include <fcntl.h>
|
2015-11-18 22:46:33 +01:00
|
|
|
#include <limits.h>
|
2015-11-30 21:43:37 +01:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
2015-10-27 14:58:05 +01:00
|
|
|
#include <sys/inotify.h>
|
2019-04-18 10:47:11 +02:00
|
|
|
#include <sys/stat.h>
|
2015-10-27 14:58:05 +01:00
|
|
|
#include <sys/types.h>
|
2015-10-26 21:16:26 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2019-03-14 12:24:39 +01:00
|
|
|
#include "errno-util.h"
|
2015-10-26 21:16:26 +01:00
|
|
|
#include "time-util.h"
|
|
|
|
|
2019-04-25 01:19:07 +02:00
|
|
|
#define MODE_INVALID ((mode_t) -1)
|
|
|
|
|
|
|
|
/* The following macros add 1 when converting things, since 0 is a valid mode, while the pointer
|
|
|
|
* NULL is special */
|
|
|
|
#define PTR_TO_MODE(p) ((mode_t) ((uintptr_t) (p)-1))
|
|
|
|
#define MODE_TO_PTR(u) ((void *) ((uintptr_t) (u)+1))
|
|
|
|
|
2015-10-26 21:16:26 +01:00
|
|
|
int unlink_noerrno(const char *path);
|
|
|
|
|
|
|
|
int rmdir_parents(const char *path, const char *stop);
|
|
|
|
|
|
|
|
int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
|
|
|
|
|
|
|
|
int readlinkat_malloc(int fd, const char *p, char **ret);
|
|
|
|
int readlink_malloc(const char *p, char **r);
|
|
|
|
int readlink_value(const char *p, char **ret);
|
|
|
|
int readlink_and_make_absolute(const char *p, char **r);
|
|
|
|
|
|
|
|
int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
|
2018-06-14 04:26:29 +02:00
|
|
|
int fchmod_and_chown(int fd, mode_t mode, uid_t uid, gid_t gid);
|
2015-10-26 21:16:26 +01:00
|
|
|
|
|
|
|
int fchmod_umask(int fd, mode_t mode);
|
2018-04-11 16:58:49 +02:00
|
|
|
int fchmod_opath(int fd, mode_t m);
|
2015-10-26 21:16:26 +01:00
|
|
|
|
2020-09-25 16:40:02 +02:00
|
|
|
int futimens_opath(int fd, const struct timespec ts[2]);
|
|
|
|
|
2015-10-26 21:16:26 +01:00
|
|
|
int fd_warn_permissions(const char *path, int fd);
|
2020-06-02 16:44:34 +02:00
|
|
|
int stat_warn_permissions(const char *path, const struct stat *st);
|
2015-10-26 21:16:26 +01:00
|
|
|
|
|
|
|
#define laccess(path, mode) faccessat(AT_FDCWD, (path), (mode), AT_SYMLINK_NOFOLLOW)
|
|
|
|
|
|
|
|
int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode);
|
|
|
|
int touch(const char *path);
|
|
|
|
|
2018-09-23 09:17:03 +02:00
|
|
|
int symlink_idempotent(const char *from, const char *to, bool make_relative);
|
2015-10-26 21:16:26 +01:00
|
|
|
|
|
|
|
int symlink_atomic(const char *from, const char *to);
|
|
|
|
int mknod_atomic(const char *path, mode_t mode, dev_t dev);
|
|
|
|
int mkfifo_atomic(const char *path, mode_t mode);
|
2018-04-27 18:20:38 +02:00
|
|
|
int mkfifoat_atomic(int dir_fd, const char *path, mode_t mode);
|
2015-10-26 21:16:26 +01:00
|
|
|
|
|
|
|
int get_files_in_directory(const char *path, char ***list);
|
2015-10-27 14:58:05 +01:00
|
|
|
|
2016-07-26 17:23:28 +02:00
|
|
|
int tmp_dir(const char **ret);
|
|
|
|
int var_tmp_dir(const char **ret);
|
2016-06-30 16:59:06 +02:00
|
|
|
|
2018-02-16 06:58:33 +01:00
|
|
|
int unlink_or_warn(const char *filename);
|
|
|
|
|
2015-10-27 14:58:05 +01:00
|
|
|
#define INOTIFY_EVENT_MAX (sizeof(struct inotify_event) + NAME_MAX + 1)
|
|
|
|
|
|
|
|
#define FOREACH_INOTIFY_EVENT(e, buffer, sz) \
|
|
|
|
for ((e) = &buffer.ev; \
|
|
|
|
(uint8_t*) (e) < (uint8_t*) (buffer.raw) + (sz); \
|
|
|
|
(e) = (struct inotify_event*) ((uint8_t*) (e) + sizeof(struct inotify_event) + (e)->len))
|
|
|
|
|
|
|
|
union inotify_event_buffer {
|
|
|
|
struct inotify_event ev;
|
|
|
|
uint8_t raw[INOTIFY_EVENT_MAX];
|
|
|
|
};
|
2016-04-25 00:18:27 +02:00
|
|
|
|
|
|
|
int inotify_add_watch_fd(int fd, int what, uint32_t mask);
|
2019-09-17 11:16:52 +02:00
|
|
|
int inotify_add_watch_and_warn(int fd, const char *pathname, uint32_t mask);
|
2016-09-24 12:41:30 +02:00
|
|
|
|
2016-11-29 16:49:30 +01:00
|
|
|
enum {
|
2019-07-18 13:14:17 +02:00
|
|
|
CHASE_PREFIX_ROOT = 1 << 0, /* The specified path will be prefixed by the specified root before beginning the iteration */
|
|
|
|
CHASE_NONEXISTENT = 1 << 1, /* It's OK if the path doesn't actually exist. */
|
|
|
|
CHASE_NO_AUTOFS = 1 << 2, /* Return -EREMOTE if autofs mount point found */
|
2020-10-01 13:52:16 +02:00
|
|
|
CHASE_SAFE = 1 << 3, /* Return -EPERM if we ever traverse from unprivileged to privileged files or directories */
|
2019-10-24 10:33:20 +02:00
|
|
|
CHASE_TRAIL_SLASH = 1 << 4, /* Any trailing slash will be preserved */
|
|
|
|
CHASE_STEP = 1 << 5, /* Just execute a single step of the normalization */
|
2020-04-16 17:50:21 +02:00
|
|
|
CHASE_NOFOLLOW = 1 << 6, /* Do not follow the path's right-most component. With ret_fd, when the path's
|
2019-10-24 10:33:20 +02:00
|
|
|
* right-most component refers to symlink, return O_PATH fd of the symlink. */
|
|
|
|
CHASE_WARN = 1 << 7, /* Emit an appropriate warning when an error is encountered */
|
2016-11-29 16:49:30 +01:00
|
|
|
};
|
|
|
|
|
2018-04-04 17:06:12 +02:00
|
|
|
/* How many iterations to execute before returning -ELOOP */
|
|
|
|
#define CHASE_SYMLINKS_MAX 32
|
|
|
|
|
2019-10-24 10:33:20 +02:00
|
|
|
int chase_symlinks(const char *path_with_prefix, const char *root, unsigned flags, char **ret_path, int *ret_fd);
|
2016-12-01 23:19:31 +01:00
|
|
|
|
2018-03-26 14:15:43 +02:00
|
|
|
int chase_symlinks_and_open(const char *path, const char *root, unsigned chase_flags, int open_flags, char **ret_path);
|
|
|
|
int chase_symlinks_and_opendir(const char *path, const char *root, unsigned chase_flags, char **ret_path, DIR **ret_dir);
|
2019-10-24 10:33:20 +02:00
|
|
|
int chase_symlinks_and_stat(const char *path, const char *root, unsigned chase_flags, char **ret_path, struct stat *ret_stat, int *ret_fd);
|
2018-03-26 14:15:43 +02:00
|
|
|
|
2016-12-01 23:19:31 +01:00
|
|
|
/* Useful for usage with _cleanup_(), removes a directory and frees the pointer */
|
|
|
|
static inline void rmdir_and_free(char *p) {
|
2018-01-10 17:21:15 +01:00
|
|
|
PROTECT_ERRNO;
|
2016-12-01 23:19:31 +01:00
|
|
|
(void) rmdir(p);
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rmdir_and_free);
|
2017-02-21 17:44:35 +01:00
|
|
|
|
|
|
|
static inline void unlink_and_free(char *p) {
|
2018-01-10 17:21:15 +01:00
|
|
|
(void) unlink_noerrno(p);
|
2017-02-21 17:44:35 +01:00
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
DEFINE_TRIVIAL_CLEANUP_FUNC(char*, unlink_and_free);
|
2017-11-16 18:56:25 +01:00
|
|
|
|
|
|
|
int access_fd(int fd, int mode);
|
2018-02-09 09:50:31 +01:00
|
|
|
|
2018-05-16 11:35:41 +02:00
|
|
|
void unlink_tempfilep(char (*p)[]);
|
2020-04-29 13:58:53 +02:00
|
|
|
|
|
|
|
typedef enum UnlinkDeallocateFlags {
|
|
|
|
UNLINK_REMOVEDIR = 1 << 0,
|
|
|
|
UNLINK_ERASE = 1 << 1,
|
|
|
|
} UnlinkDeallocateFlags;
|
|
|
|
|
|
|
|
int unlinkat_deallocate(int fd, const char *name, UnlinkDeallocateFlags flags);
|
2018-02-19 18:23:38 +01:00
|
|
|
|
|
|
|
int fsync_directory_of_file(int fd);
|
2019-07-22 13:48:12 +02:00
|
|
|
int fsync_full(int fd);
|
2018-06-25 17:24:09 +02:00
|
|
|
int fsync_path_at(int at_fd, const char *path);
|
2018-07-20 11:57:24 +02:00
|
|
|
|
2019-01-23 16:08:55 +01:00
|
|
|
int syncfs_path(int atfd, const char *path);
|
|
|
|
|
2018-07-20 11:57:24 +02:00
|
|
|
int open_parent(const char *path, int flags, mode_t mode);
|
2020-05-01 19:37:24 +02:00
|
|
|
|
|
|
|
int path_is_encrypted(const char *path);
|