/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 "vfs.h" |
22 | | |
23 | | static inline int __write_all(const struct ul_vfs_ops *vfs, int fd, |
24 | | const void *buf, size_t count) |
25 | 0 | { |
26 | 0 | while (count) { |
27 | 0 | ssize_t tmp; |
28 | |
|
29 | 0 | errno = 0; |
30 | 0 | tmp = ul_vfs_write(vfs, fd, buf, count); |
31 | 0 | if (tmp > 0) { |
32 | 0 | count -= tmp; |
33 | 0 | if (count) |
34 | 0 | buf = (const void *) ((const char *) buf + tmp); |
35 | 0 | } else if (errno != EINTR && errno != EAGAIN) |
36 | 0 | return -1; |
37 | 0 | if (errno == EAGAIN) /* Try later, *sigh* */ |
38 | 0 | xusleep(250000); |
39 | 0 | } |
40 | 0 | return 0; |
41 | 0 | } Unexecuted instantiation: gen_uuid.c:__write_all Unexecuted instantiation: probe.c:__write_all Unexecuted instantiation: blkdev.c:__write_all Unexecuted instantiation: fileutils.c:__write_all Unexecuted instantiation: path.c:__write_all Unexecuted instantiation: sysfs.c:__write_all Unexecuted instantiation: canonicalize.c:__write_all Unexecuted instantiation: env.c:__write_all |
42 | | |
43 | 0 | #define ul_write_all(fd, buf, count) __write_all(NULL, (fd), (buf), (count)) |
44 | 0 | #define ul_vfs_write_all(vfs, fd, buf, count) __write_all((vfs), (fd), (buf), (count)) |
45 | | |
46 | | static inline int ul_fwrite_all(const void *ptr, size_t size, |
47 | | size_t nmemb, FILE *stream) |
48 | 0 | { |
49 | 0 | while (nmemb) { |
50 | 0 | size_t tmp; |
51 | 0 |
|
52 | 0 | errno = 0; |
53 | 0 | tmp = fwrite(ptr, size, nmemb, stream); |
54 | 0 | if (tmp > 0) { |
55 | 0 | nmemb -= tmp; |
56 | 0 | if (nmemb) |
57 | 0 | ptr = (const void *) ((const char *) ptr + (tmp * size)); |
58 | 0 | } else if (errno != EINTR && errno != EAGAIN) |
59 | 0 | return -1; |
60 | 0 | if (errno == EAGAIN) /* Try later, *sigh* */ |
61 | 0 | xusleep(250000); |
62 | 0 | } |
63 | 0 | return 0; |
64 | 0 | } Unexecuted instantiation: gen_uuid.c:ul_fwrite_all Unexecuted instantiation: probe.c:ul_fwrite_all Unexecuted instantiation: blkdev.c:ul_fwrite_all Unexecuted instantiation: fileutils.c:ul_fwrite_all Unexecuted instantiation: path.c:ul_fwrite_all Unexecuted instantiation: sysfs.c:ul_fwrite_all Unexecuted instantiation: canonicalize.c:ul_fwrite_all Unexecuted instantiation: env.c:ul_fwrite_all |
65 | | |
66 | | static inline ssize_t __read_all(const struct ul_vfs_ops *vfs, int fd, |
67 | | char *buf, size_t count) |
68 | 0 | { |
69 | 0 | ssize_t ret; |
70 | 0 | ssize_t c = 0; |
71 | 0 | int tries = 0; |
72 | |
|
73 | 0 | memset(buf, 0, count); |
74 | 0 | while (count > 0) { |
75 | 0 | ret = ul_vfs_read(vfs, fd, buf, count); |
76 | 0 | if (ret < 0) { |
77 | 0 | if ((errno == EAGAIN || errno == EINTR) && (tries++ < 5)) { |
78 | 0 | xusleep(250000); |
79 | 0 | continue; |
80 | 0 | } |
81 | 0 | return c ? c : -1; |
82 | 0 | } |
83 | 0 | if (ret == 0) |
84 | 0 | return c; |
85 | 0 | tries = 0; |
86 | 0 | count -= ret; |
87 | 0 | buf += ret; |
88 | 0 | c += ret; |
89 | 0 | } |
90 | 0 | return c; |
91 | 0 | } Unexecuted instantiation: gen_uuid.c:__read_all Unexecuted instantiation: probe.c:__read_all Unexecuted instantiation: blkdev.c:__read_all Unexecuted instantiation: fileutils.c:__read_all Unexecuted instantiation: path.c:__read_all Unexecuted instantiation: sysfs.c:__read_all Unexecuted instantiation: canonicalize.c:__read_all Unexecuted instantiation: env.c:__read_all |
92 | | |
93 | 0 | #define ul_read_all(fd, buf, count) __read_all(NULL, (fd), (buf), (count)) |
94 | 0 | #define ul_vfs_read_all(vfs, fd, buf, count) __read_all((vfs), (fd), (buf), (count)) |
95 | | |
96 | | static inline ssize_t ul_read_all_alloc(int fd, char **buf) |
97 | 0 | { |
98 | 0 | size_t size = 1024, c = 0; |
99 | 0 | ssize_t ret; |
100 | |
|
101 | 0 | *buf = malloc(size); |
102 | 0 | if (!*buf) |
103 | 0 | return -1; |
104 | | |
105 | 0 | while (1) { |
106 | 0 | ret = ul_read_all(fd, *buf + c, size - c); |
107 | 0 | if (ret < 0) { |
108 | 0 | free(*buf); |
109 | 0 | *buf = NULL; |
110 | 0 | return -1; |
111 | 0 | } |
112 | | |
113 | 0 | if (ret == 0) |
114 | 0 | return c; |
115 | | |
116 | 0 | c += ret; |
117 | 0 | if (c == size) { |
118 | 0 | size *= 2; |
119 | 0 | *buf = realloc(*buf, size); |
120 | 0 | if (!*buf) |
121 | 0 | return -1; |
122 | 0 | } |
123 | 0 | } |
124 | 0 | } Unexecuted instantiation: gen_uuid.c:ul_read_all_alloc Unexecuted instantiation: probe.c:ul_read_all_alloc Unexecuted instantiation: blkdev.c:ul_read_all_alloc Unexecuted instantiation: fileutils.c:ul_read_all_alloc Unexecuted instantiation: path.c:ul_read_all_alloc Unexecuted instantiation: sysfs.c:ul_read_all_alloc Unexecuted instantiation: canonicalize.c:ul_read_all_alloc Unexecuted instantiation: env.c:ul_read_all_alloc |
125 | | |
126 | | static inline ssize_t ul_sendfile_all(int out __attribute__((__unused__)), |
127 | | int in __attribute__((__unused__)), |
128 | | off_t *off __attribute__((__unused__)), |
129 | | size_t count __attribute__((__unused__))) |
130 | 0 | { |
131 | 0 | #if defined(HAVE_SENDFILE) && defined(__linux__) |
132 | 0 | ssize_t ret; |
133 | 0 | ssize_t c = 0; |
134 | 0 | int tries = 0; |
135 | 0 | while (count) { |
136 | 0 | ret = sendfile(out, in, off, count); |
137 | 0 | if (ret < 0) { |
138 | 0 | if ((errno == EAGAIN || errno == EINTR) && (tries++ < 5)) { |
139 | 0 | xusleep(250000); |
140 | 0 | continue; |
141 | 0 | } |
142 | 0 | return c ? c : -1; |
143 | 0 | } |
144 | 0 | if (ret == 0) |
145 | 0 | return c; |
146 | 0 | tries = 0; |
147 | 0 | count -= ret; |
148 | 0 | c += ret; |
149 | 0 | } |
150 | 0 | return c; |
151 | | #else |
152 | | errno = ENOSYS; |
153 | | return -1; |
154 | | #endif |
155 | 0 | } Unexecuted instantiation: gen_uuid.c:ul_sendfile_all Unexecuted instantiation: probe.c:ul_sendfile_all Unexecuted instantiation: blkdev.c:ul_sendfile_all Unexecuted instantiation: fileutils.c:ul_sendfile_all Unexecuted instantiation: path.c:ul_sendfile_all Unexecuted instantiation: sysfs.c:ul_sendfile_all Unexecuted instantiation: canonicalize.c:ul_sendfile_all Unexecuted instantiation: env.c:ul_sendfile_all |
156 | | #endif /* UTIL_LINUX_ALL_IO_H */ |