about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2019-11-15 09:50:36 +0000
committerEric Wong <e@80x24.org>2019-11-16 11:05:23 +0000
commitca783e786a2559ecd161c33be17e46d3baaa25be (patch)
tree13bf12e65cbcd238285f542264eccda2fe14b29e
parentd155f989769861be45938345350f07365ca945c1 (diff)
downloadpublic-inbox-ca783e786a2559ecd161c33be17e46d3baaa25be.tar.gz
Avoid 'Variable "%s" will not stay shared' warnings
when the contents of this script eval'ed into a sub.

We also need to rely on ->DESTROY instead of END{}
to unlink the lock file on sub exit.
-rwxr-xr-xscript/public-inbox-init27
1 files changed, 22 insertions, 5 deletions
diff --git a/script/public-inbox-init b/script/public-inbox-init
index 50711266..da683657 100755
--- a/script/public-inbox-init
+++ b/script/public-inbox-init
@@ -5,7 +5,12 @@
 # Initializes a public-inbox, basically a wrapper for git-init(1)
 use strict;
 use warnings;
-my $usage = "public-inbox-init NAME INBOX_DIR HTTP_URL ADDRESS [ADDRESS..]";
+sub usage {
+        print STDERR <<EOF;
+Usage: public-inbox-init NAME INBOX_DIR HTTP_URL ADDRESS [ADDRESS..]
+EOF
+        exit 1;
+}
 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
 use PublicInbox::Admin;
 PublicInbox::Admin::require_or_die('-base');
@@ -19,7 +24,6 @@ use Fcntl qw(:DEFAULT);
 use Cwd qw/abs_path/;
 
 sub x { system(@_) and die join(' ', @_). " failed: $?\n" }
-sub usage { print STDERR "Usage: $usage\n"; exit 1 }
 my $version = undef;
 my $indexlevel = undef;
 my $skip_epoch;
@@ -57,12 +61,10 @@ my ($fh, $pi_config_tmp) = tempfile('pi-init-XXXXXXXX', DIR => $dir);
 my $lockfile = "$pi_config.lock";
 my $lockfh;
 sysopen($lockfh, $lockfile, O_RDWR|O_CREAT|O_EXCL) or do {
-        $lockfh = undef;
         warn "could not open config file: $lockfile: $!\n";
         exit(255);
 };
-END { unlink($lockfile) if $lockfh };
-
+my $auto_unlink = UnlinkMe->new($lockfile);
 my $perm;
 if (-e $pi_config) {
         open(my $oh, '<', $pi_config) or die "unable to read $pi_config: $!\n";
@@ -166,3 +168,18 @@ if (defined $perm) {
 
 rename $pi_config_tmp, $pi_config or
         die "failed to rename `$pi_config_tmp' to `$pi_config': $!\n";
+$auto_unlink->DESTROY;
+
+package UnlinkMe;
+use strict;
+
+sub new {
+        my ($klass, $file) = @_;
+        bless { file => $file }, $klass;
+}
+
+sub DESTROY {
+        my $f = delete($_[0]->{file});
+        unlink($f) if defined($f);
+}
+1;