Refactor remote_command() function

Use dynamic rather than fixed buffer to generate the command string.
This commit is contained in:
Ian Barwick
2019-09-17 15:54:01 +09:00
parent 50d4cee877
commit 8e6d111f32

View File

@@ -1,6 +1,8 @@
/* /*
* sysutils.c * sysutils.c
* *
* Functions which need to be executed on the local system.
*
* Copyright (c) 2ndQuadrant, 2010-2019 * Copyright (c) 2ndQuadrant, 2010-2019
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
@@ -115,7 +117,7 @@ bool
remote_command(const char *host, const char *user, const char *command, const char *ssh_options, PQExpBufferData *outputbuf) remote_command(const char *host, const char *user, const char *command, const char *ssh_options, PQExpBufferData *outputbuf)
{ {
FILE *fp; FILE *fp;
char ssh_command[MAXLEN] = ""; PQExpBufferData ssh_command;
PQExpBufferData ssh_host; PQExpBufferData ssh_host;
char output[MAXLEN] = ""; char output[MAXLEN] = "";
@@ -129,7 +131,9 @@ remote_command(const char *host, const char *user, const char *command, const ch
appendPQExpBufferStr(&ssh_host, host); appendPQExpBufferStr(&ssh_host, host);
maxlen_snprintf(ssh_command, initPQExpBuffer(&ssh_command);
appendPQExpBuffer(&ssh_command,
"ssh -o Batchmode=yes %s %s %s", "ssh -o Batchmode=yes %s %s %s",
ssh_options, ssh_options,
ssh_host.data, ssh_host.data,
@@ -137,16 +141,19 @@ remote_command(const char *host, const char *user, const char *command, const ch
termPQExpBuffer(&ssh_host); termPQExpBuffer(&ssh_host);
log_debug("remote_command():\n %s", ssh_command); log_debug("remote_command():\n %s", ssh_command.data);
fp = popen(ssh_command, "r"); fp = popen(ssh_command.data, "r");
if (fp == NULL) if (fp == NULL)
{ {
log_error(_("unable to execute remote command:\n %s"), ssh_command); log_error(_("unable to execute remote command:\n %s"), ssh_command.data);
termPQExpBuffer(&ssh_command);
return false; return false;
} }
termPQExpBuffer(&ssh_command);
if (outputbuf != NULL) if (outputbuf != NULL)
{ {
/* TODO: better error handling */ /* TODO: better error handling */