don't let make_path fail if directory already existed

This commit is contained in:
Johannes 'josch' Schauer 2019-11-21 21:43:23 +01:00
parent 4b82a664da
commit f5afbfaab0
Signed by untrusted user: josch
GPG key ID: F2CBA5C78FBD83E1

View file

@ -781,7 +781,18 @@ sub run_chroot(&$) {
rmdir "$options->{root}/$fname" or warn "cannot rmdir $fname: $!"; rmdir "$options->{root}/$fname" or warn "cannot rmdir $fname: $!";
} }
} }
make_path "$options->{root}/$fname" or error "cannot make_path $fname"; if (-e "$options->{root}/$fname") {
if (! -d "$options->{root}/$fname") {
error "$fname already exists but is not a directory";
}
} else {
my $num_created = make_path "$options->{root}/$fname", {error => \my $err};
if ($err && @$err) {
error (join "; ", (map {"cannot create " . (join ": ", %{$_})} @$err));
} elsif ($num_created == 0) {
error "cannot create $options->{root}/$fname";
}
}
chmod $mode, "$options->{root}/$fname" or error "cannot chmod $fname: $!"; chmod $mode, "$options->{root}/$fname" or error "cannot chmod $fname: $!";
} }
if ($options->{mode} eq 'unshare') { if ($options->{mode} eq 'unshare') {
@ -1004,7 +1015,18 @@ sub setup {
'/var/lib/dpkg/updates'); '/var/lib/dpkg/updates');
} }
foreach my $dir (@directories) { foreach my $dir (@directories) {
make_path("$options->{root}/$dir") or error "failed to create $dir: $!"; if (-e "$options->{root}/$dir") {
if (! -d "$options->{root}/$dir") {
error "$dir already exists but is not a directory";
}
} else {
my $num_created = make_path "$options->{root}/$dir", {error => \my $err};
if ($err && @$err) {
error (join "; ", (map {"cannot create " . (join ": ", %{$_})} @$err));
} elsif ($num_created == 0) {
error "cannot create $options->{root}/$dir";
}
}
} }
} }
@ -1148,9 +1170,17 @@ sub setup {
} elsif ($type == 4) { # block special } elsif ($type == 4) { # block special
0 == system('mknod', "$options->{root}/$fname", 'b', $devmajor, $devminor) or error "mknod failed: $?"; 0 == system('mknod', "$options->{root}/$fname", 'b', $devmajor, $devminor) or error "mknod failed: $?";
} elsif ($type == 5) { # directory } elsif ($type == 5) { # directory
make_path "$options->{root}/$fname", { error => \my $err }; if (-e "$options->{root}/$fname") {
if (@$err) { if (! -d "$options->{root}/$fname") {
error "cannot create $fname"; error "$fname already exists but is not a directory";
}
} else {
my $num_created = make_path "$options->{root}/$fname", {error => \my $err};
if ($err && @$err) {
error (join "; ", (map {"cannot create " . (join ": ", %{$_})} @$err));
} elsif ($num_created == 0) {
error "cannot create $options->{root}/$fname";
}
} }
} else { } else {
error "unsupported type: $type"; error "unsupported type: $type";
@ -2326,7 +2356,12 @@ sub main() {
} }
closedir($dh); closedir($dh);
} else { } else {
make_path($options->{root}) or error "cannot create root: $!"; my $num_created = make_path "$options->{root}", {error => \my $err};
if ($err && @$err) {
error (join "; ", (map {"cannot create " . (join ": ", %{$_})} @$err));
} elsif ($num_created == 0) {
error "cannot create $options->{root}";
}
} }
} }