blob: 8917f470f7daefc67f75fe23f5317e62934dfa24 [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenbdfd0d72001-10-24 05:00:29 +00005 * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
Eric Andersenaad1a882001-03-16 22:47:14 +00006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Eric Andersenaad1a882001-03-16 22:47:14 +000020 */
21
22#include <stdio.h>
Eric Andersen1ca20a72001-03-21 07:34:27 +000023#include <string.h>
Eric Andersenaad1a882001-03-16 22:47:14 +000024#include <dirent.h>
25#include <sys/stat.h>
Eric Andersenfd402942001-04-10 17:53:49 +000026#include <stdlib.h> /* free() */
Eric Andersenaad1a882001-03-16 22:47:14 +000027#include "libbb.h"
28
Eric Andersenaad1a882001-03-16 22:47:14 +000029#undef DEBUG_RECURS_ACTION
30
31
32/*
33 * Walk down all the directories under the specified
34 * location, and do something (something specified
35 * by the fileAction and dirAction function pointers).
36 *
37 * Unfortunately, while nftw(3) could replace this and reduce
38 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
39 * and so isn't sufficiently portable to take over since glibc2.1
40 * is so stinking huge.
41 */
42int recursive_action(const char *fileName,
43 int recurse, int followLinks, int depthFirst,
44 int (*fileAction) (const char *fileName,
45 struct stat * statbuf,
46 void* userData),
47 int (*dirAction) (const char *fileName,
48 struct stat * statbuf,
49 void* userData),
50 void* userData)
51{
52 int status;
53 struct stat statbuf;
54 struct dirent *next;
55
Matt Kraai1f0c4362001-12-20 23:13:26 +000056 if (followLinks)
Eric Andersenaad1a882001-03-16 22:47:14 +000057 status = stat(fileName, &statbuf);
58 else
59 status = lstat(fileName, &statbuf);
60
61 if (status < 0) {
62#ifdef DEBUG_RECURS_ACTION
63 fprintf(stderr,
64 "status=%d followLinks=%d TRUE=%d\n",
65 status, followLinks, TRUE);
66#endif
67 perror_msg("%s", fileName);
68 return FALSE;
69 }
70
Matt Kraai1f0c4362001-12-20 23:13:26 +000071 if (! followLinks && (S_ISLNK(statbuf.st_mode))) {
Eric Andersenaad1a882001-03-16 22:47:14 +000072 if (fileAction == NULL)
73 return TRUE;
74 else
75 return fileAction(fileName, &statbuf, userData);
76 }
77
Matt Kraai1f0c4362001-12-20 23:13:26 +000078 if (! recurse) {
Eric Andersenaad1a882001-03-16 22:47:14 +000079 if (S_ISDIR(statbuf.st_mode)) {
80 if (dirAction != NULL)
81 return (dirAction(fileName, &statbuf, userData));
82 else
83 return TRUE;
84 }
85 }
86
87 if (S_ISDIR(statbuf.st_mode)) {
88 DIR *dir;
89
Matt Kraai1f0c4362001-12-20 23:13:26 +000090 if (dirAction != NULL && ! depthFirst) {
Eric Andersenaad1a882001-03-16 22:47:14 +000091 status = dirAction(fileName, &statbuf, userData);
Matt Kraai1f0c4362001-12-20 23:13:26 +000092 if (! status) {
Eric Andersenaad1a882001-03-16 22:47:14 +000093 perror_msg("%s", fileName);
94 return FALSE;
95 } else if (status == SKIP)
96 return TRUE;
97 }
98 dir = opendir(fileName);
99 if (!dir) {
100 perror_msg("%s", fileName);
101 return FALSE;
102 }
103 status = TRUE;
104 while ((next = readdir(dir)) != NULL) {
Eric Andersenfd402942001-04-10 17:53:49 +0000105 char *nextFile;
Eric Andersenaad1a882001-03-16 22:47:14 +0000106
107 if ((strcmp(next->d_name, "..") == 0)
108 || (strcmp(next->d_name, ".") == 0)) {
109 continue;
110 }
Eric Andersenfd402942001-04-10 17:53:49 +0000111 nextFile = concat_path_file(fileName, next->d_name);
Matt Kraai1f0c4362001-12-20 23:13:26 +0000112 if (! recursive_action(nextFile, TRUE, followLinks, depthFirst,
113 fileAction, dirAction, userData)) {
Eric Andersenaad1a882001-03-16 22:47:14 +0000114 status = FALSE;
115 }
Eric Andersenfd402942001-04-10 17:53:49 +0000116 free(nextFile);
Eric Andersenaad1a882001-03-16 22:47:14 +0000117 }
118 closedir(dir);
Matt Kraai1f0c4362001-12-20 23:13:26 +0000119 if (dirAction != NULL && depthFirst) {
120 if (! dirAction(fileName, &statbuf, userData)) {
Eric Andersenaad1a882001-03-16 22:47:14 +0000121 perror_msg("%s", fileName);
122 return FALSE;
123 }
124 }
Matt Kraai1f0c4362001-12-20 23:13:26 +0000125 if (! status)
Eric Andersenaad1a882001-03-16 22:47:14 +0000126 return FALSE;
127 } else {
128 if (fileAction == NULL)
129 return TRUE;
130 else
131 return fileAction(fileName, &statbuf, userData);
132 }
133 return TRUE;
134}
135
136
137/* END CODE */
138/*
139Local Variables:
140c-file-style: "linux"
141c-basic-offset: 4
142tab-width: 4
143End:
144*/