Use native methods to open input in TestLib::slurp_file on Windows.

It is hoped that this will avoid some errors that we have seen before,
but lately with greater frequency, in buildfarm animals.

For now apply only to master. If this proves effective it can be
backpatched.

Discussion: https://postgr.es/m/13900.1572839580@sss.pgh.pa.us

Author: Juan José Santamaría Flecha
This commit is contained in:
Andrew Dunstan 2019-11-24 18:25:22 -05:00
parent d3aa114ac4
commit 114541d58e
1 changed files with 23 additions and 4 deletions

View File

@ -112,6 +112,11 @@ BEGIN
# Must be set early
$windows_os = $Config{osname} eq 'MSWin32' || $Config{osname} eq 'msys';
if ($windows_os)
{
require Win32API::File;
Win32API::File->import(qw(createFile OsFHandleOpen CloseHandle));
}
}
=pod
@ -394,10 +399,24 @@ sub slurp_file
{
my ($filename) = @_;
local $/;
open(my $in, '<', $filename)
or die "could not read \"$filename\": $!";
my $contents = <$in>;
close $in;
my $contents;
if (!$windows_os)
{
open(my $in, '<', $filename)
or die "could not read \"$filename\": $!";
$contents = <$in>;
close $in;
}
else
{
my $fHandle = createFile($filename, "r", "rwd")
or die "could not open \"$filename\": $^E";
OsFHandleOpen(my $fh = IO::Handle->new(), $fHandle, 'r')
or die "could not read \"$filename\": $^E\n";
$contents = <$fh>;
CloseHandle($fHandle)
or die "could not close \"$filename\": $^E\n";
}
$contents =~ s/\r//g if $Config{osname} eq 'msys';
return $contents;
}