mirror of
https://github.com/MariaDB/server.git
synced 2025-08-07 00:04:31 +03:00
MDEV-35350: Backport search_pattern_in_file.inc for SEARCH_WAIT functionality
For easier diff comparison, this commit takes search_pattern_in_file.inc from 10.11 as-is for the SEARCH_WAIT functionality added by Kristian Nielsen. The changes to make it replace wait_for_pattern_in_file.inc are in the following commit. Note that this commit breaks existing wait_for_pattern_in_file.inc usage, so any tests which use it will fail if building here.
This commit is contained in:
@@ -18,27 +18,15 @@
|
|||||||
# Optionally, SEARCH_ABORT can be set to "FOUND" or "NOT FOUND" and this
|
# Optionally, SEARCH_ABORT can be set to "FOUND" or "NOT FOUND" and this
|
||||||
# will abort if the search result doesn't match the requested one.
|
# will abort if the search result doesn't match the requested one.
|
||||||
#
|
#
|
||||||
# Optionally, SEARCH_ABORT_IS_SUCCESS can be set to inverse the error logic
|
# Optionally, SEARCH_WAIT can be set to "FOUND" or "NOT FOUND", and this
|
||||||
# when the SEARCH_ABORT condition is met. The specific cases and behaviors
|
# will wait for the condition to occur. The timeout can be set in
|
||||||
# become:
|
# SEARCH_TIMEOUT, default is 60 seconds.
|
||||||
#
|
|
||||||
# 1) In the normal case (SEARCH_ABORT_IS_SUCCESS is NOT set), when the
|
|
||||||
# matching result (FOUND N or NOT FOUND) matches SEARCH_ABORT, exit in
|
|
||||||
# error (via die); otherwise, we assume success and print the matching
|
|
||||||
# result. This is used in the normal search_pattern_in_file.inc case,
|
|
||||||
# as well as wait_for_pattern_in_file.inc when searching that some
|
|
||||||
# pattern exists at all (i.e. a result of NOT FOUND will trigger die,
|
|
||||||
# so the script can continue waiting until the result is found, and
|
|
||||||
# finally print the result).
|
|
||||||
#
|
|
||||||
# 2) If SEARCH_ABORT_IS_SUCCESS is set, then we want to inverse the logic
|
|
||||||
# from case (1). That is, if the match result is SEARCH_ABORT, this
|
|
||||||
# is the success case, and we die for all other matching results.
|
|
||||||
#
|
#
|
||||||
# Optionally, SEARCH_OUTPUT can be set to control the format of output.
|
# Optionally, SEARCH_OUTPUT can be set to control the format of output.
|
||||||
# Supported formats:
|
# Supported formats:
|
||||||
# - (default) : "FOUND n /pattern/ in FILE " or "NOT FOUND ..."
|
# - (default) : "FOUND n /pattern/ in FILE " or "NOT FOUND ..."
|
||||||
# - "matches" : Each match is printed, on a separate line
|
# - "matches" : Each match is printed, on a separate line
|
||||||
|
# - "count" : "FOUND n matches in FILE" or "NOT FOUND ..." (omit pattern)
|
||||||
#
|
#
|
||||||
# In case of
|
# In case of
|
||||||
# - SEARCH_FILE and/or SEARCH_PATTERN is not set
|
# - SEARCH_FILE and/or SEARCH_PATTERN is not set
|
||||||
@@ -71,54 +59,65 @@ perl;
|
|||||||
my @search_files= glob($ENV{SEARCH_FILE});
|
my @search_files= glob($ENV{SEARCH_FILE});
|
||||||
my $search_pattern= $ENV{SEARCH_PATTERN} or die "SEARCH_PATTERN not set";
|
my $search_pattern= $ENV{SEARCH_PATTERN} or die "SEARCH_PATTERN not set";
|
||||||
my $search_range= $ENV{SEARCH_RANGE};
|
my $search_range= $ENV{SEARCH_RANGE};
|
||||||
my $content;
|
my $timeout= $ENV{SEARCH_TIMEOUT} || 60;
|
||||||
foreach my $search_file (@search_files) {
|
my @matches;
|
||||||
open(FILE, '<', $search_file) || die("Can't open file $search_file: $!");
|
my $res;
|
||||||
my $file_content;
|
|
||||||
if ($search_range > 0) {
|
my $start_time= time();
|
||||||
read(FILE, $file_content, $search_range, 0);
|
for (;;) {
|
||||||
} elsif ($search_range < 0) {
|
my $content;
|
||||||
my $size= -s $search_file;
|
foreach my $search_file (@search_files) {
|
||||||
$search_range = -$size if $size > -$search_range;
|
open(FILE, '<', $search_file) || die("Can't open file $search_file: $!");
|
||||||
seek(FILE, $search_range, 2);
|
my $file_content;
|
||||||
read(FILE, $file_content, -$search_range, 0);
|
if ($search_range > 0) {
|
||||||
} else {
|
read(FILE, $file_content, $search_range, 0);
|
||||||
while(<FILE>) { # error log
|
} elsif ($search_range < 0) {
|
||||||
if (/^CURRENT_TEST:/) {
|
my $size= -s $search_file;
|
||||||
$content='';
|
$search_range = -$size if $size > -$search_range;
|
||||||
|
seek(FILE, $search_range, 2);
|
||||||
|
read(FILE, $file_content, -$search_range, 0);
|
||||||
} else {
|
} else {
|
||||||
$content.=$_;
|
while(<FILE>) { # error log
|
||||||
|
if (/^CURRENT_TEST:/) {
|
||||||
|
$content='';
|
||||||
|
} else {
|
||||||
|
$content.=$_;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
close(FILE);
|
||||||
|
$content.= $file_content;
|
||||||
}
|
}
|
||||||
close(FILE);
|
@matches= ($content =~ /$search_pattern/gs);
|
||||||
$content.= $file_content;
|
$res=@matches ? "FOUND " . scalar(@matches) : "NOT FOUND";
|
||||||
|
|
||||||
|
if (($ENV{SEARCH_WAIT} eq 'FOUND' && $res eq 'NOT FOUND') ||
|
||||||
|
($ENV{SEARCH_WAIT} eq 'NOT FOUND' && $res =~ m{^FOUND })) {
|
||||||
|
if (time() - $start_time < $timeout) {
|
||||||
|
# Millisceond sleep emulated with select
|
||||||
|
select(undef, undef, undef, 0.1);
|
||||||
|
next;
|
||||||
|
}
|
||||||
|
die "Timeout waiting for $ENV{SEARCH_WAIT} ".
|
||||||
|
"for /$search_pattern/ in $ENV{SEARCH_FILE}\n";
|
||||||
|
}
|
||||||
|
last;
|
||||||
}
|
}
|
||||||
my @matches= ($content =~ /$search_pattern/gs);
|
|
||||||
my $res=@matches ? "FOUND " . scalar(@matches) : "NOT FOUND";
|
|
||||||
|
|
||||||
$ENV{SEARCH_FILE} =~ s{^.*?([^/\\]+)$}{$1};
|
$ENV{SEARCH_FILE} =~ s{^.*?([^/\\]+)$}{$1};
|
||||||
|
|
||||||
my $print_func= sub { print($_[0]); };
|
|
||||||
my $die_func= sub { die($_[0]); };
|
|
||||||
my $abort_func;
|
|
||||||
my $match_func;
|
|
||||||
if ($ENV{SEARCH_ABORT} and $ENV{SEARCH_ABORT_IS_SUCCESS}) {
|
|
||||||
$abort_func= \&$print_func;
|
|
||||||
$match_func= \&$die_func;
|
|
||||||
} else {
|
|
||||||
$abort_func= \&$die_func;
|
|
||||||
$match_func= \&$print_func;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($ENV{SEARCH_OUTPUT} eq "matches") {
|
if ($ENV{SEARCH_OUTPUT} eq "matches") {
|
||||||
foreach (@matches) {
|
foreach (@matches) {
|
||||||
print $_ . "\n";
|
print $_ . "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
elsif ($ENV{SEARCH_OUTPUT} eq "count")
|
||||||
|
{
|
||||||
|
print "$res matches in $ENV{SEARCH_FILE}\n";
|
||||||
|
}
|
||||||
elsif ($ENV{SEARCH_ABORT} and $res =~ /^$ENV{SEARCH_ABORT}/) {
|
elsif ($ENV{SEARCH_ABORT} and $res =~ /^$ENV{SEARCH_ABORT}/) {
|
||||||
&$abort_func("$res /$search_pattern/ in $ENV{SEARCH_FILE}\n");
|
die "$res /$search_pattern/ in $ENV{SEARCH_FILE}\n";
|
||||||
} else {
|
} else {
|
||||||
&$match_func("$res /$search_pattern/ in $ENV{SEARCH_FILE}\n");
|
print "$res /$search_pattern/ in $ENV{SEARCH_FILE}\n";
|
||||||
}
|
}
|
||||||
EOF
|
EOF
|
||||||
|
Reference in New Issue
Block a user