1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # dumps using the ".dump" command of sqlite3(1)
5 package PublicInbox::WwwAltId;
7 use PublicInbox::Qspawn;
8 use PublicInbox::WwwStream;
9 use PublicInbox::AltId;
10 use PublicInbox::Spawn qw(which);
11 our $sqlite3 = $ENV{SQLITE3};
13 # returns prefix => pathname mapping
14 # (pathname is NOT public, but prefix is used for Xapian queries)
17 my $altid = $ibx->{altid} or return {};
19 my $x = PublicInbox::AltId->new($ibx, $_);
20 "$x->{prefix}" => $x->{filename}
25 sub sqlite3_missing ($) {
26 PublicInbox::WwwResponse::oneshot($_[0], 501, \<<EOF);
27 <pre>sqlite3 not available
29 The administrator needs to install the sqlite3(1) binary
30 to support gzipped sqlite3 dumps.</pre>
36 my ($r, $bref, $ctx) = @_;
37 return PublicInbox::WwwResponse::oneshot($ctx, 500) if !defined($r);
39 my $err = eval { $ctx->{env}->{'psgi.errors'} } // \*STDERR;
40 $err->print("unexpected EOF from sqlite3\n");
41 return PublicInbox::WwwResponse::oneshot($ctx, 501);
43 [200, [ qw(Content-Type application/gzip), 'Content-Disposition',
44 "inline; filename=$ctx->{altid_pfx}.sql.gz" ] ]
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.
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>
62 eval { require PublicInbox::GzipFilter } or
63 return PublicInbox::WwwStream::oneshot($ctx, 501, \<<EOF);
64 <pre>gzip output not available
66 The administrator needs to install the Compress::Raw::Zlib Perl module
67 to support gzipped sqlite3 dumps.</pre>
69 $sqlite3 //= which('sqlite3');
70 if (!defined($sqlite3)) {
71 return PublicInbox::WwwStream::oneshot($ctx, 501, \<<EOF);
72 <pre>sqlite3 not available
74 The administrator needs to install the sqlite3(1) binary
75 to support gzipped sqlite3 dumps.</pre>
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: $!";
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);