mirror of
https://github.com/systemd/systemd-stable.git
synced 2024-12-28 07:21:32 +03:00
Merge pull request #12434 from poettering/rm-rf-children-take-ptr
minor rm_rf_children() modernizations
This commit is contained in:
commit
cb9ff7532b
@ -33,8 +33,8 @@ int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) {
|
|||||||
|
|
||||||
assert(fd >= 0);
|
assert(fd >= 0);
|
||||||
|
|
||||||
/* This returns the first error we run into, but nevertheless
|
/* This returns the first error we run into, but nevertheless tries to go on. This closes the passed
|
||||||
* tries to go on. This closes the passed fd. */
|
* fd, in all cases, including on failure.. */
|
||||||
|
|
||||||
if (!(flags & REMOVE_PHYSICAL)) {
|
if (!(flags & REMOVE_PHYSICAL)) {
|
||||||
|
|
||||||
@ -85,7 +85,7 @@ int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) {
|
|||||||
is_dir = de->d_type == DT_DIR;
|
is_dir = de->d_type == DT_DIR;
|
||||||
|
|
||||||
if (is_dir) {
|
if (is_dir) {
|
||||||
int subdir_fd;
|
_cleanup_close_ int subdir_fd = -1;
|
||||||
|
|
||||||
/* if root_dev is set, remove subdirectories only if device is same */
|
/* if root_dev is set, remove subdirectories only if device is same */
|
||||||
if (root_dev && st.st_dev != root_dev->st_dev)
|
if (root_dev && st.st_dev != root_dev->st_dev)
|
||||||
@ -104,13 +104,10 @@ int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) {
|
|||||||
if (ret == 0 && r != -ENOENT)
|
if (ret == 0 && r != -ENOENT)
|
||||||
ret = r;
|
ret = r;
|
||||||
|
|
||||||
safe_close(subdir_fd);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (r) {
|
if (r > 0)
|
||||||
safe_close(subdir_fd);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
|
|
||||||
if ((flags & REMOVE_SUBVOLUME) && st.st_ino == 256) {
|
if ((flags & REMOVE_SUBVOLUME) && st.st_ino == 256) {
|
||||||
|
|
||||||
@ -122,24 +119,18 @@ int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) {
|
|||||||
if (ret == 0)
|
if (ret == 0)
|
||||||
ret = r;
|
ret = r;
|
||||||
|
|
||||||
safe_close(subdir_fd);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ENOTTY, then it wasn't a
|
/* ENOTTY, then it wasn't a btrfs subvolume, continue below. */
|
||||||
* btrfs subvolume, continue
|
} else
|
||||||
* below. */
|
|
||||||
} else {
|
|
||||||
/* It was a subvolume, continue. */
|
/* It was a subvolume, continue. */
|
||||||
safe_close(subdir_fd);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/* We pass REMOVE_PHYSICAL here, to avoid
|
/* We pass REMOVE_PHYSICAL here, to avoid doing the fstatfs() to check the file
|
||||||
* doing the fstatfs() to check the file
|
|
||||||
* system type again for each directory */
|
* system type again for each directory */
|
||||||
r = rm_rf_children(subdir_fd, flags | REMOVE_PHYSICAL, root_dev);
|
r = rm_rf_children(TAKE_FD(subdir_fd), flags | REMOVE_PHYSICAL, root_dev);
|
||||||
if (r < 0 && ret == 0)
|
if (r < 0 && ret == 0)
|
||||||
ret = r;
|
ret = r;
|
||||||
|
|
||||||
|
@ -120,10 +120,8 @@ int switch_root(const char *new_root,
|
|||||||
|
|
||||||
if (fstat(old_root_fd, &rb) < 0)
|
if (fstat(old_root_fd, &rb) < 0)
|
||||||
log_warning_errno(errno, "Failed to stat old root directory, leaving: %m");
|
log_warning_errno(errno, "Failed to stat old root directory, leaving: %m");
|
||||||
else {
|
else
|
||||||
(void) rm_rf_children(old_root_fd, 0, &rb);
|
(void) rm_rf_children(TAKE_FD(old_root_fd), 0, &rb); /* takes possession of the dir fd, even on failure */
|
||||||
old_root_fd = -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user