tweaks: create one single cache.

sort options in usage.
be paranoid about changes.
This commit is contained in:
espie 2010-06-05 07:59:05 +00:00
parent 3bcd1abe0f
commit ab58ca5421

View File

@ -1,6 +1,6 @@
#! /usr/bin/perl
# ex:ts=8 sw=4:
# $OpenBSD: pkg_subst,v 1.5 2010/06/05 07:48:00 landry Exp $
# $OpenBSD: pkg_subst,v 1.6 2010/06/05 07:59:05 espie Exp $
#
# Copyright (c) 2008 Marc Espie <espie@openbsd.org>
#
@ -16,7 +16,7 @@
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# very simple script, only allows substitutions
# rather simple script
use strict;
use warnings;
@ -29,25 +29,29 @@ my $subst = OpenBSD::Subst->new;
our $opt_c;
my ($fuid, $fgid);
my ($uidc, $gidc);
set_usage(
'pkg_subst [-c] [-o owner] [-g group] [-Dvar=value ...] [file ...]');
'pkg_subst [-c] [-Dvar=value ...] [-g group] [-o owner] [file ...]');
try {
getopts('D:o:g:ch',
getopts('D:g:o:ch',
{'D' =>
sub {
$subst->parse_option(shift);
},
'o' => sub {
my $uidc = OpenBSD::UidCache->new;
my $owner = shift;
$fuid = $uidc->lookup($owner,-1);
die "$owner is not a valid user" if ($fuid == -1);
$uidc //= OpenBSD::UidCache->new;
$fuid = $uidc->lookup($owner, -1);
die "$owner is not a valid user" if $fuid == -1;
},
'g' => sub {
my $gidc = OpenBSD::GidCache->new;
my $group = shift;
$fgid = $gidc->lookup($group,-1);
die "$group is not a valid group" if ($fgid == -1);
$gidc //= OpenBSD::GidCache->new;
$fgid = $gidc->lookup($group, -1);
die "$group is not a valid group" if $fgid == -1;
},
'h' => sub { Usage(); },
});
@ -74,10 +78,16 @@ while (my $src = shift) {
rename($dest, $src) or die "Can't rename $dest: $!";
}
$subst->copy($src, $dest);
# copy rights as well (and owner/group if we're root)
# copy rights, owner, group as well
my ($uid, $gid, $mode) = (stat $src)[4, 5, 2];
if ($< == 0) {
chown $fuid // $uid, $fgid // $gid, $dest; # not checked
my $r1 = chown $fuid // $uid, $fgid // $gid, $dest;
my $r2 = chmod $mode & 07777, $dest;
if (defined $fuid || defined $fgid || $< == 0) {
if ($r1 == 0) {
die "chown on $dest failed";
}
if ($r2 == 0) {
die "chmod on $dest failed";
}
}
chmod $mode & 07777, $dest; # not checked
}