1 | /* |
1 | /* |
2 | * libeio implementation |
2 | * libeio implementation |
3 | * |
3 | * |
4 | * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann <libeio@schmorp.de> |
4 | * Copyright (c) 2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann <libeio@schmorp.de> |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
8 | * tion, are permitted provided that the following conditions are met: |
8 | * tion, are permitted provided that the following conditions are met: |
9 | * |
9 | * |
… | |
… | |
208 | #endif |
208 | #endif |
209 | |
209 | |
210 | #define D_NAME(entp) entp->d_name |
210 | #define D_NAME(entp) entp->d_name |
211 | |
211 | |
212 | /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ |
212 | /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ |
213 | #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ |
213 | #if __FreeBSD__ || __NetBSD__ || __OpenBSD__ |
214 | #define _DIRENT_HAVE_D_TYPE /* sigh */ |
214 | #define _DIRENT_HAVE_D_TYPE /* sigh */ |
215 | #define D_INO(de) (de)->d_fileno |
215 | #define D_INO(de) (de)->d_fileno |
216 | #define D_NAMLEN(de) (de)->d_namlen |
216 | #define D_NAMLEN(de) (de)->d_namlen |
217 | #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600 |
217 | #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600 |
218 | #define D_INO(de) (de)->d_ino |
218 | #define D_INO(de) (de)->d_ino |
… | |
… | |
1102 | todo -= len; |
1102 | todo -= len; |
1103 | } |
1103 | } |
1104 | |
1104 | |
1105 | FUBd; |
1105 | FUBd; |
1106 | |
1106 | |
1107 | errno = 0; |
1107 | /* linux's readahead basically only fails for EBADF or EINVAL (not mmappable) */ |
|
|
1108 | /* but not for e.g. EIO or eof, so we also never fail */ |
1108 | return count; |
1109 | return 0; |
1109 | } |
1110 | } |
1110 | |
1111 | |
1111 | #endif |
1112 | #endif |
1112 | |
1113 | |
1113 | /* sendfile always needs emulation */ |
1114 | /* sendfile always needs emulation */ |
… | |
… | |
1148 | |
1149 | |
1149 | /* according to source inspection, this is correct, and useful behaviour */ |
1150 | /* according to source inspection, this is correct, and useful behaviour */ |
1150 | if (sbytes) |
1151 | if (sbytes) |
1151 | res = sbytes; |
1152 | res = sbytes; |
1152 | |
1153 | |
1153 | # elif defined (__APPLE__) |
1154 | # elif defined __APPLE__ |
1154 | off_t sbytes = count; |
1155 | off_t sbytes = count; |
1155 | res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); |
1156 | res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); |
1156 | |
1157 | |
1157 | /* according to the manpage, sbytes is always valid */ |
1158 | /* according to the manpage, sbytes is always valid */ |
1158 | if (sbytes) |
1159 | if (sbytes) |
… | |
… | |
1381 | return 0; |
1382 | return 0; |
1382 | } |
1383 | } |
1383 | |
1384 | |
1384 | /*****************************************************************************/ |
1385 | /*****************************************************************************/ |
1385 | /* requests implemented outside eio_execute, because they are so large */ |
1386 | /* requests implemented outside eio_execute, because they are so large */ |
|
|
1387 | |
|
|
1388 | static void |
|
|
1389 | eio__lseek (eio_req *req) |
|
|
1390 | { |
|
|
1391 | /* this usually gets optimised away completely, or your compiler sucks, */ |
|
|
1392 | /* or the whence constants really are not 0, 1, 2 */ |
|
|
1393 | int whence = req->int2 == EIO_SEEK_SET ? SEEK_SET |
|
|
1394 | : req->int2 == EIO_SEEK_CUR ? SEEK_CUR |
|
|
1395 | : req->int2 == EIO_SEEK_END ? SEEK_END |
|
|
1396 | : req->int2; |
|
|
1397 | |
|
|
1398 | req->offs = lseek (req->int1, req->offs, whence); |
|
|
1399 | req->result = req->offs == (off_t)-1 ? -1 : 0; |
|
|
1400 | } |
1386 | |
1401 | |
1387 | /* result will always end up in tmpbuf, there is always space for adding a 0-byte */ |
1402 | /* result will always end up in tmpbuf, there is always space for adding a 0-byte */ |
1388 | static int |
1403 | static int |
1389 | eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) |
1404 | eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) |
1390 | { |
1405 | { |
… | |
… | |
2314 | req->result = req->wd == EIO_INVALID_WD ? -1 : 0; |
2329 | req->result = req->wd == EIO_INVALID_WD ? -1 : 0; |
2315 | break; |
2330 | break; |
2316 | case EIO_WD_CLOSE: req->result = 0; |
2331 | case EIO_WD_CLOSE: req->result = 0; |
2317 | eio_wd_close_sync (req->wd); break; |
2332 | eio_wd_close_sync (req->wd); break; |
2318 | |
2333 | |
|
|
2334 | case EIO_SEEK: eio__lseek (req); break; |
2319 | case EIO_READ: ALLOC (req->size); |
2335 | case EIO_READ: ALLOC (req->size); |
2320 | req->result = req->offs >= 0 |
2336 | req->result = req->offs >= 0 |
2321 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
2337 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
2322 | : read (req->int1, req->ptr2, req->size); break; |
2338 | : read (req->int1, req->ptr2, req->size); break; |
2323 | case EIO_WRITE: req->result = req->offs >= 0 |
2339 | case EIO_WRITE: req->result = req->offs >= 0 |
… | |
… | |
2569 | eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data) |
2585 | eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data) |
2570 | { |
2586 | { |
2571 | REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND; |
2587 | REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND; |
2572 | } |
2588 | } |
2573 | |
2589 | |
|
|
2590 | eio_req *eio_seek (int fd, off_t offset, int whence, int pri, eio_cb cb, void *data) |
|
|
2591 | { |
|
|
2592 | REQ (EIO_SEEK); req->int1 = fd; req->offs = offset; req->int2 = whence; SEND; |
|
|
2593 | } |
|
|
2594 | |
2574 | eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) |
2595 | eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) |
2575 | { |
2596 | { |
2576 | REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; |
2597 | REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; |
2577 | } |
2598 | } |
2578 | |
2599 | |