blob: 513aff31579b363ee7f7bc31587ff336ba730438 [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 Vlasenko68404f12008-03-17 09:00:54 +000025static int true_action(const char *fileName ATTRIBUTE_UNUSED,
26 struct stat *statbuf ATTRIBUTE_UNUSED,
27 void* userData ATTRIBUTE_UNUSED,
28 int depth ATTRIBUTE_UNUSED)
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 *
37 * if !depthFirst, dirAction return value of 0 (FALSE) or 2 (SKIP)
38 * prevents recursion into that directory, instead
Denis Vlasenkof7996f32007-01-11 17:20:00 +000039 * recursive_action() returns 0 (if FALSE) or 1 (if SKIP).
Denis Vlasenko5d499e12006-10-29 19:07:01 +000040 *
Denis Vlasenko8c35d652006-10-27 23:42:25 +000041 * followLinks=0/1 differs mainly in handling of links to dirs.
42 * 0: lstat(statbuf). Calls fileAction on link name even if points to dir.
43 * 1: stat(statbuf). Calls dirAction and optionally recurse on link to dir.
44 */
45
Eric Andersenaad1a882001-03-16 22:47:14 +000046int recursive_action(const char *fileName,
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +000047 unsigned flags,
Denis Vlasenko8c35d652006-10-27 23:42:25 +000048 int (*fileAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
49 int (*dirAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
50 void* userData,
Denis Vlasenkod166f832007-07-05 00:12:55 +000051 unsigned depth)
Eric Andersenaad1a882001-03-16 22:47:14 +000052{
Eric Andersenaad1a882001-03-16 22:47:14 +000053 struct stat statbuf;
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +000054 int status;
55 DIR *dir;
Eric Andersenaad1a882001-03-16 22:47:14 +000056 struct dirent *next;
57
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +000058 if (!fileAction) fileAction = true_action;
59 if (!dirAction) dirAction = true_action;
Eric Andersenaad1a882001-03-16 22:47:14 +000060
Denis Vlasenkod166f832007-07-05 00:12:55 +000061 status = ACTION_FOLLOWLINKS; /* hijack a variable for bitmask... */
62 if (!depth) status = ACTION_FOLLOWLINKS | ACTION_FOLLOWLINKS_L0;
63 status = ((flags & status) ? stat : lstat)(fileName, &statbuf);
Eric Andersenaad1a882001-03-16 22:47:14 +000064 if (status < 0) {
65#ifdef DEBUG_RECURS_ACTION
Denis Vlasenkod166f832007-07-05 00:12:55 +000066 bb_error_msg("status=%d flags=%x", status, flags);
Eric Andersenaad1a882001-03-16 22:47:14 +000067#endif
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +000068 goto done_nak_warn;
Eric Andersenaad1a882001-03-16 22:47:14 +000069 }
70
Denis Vlasenko8c35d652006-10-27 23:42:25 +000071 /* If S_ISLNK(m), then we know that !S_ISDIR(m).
72 * Then we can skip checking first part: if it is true, then
73 * (!dir) is also true! */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +000074 if ( /* (!(flags & ACTION_FOLLOWLINKS) && S_ISLNK(statbuf.st_mode)) || */
Denis Vlasenko8c35d652006-10-27 23:42:25 +000075 !S_ISDIR(statbuf.st_mode)
76 ) {
77 return fileAction(fileName, &statbuf, userData, depth);
Eric Andersenaad1a882001-03-16 22:47:14 +000078 }
79
Denis Vlasenko8c35d652006-10-27 23:42:25 +000080 /* It's a directory (or a link to one, and followLinks is set) */
81
Denis Vlasenkobbd695d2007-04-08 10:52:28 +000082 if (!(flags & ACTION_RECURSE)) {
Denis Vlasenko8c35d652006-10-27 23:42:25 +000083 return dirAction(fileName, &statbuf, userData, depth);
Eric Andersenaad1a882001-03-16 22:47:14 +000084 }
85
Denis Vlasenkobbd695d2007-04-08 10:52:28 +000086 if (!(flags & ACTION_DEPTHFIRST)) {
Denis Vlasenko8c35d652006-10-27 23:42:25 +000087 status = dirAction(fileName, &statbuf, userData, depth);
Denis Vlasenkod166f832007-07-05 00:12:55 +000088 if (!status)
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +000089 goto done_nak_warn;
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +000090 if (status == SKIP)
Eric Andersenaad1a882001-03-16 22:47:14 +000091 return TRUE;
Eric Andersenaad1a882001-03-16 22:47:14 +000092 }
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +000093
94 dir = opendir(fileName);
95 if (!dir) {
Denis Vlasenko5e2db5e2006-12-12 23:46:31 +000096 /* findutils-4.1.20 reports this */
97 /* (i.e. it doesn't silently return with exit code 1) */
98 /* To trigger: "find -exec rm -rf {} \;" */
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +000099 goto done_nak_warn;
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +0000100 }
101 status = TRUE;
102 while ((next = readdir(dir)) != NULL) {
103 char *nextFile;
104
105 nextFile = concat_subpath_file(fileName, next->d_name);
106 if (nextFile == NULL)
107 continue;
Denis Vlasenkod166f832007-07-05 00:12:55 +0000108 /* now descend into it (NB: ACTION_RECURSE is set in flags) */
109 if (!recursive_action(nextFile, flags, fileAction, dirAction, userData, depth+1))
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +0000110 status = FALSE;
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +0000111 free(nextFile);
112 }
113 closedir(dir);
Denis Vlasenkod166f832007-07-05 00:12:55 +0000114
115 if (flags & ACTION_DEPTHFIRST) {
116 if (!dirAction(fileName, &statbuf, userData, depth))
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000117 goto done_nak_warn;
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +0000118 }
119
120 if (!status)
121 return FALSE;
Eric Andersenaad1a882001-03-16 22:47:14 +0000122 return TRUE;
Denis Vlasenkod166f832007-07-05 00:12:55 +0000123
124 done_nak_warn:
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000125 bb_simple_perror_msg(fileName);
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000126 return FALSE;
Eric Andersenaad1a882001-03-16 22:47:14 +0000127}