Skip to content

Commit de75766

Browse files
timgimyeemohawk2
authored andcommitted
Add WRITE_ONLY option to open temp file with O_WRONLY
1 parent 441aa56 commit de75766

File tree

3 files changed

+58
-15
lines changed

3 files changed

+58
-15
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 WRITE_ONLY option to open temp file with O_WRONLY
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: 27 additions & 13 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+
# "write_only" => Indicates that O_WRONLY should be used. Default is false.
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
@@ -501,13 +502,16 @@ sub _gettemp {
501502
# Attempt to open the file
502503
my $open_success = undef;
503504
if ( $^O eq 'VMS' and $options{"unlink_on_close"} && !$KEEP_ALL) {
505+
my $flags = $OPENFLAGS;
506+
$flags = ($flags & ~O_RDWR) | O_WRONLY if $options{write_only};
504507
# make it auto delete on close by setting FAB$V_DLT bit
505-
$fh = VMS::Stdio::vmssysopen($path, $OPENFLAGS, 0600, 'fop=dlt');
508+
$fh = VMS::Stdio::vmssysopen($path, $flags, 0600, 'fop=dlt');
506509
$open_success = $fh;
507510
} else {
508511
my $flags = ( ($options{"unlink_on_close"} && !$KEEP_ALL) ?
509512
$OPENTEMPFLAGS :
510513
$OPENFLAGS );
514+
$flags = ($flags & ~O_RDWR) | O_WRONLY if $options{write_only};
511515
$flags |= $LOCKFLAG if (defined $LOCKFLAG && $options{use_exlock});
512516
$open_success = sysopen($fh, $path, $flags, 0600);
513517
}
@@ -1048,7 +1052,8 @@ that the temporary file is removed by the object destructor
10481052
if UNLINK is set to true (the default).
10491053
10501054
Supported arguments are the same as for C<tempfile>: UNLINK
1051-
(defaulting to true), DIR, EXLOCK and SUFFIX. Additionally, the filename
1055+
(defaulting to true), DIR, EXLOCK, WRITE_ONLY and SUFFIX.
1056+
Additionally, the filename
10521057
template is specified using the TEMPLATE option. The OPEN option
10531058
is not supported (the file is always opened).
10541059
@@ -1359,6 +1364,11 @@ versions, explicitly set C<< EXLOCK=>0 >>.
13591364
13601365
($fh, $filename) = tempfile($template, EXLOCK => 1);
13611366
1367+
Normally, the temporary filehandle is opened for both reading
1368+
and writing. To open for writng only, use C<WRITE_ONLY>.
1369+
1370+
($fh, $filename) = tempfile($template, WRITE_ONLY => 1);
1371+
13621372
Options can be combined as required.
13631373
13641374
Will croak() if there is an error.
@@ -1371,6 +1381,8 @@ TMPDIR flag available since 0.19.
13711381
13721382
EXLOCK flag available since 0.19.
13731383
1384+
WRITE_ONLY flag available since 0.24.
1385+
13741386
=cut
13751387

13761388
sub tempfile {
@@ -1382,12 +1394,13 @@ sub tempfile {
13821394

13831395
# Default options
13841396
my %options = (
1385-
"DIR" => undef, # Directory prefix
1386-
"SUFFIX" => '', # Template suffix
1387-
"UNLINK" => 0, # Do not unlink file on exit
1388-
"OPEN" => 1, # Open file
1389-
"TMPDIR" => 0, # Place tempfile in tempdir if template specified
1390-
"EXLOCK" => 0, # Open file with O_EXLOCK
1397+
"DIR" => undef, # Directory prefix
1398+
"SUFFIX" => '', # Template suffix
1399+
"UNLINK" => 0, # Do not unlink file on exit
1400+
"OPEN" => 1, # Open file
1401+
"TMPDIR" => 0, # Place tempfile in tempdir if template specified
1402+
"EXLOCK" => 0, # Open file with O_EXLOCK
1403+
"WRITE_ONLY" => 0, # Open file with O_WRONLY
13911404
);
13921405

13931406
# Check to see whether we have an odd or even number of arguments
@@ -1464,12 +1477,13 @@ sub tempfile {
14641477
my ($fh, $path, $errstr);
14651478
croak "Error in tempfile() using template $template: $errstr"
14661479
unless (($fh, $path) = _gettemp($template,
1467-
"open" => $options{'OPEN'},
1468-
"mkdir"=> 0 ,
1480+
"open" => $options{OPEN},
1481+
"mkdir" => 0,
14691482
"unlink_on_close" => $unlink_on_close,
1470-
"suffixlen" => length($options{'SUFFIX'}),
1471-
"ErrStr" => \$errstr,
1472-
"use_exlock" => $options{EXLOCK},
1483+
"suffixlen" => length($options{SUFFIX}),
1484+
"ErrStr" => \$errstr,
1485+
"use_exlock" => $options{EXLOCK},
1486+
"write_only" => $options{WRITE_ONLY},
14731487
) );
14741488

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

t/tempfile.t

Lines changed: 30 additions & 2 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 => 33;
66
use File::Spec;
77
use Cwd qw/ cwd /;
88

@@ -95,8 +95,36 @@ push(@files, File::Spec->rel2abs($tempfile));
9595
DIR => $tempdir,
9696
);
9797

98-
9998
ok( (-f $tempfile ), "Local tempfile in tempdir exists");
99+
{
100+
# Catch warning when reading from write-only filehandle
101+
# or writing to read-only filehandle.
102+
my $e;
103+
local $SIG{__WARN__} = sub { $e++ };
104+
print $fh 42;
105+
<$fh>;
106+
ok( !$e, "...and filehandle opened for reading and writing" );
107+
}
108+
push(@files, File::Spec->rel2abs($tempfile));
109+
110+
# Test tempfile
111+
# ..and write-only this time
112+
($fh, $tempfile) = tempfile(
113+
DIR => $tempdir,
114+
WRITE_ONLY => 1,
115+
);
116+
117+
ok( (-f $tempfile ), "Local WRITE_ONLY tempfile in tempdir exists");
118+
{
119+
# Catch warning when reading from write-only filehandle
120+
# or writing to read-only filehandle.
121+
my $e;
122+
local $SIG{__WARN__} = sub { $e++ };
123+
print $fh 42;
124+
ok( !$e, "...and filehandle opened for writing" );
125+
<$fh>;
126+
ok( $e, "...but not reading" );
127+
}
100128
push(@files, File::Spec->rel2abs($tempfile));
101129

102130
# Test tempfile

0 commit comments

Comments
 (0)