blob: 28a493403276e98e66503fc7251a8bb7e0852617 [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
14
15/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +000016 * Walk down all the directories under the specified
Eric Andersenaad1a882001-03-16 22:47:14 +000017 * location, and do something (something specified
18 * by the fileAction and dirAction function pointers).
19 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +000020 * Unfortunately, while nftw(3) could replace this and reduce
21 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersenaad1a882001-03-16 22:47:14 +000022 * and so isn't sufficiently portable to take over since glibc2.1
23 * is so stinking huge.
24 */
25int recursive_action(const char *fileName,
Denis Vlasenko0c45bb232006-09-09 12:49:03 +000026 int recurse, int followLinks, int depthFirst,
27 int (*fileAction) (const char *fileName, struct stat * statbuf, void* userData),
28 int (*dirAction) (const char *fileName, struct stat * statbuf, void* userData),
29 void* userData)
Eric Andersenaad1a882001-03-16 22:47:14 +000030{
31 int status;
32 struct stat statbuf;
33 struct dirent *next;
34
Matt Kraai1f0c4362001-12-20 23:13:26 +000035 if (followLinks)
Eric Andersenaad1a882001-03-16 22:47:14 +000036 status = stat(fileName, &statbuf);
37 else
38 status = lstat(fileName, &statbuf);
39
40 if (status < 0) {
41#ifdef DEBUG_RECURS_ACTION
Manuel Novoa III cad53642003-03-19 09:13:01 +000042 bb_error_msg("status=%d followLinks=%d TRUE=%d",
Eric Andersenaad1a882001-03-16 22:47:14 +000043 status, followLinks, TRUE);
44#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +000045 bb_perror_msg("%s", fileName);
Eric Andersenaad1a882001-03-16 22:47:14 +000046 return FALSE;
47 }
48
Matt Kraai1f0c4362001-12-20 23:13:26 +000049 if (! followLinks && (S_ISLNK(statbuf.st_mode))) {
Eric Andersenaad1a882001-03-16 22:47:14 +000050 if (fileAction == NULL)
51 return TRUE;
52 else
53 return fileAction(fileName, &statbuf, userData);
54 }
55
Matt Kraai1f0c4362001-12-20 23:13:26 +000056 if (! recurse) {
Eric Andersenaad1a882001-03-16 22:47:14 +000057 if (S_ISDIR(statbuf.st_mode)) {
58 if (dirAction != NULL)
59 return (dirAction(fileName, &statbuf, userData));
60 else
61 return TRUE;
62 }
63 }
64
65 if (S_ISDIR(statbuf.st_mode)) {
66 DIR *dir;
67
Matt Kraai1f0c4362001-12-20 23:13:26 +000068 if (dirAction != NULL && ! depthFirst) {
Eric Andersenaad1a882001-03-16 22:47:14 +000069 status = dirAction(fileName, &statbuf, userData);
Matt Kraai1f0c4362001-12-20 23:13:26 +000070 if (! status) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000071 bb_perror_msg("%s", fileName);
Eric Andersenaad1a882001-03-16 22:47:14 +000072 return FALSE;
73 } else if (status == SKIP)
74 return TRUE;
75 }
Rob Landleyd921b2e2006-08-03 15:41:12 +000076 dir = opendir(fileName);
Eric Andersenaad1a882001-03-16 22:47:14 +000077 if (!dir) {
Eric Andersenaad1a882001-03-16 22:47:14 +000078 return FALSE;
79 }
80 status = TRUE;
Eric Andersen66a56aa2004-04-07 17:59:04 +000081 while ((next = readdir(dir)) != NULL) {
Eric Andersenfd402942001-04-10 17:53:49 +000082 char *nextFile;
Eric Andersenaad1a882001-03-16 22:47:14 +000083
Glenn L McGrath393183d2003-05-26 14:07:50 +000084 nextFile = concat_subpath_file(fileName, next->d_name);
85 if(nextFile == NULL)
Eric Andersenaad1a882001-03-16 22:47:14 +000086 continue;
Matt Kraai1f0c4362001-12-20 23:13:26 +000087 if (! recursive_action(nextFile, TRUE, followLinks, depthFirst,
88 fileAction, dirAction, userData)) {
Eric Andersenaad1a882001-03-16 22:47:14 +000089 status = FALSE;
90 }
Eric Andersenfd402942001-04-10 17:53:49 +000091 free(nextFile);
Eric Andersenaad1a882001-03-16 22:47:14 +000092 }
93 closedir(dir);
Matt Kraai1f0c4362001-12-20 23:13:26 +000094 if (dirAction != NULL && depthFirst) {
95 if (! dirAction(fileName, &statbuf, userData)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000096 bb_perror_msg("%s", fileName);
Eric Andersenaad1a882001-03-16 22:47:14 +000097 return FALSE;
98 }
99 }
Matt Kraai1f0c4362001-12-20 23:13:26 +0000100 if (! status)
Eric Andersenaad1a882001-03-16 22:47:14 +0000101 return FALSE;
102 } else {
103 if (fileAction == NULL)
104 return TRUE;
105 else
106 return fileAction(fileName, &statbuf, userData);
107 }
108 return TRUE;
109}