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);
|
|
|
|
if (fd < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return fdopen(fd, mode);
|
|
|
|
}
|
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);
|
2012-04-23 13:52:41 +02:00
|
|
|
extern int get_fd_tabsize(void);
|
|
|
|
|
2014-06-09 10:59:18 +02:00
|
|
|
extern int 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;
|
|
|
|
}
|
|
|
|
|
2019-10-17 10:36:27 +02:00
|
|
|
extern void close_all_fds(const int exclude[], size_t exsz);
|
|
|
|
|
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 */
|