Add basic support for mini2440 board to barebox.
[barebox-mini2440.git] / lib / recursive_action.c
blob1ef758df618b7f505898207ab2317adbea148f2e
1 /* vi: set sw=8 ts=8: */
2 /*
3 * Utility routines.
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 */
9 #ifdef __BAREBOX__
11 #include <common.h>
12 #include <fs.h>
13 #include <linux/stat.h>
14 #include <malloc.h>
15 #include <libbb.h>
17 #endif
20 * Walk down all the directories under the specified
21 * location, and do something (something specified
22 * by the fileAction and dirAction function pointers).
24 * Unfortunately, while nftw(3) could replace this and reduce
25 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
26 * and so isn't sufficiently portable to take over since glibc2.1
27 * is so stinking huge.
30 static int true_action(const char *fileName, struct stat *statbuf,
31 void* userData, int depth)
33 return 1;
36 /* fileAction return value of 0 on any file in directory will make
37 * recursive_action() return 0, but it doesn't stop directory traversal
38 * (fileAction/dirAction will be called on each file).
40 * if !depthFirst, dirAction return value of 0 (FALSE) or 2 (SKIP)
41 * prevents recursion into that directory, instead
42 * recursive_action() returns 0 (if FALSE) or 1 (if SKIP).
44 * followLinks=0/1 differs mainly in handling of links to dirs.
45 * 0: lstat(statbuf). Calls fileAction on link name even if points to dir.
46 * 1: stat(statbuf). Calls dirAction and optionally recurse on link to dir.
49 int recursive_action(const char *fileName,
50 unsigned flags,
51 int (*fileAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
52 int (*dirAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
53 void* userData,
54 const unsigned depth)
56 struct stat statbuf;
57 int status;
58 DIR *dir;
59 struct dirent *next;
61 if (!fileAction) fileAction = true_action;
62 if (!dirAction) dirAction = true_action;
63 status = stat(fileName, &statbuf);
65 if (status < 0) {
66 #ifdef DEBUG_RECURS_ACTION
67 bb_error_msg("status=%d followLinks=%d TRUE=%d",
68 status, flags & ACTION_FOLLOWLINKS, TRUE);
69 #endif
70 goto done_nak_warn;
73 /* If S_ISLNK(m), then we know that !S_ISDIR(m).
74 * Then we can skip checking first part: if it is true, then
75 * (!dir) is also true! */
76 if ( /* (!(flags & ACTION_FOLLOWLINKS) && S_ISLNK(statbuf.st_mode)) || */
77 !S_ISDIR(statbuf.st_mode)
78 ) {
79 return fileAction(fileName, &statbuf, userData, depth);
82 /* It's a directory (or a link to one, and followLinks is set) */
84 if (!(flags & ACTION_RECURSE)) {
85 return dirAction(fileName, &statbuf, userData, depth);
88 if (!(flags & ACTION_DEPTHFIRST)) {
89 status = dirAction(fileName, &statbuf, userData, depth);
90 if (!status) {
91 goto done_nak_warn;
93 if (status == 2)
94 return 1;
97 dir = opendir(fileName);
98 if (!dir) {
99 /* findutils-4.1.20 reports this */
100 /* (i.e. it doesn't silently return with exit code 1) */
101 /* To trigger: "find -exec rm -rf {} \;" */
102 goto done_nak_warn;
104 status = 1;
105 while ((next = readdir(dir)) != NULL) {
106 char *nextFile;
108 nextFile = concat_subpath_file(fileName, next->d_name);
109 if (nextFile == NULL)
110 continue;
111 /* now descend into it, forcing recursion. */
112 if (!recursive_action(nextFile, flags | ACTION_RECURSE,
113 fileAction, dirAction, userData, depth+1)) {
114 status = 0;
116 free(nextFile);
118 closedir(dir);
119 if ((flags & ACTION_DEPTHFIRST) &&
120 !dirAction(fileName, &statbuf, userData, depth)) {
121 goto done_nak_warn;
124 if (!status)
125 return 0;
126 return 1;
127 done_nak_warn:
128 printf("%s", fileName);
129 return 0;
132 #ifdef __BAREBOX__
133 EXPORT_SYMBOL(recursive_action);
134 #endif