-
Notifications
You must be signed in to change notification settings - Fork 204
/
file.c
297 lines (267 loc) · 7.91 KB
/
file.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
/*
Copyright (c) 2016 Paulo Faria
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
#include <errno.h>
#include <fcntl.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "libmill.h"
#include "utils.h"
#ifndef MILL_FILE_BUFLEN
#define MILL_FILE_BUFLEN (4096)
#endif
struct mill_file {
int fd;
size_t ifirst;
size_t ilen;
size_t olen;
char ibuf[MILL_FILE_BUFLEN];
char obuf[MILL_FILE_BUFLEN];
};
static void mill_filetune(int fd) {
/* Make the file descriptor non-blocking. */
int opt = fcntl(fd, F_GETFL, 0);
if (opt == -1)
opt = 0;
int rc = fcntl(fd, F_SETFL, opt | O_NONBLOCK);
mill_assert(rc != -1);
}
struct mill_file *mill_mfopen_(const char *pathname, int flags, mode_t mode) {
/* Open the file. */
int fd = open(pathname, flags, mode);
if (fd == -1)
return NULL;
mill_filetune(fd);
/* Create the object. */
struct mill_file *f = malloc(sizeof(struct mill_file));
if(!f) {
fdclean(fd);
close(fd);
errno = ENOMEM;
return NULL;
}
f->fd = fd;
f->ifirst = 0;
f->ilen = 0;
f->olen = 0;
errno = 0;
return f;
}
size_t mill_mfwrite_(struct mill_file *f, const void *buf, size_t len,
int64_t deadline) {
/* If it fits into the output buffer copy it there and be done. */
if(f->olen + len <= MILL_FILE_BUFLEN) {
memcpy(&f->obuf[f->olen], buf, len);
f->olen += len;
errno = 0;
return len;
}
/* If it doesn't fit, flush the output buffer first. */
mfflush(f, deadline);
if(errno != 0)
return 0;
/* Try to fit it into the buffer once again. */
if(f->olen + len <= MILL_FILE_BUFLEN) {
memcpy(&f->obuf[f->olen], buf, len);
f->olen += len;
errno = 0;
return len;
}
/* The data chunk to send is longer than the output buffer.
Let's do the writing in-place. */
char *pos = (char*)buf;
size_t remaining = len;
while(remaining) {
ssize_t sz = write(f->fd, pos, remaining);
if(sz == -1) {
if(errno != EAGAIN && errno != EWOULDBLOCK)
return 0;
int rc = fdwait(f->fd, FDW_OUT, deadline);
if(rc == 0) {
errno = ETIMEDOUT;
return len - remaining;
}
mill_assert(rc == FDW_OUT);
continue;
}
pos += sz;
remaining -= sz;
}
return len;
}
void mill_mfflush_(struct mill_file *f, int64_t deadline) {
if(!f->olen) {
errno = 0;
return;
}
char *pos = f->obuf;
size_t remaining = f->olen;
while(remaining) {
ssize_t sz = write(f->fd, pos, remaining);
if(sz == -1) {
if(errno != EAGAIN && errno != EWOULDBLOCK)
return;
int rc = fdwait(f->fd, FDW_OUT, deadline);
if(rc == 0) {
errno = ETIMEDOUT;
return;
}
mill_assert(rc == FDW_OUT);
continue;
}
pos += sz;
remaining -= sz;
}
f->olen = 0;
errno = 0;
}
size_t mill_mfread_(struct mill_file *f, void *buf, size_t len,
int64_t deadline) {
/* If there's enough data in the buffer it's easy. */
if(f->ilen >= len) {
memcpy(buf, &f->ibuf[f->ifirst], len);
f->ifirst += len;
f->ilen -= len;
errno = 0;
return len;
}
/* Let's move all the data from the buffer first. */
char *pos = (char*)buf;
size_t remaining = len;
memcpy(pos, &f->ibuf[f->ifirst], f->ilen);
pos += f->ilen;
remaining -= f->ilen;
f->ifirst = 0;
f->ilen = 0;
mill_assert(remaining);
while(1) {
if(remaining > MILL_FILE_BUFLEN) {
/* If we still have a lot to read try to read it in one go directly
into the destination buffer. */
ssize_t sz = read(f->fd, pos, remaining);
if(!sz) {
return len - remaining;
}
if(sz == -1) {
if(errno != EAGAIN && errno != EWOULDBLOCK)
return len - remaining;
sz = 0;
}
if((size_t)sz == remaining) {
errno = 0;
return len;
}
pos += sz;
remaining -= sz;
if (sz != 0 && mfeof(f)) {
return len - remaining;
}
}
else {
/* If we have just a little to read try to read the full connection
buffer to minimise the number of system calls. */
ssize_t sz = read(f->fd, f->ibuf, MILL_FILE_BUFLEN);
if(!sz) {
return len - remaining;
}
if(sz == -1) {
if(errno != EAGAIN && errno != EWOULDBLOCK)
return len - remaining;
sz = 0;
}
if((size_t)sz < remaining) {
memcpy(pos, f->ibuf, sz);
pos += sz;
remaining -= sz;
f->ifirst = 0;
f->ilen = 0;
}
else {
memcpy(pos, f->ibuf, remaining);
f->ifirst = remaining;
f->ilen = sz - remaining;
errno = 0;
return len;
}
if (sz != 0 && mfeof(f)) {
return len - remaining;
}
}
/* Wait till there's more data to read. */
int res = fdwait(f->fd, FDW_IN, deadline);
if (!res) {
errno = ETIMEDOUT;
return len - remaining;
}
}
}
void mill_mfclose_(struct mill_file *f) {
fdclean(f->fd);
int rc = close(f->fd);
mill_assert(rc == 0);
free(f);
return;
}
off_t mill_mftell_(struct mill_file *f) {
return lseek(f->fd, 0, SEEK_CUR) - f->ilen;
}
off_t mill_mfseek_(struct mill_file *f, off_t offset) {
f->ifirst = 0;
f->ilen = 0;
f->olen = 0;
return lseek(f->fd, offset, SEEK_SET);
}
int mill_mfeof_(struct mill_file *f) {
off_t current = lseek(f->fd, 0, SEEK_CUR);
if (current == -1)
return -1;
off_t eof = lseek(f->fd, 0, SEEK_END);
if (eof == -1)
return -1;
off_t res = lseek(f->fd, current, SEEK_SET);
if (res == -1)
return -1;
return (current == eof);
}
struct mill_file *mill_mfin_(void) {
static struct mill_file f = {-1, 0, 0, 0};
if(mill_slow(f.fd < 0)) {
mill_filetune(STDIN_FILENO);
f.fd = STDIN_FILENO;
}
return &f;
}
struct mill_file *mill_mfout_(void) {
static struct mill_file f = {-1, 0, 0, 0};
if(mill_slow(f.fd < 0)) {
mill_filetune(STDOUT_FILENO);
f.fd = STDOUT_FILENO;
}
return &f;
}
struct mill_file *mill_mferr_(void) {
static struct mill_file f = {-1, 0, 0, 0};
if(mill_slow(f.fd < 0)) {
mill_filetune(STDERR_FILENO);
f.fd = STDERR_FILENO;
}
return &f;
}