chiark / gitweb /
git-cache-proxy: gc: Break out $gccheck
[chiark-utils.git] / scripts / git-cache-proxy
index 518ca9a8fe8c34b816b34e1a0dd3b176e703bbbc..48c397c4fb0d4b81dcef369fc678772e98b1edd2 100755 (executable)
@@ -321,6 +321,19 @@ sub clonefetch () {
 
     if ($fetch) {
 
+       my $rbits = '';
+       vec($rbits,0,1) = 1;
+       my $ebits = $rbits;
+       my $r=select $rbits,undef,$ebits,0;
+       $r>=0 or fail "select recheck STDOUT failed: $!";
+       if ($r) {
+           servinfo 'client disconnected (stdin unexpectedly'.
+               (vec($rbits,0,1) ? ' readable' : '').
+               (vec($ebits,0,1) ? ' exception' : '').
+               ')';
+           exit 0;
+       }
+
        our @cmd;
 
        if (!$exists) {
@@ -417,29 +430,37 @@ sub housekeeping () {
        }
        my ($mode_what,$mode_locknb,$mode_action);
        if (-M _ <= $treeexpiredays) {
-           if (!lstat "$gcdone") {
-               $! == ENOENT or hkfail "$gcdone: lstat: $!";
-               logm 'debug',
- "housekeeping: subdirs $subdir: touched recently, never gc'd!";
-           } elsif (-M _ <= $gcintervaldays) {
-               logm 'debug',
- "housekeeping: subdirs $subdir: touched recently, gc'd recently";
-               next;
-           } else {
-               logm 'debug',
- "housekeeping: subdirs $subdir: touched recently, needs gc";
-           }
+           my $gccheck = sub {
+               if (!lstat "$gcdone") {
+                   $! == ENOENT or hkfail "$gcdone: lstat: $!";
+                   return 1, "touched recently, never gc'd!";
+               } elsif (-M _ <= $gcintervaldays) {
+                   return 0, "touched recently, gc'd recently";
+               } else {
+                   return 1, "touched recently, needs gc";
+               }
+           };
+           my ($needsgc, $gcmsg) = $gccheck->();
+           logm 'debug', "housekeeping: subdirs $subdir: $gcmsg";
+           next unless $needsgc;
            $mode_what = 'garbage collecting';
            $mode_locknb = 0;
            $mode_action = sub {
                my $gclog = "$subdir/gc.log";
                unlink $gclog or $!==ENOENT or hkfail "remove $gclog: $!";
-               my $r = system qw(sh -ec),
-                   'cd "$1"; shift; exec "$@"', 'x', "$subdir\\.git",
-                   qw(git gc --quiet);
-               if ($r) {
+               my $child = fork // hkfail "fork (for $subdir): $!";
+               if (!$child) {
+                   if (!chdir "$subdir\\.git") {
+                       exit 0 if $!==ENOENT;
+                       die "for gc: chdir $subdir: $!\n";
+                   }
+                   exec qw(git gc --quiet);
+                   die "exec git gc (for $subdir): $!\n";
+               }
+               waitpid($child, 0) == $child or hkfail "waitpid failed! $!";
+               if ($?) {
                    logm 'err',
                      "housekeeping: subdirs $subdir: gc failed (status $r)";
"housekeeping: subdirs $subdir: gc failed (wait status $?)";
                } else {
                    update_gcstamp("$subdir\\.git");
                    logm 'debug',