blob: d491b781b073f69e942965590e26c5c02c524e4c [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,
26 int recurse, int followLinks, int depthFirst,
27 int (*fileAction) (const char *fileName,
28 struct stat * statbuf,
29 void* userData),
30 int (*dirAction) (const char *fileName,
31 struct stat * statbuf,
32 void* userData),
33 void* userData)
34{
35 int status;
36 struct stat statbuf;
37 struct dirent *next;
38
Matt Kraai1f0c4362001-12-20 23:13:26 +000039 if (followLinks)
Eric Andersenaad1a882001-03-16 22:47:14 +000040 status = stat(fileName, &statbuf);
41 else
42 status = lstat(fileName, &statbuf);
43
44 if (status < 0) {
45#ifdef DEBUG_RECURS_ACTION
Manuel Novoa III cad53642003-03-19 09:13:01 +000046 bb_error_msg("status=%d followLinks=%d TRUE=%d",
Eric Andersenaad1a882001-03-16 22:47:14 +000047 status, followLinks, TRUE);
48#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +000049 bb_perror_msg("%s", fileName);
Eric Andersenaad1a882001-03-16 22:47:14 +000050 return FALSE;
51 }
52
Matt Kraai1f0c4362001-12-20 23:13:26 +000053 if (! followLinks && (S_ISLNK(statbuf.st_mode))) {
Eric Andersenaad1a882001-03-16 22:47:14 +000054 if (fileAction == NULL)
55 return TRUE;
56 else
57 return fileAction(fileName, &statbuf, userData);
58 }
59
Matt Kraai1f0c4362001-12-20 23:13:26 +000060 if (! recurse) {
Eric Andersenaad1a882001-03-16 22:47:14 +000061 if (S_ISDIR(statbuf.st_mode)) {
62 if (dirAction != NULL)
63 return (dirAction(fileName, &statbuf, userData));
64 else
65 return TRUE;
66 }
67 }
68
69 if (S_ISDIR(statbuf.st_mode)) {
70 DIR *dir;
71
Matt Kraai1f0c4362001-12-20 23:13:26 +000072 if (dirAction != NULL && ! depthFirst) {
Eric Andersenaad1a882001-03-16 22:47:14 +000073 status = dirAction(fileName, &statbuf, userData);
Matt Kraai1f0c4362001-12-20 23:13:26 +000074 if (! status) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000075 bb_perror_msg("%s", fileName);
Eric Andersenaad1a882001-03-16 22:47:14 +000076 return FALSE;
77 } else if (status == SKIP)
78 return TRUE;
79 }
Rob Landleyd921b2e2006-08-03 15:41:12 +000080 dir = opendir(fileName);
Eric Andersenaad1a882001-03-16 22:47:14 +000081 if (!dir) {
Eric Andersenaad1a882001-03-16 22:47:14 +000082 return FALSE;
83 }
84 status = TRUE;
Eric Andersen66a56aa2004-04-07 17:59:04 +000085 while ((next = readdir(dir)) != NULL) {
Eric Andersenfd402942001-04-10 17:53:49 +000086 char *nextFile;
Eric Andersenaad1a882001-03-16 22:47:14 +000087
Glenn L McGrath393183d2003-05-26 14:07:50 +000088 nextFile = concat_subpath_file(fileName, next->d_name);
89 if(nextFile == NULL)
Eric Andersenaad1a882001-03-16 22:47:14 +000090 continue;
Matt Kraai1f0c4362001-12-20 23:13:26 +000091 if (! recursive_action(nextFile, TRUE, followLinks, depthFirst,
92 fileAction, dirAction, userData)) {
Eric Andersenaad1a882001-03-16 22:47:14 +000093 status = FALSE;
94 }
Eric Andersenfd402942001-04-10 17:53:49 +000095 free(nextFile);
Eric Andersenaad1a882001-03-16 22:47:14 +000096 }
97 closedir(dir);
Matt Kraai1f0c4362001-12-20 23:13:26 +000098 if (dirAction != NULL && depthFirst) {
99 if (! dirAction(fileName, &statbuf, userData)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000100 bb_perror_msg("%s", fileName);
Eric Andersenaad1a882001-03-16 22:47:14 +0000101 return FALSE;
102 }
103 }
Matt Kraai1f0c4362001-12-20 23:13:26 +0000104 if (! status)
Eric Andersenaad1a882001-03-16 22:47:14 +0000105 return FALSE;
106 } else {
107 if (fileAction == NULL)
108 return TRUE;
109 else
110 return fileAction(fileName, &statbuf, userData);
111 }
112 return TRUE;
113}