libArchive.pm.in 47.9 KB
Newer Older
1 2 3
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
Timothy Stack's avatar
Timothy Stack committed
4
# Copyright (c) 2005, 2006 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 23 24 25 26 27 28 29
use English;
use File::stat;
use File::Basename;
use POSIX qw(strftime);
use Time::HiRes qw(gettimeofday);

# Configure variables
my $TB		= "@prefix@";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
30 31
my $MAINSITE    = @TBMAINSITE@;
my $ALLOWEDPID  = "testbed";
32 33 34 35 36 37 38 39
# XXX
my $ARCHIVEDIR  = "/usr/testbed/exparchive";
my $TESTMODE    = @TESTMODE@;
my $TBOPS       = "@TBOPSEMAIL@";
my $ELABINELAB  = @ELABINELAB@;
my $MD5		= "/sbin/md5";
my $MKDIR       = "/bin/mkdir";
my $CHMOD       = "/bin/chmod";
40
my $CHGRP       = "/usr/bin/chgrp";
41
my $TAR		= "/usr/bin/tar";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
42
my $RSYNC	= "/usr/local/bin/rsync";
43
my $RM		= "/bin/rm";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
44
my $REALPATH	= "/bin/realpath";
45 46 47
my $SVN         = "/usr/local/bin/svn";
my $SVNADMIN    = "/usr/local/bin/svnadmin";
my $IMPORTER    = "$TB/sbin/svn_load_dirs.pl";
48
my $inittag     = 'root';
49
my $defaultview = 'head';
50
my $debug       = 2;
51
my $svnopt      = ($debug ? "" : "-q");
52 53 54 55
my %ROOTS       = ("proj"   => "proj",
		   "users"  => "users",
		   "share"  => "share",
		   "groups" => "groups");
56 57 58 59 60 61 62 63 64 65 66

# Little helper and debug function.
sub mysystem($)
{
    my ($command) = @_;

    print STDERR "Running '$command'\n"
	if ($debug);
    system($command);
}

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
# Another little helper for scripts that include this library.
sub setdebug($)
{
    my ($toggle) = @_;

    if ($toggle) {
	$debug  = 1;
	$svnopt = "";
    }
    else {
	$debug  = 0;
	$svnopt = "-q";
    }
}

82 83 84 85
#
# Create a new archive. Returns -1 if any error. Otherwise return
# the new record index.
# 
86
sub ArchiveCreate(;$$$)
87
{
88
    my ($tag, $view, $unix_gid) = @_;
89
    
90 91
    my $idx;
    my $dir;
92 93 94

    $tag = $inittag
	if (!defined($tag));
95 96
    $view = $defaultview
	if (!defined($view));
97

98
    # Tags cannot must start with a letter.
99 100 101
    if ($tag =~ /^\d+/) {
	$tag = "T" . $tag;
    }
102 103 104 105 106

    #
    # Need to create the directory for it, once we have the index.
    # 
    my $query_result =
Leigh B. Stoller's avatar
Leigh B. Stoller committed
107
	DBQueryWarn("insert into archives set ".
108
		    "  idx=NULL, ".
109 110 111 112 113 114 115 116 117 118 119 120 121 122
		    "  date_created=UNIX_TIMESTAMP(now())");

    return -1
	if (!$query_result);

    $idx = $query_result->insertid;
    $dir = "$ARCHIVEDIR/$idx";

    #
    # Create the directory and store the absolute path into the new record.
    # This should probably be a privledged operation at some point.
    # 
    mysystem("$MKDIR $dir") == 0 or goto bad;
    mysystem("$CHMOD 775 $dir") == 0 or goto bad;
123 124 125
    if (defined($unix_gid)) {
	mysystem("$CHGRP $unix_gid $dir") == 0 or goto bad;
    }
126

Leigh B. Stoller's avatar
Leigh B. Stoller committed
127
    DBQueryWarn("update archives set directory='$dir' where idx='$idx'")
128 129 130
	or goto bad;

    #
131
    # Make subdirs. One to hold the control tree, and the other
132 133
    # to hold currently checked out versions of the tree. Lastly, we
    # need a place to copyin files before they are added to the repo.
134
    #
135
    my $repodir   = "$dir/repo";
136 137
    my $checkouts = "$dir/checkouts";
    my $checkins  = "$dir/checkins";
138

139 140
    if (! mkdir("$repodir", 0777)) {
	print STDERR "ArchiveCreate: Could not mkdir $repodir: $!\n";
141 142
	goto bad;
    }
143 144
    if (! chmod(0777, "$repodir")) {
	print STDERR "ArchiveCreate: Could not chmod $repodir: $!\n";
145 146
	goto bad;
    }
147 148
    if (! mkdir("$checkouts", 0777)) {
	print STDERR "ArchiveCreate: Could not mkdir $checkouts: $!\n";
149 150
	goto bad;
    }
151 152
    if (! chmod(0777, "$checkouts")) {
	print STDERR "ArchiveCreate: Could not chmod $checkouts: $!\n";
153 154
	goto bad;
    }
155 156
    if (! mkdir("$checkins", 0777)) {
	print STDERR "ArchiveCreate: Could not mkdir $checkins: $!\n";
157 158
	goto bad;
    }
159 160 161 162 163 164 165 166 167 168
    if (! chmod(0777, "$checkins")) {
	print STDERR "ArchiveCreate: Could not chmod $checkins: $!\n";
	goto bad;
    }
    if (! mkdir("$checkins/$view", 0777)) {
	print STDERR "ArchiveCreate: Could not mkdir $checkins/$view: $!\n";
	goto bad;
    }
    if (! chmod(0777, "$checkins/$view")) {
	print STDERR "ArchiveCreate: Could not chmod $checkins/$view: $!\n";
169 170
	goto bad;
    }
171 172
    # Initialize the repo.
    mysystem("$SVNADMIN create $repodir") == 0
173
	or goto bad;
174 175 176 177
    
    # Create an stub directory tree and import it as "root"
    mysystem("cd $dir; mkdir ignore; cd ignore; mkdir $view; ".
             "         mkdir $view/trunk $view/savepoint $view/tags; ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
178
	     "         mkdir $view/history; ".
Timothy Stack's avatar
Timothy Stack committed
179
	     "$SVN import $svnopt -m 'ArchiveCreate' ".
180
	     "     $view file://$repodir/$view")
181 182 183
	== 0 or goto bad;

    # Create a branch tag in the tags directory to base differences against.
184
    mysystem("$SVN copy $svnopt -m 'ArchiveCreate Branch' ".
185 186
	     "          file://$repodir/$view/trunk ".
	     "          file://$repodir/$view/tags/${tag}-branch")
187
	== 0 or goto bad;
188
    
189 190
    # Now check it out. This creates the $checkouts/$view directory.
    mysystem("cd $checkouts; ".
191
	     "$SVN checkout $svnopt file://$repodir/$view")
192 193
	== 0 or goto bad;

194 195 196 197 198
    # Now enter the default view (okay, branch) of this archive.
    DBQueryWarn("insert into archive_views set ".
		"  current_tag='$tag', archive_idx='$idx', view='$view', ".
		"  date_created=UNIX_TIMESTAMP(now())") or goto bad;
    
199
    # Now enter an initial tag for the tree. Nothing actually gets tagged.
200 201
    DBQueryWarn("insert into archive_tags set idx=NULL, ".
		"  tag='$tag', archive_idx='$idx', view='$view', ".
202 203 204 205 206 207 208
		"  date_created=UNIX_TIMESTAMP(now())") or goto bad;
    
    return $idx;

  bad:
#    mysystem("$RM -rf $dir")
#        if (defined($dir));
209 210 211
    DBQueryFatal("delete from archive_views ".
		 "where view='$view' and archive_idx='$idx'")
	if (defined($idx));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
212
    DBQueryFatal("delete from archives where idx='$idx'")
213 214 215 216 217 218 219 220 221 222
	if (defined($idx));
    return -1;
}

#
# Add a file to an archive. Returns -1 if any error. Otherwise return 0.
# All this does is copy the file (and its directory structure) into the
# temporary store. Later, after all the files are in the tree, must
# commit it to the repo. 
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
223
sub ArchiveAdd($$;$$)
224
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
225
    my ($archive_idx, $pathname, $view, $exact) = @_;
226
    my $rootdir;
227 228 229

    $view = $defaultview
	if (!defined($view));
230

Leigh B. Stoller's avatar
Leigh B. Stoller committed
231 232 233
    $exact = 0
	if (!defined($exact));

234 235 236 237 238 239 240 241 242
    # Taint check path before handing off to shell below.
    if ($pathname =~ /^([-\w\/\.\+\@,~]+)$/) {
	$pathname = $1;
    }
    else {
	print STDERR "ArchiveAdd: ".
	    "Illegal characters in: $pathname\n";
    }

243
    if (! -e $pathname || ! -r $pathname) {
244
	print STDERR "*** ArchiveAdd: $pathname cannot be read!\n";
245
	return 0;
246 247 248
    }

    #
249 250
    # Use realpath to check that the path does not contain links to
    # files outside the directory space the user is allowed to access.
251
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
252
    my $realpath = `$REALPATH $pathname`;
253
    if ($realpath =~ /^([-\w\/\.\+\@,~]+)$/) {
254 255 256
	$realpath = $1;
    }
    else {
257
	print STDERR "ArchiveAdd: ".
258 259
	    "Bad data returned by realpath: $realpath\n";
    }
260

261
    #
262 263
    # Strip leading /dir from the pathname, and taint check it. We want
    # a relative path to the rootdiir so we can copy it in. 
264
    #
265 266 267
    if ($realpath =~ /^[\/]+(\w+)\/([-\w\/\.\+\@,~]+)$/) {
	$rootdir  = $1;
	$pathname = $2;
268 269
    }
    else {
270 271 272 273 274 275 276 277 278 279
	print STDERR "ArchiveAdd: Illegal characters in pathname $pathname\n";
	return -1;
    }

    #
    # The file must reside in one of the Emulab "root" filesystems.
    #
    if (! exists($ROOTS{$rootdir})) {
	print STDERR "ArchiveAdd: ".
	    "$realpath does not resolve to an allowed directory!\n";
280 281 282 283 284 285 286 287
	return -1;
    }

    #
    # See if the archive exists and if it does, get the pathname to it.
    #
    my $directory;
    if (GetArchiveDirectory($archive_idx, \$directory) < 0) {
288
	print STDERR "ArchiveAdd: ".
289 290 291 292
	    "Archive '$archive_idx' does not exist in the DB!\n";
	return -1;
    }
    if (! -d $directory || ! -w $directory) {
293
	print STDERR "ArchiveAdd: $directory cannot be written!\n";
294 295
	return -1;
    }
296
    my $repodir   = "$directory/repo";
297 298
    my $checkout  = "$directory/checkouts/$view";
    my $checkin   = "$directory/checkins/$view";
299 300
    
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
301 302 303 304
    # Copy the file in. We use tar on individual files (to retain the
    # directory structure and mode bits, etc). On a directory, use either
    # tar or rsync, depending on whether we want an exact copy (removing
    # files in the target that are not present in the source). 
305
    #
306 307 308 309 310 311 312
    if (! -e "$checkin/$rootdir") {
	mysystem("$MKDIR $checkin/$rootdir") == 0 or return -1;
    }
    
    if (-f "/${rootdir}/${pathname}" || !$exact) {
	mysystem("$TAR cf - -C /$rootdir $pathname | ".
		 "$TAR xf - -C $checkin/$rootdir");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
313 314
    }
    else {
315 316
	mysystem("cd /$rootdir; ".
		 "$RSYNC -R -ax --delete ${pathname} $checkin/$rootdir");
317 318
    }
    if ($?) {
319
	print STDERR "ArchiveAdd: Could not copy in $realpath\n";
320 321 322 323 324 325
	return -1;
    }
    return 0;
}

#
326 327 328 329 330
# Bring the current contents of the temporary store into the archive as a
# checkpoint (or a savepoint I guess). Implementationlly, its really a
# branch off the current import revision, since it does not become a new
# head revision until the entire branch (all the savepoints) is committed
# later, say when the experiment is swapped out.
331
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
332
sub ArchiveSavePoint($;$$$)
333
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
334
    my ($archive_idx, $savetag, $view, $altdir) = @_;
335 336 337 338 339 340 341 342
    my $cwd;

    $view = $defaultview
	if (!defined($view));

    return -1
	if (TBScriptLock("archive_${archive_idx}", 0, 120) !=
	    TBSCRIPTLOCK_OKAY());
343 344 345 346 347 348

    #
    # See if the archive exists and if it does, get the pathname to it.
    #
    my $directory;
    if (GetArchiveDirectory($archive_idx, \$directory) < 0) {
349
	print STDERR "ArchiveSavePoint: ".
350
	    "Archive '$archive_idx' does not exist in the DB!\n";
351
	goto bad;
352 353
    }
    if (! -d $directory || ! -w $directory) {
354
	print STDERR "ArchiveSavePoint: $directory cannot be written!\n";
355
	goto bad;
356
    }
357
    my $repodir   = "$directory/repo";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
358
    my $checkin   = (defined($altdir) ? $altdir : "$directory/checkins/$view");
359 360 361 362 363 364

    # Need to remember where we came from!
    chomp($cwd = `pwd`);
    # Must do the taint check too.
    if ($cwd =~ /^([-\w\.\/]+)$/) {
	$cwd = $1;
365
    }
366 367
    else {
	print STDERR "ArchiveSavePoint: Bad data in $cwd!\n";
368
	goto bad;
369 370
    }

371
    # Get the current tag for the archive.
372 373
    my ($archive_tag);
    if (GetArchiveCurrentTags($archive_idx, $view, \$archive_tag) < 0) {
374 375 376 377 378 379 380 381 382 383 384 385 386 387
	print STDERR "ArchiveSavePoint: ".
	    "Archive '$archive_idx' does not have a current tag!\n";
	goto bad;
    }

    # Make up a savetag if one not provided.
    if (!defined($savetag)) {
	my ($seconds, $microseconds) = gettimeofday();
	
	$savetag  = POSIX::strftime("T20%y%m%d-%H%M%S-", localtime());
	$savetag .= $microseconds;
	$savetag .= "_savepoint";
    }

388
    # Tags must start with a letter.
389 390 391 392
    if ($savetag =~ /^\d+/) {
	$savetag = "T" . $savetag;
    }
    
393
    #
394
    # Use svn import command. This is nice cause it handles all the
395 396 397
    # subdirs and stuff in one shot, instead of trying to deal with
    # each file and directory individually.
    #
398 399 400 401
    if (! chdir("$checkin")) {
	print STDERR "ArchiveSavePoint: Cannot chdir to $checkin!\n";
	goto bad;
    }
402
    mysystem("$IMPORTER -no_user_input file://$repodir ".
403 404
	     "          $view/savepoint . " .
	     ($debug < 2 ? "> /dev/null" : ""))
405 406 407 408 409
	== 0 or goto bad;

    #
    # Create the tag for this savepoint. 
    # 
410
    mysystem("$SVN copy $svnopt -m 'ArchiveCreate Branch' ".
411 412 413 414
	     "          file://$repodir/$view/savepoint ".
	     "          file://$repodir/$view/tags/${savetag}")
	== 0 or goto bad;
    
415 416 417 418

    #
    # And record the new tag. 
    # 
419
    DBQueryWarn("insert into archive_tags set idx=NULL, ".
420
		"  tag='$savetag', view='$view', ".
421
		"  archive_idx='$archive_idx', ".
422
		"  date_created=UNIX_TIMESTAMP(now())") or goto bad;
423

424
  okay:
425 426 427
    TBScriptUnlock();
    chdir($cwd)
	if (defined($cwd));
428 429 430
    return 0;

  bad:
431 432 433
    TBScriptUnlock();
    chdir($cwd)
	if (defined($cwd));
434 435 436 437 438 439 440
    return -1;
}

#
# Commit the current contents of the temporary store to the archive.
# Returns -1 if any error. Otherwise return 0.
#
441
sub ArchiveCommit($;$$$)
442
{
443
    my ($archive_idx, $newtag, $view, $altdir) = @_;
444
    my $noactivity = 0;
445 446 447 448 449 450 451 452
    my $cwd;

    $view = $defaultview
	if (!defined($view));

    return -1
	if (TBScriptLock("archive_${archive_idx}", 0, 120) !=
	    TBSCRIPTLOCK_OKAY());
453 454 455 456 457 458 459 460

    #
    # See if the archive exists and if it does, get the pathname to it.
    #
    my $directory;
    if (GetArchiveDirectory($archive_idx, \$directory) < 0) {
	print STDERR "ArchiveCommit: ".
	    "Archive '$archive_idx' does not exist in the DB!\n";
461
	goto bad;
462 463 464
    }
    if (! -d $directory || ! -w $directory) {
	print STDERR "ArchiveCommit: $directory cannot be written!\n";
465
	goto bad;
466
    }
467
    my $repodir   = "$directory/repo";
468 469
    my $checkout  = "$directory/checkouts/$view";
    my $checkin   = (defined($altdir) ? $altdir : "$directory/checkins/$view");
470 471 472 473 474 475 476 477

    # Need to remember where we came from!
    chomp($cwd = `pwd`);
    # Must do the taint check too.
    if ($cwd =~ /^([-\w\.\/]+)$/) {
	$cwd = $1;
    }
    else {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
478
	print STDERR "ArchiveCommit: Bad data in $cwd!\n";
479
	goto bad;
480 481
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
482 483
    if (! chdir("$directory")) {
	print STDERR "ArchiveCommit: Cannot chdir to $directory!\n";
484
	goto bad;
485 486 487
    }

    # Get the current tags for the archive.
488
    my ($archive_tag, $previous_tag);
489
    if (GetArchiveCurrentTags($archive_idx, $view, \$archive_tag,
490
			      \$previous_tag) < 0) {
491 492 493 494 495 496
	print STDERR "ArchiveCommit: ".
	    "Archive '$archive_idx' does not have a current tag!\n";
	goto bad;
    }

    #
497 498
    # Form new tag.
    # 
499 500 501
    my ($seconds, $microseconds) = gettimeofday();
    my $import_tag  = POSIX::strftime("T20%y%m%d-%H%M%S-", localtime());
    $import_tag    .= $microseconds;
502 503

    if (!defined($newtag)) {
504
	$newtag = $import_tag . "_commit";
505 506
    }

507
    # Tags cannot must start with a letter.
508 509 510
    if ($newtag =~ /^\d+/) {
	$newtag = "T" . $newtag;
    }
511

512 513 514 515 516 517 518
    #
    # Lets see if there has been any action since the last commit.
    # Check for new tags, and if nothing has happened, just drop a
    # new set of tags in and return.
    #
    my $query_result =
	DBQueryWarn("select tag from archive_tags ".
519
		    "where archive_idx='$archive_idx' and view='$view' ".
520 521 522 523 524 525 526 527 528 529
		    "order by idx desc limit 1");
    goto bad
	if (!$query_result || !$query_result->numrows);
    
    my ($tmptag) = $query_result->fetchrow_array();
    if ($tmptag eq $archive_tag) {
	print "ArchiveCommit: Archive is clean; no need to commit.\n";
	$noactivity = 1;
    }
    
530
    #
531 532 533
    # Okay, do the commit to the trunk for this view.
    # 
    if (! $noactivity) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
534 535 536 537 538 539 540 541 542
	#
	# Create a temporary spot to work in.
	# 
	if (! mkdir("merge.$$", 0777)) {
	    print STDERR "ArchiveCommit: Could not mkdir merge.$$: $!\n";
	    goto bad;
	}
	chdir("merge.$$");
	
543
	#
544
	# Check out the current trunk.
545
	#
546 547 548
	mysystem("$SVN checkout $svnopt file://$repodir/$view/trunk")
	    == 0 or goto bad;

549
	#
550 551 552 553 554 555 556 557 558 559
	# Do a merge from the branch off the previous commit tag, with
	# the savepoint. This is merged into the working directory we
	# just checked out above. This will do the work of scheduling
	# additions and deletions.
	#
	mysystem("$SVN merge $svnopt ".
		 "     file://$repodir/$view/tags/${archive_tag}-branch ".
		 "     file://$repodir/$view/savepoint ".
		 "     trunk")
	    == 0 or goto bad;
560

561 562 563 564 565 566
	#
	# Now commit to the trunk!
	#
	mysystem("$SVN commit $svnopt ".
		 "     -m 'Commit merge of ${archive_tag} to trunk' trunk")
	    == 0 or goto bad;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
567 568 569 570 571 572 573

	# Clean the temp dir,
	chdir("$directory");
	mysystem("/bin/rm -rf merge.$$");
	if ($?) {
	    print STDERR "ArchiveCommit: Could not remove merge.$$\n";
	}
574
    }
575

576
    # Create a tag in the tags directory for the commit.
577
    mysystem("$SVN copy $svnopt -m 'ArchiveCommit' ".
578 579 580 581
	     "          file://$repodir/$view/trunk ".
	     "          file://$repodir/$view/tags/${newtag}")
	== 0 or goto bad;
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
582 583 584
    # Create a tag in the history directory for the commit. The
    # history directory has just the commit tags, so its easy to go
    # back in time.
585
    mysystem("$SVN copy $svnopt -m 'ArchiveCommit' ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
586 587 588 589
	     "          file://$repodir/$view/trunk ".
	     "          file://$repodir/$view/history/${newtag}")
	== 0 or goto bad;
    
590
    # Create a branch tag in the tags directory to base differences against.
591
    mysystem("$SVN copy $svnopt -m 'ArchiveCommit Branch' ".
592 593 594 595
	     "          file://$repodir/$view/trunk ".
	     "          file://$repodir/$view/tags/${newtag}-branch")
	== 0 or goto bad;
    
596
    DBQueryWarn("insert into archive_tags set idx=NULL, ".
597
		"  tag='$newtag', view='$view', ".
598
		"  archive_idx='$archive_idx', ".
599 600
		"  date_created=UNIX_TIMESTAMP(now())")
	or goto bad;
601

602
    DBQueryWarn("update archive_views set ".
603
		"  current_tag='$newtag', ".
604
		"  previous_tag='$archive_tag' ".
605 606
		"where archive_idx='$archive_idx' and view='$view'")
	or goto bad;
607 608

    # Clean the temp dir for later.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
   if (0) {
    if (!defined($altdir)) {
	mysystem("/bin/rm -rf $checkin");
	if ($?) {
	    print STDERR "ArchiveCommit: Could not remove $checkin!\n";
	    goto bad;
	}
	if (! mkdir("$checkin", 0777)) {
	    print STDERR "ArchiveCommit: Could not mkdir $checkin: $!\n";
	    goto bad;
	}
	if (! chmod(0777, "$checkin")) {
	    print STDERR "ArchiveCommit: Could not chmod $checkin: $!\n";
	    goto bad;
	}
624
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
625
   }
626

627 628 629
    # And now into the checkout dir to checkout a current copy.
    if (! chdir("$checkout")) {
	print STDERR "ArchiveCommit: Cannot chdir to $checkout!\n";
630
	goto bad;
631
    }
632
    mysystem("$SVN $svnopt update trunk");
633
    if ($?) {
634
	print STDERR "ArchiveCommit: Could not update head revision!\n";
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
	goto bad;
    }
  okay:
    TBScriptUnlock();
    chdir($cwd)
	if (defined($cwd));
    return 0;

  bad:
    TBScriptUnlock();
    chdir($cwd)
	if (defined($cwd));
    return -1;
}

#
# Fork an archive (okay, branch) for use in separate development. Note that
# once forked, there is no join, as that would be impossibly hard to deal
# with cause of conflicts.
#
655
sub ArchiveFork($$;$$$)
656
{
657 658
    my ($archive_idx, $newview, $branchtag, $newtag, $view) = @_;
    my $sourcepoint;
659 660 661 662 663 664 665 666 667 668 669 670 671 672
    my $cwd;

    $view = $defaultview
	if (!defined($view));

    return -1
	if (TBScriptLock("archive_${archive_idx}", 0, 120) !=
	    TBSCRIPTLOCK_OKAY());

    #
    # See if the archive exists and if it does, get the pathname to it.
    #
    my $directory;
    if (GetArchiveDirectory($archive_idx, \$directory) < 0) {
673
	print STDERR "ArchiveCommit: ".
674 675 676 677 678
	    "Archive '$archive_idx' does not exist in the DB!\n";
	goto bad;
    }
    if (! -d $directory || ! -w $directory) {
	print STDERR "ArchiveCommit: $directory cannot be written!\n";
679
	goto bad;
680
    }
681
    my $repodir     = "$directory/repo";
682 683 684 685 686 687 688 689 690 691 692 693 694
    my $checkouts   = "$directory/checkouts";
    my $checkout    = "$directory/checkouts/$view";
    my $checkin     = "$directory/checkins/$view";
    my $newcheckout = "$directory/checkouts/$newview";
    my $newcheckin  = "$directory/checkins/$newview";

    # Need to remember where we came from!
    chomp($cwd = `pwd`);
    # Must do the taint check too.
    if ($cwd =~ /^([-\w\.\/]+)$/) {
	$cwd = $1;
    }
    else {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
695
	print STDERR "ArchiveFork: Bad data in $cwd!\n";
696 697 698 699
	goto bad;
    }

    # Get the current tags for the archive.
700
    my ($archive_tag, $previous_tag);
701
    if (GetArchiveCurrentTags($archive_idx, $view, \$archive_tag,
702
			      \$previous_tag) < 0) {
703 704 705 706 707
	print STDERR "ArchiveCommit: ".
	    "Archive '$archive_idx' does not have a current tag!\n";
	goto bad;
    }

708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
    #
    # Form new tag.
    # 
    my ($seconds, $microseconds) = gettimeofday();
    my $import_tag  = POSIX::strftime("T20%y%m%d-%H%M%S-", localtime());
    $import_tag    .= $microseconds;

    if (!defined($newtag)) {
	$newtag = $import_tag . "_fork";
    }

    # Tags cannot must start with a letter.
    if ($newtag =~ /^\d+/) {
	$newtag = "T" . $newtag;
    }

724 725 726 727 728 729 730 731 732
    # Create new view directories and checkout.
    if (! mkdir("$newcheckin", 0777)) {
	print STDERR "ArchiveCreate: Could not mkdir $newcheckin: $!\n";
	goto bad;
    }
    if (! chmod(0777, "$newcheckin")) {
	print STDERR "ArchiveCreate: Could not chmod $newcheckin: $!\n";
	goto bad;
    }
733 734

    # Create newview directory in the repo.
735
    mysystem("$SVN mkdir $svnopt -m 'ArchiveFork' ".
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
	     "          file://$repodir/$newview")
	== 0 or goto bad;

    # Split at the trunk if no branchpoint provided.
    if (defined($branchtag)) {
	$sourcepoint = "tags/$branchtag";
    }
    else {
	$sourcepoint = "trunk"
    }

    # Copy the trunk of the old view.
    mysystem("$SVN copy $svnopt -m 'ArchiveFork' ".
	     "          file://$repodir/$view/$sourcepoint ".
	     "          file://$repodir/$newview/trunk")
	== 0 or goto bad;

753 754 755 756 757 758 759
    # Ditto for the savepoints directory. 
    mysystem("$SVN copy $svnopt -m 'ArchiveFork' ".
	     "          file://$repodir/$view/savepoint ".
	     "          file://$repodir/$newview/savepoint")
	== 0 or goto bad;

    # Do not want to copy the tags/history directories. Add new ones.
760
    mysystem("$SVN mkdir $svnopt -m 'ArchiveFork' ".
761
	     "          file://$repodir/$newview/history ".
762 763 764
	     "          file://$repodir/$newview/tags")
	== 0 or goto bad;

765 766 767 768 769 770
    # Create a branch tag in the tags directory to base differences against.
    mysystem("$SVN copy $svnopt -m 'ArchiveFork Branch' ".
	     "          file://$repodir/$newview/trunk ".
	     "          file://$repodir/$newview/tags/${newtag}-branch")
	== 0 or goto bad;
    
771
    # Now check it out. This creates the checkouts/$newview directory.
772
    mysystem("cd $checkouts; ".
773
	     "$SVN checkout $svnopt file://$repodir/$newview")
774 775
	== 0 or goto bad;

776 777 778 779 780
    # Now enter the newview (okay, branch) of this archive.
    DBQueryWarn("insert into archive_views set ".
		"  current_tag='$newtag', archive_idx='$archive_idx', ".
		"  view='$newview', ".
		"  date_created=UNIX_TIMESTAMP(now())") or goto bad;
781
    
782 783 784
    # Now enter an initial tag for the new view. Nothing actually gets tagged.
    DBQueryWarn("insert into archive_tags set idx=NULL, ".
		"  tag='$newtag', archive_idx='$archive_idx', ".
785
		"  view='$newview', ".
786
		"  date_created=UNIX_TIMESTAMP(now())") or goto bad;
787

788
  okay:
789 790 791
    TBScriptUnlock();
    chdir($cwd)
	if (defined($cwd));
792
    return 0;
793 794

  bad:
795 796 797
    TBScriptUnlock();
    chdir($cwd)
	if (defined($cwd));
798
    return -1;
799 800
}

801 802 803
#
# Checkout a copy of the archive, optionally at a particular view/branch.
#
804
sub ArchiveCheckout($$;$$$)
805
{
806
    my ($archive_idx, $target, $view, $tag, $subdir) = @_;
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
    my $sourcepoint;
    my $cwd;

    $view = $defaultview
	if (!defined($view));

    return -1
	if (TBScriptLock("archive_${archive_idx}", 0, 120) !=
	    TBSCRIPTLOCK_OKAY());

    #
    # See if the archive exists and if it does, get the pathname to it.
    #
    my $directory;
    if (GetArchiveDirectory($archive_idx, \$directory) < 0) {
	print STDERR "ArchiveCheckout: ".
	    "Archive '$archive_idx' does not exist in the DB!\n";
	goto bad;
    }
    if (! -d $directory || ! -w $directory) {
	print STDERR "ArchiveCheckout: $directory cannot be written!\n";
	goto bad;
    }
    my $repodir     = "$directory/repo";

    #
    # We allow target to exist or not exist.
    #
    if (! -e $target) {
	if (! mkdir("$target", 0777)) {
	    print STDERR "ArchiveCheckout: Could not mkdir $target: $!\n";
	    return -1;
	}
	if (! chmod(0777, "$target")) {
	    print STDERR "ArchiveCheckout: ".
		"Could not chmod directory $target: $!\n";
	    return -1;
	}
    }

    # Need to remember where we came from!
    chomp($cwd = `pwd`);
    # Must do the taint check too.
    if ($cwd =~ /^([-\w\.\/]+)$/) {
	$cwd = $1;
    }
    else {
	print STDERR "ArchiveCheckout: Bad data in $cwd!\n";
	goto bad;
    }

    # Where to find what we want.
    if (defined($tag)) {
	$sourcepoint = "tags/$tag";
    }
    else {
	$sourcepoint = "trunk"
    }
865 866 867
    # Allowed to check out a particular subdir (but not a file).
    $sourcepoint = $sourcepoint . "/$subdir"
	if (defined($subdir));
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885

    # Now check it out. This creates the $checkouts/$view directory.
    mysystem("cd $target; ".
	     "$SVN checkout $svnopt file://$repodir/$view/$sourcepoint .")
	== 0 or goto bad;
  okay:
    TBScriptUnlock();
    chdir($cwd)
	if (defined($cwd));
    return 0;

  bad:
    TBScriptUnlock();
    chdir($cwd)
	if (defined($cwd));
    return -1;
}

886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
#
# Archive the Archive, moving it into the expinfo directory.
# I intend this to be run only when an experiment is terminated.
# No need to have two copies.
#
sub ArchiveArchive($$)
{
    my ($archive_idx, $dirpath) = @_;
    my $target = "$dirpath/Archive";

    #
    # See if the archive exists and if it does, get the pathname to it.
    #
    my $directory;
    if (GetArchiveDirectory($archive_idx, \$directory) < 0) {
901
	print STDERR "ArchiveArchive: ".
902 903 904
	    "Archive '$archive_idx' does not exist in the DB!\n";
	return -1;
    }
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920

    #
    # Need additional check to make sure that it has not already been
    # archived.
    #
    my ($archived, $date_archived);
    
    if (IsArchiveArchived($archive_idx, \$archived, \$date_archived) < 0) {
	return -1;
    }
    if ($archived) {
	print STDERR "ArchiveArchive: ".
	    "Archive '$archive_idx' already archived on $date_archived!\n";
	return 0;
    }
    
921
    if (! -d $directory || ! -w $directory) {
922
	print STDERR "ArchiveArchive: $directory cannot be written!\n";
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
	return -1;
    }

    if (! -e $target) {
	if (! mkdir("$target", 0777)) {
	    print STDERR "ArchiveArchive: Could not mkdir $target: $!\n";
	    return -1;
	}
	if (! chmod(0777, "$target")) {
	    print STDERR "ArchiveArchive: ".
		"Could not chmod directory $target: $!\n";
	    return -1;
	}
    }    

    #
    # Tar up the whole thing and move it across.
    #
941
    mysystem("$TAR cf - -C $directory repo | tar xf - -C $target");
942 943 944 945
    if ($?) {
	print STDERR "ArchiveArchive: Could not copy in $directory\n";
	return -1;
    }
946 947 948 949

    #
    # Update its location in the DB, and remove the old directory.
    # 
950 951 952 953
    DBQueryWarn("update archives set ".
		"   directory='$target', ".
		"   archived=1, ".
		"   date_archived=UNIX_TIMESTAMP(now()) ".
954 955 956 957 958 959 960 961 962
		"where idx='$archive_idx'")
	or return -1;
    
    mysystem("/bin/rm -rf $directory");
    if ($?) {
	print STDERR "ArchiveArchive: ".
	    "Could not remove contents of $directory!\n";
	return -1;
    }
963 964 965 966 967 968 969
    return 0;
}

#
# Destroy an archive. The DB state is retained unless optional flag says
# to clean it.
#
970
sub ArchiveDestroy($$;$)
971
{
972 973 974 975
    my ($archive_idx, $clean, $view) = @_;

    $view = $defaultview
	if (!defined($view));
976 977 978 979 980 981 982 983 984 985

    #
    # See if the archive exists and if it does, get the pathname to it.
    #
    my $directory;
    if (GetArchiveDirectory($archive_idx, \$directory) < 0) {
	print STDERR "ArchiveDestroy: ".
	    "Archive '$archive_idx' does not exist in the DB!\n";
	return -1;
    }
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001

    #
    # Need additional check to make sure that it has not already been
    # archived. Do not want to do anything, unless clean is specified.
    #
    my ($archived, $date_archived);
    
    if (IsArchiveArchived($archive_idx, \$archived, \$date_archived) < 0) {
	return -1;
    }
    if ($archived && !$clean) {
	print STDERR "ArchiveDestroy: ".
	    "Archive '$archive_idx' archived on $date_archived!\n";
	return 0;
    }
    
1002 1003 1004 1005
    if (! -d $directory || ! -w $directory) {
	return 0;
    }

1006 1007 1008
    # Is it shared? If so, certainly do not delete it!
    my $shared;
    if (IsArchiveShared($archive_idx, \$shared) < 0) {
1009 1010
	return -1;
    }
1011 1012 1013 1014 1015 1016 1017 1018 1019
    if (! $shared) {
	mysystem("/bin/rm -rf $directory");
	if ($?) {
	    print STDERR "ArchiveDestroy: ".
		"Could not remove contents of $directory!\n";
	    return -1;
	}
    }
    
1020
    if ($clean) {
1021
	(DBQueryWarn("delete from archive_tags ".
1022
		     "where archive_idx='$archive_idx' and view='$view'") &&
1023
	 DBQueryWarn("delete from archive_views ".
1024 1025 1026 1027 1028 1029 1030
		     "where archive_idx='$archive_idx' and view='$view'"))
	    || return -1;
	
	if (! $shared) {
	    DBQueryWarn("delete from archives ".
			"where idx='$archive_idx'") || return -1;
	}
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
    }
    return 0;
}

#
# Get the directory for an archive, given its index. Returns -1 on error,
# zero otherwise. 
#
sub GetArchiveDirectory($$)
{
    my ($idx, $rvalp) = @_;
    
    my $query_result =
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1044
	DBQueryWarn("select directory from archives where idx='$idx'");
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054

    return -1
	if (!$query_result || !$query_result->numrows);
    my ($dir) = $query_result->fetchrow_array();

    $$rvalp = $dir
	if (defined($rvalp));
    return 0;
}

1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
#
# See if the archive has already been archived away, and when.
#
sub IsArchiveArchived($$$)
{
    my ($idx, $parch, $pdate) = @_;
    
    my $query_result =
	DBQueryWarn("select archived,FROM_UNIXTIME(date_archived) ".
		    "  from archives where idx='$idx'");

    return -1
	if (!$query_result || !$query_result->numrows);
    
    my ($archived,$date_archived) = $query_result->fetchrow_array();

    $$parch = $archived
	if (defined($parch));
    $$pdate = $date_archived
	if (defined($pdate));
    
    return 0;
}

1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
#
# See if the archive is shared amongst more then one experiment. This
# only looks at current experiments. Once an archive is archived, not
# sure what we will do if the user want to branch from it. 
#
sub IsArchiveShared($$)
{
    my ($idx, $prval) = @_;
    
    my $query_result =
	DBQueryWarn("select e.pid,e.eid,s.archive_idx from experiments as e ".
		    "left join experiment_stats as s on s.exptidx=e.idx ".
		    "where s.archive_idx='$idx'");

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

1102 1103 1104 1105
#
# Get the current tag for an archive, given its index. Returns -1 on error,
# zero otherwise. Place tag in the return pointer.
#
1106
sub GetArchiveCurrentTags($$$;$)
1107
{
1108
    my ($archive_idx, $view, $tagp, $prevp) = @_;
1109 1110
    
    my $query_result =
1111
	DBQueryWarn("select v.current_tag,v.previous_tag ".