From 16d2a4a8d9b9ad15425fdb795f7ae504028b3b83 Mon Sep 17 00:00:00 2001 From: Johannes 'josch' Schauer Date: Tue, 20 Nov 2018 08:34:13 +0100 Subject: [PATCH] allow empty lost+found directory in target directory --- mmdebstrap | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/mmdebstrap b/mmdebstrap index 055d7b5..49ede6a 100755 --- a/mmdebstrap +++ b/mmdebstrap @@ -1616,14 +1616,35 @@ sub main() { if (!-d $options->{root}) { die "$options->{root} exists and is not a directory"; } - opendir(my $dh, $options->{root}) or die "Can't opendir($options->{root}): $!"; - # 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 $dh; - readdir $dh; + # check if the directory is empty or contains nothing more than an + # empty lost+found directory. The latter exists on freshly created + # ext3 and ext4 partitions. # 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 { make_path($options->{root}) or die "cannot create root: $!"; }