blob: b1c4bfad7ccff508e1322bd79c3af9e84e3bcec9 [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenaad1a882001-03-16 22:47:14 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenaad1a882001-03-16 22:47:14 +00008 */
Eric Andersenaad1a882001-03-16 22:47:14 +00009#include "libbb.h"
10
Eric Andersenaad1a882001-03-16 22:47:14 +000011#undef DEBUG_RECURS_ACTION
12
Eric Andersenaad1a882001-03-16 22:47:14 +000013/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +000014 * Walk down all the directories under the specified
Eric Andersenaad1a882001-03-16 22:47:14 +000015 * location, and do something (something specified
16 * by the fileAction and dirAction function pointers).
17 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +000018 * Unfortunately, while nftw(3) could replace this and reduce
19 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersenaad1a882001-03-16 22:47:14 +000020 * and so isn't sufficiently portable to take over since glibc2.1
21 * is so stinking huge.
22 */
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +000023
Denys Vlasenko689d0652020-10-01 21:52:16 +020024static int FAST_FUNC true_action(struct recursive_state *state UNUSED_PARAM,
25 const char *fileName UNUSED_PARAM,
26 struct stat *statbuf UNUSED_PARAM)
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +000027{
28 return TRUE;
29}
30
Denys Vlasenko4f0b5402017-04-06 15:22:24 +020031/* fileName is (l)stat'ed (depending on ACTION_FOLLOWLINKS[_L0]).
32 *
33 * If it is a file: fileAction in run on it, its return value is returned.
34 *
35 * In case we are in a recursive invocation (see below):
36 * normally, fileAction should return 1 (TRUE) to indicate that
37 * everything is okay and processing should continue.
38 * fileAction return value of 0 (FALSE) on any file in directory will make
39 * recursive_action() also return 0, but it doesn't stop directory traversal
Denis Vlasenko5d499e12006-10-29 19:07:01 +000040 * (fileAction/dirAction will be called on each file).
41 *
Denys Vlasenko4f0b5402017-04-06 15:22:24 +020042 * [TODO: maybe introduce -1 to mean "stop traversal NOW and return"]
43 *
44 * If it is a directory:
45 *
46 * If !ACTION_RECURSE, dirAction is called and its
Denis Vlasenko2649f212008-06-26 03:26:57 +000047 * return value is returned from recursive_action(). No recursion.
48 *
Denys Vlasenko4f0b5402017-04-06 15:22:24 +020049 * If ACTION_RECURSE, directory is opened, and recursive_action() is called
50 * on each file/subdirectory.
Denis Vlasenko2649f212008-06-26 03:26:57 +000051 * If any one of these calls returns 0, current recursive_action() returns 0.
52 *
Denys Vlasenko4f0b5402017-04-06 15:22:24 +020053 * If !ACTION_DEPTHFIRST, dirAction is called before recurse.
54 * Return value of 0 (FALSE) is an error: prevents recursion,
55 * the warning is printed (unless ACTION_QUIET) and recursive_action() returns 0.
56 * Return value of 2 (SKIP) prevents recursion, instead recursive_action()
57 * returns 1 (TRUE, no error).
58 *
Denis Vlasenko2649f212008-06-26 03:26:57 +000059 * If ACTION_DEPTHFIRST, dirAction is called after recurse.
60 * If it returns 0, the warning is printed and recursive_action() returns 0.
61 *
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +020062 * ACTION_FOLLOWLINKS mainly controls handling of links to dirs.
Denis Vlasenko8c35d652006-10-27 23:42:25 +000063 * 0: lstat(statbuf). Calls fileAction on link name even if points to dir.
64 * 1: stat(statbuf). Calls dirAction and optionally recurse on link to dir.
65 */
66
Denys Vlasenko689d0652020-10-01 21:52:16 +020067static int recursive_action1(recursive_state_t *state, const char *fileName)
Eric Andersenaad1a882001-03-16 22:47:14 +000068{
Eric Andersenaad1a882001-03-16 22:47:14 +000069 struct stat statbuf;
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +020070 unsigned follow;
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +000071 int status;
72 DIR *dir;
Eric Andersenaad1a882001-03-16 22:47:14 +000073 struct dirent *next;
74
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +020075 follow = ACTION_FOLLOWLINKS;
Denys Vlasenko689d0652020-10-01 21:52:16 +020076 if (state->depth == 0)
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +020077 follow = ACTION_FOLLOWLINKS | ACTION_FOLLOWLINKS_L0;
Denys Vlasenko689d0652020-10-01 21:52:16 +020078 follow &= state->flags;
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +020079 status = (follow ? stat : lstat)(fileName, &statbuf);
Eric Andersenaad1a882001-03-16 22:47:14 +000080 if (status < 0) {
81#ifdef DEBUG_RECURS_ACTION
Denys Vlasenko689d0652020-10-01 21:52:16 +020082 bb_error_msg("status=%d flags=%x", status, state->flags);
Eric Andersenaad1a882001-03-16 22:47:14 +000083#endif
Denys Vlasenko689d0652020-10-01 21:52:16 +020084 if ((state->flags & ACTION_DANGLING_OK)
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +020085 && errno == ENOENT
86 && lstat(fileName, &statbuf) == 0
87 ) {
88 /* Dangling link */
Denys Vlasenko689d0652020-10-01 21:52:16 +020089 return state->fileAction(state, fileName, &statbuf);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +020090 }
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +000091 goto done_nak_warn;
Eric Andersenaad1a882001-03-16 22:47:14 +000092 }
93
Denis Vlasenko8c35d652006-10-27 23:42:25 +000094 /* If S_ISLNK(m), then we know that !S_ISDIR(m).
95 * Then we can skip checking first part: if it is true, then
96 * (!dir) is also true! */
Denys Vlasenko689d0652020-10-01 21:52:16 +020097 if ( /* (!(state->flags & ACTION_FOLLOWLINKS) && S_ISLNK(statbuf.st_mode)) || */
Denis Vlasenko8c35d652006-10-27 23:42:25 +000098 !S_ISDIR(statbuf.st_mode)
99 ) {
Denys Vlasenko689d0652020-10-01 21:52:16 +0200100 return state->fileAction(state, fileName, &statbuf);
Eric Andersenaad1a882001-03-16 22:47:14 +0000101 }
102
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000103 /* It's a directory (or a link to one, and followLinks is set) */
104
Denys Vlasenko689d0652020-10-01 21:52:16 +0200105 if (!(state->flags & ACTION_RECURSE)) {
106 return state->dirAction(state, fileName, &statbuf);
Eric Andersenaad1a882001-03-16 22:47:14 +0000107 }
108
Denys Vlasenko689d0652020-10-01 21:52:16 +0200109 if (!(state->flags & ACTION_DEPTHFIRST)) {
110 status = state->dirAction(state, fileName, &statbuf);
Denys Vlasenko4f0b5402017-04-06 15:22:24 +0200111 if (status == FALSE)
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000112 goto done_nak_warn;
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +0000113 if (status == SKIP)
Eric Andersenaad1a882001-03-16 22:47:14 +0000114 return TRUE;
Eric Andersenaad1a882001-03-16 22:47:14 +0000115 }
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +0000116
117 dir = opendir(fileName);
118 if (!dir) {
Denis Vlasenko5e2db5e2006-12-12 23:46:31 +0000119 /* findutils-4.1.20 reports this */
120 /* (i.e. it doesn't silently return with exit code 1) */
121 /* To trigger: "find -exec rm -rf {} \;" */
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000122 goto done_nak_warn;
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +0000123 }
124 status = TRUE;
125 while ((next = readdir(dir)) != NULL) {
126 char *nextFile;
Denys Vlasenko4f0b5402017-04-06 15:22:24 +0200127 int s;
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +0000128
129 nextFile = concat_subpath_file(fileName, next->d_name);
130 if (nextFile == NULL)
131 continue;
Denys Vlasenko4f0b5402017-04-06 15:22:24 +0200132
Denis Vlasenko2649f212008-06-26 03:26:57 +0000133 /* process every file (NB: ACTION_RECURSE is set in flags) */
Denys Vlasenko689d0652020-10-01 21:52:16 +0200134 state->depth++;
135 s = recursive_action1(state, nextFile);
Denys Vlasenko4f0b5402017-04-06 15:22:24 +0200136 if (s == FALSE)
Denis Vlasenkoa8a3b492008-07-04 10:29:30 +0000137 status = FALSE;
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +0000138 free(nextFile);
Denys Vlasenko689d0652020-10-01 21:52:16 +0200139 state->depth--;
140
Denys Vlasenko4f0b5402017-04-06 15:22:24 +0200141//#define RECURSE_RESULT_ABORT -1
Denis Vlasenko671691c2008-07-04 10:25:44 +0000142// if (s == RECURSE_RESULT_ABORT) {
143// closedir(dir);
144// return s;
145// }
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +0000146 }
147 closedir(dir);
Denis Vlasenkod166f832007-07-05 00:12:55 +0000148
Denys Vlasenko689d0652020-10-01 21:52:16 +0200149 if (state->flags & ACTION_DEPTHFIRST) {
150 if (!state->dirAction(state, fileName, &statbuf))
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000151 goto done_nak_warn;
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +0000152 }
153
Denis Vlasenko2649f212008-06-26 03:26:57 +0000154 return status;
Denis Vlasenkod166f832007-07-05 00:12:55 +0000155
156 done_nak_warn:
Denys Vlasenko689d0652020-10-01 21:52:16 +0200157 if (!(state->flags & ACTION_QUIET))
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000158 bb_simple_perror_msg(fileName);
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000159 return FALSE;
Eric Andersenaad1a882001-03-16 22:47:14 +0000160}
Denys Vlasenko689d0652020-10-01 21:52:16 +0200161
162int FAST_FUNC recursive_action(const char *fileName,
163 unsigned flags,
164 int FAST_FUNC (*fileAction)(struct recursive_state *state, const char *fileName, struct stat* statbuf),
165 int FAST_FUNC (*dirAction)(struct recursive_state *state, const char *fileName, struct stat* statbuf),
166 void *userData)
167{
168 /* Keeping a part of variables of recusive descent in a "state structure"
169 * instead of passing ALL of them down as parameters of recursive_action1()
170 * relieves register pressure, both in recursive_action1()
171 * and in every file/dirAction().
172 */
173 recursive_state_t state;
174 state.flags = flags;
175 state.depth = 0;
176 state.userData = userData;
177 state.fileAction = fileAction ? fileAction : true_action;
178 state.dirAction = dirAction ? dirAction : true_action;
179
180 return recursive_action1(&state, fileName);
181}