summaryrefslogtreecommitdiffstats
path: root/lib/unlink-recursive.c
blob: f28c6dae5b3cd48a68c053387673ceb9ee3c0f16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <common.h>
#include <libfile.h>
#include <errno.h>
#include <libbb.h>
#include <fs.h>

static char unlink_recursive_failedpath[PATH_MAX];

static int file_action(const char *filename, struct stat *statbuf,
			    void *userdata, int depth)
{
	if (unlink(filename)) {
		strcpy(unlink_recursive_failedpath, filename);
		return 0;
	}

	return 1;
}

static int dir_action(const char *dirname, struct stat *statbuf,
			    void *userdata, int depth)
{
	if (rmdir(dirname)) {
		strcpy(unlink_recursive_failedpath, dirname);
		return 0;
	}

	return 1;
}

int unlink_recursive(const char *path, char **failedpath)
{
	int ret;

	if (failedpath)
		*failedpath = NULL;

	ret = recursive_action(path, ACTION_RECURSE | ACTION_DEPTHFIRST,
			file_action, dir_action, NULL, 0);

	if (!ret && failedpath)
		*failedpath = unlink_recursive_failedpath;

	return ret ? 0 : -errno;
}