Skip to content

Commit 5ba397f

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 c53ff69 commit 5ba397f

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
@@ -137,7 +137,7 @@ BEGIN
137137
if ($windows_os)
138138
{
139139
require Win32API::File;
140-
Win32API::File->import(qw(createFile OsFHandleOpen CloseHandle setFilePointer));
140+
Win32API::File->import(qw(createFile OsFHandleOpen CloseHandle));
141141
}
142142

143143
# Specifies whether to use Unix sockets for test setups. On
@@ -435,33 +435,33 @@ sub slurp_file
435435
my ($filename, $offset) = @_;
436436
local $/;
437437
my $contents;
438+
my $fh;
439+
440+
# On windows open file using win32 APIs, to allow us to set the
441+
# FILE_SHARE_DELETE flag ("d" below), otherwise other accesses to the file
442+
# may fail.
438443
if ($Config{osname} ne 'MSWin32')
439444
{
440-
open(my $in, '<', $filename)
445+
open($fh, '<', $filename)
441446
or die "could not read \"$filename\": $!";
442-
if (defined($offset))
443-
{
444-
seek($in, $offset, SEEK_SET)
445-
or die "could not seek \"$filename\": $!";
446-
}
447-
$contents = <$in>;
448-
close $in;
449447
}
450448
else
451449
{
452450
my $fHandle = createFile($filename, "r", "rwd")
453451
or die "could not open \"$filename\": $^E";
454-
OsFHandleOpen(my $fh = IO::Handle->new(), $fHandle, 'r')
452+
OsFHandleOpen($fh = IO::Handle->new(), $fHandle, 'r')
455453
or die "could not read \"$filename\": $^E\n";
456-
if (defined($offset))
457-
{
458-
setFilePointer($fh, $offset, qw(FILE_BEGIN))
459-
or die "could not seek \"$filename\": $^E\n";
460-
}
461-
$contents = <$fh>;
462-
CloseHandle($fHandle)
463-
or die "could not close \"$filename\": $^E\n";
464454
}
455+
456+
if (defined($offset))
457+
{
458+
seek($fh, $offset, SEEK_SET)
459+
or die "could not seek \"$filename\": $!";
460+
}
461+
462+
$contents = <$fh>;
463+
close $fh;
464+
465465
$contents =~ s/\r\n/\n/g if $Config{osname} eq 'msys';
466466
return $contents;
467467
}

0 commit comments

Comments
 (0)