Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 6 | * |
| 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 Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <stdio.h> |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 23 | #include <string.h> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 24 | #include <dirent.h> |
| 25 | #include <sys/stat.h> |
Eric Andersen | fd40294 | 2001-04-10 17:53:49 +0000 | [diff] [blame] | 26 | #include <stdlib.h> /* free() */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 27 | #include "libbb.h" |
| 28 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 29 | #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 | */ |
| 42 | int 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 Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 56 | if (followLinks) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 57 | status = stat(fileName, &statbuf); |
| 58 | else |
| 59 | status = lstat(fileName, &statbuf); |
| 60 | |
| 61 | if (status < 0) { |
| 62 | #ifdef DEBUG_RECURS_ACTION |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 63 | bb_error_msg("status=%d followLinks=%d TRUE=%d", |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 64 | status, followLinks, TRUE); |
| 65 | #endif |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 66 | bb_perror_msg("%s", fileName); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 67 | return FALSE; |
| 68 | } |
| 69 | |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 70 | if (! followLinks && (S_ISLNK(statbuf.st_mode))) { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 71 | if (fileAction == NULL) |
| 72 | return TRUE; |
| 73 | else |
| 74 | return fileAction(fileName, &statbuf, userData); |
| 75 | } |
| 76 | |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 77 | if (! recurse) { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 78 | if (S_ISDIR(statbuf.st_mode)) { |
| 79 | if (dirAction != NULL) |
| 80 | return (dirAction(fileName, &statbuf, userData)); |
| 81 | else |
| 82 | return TRUE; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | if (S_ISDIR(statbuf.st_mode)) { |
| 87 | DIR *dir; |
| 88 | |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 89 | if (dirAction != NULL && ! depthFirst) { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 90 | status = dirAction(fileName, &statbuf, userData); |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 91 | if (! status) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 92 | bb_perror_msg("%s", fileName); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 93 | return FALSE; |
| 94 | } else if (status == SKIP) |
| 95 | return TRUE; |
| 96 | } |
| 97 | dir = opendir(fileName); |
| 98 | if (!dir) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 99 | bb_perror_msg("%s", fileName); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 100 | return FALSE; |
| 101 | } |
| 102 | status = TRUE; |
| 103 | while ((next = readdir(dir)) != NULL) { |
Eric Andersen | fd40294 | 2001-04-10 17:53:49 +0000 | [diff] [blame] | 104 | char *nextFile; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 105 | |
Glenn L McGrath | 393183d | 2003-05-26 14:07:50 +0000 | [diff] [blame] | 106 | nextFile = concat_subpath_file(fileName, next->d_name); |
| 107 | if(nextFile == NULL) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 108 | continue; |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 109 | if (! recursive_action(nextFile, TRUE, followLinks, depthFirst, |
| 110 | fileAction, dirAction, userData)) { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 111 | status = FALSE; |
| 112 | } |
Eric Andersen | fd40294 | 2001-04-10 17:53:49 +0000 | [diff] [blame] | 113 | free(nextFile); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 114 | } |
| 115 | closedir(dir); |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 116 | if (dirAction != NULL && depthFirst) { |
| 117 | if (! dirAction(fileName, &statbuf, userData)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 118 | bb_perror_msg("%s", fileName); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 119 | return FALSE; |
| 120 | } |
| 121 | } |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 122 | if (! status) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 123 | return FALSE; |
| 124 | } else { |
| 125 | if (fileAction == NULL) |
| 126 | return TRUE; |
| 127 | else |
| 128 | return fileAction(fileName, &statbuf, userData); |
| 129 | } |
| 130 | return TRUE; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | /* END CODE */ |
| 135 | /* |
| 136 | Local Variables: |
| 137 | c-file-style: "linux" |
| 138 | c-basic-offset: 4 |
| 139 | tab-width: 4 |
| 140 | End: |
| 141 | */ |