]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
treewide: simplify File::Path mkpath/make_path callers
authorEric Wong <e@80x24.org>
Wed, 22 Feb 2023 17:25:55 +0000 (17:25 +0000)
committerEric Wong <e@80x24.org>
Wed, 22 Feb 2023 21:36:19 +0000 (21:36 +0000)
File::Path already accounts for the existence of directories,
handles races from redundant mkdir(2), and croaks on
unrecoverable errors.  So there's no point in doing any
of that on our end.

Furthermore, avoiding the overhead of loading File::Path doesn't
seem worth it to save 20-60ms given the overhead of loading
our other code.  Instead, try to reduce optree overhead on
our code, instead, since File::Path gets used in a bunch of
places.

We'll also favor the newer make_path for multi-directory
invocations to avoid bloating our own optree to create an
arrayref, but mkpath is one fewer subroutine call within
File::Path itself, right now.

lib/PublicInbox/Emergency.pm
lib/PublicInbox/Import.pm
lib/PublicInbox/LEI.pm
lib/PublicInbox/LeiMirror.pm
lib/PublicInbox/LeiToMail.pm
lib/PublicInbox/SharedKV.pm
lib/PublicInbox/TestCommon.pm
t/solver_git.t
xt/imapd-mbsync-oimap.t

index 5a1ed1d7745bfb4291a19d5b100a7e80fad887be..56c58dd13996372fca6027f0146fd153fcac349f 100644 (file)
@@ -9,18 +9,11 @@ use Fcntl qw(:DEFAULT SEEK_SET);
 use Sys::Hostname qw(hostname);
 use IO::Handle; # ->flush
 use Errno qw(EEXIST);
+use File::Path ();
 
 sub new {
        my ($class, $dir) = @_;
-
-       foreach (qw(new tmp cur)) {
-               my $d = "$dir/$_";
-               next if -d $d;
-               require File::Path;
-               if (!File::Path::mkpath($d) && !-d $d) {
-                       die "failed to mkpath($d): $!\n";
-               }
-       }
+       File::Path::make_path(map { $dir.$_ } qw(/tmp /new /cur));
        bless { dir => $dir, t => 0 }, $class;
 }
 
index 0419217419e65116d57c0f2d0613b74f601e0b9a..39719bcb43c577a1dd3749d2b49f6580d0111ea3 100644 (file)
@@ -464,7 +464,7 @@ sub init_bare {
        my ($dir, $head) = @_; # or self
        $dir = $dir->{git}->{git_dir} if ref($dir);
        require File::Path;
-       File::Path::mkpath([ map { "$dir/$_" } qw(objects/info refs/heads) ]);
+       File::Path::make_path(map { $dir.$_ } qw(/objects/info /refs/heads));
        $INIT_FILES[1] //= 'ref: '.default_branch."\n";
        my @fn_contents = @INIT_FILES;
        $fn_contents[1] = "ref: refs/heads/$head\n" if defined $head;
index d05b20dee4873ce57a6e0d9c44bf54d21dbe6c37..b83de91d43e4db84a1011811e461a56fc3555aa6 100644 (file)
@@ -24,7 +24,7 @@ use PublicInbox::Eml;
 use PublicInbox::Import;
 use PublicInbox::ContentHash qw(git_sha);
 use Time::HiRes qw(stat); # ctime comparisons for config cache
-use File::Path qw(mkpath);
+use File::Path ();
 use File::Spec;
 use Sys::Syslog qw(openlog syslog closelog);
 our $quit = \&CORE::exit;
@@ -852,7 +852,7 @@ sub _lei_cfg ($;$) {
                        return bless {}, 'PublicInbox::Config';
                }
                my ($cfg_dir) = ($f =~ m!(.*?/)[^/]+\z!);
-               -d $cfg_dir or mkpath($cfg_dir) or die "mkpath($cfg_dir): $!\n";
+               File::Path::mkpath($cfg_dir);
                open my $fh, '>>', $f or die "open($f): $!\n";
                @st = stat($fh) or die "fstat($f): $!\n";
                $cur_st = pack('dd', $st[10], $st[7]);
index e4aa620e7a7b7f76f6dc2770e6b857b6f49ddb1e..8731119877d60029475074c89e70704a40ba5cf2 100644 (file)
@@ -913,7 +913,7 @@ failed to extract epoch number from $src
        # filter out the epochs we skipped
        $self->{chg}->{manifest} = 1 if $m && delete(@$m{@skip});
 
-       (!$self->{dry_run} && !-d $dst) and File::Path::mkpath($dst);
+       $self->{dry_run} or File::Path::mkpath($dst);
 
        $lei->{opt}->{'inbox-config'} =~ /\A(?:always|v2)\z/s and
                _get_txt_start($task, '_/text/config/raw', $fini);
index 31eba794066a9871a9e6b80567f1e3c296d16650..e357ee00014834d1ee8236bd7349f2bee979f36a 100644 (file)
@@ -462,13 +462,8 @@ sub new {
 sub _pre_augment_maildir {
        my ($self, $lei) = @_;
        my $dst = $lei->{ovv}->{dst};
-       for my $x (qw(tmp new cur)) {
-               my $d = $dst.$x;
-               next if -d $d;
-               require File::Path;
-               File::Path::mkpath($d);
-               -d $d or die "$d is not a directory";
-       }
+       require File::Path;
+       File::Path::make_path(map { $dst.$_ } qw(tmp new cur));
        # for utime, so no opendir
        open $self->{poke_dh}, '<', "${dst}cur" or die "open ${dst}cur: $!";
 }
index 90ccf2b4388c34dc48b21ad647c60b47b1ab98c0..89ab3f7401d1b901d1ed7e73c86f65b77b2caab1 100644 (file)
@@ -11,7 +11,7 @@ use parent qw(PublicInbox::Lock);
 use File::Temp qw(tempdir);
 use DBI qw(:sql_types); # SQL_BLOB
 use PublicInbox::Spawn;
-use File::Path qw(rmtree make_path);
+use File::Path qw(rmtree);
 
 sub dbh {
        my ($self, $lock) = @_;
@@ -43,7 +43,7 @@ CREATE TABLE IF NOT EXISTS kv (
 sub new {
        my ($cls, $dir, $base, $opt) = @_;
        my $self = bless { opt => $opt }, $cls;
-       make_path($dir) if defined($dir) && !-d $dir;
+       File::Path::mkpath($dir) if defined($dir);
        $dir //= $self->{"tmp$$.$self"} = tempdir("skv.$$-XXXX", TMPDIR => 1);
        $base //= '';
        my $f = $self->{filename} = "$dir/$base.sqlite3";
index 8a34e45a24a48d2d6682e0298380bdbe222c2b54..fe7af452eec67a5336f33f1f4aeb5823a05e4496 100644 (file)
@@ -607,7 +607,7 @@ SKIP: {
        $lei_opt = { 1 => \$lei_out, 2 => \$lei_err };
        my ($daemon_pid, $for_destroy, $daemon_xrd);
        my $tmpdir = $test_opt->{tmpdir};
-       File::Path::mkpath($tmpdir) if (defined $tmpdir && !-d $tmpdir);
+       File::Path::mkpath($tmpdir) if defined $tmpdir;
        ($tmpdir, $for_destroy) = tmpdir unless $tmpdir;
        state $persist_xrd = $ENV{TEST_LEI_DAEMON_PERSIST_DIR};
        SKIP: {
index e8d9feb9366182d64cf2919432f076a5aa2dab92..02d90e5dfc7678c026318a5bcd687ce6bdb08fed 100644 (file)
@@ -30,7 +30,7 @@ my $ibx = create_inbox 'v2', version => 2,
        $im->add($patch2) or BAIL_OUT;
 };
 my $md = "$tmpdir/md";
-File::Path::mkpath([map { $md.$_ } (qw(/ /cur /new /tmp))]);
+File::Path::make_path(map { $md.$_ } (qw(/cur /new /tmp)));
 symlink(abs_path('t/solve/0001-simple-mod.patch'), "$md/cur/foo:2,") or
        xbail "symlink: $!";
 
index b02811055e5336a8e4124c08f636a8a15aeaf639..f99779a15df7634d12fec2c2b0e43d44ba812666 100644 (file)
@@ -4,7 +4,7 @@
 # ensure mbsync and offlineimap compatibility
 use strict;
 use v5.10.1;
-use File::Path qw(mkpath);
+use File::Path qw(make_path);
 use PublicInbox::TestCommon;
 use PublicInbox::Spawn qw(spawn);
 require_mods(qw(-imapd));
@@ -41,7 +41,7 @@ my ($host, $port) = ($sock->sockhost, $sock->sockport);
 my %pids;
 
 SKIP: {
-       mkpath([map { "$tmpdir/oimapdir/$_" } qw(cur new tmp)]);
+       make_path(map { "$tmpdir/oimapdir/$_" } qw(cur new tmp));
        my $oimap = require_cmd('offlineimap', 1) or
                skip 'no offlineimap(1)', 1;
        open my $fh, '>', "$tmpdir/.offlineimaprc" or BAIL_OUT "open: $!";
@@ -78,7 +78,7 @@ EOF
 }
 
 SKIP: {
-       mkpath([map { "$tmpdir/mbsyncdir/test/$_" } qw(cur new tmp)]);
+       make_path(map { "$tmpdir/mbsyncdir/test/$_" } qw(cur new tmp));
        my $mbsync = require_cmd('mbsync', 1) or skip 'no mbsync(1)', 1;
        open my $fh, '>', "$tmpdir/.mbsyncrc" or BAIL_OUT "open: $!";
        print $fh <<EOF or BAIL_OUT "print: $!";