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 | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 6 | * |
Bernhard Reutner-Fischer | cb44816 | 2006-04-12 07:35:12 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <stdio.h> |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 11 | #include <string.h> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 12 | #include <dirent.h> |
| 13 | #include <sys/stat.h> |
Eric Andersen | fd40294 | 2001-04-10 17:53:49 +0000 | [diff] [blame] | 14 | #include <stdlib.h> /* free() */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 15 | #include "libbb.h" |
| 16 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 17 | #undef DEBUG_RECURS_ACTION |
| 18 | |
| 19 | |
| 20 | /* |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 21 | * Walk down all the directories under the specified |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 22 | * location, and do something (something specified |
| 23 | * by the fileAction and dirAction function pointers). |
| 24 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 25 | * Unfortunately, while nftw(3) could replace this and reduce |
| 26 | * code size a bit, nftw() wasn't supported before GNU libc 2.1, |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 27 | * and so isn't sufficiently portable to take over since glibc2.1 |
| 28 | * is so stinking huge. |
| 29 | */ |
| 30 | int 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 Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 44 | if (followLinks) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 45 | status = stat(fileName, &statbuf); |
| 46 | else |
| 47 | status = lstat(fileName, &statbuf); |
| 48 | |
| 49 | if (status < 0) { |
| 50 | #ifdef DEBUG_RECURS_ACTION |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 51 | bb_error_msg("status=%d followLinks=%d TRUE=%d", |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 52 | status, followLinks, TRUE); |
| 53 | #endif |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 54 | bb_perror_msg("%s", fileName); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 55 | return FALSE; |
| 56 | } |
| 57 | |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 58 | if (! followLinks && (S_ISLNK(statbuf.st_mode))) { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 59 | if (fileAction == NULL) |
| 60 | return TRUE; |
| 61 | else |
| 62 | return fileAction(fileName, &statbuf, userData); |
| 63 | } |
| 64 | |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 65 | if (! recurse) { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 66 | 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 Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 77 | if (dirAction != NULL && ! depthFirst) { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 78 | status = dirAction(fileName, &statbuf, userData); |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 79 | if (! status) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 80 | bb_perror_msg("%s", fileName); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 81 | return FALSE; |
| 82 | } else if (status == SKIP) |
| 83 | return TRUE; |
| 84 | } |
Bernhard Reutner-Fischer | cb44816 | 2006-04-12 07:35:12 +0000 | [diff] [blame] | 85 | dir = bb_opendir(fileName); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 86 | if (!dir) { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 87 | return FALSE; |
| 88 | } |
| 89 | status = TRUE; |
Eric Andersen | 66a56aa | 2004-04-07 17:59:04 +0000 | [diff] [blame] | 90 | while ((next = readdir(dir)) != NULL) { |
Eric Andersen | fd40294 | 2001-04-10 17:53:49 +0000 | [diff] [blame] | 91 | char *nextFile; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 92 | |
Glenn L McGrath | 393183d | 2003-05-26 14:07:50 +0000 | [diff] [blame] | 93 | nextFile = concat_subpath_file(fileName, next->d_name); |
| 94 | if(nextFile == NULL) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 95 | continue; |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 96 | if (! recursive_action(nextFile, TRUE, followLinks, depthFirst, |
| 97 | fileAction, dirAction, userData)) { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 98 | status = FALSE; |
| 99 | } |
Eric Andersen | fd40294 | 2001-04-10 17:53:49 +0000 | [diff] [blame] | 100 | free(nextFile); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 101 | } |
| 102 | closedir(dir); |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 103 | if (dirAction != NULL && depthFirst) { |
| 104 | if (! dirAction(fileName, &statbuf, userData)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 105 | bb_perror_msg("%s", fileName); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 106 | return FALSE; |
| 107 | } |
| 108 | } |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 109 | if (! status) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 110 | return FALSE; |
| 111 | } else { |
| 112 | if (fileAction == NULL) |
| 113 | return TRUE; |
| 114 | else |
| 115 | return fileAction(fileName, &statbuf, userData); |
| 116 | } |
| 117 | return TRUE; |
| 118 | } |