-
Notifications
You must be signed in to change notification settings - Fork 65
fix: improve hook command execution and parsing #1689
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,11 +7,12 @@ | |
| #include "hooks.h" | ||
|
|
||
| #include "cmd.h" | ||
| #include "configure.h" | ||
| #include "linglong/common/error.h" | ||
| #include "linglong/common/strings.h" | ||
| #include "linglong/utils/error/error.h" | ||
| #include "linglong/utils/log/log.h" | ||
|
|
||
| #include <fmt/format.h> | ||
|
|
||
| #include <array> | ||
|
|
@@ -46,7 +47,8 @@ | |
| cmd.setEnv(name, value); | ||
| } | ||
|
|
||
| auto result = cmd.exec({ "-c", command }); | ||
| cmd.toStdin(command); | ||
| auto result = cmd.exec({}); | ||
| if (!result.has_value()) { | ||
| return LINGLONG_ERR( | ||
| fmt::format("Hook command '{}' failed: {}.", command, result.error())); | ||
|
|
@@ -59,6 +61,17 @@ | |
| { | ||
| LINGLONG_TRACE("Parsing install hooks"); | ||
|
|
||
| auto extractValue = [](const std::string &line, std::size_t prefixLen) -> std::string { | ||
| auto trimmed = linglong::common::strings::trim(std::string_view(line).substr(prefixLen)); | ||
| if (trimmed.size() >= 2 | ||
| && ((trimmed.front() == '"' && trimmed.back() == '"') | ||
| || (trimmed.front() == '\'' && trimmed.back() == '\''))) { | ||
| trimmed.remove_prefix(1); | ||
| trimmed.remove_suffix(1); | ||
| } | ||
| return std::string(trimmed); | ||
| }; | ||
|
|
||
| std::error_code ec; | ||
| for (const auto &entry : std::filesystem::directory_iterator(LINGLONG_INSTALL_HOOKS_DIR, ec)) { | ||
| if (ec) { | ||
|
|
@@ -84,21 +97,21 @@ | |
| std::size_t pos = line.find(PRE_INSTALL_ACTION_PREFIX); | ||
| if (pos != std::string::npos) { | ||
| preInstallCommands.emplace_back( | ||
| line.substr(pos + PRE_INSTALL_ACTION_PREFIX.length())); | ||
| extractValue(line, pos + PRE_INSTALL_ACTION_PREFIX.length())); | ||
| break; | ||
| } | ||
|
|
||
| pos = line.find(POST_INSTALL_ACTION_PREFIX); | ||
| if (pos != std::string::npos) { | ||
| postInstallCommands.emplace_back( | ||
| line.substr(pos + POST_INSTALL_ACTION_PREFIX.length())); | ||
| extractValue(line, pos + POST_INSTALL_ACTION_PREFIX.length())); | ||
| break; | ||
| } | ||
|
|
||
| pos = line.find(POST_UNINSTALL_ACTION_PREFIX); | ||
| if (pos != std::string::npos) { | ||
| postUninstallCommands.emplace_back( | ||
| line.substr(pos + POST_UNINSTALL_ACTION_PREFIX.length())); | ||
| extractValue(line, pos + POST_UNINSTALL_ACTION_PREFIX.length())); | ||
| break; | ||
| } | ||
|
Comment on lines
97
to
116
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If a hook command is empty (e.g., std::size_t pos = line.find(PRE_INSTALL_ACTION_PREFIX);
if (pos != std::string::npos) {
if (auto val = extractValue(line, pos + PRE_INSTALL_ACTION_PREFIX.length()); !val.empty()) {
preInstallCommands.emplace_back(std::move(val));
}
break;
}
pos = line.find(POST_INSTALL_ACTION_PREFIX);
if (pos != std::string::npos) {
if (auto val = extractValue(line, pos + POST_INSTALL_ACTION_PREFIX.length()); !val.empty()) {
postInstallCommands.emplace_back(std::move(val));
}
break;
}
pos = line.find(POST_UNINSTALL_ACTION_PREFIX);
if (pos != std::string::npos) {
if (auto val = extractValue(line, pos + POST_UNINSTALL_ACTION_PREFIX.length()); !val.empty()) {
postUninstallCommands.emplace_back(std::move(val));
}
break;
} |
||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the hook command contains leading or trailing whitespace inside the surrounding quotes (e.g.,
ll-pre-install=" echo hello "), stripping the quotes will leave the leading/trailing whitespace intact. Additionally, if the command only contains spaces inside the quotes (e.g.,ll-pre-install=" "), it will result in a whitespace-only command. Trimming the string view again after stripping the quotes ensures that any such whitespace is cleaned up and empty commands are correctly identified.