1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker * readdir accelerator
3*6a54128fSAndroid Build Coastguard Worker *
4*6a54128fSAndroid Build Coastguard Worker * (C) Copyright 2003, 2004, 2008 by Theodore Ts'o.
5*6a54128fSAndroid Build Coastguard Worker *
6*6a54128fSAndroid Build Coastguard Worker * 2008-06-08 Modified by Ross Boylan <RossBoylan stanfordalumni org>
7*6a54128fSAndroid Build Coastguard Worker * Added support for readdir_r and readdir64_r calls. Note
8*6a54128fSAndroid Build Coastguard Worker * this has not been tested on anything other than GNU/Linux i386,
9*6a54128fSAndroid Build Coastguard Worker * and that the regular readdir wrapper will take slightly more
10*6a54128fSAndroid Build Coastguard Worker * space than Ted's original since it now includes a lock.
11*6a54128fSAndroid Build Coastguard Worker *
12*6a54128fSAndroid Build Coastguard Worker * Compile using the command:
13*6a54128fSAndroid Build Coastguard Worker *
14*6a54128fSAndroid Build Coastguard Worker * gcc -o spd_readdir.so -shared -fpic spd_readdir.c -ldl
15*6a54128fSAndroid Build Coastguard Worker *
16*6a54128fSAndroid Build Coastguard Worker * Use it by setting the LD_PRELOAD environment variable:
17*6a54128fSAndroid Build Coastguard Worker *
18*6a54128fSAndroid Build Coastguard Worker * export LD_PRELOAD=/usr/local/sbin/spd_readdir.so
19*6a54128fSAndroid Build Coastguard Worker *
20*6a54128fSAndroid Build Coastguard Worker * %Begin-Header%
21*6a54128fSAndroid Build Coastguard Worker * This file may be redistributed under the terms of the GNU Public
22*6a54128fSAndroid Build Coastguard Worker * License.
23*6a54128fSAndroid Build Coastguard Worker * %End-Header%
24*6a54128fSAndroid Build Coastguard Worker *
25*6a54128fSAndroid Build Coastguard Worker */
26*6a54128fSAndroid Build Coastguard Worker
27*6a54128fSAndroid Build Coastguard Worker #define ALLOC_STEPSIZE 100
28*6a54128fSAndroid Build Coastguard Worker #define MAX_DIRSIZE 0
29*6a54128fSAndroid Build Coastguard Worker
30*6a54128fSAndroid Build Coastguard Worker #define DEBUG
31*6a54128fSAndroid Build Coastguard Worker /* Util we autoconfiscate spd_readdir... */
32*6a54128fSAndroid Build Coastguard Worker #define HAVE___SECURE_GETENV 1
33*6a54128fSAndroid Build Coastguard Worker #define HAVE_PRCTL 1
34*6a54128fSAndroid Build Coastguard Worker #define HAVE_SYS_PRCTL_H 1
35*6a54128fSAndroid Build Coastguard Worker
36*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
37*6a54128fSAndroid Build Coastguard Worker #define DEBUG_DIR(x) {if (do_debug) { x; }}
38*6a54128fSAndroid Build Coastguard Worker #else
39*6a54128fSAndroid Build Coastguard Worker #define DEBUG_DIR(x)
40*6a54128fSAndroid Build Coastguard Worker #endif
41*6a54128fSAndroid Build Coastguard Worker
42*6a54128fSAndroid Build Coastguard Worker #define _GNU_SOURCE
43*6a54128fSAndroid Build Coastguard Worker #define __USE_LARGEFILE64
44*6a54128fSAndroid Build Coastguard Worker
45*6a54128fSAndroid Build Coastguard Worker #include <stdio.h>
46*6a54128fSAndroid Build Coastguard Worker #include <unistd.h>
47*6a54128fSAndroid Build Coastguard Worker #include <sys/types.h>
48*6a54128fSAndroid Build Coastguard Worker #include <sys/stat.h>
49*6a54128fSAndroid Build Coastguard Worker #include <stdlib.h>
50*6a54128fSAndroid Build Coastguard Worker #include <string.h>
51*6a54128fSAndroid Build Coastguard Worker #include <dirent.h>
52*6a54128fSAndroid Build Coastguard Worker #include <errno.h>
53*6a54128fSAndroid Build Coastguard Worker #include <dlfcn.h>
54*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_SYS_PRCTL_H
55*6a54128fSAndroid Build Coastguard Worker #include <sys/prctl.h>
56*6a54128fSAndroid Build Coastguard Worker #else
57*6a54128fSAndroid Build Coastguard Worker #define PR_GET_DUMPABLE 3
58*6a54128fSAndroid Build Coastguard Worker #endif
59*6a54128fSAndroid Build Coastguard Worker #include <pthread.h>
60*6a54128fSAndroid Build Coastguard Worker
61*6a54128fSAndroid Build Coastguard Worker struct dirent_s {
62*6a54128fSAndroid Build Coastguard Worker unsigned long long d_ino;
63*6a54128fSAndroid Build Coastguard Worker long long d_off;
64*6a54128fSAndroid Build Coastguard Worker unsigned short int d_reclen;
65*6a54128fSAndroid Build Coastguard Worker unsigned char d_type;
66*6a54128fSAndroid Build Coastguard Worker char *d_name;
67*6a54128fSAndroid Build Coastguard Worker };
68*6a54128fSAndroid Build Coastguard Worker
69*6a54128fSAndroid Build Coastguard Worker struct dir_s {
70*6a54128fSAndroid Build Coastguard Worker DIR *dir;
71*6a54128fSAndroid Build Coastguard Worker pthread_mutex_t lock; /* Mutex lock for this structure. */
72*6a54128fSAndroid Build Coastguard Worker int num;
73*6a54128fSAndroid Build Coastguard Worker int max;
74*6a54128fSAndroid Build Coastguard Worker struct dirent_s *dp;
75*6a54128fSAndroid Build Coastguard Worker int pos;
76*6a54128fSAndroid Build Coastguard Worker int direct;
77*6a54128fSAndroid Build Coastguard Worker struct dirent ret_dir;
78*6a54128fSAndroid Build Coastguard Worker struct dirent64 ret_dir64;
79*6a54128fSAndroid Build Coastguard Worker };
80*6a54128fSAndroid Build Coastguard Worker
81*6a54128fSAndroid Build Coastguard Worker static int (*real_closedir)(DIR *dir) = 0;
82*6a54128fSAndroid Build Coastguard Worker static DIR *(*real_opendir)(const char *name) = 0;
83*6a54128fSAndroid Build Coastguard Worker static DIR *(*real_fdopendir)(int fd) = 0;
84*6a54128fSAndroid Build Coastguard Worker static void *(*real_rewinddir)(DIR *dirp) = 0;
85*6a54128fSAndroid Build Coastguard Worker static struct dirent *(*real_readdir)(DIR *dir) = 0;
86*6a54128fSAndroid Build Coastguard Worker static int (*real_readdir_r)(DIR *dir, struct dirent *entry,
87*6a54128fSAndroid Build Coastguard Worker struct dirent **result) = 0;
88*6a54128fSAndroid Build Coastguard Worker static struct dirent64 *(*real_readdir64)(DIR *dir) = 0;
89*6a54128fSAndroid Build Coastguard Worker static int (*real_readdir64_r)(DIR *dir, struct dirent64 *entry,
90*6a54128fSAndroid Build Coastguard Worker struct dirent64 **result) = 0;
91*6a54128fSAndroid Build Coastguard Worker static off_t (*real_telldir)(DIR *dir) = 0;
92*6a54128fSAndroid Build Coastguard Worker static void (*real_seekdir)(DIR *dir, off_t offset) = 0;
93*6a54128fSAndroid Build Coastguard Worker static int (*real_dirfd)(DIR *dir) = 0;
94*6a54128fSAndroid Build Coastguard Worker static unsigned long max_dirsize = MAX_DIRSIZE;
95*6a54128fSAndroid Build Coastguard Worker static int num_open = 0;
96*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
97*6a54128fSAndroid Build Coastguard Worker static int do_debug = 0;
98*6a54128fSAndroid Build Coastguard Worker #endif
99*6a54128fSAndroid Build Coastguard Worker
safe_getenv(const char * arg)100*6a54128fSAndroid Build Coastguard Worker static char *safe_getenv(const char *arg)
101*6a54128fSAndroid Build Coastguard Worker {
102*6a54128fSAndroid Build Coastguard Worker if ((getuid() != geteuid()) || (getgid() != getegid()))
103*6a54128fSAndroid Build Coastguard Worker return NULL;
104*6a54128fSAndroid Build Coastguard Worker #if HAVE_PRCTL
105*6a54128fSAndroid Build Coastguard Worker if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
106*6a54128fSAndroid Build Coastguard Worker return NULL;
107*6a54128fSAndroid Build Coastguard Worker #else
108*6a54128fSAndroid Build Coastguard Worker #if (defined(linux) && defined(SYS_prctl))
109*6a54128fSAndroid Build Coastguard Worker if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
110*6a54128fSAndroid Build Coastguard Worker return NULL;
111*6a54128fSAndroid Build Coastguard Worker #endif
112*6a54128fSAndroid Build Coastguard Worker #endif
113*6a54128fSAndroid Build Coastguard Worker
114*6a54128fSAndroid Build Coastguard Worker #if HAVE___SECURE_GETENV
115*6a54128fSAndroid Build Coastguard Worker return __secure_getenv(arg);
116*6a54128fSAndroid Build Coastguard Worker #else
117*6a54128fSAndroid Build Coastguard Worker return getenv(arg);
118*6a54128fSAndroid Build Coastguard Worker #endif
119*6a54128fSAndroid Build Coastguard Worker }
120*6a54128fSAndroid Build Coastguard Worker
setup_ptr()121*6a54128fSAndroid Build Coastguard Worker static void setup_ptr()
122*6a54128fSAndroid Build Coastguard Worker {
123*6a54128fSAndroid Build Coastguard Worker char *cp;
124*6a54128fSAndroid Build Coastguard Worker
125*6a54128fSAndroid Build Coastguard Worker real_opendir = dlsym(RTLD_NEXT, "opendir");
126*6a54128fSAndroid Build Coastguard Worker real_fdopendir = dlsym(RTLD_NEXT, "fdopendir");
127*6a54128fSAndroid Build Coastguard Worker real_closedir = dlsym(RTLD_NEXT, "closedir");
128*6a54128fSAndroid Build Coastguard Worker real_rewinddir = dlsym(RTLD_NEXT, "rewinddir");
129*6a54128fSAndroid Build Coastguard Worker real_readdir = dlsym(RTLD_NEXT, "readdir");
130*6a54128fSAndroid Build Coastguard Worker real_readdir_r = dlsym(RTLD_NEXT, "readdir_r");
131*6a54128fSAndroid Build Coastguard Worker real_readdir64 = dlsym(RTLD_NEXT, "readdir64");
132*6a54128fSAndroid Build Coastguard Worker real_readdir64_r = dlsym(RTLD_NEXT, "readdir64_r");
133*6a54128fSAndroid Build Coastguard Worker real_telldir = dlsym(RTLD_NEXT, "telldir");
134*6a54128fSAndroid Build Coastguard Worker real_seekdir = dlsym(RTLD_NEXT, "seekdir");
135*6a54128fSAndroid Build Coastguard Worker real_dirfd = dlsym(RTLD_NEXT, "dirfd");
136*6a54128fSAndroid Build Coastguard Worker if ((cp = safe_getenv("SPD_READDIR_MAX_SIZE")) != NULL) {
137*6a54128fSAndroid Build Coastguard Worker max_dirsize = atol(cp);
138*6a54128fSAndroid Build Coastguard Worker }
139*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
140*6a54128fSAndroid Build Coastguard Worker if (safe_getenv("SPD_READDIR_DEBUG")) {
141*6a54128fSAndroid Build Coastguard Worker printf("initialized!\n");
142*6a54128fSAndroid Build Coastguard Worker do_debug++;
143*6a54128fSAndroid Build Coastguard Worker }
144*6a54128fSAndroid Build Coastguard Worker #endif
145*6a54128fSAndroid Build Coastguard Worker }
146*6a54128fSAndroid Build Coastguard Worker
free_cached_dir(struct dir_s * dirstruct)147*6a54128fSAndroid Build Coastguard Worker static void free_cached_dir(struct dir_s *dirstruct)
148*6a54128fSAndroid Build Coastguard Worker {
149*6a54128fSAndroid Build Coastguard Worker int i;
150*6a54128fSAndroid Build Coastguard Worker
151*6a54128fSAndroid Build Coastguard Worker pthread_mutex_destroy(&(dirstruct->lock));
152*6a54128fSAndroid Build Coastguard Worker
153*6a54128fSAndroid Build Coastguard Worker if (!dirstruct->dp)
154*6a54128fSAndroid Build Coastguard Worker return;
155*6a54128fSAndroid Build Coastguard Worker
156*6a54128fSAndroid Build Coastguard Worker for (i=0; i < dirstruct->num; i++) {
157*6a54128fSAndroid Build Coastguard Worker free(dirstruct->dp[i].d_name);
158*6a54128fSAndroid Build Coastguard Worker }
159*6a54128fSAndroid Build Coastguard Worker free(dirstruct->dp);
160*6a54128fSAndroid Build Coastguard Worker dirstruct->dp = 0;
161*6a54128fSAndroid Build Coastguard Worker dirstruct->max = dirstruct->num = 0;
162*6a54128fSAndroid Build Coastguard Worker }
163*6a54128fSAndroid Build Coastguard Worker
ino_cmp(const void * a,const void * b)164*6a54128fSAndroid Build Coastguard Worker static int ino_cmp(const void *a, const void *b)
165*6a54128fSAndroid Build Coastguard Worker {
166*6a54128fSAndroid Build Coastguard Worker const struct dirent_s *ds_a = (const struct dirent_s *) a;
167*6a54128fSAndroid Build Coastguard Worker const struct dirent_s *ds_b = (const struct dirent_s *) b;
168*6a54128fSAndroid Build Coastguard Worker ino_t i_a, i_b;
169*6a54128fSAndroid Build Coastguard Worker
170*6a54128fSAndroid Build Coastguard Worker i_a = ds_a->d_ino;
171*6a54128fSAndroid Build Coastguard Worker i_b = ds_b->d_ino;
172*6a54128fSAndroid Build Coastguard Worker
173*6a54128fSAndroid Build Coastguard Worker if (ds_a->d_name[0] == '.') {
174*6a54128fSAndroid Build Coastguard Worker if (ds_a->d_name[1] == 0)
175*6a54128fSAndroid Build Coastguard Worker i_a = 0;
176*6a54128fSAndroid Build Coastguard Worker else if ((ds_a->d_name[1] == '.') && (ds_a->d_name[2] == 0))
177*6a54128fSAndroid Build Coastguard Worker i_a = 1;
178*6a54128fSAndroid Build Coastguard Worker }
179*6a54128fSAndroid Build Coastguard Worker if (ds_b->d_name[0] == '.') {
180*6a54128fSAndroid Build Coastguard Worker if (ds_b->d_name[1] == 0)
181*6a54128fSAndroid Build Coastguard Worker i_b = 0;
182*6a54128fSAndroid Build Coastguard Worker else if ((ds_b->d_name[1] == '.') && (ds_b->d_name[2] == 0))
183*6a54128fSAndroid Build Coastguard Worker i_b = 1;
184*6a54128fSAndroid Build Coastguard Worker }
185*6a54128fSAndroid Build Coastguard Worker
186*6a54128fSAndroid Build Coastguard Worker return (i_a - i_b);
187*6a54128fSAndroid Build Coastguard Worker }
188*6a54128fSAndroid Build Coastguard Worker
alloc_dirstruct(DIR * dir)189*6a54128fSAndroid Build Coastguard Worker static struct dir_s *alloc_dirstruct(DIR *dir)
190*6a54128fSAndroid Build Coastguard Worker {
191*6a54128fSAndroid Build Coastguard Worker struct dir_s *dirstruct;
192*6a54128fSAndroid Build Coastguard Worker static pthread_mutexattr_t mutexattr;
193*6a54128fSAndroid Build Coastguard Worker mutexattr.__align = PTHREAD_MUTEX_RECURSIVE;
194*6a54128fSAndroid Build Coastguard Worker
195*6a54128fSAndroid Build Coastguard Worker dirstruct = malloc(sizeof(struct dir_s));
196*6a54128fSAndroid Build Coastguard Worker if (dirstruct)
197*6a54128fSAndroid Build Coastguard Worker memset(dirstruct, 0, sizeof(struct dir_s));
198*6a54128fSAndroid Build Coastguard Worker dirstruct->dir = dir;
199*6a54128fSAndroid Build Coastguard Worker pthread_mutex_init(&(dirstruct->lock), &mutexattr);
200*6a54128fSAndroid Build Coastguard Worker return dirstruct;
201*6a54128fSAndroid Build Coastguard Worker }
202*6a54128fSAndroid Build Coastguard Worker
cache_dirstruct(struct dir_s * dirstruct)203*6a54128fSAndroid Build Coastguard Worker static void cache_dirstruct(struct dir_s *dirstruct)
204*6a54128fSAndroid Build Coastguard Worker {
205*6a54128fSAndroid Build Coastguard Worker struct dirent_s *ds, *dnew;
206*6a54128fSAndroid Build Coastguard Worker struct dirent64 *d;
207*6a54128fSAndroid Build Coastguard Worker
208*6a54128fSAndroid Build Coastguard Worker while ((d = (*real_readdir64)(dirstruct->dir)) != NULL) {
209*6a54128fSAndroid Build Coastguard Worker if (dirstruct->num >= dirstruct->max) {
210*6a54128fSAndroid Build Coastguard Worker dirstruct->max += ALLOC_STEPSIZE;
211*6a54128fSAndroid Build Coastguard Worker DEBUG_DIR(printf("Reallocating to size %d\n",
212*6a54128fSAndroid Build Coastguard Worker dirstruct->max));
213*6a54128fSAndroid Build Coastguard Worker dnew = realloc(dirstruct->dp,
214*6a54128fSAndroid Build Coastguard Worker dirstruct->max * sizeof(struct dir_s));
215*6a54128fSAndroid Build Coastguard Worker if (!dnew)
216*6a54128fSAndroid Build Coastguard Worker goto nomem;
217*6a54128fSAndroid Build Coastguard Worker dirstruct->dp = dnew;
218*6a54128fSAndroid Build Coastguard Worker }
219*6a54128fSAndroid Build Coastguard Worker ds = &dirstruct->dp[dirstruct->num++];
220*6a54128fSAndroid Build Coastguard Worker ds->d_ino = d->d_ino;
221*6a54128fSAndroid Build Coastguard Worker ds->d_off = d->d_off;
222*6a54128fSAndroid Build Coastguard Worker ds->d_reclen = d->d_reclen;
223*6a54128fSAndroid Build Coastguard Worker ds->d_type = d->d_type;
224*6a54128fSAndroid Build Coastguard Worker if ((ds->d_name = malloc(strlen(d->d_name)+1)) == NULL) {
225*6a54128fSAndroid Build Coastguard Worker dirstruct->num--;
226*6a54128fSAndroid Build Coastguard Worker goto nomem;
227*6a54128fSAndroid Build Coastguard Worker }
228*6a54128fSAndroid Build Coastguard Worker strcpy(ds->d_name, d->d_name);
229*6a54128fSAndroid Build Coastguard Worker DEBUG_DIR(printf("readdir: %lu %s\n",
230*6a54128fSAndroid Build Coastguard Worker (unsigned long) d->d_ino, d->d_name));
231*6a54128fSAndroid Build Coastguard Worker }
232*6a54128fSAndroid Build Coastguard Worker qsort(dirstruct->dp, dirstruct->num, sizeof(struct dirent_s), ino_cmp);
233*6a54128fSAndroid Build Coastguard Worker return;
234*6a54128fSAndroid Build Coastguard Worker nomem:
235*6a54128fSAndroid Build Coastguard Worker DEBUG_DIR(printf("No memory, backing off to direct readdir\n"));
236*6a54128fSAndroid Build Coastguard Worker free_cached_dir(dirstruct);
237*6a54128fSAndroid Build Coastguard Worker dirstruct->direct = 1;
238*6a54128fSAndroid Build Coastguard Worker }
239*6a54128fSAndroid Build Coastguard Worker
opendir(const char * name)240*6a54128fSAndroid Build Coastguard Worker DIR *opendir(const char *name)
241*6a54128fSAndroid Build Coastguard Worker {
242*6a54128fSAndroid Build Coastguard Worker DIR *dir;
243*6a54128fSAndroid Build Coastguard Worker struct dir_s *dirstruct;
244*6a54128fSAndroid Build Coastguard Worker struct stat st;
245*6a54128fSAndroid Build Coastguard Worker
246*6a54128fSAndroid Build Coastguard Worker if (!real_opendir)
247*6a54128fSAndroid Build Coastguard Worker setup_ptr();
248*6a54128fSAndroid Build Coastguard Worker
249*6a54128fSAndroid Build Coastguard Worker DEBUG_DIR(printf("Opendir(%s) (%d open)\n", name, num_open++));
250*6a54128fSAndroid Build Coastguard Worker dir = (*real_opendir)(name);
251*6a54128fSAndroid Build Coastguard Worker if (!dir)
252*6a54128fSAndroid Build Coastguard Worker return NULL;
253*6a54128fSAndroid Build Coastguard Worker
254*6a54128fSAndroid Build Coastguard Worker dirstruct = alloc_dirstruct(dir);
255*6a54128fSAndroid Build Coastguard Worker if (!dirstruct) {
256*6a54128fSAndroid Build Coastguard Worker (*real_closedir)(dir);
257*6a54128fSAndroid Build Coastguard Worker errno = -ENOMEM;
258*6a54128fSAndroid Build Coastguard Worker return NULL;
259*6a54128fSAndroid Build Coastguard Worker }
260*6a54128fSAndroid Build Coastguard Worker
261*6a54128fSAndroid Build Coastguard Worker if (max_dirsize && (stat(name, &st) == 0) &&
262*6a54128fSAndroid Build Coastguard Worker (st.st_size > max_dirsize)) {
263*6a54128fSAndroid Build Coastguard Worker DEBUG_DIR(printf("Directory size %ld, using direct readdir\n",
264*6a54128fSAndroid Build Coastguard Worker st.st_size));
265*6a54128fSAndroid Build Coastguard Worker dirstruct->direct = 1;
266*6a54128fSAndroid Build Coastguard Worker return (DIR *) dirstruct;
267*6a54128fSAndroid Build Coastguard Worker }
268*6a54128fSAndroid Build Coastguard Worker
269*6a54128fSAndroid Build Coastguard Worker cache_dirstruct(dirstruct);
270*6a54128fSAndroid Build Coastguard Worker return ((DIR *) dirstruct);
271*6a54128fSAndroid Build Coastguard Worker }
272*6a54128fSAndroid Build Coastguard Worker
fdopendir(int fd)273*6a54128fSAndroid Build Coastguard Worker DIR *fdopendir(int fd)
274*6a54128fSAndroid Build Coastguard Worker {
275*6a54128fSAndroid Build Coastguard Worker DIR *dir;
276*6a54128fSAndroid Build Coastguard Worker struct dir_s *dirstruct;
277*6a54128fSAndroid Build Coastguard Worker struct stat st;
278*6a54128fSAndroid Build Coastguard Worker
279*6a54128fSAndroid Build Coastguard Worker if (!real_fdopendir)
280*6a54128fSAndroid Build Coastguard Worker setup_ptr();
281*6a54128fSAndroid Build Coastguard Worker
282*6a54128fSAndroid Build Coastguard Worker DEBUG_DIR(printf("fdopendir(%d) (%d open)\n", fd, num_open++));
283*6a54128fSAndroid Build Coastguard Worker dir = (*real_fdopendir)(fd);
284*6a54128fSAndroid Build Coastguard Worker if (!dir)
285*6a54128fSAndroid Build Coastguard Worker return NULL;
286*6a54128fSAndroid Build Coastguard Worker
287*6a54128fSAndroid Build Coastguard Worker dirstruct = alloc_dirstruct(dir);
288*6a54128fSAndroid Build Coastguard Worker if (!dirstruct) {
289*6a54128fSAndroid Build Coastguard Worker (*real_closedir)(dir);
290*6a54128fSAndroid Build Coastguard Worker errno = -ENOMEM;
291*6a54128fSAndroid Build Coastguard Worker return NULL;
292*6a54128fSAndroid Build Coastguard Worker }
293*6a54128fSAndroid Build Coastguard Worker
294*6a54128fSAndroid Build Coastguard Worker if (max_dirsize && (fstat(fd, &st) == 0) &&
295*6a54128fSAndroid Build Coastguard Worker (st.st_size > max_dirsize)) {
296*6a54128fSAndroid Build Coastguard Worker DEBUG_DIR(printf("Directory size %ld, using direct readdir\n",
297*6a54128fSAndroid Build Coastguard Worker st.st_size));
298*6a54128fSAndroid Build Coastguard Worker dirstruct->dir = dir;
299*6a54128fSAndroid Build Coastguard Worker dirstruct->direct = 1;
300*6a54128fSAndroid Build Coastguard Worker return (DIR *) dirstruct;
301*6a54128fSAndroid Build Coastguard Worker }
302*6a54128fSAndroid Build Coastguard Worker
303*6a54128fSAndroid Build Coastguard Worker cache_dirstruct(dirstruct);
304*6a54128fSAndroid Build Coastguard Worker return ((DIR *) dirstruct);
305*6a54128fSAndroid Build Coastguard Worker }
306*6a54128fSAndroid Build Coastguard Worker
closedir(DIR * dir)307*6a54128fSAndroid Build Coastguard Worker int closedir(DIR *dir)
308*6a54128fSAndroid Build Coastguard Worker {
309*6a54128fSAndroid Build Coastguard Worker struct dir_s *dirstruct = (struct dir_s *) dir;
310*6a54128fSAndroid Build Coastguard Worker
311*6a54128fSAndroid Build Coastguard Worker DEBUG_DIR(printf("Closedir (%d open)\n", --num_open));
312*6a54128fSAndroid Build Coastguard Worker if (dirstruct->dir)
313*6a54128fSAndroid Build Coastguard Worker (*real_closedir)(dirstruct->dir);
314*6a54128fSAndroid Build Coastguard Worker
315*6a54128fSAndroid Build Coastguard Worker free_cached_dir(dirstruct);
316*6a54128fSAndroid Build Coastguard Worker free(dirstruct);
317*6a54128fSAndroid Build Coastguard Worker return 0;
318*6a54128fSAndroid Build Coastguard Worker }
319*6a54128fSAndroid Build Coastguard Worker
readdir(DIR * dir)320*6a54128fSAndroid Build Coastguard Worker struct dirent *readdir(DIR *dir)
321*6a54128fSAndroid Build Coastguard Worker {
322*6a54128fSAndroid Build Coastguard Worker struct dir_s *dirstruct = (struct dir_s *) dir;
323*6a54128fSAndroid Build Coastguard Worker struct dirent_s *ds;
324*6a54128fSAndroid Build Coastguard Worker
325*6a54128fSAndroid Build Coastguard Worker if (dirstruct->direct)
326*6a54128fSAndroid Build Coastguard Worker return (*real_readdir)(dirstruct->dir);
327*6a54128fSAndroid Build Coastguard Worker
328*6a54128fSAndroid Build Coastguard Worker if (dirstruct->pos >= dirstruct->num)
329*6a54128fSAndroid Build Coastguard Worker return NULL;
330*6a54128fSAndroid Build Coastguard Worker
331*6a54128fSAndroid Build Coastguard Worker ds = &dirstruct->dp[dirstruct->pos++];
332*6a54128fSAndroid Build Coastguard Worker dirstruct->ret_dir.d_ino = ds->d_ino;
333*6a54128fSAndroid Build Coastguard Worker dirstruct->ret_dir.d_off = ds->d_off;
334*6a54128fSAndroid Build Coastguard Worker dirstruct->ret_dir.d_reclen = ds->d_reclen;
335*6a54128fSAndroid Build Coastguard Worker dirstruct->ret_dir.d_type = ds->d_type;
336*6a54128fSAndroid Build Coastguard Worker strncpy(dirstruct->ret_dir.d_name, ds->d_name,
337*6a54128fSAndroid Build Coastguard Worker sizeof(dirstruct->ret_dir.d_name));
338*6a54128fSAndroid Build Coastguard Worker
339*6a54128fSAndroid Build Coastguard Worker return (&dirstruct->ret_dir);
340*6a54128fSAndroid Build Coastguard Worker }
341*6a54128fSAndroid Build Coastguard Worker
readdir_r(DIR * dir,struct dirent * entry,struct dirent ** result)342*6a54128fSAndroid Build Coastguard Worker int readdir_r(DIR *dir, struct dirent *entry, struct dirent **result)
343*6a54128fSAndroid Build Coastguard Worker {
344*6a54128fSAndroid Build Coastguard Worker struct dir_s *dirstruct = (struct dir_s *) dir;
345*6a54128fSAndroid Build Coastguard Worker struct dirent_s *ds;
346*6a54128fSAndroid Build Coastguard Worker
347*6a54128fSAndroid Build Coastguard Worker if (dirstruct->direct)
348*6a54128fSAndroid Build Coastguard Worker return (*real_readdir_r)(dirstruct->dir, entry, result);
349*6a54128fSAndroid Build Coastguard Worker
350*6a54128fSAndroid Build Coastguard Worker pthread_mutex_lock(&(dirstruct->lock));
351*6a54128fSAndroid Build Coastguard Worker if (dirstruct->pos >= dirstruct->num) {
352*6a54128fSAndroid Build Coastguard Worker *result = NULL;
353*6a54128fSAndroid Build Coastguard Worker } else {
354*6a54128fSAndroid Build Coastguard Worker ds = &dirstruct->dp[dirstruct->pos++];
355*6a54128fSAndroid Build Coastguard Worker entry->d_ino = ds->d_ino;
356*6a54128fSAndroid Build Coastguard Worker entry->d_off = ds->d_off;
357*6a54128fSAndroid Build Coastguard Worker entry->d_reclen = ds->d_reclen;
358*6a54128fSAndroid Build Coastguard Worker entry->d_type = ds->d_type;
359*6a54128fSAndroid Build Coastguard Worker strncpy(entry->d_name, ds->d_name, sizeof(entry->d_name));
360*6a54128fSAndroid Build Coastguard Worker *result = entry;
361*6a54128fSAndroid Build Coastguard Worker }
362*6a54128fSAndroid Build Coastguard Worker pthread_mutex_unlock(&(dirstruct->lock));
363*6a54128fSAndroid Build Coastguard Worker return 0;
364*6a54128fSAndroid Build Coastguard Worker }
365*6a54128fSAndroid Build Coastguard Worker
readdir64(DIR * dir)366*6a54128fSAndroid Build Coastguard Worker struct dirent64 *readdir64(DIR *dir)
367*6a54128fSAndroid Build Coastguard Worker {
368*6a54128fSAndroid Build Coastguard Worker struct dir_s *dirstruct = (struct dir_s *) dir;
369*6a54128fSAndroid Build Coastguard Worker struct dirent_s *ds;
370*6a54128fSAndroid Build Coastguard Worker
371*6a54128fSAndroid Build Coastguard Worker if (dirstruct->direct)
372*6a54128fSAndroid Build Coastguard Worker return (*real_readdir64)(dirstruct->dir);
373*6a54128fSAndroid Build Coastguard Worker
374*6a54128fSAndroid Build Coastguard Worker if (dirstruct->pos >= dirstruct->num)
375*6a54128fSAndroid Build Coastguard Worker return NULL;
376*6a54128fSAndroid Build Coastguard Worker
377*6a54128fSAndroid Build Coastguard Worker ds = &dirstruct->dp[dirstruct->pos++];
378*6a54128fSAndroid Build Coastguard Worker dirstruct->ret_dir64.d_ino = ds->d_ino;
379*6a54128fSAndroid Build Coastguard Worker dirstruct->ret_dir64.d_off = ds->d_off;
380*6a54128fSAndroid Build Coastguard Worker dirstruct->ret_dir64.d_reclen = ds->d_reclen;
381*6a54128fSAndroid Build Coastguard Worker dirstruct->ret_dir64.d_type = ds->d_type;
382*6a54128fSAndroid Build Coastguard Worker strncpy(dirstruct->ret_dir64.d_name, ds->d_name,
383*6a54128fSAndroid Build Coastguard Worker sizeof(dirstruct->ret_dir64.d_name));
384*6a54128fSAndroid Build Coastguard Worker
385*6a54128fSAndroid Build Coastguard Worker return (&dirstruct->ret_dir64);
386*6a54128fSAndroid Build Coastguard Worker }
387*6a54128fSAndroid Build Coastguard Worker
readdir64_r(DIR * __restrict dir,struct dirent64 * __restrict entry,struct dirent64 ** __restrict result)388*6a54128fSAndroid Build Coastguard Worker int readdir64_r (DIR *__restrict dir,
389*6a54128fSAndroid Build Coastguard Worker struct dirent64 *__restrict entry,
390*6a54128fSAndroid Build Coastguard Worker struct dirent64 **__restrict result)
391*6a54128fSAndroid Build Coastguard Worker {
392*6a54128fSAndroid Build Coastguard Worker struct dir_s *dirstruct = (struct dir_s *) dir;
393*6a54128fSAndroid Build Coastguard Worker struct dirent_s *ds;
394*6a54128fSAndroid Build Coastguard Worker
395*6a54128fSAndroid Build Coastguard Worker if (dirstruct->direct)
396*6a54128fSAndroid Build Coastguard Worker return (*real_readdir64_r)(dir, entry, result);
397*6a54128fSAndroid Build Coastguard Worker pthread_mutex_lock(&(dirstruct->lock));
398*6a54128fSAndroid Build Coastguard Worker if (dirstruct->pos >= dirstruct->num) {
399*6a54128fSAndroid Build Coastguard Worker *result = NULL;
400*6a54128fSAndroid Build Coastguard Worker } else {
401*6a54128fSAndroid Build Coastguard Worker ds = &dirstruct->dp[dirstruct->pos++];
402*6a54128fSAndroid Build Coastguard Worker entry->d_ino = ds->d_ino;
403*6a54128fSAndroid Build Coastguard Worker entry->d_off = ds->d_off;
404*6a54128fSAndroid Build Coastguard Worker entry->d_reclen = ds->d_reclen;
405*6a54128fSAndroid Build Coastguard Worker entry->d_type = ds->d_type;
406*6a54128fSAndroid Build Coastguard Worker strncpy(entry->d_name, ds->d_name,
407*6a54128fSAndroid Build Coastguard Worker sizeof(entry->d_name));
408*6a54128fSAndroid Build Coastguard Worker *result = entry;
409*6a54128fSAndroid Build Coastguard Worker }
410*6a54128fSAndroid Build Coastguard Worker pthread_mutex_unlock(&(dirstruct->lock));
411*6a54128fSAndroid Build Coastguard Worker return 0;
412*6a54128fSAndroid Build Coastguard Worker }
413*6a54128fSAndroid Build Coastguard Worker
telldir(DIR * dir)414*6a54128fSAndroid Build Coastguard Worker off_t telldir(DIR *dir)
415*6a54128fSAndroid Build Coastguard Worker {
416*6a54128fSAndroid Build Coastguard Worker struct dir_s *dirstruct = (struct dir_s *) dir;
417*6a54128fSAndroid Build Coastguard Worker
418*6a54128fSAndroid Build Coastguard Worker if (dirstruct->direct)
419*6a54128fSAndroid Build Coastguard Worker return (*real_telldir)(dirstruct->dir);
420*6a54128fSAndroid Build Coastguard Worker
421*6a54128fSAndroid Build Coastguard Worker return ((off_t) dirstruct->pos);
422*6a54128fSAndroid Build Coastguard Worker }
423*6a54128fSAndroid Build Coastguard Worker
seekdir(DIR * dir,off_t offset)424*6a54128fSAndroid Build Coastguard Worker void seekdir(DIR *dir, off_t offset)
425*6a54128fSAndroid Build Coastguard Worker {
426*6a54128fSAndroid Build Coastguard Worker struct dir_s *dirstruct = (struct dir_s *) dir;
427*6a54128fSAndroid Build Coastguard Worker
428*6a54128fSAndroid Build Coastguard Worker if (dirstruct->direct) {
429*6a54128fSAndroid Build Coastguard Worker (*real_seekdir)(dirstruct->dir, offset);
430*6a54128fSAndroid Build Coastguard Worker return;
431*6a54128fSAndroid Build Coastguard Worker }
432*6a54128fSAndroid Build Coastguard Worker
433*6a54128fSAndroid Build Coastguard Worker dirstruct->pos = offset;
434*6a54128fSAndroid Build Coastguard Worker }
435*6a54128fSAndroid Build Coastguard Worker
rewinddir(DIR * dir)436*6a54128fSAndroid Build Coastguard Worker void rewinddir(DIR *dir)
437*6a54128fSAndroid Build Coastguard Worker {
438*6a54128fSAndroid Build Coastguard Worker struct dir_s *dirstruct = (struct dir_s *) dir;
439*6a54128fSAndroid Build Coastguard Worker
440*6a54128fSAndroid Build Coastguard Worker (*real_rewinddir)(dirstruct->dir);
441*6a54128fSAndroid Build Coastguard Worker if (dirstruct->direct)
442*6a54128fSAndroid Build Coastguard Worker return;
443*6a54128fSAndroid Build Coastguard Worker
444*6a54128fSAndroid Build Coastguard Worker pthread_mutex_lock(&(dirstruct->lock));
445*6a54128fSAndroid Build Coastguard Worker dirstruct->pos = 0;
446*6a54128fSAndroid Build Coastguard Worker free_cached_dir(dirstruct);
447*6a54128fSAndroid Build Coastguard Worker cache_dirstruct(dirstruct);
448*6a54128fSAndroid Build Coastguard Worker pthread_mutex_unlock(&(dirstruct->lock));
449*6a54128fSAndroid Build Coastguard Worker }
450*6a54128fSAndroid Build Coastguard Worker
dirfd(DIR * dir)451*6a54128fSAndroid Build Coastguard Worker int dirfd(DIR *dir)
452*6a54128fSAndroid Build Coastguard Worker {
453*6a54128fSAndroid Build Coastguard Worker struct dir_s *dirstruct = (struct dir_s *) dir;
454*6a54128fSAndroid Build Coastguard Worker int fd = (*real_dirfd)(dirstruct->dir);
455*6a54128fSAndroid Build Coastguard Worker
456*6a54128fSAndroid Build Coastguard Worker DEBUG_DIR(printf("dirfd %d, %p\n", fd, real_dirfd));
457*6a54128fSAndroid Build Coastguard Worker return fd;
458*6a54128fSAndroid Build Coastguard Worker }
459