Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
mxtools/pkgadmin/pkgadmin
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executable file
125 lines (107 sloc)
2.36 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/local/system/perl/bin/perl | |
use strict; | |
use warnings; | |
our $USAGE=<<"EOF"; | |
usage: | |
$0 list # show package status | |
$0 update # update /var/pkg according to policy and usage | |
EOF | |
our $PKGDIR='/var/pkg'; | |
sub sys { | |
my @cmd=@_; | |
print join(' ',@cmd),"\n"; | |
system @cmd; | |
$? and exit 1; | |
} | |
sub scandir { | |
my ($dirname)=@_; | |
opendir my $dir,$dirname or die "$dirname: $!\n"; | |
return sort grep !/^\./,readdir $dir; | |
} | |
our %WANT_LOCAL; | |
our %IS_MOUNTED; | |
sub get_mounts { | |
open my $in,'<','/proc/self/mountinfo' or die "/proc/self/mountinfo $!\n"; | |
while (<$in>) { | |
my @f=split; | |
my $root_within_fs=$f[3]; | |
if ($root_within_fs=~m"^$PKGDIR/(.+)$") { | |
my ($pkg)=$1; | |
$IS_MOUNTED{$pkg}=1; | |
}; | |
} | |
} | |
sub read_mxpkg { | |
open my $in,'<','/etc/mxpkg' or die "/etc/mxpkg: $!\n"; | |
while (<$in>) { | |
s/#.*//; | |
/\S/ or next; | |
my ($pkg,$flags)=split ' '; | |
$flags eq 'LOCAL' and $WANT_LOCAL{$pkg}=1; | |
} | |
} | |
sub list { | |
my %is_local=map {$_=>1} scandir($PKGDIR); | |
read_mxpkg(); | |
get_mounts(); | |
my %pkg=map {$_=>1} (keys %is_local,keys %WANT_LOCAL); | |
for my $pkg (sort keys %pkg) { | |
my $want_local=$WANT_LOCAL{$pkg}; | |
my $is_local =$is_local{$pkg}; | |
my $is_mounted=$IS_MOUNTED{$pkg}; | |
printf "%s %s %s %s\n", | |
($want_local?'WANT':' '), | |
($is_local ?'HAVE':' '), | |
($is_mounted?'MNT':' '), | |
$pkg; | |
} | |
} | |
sub update { | |
read_mxpkg(); | |
for my $dir (scandir $PKGDIR) { | |
$dir=~/\.TRASH$/ and next; | |
$WANT_LOCAL{$dir} and next; | |
-e "$PKGDIR/$dir.TRASH" and next; | |
sys('mv',"$PKGDIR/$dir","$PKGDIR/$dir.TRASH"); | |
sys("/usr/sbin/make-automaps"); | |
} | |
get_mounts(); | |
for my $pkg (scandir $PKGDIR) { | |
$pkg=~/\.TRASH$/ or next; | |
if ($IS_MOUNTED{$pkg}) { | |
print "$pkg: still mounted\n"; | |
} else { | |
$pkg or die; | |
sys('rm','-rf',"$PKGDIR/$pkg"); | |
} | |
} | |
for my $pkg (sort keys %WANT_LOCAL) { | |
-d "$PKGDIR/$pkg" and next; | |
-d "/package/pkg/$pkg" or next; | |
sys( | |
'cmirror', | |
'--allowremotefs', | |
'--delete', | |
'--mkdir', | |
'--exclude','./build', | |
'--exclude','./build.tar*', | |
'--nice', | |
"/package/pkg/$pkg","$PKGDIR/$pkg.INCOMING" | |
); | |
sys('mv',"$PKGDIR/$pkg.INCOMING","$PKGDIR/$pkg"); | |
sys("/usr/sbin/make-automaps"); | |
} | |
} | |
umask 022; | |
-d $PKGDIR or sys('mkdir','-p',$PKGDIR); | |
@ARGV or die $USAGE; | |
my ($cmd,@args)=@ARGV; | |
if ($cmd eq 'list') { | |
@args==0 or die $USAGE; | |
list(); | |
} elsif ($cmd eq 'update') { | |
@args==0 or die $USAGE; | |
update(); | |
} else { | |
die $USAGE; | |
} | |