chiark / gitweb /
git-cache-proxy: wip housekeeping, before introduce new locking protocol
[chiark-utils.git] / scripts / git-cache-proxy
index df522a0126238d40d97a0c342ea7e22910269bd5..12a4f52cdff44f3f412b6ce0cc4ca32574609393 100755 (executable)
@@ -40,6 +40,7 @@ use POSIX;
 use Socket;
 use Sys::Syslog;
 use Fcntl qw(:flock SEEK_SET);
 use Socket;
 use Sys::Syslog;
 use Fcntl qw(:flock SEEK_SET);
+use File::Path qw(remove_tree);
 
 our $us = 'git-cache-proxy';
 
 
 our $us = 'git-cache-proxy';
 
@@ -126,6 +127,8 @@ sub gitfail ($) {
 
 #---------- argument parsing ----------
 
 
 #---------- argument parsing ----------
 
+our $housekeepingthreshdays = 1;
+our $treeexpiredays = 21;
 our $fetchtimeout = 1800;
 our $maxfetchtimeout = 3600;
 our $cachedir = '/var/cache/git-cache-proxy';
 our $fetchtimeout = 1800;
 our $maxfetchtimeout = 3600;
 our $cachedir = '/var/cache/git-cache-proxy';
@@ -156,71 +159,81 @@ for (;;) {
 
 chdir $cachedir or fail "chdir $cachedir: $!";
 
 
 chdir $cachedir or fail "chdir $cachedir: $!";
 
-our ($service,$specpath,$spechost);
-
-$SIG{ALRM} = sub { fail "timeout" };
-alarm 30;
+our ($service,$specpath,$spechost,$subdir);
+our ($tmpd,$gitd,$lock);
+our ($fetch,$url);
 
 sub xread {
     my $length = shift;
     my $buffer = "";
     while ($length > length $buffer) {
         my $ret = sysread STDIN, $buffer, $length, length $buffer;
 
 sub xread {
     my $length = shift;
     my $buffer = "";
     while ($length > length $buffer) {
         my $ret = sysread STDIN, $buffer, $length, length $buffer;
-        fail "Expected $length bytes, got ".length $buffer
+        fail "expected $length bytes, got ".length $buffer
                             if defined $ret and $ret == 0;
         fail "read: $!" if not defined $ret and $! != EINTR and $! != EAGAIN;
     }
     return $buffer;
 }
                             if defined $ret and $ret == 0;
         fail "read: $!" if not defined $ret and $! != EINTR and $! != EAGAIN;
     }
     return $buffer;
 }
-my $hex_len = xread 4;
-fail "Bad hex in packet length" unless $hex_len =~ m|^[0-9a-fA-F]{4}$|;
-my $line = xread -4 + hex $hex_len;
-unless (($service,$specpath,$spechost) = $line =~
-        m|^(git-[a-z-]+) /*([!-~ ]+)\0host=([!-~]+)\0$|) {
-    $line =~ s|[^ -~]+| |g;
-    gitfail "unknown/unsupported instruction `$line'"
+
+sub servinfo ($) {
+    my ($msg) = @_;
+    logm 'info', "service `$specpath': $msg";
 }
 
 }
 
-alarm 0;
-
-$service eq 'git-upload-pack'
-    or gitfail "unknown/unsupported service `$service'";
-
-my $fetch = 2; # 0:don't; 1:try; 2:force
-my $url = $specpath;
-
-while ($url =~ s#\s+(\[)([^][{}]+)\]$## ||
-       $url =~ s#\s+(\{)([^][{}]+)\}$##) {
-    $_ = $2;
-    my $must = $1 eq '{';
-    if (m/^fetch=try$/) {
-       $fetch = 1;
-    } elsif (m/^fetch=no$/) {
-       $fetch = 0;
-    } elsif (m/^fetch=must$/) {
-       $fetch = 2; # the default
-    } elsif (m/^timeout=(\d+)$/) {
-       $fetchtimeout = $1 <= $maxfetchtimeout ? $1 : $maxfetchtimeout;
-    } elsif ($must) {
-       gitfail "unknown/unsupported option `$_'";
+sub readcommand () {
+    $SIG{ALRM} = sub { fail "timeout" };
+    alarm 30;
+
+    my $hex_len = xread 4;
+    fail "Bad hex in packet length" unless $hex_len =~ m|^[0-9a-fA-F]{4}$|;
+    my $line = xread -4 + hex $hex_len;
+    unless (($service,$specpath,$spechost) = $line =~
+           m|^(git-[a-z-]+) /*([!-~ ]+)\0host=([!-~]+)\0$|) {
+       $line =~ s|[^ -~]+| |g;
+       gitfail "unknown/unsupported instruction `$line'"
     }
     }
-}
 
 
-$url =~ m{^(?:https?|git)://[-.0-9a-z]+/}
-    or gitfail "unknown/unsupported url scheme or format `$url'";
+    alarm 0;
+
+    $service eq 'git-upload-pack'
+       or gitfail "unknown/unsupported service `$service'";
+
+    $fetch = 2; # 0:don't; 1:try; 2:force
+    $url = $specpath;
+
+    while ($url =~ s#\s+(\[)([^][{}]+)\]$## ||
+          $url =~ s#\s+(\{)([^][{}]+)\}$##) {
+       $_ = $2;
+       my $must = $1 eq '{';
+       if (m/^fetch=try$/) {
+           $fetch = 1;
+       } elsif (m/^fetch=no$/) {
+           $fetch = 0;
+       } elsif (m/^fetch=must$/) {
+           $fetch = 2; # the default
+       } elsif (m/^timeout=(\d+)$/) {
+           $fetchtimeout = $1 <= $maxfetchtimeout ? $1 : $maxfetchtimeout;
+       } elsif ($must) {
+           gitfail "unknown/unsupported option `$_'";
+       }
+    }
 
 
-our $subdir = $url;
-$subdir =~ s|\\|\\\\|g;
-$subdir =~ s|,|\\,|g;
-$subdir =~ s|/|,|g;
+    $url =~ m{^(?:https?|git)://[-.0-9a-z]+/}
+        or gitfail "unknown/unsupported url scheme or format `$url'";
 
 
-logm 'info', "$specpath locking";
+    $subdir = $url;
+    $subdir =~ s|\\|\\\\|g;
+    $subdir =~ s|,|\\,|g;
+    $subdir =~ s|/|,|g;
 
 
-my $tmpd= "$subdir\\.tmp";
-my $gitd= "$subdir\\.git";
-my $lock = "$subdir\\.lock";
+    $tmpd= "$subdir\\.tmp";
+    $gitd= "$subdir\\.git";
+    $lock = "$subdir\\.lock";
 
 
-for (;;) {
+    servinfo "locking";
+}
+
+sub clonefetch () {
     open LOCK, "+>", $lock or fail "open/create $lock: $!";
     flock LOCK, LOCK_EX or fail "lock exclusive $lock: $!";
 
     open LOCK, "+>", $lock or fail "open/create $lock: $!";
     flock LOCK, LOCK_EX or fail "lock exclusive $lock: $!";
 
@@ -236,10 +249,10 @@ for (;;) {
        if (!$exists) {
            system qw(rm -rf --), $tmpd;
            @cmd = (qw(git clone -q --mirror), $url, $tmpd);
        if (!$exists) {
            system qw(rm -rf --), $tmpd;
            @cmd = (qw(git clone -q --mirror), $url, $tmpd);
-           logm 'info', "$specpath cloning @cmd";
+           servinfo "cloning";
        } else {
            @cmd = (qw(git remote update --prune));
        } else {
            @cmd = (qw(git remote update --prune));
-           logm 'info', "$specpath fetching @cmd";
+           servinfo "fetching";
        }
        my $cmd = "@cmd[0..1]";
 
        }
        my $cmd = "@cmd[0..1]";
 
@@ -258,10 +271,9 @@ for (;;) {
        my $timedout = 0;
        {
            local $SIG{ALRM} = sub {
        my $timedout = 0;
        {
            local $SIG{ALRM} = sub {
-               logm 'info', "$specpath fetch/clone timeout";
+               servinfo "fetch/clone timeout";
                $timedout=1; kill 9, -$child;
            };
                $timedout=1; kill 9, -$child;
            };
-logm 'info', "timeout=$fetchtimeout";
            alarm($fetchtimeout);
            $!=0; { local $/=undef; $fetcherr = <FETCHERR>; }
            !FETCHERR->error or fail "read pipe from fetch/clone: $!";
            alarm($fetchtimeout);
            $!=0; { local $/=undef; $fetcherr = <FETCHERR>; }
            !FETCHERR->error or fail "read pipe from fetch/clone: $!";
@@ -284,7 +296,7 @@ logm 'info', "timeout=$fetchtimeout";
            if ($fetch >= 2) {
                gitfail $fetchfail;
            } else {
            if ($fetch >= 2) {
                gitfail $fetchfail;
            } else {
-               logm 'info', "$specpath fetch/clone failed: $fetchfail";
+               servinfo "fetch/clone failed: $fetchfail";
            }
        }
 
            }
        }
 
@@ -300,21 +312,91 @@ logm 'info', "timeout=$fetchtimeout";
        gitfail "no cached data, and not cloned: $fetchfail";
     }
 
        gitfail "no cached data, and not cloned: $fetchfail";
     }
 
-    logm 'info', "$specpath sharing";
+    servinfo "sharing";
     flock LOCK, LOCK_UN or fail "unlock $lock: $!";
     flock LOCK, LOCK_SH or fail "lock shared $lock: $!";
     # actually, just relocking as shared would have the same semantics
     # but it's best to be explicit
 
     if (chdir $gitd) {
     flock LOCK, LOCK_UN or fail "unlock $lock: $!";
     flock LOCK, LOCK_SH or fail "lock shared $lock: $!";
     # actually, just relocking as shared would have the same semantics
     # but it's best to be explicit
 
     if (chdir $gitd) {
-       last;
+       return 1;
     }
     $!==ENOENT or fail "chdir $gitd: $!";
 
     # Well, err, someone must have taken the lock in between
     # and garbage collected it.  How annoying.
     }
     $!==ENOENT or fail "chdir $gitd: $!";
 
     # Well, err, someone must have taken the lock in between
     # and garbage collected it.  How annoying.
+    return 0;
+}
+
+sub housekeeping () {
+    foreach $lock (<[a-z]*\\.lock>) {
+       if (!lstat $lock) {
+           $! == ENOENT or fail "housekeeping: $lock: stat: $!";
+           next;
+       }
+       if (-M _ <= $treeexpiredays) {
+           logm 'debug', "housekeeping: $lock: not too old";
+           next;
+       }
+       my $subdir = $lock;  $subdir =~ s/\\.lock$//;
+       my $ok = 1;
+       foreach my $suffix (qw(tmp git)) {
+           my $dir = "${subdir}\\.$suffix";
+           my $errs;
+           remove_tree($dir, { safe=>1, error=>\$errs });
+           $ok = 0 if @$errs;
+           foreach my $err (@$errs) {
+               logm 'warning', "problem deleting: $err[0]: $err[1]";
+           }
+       }
+       if ($ok) {
+           
+
+sub housekeepingcheck ($$) {
+    my ($dofork, $force) = @_;
+    open HLOCK, "+>", "Housekeeping.lock" 
+       or fail "open/create Housekeeping.lock: $!";
+    if (!$force) {
+       if (flock HLOCK, LOCK_EX|LOCK_NB) {
+           logm 'debug', "housekeeping lock taken, not running";
+           close HLOCK;
+           return 0;
+       }
+    }
+    if ($force) {
+       logm 'info', "housekeeping forced";
+    } elsif (!lstat "Housekeeping.stamp") {
+       $! == ENOENT or fail "stat housekeeping.stamp: $!";
+       logm 'info', "housekeeping stamp missing, will run";
+    } elsif (-M _ <= $housekeepingthreshdays) {
+       logm 'debug', "housekeeping done recently";
+       close HLOCK;
+       return 0;
+    }
+    if ($dofork) {
+       my $child = fork;
+       defined $child or fail "fork for housekeeping: $!";
+       if (!$child) {
+           housekeeping();
+           exit 0;
+       }
+       return 1;
+    } else {
+       housekeeping();
+       return 1;
+    }
+}
+
+sub runcommand () {
+    servinfo "servicing";
+    exec qw(git-upload-pack --strict --timeout=1000 .)
+       or fail "exec git-upload-pack: $!";
+}
+
+sub daemonservice () {
+    readcommand();
+    while (!clonefetch()) { }
+    runcommand();
 }
 
 }
 
-logm 'info', "$specpath servicing";
-exec qw(git-upload-pack --strict --timeout=1000 .)
-    or fail "exec git-upload-pack: $!";
+daemonservice();