Use native methods to open input in TestLib::slurp_file on Windows.
authorAndrew Dunstan <[email protected]>
Sun, 24 Nov 2019 23:25:22 +0000 (18:25 -0500)
committerAndrew Dunstan <[email protected]>
Sun, 24 Nov 2019 23:25:22 +0000 (18:25 -0500)
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
backed.

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

Author: Juan José Santamaría Flecha

src/test/perl/TestLib.pm

index 905d0d178ff2caaec94d6a9a2186739c939c92dd..a377cdb226a65054134e9212c2c4aa18cf71d04e 100644 (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;
 }