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,
28 struct stat * statbuf,
30 int (*dirAction) (const char *fileName,
31 struct stat * statbuf,
40 status = stat(fileName, &statbuf);
42 status = lstat(fileName, &statbuf);
45 #ifdef DEBUG_RECURS_ACTION
46 bb_error_msg("status=%d followLinks=%d TRUE=%d",
47 status, followLinks, TRUE);
49 bb_perror_msg("%s", fileName);
53 if (! followLinks && (S_ISLNK(statbuf.st_mode))) {
54 if (fileAction == NULL)
57 return fileAction(fileName, &statbuf, userData);
61 if (S_ISDIR(statbuf.st_mode)) {
62 if (dirAction != NULL)
63 return (dirAction(fileName, &statbuf, userData));
69 if (S_ISDIR(statbuf.st_mode)) {
72 if (dirAction != NULL && ! depthFirst) {
73 status = dirAction(fileName, &statbuf, userData);
75 bb_perror_msg("%s", fileName);
77 } else if (status == SKIP)
80 dir = opendir(fileName);
85 while ((next = readdir(dir)) != NULL) {
88 nextFile = concat_subpath_file(fileName, next->d_name);
91 if (! recursive_action(nextFile, TRUE, followLinks, depthFirst,
92 fileAction, dirAction, userData)) {
98 if (dirAction != NULL && depthFirst) {
99 if (! dirAction(fileName, &statbuf, userData)) {
100 bb_perror_msg("%s", fileName);
107 if (fileAction == NULL)
110 return fileAction(fileName, &statbuf, userData);