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 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
| 11 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 12 | #undef DEBUG_RECURS_ACTION |
| 13 | |
| 14 | |
| 15 | /* |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 16 | * Walk down all the directories under the specified |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 17 | * location, and do something (something specified |
| 18 | * by the fileAction and dirAction function pointers). |
| 19 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 20 | * Unfortunately, while nftw(3) could replace this and reduce |
| 21 | * 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] | 22 | * and so isn't sufficiently portable to take over since glibc2.1 |
| 23 | * is so stinking huge. |
| 24 | */ |
| 25 | int 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 Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 39 | if (followLinks) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 40 | status = stat(fileName, &statbuf); |
| 41 | else |
| 42 | status = lstat(fileName, &statbuf); |
| 43 | |
| 44 | if (status < 0) { |
| 45 | #ifdef DEBUG_RECURS_ACTION |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 46 | bb_error_msg("status=%d followLinks=%d TRUE=%d", |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 47 | status, followLinks, TRUE); |
| 48 | #endif |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 49 | bb_perror_msg("%s", fileName); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 50 | return FALSE; |
| 51 | } |
| 52 | |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 53 | if (! followLinks && (S_ISLNK(statbuf.st_mode))) { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 54 | if (fileAction == NULL) |
| 55 | return TRUE; |
| 56 | else |
| 57 | return fileAction(fileName, &statbuf, userData); |
| 58 | } |
| 59 | |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 60 | if (! recurse) { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 61 | 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 Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 72 | if (dirAction != NULL && ! depthFirst) { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 73 | status = dirAction(fileName, &statbuf, userData); |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 74 | if (! status) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 75 | bb_perror_msg("%s", fileName); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 76 | return FALSE; |
| 77 | } else if (status == SKIP) |
| 78 | return TRUE; |
| 79 | } |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 80 | dir = opendir(fileName); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 81 | if (!dir) { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 82 | return FALSE; |
| 83 | } |
| 84 | status = TRUE; |
Eric Andersen | 66a56aa | 2004-04-07 17:59:04 +0000 | [diff] [blame] | 85 | while ((next = readdir(dir)) != NULL) { |
Eric Andersen | fd40294 | 2001-04-10 17:53:49 +0000 | [diff] [blame] | 86 | char *nextFile; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 87 | |
Glenn L McGrath | 393183d | 2003-05-26 14:07:50 +0000 | [diff] [blame] | 88 | nextFile = concat_subpath_file(fileName, next->d_name); |
| 89 | if(nextFile == NULL) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 90 | continue; |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 91 | if (! recursive_action(nextFile, TRUE, followLinks, depthFirst, |
| 92 | fileAction, dirAction, userData)) { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 93 | status = FALSE; |
| 94 | } |
Eric Andersen | fd40294 | 2001-04-10 17:53:49 +0000 | [diff] [blame] | 95 | free(nextFile); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 96 | } |
| 97 | closedir(dir); |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 98 | if (dirAction != NULL && depthFirst) { |
| 99 | if (! dirAction(fileName, &statbuf, userData)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 100 | bb_perror_msg("%s", fileName); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 101 | return FALSE; |
| 102 | } |
| 103 | } |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 104 | if (! status) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 105 | return FALSE; |
| 106 | } else { |
| 107 | if (fileAction == NULL) |
| 108 | return TRUE; |
| 109 | else |
| 110 | return fileAction(fileName, &statbuf, userData); |
| 111 | } |
| 112 | return TRUE; |
| 113 | } |