8f2b8b9320f05e1b1cfda1287aef5bbeaf1171de
[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 source tree.
8  */
9
10 #include "libbb.h"
11
12 #undef DEBUG_RECURS_ACTION
13
14 /*
15  * Walk down all the directories under the specified
16  * location, and do something (something specified
17  * by the fileAction and dirAction function pointers).
18  *
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.
23  */
24
25 static int FAST_FUNC true_action(const char *fileName UNUSED_PARAM,
26                 struct stat *statbuf UNUSED_PARAM,
27                 void* userData UNUSED_PARAM,
28                 int depth UNUSED_PARAM)
29 {
30         return TRUE;
31 }
32
33 /* fileName is (l)stat'ed (depending on ACTION_FOLLOWLINKS[_L0]).
34  *
35  * If it is a file: fileAction in run on it, its return value is returned.
36  *
37  * In case we are in a recursive invocation (see below):
38  * normally, fileAction should return 1 (TRUE) to indicate that
39  * everything is okay and processing should continue.
40  * fileAction return value of 0 (FALSE) on any file in directory will make
41  * recursive_action() also return 0, but it doesn't stop directory traversal
42  * (fileAction/dirAction will be called on each file).
43  *
44  * [TODO: maybe introduce -1 to mean "stop traversal NOW and return"]
45  *
46  * If it is a directory:
47  *
48  * If !ACTION_RECURSE, dirAction is called and its
49  * return value is returned from recursive_action(). No recursion.
50  *
51  * If ACTION_RECURSE, directory is opened, and recursive_action() is called
52  * on each file/subdirectory.
53  * If any one of these calls returns 0, current recursive_action() returns 0.
54  *
55  * If !ACTION_DEPTHFIRST, dirAction is called before recurse.
56  * Return value of 0 (FALSE) is an error: prevents recursion,
57  * the warning is printed (unless ACTION_QUIET) and recursive_action() returns 0.
58  * Return value of 2 (SKIP) prevents recursion, instead recursive_action()
59  * returns 1 (TRUE, no error).
60  *
61  * If ACTION_DEPTHFIRST, dirAction is called after recurse.
62  * If it returns 0, the warning is printed and recursive_action() returns 0.
63  *
64  * ACTION_FOLLOWLINKS mainly controls handling of links to dirs.
65  * 0: lstat(statbuf). Calls fileAction on link name even if points to dir.
66  * 1: stat(statbuf). Calls dirAction and optionally recurse on link to dir.
67  */
68
69 int FAST_FUNC recursive_action(const char *fileName,
70                 unsigned flags,
71                 int FAST_FUNC (*fileAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
72                 int FAST_FUNC (*dirAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
73                 void* userData,
74                 unsigned depth)
75 {
76         struct stat statbuf;
77         unsigned follow;
78         int status;
79         DIR *dir;
80         struct dirent *next;
81
82         if (!fileAction) fileAction = true_action;
83         if (!dirAction) dirAction = true_action;
84
85         follow = ACTION_FOLLOWLINKS;
86         if (depth == 0)
87                 follow = ACTION_FOLLOWLINKS | ACTION_FOLLOWLINKS_L0;
88         follow &= flags;
89         status = (follow ? stat : lstat)(fileName, &statbuf);
90         if (status < 0) {
91 #ifdef DEBUG_RECURS_ACTION
92                 bb_error_msg("status=%d flags=%x", status, flags);
93 #endif
94                 if ((flags & ACTION_DANGLING_OK)
95                  && errno == ENOENT
96                  && lstat(fileName, &statbuf) == 0
97                 ) {
98                         /* Dangling link */
99                         return fileAction(fileName, &statbuf, userData, depth);
100                 }
101                 goto done_nak_warn;
102         }
103
104         /* If S_ISLNK(m), then we know that !S_ISDIR(m).
105          * Then we can skip checking first part: if it is true, then
106          * (!dir) is also true! */
107         if ( /* (!(flags & ACTION_FOLLOWLINKS) && S_ISLNK(statbuf.st_mode)) || */
108          !S_ISDIR(statbuf.st_mode)
109         ) {
110                 return fileAction(fileName, &statbuf, userData, depth);
111         }
112
113         /* It's a directory (or a link to one, and followLinks is set) */
114
115         if (!(flags & ACTION_RECURSE)) {
116                 return dirAction(fileName, &statbuf, userData, depth);
117         }
118
119         if (!(flags & ACTION_DEPTHFIRST)) {
120                 status = dirAction(fileName, &statbuf, userData, depth);
121                 if (status == FALSE)
122                         goto done_nak_warn;
123                 if (status == SKIP)
124                         return TRUE;
125         }
126
127         dir = opendir(fileName);
128         if (!dir) {
129                 /* findutils-4.1.20 reports this */
130                 /* (i.e. it doesn't silently return with exit code 1) */
131                 /* To trigger: "find -exec rm -rf {} \;" */
132                 goto done_nak_warn;
133         }
134         status = TRUE;
135         while ((next = readdir(dir)) != NULL) {
136                 char *nextFile;
137                 int s;
138
139                 nextFile = concat_subpath_file(fileName, next->d_name);
140                 if (nextFile == NULL)
141                         continue;
142
143                 /* process every file (NB: ACTION_RECURSE is set in flags) */
144                 s = recursive_action(nextFile, flags, fileAction, dirAction,
145                                                 userData, depth + 1);
146                 if (s == FALSE)
147                         status = FALSE;
148                 free(nextFile);
149 //#define RECURSE_RESULT_ABORT -1
150 //              if (s == RECURSE_RESULT_ABORT) {
151 //                      closedir(dir);
152 //                      return s;
153 //              }
154         }
155         closedir(dir);
156
157         if (flags & ACTION_DEPTHFIRST) {
158                 if (!dirAction(fileName, &statbuf, userData, depth))
159                         goto done_nak_warn;
160         }
161
162         return status;
163
164  done_nak_warn:
165         if (!(flags & ACTION_QUIET))
166                 bb_simple_perror_msg(fileName);
167         return FALSE;
168 }