1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-03-08 16:58:29 +03:00

virCommandToString: Allow stripping command path

In tests we don't want to use the full path to commands as it's
unpleasant to keep that working on all systems.

Add an integrated way to strip the prefix which will be used to replace
virTestClearCommandPath() as a more systemic solution.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
This commit is contained in:
Peter Krempa 2021-04-01 16:09:54 +02:00
parent d8f2027a79
commit 2116063791
3 changed files with 25 additions and 3 deletions

View File

@ -2030,6 +2030,7 @@ virCommandSetUID;
virCommandSetUmask;
virCommandSetWorkingDirectory;
virCommandToString;
virCommandToStringFull;
virCommandWait;
virCommandWriteArgLog;
virFork;

View File

@ -2055,9 +2055,10 @@ virCommandWriteArgLog(virCommandPtr cmd, int logfd)
/**
* virCommandToString:
* virCommandToStringFull:
* @cmd: the command to convert
* @linebreaks: true to break line after each env var or option
* @stripCommandPath: strip the path leading to the binary of @cmd
*
* Call after adding all arguments and environment settings, but
* before Run/RunAsync, to return a string representation of the
@ -2067,11 +2068,15 @@ virCommandWriteArgLog(virCommandPtr cmd, int logfd)
* Caller is responsible for freeing the resulting string.
*/
char *
virCommandToString(virCommandPtr cmd, bool linebreaks)
virCommandToStringFull(virCommandPtr cmd,
bool linebreaks,
bool stripCommandPath)
{
size_t i;
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
bool prevopt = false;
const char *command = cmd->args[0];
g_autofree char *basename = NULL;
/* Cannot assume virCommandRun will be called; so report the error
* now. If virCommandRun is called, it will report the same error. */
@ -2097,7 +2102,11 @@ virCommandToString(virCommandPtr cmd, bool linebreaks)
if (linebreaks)
virBufferAddLit(&buf, "\\\n");
}
virBufferEscapeShell(&buf, cmd->args[0]);
if (stripCommandPath)
command = basename = g_path_get_basename(command);
virBufferEscapeShell(&buf, command);
for (i = 1; i < cmd->nargs; i++) {
virBufferAddChar(&buf, ' ');
if (linebreaks) {
@ -2116,6 +2125,14 @@ virCommandToString(virCommandPtr cmd, bool linebreaks)
}
char *
virCommandToString(virCommandPtr cmd,
bool linebreaks)
{
return virCommandToStringFull(cmd, linebreaks, false);
}
int
virCommandGetArgList(virCommandPtr cmd,
char ***args,

View File

@ -171,6 +171,10 @@ void virCommandWriteArgLog(virCommandPtr cmd,
int logfd);
char *virCommandToString(virCommandPtr cmd, bool linebreaks) G_GNUC_WARN_UNUSED_RESULT;
char *virCommandToStringFull(virCommandPtr cmd,
bool linebreaks,
bool stripCommandPath);
int virCommandGetArgList(virCommandPtr cmd, char ***args, size_t *nargs);
int virCommandExec(virCommandPtr cmd, gid_t *groups, int ngroups) G_GNUC_WARN_UNUSED_RESULT;