mirror of
https://github.com/ashaduri/gsmartcontrol.git
synced 2026-09-26 22:05:34 +00:00
Use double-quoting of commands in Windows.
This commit is contained in:
@@ -54,8 +54,8 @@ class AsyncCommandExecutor : public hz::ErrorHolder {
|
||||
|
||||
|
||||
/// Set the command to execute. Call before execute().
|
||||
/// Note: The command and the arguments _must_ be shell-escaped.
|
||||
/// Use g_shell_quote() or Glib::shell_quote(). Note that each argument
|
||||
/// Note: The command and the arguments _must_ be shell-escaped
|
||||
/// using CommandExecutor::shell_quote(). Note that each argument
|
||||
/// must be escaped separately.
|
||||
void set_command(const std::string& command_exec, const std::string& command_args);
|
||||
|
||||
|
||||
@@ -13,6 +13,8 @@ Copyright:
|
||||
#include <glib.h> // g_usleep()
|
||||
|
||||
#include "command_executor.h"
|
||||
#include "build_config.h"
|
||||
#include "hz/string_algo.h"
|
||||
|
||||
|
||||
|
||||
@@ -251,6 +253,18 @@ std::string CommandExecutor::get_error_header()
|
||||
|
||||
|
||||
|
||||
std::string CommandExecutor::shell_quote(const std::string& str)
|
||||
{
|
||||
if (!BuildEnv::is_kernel_family_windows()) {
|
||||
return Glib::shell_quote(str);
|
||||
}
|
||||
// This may be somewhat insecure, but g_spawn_command_line_async()
|
||||
// does not work with single quotes on Windows.
|
||||
return "\"" + hz::string_replace_copy(str, "\"", "\\\"") + "\"";
|
||||
}
|
||||
|
||||
|
||||
|
||||
sigc::signal<bool, CommandExecutor::TickStatus>& CommandExecutor::signal_execute_tick()
|
||||
{
|
||||
return signal_execute_tick_;
|
||||
|
||||
@@ -162,6 +162,12 @@ class CommandExecutor : public sigc::trackable {
|
||||
[[nodiscard]] std::string get_error_header();
|
||||
|
||||
|
||||
/// Quote a string for shell execution. This is similar to
|
||||
/// g_shell_quote(), but it uses double quotes in Windows so that
|
||||
/// the command can be actually executed by g_spawn_command_line_async().
|
||||
[[nodiscard]] static std::string shell_quote(const std::string& str);
|
||||
|
||||
|
||||
// ----------------- Signals
|
||||
|
||||
|
||||
|
||||
@@ -115,9 +115,9 @@ hz::ExpectedVoid<SmartctlExecutorError> execute_smartctl(const std::string& devi
|
||||
device_specific_options += " ";
|
||||
|
||||
|
||||
smartctl_ex->set_command(Glib::shell_quote(hz::fs_path_to_string(smartctl_binary)),
|
||||
smartctl_ex->set_command(CommandExecutor::shell_quote(hz::fs_path_to_string(smartctl_binary)),
|
||||
smartctl_def_options + device_specific_options + command_options
|
||||
+ " " + Glib::shell_quote(device));
|
||||
+ " " + CommandExecutor::shell_quote(device));
|
||||
|
||||
if (!smartctl_ex->execute() || !smartctl_ex->get_error_msg().empty()) {
|
||||
debug_out_warn("app", DBG_FUNC_MSG << "Smartctl binary did not execute cleanly.\n");
|
||||
|
||||
@@ -53,7 +53,7 @@ inline hz::ExpectedVoid<StorageDetectorError> execute_tw_cli(const CommandExecut
|
||||
}
|
||||
|
||||
for (const auto& bin : binaries) {
|
||||
executor->set_command(Glib::shell_quote(bin), command_options);
|
||||
executor->set_command(CommandExecutor::shell_quote(bin), command_options);
|
||||
|
||||
if (!executor->execute() || !executor->get_error_msg().empty()) {
|
||||
debug_out_warn("app", DBG_FUNC_MSG << "Error while executing tw_cli binary.\n");
|
||||
|
||||
@@ -206,7 +206,7 @@ hz::ExpectedVoid<StorageDetectorError> get_scan_open_multiport_devices(std::vect
|
||||
if (!smartctl_def_options.empty())
|
||||
smartctl_def_options += " ";
|
||||
|
||||
smartctl_ex->set_command(Glib::shell_quote(hz::fs_path_to_string(smartctl_binary)),
|
||||
smartctl_ex->set_command(CommandExecutor::shell_quote(hz::fs_path_to_string(smartctl_binary)),
|
||||
smartctl_def_options + "--scan-open");
|
||||
|
||||
if (bool execute_status = smartctl_ex->execute(); !execute_status) {
|
||||
@@ -280,7 +280,7 @@ inline hz::ExpectedVoid<StorageDetectorError> execute_areca_cli(const CommandExe
|
||||
{
|
||||
std::shared_ptr<CommandExecutor> executor = ex_factory->create_executor(CommandExecutorFactory::ExecutorType::ArecaCli);
|
||||
|
||||
executor->set_command(Glib::shell_quote(cli_binary), command_options);
|
||||
executor->set_command(CommandExecutor::shell_quote(cli_binary), command_options);
|
||||
|
||||
if (!executor->execute() || !executor->get_error_msg().empty()) {
|
||||
debug_out_warn("app", DBG_FUNC_MSG << "Error while executing Areca cli binary.\n");
|
||||
|
||||
@@ -104,8 +104,8 @@ GscMainWindow::GscMainWindow(BaseObjectType* gtkcobj, Glib::RefPtr<Gtk::Builder>
|
||||
ex.create_running_dialog(this);
|
||||
ex.set_running_msg(_("Checking if smartctl is executable..."));
|
||||
|
||||
// ex.set_command(Glib::shell_quote(smartctl_binary), smartctl_def_options + "-V"); // --version
|
||||
ex.set_command(Glib::shell_quote(smartctl_binary), "-V"); // --version
|
||||
// ex.set_command(CommandExecutor::shell_quote(smartctl_binary), smartctl_def_options + "-V"); // --version
|
||||
ex.set_command(CommandExecutor::shell_quote(smartctl_binary), "-V"); // --version
|
||||
|
||||
if (!ex.execute() || !ex.get_error_msg().empty()) {
|
||||
error_msg = ex.get_error_msg();
|
||||
@@ -946,7 +946,7 @@ void GscMainWindow::run_update_drivedb()
|
||||
if (smartctl_binary.is_absolute()) {
|
||||
update_binary_path = smartctl_binary.parent_path() / update_binary_path;
|
||||
}
|
||||
std::string update_binary = Glib::shell_quote(hz::fs_path_to_string(update_binary_path));
|
||||
std::string update_binary = CommandExecutor::shell_quote(hz::fs_path_to_string(update_binary_path));
|
||||
|
||||
if constexpr(!BuildEnv::is_kernel_family_windows()) { // X11
|
||||
update_binary = "xterm -hold -e " + update_binary;
|
||||
|
||||
Reference in New Issue
Block a user