virsh: Add new file for utility functions and move a few

Don't accumulate helpers in virsh.c
This commit is contained in:
Peter Krempa 2017-04-10 17:06:15 +02:00
parent b4c2ac8d56
commit e8a61ae4bd
8 changed files with 105 additions and 45 deletions

View File

@ -226,6 +226,7 @@ virsh_SOURCES = \
virsh-pool.c virsh-pool.h \
virsh-secret.c virsh-secret.h \
virsh-snapshot.c virsh-snapshot.h \
virsh-util.c virsh-util.h \
virsh-volume.c virsh-volume.h \
$(NULL)

View File

@ -25,6 +25,7 @@
#include <config.h>
#include "virsh-domain-monitor.h"
#include "virsh-util.h"
#include <libxml/parser.h>
#include <libxml/tree.h>

View File

@ -25,6 +25,7 @@
#include <config.h>
#include "virsh-domain.h"
#include "virsh-util.h"
#include <fcntl.h>
#include <poll.h>

66
tools/virsh-util.c Normal file
View File

@ -0,0 +1,66 @@
/*
* virsh-util.c: helpers for virsh
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include "virsh-util.h"
#include "virfile.h"
int
virshDomainState(vshControl *ctl,
virDomainPtr dom,
int *reason)
{
virDomainInfo info;
virshControlPtr priv = ctl->privData;
if (reason)
*reason = -1;
if (!priv->useGetInfo) {
int state;
if (virDomainGetState(dom, &state, reason, 0) < 0) {
virErrorPtr err = virGetLastError();
if (err && err->code == VIR_ERR_NO_SUPPORT)
priv->useGetInfo = true;
else
return -1;
} else {
return state;
}
}
/* fall back to virDomainGetInfo if virDomainGetState is not supported */
if (virDomainGetInfo(dom, &info) < 0)
return -1;
else
return info.state;
}
int
virshStreamSink(virStreamPtr st ATTRIBUTE_UNUSED,
const char *bytes,
size_t nbytes,
void *opaque)
{
int *fd = opaque;
return safewrite(*fd, bytes, nbytes);
}

35
tools/virsh-util.h Normal file
View File

@ -0,0 +1,35 @@
/*
* virsh-util.h: helpers for virsh
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef VIRSH_UTIL_H
# define VIRSH_UTIL_H
# include "virsh.h"
int
virshDomainState(vshControl *ctl,
virDomainPtr dom,
int *reason);
int
virshStreamSink(virStreamPtr st,
const char *bytes,
size_t nbytes,
void *opaque);
#endif /* VIRSH_UTIL_H */

View File

@ -25,6 +25,7 @@
#include <config.h>
#include "virsh-volume.h"
#include "virsh-util.h"
#include <fcntl.h>

View File

@ -257,14 +257,6 @@ virshReconnect(vshControl *ctl, const char *name, bool readonly, bool force)
return 0;
}
int virshStreamSink(virStreamPtr st ATTRIBUTE_UNUSED,
const char *bytes, size_t nbytes, void *opaque)
{
int *fd = opaque;
return safewrite(*fd, bytes, nbytes);
}
/* ---------------
* Command Connect
* ---------------
@ -347,39 +339,6 @@ virshConnectionHandler(vshControl *ctl)
}
/* ---------------
* Misc utils
* ---------------
*/
int
virshDomainState(vshControl *ctl, virDomainPtr dom, int *reason)
{
virDomainInfo info;
virshControlPtr priv = ctl->privData;
if (reason)
*reason = -1;
if (!priv->useGetInfo) {
int state;
if (virDomainGetState(dom, &state, reason, 0) < 0) {
virErrorPtr err = virGetLastError();
if (err && err->code == VIR_ERR_NO_SUPPORT)
priv->useGetInfo = true;
else
return -1;
} else {
return state;
}
}
/* fall back to virDomainGetInfo if virDomainGetState is not supported */
if (virDomainGetInfo(dom, &info) < 0)
return -1;
else
return info.state;
}
/*
* Initialize connection.
*/

View File

@ -145,9 +145,5 @@ typedef enum {
} virshLookupByFlags;
virConnectPtr virshConnect(vshControl *ctl, const char *uri, bool readonly);
int virshDomainState(vshControl *ctl, virDomainPtr dom, int *reason);
int virshStreamSink(virStreamPtr st, const char *bytes, size_t nbytes,
void *opaque);
#endif /* VIRSH_H */