aboutsummaryrefslogtreecommitdiff
path: root/cftw.c
blob: 5083cc4a20c75639c5fedd489a8fbc8992a30227 (plain) (blame)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#define _POSIX_C_SOURCE 200809L

#include "cftw.h"
#include <dynstr.h>
#include <dirent.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

struct cftw_entry
{
    DIR *d;
    char *dirpath;
    struct cftw_entry *child;
};

struct cftw
{
    bool done;
    char *dirpath;
    int (*fn)(const char *, const struct stat *, bool *, void *);
    void *user;
    struct cftw_entry *root;
};

static int free_entry(struct cftw_entry *const e)
{
    int ret = 0;

    if (e->d && closedir(e->d))
    {
        fprintf(stderr, "%s: closedir(2) %s: %s\n", __func__, e->dirpath,
            strerror(errno));
        ret = -1;
    }

    free(e->dirpath);
    free(e);
    return ret;
}

void cftw_free(struct cftw *const c)
{
    if (!c)
        return;

    for (struct cftw_entry *e = c->root; e;)
    {
        struct cftw_entry *const child = e->child;

        free_entry(e);
        e = child;
    }

    free(c->dirpath);
    free(c);
}

static struct cftw_entry *entry(const char *const dirpath)
{
    struct cftw_entry *ret = NULL;
    char *dirdup = NULL;
    DIR *const d = opendir(dirpath);

    if (!d)
    {
        fprintf(stderr, "%s: opendir(2) %s: %s\n", __func__, dirpath,
            strerror(errno));
        goto failure;
    }
    else if (!(dirdup = strdup(dirpath)))
    {
        fprintf(stderr, "%s: strdup(3): %s\n", __func__, strerror(errno));
        goto failure;
    }
    else if (!(ret = malloc(sizeof *ret)))
    {
        fprintf(stderr, "%s: malloc(3): %s\n", __func__, strerror(errno));
        goto failure;
    }

    *ret = (const struct cftw_entry)
    {
        .dirpath = dirdup,
        .d = d
    };

    return ret;

failure:

    if (d && closedir(d))
        fprintf(stderr, "%s: closedir(2) %s: %s\n", __func__, dirpath,
            strerror(errno));

    free(dirdup);
    free(ret);
    return NULL;
}

static enum cftw_state run(struct cftw *const c, struct cftw_entry *const e,
    struct cftw_entry *const root)
{
    int error = 0;
    struct dirent *de;
    struct dynstr d;
    const char *const dirpath = e->dirpath;
    struct stat sb;

    dynstr_init(&d);
    errno = 0;
    de = readdir(e->d);

    if (errno)
    {
        fprintf(stderr, "%s: readdir(3): %s\n", __func__, strerror(errno));
        goto failure;
    }
    else if (!de)
    {
        if (e == root)
            return CFTW_OK;
        else if (stat(dirpath, &sb))
        {
            fprintf(stderr, "%s: stat(2) %s: %s\n", __func__, dirpath,
                strerror(errno));
            goto failure;
        }

        return c->fn(dirpath, &sb, &c->done, c->user) ? CFTW_FATAL : CFTW_OK;
    }

    const char *const path = de->d_name;

    if (!strcmp(path, ".") || !strcmp(path, ".."))
        return CFTW_AGAIN;

    const char *const sep = dirpath[strlen(dirpath) - 1] == '/' ? "" : "/";

    if (dynstr_append(&d, "%s%s%s", dirpath, sep, path))
    {
        fprintf(stderr, "%s: dynstr_append failed\n", __func__);
        goto failure;
    }

    const int r = stat(d.str, &sb);

    if (r)
    {
        fprintf(stderr, "%s: stat(2) %s: %s\n", __func__, path,
            strerror(errno));
        goto failure;
    }
    else if (S_ISDIR(sb.st_mode))
    {
        if (!(e->child = entry(d.str)))
            goto failure;
    }
    else if (S_ISREG(sb.st_mode))
        error = c->fn(d.str, &sb, &c->done, c->user);
    else
    {
        fprintf(stderr, "%s: unexpected st_mode %ju\n",
            __func__, (uintmax_t)sb.st_mode);
        goto failure;
    }

    dynstr_free(&d);

    if (error)
        return CFTW_FATAL;
    else if (c->done)
        return CFTW_OK;

    return CFTW_AGAIN;

failure:
    dynstr_free(&d);
    return CFTW_FATAL;
}

static enum cftw_state step(struct cftw *const c, struct cftw_entry *const e,
    struct cftw_entry *const root)
{
    if (e->child)
    {
        switch (step(c, e->child, root))
        {
            case CFTW_AGAIN:
                return CFTW_AGAIN;

            case CFTW_FATAL:
               free_entry(e->child);
               return CFTW_FATAL;

            case CFTW_OK:
                if (free_entry(e->child))
                    return CFTW_FATAL;

                e->child = NULL;
                break;
        }

        return CFTW_AGAIN;
    }

    return run(c, e, root);
}

enum cftw_state cftw_step(struct cftw *const c)
{
    return step(c, c->root, c->root);
}

struct cftw *cftw(const char *const dirpath, int (*const fn)(const char *,
    const struct stat *, bool *, void *), void *const user)
{
    struct cftw *ret = NULL;
    char *const dirdup = strdup(dirpath);

    if (!dirdup)
    {
        fprintf(stderr, "%s: strdup(3): %s\n", __func__, strerror(errno));
        goto failure;
    }
    else if (!(ret = malloc(sizeof *ret)))
    {
        fprintf(stderr, "%s: malloc(3): %s\n", __func__, strerror(errno));
        goto failure;
    }

    *ret = (const struct cftw)
    {
        .dirpath = dirdup,
        .fn = fn,
        .user = user,
        .root = entry(dirpath)
    };

    if (!ret->root)
        goto failure;

    return ret;

failure:
    free(dirdup);
    free(ret);
    return NULL;
}