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 --skip-artnum=NUM NNTP article numbers to skip
26 --skip-epoch=NUM epochs to skip (-V2 only)
27 -J JOBS number of indexing jobs (-V2 only), (default: 4)
29 See public-inbox-init(1) man page for full documentation.
32 require PublicInbox::Admin;
33 PublicInbox::Admin::require_or_die('-base');
35 my ($version, $indexlevel, $skip_epoch, $skip_artnum, $jobs, $show_help);
37 'V|version=i' => \$version,
38 'L|index-level|indexlevel=s' => \$indexlevel,
39 'S|skip|skip-epoch=i' => \$skip_epoch,
40 'N|skip-artnum=i' => \$skip_artnum,
42 'help|?' => \$show_help,
45 print STDERR "Usage: $usage\n";
48 GetOptions(%opts) or $usage_cb->();
49 if ($show_help) { print $help; exit 0 };
50 PublicInbox::Admin::indexlevel_ok_or_die($indexlevel) if defined $indexlevel;
51 my $name = shift @ARGV or $usage_cb->();
52 my $inboxdir = shift @ARGV or $usage_cb->();
53 my $http_url = shift @ARGV or $usage_cb->();
54 my (@address) = @ARGV;
55 @address or $usage_cb->();
58 require PublicInbox::Config;
59 my $pi_config = PublicInbox::Config->default_file;
60 require File::Basename;
61 my $dir = File::Basename::dirname($pi_config);
63 File::Path::mkpath($dir); # will croak on fatal errors
65 # first, we grab a flock to prevent simultaneous public-inbox-init
66 # processes from trampling over each other, or exiting with 255 on
67 # O_EXCL failure below. This gets unlocked automatically on exit:
68 require PublicInbox::Lock;
69 my $lock_obj = { lock_path => "$pi_config.flock" };
70 PublicInbox::Lock::lock_acquire($lock_obj);
72 # git-config will operate on this (and rename on success):
74 my $fh = File::Temp->new(TEMPLATE => 'pi-init-XXXXXXXX', DIR => $dir);
76 # Now, we grab another lock to use git-config(1) locking, so it won't
77 # wait on the lock, unlike some of our internal flock()-based locks.
78 # This is to prevent direct git-config(1) usage from clobbering our
80 my $lockfile = "$pi_config.lock";
82 sysopen($lockfh, $lockfile, O_RDWR|O_CREAT|O_EXCL) or do {
83 warn "could not open config file: $lockfile: $!\n";
86 my $auto_unlink = UnlinkMe->new($lockfile);
89 open(my $oh, '<', $pi_config) or die "unable to read $pi_config: $!\n";
92 defined $perm or die "(f)stat failed on $pi_config: $!\n";
93 chmod($perm & 07777, $fh) or
94 die "(f)chmod failed on future $pi_config: $!\n";
100 print $fh $old or die "failed to write: $!\n";
101 close $oh or die "failed to close $pi_config: $!\n";
103 # yes, this conflict checking is racy if multiple instances of this
104 # script are run by the same $PI_DIR
105 my $cfg = PublicInbox::Config->new;
107 foreach my $addr (@address) {
108 my $found = $cfg->lookup($addr);
110 if ($found->{name} ne $name) {
112 "`$addr' already defined for ",
113 "`$found->{name}',\n",
114 "does not match intend `$name'\n";
117 $seen{lc($addr)} = 1;
122 exit(1) if $conflict;
124 my $ibx = $cfg->lookup_name($name);
125 $indexlevel //= $ibx->{indexlevel} if $ibx;
127 my $pi_config_tmp = $fh->filename;
128 close($fh) or die "failed to close $pi_config_tmp: $!\n";
130 my $pfx = "publicinbox.$name";
131 my @x = (qw/git config/, "--file=$pi_config_tmp");
134 $inboxdir = Cwd::abs_path($inboxdir);
135 die "`\\n' not allowed in `$inboxdir'\n" if $inboxdir =~ /\n/s;
136 if (-f "$inboxdir/inbox.lock") {
137 if (!defined $version) {
139 } elsif ($version != 2) {
140 die "$inboxdir is a -V2 inbox, -V$version specified\n"
142 } elsif (-d "$inboxdir/objects") {
143 if (!defined $version) {
145 } elsif ($version != 1) {
146 die "$inboxdir is a -V1 inbox, -V$version specified\n"
150 $version = 1 unless defined $version;
152 if ($version == 1 && defined $skip_epoch) {
153 die "--skip-epoch is only supported for -V2 inboxes\n";
156 my $ibx = PublicInbox::Inbox->new({
157 inboxdir => $inboxdir,
160 -primary_address => $address[0],
161 indexlevel => $indexlevel,
166 die "--jobs is only supported for -V2 inboxes\n" if $version == 1;
167 die "--jobs=$jobs must be >= 1\n" if $jobs <= 0;
168 $creat_opt->{nproc} = $jobs;
171 require PublicInbox::InboxWritable;
172 $ibx = PublicInbox::InboxWritable->new($ibx, $creat_opt);
173 $ibx->init_inbox(0, $skip_epoch, $skip_artnum);
175 # needed for git prior to v2.1.0
176 umask(0077) if defined $perm;
178 foreach my $addr (@address) {
179 next if $seen{lc($addr)};
180 PublicInbox::Import::run_die([@x, "--add", "$pfx.address", $addr]);
182 PublicInbox::Import::run_die([@x, "$pfx.url", $http_url]);
183 PublicInbox::Import::run_die([@x, "$pfx.inboxdir", $inboxdir]);
185 if (defined($indexlevel)) {
186 PublicInbox::Import::run_die([@x, "$pfx.indexlevel", $indexlevel]);
189 # needed for git prior to v2.1.0
191 chmod($perm & 07777, $pi_config_tmp) or
192 die "(f)chmod failed on future $pi_config: $!\n";
195 rename $pi_config_tmp, $pi_config or
196 die "failed to rename `$pi_config_tmp' to `$pi_config': $!\n";
197 $auto_unlink->DESTROY;
203 my ($klass, $file) = @_;
204 bless { file => $file }, $klass;
208 my $f = delete($_[0]->{file});
209 unlink($f) if defined($f);