]> git.ipfire.org Git - thirdparty/util-linux.git/blame - include/fileutils.h
Merge branch 'lsfd-cmd--dir' of https://github.com/masatake/util-linux
[thirdparty/util-linux.git] / include / fileutils.h
CommitLineData
faeb1b64
KZ
1/*
2 * No copyright is claimed. This code is in the public domain; do with
3 * it what you wish.
4 **/
6b79eb38
SK
5#ifndef UTIL_LINUX_FILEUTILS
6#define UTIL_LINUX_FILEUTILS
7
372112e9
KZ
8#include <stdio.h>
9#include <fcntl.h>
10#include <unistd.h>
7761bd3b 11#include <dirent.h>
3f684db0 12#include <sys/stat.h>
372112e9
KZ
13
14#include "c.h"
15
4d751c00
RM
16extern int mkstemp_cloexec(char *template);
17
bde91c85 18extern int xmkstemp(char **tmpname, const char *dir, const char *prefix);
6b79eb38 19
bde91c85 20static inline FILE *xfmkstemp(char **tmpname, const char *dir, const char *prefix)
7961acce
SK
21{
22 int fd;
23 FILE *ret;
f272b32c 24
bde91c85 25 fd = xmkstemp(tmpname, dir, prefix);
f272b32c 26 if (fd == -1)
7961acce 27 return NULL;
f272b32c 28
7e3729e7 29 if (!(ret = fdopen(fd, "w+" UL_CLOEXECSTR))) {
7961acce
SK
30 close(fd);
31 return NULL;
32 }
33 return ret;
34}
be92327e 35
dc049516 36#ifdef HAVE_OPENAT
1a048dc5
RM
37static inline FILE *fopen_at(int dir, const char *filename,
38 int flags, const char *mode)
39{
40 int fd = openat(dir, filename, flags);
c8d931eb
MY
41 FILE *ret;
42
1a048dc5
RM
43 if (fd < 0)
44 return NULL;
45
c8d931eb
MY
46 ret = fdopen(fd, mode);
47 if (!ret)
48 close(fd);
49 return ret;
1a048dc5 50}
dc049516 51#endif
1a048dc5 52
3f684db0
SK
53static inline int is_same_inode(const int fd, const struct stat *st)
54{
55 struct stat f;
56
57 if (fstat(fd, &f) < 0)
58 return 0;
59 else if (f.st_dev != st->st_dev || f.st_ino != st->st_ino)
60 return 0;
61 return 1;
62}
63
8e86d93d 64extern int dup_fd_cloexec(int oldfd, int lowfd);
a75700d8 65extern unsigned int get_fd_tabsize(void);
be92327e 66
867df261 67extern int ul_mkdir_p(const char *path, mode_t mode);
d4eaabc8 68extern char *stripoff_last_component(char *path);
934530c7 69
7761bd3b
KZ
70/* This is readdir()-like function, but skips "." and ".." directory entries */
71static inline struct dirent *xreaddir(DIR *dp)
72{
73 struct dirent *d;
74
75 while ((d = readdir(dp))) {
76 if (!strcmp(d->d_name, ".") ||
77 !strcmp(d->d_name, ".."))
78 continue;
79 break;
80 }
81 return d;
82}
83
30c59d67 84
319563bf 85#ifdef HAVE_SYS_SYSCALL_H
f0649c0d 86# include <sys/syscall.h>
30c59d67 87
f191801c 88# if !defined(HAVE_CLOSE_RANGE) && defined(SYS_close_range)
f0649c0d 89# include <sys/types.h>
b8d99a61 90static inline int close_range(unsigned int first, unsigned int last, int flags)
f0649c0d 91{
b8d99a61 92 return syscall(SYS_close_range, first, last, flags);
f0649c0d 93}
f0649c0d
SK
94# define HAVE_CLOSE_RANGE 1
95# endif /* SYS_close_range */
30c59d67 96
7d679f29 97# if !defined(HAVE_STATX) && defined(HAVE_STRUCT_STATX) && defined(SYS_statx)
30c59d67
KZ
98static inline int statx(int fd, const char *restrict path, int flags,
99 unsigned int mask, struct statx *stx)
100{
101 return syscall(SYS_statx, fd, path, flags, mask, stx);
102}
7d679f29 103# define HAVE_STATX 1
30c59d67
KZ
104# endif /* SYS_statx */
105
319563bf 106#endif /* HAVE_SYS_SYSCALL_H */
f0649c0d 107
30c59d67 108
867df261 109extern void ul_close_all_fds(unsigned int first, unsigned int last);
488f65fc 110
cabbf61f
EC
111#define UL_COPY_READ_ERROR (-1)
112#define UL_COPY_WRITE_ERROR (-2)
b9dcd384
EC
113int ul_copy_file(int from, int to);
114
2d24f963
KZ
115
116extern int ul_reopen(int fd, int flags);
117
be92327e 118#endif /* UTIL_LINUX_FILEUTILS */