mirror of
https://github.com/systemd/systemd-stable.git
synced 2025-01-31 01:47:15 +03:00
shared/rm_rf: refactor rm_rf() to shorten code a bit
(cherry picked from commit 84ced330020c0bae57bd4628f1f44eec91304e69)
This commit is contained in:
parent
47741ff9ea
commit
664529efa9
@ -249,7 +249,7 @@ int rm_rf_children(
|
|||||||
}
|
}
|
||||||
|
|
||||||
int rm_rf(const char *path, RemoveFlags flags) {
|
int rm_rf(const char *path, RemoveFlags flags) {
|
||||||
int fd, r;
|
int fd, r, q = 0;
|
||||||
|
|
||||||
assert(path);
|
assert(path);
|
||||||
|
|
||||||
@ -281,18 +281,22 @@ int rm_rf(const char *path, RemoveFlags flags) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
|
fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
|
||||||
if (fd < 0) {
|
if (fd >= 0) {
|
||||||
|
/* We have a dir */
|
||||||
|
r = rm_rf_children(fd, flags, NULL);
|
||||||
|
|
||||||
|
if (FLAGS_SET(flags, REMOVE_ROOT))
|
||||||
|
q = RET_NERRNO(rmdir(path));
|
||||||
|
} else {
|
||||||
if (FLAGS_SET(flags, REMOVE_MISSING_OK) && errno == ENOENT)
|
if (FLAGS_SET(flags, REMOVE_MISSING_OK) && errno == ENOENT)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (!IN_SET(errno, ENOTDIR, ELOOP))
|
if (!IN_SET(errno, ENOTDIR, ELOOP))
|
||||||
return -errno;
|
return -errno;
|
||||||
|
|
||||||
if (FLAGS_SET(flags, REMOVE_ONLY_DIRECTORIES))
|
if (FLAGS_SET(flags, REMOVE_ONLY_DIRECTORIES) || !FLAGS_SET(flags, REMOVE_ROOT))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (FLAGS_SET(flags, REMOVE_ROOT)) {
|
|
||||||
|
|
||||||
if (!FLAGS_SET(flags, REMOVE_PHYSICAL)) {
|
if (!FLAGS_SET(flags, REMOVE_PHYSICAL)) {
|
||||||
struct statfs s;
|
struct statfs s;
|
||||||
|
|
||||||
@ -304,26 +308,15 @@ int rm_rf(const char *path, RemoveFlags flags) {
|
|||||||
path);
|
path);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (unlink(path) < 0) {
|
r = 0;
|
||||||
if (FLAGS_SET(flags, REMOVE_MISSING_OK) && errno == ENOENT)
|
q = RET_NERRNO(unlink(path));
|
||||||
return 0;
|
|
||||||
|
|
||||||
return -errno;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
if (r < 0)
|
||||||
}
|
|
||||||
|
|
||||||
r = rm_rf_children(fd, flags, NULL);
|
|
||||||
|
|
||||||
if (FLAGS_SET(flags, REMOVE_ROOT) &&
|
|
||||||
rmdir(path) < 0 &&
|
|
||||||
r >= 0 &&
|
|
||||||
(!FLAGS_SET(flags, REMOVE_MISSING_OK) || errno != ENOENT))
|
|
||||||
r = -errno;
|
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
|
if (q < 0 && (q != -ENOENT || !FLAGS_SET(flags, REMOVE_MISSING_OK)))
|
||||||
|
return q;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int rm_rf_child(int fd, const char *name, RemoveFlags flags) {
|
int rm_rf_child(int fd, const char *name, RemoveFlags flags) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user