| 1 |
package FFI::Temp; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use 5.008004; |
| 6 |
use Carp qw( croak ); |
| 7 |
use File::Spec; |
| 8 |
use File::Temp qw( tempdir ); |
| 9 |
|
| 10 |
# ABSTRACT: Temp Dir support for FFI::Platypus |
| 11 |
our $VERSION = '2.12'; # VERSION |
| 12 |
|
| 13 |
|
| 14 |
# problem with vanilla File::Temp is that is often uses |
| 15 |
# as /tmp that has noexec turned on. Workaround is to |
| 16 |
# create a temp directory in the build directory, but |
| 17 |
# we have to be careful about cleanup. This puts all that |
| 18 |
# (attempted) carefulness in one place so that when we |
| 19 |
# later discover it isn't so careful we can fix it in |
| 20 |
# one place rather thabn alllll the places that we need |
| 21 |
# temp directories. |
| 22 |
|
| 23 |
my %root; |
| 24 |
|
| 25 |
sub _root |
| 26 |
{ |
| 27 |
my $root = File::Spec->rel2abs(File::Spec->catdir(".tmp")); |
| 28 |
my $lock = File::Spec->catfile($root, "l$$"); |
| 29 |
|
| 30 |
foreach my $try (0..9) |
| 31 |
{ |
| 32 |
sleep $try if $try != 0; |
| 33 |
mkdir $root or die "unable to create temp root $!" unless -d $root; |
| 34 |
|
| 35 |
# There is a race condition here if the FFI::Temp is |
| 36 |
# used in parallel. To work around we run this 10 |
| 37 |
# times until it works. There is still a race condition |
| 38 |
# if it fails 10 times, but hopefully that is unlikely. |
| 39 |
|
| 40 |
# ??: doesn't account for fork, but probably doesn't need to. |
| 41 |
open my $fh, '>', $lock or next; |
| 42 |
close $fh or next; |
| 43 |
|
| 44 |
$root{$root} = 1; |
| 45 |
return $root; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
END { |
| 50 |
foreach my $root (keys %root) |
| 51 |
{ |
| 52 |
my $lock = File::Spec->catfile($root, "l$$"); |
| 53 |
unlink $lock; |
| 54 |
# try to delete if possible. |
| 55 |
# if not possible then punt |
| 56 |
rmdir $root if -d $root; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
sub newdir |
| 61 |
{ |
| 62 |
my $class = shift; |
| 63 |
croak "uneven" if @_ % 2; |
| 64 |
File::Temp->newdir(DIR => _root, @_); |
| 65 |
} |
| 66 |
|
| 67 |
sub new |
| 68 |
{ |
| 69 |
my $class = shift; |
| 70 |
croak "uneven" if @_ % 2; |
| 71 |
File::Temp->new(DIR => _root, @_); |
| 72 |
} |
| 73 |
|
| 74 |
1; |
| 75 |
|
| 76 |
__END__ |
| 77 |
|
| 78 |
=pod |
| 79 |
|
| 80 |
=encoding UTF-8 |
| 81 |
|
| 82 |
=head1 NAME |
| 83 |
|
| 84 |
FFI::Temp - Temp Dir support for FFI::Platypus |
| 85 |
|
| 86 |
=head1 VERSION |
| 87 |
|
| 88 |
version 2.12 |
| 89 |
|
| 90 |
=head1 DESCRIPTION |
| 91 |
|
| 92 |
This class is private to L<FFI::Platypus>. |
| 93 |
|
| 94 |
=head1 AUTHOR |
| 95 |
|
| 96 |
Author: Graham Ollis E<lt>[email protected]<gt> |
| 97 |
|
| 98 |
Contributors: |
| 99 |
|
| 100 |
Bakkiaraj Murugesan (bakkiaraj) |
| 101 |
|
| 102 |
Dylan Cali (calid) |
| 103 |
|
| 104 |
pipcet |
| 105 |
|
| 106 |
Zaki Mughal (zmughal) |
| 107 |
|
| 108 |
Fitz Elliott (felliott) |
| 109 |
|
| 110 |
Vickenty Fesunov (vyf) |
| 111 |
|
| 112 |
Gregor Herrmann (gregoa) |
| 113 |
|
| 114 |
Shlomi Fish (shlomif) |
| 115 |
|
| 116 |
Damyan Ivanov |
| 117 |
|
| 118 |
Ilya Pavlov (Ilya33) |
| 119 |
|
| 120 |
Petr Písař (ppisar) |
| 121 |
|
| 122 |
Mohammad S Anwar (MANWAR) |
| 123 |
|
| 124 |
Håkon Hægland (hakonhagland, HAKONH) |
| 125 |
|
| 126 |
Meredith (merrilymeredith, MHOWARD) |
| 127 |
|
| 128 |
Diab Jerius (DJERIUS) |
| 129 |
|
| 130 |
Eric Brine (IKEGAMI) |
| 131 |
|
| 132 |
szTheory |
| 133 |
|
| 134 |
José Joaquín Atria (JJATRIA) |
| 135 |
|
| 136 |
Pete Houston (openstrike, HOUSTON) |
| 137 |
|
| 138 |
Lukas Mai (MAUKE) |
| 139 |
|
| 140 |
=head1 COPYRIGHT AND LICENSE |
| 141 |
|
| 142 |
This software is copyright (c) 2015-2022 by Graham Ollis. |
| 143 |
|
| 144 |
This is free software; you can redistribute it and/or modify it under |
| 145 |
the same terms as the Perl 5 programming language system itself. |
| 146 |
|
| 147 |
=cut |