]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Tmpfile.pm
eb0fce0036be0c168f5f94212589661508c3e1b8
[public-inbox.git] / lib / PublicInbox / Tmpfile.pm
1 # Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 package PublicInbox::Tmpfile;
4 use strict;
5 use v5.10.1;
6 use parent qw(Exporter);
7 our @EXPORT = qw(tmpfile);
8 use Fcntl qw(:DEFAULT);
9 use Errno qw(EEXIST);
10 use File::Spec;
11
12 # use tmpfile instead of open(..., '+>', undef) so we can get an
13 # unlinked filename which makes sense when viewed with lsof
14 # (at least on Linux)
15 # And if we ever stop caring to have debuggable filenames, O_TMPFILE :)
16 #
17 # This is also for Perl <5.32 which lacks: open(..., '+>>', undef)
18 # <https://rt.perl.org/Ticket/Display.html?id=134221>
19 sub tmpfile ($;$$) {
20         my ($id, $sock, $append) = @_;
21         if (defined $sock) {
22                 # add the socket inode number so we can figure out which
23                 # socket it belongs to
24                 my @st = stat($sock);
25                 $id .= '-ino:'.$st[1];
26         }
27         $id =~ tr!/!^!;
28
29         my $fl = O_RDWR | O_CREAT | O_EXCL;
30         $fl |= O_APPEND if $append;
31         do {
32                 my $fn = File::Spec->tmpdir . "/$id-".time.'-'.rand;
33                 if (sysopen(my $fh, $fn, $fl, 0600)) { # likely
34                         unlink($fn) or warn "unlink($fn): $!"; # FS broken
35                         return $fh; # success
36                 }
37         } while ($! == EEXIST);
38         undef  # EMFILE/ENFILE/ENOSPC/ENOMEM
39 }
40
41 1;