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