[821] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /*
|
---|
| 3 | * Utility routines.
|
---|
| 4 | *
|
---|
| 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
---|
| 6 | *
|
---|
| 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | #include "libbb.h"
|
---|
| 11 |
|
---|
| 12 | #undef DEBUG_RECURS_ACTION
|
---|
| 13 |
|
---|
| 14 | /*
|
---|
| 15 | * Walk down all the directories under the specified
|
---|
| 16 | * location, and do something (something specified
|
---|
| 17 | * by the fileAction and dirAction function pointers).
|
---|
| 18 | *
|
---|
| 19 | * Unfortunately, while nftw(3) could replace this and reduce
|
---|
| 20 | * code size a bit, nftw() wasn't supported before GNU libc 2.1,
|
---|
| 21 | * and so isn't sufficiently portable to take over since glibc2.1
|
---|
| 22 | * is so stinking huge.
|
---|
| 23 | */
|
---|
[1770] | 24 |
|
---|
| 25 | static int true_action(const char *fileName, struct stat *statbuf,
|
---|
| 26 | void* userData, int depth)
|
---|
| 27 | {
|
---|
| 28 | return TRUE;
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 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
|
---|
| 37 | * recursive_action() returns 0 (if FALSE) or 1 (if SKIP).
|
---|
| 38 | *
|
---|
| 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 |
|
---|
[821] | 44 | int recursive_action(const char *fileName,
|
---|
[1770] | 45 | unsigned flags,
|
---|
| 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,
|
---|
| 49 | unsigned depth)
|
---|
[821] | 50 | {
|
---|
[1770] | 51 | struct stat statbuf;
|
---|
[821] | 52 | int status;
|
---|
[1770] | 53 | DIR *dir;
|
---|
[821] | 54 | struct dirent *next;
|
---|
| 55 |
|
---|
[1770] | 56 | if (!fileAction) fileAction = true_action;
|
---|
| 57 | if (!dirAction) dirAction = true_action;
|
---|
[821] | 58 |
|
---|
[1770] | 59 | status = ACTION_FOLLOWLINKS; /* hijack a variable for bitmask... */
|
---|
| 60 | if (!depth) status = ACTION_FOLLOWLINKS | ACTION_FOLLOWLINKS_L0;
|
---|
| 61 | status = ((flags & status) ? stat : lstat)(fileName, &statbuf);
|
---|
[821] | 62 | if (status < 0) {
|
---|
| 63 | #ifdef DEBUG_RECURS_ACTION
|
---|
[1770] | 64 | bb_error_msg("status=%d flags=%x", status, flags);
|
---|
[821] | 65 | #endif
|
---|
[1770] | 66 | goto done_nak_warn;
|
---|
[821] | 67 | }
|
---|
| 68 |
|
---|
[1770] | 69 | /* If S_ISLNK(m), then we know that !S_ISDIR(m).
|
---|
| 70 | * Then we can skip checking first part: if it is true, then
|
---|
| 71 | * (!dir) is also true! */
|
---|
| 72 | if ( /* (!(flags & ACTION_FOLLOWLINKS) && S_ISLNK(statbuf.st_mode)) || */
|
---|
| 73 | !S_ISDIR(statbuf.st_mode)
|
---|
| 74 | ) {
|
---|
| 75 | return fileAction(fileName, &statbuf, userData, depth);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | /* It's a directory (or a link to one, and followLinks is set) */
|
---|
| 79 |
|
---|
| 80 | if (!(flags & ACTION_RECURSE)) {
|
---|
| 81 | return dirAction(fileName, &statbuf, userData, depth);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | if (!(flags & ACTION_DEPTHFIRST)) {
|
---|
| 85 | status = dirAction(fileName, &statbuf, userData, depth);
|
---|
| 86 | if (!status)
|
---|
| 87 | goto done_nak_warn;
|
---|
| 88 | if (status == SKIP)
|
---|
[821] | 89 | return TRUE;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[1770] | 92 | dir = opendir(fileName);
|
---|
| 93 | if (!dir) {
|
---|
| 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 {} \;" */
|
---|
| 97 | goto done_nak_warn;
|
---|
[821] | 98 | }
|
---|
[1770] | 99 | status = TRUE;
|
---|
| 100 | while ((next = readdir(dir)) != NULL) {
|
---|
| 101 | char *nextFile;
|
---|
[821] | 102 |
|
---|
[1770] | 103 | nextFile = concat_subpath_file(fileName, next->d_name);
|
---|
| 104 | if (nextFile == NULL)
|
---|
| 105 | continue;
|
---|
| 106 | /* now descend into it (NB: ACTION_RECURSE is set in flags) */
|
---|
| 107 | if (!recursive_action(nextFile, flags, fileAction, dirAction, userData, depth+1))
|
---|
| 108 | status = FALSE;
|
---|
| 109 | free(nextFile);
|
---|
| 110 | }
|
---|
| 111 | closedir(dir);
|
---|
[821] | 112 |
|
---|
[1770] | 113 | if (flags & ACTION_DEPTHFIRST) {
|
---|
| 114 | if (!dirAction(fileName, &statbuf, userData, depth))
|
---|
| 115 | goto done_nak_warn;
|
---|
| 116 | }
|
---|
[821] | 117 |
|
---|
[1770] | 118 | if (!status)
|
---|
| 119 | return FALSE;
|
---|
[821] | 120 | return TRUE;
|
---|
[1770] | 121 |
|
---|
| 122 | done_nak_warn:
|
---|
| 123 | bb_perror_msg("%s", fileName);
|
---|
| 124 | return FALSE;
|
---|
[821] | 125 | }
|
---|