Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for the presence of the MySQL binaries before calling them #389

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions commands/db.bee.inc
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,13 @@ function db_export_bee_callback($arguments, $options) {
$extra = '--no-tablespaces ';
}

$export_command = bee_get_executable_path('mysqldump');
if (!$export_command) {
bee_message(bt("The MySQL export command 'mysqldump' cannot be found in this system. Please install it and try again."), 'error');
return FALSE;
}
// Export and compress the database.
$export_command = 'mysqldump ';
$export_command .= ' ';
$export_command .= $connection_string;
$export_command .= ' ';
$export_command .= $extra;
Expand Down Expand Up @@ -172,11 +177,21 @@ function db_import_bee_callback($arguments, $options) {
}

// Import the database.
$mysql_bin = bee_get_executable_path('mysql');
if (!$mysql_bin) {
bee_message(bt("The MySQL client command 'mysql' cannot be found in this system. Please install it and try again."), 'error');
return FALSE;
}
$import_command = '';
if ($gzip) {
$import_command .= "gunzip -c $filename | ";
$gunzip_bin = bee_get_executable_path('gunzip');
if (!$gunzip_bin) {
bee_message(bt("The gzip decompressor command 'gunzip' cannot be found in this system. Please install it and try again."), 'error');
return FALSE;
}
$import_command .= "$gunzip_bin -c $filename | ";
}
$import_command .= 'mysql ' . $connection_string;
$import_command .= $mysql_bin . ' ' . $connection_string;
if (!$gzip) {
$import_command .= " < $filename";
}
Expand Down Expand Up @@ -218,7 +233,12 @@ function db_drop_bee_callback($arguments, $options) {
$connection_file = $connection_details['filename'];

// Drop the existing backdrop database as configured.
$command = 'mysql ' . $connection_string . ' -e "drop database ' . $db_database . '";';
$mysql_command = bee_get_executable_path('mysql');
if (!$mysql_command) {
bee_message(bt("The MySQL client command 'mysql' cannot be found in this system. Please install it and try again."), 'error');
return FALSE;
}
$command = $mysql_command . ' ' . $connection_string . ' -e "drop database ' . $db_database . '";';
$result = proc_close(proc_open($command, array(STDIN, STDOUT, STDERR), $pipes));

if ($result == -1) {
Expand All @@ -233,7 +253,7 @@ function db_drop_bee_callback($arguments, $options) {
}

// Re-create the existing backdrop database as configured.
$command = 'mysql ' . $connection_string . ' -e "create database ' . $db_database . '";';
$command = $mysql_command .' ' . $connection_string . ' -e "create database ' . $db_database . '";';
$result = proc_close(proc_open($command, array(STDIN, STDOUT, STDERR), $pipes));

if ($result == -1) {
Expand Down Expand Up @@ -265,7 +285,12 @@ function sql_bee_callback($arguments, $options) {
$connection_file = $connection_details['filename'];

// Open SQL command-line.
$command = 'mysql ' . $connection_string;
$mysql_command = bee_get_executable_path('mysql');
if (!$mysql_command) {
bee_message(bt("The MySQL client command 'mysql' cannot be found in this system. Please install it and try again."), 'error');
return FALSE;
}
$command = $mysql_command . ' ' . $connection_string;
proc_close(proc_open($command, array(STDIN, STDOUT, STDERR), $pipes));
// Remove the temporary mysql options file.
bee_delete($connection_file);
Expand Down
26 changes: 26 additions & 0 deletions includes/filesystem.inc
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,29 @@ function bee_copy($source, $destination, $self = TRUE) {

return TRUE;
}

/**
* Get the full path for a command executable if it exists in PATH.
*
* If your function calls an executable on the system rather than using `bee`,
* `backdrop` or `php` functions, you can use this function to get the full
* path of the executable or return FALSE if the executable doesn't exist. The
* result can then be checked in your calling function, and you can exit with a
* user friendly error message if the executable doesn't exist in the system.
*
* @param string $command
* Name of the command to be searched.
*
* @return string|FALSE
* Full path to the first occurence of $command in the search path.
* Returns FALSE if no matching command is found.
*/
function bee_get_executable_path($command) {
foreach (explode(":", getenv('PATH')) as $directory) {
$candidate = "$directory/$command";
if (is_executable($candidate)) {
return $candidate;
}
}
return FALSE;
}
Loading