Index: trunk/ippScripts/scripts/permcheck.pl
===================================================================
--- trunk/ippScripts/scripts/permcheck.pl	(revision 39955)
+++ trunk/ippScripts/scripts/permcheck.pl	(revision 39956)
@@ -344,175 +344,205 @@
     foreach my $key (@keys) {
 	# Testing code.
-	print "$key\n";
-	next;
-    }
-
-# 	# neb-stat level handling
-# 	my $stat = $neb->stat($key);
-# 	die "nebulous key: $key not found" unless $stat;   
-# 	my $instances;
-# 	my $md5sum = '';
+#	print "$key\n";
+#	next;
+#       }
+
+ 	# neb-stat level handling
+ 	my $stat = $neb->stat($key);
+ 	die "nebulous key: $key not found" unless $stat;   
+ 	my $instances;
+ 	my $md5sum = '';
 
 # 	# This needs to be in an eval, because although we expect things to exist, they
 # 	# may not.  This is a fatal error in rawcheck, but need not be here.
-# 	eval {
-# 	    $instances = $neb->find_instances($key, 'any');
-# 	};
-# 	unless (defined($instances)) { print "## skipping due to zero instances\n"; next; }
-# 	die "no instances found" unless $instances;   
-	
-# 	my $Ngood;
-# 	my %good_instances = ();
-# 	my %bad_instances  = ();
-
-# 	my @files = map {URI->new($_)->file if $_} @$instances;
-# 	my @validation = ();
-
-# 	for (my $i = 0; $i <= $#files; $i++) {
-# 	    my ($instance_exists,$instance_md5sum,$instance_host,$instance_volume,$instance_site, $is_good);
-# 	    ($instance_host,$instance_volume) = parse_volume($files[$i]);
-# 	    $instance_site = $volume_map{$instance_volume};
-# 	    $is_good = 0;
-
-# 	    if (-e $files[$i]) {
-# 		$instance_exists = 1;
-# 		$existing_copies++;
-# 		$instance_md5sum = local_md5sum($files[$i]);
-
-# 		# This is bad, but I don't know what the right solution is.  We don't have 
-# 		# the md5sum a priori.  I think this is also the only major change needed 
-# 		# from rawcheck.pl
-# 		if (($md5sum eq '')&&($instance_md5sum ne 'd41d8cd98f00b204e9800998ecf8427e')) {
-# 		    $md5sum = $instance_md5sum;
-# 		}
-
-# 		if ($instance_md5sum eq $md5sum) {
-# 		    push @{ $good_instance{$instance_site} }, $i;
-# 		    $is_good = 1;
-# 		    $Ngood++;
-# 		}
-# 		else {
-# 		    push @{ $bad_instances{$instance_site} }, $i;
-# 		}
-# 	    }
-# 	    else {
-# 		$instance_exists = 0;
-# 		$instance_md5sum = 'NON-EXISTANT';
-# 		push @{ $bad_instances{$instance_site} }, $i;
-# 	    }
+ 	eval {
+ 	    $instances = $neb->find_instances($key, 'any');
+ 	};
+ 	unless (defined($instances)) { print "## skipping due to zero instances\n"; next; }
+ 	die "no instances found" unless $instances;   
+
+	my $existing_copies = 0;
+ 	my $Ngood = 0;
+	my $Nbad = 0;
+ 	my %good_instances = ();
+ 	my %bad_instances  = ();
+
+	#unsafe
+	my $continue_checking_md5sums = 1;
+	my %good_instances_unchecked  = ();
+	#end unsafe
+
+ 	my @files = map {URI->new($_)->file if $_} @$instances;
+ 	my @validation = ();
+
+ 	for (my $i = 0; $i <= $#files; $i++) {
+ 	    my ($instance_exists,$instance_md5sum,$instance_host,$instance_volume,$instance_site, $is_good);
+ 	    ($instance_host,$instance_volume) = parse_volume($files[$i]);
+ 	    $instance_site = $volume_map{$instance_volume};
+ 	    $is_good = 0;
+
+ 	    if (-e $files[$i]) {
+ 		$instance_exists = 1;
+ 		$existing_copies++;
+		if ($continue_checking_md5sums == 1) { #unsafe
+		    $instance_md5sum = local_md5sum($files[$i]);
+
+		    # This is bad, but I don't know what the right solution is.  We don't have 
+		    # the md5sum a priori.  I think this is also the only major change needed 
+		    # from rawcheck.pl
+		    if (($md5sum eq '')&&($instance_md5sum ne 'd41d8cd98f00b204e9800998ecf8427e')) {
+			$md5sum = $instance_md5sum;
+		    }
+		    
+		    if ($instance_md5sum eq $md5sum) {
+			push @{ $good_instances{$instance_site} }, $i;
+			$is_good = 1;
+			$Ngood++;
+		    }               
+		    else {
+			push @{ $bad_instances{$instance_site} }, $i;
+			$Nbad++;
+		    }
+		} #unsafe
+		else { #unsafe
+		    $instance_md5sum = 'CHECKING_STOPPED';
+		    push @{ $good_instances_unchecked{$instance_site} }, $i;
+		    $is_good = 1;
+		    $Ngood++;
+		} #end unsafe
+	    }
+	    else {
+		$instance_exists = 0;
+		$instance_md5sum = 'NON-EXISTANT';
+		push @{ $bad_instances{$instance_site} }, $i;
+	    }
 	    
-# 	    $validation[$i] = sprintf("    % 3d %d %32s %s %s %s\n",
-# 				      $instance_exists,$is_good,$instance_md5sum,
-# 				      $files[$i],$instance_host,$instance_volume);
-# 	}
-
-# 	$time = time() - $timer_start;
-#         # object_id ext_id epoch available existing total timer
-# 	printf("%s %s %s %d %d %d %d\n",@$stat[0],@$stat[1],@$stat[4],@$stat[6],$existing_copies,@$stat[7],$timer);
-# 	# instance_exists is_good instance_md5sum file instance_host instance_volume
-# 	my $val_string = join('',@validation);
-# 	print "$val_string";
+	    $validation[$i] = sprintf("    % 3d %d %32s %s %s %s\n",
+				      $instance_exists,$is_good,$instance_md5sum,
+				      $files[$i],$instance_host,$instance_volume);
+
+	    #unsafe
+	    # Remove this section when we decide to go back to actually checking everything
+	    # to make sure we don't have any secret data corruption
+	    if ($Ngood > 0) {
+		print "# Breaking from validation check, as a valid copy has been found.\n";
+		$continue_checking_md5sums = 0;
+	    }
+	    #end unsafe
+	}
+
+ 	$timer = time() - $timer_start;
+        # object_id ext_id epoch available existing total timer
+ 	printf("%s %s %s %d %d %d %d\n",@$stat[0],@$stat[1],@$stat[4],@$stat[6],$existing_copies,@$stat[7],$timer);
+ 	# instance_exists is_good instance_md5sum file instance_host instance_volume
+ 	my $val_string = join('',@validation);
+ 	print "$val_string";
 
 # 	# Decide what to do
 	
-# 	if ($Ngood == 0) {
-# 	    # If we have no copies, we can't do anything.
-# 	    die "No valid instance of key: $key\n";
-# 	}
-# 	## We have more than zero bad copies.  We may cull some of these in the future, but we should try to
-# 	## leave everything in the best state possible.
-# 	printf(">> %d %d\n",$Ngood, $#files + 1);
-# 	my $site_key; # Scope issues?
-
-# 	if ($Ngood != $#files + 1) {
-# 	    my $good_copy;
-# 	    my $good_copy_index;
-# 	    foreach $site_key (keys %good_instances) {
-# 		if ($#{ $good_instances{$site_key} } != -1) {
-# 		    $good_copy_index = $good_instances{$site_key}[0];
-# 		    $good_copy = $files[$good_copy_index];
-# 		    last;
-# 		}
-# 	    }
-# 	    printf(">> GOOD: $good_copy\n");
-# 	    foreach $site_key (keys %bad_instances) {
-# 		foreach my $bad_copy_index (@{ $bad_instances{$site_key} }) {
-# 		    print "cp $good_copy $files[$bad_copy_index]\n";
-# 		    if ($do_ops) {
-# 			system("cp $good_copy $files[$bad_copy_index]");
-# 		    }
-# 		    my $tmpmd5 = local_md5sum($files[$bad_copy_index]);
-# 		    if ($tmpmd5 ne $md5sum) { 
-#                     ## This isn't super critical, so we don't need to die here.
-# 			warn "Post-repair md5sum does not match! $tmpmd5 != $md5sum: $files[$bad_copy_index]";
-# 		    }
-# 		    else {
-#                     ## success
-# 			push @{ $good_instances{$site_key} }, $bad_copy_index;
-# 		    }
-# 		}
-# 	    }
-#         ## We've done work here, so we can't do a cull this iteration.
-# 	    if ($do_cull == 1) { $do_cull = -1; }
-# 	}
-
-# 	## We can now attempt to make replicated copies to the sites that require additional copies.
-# 	foreach $site_key (keys %requirement_map) {
-# 	    my $have_instances = $#{ $good_instances{$site_key} } + 1;
-# 	    print "## $site_key $have_instances $requirement_map{$site_key}\n";
-# 	    if ($#{ $good_instances{$site_key} } + 1 < $requirement_map{$site_key}) {
-# 		my $rep_vol = get_random_site_volume($site_key);
-# 		print "neb-replicate --volume $rep_vol  $key\n";
-# 		if ($do_ops) {
-# 		    $neb->replicate($key,$rep_vol) or die "failed to replicate the single valid copy to the backup node";
-# 		    if ($@) { die $@; }
-                
-#                 # Begin my best validation thought
-# 		    system("sync") == 0 or die "Couldn't sync?";
-# 		    my $uris = $neb->find_instances($key,$rep_vol);
-# 		    @$uris = map {URI->new($_)->file if $_} @$uris;
-# 		    my $tmpmd5 = local_md5sum(${ $uris }[0]);
-
-#                 my $validation_str = sprintf("% 3d %d %32s %s %s %s",
-#                                           -1,-1,$tmpmd5,
-# 					     ${ $uris }[0],"repl",$rep_vol);
-#                 print
-#                     join("\n" . " " x 4, $validation_str), "\n";
-                
-# 		    if ($tmpmd5 ne $md5sum) { 
-# 			die "Post-replication md5sum does not match! $tmpmd5 != $md5sum";
-# 		    }
-#                 # End my best validation thought.
-# 		}       
-#             ## We've done work here, so we can't do a cull this iteration.
-# 		if ($do_cull == 1) { $do_cull = -1; }
-# 	    }
-# 	}
+ 	if ($Ngood == 0) {
+ 	    # If we have no copies, we can't do anything.
+ 	    die "No valid instance of key: $key\n";
+ 	}
+ 	## We have more than zero bad copies.  We may cull some of these in the future, but we should try to
+ 	## leave everything in the best state possible.
+ 	printf(">> %d %d\n",$Ngood, $#files + 1);
+	if ($Nbad > 0) {
+	    my $good_copy;
+	    my $good_copy_index;
+	    foreach my $site_key (keys %good_instances) {
+		if ($#{ $good_instances{$site_key} } != -1) {
+		    $good_copy_index = $good_instances{$site_key}[0];
+		    $good_copy = $files[$good_copy_index];
+		    last;
+		}
+	    }
+	    printf(">> GOOD: $good_copy\n");
+	    foreach my $site_key (keys %bad_instances) {
+		foreach my $bad_copy_index (@{ $bad_instances{$site_key} }) {
+		    print "cp $good_copy $files[$bad_copy_index]\n";
+		    if ($do_ops) {
+			system("cp $good_copy $files[$bad_copy_index]");
+		    }
+		    my $tmpmd5 = local_md5sum($files[$bad_copy_index]);
+		    if ($tmpmd5 ne $md5sum) { 
+                    ## This isn't super critical, so we don't need to die here.
+			warn "Post-repair md5sum does not match! $tmpmd5 != $md5sum: $files[$bad_copy_index]";
+		    }
+		    else {
+                    ## success
+			push @{ $good_instances{$site_key} }, $bad_copy_index;
+		    }
+		}
+	    }
+	    ## We've done work here, so we can't do a cull this iteration.
+	    if ($do_cull == 1) { $do_cull = -1; }
+	}
+
+	#unsafe
+	foreach my $site_key (keys %good_instances_unchecked) {
+	    push @{ $good_instances{$site_key} }, @{ $good_instances_unchecked{$site_key} };
+	}
+	#end unsafe
+	
+	## We can now attempt to make replicated copies to the sites that require additional copies.
+	foreach my $site_key (keys %requirement_map) {
+	    my $have_instances = $#{ $good_instances{$site_key} } + 1;
+	    print "## $site_key $have_instances $requirement_map{$site_key}\n";
+	    if ($#{ $good_instances{$site_key} } + 1 < $requirement_map{$site_key}) {
+		my $rep_vol = get_random_site_volume($site_key);
+		print "neb-replicate --volume $rep_vol  $key\n";
+		if ($do_ops) {
+		    $neb->replicate($key,$rep_vol) or die "failed to replicate the single valid copy to the backup node";
+		    if ($@) { die $@; }
+		    
+		    # Begin my best validation thought
+		    system("sync") == 0 or die "Couldn't sync?";
+		    my $uris = $neb->find_instances($key,$rep_vol);
+		    @$uris = map {URI->new($_)->file if $_} @$uris;
+		    my $tmpmd5 = local_md5sum(${ $uris }[0]);
+		    
+		    my $validation_str = sprintf("% 3d %d %32s %s %s %s",
+						 -1,-1,$tmpmd5,
+						 ${ $uris }[0],"repl",$rep_vol);
+		    print
+			join("\n" . " " x 4, $validation_str), "\n";
+		    
+		    if ($tmpmd5 ne $md5sum) { 
+			die "Post-replication md5sum does not match! $tmpmd5 != $md5sum";
+		    }
+		    # End my best validation thought.
+		}       
+		## We've done work here, so we can't do a cull this iteration.
+		if ($do_cull == 1) { $do_cull = -1; }
+	    }
+	}
+	
+	## Do culls if that's what we were going to do.
+	if ($do_cull == -1) {
+	    die "Cull option passed, but files were modified in the scan/repair/replicate phase.  Not running cull!\n";
+	}
+	elsif ($do_cull == 1) {
+	    ## At this point, we should have no files in the bad_instances lists, because we've repaired them.
+	    foreach my $site_key (keys %good_instances) {
+		if ($#{ $good_instances{$site_key} } + 1 > $requirement_map{$site_key}) {
+		    for ($i = $requirement_map{$site_key}; $i <= $#{ $good_instances{$site_key} }; $i++) {
+			my $cull_index = ${ $good_instances{$site_key} }[$i];
+			my ($instance_host,$instance_volume) = parse_volume($files[$cull_index]);
+			print "neb-cull --volume $instance_volume $key\n";
+			if ($do_ops) {
+			    # The tilde here is to force hard volumes.  Don't touch it.
+			    # Also: the 2 is a "minimum number of copies" restriction.  Let's not be crazy here.
+			    $neb->cull($key,"~${instance_volume}",2) or die "failed to cull a superfluous instance";
+			    if ($@) { die "$@"; }
+			}
+		    } # End loop over extra instances
+		} # End check for sites with extra instances
+	    } # End loop over sites.
+	} # End cull
+	
+    } ## End loop over keys.
+} ## Endd loop over stage components.
     
-#     ## Do culls if that's what we were going to do.
-# 	if ($do_cull == -1) {
-# 	    die "Cull option passed, but files were modified in the scan/repair/replicate phase.  Not running cull!\n";
-# 	}
-# 	elsif ($do_cull == 1) {
-#         ## At this point, we should have no files in the bad_instances lists, because we've repaired them.
-# 	    foreach my $site_key (keys %good_instances) {
-# 		if ($#{ $good_instances{$site_key} } + 1 > $requirement_map{$site_key}) {
-# 		    for ($i = $requirement_map{$site_key}; $i <= $#{ $good_instances{$site_key} }; $i++) {
-# 			my $cull_index = ${ $good_instances{$site_key} }[$i];
-#                     my ($instance_host,$instance_volume) = parse_volume($files[$cull_index]);
-#                     print "neb-cull --volume $instance_volume $key\n";
-#                     if ($do_ops) {
-#                         # The tilde here is to force hard volumes.  Don't touch it.
-#                         # Also: the 2 is a "minimum number of copies" restriction.  Let's not be crazy here.
-#                         $neb->cull($key,"~${instance_volume}",2) or die "failed to cull a superfluous instance";
-#                         if ($@) { die "$@"; }
-#                     }
-#                 } # End loop over extra instances
-#             } # End check for sites with extra instances
-#         } # End loop over sites.
-#     } # End cull
-
-} ## End loop over imfiles.
-
 
 sub local_md5sum {
@@ -522,5 +552,5 @@
     $host =~ s/\.\d//;
 #    print "$filename $host $volume\n";
-    my $response = `ssh $host md5sum $filename`;
+    my $response = `ssh $host remote_md5sum.pl $filename`;
     chomp($response);
     my ($sum, undef) = split /\s+/, $response;
