]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwAltId.pm
34641a926c0c4393ac08562f437c07b89d04cff6
[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 # returns prefix => pathname mapping
14 # (pathname is NOT public, but prefix is used for Xapian queries)
15 sub altid_map ($) {
16         my ($ibx) = @_;
17         my $altid = $ibx->{altid} or return {};
18         my %h = map {;
19                 my $x = PublicInbox::AltId->new($ibx, $_);
20                 "$x->{prefix}" => $x->{filename}
21         } @$altid;
22         \%h;
23 }
24
25 sub sqlite3_missing ($) {
26         PublicInbox::WwwResponse::oneshot($_[0], 501, \<<EOF);
27 <pre>sqlite3 not available
28
29 The administrator needs to install the sqlite3(1) binary
30 to support gzipped sqlite3 dumps.</pre>
31 </pre>
32 EOF
33 }
34
35 sub check_output {
36         my ($r, $bref, $ctx) = @_;
37         return PublicInbox::WwwResponse::oneshot($ctx, 500) if !defined($r);
38         if ($r == 0) {
39                 my $err = eval { $ctx->{env}->{'psgi.errors'} } // \*STDERR;
40                 $err->print("unexpected EOF from sqlite3\n");
41                 return PublicInbox::WwwResponse::oneshot($ctx, 501);
42         }
43         [200, [ qw(Content-Type application/gzip), 'Content-Disposition',
44                 "inline; filename=$ctx->{altid_pfx}.sql.gz" ] ]
45 }
46
47 # POST $INBOX/$prefix.sql.gz
48 # we use the sqlite3(1) binary here since that's where the ".dump"
49 # command is implemented, not (AFAIK) in the libsqlite3 library
50 # and thus not usable from DBD::SQLite.
51 sub sqldump ($$) {
52         my ($ctx, $altid_pfx) = @_;
53         my $ibx = $ctx->{-inbox};
54         my $altid_map = $ibx->{-altid_map} //= altid_map($ibx);
55         my $fn = $altid_map->{$altid_pfx};
56         unless (defined $fn) {
57                 return PublicInbox::WwwStream::oneshot($ctx, 404, \<<EOF);
58 <pre>`$altid_pfx' is not a valid altid for this inbox</pre>
59 EOF
60         }
61
62         eval { require PublicInbox::GzipFilter } or
63                 return PublicInbox::WwwStream::oneshot($ctx, 501, \<<EOF);
64 <pre>gzip output not available
65
66 The administrator needs to install the Compress::Raw::Zlib Perl module
67 to support gzipped sqlite3 dumps.</pre>
68 EOF
69         $sqlite3 //= which('sqlite3');
70         if (!defined($sqlite3)) {
71                 return PublicInbox::WwwStream::oneshot($ctx, 501, \<<EOF);
72 <pre>sqlite3 not available
73
74 The administrator needs to install the sqlite3(1) binary
75 to support gzipped sqlite3 dumps.</pre>
76 </pre>
77 EOF
78         }
79
80         # setup stdin, POSIX requires writes <= 512 bytes to succeed so
81         # we can close the pipe right away.
82         pipe(my ($r, $w)) or die "pipe: $!";
83         syswrite($w, ".dump\n") == 6 or die "write: $!";
84         close($w) or die "close: $!";
85
86         # TODO: use -readonly if available with newer sqlite3(1)
87         my $qsp = PublicInbox::Qspawn->new([$sqlite3, $fn], undef, { 0 => $r });
88         my $env = $ctx->{env};
89         $ctx->{altid_pfx} = $altid_pfx;
90         $env->{'qspawn.filter'} = PublicInbox::GzipFilter->new;
91         $qsp->psgi_return($env, undef, \&check_output, $ctx);
92 }
93
94 1;