ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libeio/eio.c
(Generate patch)

Comparing libeio/eio.c (file contents):
Revision 1.86 by root, Thu Jul 14 18:30:10 2011 UTC vs.
Revision 1.91 by root, Sun Jul 17 04:20:04 2011 UTC

58#include <sys/stat.h> 58#include <sys/stat.h>
59#include <limits.h> 59#include <limits.h>
60#include <fcntl.h> 60#include <fcntl.h>
61#include <assert.h> 61#include <assert.h>
62 62
63#include <sys/statvfs.h>
64/* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */ 63/* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */
65/* intptr_t only comes from stdint.h, says idiot openbsd coder */ 64/* intptr_t only comes from stdint.h, says idiot openbsd coder */
66#if HAVE_STDINT_H 65#if HAVE_STDINT_H
67# include <stdint.h> 66# include <stdint.h>
68#endif 67#endif
69 68
70#ifndef ECANCELED 69#ifndef ECANCELED
71# define ECANCELED EDOM 70# define ECANCELED EDOM
72#endif 71#endif
72#ifndef ELOOP
73# define ELOOP EDOM
74#endif
73 75
74static void eio_destroy (eio_req *req); 76static void eio_destroy (eio_req *req);
75 77
76#ifndef EIO_FINISH 78#ifndef EIO_FINISH
77# define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0 79# define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0
96 98
97#define EIO_ENOSYS() EIO_ERRNO (ENOSYS, -1) 99#define EIO_ENOSYS() EIO_ERRNO (ENOSYS, -1)
98 100
99#ifdef _WIN32 101#ifdef _WIN32
100 102
103 #undef PAGESIZE
101 #define PAGESIZE 4096 /* GetSystemInfo? */ 104 #define PAGESIZE 4096 /* GetSystemInfo? */
102 105
106 #ifdef EIO_STRUCT_STATI64
103 #define stat(path,buf) _stati64 (path,buf) 107 #define stat(path,buf) _stati64 (path,buf)
108 #define fstat(fd,buf) _fstati64 (path,buf)
109 #endif
104 #define lstat(path,buf) stat (path,buf) 110 #define lstat(path,buf) stat (path,buf)
105 #define fstat(fd,buf) _fstati64 (path,buf)
106 #define fsync(fd) (FlushFileBuffers (EIO_FD_TO_WIN32_HANDLE (fd)) ? 0 : EIO_ERRNO (EBADF, -1)) 111 #define fsync(fd) (FlushFileBuffers ((HANDLE)EIO_FD_TO_WIN32_HANDLE (fd)) ? 0 : EIO_ERRNO (EBADF, -1))
107 #define mkdir(path,mode) _mkdir (path) 112 #define mkdir(path,mode) _mkdir (path)
108 #define link(old,neu) (CreateHardLink (neu, old, 0) ? 0 : EIO_ERRNO (ENOENT, -1)) 113 #define link(old,neu) (CreateHardLink (neu, old, 0) ? 0 : EIO_ERRNO (ENOENT, -1))
109 114
115 #define chmod(path,mode) _chmod (path, mode)
116 #define dup(fd) _dup (fd)
117 #define dup2(fd1,fd2) _dup2 (fd1, fd2)
118
119 #define fchmod(fd,mode) EIO_ENOSYS ()
110 #define chown(path,uid,gid) EIO_ENOSYS () 120 #define chown(path,uid,gid) EIO_ENOSYS ()
111 #define fchown(fd,uid,gid) EIO_ENOSYS () 121 #define fchown(fd,uid,gid) EIO_ENOSYS ()
112 #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */ 122 #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */
113 #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */ 123 #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */
114 #define mknod(path,mode,dev) EIO_ENOSYS () 124 #define mknod(path,mode,dev) EIO_ENOSYS ()
115 #define sync() EIO_ENOSYS () 125 #define sync() EIO_ENOSYS ()
126 #define readlink(path,buf,s) EIO_ENOSYS ()
127 #define statvfs(path,buf) EIO_ENOSYS ()
128 #define fstatvfs(fd,buf) EIO_ENOSYS ()
116 129
117 /* we could even stat and see if it exists */ 130 /* we could even stat and see if it exists */
118 static int 131 static int
119 symlink (const char *old, const char *neu) 132 symlink (const char *old, const char *neu)
120 { 133 {
134 #if WINVER >= 0x0600
121 if (CreateSymbolicLink (neu, old, 1)) 135 if (CreateSymbolicLink (neu, old, 1))
122 return 0; 136 return 0;
123 137
124 if (CreateSymbolicLink (neu, old, 0)) 138 if (CreateSymbolicLink (neu, old, 0))
125 return 0; 139 return 0;
140 #endif
126 141
127 return EIO_ERRNO (ENOENT, -1); 142 return EIO_ERRNO (ENOENT, -1);
128 } 143 }
144
145 /* POSIX API only */
146 #define CreateHardLink(neu,old,flags) 0
147 #define CreateSymbolicLink(neu,old,flags) 0
148
149 struct statvfs
150 {
151 int dummy;
152 };
153
154 #define DT_DIR EIO_DT_DIR
155 #define DT_REG EIO_DT_REG
156 #define D_NAME(entp) entp.cFileName
157 #define D_TYPE(entp) (entp.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ? DT_DIR : DT_REG)
129 158
130#else 159#else
131 160
132 #include <sys/time.h> 161 #include <sys/time.h>
133 #include <sys/select.h> 162 #include <sys/select.h>
138 #include <dirent.h> 167 #include <dirent.h>
139 168
140 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES 169 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES
141 #include <sys/mman.h> 170 #include <sys/mman.h>
142 #endif 171 #endif
172
173 #define D_NAME(entp) entp->d_name
143 174
144 /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ 175 /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */
145 #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ 176 #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__
146 #define _DIRENT_HAVE_D_TYPE /* sigh */ 177 #define _DIRENT_HAVE_D_TYPE /* sigh */
147 #define D_INO(de) (de)->d_fileno 178 #define D_INO(de) (de)->d_fileno
185#endif 216#endif
186#ifndef D_INO 217#ifndef D_INO
187# define D_INO(de) 0 218# define D_INO(de) 0
188#endif 219#endif
189#ifndef D_NAMLEN 220#ifndef D_NAMLEN
190# define D_NAMLEN(de) strlen ((de)->d_name) 221# define D_NAMLEN(entp) strlen (D_NAME (entp))
191#endif 222#endif
192 223
193/* used for struct dirent, AIX doesn't provide it */ 224/* used for struct dirent, AIX doesn't provide it */
194#ifndef NAME_MAX 225#ifndef NAME_MAX
195# define NAME_MAX 4096 226# define NAME_MAX 4096
229#define ETP_WORKER_CLEAR(req) \ 260#define ETP_WORKER_CLEAR(req) \
230 if (wrk->dbuf) \ 261 if (wrk->dbuf) \
231 { \ 262 { \
232 free (wrk->dbuf); \ 263 free (wrk->dbuf); \
233 wrk->dbuf = 0; \ 264 wrk->dbuf = 0; \
234 } \
235 \
236 if (wrk->dirp) \
237 { \
238 closedir (wrk->dirp); \
239 wrk->dirp = 0; \
240 } 265 }
241 266
242#define ETP_WORKER_COMMON \ 267#define ETP_WORKER_COMMON \
243 void *dbuf; \ 268 void *dbuf;
244 DIR *dirp;
245 269
246/*****************************************************************************/ 270/*****************************************************************************/
247 271
248#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) 272#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1)
249 273
375} etp_reqq; 399} etp_reqq;
376 400
377static etp_reqq req_queue; 401static etp_reqq req_queue;
378static etp_reqq res_queue; 402static etp_reqq res_queue;
379 403
404static void ecb_noinline ecb_cold
405reqq_init (etp_reqq *q)
406{
407 int pri;
408
409 for (pri = 0; pri < ETP_NUM_PRI; ++pri)
410 q->qs[pri] = q->qe[pri] = 0;
411
412 q->size = 0;
413}
414
380static int ecb_noinline 415static int ecb_noinline
381reqq_push (etp_reqq *q, ETP_REQ *req) 416reqq_push (etp_reqq *q, ETP_REQ *req)
382{ 417{
383 int pri = req->pri; 418 int pri = req->pri;
384 req->next = 0; 419 req->next = 0;
431 X_MUTEX_CREATE (reqlock); 466 X_MUTEX_CREATE (reqlock);
432 X_COND_CREATE (reqwait); 467 X_COND_CREATE (reqwait);
433} 468}
434 469
435static void ecb_cold 470static void ecb_cold
436etp_atfork_prepare (void)
437{
438}
439
440static void ecb_cold
441etp_atfork_parent (void)
442{
443}
444
445static void ecb_cold
446etp_atfork_child (void) 471etp_atfork_child (void)
447{ 472{
448 ETP_REQ *prv; 473 reqq_init (&req_queue);
474 reqq_init (&res_queue);
449 475
450 while ((prv = reqq_shift (&req_queue))) 476 wrk_first.next =
451 ETP_DESTROY (prv); 477 wrk_first.prev = &wrk_first;
452
453 while ((prv = reqq_shift (&res_queue)))
454 ETP_DESTROY (prv);
455
456 while (wrk_first.next != &wrk_first)
457 {
458 etp_worker *wrk = wrk_first.next;
459
460 if (wrk->req)
461 ETP_DESTROY (wrk->req);
462
463 etp_worker_clear (wrk);
464 etp_worker_free (wrk);
465 }
466 478
467 started = 0; 479 started = 0;
468 idle = 0; 480 idle = 0;
469 nreqs = 0; 481 nreqs = 0;
470 nready = 0; 482 nready = 0;
475 487
476static void ecb_cold 488static void ecb_cold
477etp_once_init (void) 489etp_once_init (void)
478{ 490{
479 etp_thread_init (); 491 etp_thread_init ();
480 X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child); 492 X_THREAD_ATFORK (0, 0, etp_atfork_child);
481} 493}
482 494
483static int ecb_cold 495static int ecb_cold
484etp_init (void (*want_poll)(void), void (*done_poll)(void)) 496etp_init (void (*want_poll)(void), void (*done_poll)(void))
485{ 497{
876# undef pread 888# undef pread
877# undef pwrite 889# undef pwrite
878# define pread eio__pread 890# define pread eio__pread
879# define pwrite eio__pwrite 891# define pwrite eio__pwrite
880 892
881static ssize_t 893static eio_ssize_t
882eio__pread (int fd, void *buf, size_t count, off_t offset) 894eio__pread (int fd, void *buf, size_t count, off_t offset)
883{ 895{
884 ssize_t res; 896 eio_ssize_t res;
885 off_t ooffset; 897 off_t ooffset;
886 898
887 X_LOCK (preadwritelock); 899 X_LOCK (preadwritelock);
888 ooffset = lseek (fd, 0, SEEK_CUR); 900 ooffset = lseek (fd, 0, SEEK_CUR);
889 lseek (fd, offset, SEEK_SET); 901 lseek (fd, offset, SEEK_SET);
892 X_UNLOCK (preadwritelock); 904 X_UNLOCK (preadwritelock);
893 905
894 return res; 906 return res;
895} 907}
896 908
897static ssize_t 909static eio_ssize_t
898eio__pwrite (int fd, void *buf, size_t count, off_t offset) 910eio__pwrite (int fd, void *buf, size_t count, off_t offset)
899{ 911{
900 ssize_t res; 912 eio_ssize_t res;
901 off_t ooffset; 913 off_t ooffset;
902 914
903 X_LOCK (preadwritelock); 915 X_LOCK (preadwritelock);
904 ooffset = lseek (fd, 0, SEEK_CUR); 916 ooffset = lseek (fd, 0, SEEK_CUR);
905 lseek (fd, offset, SEEK_SET); 917 lseek (fd, offset, SEEK_SET);
994 1006
995#if !HAVE_READAHEAD 1007#if !HAVE_READAHEAD
996# undef readahead 1008# undef readahead
997# define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) 1009# define readahead(fd,offset,count) eio__readahead (fd, offset, count, self)
998 1010
999static ssize_t 1011static eio_ssize_t
1000eio__readahead (int fd, off_t offset, size_t count, etp_worker *self) 1012eio__readahead (int fd, off_t offset, size_t count, etp_worker *self)
1001{ 1013{
1002 size_t todo = count; 1014 size_t todo = count;
1003 dBUF; 1015 dBUF;
1004 1016
1016} 1028}
1017 1029
1018#endif 1030#endif
1019 1031
1020/* sendfile always needs emulation */ 1032/* sendfile always needs emulation */
1021static ssize_t 1033static eio_ssize_t
1022eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self) 1034eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self)
1023{ 1035{
1024 ssize_t written = 0; 1036 eio_ssize_t written = 0;
1025 ssize_t res; 1037 eio_ssize_t res;
1026 1038
1027 if (!count) 1039 if (!count)
1028 return 0; 1040 return 0;
1029 1041
1030 for (;;) 1042 for (;;)
1125 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK 1137 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
1126 /* BSDs */ 1138 /* BSDs */
1127#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */ 1139#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */
1128 || errno == ENOTSUP 1140 || errno == ENOTSUP
1129#endif 1141#endif
1142#ifdef EOPNOTSUPP /* windows */
1130 || errno == EOPNOTSUPP /* BSDs */ 1143 || errno == EOPNOTSUPP /* BSDs */
1144#endif
1131#if __solaris 1145#if __solaris
1132 || errno == EAFNOSUPPORT || errno == EPROTOTYPE 1146 || errno == EAFNOSUPPORT || errno == EPROTOTYPE
1133#endif 1147#endif
1134 ) 1148 )
1135 ) 1149 )
1139 1153
1140 res = 0; 1154 res = 0;
1141 1155
1142 while (count) 1156 while (count)
1143 { 1157 {
1144 ssize_t cnt; 1158 eio_ssize_t cnt;
1145 1159
1146 cnt = pread (ifd, eio_buf, count > EIO_BUFSIZE ? EIO_BUFSIZE : count, offset); 1160 cnt = pread (ifd, eio_buf, count > EIO_BUFSIZE ? EIO_BUFSIZE : count, offset);
1147 1161
1148 if (cnt <= 0) 1162 if (cnt <= 0)
1149 { 1163 {
1197 /* round up length */ 1211 /* round up length */
1198 *length = (*length + mask) & ~mask; 1212 *length = (*length + mask) & ~mask;
1199} 1213}
1200 1214
1201#if !_POSIX_MEMLOCK 1215#if !_POSIX_MEMLOCK
1202# define eio__mlockall(a) eio_nosyscall() 1216# define eio__mlockall(a) EIO_ENOSYS ()
1203#else 1217#else
1204 1218
1205static int 1219static int
1206eio__mlockall (int flags) 1220eio__mlockall (int flags)
1207{ 1221{
1357 res += strlen (res); 1371 res += strlen (res);
1358 } 1372 }
1359 1373
1360 while (*rel) 1374 while (*rel)
1361 { 1375 {
1362 ssize_t len, linklen; 1376 eio_ssize_t len, linklen;
1363 char *beg = rel; 1377 char *beg = rel;
1364 1378
1365 while (*rel && *rel != '/') 1379 while (*rel && *rel != '/')
1366 ++rel; 1380 ++rel;
1367 1381
1460 1474
1461#define EIO_SORT_CUTOFF 30 /* quite high, but performs well on many filesystems */ 1475#define EIO_SORT_CUTOFF 30 /* quite high, but performs well on many filesystems */
1462#define EIO_SORT_FAST 60 /* when to only use insertion sort */ 1476#define EIO_SORT_FAST 60 /* when to only use insertion sort */
1463 1477
1464static void 1478static void
1465eio_dent_radix_sort (eio_dirent *dents, int size, signed char score_bits, ino_t inode_bits) 1479eio_dent_radix_sort (eio_dirent *dents, int size, signed char score_bits, eio_ino_t inode_bits)
1466{ 1480{
1467 unsigned char bits [9 + sizeof (ino_t) * 8]; 1481 unsigned char bits [9 + sizeof (eio_ino_t) * 8];
1468 unsigned char *bit = bits; 1482 unsigned char *bit = bits;
1469 1483
1470 assert (CHAR_BIT == 8); 1484 assert (CHAR_BIT == 8);
1471 assert (sizeof (eio_dirent) * 8 < 256); 1485 assert (sizeof (eio_dirent) * 8 < 256);
1472 assert (offsetof (eio_dirent, inode)); /* we use bit #0 as sentinel */ 1486 assert (offsetof (eio_dirent, inode)); /* we use bit #0 as sentinel */
1474 1488
1475 if (size <= EIO_SORT_FAST) 1489 if (size <= EIO_SORT_FAST)
1476 return; 1490 return;
1477 1491
1478 /* first prepare an array of bits to test in our radix sort */ 1492 /* first prepare an array of bits to test in our radix sort */
1479 /* try to take endianness into account, as well as differences in ino_t sizes */ 1493 /* try to take endianness into account, as well as differences in eio_ino_t sizes */
1480 /* inode_bits must contain all inodes ORed together */ 1494 /* inode_bits must contain all inodes ORed together */
1481 /* which is used to skip bits that are 0 everywhere, which is very common */ 1495 /* which is used to skip bits that are 0 everywhere, which is very common */
1482 { 1496 {
1483 ino_t endianness; 1497 eio_ino_t endianness;
1484 int i, j; 1498 int i, j;
1485 1499
1486 /* we store the byte offset of byte n into byte n of "endianness" */ 1500 /* we store the byte offset of byte n into byte n of "endianness" */
1487 for (i = 0; i < sizeof (ino_t); ++i) 1501 for (i = 0; i < sizeof (eio_ino_t); ++i)
1488 ((unsigned char *)&endianness)[i] = i; 1502 ((unsigned char *)&endianness)[i] = i;
1489 1503
1490 *bit++ = 0; 1504 *bit++ = 0;
1491 1505
1492 for (i = 0; i < sizeof (ino_t); ++i) 1506 for (i = 0; i < sizeof (eio_ino_t); ++i)
1493 { 1507 {
1494 /* shifting off the byte offsets out of "endianness" */ 1508 /* shifting off the byte offsets out of "endianness" */
1495 int offs = (offsetof (eio_dirent, inode) + (endianness & 0xff)) * 8; 1509 int offs = (offsetof (eio_dirent, inode) + (endianness & 0xff)) * 8;
1496 endianness >>= 8; 1510 endianness >>= 8;
1497 1511
1498 for (j = 0; j < 8; ++j) 1512 for (j = 0; j < 8; ++j)
1499 if (inode_bits & (((ino_t)1) << (i * 8 + j))) 1513 if (inode_bits & (((eio_ino_t)1) << (i * 8 + j)))
1500 *bit++ = offs + j; 1514 *bit++ = offs + j;
1501 } 1515 }
1502 1516
1503 for (j = 0; j < 8; ++j) 1517 for (j = 0; j < 8; ++j)
1504 if (score_bits & (1 << j)) 1518 if (score_bits & (1 << j))
1505 *bit++ = offsetof (eio_dirent, score) * 8 + j; 1519 *bit++ = offsetof (eio_dirent, score) * 8 + j;
1506 } 1520 }
1507 1521
1508 /* now actually do the sorting (a variant of MSD radix sort) */ 1522 /* now actually do the sorting (a variant of MSD radix sort) */
1509 { 1523 {
1510 eio_dirent *base_stk [9 + sizeof (ino_t) * 8], *base; 1524 eio_dirent *base_stk [9 + sizeof (eio_ino_t) * 8], *base;
1511 eio_dirent *end_stk [9 + sizeof (ino_t) * 8], *end; 1525 eio_dirent *end_stk [9 + sizeof (eio_ino_t) * 8], *end;
1512 unsigned char *bit_stk [9 + sizeof (ino_t) * 8]; 1526 unsigned char *bit_stk [9 + sizeof (eio_ino_t) * 8];
1513 int stk_idx = 0; 1527 int stk_idx = 0;
1514 1528
1515 base_stk [stk_idx] = dents; 1529 base_stk [stk_idx] = dents;
1516 end_stk [stk_idx] = dents + size; 1530 end_stk [stk_idx] = dents + size;
1517 bit_stk [stk_idx] = bit - 1; 1531 bit_stk [stk_idx] = bit - 1;
1596 } 1610 }
1597 } 1611 }
1598} 1612}
1599 1613
1600static void 1614static void
1601eio_dent_sort (eio_dirent *dents, int size, signed char score_bits, ino_t inode_bits) 1615eio_dent_sort (eio_dirent *dents, int size, signed char score_bits, eio_ino_t inode_bits)
1602{ 1616{
1603 if (size <= 1) 1617 if (size <= 1)
1604 return; /* our insertion sort relies on size > 0 */ 1618 return; /* our insertion sort relies on size > 0 */
1605 1619
1606 /* first we use a radix sort, but only for dirs >= EIO_SORT_FAST */ 1620 /* first we use a radix sort, but only for dirs >= EIO_SORT_FAST */
1614 1628
1615/* read a full directory */ 1629/* read a full directory */
1616static void 1630static void
1617eio__scandir (eio_req *req, etp_worker *self) 1631eio__scandir (eio_req *req, etp_worker *self)
1618{ 1632{
1619 DIR *dirp;
1620 EIO_STRUCT_DIRENT *entp;
1621 char *name, *names; 1633 char *name, *names;
1622 int namesalloc = 4096; 1634 int namesalloc = 4096 - sizeof (void *) * 4;
1623 int namesoffs = 0; 1635 int namesoffs = 0;
1624 int flags = req->int1; 1636 int flags = req->int1;
1625 eio_dirent *dents = 0; 1637 eio_dirent *dents = 0;
1626 int dentalloc = 128; 1638 int dentalloc = 128;
1627 int dentoffs = 0; 1639 int dentoffs = 0;
1628 ino_t inode_bits = 0; 1640 eio_ino_t inode_bits = 0;
1641#ifdef _WIN32
1642 HANDLE dirp;
1643 WIN32_FIND_DATA entp;
1644#else
1645 DIR *dirp;
1646 EIO_STRUCT_DIRENT *entp;
1647#endif
1629 1648
1630 req->result = -1; 1649 req->result = -1;
1631 1650
1632 if (!(flags & EIO_READDIR_DENTS)) 1651 if (!(flags & EIO_READDIR_DENTS))
1633 flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER); 1652 flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER);
1634 1653
1635 X_LOCK (wrklock); 1654#ifdef _WIN32
1636 /* the corresponding closedir is in ETP_WORKER_CLEAR */ 1655 {
1656 char *path = malloc (MAX_PATH);
1657 _snprintf (path, MAX_PATH, "%s/*", (const char *)req->ptr1);
1658 dirp = FindFirstFile (path, &entp);
1659 free (path);
1660
1661 if (!dirp)
1662 {
1663 switch (GetLastError ())
1664 {
1665 case ERROR_FILE_NOT_FOUND:
1666 req->result = 0;
1667 break;
1668
1669 case ERROR_PATH_NOT_FOUND:
1670 case ERROR_NO_MORE_FILES:
1671 errno = ENOENT;
1672 break;
1673
1674 case ERROR_NOT_ENOUGH_MEMORY:
1675 errno = ENOMEM;
1676 break;
1677
1678 default:
1679 errno = EINVAL;
1680 break;
1681 }
1682
1683 }
1684 }
1685#else
1637 self->dirp = dirp = opendir (req->ptr1); 1686 dirp = opendir (req->ptr1);
1687#endif
1638 1688
1639 if (req->flags & EIO_FLAG_PTR1_FREE) 1689 if (req->flags & EIO_FLAG_PTR1_FREE)
1640 free (req->ptr1); 1690 free (req->ptr1);
1641 1691
1642 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE; 1692 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE;
1643 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; 1693 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0;
1644 req->ptr2 = names = malloc (namesalloc); 1694 req->ptr2 = names = malloc (namesalloc);
1645 X_UNLOCK (wrklock);
1646 1695
1647 if (dirp && names && (!flags || dents)) 1696 if (dirp && names && (!flags || dents))
1648 for (;;) 1697 for (;;)
1649 { 1698 {
1699 int more;
1700
1701#ifdef _WIN32
1702 more = dirp;
1703#else
1650 errno = 0; 1704 errno = 0;
1651 entp = readdir (dirp); 1705 entp = readdir (dirp);
1706 more = entp;
1707#endif
1652 1708
1653 if (!entp) 1709 if (!more)
1654 { 1710 {
1711#ifndef _WIN32
1712 int old_errno = errno;
1713 closedir (dirp);
1714 errno = old_errno;
1715
1655 if (errno) 1716 if (errno)
1656 break; 1717 break;
1718#endif
1657 1719
1658 /* sort etc. */ 1720 /* sort etc. */
1659 req->int1 = flags; 1721 req->int1 = flags;
1660 req->result = dentoffs; 1722 req->result = dentoffs;
1661 1723
1690 1752
1691 break; 1753 break;
1692 } 1754 }
1693 1755
1694 /* now add the entry to our list(s) */ 1756 /* now add the entry to our list(s) */
1695 name = entp->d_name; 1757 name = D_NAME (entp);
1696 1758
1697 /* skip . and .. entries */ 1759 /* skip . and .. entries */
1698 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 1760 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
1699 { 1761 {
1700 int len = D_NAMLEN (entp) + 1; 1762 int len = D_NAMLEN (entp) + 1;
1701 1763
1702 while (ecb_expect_false (namesoffs + len > namesalloc)) 1764 while (ecb_expect_false (namesoffs + len > namesalloc))
1703 { 1765 {
1704 namesalloc *= 2; 1766 namesalloc *= 2;
1705 X_LOCK (wrklock);
1706 req->ptr2 = names = realloc (names, namesalloc); 1767 req->ptr2 = names = realloc (names, namesalloc);
1707 X_UNLOCK (wrklock);
1708 1768
1709 if (!names) 1769 if (!names)
1710 break; 1770 break;
1711 } 1771 }
1712 1772
1717 struct eio_dirent *ent; 1777 struct eio_dirent *ent;
1718 1778
1719 if (ecb_expect_false (dentoffs == dentalloc)) 1779 if (ecb_expect_false (dentoffs == dentalloc))
1720 { 1780 {
1721 dentalloc *= 2; 1781 dentalloc *= 2;
1722 X_LOCK (wrklock);
1723 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent)); 1782 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent));
1724 X_UNLOCK (wrklock);
1725 1783
1726 if (!dents) 1784 if (!dents)
1727 break; 1785 break;
1728 } 1786 }
1729 1787
1809 if (EIO_CANCELLED (req)) 1867 if (EIO_CANCELLED (req))
1810 { 1868 {
1811 errno = ECANCELED; 1869 errno = ECANCELED;
1812 break; 1870 break;
1813 } 1871 }
1872
1873#ifdef _WIN32
1874 if (!FindNextFile (dirp, &entp))
1875 {
1876 FindClose (dirp);
1877 dirp = 0;
1878 }
1879#endif
1814 } 1880 }
1815} 1881}
1816 1882
1817/*****************************************************************************/ 1883/*****************************************************************************/
1818 1884
1834X_THREAD_PROC (etp_proc) 1900X_THREAD_PROC (etp_proc)
1835{ 1901{
1836 ETP_REQ *req; 1902 ETP_REQ *req;
1837 struct timespec ts; 1903 struct timespec ts;
1838 etp_worker *self = (etp_worker *)thr_arg; 1904 etp_worker *self = (etp_worker *)thr_arg;
1839 int timeout;
1840 1905
1841 /* try to distribute timeouts somewhat evenly */ 1906 /* try to distribute timeouts somewhat evenly */
1842 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); 1907 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
1843 1908
1844 for (;;) 1909 for (;;)
2178eio_req *eio_fchmod (int fd, mode_t mode, int pri, eio_cb cb, void *data) 2243eio_req *eio_fchmod (int fd, mode_t mode, int pri, eio_cb cb, void *data)
2179{ 2244{
2180 REQ (EIO_FCHMOD); req->int1 = fd; req->int2 = (long)mode; SEND; 2245 REQ (EIO_FCHMOD); req->int1 = fd; req->int2 = (long)mode; SEND;
2181} 2246}
2182 2247
2183eio_req *eio_fchown (int fd, uid_t uid, gid_t gid, int pri, eio_cb cb, void *data) 2248eio_req *eio_fchown (int fd, eio_uid_t uid, eio_gid_t gid, int pri, eio_cb cb, void *data)
2184{ 2249{
2185 REQ (EIO_FCHOWN); req->int1 = fd; req->int2 = (long)uid; req->int3 = (long)gid; SEND; 2250 REQ (EIO_FCHOWN); req->int1 = fd; req->int2 = (long)uid; req->int3 = (long)gid; SEND;
2186} 2251}
2187 2252
2188eio_req *eio_dup2 (int fd, int fd2, int pri, eio_cb cb, void *data) 2253eio_req *eio_dup2 (int fd, int fd2, int pri, eio_cb cb, void *data)
2208eio_req *eio_truncate (const char *path, off_t offset, int pri, eio_cb cb, void *data) 2273eio_req *eio_truncate (const char *path, off_t offset, int pri, eio_cb cb, void *data)
2209{ 2274{
2210 REQ (EIO_TRUNCATE); PATH; req->offs = offset; SEND; 2275 REQ (EIO_TRUNCATE); PATH; req->offs = offset; SEND;
2211} 2276}
2212 2277
2213eio_req *eio_chown (const char *path, uid_t uid, gid_t gid, int pri, eio_cb cb, void *data) 2278eio_req *eio_chown (const char *path, eio_uid_t uid, eio_gid_t gid, int pri, eio_cb cb, void *data)
2214{ 2279{
2215 REQ (EIO_CHOWN); PATH; req->int2 = (long)uid; req->int3 = (long)gid; SEND; 2280 REQ (EIO_CHOWN); PATH; req->int2 = (long)uid; req->int3 = (long)gid; SEND;
2216} 2281}
2217 2282
2218eio_req *eio_chmod (const char *path, mode_t mode, int pri, eio_cb cb, void *data) 2283eio_req *eio_chmod (const char *path, mode_t mode, int pri, eio_cb cb, void *data)
2365} 2430}
2366 2431
2367/*****************************************************************************/ 2432/*****************************************************************************/
2368/* misc garbage */ 2433/* misc garbage */
2369 2434
2370ssize_t 2435eio_ssize_t
2371eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) 2436eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count)
2372{ 2437{
2373 etp_worker wrk; 2438 etp_worker wrk;
2374 ssize_t ret; 2439 eio_ssize_t ret;
2375 2440
2376 wrk.dbuf = 0; 2441 wrk.dbuf = 0;
2377 2442
2378 ret = eio__sendfile (ofd, ifd, offset, count, &wrk); 2443 ret = eio__sendfile (ofd, ifd, offset, count, &wrk);
2379 2444

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines