1 | /* |
1 | /* |
2 | * libeio implementation |
2 | * libeio implementation |
3 | * |
3 | * |
4 | * Copyright (c) 2007,2008 Marc Alexander Lehmann <libeio@schmorp.de> |
4 | * Copyright (c) 2007,2008,2009 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 | * |
… | |
… | |
68 | #endif |
68 | #endif |
69 | |
69 | |
70 | #ifdef _WIN32 |
70 | #ifdef _WIN32 |
71 | |
71 | |
72 | /*doh*/ |
72 | /*doh*/ |
73 | |
|
|
74 | #else |
73 | #else |
75 | |
74 | |
76 | # include "config.h" |
75 | # include "config.h" |
77 | # include <sys/time.h> |
76 | # include <sys/time.h> |
78 | # include <sys/select.h> |
77 | # include <sys/select.h> |
79 | # include <unistd.h> |
78 | # include <unistd.h> |
80 | # include <utime.h> |
79 | # include <utime.h> |
81 | # include <signal.h> |
80 | # include <signal.h> |
82 | # include <dirent.h> |
81 | # include <dirent.h> |
|
|
82 | |
|
|
83 | /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ |
|
|
84 | # if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) |
|
|
85 | # define _DIRENT_HAVE_D_TYPE /* sigh */ |
|
|
86 | # define D_INO(de) (de)->d_fileno |
|
|
87 | # define D_NAMLEN(de) (de)->d_namlen |
|
|
88 | # elif defined(__linux) || defined(d_ino) || _XOPEN_SOURCE >= 600 |
|
|
89 | # define D_INO(de) (de)->d_ino |
|
|
90 | # endif |
|
|
91 | |
|
|
92 | #ifdef _D_EXACT_NAMLEN |
|
|
93 | # undef D_NAMLEN |
|
|
94 | # define D_NAMLEN(de) _D_EXACT_NAMLEN (de) |
|
|
95 | #endif |
|
|
96 | |
|
|
97 | # ifdef _DIRENT_HAVE_D_TYPE |
|
|
98 | # define D_TYPE(de) (de)->d_type |
|
|
99 | # endif |
83 | |
100 | |
84 | # ifndef EIO_STRUCT_DIRENT |
101 | # ifndef EIO_STRUCT_DIRENT |
85 | # define EIO_STRUCT_DIRENT struct dirent |
102 | # define EIO_STRUCT_DIRENT struct dirent |
86 | # endif |
103 | # endif |
87 | |
104 | |
… | |
… | |
100 | # else |
117 | # else |
101 | # error sendfile support requested but not available |
118 | # error sendfile support requested but not available |
102 | # endif |
119 | # endif |
103 | #endif |
120 | #endif |
104 | |
121 | |
|
|
122 | #ifndef D_TYPE |
|
|
123 | # define D_TYPE(de) 0 |
|
|
124 | #endif |
|
|
125 | #ifndef D_INO |
|
|
126 | # define D_INO(de) 0 |
|
|
127 | #endif |
|
|
128 | #ifndef D_NAMLEN |
|
|
129 | # define D_NAMLEN(de) strlen ((de)->d_name) |
|
|
130 | #endif |
|
|
131 | |
105 | /* number of seconds after which an idle threads exit */ |
132 | /* number of seconds after which an idle threads exit */ |
106 | #define IDLE_TIMEOUT 10 |
133 | #define IDLE_TIMEOUT 10 |
107 | |
134 | |
108 | /* used for struct dirent, AIX doesn't provide it */ |
135 | /* used for struct dirent, AIX doesn't provide it */ |
109 | #ifndef NAME_MAX |
136 | #ifndef NAME_MAX |
… | |
… | |
159 | if (wrk->dirp) \ |
186 | if (wrk->dirp) \ |
160 | { \ |
187 | { \ |
161 | closedir (wrk->dirp); \ |
188 | closedir (wrk->dirp); \ |
162 | wrk->dirp = 0; \ |
189 | wrk->dirp = 0; \ |
163 | } |
190 | } |
|
|
191 | |
164 | #define ETP_WORKER_COMMON \ |
192 | #define ETP_WORKER_COMMON \ |
165 | void *dbuf; \ |
193 | void *dbuf; \ |
166 | DIR *dirp; |
194 | DIR *dirp; |
167 | |
195 | |
168 | /*****************************************************************************/ |
196 | /*****************************************************************************/ |
… | |
… | |
771 | |
799 | |
772 | X_LOCK (preadwritelock); |
800 | X_LOCK (preadwritelock); |
773 | ooffset = lseek (fd, 0, SEEK_CUR); |
801 | ooffset = lseek (fd, 0, SEEK_CUR); |
774 | lseek (fd, offset, SEEK_SET); |
802 | lseek (fd, offset, SEEK_SET); |
775 | res = write (fd, buf, count); |
803 | res = write (fd, buf, count); |
776 | lseek (fd, offset, SEEK_SET); |
804 | lseek (fd, ooffset, SEEK_SET); |
777 | X_UNLOCK (preadwritelock); |
805 | X_UNLOCK (preadwritelock); |
778 | |
806 | |
779 | return res; |
807 | return res; |
780 | } |
808 | } |
781 | #endif |
809 | #endif |
… | |
… | |
961 | } |
989 | } |
962 | |
990 | |
963 | return res; |
991 | return res; |
964 | } |
992 | } |
965 | |
993 | |
|
|
994 | static int |
|
|
995 | eio_dent_cmp (const void *a_, const void *b_) |
|
|
996 | { |
|
|
997 | const eio_dirent *a = (const eio_dirent *)a_; |
|
|
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 */ |
|
|
1002 | } |
|
|
1003 | |
966 | /* read a full directory */ |
1004 | /* read a full directory */ |
967 | static void |
1005 | static void |
968 | eio__scandir (eio_req *req, etp_worker *self) |
1006 | eio__scandir (eio_req *req, etp_worker *self) |
969 | { |
1007 | { |
970 | DIR *dirp; |
1008 | DIR *dirp; |
971 | EIO_STRUCT_DIRENT *entp; |
1009 | EIO_STRUCT_DIRENT *entp; |
972 | char *name, *names; |
1010 | unsigned char *name, *names; |
973 | int memlen = 4096; |
1011 | int namesalloc = 4096; |
974 | int memofs = 0; |
1012 | int namesoffs = 0; |
|
|
1013 | int flags = req->int1; |
|
|
1014 | eio_dirent *dents = 0; |
|
|
1015 | int dentalloc = 128; |
975 | int res = 0; |
1016 | int dentoffs = 0; |
|
|
1017 | |
|
|
1018 | req->result = -1; |
|
|
1019 | |
|
|
1020 | if (!(flags & EIO_READDIR_DENTS)) |
|
|
1021 | flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER); |
976 | |
1022 | |
977 | X_LOCK (wrklock); |
1023 | X_LOCK (wrklock); |
978 | /* the corresponding closedir is in ETP_WORKER_CLEAR */ |
1024 | /* the corresponding closedir is in ETP_WORKER_CLEAR */ |
979 | self->dirp = dirp = opendir (req->ptr1); |
1025 | self->dirp = dirp = opendir (req->ptr1); |
980 | req->flags |= EIO_FLAG_PTR2_FREE; |
1026 | req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE; |
981 | req->ptr2 = names = malloc (memlen); |
1027 | req->ptr1 = names = malloc (namesalloc); |
|
|
1028 | req->ptr2 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; |
982 | X_UNLOCK (wrklock); |
1029 | X_UNLOCK (wrklock); |
983 | |
1030 | |
984 | if (dirp && names) |
1031 | if (dirp && names && (!flags || dents)) |
985 | for (;;) |
1032 | for (;;) |
986 | { |
1033 | { |
987 | errno = 0; |
1034 | errno = 0; |
988 | entp = readdir (dirp); |
1035 | entp = readdir (dirp); |
989 | |
1036 | |
990 | if (!entp) |
1037 | if (!entp) |
|
|
1038 | { |
|
|
1039 | if (errno) |
|
|
1040 | break; |
|
|
1041 | |
|
|
1042 | /* sort etc. */ |
|
|
1043 | req->int1 = flags; |
|
|
1044 | req->result = dentoffs; |
|
|
1045 | |
|
|
1046 | if (dents) |
|
|
1047 | { |
|
|
1048 | eio_dirent *ent = dents + dentoffs; |
|
|
1049 | |
|
|
1050 | while (ent > dents) |
|
|
1051 | (--ent)->name = names + (size_t)ent->name; |
|
|
1052 | } |
|
|
1053 | |
|
|
1054 | if (flags & EIO_READDIR_STAT_ORDER |
|
|
1055 | || !(~flags & (EIO_READDIR_DIRS_FIRST | EIO_READDIR_FOUND_UNKNOWN))) |
|
|
1056 | { |
|
|
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) |
|
|
1061 | { |
|
|
1062 | /* in this case, all is known, and we just put dirs first and sort them */ |
|
|
1063 | eio_dirent *ent = dents + dentoffs; |
|
|
1064 | eio_dirent *dir = dents; |
|
|
1065 | |
|
|
1066 | while (ent > dir) |
|
|
1067 | { |
|
|
1068 | if (dir->type == DT_DIR) |
|
|
1069 | ++dir; |
|
|
1070 | else |
|
|
1071 | { |
|
|
1072 | --ent; |
|
|
1073 | |
|
|
1074 | if (ent->type == DT_DIR) |
|
|
1075 | { |
|
|
1076 | eio_dirent tmp = *dir; |
|
|
1077 | *dir = *ent; |
|
|
1078 | *ent = tmp; |
|
|
1079 | |
|
|
1080 | ++dir; |
|
|
1081 | } |
|
|
1082 | } |
|
|
1083 | } |
|
|
1084 | |
|
|
1085 | /* now sort the dirs only */ |
|
|
1086 | qsort (dents, dir - dents, sizeof (*dents), eio_dent_cmp); |
|
|
1087 | } |
|
|
1088 | |
|
|
1089 | /* only provide the names array unless DENTS is specified */ |
|
|
1090 | if (!(flags & EIO_READDIR_DENTS)) |
|
|
1091 | { |
|
|
1092 | X_LOCK (wrklock); |
|
|
1093 | assert (!dents); |
|
|
1094 | req->ptr1 = 0; |
|
|
1095 | req->ptr2 = names; |
|
|
1096 | X_UNLOCK (wrklock); |
|
|
1097 | } |
|
|
1098 | |
991 | break; |
1099 | break; |
|
|
1100 | } |
992 | |
1101 | |
|
|
1102 | /* now add the entry to our list(s) */ |
993 | name = entp->d_name; |
1103 | name = entp->d_name; |
994 | |
1104 | |
|
|
1105 | /* skip . and .. entries */ |
995 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
1106 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
996 | { |
1107 | { |
997 | int len = strlen (name) + 1; |
1108 | int len = D_NAMLEN (entp) + 1; |
998 | |
1109 | |
999 | res++; |
1110 | while (expect_false (namesoffs + len > namesalloc)) |
1000 | |
|
|
1001 | while (memofs + len > memlen) |
|
|
1002 | { |
1111 | { |
1003 | memlen *= 2; |
1112 | namesalloc *= 2; |
1004 | X_LOCK (wrklock); |
1113 | X_LOCK (wrklock); |
1005 | req->ptr2 = names = realloc (names, memlen); |
1114 | req->ptr1 = names = realloc (names, namesalloc); |
1006 | X_UNLOCK (wrklock); |
1115 | X_UNLOCK (wrklock); |
1007 | |
1116 | |
1008 | if (!names) |
1117 | if (!names) |
1009 | break; |
1118 | break; |
1010 | } |
1119 | } |
1011 | |
1120 | |
1012 | memcpy (names + memofs, name, len); |
1121 | memcpy (names + namesoffs, name, len); |
|
|
1122 | |
|
|
1123 | if (dents) |
|
|
1124 | { |
|
|
1125 | struct eio_dirent *ent; |
|
|
1126 | |
|
|
1127 | if (expect_false (dentoffs == dentalloc)) |
|
|
1128 | { |
|
|
1129 | dentalloc *= 2; |
|
|
1130 | X_LOCK (wrklock); |
|
|
1131 | req->ptr2 = dents = realloc (dents, dentalloc * sizeof (eio_dirent)); |
|
|
1132 | X_UNLOCK (wrklock); |
|
|
1133 | |
|
|
1134 | if (!dents) |
|
|
1135 | break; |
|
|
1136 | } |
|
|
1137 | |
|
|
1138 | ent = dents + dentoffs; |
|
|
1139 | |
|
|
1140 | ent->name = (char *)(size_t)namesoffs; /* rather dirtily we store the offset in the pointer */ |
|
|
1141 | ent->namelen = len - 1; |
|
|
1142 | ent->inode = D_INO (entp); |
|
|
1143 | |
|
|
1144 | switch (D_TYPE (entp)) |
|
|
1145 | { |
|
|
1146 | default: |
|
|
1147 | ent->type = EIO_DT_UNKNOWN; |
|
|
1148 | flags |= EIO_READDIR_FOUND_UNKNOWN; |
|
|
1149 | break; |
|
|
1150 | |
|
|
1151 | #ifdef DT_FIFO |
|
|
1152 | case DT_FIFO: ent->type = EIO_DT_FIFO; break; |
|
|
1153 | #endif |
|
|
1154 | #ifdef DT_CHR |
|
|
1155 | case DT_CHR: ent->type = EIO_DT_CHR; break; |
|
|
1156 | #endif |
|
|
1157 | #ifdef DT_MPC |
|
|
1158 | case DT_MPC: ent->type = EIO_DT_MPC; break; |
|
|
1159 | #endif |
|
|
1160 | #ifdef DT_DIR |
|
|
1161 | case DT_DIR: ent->type = EIO_DT_DIR; break; |
|
|
1162 | #endif |
|
|
1163 | #ifdef DT_NAM |
|
|
1164 | case DT_NAM: ent->type = EIO_DT_NAM; break; |
|
|
1165 | #endif |
|
|
1166 | #ifdef DT_BLK |
|
|
1167 | case DT_BLK: ent->type = EIO_DT_BLK; break; |
|
|
1168 | #endif |
|
|
1169 | #ifdef DT_MPB |
|
|
1170 | case DT_MPB: ent->type = EIO_DT_MPB; break; |
|
|
1171 | #endif |
|
|
1172 | #ifdef DT_REG |
|
|
1173 | case DT_REG: ent->type = EIO_DT_REG; break; |
|
|
1174 | #endif |
|
|
1175 | #ifdef DT_NWK |
|
|
1176 | case DT_NWK: ent->type = EIO_DT_NWK; break; |
|
|
1177 | #endif |
|
|
1178 | #ifdef DT_CMP |
|
|
1179 | case DT_CMP: ent->type = EIO_DT_CMP; break; |
|
|
1180 | #endif |
|
|
1181 | #ifdef DT_LNK |
|
|
1182 | case DT_LNK: ent->type = EIO_DT_LNK; break; |
|
|
1183 | #endif |
|
|
1184 | #ifdef DT_SOCK |
|
|
1185 | case DT_SOCK: ent->type = EIO_DT_SOCK; break; |
|
|
1186 | #endif |
|
|
1187 | #ifdef DT_DOOR |
|
|
1188 | case DT_DOOR: ent->type = EIO_DT_DOOR; break; |
|
|
1189 | #endif |
|
|
1190 | #ifdef DT_WHT |
|
|
1191 | case DT_WHT: ent->type = EIO_DT_WHT; break; |
|
|
1192 | #endif |
|
|
1193 | } |
|
|
1194 | |
|
|
1195 | ent->score = 0; |
|
|
1196 | |
|
|
1197 | if (flags & EIO_READDIR_DIRS_FIRST) |
|
|
1198 | { |
|
|
1199 | if (ent->type == EIO_DT_UNKNOWN) |
|
|
1200 | { |
|
|
1201 | if (*name == '.') /* leading dots are likely directories, and, in any case, rare */ |
|
|
1202 | ent->score = 98; |
|
|
1203 | 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 */ |
|
|
1205 | } |
|
|
1206 | else if (ent->type == EIO_DT_DIR) |
|
|
1207 | ent->score = 100; |
|
|
1208 | } |
|
|
1209 | } |
|
|
1210 | |
1013 | memofs += len; |
1211 | namesoffs += len; |
|
|
1212 | ++dentoffs; |
1014 | } |
1213 | } |
1015 | } |
1214 | } |
1016 | |
1215 | else |
1017 | if (errno) |
|
|
1018 | res = -1; |
|
|
1019 | |
|
|
1020 | req->result = res; |
1216 | req->result = -1; |
1021 | } |
1217 | } |
1022 | |
1218 | |
1023 | #if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) |
1219 | #if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) |
1024 | # undef msync |
1220 | # undef msync |
1025 | # define msync(a,b,c) ((errno = ENOSYS), -1) |
1221 | # define msync(a,b,c) ((errno = ENOSYS), -1) |
… | |
… | |
1445 | eio_req *eio_rmdir (const char *path, int pri, eio_cb cb, void *data) |
1641 | eio_req *eio_rmdir (const char *path, int pri, eio_cb cb, void *data) |
1446 | { |
1642 | { |
1447 | return eio__1path (EIO_RMDIR, path, pri, cb, data); |
1643 | return eio__1path (EIO_RMDIR, path, pri, cb, data); |
1448 | } |
1644 | } |
1449 | |
1645 | |
1450 | eio_req *eio_readdir (const char *path, int pri, eio_cb cb, void *data) |
1646 | eio_req *eio_readdir (const char *path, int flags, int pri, eio_cb cb, void *data) |
1451 | { |
1647 | { |
1452 | return eio__1path (EIO_READDIR, path, pri, cb, data); |
1648 | REQ (EIO_READDIR); PATH; req->int1 = flags; SEND; |
1453 | } |
1649 | } |
1454 | |
1650 | |
1455 | eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data) |
1651 | eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data) |
1456 | { |
1652 | { |
1457 | REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->int3 = (long)dev; SEND; |
1653 | REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->int3 = (long)dev; SEND; |