]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Inbox.pm
inbox: perform cleanup of Git objects for coderepos
[public-inbox.git] / lib / PublicInbox / Inbox.pm
1 # Copyright (C) 2016-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # Represents a public-inbox (which may have multiple mailing addresses)
5 package PublicInbox::Inbox;
6 use strict;
7 use warnings;
8 use PublicInbox::Git;
9 use PublicInbox::MID qw(mid2path);
10 use Devel::Peek qw(SvREFCNT);
11 use PublicInbox::MIME;
12 use POSIX qw(strftime);
13
14 my $cleanup_timer;
15 eval {
16         $cleanup_timer = 'disabled';
17         require PublicInbox::EvCleanup;
18         $cleanup_timer = undef; # OK if we get here
19 };
20 my $cleanup_broken = $@;
21
22 my $CLEANUP = {}; # string(inbox) -> inbox
23 sub cleanup_task () {
24         $cleanup_timer = undef;
25         my $next = {};
26         for my $ibx (values %$CLEANUP) {
27                 my $again;
28                 foreach my $f (qw(mm search)) {
29                         delete $ibx->{$f} if SvREFCNT($ibx->{$f}) == 1;
30                 }
31                 my $expire = time - 60;
32                 if (my $git = $ibx->{git}) {
33                         $again = $git->cleanup($expire);
34                 }
35                 if (my $gits = $ibx->{-repo_objs}) {
36                         foreach my $git (@$gits) {
37                                 $again = 1 if $git->cleanup($expire);
38                         }
39                 }
40                 $again ||= !!($ibx->{mm} || $ibx->{search});
41                 $next->{"$ibx"} = $ibx if $again;
42         }
43         $CLEANUP = $next;
44 }
45
46 sub _cleanup_later ($) {
47         my ($self) = @_;
48         return if $cleanup_broken;
49         return unless PublicInbox::EvCleanup::enabled();
50         $cleanup_timer ||= PublicInbox::EvCleanup::later(*cleanup_task);
51         $CLEANUP->{"$self"} = $self;
52 }
53
54 sub _set_uint ($$$) {
55         my ($opts, $field, $default) = @_;
56         my $val = $opts->{$field};
57         if (defined $val) {
58                 $val = $val->[-1] if ref($val) eq 'ARRAY';
59                 $val = undef if $val !~ /\A\d+\z/;
60         }
61         $opts->{$field} = $val || $default;
62 }
63
64 sub _set_limiter ($$$) {
65         my ($self, $pi_config, $pfx) = @_;
66         my $lkey = "-${pfx}_limiter";
67         $self->{$lkey} ||= eval {
68                 # full key is: publicinbox.$NAME.httpbackendmax
69                 my $mkey = $pfx.'max';
70                 my $val = $self->{$mkey} or return;
71                 my $lim;
72                 if ($val =~ /\A\d+\z/) {
73                         require PublicInbox::Qspawn;
74                         $lim = PublicInbox::Qspawn::Limiter->new($val);
75                 } elsif ($val =~ /\A[a-z][a-z0-9]*\z/) {
76                         $lim = $pi_config->limiter($val);
77                         warn "$mkey limiter=$val not found\n" if !$lim;
78                 } else {
79                         warn "$mkey limiter=$val not understood\n";
80                 }
81                 $lim;
82         }
83 }
84
85 sub new {
86         my ($class, $opts) = @_;
87         my $v = $opts->{address} ||= 'public-inbox@example.com';
88         my $p = $opts->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
89         $opts->{domain} = ($p =~ /\@(\S+)\z/) ? $1 : 'localhost';
90         my $pi_config = delete $opts->{-pi_config};
91         _set_limiter($opts, $pi_config, 'httpbackend');
92         _set_uint($opts, 'feedmax', 25);
93         $opts->{nntpserver} ||= $pi_config->{'publicinbox.nntpserver'};
94         my $dir = $opts->{mainrepo};
95         if (defined $dir && -f "$dir/inbox.lock") {
96                 $opts->{version} = 2;
97         }
98         bless $opts, $class;
99 }
100
101 sub git_part {
102         my ($self, $part) = @_;
103         ($self->{version} || 1) == 2 or return;
104         $self->{"$part.git"} ||= eval {
105                 my $git_dir = "$self->{mainrepo}/git/$part.git";
106                 my $g = PublicInbox::Git->new($git_dir);
107                 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
108                 # no cleanup needed, we never cat-file off this, only clone
109                 $g;
110         };
111 }
112
113 sub git {
114         my ($self) = @_;
115         $self->{git} ||= eval {
116                 my $git_dir = $self->{mainrepo};
117                 $git_dir .= '/all.git' if (($self->{version} || 1) == 2);
118                 my $g = PublicInbox::Git->new($git_dir);
119                 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
120                 _cleanup_later($self);
121                 $g;
122         };
123 }
124
125 sub max_git_part {
126         my ($self) = @_;
127         my $v = $self->{version};
128         return unless defined($v) && $v == 2;
129         my $part = $self->{-max_git_part};
130         my $changed = git($self)->alternates_changed;
131         if (!defined($part) || $changed) {
132                 $self->git->cleanup if $changed;
133                 my $gits = "$self->{mainrepo}/git";
134                 if (opendir my $dh, $gits) {
135                         my $max = -1;
136                         while (defined(my $git_dir = readdir($dh))) {
137                                 $git_dir =~ m!\A(\d+)\.git\z! or next;
138                                 $max = $1 if $1 > $max;
139                         }
140                         $part = $self->{-max_git_part} = $max if $max >= 0;
141                 } else {
142                         warn "opendir $gits failed: $!\n";
143                 }
144         }
145         $part;
146 }
147
148 sub mm {
149         my ($self) = @_;
150         $self->{mm} ||= eval {
151                 require PublicInbox::Msgmap;
152                 _cleanup_later($self);
153                 my $dir = $self->{mainrepo};
154                 if (($self->{version} || 1) >= 2) {
155                         PublicInbox::Msgmap->new_file("$dir/msgmap.sqlite3");
156                 } else {
157                         PublicInbox::Msgmap->new($dir);
158                 }
159         };
160 }
161
162 sub search {
163         my ($self) = @_;
164         $self->{search} ||= eval {
165                 _cleanup_later($self);
166                 PublicInbox::Search->new($self, $self->{altid});
167         };
168 }
169
170 sub try_cat {
171         my ($path) = @_;
172         my $rv = '';
173         if (open(my $fh, '<', $path)) {
174                 local $/;
175                 $rv = <$fh>;
176         }
177         $rv;
178 }
179
180 sub description {
181         my ($self) = @_;
182         my $desc = $self->{description};
183         return $desc if defined $desc;
184         $desc = try_cat("$self->{mainrepo}/description");
185         local $/ = "\n";
186         chomp $desc;
187         $desc =~ s/\s+/ /smg;
188         $desc = '($REPO_DIR/description missing)' if $desc eq '';
189         $self->{description} = $desc;
190 }
191
192 sub cloneurl {
193         my ($self) = @_;
194         my $url = $self->{cloneurl};
195         return $url if $url;
196         $url = try_cat("$self->{mainrepo}/cloneurl");
197         my @url = split(/\s+/s, $url);
198         local $/ = "\n";
199         chomp @url;
200         $self->{cloneurl} = \@url;
201 }
202
203 sub base_url {
204         my ($self, $env) = @_;
205         if ($env) { # PSGI env
206                 my $scheme = $env->{'psgi.url_scheme'};
207                 my $host_port = $env->{HTTP_HOST} ||
208                         "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
209                 my $url = "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/');
210                 # for mount in Plack::Builder
211                 $url .= '/' if $url !~ m!/\z!;
212                 $url .= $self->{name} . '/';
213         } else {
214                 # either called from a non-PSGI environment (e.g. NNTP/POP3)
215                 $self->{-base_url} ||= do {
216                         my $url = $self->{url} or return undef;
217                         # expand protocol-relative URLs to HTTPS if we're
218                         # not inside a web server
219                         $url = "https:$url" if $url =~ m!\A//!;
220                         $url .= '/' if $url !~ m!/\z!;
221                         $url;
222                 };
223         }
224 }
225
226 sub nntp_url {
227         my ($self) = @_;
228         $self->{-nntp_url} ||= do {
229                 # no checking for nntp_usable here, we can point entirely
230                 # to non-local servers or users run by a different user
231                 my $ns = $self->{nntpserver};
232                 my $group = $self->{newsgroup};
233                 my @urls;
234                 if ($ns && $group) {
235                         $ns = [ $ns ] if ref($ns) ne 'ARRAY';
236                         @urls = map {
237                                 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
238                                 $u .= '/' if $u !~ m!/\z!;
239                                 $u.$group;
240                         } @$ns;
241                 }
242
243                 my $mirrors = $self->{nntpmirror};
244                 if ($mirrors) {
245                         my @m;
246                         foreach (@$mirrors) {
247                                 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
248                                 if ($u =~ m!\Anntps?://[^/]+/?\z!) {
249                                         if ($group) {
250                                                 $u .= '/' if $u !~ m!/\z!;
251                                                 $u .= $group;
252                                         } else {
253                                                 warn
254 "publicinbox.$self->{name}.nntpmirror=$_ missing newsgroup name\n";
255                                         }
256                                 }
257                                 # else: allow full URLs like:
258                                 # nntp://news.example.com/alt.example
259                                 push @m, $u;
260                         }
261                         my %seen = map { $_ => 1 } @urls;
262                         foreach my $u (@m) {
263                                 next if $seen{$u};
264                                 $seen{$u} = 1;
265                                 push @urls, $u;
266                         }
267                 }
268                 \@urls;
269         };
270 }
271
272 sub nntp_usable {
273         my ($self) = @_;
274         my $ret = $self->mm && $self->search;
275         $self->{mm} = $self->{search} = undef;
276         $ret;
277 }
278
279 sub msg_by_path ($$;$) {
280         my ($self, $path, $ref) = @_;
281         # TODO: allow other refs:
282         my $str = git($self)->cat_file('HEAD:'.$path, $ref);
283         $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
284         $str;
285 }
286
287 sub msg_by_smsg ($$;$) {
288         my ($self, $smsg, $ref) = @_;
289
290         # ghosts may have undef smsg (from SearchThread.node) or
291         # no {blob} field
292         return unless defined $smsg;
293         defined(my $blob = $smsg->{blob}) or return;
294
295         my $str = git($self)->cat_file($blob, $ref);
296         $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
297         $str;
298 }
299
300 sub smsg_mime {
301         my ($self, $smsg, $ref) = @_;
302         if (my $s = msg_by_smsg($self, $smsg, $ref)) {
303                 $smsg->{mime} = PublicInbox::MIME->new($s);
304                 return $smsg;
305         }
306 }
307
308 sub mid2num($$) {
309         my ($self, $mid) = @_;
310         my $mm = mm($self) or return;
311         $mm->num_for($mid);
312 }
313
314 sub smsg_by_mid ($$) {
315         my ($self, $mid) = @_;
316         my $srch = search($self) or return;
317         # favor the Message-ID we used for the NNTP article number:
318         defined(my $num = mid2num($self, $mid)) or return;
319         my $smsg = $srch->lookup_article($num) or return;
320         PublicInbox::SearchMsg::psgi_cull($smsg);
321 }
322
323 sub msg_by_mid ($$;$) {
324         my ($self, $mid, $ref) = @_;
325         my $srch = search($self) or
326                 return msg_by_path($self, mid2path($mid), $ref);
327         my $smsg = smsg_by_mid($self, $mid);
328         $smsg ? msg_by_smsg($self, $smsg, $ref) : undef;
329 }
330
331 sub recent {
332         my ($self, $opts, $after, $before) = @_;
333         search($self)->{over_ro}->recent($opts, $after, $before);
334 }
335
336 1;