mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-22 13:45:38 +00:00
virsh: Add new file for utility functions and move a few
Don't accumulate helpers in virsh.c
This commit is contained in:
parent
b4c2ac8d56
commit
e8a61ae4bd
@ -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)
|
||||
|
||||
|
@ -25,6 +25,7 @@
|
||||
|
||||
#include <config.h>
|
||||
#include "virsh-domain-monitor.h"
|
||||
#include "virsh-util.h"
|
||||
|
||||
#include <libxml/parser.h>
|
||||
#include <libxml/tree.h>
|
||||
|
@ -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
66
tools/virsh-util.c
Normal 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
35
tools/virsh-util.h
Normal 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 */
|
@ -25,6 +25,7 @@
|
||||
|
||||
#include <config.h>
|
||||
#include "virsh-volume.h"
|
||||
#include "virsh-util.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
|
||||
|
@ -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.
|
||||
*/
|
||||
|
@ -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 */
|
||||
|
Loading…
Reference in New Issue
Block a user