Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2f74db1

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 8162464 commit 2f74db1

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

src/test/perl/TestLib.pm

+18-18
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
@@ -492,33 +492,33 @@ sub slurp_file
492492
my ($filename, $offset) = @_;
493493
local $/;
494494
my $contents;
495+
my $fh;
496+
497+
# On windows open file using win32 APIs, to allow us to set the
498+
# FILE_SHARE_DELETE flag ("d" below), otherwise other accesses to the file
499+
# may fail.
495500
if ($Config{osname} ne 'MSWin32')
496501
{
497-
open(my $in, '<', $filename)
502+
open($fh, '<', $filename)
498503
or croak "could not read \"$filename\": $!";
499-
if (defined($offset))
500-
{
501-
seek($in, $offset, SEEK_SET)
502-
or croak "could not seek \"$filename\": $!";
503-
}
504-
$contents = <$in>;
505-
close $in;
506504
}
507505
else
508506
{
509507
my $fHandle = createFile($filename, "r", "rwd")
510508
or croak "could not open \"$filename\": $^E";
511-
OsFHandleOpen(my $fh = IO::Handle->new(), $fHandle, 'r')
509+
OsFHandleOpen($fh = IO::Handle->new(), $fHandle, 'r')
512510
or croak "could not read \"$filename\": $^E\n";
513-
if (defined($offset))
514-
{
515-
setFilePointer($fh, $offset, qw(FILE_BEGIN))
516-
or croak "could not seek \"$filename\": $^E\n";
517-
}
518-
$contents = <$fh>;
519-
CloseHandle($fHandle)
520-
or croak "could not close \"$filename\": $^E\n";
521511
}
512+
513+
if (defined($offset))
514+
{
515+
seek($fh, $offset, SEEK_SET)
516+
or croak "could not seek \"$filename\": $!";
517+
}
518+
519+
$contents = <$fh>;
520+
close $fh;
521+
522522
$contents =~ s/\r\n/\n/g if $Config{osname} eq 'msys';
523523
return $contents;
524524
}

0 commit comments

Comments
 (0)