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);
28 my $indexlevel = undef;
31 'V|version=i' => \$version,
32 'L|indexlevel=s' => \$indexlevel,
33 'S|skip|skip-epoch=i' => \$skip_epoch,
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);
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);
107 if (!defined($indexlevel) && $ibx->{indexlevel}) {
108 $indexlevel = $ibx->{indexlevel};
112 close $fh or die "failed to close $pi_config_tmp: $!\n";
114 my $pfx = "publicinbox.$name";
115 my @x = (qw/git config/, "--file=$pi_config_tmp");
117 $inboxdir = abs_path($inboxdir);
118 if (-f "$inboxdir/inbox.lock") {
119 if (!defined $version) {
121 } elsif ($version != 2) {
122 die "$inboxdir is a -V2 repo, -V$version specified\n"
124 } elsif (-d "$inboxdir/objects") {
125 if (!defined $version) {
127 } elsif ($version != 1) {
128 die "$inboxdir is a -V1 repo, -V$version specified\n"
132 $version = 1 unless defined $version;
134 if ($version == 1 && defined $skip_epoch) {
135 die "--skip-epoch is only supported for -V2 repos\n";
138 my $ibx = PublicInbox::Inbox->new({
139 inboxdir => $inboxdir,
142 -primary_address => $address[0],
143 indexlevel => $indexlevel,
147 PublicInbox::InboxWritable->new($ibx, $creat_opt)->init_inbox(0, $skip_epoch);
149 # needed for git prior to v2.1.0
150 umask(0077) if defined $perm;
152 foreach my $addr (@address) {
153 next if $seen{lc($addr)};
154 PublicInbox::Import::run_die([@x, "--add", "$pfx.address", $addr]);
156 PublicInbox::Import::run_die([@x, "$pfx.url", $http_url]);
157 PublicInbox::Import::run_die([@x, "$pfx.inboxdir", $inboxdir]);
159 if (defined($indexlevel)) {
160 PublicInbox::Import::run_die([@x, "$pfx.indexlevel", $indexlevel]);
163 # needed for git prior to v2.1.0
165 chmod($perm & 07777, $pi_config_tmp) or
166 die "(f)chmod failed on future $pi_config: $!\n";
169 rename $pi_config_tmp, $pi_config or
170 die "failed to rename `$pi_config_tmp' to `$pi_config': $!\n";
171 $auto_unlink->DESTROY;
177 my ($klass, $file) = @_;
178 bless { file => $file }, $klass;
182 my $f = delete($_[0]->{file});
183 unlink($f) if defined($f);