OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [libc/] [misc/] [ftw/] [ftw.c] - Blame information for rev 1774

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/* File tree walker functions.
2
   Copyright (C) 1996-2001, 2002, 2003 Free Software Foundation, Inc.
3
   This file is part of the GNU C Library.
4
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
5
 
6
   The GNU C Library is free software; you can redistribute it and/or
7
   modify it under the terms of the GNU Lesser General Public
8
   License as published by the Free Software Foundation; either
9
   version 2.1 of the License, or (at your option) any later version.
10
 
11
   The GNU C Library is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
   Lesser General Public License for more details.
15
 
16
   You should have received a copy of the GNU Lesser General Public
17
   License along with the GNU C Library; if not, write to the Free
18
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19
   02111-1307 USA.  */
20
 
21
#define _GNU_SOURCE
22
#include <features.h>
23
 
24
 
25
#if defined (__UCLIBC_HAS_LFS__) && defined L_ftw64
26
#define L_ftw
27
 
28
/* If Large file support is enabled, transparently remap
29
 * things to use the 64-bit interfaces */
30
#if defined _FILE_OFFSET_BITS && _FILE_OFFSET_BITS != 64 
31
#undef _FILE_OFFSET_BITS
32
#define _FILE_OFFSET_BITS   64
33
#endif
34
#ifndef __USE_LARGEFILE64
35
# define __USE_LARGEFILE64  1
36
#endif
37
#ifndef __USE_FILE_OFFSET64
38
# define __USE_FILE_OFFSET64  1
39
#endif
40
 
41
#define FTW_NAME ftw64
42
#define NFTW_NAME nftw64
43
#define INO_T ino64_t
44
#define STAT stat64
45
#define LSTAT lstat64
46
#define XSTAT stat64
47
#define FTW_FUNC_T __ftw64_func_t
48
#define NFTW_FUNC_T __nftw64_func_t
49
#else
50
#define FTW_NAME ftw
51
#define NFTW_NAME nftw
52
#define INO_T ino_t
53
#define STAT stat
54
#define LSTAT lstat
55
#define XSTAT stat
56
#define FTW_FUNC_T __ftw_func_t
57
#define NFTW_FUNC_T __nftw_func_t
58
#endif
59
 
60
#ifdef L_ftw
61
 
62
#include <alloca.h>
63
#include <errno.h>
64
#include <ftw.h>
65
#include <limits.h>
66
#include <search.h>
67
#include <stdlib.h>
68
#include <string.h>
69
#include <unistd.h>
70
#include <sys/param.h>
71
#include <sys/stat.h>
72
#include <assert.h>
73
#include <dirent.h>
74
 
75
/* We define PATH_MAX if the system does not provide a definition.
76
   This does not artificially limit any operation.  PATH_MAX is simply
77
   used as a guesstimate for the expected maximal path length.
78
   Buffers will be enlarged if necessary.  */
79
#ifndef PATH_MAX
80
# define PATH_MAX 1024
81
#endif
82
 
83
struct dir_data
84
{
85
    DIR *stream;
86
    char *content;
87
};
88
 
89
struct known_object
90
{
91
    dev_t dev;
92
    INO_T ino;
93
};
94
 
95
struct ftw_data
96
{
97
    /* Array with pointers to open directory streams.  */
98
    struct dir_data **dirstreams;
99
    size_t actdir;
100
    size_t maxdir;
101
 
102
    /* Buffer containing name of currently processed object.  */
103
    char *dirbuf;
104
    size_t dirbufsize;
105
 
106
    /* Passed as fourth argument to `nftw' callback.  The `base' member
107
       tracks the content of the `dirbuf'.  */
108
    struct FTW ftw;
109
 
110
    /* Flags passed to `nftw' function.  0 for `ftw'.  */
111
    int flags;
112
 
113
    /* Conversion array for flag values.  It is the identity mapping for
114
       `nftw' calls, otherwise it maps the values to those known by
115
       `ftw'.  */
116
    const int *cvt_arr;
117
 
118
    /* Callback function.  We always use the `nftw' form.  */
119
    NFTW_FUNC_T func;
120
 
121
    /* Device of starting point.  Needed for FTW_MOUNT.  */
122
    dev_t dev;
123
 
124
    /* Data structure for keeping fingerprints of already processed
125
       object.  This is needed when not using FTW_PHYS.  */
126
    void *known_objects;
127
};
128
 
129
 
130
/* Internally we use the FTW_* constants used for `nftw'.  When invoked
131
   as `ftw', map each flag to the subset of values used by `ftw'.  */
132
static const int nftw_arr[] =
133
{
134
    FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_SL, FTW_DP, FTW_SLN
135
};
136
 
137
static const int ftw_arr[] =
138
{
139
    FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_F, FTW_D, FTW_NS
140
};
141
 
142
/* Forward declarations of local functions.  */
143
static int ftw_dir (struct ftw_data *data, struct STAT *st) internal_function;
144
 
145
 
146
static int
147
object_compare (const void *p1, const void *p2)
148
{
149
    /* We don't need a sophisticated and useful comparison.  We are only
150
       interested in equality.  However, we must be careful not to
151
       accidentally compare `holes' in the structure.  */
152
    const struct known_object *kp1 = p1, *kp2 = p2;
153
    int cmp1;
154
    cmp1 = (kp1->ino > kp2->ino) - (kp1->ino < kp2->ino);
155
    if (cmp1 != 0)
156
        return cmp1;
157
    return (kp1->dev > kp2->dev) - (kp1->dev < kp2->dev);
158
}
159
 
160
 
161
static inline int
162
add_object (struct ftw_data *data, struct STAT *st)
163
{
164
    struct known_object *newp = malloc (sizeof (struct known_object));
165
    if (newp == NULL)
166
        return -1;
167
    newp->dev = st->st_dev;
168
    newp->ino = st->st_ino;
169
    return tsearch (newp, &data->known_objects, object_compare) ? 0 : -1;
170
}
171
 
172
 
173
static inline int
174
find_object (struct ftw_data *data, struct STAT *st)
175
{
176
    struct known_object obj;
177
    obj.dev = st->st_dev;
178
    obj.ino = st->st_ino;
179
    return tfind (&obj, &data->known_objects, object_compare) != NULL;
180
}
181
 
182
 
183
static inline int
184
__attribute ((always_inline))
185
open_dir_stream (struct ftw_data *data, struct dir_data *dirp)
186
{
187
    int result = 0;
188
 
189
    if (data->dirstreams[data->actdir] != NULL)
190
    {
191
        /* Oh, oh.  We must close this stream.  Get all remaining
192
           entries and store them as a list in the `content' member of
193
           the `struct dir_data' variable.  */
194
        size_t bufsize = 1024;
195
        char *buf = malloc (bufsize);
196
 
197
        if (buf == NULL)
198
            result = -1;
199
        else
200
        {
201
            DIR *st = data->dirstreams[data->actdir]->stream;
202
            struct dirent *d;
203
            size_t actsize = 0;
204
 
205
            while ((d = readdir (st)) != NULL)
206
            {
207
                size_t this_len = _D_EXACT_NAMLEN (d);
208
                if (actsize + this_len + 2 >= bufsize)
209
                {
210
                    char *newp;
211
                    bufsize += MAX (1024, 2 * this_len);
212
                    newp = (char *) realloc (buf, bufsize);
213
                    if (newp == NULL)
214
                    {
215
                        /* No more memory.  */
216
                        int save_err = errno;
217
                        free (buf);
218
                        __set_errno (save_err);
219
                        result = -1;
220
                        break;
221
                    }
222
                    buf = newp;
223
                }
224
 
225
                *((char *) mempcpy (buf + actsize, d->d_name, this_len))
226
                    = '\0';
227
                actsize += this_len + 1;
228
            }
229
 
230
            /* Terminate the list with an additional NUL byte.  */
231
            buf[actsize++] = '\0';
232
 
233
            /* Shrink the buffer to what we actually need.  */
234
            data->dirstreams[data->actdir]->content = realloc (buf, actsize);
235
            if (data->dirstreams[data->actdir]->content == NULL)
236
            {
237
                int save_err = errno;
238
                free (buf);
239
                __set_errno (save_err);
240
                result = -1;
241
            }
242
            else
243
            {
244
                closedir (st);
245
                data->dirstreams[data->actdir]->stream = NULL;
246
                data->dirstreams[data->actdir] = NULL;
247
            }
248
        }
249
    }
250
 
251
    /* Open the new stream.  */
252
    if (result == 0)
253
    {
254
        const char *name = ((data->flags & FTW_CHDIR)
255
                ? data->dirbuf + data->ftw.base: data->dirbuf);
256
        assert (data->dirstreams[data->actdir] == NULL);
257
 
258
        dirp->stream = opendir (name);
259
        if (dirp->stream == NULL)
260
            result = -1;
261
        else
262
        {
263
            dirp->content = NULL;
264
            data->dirstreams[data->actdir] = dirp;
265
 
266
            if (++data->actdir == data->maxdir)
267
                data->actdir = 0;
268
        }
269
    }
270
 
271
    return result;
272
}
273
 
274
 
275
static int
276
internal_function
277
process_entry (struct ftw_data *data, struct dir_data *dir, const char *name, size_t namlen)
278
{
279
    struct STAT st;
280
    int result = 0;
281
    int flag = 0;
282
    size_t new_buflen;
283
 
284
    if (name[0] == '.' && (name[1] == '\0'
285
                || (name[1] == '.' && name[2] == '\0')))
286
        /* Don't process the "." and ".." entries.  */
287
        return 0;
288
 
289
    new_buflen = data->ftw.base + namlen + 2;
290
    if (data->dirbufsize < new_buflen)
291
    {
292
        /* Enlarge the buffer.  */
293
        char *newp;
294
 
295
        data->dirbufsize = 2 * new_buflen;
296
        newp = (char *) realloc (data->dirbuf, data->dirbufsize);
297
        if (newp == NULL)
298
            return -1;
299
        data->dirbuf = newp;
300
    }
301
 
302
    *((char *) mempcpy (data->dirbuf + data->ftw.base, name, namlen)) = '\0';
303
 
304
    if ((data->flags & FTW_CHDIR) == 0)
305
        name = data->dirbuf;
306
 
307
    if (((data->flags & FTW_PHYS)
308
                ? LSTAT (name, &st)
309
                : XSTAT (name, &st)) < 0)
310
    {
311
        if (errno != EACCES && errno != ENOENT)
312
            result = -1;
313
        else if (!(data->flags & FTW_PHYS)
314
                && LSTAT (name, &st) == 0
315
                && S_ISLNK (st.st_mode))
316
            flag = FTW_SLN;
317
        else
318
            flag = FTW_NS;
319
    }
320
    else
321
    {
322
        if (S_ISDIR (st.st_mode))
323
            flag = FTW_D;
324
        else if (S_ISLNK (st.st_mode))
325
            flag = FTW_SL;
326
        else
327
            flag = FTW_F;
328
    }
329
 
330
    if (result == 0
331
            && (flag == FTW_NS
332
                || !(data->flags & FTW_MOUNT) || st.st_dev == data->dev))
333
    {
334
        if (flag == FTW_D)
335
        {
336
            if ((data->flags & FTW_PHYS)
337
                    || (!find_object (data, &st)
338
                        /* Remember the object.  */
339
                        && (result = add_object (data, &st)) == 0))
340
            {
341
                result = ftw_dir (data, &st);
342
 
343
                if (result == 0 && (data->flags & FTW_CHDIR))
344
                {
345
                    /* Change back to the parent directory.  */
346
                    int done = 0;
347
                    if (dir->stream != NULL)
348
                        if (fchdir (dirfd (dir->stream)) == 0)
349
                            done = 1;
350
 
351
                    if (!done)
352
                    {
353
                        if (data->ftw.base == 1)
354
                        {
355
                            if (chdir ("/") < 0)
356
                                result = -1;
357
                        }
358
                        else
359
                            if (chdir ("..") < 0)
360
                                result = -1;
361
                    }
362
                }
363
            }
364
        }
365
        else
366
            result = (*data->func) (data->dirbuf, &st, data->cvt_arr[flag],
367
                    &data->ftw);
368
    }
369
 
370
    return result;
371
}
372
 
373
static int
374
internal_function
375
ftw_dir (struct ftw_data *data, struct STAT *st)
376
{
377
    struct dir_data dir;
378
    struct dirent *d;
379
    int previous_base = data->ftw.base;
380
    int result;
381
    char *startp;
382
 
383
    /* Open the stream for this directory.  This might require that
384
       another stream has to be closed.  */
385
    result = open_dir_stream (data, &dir);
386
    if (result != 0)
387
    {
388
        if (errno == EACCES)
389
            /* We cannot read the directory.  Signal this with a special flag.  */
390
            result = (*data->func) (data->dirbuf, st, FTW_DNR, &data->ftw);
391
 
392
        return result;
393
    }
394
 
395
    /* First, report the directory (if not depth-first).  */
396
    if (!(data->flags & FTW_DEPTH))
397
    {
398
        result = (*data->func) (data->dirbuf, st, FTW_D, &data->ftw);
399
        if (result != 0)
400
            return result;
401
    }
402
 
403
    /* If necessary, change to this directory.  */
404
    if (data->flags & FTW_CHDIR)
405
    {
406
        if (fchdir (dirfd (dir.stream)) < 0)
407
        {
408
            int save_err = errno;
409
            closedir (dir.stream);
410
            __set_errno (save_err);
411
 
412
            if (data->actdir-- == 0)
413
                data->actdir = data->maxdir - 1;
414
            data->dirstreams[data->actdir] = NULL;
415
 
416
            return -1;
417
        }
418
    }
419
 
420
    /* Next, update the `struct FTW' information.  */
421
    ++data->ftw.level;
422
    startp = strchr (data->dirbuf, '\0');
423
    /* There always must be a directory name.  */
424
    assert (startp != data->dirbuf);
425
    if (startp[-1] != '/')
426
        *startp++ = '/';
427
    data->ftw.base = startp - data->dirbuf;
428
 
429
    while (dir.stream != NULL && (d = readdir (dir.stream)) != NULL)
430
    {
431
        result = process_entry (data, &dir, d->d_name, _D_EXACT_NAMLEN (d));
432
        if (result != 0)
433
            break;
434
    }
435
 
436
    if (dir.stream != NULL)
437
    {
438
        /* The stream is still open.  I.e., we did not need more
439
           descriptors.  Simply close the stream now.  */
440
        int save_err = errno;
441
 
442
        assert (dir.content == NULL);
443
 
444
        closedir (dir.stream);
445
        __set_errno (save_err);
446
 
447
        if (data->actdir-- == 0)
448
            data->actdir = data->maxdir - 1;
449
        data->dirstreams[data->actdir] = NULL;
450
    }
451
    else
452
    {
453
        int save_err;
454
        char *runp = dir.content;
455
 
456
        while (result == 0 && *runp != '\0')
457
        {
458
            char *endp = strchr (runp, '\0');
459
 
460
            result = process_entry (data, &dir, runp, endp - runp);
461
 
462
            runp = endp + 1;
463
        }
464
 
465
        save_err = errno;
466
        free (dir.content);
467
        __set_errno (save_err);
468
    }
469
 
470
    /* Prepare the return, revert the `struct FTW' information.  */
471
    data->dirbuf[data->ftw.base - 1] = '\0';
472
    --data->ftw.level;
473
    data->ftw.base = previous_base;
474
 
475
    /* Finally, if we process depth-first report the directory.  */
476
    if (result == 0 && (data->flags & FTW_DEPTH))
477
        result = (*data->func) (data->dirbuf, st, FTW_DP, &data->ftw);
478
 
479
    return result;
480
}
481
 
482
 
483
static int
484
internal_function
485
ftw_startup (const char *dir, int is_nftw, void *func, int descriptors, int flags)
486
{
487
    struct ftw_data data;
488
    struct STAT st;
489
    int result = 0;
490
    int save_err;
491
    char *cwd = NULL;
492
    char *cp;
493
 
494
    /* First make sure the parameters are reasonable.  */
495
    if (unlikely(dir==NULL || *dir=='\0')) {
496
        __set_errno (ENOENT);
497
        return -1;
498
    }
499
    if ((strlen(dir)+1) > NAME_MAX) {
500
        __set_errno(ENAMETOOLONG);
501
        return -1;
502
    }
503
 
504
    data.maxdir = descriptors < 1 ? 1 : descriptors;
505
    data.actdir = 0;
506
    data.dirstreams = (struct dir_data **) alloca (data.maxdir
507
            * sizeof (struct dir_data *));
508
    memset (data.dirstreams, '\0', data.maxdir * sizeof (struct dir_data *));
509
 
510
    /* PATH_MAX is always defined when we get here.  */
511
    data.dirbufsize = MAX (2 * strlen (dir), PATH_MAX);
512
    data.dirbuf = (char *) malloc (data.dirbufsize);
513
    if (data.dirbuf == NULL)
514
        return -1;
515
    cp = stpcpy (data.dirbuf, dir);
516
    /* Strip trailing slashes.  */
517
    while (cp > data.dirbuf + 1 && cp[-1] == '/')
518
        --cp;
519
    *cp = '\0';
520
 
521
    data.ftw.level = 0;
522
 
523
    /* Find basename.  */
524
    while (cp > data.dirbuf && cp[-1] != '/')
525
        --cp;
526
    data.ftw.base = cp - data.dirbuf;
527
 
528
    data.flags = flags;
529
 
530
    /* This assignment might seem to be strange but it is what we want.
531
       The trick is that the first three arguments to the `ftw' and
532
       `nftw' callback functions are equal.  Therefore we can call in
533
       every case the callback using the format of the `nftw' version
534
       and get the correct result since the stack layout for a function
535
       call in C allows this.  */
536
    data.func = (NFTW_FUNC_T) func;
537
 
538
    /* Since we internally use the complete set of FTW_* values we need
539
       to reduce the value range before calling a `ftw' callback.  */
540
    data.cvt_arr = is_nftw ? nftw_arr : ftw_arr;
541
 
542
    /* No object known so far.  */
543
    data.known_objects = NULL;
544
 
545
    /* Now go to the directory containing the initial file/directory.  */
546
    if (flags & FTW_CHDIR)
547
    {
548
        /* GNU extension ahead.  */
549
        cwd =  getcwd (NULL, 0);
550
        if (cwd == NULL)
551
            result = -1;
552
        else if (data.ftw.base > 0)
553
        {
554
            /* Change to the directory the file is in.  In data.dirbuf
555
               we have a writable copy of the file name.  Just NUL
556
               terminate it for now and change the directory.  */
557
            if (data.ftw.base == 1)
558
                /* I.e., the file is in the root directory.  */
559
                result = chdir ("/");
560
            else
561
            {
562
                char ch = data.dirbuf[data.ftw.base - 1];
563
                data.dirbuf[data.ftw.base - 1] = '\0';
564
                result = chdir (data.dirbuf);
565
                data.dirbuf[data.ftw.base - 1] = ch;
566
            }
567
        }
568
    }
569
 
570
    /* Get stat info for start directory.  */
571
    if (result == 0)
572
    {
573
        const char *name = ((data.flags & FTW_CHDIR)
574
                ? data.dirbuf + data.ftw.base
575
                : data.dirbuf);
576
 
577
        if (((flags & FTW_PHYS)
578
                    ? LSTAT (name, &st)
579
                    : XSTAT (name, &st)) < 0)
580
        {
581
            if (!(flags & FTW_PHYS)
582
                    && errno == ENOENT
583
                    && LSTAT (name, &st) == 0
584
                    && S_ISLNK (st.st_mode))
585
                result = (*data.func) (data.dirbuf, &st, data.cvt_arr[FTW_SLN],
586
                        &data.ftw);
587
            else
588
                /* No need to call the callback since we cannot say anything
589
                   about the object.  */
590
                result = -1;
591
        }
592
        else
593
        {
594
            if (S_ISDIR (st.st_mode))
595
            {
596
                /* Remember the device of the initial directory in case
597
                   FTW_MOUNT is given.  */
598
                data.dev = st.st_dev;
599
 
600
                /* We know this directory now.  */
601
                if (!(flags & FTW_PHYS))
602
                    result = add_object (&data, &st);
603
 
604
                if (result == 0)
605
                    result = ftw_dir (&data, &st);
606
            }
607
            else
608
            {
609
                int flag = S_ISLNK (st.st_mode) ? FTW_SL : FTW_F;
610
 
611
                result = (*data.func) (data.dirbuf, &st, data.cvt_arr[flag],
612
                        &data.ftw);
613
            }
614
        }
615
    }
616
 
617
    /* Return to the start directory (if necessary).  */
618
    if (cwd != NULL)
619
    {
620
        int save_err = errno;
621
        chdir (cwd);
622
        free (cwd);
623
        __set_errno (save_err);
624
    }
625
 
626
    /* Free all memory.  */
627
    save_err = errno;
628
    tdestroy (data.known_objects, free);
629
    free (data.dirbuf);
630
    __set_errno (save_err);
631
 
632
    return result;
633
}
634
 
635
 
636
 
637
/* Entry points.  */
638
 
639
int FTW_NAME (const char *path, FTW_FUNC_T func, int descriptors)
640
{
641
    return ftw_startup (path, 0, func, descriptors, 0);
642
}
643
 
644
int NFTW_NAME (const char *path, NFTW_FUNC_T func, int descriptors, int flags)
645
{
646
    return ftw_startup (path, 1, func, descriptors, flags);
647
}
648
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.