Skip to content

Commit d5fa3c0

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 cd24791 commit d5fa3c0

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
@@ -90,7 +90,7 @@ BEGIN
9090
if ($windows_os)
9191
{
9292
require Win32API::File;
93-
Win32API::File->import(qw(createFile OsFHandleOpen CloseHandle setFilePointer));
93+
Win32API::File->import(qw(createFile OsFHandleOpen CloseHandle));
9494
}
9595
}
9696

@@ -259,33 +259,33 @@ sub slurp_file
259259
my ($filename, $offset) = @_;
260260
local $/;
261261
my $contents;
262+
my $fh;
263+
264+
# On windows open file using win32 APIs, to allow us to set the
265+
# FILE_SHARE_DELETE flag ("d" below), otherwise other accesses to the file
266+
# may fail.
262267
if ($Config{osname} ne 'MSWin32')
263268
{
264-
open(my $in, '<', $filename)
269+
open($fh, '<', $filename)
265270
or die "could not read \"$filename\": $!";
266-
if (defined($offset))
267-
{
268-
seek($in, $offset, SEEK_SET)
269-
or die "could not seek \"$filename\": $!";
270-
}
271-
$contents = <$in>;
272-
close $in;
273271
}
274272
else
275273
{
276274
my $fHandle = createFile($filename, "r", "rwd")
277275
or die "could not open \"$filename\": $^E";
278-
OsFHandleOpen(my $fh = IO::Handle->new(), $fHandle, 'r')
276+
OsFHandleOpen($fh = IO::Handle->new(), $fHandle, 'r')
279277
or die "could not read \"$filename\": $^E\n";
280-
if (defined($offset))
281-
{
282-
setFilePointer($fh, $offset, qw(FILE_BEGIN))
283-
or die "could not seek \"$filename\": $^E\n";
284-
}
285-
$contents = <$fh>;
286-
CloseHandle($fHandle)
287-
or die "could not close \"$filename\": $^E\n";
288278
}
279+
280+
if (defined($offset))
281+
{
282+
seek($fh, $offset, SEEK_SET)
283+
or die "could not seek \"$filename\": $!";
284+
}
285+
286+
$contents = <$fh>;
287+
close $fh;
288+
289289
$contents =~ s/\r\n/\n/g if $Config{osname} eq 'msys';
290290
return $contents;
291291
}

0 commit comments

Comments
 (0)