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>
# include <sys/types.h>
2015-10-26 21:16:26 +01:00
# include <unistd.h>
# include "time-util.h"
2018-01-10 17:21:15 +01:00
# include "util.h"
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
int fd_warn_permissions ( const char * path , int fd ) ;
# 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 ) ;
2016-09-24 12:41:30 +02:00
2016-11-29 16:49:30 +01:00
enum {
2018-06-07 16:03:43 +02:00
CHASE_PREFIX_ROOT = 1 < < 0 , /* If set, the specified path will be prefixed by the specified root before beginning the iteration */
CHASE_NONEXISTENT = 1 < < 1 , /* If set, it's OK if the path doesn't actually exist. */
CHASE_NO_AUTOFS = 1 < < 2 , /* If set, return -EREMOTE if autofs mount point found */
CHASE_SAFE = 1 < < 3 , /* If set, return EPERM if we ever traverse from unprivileged to privileged files or directories */
CHASE_OPEN = 1 < < 4 , /* If set, return an O_PATH object to the final component */
CHASE_TRAIL_SLASH = 1 < < 5 , /* If set, any trailing slash will be preserved */
CHASE_STEP = 1 < < 6 , /* If set, just execute a single step of the normalization */
2018-04-26 22:46:55 +02:00
CHASE_NOFOLLOW = 1 < < 7 , /* Only valid with CHASE_OPEN: when the path's right-most component refers to symlink return O_PATH fd of the symlink, rather than following it. */
2018-11-29 11:08:52 +01:00
CHASE_WARN = 1 < < 8 , /* 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
2016-11-29 16:49:30 +01:00
int chase_symlinks ( const char * path_with_prefix , const char * root , unsigned flags , char * * ret ) ;
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 ) ;
2018-04-18 16:19:46 +02:00
int chase_symlinks_and_stat ( const char * path , const char * root , unsigned chase_flags , char * * ret_path , struct stat * ret_stat ) ;
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 ) [ ] ) ;
2018-02-09 09:50:31 +01:00
int unlinkat_deallocate ( int fd , const char * name , int flags ) ;
2018-02-19 18:23:38 +01:00
int fsync_directory_of_file ( 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
int open_parent ( const char * path , int flags , mode_t mode ) ;