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

Comparing libeio/eio.c (file contents):
Revision 1.30 by root, Wed Jun 3 12:24:49 2009 UTC vs.
Revision 1.32 by root, Sat Jun 6 18:06:55 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 *
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 D_INO(de) (de)->d_fileno
86# define _DIRENT_HAVE_D_TYPE /* sigh */
87# elif defined(__linux) || defined(d_ino) || _XOPEN_SOURCE >= 600
88# define D_INO(de) (de)->d_ino
89# endif
90
91# ifdef _DIRENT_HAVE_D_TYPE
92# define D_TYPE(de) (de)->d_type
93# endif
83 94
84# ifndef EIO_STRUCT_DIRENT 95# ifndef EIO_STRUCT_DIRENT
85# define EIO_STRUCT_DIRENT struct dirent 96# define EIO_STRUCT_DIRENT struct dirent
86# endif 97# endif
87 98
100# else 111# else
101# error sendfile support requested but not available 112# error sendfile support requested but not available
102# endif 113# endif
103#endif 114#endif
104 115
116#ifndef D_TYPE
117# define D_TYPE(de) 0
118#endif
119
120#ifndef D_INO
121# define D_INO(de) 0
122#endif
123
105/* number of seconds after which an idle threads exit */ 124/* number of seconds after which an idle threads exit */
106#define IDLE_TIMEOUT 10 125#define IDLE_TIMEOUT 10
107 126
108/* used for struct dirent, AIX doesn't provide it */ 127/* used for struct dirent, AIX doesn't provide it */
109#ifndef NAME_MAX 128#ifndef NAME_MAX
159 if (wrk->dirp) \ 178 if (wrk->dirp) \
160 { \ 179 { \
161 closedir (wrk->dirp); \ 180 closedir (wrk->dirp); \
162 wrk->dirp = 0; \ 181 wrk->dirp = 0; \
163 } 182 }
183
164#define ETP_WORKER_COMMON \ 184#define ETP_WORKER_COMMON \
165 void *dbuf; \ 185 void *dbuf; \
166 DIR *dirp; 186 DIR *dirp;
167 187
168/*****************************************************************************/ 188/*****************************************************************************/
961 } 981 }
962 982
963 return res; 983 return res;
964} 984}
965 985
986static int
987eio_dent_cmp (const void *a_, const void *b_)
988{
989 const eio_dirent *a = (const eio_dirent *)a_;
990 const eio_dirent *b = (const eio_dirent *)b_;
991
992 return (int)b->score - (int)a->score ? (int)b->score - (int)a->score
993 : a->inode < b->inode ? -1 : a->inode > b->inode ? 1 : 0; /* int might be < ino_t */
994}
995
966/* read a full directory */ 996/* read a full directory */
967static void 997static void
968eio__scandir (eio_req *req, etp_worker *self) 998eio__scandir (eio_req *req, etp_worker *self)
969{ 999{
970 DIR *dirp; 1000 DIR *dirp;
971 EIO_STRUCT_DIRENT *entp; 1001 EIO_STRUCT_DIRENT *entp;
972 char *name, *names; 1002 unsigned char *name, *names;
973 int memlen = 4096; 1003 int namesalloc = 4096;
974 int memofs = 0; 1004 int namesoffs = 0;
1005 int flags = req->int1;
1006 eio_dirent *dents = 0;
1007 int dentalloc = 128;
975 int res = 0; 1008 int dentoffs = 0;
1009
1010 req->result = -1;
1011
1012 if (!(flags & EIO_READDIR_DENTS))
1013 flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER);
976 1014
977 X_LOCK (wrklock); 1015 X_LOCK (wrklock);
978 /* the corresponding closedir is in ETP_WORKER_CLEAR */ 1016 /* the corresponding closedir is in ETP_WORKER_CLEAR */
979 self->dirp = dirp = opendir (req->ptr1); 1017 self->dirp = dirp = opendir (req->ptr1);
980 req->flags |= EIO_FLAG_PTR2_FREE; 1018 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE;
981 req->ptr2 = names = malloc (memlen); 1019 req->ptr1 = names = malloc (namesalloc);
1020 req->ptr2 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0;
982 X_UNLOCK (wrklock); 1021 X_UNLOCK (wrklock);
983 1022
984 if (dirp && names) 1023 if (dirp && names && (!flags || dents))
985 for (;;) 1024 for (;;)
986 { 1025 {
987 errno = 0; 1026 errno = 0;
988 entp = readdir (dirp); 1027 entp = readdir (dirp);
989 1028
990 if (!entp) 1029 if (!entp)
1030 {
1031 if (errno)
1032 break;
1033
1034 /* sort etc. */
1035 req->int1 = flags;
1036 req->result = dentoffs;
1037
1038 if (dents)
1039 {
1040 eio_dirent *ent = dents + dentoffs;
1041
1042 while (ent > dents)
1043 (--ent)->name = names + (size_t)ent->name;
1044 }
1045
1046 if (flags & EIO_READDIR_STAT_ORDER
1047 || !(~flags & (EIO_READDIR_DIRS_FIRST | EIO_READDIR_FOUND_UNKNOWN)))
1048 {
1049 /* pray your qsort doesn't use quicksort */
1050 qsort (dents, dentoffs, sizeof (*dents), eio_dent_cmp); /* score depends of DIRS_FIRST */
1051 }
1052 else if (flags & EIO_READDIR_DIRS_FIRST)
1053 {
1054 /* in this case, all is known, and we just put dirs first and sort them */
1055 eio_dirent *ent = dents + dentoffs;
1056 eio_dirent *dir = dents;
1057
1058 while (ent > dir)
1059 {
1060 if (dir->type == DT_DIR)
1061 ++dir;
1062 else
1063 {
1064 --ent;
1065
1066 if (ent->type == DT_DIR)
1067 {
1068 eio_dirent tmp = *dir;
1069 *dir = *ent;
1070 *ent = tmp;
1071
1072 ++dir;
1073 }
1074 }
1075 }
1076
1077 /* now sort the dirs only */
1078 qsort (dents, dir - dents, sizeof (*dents), eio_dent_cmp);
1079 }
1080
1081 /* only provide the names array unless DENTS is specified */
1082 if (!(flags & EIO_READDIR_DENTS))
1083 {
1084 X_LOCK (wrklock);
1085 assert (!dents);
1086 req->ptr1 = 0;
1087 req->ptr2 = names;
1088 X_UNLOCK (wrklock);
1089 }
1090
991 break; 1091 break;
1092 }
992 1093
1094 /* now add the entry to our list(s) */
993 name = entp->d_name; 1095 name = entp->d_name;
994 1096
1097 /* skip . and .. entries */
995 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 1098 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
996 { 1099 {
997 int len = strlen (name) + 1; 1100 int len = strlen (name) + 1;
998 1101
999 res++; 1102 while (expect_false (namesoffs + len > namesalloc))
1000
1001 while (memofs + len > memlen)
1002 { 1103 {
1003 memlen *= 2; 1104 namesalloc *= 2;
1004 X_LOCK (wrklock); 1105 X_LOCK (wrklock);
1005 req->ptr2 = names = realloc (names, memlen); 1106 req->ptr1 = names = realloc (names, namesalloc);
1006 X_UNLOCK (wrklock); 1107 X_UNLOCK (wrklock);
1007 1108
1008 if (!names) 1109 if (!names)
1009 break; 1110 break;
1010 } 1111 }
1011 1112
1012 memcpy (names + memofs, name, len); 1113 memcpy (names + namesoffs, name, len);
1114
1115 if (dents)
1116 {
1117 struct eio_dirent *ent;
1118
1119 if (expect_false (dentoffs == dentalloc))
1120 {
1121 dentalloc *= 2;
1122 X_LOCK (wrklock);
1123 req->ptr2 = dents = realloc (dents, dentalloc * sizeof (eio_dirent));
1124 X_UNLOCK (wrklock);
1125
1126 if (!dents)
1127 break;
1128 }
1129
1130 ent = dents + dentoffs;
1131
1132 ent->name = (char *)(size_t)namesoffs; /* rather dirtily we store the offset in the pointer */
1133 ent->namelen = len - 1;
1134 ent->inode = D_INO (entp);
1135
1136 switch (D_TYPE (entp))
1137 {
1138 default:
1139 ent->type = EIO_DT_UNKNOWN;
1140 flags |= EIO_READDIR_FOUND_UNKNOWN;
1141 break;
1142
1143 #ifdef DT_FIFO
1144 case DT_FIFO: ent->type = EIO_DT_FIFO; break;
1145 #endif
1146 #ifdef DT_CHR
1147 case DT_CHR: ent->type = EIO_DT_CHR; break;
1148 #endif
1149 #ifdef DT_DIR
1150 case DT_DIR: ent->type = EIO_DT_DIR; break;
1151 #endif
1152 #ifdef DT_BLK
1153 case DT_BLK: ent->type = EIO_DT_BLK; break;
1154 #endif
1155 #ifdef DT_REG
1156 case DT_REG: ent->type = EIO_DT_REG; break;
1157 #endif
1158 #ifdef DT_LNK
1159 case DT_LNK: ent->type = EIO_DT_LNK; break;
1160 #endif
1161 #ifdef DT_SOCK
1162 case DT_SOCK: ent->type = EIO_DT_SOCK; break;
1163 #endif
1164 #ifdef DT_WHT
1165 case DT_WHT: ent->type = EIO_DT_WHT; break;
1166 #endif
1167 }
1168
1169 ent->score = 0;
1170
1171 if (flags & EIO_READDIR_DIRS_FIRST)
1172 {
1173 if (ent->type == EIO_DT_UNKNOWN)
1174 {
1175 if (*name == '.') /* leading dots are likely directories, and, in any case, rare */
1176 ent->score = 98;
1177 else if (!strchr (name, '.')) /* absense of dots indicate likely dirs */
1178 ent->score = len <= 4 ? 5 : len <= 7 ? 4 : 1; /* shorter == more likely dir, but avoid too many classes */
1179 }
1180 else if (ent->type == DT_DIR)
1181 ent->score = 100;
1182 }
1183 }
1184
1013 memofs += len; 1185 namesoffs += len;
1186 ++dentoffs;
1014 } 1187 }
1015 } 1188 }
1016 1189 else
1017 if (errno)
1018 res = -1;
1019
1020 req->result = res; 1190 req->result = -1;
1021} 1191}
1022 1192
1023#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) 1193#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO)
1024# undef msync 1194# undef msync
1025# define msync(a,b,c) ((errno = ENOSYS), -1) 1195# define msync(a,b,c) ((errno = ENOSYS), -1)
1445eio_req *eio_rmdir (const char *path, int pri, eio_cb cb, void *data) 1615eio_req *eio_rmdir (const char *path, int pri, eio_cb cb, void *data)
1446{ 1616{
1447 return eio__1path (EIO_RMDIR, path, pri, cb, data); 1617 return eio__1path (EIO_RMDIR, path, pri, cb, data);
1448} 1618}
1449 1619
1450eio_req *eio_readdir (const char *path, int pri, eio_cb cb, void *data) 1620eio_req *eio_readdir (const char *path, int flags, int pri, eio_cb cb, void *data)
1451{ 1621{
1452 return eio__1path (EIO_READDIR, path, pri, cb, data); 1622 REQ (EIO_READDIR); PATH; req->int1 = flags; SEND;
1453} 1623}
1454 1624
1455eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data) 1625eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data)
1456{ 1626{
1457 REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->int3 = (long)dev; SEND; 1627 REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->int3 = (long)dev; SEND;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines