@matheusbsbs / matheus-bagliano / commits / a5e407988b
Fork 0

git-cvsserver: detect/diagnose write failure, etc.

There were many operations that did not notice and report errors to the CVS client, which would have resulted in corrupt working tree. Signed-off-by: Jim Meyering <jim@meyering.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jim Meyering committed Jul 14, 2007 at 20:48 UTC a5e407988b35b7353bd03c770afc647670c25981
1 file changed +33 -17
git-cvsserver.perl
+33 -17
@@ -623,8 +623,12 @@ sub req_Modified
623 my ( $cmd, $data ) = @_;
624
625 my $mode = <STDIN>;
626 + defined $mode
627 + or (print "E end of file reading mode for $data\n"), return;
628 chomp $mode;
629 my $size = <STDIN>;
630 + defined $size
631 + or (print "E end of file reading size of $data\n"), return;
632 chomp $size;
633
634 # Grab config information
@@ -644,7 +648,8 @@ sub req_Modified
648 $bytesleft -= $blocksize;
649 }
650
647 - close $fh;
651 + close $fh
652 + or (print "E failed to write temporary, $filename: $!\n"), return;
653
654 # Ensure we have something sensible for the file mode
655 if ( $mode =~ /u=(\w+)/ )
@@ -901,8 +906,13 @@ sub req_update
906 # projects (heads in this case) to checkout.
907 #
908 if ($state->{module} eq '') {
909 + my $heads_dir = $state->{CVSROOT} . '/refs/heads';
910 + if (!opendir HEADS, $heads_dir) {
911 + print "E [server aborted]: Failed to open directory, "
912 + . "$heads_dir: $!\nerror\n";
913 + return 0;
914 + }
915 print "E cvs update: Updating .\n";
905 - opendir HEADS, $state->{CVSROOT} . '/refs/heads';
916 while (my $head = readdir(HEADS)) {
917 if (-f $state->{CVSROOT} . '/refs/heads/' . $head) {
918 print "E cvs update: New directory `$head'\n";
@@ -1737,14 +1747,16 @@ sub req_annotate
1747 system("git-read-tree", $lastseenin);
1748 unless ($? == 0)
1749 {
1740 - die "Error running git-read-tree $lastseenin $file_index $!";
1750 + print "E error running git-read-tree $lastseenin $file_index $!\n";
1751 + return;
1752 }
1753 $log->info("Created index '$file_index' with commit $lastseenin - exit status $?");
1754
1755 # do a checkout of the file
1756 system('git-checkout-index', '-f', '-u', $filename);
1757 unless ($? == 0) {
1747 - die "Error running git-checkout-index -f -u $filename : $!";
1758 + print "E error running git-checkout-index -f -u $filename : $!\n";
1759 + return;
1760 }
1761
1762 $log->info("Annotate $filename");
@@ -1754,7 +1766,11 @@ sub req_annotate
1766 # git-jsannotate telling us about commits we are hiding
1767 # from the client.
1768
1757 - open(ANNOTATEHINTS, ">$tmpdir/.annotate_hints") or die "Error opening > $tmpdir/.annotate_hints $!";
1769 + my $a_hints = "$tmpdir/.annotate_hints";
1770 + if (!open(ANNOTATEHINTS, '>', $a_hints)) {
1771 + print "E failed to open '$a_hints' for writing: $!\n";
1772 + return;
1773 + }
1774 for (my $i=0; $i < @$revisions; $i++)
1775 {
1776 print ANNOTATEHINTS $revisions->[$i][2];
@@ -1765,11 +1781,14 @@ sub req_annotate
1781 }
1782
1783 print ANNOTATEHINTS "\n";
1768 - close ANNOTATEHINTS;
1784 + close ANNOTATEHINTS
1785 + or (print "E failed to write $a_hints: $!\n"), return;
1786
1770 - my $annotatecmd = 'git-annotate';
1771 - open(ANNOTATE, "-|", $annotatecmd, '-l', '-S', "$tmpdir/.annotate_hints", $filename)
1772 - or die "Error invoking $annotatecmd -l -S $tmpdir/.annotate_hints $filename : $!";
1787 + my @cmd = (qw(git-annotate -l -S), $a_hints, $filename);
1788 + if (!open(ANNOTATE, "-|", @cmd)) {
1789 + print "E error invoking ". join(' ',@cmd) .": $!\n";
1790 + return;
1791 + }
1792 my $metadata = {};
1793 print "E Annotations for $filename\n";
1794 print "E ***************\n";
@@ -1996,12 +2015,12 @@ sub transmitfile
2015 {
2016 open NEWFILE, ">", $targetfile or die("Couldn't open '$targetfile' for writing : $!");
2017 print NEWFILE $_ while ( <$fh> );
1999 - close NEWFILE;
2018 + close NEWFILE or die("Failed to write '$targetfile': $!");
2019 } else {
2020 print "$size\n";
2021 print while ( <$fh> );
2022 }
2004 - close $fh or die ("Couldn't close filehandle for transmitfile()");
2023 + close $fh or die ("Couldn't close filehandle for transmitfile(): $!");
2024 } else {
2025 die("Couldn't execute git-cat-file");
2026 }
@@ -2501,17 +2520,14 @@ sub update
2520 if ($parent eq $lastpicked) {
2521 next;
2522 }
2504 - open my $p, 'git-merge-base '. $lastpicked . ' '
2505 - . $parent . '|';
2506 - my @output = (<$p>);
2507 - close $p;
2508 - my $base = join('', @output);
2523 + my $base = safe_pipe_capture('git-merge-base',
2524 + $lastpicked, $parent);
2525 chomp $base;
2526 if ($base) {
2527 my @merged;
2528 # print "want to log between $base $parent \n";
2529 open(GITLOG, '-|', 'git-log', "$base..$parent")
2514 - or die "Cannot call git-log: $!";
2530 + or die "Cannot call git-log: $!";
2531 my $mergedhash;
2532 while (<GITLOG>) {
2533 chomp;