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>
5 # Initializes a public-inbox, basically a wrapper for git-init(1)
10 Usage: public-inbox-init NAME INBOX_DIR HTTP_URL ADDRESS [ADDRESS..]
14 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
15 use PublicInbox::Admin;
16 PublicInbox::Admin::require_or_die('-base');
17 use PublicInbox::Config;
18 use PublicInbox::InboxWritable;
19 use PublicInbox::Import;
20 use File::Temp qw/tempfile/;
21 use PublicInbox::Lock;
22 use File::Basename qw/dirname/;
23 use File::Path qw/mkpath/;
24 use Fcntl qw(:DEFAULT);
27 my ($version, $indexlevel, $skip_epoch, $skip_artnum, $jobs);
29 'V|version=i' => \$version,
30 'L|indexlevel=s' => \$indexlevel,
31 'S|skip|skip-epoch=i' => \$skip_epoch,
32 'N|skip-artnum=i' => \$skip_artnum,
35 GetOptions(%opts) or usage();
36 PublicInbox::Admin::indexlevel_ok_or_die($indexlevel) if defined $indexlevel;
37 my $name = shift @ARGV or usage();
38 my $inboxdir = shift @ARGV or usage();
39 my $http_url = shift @ARGV or usage();
40 my (@address) = @ARGV;
44 my $pi_config = PublicInbox::Config->default_file;
45 my $dir = dirname($pi_config);
46 mkpath($dir); # will croak on fatal errors
48 # first, we grab a flock to prevent simultaneous public-inbox-init
49 # processes from trampling over each other, or exiting with 255 on
50 # O_EXCL failure below. This gets unlocked automatically on exit:
51 my $lock_obj = { lock_path => "$pi_config.flock" };
52 PublicInbox::Lock::lock_acquire($lock_obj);
54 # git-config will operate on this (and rename on success):
55 my ($fh, $pi_config_tmp) = tempfile('pi-init-XXXXXXXX', DIR => $dir);
56 my $cfg_tmp = UnlinkMe->new($pi_config_tmp);
58 # Now, we grab another lock to use git-config(1) locking, so it won't
59 # wait on the lock, unlike some of our internal flock()-based locks.
60 # This is to prevent direct git-config(1) usage from clobbering our
62 my $lockfile = "$pi_config.lock";
64 sysopen($lockfh, $lockfile, O_RDWR|O_CREAT|O_EXCL) or do {
65 warn "could not open config file: $lockfile: $!\n";
68 my $auto_unlink = UnlinkMe->new($lockfile);
71 open(my $oh, '<', $pi_config) or die "unable to read $pi_config: $!\n";
74 defined $perm or die "(f)stat failed on $pi_config: $!\n";
75 chmod($perm & 07777, $fh) or
76 die "(f)chmod failed on future $pi_config: $!\n";
82 print $fh $old or die "failed to write: $!\n";
83 close $oh or die "failed to close $pi_config: $!\n";
85 # yes, this conflict checking is racy if multiple instances of this
86 # script are run by the same $PI_DIR
87 my $cfg = PublicInbox::Config->new;
89 foreach my $addr (@address) {
90 my $found = $cfg->lookup($addr);
92 if ($found->{name} ne $name) {
94 "`$addr' already defined for ",
95 "`$found->{name}',\n",
96 "does not match intend `$name'\n";
104 exit(1) if $conflict;
106 my $ibx = $cfg->lookup_name($name);
108 if (!defined($indexlevel) && $ibx->{indexlevel}) {
109 $indexlevel = $ibx->{indexlevel};
113 close $fh or die "failed to close $pi_config_tmp: $!\n";
115 my $pfx = "publicinbox.$name";
116 my @x = (qw/git config/, "--file=$pi_config_tmp");
118 $inboxdir = abs_path($inboxdir);
119 die "`\\n' not allowed in `$inboxdir'\n" if $inboxdir =~ /\n/s;
120 if (-f "$inboxdir/inbox.lock") {
121 if (!defined $version) {
123 } elsif ($version != 2) {
124 die "$inboxdir is a -V2 inbox, -V$version specified\n"
126 } elsif (-d "$inboxdir/objects") {
127 if (!defined $version) {
129 } elsif ($version != 1) {
130 die "$inboxdir is a -V1 inbox, -V$version specified\n"
134 $version = 1 unless defined $version;
136 if ($version == 1 && defined $skip_epoch) {
137 die "--skip-epoch is only supported for -V2 inboxes\n";
140 my $ibx = PublicInbox::Inbox->new({
141 inboxdir => $inboxdir,
144 -primary_address => $address[0],
145 indexlevel => $indexlevel,
150 die "--jobs is only supported for -V2 inboxes\n" if $version == 1;
151 die "--jobs=$jobs must be >= 1\n" if $jobs <= 0;
152 $creat_opt->{nproc} = $jobs;
155 $ibx = PublicInbox::InboxWritable->new($ibx, $creat_opt);
156 $ibx->init_inbox(0, $skip_epoch, $skip_artnum);
158 # needed for git prior to v2.1.0
159 umask(0077) if defined $perm;
161 foreach my $addr (@address) {
162 next if $seen{lc($addr)};
163 PublicInbox::Import::run_die([@x, "--add", "$pfx.address", $addr]);
165 PublicInbox::Import::run_die([@x, "$pfx.url", $http_url]);
166 PublicInbox::Import::run_die([@x, "$pfx.inboxdir", $inboxdir]);
168 if (defined($indexlevel)) {
169 PublicInbox::Import::run_die([@x, "$pfx.indexlevel", $indexlevel]);
172 # needed for git prior to v2.1.0
174 chmod($perm & 07777, $pi_config_tmp) or
175 die "(f)chmod failed on future $pi_config: $!\n";
178 rename $pi_config_tmp, $pi_config or
179 die "failed to rename `$pi_config_tmp' to `$pi_config': $!\n";
180 delete $cfg_tmp->{file};
181 $auto_unlink->DESTROY;
187 my ($klass, $file) = @_;
188 bless { file => $file }, $klass;
192 my $f = delete($_[0]->{file});
193 unlink($f) if defined($f);