unify checking if tools exist by running them with --version

This commit is contained in:
Johannes Schauer Marin Rodrigues 2022-05-24 15:40:38 +02:00
parent 0f9c6543c4
commit 27926c75f9
Signed by untrusted user: josch
GPG key ID: F2CBA5C78FBD83E1

View file

@ -206,6 +206,26 @@ sub minor {
return $right->bior($left); return $right->bior($left);
} }
sub can_execute {
my $tool = shift;
my $pid = open my $fh, '-|' // return 0;
if ($pid == 0) {
open(STDERR, '>&', STDOUT) or die;
exec {$tool} $tool, '--version' or die;
}
chomp(
my $content = do { local $/; <$fh> }
);
close $fh;
if ($? != 0) {
return 0;
}
if (length $content == 0) {
return 0;
}
return 1;
}
# check whether a directory is mounted by comparing the device number of the # check whether a directory is mounted by comparing the device number of the
# directory itself with its parent # directory itself with its parent
sub is_mountpoint { sub is_mountpoint {
@ -4346,14 +4366,7 @@ sub main() {
'apt-config', 'tar', 'rm', 'find', 'apt-config', 'tar', 'rm', 'find',
'env' 'env'
) { ) {
my $found = 0; if (!can_execute $tool) {
foreach my $path (split /:/, $ENV{PATH}) {
if (-f "$path/$tool" && -x _ ) {
$found = 1;
last;
}
}
if (!$found) {
error "cannot find $tool"; error "cannot find $tool";
} }
} }
@ -4435,7 +4448,7 @@ sub main() {
# if we are not root, unshare mode is our best option if # if we are not root, unshare mode is our best option if
# test_unshare_userns() succeeds # test_unshare_userns() succeeds
$options->{mode} = 'unshare'; $options->{mode} = 'unshare';
} elsif (system('fakechroot --version>/dev/null') == 0) { } elsif (can_execute 'fakechroot') {
# the next fallback is fakechroot # the next fallback is fakechroot
# exec ourselves again but within fakechroot # exec ourselves again but within fakechroot
my @prefix = (); my @prefix = ();
@ -4443,7 +4456,7 @@ sub main() {
@prefix = ($EXECUTABLE_NAME, '-MDevel::Cover=-silent,-nogcov'); @prefix = ($EXECUTABLE_NAME, '-MDevel::Cover=-silent,-nogcov');
} }
exec 'fakechroot', 'fakeroot', @prefix, $PROGRAM_NAME, @ARGVORIG; exec 'fakechroot', 'fakeroot', @prefix, $PROGRAM_NAME, @ARGVORIG;
} elsif (system('proot --version>/dev/null') == 0) { } elsif (can_execute 'proot') {
# and lastly, proot # and lastly, proot
$options->{mode} = 'proot'; $options->{mode} = 'proot';
} else { } else {
@ -4455,13 +4468,13 @@ sub main() {
error "need to be root"; error "need to be root";
} }
} elsif ($options->{mode} eq 'proot') { } elsif ($options->{mode} eq 'proot') {
if (system('proot --version>/dev/null') != 0) { if (!can_execute 'proot') {
error "need working proot binary"; error "need working proot binary";
} }
} elsif ($options->{mode} eq 'fakechroot') { } elsif ($options->{mode} eq 'fakechroot') {
if (&{$check_fakechroot_running}()) { if (&{$check_fakechroot_running}()) {
# fakechroot is already running # fakechroot is already running
} elsif (system('fakechroot --version>/dev/null') != 0) { } elsif (!can_execute 'fakechroot') {
error "need working fakechroot binary"; error "need working fakechroot binary";
} else { } else {
# exec ourselves again but within fakechroot # exec ourselves again but within fakechroot
@ -4561,7 +4574,7 @@ sub main() {
} }
if (any { $_ eq $options->{mode} } ('root', 'unshare')) { if (any { $_ eq $options->{mode} } ('root', 'unshare')) {
if (system('mount --version>/dev/null') != 0) { if (!can_execute 'mount') {
warning "cannot execute mount"; warning "cannot execute mount";
$options->{canmount} = 0; $options->{canmount} = 0;
} }
@ -4639,7 +4652,7 @@ sub main() {
} elsif ($options->{variant} eq "extract") { } elsif ($options->{variant} eq "extract") {
info "skipping emulation check for extract variant"; info "skipping emulation check for extract variant";
} elsif ($hostarch ne $options->{nativearch}) { } elsif ($hostarch ne $options->{nativearch}) {
if (system('arch-test --version>/dev/null') != 0) { if (!can_execute 'arch-test') {
error "install arch-test for foreign architecture support"; error "install arch-test for foreign architecture support";
} }
my $withemu = 0; my $withemu = 0;
@ -4730,9 +4743,7 @@ sub main() {
if (!exists $deb2qemu->{ $options->{nativearch} }) { if (!exists $deb2qemu->{ $options->{nativearch} }) {
warning "no mapping from $options->{nativearch} to" warning "no mapping from $options->{nativearch} to"
. " qemu-user binary"; . " qemu-user binary";
} elsif ( } elsif (!can_execute '/usr/sbin/update-binfmts') {
system('/usr/sbin/update-binfmts --version>/dev/null')
!= 0) {
warning "cannot find /usr/sbin/update-binfmts"; warning "cannot find /usr/sbin/update-binfmts";
} else { } else {
my $binfmt_identifier my $binfmt_identifier