-
Notifications
You must be signed in to change notification settings - Fork 0
/
iobuf.c
332 lines (264 loc) · 8.37 KB
/
iobuf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* Copyright (c) 2012-2018, David Hauweele <david@hauweele.net>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef __linux__
# define _POSIX_C_SOURCE 200112L
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include "iobuf.h"
#ifndef MIN
# define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif /* MIN */
struct iofile {
int fd;
unsigned int write_size;
unsigned int read_size;
char *write_buf;
char *read_buf;
char buf[IOBUF_SIZE * 2];
};
static ssize_t fill_buffer(iofile_t file)
{
ssize_t partial_read = IOBUF_SIZE * 2; /* no refill */
if(file->read_size == 0) {
partial_read = read(file->fd, file->buf + IOBUF_SIZE, IOBUF_SIZE);
if(partial_read < 0) /* read error */
return partial_read;
file->read_size = partial_read;
file->read_buf = file->buf + IOBUF_SIZE;
}
return partial_read;
}
int iobuf_flush(iofile_t file)
{
int write_size = file->write_size;
while(write_size) {
ssize_t partial_write = write(file->fd, file->buf, write_size);
if(partial_write < 0)
return partial_write;
write_size -= partial_write;
}
file->write_size = 0;
file->write_buf = file->buf;
return 0;
}
iofile_t iobuf_dopen(int fd)
{
struct iofile *file = malloc(sizeof(struct iofile));
if(!file)
return NULL;
file->fd = fd;
file->write_buf = file->buf;
file->read_buf = file->buf + IOBUF_SIZE;
file->write_size = file->read_size = 0;
/* We only declare the access pattern on architectures
that are known to support posix_fadvise. */
#if defined(__linux__) || defined(__FreeBSD__)
posix_fadvise(file->fd, 0, 0, POSIX_FADV_SEQUENTIAL);
#endif
return file;
}
iofile_t iobuf_open(const char *pathname, int flags, mode_t mode)
{
int fd = open(pathname, flags, mode);
if(fd < 0)
return NULL;
return iobuf_dopen(fd);
}
ssize_t iobuf_write(iofile_t file, const void *buf, size_t count)
{
if(count > (IOBUF_SIZE - file->write_size)) {
ssize_t partial_write;
partial_write = iobuf_flush(file);
if(partial_write < 0)
return partial_write;
if(count > IOBUF_SIZE) {
ssize_t full_write;
full_write = write(file->fd, buf, count);
if(full_write < 0)
return full_write;
return partial_write + full_write;
}
}
memcpy(file->write_buf, buf, count);
file->write_size += count;
file->write_buf += count;
return count;
}
ssize_t iobuf_read(iofile_t file, void *buf, size_t count)
{
char *cbuf = buf;
do {
ssize_t partial_read = fill_buffer(file);
if(partial_read == 0)
goto EXIT;
else if(partial_read < 0)
return partial_read;
partial_read = MIN(count, file->read_size);
memcpy(cbuf, file->read_buf, partial_read);
file->read_buf += partial_read;
file->read_size -= partial_read;
count -= partial_read;
cbuf += partial_read;
} while(count);
EXIT:
return cbuf - (char *)buf;
}
int iobuf_close(iofile_t file)
{
int ret;
if(file->write_size) {
ret = iobuf_flush(file);
if(ret < 0)
return ret;
}
ret = close(file->fd);
if(ret < 0)
return ret;
free(file);
return ret;
}
int iobuf_putc(char c, iofile_t file)
{
if(file->write_size == IOBUF_SIZE) {
ssize_t partial_write;
partial_write = iobuf_flush(file);
if(partial_write < 0)
return partial_write;
}
*file->write_buf = c;
file->write_size++;
file->write_buf++;
return c;
}
int iobuf_getc(iofile_t file)
{
ssize_t partial_read = fill_buffer(file);
if(partial_read < 0)
return partial_read;
else if(!partial_read)
return GETC_EOF;
file->read_buf++;
file->read_size--;
return *file->read_buf;
}
ssize_t iobuf_gets(iofile_t file, void *buf, size_t count)
{
char *cbuf = buf;
count--;
do {
char *eol;
ssize_t partial_read = fill_buffer(file);
if(partial_read == 0)
goto EXIT;
else if(partial_read < 0)
return partial_read;
partial_read = MIN(count, file->read_size);
eol = memchr(file->read_buf, '\n', partial_read);
if(eol) {
partial_read = eol - file->read_buf + 1; /* keep newline */
count = partial_read; /* will be zero and break */
}
memcpy(cbuf, file->read_buf, partial_read);
file->read_buf += partial_read;
file->read_size -= partial_read;
count -= partial_read;
cbuf += partial_read;
} while(count);
EXIT:
/* mark EOL */
*cbuf = '\0';
return cbuf - (char *)buf;
}
off_t iobuf_lseek(iofile_t file, off_t offset, int whence)
{
if(whence == SEEK_CUR) {
/* There may be an overflow here. We merely assume that the user won't use
an offset large enough to overflow. */
off_t partial = offset - file->read_size;
/* If we seek inside the buffer (short relative seek) then we may avoid
having to drain the buffers. This may improve the performances as most
relative seeks are very short and absolute seeks are generally
unrecoverable. */
if(partial > 0)
offset = partial;
else if(partial + IOBUF_SIZE < 0)
offset = - (offset + file->read_size);
else {
file->read_buf += offset;
file->read_size -= offset;
/* We do not know about the absolute offset location in the file. As it
would inflict a slight overhead to other funtions just for the benefit
of returning the absolute location that most peoples will ignore in
this specific case, we just ignore and return zero to let the user know
that one syscall has been spared. */
return 0;
}
}
off_t res = lseek(file->fd, offset, whence);
if(res < 0)
return res;
file->read_size = 0;
file->read_buf = file->buf + 2 * IOBUF_SIZE;
if(file->write_size)
iobuf_flush(file);
return res;
}
#if defined(__linux__) && defined(_LARGEFILE64_SOURCE)
off64_t iobuf_lseek64(iofile_t file, off64_t offset, int whence)
{
if(whence == SEEK_CUR) {
/* There may be an overflow here. We merely assume that the user won't use
an offset large enough to overflow. */
off64_t partial = offset - file->read_size;
/* If we seek inside the buffer (short relative seek) then we may avoid
having to drain the buffers. This may improve the performances as most
relative seeks are very short and absolute seeks are generally
unrecoverable. */
if(partial > 0)
offset = partial;
else if(partial + IOBUF_SIZE < 0)
offset = - (offset + file->read_size);
else {
file->read_buf += offset;
file->read_size -= offset;
/* We do not know about the absolute offset location in the file. As it
would inflict a slight overhead to other funtions just for the benefit
of returning the absolute location that most peoples will ignore in
this specific case, we just ignore and return zero to let the user know
that one syscall has been spared. */
return 0;
}
}
off64_t res = lseek64(file->fd, offset, whence);
if(res < 0)
return res;
file->read_size = 0;
file->read_buf = file->buf + 2 * IOBUF_SIZE;
if(file->write_size)
iobuf_flush(file);
return res;
}
#endif