blob: 6b005e22dfe19af5e31803c622cc30ce95ac307a [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
10#include <stdio.h>
Eric Andersen1ca20a72001-03-21 07:34:27 +000011#include <string.h>
Eric Andersenaad1a882001-03-16 22:47:14 +000012#include <dirent.h>
13#include <sys/stat.h>
Eric Andersenfd402942001-04-10 17:53:49 +000014#include <stdlib.h> /* free() */
Eric Andersenaad1a882001-03-16 22:47:14 +000015#include "libbb.h"
16
Eric Andersenaad1a882001-03-16 22:47:14 +000017#undef DEBUG_RECURS_ACTION
18
19
20/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +000021 * Walk down all the directories under the specified
Eric Andersenaad1a882001-03-16 22:47:14 +000022 * location, and do something (something specified
23 * by the fileAction and dirAction function pointers).
24 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +000025 * Unfortunately, while nftw(3) could replace this and reduce
26 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersenaad1a882001-03-16 22:47:14 +000027 * and so isn't sufficiently portable to take over since glibc2.1
28 * is so stinking huge.
29 */
30int recursive_action(const char *fileName,
31 int recurse, int followLinks, int depthFirst,
32 int (*fileAction) (const char *fileName,
33 struct stat * statbuf,
34 void* userData),
35 int (*dirAction) (const char *fileName,
36 struct stat * statbuf,
37 void* userData),
38 void* userData)
39{
40 int status;
41 struct stat statbuf;
42 struct dirent *next;
43
Matt Kraai1f0c4362001-12-20 23:13:26 +000044 if (followLinks)
Eric Andersenaad1a882001-03-16 22:47:14 +000045 status = stat(fileName, &statbuf);
46 else
47 status = lstat(fileName, &statbuf);
48
49 if (status < 0) {
50#ifdef DEBUG_RECURS_ACTION
Manuel Novoa III cad53642003-03-19 09:13:01 +000051 bb_error_msg("status=%d followLinks=%d TRUE=%d",
Eric Andersenaad1a882001-03-16 22:47:14 +000052 status, followLinks, TRUE);
53#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +000054 bb_perror_msg("%s", fileName);
Eric Andersenaad1a882001-03-16 22:47:14 +000055 return FALSE;
56 }
57
Matt Kraai1f0c4362001-12-20 23:13:26 +000058 if (! followLinks && (S_ISLNK(statbuf.st_mode))) {
Eric Andersenaad1a882001-03-16 22:47:14 +000059 if (fileAction == NULL)
60 return TRUE;
61 else
62 return fileAction(fileName, &statbuf, userData);
63 }
64
Matt Kraai1f0c4362001-12-20 23:13:26 +000065 if (! recurse) {
Eric Andersenaad1a882001-03-16 22:47:14 +000066 if (S_ISDIR(statbuf.st_mode)) {
67 if (dirAction != NULL)
68 return (dirAction(fileName, &statbuf, userData));
69 else
70 return TRUE;
71 }
72 }
73
74 if (S_ISDIR(statbuf.st_mode)) {
75 DIR *dir;
76
Matt Kraai1f0c4362001-12-20 23:13:26 +000077 if (dirAction != NULL && ! depthFirst) {
Eric Andersenaad1a882001-03-16 22:47:14 +000078 status = dirAction(fileName, &statbuf, userData);
Matt Kraai1f0c4362001-12-20 23:13:26 +000079 if (! status) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000080 bb_perror_msg("%s", fileName);
Eric Andersenaad1a882001-03-16 22:47:14 +000081 return FALSE;
82 } else if (status == SKIP)
83 return TRUE;
84 }
Bernhard Reutner-Fischercb448162006-04-12 07:35:12 +000085 dir = bb_opendir(fileName);
Eric Andersenaad1a882001-03-16 22:47:14 +000086 if (!dir) {
Eric Andersenaad1a882001-03-16 22:47:14 +000087 return FALSE;
88 }
89 status = TRUE;
Eric Andersen66a56aa2004-04-07 17:59:04 +000090 while ((next = readdir(dir)) != NULL) {
Eric Andersenfd402942001-04-10 17:53:49 +000091 char *nextFile;
Eric Andersenaad1a882001-03-16 22:47:14 +000092
Glenn L McGrath393183d2003-05-26 14:07:50 +000093 nextFile = concat_subpath_file(fileName, next->d_name);
94 if(nextFile == NULL)
Eric Andersenaad1a882001-03-16 22:47:14 +000095 continue;
Matt Kraai1f0c4362001-12-20 23:13:26 +000096 if (! recursive_action(nextFile, TRUE, followLinks, depthFirst,
97 fileAction, dirAction, userData)) {
Eric Andersenaad1a882001-03-16 22:47:14 +000098 status = FALSE;
99 }
Eric Andersenfd402942001-04-10 17:53:49 +0000100 free(nextFile);
Eric Andersenaad1a882001-03-16 22:47:14 +0000101 }
102 closedir(dir);
Matt Kraai1f0c4362001-12-20 23:13:26 +0000103 if (dirAction != NULL && depthFirst) {
104 if (! dirAction(fileName, &statbuf, userData)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000105 bb_perror_msg("%s", fileName);
Eric Andersenaad1a882001-03-16 22:47:14 +0000106 return FALSE;
107 }
108 }
Matt Kraai1f0c4362001-12-20 23:13:26 +0000109 if (! status)
Eric Andersenaad1a882001-03-16 22:47:14 +0000110 return FALSE;
111 } else {
112 if (fileAction == NULL)
113 return TRUE;
114 else
115 return fileAction(fileName, &statbuf, userData);
116 }
117 return TRUE;
118}
119
120
121/* END CODE */
122/*
123Local Variables:
124c-file-style: "linux"
125c-basic-offset: 4
126tab-width: 4
127End:
128*/