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
16 * Walk down all the directories under the specified
17 * location, and do something (something specified
18 * by the fileAction and dirAction function pointers).
20 * Unfortunately, while nftw(3) could replace this and reduce
21 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
22 * and so isn't sufficiently portable to take over since glibc2.1
23 * is so stinking huge.
25 int recursive_action(const char *fileName,
26 int recurse, int followLinks, int depthFirst,
27 int (*fileAction) (const char *fileName, struct stat * statbuf, void* userData),
28 int (*dirAction) (const char *fileName, struct stat * statbuf, void* userData),
36 status = stat(fileName, &statbuf);
38 status = lstat(fileName, &statbuf);
41 #ifdef DEBUG_RECURS_ACTION
42 bb_error_msg("status=%d followLinks=%d TRUE=%d",
43 status, followLinks, TRUE);
45 bb_perror_msg("%s", fileName);
49 if (!followLinks && (S_ISLNK(statbuf.st_mode))) {
50 if (fileAction == NULL)
53 return fileAction(fileName, &statbuf, userData);
57 if (S_ISDIR(statbuf.st_mode)) {
58 if (dirAction != NULL)
59 return (dirAction(fileName, &statbuf, userData));
65 if (S_ISDIR(statbuf.st_mode)) {
68 if (dirAction != NULL && !depthFirst) {
69 status = dirAction(fileName, &statbuf, userData);
71 bb_perror_msg("%s", fileName);
73 } else if (status == SKIP)
76 dir = opendir(fileName);
81 while ((next = readdir(dir)) != NULL) {
84 nextFile = concat_subpath_file(fileName, next->d_name);
87 if (!recursive_action(nextFile, TRUE, followLinks, depthFirst,
88 fileAction, dirAction, userData)) {
94 if (dirAction != NULL && depthFirst) {
95 if (!dirAction(fileName, &statbuf, userData)) {
96 bb_perror_msg("%s", fileName);
103 if (fileAction == NULL)
106 return fileAction(fileName, &statbuf, userData);