rm-rf.c revision b26fa1a2fbcfee7d03b0c8fd15ec3aa64ae70b9f
/***
This file is part of systemd.
Copyright 2015 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stddef.h>
#include <unistd.h>
#include "btrfs-util.h"
#include "fd-util.h"
#include "log.h"
#include "macro.h"
#include "mount-util.h"
#include "path-util.h"
#include "rm-rf.h"
#include "stat-util.h"
#include "string-util.h"
int ret = 0, r;
/* This returns the first error we run into, but nevertheless
* tries to go on. This closes the passed fd. */
if (!(flags & REMOVE_PHYSICAL)) {
r = fd_is_temporary_fs(fd);
if (r < 0) {
safe_close(fd);
return r;
}
if (!r) {
/* We refuse to clean physical file systems
* with this call, unless explicitly
* requested. This is extra paranoia just to
* be sure we never ever remove non-state
* data */
log_error("Attempted to remove disk file system, and we can't allow that.");
safe_close(fd);
return -EPERM;
}
}
if (!d) {
safe_close(fd);
}
for (;;) {
bool is_dir;
errno = 0;
if (!de) {
return ret;
}
continue;
continue;
}
} else
if (is_dir) {
int subdir_fd;
/* if root_dev is set, remove subdirectories only if device is same */
continue;
if (subdir_fd < 0) {
continue;
}
/* Stop at mount points */
if (r < 0) {
ret = r;
continue;
}
if (r) {
continue;
}
/* This could be a subvolume, try to remove it */
if (r < 0) {
if (ret == 0)
ret = r;
continue;
}
/* ENOTTY, then it wasn't a
* btrfs subvolume, continue
* below. */
} else {
/* It was a subvolume, continue. */
continue;
}
}
/* We pass REMOVE_PHYSICAL here, to avoid
* doing the fstatfs() to check the file
* system type again for each directory */
if (r < 0 && ret == 0)
ret = r;
}
} else if (!(flags & REMOVE_ONLY_DIRECTORIES)) {
}
}
}
}
int fd, r;
struct statfs s;
/* We refuse to clean the root file system with this
* call. This is extra paranoia to never cause a really
* seriously broken system. */
log_error("Attempted to remove entire root file system, and we can't allow that.");
return -EPERM;
}
if ((flags & (REMOVE_SUBVOLUME|REMOVE_ROOT|REMOVE_PHYSICAL)) == (REMOVE_SUBVOLUME|REMOVE_ROOT|REMOVE_PHYSICAL)) {
/* Try to remove as subvolume first */
if (r >= 0)
return r;
return r;
/* Not btrfs or not a subvolume */
}
if (fd < 0) {
return -errno;
if (!(flags & REMOVE_PHYSICAL)) {
return -errno;
if (!is_temporary_fs(&s)) {
log_error("Attempted to remove disk file system, and we can't allow that.");
return -EPERM;
}
}
return -errno;
return 0;
}
if (flags & REMOVE_ROOT) {
r = -errno;
}
}
return r;
}