]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-init
init+index: support --skip-docdata for Xapian
[public-inbox.git] / script / public-inbox-init
1 #!perl -w
2 # Copyright (C) 2014-2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use v5.10.1;
6 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
7 use Fcntl qw(:DEFAULT);
8 my $usage = 'public-inbox-init NAME INBOX_DIR HTTP_URL ADDRESS [ADDRESS..]';
9 my $help = <<EOF; # the following should fit w/o scrolling in 80x24 term:
10 usage: $usage
11
12   Initialize a public-inbox
13
14 required arguments:
15
16   NAME                the name of the inbox
17   INBOX_DIR           pathname the inbox
18   HTTP_URL            HTTP (or HTTPS) URL
19   ADDRESS             email address(es), may be specified multiple times
20
21 options:
22
23   -V2                 use scalable public-inbox-v2-format(5)
24   -L LEVEL            index level `basic', `medium', or `full' (default: full)
25   --ng NEWSGROUP      set NNTP newsgroup name
26   --skip-artnum=NUM   NNTP article numbers to skip
27   --skip-epoch=NUM    epochs to skip (-V2 only)
28   -J JOBS             number of indexing jobs (-V2 only), (default: 4)
29
30 See public-inbox-init(1) man page for full documentation.
31 EOF
32
33 require PublicInbox::Admin;
34 PublicInbox::Admin::require_or_die('-base');
35
36 my ($version, $indexlevel, $skip_epoch, $skip_artnum, $jobs, $show_help);
37 my $skip_docdata;
38 my $ng = '';
39 my %opts = (
40         'V|version=i' => \$version,
41         'L|index-level|indexlevel=s' => \$indexlevel,
42         'S|skip|skip-epoch=i' => \$skip_epoch,
43         'skip-artnum=i' => \$skip_artnum,
44         'j|jobs=i' => \$jobs,
45         'ng|newsgroup=s' => \$ng,
46         'skip-docdata' => \$skip_docdata,
47         'help|?' => \$show_help,
48 );
49 my $usage_cb = sub {
50         print STDERR "Usage: $usage\n";
51         exit 1;
52 };
53 GetOptions(%opts) or $usage_cb->();
54 if ($show_help) { print $help; exit 0 };
55 PublicInbox::Admin::indexlevel_ok_or_die($indexlevel) if defined $indexlevel;
56 my $name = shift @ARGV or $usage_cb->();
57 my $inboxdir = shift @ARGV or $usage_cb->();
58 my $http_url = shift @ARGV or $usage_cb->();
59 my (@address) = @ARGV;
60 @address or $usage_cb->();
61
62 $ng =~ m![^A-Za-z0-9/_\.\-\~\@\+\=:]! and
63         die "--newsgroup `$ng' is not valid\n";
64 ($ng =~ m!\A\.! || $ng =~ m!\.\z!) and
65         die "--newsgroup `$ng' must not start or end with `.'\n";
66
67 require PublicInbox::Config;
68 my $pi_config = PublicInbox::Config->default_file;
69 require File::Basename;
70 my $dir = File::Basename::dirname($pi_config);
71 require File::Path;
72 File::Path::mkpath($dir); # will croak on fatal errors
73
74 # first, we grab a flock to prevent simultaneous public-inbox-init
75 # processes from trampling over each other, or exiting with 255 on
76 # O_EXCL failure below.  This gets unlocked automatically on exit:
77 require PublicInbox::Lock;
78 my $lock_obj = { lock_path => "$pi_config.flock" };
79 PublicInbox::Lock::lock_acquire($lock_obj);
80
81 # git-config will operate on this (and rename on success):
82 require File::Temp;
83 my $fh = File::Temp->new(TEMPLATE => 'pi-init-XXXXXXXX', DIR => $dir);
84
85 # Now, we grab another lock to use git-config(1) locking, so it won't
86 # wait on the lock, unlike some of our internal flock()-based locks.
87 # This is to prevent direct git-config(1) usage from clobbering our
88 # changes.
89 my $lockfile = "$pi_config.lock";
90 my $lockfh;
91 sysopen($lockfh, $lockfile, O_RDWR|O_CREAT|O_EXCL) or do {
92         warn "could not open config file: $lockfile: $!\n";
93         exit(255);
94 };
95 my $auto_unlink = UnlinkMe->new($lockfile);
96 my ($perm, %seen);
97 if (-e $pi_config) {
98         open(my $oh, '<', $pi_config) or die "unable to read $pi_config: $!\n";
99         my @st = stat($oh);
100         $perm = $st[2];
101         defined $perm or die "(f)stat failed on $pi_config: $!\n";
102         chmod($perm & 07777, $fh) or
103                 die "(f)chmod failed on future $pi_config: $!\n";
104         my $old;
105         {
106                 local $/;
107                 $old = <$oh>;
108         }
109         print $fh $old or die "failed to write: $!\n";
110         close $oh or die "failed to close $pi_config: $!\n";
111
112         # yes, this conflict checking is racy if multiple instances of this
113         # script are run by the same $PI_DIR
114         my $cfg = PublicInbox::Config->new;
115         my $conflict;
116         foreach my $addr (@address) {
117                 my $found = $cfg->lookup($addr);
118                 if ($found) {
119                         if ($found->{name} ne $name) {
120                                 print STDERR
121                                         "`$addr' already defined for ",
122                                         "`$found->{name}',\n",
123                                         "does not match intend `$name'\n";
124                                 $conflict = 1;
125                         } else {
126                                 $seen{lc($addr)} = 1;
127                         }
128                 }
129         }
130
131         exit(1) if $conflict;
132
133         my $ibx = $cfg->lookup_name($name);
134         $indexlevel //= $ibx->{indexlevel} if $ibx;
135 }
136 my $pi_config_tmp = $fh->filename;
137 close($fh) or die "failed to close $pi_config_tmp: $!\n";
138
139 my $pfx = "publicinbox.$name";
140 my @x = (qw/git config/, "--file=$pi_config_tmp");
141
142 require Cwd;
143 $inboxdir = Cwd::abs_path($inboxdir);
144 die "`\\n' not allowed in `$inboxdir'\n" if $inboxdir =~ /\n/s;
145 if (-f "$inboxdir/inbox.lock") {
146         if (!defined $version) {
147                 $version = 2;
148         } elsif ($version != 2) {
149                 die "$inboxdir is a -V2 inbox, -V$version specified\n"
150         }
151 } elsif (-d "$inboxdir/objects") {
152         if (!defined $version) {
153                 $version = 1;
154         } elsif ($version != 1) {
155                 die "$inboxdir is a -V1 inbox, -V$version specified\n"
156         }
157 }
158
159 $version = 1 unless defined $version;
160
161 if ($version == 1 && defined $skip_epoch) {
162         die "--skip-epoch is only supported for -V2 inboxes\n";
163 }
164
165 my $ibx = PublicInbox::Inbox->new({
166         inboxdir => $inboxdir,
167         name => $name,
168         version => $version,
169         -primary_address => $address[0],
170         indexlevel => $indexlevel,
171 });
172
173 my $creat_opt = {};
174 if (defined $jobs) {
175         die "--jobs is only supported for -V2 inboxes\n" if $version == 1;
176         die "--jobs=$jobs must be >= 1\n" if $jobs <= 0;
177         $creat_opt->{nproc} = $jobs;
178 }
179
180 require PublicInbox::InboxWritable;
181 $ibx = PublicInbox::InboxWritable->new($ibx, $creat_opt);
182 if ($skip_docdata) {
183         $ibx->{indexlevel} //= 'full'; # ensure init_inbox writes xdb
184         $ibx->{indexlevel} eq 'basic' and
185                 die "--skip-docdata ignored with --indexlevel=basic\n";
186         $ibx->{-skip_docdata} = $skip_docdata;
187 }
188 $ibx->init_inbox(0, $skip_epoch, $skip_artnum);
189
190 # needed for git prior to v2.1.0
191 umask(0077) if defined $perm;
192
193 foreach my $addr (@address) {
194         next if $seen{lc($addr)};
195         PublicInbox::Import::run_die([@x, "--add", "$pfx.address", $addr]);
196 }
197 PublicInbox::Import::run_die([@x, "$pfx.url", $http_url]);
198 PublicInbox::Import::run_die([@x, "$pfx.inboxdir", $inboxdir]);
199
200 if (defined($indexlevel)) {
201         PublicInbox::Import::run_die([@x, "$pfx.indexlevel", $indexlevel]);
202 }
203 PublicInbox::Import::run_die([@x, "$pfx.newsgroup", $ng]) if $ng ne '';
204
205 # needed for git prior to v2.1.0
206 if (defined $perm) {
207         chmod($perm & 07777, $pi_config_tmp) or
208                         die "(f)chmod failed on future $pi_config: $!\n";
209 }
210
211 rename $pi_config_tmp, $pi_config or
212         die "failed to rename `$pi_config_tmp' to `$pi_config': $!\n";
213 $auto_unlink->DESTROY;
214
215 package UnlinkMe;
216 use strict;
217
218 sub new {
219         my ($klass, $file) = @_;
220         bless { file => $file }, $klass;
221 }
222
223 sub DESTROY {
224         my $f = delete($_[0]->{file});
225         unlink($f) if defined($f);
226 }
227 1;