/src/util-linux/include/all-io.h
Line | Count | Source |
1 | | /* |
2 | | * No copyright is claimed. This code is in the public domain; do with |
3 | | * it what you wish. |
4 | | * |
5 | | * Written by Karel Zak <kzak@redhat.com> |
6 | | * Petr Uzel <petr.uzel@suse.cz> |
7 | | */ |
8 | | |
9 | | #ifndef UTIL_LINUX_ALL_IO_H |
10 | | #define UTIL_LINUX_ALL_IO_H |
11 | | |
12 | | #include <string.h> |
13 | | #include <unistd.h> |
14 | | #include <errno.h> |
15 | | #include <sys/types.h> |
16 | | #ifdef HAVE_SYS_SENDFILE_H |
17 | | # include <sys/sendfile.h> |
18 | | #endif |
19 | | |
20 | | #include "c.h" |
21 | | #include "timeutils.h" |
22 | | #include "vfs.h" |
23 | | |
24 | | static inline int __write_all(const struct ul_vfs_ops *vfs, int fd, |
25 | | const void *buf, size_t count) |
26 | 0 | { |
27 | 0 | while (count) { |
28 | 0 | ssize_t tmp; |
29 | |
|
30 | 0 | errno = 0; |
31 | 0 | tmp = ul_vfs_write(vfs, fd, buf, count); |
32 | 0 | if (tmp > 0) { |
33 | 0 | count -= tmp; |
34 | 0 | if (count) |
35 | 0 | buf = (const void *) ((const char *) buf + tmp); |
36 | 0 | } else if (errno != EINTR && errno != EAGAIN) |
37 | 0 | return -1; |
38 | 0 | if (errno == EAGAIN) /* Try later, *sigh* */ |
39 | 0 | xusleep(250000); |
40 | 0 | } |
41 | 0 | return 0; |
42 | 0 | } Unexecuted instantiation: last.c:__write_all Unexecuted instantiation: fileutils.c:__write_all |
43 | | |
44 | 0 | #define ul_write_all(fd, buf, count) __write_all(NULL, (fd), (buf), (count)) |
45 | | #define ul_vfs_write_all(vfs, fd, buf, count) __write_all((vfs), (fd), (buf), (count)) |
46 | | |
47 | | static inline int ul_fwrite_all(const void *ptr, size_t size, |
48 | | size_t nmemb, FILE *stream) |
49 | 0 | { |
50 | 0 | while (nmemb) { |
51 | 0 | size_t tmp; |
52 | 0 |
|
53 | 0 | errno = 0; |
54 | 0 | tmp = fwrite(ptr, size, nmemb, stream); |
55 | 0 | if (tmp > 0) { |
56 | 0 | nmemb -= tmp; |
57 | 0 | if (nmemb) |
58 | 0 | ptr = (const void *) ((const char *) ptr + (tmp * size)); |
59 | 0 | } else if (errno != EINTR && errno != EAGAIN) |
60 | 0 | return -1; |
61 | 0 | if (errno == EAGAIN) /* Try later, *sigh* */ |
62 | 0 | xusleep(250000); |
63 | 0 | } |
64 | 0 | return 0; |
65 | 0 | } Unexecuted instantiation: last.c:ul_fwrite_all Unexecuted instantiation: fileutils.c:ul_fwrite_all |
66 | | |
67 | | static inline ssize_t __read_all(const struct ul_vfs_ops *vfs, int fd, |
68 | | char *buf, size_t count) |
69 | 0 | { |
70 | 0 | ssize_t ret; |
71 | 0 | ssize_t c = 0; |
72 | 0 | int tries = 0; |
73 | |
|
74 | 0 | memset(buf, 0, count); |
75 | 0 | while (count > 0) { |
76 | 0 | ret = ul_vfs_read(vfs, fd, buf, count); |
77 | 0 | if (ret < 0) { |
78 | 0 | if ((errno == EAGAIN || errno == EINTR) && (tries++ < 5)) { |
79 | 0 | xusleep(250000); |
80 | 0 | continue; |
81 | 0 | } |
82 | 0 | return c ? c : -1; |
83 | 0 | } |
84 | 0 | if (ret == 0) |
85 | 0 | return c; |
86 | 0 | tries = 0; |
87 | 0 | count -= ret; |
88 | 0 | buf += ret; |
89 | 0 | c += ret; |
90 | 0 | } |
91 | 0 | return c; |
92 | 0 | } Unexecuted instantiation: last.c:__read_all Unexecuted instantiation: fileutils.c:__read_all |
93 | | |
94 | 0 | #define ul_read_all(fd, buf, count) __read_all(NULL, (fd), (buf), (count)) |
95 | | #define ul_vfs_read_all(vfs, fd, buf, count) __read_all((vfs), (fd), (buf), (count)) |
96 | | |
97 | | static inline ssize_t ul_read_all_alloc(int fd, char **buf) |
98 | 0 | { |
99 | 0 | size_t size = 1024, c = 0; |
100 | 0 | ssize_t ret; |
101 | 0 |
|
102 | 0 | *buf = malloc(size); |
103 | 0 | if (!*buf) |
104 | 0 | return -1; |
105 | 0 |
|
106 | 0 | while (1) { |
107 | 0 | ret = ul_read_all(fd, *buf + c, size - c); |
108 | 0 | if (ret < 0) { |
109 | 0 | free(*buf); |
110 | 0 | *buf = NULL; |
111 | 0 | return -1; |
112 | 0 | } |
113 | 0 |
|
114 | 0 | if (ret == 0) |
115 | 0 | return c; |
116 | 0 |
|
117 | 0 | c += ret; |
118 | 0 | if (c == size) { |
119 | 0 | size *= 2; |
120 | 0 | *buf = realloc(*buf, size); |
121 | 0 | if (!*buf) |
122 | 0 | return -1; |
123 | 0 | } |
124 | 0 | } |
125 | 0 | } Unexecuted instantiation: last.c:ul_read_all_alloc Unexecuted instantiation: fileutils.c:ul_read_all_alloc |
126 | | |
127 | | static inline ssize_t ul_sendfile_all(int out __attribute__((__unused__)), |
128 | | int in __attribute__((__unused__)), |
129 | | off_t *off __attribute__((__unused__)), |
130 | | size_t count __attribute__((__unused__))) |
131 | 0 | { |
132 | 0 | #if defined(HAVE_SENDFILE) && defined(__linux__) |
133 | 0 | ssize_t ret; |
134 | 0 | ssize_t c = 0; |
135 | 0 | int tries = 0; |
136 | 0 | while (count) { |
137 | 0 | ret = sendfile(out, in, off, count); |
138 | 0 | if (ret < 0) { |
139 | 0 | if ((errno == EAGAIN || errno == EINTR) && (tries++ < 5)) { |
140 | 0 | xusleep(250000); |
141 | 0 | continue; |
142 | 0 | } |
143 | 0 | return c ? c : -1; |
144 | 0 | } |
145 | 0 | if (ret == 0) |
146 | 0 | return c; |
147 | 0 | tries = 0; |
148 | 0 | count -= ret; |
149 | 0 | c += ret; |
150 | 0 | } |
151 | 0 | return c; |
152 | | #else |
153 | | errno = ENOSYS; |
154 | | return -1; |
155 | | #endif |
156 | 0 | } Unexecuted instantiation: last.c:ul_sendfile_all Unexecuted instantiation: fileutils.c:ul_sendfile_all |
157 | | #endif /* UTIL_LINUX_ALL_IO_H */ |