-package File::Temp; # git description: v0.2308-7-g3bb4d88
+package File::Temp; # git description: v0.2309-16-g1d3137c
# ABSTRACT: return name and handle of a temporary file safely
-our $VERSION = '0.2309';
+our $VERSION = '0.2310';
#pod =begin :__INTERNALS
#pod
# use of the O_TEMPORARY flag to sysopen.
# Usually irrelevant on unix
# "use_exlock" => Indicates that O_EXLOCK should be used. Default is false.
+# "file_permissions" => file permissions for sysopen(). Default is 0600.
# Optionally a reference to a scalar can be passed into the function
# On error this will be used to store the reason for the error
# Default options
my %options = (
- "open" => 0,
- "mkdir" => 0,
- "suffixlen" => 0,
- "unlink_on_close" => 0,
- "use_exlock" => 0,
- "ErrStr" => \$tempErrStr,
+ "open" => 0,
+ "mkdir" => 0,
+ "suffixlen" => 0,
+ "unlink_on_close" => 0,
+ "use_exlock" => 0,
+ "ErrStr" => \$tempErrStr,
+ "file_permissions" => undef,
);
# Read the template
}
}
+ my $perms = $options{file_permissions};
+ my $has_perms = defined $perms;
+ $perms = 0600 unless $has_perms;
# Now try MAX_TRIES time to open the file
for (my $i = 0; $i < MAX_TRIES; $i++) {
my $open_success = undef;
if ( $^O eq 'VMS' and $options{"unlink_on_close"} && !$KEEP_ALL) {
# make it auto delete on close by setting FAB$V_DLT bit
- $fh = VMS::Stdio::vmssysopen($path, $OPENFLAGS, 0600, 'fop=dlt');
+ $fh = VMS::Stdio::vmssysopen($path, $OPENFLAGS, $perms, 'fop=dlt');
$open_success = $fh;
} else {
my $flags = ( ($options{"unlink_on_close"} && !$KEEP_ALL) ?
$OPENTEMPFLAGS :
$OPENFLAGS );
$flags |= $LOCKFLAG if (defined $LOCKFLAG && $options{use_exlock});
- $open_success = sysopen($fh, $path, $flags, 0600);
+ $open_success = sysopen($fh, $path, $flags, $perms);
}
if ( $open_success ) {
# in case of odd umask force rw
- chmod(0600, $path);
+ chmod($perms, $path) unless $has_perms;
# Opened successfully - return file handle and name
return ($fh, $path);
sub _can_unlink_opened_file {
- if (grep { $^O eq $_ } qw/MSWin32 os2 VMS dos MacOS haiku/) {
+ if (grep $^O eq $_, qw/MSWin32 os2 VMS dos MacOS haiku/) {
return 0;
} else {
return 1;
sub _parse_args {
my $leading_template = (scalar(@_) % 2 == 1 ? shift(@_) : '' );
my %args = @_;
- %args = map { uc($_), $args{$_} } keys %args;
+ %args = map +(uc($_) => $args{$_}), keys %args;
# template (store it in an array so that it will
# disappear from the arg list of tempfile)
#pod if UNLINK is set to true (the default).
#pod
#pod Supported arguments are the same as for C<tempfile>: UNLINK
-#pod (defaulting to true), DIR, EXLOCK and SUFFIX. Additionally, the filename
+#pod (defaulting to true), DIR, EXLOCK, PERMS and SUFFIX.
+#pod Additionally, the filename
#pod template is specified using the TEMPLATE option. The OPEN option
#pod is not supported (the file is always opened).
#pod
#pod
#pod ($fh, $filename) = tempfile($template, EXLOCK => 1);
#pod
+#pod By default, the temp file is created with 0600 file permissions.
+#pod Use C<PERMS> to change this:
+#pod
+#pod ($fh, $filename) = tempfile($template, PERMS => 0666);
+#pod
#pod Options can be combined as required.
#pod
#pod Will croak() if there is an error.
#pod
#pod EXLOCK flag available since 0.19.
#pod
+#pod PERMS flag available since 0.24.
+#pod
#pod =cut
sub tempfile {
"SUFFIX" => '', # Template suffix
"UNLINK" => 0, # Do not unlink file on exit
"OPEN" => 1, # Open file
- "TMPDIR" => 0, # Place tempfile in tempdir if template specified
- "EXLOCK" => 0, # Open file with O_EXLOCK
+ "TMPDIR" => 0, # Place tempfile in tempdir if template specified
+ "EXLOCK" => 0, # Open file with O_EXLOCK
+ "PERMS" => undef, # File permissions
);
# Check to see whether we have an odd or even number of arguments
my ($fh, $path, $errstr);
croak "Error in tempfile() using template $template: $errstr"
unless (($fh, $path) = _gettemp($template,
- "open" => $options{'OPEN'},
- "mkdir"=> 0 ,
- "unlink_on_close" => $unlink_on_close,
- "suffixlen" => length($options{'SUFFIX'}),
- "ErrStr" => \$errstr,
- "use_exlock" => $options{EXLOCK},
+ "open" => $options{OPEN},
+ "mkdir" => 0,
+ "unlink_on_close" => $unlink_on_close,
+ "suffixlen" => length($options{SUFFIX}),
+ "ErrStr" => \$errstr,
+ "use_exlock" => $options{EXLOCK},
+ "file_permissions" => $options{PERMS},
) );
# Set up an exit handler that can do whatever is right for the
package ## hide from PAUSE
File::Temp::Dir;
-our $VERSION = '0.2309';
+our $VERSION = '0.2310';
use File::Path qw/ rmtree /;
use strict;
=head1 VERSION
-version 0.2309
+version 0.2310
=head1 SYNOPSIS
if UNLINK is set to true (the default).
Supported arguments are the same as for C<tempfile>: UNLINK
-(defaulting to true), DIR, EXLOCK and SUFFIX. Additionally, the filename
+(defaulting to true), DIR, EXLOCK, PERMS and SUFFIX.
+Additionally, the filename
template is specified using the TEMPLATE option. The OPEN option
is not supported (the file is always opened).
($fh, $filename) = tempfile($template, EXLOCK => 1);
+By default, the temp file is created with 0600 file permissions.
+Use C<PERMS> to change this:
+
+ ($fh, $filename) = tempfile($template, PERMS => 0666);
+
Options can be combined as required.
Will croak() if there is an error.
EXLOCK flag available since 0.19.
+PERMS flag available since 0.24.
+
=item B<tempdir>
This is the recommended interface for creation of temporary
=head1 CONTRIBUTORS
-=for stopwords David Golden Karen Etheridge Slaven Rezic Peter Rabbitson Olivier Mengue Kevin Ryde John Acklam James E. Keenan Brian Mowrey Dagfinn Ilmari Mannsåker Steinbrunner Ed Avis Guillem Jover Ben Tilly
+=for stopwords Tim Jenness Karen Etheridge David Golden Slaven Rezic mohawk2 Roy Ivy III Peter Rabbitson Olivier Mengué John Acklam Gim Yee Nicolas R Brian Mowrey Dagfinn Ilmari Mannsåker Steinbrunner Ed Avis Guillem Jover James E. Keenan Kevin Ryde Ben Tilly
=over 4
=item *
-David Golden <dagolden@cpan.org>
+Tim Jenness <t.jenness@jach.hawaii.edu>
=item *
=item *
-Slaven Rezic <slaven@rezic.de>
+David Golden <dagolden@cpan.org>
=item *
-Peter Rabbitson <ribasushi@cpan.org>
+Slaven Rezic <srezic@cpan.org>
=item *
-Olivier Mengue <dolmen@cpan.org>
+mohawk2 <mohawk2@users.noreply.github.com>
=item *
-David Golden <xdg@xdg.me>
+Roy Ivy III <rivy.dev@gmail.com>
=item *
-Kevin Ryde <user42@zip.com.au>
+Peter Rabbitson <ribasushi@cpan.org>
+
+=item *
+
+Olivier Mengué <dolmen@cpan.org>
=item *
=item *
-Slaven Rezic <slaven.rezic@idealo.de>
+Tim Gim Yee <tim.gim.yee@gmail.com>
=item *
-James E. Keenan <jkeen@verizon.net>
+Nicolas R <atoomic@cpan.org>
=item *
=item *
+James E. Keenan <jkeen@verizon.net>
+
+=item *
+
+Kevin Ryde <user42@zip.com.au>
+
+=item *
+
Ben Tilly <btilly@gmail.com>
=back
=head1 COPYRIGHT AND LICENSE
-This software is copyright (c) 2019 by Tim Jenness and the UK Particle Physics and Astronomy Research Council.
+This software is copyright (c) 2020 by Tim Jenness and the UK Particle Physics and Astronomy Research Council.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
# Tempfile
# Open tempfile in some directory, unlink at end
-my $fh = new File::Temp( SUFFIX => '.txt' );
+my $fh = File::Temp->new( SUFFIX => '.txt' );
ok( (-f "$fh"), "File $fh exists" );
# Should still be around after closing
push(@dirs, $tempdir);
# Create file in the temp dir
-$fh = new File::Temp(
+$fh = File::Temp->new(
DIR => $tempdir,
SUFFIX => '.dat',
);
# Test tempfile
# ..and again (without unlinking it)
-$fh = new File::Temp( DIR => $tempdir, UNLINK => 0 );
+$fh = File::Temp->new( DIR => $tempdir, UNLINK => 0 );
print "# TEMPFILE: Created $fh\n";
ok( (-f "$fh" ), "Second file $fh exists in tempdir [nounlink]?");
# and another (with template)
-$fh = new File::Temp( TEMPLATE => 'helloXXXXXXX',
+$fh = File::Temp->new( TEMPLATE => 'helloXXXXXXX',
DIR => $tempdir,
SUFFIX => '.dat',
);
# Create a temporary file that should stay around after
# it has been closed
-$fh = new File::Temp( TEMPLATE => 'permXXXXXXX', UNLINK => 0);
+$fh = File::Temp->new( TEMPLATE => 'permXXXXXXX', UNLINK => 0);
print "# TEMPFILE: Created $fh\n";
ok( -f "$fh", "File $fh exists?" );
# Now create a temp file that will remain when the object
# goes out of scope because of $KEEP_ALL
-$fh = new File::Temp( TEMPLATE => 'permXXXXXXX', UNLINK => 1);
+$fh = File::Temp->new( TEMPLATE => 'permXXXXXXX', UNLINK => 1);
print "# TEMPFILE: Created $fh\n";
ok( -f "$fh", "File $fh exists?" );