archive_list.in 6.31 KB
Newer Older
1 2 3
#!/usr/bin/perl -wT
#
# Copyright (c) 2005-2007 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23 24 25
#
use English;
use Getopt::Std;
26 27 28
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
use POSIX qw(strftime);
use File::Basename;
29 30 31 32 33 34 35

#
# Command line interface to experiment archive module.
#
sub usage()
{
    print STDERR
36
	"Usage: archive_list [-t tag] [-i file] <idx> <view>\n";
37 38
    exit(-1);
}
39
my $optlist = "dt:i:q:";
40 41
my $debug   = 0;
my $tag     = undef;
42 43
my $file    = undef;
my $query   = undef;
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

#
# Configure variables
#
my $TB		    = "@prefix@";
my $TBOPS           = "@TBOPSEMAIL@";

# Protos
sub fatal($);

#
# Turn off line buffering on output
#
$| = 1;

# un-taint path
$ENV{'PATH'} = "/bin:/usr/bin:/usr/local/bin:$TB/bin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
use libaudit;
use Archive;
use User;
use Experiment;

#
# Map invoking user to object.
#
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    fatal("You ($UID) do not exist!");
}

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
    Archive->setdebug(1);
}
94 95 96
if (defined($options{"q"})) {
    $query = $options{"q"};
}
97 98 99 100 101 102 103 104 105 106
if (defined($options{"t"})) {
    $tag = $options{"t"};

    if ($tag =~ /^([-\w]*)$/) {
	$tag = $1;
    }
    else {
	fatal("Bad data in tag: $tag");
    }
}
107 108 109 110 111 112 113 114 115 116
if (defined($options{"i"})) {
    $file = $options{"i"};

    if ($file =~ /^([\d]*)$/) {
	$file = $1;
    }
    else {
	fatal("Bad data in file: $file");
    }
}
117 118 119 120 121 122 123 124 125 126 127 128 129
if (@ARGV != 2) {
    usage();
}
my $archive_idx  = $ARGV[0];
my $archive_view = $ARGV[1];

if ($archive_view =~ /^([-\w]*)$/) {
    $archive_view = $1;
}
else {
    fatal("Bad data in view: $archive_view");
}

130 131 132 133 134 135 136 137 138
# Derive the URL we need to use. Need to scrub out the file argument since
# we are going to replace that.
my $url = "archive_list.php?";
if (defined($query)) {
    $query =~ s/&file=\d*//;
    $url   .= $query . "&";
}

#
139 140 141 142 143 144
#
#
my $archive = Archive->Lookup($archive_idx);
if (!defined($archive)) {
    fatal("Could not map archive $archive_idx to its object");
}
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
my $zipfile = $archive->ZipFile($archive_view, $tag);
if (!defined($zipfile)) {
    fatal("Could not find zipfile for $archive_view in $archive");
}
if (! -e $zipfile) {
    fatal("Could not find zipfile $zipfile");
}

#
# I guess this code should be in the Archive Library?
#
my $zip     = Archive::Zip->new($zipfile);
my @members = $zip->members();
my $index    = 0;

#
# Spit a member
#
sub SpitMember($$$)
{
    my ($index, $member, $fname) = @_;

    my $size  = sprintf("%10s", $member->uncompressedSize());
    my $mtime = $member->lastModTime();
    
#    $size =~ s/ /&nbsp;/g;
#    $size .= "&nbsp;&sbsp;";

    return "${size} " . strftime("%+", gmtime($mtime)) .
	"  " . "<a href=${url}file=$index>$fname</a>\n";
}

#
# Spit out the header info.
#
sub SpitHeader($;$)
{
    my ($type, $fname) = @_;

    if ($type eq "listing") {
	return "Content-Type: text/html\n";
    }
    elsif ($type eq "textfile") {
	return "Content-Type: text/plain\n";
    }
    elsif ($type eq "binary") {
	return "Content-Type: application/octet-stream\n".
	       "Content-Disposition: attachment; filename=$fname\n";
    }
    return "";
}

#
# No target file/dir, find the top level entries and spit back.
#
if (!defined($file)) {
    print SpitHeader("listing") . "\n";
    print "<pre>";
    
    foreach my $member (@members) {
	my $fname = $member->fileName();

	if ($fname =~ /^([^\/]*)\/$/) {
	    print SpitMember($index, $member, $fname);
	}
	$index++;
    }
    print "</pre>\n";
    exit(0);
}

#
# Handle a directory, expanding it.
#
sub HandleDirectory($$)
{
    my ($index, $dirmember) = @_;

    print SpitHeader("listing") . "\n";
    print "<pre>";

    # Show .
    print SpitMember($index, $dirmember, ".");

    # Find directory one level up and show ..
    my $dotdotname = dirname($dirmember->fileName()) . "/";
    $index = 0;
    foreach my $member (@members) {
	my $fname = $member->fileName();

	if ($dotdotname eq $fname) {
	    print SpitMember($index, $member, "..");
	    last;
	}
	$index++;
    }

    #
    # We want to find all the toplevel entries in the directory.
    #
    my $dirname = $dirmember->fileName();
    $index = 0;

    foreach my $member (@members) {
	my $fname = $member->fileName();

	if ($dirname eq $fname) {
	    ;
	}
	elsif ($fname =~ /^${dirname}([^\/]*(\/)?)$/) {
	    print SpitMember($index, $member, $1);
	}
	$index++;
    }
    print "</pre>\n";
    return 0;
}

#
# Handle a file; spitting it out.
#
sub HandleFile($$)
{
    my ($index, $member) = @_;

    if ($member->isTextFile()) {
	print SpitHeader("textfile") . "\n";
    }
    elsif ($member->isBinaryFile()) {
	
	print SpitHeader("binary", basename($member->fileName())) . "\n";
    }
    print $member->contents();
    return 0;
}

#
# Handle a single member, whatever it is.
#
sub HandleMember($$)
{
    my ($index, $member) = @_;

    if ($member->isDirectory()) {
	return HandleDirectory($index, $member);
    }
    elsif ($member->isTextFile() || $member->isBinaryFile()) {
	return HandleFile($index, $member);
    }
    fatal("HandleMember: Unknown type for ". $member->fileName());
}

#
# Find the target. If its a directory, do another listing. Otherwise
# spit out the file.
#
foreach $member (@members) {
    if ($index == $file) {
	exit(HandleMember($index, $member));
    }
    $index++;
}
fatal("Could not find archive member $file in $zipfile");
308 309 310 311 312 313 314 315 316

sub fatal($)
{
    my ($mesg) = $_[0];

    die("*** $0:\n".
	"    $mesg\n");
}