summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/insmod-modutils/util/xftw.c
blob: fe764a63c019e2d1716ffb92b2ca093f99b6a10e (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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
 * modutils specific implementation of ftw().
 *
 * Copyright 2000:
 *  Keith Owens <kaos@ocs.com.au> August 2000
 *
 * This file is part of the Linux modutils.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/*
    modutils requires special processing during the file tree walk
    of /lib/modules/<version> and any paths that the user specifies.
    The standard ftw() does a blind walk of all paths and can end
    up following the build symlink down the kernel source tree.
    Although nftw() has the option to get more control such as not
    automatically following symbolic links, even that is not enough
    for modutils.  The requirements are:

    Paths must be directories or symlinks to directories.

    Each directory is read and sorted into alphabetical order
    before processing.

    A directory is type 1 iff it was specified on a path statement
    (either explicit or default) and the directory contains a
    subdirectory with one of the known names and the directory name
    does not end with "/kernel".  Otherwise it is type 2.

    In a type 1 directory, walk the kernel subdirectory if it exists,
    then the old known names in their historical order then any
    remaining directory entries in alphabetical order and finally any
    non-directory entries in alphabetical order.

    Entries in a type 1 directory are filtered against the "prune"
    list.  A type 1 directory can contain additional files which
    are not modules nor symlinks to modules.  The prune list skips
    known additional files, if a distribution wants to store
    additional text files in the top level directory they should be
    added to the prune list.

    A type 2 directory must contain only modules or symlinks to
    modules.  They are processed in alphabetical order, without
    pruning.  Symlinks to directories are an error in type 2
    directories.

    The user function is not called for type 1 directories, nor for
    pruned entries.  It is called for type 2 directories and their
    contents.  It is also called for any files left in a type 1
    directory after pruning and processing type 2 subdirectories.
    The user function never sees symlinks, they are resolved before
    calling the function.

    Why have different directory types?  The original file tree
    walk was not well defined.  Some users specified each directory
    individually, others just pointed at the top level directory.
    Either version worked until the "build" symlink was added.  Now
    users who specify the top level directory end up running the
    entire kernel source tree looking for modules, not nice.  We
    cannot just ignore symlinks because pcmcia uses symlinks to
    modules for backwards compatibility.

    Type 1 is when a user specifies the top level directory which needs
    special processing, type 2 is individual subdirectories.  But the
    only way to tell the difference is by looking at the contents.  The
    "/kernel" directory introduced in 2.3.12 either contains nothing
    (old make modules_install) or contains all the kernel modules using
    the same tree structure as the source.  Because "/kernel" can
    contain old names but is really a type 2 directory, it is detected
    as a special case.
 */

#include <dirent.h>
#include <errno.h>
#include <limits.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

#include "util.h"
#include "config.h"

extern char *tbpath[];

extern OPT_LIST *prune_list;
extern int n_prune_list;

extern char *tbtype[];

struct xftw_dirent {
    struct stat statbuf;
    char *name;
    char *fullname;
};

#define XFTW_MAXDEPTH 64    /* Maximum directory depth handled */

typedef struct {
    struct xftw_dirent *contents;
    int size;
    int used;
} xftw_tree_t;

static xftw_tree_t tree[XFTW_MAXDEPTH];

/* Free all data for one tree level */
static void xftw_free_tree(int depth)
{
    int i;
    xftw_tree_t *t = tree+depth;
    for (i = 0; i < t->size; ++i) {
	free(t->contents[i].name);
	free(t->contents[i].fullname);
    }
    free(t->contents);
    t->contents = NULL;
    t->size = 0;
    t->used = 0;
}

/* Increment dirents used at this depth, resizing if necessary */
static void xftw_add_dirent(int depth)
{
    xftw_tree_t *t = tree+depth;
    int i, size = t->size;
    if (++t->used < size)
	return;
    size += 10; /* arbitrary increment */
    t->contents = xrealloc(t->contents, size*sizeof(*(t->contents)));
    for (i = t->size; i < size; ++i) {
	memset(&(t->contents[i].statbuf), 0, sizeof(t->contents[i].statbuf));
	t->contents[i].name = NULL;
	t->contents[i].fullname = NULL;
    }
    t->size = size;
}

/* Concatenate directory name and entry name into one string.
 * Note: caller must free result or leak.
 */
static char *xftw_dir_name(const char *directory, const char *entry)
{
    int i = strlen(directory);
    char *name;
    if (entry)
	i += strlen(entry);
    i += 2;
    name = xmalloc(i);
    strcpy(name, directory);	/* safe, xmalloc */
    if (*directory && entry)
	strcat(name, "/");	/* safe, xmalloc */
    if (entry)
	strcat(name, entry);	/* safe, xmalloc */
    return(name);
}

/* Call the user function for a directory entry */
static int xftw_do_name(const char *directory, const char *entry, struct stat *sb, xftw_func_t funcptr)
{
    int ret = 0;
    char *name = xftw_dir_name(directory, entry);

    if (S_ISLNK(sb->st_mode)) {
	char real[PATH_MAX], *newname;
	verbose("resolving %s symlink to ", name);
	if (!(newname = realpath(name, real))) {
	    if (errno == ENOENT) {
		verbose("%s: does not exist, dangling symlink ignored\n", real);
		goto cleanup;
	    }
	    perror("... failed");
	    goto cleanup;
	}
	verbose("%s ", newname);
	if (lstat(newname, sb)) {
	    error("lstat on %s failed ", newname);
	    perror("");
	    goto cleanup;
	}
	free(name);
	name = xstrdup(newname);
    }

    if (!S_ISREG(sb->st_mode) &&
	!S_ISDIR(sb->st_mode)) {
	error("%s is not plain file nor directory\n", name);
	goto cleanup;
    }
	
    verbose("user function %s\n", name);
    ret = (*funcptr)(name, sb);
cleanup:
    free(name);
    return(ret);
}

/* Sort directory entries into alphabetical order */
static int xftw_sortdir(const void *a, const void *b)
{
    return(strcmp(((struct xftw_dirent *)a)->name, ((struct xftw_dirent *)b)->name));
}

/* Read a directory and sort it, ignoring "." and ".." */
static int xftw_readdir(const char *directory, int depth)
{
    DIR *d;
    struct dirent *ent;
    verbose("xftw_readdir %s\n", directory);
    if (!(d = opendir(directory))) {
	perror(directory);
	return(1);
    }
    while ((ent = readdir(d))) {
	char *name;
	struct xftw_dirent *f;
	if (strcmp(ent->d_name, ".") == 0 ||
	    strcmp(ent->d_name, "..") == 0)
	    continue;
	name = xftw_dir_name(directory, ent->d_name);
	xftw_add_dirent(depth); 
	f = tree[depth].contents+tree[depth].used-1;
	f->name = xstrdup(ent->d_name);
	f->fullname = name;     /* do not free name, it is in use */
	if (lstat(name, &(f->statbuf))) {
	    perror(name);
	    return(1);
	}
    }
    closedir(d);
    qsort(tree[depth].contents, tree[depth].used, sizeof(*(tree[0].contents)), &xftw_sortdir);
    return(0);
}

/* Process a type 2 directory */
int xftw_type2(const char *directory, const char *entry, int depth, xftw_func_t funcptr)
{
    int ret, i;
    xftw_tree_t *t = tree+depth;
    struct stat statbuf;
    char *dirname = xftw_dir_name(directory, entry);

    verbose("type 2 %s\n", dirname);
    if (depth > XFTW_MAXDEPTH) {
	error("xftw_type2 exceeded maxdepth\n");
	ret = 1;
	goto cleanup;
    }
    if ((ret = xftw_readdir(dirname, depth)))
	goto cleanup;

    t = tree+depth;
    /* user function sees type 2 directories */
    if ((ret = lstat(dirname, &statbuf)) ||
	(ret = xftw_do_name("", dirname, &statbuf, funcptr)))
	goto cleanup;

    /* user sees all contents of type 2 directory, no pruning */
    for (i = 0; i < t->used; ++i) {
	struct xftw_dirent *c = t->contents+i;
	if (S_ISLNK(c->statbuf.st_mode)) {
	    if (!stat(c->name, &(c->statbuf))) {
		if (S_ISDIR(c->statbuf.st_mode)) {
		    error("symlink to directory is not allowed, %s ignored\n", c->name);
		    *(c->name) = '\0';  /* ignore it */
		}
	    }
	}
	if (!*(c->name))
	    continue;
	if (S_ISDIR(c->statbuf.st_mode)) {
	    /* recursion is the curse of the programming classes */
	    ret = xftw_type2(dirname, c->name, depth+1, funcptr);
	    if (ret)
		goto cleanup;
	}
	else if ((ret = xftw_do_name(dirname, c->name, &(c->statbuf), funcptr)))
	    goto cleanup;
	*(c->name) = '\0';  /* processed */
    }

    ret = 0;
cleanup:
    free(dirname);
    return(ret);
}

/* Only external visible function.  Decide on the type of directory and
 * process accordingly.
 */
int xftw(const char *directory, xftw_func_t funcptr)
{
    struct stat statbuf;
    int ret, i, j, type;
    xftw_tree_t *t;
    struct xftw_dirent *c;

    verbose("xftw starting at %s ", directory);
    if (lstat(directory, &statbuf)) {
	verbose("lstat on %s failed\n", directory);
	return(0);
    }
    if (S_ISLNK(statbuf.st_mode)) {
	char real[PATH_MAX];
	verbose("resolving symlink to ");
	if (!(directory = realpath(directory, real))) {
	    if (errno == ENOENT) {
		verbose("%s: does not exist, dangling symlink ignored\n", real);
		return(0);
	    }
	    perror("... failed");
	    return(-1);
	}
	verbose("%s ", directory);
	if (lstat(directory, &statbuf)) {
	    error("lstat on %s failed ", directory);
	    perror("");
	    return(-1);
	}
    }
    if (!S_ISDIR(statbuf.st_mode)) {
	error("%s is not a directory\n", directory);
	return(-1);
    }
    verbose("\n");

    /* All returns after this point must be via cleanup */

    if ((ret = xftw_readdir(directory, 0)))
	goto cleanup;

    t = tree;   /* depth 0 */
    type = 2;
    for (i = 0 ; type == 2 && i < t->used; ++i) {
	c = t->contents+i;
	for (j = 0; tbtype[j]; ++j) {
	    if (strcmp(c->name, tbtype[j]) == 0 &&
		S_ISDIR(c->statbuf.st_mode)) {
		const char *p = directory + strlen(directory) - 1;
		if (*p == '/')
		    --p;
		if (p - directory >= 6 && strncmp(p-6, "/kernel", 7) == 0)
		    continue;	/* "/kernel" path is a special case, type 2 */
		type = 1;   /* known subdirectory */
		break;
	    }
	}
    }

    if (type == 1) {
	OPT_LIST *p;
	/* prune entries in type 1 directories only */
	for (i = 0 ; i < t->used; ++i) {
	    for (p = prunelist; p->name; ++p) {
		c = t->contents+i;
		if (strcmp(p->name, c->name) == 0) {
		    verbose("pruned %s\n", c->name);
		    *(c->name) = '\0';  /* ignore */
		}
	    }
	}
	/* run known subdirectories first in historical order, "kernel" is now top of list */
	for (i = 0 ; i < t->used; ++i) {
	    c = t->contents+i;
	    for (j = 0; tbtype[j]; ++j) {
		if (*(c->name) &&
		    strcmp(c->name, tbtype[j]) == 0 &&
		    S_ISDIR(c->statbuf.st_mode)) {
		    if ((ret = xftw_type2(directory, c->name, 1, funcptr)))
			goto cleanup;
		    *(c->name) = '\0';  /* processed */
		}
	    }
	}
	/* any other directories left, in alphabetical order */
	for (i = 0 ; i < t->used; ++i) {
	    c = t->contents+i;
	    if (*(c->name) &&
	        S_ISDIR(c->statbuf.st_mode)) {
		if ((ret = xftw_type2(directory, c->name, 1, funcptr)))
		    goto cleanup;
		*(c->name) = '\0';  /* processed */
	    }
	}
	/* anything else is passed to the user function */
	for (i = 0 ; i < t->used; ++i) {
	    c = t->contents+i;
	    if (*(c->name)) {
		verbose("%s found in type 1 directory %s\n", c->name, directory);
		if ((ret = xftw_do_name(directory, c->name, &(c->statbuf), funcptr)))
		    goto cleanup;
		*(c->name) = '\0';  /* processed */
	    }
	}
    }
    else {
	/* type 2 */
	xftw_free_tree(0);
	if ((ret = xftw_type2(directory, NULL, 0, funcptr)))
	    goto cleanup;
    }

    /* amazing, it all worked */
    ret = 0;
cleanup:
    for (i = 0; i < XFTW_MAXDEPTH; ++i)
	xftw_free_tree(i);
    return(ret);
}