Coverage Report

Created: 2026-12-01 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/lib/recursive_action.c
Line
Count
Source
1
/* vi: set sw=8 ts=8: */
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
#ifdef __BAREBOX__
10
11
#include <common.h>
12
#include <fs.h>
13
#include <linux/stat.h>
14
#include <malloc.h>
15
#include <libbb.h>
16
#include <xfuncs.h>
17
18
/*
19
 * Sorting not required for barebox itself currently, support for it is only added
20
 * for generating reproducible envfs images on the build host.
21
 */
22
14.8k
#define DO_SORT(flags)  0
23
24
#else
25
26
#define DO_SORT(flags)  ((flags) & ACTION_SORT)
27
28
#endif
29
30
#include <linux/list.h>
31
#include <linux/list_sort.h>
32
33
struct dirlist {
34
  char *dirname;
35
  struct list_head list;
36
};
37
38
/*
39
 * Walk down all the directories under the specified
40
 * location, and do something (something specified
41
 * by the fileAction and dirAction function pointers).
42
 *
43
 * Unfortunately, while nftw(3) could replace this and reduce
44
 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
45
 * and so isn't sufficiently portable to take over since glibc2.1
46
 * is so stinking huge.
47
 */
48
49
static int true_action(const char *fileName, struct stat *statbuf,
50
            void* userData, int depth)
51
6.97k
{
52
6.97k
  return 1;
53
6.97k
}
54
55
static int cmp_dirlist(void *priv, struct list_head *a, struct list_head *b)
56
0
{
57
0
  struct dirlist *ra, *rb;
58
0
59
0
  if (a == b)
60
0
    return 0;
61
0
62
0
  ra = list_entry(a, struct dirlist, list);
63
0
  rb = list_entry(b, struct dirlist, list);
64
0
65
0
  return strcmp(ra->dirname, rb->dirname);
66
0
}
67
68
/* fileAction return value of 0 on any file in directory will make
69
 * recursive_action() return 0, but it doesn't stop directory traversal
70
 * (fileAction/dirAction will be called on each file).
71
 *
72
 * if !depthFirst, dirAction return value of 0 (FALSE) or 2 (SKIP)
73
 * prevents recursion into that directory, instead
74
 * recursive_action() returns 0 (if FALSE) or 1 (if SKIP).
75
 *
76
 * followLinks=0/1 differs mainly in handling of links to dirs.
77
 * 0: lstat(statbuf). Calls fileAction on link name even if points to dir.
78
 * 1: stat(statbuf). Calls dirAction and optionally recurse on link to dir.
79
 */
80
81
int recursive_action(const char *fileName,
82
    unsigned flags,
83
    int (*fileAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
84
    int (*dirAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
85
    void* userData,
86
    const unsigned depth)
87
11.0k
{
88
11.0k
  struct stat statbuf;
89
11.0k
  unsigned follow;
90
11.0k
  int status;
91
11.0k
  DIR *dir;
92
11.0k
  struct dirent *next;
93
11.0k
  struct dirlist *entry, *entry_tmp;
94
11.0k
  LIST_HEAD(dirs);
95
96
11.0k
  if (!fileAction) fileAction = true_action;
97
11.0k
  if (!dirAction) dirAction = true_action;
98
99
11.0k
  follow = ACTION_FOLLOWLINKS;
100
11.0k
  if (depth == 0)
101
224
    follow = ACTION_FOLLOWLINKS;
102
11.0k
  follow &= flags;
103
11.0k
  status = (follow ? stat : lstat)(fileName, &statbuf);
104
11.0k
  if (status < 0) {
105
#ifdef DEBUG_RECURS_ACTION
106
    bb_error_msg("status=%d followLinks=%d TRUE=%d",
107
        status, flags & ACTION_FOLLOWLINKS, TRUE);
108
#endif
109
0
    goto done_nak_warn;
110
0
  }
111
112
  /* If S_ISLNK(m), then we know that !S_ISDIR(m).
113
   * Then we can skip checking first part: if it is true, then
114
   * (!dir) is also true! */
115
11.0k
  if ( /* (!(flags & ACTION_FOLLOWLINKS) && S_ISLNK(statbuf.st_mode)) || */
116
11.0k
   !S_ISDIR(statbuf.st_mode)
117
11.0k
  ) {
118
7.10k
    return fileAction(fileName, &statbuf, userData, depth);
119
7.10k
  }
120
121
  /* It's a directory (or a link to one, and followLinks is set) */
122
123
3.96k
  if (!(flags & ACTION_RECURSE)) {
124
0
    return dirAction(fileName, &statbuf, userData, depth);
125
0
  }
126
127
3.96k
  if (!(flags & ACTION_DEPTHFIRST)) {
128
0
    status = dirAction(fileName, &statbuf, userData, depth);
129
0
    if (!status) {
130
0
      goto done_nak_warn;
131
0
    }
132
0
    if (status == 2)
133
0
      return 1;
134
0
  }
135
136
3.96k
  dir = opendir(fileName);
137
3.96k
  if (!dir) {
138
    /* findutils-4.1.20 reports this */
139
    /* (i.e. it doesn't silently return with exit code 1) */
140
    /* To trigger: "find -exec rm -rf {} \;" */
141
0
    goto done_nak_warn;
142
0
  }
143
144
3.96k
  status = 1;
145
22.7k
  while ((next = readdir(dir)) != NULL) {
146
18.7k
    char *nextFile = concat_subpath_file(fileName, next->d_name);
147
18.7k
    if (nextFile == NULL)
148
7.93k
      continue;
149
150
10.8k
    if (DO_SORT(flags)) {
151
0
      struct dirlist *e = xmalloc(sizeof(*e));
152
0
      e->dirname = nextFile;
153
0
      list_add(&e->list, &dirs);
154
10.8k
    } else {
155
      /* descend into it, forcing recursion. */
156
10.8k
      if (!recursive_action(nextFile, flags | ACTION_RECURSE,
157
10.8k
            fileAction, dirAction, userData, depth+1)) {
158
0
        status = 0;
159
0
      }
160
10.8k
      free(nextFile);
161
10.8k
    }
162
10.8k
  }
163
164
3.96k
  if (DO_SORT(flags)) {
165
0
    list_sort(NULL, &dirs, &cmp_dirlist);
166
167
0
    list_for_each_entry_safe(entry, entry_tmp, &dirs, list){
168
      /* descend into it, forcing recursion. */
169
0
      if (!recursive_action(entry->dirname, flags | ACTION_RECURSE,
170
0
            fileAction, dirAction, userData, depth+1)) {
171
0
        status = 0;
172
0
      }
173
174
0
      list_del(&entry->list);
175
0
      free(entry->dirname);
176
0
    }
177
0
  }
178
3.96k
  closedir(dir);
179
3.96k
  if ((flags & ACTION_DEPTHFIRST) &&
180
3.96k
    !dirAction(fileName, &statbuf, userData, depth)) {
181
0
      goto done_nak_warn;
182
0
  }
183
184
3.96k
  if (!status)
185
0
    return 0;
186
3.96k
  return 1;
187
0
done_nak_warn:
188
0
  return 0;
189
3.96k
}
190
191
#ifdef __BAREBOX__
192
EXPORT_SYMBOL(recursive_action);
193
#endif