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;
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|index-level|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 = File::Temp->new(TEMPLATE => 'pi-init-XXXXXXXX', DIR => $dir);
57 # Now, we grab another lock to use git-config(1) locking, so it won't
58 # wait on the lock, unlike some of our internal flock()-based locks.
59 # This is to prevent direct git-config(1) usage from clobbering our
61 my $lockfile = "$pi_config.lock";
63 sysopen($lockfh, $lockfile, O_RDWR|O_CREAT|O_EXCL) or do {
64 warn "could not open config file: $lockfile: $!\n";
67 my $auto_unlink = UnlinkMe->new($lockfile);
70 open(my $oh, '<', $pi_config) or die "unable to read $pi_config: $!\n";
73 defined $perm or die "(f)stat failed on $pi_config: $!\n";
74 chmod($perm & 07777, $fh) or
75 die "(f)chmod failed on future $pi_config: $!\n";
81 print $fh $old or die "failed to write: $!\n";
82 close $oh or die "failed to close $pi_config: $!\n";
84 # yes, this conflict checking is racy if multiple instances of this
85 # script are run by the same $PI_DIR
86 my $cfg = PublicInbox::Config->new;
88 foreach my $addr (@address) {
89 my $found = $cfg->lookup($addr);
91 if ($found->{name} ne $name) {
93 "`$addr' already defined for ",
94 "`$found->{name}',\n",
95 "does not match intend `$name'\n";
103 exit(1) if $conflict;
105 my $ibx = $cfg->lookup_name($name);
106 $indexlevel //= $ibx->{indexlevel} if $ibx;
108 my $pi_config_tmp = $fh->filename;
109 close($fh) or die "failed to close $pi_config_tmp: $!\n";
111 my $pfx = "publicinbox.$name";
112 my @x = (qw/git config/, "--file=$pi_config_tmp");
114 $inboxdir = abs_path($inboxdir);
115 die "`\\n' not allowed in `$inboxdir'\n" if $inboxdir =~ /\n/s;
116 if (-f "$inboxdir/inbox.lock") {
117 if (!defined $version) {
119 } elsif ($version != 2) {
120 die "$inboxdir is a -V2 inbox, -V$version specified\n"
122 } elsif (-d "$inboxdir/objects") {
123 if (!defined $version) {
125 } elsif ($version != 1) {
126 die "$inboxdir is a -V1 inbox, -V$version specified\n"
130 $version = 1 unless defined $version;
132 if ($version == 1 && defined $skip_epoch) {
133 die "--skip-epoch is only supported for -V2 inboxes\n";
136 my $ibx = PublicInbox::Inbox->new({
137 inboxdir => $inboxdir,
140 -primary_address => $address[0],
141 indexlevel => $indexlevel,
146 die "--jobs is only supported for -V2 inboxes\n" if $version == 1;
147 die "--jobs=$jobs must be >= 1\n" if $jobs <= 0;
148 $creat_opt->{nproc} = $jobs;
151 $ibx = PublicInbox::InboxWritable->new($ibx, $creat_opt);
152 $ibx->init_inbox(0, $skip_epoch, $skip_artnum);
154 # needed for git prior to v2.1.0
155 umask(0077) if defined $perm;
157 foreach my $addr (@address) {
158 next if $seen{lc($addr)};
159 PublicInbox::Import::run_die([@x, "--add", "$pfx.address", $addr]);
161 PublicInbox::Import::run_die([@x, "$pfx.url", $http_url]);
162 PublicInbox::Import::run_die([@x, "$pfx.inboxdir", $inboxdir]);
164 if (defined($indexlevel)) {
165 PublicInbox::Import::run_die([@x, "$pfx.indexlevel", $indexlevel]);
168 # needed for git prior to v2.1.0
170 chmod($perm & 07777, $pi_config_tmp) or
171 die "(f)chmod failed on future $pi_config: $!\n";
174 rename $pi_config_tmp, $pi_config or
175 die "failed to rename `$pi_config_tmp' to `$pi_config': $!\n";
176 $auto_unlink->DESTROY;
182 my ($klass, $file) = @_;
183 bless { file => $file }, $klass;
187 my $f = delete($_[0]->{file});
188 unlink($f) if defined($f);