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, |
Denis Vlasenko | 0c45bb23 | 2006-09-09 12:49:03 +0000 | [diff] [blame] | 26 | int recurse, int followLinks, int depthFirst, |
| 27 | int (*fileAction) (const char *fileName, struct stat * statbuf, void* userData), |
| 28 | int (*dirAction) (const char *fileName, struct stat * statbuf, void* userData), |
| 29 | void* userData) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 30 | { |
| 31 | int status; |
| 32 | struct stat statbuf; |
| 33 | struct dirent *next; |
| 34 | |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 35 | if (followLinks) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 36 | status = stat(fileName, &statbuf); |
| 37 | else |
| 38 | status = lstat(fileName, &statbuf); |
| 39 | |
| 40 | if (status < 0) { |
| 41 | #ifdef DEBUG_RECURS_ACTION |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 42 | bb_error_msg("status=%d followLinks=%d TRUE=%d", |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 43 | status, followLinks, TRUE); |
| 44 | #endif |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 45 | bb_perror_msg("%s", fileName); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 46 | return FALSE; |
| 47 | } |
| 48 | |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 49 | if (! followLinks && (S_ISLNK(statbuf.st_mode))) { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 50 | if (fileAction == NULL) |
| 51 | return TRUE; |
| 52 | else |
| 53 | return fileAction(fileName, &statbuf, userData); |
| 54 | } |
| 55 | |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 56 | if (! recurse) { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 57 | if (S_ISDIR(statbuf.st_mode)) { |
| 58 | if (dirAction != NULL) |
| 59 | return (dirAction(fileName, &statbuf, userData)); |
| 60 | else |
| 61 | return TRUE; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | if (S_ISDIR(statbuf.st_mode)) { |
| 66 | DIR *dir; |
| 67 | |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 68 | if (dirAction != NULL && ! depthFirst) { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 69 | status = dirAction(fileName, &statbuf, userData); |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 70 | if (! status) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 71 | bb_perror_msg("%s", fileName); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 72 | return FALSE; |
| 73 | } else if (status == SKIP) |
| 74 | return TRUE; |
| 75 | } |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 76 | dir = opendir(fileName); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 77 | if (!dir) { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 78 | return FALSE; |
| 79 | } |
| 80 | status = TRUE; |
Eric Andersen | 66a56aa | 2004-04-07 17:59:04 +0000 | [diff] [blame] | 81 | while ((next = readdir(dir)) != NULL) { |
Eric Andersen | fd40294 | 2001-04-10 17:53:49 +0000 | [diff] [blame] | 82 | char *nextFile; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 83 | |
Glenn L McGrath | 393183d | 2003-05-26 14:07:50 +0000 | [diff] [blame] | 84 | nextFile = concat_subpath_file(fileName, next->d_name); |
| 85 | if(nextFile == NULL) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 86 | continue; |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 87 | if (! recursive_action(nextFile, TRUE, followLinks, depthFirst, |
| 88 | fileAction, dirAction, userData)) { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 89 | status = FALSE; |
| 90 | } |
Eric Andersen | fd40294 | 2001-04-10 17:53:49 +0000 | [diff] [blame] | 91 | free(nextFile); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 92 | } |
| 93 | closedir(dir); |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 94 | if (dirAction != NULL && depthFirst) { |
| 95 | if (! dirAction(fileName, &statbuf, userData)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 96 | bb_perror_msg("%s", fileName); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 97 | return FALSE; |
| 98 | } |
| 99 | } |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 100 | if (! status) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 101 | return FALSE; |
| 102 | } else { |
| 103 | if (fileAction == NULL) |
| 104 | return TRUE; |
| 105 | else |
| 106 | return fileAction(fileName, &statbuf, userData); |
| 107 | } |
| 108 | return TRUE; |
| 109 | } |