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;
7 our @EXPORT = qw(tmpfile);
8 use Fcntl qw(:DEFAULT);
12 # use tmpfile instead of open(..., '+>', undef) so we can get an
13 # unlinked filename which makes sense when viewed with lsof
15 # And if we ever stop caring to have debuggable filenames, O_TMPFILE :)
17 my ($id, $sock, $append) = @_;
19 # add the socket inode number so we can figure out which
20 # socket it belongs to
22 $id .= '-ino:'.$st[1];
26 my $fl = O_RDWR | O_CREAT | O_EXCL;
27 $fl |= O_APPEND if $append;
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
34 } while ($! == EEXIST);
35 undef # EMFILE/ENFILE/ENOSPC/ENOMEM