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