Skip to content

Commit 9e3be5c

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 b5f34ae commit 9e3be5c

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
@@ -82,7 +82,7 @@ BEGIN
8282
if ($windows_os)
8383
{
8484
require Win32API::File;
85-
Win32API::File->import(qw(createFile OsFHandleOpen CloseHandle setFilePointer));
85+
Win32API::File->import(qw(createFile OsFHandleOpen CloseHandle));
8686
}
8787
}
8888

@@ -234,33 +234,33 @@ sub slurp_file
234234
my ($filename, $offset) = @_;
235235
local $/;
236236
my $contents;
237+
my $fh;
238+
239+
# On windows open file using win32 APIs, to allow us to set the
240+
# FILE_SHARE_DELETE flag ("d" below), otherwise other accesses to the file
241+
# may fail.
237242
if ($Config{osname} ne 'MSWin32')
238243
{
239-
open(my $in, '<', $filename)
244+
open($fh, '<', $filename)
240245
or die "could not read \"$filename\": $!";
241-
if (defined($offset))
242-
{
243-
seek($in, $offset, SEEK_SET)
244-
or die "could not seek \"$filename\": $!";
245-
}
246-
$contents = <$in>;
247-
close $in;
248246
}
249247
else
250248
{
251249
my $fHandle = createFile($filename, "r", "rwd")
252250
or die "could not open \"$filename\": $^E";
253-
OsFHandleOpen(my $fh = IO::Handle->new(), $fHandle, 'r')
251+
OsFHandleOpen($fh = IO::Handle->new(), $fHandle, 'r')
254252
or die "could not read \"$filename\": $^E\n";
255-
if (defined($offset))
256-
{
257-
setFilePointer($fh, $offset, qw(FILE_BEGIN))
258-
or die "could not seek \"$filename\": $^E\n";
259-
}
260-
$contents = <$fh>;
261-
CloseHandle($fHandle)
262-
or die "could not close \"$filename\": $^E\n";
263253
}
254+
255+
if (defined($offset))
256+
{
257+
seek($fh, $offset, SEEK_SET)
258+
or die "could not seek \"$filename\": $!";
259+
}
260+
261+
$contents = <$fh>;
262+
close $fh;
263+
264264
$contents =~ s/\r\n/\n/g if $Config{osname} eq 'msys';
265265
return $contents;
266266
}

0 commit comments

Comments
 (0)