allow empty lost+found directory in target directory

This commit is contained in:
Johannes 'josch' Schauer 2018-11-20 08:34:13 +01:00
parent 1730a17286
commit 16d2a4a8d9
Signed by untrusted user: josch
GPG key ID: F2CBA5C78FBD83E1

View file

@ -1616,14 +1616,35 @@ sub main() {
if (!-d $options->{root}) { if (!-d $options->{root}) {
die "$options->{root} exists and is not a directory"; die "$options->{root} exists and is not a directory";
} }
opendir(my $dh, $options->{root}) or die "Can't opendir($options->{root}): $!"; # check if the directory is empty or contains nothing more than an
# Attempt reading the directory thrice. If the third time succeeds, # empty lost+found directory. The latter exists on freshly created
# then it has more entries than just "." and ".." and must thus not # ext3 and ext4 partitions.
# be empty.
readdir $dh;
readdir $dh;
# rationale for requiring an empty directory: https://bugs.debian.org/833525 # rationale for requiring an empty directory: https://bugs.debian.org/833525
die "$options->{root} is not empty" if (readdir $dh); opendir(my $dh, $options->{root}) or die "Can't opendir($options->{root}): $!";
while (my $entry = readdir $dh) {
# skip the "." and ".." entries
next if $entry eq ".";
next if $entry eq "..";
# if the entry is a directory named "lost+found" then skip it
# if it's empty
if ($entry eq "lost+found" and -d "$options->{root}/$entry") {
opendir(my $dh2, "$options->{root}/$entry");
# Attempt reading the directory thrice. If the third time
# succeeds, then it has more entries than just "." and ".."
# and must thus not be empty.
readdir $dh2;
readdir $dh2;
# rationale for requiring an empty directory:
# https://bugs.debian.org/833525
if (readdir $dh2) {
die "$options->{root} contains a non-empty lost+found directory";
}
closedir($dh2);
} else {
die "$options->{root} is not empty";
}
}
closedir($dh);
} else { } else {
make_path($options->{root}) or die "cannot create root: $!"; make_path($options->{root}) or die "cannot create root: $!";
} }