/src/barebox/lib/unlink-recursive.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-only |
2 | | |
3 | | #include <common.h> |
4 | | #include <libfile.h> |
5 | | #include <errno.h> |
6 | | #include <libbb.h> |
7 | | #include <fs.h> |
8 | | |
9 | | static char unlink_recursive_failedpath[PATH_MAX]; |
10 | | |
11 | | static int file_action(const char *filename, struct stat *statbuf, |
12 | | void *userdata, int depth) |
13 | 120 | { |
14 | 120 | if (unlink(filename)) { |
15 | 0 | strcpy(unlink_recursive_failedpath, filename); |
16 | 0 | return 0; |
17 | 0 | } |
18 | | |
19 | 120 | return 1; |
20 | 120 | } |
21 | | |
22 | | static int dir_action(const char *dirname, struct stat *statbuf, |
23 | | void *userdata, int depth) |
24 | 210 | { |
25 | 210 | if (rmdir(dirname)) { |
26 | 0 | strcpy(unlink_recursive_failedpath, dirname); |
27 | 0 | return 0; |
28 | 0 | } |
29 | | |
30 | 210 | return 1; |
31 | 210 | } |
32 | | |
33 | | int unlink_recursive(const char *path, char **failedpath) |
34 | 30 | { |
35 | 30 | int ret; |
36 | | |
37 | 30 | if (failedpath) |
38 | 0 | *failedpath = NULL; |
39 | | |
40 | 30 | ret = recursive_action(path, ACTION_RECURSE | ACTION_DEPTHFIRST, |
41 | 30 | file_action, dir_action, NULL, 0); |
42 | | |
43 | 30 | if (!ret && failedpath) |
44 | 0 | *failedpath = unlink_recursive_failedpath; |
45 | | |
46 | 30 | return ret ? 0 : -errno; |
47 | 30 | } |