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 <stdio.h>
|
---|
11 | #include <string.h>
|
---|
12 | #include <dirent.h>
|
---|
13 | #include <sys/stat.h>
|
---|
14 | #include <stdlib.h> /* free() */
|
---|
15 | #include "libbb.h"
|
---|
16 |
|
---|
17 | #undef DEBUG_RECURS_ACTION
|
---|
18 |
|
---|
19 |
|
---|
20 | /*
|
---|
21 | * Walk down all the directories under the specified
|
---|
22 | * location, and do something (something specified
|
---|
23 | * by the fileAction and dirAction function pointers).
|
---|
24 | *
|
---|
25 | * Unfortunately, while nftw(3) could replace this and reduce
|
---|
26 | * code size a bit, nftw() wasn't supported before GNU libc 2.1,
|
---|
27 | * and so isn't sufficiently portable to take over since glibc2.1
|
---|
28 | * is so stinking huge.
|
---|
29 | */
|
---|
30 | int recursive_action(const char *fileName,
|
---|
31 | int recurse, int followLinks, int depthFirst,
|
---|
32 | int (*fileAction) (const char *fileName,
|
---|
33 | struct stat * statbuf,
|
---|
34 | void* userData),
|
---|
35 | int (*dirAction) (const char *fileName,
|
---|
36 | struct stat * statbuf,
|
---|
37 | void* userData),
|
---|
38 | void* userData)
|
---|
39 | {
|
---|
40 | int status;
|
---|
41 | struct stat statbuf;
|
---|
42 | struct dirent *next;
|
---|
43 |
|
---|
44 | if (followLinks)
|
---|
45 | status = stat(fileName, &statbuf);
|
---|
46 | else
|
---|
47 | status = lstat(fileName, &statbuf);
|
---|
48 |
|
---|
49 | if (status < 0) {
|
---|
50 | #ifdef DEBUG_RECURS_ACTION
|
---|
51 | bb_error_msg("status=%d followLinks=%d TRUE=%d",
|
---|
52 | status, followLinks, TRUE);
|
---|
53 | #endif
|
---|
54 | bb_perror_msg("%s", fileName);
|
---|
55 | return FALSE;
|
---|
56 | }
|
---|
57 |
|
---|
58 | if (! followLinks && (S_ISLNK(statbuf.st_mode))) {
|
---|
59 | if (fileAction == NULL)
|
---|
60 | return TRUE;
|
---|
61 | else
|
---|
62 | return fileAction(fileName, &statbuf, userData);
|
---|
63 | }
|
---|
64 |
|
---|
65 | if (! recurse) {
|
---|
66 | if (S_ISDIR(statbuf.st_mode)) {
|
---|
67 | if (dirAction != NULL)
|
---|
68 | return (dirAction(fileName, &statbuf, userData));
|
---|
69 | else
|
---|
70 | return TRUE;
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | if (S_ISDIR(statbuf.st_mode)) {
|
---|
75 | DIR *dir;
|
---|
76 |
|
---|
77 | if (dirAction != NULL && ! depthFirst) {
|
---|
78 | status = dirAction(fileName, &statbuf, userData);
|
---|
79 | if (! status) {
|
---|
80 | bb_perror_msg("%s", fileName);
|
---|
81 | return FALSE;
|
---|
82 | } else if (status == SKIP)
|
---|
83 | return TRUE;
|
---|
84 | }
|
---|
85 | dir = bb_opendir(fileName);
|
---|
86 | if (!dir) {
|
---|
87 | return FALSE;
|
---|
88 | }
|
---|
89 | status = TRUE;
|
---|
90 | while ((next = readdir(dir)) != NULL) {
|
---|
91 | char *nextFile;
|
---|
92 |
|
---|
93 | nextFile = concat_subpath_file(fileName, next->d_name);
|
---|
94 | if(nextFile == NULL)
|
---|
95 | continue;
|
---|
96 | if (! recursive_action(nextFile, TRUE, followLinks, depthFirst,
|
---|
97 | fileAction, dirAction, userData)) {
|
---|
98 | status = FALSE;
|
---|
99 | }
|
---|
100 | free(nextFile);
|
---|
101 | }
|
---|
102 | closedir(dir);
|
---|
103 | if (dirAction != NULL && depthFirst) {
|
---|
104 | if (! dirAction(fileName, &statbuf, userData)) {
|
---|
105 | bb_perror_msg("%s", fileName);
|
---|
106 | return FALSE;
|
---|
107 | }
|
---|
108 | }
|
---|
109 | if (! status)
|
---|
110 | return FALSE;
|
---|
111 | } else {
|
---|
112 | if (fileAction == NULL)
|
---|
113 | return TRUE;
|
---|
114 | else
|
---|
115 | return fileAction(fileName, &statbuf, userData);
|
---|
116 | }
|
---|
117 | return TRUE;
|
---|
118 | }
|
---|