/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 | 3.70k | { |
27 | 7.40k | while (count) { |
28 | 3.70k | ssize_t tmp; |
29 | | |
30 | 3.70k | errno = 0; |
31 | 3.70k | tmp = ul_vfs_write(vfs, fd, buf, count); |
32 | 3.70k | if (tmp > 0) { |
33 | 3.70k | count -= tmp; |
34 | 3.70k | if (count) |
35 | 0 | buf = (const void *) ((const char *) buf + tmp); |
36 | 3.70k | } else if (errno != EINTR && errno != EAGAIN) |
37 | 0 | return -1; |
38 | 3.70k | if (errno == EAGAIN) /* Try later, *sigh* */ |
39 | 0 | xusleep(250000); |
40 | 3.70k | } |
41 | 3.70k | return 0; |
42 | 3.70k | } Unexecuted instantiation: last.c:__write_all Unexecuted instantiation: fileutils.c:__write_all Unexecuted instantiation: canonicalize.c:__write_all Unexecuted instantiation: env.c:__write_all Unexecuted instantiation: path.c:__write_all Unexecuted instantiation: sysfs.c:__write_all Unexecuted instantiation: blkdev.c:__write_all Unexecuted instantiation: probe.c:__write_all Line | Count | Source | 26 | 3.70k | { | 27 | 7.40k | while (count) { | 28 | 3.70k | ssize_t tmp; | 29 | | | 30 | 3.70k | errno = 0; | 31 | 3.70k | tmp = ul_vfs_write(vfs, fd, buf, count); | 32 | 3.70k | if (tmp > 0) { | 33 | 3.70k | count -= tmp; | 34 | 3.70k | if (count) | 35 | 0 | buf = (const void *) ((const char *) buf + tmp); | 36 | 3.70k | } else if (errno != EINTR && errno != EAGAIN) | 37 | 0 | return -1; | 38 | 3.70k | if (errno == EAGAIN) /* Try later, *sigh* */ | 39 | 0 | xusleep(250000); | 40 | 3.70k | } | 41 | 3.70k | return 0; | 42 | 3.70k | } |
Unexecuted instantiation: sun.c:__write_all Unexecuted instantiation: sgi.c:__write_all Unexecuted instantiation: bsd.c:__write_all Unexecuted instantiation: gpt.c:__write_all Unexecuted instantiation: gen_uuid.c:__write_all |
43 | | |
44 | 3.70k | #define ul_write_all(fd, buf, count) __write_all(NULL, (fd), (buf), (count)) |
45 | 0 | #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 Unexecuted instantiation: canonicalize.c:ul_fwrite_all Unexecuted instantiation: env.c:ul_fwrite_all Unexecuted instantiation: path.c:ul_fwrite_all Unexecuted instantiation: sysfs.c:ul_fwrite_all Unexecuted instantiation: blkdev.c:ul_fwrite_all Unexecuted instantiation: probe.c:ul_fwrite_all Unexecuted instantiation: script.c:ul_fwrite_all Unexecuted instantiation: sun.c:ul_fwrite_all Unexecuted instantiation: sgi.c:ul_fwrite_all Unexecuted instantiation: bsd.c:ul_fwrite_all Unexecuted instantiation: gpt.c:ul_fwrite_all Unexecuted instantiation: gen_uuid.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 | 5.30k | { |
70 | 5.30k | ssize_t ret; |
71 | 5.30k | ssize_t c = 0; |
72 | 5.30k | int tries = 0; |
73 | | |
74 | 5.30k | memset(buf, 0, count); |
75 | 10.6k | while (count > 0) { |
76 | 8.77k | ret = ul_vfs_read(vfs, fd, buf, count); |
77 | 8.77k | 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 | 8.77k | if (ret == 0) |
85 | 3.47k | return c; |
86 | 5.30k | tries = 0; |
87 | 5.30k | count -= ret; |
88 | 5.30k | buf += ret; |
89 | 5.30k | c += ret; |
90 | 5.30k | } |
91 | 1.83k | return c; |
92 | 5.30k | } Unexecuted instantiation: last.c:__read_all Unexecuted instantiation: fileutils.c:__read_all Unexecuted instantiation: canonicalize.c:__read_all Unexecuted instantiation: env.c:__read_all Unexecuted instantiation: path.c:__read_all Unexecuted instantiation: sysfs.c:__read_all Unexecuted instantiation: blkdev.c:__read_all Unexecuted instantiation: probe.c:__read_all Unexecuted instantiation: script.c:__read_all Unexecuted instantiation: sun.c:__read_all Unexecuted instantiation: sgi.c:__read_all Line | Count | Source | 69 | 5.30k | { | 70 | 5.30k | ssize_t ret; | 71 | 5.30k | ssize_t c = 0; | 72 | 5.30k | int tries = 0; | 73 | | | 74 | 5.30k | memset(buf, 0, count); | 75 | 10.6k | while (count > 0) { | 76 | 8.77k | ret = ul_vfs_read(vfs, fd, buf, count); | 77 | 8.77k | 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 | 8.77k | if (ret == 0) | 85 | 3.47k | return c; | 86 | 5.30k | tries = 0; | 87 | 5.30k | count -= ret; | 88 | 5.30k | buf += ret; | 89 | 5.30k | c += ret; | 90 | 5.30k | } | 91 | 1.83k | return c; | 92 | 5.30k | } |
Unexecuted instantiation: gpt.c:__read_all Unexecuted instantiation: gen_uuid.c:__read_all |
93 | | |
94 | 0 | #define ul_read_all(fd, buf, count) __read_all(NULL, (fd), (buf), (count)) |
95 | 5.30k | #define ul_vfs_read_all(vfs, fd, buf, count) __read_all((vfs), (fd), (buf), (count)) |
96 | | |
97 | | /* |
98 | | * Reads all data from @fd to a newly allocated buffer and returns the number |
99 | | * of bytes read, or -1 on error. |
100 | | * |
101 | | * The caller gets a buffer only when there is something in it: *buf is set to |
102 | | * NULL for an empty read as well as on error. In other words, there is |
103 | | * nothing to free unless the return value is greater than zero. |
104 | | */ |
105 | | static inline ssize_t ul_read_all_alloc(int fd, char **buf) |
106 | 0 | { |
107 | 0 | size_t size = 1024, c = 0; |
108 | 0 | ssize_t ret; |
109 | 0 | char *tmp; |
110 | |
|
111 | 0 | *buf = malloc(size); |
112 | 0 | if (!*buf) |
113 | 0 | return -1; |
114 | | |
115 | 0 | while (1) { |
116 | 0 | ret = ul_read_all(fd, *buf + c, size - c); |
117 | 0 | if (ret < 0) |
118 | 0 | goto fail; |
119 | | |
120 | 0 | if (ret == 0) |
121 | 0 | break; |
122 | | |
123 | 0 | c += ret; |
124 | 0 | if (c == size) { |
125 | 0 | size *= 2; |
126 | 0 | tmp = realloc(*buf, size); |
127 | 0 | if (!tmp) |
128 | 0 | goto fail; |
129 | 0 | *buf = tmp; |
130 | 0 | } |
131 | 0 | } |
132 | | |
133 | 0 | if (c == 0) { |
134 | | /* empty read; don't hand over a buffer to free */ |
135 | 0 | free(*buf); |
136 | 0 | *buf = NULL; |
137 | 0 | } |
138 | 0 | return c; |
139 | 0 | fail: |
140 | 0 | free(*buf); |
141 | 0 | *buf = NULL; |
142 | 0 | return -1; |
143 | 0 | } Unexecuted instantiation: last.c:ul_read_all_alloc Unexecuted instantiation: fileutils.c:ul_read_all_alloc Unexecuted instantiation: canonicalize.c:ul_read_all_alloc Unexecuted instantiation: env.c:ul_read_all_alloc Unexecuted instantiation: path.c:ul_read_all_alloc Unexecuted instantiation: sysfs.c:ul_read_all_alloc Unexecuted instantiation: blkdev.c:ul_read_all_alloc Unexecuted instantiation: probe.c:ul_read_all_alloc Unexecuted instantiation: script.c:ul_read_all_alloc Unexecuted instantiation: sun.c:ul_read_all_alloc Unexecuted instantiation: sgi.c:ul_read_all_alloc Unexecuted instantiation: bsd.c:ul_read_all_alloc Unexecuted instantiation: gpt.c:ul_read_all_alloc Unexecuted instantiation: gen_uuid.c:ul_read_all_alloc |
144 | | |
145 | | static inline ssize_t ul_sendfile_all(int out __attribute__((__unused__)), |
146 | | int in __attribute__((__unused__)), |
147 | | off_t *off __attribute__((__unused__)), |
148 | | size_t count __attribute__((__unused__))) |
149 | 0 | { |
150 | 0 | #if defined(HAVE_SENDFILE) && defined(__linux__) |
151 | 0 | ssize_t ret; |
152 | 0 | ssize_t c = 0; |
153 | 0 | int tries = 0; |
154 | 0 | while (count) { |
155 | 0 | ret = sendfile(out, in, off, count); |
156 | 0 | if (ret < 0) { |
157 | 0 | if ((errno == EAGAIN || errno == EINTR) && (tries++ < 5)) { |
158 | 0 | xusleep(250000); |
159 | 0 | continue; |
160 | 0 | } |
161 | 0 | return c ? c : -1; |
162 | 0 | } |
163 | 0 | if (ret == 0) |
164 | 0 | return c; |
165 | 0 | tries = 0; |
166 | 0 | count -= ret; |
167 | 0 | c += ret; |
168 | 0 | } |
169 | 0 | return c; |
170 | | #else |
171 | | errno = ENOSYS; |
172 | | return -1; |
173 | | #endif |
174 | 0 | } Unexecuted instantiation: last.c:ul_sendfile_all Unexecuted instantiation: fileutils.c:ul_sendfile_all Unexecuted instantiation: canonicalize.c:ul_sendfile_all Unexecuted instantiation: env.c:ul_sendfile_all Unexecuted instantiation: path.c:ul_sendfile_all Unexecuted instantiation: sysfs.c:ul_sendfile_all Unexecuted instantiation: blkdev.c:ul_sendfile_all Unexecuted instantiation: probe.c:ul_sendfile_all Unexecuted instantiation: script.c:ul_sendfile_all Unexecuted instantiation: sun.c:ul_sendfile_all Unexecuted instantiation: sgi.c:ul_sendfile_all Unexecuted instantiation: bsd.c:ul_sendfile_all Unexecuted instantiation: gpt.c:ul_sendfile_all Unexecuted instantiation: gen_uuid.c:ul_sendfile_all |
175 | | #endif /* UTIL_LINUX_ALL_IO_H */ |