Merge pull request #29677 from keszybz/rewinddir-alternative-fix

Rewind dir fd before using it for cleanup
This commit is contained in:
Luca Boccassi 2023-10-24 19:41:02 +01:00 committed by GitHub
commit 648fbfe621
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 4 deletions

View file

@ -638,6 +638,10 @@ static void hardlink_context_destroy(HardlinkContext *c) {
* _cleanup_() so that we really delete this, even on failure. */
if (c->dir_fd >= 0) {
/* <dir_fd> might be have already been used for reading, so we need to rewind it. */
if (lseek(c->dir_fd, 0, SEEK_SET) < 0)
log_debug_errno(errno, "Failed to lseek on file descriptor, ignoring: %m");
r = rm_rf_children(TAKE_FD(c->dir_fd), REMOVE_PHYSICAL, NULL); /* consumes dir_fd in all cases, even on failure */
if (r < 0)
log_debug_errno(r, "Failed to remove hardlink store (%s) contents, ignoring: %m", c->subdir);

View file

@ -365,10 +365,6 @@ static int rm_rf_children_impl(
strna(path));
}
}
/* Make sure we reset the iterator since we don't know the state the passed in file
* descriptor is in. */
rewinddir(d);
}
FOREACH_DIRENT_ALL(de, d, return -errno) {

View file

@ -25,6 +25,8 @@ int fstatat_harder(int dfd,
int fstatat_flags,
RemoveFlags remove_flags);
/* Note: directory file descriptors passed to the functions below must be
* positioned at the beginning. If the fd was already used for reading, rewind it. */
int rm_rf_children(int fd, RemoveFlags flags, const struct stat *root_dev);
int rm_rf_child(int fd, const char *name, RemoveFlags flags);
int rm_rf_at(int dir_fd, const char *path, RemoveFlags flags);