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

Comparing cvsroot/libeio/eio.c (file contents):
Revision 1.33 by root, Sat Jun 6 19:44:17 2009 UTC vs.
Revision 1.37 by root, Fri Jun 12 16:48:08 2009 UTC

73#else 73#else
74 74
75# include "config.h" 75# include "config.h"
76# include <sys/time.h> 76# include <sys/time.h>
77# include <sys/select.h> 77# include <sys/select.h>
78# include <sys/mman.h>
78# include <unistd.h> 79# include <unistd.h>
79# include <utime.h> 80# include <utime.h>
80# include <signal.h> 81# include <signal.h>
81# include <dirent.h> 82# include <dirent.h>
82 83
989 } 990 }
990 991
991 return res; 992 return res;
992} 993}
993 994
994static int 995static signed char
995eio_dent_cmp (const void *a_, const void *b_) 996eio_dent_cmp (const eio_dirent *a, const eio_dirent *b)
996{ 997{
997 const eio_dirent *a = (const eio_dirent *)a_; 998 return b->score - a->score ? b->score - a->score /* works because our signed char is always 0..100 */
998 const eio_dirent *b = (const eio_dirent *)b_;
999
1000 return (int)b->score - (int)a->score ? (int)b->score - (int)a->score
1001 : a->inode < b->inode ? -1 : a->inode > b->inode ? 1 : 0; /* int might be < ino_t */ 999 : a->inode < b->inode ? -1 : a->inode > b->inode ? 1 : 0;
1000}
1001
1002#define EIO_QSORT_CUTOFF 20 /* quite high, but performs well on many filesystems */
1003
1004static void
1005eio_dent_sort (eio_dirent *dents, int size)
1006{
1007 /* should be good for 2**31 entries */
1008 struct rng { int l, r; } rng [32];
1009 int i, j;
1010
1011 i = 0;
1012 rng[0].l = 0;
1013 rng[0].r = size;
1014
1015 while (expect_true (i >= 0))
1016 {
1017 int L = rng [i].l;
1018 int R = rng [i].r - 1;
1019
1020 if (expect_false (L + EIO_QSORT_CUTOFF < R))
1021 {
1022 eio_dirent piv = dents [L];
1023
1024 while (L < R)
1025 {
1026 while (eio_dent_cmp (&dents [R], &piv) >= 0 && L < R)
1027 --R;
1028
1029 if (L < R)
1030 dents [L++] = dents [R];
1031
1032 while (eio_dent_cmp (&dents [L], &piv) <= 0 && L < R)
1033 ++L;
1034
1035 if (L < R)
1036 dents [R--] = dents [L];
1037 }
1038
1039 dents [L] = piv;
1040
1041 ++i;
1042 rng [i].l = L + 1;
1043 rng [i].r = rng [i - 1].r;
1044 rng [i - 1].r = L;
1045
1046 if (rng [i].r - rng [i].l > rng [i - 1].r - rng [i - 1].l)
1047 {
1048 struct rng t;
1049
1050 t = rng [i]; rng [i] = rng [i - 1]; rng [i - 1] = t;
1051 }
1052 }
1053 else
1054 --i;
1055 }
1056
1057 /* use a simple insertion sort at the end */
1058 for (i = 1; i < size; ++i)
1059 {
1060 eio_dirent value = dents [i];
1061
1062 for (j = i - 1; j >= 0 && eio_dent_cmp (&dents [j], &value) > 0; --j)
1063 dents [j + 1] = dents [j];
1064
1065 dents [j + 1] = value;
1066 }
1002} 1067}
1003 1068
1004/* read a full directory */ 1069/* read a full directory */
1005static void 1070static void
1006eio__scandir (eio_req *req, etp_worker *self) 1071eio__scandir (eio_req *req, etp_worker *self)
1007{ 1072{
1008 DIR *dirp; 1073 DIR *dirp;
1009 EIO_STRUCT_DIRENT *entp; 1074 EIO_STRUCT_DIRENT *entp;
1010 unsigned char *name, *names; 1075 char *name, *names;
1011 int namesalloc = 4096; 1076 int namesalloc = 4096;
1012 int namesoffs = 0; 1077 int namesoffs = 0;
1013 int flags = req->int1; 1078 int flags = req->int1;
1014 eio_dirent *dents = 0; 1079 eio_dirent *dents = 0;
1015 int dentalloc = 128; 1080 int dentalloc = 128;
1051 (--ent)->name = names + (size_t)ent->name; 1116 (--ent)->name = names + (size_t)ent->name;
1052 } 1117 }
1053 1118
1054 if (flags & EIO_READDIR_STAT_ORDER 1119 if (flags & EIO_READDIR_STAT_ORDER
1055 || !(~flags & (EIO_READDIR_DIRS_FIRST | EIO_READDIR_FOUND_UNKNOWN))) 1120 || !(~flags & (EIO_READDIR_DIRS_FIRST | EIO_READDIR_FOUND_UNKNOWN)))
1056 { 1121 eio_dent_sort (dents, dentoffs); /* score depends of DIRS_FIRST */
1057 /* pray your qsort doesn't use quicksort */
1058 qsort (dents, dentoffs, sizeof (*dents), eio_dent_cmp); /* score depends of DIRS_FIRST */
1059 }
1060 else if (flags & EIO_READDIR_DIRS_FIRST) 1122 else if (flags & EIO_READDIR_DIRS_FIRST)
1061 { 1123 {
1062 /* in this case, all is known, and we just put dirs first and sort them */ 1124 /* in this case, all is known, and we just put dirs first and sort them */
1063 eio_dirent *ent = dents + dentoffs; 1125 eio_dirent *ent = dents + dentoffs;
1064 eio_dirent *dir = dents; 1126 eio_dirent *dir = dents;
1065 1127
1128 /* now move dirs to the front, and non-dirs to the back */
1129 /* by walking from both sides and swapping if necessary */
1066 while (ent > dir) 1130 while (ent > dir)
1067 { 1131 {
1068 if (dir->type == DT_DIR) 1132 if (dir->type == DT_DIR)
1069 ++dir; 1133 ++dir;
1070 else 1134 else
1081 } 1145 }
1082 } 1146 }
1083 } 1147 }
1084 1148
1085 /* now sort the dirs only */ 1149 /* now sort the dirs only */
1086 qsort (dents, dir - dents, sizeof (*dents), eio_dent_cmp); 1150 eio_dent_sort (dents, dir - dents);
1087 } 1151 }
1088 1152
1089 /* only provide the names array unless DENTS is specified */ 1153 /* only provide the names array unless DENTS is specified */
1090 if (!(flags & EIO_READDIR_DENTS)) 1154 if (!(flags & EIO_READDIR_DENTS))
1091 { 1155 {
1199 if (ent->type == EIO_DT_UNKNOWN) 1263 if (ent->type == EIO_DT_UNKNOWN)
1200 { 1264 {
1201 if (*name == '.') /* leading dots are likely directories, and, in any case, rare */ 1265 if (*name == '.') /* leading dots are likely directories, and, in any case, rare */
1202 ent->score = 98; 1266 ent->score = 98;
1203 else if (!strchr (name, '.')) /* absense of dots indicate likely dirs */ 1267 else if (!strchr (name, '.')) /* absense of dots indicate likely dirs */
1204 ent->score = len <= 2 ? len + 60 : len <= 4 ? 50 : len <= 7 ? 40 : 10; /* shorter == more likely dir, but avoid too many classes */ 1268 ent->score = len <= 2 ? len + 6 : len <= 4 ? 5 : len <= 7 ? 4 : 1; /* shorter == more likely dir, but avoid too many classes */
1205 } 1269 }
1206 else if (ent->type == EIO_DT_DIR) 1270 else if (ent->type == EIO_DT_DIR)
1207 ent->score = 100; 1271 ent->score = 100;
1208 } 1272 }
1209 } 1273 }
1210 1274
1211 namesoffs += len; 1275 namesoffs += len;
1212 ++dentoffs; 1276 ++dentoffs;
1213 } 1277 }
1278
1279 if (EIO_CANCELLED (req))
1280 {
1281 errno = ECANCELED;
1282 break;
1283 }
1214 } 1284 }
1215 else
1216 req->result = -1;
1217} 1285}
1218 1286
1219#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) 1287#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO)
1220# undef msync 1288# undef msync
1221# define msync(a,b,c) ((errno = ENOSYS), -1) 1289# define msync(a,b,c) ((errno = ENOSYS), -1)
1742/* misc garbage */ 1810/* misc garbage */
1743 1811
1744ssize_t eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) 1812ssize_t eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count)
1745{ 1813{
1746 etp_worker wrk; 1814 etp_worker wrk;
1815 ssize_t ret;
1747 1816
1748 wrk.dbuf = 0; 1817 wrk.dbuf = 0;
1749 1818
1750 eio__sendfile (ofd, ifd, offset, count, &wrk); 1819 ret = eio__sendfile (ofd, ifd, offset, count, &wrk);
1751 1820
1752 if (wrk.dbuf) 1821 if (wrk.dbuf)
1753 free (wrk.dbuf); 1822 free (wrk.dbuf);
1754}
1755 1823
1824 return ret;
1825}
1826

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines