#!/usr/bin/perl -w
#
# git caching proxy
-#
+
# usage: run it on some port, and then clone or fetch
# "git://<realhost>:<realport>/<real-git-url>[ <options>]"
# where <real-git-url> is http://<host>/... or git://<host>/...
# fetch=try use what is in the cache if the fetch/clone fails
# timeout=<seconds> length of time to allow for fetch/clone
+# git-cache-proxy is free software; you can redistribute it and/or
+# modify them under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 3, or (at
+# your option) any later version.
+#
+# git-cache-proxy is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, consult the Free Software Foundation's
+# website at www.fsf.org, or the GNU Project website at www.gnu.org.
+#
+# (Some code taken from userv-utils's git-daemon.in and git-service.in
+# which were written by Tony Finch <dot@dotat.at> and subsequently
+# heavily modified by Ian Jackson <ijackson@chiark.greenend.org.uk>
+# and were released under CC0 1.0. The whole program is now GPLv3+.)
+
use strict;
use warnings;
use Fcntl qw(:flock SEEK_SET);
our $us = 'git-cache-proxy';
+
+#---------- error handling and logging ----------
+
+# This is a bit fiddly, because we want to catch errors sent to stderr
+# and dump them to syslog if we can, but only if we are running as an
+# inetd service.
+
our $log; # filehandle (ref), or "1" meaning syslog
-BEGIN {
- open STDERR, ">/dev/null" or exit 255;
- open TEMPERR, "+>", undef or exit 255;
- open STDERR, ">&TEMPERR" or exit 255;
+sub ntoa {
+ my $sockaddr = shift;
+ return ('(local)') unless defined $sockaddr;
+ my ($port,$addr) = sockaddr_in $sockaddr;
+ $addr = inet_ntoa $addr;
+ return ("[$addr]:$port",$addr,$port);
+}
- sub ntoa {
- my $sockaddr = shift;
- return ('(local)') unless defined $sockaddr;
- my ($port,$addr) = sockaddr_in $sockaddr;
- $addr = inet_ntoa $addr;
- return ("[$addr]:$port",$addr,$port);
- }
+our ($client) = ntoa getpeername STDIN;
+our ($server) = ntoa getsockname STDIN;
- our ($client,$client_addr,$client_port) = ntoa getpeername STDIN;
- our ($server,$server_addr,$server_port) = ntoa getsockname STDIN;
+sub ensurelog () {
+ return if $log;
+ openlog $us, qw(pid), 'daemon';
+ $log = 1;
+}
- sub ensurelog () {
- return if $log;
- openlog $us, qw(pid), 'daemon';
- $log = 1;
+sub logm ($$) {
+ my ($pri, $msg) = @_;
+ if ($client eq '(local)') {
+ print STDERR "$us: $pri: $msg\n" or die $!;
+ exit 1;
}
-
- sub logm ($$) {
- my ($pri, $msg) = @_;
- ensurelog();
- my $mainmsg = sprintf "%s-%s: %s", $server, $client, $msg;
- if (ref $log) {
- my $wholemsg = sprintf("%s [%d] %s: %s\n",
- strftime("%Y-%m-%d %H:%M:%S Z", gmtime),
- $$,
- $pri,
- $mainmsg);
- print $log $wholemsg;
- } else {
- syslog $pri, $mainmsg;
- }
+ ensurelog();
+ my $mainmsg = sprintf "%s-%s: %s", $server, $client, $msg;
+ if (ref $log) {
+ my $wholemsg = sprintf("%s [%d] %s: %s\n",
+ strftime("%Y-%m-%d %H:%M:%S Z", gmtime),
+ $$,
+ $pri,
+ $mainmsg);
+ print $log $wholemsg;
+ } else {
+ syslog $pri, $mainmsg;
}
+}
+
+if ($client ne '(local)') {
+ open STDERR, ">/dev/null" or exit 255;
+ open TEMPERR, "+>", undef or exit 255;
+ open STDERR, ">&TEMPERR" or exit 255;
+}
- END {
+END {
+ if ($client ne '(local)') {
if ($?) { logm 'crit', "crashing ($?)"; }
seek TEMPERR, 0, SEEK_SET;
while (<TEMPERR>) {
chomp;
logm 'crit', $_;
}
- exit $?;
}
+ exit $?;
}
-our $fetchtimeout = 1800;
-our $maxfetchtimeout = 3600;
-
sub fail ($) {
my ($msg) = @_;
logm 'err', $msg;
exit 0;
}
+#---------- argument parsing ----------
+
+our $fetchtimeout = 1800;
+our $maxfetchtimeout = 3600;
our $cachedir = '/var/cache/git-cache-proxy';
for (;;) {
!@ARGV or fail "bad usage: no non-option arguments permitted";
+#---------- main program ----------
+
chdir $cachedir or fail "chdir $cachedir: $!";
our ($service,$specpath,$spechost);