Path: blob/main/Tools/scripts/pkg-stash/pkg-stash.pl
18157 views
#!/usr/bin/perl -wT12use strict;34use Sys::Hostname;5use File::Basename;6use Getopt::Std;7use POSIX qw(strftime);89sub usage() {1011die("Usage:\n"12."\tpkg-stash [-D base] [-d dir] [-g group] [-o owner] [-cfNn] filename..\n"13."\tpkg-stash [-D base] [-d dir] [-g group] [-o owner] -p\n");14}1516sub stashfile($ %) {17my ($path, %args) = @_;18my ($dir, $base, $ext);19my ($ts, $fname);20my (@opts, @cmd);2122($base, $dir, $ext) = fileparse($path, '\.tgz', '\.tar\.gz', '\.tbz', '\.tbz2');23if ($args{'nostamp'}) {24$ts = "";25} else {26$ts = "-ts".strftime("%Y%m%d%H%M", localtime());27}28$fname = "$base$ts$ext";2930@cmd = ("install");31push(@cmd, '-v') if ($args{'verbose'});32push(@cmd, $args{'copy'}) if ($args{'copy'} ne "");33push(@cmd, $args{'owner'}) if ($args{'owner'} ne "");34push(@cmd, $args{'group'}) if ($args{'group'} ne "");35push(@cmd, $path, "$args{dir}/$fname");3637if ($args{'noact'}) {38print join(' ', @cmd)."\n";39return 1;40}41if (system(@cmd) != 0) {42warn "Installing $path to $args{dir}/$fname failed: $?\n";43}44if (system('rm', $path) != 0) {45warn "Removing %path failed: $?\n";46}47}4849MAIN:{50my %stashargs = (51"base" => "/var/backups/packages/",52"copy" => "",53"dir" => "",54"group" => "",55"noact" => 0,56"nostamp" => 0,57"owner" => "",58"verbose" => 0,59);60my $printonly = 0;61my %opts;62my $path;6364getopts("CcD:d:fg:Nno:pv", \%opts) or65usage();66$stashargs{'base'} = $opts{'D'} if (defined($opts{'D'}));67$stashargs{'copy'} = 'c' if (defined($opts{'c'}));68$stashargs{'copy'} = 'C' if (defined($opts{'C'}));69$stashargs{'dir'} = $opts{'d'} if (defined($opts{'d'}));70$stashargs{'force'} = 1 if (defined($opts{'f'}));71$stashargs{'group'} = "-g $opts{g}" if (defined($opts{'g'}));72$stashargs{'nostamp'} = 1 if (defined($opts{'N'}));73$stashargs{'noact'} = 1 if (defined($opts{'n'}));74$stashargs{'owner'} = "-o $opts{o}" if (defined($opts{'o'}));75$stashargs{'verbose'} = 1 if (defined($opts{'v'}));76$printonly = 1 if (defined($opts{'p'}));7778if ($stashargs{'dir'} eq "") {79my $hostname = hostname();8081$hostname =~ s/\..*//;82$stashargs{'dir'} = $stashargs{'base'}.$hostname;83}8485# Do nada?86if ($printonly) {87print $stashargs{'dir'}."\n";88exit(0);89}9091# Force taint mode into submission92delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};93$ENV{'PATH'} = '/bin:/usr/bin';9495# Okay, process the arguments..96if ($#ARGV == -1) {97usage();98}99foreach $path (@ARGV) {100stashfile($path, %stashargs);101}102}103104105