]> Sergey Matveev's repositories - public-inbox.git/commitdiff
v2writable: git_hash_raw: avoid $TMPDIR write
authorEric Wong <e@yhbt.net>
Fri, 17 Jul 2020 07:25:07 +0000 (07:25 +0000)
committerEric Wong <e@yhbt.net>
Fri, 17 Jul 2020 20:56:55 +0000 (20:56 +0000)
We can rely on FD_CLOEXEC being set by default (since Perl 5.6+)
on pipes to avoid FS/page-cache traffic, here.  We also know
"git hash-object" won't output anything until it's consumed all
of its standard input; so there's no danger of a deadlock even
in the the unlikely case git uses a hash that can't fit into
PIPE_BUF :P

lib/PublicInbox/V2Writable.pm

index dffe90d8cb05b4bffe8acd7c9f914e3cd0d8f79d..0582dd5e32e8e87484c64d01447253af221e4a0c 100644 (file)
@@ -482,14 +482,12 @@ sub purge {
 sub git_hash_raw ($$) {
        my ($self, $raw) = @_;
        # grab the expected OID we have to reindex:
-       open my $tmp_fh, '+>', undef or die "failed to open tmp: $!";
-       $tmp_fh->autoflush(1);
-       print $tmp_fh $$raw or die "print \$tmp_fh: $!";
-       sysseek($tmp_fh, 0, 0) or die "seek failed: $!";
-
+       pipe(my($in, $w)) or die "pipe: $!";
        my $git_dir = $self->{-inbox}->git->{git_dir};
        my $cmd = ['git', "--git-dir=$git_dir", qw(hash-object --stdin)];
-       my $r = popen_rd($cmd, undef, { 0 => $tmp_fh });
+       my $r = popen_rd($cmd, undef, { 0 => $in });
+       print $w $$raw or die "print \$w: $!";
+       close $w or die "close \$w: $!";
        local $/ = "\n";
        chomp(my $oid = <$r>);
        close $r or die "git hash-object failed: $?";