Skip to content

Commit 5d28bd0

Browse files
timgimyeemohawk2
authored andcommitted
Add WRITE_ONLY option to open temp file with O_WRONLY
1 parent ee172ee commit 5d28bd0

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

Changes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
- add AppVeyor CI
33
- Add PERMS options to create temp file with given file permissions
44
- Document exportable functions, constants and :tags
5+
- Add WRITE_ONLY option to open temp file with O_WRONLY
56

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

lib/File/Temp.pm

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ my %FILES_CREATED_BY_OBJECT;
308308
# Usually irrelevant on unix
309309
# "use_exlock" => Indicates that O_EXLOCK should be used. Default is false.
310310
# "file_permissions" => file permissions for sysopen(). Default is 0600.
311+
# "write_only" => Indicates that O_WRONLY should be used. Default is false.
311312

312313
# Optionally a reference to a scalar can be passed into the function
313314
# On error this will be used to store the reason for the error
@@ -506,13 +507,16 @@ sub _gettemp {
506507
# Attempt to open the file
507508
my $open_success = undef;
508509
if ( $^O eq 'VMS' and $options{"unlink_on_close"} && !$KEEP_ALL) {
510+
my $flags = $OPENFLAGS;
511+
$flags = ($flags & ~O_RDWR) | O_WRONLY if $options{write_only};
509512
# make it auto delete on close by setting FAB$V_DLT bit
510-
$fh = VMS::Stdio::vmssysopen($path, $OPENFLAGS, $perms, 'fop=dlt');
513+
$fh = VMS::Stdio::vmssysopen($path, $flags, $perms, 'fop=dlt');
511514
$open_success = $fh;
512515
} else {
513516
my $flags = ( ($options{"unlink_on_close"} && !$KEEP_ALL) ?
514517
$OPENTEMPFLAGS :
515518
$OPENFLAGS );
519+
$flags = ($flags & ~O_RDWR) | O_WRONLY if $options{write_only};
516520
$flags |= $LOCKFLAG if (defined $LOCKFLAG && $options{use_exlock});
517521
$open_success = sysopen($fh, $path, $flags, $perms);
518522
}
@@ -1053,7 +1057,7 @@ that the temporary file is removed by the object destructor
10531057
if UNLINK is set to true (the default).
10541058
10551059
Supported arguments are the same as for C<tempfile>: UNLINK
1056-
(defaulting to true), DIR, EXLOCK, PERMS and SUFFIX.
1060+
(defaulting to true), DIR, EXLOCK, PERMS, WRITE_ONLY and SUFFIX.
10571061
Additionally, the filename
10581062
template is specified using the TEMPLATE option. The OPEN option
10591063
is not supported (the file is always opened).
@@ -1370,6 +1374,11 @@ Use C<PERMS> to change this:
13701374
13711375
($fh, $filename) = tempfile($template, PERMS => 0666);
13721376
1377+
Normally, the temporary filehandle is opened for both reading
1378+
and writing. To open for writing only, use C<WRITE_ONLY>.
1379+
1380+
($fh, $filename) = tempfile($template, WRITE_ONLY => 1);
1381+
13731382
Options can be combined as required.
13741383
13751384
Will croak() if there is an error.
@@ -1384,6 +1393,8 @@ EXLOCK flag available since 0.19.
13841393
13851394
PERMS flag available since 0.24.
13861395
1396+
WRITE_ONLY flag available since 0.24.
1397+
13871398
=cut
13881399

13891400
sub tempfile {
@@ -1402,6 +1413,7 @@ sub tempfile {
14021413
"TMPDIR" => 0, # Place tempfile in tempdir if template specified
14031414
"EXLOCK" => 0, # Open file with O_EXLOCK
14041415
"PERMS" => undef, # File permissions
1416+
"WRITE_ONLY" => 0, # Open file with O_WRONLY
14051417
);
14061418

14071419
# Check to see whether we have an odd or even number of arguments
@@ -1485,6 +1497,7 @@ sub tempfile {
14851497
"ErrStr" => \$errstr,
14861498
"use_exlock" => $options{EXLOCK},
14871499
"file_permissions" => $options{PERMS},
1500+
"write_only" => $options{WRITE_ONLY},
14881501
) );
14891502

14901503
# 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 => 30;
5+
use Test::More tests => 35;
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)