blob: 8f2b8b9320f05e1b1cfda1287aef5bbeaf1171de [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 */
9
Eric Andersenaad1a882001-03-16 22:47:14 +000010#include "libbb.h"
11
Eric Andersenaad1a882001-03-16 22:47:14 +000012#undef DEBUG_RECURS_ACTION
13
Eric Andersenaad1a882001-03-16 22:47:14 +000014/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +000015 * Walk down all the directories under the specified
Eric Andersenaad1a882001-03-16 22:47:14 +000016 * location, and do something (something specified
17 * by the fileAction and dirAction function pointers).
18 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +000019 * Unfortunately, while nftw(3) could replace this and reduce
20 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersenaad1a882001-03-16 22:47:14 +000021 * and so isn't sufficiently portable to take over since glibc2.1
22 * is so stinking huge.
23 */
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +000024
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000025static 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)
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +000029{
30 return TRUE;
31}
32
Denys Vlasenko4f0b5402017-04-06 15:22:24 +020033/* 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
Denis Vlasenko5d499e12006-10-29 19:07:01 +000042 * (fileAction/dirAction will be called on each file).
43 *
Denys Vlasenko4f0b5402017-04-06 15:22:24 +020044 * [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
Denis Vlasenko2649f212008-06-26 03:26:57 +000049 * return value is returned from recursive_action(). No recursion.
50 *
Denys Vlasenko4f0b5402017-04-06 15:22:24 +020051 * If ACTION_RECURSE, directory is opened, and recursive_action() is called
52 * on each file/subdirectory.
Denis Vlasenko2649f212008-06-26 03:26:57 +000053 * If any one of these calls returns 0, current recursive_action() returns 0.
54 *
Denys Vlasenko4f0b5402017-04-06 15:22:24 +020055 * 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 *
Denis Vlasenko2649f212008-06-26 03:26:57 +000061 * If ACTION_DEPTHFIRST, dirAction is called after recurse.
62 * If it returns 0, the warning is printed and recursive_action() returns 0.
63 *
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +020064 * ACTION_FOLLOWLINKS mainly controls handling of links to dirs.
Denis Vlasenko8c35d652006-10-27 23:42:25 +000065 * 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
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000069int FAST_FUNC recursive_action(const char *fileName,
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +000070 unsigned flags,
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000071 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),
Denis Vlasenko8c35d652006-10-27 23:42:25 +000073 void* userData,
Denis Vlasenkod166f832007-07-05 00:12:55 +000074 unsigned depth)
Eric Andersenaad1a882001-03-16 22:47:14 +000075{
Eric Andersenaad1a882001-03-16 22:47:14 +000076 struct stat statbuf;
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +020077 unsigned follow;
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +000078 int status;
79 DIR *dir;
Eric Andersenaad1a882001-03-16 22:47:14 +000080 struct dirent *next;
81
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +000082 if (!fileAction) fileAction = true_action;
83 if (!dirAction) dirAction = true_action;
Eric Andersenaad1a882001-03-16 22:47:14 +000084
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +020085 follow = ACTION_FOLLOWLINKS;
86 if (depth == 0)
87 follow = ACTION_FOLLOWLINKS | ACTION_FOLLOWLINKS_L0;
88 follow &= flags;
89 status = (follow ? stat : lstat)(fileName, &statbuf);
Eric Andersenaad1a882001-03-16 22:47:14 +000090 if (status < 0) {
91#ifdef DEBUG_RECURS_ACTION
Denis Vlasenkod166f832007-07-05 00:12:55 +000092 bb_error_msg("status=%d flags=%x", status, flags);
Eric Andersenaad1a882001-03-16 22:47:14 +000093#endif
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +020094 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 }
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000101 goto done_nak_warn;
Eric Andersenaad1a882001-03-16 22:47:14 +0000102 }
103
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000104 /* 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! */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000107 if ( /* (!(flags & ACTION_FOLLOWLINKS) && S_ISLNK(statbuf.st_mode)) || */
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000108 !S_ISDIR(statbuf.st_mode)
109 ) {
110 return fileAction(fileName, &statbuf, userData, depth);
Eric Andersenaad1a882001-03-16 22:47:14 +0000111 }
112
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000113 /* It's a directory (or a link to one, and followLinks is set) */
114
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000115 if (!(flags & ACTION_RECURSE)) {
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000116 return dirAction(fileName, &statbuf, userData, depth);
Eric Andersenaad1a882001-03-16 22:47:14 +0000117 }
118
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000119 if (!(flags & ACTION_DEPTHFIRST)) {
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000120 status = dirAction(fileName, &statbuf, userData, depth);
Denys Vlasenko4f0b5402017-04-06 15:22:24 +0200121 if (status == FALSE)
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000122 goto done_nak_warn;
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +0000123 if (status == SKIP)
Eric Andersenaad1a882001-03-16 22:47:14 +0000124 return TRUE;
Eric Andersenaad1a882001-03-16 22:47:14 +0000125 }
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +0000126
127 dir = opendir(fileName);
128 if (!dir) {
Denis Vlasenko5e2db5e2006-12-12 23:46:31 +0000129 /* 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 {} \;" */
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000132 goto done_nak_warn;
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +0000133 }
134 status = TRUE;
135 while ((next = readdir(dir)) != NULL) {
136 char *nextFile;
Denys Vlasenko4f0b5402017-04-06 15:22:24 +0200137 int s;
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +0000138
139 nextFile = concat_subpath_file(fileName, next->d_name);
140 if (nextFile == NULL)
141 continue;
Denys Vlasenko4f0b5402017-04-06 15:22:24 +0200142
Denis Vlasenko2649f212008-06-26 03:26:57 +0000143 /* process every file (NB: ACTION_RECURSE is set in flags) */
Denys Vlasenko4f0b5402017-04-06 15:22:24 +0200144 s = recursive_action(nextFile, flags, fileAction, dirAction,
145 userData, depth + 1);
146 if (s == FALSE)
Denis Vlasenkoa8a3b492008-07-04 10:29:30 +0000147 status = FALSE;
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +0000148 free(nextFile);
Denys Vlasenko4f0b5402017-04-06 15:22:24 +0200149//#define RECURSE_RESULT_ABORT -1
Denis Vlasenko671691c2008-07-04 10:25:44 +0000150// if (s == RECURSE_RESULT_ABORT) {
151// closedir(dir);
152// return s;
153// }
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +0000154 }
155 closedir(dir);
Denis Vlasenkod166f832007-07-05 00:12:55 +0000156
157 if (flags & ACTION_DEPTHFIRST) {
158 if (!dirAction(fileName, &statbuf, userData, depth))
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000159 goto done_nak_warn;
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +0000160 }
161
Denis Vlasenko2649f212008-06-26 03:26:57 +0000162 return status;
Denis Vlasenkod166f832007-07-05 00:12:55 +0000163
164 done_nak_warn:
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000165 if (!(flags & ACTION_QUIET))
166 bb_simple_perror_msg(fileName);
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000167 return FALSE;
Eric Andersenaad1a882001-03-16 22:47:14 +0000168}