1 /* vi: set sw=4 ts=4: */
2 /*
3  * Utility routines.
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8  */
9 
10 #include "libbb.h"
11 
12 #undef DEBUG_RECURS_ACTION
13 
14 /*
15  * Walk down all the directories under the specified
16  * location, and do something (something specified
17  * by the fileAction and dirAction function pointers).
18  *
19  * Unfortunately, while nftw(3) could replace this and reduce
20  * code size a bit, nftw() wasn't supported before GNU libc 2.1,
21  * and so isn't sufficiently portable to take over since glibc2.1
22  * is so stinking huge.
23  */
24 
true_action(const char * fileName UNUSED_PARAM,struct stat * statbuf UNUSED_PARAM,void * userData UNUSED_PARAM,int depth UNUSED_PARAM)25 static 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)
29 {
30 	return TRUE;
31 }
32 
33 /* 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 !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)
50  *
51  * ACTION_FOLLOWLINKS mainly controls 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 
recursive_action(const char * fileName,unsigned flags,int FAST_FUNC (* fileAction)(const char * fileName,struct stat * statbuf,void * userData,int depth),int FAST_FUNC (* dirAction)(const char * fileName,struct stat * statbuf,void * userData,int depth),void * userData,unsigned depth)56 int FAST_FUNC recursive_action(const char *fileName,
57 		unsigned flags,
58 		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),
60 		void* userData,
61 		unsigned depth)
62 {
63 	struct stat statbuf;
64 	unsigned follow;
65 	int status;
66 	DIR *dir;
67 	struct dirent *next;
68 
69 	if (!fileAction) fileAction = true_action;
70 	if (!dirAction) dirAction = true_action;
71 
72 	follow = ACTION_FOLLOWLINKS;
73 	if (depth == 0)
74 		follow = ACTION_FOLLOWLINKS | ACTION_FOLLOWLINKS_L0;
75 	follow &= flags;
76 	status = (follow ? stat : lstat)(fileName, &statbuf);
77 	if (status < 0) {
78 #ifdef DEBUG_RECURS_ACTION
79 		bb_error_msg("status=%d flags=%x", status, flags);
80 #endif
81 		if ((flags & ACTION_DANGLING_OK)
82 		 && errno == ENOENT
83 		 && lstat(fileName, &statbuf) == 0
84 		) {
85 			/* Dangling link */
86 			return fileAction(fileName, &statbuf, userData, depth);
87 		}
88 		goto done_nak_warn;
89 	}
90 
91 	/* If S_ISLNK(m), then we know that !S_ISDIR(m).
92 	 * Then we can skip checking first part: if it is true, then
93 	 * (!dir) is also true! */
94 	if ( /* (!(flags & ACTION_FOLLOWLINKS) && S_ISLNK(statbuf.st_mode)) || */
95 	 !S_ISDIR(statbuf.st_mode)
96 	) {
97 		return fileAction(fileName, &statbuf, userData, depth);
98 	}
99 
100 	/* It's a directory (or a link to one, and followLinks is set) */
101 
102 	if (!(flags & ACTION_RECURSE)) {
103 		return dirAction(fileName, &statbuf, userData, depth);
104 	}
105 
106 	if (!(flags & ACTION_DEPTHFIRST)) {
107 		status = dirAction(fileName, &statbuf, userData, depth);
108 		if (!status)
109 			goto done_nak_warn;
110 		if (status == SKIP)
111 			return TRUE;
112 	}
113 
114 	dir = opendir(fileName);
115 	if (!dir) {
116 		/* findutils-4.1.20 reports this */
117 		/* (i.e. it doesn't silently return with exit code 1) */
118 		/* To trigger: "find -exec rm -rf {} \;" */
119 		goto done_nak_warn;
120 	}
121 	status = TRUE;
122 	while ((next = readdir(dir)) != NULL) {
123 		char *nextFile;
124 
125 		nextFile = concat_subpath_file(fileName, next->d_name);
126 		if (nextFile == NULL)
127 			continue;
128 		/* process every file (NB: ACTION_RECURSE is set in flags) */
129 		if (!recursive_action(nextFile, flags, fileAction, dirAction,
130 						userData, depth + 1))
131 			status = FALSE;
132 //		s = recursive_action(nextFile, flags, fileAction, dirAction,
133 //						userData, depth + 1);
134 		free(nextFile);
135 //#define RECURSE_RESULT_ABORT 3
136 //		if (s == RECURSE_RESULT_ABORT) {
137 //			closedir(dir);
138 //			return s;
139 //		}
140 //		if (s == FALSE)
141 //			status = FALSE;
142 	}
143 	closedir(dir);
144 
145 	if (flags & ACTION_DEPTHFIRST) {
146 		if (!dirAction(fileName, &statbuf, userData, depth))
147 			goto done_nak_warn;
148 	}
149 
150 	return status;
151 
152  done_nak_warn:
153 	if (!(flags & ACTION_QUIET))
154 		bb_simple_perror_msg(fileName);
155 	return FALSE;
156 }
157