Skip to content

Commit 0a134c8

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 d0b0b70 commit 0a134c8

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
@@ -96,7 +96,7 @@ BEGIN
9696
if ($windows_os)
9797
{
9898
require Win32API::File;
99-
Win32API::File->import(qw(createFile OsFHandleOpen CloseHandle setFilePointer));
99+
Win32API::File->import(qw(createFile OsFHandleOpen CloseHandle));
100100
}
101101
}
102102

@@ -272,33 +272,33 @@ sub slurp_file
272272
my ($filename, $offset) = @_;
273273
local $/;
274274
my $contents;
275+
my $fh;
276+
277+
# On windows open file using win32 APIs, to allow us to set the
278+
# FILE_SHARE_DELETE flag ("d" below), otherwise other accesses to the file
279+
# may fail.
275280
if ($Config{osname} ne 'MSWin32')
276281
{
277-
open(my $in, '<', $filename)
282+
open($fh, '<', $filename)
278283
or die "could not read \"$filename\": $!";
279-
if (defined($offset))
280-
{
281-
seek($in, $offset, SEEK_SET)
282-
or die "could not seek \"$filename\": $!";
283-
}
284-
$contents = <$in>;
285-
close $in;
286284
}
287285
else
288286
{
289287
my $fHandle = createFile($filename, "r", "rwd")
290288
or die "could not open \"$filename\": $^E";
291-
OsFHandleOpen(my $fh = IO::Handle->new(), $fHandle, 'r')
289+
OsFHandleOpen($fh = IO::Handle->new(), $fHandle, 'r')
292290
or die "could not read \"$filename\": $^E\n";
293-
if (defined($offset))
294-
{
295-
setFilePointer($fh, $offset, qw(FILE_BEGIN))
296-
or die "could not seek \"$filename\": $^E\n";
297-
}
298-
$contents = <$fh>;
299-
CloseHandle($fHandle)
300-
or die "could not close \"$filename\": $^E\n";
301291
}
292+
293+
if (defined($offset))
294+
{
295+
seek($fh, $offset, SEEK_SET)
296+
or die "could not seek \"$filename\": $!";
297+
}
298+
299+
$contents = <$fh>;
300+
close $fh;
301+
302302
$contents =~ s/\r\n/\n/g if $Config{osname} eq 'msys';
303303
return $contents;
304304
}

0 commit comments

Comments
 (0)