1# -*- cperl -*- 2# Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved. 3# 4# This program is free software; you can redistribute it and/or modify 5# it under the terms of the GNU General Public License, version 2.0, 6# as published by the Free Software Foundation. 7# 8# This program is also distributed with certain software (including 9# but not limited to OpenSSL) that is licensed under separate terms, 10# as designated in a particular file or component or in included license 11# documentation. The authors of MySQL hereby grant you an additional 12# permission to link the program and your derivative works with the 13# separately licensed software that they have included with MySQL. 14# 15# This program is distributed in the hope that it will be useful, 16# but WITHOUT ANY WARRANTY; without even the implied warranty of 17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18# GNU General Public License, version 2.0, for more details. 19# 20# You should have received a copy of the GNU General Public License 21# along with this program; if not, write to the Free Software 22# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 23 24# This is a library file used by the Perl version of mysql-test-run, 25# and is part of the translation of the Bourne shell script with the 26# same name. 27 28use strict; 29 30sub mtr_get_pid_from_file ($); 31sub mtr_get_opts_from_file ($); 32sub mtr_fromfile ($); 33sub mtr_tofile ($@); 34sub mtr_tonewfile($@); 35sub mtr_lastlinefromfile($); 36sub mtr_appendfile_to_file ($$); 37sub mtr_grab_file($); 38 39 40############################################################################## 41# 42# 43# 44############################################################################## 45 46sub mtr_get_pid_from_file ($) { 47 my $pid_file_path= shift; 48 my $TOTAL_ATTEMPTS= 30; 49 my $timeout= 1; 50 51 # We should read from the file until we get correct pid. As it is 52 # stated in BUG#21884, pid file can be empty at some moment. So, we should 53 # read it until we get valid data. 54 55 for (my $cur_attempt= 1; $cur_attempt <= $TOTAL_ATTEMPTS; ++$cur_attempt) 56 { 57 mtr_debug("Reading pid file '$pid_file_path' " . 58 "($cur_attempt of $TOTAL_ATTEMPTS)..."); 59 60 open(FILE, '<', $pid_file_path) 61 or mtr_error("can't open file \"$pid_file_path\": $!"); 62 63 # Read pid number from file 64 my $pid= <FILE>; 65 chomp $pid; 66 close FILE; 67 68 return $pid if $pid=~ /^(\d+)/; 69 70 mtr_debug("Pid file '$pid_file_path' does not yet contain pid number.\n" . 71 "Sleeping $timeout second(s) more..."); 72 73 sleep($timeout); 74 } 75 76 mtr_error("Pid file '$pid_file_path' is corrupted. " . 77 "Can not retrieve PID in " . 78 ($timeout * $TOTAL_ATTEMPTS) . " seconds."); 79} 80 81sub mtr_get_opts_from_file ($) { 82 my $file= shift; 83 84 open(FILE,"<",$file) or mtr_error("can't open file \"$file\": $!"); 85 my @args; 86 while ( <FILE> ) 87 { 88 chomp; 89 90 # --init_connect=set @a='a\\0c' 91 s/^\s+//; # Remove leading space 92 s/\s+$//; # Remove ending space 93 94 # This is strange, but we need to fill whitespace inside 95 # quotes with something, to remove later. We do this to 96 # be able to split on space. Else, we have trouble with 97 # options like 98 # 99 # --someopt="--insideopt1 --insideopt2" 100 # 101 # But still with this, we are not 100% sure it is right, 102 # we need a shell to do it right. 103 104# print STDERR "\n"; 105# print STDERR "AAA: $_\n"; 106 107 s/\'([^\'\"]*)\'/unspace($1,"\x0a")/ge; 108 s/\"([^\'\"]*)\"/unspace($1,"\x0b")/ge; 109 s/\'([^\'\"]*)\'/unspace($1,"\x0a")/ge; 110 s/\"([^\'\"]*)\"/unspace($1,"\x0b")/ge; 111 112# print STDERR "BBB: $_\n"; 113 114# foreach my $arg (/(--?\w.*?)(?=\s+--?\w|$)/) 115 116 # FIXME ENV vars should be expanded!!!! 117 118 foreach my $arg (split(/[ \t]+/)) 119 { 120 $arg =~ tr/\x11\x0a\x0b/ \'\"/; # Put back real chars 121 # The outermost quotes has to go 122 $arg =~ s/^([^\'\"]*)\'(.*)\'([^\'\"]*)$/$1$2$3/ 123 or $arg =~ s/^([^\'\"]*)\"(.*)\"([^\'\"]*)$/$1$2$3/; 124 $arg =~ s/\\\\/\\/g; 125 126 $arg =~ s/\$\{(\w+)\}/envsubst($1)/ge; 127 $arg =~ s/\$(\w+)/envsubst($1)/ge; 128 129# print STDERR "ARG: $arg\n"; 130 # Do not pass empty string since my_getopt is not capable to handle it. 131 if (length($arg)) 132 { 133 push(@args, $arg) 134 } 135 } 136 } 137 close FILE; 138 return \@args; 139} 140 141sub envsubst { 142 my $string= shift; 143 144 if ( ! defined $ENV{$string} ) 145 { 146 mtr_error("opt file referense \$$string that is unknown"); 147 } 148 149 return $ENV{$string}; 150} 151 152sub unspace { 153 my $string= shift; 154 my $quote= shift; 155 $string =~ s/[ \t]/\x11/g; 156 return "$quote$string$quote"; 157} 158 159# Read a whole file, stripping leading and trailing whitespace. 160sub mtr_fromfile ($) { 161 my $file= shift; 162 163 open(FILE,"<",$file) or mtr_error("can't open file \"$file\": $!"); 164 my $text= join('', <FILE>); 165 close FILE; 166 $text =~ s/^\s+//; # Remove starting space, incl newlines 167 $text =~ s/\s+$//; # Remove ending space, incl newlines 168 return $text; 169} 170 171sub mtr_lastlinefromfile ($) { 172 my $file= shift; 173 my $text; 174 175 open(FILE,"<",$file) or mtr_error("can't open file \"$file\": $!"); 176 while (my $line= <FILE>) 177 { 178 $text= $line; 179 } 180 close FILE; 181 return $text; 182} 183 184 185sub mtr_tofile ($@) { 186 my $file= shift; 187 188 open(FILE,">>",$file) or mtr_error("can't open file \"$file\": $!"); 189 print FILE join("", @_); 190 close FILE; 191} 192 193sub mtr_tonewfile ($@) { 194 my $file= shift; 195 196 open(FILE,">",$file) or mtr_error("can't open file \"$file\": $!"); 197 print FILE join("", @_); 198 close FILE; 199} 200 201sub mtr_appendfile_to_file ($$) { 202 my $from_file= shift; 203 my $to_file= shift; 204 205 open(TOFILE,">>",$to_file) or mtr_error("can't open file \"$to_file\": $!"); 206 open(FROMFILE,"<",$from_file) 207 or mtr_error("can't open file \"$from_file\": $!"); 208 print TOFILE while (<FROMFILE>); 209 close FROMFILE; 210 close TOFILE; 211} 212 213# Read a whole file verbatim. 214sub mtr_grab_file($) { 215 my $file= shift; 216 open(FILE, '<', $file) 217 or return undef; 218 local $/= undef; 219 my $data= scalar(<FILE>); 220 close FILE; 221 return $data; 222} 223 224 2251; 226