blob: 3ec596a35772d99bd677612080499ab44381f3bd [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 *
Bernhard Reutner-Fischercb448162006-04-12 07:35:12 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
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
Denis Vlasenko5d499e12006-10-29 19:07:01 +000033/* fileAction return value of 0 on any file in directory will make
34 * recursive_action() return 0, but it doesn't stop directory traversal
35 * (fileAction/dirAction will be called on each file).
36 *
Denis Vlasenko2649f212008-06-26 03:26:57 +000037 * If !ACTION_RECURSE, dirAction is called on the directory and its
38 * return value is returned from recursive_action(). No recursion.
39 *
40 * If ACTION_RECURSE, recursive_action() is called on each directory.
41 * If any one of these calls returns 0, current recursive_action() returns 0.
42 *
43 * If ACTION_DEPTHFIRST, dirAction is called after recurse.
44 * If it returns 0, the warning is printed and recursive_action() returns 0.
45 *
46 * If !ACTION_DEPTHFIRST, dirAction is called before we recurse.
47 * Return value of 0 (FALSE) or 2 (SKIP) prevents recursion
48 * into that directory, instead recursive_action() returns 0 (if FALSE)
49 * or 1 (if SKIP)
Denis Vlasenko5d499e12006-10-29 19:07:01 +000050 *
Denis Vlasenko8c35d652006-10-27 23:42:25 +000051 * followLinks=0/1 differs mainly in handling of links to dirs.
52 * 0: lstat(statbuf). Calls fileAction on link name even if points to dir.
53 * 1: stat(statbuf). Calls dirAction and optionally recurse on link to dir.
54 */
55
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000056int FAST_FUNC recursive_action(const char *fileName,
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +000057 unsigned flags,
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000058 int FAST_FUNC (*fileAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
59 int FAST_FUNC (*dirAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
Denis Vlasenko8c35d652006-10-27 23:42:25 +000060 void* userData,
Denis Vlasenkod166f832007-07-05 00:12:55 +000061 unsigned depth)
Eric Andersenaad1a882001-03-16 22:47:14 +000062{
Eric Andersenaad1a882001-03-16 22:47:14 +000063 struct stat statbuf;
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +000064 int status;
65 DIR *dir;
Eric Andersenaad1a882001-03-16 22:47:14 +000066 struct dirent *next;
67
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +000068 if (!fileAction) fileAction = true_action;
69 if (!dirAction) dirAction = true_action;
Eric Andersenaad1a882001-03-16 22:47:14 +000070
Denis Vlasenkod166f832007-07-05 00:12:55 +000071 status = ACTION_FOLLOWLINKS; /* hijack a variable for bitmask... */
Denis Vlasenko2649f212008-06-26 03:26:57 +000072 if (!depth)
73 status = ACTION_FOLLOWLINKS | ACTION_FOLLOWLINKS_L0;
Denis Vlasenkod166f832007-07-05 00:12:55 +000074 status = ((flags & status) ? stat : lstat)(fileName, &statbuf);
Eric Andersenaad1a882001-03-16 22:47:14 +000075 if (status < 0) {
76#ifdef DEBUG_RECURS_ACTION
Denis Vlasenkod166f832007-07-05 00:12:55 +000077 bb_error_msg("status=%d flags=%x", status, flags);
Eric Andersenaad1a882001-03-16 22:47:14 +000078#endif
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +000079 goto done_nak_warn;
Eric Andersenaad1a882001-03-16 22:47:14 +000080 }
81
Denis Vlasenko8c35d652006-10-27 23:42:25 +000082 /* If S_ISLNK(m), then we know that !S_ISDIR(m).
83 * Then we can skip checking first part: if it is true, then
84 * (!dir) is also true! */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +000085 if ( /* (!(flags & ACTION_FOLLOWLINKS) && S_ISLNK(statbuf.st_mode)) || */
Denis Vlasenko8c35d652006-10-27 23:42:25 +000086 !S_ISDIR(statbuf.st_mode)
87 ) {
88 return fileAction(fileName, &statbuf, userData, depth);
Eric Andersenaad1a882001-03-16 22:47:14 +000089 }
90
Denis Vlasenko8c35d652006-10-27 23:42:25 +000091 /* It's a directory (or a link to one, and followLinks is set) */
92
Denis Vlasenkobbd695d2007-04-08 10:52:28 +000093 if (!(flags & ACTION_RECURSE)) {
Denis Vlasenko8c35d652006-10-27 23:42:25 +000094 return dirAction(fileName, &statbuf, userData, depth);
Eric Andersenaad1a882001-03-16 22:47:14 +000095 }
96
Denis Vlasenkobbd695d2007-04-08 10:52:28 +000097 if (!(flags & ACTION_DEPTHFIRST)) {
Denis Vlasenko8c35d652006-10-27 23:42:25 +000098 status = dirAction(fileName, &statbuf, userData, depth);
Denis Vlasenkod166f832007-07-05 00:12:55 +000099 if (!status)
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000100 goto done_nak_warn;
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +0000101 if (status == SKIP)
Eric Andersenaad1a882001-03-16 22:47:14 +0000102 return TRUE;
Eric Andersenaad1a882001-03-16 22:47:14 +0000103 }
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +0000104
105 dir = opendir(fileName);
106 if (!dir) {
Denis Vlasenko5e2db5e2006-12-12 23:46:31 +0000107 /* findutils-4.1.20 reports this */
108 /* (i.e. it doesn't silently return with exit code 1) */
109 /* To trigger: "find -exec rm -rf {} \;" */
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000110 goto done_nak_warn;
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +0000111 }
112 status = TRUE;
113 while ((next = readdir(dir)) != NULL) {
114 char *nextFile;
115
116 nextFile = concat_subpath_file(fileName, next->d_name);
117 if (nextFile == NULL)
118 continue;
Denis Vlasenko2649f212008-06-26 03:26:57 +0000119 /* process every file (NB: ACTION_RECURSE is set in flags) */
Denis Vlasenkoa8a3b492008-07-04 10:29:30 +0000120 if (!recursive_action(nextFile, flags, fileAction, dirAction,
121 userData, depth + 1))
122 status = FALSE;
123// s = recursive_action(nextFile, flags, fileAction, dirAction,
124// userData, depth + 1);
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +0000125 free(nextFile);
Denis Vlasenko671691c2008-07-04 10:25:44 +0000126//#define RECURSE_RESULT_ABORT 3
127// if (s == RECURSE_RESULT_ABORT) {
128// closedir(dir);
129// return s;
130// }
131// if (s == FALSE)
132// status = FALSE;
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +0000133 }
134 closedir(dir);
Denis Vlasenkod166f832007-07-05 00:12:55 +0000135
136 if (flags & ACTION_DEPTHFIRST) {
137 if (!dirAction(fileName, &statbuf, userData, depth))
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000138 goto done_nak_warn;
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +0000139 }
140
Denis Vlasenko2649f212008-06-26 03:26:57 +0000141 return status;
Denis Vlasenkod166f832007-07-05 00:12:55 +0000142
143 done_nak_warn:
Denis Vlasenko6e69e422008-07-27 12:10:07 +0000144 if (!(flags & ACTION_QUIET))
145 bb_simple_perror_msg(fileName);
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000146 return FALSE;
Eric Andersenaad1a882001-03-16 22:47:14 +0000147}