2012-11-29 19:21:26 +00:00
|
|
|
#include <common.h>
|
2016-04-12 09:20:31 +00:00
|
|
|
#include <libfile.h>
|
2013-07-10 07:27:06 +00:00
|
|
|
#include <errno.h>
|
2012-11-29 19:21:26 +00:00
|
|
|
#include <libbb.h>
|
|
|
|
#include <fs.h>
|
|
|
|
|
|
|
|
static char unlink_recursive_failedpath[PATH_MAX];
|
|
|
|
|
|
|
|
struct data {
|
|
|
|
int error;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int file_action(const char *filename, struct stat *statbuf,
|
|
|
|
void *userdata, int depth)
|
|
|
|
{
|
|
|
|
struct data *data = userdata;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = unlink(filename);
|
|
|
|
if (ret) {
|
|
|
|
strcpy(unlink_recursive_failedpath, filename);
|
|
|
|
data->error = ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dir_action(const char *dirname, struct stat *statbuf,
|
|
|
|
void *userdata, int depth)
|
|
|
|
{
|
|
|
|
struct data *data = userdata;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = rmdir(dirname);
|
|
|
|
if (ret) {
|
|
|
|
strcpy(unlink_recursive_failedpath, dirname);
|
|
|
|
data->error = ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int unlink_recursive(const char *path, char **failedpath)
|
|
|
|
{
|
|
|
|
struct data data = {};
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (failedpath)
|
|
|
|
*failedpath = NULL;
|
|
|
|
|
|
|
|
ret = recursive_action(path, ACTION_RECURSE | ACTION_DEPTHFIRST,
|
|
|
|
file_action, dir_action, &data, 0);
|
|
|
|
|
|
|
|
if (!ret && failedpath)
|
|
|
|
*failedpath = unlink_recursive_failedpath;
|
|
|
|
|
2014-04-08 15:17:46 +00:00
|
|
|
return ret ? 0 : -errno;
|
2012-11-29 19:21:26 +00:00
|
|
|
}
|