diff options
Diffstat (limited to 'script/public-inbox-init')
-rwxr-xr-x | script/public-inbox-init | 49 |
1 files changed, 22 insertions, 27 deletions
diff --git a/script/public-inbox-init b/script/public-inbox-init index 1223d47e..cf6443f7 100755 --- a/script/public-inbox-init +++ b/script/public-inbox-init @@ -1,9 +1,10 @@ #!perl -w -# Copyright (C) 2014-2021 all contributors <meta@public-inbox.org> +# Copyright (C) all contributors <meta@public-inbox.org> # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt> use strict; use v5.10.1; use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/; +use autodie qw(open chmod close rename); use Fcntl qw(:DEFAULT); my $help = <<EOF; # the following should fit w/o scrolling in 80x24 term: usage: public-inbox-init NAME INBOX_DIR HTTP_URL ADDRESS [ADDRESS..] @@ -60,7 +61,7 @@ my $inboxdir = shift @ARGV or $usage_cb->(); my $http_url = shift @ARGV or $usage_cb->(); my (@address) = @ARGV; @address or $usage_cb->(); -+PublicInbox::Admin::do_chdir(\@chdir); +PublicInbox::Admin::do_chdir(\@chdir); @c_extra = map { my ($k, $v) = split(/=/, $_, 2); @@ -121,18 +122,17 @@ sysopen($lockfh, $lockfile, O_RDWR|O_CREAT|O_EXCL) or do { exit(255); }; require PublicInbox::OnDestroy; -my $auto_unlink = PublicInbox::OnDestroy->new($$, sub { unlink $lockfile }); -my ($perm, %seen); +my $auto_unlink = PublicInbox::OnDestroy::on_destroy(sub { unlink $lockfile }); +my $perm = 0644 & ~umask; +my %seen; if (-e $pi_config) { - open(my $oh, '<', $pi_config) or die "unable to read $pi_config: $!\n"; - my @st = stat($oh); + require PublicInbox::IO; + open(my $oh, '<', $pi_config); + my @st = stat($oh) or die "(f)stat failed on $pi_config: $!\n"; $perm = $st[2]; - defined $perm or die "(f)stat failed on $pi_config: $!\n"; - chmod($perm & 07777, $fh) or - die "(f)chmod failed on future $pi_config: $!\n"; - defined(my $old = do { local $/; <$oh> }) or die "read $pi_config: $!\n"; - print $fh $old or die "failed to write: $!\n"; - close $oh or die "failed to close $pi_config: $!\n"; + chmod($perm & 07777, $fh); + print $fh PublicInbox::IO::read_all($oh); + close $oh; # yes, this conflict checking is racy if multiple instances of this # script are run by the same $PI_DIR @@ -159,7 +159,7 @@ if (-e $pi_config) { $indexlevel //= $ibx->{indexlevel} if $ibx; } my $pi_config_tmp = $fh->filename; -close($fh) or die "failed to close $pi_config_tmp: $!\n"; +close($fh); my $pfx = "publicinbox.$name"; my @x = (qw/git config/, "--file=$pi_config_tmp"); @@ -212,8 +212,14 @@ if ($skip_docdata) { } $ibx->init_inbox(0, $skip_epoch, $skip_artnum); +my $f = "$inboxdir/description"; +if (sysopen $fh, $f, O_CREAT|O_EXCL|O_WRONLY) { + print $fh "public inbox for $address[0]\n"; + close $fh; +} + # needed for git prior to v2.1.0 -umask(0077) if defined $perm; +umask(0077); require PublicInbox::Spawn; PublicInbox::Spawn->import(qw(run_die)); @@ -240,17 +246,6 @@ for my $kv (@c_extra) { } # needed for git prior to v2.1.0 -if (defined $perm) { - chmod($perm & 07777, $pi_config_tmp) or - die "(f)chmod failed on future $pi_config: $!\n"; -} - -rename $pi_config_tmp, $pi_config or - die "failed to rename `$pi_config_tmp' to `$pi_config': $!\n"; +chmod($perm & 07777, $pi_config_tmp); +rename $pi_config_tmp, $pi_config; undef $auto_unlink; # trigger ->DESTROY - -my $f = "$inboxdir/description"; -if (sysopen $fh, $f, O_CREAT|O_EXCL|O_WRONLY) { - print $fh "public inbox for $address[0]\n" or die "print($f): $!"; - close $fh or die "close($f): $!"; -} |