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 # TODO: O_APPEND support (this is the reason I'm not using File::Temp)
16 # And if we ever stop caring to have debuggable filenames, O_TMPFILE :)
20 # add the socket inode number so we can figure out which
21 # socket it belongs to
23 $id .= '-ino:'.$st[1];
27 my $fl = O_RDWR | O_CREAT | O_EXCL;
29 my $fn = File::Spec->tmpdir . "/$id-".time.'-'.rand;
30 if (sysopen(my $fh, $fn, $fl, 0600)) { # likely
31 unlink($fn) or die "unlink($fn): $!"; # FS broken
34 } while ($! == EEXIST);
35 undef # EMFILE/ENFILE/ENOSPC/ENOMEM