util: command: Introduce virCommandToStringBuf

The new version allows passing a virBuffer to format the string into.
This will be helpful in solving a memory lean in wrong usage of
virCommandToString and also in tests where we need to add a newline
after the command in certain cases.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2021-06-10 17:43:59 +02:00
parent 50261966fd
commit 2d018bf769
3 changed files with 37 additions and 18 deletions

View File

@ -2039,6 +2039,7 @@ virCommandSetUID;
virCommandSetUmask; virCommandSetUmask;
virCommandSetWorkingDirectory; virCommandSetWorkingDirectory;
virCommandToString; virCommandToString;
virCommandToStringBuf;
virCommandToStringFull; virCommandToStringFull;
virCommandWait; virCommandWait;
virCommandWriteArgLog; virCommandWriteArgLog;

View File

@ -2053,8 +2053,9 @@ virCommandWriteArgLog(virCommand *cmd, int logfd)
/** /**
* virCommandToStringFull: * virCommandToStringBuf:
* @cmd: the command to convert * @cmd: the command to convert
* @buf: buffer to format @cmd into
* @linebreaks: true to break line after each env var or option * @linebreaks: true to break line after each env var or option
* @stripCommandPath: strip the path leading to the binary of @cmd * @stripCommandPath: strip the path leading to the binary of @cmd
* *
@ -2062,16 +2063,15 @@ virCommandWriteArgLog(virCommand *cmd, int logfd)
* before Run/RunAsync, to return a string representation of the * before Run/RunAsync, to return a string representation of the
* environment and arguments of cmd, suitably quoted for pasting into * environment and arguments of cmd, suitably quoted for pasting into
* a shell. If virCommandRun cannot succeed (because of an * a shell. If virCommandRun cannot succeed (because of an
* out-of-memory condition while building cmd), NULL will be returned. * out-of-memory condition while building cmd), -1 will be returned.
* Caller is responsible for freeing the resulting string.
*/ */
char * int
virCommandToStringFull(virCommand *cmd, virCommandToStringBuf(virCommand *cmd,
bool linebreaks, virBuffer *buf,
bool stripCommandPath) bool linebreaks,
bool stripCommandPath)
{ {
size_t i; size_t i;
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
const char *command = cmd->args[0]; const char *command = cmd->args[0];
g_autofree char *basename = NULL; g_autofree char *basename = NULL;
bool had_option = false; bool had_option = false;
@ -2080,7 +2080,7 @@ virCommandToStringFull(virCommand *cmd,
* now. If virCommandRun is called, it will report the same error. */ * now. If virCommandRun is called, it will report the same error. */
if (virCommandHasError(cmd)) { if (virCommandHasError(cmd)) {
virCommandRaiseError(cmd); virCommandRaiseError(cmd);
return NULL; return -1;
} }
for (i = 0; i < cmd->nenv; i++) { for (i = 0; i < cmd->nenv; i++) {
@ -2091,22 +2091,22 @@ virCommandToStringFull(virCommand *cmd,
if (!eq) { if (!eq) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("invalid use of command API")); _("invalid use of command API"));
return NULL; return -1;
} }
eq++; eq++;
virBufferAdd(&buf, cmd->env[i], eq - cmd->env[i]); virBufferAdd(buf, cmd->env[i], eq - cmd->env[i]);
virBufferEscapeShell(&buf, eq); virBufferEscapeShell(buf, eq);
virBufferAddChar(&buf, ' '); virBufferAddChar(buf, ' ');
if (linebreaks) if (linebreaks)
virBufferAddLit(&buf, "\\\n"); virBufferAddLit(buf, "\\\n");
} }
if (stripCommandPath) if (stripCommandPath)
command = basename = g_path_get_basename(command); command = basename = g_path_get_basename(command);
virBufferEscapeShell(&buf, command); virBufferEscapeShell(buf, command);
for (i = 1; i < cmd->nargs; i++) { for (i = 1; i < cmd->nargs; i++) {
virBufferAddChar(&buf, ' '); virBufferAddChar(buf, ' ');
if (linebreaks) { if (linebreaks) {
/* we don't want a linebreak only if /* we don't want a linebreak only if
@ -2131,11 +2131,25 @@ virCommandToStringFull(virCommand *cmd,
} }
if (linebreak) if (linebreak)
virBufferAddLit(&buf, "\\\n"); virBufferAddLit(buf, "\\\n");
} }
virBufferEscapeShell(&buf, cmd->args[i]); virBufferEscapeShell(buf, cmd->args[i]);
} }
return 0;
}
char *
virCommandToStringFull(virCommand *cmd,
bool linebreaks,
bool stripCommandPath)
{
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
if (virCommandToStringBuf(cmd, &buf, linebreaks, stripCommandPath))
return NULL;
return virBufferContentAndReset(&buf); return virBufferContentAndReset(&buf);
} }

View File

@ -173,6 +173,10 @@ char *virCommandToString(virCommand *cmd, bool linebreaks) G_GNUC_WARN_UNUSED_RE
char *virCommandToStringFull(virCommand *cmd, char *virCommandToStringFull(virCommand *cmd,
bool linebreaks, bool linebreaks,
bool stripCommandPath); bool stripCommandPath);
int virCommandToStringBuf(virCommand *cmd,
virBuffer *buf,
bool linebreaks,
bool stripCommandPath);
int virCommandGetArgList(virCommand *cmd, char ***args, size_t *nargs); int virCommandGetArgList(virCommand *cmd, char ***args, size_t *nargs);