d491b781b073f69e942965590e26c5c02c524e4c
[oweals/busybox.git] / libbb / recursive_action.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Utility routines.
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 #include "libbb.h"
11
12 #undef DEBUG_RECURS_ACTION
13
14
15 /*
16  * Walk down all the directories under the specified
17  * location, and do something (something specified
18  * by the fileAction and dirAction function pointers).
19  *
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.
24  */
25 int recursive_action(const char *fileName,
26                                         int recurse, int followLinks, int depthFirst,
27                                         int (*fileAction) (const char *fileName,
28                                                                            struct stat * statbuf,
29                                                                            void* userData),
30                                         int (*dirAction) (const char *fileName,
31                                                                           struct stat * statbuf,
32                                                                           void* userData),
33                                         void* userData)
34 {
35         int status;
36         struct stat statbuf;
37         struct dirent *next;
38
39         if (followLinks)
40                 status = stat(fileName, &statbuf);
41         else
42                 status = lstat(fileName, &statbuf);
43
44         if (status < 0) {
45 #ifdef DEBUG_RECURS_ACTION
46                 bb_error_msg("status=%d followLinks=%d TRUE=%d",
47                                 status, followLinks, TRUE);
48 #endif
49                 bb_perror_msg("%s", fileName);
50                 return FALSE;
51         }
52
53         if (! followLinks && (S_ISLNK(statbuf.st_mode))) {
54                 if (fileAction == NULL)
55                         return TRUE;
56                 else
57                         return fileAction(fileName, &statbuf, userData);
58         }
59
60         if (! recurse) {
61                 if (S_ISDIR(statbuf.st_mode)) {
62                         if (dirAction != NULL)
63                                 return (dirAction(fileName, &statbuf, userData));
64                         else
65                                 return TRUE;
66                 }
67         }
68
69         if (S_ISDIR(statbuf.st_mode)) {
70                 DIR *dir;
71
72                 if (dirAction != NULL && ! depthFirst) {
73                         status = dirAction(fileName, &statbuf, userData);
74                         if (! status) {
75                                 bb_perror_msg("%s", fileName);
76                                 return FALSE;
77                         } else if (status == SKIP)
78                                 return TRUE;
79                 }
80                 dir = opendir(fileName);
81                 if (!dir) {
82                         return FALSE;
83                 }
84                 status = TRUE;
85                 while ((next = readdir(dir)) != NULL) {
86                         char *nextFile;
87
88                         nextFile = concat_subpath_file(fileName, next->d_name);
89                         if(nextFile == NULL)
90                                 continue;
91                         if (! recursive_action(nextFile, TRUE, followLinks, depthFirst,
92                                                 fileAction, dirAction, userData)) {
93                                 status = FALSE;
94                         }
95                         free(nextFile);
96                 }
97                 closedir(dir);
98                 if (dirAction != NULL && depthFirst) {
99                         if (! dirAction(fileName, &statbuf, userData)) {
100                                 bb_perror_msg("%s", fileName);
101                                 return FALSE;
102                         }
103                 }
104                 if (! status)
105                         return FALSE;
106         } else {
107                 if (fileAction == NULL)
108                         return TRUE;
109                 else
110                         return fileAction(fileName, &statbuf, userData);
111         }
112         return TRUE;
113 }