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

Comparing libeio/eio.c (file contents):
Revision 1.27 by root, Wed Oct 22 18:15:36 2008 UTC vs.
Revision 1.36 by root, Fri Jun 12 00:43:16 2009 UTC

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 *
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#include "eio.h" 40#include "eio.h"
41
42#ifdef EIO_STACKSIZE
43# define XTHREAD_STACKSIZE EIO_STACKSIZE
44#endif
41#include "xthread.h" 45#include "xthread.h"
42 46
43#include <errno.h> 47#include <errno.h>
44#include <stddef.h> 48#include <stddef.h>
45#include <stdlib.h> 49#include <stdlib.h>
64#endif 68#endif
65 69
66#ifdef _WIN32 70#ifdef _WIN32
67 71
68 /*doh*/ 72 /*doh*/
69
70#else 73#else
71 74
72# include "config.h" 75# include "config.h"
73# include <sys/time.h> 76# include <sys/time.h>
74# include <sys/select.h> 77# include <sys/select.h>
75# include <unistd.h> 78# include <unistd.h>
76# include <utime.h> 79# include <utime.h>
77# include <signal.h> 80# include <signal.h>
78# 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
79 100
80# ifndef EIO_STRUCT_DIRENT 101# ifndef EIO_STRUCT_DIRENT
81# define EIO_STRUCT_DIRENT struct dirent 102# define EIO_STRUCT_DIRENT struct dirent
82# endif 103# endif
83 104
96# else 117# else
97# error sendfile support requested but not available 118# error sendfile support requested but not available
98# endif 119# endif
99#endif 120#endif
100 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
101/* number of seconds after which an idle threads exit */ 132/* number of seconds after which an idle threads exit */
102#define IDLE_TIMEOUT 10 133#define IDLE_TIMEOUT 10
103 134
104/* used for struct dirent, AIX doesn't provide it */ 135/* used for struct dirent, AIX doesn't provide it */
105#ifndef NAME_MAX 136#ifndef NAME_MAX
155 if (wrk->dirp) \ 186 if (wrk->dirp) \
156 { \ 187 { \
157 closedir (wrk->dirp); \ 188 closedir (wrk->dirp); \
158 wrk->dirp = 0; \ 189 wrk->dirp = 0; \
159 } 190 }
191
160#define ETP_WORKER_COMMON \ 192#define ETP_WORKER_COMMON \
161 void *dbuf; \ 193 void *dbuf; \
162 DIR *dirp; 194 DIR *dirp;
163 195
164/*****************************************************************************/ 196/*****************************************************************************/
767 799
768 X_LOCK (preadwritelock); 800 X_LOCK (preadwritelock);
769 ooffset = lseek (fd, 0, SEEK_CUR); 801 ooffset = lseek (fd, 0, SEEK_CUR);
770 lseek (fd, offset, SEEK_SET); 802 lseek (fd, offset, SEEK_SET);
771 res = write (fd, buf, count); 803 res = write (fd, buf, count);
772 lseek (fd, offset, SEEK_SET); 804 lseek (fd, ooffset, SEEK_SET);
773 X_UNLOCK (preadwritelock); 805 X_UNLOCK (preadwritelock);
774 806
775 return res; 807 return res;
776} 808}
777#endif 809#endif
829 | (flags & EIO_SYNC_FILE_RANGE_WAIT_AFTER ? SYNC_FILE_RANGE_WAIT_AFTER : 0); 861 | (flags & EIO_SYNC_FILE_RANGE_WAIT_AFTER ? SYNC_FILE_RANGE_WAIT_AFTER : 0);
830 } 862 }
831 863
832 res = sync_file_range (fd, offset, nbytes, flags); 864 res = sync_file_range (fd, offset, nbytes, flags);
833 865
834 if (res != ENOSYS) 866 if (!res || errno != ENOSYS)
835 return res; 867 return res;
836#endif 868#endif
837 869
838 /* even though we could play tricks with the flags, it's better to always 870 /* even though we could play tricks with the flags, it's better to always
839 * call fdatasync, as thta matches the expectation of it's users best */ 871 * call fdatasync, as thta matches the expectation of it's users best */
957 } 989 }
958 990
959 return res; 991 return res;
960} 992}
961 993
994static int
995eio_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
962/* read a full directory */ 1004/* read a full directory */
963static void 1005static void
964eio__scandir (eio_req *req, etp_worker *self) 1006eio__scandir (eio_req *req, etp_worker *self)
965{ 1007{
966 DIR *dirp; 1008 DIR *dirp;
967 EIO_STRUCT_DIRENT *entp; 1009 EIO_STRUCT_DIRENT *entp;
968 char *name, *names; 1010 unsigned char *name, *names;
969 int memlen = 4096; 1011 int namesalloc = 4096;
970 int memofs = 0; 1012 int namesoffs = 0;
1013 int flags = req->int1;
1014 eio_dirent *dents = 0;
1015 int dentalloc = 128;
971 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);
972 1022
973 X_LOCK (wrklock); 1023 X_LOCK (wrklock);
974 /* the corresponding closedir is in ETP_WORKER_CLEAR */ 1024 /* the corresponding closedir is in ETP_WORKER_CLEAR */
975 self->dirp = dirp = opendir (req->ptr1); 1025 self->dirp = dirp = opendir (req->ptr1);
976 req->flags |= EIO_FLAG_PTR2_FREE; 1026 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE;
977 req->ptr2 = names = malloc (memlen); 1027 req->ptr1 = names = malloc (namesalloc);
1028 req->ptr2 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0;
978 X_UNLOCK (wrklock); 1029 X_UNLOCK (wrklock);
979 1030
980 if (dirp && names) 1031 if (dirp && names && (!flags || dents))
981 for (;;) 1032 for (;;)
982 { 1033 {
983 errno = 0; 1034 errno = 0;
984 entp = readdir (dirp); 1035 entp = readdir (dirp);
985 1036
986 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 /* now move dirs to the front, and non-dirs to the back */
1067 /* by walkign from both sides and swapping if necessary */
1068 while (ent > dir)
1069 {
1070 if (dir->type == DT_DIR)
1071 ++dir;
1072 else
1073 {
1074 --ent;
1075
1076 if (ent->type == DT_DIR)
1077 {
1078 eio_dirent tmp = *dir;
1079 *dir = *ent;
1080 *ent = tmp;
1081
1082 ++dir;
1083 }
1084 }
1085 }
1086
1087 /* now sort the dirs only */
1088 qsort (dents, dir - dents, sizeof (*dents), eio_dent_cmp);
1089 }
1090
1091 /* only provide the names array unless DENTS is specified */
1092 if (!(flags & EIO_READDIR_DENTS))
1093 {
1094 X_LOCK (wrklock);
1095 assert (!dents);
1096 req->ptr1 = 0;
1097 req->ptr2 = names;
1098 X_UNLOCK (wrklock);
1099 }
1100
987 break; 1101 break;
1102 }
988 1103
1104 /* now add the entry to our list(s) */
989 name = entp->d_name; 1105 name = entp->d_name;
990 1106
1107 /* skip . and .. entries */
991 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 1108 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
992 { 1109 {
993 int len = strlen (name) + 1; 1110 int len = D_NAMLEN (entp) + 1;
994 1111
995 res++; 1112 while (expect_false (namesoffs + len > namesalloc))
996
997 while (memofs + len > memlen)
998 { 1113 {
999 memlen *= 2; 1114 namesalloc *= 2;
1000 X_LOCK (wrklock); 1115 X_LOCK (wrklock);
1001 req->ptr2 = names = realloc (names, memlen); 1116 req->ptr1 = names = realloc (names, namesalloc);
1002 X_UNLOCK (wrklock); 1117 X_UNLOCK (wrklock);
1003 1118
1004 if (!names) 1119 if (!names)
1005 break; 1120 break;
1006 } 1121 }
1007 1122
1008 memcpy (names + memofs, name, len); 1123 memcpy (names + namesoffs, name, len);
1124
1125 if (dents)
1126 {
1127 struct eio_dirent *ent;
1128
1129 if (expect_false (dentoffs == dentalloc))
1130 {
1131 dentalloc *= 2;
1132 X_LOCK (wrklock);
1133 req->ptr2 = dents = realloc (dents, dentalloc * sizeof (eio_dirent));
1134 X_UNLOCK (wrklock);
1135
1136 if (!dents)
1137 break;
1138 }
1139
1140 ent = dents + dentoffs;
1141
1142 ent->name = (char *)(size_t)namesoffs; /* rather dirtily we store the offset in the pointer */
1143 ent->namelen = len - 1;
1144 ent->inode = D_INO (entp);
1145
1146 switch (D_TYPE (entp))
1147 {
1148 default:
1149 ent->type = EIO_DT_UNKNOWN;
1150 flags |= EIO_READDIR_FOUND_UNKNOWN;
1151 break;
1152
1153 #ifdef DT_FIFO
1154 case DT_FIFO: ent->type = EIO_DT_FIFO; break;
1155 #endif
1156 #ifdef DT_CHR
1157 case DT_CHR: ent->type = EIO_DT_CHR; break;
1158 #endif
1159 #ifdef DT_MPC
1160 case DT_MPC: ent->type = EIO_DT_MPC; break;
1161 #endif
1162 #ifdef DT_DIR
1163 case DT_DIR: ent->type = EIO_DT_DIR; break;
1164 #endif
1165 #ifdef DT_NAM
1166 case DT_NAM: ent->type = EIO_DT_NAM; break;
1167 #endif
1168 #ifdef DT_BLK
1169 case DT_BLK: ent->type = EIO_DT_BLK; break;
1170 #endif
1171 #ifdef DT_MPB
1172 case DT_MPB: ent->type = EIO_DT_MPB; break;
1173 #endif
1174 #ifdef DT_REG
1175 case DT_REG: ent->type = EIO_DT_REG; break;
1176 #endif
1177 #ifdef DT_NWK
1178 case DT_NWK: ent->type = EIO_DT_NWK; break;
1179 #endif
1180 #ifdef DT_CMP
1181 case DT_CMP: ent->type = EIO_DT_CMP; break;
1182 #endif
1183 #ifdef DT_LNK
1184 case DT_LNK: ent->type = EIO_DT_LNK; break;
1185 #endif
1186 #ifdef DT_SOCK
1187 case DT_SOCK: ent->type = EIO_DT_SOCK; break;
1188 #endif
1189 #ifdef DT_DOOR
1190 case DT_DOOR: ent->type = EIO_DT_DOOR; break;
1191 #endif
1192 #ifdef DT_WHT
1193 case DT_WHT: ent->type = EIO_DT_WHT; break;
1194 #endif
1195 }
1196
1197 ent->score = 0;
1198
1199 if (flags & EIO_READDIR_DIRS_FIRST)
1200 {
1201 if (ent->type == EIO_DT_UNKNOWN)
1202 {
1203 if (*name == '.') /* leading dots are likely directories, and, in any case, rare */
1204 ent->score = 98;
1205 else if (!strchr (name, '.')) /* absense of dots indicate likely dirs */
1206 ent->score = len <= 2 ? len + 6 : len <= 4 ? 5 : len <= 7 ? 4 : 1; /* shorter == more likely dir, but avoid too many classes */
1207 }
1208 else if (ent->type == EIO_DT_DIR)
1209 ent->score = 100;
1210 }
1211 }
1212
1009 memofs += len; 1213 namesoffs += len;
1214 ++dentoffs;
1215 }
1216
1217 if (EIO_CANCELLED (req))
1218 {
1219 errno = ECANCELED;
1220 break;
1010 } 1221 }
1011 } 1222 }
1012
1013 if (errno)
1014 res = -1;
1015
1016 req->result = res;
1017} 1223}
1018 1224
1019#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) 1225#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO)
1020# undef msync 1226# undef msync
1021# define msync(a,b,c) ENOSYS 1227# define msync(a,b,c) ((errno = ENOSYS), -1)
1022#endif 1228#endif
1023 1229
1024int 1230int
1025eio__mtouch (void *mem, size_t len, int flags) 1231eio__mtouch (void *mem, size_t len, int flags)
1026{ 1232{
1441eio_req *eio_rmdir (const char *path, int pri, eio_cb cb, void *data) 1647eio_req *eio_rmdir (const char *path, int pri, eio_cb cb, void *data)
1442{ 1648{
1443 return eio__1path (EIO_RMDIR, path, pri, cb, data); 1649 return eio__1path (EIO_RMDIR, path, pri, cb, data);
1444} 1650}
1445 1651
1446eio_req *eio_readdir (const char *path, int pri, eio_cb cb, void *data) 1652eio_req *eio_readdir (const char *path, int flags, int pri, eio_cb cb, void *data)
1447{ 1653{
1448 return eio__1path (EIO_READDIR, path, pri, cb, data); 1654 REQ (EIO_READDIR); PATH; req->int1 = flags; SEND;
1449} 1655}
1450 1656
1451eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data) 1657eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data)
1452{ 1658{
1453 REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->int3 = (long)dev; SEND; 1659 REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->int3 = (long)dev; SEND;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines