]>
Commit | Line | Data |
---|---|---|
30a4f2a8 | 1 | /* |
f88211e8 | 2 | * $Id: disk.cc,v 1.71 1997/06/02 19:56:01 wessels Exp $ |
30a4f2a8 | 3 | * |
4 | * DEBUG: section 6 Disk I/O Routines | |
5 | * AUTHOR: Harvest Derived | |
6 | * | |
42c04c16 | 7 | * SQUID Internet Object Cache http://squid.nlanr.net/Squid/ |
30a4f2a8 | 8 | * -------------------------------------------------------- |
9 | * | |
10 | * Squid is the result of efforts by numerous individuals from the | |
11 | * Internet community. Development is led by Duane Wessels of the | |
12 | * National Laboratory for Applied Network Research and funded by | |
13 | * the National Science Foundation. | |
14 | * | |
15 | * This program is free software; you can redistribute it and/or modify | |
16 | * it under the terms of the GNU General Public License as published by | |
17 | * the Free Software Foundation; either version 2 of the License, or | |
18 | * (at your option) any later version. | |
19 | * | |
20 | * This program is distributed in the hope that it will be useful, | |
21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
23 | * GNU General Public License for more details. | |
24 | * | |
25 | * You should have received a copy of the GNU General Public License | |
26 | * along with this program; if not, write to the Free Software | |
27 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
28 | * | |
29 | */ | |
30 | ||
31 | /* | |
32 | * Copyright (c) 1994, 1995. All rights reserved. | |
33 | * | |
34 | * The Harvest software was developed by the Internet Research Task | |
35 | * Force Research Group on Resource Discovery (IRTF-RD): | |
36 | * | |
37 | * Mic Bowman of Transarc Corporation. | |
38 | * Peter Danzig of the University of Southern California. | |
39 | * Darren R. Hardy of the University of Colorado at Boulder. | |
40 | * Udi Manber of the University of Arizona. | |
41 | * Michael F. Schwartz of the University of Colorado at Boulder. | |
42 | * Duane Wessels of the University of Colorado at Boulder. | |
43 | * | |
44 | * This copyright notice applies to software in the Harvest | |
45 | * ``src/'' directory only. Users should consult the individual | |
46 | * copyright notices in the ``components/'' subdirectories for | |
47 | * copyright information about other software bundled with the | |
48 | * Harvest source code distribution. | |
49 | * | |
50 | * TERMS OF USE | |
51 | * | |
52 | * The Harvest software may be used and re-distributed without | |
53 | * charge, provided that the software origin and research team are | |
54 | * cited in any use of the system. Most commonly this is | |
55 | * accomplished by including a link to the Harvest Home Page | |
56 | * (http://harvest.cs.colorado.edu/) from the query page of any | |
57 | * Broker you deploy, as well as in the query result pages. These | |
58 | * links are generated automatically by the standard Broker | |
59 | * software distribution. | |
60 | * | |
61 | * The Harvest software is provided ``as is'', without express or | |
62 | * implied warranty, and with no support nor obligation to assist | |
63 | * in its use, correction, modification or enhancement. We assume | |
64 | * no liability with respect to the infringement of copyrights, | |
65 | * trade secrets, or any patents, and are not responsible for | |
66 | * consequential damages. Proper use of the Harvest software is | |
67 | * entirely the responsibility of the user. | |
68 | * | |
69 | * DERIVATIVE WORKS | |
70 | * | |
71 | * Users may make derivative works from the Harvest software, subject | |
72 | * to the following constraints: | |
73 | * | |
74 | * - You must include the above copyright notice and these | |
75 | * accompanying paragraphs in all forms of derivative works, | |
76 | * and any documentation and other materials related to such | |
77 | * distribution and use acknowledge that the software was | |
78 | * developed at the above institutions. | |
79 | * | |
80 | * - You must notify IRTF-RD regarding your distribution of | |
81 | * the derivative work. | |
82 | * | |
83 | * - You must clearly notify users that your are distributing | |
84 | * a modified version and not the original Harvest software. | |
85 | * | |
86 | * - Any derivative product is also subject to these copyright | |
87 | * and use restrictions. | |
88 | * | |
89 | * Note that the Harvest software is NOT in the public domain. We | |
90 | * retain copyright, as specified above. | |
91 | * | |
92 | * HISTORY OF FREE SOFTWARE STATUS | |
93 | * | |
94 | * Originally we required sites to license the software in cases | |
95 | * where they were going to build commercial products/services | |
96 | * around Harvest. In June 1995 we changed this policy. We now | |
97 | * allow people to use the core Harvest software (the code found in | |
98 | * the Harvest ``src/'' directory) for free. We made this change | |
99 | * in the interest of encouraging the widest possible deployment of | |
100 | * the technology. The Harvest software is really a reference | |
101 | * implementation of a set of protocols and formats, some of which | |
102 | * we intend to standardize. We encourage commercial | |
103 | * re-implementations of code complying to this set of standards. | |
104 | */ | |
ed43818f | 105 | |
44a47c6e | 106 | #include "squid.h" |
090089c4 | 107 | |
108 | #define DISK_LINE_LEN 1024 | |
090089c4 | 109 | |
0a0bf5db | 110 | typedef struct disk_ctrl_t { |
111 | int fd; | |
112 | void *data; | |
113 | } disk_ctrl_t; | |
114 | ||
115 | ||
116 | typedef struct open_ctrl_t { | |
f17936ab | 117 | FOCB *callback; |
0a0bf5db | 118 | void *callback_data; |
119 | char *path; | |
120 | } open_ctrl_t; | |
121 | ||
122 | ||
090089c4 | 123 | typedef struct _dwalk_ctrl { |
124 | int fd; | |
125 | off_t offset; | |
de866d20 | 126 | int cur_len; |
090089c4 | 127 | char *buf; /* line buffer */ |
582b6456 | 128 | FILE_WALK_HD *handler; |
2318883b | 129 | void *client_data; |
582b6456 | 130 | FILE_WALK_LHD *line_handler; |
2318883b | 131 | void *line_data; |
090089c4 | 132 | } dwalk_ctrl; |
133 | ||
de866d20 | 134 | static AIOCB diskHandleWriteComplete; |
135 | static AIOCB diskHandleReadComplete; | |
136 | static AIOCB diskHandleWalkComplete; | |
95d15928 | 137 | static PF diskHandleWalk; |
138 | static PF diskHandleRead; | |
139 | static PF diskHandleWrite; | |
0a0bf5db | 140 | static void file_open_complete _PARAMS((void *, int, int)); |
24382924 | 141 | |
090089c4 | 142 | /* initialize table */ |
b8d8561b | 143 | int |
0673c0ba | 144 | disk_init(void) |
090089c4 | 145 | { |
090089c4 | 146 | return 0; |
147 | } | |
148 | ||
149 | /* Open a disk file. Return a file descriptor */ | |
684c2720 | 150 | int |
9e4ad609 | 151 | file_open(const char *path, int mode, FOCB * callback, void *callback_data) |
090089c4 | 152 | { |
090089c4 | 153 | int fd; |
0a0bf5db | 154 | open_ctrl_t *ctrlp; |
155 | ||
156 | ctrlp = xmalloc(sizeof(open_ctrl_t)); | |
157 | ctrlp->path = xstrdup(path); | |
158 | ctrlp->callback = callback; | |
159 | ctrlp->callback_data = callback_data; | |
090089c4 | 160 | |
b59c7120 | 161 | if (mode & O_WRONLY) |
162 | mode |= O_APPEND; | |
4f92c80c | 163 | mode |= SQUID_NONBLOCK; |
b59c7120 | 164 | |
090089c4 | 165 | /* Open file */ |
0a0bf5db | 166 | #if USE_ASYNC_IO |
f17936ab | 167 | if (callback != NULL) { |
9e4ad609 | 168 | aioOpen(path, mode, 0644, file_open_complete, ctrlp); |
169 | return DISK_OK; | |
0a0bf5db | 170 | } |
f17936ab | 171 | #endif |
0a0bf5db | 172 | fd = open(path, mode, 0644); |
173 | file_open_complete(ctrlp, fd, errno); | |
174 | if (fd < 0) | |
175 | return DISK_ERROR; | |
176 | return fd; | |
0a0bf5db | 177 | } |
178 | ||
179 | ||
180 | static void | |
95d15928 | 181 | file_open_complete(void *data, int fd, int errcode) |
0a0bf5db | 182 | { |
183 | open_ctrl_t *ctrlp = (open_ctrl_t *) data; | |
95d15928 | 184 | FD_ENTRY *fde; |
0a0bf5db | 185 | if (fd < 0) { |
186 | errno = errcode; | |
187 | debug(50, 0, "file_open: error opening file %s: %s\n", ctrlp->path, | |
188 | xstrerror()); | |
189 | if (ctrlp->callback) | |
190 | (ctrlp->callback) (ctrlp->callback_data, DISK_ERROR); | |
191 | xfree(ctrlp->path); | |
192 | xfree(ctrlp); | |
193 | return; | |
090089c4 | 194 | } |
3ca60c86 | 195 | commSetCloseOnExec(fd); |
5c5783a2 | 196 | fd_open(fd, FD_FILE, ctrlp->path); |
95d15928 | 197 | fde = &fd_table[fd]; |
0a0bf5db | 198 | if (ctrlp->callback) |
199 | (ctrlp->callback) (ctrlp->callback_data, fd); | |
200 | xfree(ctrlp->path); | |
201 | xfree(ctrlp); | |
202 | } | |
203 | ||
090089c4 | 204 | /* close a disk file. */ |
95d15928 | 205 | void |
b8d8561b | 206 | file_close(int fd) |
090089c4 | 207 | { |
f88211e8 | 208 | FD_ENTRY *fde = &fd_table[fd]; |
209 | assert(fd >= 0); | |
210 | assert(fde->open); | |
95d15928 | 211 | if (BIT_TEST(fde->flags, FD_WRITE_DAEMON)) { |
212 | BIT_SET(fde->flags, FD_CLOSE_REQUEST); | |
213 | return; | |
fb247d78 | 214 | } |
95d15928 | 215 | if (BIT_TEST(fde->flags, FD_WRITE_PENDING)) { |
216 | BIT_SET(fde->flags, FD_CLOSE_REQUEST); | |
217 | return; | |
218 | } | |
5c5783a2 | 219 | fd_close(fd); |
0a0bf5db | 220 | #if USE_ASYNC_IO |
95d15928 | 221 | aioClose(fd); |
0a0bf5db | 222 | #else |
95d15928 | 223 | close(fd); |
0a0bf5db | 224 | #endif |
090089c4 | 225 | } |
226 | ||
090089c4 | 227 | |
228 | /* write handler */ | |
582b6456 | 229 | static void |
95d15928 | 230 | diskHandleWrite(int fd, void *unused) |
090089c4 | 231 | { |
4a86108c | 232 | int len = 0; |
0a0bf5db | 233 | disk_ctrl_t *ctrlp; |
234 | dwrite_q *q = NULL; | |
235 | dwrite_q *wq = NULL; | |
95d15928 | 236 | FD_ENTRY *fde = &fd_table[fd]; |
de866d20 | 237 | struct _fde_disk *fdd = &fde->disk; |
238 | if (!fdd->write_q) | |
582b6456 | 239 | return; |
0a0bf5db | 240 | /* We need to combine subsequent write requests after the first */ |
de866d20 | 241 | if (fdd->write_q->next != NULL && fdd->write_q->next->next != NULL) { |
242 | len = 0; | |
243 | for (q = fdd->write_q->next; q != NULL; q = q->next) | |
0a0bf5db | 244 | len += q->len - q->cur_offset; |
245 | wq = xcalloc(1, sizeof(dwrite_q)); | |
246 | wq->buf = xmalloc(len); | |
247 | wq->len = 0; | |
248 | wq->cur_offset = 0; | |
249 | wq->next = NULL; | |
250 | wq->free = xfree; | |
251 | do { | |
de866d20 | 252 | q = fdd->write_q->next; |
0a0bf5db | 253 | len = q->len - q->cur_offset; |
de866d20 | 254 | xmemcpy(wq->buf + wq->len, q->buf + q->cur_offset, len); |
0a0bf5db | 255 | wq->len += len; |
de866d20 | 256 | fdd->write_q->next = q->next; |
0a0bf5db | 257 | if (q->free) |
258 | (q->free) (q->buf); | |
259 | safe_free(q); | |
de866d20 | 260 | } while (fdd->write_q->next != NULL); |
261 | fdd->write_q_tail = wq; | |
262 | fdd->write_q->next = wq; | |
0a0bf5db | 263 | } |
264 | ctrlp = xcalloc(1, sizeof(disk_ctrl_t)); | |
265 | ctrlp->fd = fd; | |
0a0bf5db | 266 | #if USE_ASYNC_IO |
267 | aioWrite(fd, | |
de866d20 | 268 | fdd->write_q->buf + fdd->write_q->cur_offset, |
269 | fdd->write_q->len - fdd->write_q->cur_offset, | |
0a0bf5db | 270 | diskHandleWriteComplete, |
cd1fb0eb | 271 | ctrlp); |
0a0bf5db | 272 | #else |
273 | len = write(fd, | |
de866d20 | 274 | fdd->write_q->buf + fdd->write_q->cur_offset, |
275 | fdd->write_q->len - fdd->write_q->cur_offset); | |
582b6456 | 276 | diskHandleWriteComplete(ctrlp, len, errno); |
0a0bf5db | 277 | #endif |
278 | } | |
279 | ||
de866d20 | 280 | static void |
b69f7771 | 281 | diskHandleWriteComplete(void *data, int len, int errcode) |
0a0bf5db | 282 | { |
283 | disk_ctrl_t *ctrlp = data; | |
0a0bf5db | 284 | int fd = ctrlp->fd; |
95d15928 | 285 | FD_ENTRY *fde = &fd_table[fd]; |
de866d20 | 286 | struct _fde_disk *fdd = &fde->disk; |
287 | dwrite_q *q = fdd->write_q; | |
288 | int status = DISK_OK; | |
0a0bf5db | 289 | errno = errcode; |
290 | safe_free(data); | |
b69f7771 | 291 | fd_bytes(fd, len, FD_WRITE); |
de866d20 | 292 | if (q == NULL) /* Someone aborted then write completed */ |
293 | return; | |
0a0bf5db | 294 | if (len < 0) { |
295 | if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) { | |
de866d20 | 296 | (void) 0; |
0a0bf5db | 297 | } else { |
de866d20 | 298 | status = errno == ENOSPC ? DISK_NO_SPACE_LEFT : DISK_ERROR; |
0c77d853 | 299 | debug(50, 1, "diskHandleWrite: FD %d: disk write error: %s\n", |
300 | fd, xstrerror()); | |
de866d20 | 301 | if (fdd->wrt_handle == NULL) { |
302 | /* FLUSH PENDING BUFFERS */ | |
303 | do { | |
304 | fdd->write_q = q->next; | |
305 | if (q->free) | |
306 | (q->free) (q->buf); | |
307 | safe_free(q); | |
308 | } while ((q = fdd->write_q)); | |
090089c4 | 309 | } |
0c77d853 | 310 | } |
de866d20 | 311 | len = 0; |
0a0bf5db | 312 | } |
313 | q->cur_offset += len; | |
de866d20 | 314 | assert(q->cur_offset <= q->len); |
0a0bf5db | 315 | if (q->cur_offset == q->len) { |
0c77d853 | 316 | /* complete write */ |
de866d20 | 317 | fdd->write_q = q->next; |
0a0bf5db | 318 | if (q->free) |
319 | (q->free) (q->buf); | |
320 | safe_free(q); | |
090089c4 | 321 | } |
de866d20 | 322 | if (fdd->write_q == NULL) { |
323 | /* no more data */ | |
324 | fdd->write_q_tail = NULL; | |
325 | BIT_RESET(fde->flags, FD_WRITE_PENDING); | |
326 | BIT_RESET(fde->flags, FD_WRITE_DAEMON); | |
327 | } else { | |
0a0bf5db | 328 | /* another block is queued */ |
de866d20 | 329 | commSetSelect(fd, COMM_SELECT_WRITE, diskHandleWrite, NULL, 0); |
330 | BIT_SET(fde->flags, FD_WRITE_DAEMON); | |
4a86108c | 331 | } |
de866d20 | 332 | if (fdd->wrt_handle) |
d89d1fb6 | 333 | fdd->wrt_handle(fd, status, len, fdd->wrt_handle_data); |
95d15928 | 334 | if (BIT_TEST(fde->flags, FD_CLOSE_REQUEST)) |
b59c7120 | 335 | file_close(fd); |
090089c4 | 336 | } |
337 | ||
338 | ||
090089c4 | 339 | /* write block to a file */ |
340 | /* write back queue. Only one writer at a time. */ | |
341 | /* call a handle when writing is complete. */ | |
b8d8561b | 342 | int |
3ebcfaa1 | 343 | file_write(int fd, |
684c2720 | 344 | char *ptr_to_buf, |
345 | int len, | |
d89d1fb6 | 346 | DWCB handle, |
684c2720 | 347 | void *handle_data, |
9e4ad609 | 348 | FREE * free_func) |
090089c4 | 349 | { |
c6ac7aae | 350 | dwrite_q *wq = NULL; |
e7a22b88 | 351 | FD_ENTRY *fde; |
352 | if (fd < 0) | |
353 | fatal_dump("file_write: bad FD"); | |
354 | fde = &fd_table[fd]; | |
95d15928 | 355 | if (!fde->open) { |
429fdbec | 356 | debug_trap("file_write: FILE_NOT_OPEN"); |
090089c4 | 357 | return DISK_ERROR; |
429fdbec | 358 | } |
090089c4 | 359 | /* if we got here. Caller is eligible to write. */ |
30a4f2a8 | 360 | wq = xcalloc(1, sizeof(dwrite_q)); |
090089c4 | 361 | wq->buf = ptr_to_buf; |
090089c4 | 362 | wq->len = len; |
363 | wq->cur_offset = 0; | |
364 | wq->next = NULL; | |
86ee2017 | 365 | wq->free = free_func; |
95d15928 | 366 | fde->disk.wrt_handle = handle; |
367 | fde->disk.wrt_handle_data = handle_data; | |
090089c4 | 368 | |
369 | /* add to queue */ | |
95d15928 | 370 | BIT_SET(fde->flags, FD_WRITE_PENDING); |
371 | if (!(fde->disk.write_q)) { | |
090089c4 | 372 | /* empty queue */ |
95d15928 | 373 | fde->disk.write_q = fde->disk.write_q_tail = wq; |
090089c4 | 374 | } else { |
95d15928 | 375 | fde->disk.write_q_tail->next = wq; |
376 | fde->disk.write_q_tail = wq; | |
090089c4 | 377 | } |
378 | ||
95d15928 | 379 | if (!BIT_TEST(fde->flags, FD_WRITE_DAEMON)) { |
0a0bf5db | 380 | #if USE_ASYNC_IO |
95d15928 | 381 | diskHandleWrite(fd, NULL); |
0a0bf5db | 382 | #else |
95d15928 | 383 | commSetSelect(fd, COMM_SELECT_WRITE, diskHandleWrite, NULL, 0); |
0a0bf5db | 384 | #endif |
95d15928 | 385 | BIT_SET(fde->flags, FD_WRITE_DAEMON); |
429fdbec | 386 | } |
090089c4 | 387 | return DISK_OK; |
388 | } | |
389 | ||
390 | ||
391 | ||
392 | /* Read from FD */ | |
582b6456 | 393 | static void |
394 | diskHandleRead(int fd, void *data) | |
090089c4 | 395 | { |
582b6456 | 396 | dread_ctrl *ctrl_dat = data; |
090089c4 | 397 | int len; |
0a0bf5db | 398 | disk_ctrl_t *ctrlp; |
399 | ctrlp = xcalloc(1, sizeof(disk_ctrl_t)); | |
400 | ctrlp->fd = fd; | |
cd1fb0eb | 401 | ctrlp->data = ctrl_dat; |
0a0bf5db | 402 | #if USE_ASYNC_IO |
403 | aioRead(fd, | |
de866d20 | 404 | ctrl_dat->buf + ctrl_dat->offset, |
405 | ctrl_dat->req_len - ctrl_dat->offset, | |
0a0bf5db | 406 | diskHandleReadComplete, |
cd1fb0eb | 407 | ctrlp); |
0a0bf5db | 408 | #else |
5409636c | 409 | len = read(fd, |
de866d20 | 410 | ctrl_dat->buf + ctrl_dat->offset, |
411 | ctrl_dat->req_len - ctrl_dat->offset); | |
582b6456 | 412 | diskHandleReadComplete(ctrlp, len, errno); |
090089c4 | 413 | #endif |
0a0bf5db | 414 | } |
415 | ||
de866d20 | 416 | static void |
4f92c80c | 417 | diskHandleReadComplete(void *data, int len, int errcode) |
0a0bf5db | 418 | { |
419 | disk_ctrl_t *ctrlp = data; | |
420 | dread_ctrl *ctrl_dat = ctrlp->data; | |
421 | int fd = ctrlp->fd; | |
0a0bf5db | 422 | errno = errcode; |
423 | xfree(data); | |
4f92c80c | 424 | fd_bytes(fd, len, FD_READ); |
0a0bf5db | 425 | if (len < 0) { |
426 | if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) { | |
427 | commSetSelect(fd, | |
428 | COMM_SELECT_READ, | |
cd1fb0eb | 429 | diskHandleRead, |
430 | ctrl_dat, | |
0a0bf5db | 431 | 0); |
de866d20 | 432 | return; |
0a0bf5db | 433 | } |
434 | debug(50, 1, "diskHandleRead: FD %d: error reading: %s\n", | |
435 | fd, xstrerror()); | |
436 | ctrl_dat->handler(fd, ctrl_dat->buf, | |
de866d20 | 437 | ctrl_dat->offset, |
0a0bf5db | 438 | DISK_ERROR, |
439 | ctrl_dat->client_data); | |
440 | safe_free(ctrl_dat); | |
de866d20 | 441 | return; |
090089c4 | 442 | } else if (len == 0) { |
443 | /* EOF */ | |
444 | ctrl_dat->end_of_file = 1; | |
445 | /* call handler */ | |
0a0bf5db | 446 | ctrl_dat->handler(fd, |
447 | ctrl_dat->buf, | |
de866d20 | 448 | ctrl_dat->offset, |
0a0bf5db | 449 | DISK_EOF, |
098be23b | 450 | ctrl_dat->client_data); |
090089c4 | 451 | safe_free(ctrl_dat); |
de866d20 | 452 | return; |
0a0bf5db | 453 | } else { |
de866d20 | 454 | ctrl_dat->offset += len; |
090089c4 | 455 | } |
090089c4 | 456 | /* reschedule if need more data. */ |
de866d20 | 457 | if (ctrl_dat->offset < ctrl_dat->req_len) { |
b177367b | 458 | commSetSelect(fd, |
234967c9 | 459 | COMM_SELECT_READ, |
cd1fb0eb | 460 | diskHandleRead, |
461 | ctrl_dat, | |
85d7ea98 | 462 | 0); |
de866d20 | 463 | return; |
090089c4 | 464 | } else { |
465 | /* all data we need is here. */ | |
5409636c | 466 | /* call handler */ |
467 | ctrl_dat->handler(fd, | |
468 | ctrl_dat->buf, | |
de866d20 | 469 | ctrl_dat->offset, |
5409636c | 470 | DISK_OK, |
098be23b | 471 | ctrl_dat->client_data); |
090089c4 | 472 | safe_free(ctrl_dat); |
de866d20 | 473 | return; |
090089c4 | 474 | } |
475 | } | |
476 | ||
477 | ||
478 | /* start read operation */ | |
479 | /* buffer must be allocated from the caller. | |
480 | * It must have at least req_len space in there. | |
481 | * call handler when a reading is complete. */ | |
b8d8561b | 482 | int |
d89d1fb6 | 483 | file_read(int fd, char *buf, int req_len, int offset, DRCB * handler, void *client_data) |
090089c4 | 484 | { |
485 | dread_ctrl *ctrl_dat; | |
098be23b | 486 | if (fd < 0) |
0c77d853 | 487 | fatal_dump("file_read: bad FD"); |
30a4f2a8 | 488 | ctrl_dat = xcalloc(1, sizeof(dread_ctrl)); |
090089c4 | 489 | ctrl_dat->fd = fd; |
490 | ctrl_dat->offset = offset; | |
491 | ctrl_dat->req_len = req_len; | |
492 | ctrl_dat->buf = buf; | |
de866d20 | 493 | ctrl_dat->offset = 0; |
090089c4 | 494 | ctrl_dat->end_of_file = 0; |
495 | ctrl_dat->handler = handler; | |
496 | ctrl_dat->client_data = client_data; | |
0a0bf5db | 497 | #if USE_ASYNC_IO |
498 | diskHandleRead(fd, ctrl_dat); | |
499 | #else | |
b177367b | 500 | commSetSelect(fd, |
234967c9 | 501 | COMM_SELECT_READ, |
cd1fb0eb | 502 | diskHandleRead, |
503 | ctrl_dat, | |
b177367b | 504 | 0); |
0a0bf5db | 505 | #endif |
090089c4 | 506 | return DISK_OK; |
507 | } | |
508 | ||
509 | ||
510 | /* Read from FD and pass a line to routine. Walk to EOF. */ | |
582b6456 | 511 | static void |
512 | diskHandleWalk(int fd, void *data) | |
090089c4 | 513 | { |
582b6456 | 514 | dwalk_ctrl *walk_dat = data; |
090089c4 | 515 | int len; |
0a0bf5db | 516 | disk_ctrl_t *ctrlp; |
517 | ctrlp = xcalloc(1, sizeof(disk_ctrl_t)); | |
518 | ctrlp->fd = fd; | |
cd1fb0eb | 519 | ctrlp->data = walk_dat; |
0a0bf5db | 520 | #if USE_ASYNC_IO |
521 | aioRead(fd, walk_dat->buf, | |
522 | DISK_LINE_LEN - 1, | |
523 | diskHandleWalkComplete, | |
cd1fb0eb | 524 | ctrlp); |
0a0bf5db | 525 | #else |
090089c4 | 526 | len = read(fd, walk_dat->buf, DISK_LINE_LEN - 1); |
582b6456 | 527 | diskHandleWalkComplete(ctrlp, len, errno); |
090089c4 | 528 | #endif |
0a0bf5db | 529 | } |
530 | ||
531 | ||
de866d20 | 532 | static void |
0a0bf5db | 533 | diskHandleWalkComplete(void *data, int retcode, int errcode) |
534 | { | |
535 | disk_ctrl_t *ctrlp = (disk_ctrl_t *) data; | |
536 | dwalk_ctrl *walk_dat; | |
537 | int fd; | |
538 | int len; | |
539 | LOCAL_ARRAY(char, temp_line, DISK_LINE_LEN); | |
540 | int end_pos; | |
541 | int st_pos; | |
542 | int used_bytes; | |
543 | ||
544 | walk_dat = (dwalk_ctrl *) ctrlp->data; | |
545 | fd = ctrlp->fd; | |
546 | len = retcode; | |
547 | errno = errcode; | |
548 | xfree(data); | |
549 | ||
550 | if (len < 0) { | |
551 | if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) { | |
582b6456 | 552 | commSetSelect(fd, COMM_SELECT_READ, diskHandleWalk, walk_dat, 0); |
de866d20 | 553 | return; |
0a0bf5db | 554 | } |
555 | debug(50, 1, "diskHandleWalk: FD %d: error readingd: %s\n", | |
556 | fd, xstrerror()); | |
557 | walk_dat->handler(fd, DISK_ERROR, walk_dat->client_data); | |
558 | safe_free(walk_dat->buf); | |
559 | safe_free(walk_dat); | |
de866d20 | 560 | return; |
090089c4 | 561 | } else if (len == 0) { |
562 | /* EOF */ | |
563 | walk_dat->handler(fd, DISK_EOF, walk_dat->client_data); | |
564 | safe_free(walk_dat->buf); | |
565 | safe_free(walk_dat); | |
de866d20 | 566 | return; |
090089c4 | 567 | } |
568 | /* emulate fgets here. Cut the into separate line. newline is excluded */ | |
569 | /* it throws last partial line, if exist, away. */ | |
570 | used_bytes = st_pos = end_pos = 0; | |
571 | while (end_pos < len) { | |
572 | if (walk_dat->buf[end_pos] == '\n') { | |
573 | /* new line found */ | |
e94df02d | 574 | xstrncpy(temp_line, walk_dat->buf + st_pos, end_pos - st_pos + 1); |
090089c4 | 575 | used_bytes += end_pos - st_pos + 1; |
576 | ||
577 | /* invoke line handler */ | |
578 | walk_dat->line_handler(fd, temp_line, strlen(temp_line), | |
579 | walk_dat->line_data); | |
580 | ||
581 | /* skip to next line */ | |
582 | st_pos = end_pos + 1; | |
583 | } | |
584 | end_pos++; | |
585 | } | |
586 | ||
587 | /* update file pointer to the next to be read character */ | |
588 | walk_dat->offset += used_bytes; | |
589 | ||
590 | /* reschedule it for next line. */ | |
95d15928 | 591 | commSetSelect(fd, COMM_SELECT_READ, diskHandleWalk, walk_dat, 0); |
090089c4 | 592 | } |
593 | ||
594 | ||
595 | /* start walk through whole file operation | |
596 | * read one block and chop it to a line and pass it to provided | |
597 | * handler one line at a time. | |
598 | * call a completion handler when done. */ | |
b8d8561b | 599 | int |
600 | file_walk(int fd, | |
582b6456 | 601 | FILE_WALK_HD * handler, |
b8d8561b | 602 | void *client_data, |
582b6456 | 603 | FILE_WALK_LHD * line_handler, |
b8d8561b | 604 | void *line_data) |
090089c4 | 605 | { |
606 | dwalk_ctrl *walk_dat; | |
607 | ||
30a4f2a8 | 608 | walk_dat = xcalloc(1, sizeof(dwalk_ctrl)); |
090089c4 | 609 | walk_dat->fd = fd; |
610 | walk_dat->offset = 0; | |
30a4f2a8 | 611 | walk_dat->buf = xcalloc(1, DISK_LINE_LEN); |
090089c4 | 612 | walk_dat->cur_len = 0; |
613 | walk_dat->handler = handler; | |
614 | walk_dat->client_data = client_data; | |
615 | walk_dat->line_handler = line_handler; | |
616 | walk_dat->line_data = line_data; | |
617 | ||
0a0bf5db | 618 | #if USE_ASYNC_IO |
619 | diskHandleWalk(fd, walk_dat); | |
620 | #else | |
95d15928 | 621 | commSetSelect(fd, COMM_SELECT_READ, diskHandleWalk, walk_dat, 0); |
0a0bf5db | 622 | #endif |
090089c4 | 623 | return DISK_OK; |
624 | } | |
625 | ||
b8d8561b | 626 | int |
627 | diskWriteIsComplete(int fd) | |
b59c7120 | 628 | { |
95d15928 | 629 | return fd_table[fd].disk.write_q ? 0 : 1; |
0a21bd84 | 630 | } |