… | |
… | |
993 | } |
993 | } |
994 | |
994 | |
995 | static signed char |
995 | static signed char |
996 | eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) |
996 | eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) |
997 | { |
997 | { |
998 | return b->score - a->score ? b->score - a->score /* works because our signed char is always 0..100 */ |
998 | return b->score - a->score ? b->score - a->score /* works because our signed char is always 0..100 */ |
999 | : a->inode < b->inode ? -1 : a->inode > b->inode ? 1 : 0; |
999 | : a->inode < b->inode ? -1 : a->inode > b->inode ? 1 : 0; |
1000 | } |
1000 | } |
1001 | |
1001 | |
|
|
1002 | #define EIO_DENT_CMP(i,op,j) eio_dent_cmp (&i, &j) op 0 |
|
|
1003 | #define EIO_DENT_NUM_SCORES 9 |
|
|
1004 | |
1002 | #define EIO_QSORT_CUTOFF 20 /* quite high, but performs well on many filesystems */ |
1005 | #define EIO_QSORT_CUTOFF 30 /* quite high, but performs well on many filesystems */ |
|
|
1006 | #define EIO_QSORT_SKIP 60 /* when to skip qsort completely */ |
1003 | |
1007 | |
1004 | static void |
1008 | static void |
1005 | eio_dent_sort (eio_dirent *dents, int size) |
1009 | eio_dent_sort (eio_dirent *dents, int size) |
1006 | { |
1010 | { |
1007 | int i, j; |
1011 | if (size <= 1) |
|
|
1012 | return; /* our insertion sort relies on size > 0 */ |
1008 | |
1013 | |
1009 | if (size > EIO_QSORT_CUTOFF * 3) /* skip quicksort for small directories */ |
1014 | if (size > EIO_QSORT_SKIP) /* skip quicksort for small directories */ |
1010 | { |
1015 | { |
1011 | /* first, use quicksort */ |
1016 | /* first, use quicksort */ |
1012 | /* should be good for 2**31 entries */ |
1017 | /* should be good for 2**31 entries */ |
|
|
1018 | int i; |
1013 | struct rng { int l, r; } rng [32]; |
1019 | struct rng { eio_dirent *l, *r; } rng [32]; |
1014 | |
1020 | |
1015 | i = 0; |
1021 | i = 0; |
1016 | rng[0].l = 0; |
1022 | rng[0].l = dents; |
1017 | rng[0].r = size; |
1023 | rng[0].r = dents + size; |
1018 | |
1024 | |
1019 | while (expect_true (i >= 0)) |
1025 | while (expect_true (i >= 0)) |
1020 | { |
1026 | { |
1021 | int L = rng [i].l; |
1027 | eio_dirent *L = rng [i].l; |
1022 | int R = rng [i].r - 1; |
1028 | eio_dirent *R = rng [i].r - 1; |
1023 | |
1029 | |
1024 | if (expect_false (L + EIO_QSORT_CUTOFF < R)) |
1030 | if (expect_false (L + EIO_QSORT_CUTOFF < R)) |
1025 | { |
1031 | { |
1026 | eio_dirent piv = dents [L]; |
1032 | eio_dirent *mid = &dents [((L - dents) + (R - dents)) >> 1]; |
|
|
1033 | eio_dirent piv = *mid; *mid = *L; *L = piv; |
1027 | |
1034 | |
1028 | while (L < R) |
1035 | while (L < R) |
1029 | { |
1036 | { |
1030 | while (eio_dent_cmp (&dents [R], &piv) >= 0 && L < R) |
1037 | while (EIO_DENT_CMP (*R, >=, piv) && L < R) |
1031 | --R; |
1038 | --R; |
1032 | |
1039 | |
1033 | if (L < R) |
1040 | if (L < R) |
1034 | dents [L++] = dents [R]; |
1041 | *L++ = *R; |
1035 | |
1042 | |
1036 | while (eio_dent_cmp (&dents [L], &piv) <= 0 && L < R) |
1043 | while (EIO_DENT_CMP (*L, <=, piv) && L < R) |
1037 | ++L; |
1044 | ++L; |
1038 | |
1045 | |
1039 | if (L < R) |
1046 | if (L < R) |
1040 | dents [R--] = dents [L]; |
1047 | *R-- = *L; |
1041 | } |
1048 | } |
1042 | |
1049 | |
1043 | dents [L] = piv; |
1050 | *L = piv; |
1044 | |
1051 | |
1045 | ++i; |
1052 | ++i; |
1046 | rng [i].l = L + 1; |
1053 | rng [i].l = L + 1; |
1047 | rng [i].r = rng [i - 1].r; |
1054 | rng [i].r = rng [i - 1].r; |
1048 | rng [i - 1].r = L; |
1055 | rng [i - 1].r = L; |
… | |
… | |
1057 | else |
1064 | else |
1058 | --i; |
1065 | --i; |
1059 | } |
1066 | } |
1060 | } |
1067 | } |
1061 | |
1068 | |
1062 | /* use a simple insertion sort at the end */ |
1069 | /* use an insertion sort after qsort, or for small arrays */ |
1063 | for (i = 1; i < size; ++i) |
1070 | /* first move the smallest element to the front, to act as a sentinel */ |
|
|
1071 | { |
|
|
1072 | int i; |
|
|
1073 | eio_dirent *min = dents; |
|
|
1074 | |
|
|
1075 | for (i = size > EIO_QSORT_SKIP ? EIO_QSORT_CUTOFF + 1 : size; --i; ) |
|
|
1076 | if (EIO_DENT_CMP (dents [i], <, *min)) |
|
|
1077 | min = &dents [i]; |
|
|
1078 | |
|
|
1079 | /* swap elements 0 and j (minimum) */ |
1064 | { |
1080 | { |
|
|
1081 | eio_dirent tmp = *dents; *dents = *min; *min = tmp; |
|
|
1082 | } |
|
|
1083 | } |
|
|
1084 | |
|
|
1085 | { |
|
|
1086 | /* then do standard insertion sort */ |
|
|
1087 | eio_dirent *i, *j; |
|
|
1088 | |
|
|
1089 | for (i = dents + 1; i < dents + size; ++i) |
|
|
1090 | { |
1065 | eio_dirent value = dents [i]; |
1091 | eio_dirent value = *i; |
1066 | |
1092 | |
1067 | for (j = i - 1; j >= 0 && eio_dent_cmp (&dents [j], &value) > 0; --j) |
1093 | for (j = i - 1; EIO_DENT_CMP (*j, >, value); --j) |
1068 | dents [j + 1] = dents [j]; |
1094 | j [1] = j [0]; |
1069 | |
1095 | |
1070 | dents [j + 1] = value; |
1096 | j [1] = value; |
1071 | } |
1097 | } |
|
|
1098 | } |
1072 | } |
1099 | } |
1073 | |
1100 | |
1074 | /* read a full directory */ |
1101 | /* read a full directory */ |
1075 | static void |
1102 | static void |
1076 | eio__scandir (eio_req *req, etp_worker *self) |
1103 | eio__scandir (eio_req *req, etp_worker *self) |
… | |
… | |
1133 | /* now move dirs to the front, and non-dirs to the back */ |
1160 | /* now move dirs to the front, and non-dirs to the back */ |
1134 | /* by walking from both sides and swapping if necessary */ |
1161 | /* by walking from both sides and swapping if necessary */ |
1135 | while (ent > dir) |
1162 | while (ent > dir) |
1136 | { |
1163 | { |
1137 | if (dir->type == DT_DIR) |
1164 | if (dir->type == DT_DIR) |
|
|
1165 | { |
|
|
1166 | dir->score = 0; |
1138 | ++dir; |
1167 | ++dir; |
|
|
1168 | } |
1139 | else |
1169 | else |
1140 | { |
1170 | { |
1141 | --ent; |
1171 | --ent; |
1142 | |
1172 | |
1143 | if (ent->type == DT_DIR) |
1173 | if (ent->type == DT_DIR) |
1144 | { |
1174 | { |
1145 | eio_dirent tmp = *dir; |
1175 | eio_dirent tmp = *dir; |
1146 | *dir = *ent; |
1176 | *dir = *ent; |
1147 | *ent = tmp; |
1177 | *ent = tmp; |
1148 | |
1178 | |
|
|
1179 | dir->score = 0; |
1149 | ++dir; |
1180 | ++dir; |
1150 | } |
1181 | } |
1151 | } |
1182 | } |
1152 | } |
1183 | } |
1153 | |
1184 | |
… | |
… | |
1206 | |
1237 | |
1207 | ent = dents + dentoffs; |
1238 | ent = dents + dentoffs; |
1208 | |
1239 | |
1209 | ent->name = (char *)(size_t)namesoffs; /* rather dirtily we store the offset in the pointer */ |
1240 | ent->name = (char *)(size_t)namesoffs; /* rather dirtily we store the offset in the pointer */ |
1210 | ent->namelen = len - 1; |
1241 | ent->namelen = len - 1; |
1211 | ent->inode = D_INO (entp); |
1242 | ent->inode = D_INO (entp) ? D_INO (entp) : dentoffs; |
1212 | |
1243 | |
1213 | switch (D_TYPE (entp)) |
1244 | switch (D_TYPE (entp)) |
1214 | { |
1245 | { |
1215 | default: |
1246 | default: |
1216 | ent->type = EIO_DT_UNKNOWN; |
1247 | ent->type = EIO_DT_UNKNOWN; |
… | |
… | |
1266 | if (flags & EIO_READDIR_DIRS_FIRST) |
1297 | if (flags & EIO_READDIR_DIRS_FIRST) |
1267 | { |
1298 | { |
1268 | if (ent->type == EIO_DT_UNKNOWN) |
1299 | if (ent->type == EIO_DT_UNKNOWN) |
1269 | { |
1300 | { |
1270 | if (*name == '.') /* leading dots are likely directories, and, in any case, rare */ |
1301 | if (*name == '.') /* leading dots are likely directories, and, in any case, rare */ |
1271 | ent->score = 98; |
1302 | ent->score = 7; |
1272 | else if (!strchr (name, '.')) /* absense of dots indicate likely dirs */ |
1303 | else if (!strchr (name, '.')) /* absense of dots indicate likely dirs */ |
1273 | ent->score = len <= 2 ? len + 6 : len <= 4 ? 5 : len <= 7 ? 4 : 1; /* shorter == more likely dir, but avoid too many classes */ |
1304 | ent->score = len <= 2 ? len + 4 : len <= 4 ? 3 : len <= 7 ? 2 : 1; /* shorter == more likely dir, but avoid too many classes */ |
1274 | } |
1305 | } |
1275 | else if (ent->type == EIO_DT_DIR) |
1306 | else if (ent->type == EIO_DT_DIR) |
1276 | ent->score = 100; |
1307 | ent->score = 8; |
1277 | } |
1308 | } |
1278 | } |
1309 | } |
1279 | |
1310 | |
1280 | namesoffs += len; |
1311 | namesoffs += len; |
1281 | ++dentoffs; |
1312 | ++dentoffs; |