summaryrefslogtreecommitdiffstats
path: root/lib/libc/gen/ftw.c
blob: a13aa171dbcb8c54c4491421e0ad2d37c8bd7887 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * Copyright (c) 2003 by Joel Baker.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the Author nor the names of any contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <sys/types.h> /* Because fts(3) says so */
#include <sys/stat.h>
#include <fts.h>

#include <unistd.h> /* We want strcpy */

#include <errno.h> /* Because errno is our friend */

#include "ftw.h"

/* I like symbolic values - this is only used in this file. */

enum __ftw_modes {
    MODE_FTW,
    MODE_NFTW
};

/* Prototype this so that we can have it later */

static int __ftw_core(const char *, void *, int, int, enum __ftw_modes);

/*
 * The external function calls are really just wrappers around __ftw_core,
 * since the work they do is 90% the same.
 */

int ftw (const char *dir, __ftw_func_t func, int descr) {
    return __ftw_core(dir, func, descr, 0, MODE_FTW);
}

int nftw (const char *dir, __nftw_func_t func, int descr, int flags) {
    return __ftw_core(dir, func, descr, flags, MODE_NFTW);
}

/*
typedef int (*__ftw_func_t) \
    (const char *file, const struct stat status, int flag);
typedef int (*__nftw_func_t) \
    (const char *file, const struct stat status, int flag, struct FTW detail);
*/

static int __ftw_core(const char *dir, void *func, int descr, int flags,
                      enum __ftw_modes mode) {
    FTS *hierarchy;
    FTSENT *entry;
    int fts_options;
    const char *paths[2];
    int ftw_flag, func_ret;
    struct FTW ftw_st;
    int skip_entry;
    __ftw_func_t ftw_func;
    __nftw_func_t nftw_func;
    int saved_errno;

    errno = 0;

    /* We need at least one descriptor to call fts */

    if (descr < 1) {
        errno = EINVAL;
        return -1;
    }

    /* Decide which mode we're running in, and set the FTS options suitably. */

    if (MODE_NFTW == mode) { /* NFTW mode, with all the bells and whistles. */
        fts_options = (flags & FTW_PHYS) ? FTS_PHYSICAL : FTS_LOGICAL;
        fts_options |= (flags & FTW_CHDIR) ? 0 : FTS_NOCHDIR;
        fts_options |= (flags & FTW_MOUNT) ? FTS_XDEV : 0;
    } else { /* We must be in FTW mode. Nothing else makes sense. */
        fts_options = FTS_LOGICAL;
    }

    /* FTW gets a const char *, but FTS expects a null-term array of them. */

    paths[0] = dir;
    paths[1] = NULL;

    /* Open the file hierarchy. */

    if (!(hierarchy = fts_open((char * const *)paths, fts_options, NULL))) {
        if (EACCES == errno) {
            return 0;
        } else {
            return -1;
        }
    }

    /* The main loop. Is it not nifty? Worship the loop. */

    while ((entry = fts_read(hierarchy))) {
        skip_entry = 0;

        switch (entry->fts_info) {

            case FTS_D:
                if ((MODE_NFTW != mode) || !(flags & FTW_DEPTH)) {
                    ftw_flag = FTW_D;
                } else {
                    skip_entry = 1;
                }
                break;

            case FTS_DNR:
                ftw_flag = FTW_DNR;
                break;

            case FTS_F:
                ftw_flag = FTW_F;
                break;

            case FTS_SL:
                ftw_flag = FTW_SL;
                break;

            case FTS_NS:
                ftw_flag = FTW_NS;
                break;

            /* Values that should only occur in nftw mode */

            case FTS_SLNONE:
                if (MODE_NFTW == mode) {
                    ftw_flag = FTW_SLN;
                } else {
                    ftw_flag = FTW_SL;
                }
                break;

            case FTS_DP:
                if ((MODE_NFTW == mode) && (flags & FTW_DEPTH)) {
                    ftw_flag = FTW_D;
                } else {
                    skip_entry = 1;
                }
                break;

            default:
                /* I'm not sure this is right, but we don't have a valid FTW
                 * type to call with, so cowardice seems the better part of
                 * guessing.
                 */

                skip_entry = 1;
        }

        if (MODE_FTW == mode) {
            ftw_func = (__ftw_func_t) func;
            func_ret = (*ftw_func) 
                (entry->fts_path, entry->fts_statp, ftw_flag);
        } else if (MODE_NFTW == mode) {
            ftw_st.base = (entry->fts_pathlen - entry->fts_namelen);
            ftw_st.level = entry->fts_level;

            nftw_func = (__nftw_func_t) func;
            func_ret = (*nftw_func)
                (entry->fts_path, entry->fts_statp, ftw_flag, &ftw_st);
        }

        if (0 != func_ret) {
	    saved_errno = errno;
	    fts_close(hierarchy);
	    errno = saved_errno;
            return func_ret;
        }
    }

    /* The janitors will be upset if we don't clean up after ourselves. */

    saved_errno = errno;
    fts_close(hierarchy);
    if (0 != saved_errno) { /* fts_read returned NULL, and set errno - bail */
	errno = saved_errno;
    }

    return errno ? -1 : 0;
}
OpenPOWER on IntegriCloud