1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
12 #undef DEBUG_RECURS_ACTION
15 * Walk down all the directories under the specified
16 * location, and do something (something specified
17 * by the fileAction and dirAction function pointers).
19 * Unfortunately, while nftw(3) could replace this and reduce
20 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
21 * and so isn't sufficiently portable to take over since glibc2.1
22 * is so stinking huge.
25 static int true_action(const char *fileName, struct stat *statbuf, void* userData, int depth)
31 * followLinks=0/1 differs mainly in handling of links to dirs.
32 * 0: lstat(statbuf). Calls fileAction on link name even if points to dir.
33 * 1: stat(statbuf). Calls dirAction and optionally recurse on link to dir.
36 int recursive_action(const char *fileName,
37 int recurse, int followLinks, int depthFirst,
38 int (*fileAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
39 int (*dirAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
48 if (!fileAction) fileAction = true_action;
49 if (!dirAction) dirAction = true_action;
51 status = (followLinks ? stat : lstat)(fileName, &statbuf);
54 #ifdef DEBUG_RECURS_ACTION
55 bb_error_msg("status=%d followLinks=%d TRUE=%d",
56 status, followLinks, TRUE);
58 bb_perror_msg("%s", fileName);
62 /* If S_ISLNK(m), then we know that !S_ISDIR(m).
63 * Then we can skip checking first part: if it is true, then
64 * (!dir) is also true! */
65 if ( /* (!followLinks && S_ISLNK(statbuf.st_mode)) || */
66 !S_ISDIR(statbuf.st_mode)
68 return fileAction(fileName, &statbuf, userData, depth);
71 /* It's a directory (or a link to one, and followLinks is set) */
74 return dirAction(fileName, &statbuf, userData, depth);
78 status = dirAction(fileName, &statbuf, userData, depth);
80 bb_perror_msg("%s", fileName);
87 dir = opendir(fileName);
92 while ((next = readdir(dir)) != NULL) {
95 nextFile = concat_subpath_file(fileName, next->d_name);
98 if (!recursive_action(nextFile, TRUE, followLinks, depthFirst,
99 fileAction, dirAction, userData, depth+1)) {
106 if (!dirAction(fileName, &statbuf, userData, depth)) {
107 bb_perror_msg("%s", fileName);