]> git.ipfire.org Git - thirdparty/util-linux.git/blob - include/fileutils.h
libmount: fix statx() includes
[thirdparty/util-linux.git] / include / fileutils.h
1 /*
2 * No copyright is claimed. This code is in the public domain; do with
3 * it what you wish.
4 **/
5 #ifndef UTIL_LINUX_FILEUTILS
6 #define UTIL_LINUX_FILEUTILS
7
8 #include <stdio.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <dirent.h>
12 #include <sys/stat.h>
13
14 #include "c.h"
15
16 extern int mkstemp_cloexec(char *template);
17
18 extern int xmkstemp(char **tmpname, const char *dir, const char *prefix);
19
20 static inline FILE *xfmkstemp(char **tmpname, const char *dir, const char *prefix)
21 {
22 int fd;
23 FILE *ret;
24
25 fd = xmkstemp(tmpname, dir, prefix);
26 if (fd == -1)
27 return NULL;
28
29 if (!(ret = fdopen(fd, "w+" UL_CLOEXECSTR))) {
30 close(fd);
31 return NULL;
32 }
33 return ret;
34 }
35
36 #ifdef HAVE_OPENAT
37 static inline FILE *fopen_at(int dir, const char *filename,
38 int flags, const char *mode)
39 {
40 int fd = openat(dir, filename, flags);
41 FILE *ret;
42
43 if (fd < 0)
44 return NULL;
45
46 ret = fdopen(fd, mode);
47 if (!ret)
48 close(fd);
49 return ret;
50 }
51 #endif
52
53 static 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
64 extern int dup_fd_cloexec(int oldfd, int lowfd);
65 extern unsigned int get_fd_tabsize(void);
66
67 extern int ul_mkdir_p(const char *path, mode_t mode);
68 extern char *stripoff_last_component(char *path);
69
70 /* This is readdir()-like function, but skips "." and ".." directory entries */
71 static 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
84
85 #ifdef HAVE_SYS_SYSCALL_H
86 # include <sys/syscall.h>
87
88 # if !defined(HAVE_CLOSE_RANGE) && defined(SYS_close_range)
89 # include <sys/types.h>
90 static inline int close_range(unsigned int first, unsigned int last, int flags)
91 {
92 return syscall(SYS_close_range, first, last, flags);
93 }
94 # define HAVE_CLOSE_RANGE 1
95 # endif /* SYS_close_range */
96
97 # if !defined(HAVE_STATX) && defined(HAVE_STRUCT_STATX) && defined(SYS_statx)
98 static 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 }
103 # define HAVE_STATX 1
104 # endif /* SYS_statx */
105
106 #endif /* HAVE_SYS_SYSCALL_H */
107
108
109 extern void ul_close_all_fds(unsigned int first, unsigned int last);
110
111 #define UL_COPY_READ_ERROR (-1)
112 #define UL_COPY_WRITE_ERROR (-2)
113 int ul_copy_file(int from, int to);
114
115
116 extern int ul_reopen(int fd, int flags);
117
118 #endif /* UTIL_LINUX_FILEUTILS */