… | |
… | |
35 | * and other provisions required by the GPL. If you do not delete the |
35 | * and other provisions required by the GPL. If you do not delete the |
36 | * provisions above, a recipient may use your version of this file under |
36 | * provisions above, a recipient may use your version of this file under |
37 | * either the BSD or the GPL. |
37 | * either the BSD or the GPL. |
38 | */ |
38 | */ |
39 | |
39 | |
|
|
40 | #ifndef _WIN32 |
|
|
41 | # include "config.h" |
|
|
42 | #endif |
|
|
43 | |
40 | #include "eio.h" |
44 | #include "eio.h" |
41 | |
45 | |
42 | #ifdef EIO_STACKSIZE |
46 | #ifdef EIO_STACKSIZE |
43 | # define XTHREAD_STACKSIZE EIO_STACKSIZE |
47 | # define XTHREAD_STACKSIZE EIO_STACKSIZE |
44 | #endif |
48 | #endif |
… | |
… | |
54 | #include <sys/statvfs.h> |
58 | #include <sys/statvfs.h> |
55 | #include <limits.h> |
59 | #include <limits.h> |
56 | #include <fcntl.h> |
60 | #include <fcntl.h> |
57 | #include <assert.h> |
61 | #include <assert.h> |
58 | |
62 | |
|
|
63 | /* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */ |
|
|
64 | /* intptr_t only comes form stdint.h, says idiot openbsd coder */ |
|
|
65 | #if HAVE_STDINT_H |
|
|
66 | # include <stdint.h> |
|
|
67 | #endif |
|
|
68 | |
59 | #ifndef EIO_FINISH |
69 | #ifndef EIO_FINISH |
60 | # define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0 |
70 | # define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0 |
61 | #endif |
71 | #endif |
62 | |
72 | |
63 | #ifndef EIO_DESTROY |
73 | #ifndef EIO_DESTROY |
… | |
… | |
71 | #ifdef _WIN32 |
81 | #ifdef _WIN32 |
72 | |
82 | |
73 | /*doh*/ |
83 | /*doh*/ |
74 | #else |
84 | #else |
75 | |
85 | |
76 | # include "config.h" |
|
|
77 | # include <sys/time.h> |
86 | # include <sys/time.h> |
78 | # include <sys/select.h> |
87 | # include <sys/select.h> |
79 | # include <unistd.h> |
88 | # include <unistd.h> |
80 | # include <utime.h> |
89 | # include <utime.h> |
81 | # include <signal.h> |
90 | # include <signal.h> |
… | |
… | |
1060 | } |
1069 | } |
1061 | |
1070 | |
1062 | static signed char |
1071 | static signed char |
1063 | eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) |
1072 | eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) |
1064 | { |
1073 | { |
1065 | return a->score - b->score ? a->score - b->score /* works because our signed char is always 0..100 */ |
1074 | return a->score - b->score ? a->score - b->score /* works because our signed char is always 0..100 */ |
1066 | : a->inode < b->inode ? -1 : a->inode > b->inode ? 1 : 0; |
1075 | : a->inode < b->inode ? -1 |
|
|
1076 | : a->inode > b->inode ? 1 |
|
|
1077 | : 0; |
1067 | } |
1078 | } |
1068 | |
1079 | |
1069 | #define EIO_DENT_CMP(i,op,j) eio_dent_cmp (&i, &j) op 0 |
1080 | #define EIO_DENT_CMP(i,op,j) eio_dent_cmp (&i, &j) op 0 |
1070 | |
1081 | |
1071 | #define EIO_SORT_CUTOFF 30 /* quite high, but performs well on many filesystems */ |
1082 | #define EIO_SORT_CUTOFF 30 /* quite high, but performs well on many filesystems */ |
… | |
… | |
1077 | unsigned char bits [9 + sizeof (ino_t) * 8]; |
1088 | unsigned char bits [9 + sizeof (ino_t) * 8]; |
1078 | unsigned char *bit = bits; |
1089 | unsigned char *bit = bits; |
1079 | |
1090 | |
1080 | assert (CHAR_BIT == 8); |
1091 | assert (CHAR_BIT == 8); |
1081 | assert (sizeof (eio_dirent) * 8 < 256); |
1092 | assert (sizeof (eio_dirent) * 8 < 256); |
1082 | assert (offsetof (eio_dirent, inode)); /* we use 0 as sentinel */ |
1093 | assert (offsetof (eio_dirent, inode)); /* we use bit #0 as sentinel */ |
1083 | assert (offsetof (eio_dirent, score)); /* we use 0 as sentinel */ |
1094 | assert (offsetof (eio_dirent, score)); /* we use bit #0 as sentinel */ |
1084 | |
1095 | |
1085 | if (size <= EIO_SORT_FAST) |
1096 | if (size <= EIO_SORT_FAST) |
1086 | return; |
1097 | return; |
1087 | |
1098 | |
1088 | /* first prepare an array of bits to test in our radix sort */ |
1099 | /* first prepare an array of bits to test in our radix sort */ |
… | |
… | |
1243 | flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER); |
1254 | flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER); |
1244 | |
1255 | |
1245 | X_LOCK (wrklock); |
1256 | X_LOCK (wrklock); |
1246 | /* the corresponding closedir is in ETP_WORKER_CLEAR */ |
1257 | /* the corresponding closedir is in ETP_WORKER_CLEAR */ |
1247 | self->dirp = dirp = opendir (req->ptr1); |
1258 | self->dirp = dirp = opendir (req->ptr1); |
|
|
1259 | |
1248 | req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE; |
1260 | req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE; |
1249 | req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; |
1261 | req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; |
1250 | req->ptr2 = names = malloc (namesalloc); |
1262 | req->ptr2 = names = malloc (namesalloc); |
1251 | X_UNLOCK (wrklock); |
1263 | X_UNLOCK (wrklock); |
1252 | |
1264 | |
… | |
… | |
1264 | /* sort etc. */ |
1276 | /* sort etc. */ |
1265 | req->int1 = flags; |
1277 | req->int1 = flags; |
1266 | req->result = dentoffs; |
1278 | req->result = dentoffs; |
1267 | |
1279 | |
1268 | if (flags & EIO_READDIR_STAT_ORDER) |
1280 | if (flags & EIO_READDIR_STAT_ORDER) |
1269 | eio_dent_sort (dents, dentoffs, 0, inode_bits); /* sort by inode exclusively */ |
1281 | eio_dent_sort (dents, dentoffs, flags & EIO_READDIR_DIRS_FIRST ? 7 : 0, inode_bits); |
1270 | else if (flags & EIO_READDIR_DIRS_FIRST) |
1282 | else if (flags & EIO_READDIR_DIRS_FIRST) |
1271 | if (flags & EIO_READDIR_FOUND_UNKNOWN) |
1283 | if (flags & EIO_READDIR_FOUND_UNKNOWN) |
1272 | eio_dent_sort (dents, dentoffs, 7, inode_bits); /* sort by score and inode */ |
1284 | eio_dent_sort (dents, dentoffs, 7, inode_bits); /* sort by score and inode */ |
1273 | else |
1285 | else |
1274 | { |
1286 | { |
… | |
… | |
1276 | eio_dirent *oth = dents + dentoffs; |
1288 | eio_dirent *oth = dents + dentoffs; |
1277 | eio_dirent *dir = dents; |
1289 | eio_dirent *dir = dents; |
1278 | |
1290 | |
1279 | /* now partition dirs to the front, and non-dirs to the back */ |
1291 | /* now partition dirs to the front, and non-dirs to the back */ |
1280 | /* by walking from both sides and swapping if necessary */ |
1292 | /* by walking from both sides and swapping if necessary */ |
1281 | /* also clear score, so it doesn't influence sorting */ |
|
|
1282 | while (oth > dir) |
1293 | while (oth > dir) |
1283 | { |
1294 | { |
1284 | if (dir->type == EIO_DT_DIR) |
1295 | if (dir->type == EIO_DT_DIR) |
1285 | ++dir; |
1296 | ++dir; |
1286 | else if ((--oth)->type == EIO_DT_DIR) |
1297 | else if ((--oth)->type == EIO_DT_DIR) |
… | |
… | |
1289 | |
1300 | |
1290 | ++dir; |
1301 | ++dir; |
1291 | } |
1302 | } |
1292 | } |
1303 | } |
1293 | |
1304 | |
1294 | /* now sort the dirs only */ |
1305 | /* now sort the dirs only (dirs all have the same score) */ |
1295 | eio_dent_sort (dents, dir - dents, 0, inode_bits); |
1306 | eio_dent_sort (dents, dir - dents, 0, inode_bits); |
1296 | } |
1307 | } |
1297 | |
1308 | |
1298 | break; |
1309 | break; |
1299 | } |
1310 | } |