]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Tmpfile.pm
28e87f882efcb144df8b8dfc684ce1942a231278
[public-inbox.git] / lib / PublicInbox / Tmpfile.pm
1 # Copyright (C) 2019 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 warnings;
6 use base qw(Exporter);
7 our @EXPORT = qw(tmpfile);
8 use Fcntl qw(:DEFAULT);
9 use Errno qw(EEXIST);
10 require 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 sub tmpfile ($;$$) {
17         my ($id, $sock, $append) = @_;
18         if (defined $sock) {
19                 # add the socket inode number so we can figure out which
20                 # socket it belongs to
21                 my @st = stat($sock);
22                 $id .= '-ino:'.$st[1];
23         }
24         $id =~ tr!/!^!;
25
26         my $fl = O_RDWR | O_CREAT | O_EXCL;
27         $fl |= O_APPEND if $append;
28         do {
29                 my $fn = File::Spec->tmpdir . "/$id-".time.'-'.rand;
30                 if (sysopen(my $fh, $fn, $fl, 0600)) { # likely
31                         unlink($fn) or warn "unlink($fn): $!"; # FS broken
32                         return $fh; # success
33                 }
34         } while ($! == EEXIST);
35         undef  # EMFILE/ENFILE/ENOSPC/ENOMEM
36 }
37
38 1;