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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 8 | */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 9 | #include "libbb.h" |
| 10 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 11 | #undef DEBUG_RECURS_ACTION |
| 12 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 13 | /* |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 14 | * Walk down all the directories under the specified |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 15 | * location, and do something (something specified |
| 16 | * by the fileAction and dirAction function pointers). |
| 17 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 18 | * Unfortunately, while nftw(3) could replace this and reduce |
| 19 | * 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] | 20 | * and so isn't sufficiently portable to take over since glibc2.1 |
| 21 | * is so stinking huge. |
| 22 | */ |
Denis Vlasenko | 3b8fc1c | 2006-10-27 17:59:14 +0000 | [diff] [blame] | 23 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 24 | static int FAST_FUNC true_action(const char *fileName UNUSED_PARAM, |
| 25 | struct stat *statbuf UNUSED_PARAM, |
| 26 | void* userData UNUSED_PARAM, |
| 27 | int depth UNUSED_PARAM) |
Denis Vlasenko | 3b8fc1c | 2006-10-27 17:59:14 +0000 | [diff] [blame] | 28 | { |
| 29 | return TRUE; |
| 30 | } |
| 31 | |
Denys Vlasenko | 4f0b540 | 2017-04-06 15:22:24 +0200 | [diff] [blame] | 32 | /* fileName is (l)stat'ed (depending on ACTION_FOLLOWLINKS[_L0]). |
| 33 | * |
| 34 | * If it is a file: fileAction in run on it, its return value is returned. |
| 35 | * |
| 36 | * In case we are in a recursive invocation (see below): |
| 37 | * normally, fileAction should return 1 (TRUE) to indicate that |
| 38 | * everything is okay and processing should continue. |
| 39 | * fileAction return value of 0 (FALSE) on any file in directory will make |
| 40 | * recursive_action() also return 0, but it doesn't stop directory traversal |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 41 | * (fileAction/dirAction will be called on each file). |
| 42 | * |
Denys Vlasenko | 4f0b540 | 2017-04-06 15:22:24 +0200 | [diff] [blame] | 43 | * [TODO: maybe introduce -1 to mean "stop traversal NOW and return"] |
| 44 | * |
| 45 | * If it is a directory: |
| 46 | * |
| 47 | * If !ACTION_RECURSE, dirAction is called and its |
Denis Vlasenko | 2649f21 | 2008-06-26 03:26:57 +0000 | [diff] [blame] | 48 | * return value is returned from recursive_action(). No recursion. |
| 49 | * |
Denys Vlasenko | 4f0b540 | 2017-04-06 15:22:24 +0200 | [diff] [blame] | 50 | * If ACTION_RECURSE, directory is opened, and recursive_action() is called |
| 51 | * on each file/subdirectory. |
Denis Vlasenko | 2649f21 | 2008-06-26 03:26:57 +0000 | [diff] [blame] | 52 | * If any one of these calls returns 0, current recursive_action() returns 0. |
| 53 | * |
Denys Vlasenko | 4f0b540 | 2017-04-06 15:22:24 +0200 | [diff] [blame] | 54 | * If !ACTION_DEPTHFIRST, dirAction is called before recurse. |
| 55 | * Return value of 0 (FALSE) is an error: prevents recursion, |
| 56 | * the warning is printed (unless ACTION_QUIET) and recursive_action() returns 0. |
| 57 | * Return value of 2 (SKIP) prevents recursion, instead recursive_action() |
| 58 | * returns 1 (TRUE, no error). |
| 59 | * |
Denis Vlasenko | 2649f21 | 2008-06-26 03:26:57 +0000 | [diff] [blame] | 60 | * If ACTION_DEPTHFIRST, dirAction is called after recurse. |
| 61 | * If it returns 0, the warning is printed and recursive_action() returns 0. |
| 62 | * |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 63 | * ACTION_FOLLOWLINKS mainly controls handling of links to dirs. |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 64 | * 0: lstat(statbuf). Calls fileAction on link name even if points to dir. |
| 65 | * 1: stat(statbuf). Calls dirAction and optionally recurse on link to dir. |
| 66 | */ |
| 67 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 68 | int FAST_FUNC recursive_action(const char *fileName, |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 69 | unsigned flags, |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 70 | int FAST_FUNC (*fileAction)(const char *fileName, struct stat *statbuf, void* userData, int depth), |
| 71 | int FAST_FUNC (*dirAction)(const char *fileName, struct stat *statbuf, void* userData, int depth), |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 72 | void* userData, |
Denis Vlasenko | d166f83 | 2007-07-05 00:12:55 +0000 | [diff] [blame] | 73 | unsigned depth) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 74 | { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 75 | struct stat statbuf; |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 76 | unsigned follow; |
Denis Vlasenko | 3b8fc1c | 2006-10-27 17:59:14 +0000 | [diff] [blame] | 77 | int status; |
| 78 | DIR *dir; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 79 | struct dirent *next; |
| 80 | |
Denis Vlasenko | 3b8fc1c | 2006-10-27 17:59:14 +0000 | [diff] [blame] | 81 | if (!fileAction) fileAction = true_action; |
| 82 | if (!dirAction) dirAction = true_action; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 83 | |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 84 | follow = ACTION_FOLLOWLINKS; |
| 85 | if (depth == 0) |
| 86 | follow = ACTION_FOLLOWLINKS | ACTION_FOLLOWLINKS_L0; |
| 87 | follow &= flags; |
| 88 | status = (follow ? stat : lstat)(fileName, &statbuf); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 89 | if (status < 0) { |
| 90 | #ifdef DEBUG_RECURS_ACTION |
Denis Vlasenko | d166f83 | 2007-07-05 00:12:55 +0000 | [diff] [blame] | 91 | bb_error_msg("status=%d flags=%x", status, flags); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 92 | #endif |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 93 | if ((flags & ACTION_DANGLING_OK) |
| 94 | && errno == ENOENT |
| 95 | && lstat(fileName, &statbuf) == 0 |
| 96 | ) { |
| 97 | /* Dangling link */ |
| 98 | return fileAction(fileName, &statbuf, userData, depth); |
| 99 | } |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 100 | goto done_nak_warn; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 103 | /* If S_ISLNK(m), then we know that !S_ISDIR(m). |
| 104 | * Then we can skip checking first part: if it is true, then |
| 105 | * (!dir) is also true! */ |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 106 | if ( /* (!(flags & ACTION_FOLLOWLINKS) && S_ISLNK(statbuf.st_mode)) || */ |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 107 | !S_ISDIR(statbuf.st_mode) |
| 108 | ) { |
| 109 | return fileAction(fileName, &statbuf, userData, depth); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 112 | /* It's a directory (or a link to one, and followLinks is set) */ |
| 113 | |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 114 | if (!(flags & ACTION_RECURSE)) { |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 115 | return dirAction(fileName, &statbuf, userData, depth); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 118 | if (!(flags & ACTION_DEPTHFIRST)) { |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 119 | status = dirAction(fileName, &statbuf, userData, depth); |
Denys Vlasenko | 4f0b540 | 2017-04-06 15:22:24 +0200 | [diff] [blame] | 120 | if (status == FALSE) |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 121 | goto done_nak_warn; |
Denis Vlasenko | 3b8fc1c | 2006-10-27 17:59:14 +0000 | [diff] [blame] | 122 | if (status == SKIP) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 123 | return TRUE; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 124 | } |
Denis Vlasenko | 3b8fc1c | 2006-10-27 17:59:14 +0000 | [diff] [blame] | 125 | |
| 126 | dir = opendir(fileName); |
| 127 | if (!dir) { |
Denis Vlasenko | 5e2db5e | 2006-12-12 23:46:31 +0000 | [diff] [blame] | 128 | /* findutils-4.1.20 reports this */ |
| 129 | /* (i.e. it doesn't silently return with exit code 1) */ |
| 130 | /* To trigger: "find -exec rm -rf {} \;" */ |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 131 | goto done_nak_warn; |
Denis Vlasenko | 3b8fc1c | 2006-10-27 17:59:14 +0000 | [diff] [blame] | 132 | } |
| 133 | status = TRUE; |
| 134 | while ((next = readdir(dir)) != NULL) { |
| 135 | char *nextFile; |
Denys Vlasenko | 4f0b540 | 2017-04-06 15:22:24 +0200 | [diff] [blame] | 136 | int s; |
Denis Vlasenko | 3b8fc1c | 2006-10-27 17:59:14 +0000 | [diff] [blame] | 137 | |
| 138 | nextFile = concat_subpath_file(fileName, next->d_name); |
| 139 | if (nextFile == NULL) |
| 140 | continue; |
Denys Vlasenko | 4f0b540 | 2017-04-06 15:22:24 +0200 | [diff] [blame] | 141 | |
Denis Vlasenko | 2649f21 | 2008-06-26 03:26:57 +0000 | [diff] [blame] | 142 | /* process every file (NB: ACTION_RECURSE is set in flags) */ |
Denys Vlasenko | 4f0b540 | 2017-04-06 15:22:24 +0200 | [diff] [blame] | 143 | s = recursive_action(nextFile, flags, fileAction, dirAction, |
| 144 | userData, depth + 1); |
| 145 | if (s == FALSE) |
Denis Vlasenko | a8a3b49 | 2008-07-04 10:29:30 +0000 | [diff] [blame] | 146 | status = FALSE; |
Denis Vlasenko | 3b8fc1c | 2006-10-27 17:59:14 +0000 | [diff] [blame] | 147 | free(nextFile); |
Denys Vlasenko | 4f0b540 | 2017-04-06 15:22:24 +0200 | [diff] [blame] | 148 | //#define RECURSE_RESULT_ABORT -1 |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 149 | // if (s == RECURSE_RESULT_ABORT) { |
| 150 | // closedir(dir); |
| 151 | // return s; |
| 152 | // } |
Denis Vlasenko | 3b8fc1c | 2006-10-27 17:59:14 +0000 | [diff] [blame] | 153 | } |
| 154 | closedir(dir); |
Denis Vlasenko | d166f83 | 2007-07-05 00:12:55 +0000 | [diff] [blame] | 155 | |
| 156 | if (flags & ACTION_DEPTHFIRST) { |
| 157 | if (!dirAction(fileName, &statbuf, userData, depth)) |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 158 | goto done_nak_warn; |
Denis Vlasenko | 3b8fc1c | 2006-10-27 17:59:14 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Denis Vlasenko | 2649f21 | 2008-06-26 03:26:57 +0000 | [diff] [blame] | 161 | return status; |
Denis Vlasenko | d166f83 | 2007-07-05 00:12:55 +0000 | [diff] [blame] | 162 | |
| 163 | done_nak_warn: |
Denis Vlasenko | 6e69e42 | 2008-07-27 12:10:07 +0000 | [diff] [blame] | 164 | if (!(flags & ACTION_QUIET)) |
| 165 | bb_simple_perror_msg(fileName); |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 166 | return FALSE; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 167 | } |