Skip to content

Commit cd1b233

Browse files
committed
Fix TestLib::slurp_file() with offset on windows.
3c5b068 used setFilePointer() to set the position of the filehandle, but passed the wrong filehandle, always leaving the position at 0. Instead of just fixing that, remove use of setFilePointer(), we have a perl fd at this point, so we can just use perl's seek(). Additionally, the perl filehandle wasn't closed, just the windows filehandle. Reviewed-By: Andrew Dunstan <andrew@dunslane.net> Author: Andres Freund <andres@anarazel.de> Discussion: https://postgr.es/m/20211003173038.64mmhgxctfqn7wl6@alap3.anarazel.de Backpatch: 9.6-, like 3c5b068
1 parent 07873a5 commit cd1b233

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

src/test/perl/TestLib.pm

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ BEGIN
9999
if ($windows_os)
100100
{
101101
require Win32API::File;
102-
Win32API::File->import(qw(createFile OsFHandleOpen CloseHandle setFilePointer));
102+
Win32API::File->import(qw(createFile OsFHandleOpen CloseHandle));
103103
}
104104
}
105105

@@ -280,33 +280,33 @@ sub slurp_file
280280
my ($filename, $offset) = @_;
281281
local $/;
282282
my $contents;
283+
my $fh;
284+
285+
# On windows open file using win32 APIs, to allow us to set the
286+
# FILE_SHARE_DELETE flag ("d" below), otherwise other accesses to the file
287+
# may fail.
283288
if ($Config{osname} ne 'MSWin32')
284289
{
285-
open(my $in, '<', $filename)
290+
open($fh, '<', $filename)
286291
or die "could not read \"$filename\": $!";
287-
if (defined($offset))
288-
{
289-
seek($in, $offset, SEEK_SET)
290-
or die "could not seek \"$filename\": $!";
291-
}
292-
$contents = <$in>;
293-
close $in;
294292
}
295293
else
296294
{
297295
my $fHandle = createFile($filename, "r", "rwd")
298296
or die "could not open \"$filename\": $^E";
299-
OsFHandleOpen(my $fh = IO::Handle->new(), $fHandle, 'r')
297+
OsFHandleOpen($fh = IO::Handle->new(), $fHandle, 'r')
300298
or die "could not read \"$filename\": $^E\n";
301-
if (defined($offset))
302-
{
303-
setFilePointer($fh, $offset, qw(FILE_BEGIN))
304-
or die "could not seek \"$filename\": $^E\n";
305-
}
306-
$contents = <$fh>;
307-
CloseHandle($fHandle)
308-
or die "could not close \"$filename\": $^E\n";
309299
}
300+
301+
if (defined($offset))
302+
{
303+
seek($fh, $offset, SEEK_SET)
304+
or die "could not seek \"$filename\": $!";
305+
}
306+
307+
$contents = <$fh>;
308+
close $fh;
309+
310310
$contents =~ s/\r\n/\n/g if $Config{osname} eq 'msys';
311311
return $contents;
312312
}

0 commit comments

Comments
 (0)