blob: 25a87b88e451a48b560d2d01d1c7c6bbf96188b8 [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 Vlasenko8c35d652006-10-27 23:42:25 +000025static int true_action(const char *fileName, struct stat *statbuf, void* userData, int depth)
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +000026{
27 return TRUE;
28}
29
Denis Vlasenko5d499e12006-10-29 19:07:01 +000030/* fileAction return value of 0 on any file in directory will make
31 * recursive_action() return 0, but it doesn't stop directory traversal
32 * (fileAction/dirAction will be called on each file).
33 *
34 * if !depthFirst, dirAction return value of 0 (FALSE) or 2 (SKIP)
35 * prevents recursion into that directory, instead
Denis Vlasenkof7996f32007-01-11 17:20:00 +000036 * recursive_action() returns 0 (if FALSE) or 1 (if SKIP).
Denis Vlasenko5d499e12006-10-29 19:07:01 +000037 *
Denis Vlasenko8c35d652006-10-27 23:42:25 +000038 * followLinks=0/1 differs mainly in handling of links to dirs.
39 * 0: lstat(statbuf). Calls fileAction on link name even if points to dir.
40 * 1: stat(statbuf). Calls dirAction and optionally recurse on link to dir.
41 */
42
Eric Andersenaad1a882001-03-16 22:47:14 +000043int recursive_action(const char *fileName,
Denis Vlasenko0c45bb232006-09-09 12:49:03 +000044 int recurse, int followLinks, int depthFirst,
Denis Vlasenko8c35d652006-10-27 23:42:25 +000045 int (*fileAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
46 int (*dirAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
47 void* userData,
48 int depth)
Eric Andersenaad1a882001-03-16 22:47:14 +000049{
Eric Andersenaad1a882001-03-16 22:47:14 +000050 struct stat statbuf;
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +000051 int status;
52 DIR *dir;
Eric Andersenaad1a882001-03-16 22:47:14 +000053 struct dirent *next;
54
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +000055 if (!fileAction) fileAction = true_action;
56 if (!dirAction) dirAction = true_action;
57
58 status = (followLinks ? stat : lstat)(fileName, &statbuf);
Eric Andersenaad1a882001-03-16 22:47:14 +000059
60 if (status < 0) {
61#ifdef DEBUG_RECURS_ACTION
Manuel Novoa III cad53642003-03-19 09:13:01 +000062 bb_error_msg("status=%d followLinks=%d TRUE=%d",
Eric Andersenaad1a882001-03-16 22:47:14 +000063 status, followLinks, TRUE);
64#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +000065 bb_perror_msg("%s", fileName);
Eric Andersenaad1a882001-03-16 22:47:14 +000066 return FALSE;
67 }
68
Denis Vlasenko8c35d652006-10-27 23:42:25 +000069 /* If S_ISLNK(m), then we know that !S_ISDIR(m).
70 * Then we can skip checking first part: if it is true, then
71 * (!dir) is also true! */
72 if ( /* (!followLinks && S_ISLNK(statbuf.st_mode)) || */
73 !S_ISDIR(statbuf.st_mode)
74 ) {
75 return fileAction(fileName, &statbuf, userData, depth);
Eric Andersenaad1a882001-03-16 22:47:14 +000076 }
77
Denis Vlasenko8c35d652006-10-27 23:42:25 +000078 /* It's a directory (or a link to one, and followLinks is set) */
79
Denis Vlasenko3a6755f2006-10-14 14:24:30 +000080 if (!recurse) {
Denis Vlasenko8c35d652006-10-27 23:42:25 +000081 return dirAction(fileName, &statbuf, userData, depth);
Eric Andersenaad1a882001-03-16 22:47:14 +000082 }
83
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +000084 if (!depthFirst) {
Denis Vlasenko8c35d652006-10-27 23:42:25 +000085 status = dirAction(fileName, &statbuf, userData, depth);
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +000086 if (!status) {
87 bb_perror_msg("%s", fileName);
Eric Andersenaad1a882001-03-16 22:47:14 +000088 return FALSE;
89 }
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 {} \;" */
99 bb_perror_msg("%s", fileName);
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +0000100 return FALSE;
101 }
102 status = TRUE;
103 while ((next = readdir(dir)) != NULL) {
104 char *nextFile;
105
106 nextFile = concat_subpath_file(fileName, next->d_name);
107 if (nextFile == NULL)
108 continue;
109 if (!recursive_action(nextFile, TRUE, followLinks, depthFirst,
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000110 fileAction, dirAction, userData, depth+1)) {
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +0000111 status = FALSE;
112 }
113 free(nextFile);
114 }
115 closedir(dir);
116 if (depthFirst) {
Denis Vlasenko8c35d652006-10-27 23:42:25 +0000117 if (!dirAction(fileName, &statbuf, userData, depth)) {
Denis Vlasenko3b8fc1c2006-10-27 17:59:14 +0000118 bb_perror_msg("%s", fileName);
119 return FALSE;
120 }
121 }
122
123 if (!status)
124 return FALSE;
Eric Andersenaad1a882001-03-16 22:47:14 +0000125 return TRUE;
126}