/src/util-linux/include/fileutils.h
Line | Count | Source (jump to first uncovered line) |
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 | 0 | { |
22 | 0 | int fd; |
23 | 0 | FILE *ret; |
24 | 0 |
|
25 | 0 | fd = xmkstemp(tmpname, dir, prefix); |
26 | 0 | if (fd == -1) |
27 | 0 | return NULL; |
28 | 0 |
|
29 | 0 | if (!(ret = fdopen(fd, "w+" UL_CLOEXECSTR))) { |
30 | 0 | close(fd); |
31 | 0 | return NULL; |
32 | 0 | } |
33 | 0 | return ret; |
34 | 0 | } Unexecuted instantiation: probe.c:xfmkstemp Unexecuted instantiation: blkdev.c:xfmkstemp Unexecuted instantiation: fileutils.c:xfmkstemp Unexecuted instantiation: path.c:xfmkstemp Unexecuted instantiation: sysfs.c:xfmkstemp Unexecuted instantiation: devname.c:xfmkstemp Unexecuted instantiation: save.c:xfmkstemp |
35 | | |
36 | | #ifdef HAVE_OPENAT |
37 | | static inline FILE *fopen_at(int dir, const char *filename, |
38 | | int flags, const char *mode) |
39 | 0 | { |
40 | 0 | int fd = openat(dir, filename, flags); |
41 | 0 | FILE *ret; |
42 | 0 |
|
43 | 0 | if (fd < 0) |
44 | 0 | return NULL; |
45 | 0 |
|
46 | 0 | ret = fdopen(fd, mode); |
47 | 0 | if (!ret) |
48 | 0 | close(fd); |
49 | 0 | return ret; |
50 | 0 | } Unexecuted instantiation: probe.c:fopen_at Unexecuted instantiation: blkdev.c:fopen_at Unexecuted instantiation: fileutils.c:fopen_at Unexecuted instantiation: path.c:fopen_at Unexecuted instantiation: sysfs.c:fopen_at Unexecuted instantiation: devname.c:fopen_at Unexecuted instantiation: save.c:fopen_at |
51 | | #endif |
52 | | |
53 | | static inline int is_same_inode(const int fd, const struct stat *st) |
54 | 0 | { |
55 | 0 | struct stat f; |
56 | |
|
57 | 0 | if (fstat(fd, &f) < 0) |
58 | 0 | return 0; |
59 | 0 | else if (f.st_dev != st->st_dev || f.st_ino != st->st_ino) |
60 | 0 | return 0; |
61 | 0 | return 1; |
62 | 0 | } Unexecuted instantiation: probe.c:is_same_inode Unexecuted instantiation: blkdev.c:is_same_inode Unexecuted instantiation: fileutils.c:is_same_inode Unexecuted instantiation: path.c:is_same_inode Unexecuted instantiation: sysfs.c:is_same_inode Unexecuted instantiation: devname.c:is_same_inode Unexecuted instantiation: save.c:is_same_inode |
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 | 0 | { |
73 | 0 | struct dirent *d; |
74 | |
|
75 | 0 | while ((d = readdir(dp))) { |
76 | 0 | if (!strcmp(d->d_name, ".") || |
77 | 0 | !strcmp(d->d_name, "..")) |
78 | 0 | continue; |
79 | 0 | break; |
80 | 0 | } |
81 | 0 | return d; |
82 | 0 | } Unexecuted instantiation: probe.c:xreaddir Unexecuted instantiation: blkdev.c:xreaddir Unexecuted instantiation: fileutils.c:xreaddir Unexecuted instantiation: path.c:xreaddir Unexecuted instantiation: sysfs.c:xreaddir Unexecuted instantiation: devname.c:xreaddir Unexecuted instantiation: save.c:xreaddir |
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 | 0 | #define UL_COPY_READ_ERROR (-1) |
112 | 0 | #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 | | extern char *ul_basename(char *path); |
118 | | |
119 | | #endif /* UTIL_LINUX_FILEUTILS */ |