Skip to content

Commit 186caee

Browse files
authored
Merge pull request #30 from timgimyee/file-permissions
Add PERMS options to create temp file with given file permissions
2 parents 441aa56 + 7aede1e commit 186caee

File tree

3 files changed

+58
-24
lines changed

3 files changed

+58
-24
lines changed

Changes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{{$NEXT}}
22
- add AppVeyor CI
3+
- Add PERMS options to create temp file with given file permissions
34

45
0.2309 2019-01-06 20:29:15Z
56
- fix longstanding pod formatting error (issue #19, RT#109526)

lib/File/Temp.pm

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ my %FILES_CREATED_BY_OBJECT;
307307
# use of the O_TEMPORARY flag to sysopen.
308308
# Usually irrelevant on unix
309309
# "use_exlock" => Indicates that O_EXLOCK should be used. Default is false.
310+
# "file_permissions" => file permissions for sysopen(). Default is 0600.
310311

311312
# Optionally a reference to a scalar can be passed into the function
312313
# On error this will be used to store the reason for the error
@@ -339,12 +340,13 @@ sub _gettemp {
339340

340341
# Default options
341342
my %options = (
342-
"open" => 0,
343-
"mkdir" => 0,
344-
"suffixlen" => 0,
345-
"unlink_on_close" => 0,
346-
"use_exlock" => 0,
347-
"ErrStr" => \$tempErrStr,
343+
"open" => 0,
344+
"mkdir" => 0,
345+
"suffixlen" => 0,
346+
"unlink_on_close" => 0,
347+
"use_exlock" => 0,
348+
"ErrStr" => \$tempErrStr,
349+
"file_permissions" => undef,
348350
);
349351

350352
# Read the template
@@ -480,6 +482,9 @@ sub _gettemp {
480482
}
481483
}
482484

485+
my $perms = $options{file_permissions};
486+
my $has_perms = defined $perms;
487+
$perms = 0600 unless $has_perms;
483488

484489
# Now try MAX_TRIES time to open the file
485490
for (my $i = 0; $i < MAX_TRIES; $i++) {
@@ -502,19 +507,19 @@ sub _gettemp {
502507
my $open_success = undef;
503508
if ( $^O eq 'VMS' and $options{"unlink_on_close"} && !$KEEP_ALL) {
504509
# make it auto delete on close by setting FAB$V_DLT bit
505-
$fh = VMS::Stdio::vmssysopen($path, $OPENFLAGS, 0600, 'fop=dlt');
510+
$fh = VMS::Stdio::vmssysopen($path, $OPENFLAGS, $perms, 'fop=dlt');
506511
$open_success = $fh;
507512
} else {
508513
my $flags = ( ($options{"unlink_on_close"} && !$KEEP_ALL) ?
509514
$OPENTEMPFLAGS :
510515
$OPENFLAGS );
511516
$flags |= $LOCKFLAG if (defined $LOCKFLAG && $options{use_exlock});
512-
$open_success = sysopen($fh, $path, $flags, 0600);
517+
$open_success = sysopen($fh, $path, $flags, $perms);
513518
}
514519
if ( $open_success ) {
515520

516521
# in case of odd umask force rw
517-
chmod(0600, $path);
522+
chmod($perms, $path) unless $has_perms;
518523

519524
# Opened successfully - return file handle and name
520525
return ($fh, $path);
@@ -1048,7 +1053,8 @@ that the temporary file is removed by the object destructor
10481053
if UNLINK is set to true (the default).
10491054
10501055
Supported arguments are the same as for C<tempfile>: UNLINK
1051-
(defaulting to true), DIR, EXLOCK and SUFFIX. Additionally, the filename
1056+
(defaulting to true), DIR, EXLOCK, PERMS and SUFFIX.
1057+
Additionally, the filename
10521058
template is specified using the TEMPLATE option. The OPEN option
10531059
is not supported (the file is always opened).
10541060
@@ -1359,6 +1365,11 @@ versions, explicitly set C<< EXLOCK=>0 >>.
13591365
13601366
($fh, $filename) = tempfile($template, EXLOCK => 1);
13611367
1368+
By default, the temp file is created with 0600 file permissions.
1369+
Use C<PERMS> to change this:
1370+
1371+
($fh, $filename) = tempfile($template, PERMS => 0666);
1372+
13621373
Options can be combined as required.
13631374
13641375
Will croak() if there is an error.
@@ -1371,6 +1382,8 @@ TMPDIR flag available since 0.19.
13711382
13721383
EXLOCK flag available since 0.19.
13731384
1385+
PERMS flag available since 0.24.
1386+
13741387
=cut
13751388

13761389
sub tempfile {
@@ -1386,8 +1399,9 @@ sub tempfile {
13861399
"SUFFIX" => '', # Template suffix
13871400
"UNLINK" => 0, # Do not unlink file on exit
13881401
"OPEN" => 1, # Open file
1389-
"TMPDIR" => 0, # Place tempfile in tempdir if template specified
1390-
"EXLOCK" => 0, # Open file with O_EXLOCK
1402+
"TMPDIR" => 0, # Place tempfile in tempdir if template specified
1403+
"EXLOCK" => 0, # Open file with O_EXLOCK
1404+
"PERMS" => undef, # File permissions
13911405
);
13921406

13931407
# Check to see whether we have an odd or even number of arguments
@@ -1464,12 +1478,13 @@ sub tempfile {
14641478
my ($fh, $path, $errstr);
14651479
croak "Error in tempfile() using template $template: $errstr"
14661480
unless (($fh, $path) = _gettemp($template,
1467-
"open" => $options{'OPEN'},
1468-
"mkdir"=> 0 ,
1469-
"unlink_on_close" => $unlink_on_close,
1470-
"suffixlen" => length($options{'SUFFIX'}),
1471-
"ErrStr" => \$errstr,
1472-
"use_exlock" => $options{EXLOCK},
1481+
"open" => $options{OPEN},
1482+
"mkdir" => 0,
1483+
"unlink_on_close" => $unlink_on_close,
1484+
"suffixlen" => length($options{SUFFIX}),
1485+
"ErrStr" => \$errstr,
1486+
"use_exlock" => $options{EXLOCK},
1487+
"file_permissions" => $options{PERMS},
14731488
) );
14741489

14751490
# Set up an exit handler that can do whatever is right for the

t/tempfile.t

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Test for File::Temp - tempfile function
33

44
use strict;
5-
use Test::More tests => 28;
5+
use Test::More tests => 30;
66
use File::Spec;
77
use Cwd qw/ cwd /;
88

@@ -100,13 +100,31 @@ ok( (-f $tempfile ), "Local tempfile in tempdir exists");
100100
push(@files, File::Spec->rel2abs($tempfile));
101101

102102
# Test tempfile
103-
# ..and another with changed permissions (read-only)
103+
# ..and another with default permissions
104104
($fh, $tempfile) = tempfile(
105-
DIR => $tempdir,
106-
);
107-
chmod 0444, $tempfile;
105+
DIR => $tempdir,
106+
);
107+
108+
ok( (-f $tempfile && -r _ && -w _),
109+
"Created tempfile with default permissions" );
110+
push(@files, File::Spec->rel2abs($tempfile));
111+
112+
# Test tempfile
113+
# ..and another with changed permissions
114+
($fh, $tempfile) = tempfile(
115+
DIR => $tempdir,
116+
PERMS => 0400,
117+
);
108118

109-
ok( (-f $tempfile ), "Local tempfile in tempdir exists read-only");
119+
# From perlport on chmod:
120+
#
121+
# (Win32) Only good for changing "owner" read-write access;
122+
# "group" and "other" bits are meaningless.
123+
#
124+
# So don't check actual file permissions -- it will be 0444 on Win32
125+
# instead of 0400. Instead, just check that no longer writable.
126+
ok( (-f $tempfile && -r _ && ! -w _),
127+
"Created tempfile with changed permissions" );
110128
push(@files, File::Spec->rel2abs($tempfile));
111129

112130
print "# TEMPFILE: Created $tempfile\n";

0 commit comments

Comments
 (0)