Skip to content

Commit c4465cd

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 919c08d commit c4465cd

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
@@ -149,7 +149,7 @@ BEGIN
149149
{
150150
require Win32API::File;
151151
Win32API::File->import(
152-
qw(createFile OsFHandleOpen CloseHandle setFilePointer));
152+
qw(createFile OsFHandleOpen CloseHandle));
153153
}
154154

155155
# Specifies whether to use Unix sockets for test setups. On
@@ -472,33 +472,33 @@ sub slurp_file
472472
my ($filename, $offset) = @_;
473473
local $/;
474474
my $contents;
475+
my $fh;
476+
477+
# On windows open file using win32 APIs, to allow us to set the
478+
# FILE_SHARE_DELETE flag ("d" below), otherwise other accesses to the file
479+
# may fail.
475480
if ($Config{osname} ne 'MSWin32')
476481
{
477-
open(my $in, '<', $filename)
482+
open($fh, '<', $filename)
478483
or croak "could not read \"$filename\": $!";
479-
if (defined($offset))
480-
{
481-
seek($in, $offset, SEEK_SET)
482-
or croak "could not seek \"$filename\": $!";
483-
}
484-
$contents = <$in>;
485-
close $in;
486484
}
487485
else
488486
{
489487
my $fHandle = createFile($filename, "r", "rwd")
490488
or croak "could not open \"$filename\": $^E";
491-
OsFHandleOpen(my $fh = IO::Handle->new(), $fHandle, 'r')
489+
OsFHandleOpen($fh = IO::Handle->new(), $fHandle, 'r')
492490
or croak "could not read \"$filename\": $^E\n";
493-
if (defined($offset))
494-
{
495-
setFilePointer($fh, $offset, qw(FILE_BEGIN))
496-
or croak "could not seek \"$filename\": $^E\n";
497-
}
498-
$contents = <$fh>;
499-
CloseHandle($fHandle)
500-
or croak "could not close \"$filename\": $^E\n";
501491
}
492+
493+
if (defined($offset))
494+
{
495+
seek($fh, $offset, SEEK_SET)
496+
or croak "could not seek \"$filename\": $!";
497+
}
498+
499+
$contents = <$fh>;
500+
close $fh;
501+
502502
$contents =~ s/\r\n/\n/g if $Config{osname} eq 'msys';
503503
return $contents;
504504
}

0 commit comments

Comments
 (0)