Fix switch_root delete_content()
This commit is contained in:
parent
941ffd2aed
commit
32efa14595
@ -17,42 +17,43 @@
|
|||||||
static void
|
static void
|
||||||
delete_content(const char *dir, dev_t curdevice)
|
delete_content(const char *dir, dev_t curdevice)
|
||||||
{
|
{
|
||||||
static char path[PATH_MAX];
|
char path[PATH_MAX];
|
||||||
DIR *d;
|
DIR *d;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
struct dirent *dent;
|
struct dirent *dent;
|
||||||
|
|
||||||
/* don't dive into other filesystems */
|
/* don't dive into other filesystems */
|
||||||
if (lstat(dir, &st) || st.st_dev != curdevice)
|
if (lstat(dir, &st) < 0 || st.st_dev != curdevice)
|
||||||
return;
|
return;
|
||||||
/* delete contents recursively */
|
if (!(d = opendir(dir)))
|
||||||
if (S_ISDIR(st.st_mode)) {
|
return;
|
||||||
d = opendir(dir);
|
while ((dent = readdir(d))) {
|
||||||
if (d) {
|
|
||||||
for(; (dent = readdir(d)) ;) {
|
|
||||||
/* skip ".." and "." */
|
|
||||||
if (strcmp(dent->d_name, ".") == 0 ||
|
if (strcmp(dent->d_name, ".") == 0 ||
|
||||||
strcmp(dent->d_name, "..") == 0)
|
strcmp(dent->d_name, "..") == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
/* build path and dive deeper */
|
/* build path and dive deeper */
|
||||||
if (strlcat(path, dir, sizeof(path)) >= sizeof(path))
|
if (strlcpy(path, dir, sizeof(path)) >= sizeof(path))
|
||||||
|
eprintf("path too long\n");
|
||||||
|
if (path[strlen(path) - 1] != '/')
|
||||||
|
if (strlcat(path, "/", sizeof(path)) >= sizeof(path))
|
||||||
eprintf("path too long\n");
|
eprintf("path too long\n");
|
||||||
if (strlcat(path, dent->d_name, sizeof(path)) >= sizeof(path))
|
if (strlcat(path, dent->d_name, sizeof(path)) >= sizeof(path))
|
||||||
eprintf("path too long\n");
|
eprintf("path too long\n");
|
||||||
|
|
||||||
|
if (lstat(path, &st) < 0)
|
||||||
|
weprintf("lstat %s:", path);
|
||||||
|
|
||||||
|
if (S_ISDIR(st.st_mode)) {
|
||||||
delete_content(path, curdevice);
|
delete_content(path, curdevice);
|
||||||
path[0] = 0;
|
if (rmdir(path) < 0)
|
||||||
|
weprintf("rmdir %s:", path);
|
||||||
|
} else {
|
||||||
|
if (unlink(path) < 0)
|
||||||
|
weprintf("unlink %s:", path);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
closedir(d);
|
closedir(d);
|
||||||
|
|
||||||
/* remove now empty dir */
|
|
||||||
rmdir(dir);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
/* unlink non-directory */
|
|
||||||
unlink(dir);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
Loading…
Reference in New Issue
Block a user