]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Fetch.pm
6a6daee6d6601ba43b803e7ee918f59aeac9715b
[public-inbox.git] / lib / PublicInbox / Fetch.pm
1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 # Wrapper to "git fetch" remote public-inboxes
4 package PublicInbox::Fetch;
5 use strict;
6 use v5.10.1;
7 use parent qw(PublicInbox::IPC);
8 use URI ();
9 use PublicInbox::Spawn qw(popen_rd);
10 use PublicInbox::Admin;
11 use PublicInbox::LEI;
12 use PublicInbox::LeiCurl;
13 use PublicInbox::LeiMirror;
14 use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
15 use File::Temp ();
16
17 sub new { bless {}, __PACKAGE__ }
18
19 sub fetch_cmd ($$) {
20         my ($lei, $opt) = @_;
21         my @cmd = qw(git);
22         $opt->{$_} = $lei->{$_} for (0..2);
23         # we support "-c $key=$val" for arbitrary git config options
24         # e.g.: git -c http.proxy=socks5h://127.0.0.1:9050
25         push(@cmd, '-c', $_) for @{$lei->{opt}->{c} // []};
26         push @cmd, 'fetch';
27         push @cmd, '-q' if $lei->{opt}->{quiet};
28         push @cmd, '-v' if $lei->{opt}->{verbose};
29         @cmd;
30 }
31
32 sub remote_url ($$) {
33         my ($lei, $dir) = @_; # TODO: support non-"origin"?
34         my $cmd = [ qw(git config remote.origin.url) ];
35         my $fh = popen_rd($cmd, undef, { -C => $dir, 2 => $lei->{2} });
36         my $url = <$fh>;
37         close $fh or return;
38         $url =~ s!/*\n!!s;
39         $url;
40 }
41
42 sub do_manifest ($$$) {
43         my ($lei, $dir, $ibx_uri) = @_;
44         my $muri = URI->new("$ibx_uri/manifest.js.gz");
45         my $ft = File::Temp->new(TEMPLATE => 'manifest-XXXX',
46                                 UNLINK => 1, DIR => $dir);
47         my $fn = $ft->filename;
48         my @opt = (qw(-R -o), $fn);
49         my $mf = "$dir/manifest.js.gz";
50         my $m0; # current manifest.js.gz contents
51         if (open my $fh, '<', $mf) {
52                 $m0 = eval {
53                         PublicInbox::LeiMirror::decode_manifest($fh, $mf, $mf)
54                 };
55                 $lei->err($@) if $@;
56         }
57         my $curl_cmd = $lei->{curl}->for_uri($lei, $muri, @opt);
58         my $opt = {};
59         $opt->{$_} = $lei->{$_} for (0..2);
60         my $cerr = PublicInbox::LeiMirror::run_reap($lei, $curl_cmd, $opt);
61         if ($cerr) {
62                 return [ 404 ] if ($cerr >> 8) == 22; # 404 Missing
63                 $lei->child_error($cerr, "@$curl_cmd failed");
64                 return;
65         }
66         my $m1 = PublicInbox::LeiMirror::decode_manifest($ft, $fn, $muri);
67         my $mdiff = { %$m1 };
68
69         # filter out unchanged entries.  We check modified, too, since
70         # fingerprints are SHA-1, so there's a teeny chance they'll collide
71         while (my ($k, $v0) = each %{$m0 // {}}) {
72                 my $cur = $m1->{$k} // next;
73                 my $f0 = $v0->{fingerprint} // next;
74                 my $f1 = $cur->{fingerprint} // next;
75                 my $t0 = $v0->{modified} // next;
76                 my $t1 = $cur->{modified} // next;
77                 delete($mdiff->{$k}) if $f0 eq $f1 && $t0 == $t1;
78         }
79         return unless keys %$mdiff;
80         my (undef, $v1_path, @v2_epochs) =
81                 PublicInbox::LeiMirror::deduce_epochs($mdiff, $ibx_uri->path);
82         [ 200, $v1_path, \@v2_epochs, $muri, $ft, $mf ];
83 }
84
85 sub do_fetch {
86         my ($cls, $lei, $cd) = @_;
87         my $ibx_ver;
88         $lei->{curl} //= PublicInbox::LeiCurl->new($lei) or return;
89         my $dir = PublicInbox::Admin::resolve_inboxdir($cd, \$ibx_ver);
90         my ($ibx_uri, @git_dir, @epochs);
91         if ($ibx_ver == 1) {
92                 my $url = remote_url($lei, $dir) //
93                         die "E: $dir missing remote.origin.url\n";
94                 $ibx_uri = URI->new($url);
95         } else { # v2:
96                 opendir my $dh, "$dir/git" or die "opendir $dir/git: $!";
97                 @epochs = sort { $b <=> $a } map { substr($_, 0, -4) + 0 }
98                                         grep(/\A[0-9]+\.git\z/, readdir($dh));
99                 my ($git_url, $epoch);
100                 for my $nr (@epochs) { # try newest epoch, first
101                         my $edir = "$dir/git/$nr.git";
102                         if (defined(my $url = remote_url($lei, $edir))) {
103                                 $git_url = $url;
104                                 $epoch = $nr;
105                                 last;
106                         } else {
107                                 warn "W: $edir missing remote.origin.url\n";
108                         }
109                 }
110                 $git_url or die "Unable to determine git URL\n";
111                 my $inbox_url = $git_url;
112                 $inbox_url =~ s!/git/$epoch(?:\.git)?/?\z!! or
113                         $inbox_url =~ s!/$epoch(?:\.git)?/?\z!! or die <<EOM;
114 Unable to infer inbox URL from <$git_url>
115 EOM
116                 $ibx_uri = URI->new($inbox_url);
117         }
118         $lei->qerr("# inbox URL: $ibx_uri/");
119         my $res = do_manifest($lei, $dir, $ibx_uri) or return;
120         my ($code, $v1_path, $v2_epochs, $muri, $ft, $mf) = @$res;
121         if ($code == 404) {
122                 # any pre-manifest.js.gz instances running? Just fetch all
123                 # existing ones and unconditionally try cloning the next
124                 $v2_epochs = [ map {;
125                                 "$dir/git/$_.git";
126                                 } @epochs ];
127                 push @$v2_epochs, "$dir/git/".($epochs[-1] + 1) if @epochs;
128         } else {
129                 $code == 200 or die "BUG unexpected code $code\n";
130         }
131         if ($ibx_ver == 2) {
132                 defined($v1_path) and warn <<EOM;
133 E: got v1 `$v1_path' when expecting v2 epoch(s) in <$muri>, WTF?
134 EOM
135                 @git_dir = map { "$dir/git/$_.git" } sort { $a <=> $b }
136                         map { my ($nr) = (m!/([0-9]+)\.git\z!g) } @$v2_epochs;
137         } else {
138                 $git_dir[0] = $dir;
139         }
140         # n.b. this expects all epochs are from the same host
141         my $torsocks = $lei->{curl}->torsocks($lei, $muri);
142         for my $d (@git_dir) {
143                 my $cmd;
144                 my $opt = {}; # for spawn
145                 if (-d $d) {
146                         $opt->{-C} = $d;
147                         $cmd = [ @$torsocks, fetch_cmd($lei, $opt) ];
148                 } else {
149                         my $e_uri = $ibx_uri->clone;
150                         my ($epath) = ($d =~ m!(/git/[0-9]+\.git)\z!);
151                         defined($epath) or
152                                 die "BUG: $d is not an epoch to clone\n";
153                         $e_uri->path($ibx_uri->path.$epath);
154                         $cmd = [ @$torsocks,
155                                 PublicInbox::LeiMirror::clone_cmd($lei, $opt),
156                                 $$e_uri, $d];
157                 }
158                 my $cerr = PublicInbox::LeiMirror::run_reap($lei, $cmd, $opt);
159                 # do not bail on clone failure if we didn't have a manifest
160                 if ($cerr && ($code == 200 || -d $d)) {
161                         $lei->child_error($cerr, "@$cmd failed");
162                         return;
163                 }
164         }
165         if ($ft) {
166                 my $fn = $ft->filename;
167                 rename($fn, $mf) or die "E: rename($fn, $mf): $!\n";
168                 $ft->unlink_on_destroy(0);
169         }
170 }
171
172 1;