2010-03-31 03:24:29 +05:30
|
|
|
/*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-FileCopyrightText: 1991 - 1994, Julianne Frances Haugh
|
|
|
|
* SPDX-FileCopyrightText: 1996 - 2001, Marek Michałkiewicz
|
|
|
|
* SPDX-FileCopyrightText: 2003 - 2006, Tomasz Kłoczko
|
|
|
|
* SPDX-FileCopyrightText: 2007 - 2010, Nicolas François
|
2010-03-31 03:24:29 +05:30
|
|
|
*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2010-03-31 03:24:29 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#ident "$Id$"
|
|
|
|
|
2022-08-05 21:27:22 +05:30
|
|
|
#include <fcntl.h>
|
2010-03-31 03:24:29 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "prototypes.h"
|
|
|
|
#include "defines.h"
|
|
|
|
|
2022-08-05 21:27:22 +05:30
|
|
|
static int remove_tree_at (int at_fd, const char *path, bool remove_root)
|
2010-03-31 03:24:29 +05:30
|
|
|
{
|
|
|
|
DIR *dir;
|
2022-08-05 21:27:22 +05:30
|
|
|
const struct dirent *ent;
|
|
|
|
int dir_fd, rc = 0;
|
2010-03-31 03:24:29 +05:30
|
|
|
|
2022-08-05 21:27:22 +05:30
|
|
|
dir_fd = openat (at_fd, path, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
|
|
|
|
if (dir_fd < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dir = fdopendir (dir_fd);
|
|
|
|
if (!dir) {
|
|
|
|
(void) close (dir_fd);
|
2010-03-31 03:24:29 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-08-05 21:27:22 +05:30
|
|
|
/*
|
|
|
|
* Open the source directory and delete each entry.
|
|
|
|
*/
|
2010-03-31 03:24:29 +05:30
|
|
|
while ((ent = readdir (dir))) {
|
2022-08-05 21:27:22 +05:30
|
|
|
struct stat ent_sb;
|
2010-03-31 03:24:29 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Skip the "." and ".." entries
|
|
|
|
*/
|
|
|
|
if (strcmp (ent->d_name, ".") == 0 ||
|
|
|
|
strcmp (ent->d_name, "..") == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-08-05 21:27:22 +05:30
|
|
|
rc = fstatat (dirfd(dir), ent->d_name, &ent_sb, AT_SYMLINK_NOFOLLOW);
|
|
|
|
if (rc < 0) {
|
2010-03-31 03:24:29 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-08-05 21:27:22 +05:30
|
|
|
if (S_ISDIR (ent_sb.st_mode)) {
|
2010-03-31 03:24:29 +05:30
|
|
|
/*
|
|
|
|
* Recursively delete this directory.
|
|
|
|
*/
|
2022-08-05 21:27:22 +05:30
|
|
|
if (remove_tree_at (dirfd(dir), ent->d_name, true) != 0) {
|
|
|
|
rc = -1;
|
2010-03-31 03:24:29 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Delete the file.
|
|
|
|
*/
|
2022-08-05 21:27:22 +05:30
|
|
|
if (unlinkat (dirfd(dir), ent->d_name, 0) != 0) {
|
|
|
|
rc = -1;
|
2010-03-31 03:24:29 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-05 21:27:22 +05:30
|
|
|
|
2010-03-31 03:24:29 +05:30
|
|
|
(void) closedir (dir);
|
|
|
|
|
2022-08-05 21:27:22 +05:30
|
|
|
if (remove_root && (0 == rc)) {
|
|
|
|
if (unlinkat (at_fd, path, AT_REMOVEDIR) != 0) {
|
|
|
|
rc = -1;
|
2010-03-31 03:24:29 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-05 21:27:22 +05:30
|
|
|
return rc;
|
2010-03-31 03:24:29 +05:30
|
|
|
}
|
|
|
|
|
2022-08-05 21:27:22 +05:30
|
|
|
/*
|
|
|
|
* remove_tree - delete a directory tree
|
|
|
|
*
|
|
|
|
* remove_tree() walks a directory tree and deletes all the files
|
|
|
|
* and directories.
|
|
|
|
* At the end, it deletes the root directory itself.
|
|
|
|
*/
|
|
|
|
int remove_tree (const char *root, bool remove_root)
|
|
|
|
{
|
|
|
|
return remove_tree_at (AT_FDCWD, root, remove_root);
|
|
|
|
}
|