2012-02-29 08:58:51 -06:00
|
|
|
#ifndef UTIL_LINUX_FILEUTILS
|
|
|
|
#define UTIL_LINUX_FILEUTILS
|
|
|
|
|
2014-12-05 08:30:04 -06:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2019-04-11 06:11:53 -05:00
|
|
|
#include <dirent.h>
|
2015-10-31 12:26:51 -05:00
|
|
|
#include <sys/stat.h>
|
2014-12-05 08:30:04 -06:00
|
|
|
|
|
|
|
#include "c.h"
|
|
|
|
|
2016-02-27 06:28:04 -06:00
|
|
|
extern int mkstemp_cloexec(char *template);
|
|
|
|
|
2015-08-24 03:05:55 -05:00
|
|
|
extern int xmkstemp(char **tmpname, const char *dir, const char *prefix);
|
2012-02-29 08:58:51 -06:00
|
|
|
|
2015-08-24 03:05:55 -05:00
|
|
|
static inline FILE *xfmkstemp(char **tmpname, const char *dir, const char *prefix)
|
2012-03-10 05:29:35 -06:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
FILE *ret;
|
2014-06-18 05:57:42 -05:00
|
|
|
|
2015-08-24 03:05:55 -05:00
|
|
|
fd = xmkstemp(tmpname, dir, prefix);
|
2014-06-18 05:57:42 -05:00
|
|
|
if (fd == -1)
|
2012-03-10 05:29:35 -06:00
|
|
|
return NULL;
|
2014-06-18 05:57:42 -05:00
|
|
|
|
2013-04-03 09:12:34 -05:00
|
|
|
if (!(ret = fdopen(fd, "w+" UL_CLOEXECSTR))) {
|
2012-03-10 05:29:35 -06:00
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2012-04-23 06:52:41 -05:00
|
|
|
|
2016-02-29 10:45:46 -06:00
|
|
|
#ifdef HAVE_OPENAT
|
2016-02-29 05:49:13 -06:00
|
|
|
static inline FILE *fopen_at(int dir, const char *filename,
|
|
|
|
int flags, const char *mode)
|
|
|
|
{
|
|
|
|
int fd = openat(dir, filename, flags);
|
2021-05-05 23:39:30 -05:00
|
|
|
FILE *ret;
|
|
|
|
|
2016-02-29 05:49:13 -06:00
|
|
|
if (fd < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2021-05-05 23:39:30 -05:00
|
|
|
ret = fdopen(fd, mode);
|
|
|
|
if (!ret)
|
|
|
|
close(fd);
|
|
|
|
return ret;
|
2016-02-29 05:49:13 -06:00
|
|
|
}
|
2016-02-29 10:45:46 -06:00
|
|
|
#endif
|
2016-02-29 05:49:13 -06:00
|
|
|
|
2015-10-31 12:26:51 -05: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-05 23:19:05 -05:00
|
|
|
extern int dup_fd_cloexec(int oldfd, int lowfd);
|
2020-12-02 16:56:16 -06:00
|
|
|
extern unsigned int get_fd_tabsize(void);
|
2012-04-23 06:52:41 -05:00
|
|
|
|
2021-01-28 03:27:21 -06:00
|
|
|
extern int ul_mkdir_p(const char *path, mode_t mode);
|
2014-06-09 04:54:32 -05:00
|
|
|
extern char *stripoff_last_component(char *path);
|
2014-06-09 03:59:18 -05:00
|
|
|
|
2019-04-11 06:11:53 -05: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 06:55:10 -06:00
|
|
|
#if defined(__linux__)
|
|
|
|
# include <sys/syscall.h>
|
|
|
|
# if defined(SYS_close_range)
|
|
|
|
# include <sys/types.h>
|
|
|
|
# ifndef HAVE_CLOSE_RANGE
|
|
|
|
static inline int close_range(unsigned int first, unsigned int last)
|
|
|
|
{
|
|
|
|
return syscall(SYS_close_range, first, last);
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
# define HAVE_CLOSE_RANGE 1
|
|
|
|
# endif /* SYS_close_range */
|
|
|
|
#endif /* __linux__ */
|
|
|
|
|
2020-12-02 16:56:16 -06:00
|
|
|
#ifndef HAVE_CLOSE_RANGE
|
2021-01-28 03:27:21 -06:00
|
|
|
extern void ul_close_all_fds(unsigned int first, unsigned int last);
|
2020-12-02 16:56:16 -06:00
|
|
|
#endif
|
2019-10-17 03:36:27 -05:00
|
|
|
|
2020-11-06 18:19:56 -06:00
|
|
|
#define UL_COPY_READ_ERROR (-1)
|
|
|
|
#define UL_COPY_WRITE_ERROR (-2)
|
2020-11-06 01:45:18 -06:00
|
|
|
int ul_copy_file(int from, int to);
|
|
|
|
|
2012-04-23 06:52:41 -05:00
|
|
|
#endif /* UTIL_LINUX_FILEUTILS */
|