libArchive.pm.in 19.5 KB
Newer Older
1 2 3
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2005, 2006, 2007 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# All rights reserved.
#
# XXX Need to deal with locking at some point ...
#
package libArchive;

use strict;
use Exporter;
use vars qw(@ISA @EXPORT);

@ISA    = "Exporter";
@EXPORT = qw ( );

# Must come after package declaration!
use lib '@prefix@/lib';
use libdb;
21
use libtestbed;
22
use Project;
23
use English;
24 25
use Experiment;
use Archive;
26
use Template;
27 28 29 30 31 32 33
use File::stat;
use File::Basename;
use POSIX qw(strftime);
use Time::HiRes qw(gettimeofday);

# Configure variables
my $TB		= "@prefix@";
34
my $MAINSITE    = @TBMAINSITE@;
35 36
my $ARCHSUPPORT = @ARCHIVESUPPORT@;
my $USEARCHIVE  = ($MAINSITE || $ARCHSUPPORT);
37
my $ZIPINFO     = "/usr/local/bin/zipinfo";
38
my $TAR		= "/usr/bin/tar";
39

40
sub setdebug($)
41
{
42
    my ($toggle) = @_;
43

44
    Archive::setdebug($toggle);
45 46 47 48 49 50 51
}

#
# Get the archive index for an experiment. The index is kept in the historical
# experiment_stats table, not the current experiments table. That is cause
# we keep the archive and its DB info around forever with the stats.
#
52
sub TBExperimentArchive($$$$)
53
{
54
    my ($pid, $eid, $archivep, $viewp) = @_;
55 56

    my $query_result =
57
	DBQueryWarn("select s.archive_idx,e.idx from experiments as e ".
58 59 60 61 62 63
		    "left join experiment_stats as s on s.exptidx=e.idx ".
		    "where e.pid='$pid' and e.eid='$eid'");

    return -1
	if (!$query_result || $query_result->numrows == 0);

64
    my ($archive_idx,$exptidx) = $query_result->fetchrow_array();
65

66 67 68
    # Need to deal with no archive yet!
    return 1
	if (!defined($archive_idx) || $archive_idx == 0);
69

70 71 72 73 74 75
    my $archive = Archive->Lookup($archive_idx);
    return -1
	if (!defined($archive));

    $$archivep = $archive
	if (defined($archivep));
76 77
    $$viewp = "$exptidx"
	if (defined($viewp));
78
    
79
    return 0;
80 81
}

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
#
# Grab the current tag for an experiment.
#
sub TBExperimentArchiveTag($$$)
{
    my ($pid, $eid, $tagp) = @_;

    my $query_result =
	DBQueryWarn("select r.archive_tag from experiments as e ".
		    "left join experiment_stats as s on s.exptidx=e.idx ".
		    "left join experiment_resources as r on r.idx=s.rsrcidx ".
		    "where e.pid='$pid' and e.eid='$eid'");

    return -1
	if (!$query_result || $query_result->numrows == 0);

    my ($archive_tag) = $query_result->fetchrow_array();

    # Need to deal with no archive yet!
    return 1
	if (!defined($archive_tag));

    $$tagp = $archive_tag;
    return 0;
}

108 109 110 111 112 113 114 115 116 117 118
#
# Create a new archive for an experiment. This has to update the
# experiment_stats table with the newly created archive index.
# Then we have to set the current tag for the experiment in the
# resources table for the experiment.
# Returns zero on success, -1 on failure.
#
sub TBCreateExperimentArchive($$)
{
    my ($pid, $eid) = @_;

119
    return 0
120 121 122 123 124
	if (!Archive::doarchiving($pid, $eid));

    my $experiment = Experiment->Lookup($pid, $eid);
    return -1
	if (!defined($experiment));
125
    
126 127 128 129 130
    my $exptidx   = $experiment->idx();
    my $rsrcidx   = $experiment->rsrcidx();
    my $group     = $experiment->GetGroup();
    my $unix_name = $group->unix_name();
    my $view      = "$exptidx";
131

132 133 134
    my $archive = Archive->Create($view, $unix_name);
    return -1
	if (!defined($archive));
135

136
    my $archive_idx = $archive->idx();
137

138 139 140 141
    $experiment->TableUpdate("experiment_stats",
			     "archive_idx='$archive_idx'") == 0
	or goto bad;
    
142
    return 0;
143 144

  bad:
145
    $archive->Destroy(1, $view);
146
    return -1;
147 148 149 150 151
}

#
# Add a file to an experiment archive.
#
152
sub TBExperimentArchiveAddFile($$$;$)
153
{
154
    my ($pid, $eid, $pathname, $exact) = @_;
155
    my ($archive, $view);
156

157
    return 0
158
	if (!Archive::doarchiving($pid, $eid));
159
    
160
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
161 162
    return 0
	if ($rval > 0);
163
    return -1
164
	if ($rval < 0);
165

166
    return $archive->Add($pathname, $view, $exact);
167 168
}

169 170 171 172
#
# Add files picked up by NFS tracing to an experiment archive.
#
sub TBExperimentArchiveAddTracedFiles($$)
173 174 175 176 177 178 179 180
{
    return 0;
}

#
# Add all files from the experiment directory to the archive.
#
sub TBExperimentArchiveAddUserFiles($$)
181 182
{
    my ($pid, $eid) = @_;
183
    my ($archive, $view);
184 185

    return 0
186
	if (!Archive::doarchiving($pid, $eid));
187 188 189 190

    my $experiment = Experiment->Lookup($pid, $eid);
    return -1
	if (!defined($experiment));
191
    
192
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
193 194 195 196 197
    return 0
	if ($rval > 0);
    return -1
	if ($rval < 0);

198 199 200 201 202 203 204 205 206 207 208
    my $userdir;

    if ($experiment->IsTemplate()) {
	# XXX Fix this
	my $template = Template->LookupByPidEid($pid, $eid);

	$userdir = $template->path();
    }
    else {
	$userdir = $experiment->UserDir();
    }
209

210 211
    if (-e $userdir) {
	$rval = $archive->Add("$userdir/.", $view, 1, 1);
212 213 214 215 216 217 218

	return $rval
	    if ($rval != 0);
    }
    return 0;
}

219
#
220
# Check for differences in the user files to see if we need a commit.
221
#
222
sub TBExperimentArchiveDiffUserFiles($$)
223 224
{
    my ($pid, $eid) = @_;
225
    my ($archive, $view);
226

227
    return 0
228
	if (!Archive::doarchiving($pid, $eid));
229
    
230 231 232 233
    my $experiment = Experiment->Lookup($pid, $eid);
    return -1
	if (!defined($experiment));
    
234
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
235 236 237 238 239
    return 0
	if ($rval > 0);
    return -1
	if ($rval < 0);

240 241 242 243 244 245 246 247 248 249 250
    my $userdir;

    if ($experiment->IsTemplate()) {
	# XXX Fix this
	my $template = Template->LookupByPidEid($pid, $eid);

	$userdir = $template->path();
    }
    else {
	$userdir = $experiment->UserDir();
    }
251

252
    if (-e $userdir) {
253
	$rval = $archive->Diff("$userdir/.", $view);
254 255 256 257 258 259 260

	return $rval
	    if ($rval != 0);
    }
    return 0;
}

261
#
262
# SavePoint an experiment archive. 
263
#
264
sub TBExperimentArchiveSavePoint($$;$)
265
{
266
    return 0;
267 268 269 270 271 272 273 274
}

#
# Setup for a swapmod. A pain in the butt!
#
sub TBExperimentArchivePreSwapMod($$)
{
    my ($pid, $eid) = @_;
275
    my ($archive, $view);
276

277
    return 0
278
	if (!Archive::doarchiving($pid, $eid));
279
    
280
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
281 282
    return 0
	if ($rval > 0);
283
    return -1
284
	if ($rval < 0);
285

286
    my $directory = $archive->directory();
287 288 289 290
    if (! -d $directory || ! -w $directory) {
	print STDERR "ArchivePreSwapMod: $directory cannot be written!\n";
	return -1;
    }
291
    my $checkin    = "$directory/checkins/$view";
292
    my $preswapdir = "${checkin}.preswapmod";
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315

    #
    # Make a copy of the current tree, since its easier to rollback
    # if I just wait before doing a commit (until swapmod really finishes).
    #
    if (-e $preswapdir) {
	system("/bin/rm -rf $preswapdir");
	if (-e $preswapdir) {
	    print STDERR
		"ArchivePreSwapMod: Could not delete old preswap dir!\n";
	    return -1;
	}
    }
    if (! mkdir("$preswapdir", 0777)) {
	print STDERR "ArchivePreSwapMod: Could not mkdir $preswapdir: $!\n";
	return -1;
    }
    if (! chmod(0777, "$preswapdir")) {
	print STDERR "ArchivePreSwapMod: ".
	    "Could not chmod directory $preswapdir: $!\n";
	goto bad;
    }
    
316
    mysystem("$TAR cf - -C $checkin . | tar xf - -C $preswapdir");
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
    if ($?) {
	print STDERR "ArchivePreSwapMod: Could not copy to $preswapdir\n";
	goto bad;
    }
    return 0;

  bad:
    # Be sure to remove the new directory!
    if (-e $preswapdir) {
	system("/bin/rm -rf $preswapdir");
	if (-e $preswapdir) {
	    print STDERR
		"ArchivePreSwapMod: Could not delete new preswap dir!\n";
	}
    }
    return -1;
}

335 336 337 338 339 340
#
# Next phase of a swapmod; do another snapshot after the swapout completes.
#
sub TBExperimentArchiveSwapModSwapOut($$)
{
    my ($pid, $eid) = @_;
341
    my ($archive, $view);
342 343

    return 0
344
	if (!Archive::doarchiving($pid, $eid));
345
    
346
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
347 348 349 350 351
    return 0
	if ($rval > 0);
    return -1
	if ($rval < 0);

352
    my $directory = $archive->directory();
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
    if (! -d $directory || ! -w $directory) {
	print STDERR "ArchiveSwapmodswapout: $directory cannot be written!\n";
	return -1;
    }
    my $checkin    = "$directory/checkins/$view";
    my $preswapdir = "${checkin}.swapmodswapout";

    #
    # Grab the user specified files. 
    #
    if (TBExperimentArchiveAddUserFiles($pid, $eid) < 0) {
	print STDERR "ArchiveSwapmodswapout: ".
	    "Failed to add user specified files to the experiment archive!\n";
	return -1;
    }
    
    #
    # Add the files that have been detected by tracing to the archive.
    #
    if (TBExperimentArchiveAddTracedFiles($pid, $eid) < 0) {
	print STDERR "ArchiveSwapmodswapout: ".
	    "Failed to add traced files to the experiment archive!\n";
	return -1;
    }

    #
    # Make a copy of the current tree; This is the tree that will be
    # committed as the swapout if the swapmod completes okay.
    #
    if (-e $preswapdir) {
	system("/bin/rm -rf $preswapdir");
	if (-e $preswapdir) {
	    print STDERR
		"ArchiveSwapmodswapout: Could not delete old preswap dir!\n";
	    return -1;
	}
    }
    if (! mkdir("$preswapdir", 0777)) {
	print STDERR
	    "ArchiveSwapmodswapout: Could not mkdir $preswapdir: $!\n";
	return -1;
    }
    if (! chmod(0777, "$preswapdir")) {
	print STDERR "ArchiveSwapmodswapout: ".
	    "Could not chmod directory $preswapdir: $!\n";
	goto bad;
    }
    
    mysystem("$TAR cf - -C $checkin . | tar xf - -C $preswapdir");
    if ($?) {
	print STDERR "ArchiveSwapmodswapout: Could not copy to $preswapdir\n";
	goto bad;
    }
    return 0;

  bad:
    # Be sure to remove the new directory!
    if (-e $preswapdir) {
	system("/bin/rm -rf $preswapdir");
	if (-e $preswapdir) {
	    print STDERR
		"ArchiveSwapmodswapout: Could not delete new preswap dir!\n";
	}
    }
    return -1;
}

420 421 422 423 424 425
#
# Rollback from a failed swapmod.
#
sub TBExperimentArchiveRollBack($$)
{
    my ($pid, $eid) = @_;
426
    my ($archive, $view);
427

428
    return 0
429
	if (!Archive::doarchiving($pid, $eid));
430
    
431
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
432 433
    return 0
	if ($rval > 0);
434
    return -1
435
	if ($rval < 0);
436

437
    my $directory = $archive->directory();
438 439 440 441
    if (! -d $directory || ! -w $directory) {
	print STDERR "ArchiveRollBack: $directory cannot be written!\n";
	return -1;
    }
442 443 444
    my $checkin        = "$directory/checkins/$view";
    my $preswapdir     = "${checkin}.preswapmod";
    my $swapmodswapout = "${checkin}.swapmodswapout";
445 446 447 448 449 450 451 452

    # In case we bailed out really early in the swapmod path.
    return 0
	if (! -e $preswapdir);

    #
    # Restore the old (preswap) tree to the current tree.
    #
453 454 455
    if (-e $checkin) {
	system("/bin/rm -rf $checkin");
	if (-e $checkin) {
456
	    print STDERR
457 458 459 460 461 462 463 464 465
		"ArchiveRollBack: Could not delete $checkin!\n";
	    return -1;
	}
    }
    if (-e $swapmodswapout) {
	system("/bin/rm -rf $swapmodswapout");
	if (-e $swapmodswapout) {
	    print STDERR
		"ArchiveRollBack: Could not delete $swapmodswapout!\n";
466 467 468
	    return -1;
	}
    }
469
    system("/bin/mv -f $preswapdir $checkin");
470 471
    if ($?) {
	print STDERR "ArchiveRollBack: ".
472
	    "Could not mv $preswapdir to $checkin\n";
473 474 475 476 477 478 479 480 481
	return -1;
    }
    return 0;
}

#
# Swapmod completed okay. Need to commit the old preswap directory and
# move forward. 
#
482
sub TBExperimentArchiveSwapModCommit($$$)
483
{
484
    my ($pid, $eid, $swapped) = @_;
485
    my ($archive, $view);
486

487
    return 0
488
	if (!Archive::doarchiving($pid, $eid));
489
    
490
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
491 492
    return 0
	if ($rval > 0);
493
    return -1
494
	if ($rval < 0);
495

496
    my $directory = $archive->directory();
497
    if (! -d $directory || ! -w $directory) {
498
	print STDERR "ArchiveSwapModCommit: $directory cannot be written!\n";
499 500
	return -1;
    }
501 502 503 504 505 506 507 508 509
    my $checkin        = "$directory/checkins/$view";
    my $preswapdir     = "${checkin}.preswapmod";
    my $swapmodswapout = "${checkin}.swapmodswapout";

    #
    # Where we get the previous stuff depends on whether the modify is
    # of a swapped in or swapped out experiment.
    #
    my $location = ($swapped ? $preswapdir : $swapmodswapout);
510 511

    #
512
    # We need this below.
513 514 515 516 517 518 519 520 521 522
    #
    my $query_result =
	DBQueryWarn("select s.rsrcidx from experiments as e ".
		    "left join experiment_stats as s on e.idx=s.exptidx ".
		    "where e.pid='$pid' and e.eid='$eid'");
    if (!$query_result || !$query_result->numrows) {
	return -1;
    }
    my ($rsrcidx) = $query_result->fetchrow_array();

523 524 525
    #
    # Derive a useful tag.
    # 
526 527 528
    my ($seconds, $microseconds) = gettimeofday();
    my $newtag  = POSIX::strftime("T20%y%m%d-%H%M%S-", localtime());
    $newtag .= int($microseconds / 1000);
529
    $newtag .= "_preswapmod";
530

531
    print "Doing a commit on the experiment archive ...\n";
532
    return -1
533
	if ($archive->Commit($newtag, undef, undef, $view, $location) != 0);
534 535 536 537 538 539 540 541 542 543 544 545 546

    if (!DBQueryWarn("update experiment_resources set ".
		     "      archive_tag='$newtag' ".
		     "where idx='$rsrcidx'")) {
	return -1;
    }

    #
    # Remove the preswap dir, but if it fails keep going. Will catch it
    # next time above.
    # 
    system("/bin/rm -rf $preswapdir");
    if ($?) {
547
	print STDERR "*** ArchiveSwapModCommit: Could not rm $preswapdir\n";
548
    }
549 550 551 552 553 554
    # Ditto.
    system("/bin/rm -rf $swapmodswapout");
    if ($?) {
	print STDERR
	    "*** ArchiveSwapModCommit: Could not rm $swapmodswapout\n";
    }
555 556 557 558 559 560 561

    return 0;
}

#
# Commit an experiment archive. 
#
562
sub TBCommitExperimentArchive($$$;$$$)
563
{
564
    my ($pid, $eid, $tagext, $usertagged, $mfile, $comment) = @_;
565
    my ($archive, $view);
566

567
    return 0
568
	if (!Archive::doarchiving($pid, $eid));
569
    
570
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
571 572
    return 0
	if ($rval > 0);
573
    return -1
574
	if ($rval < 0);
575

576 577 578 579 580 581
    my $experiment = Experiment->Lookup($pid, $eid);
    return -1
	if (!defined($experiment));

    my $rsrcidx     = $experiment->rsrcidx();
    my $archive_idx = $archive->idx();
582

583
    #
584 585
    # Derive a tag, unless its a user specified tag. In that case it
    # has to be unique cause we are going to use the whole thing as is.
586
    #
587 588 589 590 591 592 593 594 595 596
    my $newtag;
    if (! $usertagged) {
	my ($seconds, $microseconds) = gettimeofday();
	$newtag  = POSIX::strftime("T20%y%m%d-%H%M%S-", localtime());
	$newtag .= int($microseconds / 1000);
	$newtag .= "_${tagext}";
    }
    else {
	$newtag = $tagext;

597
	my $unique = $archive->IsTagUnique($newtag, $view);
598
	return -1
599
	    if (!defined($unique));
600 601 602 603 604 605

	if (! $unique) {
	    print STDERR "*** ArchiveCommit: Duplicate user tag: $newtag\n";
	    return -1;
	}
    }
606 607

    return -1
608
	if ($archive->Commit($newtag, $comment, $mfile, $view) != 0);
609 610 611 612

    $experiment->TableUpdate("experiment_resources",
			     "archive_tag='$newtag'", "idx='$rsrcidx'") == 0
	or goto bad;
613

614 615 616 617
    # A user specified tag is updated.
    if ($usertagged) {
	my $query_result =
	    DBQueryWarn("update archive_tags set ".
618
			"  tagtype='$Archive::TAGTYPE_USER' ".
619 620 621 622 623 624 625 626 627
			"where archive_idx='$archive_idx' and ".
			"      view='$view' and tag='$newtag'");
	return -1
	    if (!$query_result);
	if (!$query_result->affectedrows) {
	    print STDERR "*** ArchiveCommit: ".
		"Could not update user tag: $newtag\n";
	}
    }
628 629 630
    return 0;
}

631 632 633 634 635 636 637 638
#
# Tag an experiment archive. 
#
sub TBTagExperimentArchive($$$;$$)
{
    return 0;
}

639 640 641
#
# Checkout a copy of an experiment archive, optionally at a branch.
# 
642
sub TBCheckoutExperimentArchive($$$;$$)
643
{
644
    my ($pid, $eid, $path, $tag, $subdir) = @_;
645
    my ($archive, $view);
646 647

    return 0
648
	if (!Archive::doarchiving($pid, $eid));
649
    
650
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
651 652 653 654 655
    return 0
	if ($rval > 0);
    return -1
	if ($rval < 0);

656
    return $archive->Checkout($path, $view, $tag, $subdir);
657 658
}

659 660 661
#
# Checkout a copy of an experiment archive, optionally at a branch.
# 
662
sub TBCheckoutExperimentArchivebyExptIDX($$;$$)
663
{
664
    my ($exptidx, $path, $tag, $subdir) = @_;
665 666

    return 0
667
	if (!$USEARCHIVE);
668 669

    my $query_result =
670 671
	DBQueryWarn("select archive_idx from experiment_stats ".
		    "where exptidx='$exptidx'");
672
    return -1
673 674 675 676
	if (!$query_result || !$query_result->numrows);
    
    my ($archive_idx) = $query_result->fetchrow_array();
    
677
    return 0
678
	if (!$archive_idx);
679

680 681 682
    my $archive = Archive->Lookup($archive_idx);
    return -1
	if (!defined($archive));
683

684
    return $archive->Checkout($path, $exptidx, $tag, $subdir);
685 686
}

687 688 689 690 691 692
#
# Archive an experiment archive to the save area.
# 
sub TBArchiveExperimentArchive($$)
{
    my ($pid, $eid) = @_;
693
    my ($archive, $view);
694

695
    return 0
696
	if (!Archive::doarchiving($pid, $eid));
697
    
698
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
699 700
    return 0
	if ($rval > 0);
701
    return -1
702
	if ($rval < 0);
703

704
    return $archive->Archive($view);
705 706 707 708 709 710 711 712 713
}

#
# Delete an experiment archive. This just deletes it from the active area.
# Its historical DB state is retained.
#
sub TBDeleteExperimentArchive($$)
{
    my ($pid, $eid) = @_;
714
    my ($archive, $view);
715

716
    return 0
717
	if (!Archive::doarchiving($pid, $eid));
718
    
719
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
720 721
    return 0
	if ($rval > 0);
722
    return -1
723
	if ($rval < 0);
724

725
    return $archive->Destroy(0, $view);
726 727 728 729 730 731 732 733
}

#
# Destroy an experiment archive. Totally flush it from the system.
#
sub TBDestroyExperimentArchive($$)
{
    my ($pid, $eid) = @_;
734
    my ($archive, $view);
735

736
    return 0
737
	if (!Archive::doarchiving($pid, $eid));
738
    
739
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
740 741
    return 0
	if ($rval > 0);
742
    return -1
743
	if ($rval < 0);
744

745
    return $archive->Destroy(1, $view);
746 747 748 749 750 751 752 753 754
}

#
# Branch an experiment archive. Only an existing experiment can be
# branched, but that will probably change later. 
#
sub TBForkExperimentArchive($$$$$)
{
    my ($pid, $eid, $copypid, $copyeid, $copytag) = @_;
755
    my ($archive, $copyview);
756 757

    return 0
758
	if (!Archive::doarchiving($pid, $eid));
759
    
760 761
    my $rval = TBExperimentArchive($copypid, $copyeid,
				   \$archive, \$copyview);
762 763 764 765 766
    return 0
	if ($rval > 0);
    return -1
	if ($rval < 0);

767 768 769 770 771 772 773 774
    my $experiment = Experiment->Lookup($pid, $eid);
    return -1
	if (!defined($experiment));

    my $archive_idx = $archive->idx();
    my $rsrcidx     = $experiment->rsrcidx();
    my $archive_tag = "F${rsrcidx}";
    my $newview     = $experiment->idx();
775 776

    return -1
777 778 779 780 781 782 783 784 785 786 787
	if ($archive->Fork($newview, $archive_tag, $copyview) < 0);

    $experiment->TableUpdate("experiment_resources",
			     "archive_tag='$archive_tag'",
			     "idx='$rsrcidx'") == 0
	or goto bad;

    $experiment->TableUpdate("experiment_stats",
			     "archive_idx='$archive_idx'") == 0
	or goto bad;

788
    return 0;
789 790 791 792 793 794

  bad:
    # Its a shared resource, but ArchiveDestroy() checks.
    $archive->Destroy(1, $newview)
	if (defined($archive));
    return -1;
795 796
}

797 798 799 800 801 802
#
# Check that a tag is unique.
#
sub TBIsTaqUnique($$$$)
{
    my ($pid, $eid, $tag, $prval) = @_;
803
    my ($archive, $view);
804 805
    
    return 0
806
	if (!Archive::doarchiving($pid, $eid));
807
    
808
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
809 810 811 812 813
    return 0
	if ($rval > 0);
    return -1
	if ($rval < 0);

814 815 816 817 818
    my $unique = $archive->IsTagUnique($tag, $view);
    return -1
	if (!defined($unique));
    $$prval = $unique;
    return 0;
819 820
}

821 822 823 824
#
# Return a list of files in a particular spot in the archive. This avoids
# having to check it out.
#
825
sub TBListExperimentArchive($$$;$$$)
826
{
827 828
    my ($pid, $eid, $prval, $long, $tag, $root, $subdir) = @_;
    my ($archive, $view);
829 830

    return 0
831
	if (!Archive::doarchiving($pid, $eid));
832
    
833
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
834 835 836 837 838
    return 0
	if ($rval > 0);
    return -1
	if ($rval < 0);

839
    return $archive->List($prval, $view, $long, $tag, $subdir);
840 841
}

842
#
843 844
# Return a list of files in a particular spot in the archive. This avoids
# having to check it out.
845 846 847 848
#
sub TBDUExperimentArchive($$$)
{
    my ($pid, $eid, $prval) = @_;
849
    my ($archive, $view);
850 851 852

    $$prval = 0;
    return 0
853
	if (!Archive::doarchiving($pid, $eid));
854
    
855
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
856 857 858 859 860
    return 0
	if ($rval > 0);
    return -1
	if ($rval < 0);

861
    return $archive->DU($prval);
862 863
}

864 865
# _Always_ make sure that this 1 is at the end of the file...
1;
866