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 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 14 | /* |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 15 | * Walk down all the directories under the specified |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 16 | * location, and do something (something specified |
| 17 | * by the fileAction and dirAction function pointers). |
| 18 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 19 | * Unfortunately, while nftw(3) could replace this and reduce |
| 20 | * 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] | 21 | * and so isn't sufficiently portable to take over since glibc2.1 |
| 22 | * is so stinking huge. |
| 23 | */ |
Denis Vlasenko | 3b8fc1c | 2006-10-27 17:59:14 +0000 | [diff] [blame] | 24 | |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 25 | static int true_action(const char *fileName, struct stat *statbuf, |
| 26 | void* userData, int depth) |
Denis Vlasenko | 3b8fc1c | 2006-10-27 17:59:14 +0000 | [diff] [blame] | 27 | { |
| 28 | return TRUE; |
| 29 | } |
| 30 | |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 31 | /* fileAction return value of 0 on any file in directory will make |
| 32 | * recursive_action() return 0, but it doesn't stop directory traversal |
| 33 | * (fileAction/dirAction will be called on each file). |
| 34 | * |
| 35 | * if !depthFirst, dirAction return value of 0 (FALSE) or 2 (SKIP) |
| 36 | * prevents recursion into that directory, instead |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 37 | * recursive_action() returns 0 (if FALSE) or 1 (if SKIP). |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 38 | * |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 39 | * followLinks=0/1 differs mainly in handling of links to dirs. |
| 40 | * 0: lstat(statbuf). Calls fileAction on link name even if points to dir. |
| 41 | * 1: stat(statbuf). Calls dirAction and optionally recurse on link to dir. |
| 42 | */ |
| 43 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 44 | int recursive_action(const char *fileName, |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 45 | unsigned flags, |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 46 | int (*fileAction)(const char *fileName, struct stat *statbuf, void* userData, int depth), |
| 47 | int (*dirAction)(const char *fileName, struct stat *statbuf, void* userData, int depth), |
| 48 | void* userData, |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 49 | const unsigned depth) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 50 | { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 51 | struct stat statbuf; |
Denis Vlasenko | 3b8fc1c | 2006-10-27 17:59:14 +0000 | [diff] [blame] | 52 | int status; |
| 53 | DIR *dir; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 54 | struct dirent *next; |
| 55 | |
Denis Vlasenko | 3b8fc1c | 2006-10-27 17:59:14 +0000 | [diff] [blame] | 56 | if (!fileAction) fileAction = true_action; |
| 57 | if (!dirAction) dirAction = true_action; |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 58 | status = (flags & action_followLinks ? stat : lstat)(fileName, &statbuf); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 59 | |
| 60 | if (status < 0) { |
| 61 | #ifdef DEBUG_RECURS_ACTION |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 62 | bb_error_msg("status=%d followLinks=%d TRUE=%d", |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 63 | status, flags & action_followLinks, TRUE); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 64 | #endif |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 65 | goto done_nak_warn; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 68 | /* If S_ISLNK(m), then we know that !S_ISDIR(m). |
| 69 | * Then we can skip checking first part: if it is true, then |
| 70 | * (!dir) is also true! */ |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 71 | if ( /* (!(flags & action_followLinks) && S_ISLNK(statbuf.st_mode)) || */ |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 72 | !S_ISDIR(statbuf.st_mode) |
| 73 | ) { |
| 74 | return fileAction(fileName, &statbuf, userData, depth); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 77 | /* It's a directory (or a link to one, and followLinks is set) */ |
| 78 | |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 79 | if (!(flags & action_recurse)) { |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 80 | return dirAction(fileName, &statbuf, userData, depth); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 83 | if (!(flags & action_depthFirst)) { |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 84 | status = dirAction(fileName, &statbuf, userData, depth); |
Denis Vlasenko | 3b8fc1c | 2006-10-27 17:59:14 +0000 | [diff] [blame] | 85 | if (!status) { |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 86 | goto done_nak_warn; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 87 | } |
Denis Vlasenko | 3b8fc1c | 2006-10-27 17:59:14 +0000 | [diff] [blame] | 88 | if (status == SKIP) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 89 | return TRUE; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 90 | } |
Denis Vlasenko | 3b8fc1c | 2006-10-27 17:59:14 +0000 | [diff] [blame] | 91 | |
| 92 | dir = opendir(fileName); |
| 93 | if (!dir) { |
Denis Vlasenko | 5e2db5e | 2006-12-12 23:46:31 +0000 | [diff] [blame] | 94 | /* findutils-4.1.20 reports this */ |
| 95 | /* (i.e. it doesn't silently return with exit code 1) */ |
| 96 | /* To trigger: "find -exec rm -rf {} \;" */ |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 97 | goto done_nak_warn; |
Denis Vlasenko | 3b8fc1c | 2006-10-27 17:59:14 +0000 | [diff] [blame] | 98 | } |
| 99 | status = TRUE; |
| 100 | while ((next = readdir(dir)) != NULL) { |
| 101 | char *nextFile; |
| 102 | |
| 103 | nextFile = concat_subpath_file(fileName, next->d_name); |
| 104 | if (nextFile == NULL) |
| 105 | continue; |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 106 | /* now descend into it, forcing recursion. */ |
| 107 | if (!recursive_action(nextFile, flags | action_recurse, |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 108 | fileAction, dirAction, userData, depth+1)) { |
Denis Vlasenko | 3b8fc1c | 2006-10-27 17:59:14 +0000 | [diff] [blame] | 109 | status = FALSE; |
| 110 | } |
| 111 | free(nextFile); |
| 112 | } |
| 113 | closedir(dir); |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 114 | if (flags & action_depthFirst && |
| 115 | !dirAction(fileName, &statbuf, userData, depth)) { |
| 116 | goto done_nak_warn; |
Denis Vlasenko | 3b8fc1c | 2006-10-27 17:59:14 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | if (!status) |
| 120 | return FALSE; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 121 | return TRUE; |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 122 | done_nak_warn: |
| 123 | bb_perror_msg("%s", fileName); |
| 124 | return FALSE; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 125 | } |