You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
5.6 KiB

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