]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwAltId.pm
inbox: altid_map becomes a method
[public-inbox.git] / lib / PublicInbox / WwwAltId.pm
1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # dumps using the ".dump" command of sqlite3(1)
5 package PublicInbox::WwwAltId;
6 use strict;
7 use PublicInbox::Qspawn;
8 use PublicInbox::WwwStream;
9 use PublicInbox::AltId;
10 use PublicInbox::Spawn qw(which);
11 our $sqlite3 = $ENV{SQLITE3};
12
13 sub sqlite3_missing ($) {
14         PublicInbox::WwwResponse::oneshot($_[0], 501, \<<EOF);
15 <pre>sqlite3 not available
16
17 The administrator needs to install the sqlite3(1) binary
18 to support gzipped sqlite3 dumps.</pre>
19 </pre>
20 EOF
21 }
22
23 sub check_output {
24         my ($r, $bref, $ctx) = @_;
25         return PublicInbox::WwwResponse::oneshot($ctx, 500) if !defined($r);
26         if ($r == 0) {
27                 my $err = eval { $ctx->{env}->{'psgi.errors'} } // \*STDERR;
28                 $err->print("unexpected EOF from sqlite3\n");
29                 return PublicInbox::WwwResponse::oneshot($ctx, 501);
30         }
31         [200, [ qw(Content-Type application/gzip), 'Content-Disposition',
32                 "inline; filename=$ctx->{altid_pfx}.sql.gz" ] ]
33 }
34
35 # POST $INBOX/$prefix.sql.gz
36 # we use the sqlite3(1) binary here since that's where the ".dump"
37 # command is implemented, not (AFAIK) in the libsqlite3 library
38 # and thus not usable from DBD::SQLite.
39 sub sqldump ($$) {
40         my ($ctx, $altid_pfx) = @_;
41         my $ibx = $ctx->{-inbox};
42         my $altid_map = $ibx->altid_map;
43         my $fn = $altid_map->{$altid_pfx};
44         unless (defined $fn) {
45                 return PublicInbox::WwwStream::oneshot($ctx, 404, \<<EOF);
46 <pre>`$altid_pfx' is not a valid altid for this inbox</pre>
47 EOF
48         }
49
50         eval { require PublicInbox::GzipFilter } or
51                 return PublicInbox::WwwStream::oneshot($ctx, 501, \<<EOF);
52 <pre>gzip output not available
53
54 The administrator needs to install the Compress::Raw::Zlib Perl module
55 to support gzipped sqlite3 dumps.</pre>
56 EOF
57         $sqlite3 //= which('sqlite3');
58         if (!defined($sqlite3)) {
59                 return PublicInbox::WwwStream::oneshot($ctx, 501, \<<EOF);
60 <pre>sqlite3 not available
61
62 The administrator needs to install the sqlite3(1) binary
63 to support gzipped sqlite3 dumps.</pre>
64 </pre>
65 EOF
66         }
67
68         # setup stdin, POSIX requires writes <= 512 bytes to succeed so
69         # we can close the pipe right away.
70         pipe(my ($r, $w)) or die "pipe: $!";
71         syswrite($w, ".dump\n") == 6 or die "write: $!";
72         close($w) or die "close: $!";
73
74         # TODO: use -readonly if available with newer sqlite3(1)
75         my $qsp = PublicInbox::Qspawn->new([$sqlite3, $fn], undef, { 0 => $r });
76         my $env = $ctx->{env};
77         $ctx->{altid_pfx} = $altid_pfx;
78         $env->{'qspawn.filter'} = PublicInbox::GzipFilter->new;
79         $qsp->psgi_return($env, undef, \&check_output, $ctx);
80 }
81
82 1;