libArchive.pm.in 20.1 KB
Newer Older
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2005-2010 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 26 27 28 29 30 31 32 33 34 35 36
#
# 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 libdb;
37
use libtestbed;
38
use Project;
39
use English;
40 41
use Experiment;
use Archive;
42
use Template;
43 44 45 46 47 48 49
use File::stat;
use File::Basename;
use POSIX qw(strftime);
use Time::HiRes qw(gettimeofday);

# Configure variables
my $TB		= "@prefix@";
50
my $MAINSITE    = @TBMAINSITE@;
51
my $ARCHSUPPORT = @ARCHIVESUPPORT@;
52
my $USEARCHIVE  = $ARCHSUPPORT;
53
my $ZIPINFO     = "/usr/local/bin/zipinfo";
54
my $TAR		= "/usr/bin/tar";
55

56
sub setdebug($)
57
{
58
    my ($toggle) = @_;
59

60
    Archive::setdebug($toggle);
61 62 63 64 65 66 67
}

#
# 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.
#
68
sub TBExperimentArchive($$$$)
69
{
70
    my ($pid, $eid, $archivep, $viewp) = @_;
71 72

    my $query_result =
73
	DBQueryWarn("select s.archive_idx,e.idx from experiments as e ".
74 75 76 77 78 79
		    "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);

80
    my ($archive_idx,$exptidx) = $query_result->fetchrow_array();
81

82 83 84
    # Need to deal with no archive yet!
    return 1
	if (!defined($archive_idx) || $archive_idx == 0);
85

86 87 88 89 90 91
    my $archive = Archive->Lookup($archive_idx);
    return -1
	if (!defined($archive));

    $$archivep = $archive
	if (defined($archivep));
92 93
    $$viewp = "$exptidx"
	if (defined($viewp));
94
    
95
    return 0;
96 97
}

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
#
# 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;
}

124 125 126 127 128 129 130 131 132 133 134
#
# 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) = @_;

135
    return 0
136 137 138 139 140
	if (!Archive::doarchiving($pid, $eid));

    my $experiment = Experiment->Lookup($pid, $eid);
    return -1
	if (!defined($experiment));
141
    
142 143 144 145 146
    my $exptidx   = $experiment->idx();
    my $rsrcidx   = $experiment->rsrcidx();
    my $group     = $experiment->GetGroup();
    my $unix_name = $group->unix_name();
    my $view      = "$exptidx";
147

148 149 150
    my $archive = Archive->Create($view, $unix_name);
    return -1
	if (!defined($archive));
151

152
    my $archive_idx = $archive->idx();
153

154 155 156 157
    $experiment->TableUpdate("experiment_stats",
			     "archive_idx='$archive_idx'") == 0
	or goto bad;
    
158
    return 0;
159 160

  bad:
161
    $archive->Destroy(1, $view);
162
    return -1;
163 164 165 166 167
}

#
# Add a file to an experiment archive.
#
168
sub TBExperimentArchiveAddFile($$$;$)
169
{
170
    my ($pid, $eid, $pathname, $exact) = @_;
171
    my ($archive, $view);
172

173
    return 0
174
	if (!Archive::doarchiving($pid, $eid));
175
    
176
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
177 178
    return 0
	if ($rval > 0);
179
    return -1
180
	if ($rval < 0);
181

182
    return $archive->Add($pathname, $view, $exact);
183 184
}

185 186 187 188
#
# Add files picked up by NFS tracing to an experiment archive.
#
sub TBExperimentArchiveAddTracedFiles($$)
189 190 191 192 193 194 195 196
{
    return 0;
}

#
# Add all files from the experiment directory to the archive.
#
sub TBExperimentArchiveAddUserFiles($$)
197 198
{
    my ($pid, $eid) = @_;
199
    my ($archive, $view);
200 201

    return 0
202
	if (!Archive::doarchiving($pid, $eid));
203 204 205 206

    my $experiment = Experiment->Lookup($pid, $eid);
    return -1
	if (!defined($experiment));
207
    
208
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
209 210 211 212 213
    return 0
	if ($rval > 0);
    return -1
	if ($rval < 0);

214 215 216 217 218 219 220 221 222 223 224
    my $userdir;

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

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

226 227
    if (-e $userdir) {
	$rval = $archive->Add("$userdir/.", $view, 1, 1);
228 229 230 231 232 233 234

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

235
#
236
# Check for differences in the user files to see if we need a commit.
237
#
238
sub TBExperimentArchiveDiffUserFiles($$)
239 240
{
    my ($pid, $eid) = @_;
241
    my ($archive, $view);
242

243
    return 0
244
	if (!Archive::doarchiving($pid, $eid));
245
    
246 247 248 249
    my $experiment = Experiment->Lookup($pid, $eid);
    return -1
	if (!defined($experiment));
    
250
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
251 252 253 254 255
    return 0
	if ($rval > 0);
    return -1
	if ($rval < 0);

256 257 258 259 260 261 262 263 264 265 266
    my $userdir;

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

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

268
    if (-e $userdir) {
269
	$rval = $archive->Diff("$userdir/.", $view);
270 271 272 273 274 275 276

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

277
#
278
# SavePoint an experiment archive. 
279
#
280
sub TBExperimentArchiveSavePoint($$;$)
281
{
282
    return 0;
283 284 285 286 287 288 289 290
}

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

293
    return 0
294
	if (!Archive::doarchiving($pid, $eid));
295
    
296
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
297 298
    return 0
	if ($rval > 0);
299
    return -1
300
	if ($rval < 0);
301

302
    my $directory = $archive->directory();
303 304 305 306
    if (! -d $directory || ! -w $directory) {
	print STDERR "ArchivePreSwapMod: $directory cannot be written!\n";
	return -1;
    }
307
    my $checkin    = "$directory/checkins/$view";
308
    my $preswapdir = "${checkin}.preswapmod";
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331

    #
    # 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;
    }
    
332
    mysystem("$TAR cf - -C $checkin . | tar xf - -C $preswapdir");
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
    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;
}

351 352 353 354 355 356
#
# Next phase of a swapmod; do another snapshot after the swapout completes.
#
sub TBExperimentArchiveSwapModSwapOut($$)
{
    my ($pid, $eid) = @_;
357
    my ($archive, $view);
358 359

    return 0
360
	if (!Archive::doarchiving($pid, $eid));
361
    
362
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
363 364 365 366 367
    return 0
	if ($rval > 0);
    return -1
	if ($rval < 0);

368
    my $directory = $archive->directory();
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 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
    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;
}

436 437 438 439 440 441
#
# Rollback from a failed swapmod.
#
sub TBExperimentArchiveRollBack($$)
{
    my ($pid, $eid) = @_;
442
    my ($archive, $view);
443

444
    return 0
445
	if (!Archive::doarchiving($pid, $eid));
446
    
447
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
448 449
    return 0
	if ($rval > 0);
450
    return -1
451
	if ($rval < 0);
452

453
    my $directory = $archive->directory();
454 455 456 457
    if (! -d $directory || ! -w $directory) {
	print STDERR "ArchiveRollBack: $directory cannot be written!\n";
	return -1;
    }
458 459 460
    my $checkin        = "$directory/checkins/$view";
    my $preswapdir     = "${checkin}.preswapmod";
    my $swapmodswapout = "${checkin}.swapmodswapout";
461 462 463 464 465 466 467 468

    # 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.
    #
469 470 471
    if (-e $checkin) {
	system("/bin/rm -rf $checkin");
	if (-e $checkin) {
472
	    print STDERR
473 474 475 476 477 478 479 480 481
		"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";
482 483 484
	    return -1;
	}
    }
485
    system("/bin/mv -f $preswapdir $checkin");
486 487
    if ($?) {
	print STDERR "ArchiveRollBack: ".
488
	    "Could not mv $preswapdir to $checkin\n";
489 490 491 492 493 494 495 496 497
	return -1;
    }
    return 0;
}

#
# Swapmod completed okay. Need to commit the old preswap directory and
# move forward. 
#
498
sub TBExperimentArchiveSwapModCommit($$$)
499
{
500
    my ($pid, $eid, $swapped) = @_;
501
    my ($archive, $view);
502

503
    return 0
504
	if (!Archive::doarchiving($pid, $eid));
505
    
506
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
507 508
    return 0
	if ($rval > 0);
509
    return -1
510
	if ($rval < 0);
511

512
    my $directory = $archive->directory();
513
    if (! -d $directory || ! -w $directory) {
514
	print STDERR "ArchiveSwapModCommit: $directory cannot be written!\n";
515 516
	return -1;
    }
517 518 519 520 521 522 523 524 525
    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);
526 527

    #
528
    # We need this below.
529 530 531 532 533 534 535 536 537 538
    #
    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();

539 540 541
    #
    # Derive a useful tag.
    # 
542 543 544
    my ($seconds, $microseconds) = gettimeofday();
    my $newtag  = POSIX::strftime("T20%y%m%d-%H%M%S-", localtime());
    $newtag .= int($microseconds / 1000);
545
    $newtag .= "_preswapmod";
546

547
    print "Doing a commit on the experiment archive ...\n";
548
    return -1
549
	if ($archive->Commit($newtag, undef, undef, $view, $location) != 0);
550 551 552 553 554 555 556 557 558 559 560 561 562

    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 ($?) {
563
	print STDERR "*** ArchiveSwapModCommit: Could not rm $preswapdir\n";
564
    }
565 566 567 568 569 570
    # Ditto.
    system("/bin/rm -rf $swapmodswapout");
    if ($?) {
	print STDERR
	    "*** ArchiveSwapModCommit: Could not rm $swapmodswapout\n";
    }
571 572 573 574 575 576 577

    return 0;
}

#
# Commit an experiment archive. 
#
578
sub TBCommitExperimentArchive($$$;$$$)
579
{
580
    my ($pid, $eid, $tagext, $usertagged, $mfile, $comment) = @_;
581
    my ($archive, $view);
582

583
    return 0
584
	if (!Archive::doarchiving($pid, $eid));
585
    
586
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
587 588
    return 0
	if ($rval > 0);
589
    return -1
590
	if ($rval < 0);
591

592 593 594 595 596 597
    my $experiment = Experiment->Lookup($pid, $eid);
    return -1
	if (!defined($experiment));

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

599
    #
600 601
    # 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.
602
    #
603 604 605 606 607 608 609 610 611 612
    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;

613
	my $unique = $archive->IsTagUnique($newtag, $view);
614
	return -1
615
	    if (!defined($unique));
616 617 618 619 620 621

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

    return -1
624
	if ($archive->Commit($newtag, $comment, $mfile, $view) != 0);
625 626 627 628

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

630 631 632 633
    # A user specified tag is updated.
    if ($usertagged) {
	my $query_result =
	    DBQueryWarn("update archive_tags set ".
634
			"  tagtype='$Archive::TAGTYPE_USER' ".
635 636 637 638 639 640 641 642 643
			"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";
	}
    }
644 645 646
    return 0;
}

647 648 649 650 651 652 653 654
#
# Tag an experiment archive. 
#
sub TBTagExperimentArchive($$$;$$)
{
    return 0;
}

655 656 657
#
# Checkout a copy of an experiment archive, optionally at a branch.
# 
658
sub TBCheckoutExperimentArchive($$$;$$)
659
{
660
    my ($pid, $eid, $path, $tag, $subdir) = @_;
661
    my ($archive, $view);
662 663

    return 0
664
	if (!Archive::doarchiving($pid, $eid));
665
    
666
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
667 668 669 670 671
    return 0
	if ($rval > 0);
    return -1
	if ($rval < 0);

672
    return $archive->Checkout($path, $view, $tag, $subdir);
673 674
}

675 676 677
#
# Checkout a copy of an experiment archive, optionally at a branch.
# 
678
sub TBCheckoutExperimentArchivebyExptIDX($$;$$)
679
{
680
    my ($exptidx, $path, $tag, $subdir) = @_;
681 682

    return 0
683
	if (!$USEARCHIVE);
684 685

    my $query_result =
686 687
	DBQueryWarn("select archive_idx from experiment_stats ".
		    "where exptidx='$exptidx'");
688
    return -1
689 690 691 692
	if (!$query_result || !$query_result->numrows);
    
    my ($archive_idx) = $query_result->fetchrow_array();
    
693
    return 0
694
	if (!$archive_idx);
695

696 697 698
    my $archive = Archive->Lookup($archive_idx);
    return -1
	if (!defined($archive));
699

700
    return $archive->Checkout($path, $exptidx, $tag, $subdir);
701 702
}

703 704 705 706 707 708
#
# Archive an experiment archive to the save area.
# 
sub TBArchiveExperimentArchive($$)
{
    my ($pid, $eid) = @_;
709
    my ($archive, $view);
710

711
    return 0
712
	if (!Archive::doarchiving($pid, $eid));
713
    
714
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
715 716
    return 0
	if ($rval > 0);
717
    return -1
718
	if ($rval < 0);
719

720
    return $archive->Archive($view);
721 722 723 724 725 726 727 728 729
}

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

732
    return 0
733
	if (!Archive::doarchiving($pid, $eid));
734
    
735
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
736 737
    return 0
	if ($rval > 0);
738
    return -1
739
	if ($rval < 0);
740

741
    return $archive->Destroy(0, $view);
742 743 744 745 746 747 748 749
}

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

752
    return 0
753
	if (!Archive::doarchiving($pid, $eid));
754
    
755
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
756 757
    return 0
	if ($rval > 0);
758
    return -1
759
	if ($rval < 0);
760

761
    return $archive->Destroy(1, $view);
762 763 764 765 766 767 768 769 770
}

#
# 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) = @_;
771
    my ($archive, $copyview);
772 773

    return 0
774
	if (!Archive::doarchiving($pid, $eid));
775
    
776 777
    my $rval = TBExperimentArchive($copypid, $copyeid,
				   \$archive, \$copyview);
778 779 780 781 782
    return 0
	if ($rval > 0);
    return -1
	if ($rval < 0);

783 784 785 786 787 788 789 790
    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();
791 792

    return -1
793 794 795 796 797 798 799 800 801 802 803
	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;

804
    return 0;
805 806 807 808 809 810

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

813 814 815 816 817 818
#
# Check that a tag is unique.
#
sub TBIsTaqUnique($$$$)
{
    my ($pid, $eid, $tag, $prval) = @_;
819
    my ($archive, $view);
820 821
    
    return 0
822
	if (!Archive::doarchiving($pid, $eid));
823
    
824
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
825 826 827 828 829
    return 0
	if ($rval > 0);
    return -1
	if ($rval < 0);

830 831 832 833 834
    my $unique = $archive->IsTagUnique($tag, $view);
    return -1
	if (!defined($unique));
    $$prval = $unique;
    return 0;
835 836
}

837 838 839 840
#
# Return a list of files in a particular spot in the archive. This avoids
# having to check it out.
#
841
sub TBListExperimentArchive($$$;$$$)
842
{
843 844
    my ($pid, $eid, $prval, $long, $tag, $root, $subdir) = @_;
    my ($archive, $view);
845 846

    return 0
847
	if (!Archive::doarchiving($pid, $eid));
848
    
849
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
850 851 852 853 854
    return 0
	if ($rval > 0);
    return -1
	if ($rval < 0);

855
    return $archive->List($prval, $view, $long, $tag, $subdir);
856 857
}

858
#
859 860
# Return a list of files in a particular spot in the archive. This avoids
# having to check it out.
861 862 863 864
#
sub TBDUExperimentArchive($$$)
{
    my ($pid, $eid, $prval) = @_;
865
    my ($archive, $view);
866 867 868

    $$prval = 0;
    return 0
869
	if (!Archive::doarchiving($pid, $eid));
870
    
871
    my $rval = TBExperimentArchive($pid, $eid, \$archive, \$view);
872 873 874 875 876
    return 0
	if ($rval > 0);
    return -1
	if ($rval < 0);

877
    return $archive->DU($prval);
878 879
}

880 881
# _Always_ make sure that this 1 is at the end of the file...
1;
882