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>
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:
12 Initialize a public-inbox
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
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)
30 See public-inbox-init(1) man page for full documentation.
33 require PublicInbox::Admin;
34 PublicInbox::Admin::require_or_die('-base');
36 my ($version, $indexlevel, $skip_epoch, $skip_artnum, $jobs, $show_help);
39 'V|version=i' => \$version,
40 'L|index-level|indexlevel=s' => \$indexlevel,
41 'S|skip|skip-epoch=i' => \$skip_epoch,
42 'skip-artnum=i' => \$skip_artnum,
44 'ng|newsgroup=s' => \$ng,
45 'help|?' => \$show_help,
48 print STDERR "Usage: $usage\n";
51 GetOptions(%opts) or $usage_cb->();
52 if ($show_help) { print $help; exit 0 };
53 PublicInbox::Admin::indexlevel_ok_or_die($indexlevel) if defined $indexlevel;
54 my $name = shift @ARGV or $usage_cb->();
55 my $inboxdir = shift @ARGV or $usage_cb->();
56 my $http_url = shift @ARGV or $usage_cb->();
57 my (@address) = @ARGV;
58 @address or $usage_cb->();
60 $ng =~ m![^A-Za-z0-9/_\.\-\~\@\+\=:]! and
61 die "--newsgroup `$ng' is not valid\n";
62 ($ng =~ m!\A\.! || $ng =~ m!\.\z!) and
63 die "--newsgroup `$ng' must not start or end with `.'\n";
65 require PublicInbox::Config;
66 my $pi_config = PublicInbox::Config->default_file;
67 require File::Basename;
68 my $dir = File::Basename::dirname($pi_config);
70 File::Path::mkpath($dir); # will croak on fatal errors
72 # first, we grab a flock to prevent simultaneous public-inbox-init
73 # processes from trampling over each other, or exiting with 255 on
74 # O_EXCL failure below. This gets unlocked automatically on exit:
75 require PublicInbox::Lock;
76 my $lock_obj = { lock_path => "$pi_config.flock" };
77 PublicInbox::Lock::lock_acquire($lock_obj);
79 # git-config will operate on this (and rename on success):
81 my $fh = File::Temp->new(TEMPLATE => 'pi-init-XXXXXXXX', DIR => $dir);
83 # Now, we grab another lock to use git-config(1) locking, so it won't
84 # wait on the lock, unlike some of our internal flock()-based locks.
85 # This is to prevent direct git-config(1) usage from clobbering our
87 my $lockfile = "$pi_config.lock";
89 sysopen($lockfh, $lockfile, O_RDWR|O_CREAT|O_EXCL) or do {
90 warn "could not open config file: $lockfile: $!\n";
93 my $auto_unlink = UnlinkMe->new($lockfile);
96 open(my $oh, '<', $pi_config) or die "unable to read $pi_config: $!\n";
99 defined $perm or die "(f)stat failed on $pi_config: $!\n";
100 chmod($perm & 07777, $fh) or
101 die "(f)chmod failed on future $pi_config: $!\n";
107 print $fh $old or die "failed to write: $!\n";
108 close $oh or die "failed to close $pi_config: $!\n";
110 # yes, this conflict checking is racy if multiple instances of this
111 # script are run by the same $PI_DIR
112 my $cfg = PublicInbox::Config->new;
114 foreach my $addr (@address) {
115 my $found = $cfg->lookup($addr);
117 if ($found->{name} ne $name) {
119 "`$addr' already defined for ",
120 "`$found->{name}',\n",
121 "does not match intend `$name'\n";
124 $seen{lc($addr)} = 1;
129 exit(1) if $conflict;
131 my $ibx = $cfg->lookup_name($name);
132 $indexlevel //= $ibx->{indexlevel} if $ibx;
134 my $pi_config_tmp = $fh->filename;
135 close($fh) or die "failed to close $pi_config_tmp: $!\n";
137 my $pfx = "publicinbox.$name";
138 my @x = (qw/git config/, "--file=$pi_config_tmp");
141 $inboxdir = Cwd::abs_path($inboxdir);
142 die "`\\n' not allowed in `$inboxdir'\n" if $inboxdir =~ /\n/s;
143 if (-f "$inboxdir/inbox.lock") {
144 if (!defined $version) {
146 } elsif ($version != 2) {
147 die "$inboxdir is a -V2 inbox, -V$version specified\n"
149 } elsif (-d "$inboxdir/objects") {
150 if (!defined $version) {
152 } elsif ($version != 1) {
153 die "$inboxdir is a -V1 inbox, -V$version specified\n"
157 $version = 1 unless defined $version;
159 if ($version == 1 && defined $skip_epoch) {
160 die "--skip-epoch is only supported for -V2 inboxes\n";
163 my $ibx = PublicInbox::Inbox->new({
164 inboxdir => $inboxdir,
167 -primary_address => $address[0],
168 indexlevel => $indexlevel,
173 die "--jobs is only supported for -V2 inboxes\n" if $version == 1;
174 die "--jobs=$jobs must be >= 1\n" if $jobs <= 0;
175 $creat_opt->{nproc} = $jobs;
178 require PublicInbox::InboxWritable;
179 $ibx = PublicInbox::InboxWritable->new($ibx, $creat_opt);
180 $ibx->init_inbox(0, $skip_epoch, $skip_artnum);
182 # needed for git prior to v2.1.0
183 umask(0077) if defined $perm;
185 foreach my $addr (@address) {
186 next if $seen{lc($addr)};
187 PublicInbox::Import::run_die([@x, "--add", "$pfx.address", $addr]);
189 PublicInbox::Import::run_die([@x, "$pfx.url", $http_url]);
190 PublicInbox::Import::run_die([@x, "$pfx.inboxdir", $inboxdir]);
192 if (defined($indexlevel)) {
193 PublicInbox::Import::run_die([@x, "$pfx.indexlevel", $indexlevel]);
195 PublicInbox::Import::run_die([@x, "$pfx.newsgroup", $ng]) if $ng ne '';
197 # needed for git prior to v2.1.0
199 chmod($perm & 07777, $pi_config_tmp) or
200 die "(f)chmod failed on future $pi_config: $!\n";
203 rename $pi_config_tmp, $pi_config or
204 die "failed to rename `$pi_config_tmp' to `$pi_config': $!\n";
205 $auto_unlink->DESTROY;
211 my ($klass, $file) = @_;
212 bless { file => $file }, $klass;
216 my $f = delete($_[0]->{file});
217 unlink($f) if defined($f);