2012-02-29 15:58:51 +01:00
|
|
|
#ifndef UTIL_LINUX_FILEUTILS
|
|
|
|
#define UTIL_LINUX_FILEUTILS
|
|
|
|
|
2014-12-05 15:30:04 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2019-04-11 13:11:53 +02:00
|
|
|
#include <dirent.h>
|
2015-10-31 17:26:51 +00:00
|
|
|
#include <sys/stat.h>
|
2014-12-05 15:30:04 +01:00
|
|
|
|
|
|
|
#include "c.h"
|
|
|
|
|
2016-02-27 13:28:04 +01:00
|
|
|
extern int mkstemp_cloexec(char *template);
|
|
|
|
|
2015-08-24 10:05:55 +02:00
|
|
|
extern int xmkstemp(char **tmpname, const char *dir, const char *prefix);
|
2012-02-29 15:58:51 +01:00
|
|
|
|
2015-08-24 10:05:55 +02:00
|
|
|
static inline FILE *xfmkstemp(char **tmpname, const char *dir, const char *prefix)
|
2012-03-10 12:29:35 +01:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
FILE *ret;
|
2014-06-18 12:57:42 +02:00
|
|
|
|
2015-08-24 10:05:55 +02:00
|
|
|
fd = xmkstemp(tmpname, dir, prefix);
|
2014-06-18 12:57:42 +02:00
|
|
|
if (fd == -1)
|
2012-03-10 12:29:35 +01:00
|
|
|
return NULL;
|
2014-06-18 12:57:42 +02:00
|
|
|
|
2013-04-03 16:12:34 +02:00
|
|
|
if (!(ret = fdopen(fd, "w+" UL_CLOEXECSTR))) {
|
2012-03-10 12:29:35 +01:00
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2012-04-23 13:52:41 +02:00
|
|
|
|
2016-02-29 17:45:46 +01:00
|
|
|
#ifdef HAVE_OPENAT
|
2016-02-29 12:49:13 +01:00
|
|
|
static inline FILE *fopen_at(int dir, const char *filename,
|
|
|
|
int flags, const char *mode)
|
|
|
|
{
|
|
|
|
int fd = openat(dir, filename, flags);
|
2021-05-06 13:39:30 +09:00
|
|
|
FILE *ret;
|
|
|
|
|
2016-02-29 12:49:13 +01:00
|
|
|
if (fd < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2021-05-06 13:39:30 +09:00
|
|
|
ret = fdopen(fd, mode);
|
|
|
|
if (!ret)
|
|
|
|
close(fd);
|
|
|
|
return ret;
|
2016-02-29 12:49:13 +01:00
|
|
|
}
|
2016-02-29 17:45:46 +01:00
|
|
|
#endif
|
2016-02-29 12:49:13 +01:00
|
|
|
|
2015-10-31 17:26:51 +00:00
|
|
|
static inline int is_same_inode(const int fd, const struct stat *st)
|
|
|
|
{
|
|
|
|
struct stat f;
|
|
|
|
|
|
|
|
if (fstat(fd, &f) < 0)
|
|
|
|
return 0;
|
|
|
|
else if (f.st_dev != st->st_dev || f.st_ino != st->st_ino)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-06-06 06:19:05 +02:00
|
|
|
extern int dup_fd_cloexec(int oldfd, int lowfd);
|
2020-12-02 22:56:16 +00:00
|
|
|
extern unsigned int get_fd_tabsize(void);
|
2012-04-23 13:52:41 +02:00
|
|
|
|
2021-01-28 10:27:21 +01:00
|
|
|
extern int ul_mkdir_p(const char *path, mode_t mode);
|
2014-06-09 11:54:32 +02:00
|
|
|
extern char *stripoff_last_component(char *path);
|
2014-06-09 10:59:18 +02:00
|
|
|
|
2019-04-11 13:11:53 +02:00
|
|
|
/* This is readdir()-like function, but skips "." and ".." directory entries */
|
|
|
|
static inline struct dirent *xreaddir(DIR *dp)
|
|
|
|
{
|
|
|
|
struct dirent *d;
|
|
|
|
|
|
|
|
while ((d = readdir(dp))) {
|
|
|
|
if (!strcmp(d->d_name, ".") ||
|
|
|
|
!strcmp(d->d_name, ".."))
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2020-11-14 12:55:10 +00:00
|
|
|
#if defined(__linux__)
|
|
|
|
# include <sys/syscall.h>
|
|
|
|
# if defined(SYS_close_range)
|
|
|
|
# include <sys/types.h>
|
|
|
|
# ifndef HAVE_CLOSE_RANGE
|
2021-07-14 17:25:57 +02:00
|
|
|
static inline int close_range(unsigned int first, unsigned int last, int flags)
|
2020-11-14 12:55:10 +00:00
|
|
|
{
|
2021-07-14 17:25:57 +02:00
|
|
|
return syscall(SYS_close_range, first, last, flags);
|
2020-11-14 12:55:10 +00:00
|
|
|
}
|
|
|
|
# endif
|
|
|
|
# define HAVE_CLOSE_RANGE 1
|
|
|
|
# endif /* SYS_close_range */
|
|
|
|
#endif /* __linux__ */
|
|
|
|
|
2021-01-28 10:27:21 +01:00
|
|
|
extern void ul_close_all_fds(unsigned int first, unsigned int last);
|
2019-10-17 10:36:27 +02:00
|
|
|
|
2020-11-07 02:19:56 +02:00
|
|
|
#define UL_COPY_READ_ERROR (-1)
|
|
|
|
#define UL_COPY_WRITE_ERROR (-2)
|
2020-11-06 09:45:18 +02:00
|
|
|
int ul_copy_file(int from, int to);
|
|
|
|
|
2012-04-23 13:52:41 +02:00
|
|
|
#endif /* UTIL_LINUX_FILEUTILS */
|