2005-12-13 16:22:05 +00:00
|
|
|
/*
|
|
|
|
* xml.c: XML based interfaces for the libvir library
|
|
|
|
*
|
2012-01-27 18:07:21 +00:00
|
|
|
* Copyright (C) 2005, 2007-2012 Red Hat, Inc.
|
2005-12-13 16:22:05 +00:00
|
|
|
*
|
2012-07-27 09:39:53 +00:00
|
|
|
* 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
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-27 09:39:53 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2005-12-13 16:22:05 +00:00
|
|
|
*
|
|
|
|
* Daniel Veillard <veillard@redhat.com>
|
|
|
|
*/
|
|
|
|
|
2008-01-29 18:15:54 +00:00
|
|
|
#include <config.h>
|
2007-11-26 11:40:28 +00:00
|
|
|
|
2005-12-13 16:22:05 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
2007-11-26 11:40:28 +00:00
|
|
|
#include <limits.h>
|
2007-10-24 14:22:25 +00:00
|
|
|
#include <math.h> /* for isnan() */
|
2011-10-06 09:57:06 +00:00
|
|
|
#include <sys/stat.h>
|
2008-11-04 22:30:33 +00:00
|
|
|
|
|
|
|
#include "virterror_internal.h"
|
2006-02-16 22:50:52 +00:00
|
|
|
#include "xml.h"
|
2007-06-26 22:33:22 +00:00
|
|
|
#include "buf.h"
|
2008-02-27 04:35:08 +00:00
|
|
|
#include "util.h"
|
2008-05-29 19:20:22 +00:00
|
|
|
#include "memory.h"
|
2011-10-06 09:57:06 +00:00
|
|
|
#include "virfile.h"
|
2008-07-25 13:17:27 +00:00
|
|
|
|
2009-01-29 12:10:32 +00:00
|
|
|
#define VIR_FROM_THIS VIR_FROM_XML
|
|
|
|
|
2010-02-24 20:44:47 +00:00
|
|
|
#define virGenericReportError(from, code, ...) \
|
2011-04-16 08:30:22 +00:00
|
|
|
virReportErrorHelper(from, code, __FILE__, \
|
2010-03-01 23:38:28 +00:00
|
|
|
__FUNCTION__, __LINE__, __VA_ARGS__)
|
2006-02-27 22:32:54 +00:00
|
|
|
|
2010-02-24 20:44:47 +00:00
|
|
|
/* Internal data to be passed to SAX parser and used by error handler. */
|
|
|
|
struct virParserData {
|
|
|
|
int domcode;
|
|
|
|
};
|
|
|
|
|
2007-10-22 20:28:55 +00:00
|
|
|
|
|
|
|
/************************************************************************
|
|
|
|
* *
|
|
|
|
* Wrappers around libxml2 XPath specific functions *
|
|
|
|
* *
|
|
|
|
************************************************************************/
|
|
|
|
|
2007-04-06 12:28:24 +00:00
|
|
|
/**
|
|
|
|
* virXPathString:
|
|
|
|
* @xpath: the XPath string to evaluate
|
|
|
|
* @ctxt: an XPath context
|
|
|
|
*
|
|
|
|
* Convenience function to evaluate an XPath string
|
|
|
|
*
|
|
|
|
* Returns a new string which must be deallocated by the caller or NULL
|
|
|
|
* if the evaluation failed.
|
|
|
|
*/
|
|
|
|
char *
|
2010-02-04 21:52:34 +00:00
|
|
|
virXPathString(const char *xpath,
|
2008-07-25 14:27:25 +00:00
|
|
|
xmlXPathContextPtr ctxt)
|
2007-10-24 14:22:25 +00:00
|
|
|
{
|
2007-04-06 12:28:24 +00:00
|
|
|
xmlXPathObjectPtr obj;
|
2008-07-09 08:35:09 +00:00
|
|
|
xmlNodePtr relnode;
|
2007-04-06 12:28:24 +00:00
|
|
|
char *ret;
|
|
|
|
|
|
|
|
if ((ctxt == NULL) || (xpath == NULL)) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("Invalid parameter to virXPathString()"));
|
2012-03-22 11:33:35 +00:00
|
|
|
return NULL;
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
2008-07-09 08:35:09 +00:00
|
|
|
relnode = ctxt->node;
|
2007-04-06 12:28:24 +00:00
|
|
|
obj = xmlXPathEval(BAD_CAST xpath, ctxt);
|
2009-09-29 09:07:01 +00:00
|
|
|
ctxt->node = relnode;
|
2007-04-06 12:28:24 +00:00
|
|
|
if ((obj == NULL) || (obj->type != XPATH_STRING) ||
|
2007-07-31 14:27:12 +00:00
|
|
|
(obj->stringval == NULL) || (obj->stringval[0] == 0)) {
|
2008-02-07 16:49:29 +00:00
|
|
|
xmlXPathFreeObject(obj);
|
2012-03-22 11:33:35 +00:00
|
|
|
return NULL;
|
2007-07-31 14:27:12 +00:00
|
|
|
}
|
2007-04-06 12:28:24 +00:00
|
|
|
ret = strdup((char *) obj->stringval);
|
|
|
|
xmlXPathFreeObject(obj);
|
|
|
|
if (ret == NULL) {
|
2010-02-04 18:19:08 +00:00
|
|
|
virReportOOMError();
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
2012-03-22 11:33:35 +00:00
|
|
|
return ret;
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
|
|
|
|
2009-03-03 09:44:41 +00:00
|
|
|
/**
|
|
|
|
* virXPathStringLimit:
|
|
|
|
* @xpath: the XPath string to evaluate
|
|
|
|
* @maxlen: maximum length permittred string
|
|
|
|
* @ctxt: an XPath context
|
|
|
|
*
|
|
|
|
* Wrapper for virXPathString, which validates the length of the returned
|
|
|
|
* string.
|
|
|
|
*
|
|
|
|
* Returns a new string which must be deallocated by the caller or NULL if
|
|
|
|
* the evaluation failed.
|
|
|
|
*/
|
|
|
|
char *
|
2010-02-04 21:52:34 +00:00
|
|
|
virXPathStringLimit(const char *xpath,
|
2009-03-03 09:44:41 +00:00
|
|
|
size_t maxlen,
|
|
|
|
xmlXPathContextPtr ctxt)
|
|
|
|
{
|
2010-02-04 21:52:34 +00:00
|
|
|
char *tmp = virXPathString(xpath, ctxt);
|
2009-03-03 09:44:41 +00:00
|
|
|
|
|
|
|
if (tmp != NULL && strlen(tmp) >= maxlen) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("\'%s\' value longer than %zu bytes"),
|
|
|
|
xpath, maxlen);
|
2011-03-04 16:52:12 +00:00
|
|
|
VIR_FREE(tmp);
|
|
|
|
return NULL;
|
2009-03-03 09:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2007-04-06 12:28:24 +00:00
|
|
|
/**
|
|
|
|
* virXPathNumber:
|
|
|
|
* @xpath: the XPath string to evaluate
|
|
|
|
* @ctxt: an XPath context
|
|
|
|
* @value: the returned double value
|
|
|
|
*
|
|
|
|
* Convenience function to evaluate an XPath number
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success in which case @value is set,
|
|
|
|
* or -1 if the evaluation failed.
|
|
|
|
*/
|
|
|
|
int
|
2010-02-04 21:52:34 +00:00
|
|
|
virXPathNumber(const char *xpath,
|
2008-07-25 14:27:25 +00:00
|
|
|
xmlXPathContextPtr ctxt,
|
|
|
|
double *value)
|
2007-10-24 14:22:25 +00:00
|
|
|
{
|
2007-04-06 12:28:24 +00:00
|
|
|
xmlXPathObjectPtr obj;
|
2008-07-09 08:35:09 +00:00
|
|
|
xmlNodePtr relnode;
|
2007-04-06 12:28:24 +00:00
|
|
|
|
|
|
|
if ((ctxt == NULL) || (xpath == NULL) || (value == NULL)) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("Invalid parameter to virXPathNumber()"));
|
2012-03-22 11:33:35 +00:00
|
|
|
return -1;
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
2008-07-09 08:35:09 +00:00
|
|
|
relnode = ctxt->node;
|
2007-04-06 12:28:24 +00:00
|
|
|
obj = xmlXPathEval(BAD_CAST xpath, ctxt);
|
2009-09-29 09:07:01 +00:00
|
|
|
ctxt->node = relnode;
|
2007-04-06 12:28:24 +00:00
|
|
|
if ((obj == NULL) || (obj->type != XPATH_NUMBER) ||
|
|
|
|
(isnan(obj->floatval))) {
|
2007-10-24 14:22:25 +00:00
|
|
|
xmlXPathFreeObject(obj);
|
2012-03-22 11:33:35 +00:00
|
|
|
return -1;
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
2007-10-24 14:22:25 +00:00
|
|
|
|
2007-04-06 12:28:24 +00:00
|
|
|
*value = obj->floatval;
|
|
|
|
xmlXPathFreeObject(obj);
|
2012-03-22 11:33:35 +00:00
|
|
|
return 0;
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
|
|
|
|
2009-02-24 14:53:30 +00:00
|
|
|
static int
|
2010-02-04 21:52:34 +00:00
|
|
|
virXPathLongBase(const char *xpath,
|
2009-02-24 14:53:30 +00:00
|
|
|
xmlXPathContextPtr ctxt,
|
|
|
|
int base,
|
|
|
|
long *value)
|
2007-10-24 14:22:25 +00:00
|
|
|
{
|
2007-04-06 12:28:24 +00:00
|
|
|
xmlXPathObjectPtr obj;
|
2008-07-09 08:35:09 +00:00
|
|
|
xmlNodePtr relnode;
|
2007-04-06 12:28:24 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if ((ctxt == NULL) || (xpath == NULL) || (value == NULL)) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("Invalid parameter to virXPathLong()"));
|
2012-03-22 11:33:35 +00:00
|
|
|
return -1;
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
2008-07-09 08:35:09 +00:00
|
|
|
relnode = ctxt->node;
|
2007-04-06 12:28:24 +00:00
|
|
|
obj = xmlXPathEval(BAD_CAST xpath, ctxt);
|
2009-09-29 09:07:01 +00:00
|
|
|
ctxt->node = relnode;
|
2007-04-06 12:28:24 +00:00
|
|
|
if ((obj != NULL) && (obj->type == XPATH_STRING) &&
|
|
|
|
(obj->stringval != NULL) && (obj->stringval[0] != 0)) {
|
2012-04-18 23:58:44 +00:00
|
|
|
if (virStrToLong_l((char *) obj->stringval, NULL, base, value) < 0)
|
2007-04-06 12:28:24 +00:00
|
|
|
ret = -2;
|
|
|
|
} else if ((obj != NULL) && (obj->type == XPATH_NUMBER) &&
|
|
|
|
(!(isnan(obj->floatval)))) {
|
2007-10-24 14:22:25 +00:00
|
|
|
*value = (long) obj->floatval;
|
|
|
|
if (*value != obj->floatval) {
|
|
|
|
ret = -2;
|
|
|
|
}
|
2007-04-06 12:28:24 +00:00
|
|
|
} else {
|
2007-10-24 14:22:25 +00:00
|
|
|
ret = -1;
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
2007-10-24 14:22:25 +00:00
|
|
|
|
2007-04-06 12:28:24 +00:00
|
|
|
xmlXPathFreeObject(obj);
|
2012-03-22 11:33:35 +00:00
|
|
|
return ret;
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
|
|
|
|
2011-01-13 22:09:18 +00:00
|
|
|
/**
|
|
|
|
* virXPathInt:
|
|
|
|
* @xpath: the XPath string to evaluate
|
|
|
|
* @ctxt: an XPath context
|
|
|
|
* @value: the returned int value
|
|
|
|
*
|
|
|
|
* Convenience function to evaluate an XPath number
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success in which case @value is set,
|
|
|
|
* or -1 if the XPath evaluation failed or -2 if the
|
|
|
|
* value doesn't have an int format.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virXPathInt(const char *xpath,
|
|
|
|
xmlXPathContextPtr ctxt,
|
|
|
|
int *value)
|
|
|
|
{
|
|
|
|
long tmp;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = virXPathLongBase(xpath, ctxt, 10, &tmp);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
if ((int) tmp != tmp)
|
|
|
|
return -2;
|
|
|
|
*value = tmp;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-07-11 10:48:34 +00:00
|
|
|
/**
|
2009-02-24 14:53:30 +00:00
|
|
|
* virXPathLong:
|
2008-07-11 10:48:34 +00:00
|
|
|
* @xpath: the XPath string to evaluate
|
|
|
|
* @ctxt: an XPath context
|
|
|
|
* @value: the returned long value
|
|
|
|
*
|
|
|
|
* Convenience function to evaluate an XPath number
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success in which case @value is set,
|
|
|
|
* or -1 if the XPath evaluation failed or -2 if the
|
|
|
|
* value doesn't have a long format.
|
|
|
|
*/
|
|
|
|
int
|
2010-02-04 21:52:34 +00:00
|
|
|
virXPathLong(const char *xpath,
|
2009-02-24 14:53:30 +00:00
|
|
|
xmlXPathContextPtr ctxt,
|
|
|
|
long *value)
|
|
|
|
{
|
2010-02-04 21:52:34 +00:00
|
|
|
return virXPathLongBase(xpath, ctxt, 10, value);
|
2009-02-24 14:53:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virXPathLongHex:
|
|
|
|
* @xpath: the XPath string to evaluate
|
|
|
|
* @ctxt: an XPath context
|
|
|
|
* @value: the returned long value
|
|
|
|
*
|
|
|
|
* Convenience function to evaluate an XPath number
|
|
|
|
* according to a base of 16
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success in which case @value is set,
|
|
|
|
* or -1 if the XPath evaluation failed or -2 if the
|
|
|
|
* value doesn't have a long format.
|
|
|
|
*/
|
|
|
|
int
|
2010-02-04 21:52:34 +00:00
|
|
|
virXPathLongHex(const char *xpath,
|
2009-02-24 14:53:30 +00:00
|
|
|
xmlXPathContextPtr ctxt,
|
|
|
|
long *value)
|
|
|
|
{
|
2010-02-04 21:52:34 +00:00
|
|
|
return virXPathLongBase(xpath, ctxt, 16, value);
|
2009-02-24 14:53:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-02-04 21:52:34 +00:00
|
|
|
virXPathULongBase(const char *xpath,
|
2009-02-24 14:53:30 +00:00
|
|
|
xmlXPathContextPtr ctxt,
|
|
|
|
int base,
|
|
|
|
unsigned long *value)
|
2008-07-11 10:48:34 +00:00
|
|
|
{
|
|
|
|
xmlXPathObjectPtr obj;
|
|
|
|
xmlNodePtr relnode;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if ((ctxt == NULL) || (xpath == NULL) || (value == NULL)) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("Invalid parameter to virXPathULong()"));
|
2012-03-22 11:33:35 +00:00
|
|
|
return -1;
|
2008-07-11 10:48:34 +00:00
|
|
|
}
|
|
|
|
relnode = ctxt->node;
|
|
|
|
obj = xmlXPathEval(BAD_CAST xpath, ctxt);
|
2009-09-29 09:07:01 +00:00
|
|
|
ctxt->node = relnode;
|
2008-07-11 10:48:34 +00:00
|
|
|
if ((obj != NULL) && (obj->type == XPATH_STRING) &&
|
|
|
|
(obj->stringval != NULL) && (obj->stringval[0] != 0)) {
|
2012-04-18 23:58:44 +00:00
|
|
|
if (virStrToLong_ul((char *) obj->stringval, NULL, base, value) < 0)
|
2008-07-11 10:48:34 +00:00
|
|
|
ret = -2;
|
|
|
|
} else if ((obj != NULL) && (obj->type == XPATH_NUMBER) &&
|
|
|
|
(!(isnan(obj->floatval)))) {
|
|
|
|
*value = (unsigned long) obj->floatval;
|
|
|
|
if (*value != obj->floatval) {
|
|
|
|
ret = -2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlXPathFreeObject(obj);
|
2012-03-22 11:33:35 +00:00
|
|
|
return ret;
|
2008-07-11 10:48:34 +00:00
|
|
|
}
|
|
|
|
|
2011-01-13 22:09:18 +00:00
|
|
|
/**
|
|
|
|
* virXPathUInt:
|
|
|
|
* @xpath: the XPath string to evaluate
|
|
|
|
* @ctxt: an XPath context
|
|
|
|
* @value: the returned int value
|
|
|
|
*
|
|
|
|
* Convenience function to evaluate an XPath number
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success in which case @value is set,
|
|
|
|
* or -1 if the XPath evaluation failed or -2 if the
|
|
|
|
* value doesn't have an int format.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virXPathUInt(const char *xpath,
|
|
|
|
xmlXPathContextPtr ctxt,
|
|
|
|
unsigned int *value)
|
|
|
|
{
|
|
|
|
unsigned long tmp;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = virXPathULongBase(xpath, ctxt, 10, &tmp);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
if ((unsigned int) tmp != tmp)
|
|
|
|
return -2;
|
|
|
|
*value = tmp;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-24 14:53:30 +00:00
|
|
|
/**
|
|
|
|
* virXPathULong:
|
|
|
|
* @xpath: the XPath string to evaluate
|
|
|
|
* @ctxt: an XPath context
|
|
|
|
* @value: the returned long value
|
|
|
|
*
|
|
|
|
* Convenience function to evaluate an XPath number
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success in which case @value is set,
|
|
|
|
* or -1 if the XPath evaluation failed or -2 if the
|
|
|
|
* value doesn't have a long format.
|
|
|
|
*/
|
|
|
|
int
|
2010-02-04 21:52:34 +00:00
|
|
|
virXPathULong(const char *xpath,
|
2009-02-24 14:53:30 +00:00
|
|
|
xmlXPathContextPtr ctxt,
|
|
|
|
unsigned long *value)
|
|
|
|
{
|
2010-02-04 21:52:34 +00:00
|
|
|
return virXPathULongBase(xpath, ctxt, 10, value);
|
2009-02-24 14:53:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virXPathUHex:
|
|
|
|
* @xpath: the XPath string to evaluate
|
|
|
|
* @ctxt: an XPath context
|
|
|
|
* @value: the returned long value
|
|
|
|
*
|
|
|
|
* Convenience function to evaluate an XPath number
|
|
|
|
* according to base of 16
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success in which case @value is set,
|
|
|
|
* or -1 if the XPath evaluation failed or -2 if the
|
|
|
|
* value doesn't have a long format.
|
|
|
|
*/
|
|
|
|
int
|
2010-02-04 21:52:34 +00:00
|
|
|
virXPathULongHex(const char *xpath,
|
2009-02-24 14:53:30 +00:00
|
|
|
xmlXPathContextPtr ctxt,
|
|
|
|
unsigned long *value)
|
|
|
|
{
|
2010-02-04 21:52:34 +00:00
|
|
|
return virXPathULongBase(xpath, ctxt, 16, value);
|
2009-02-24 14:53:30 +00:00
|
|
|
}
|
|
|
|
|
2009-02-24 14:54:30 +00:00
|
|
|
/**
|
|
|
|
* virXPathULongLong:
|
|
|
|
* @xpath: the XPath string to evaluate
|
|
|
|
* @ctxt: an XPath context
|
|
|
|
* @value: the returned long long value
|
|
|
|
*
|
|
|
|
* Convenience function to evaluate an XPath number
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success in which case @value is set,
|
|
|
|
* or -1 if the XPath evaluation failed or -2 if the
|
|
|
|
* value doesn't have a long format.
|
|
|
|
*/
|
|
|
|
int
|
2010-02-04 21:52:34 +00:00
|
|
|
virXPathULongLong(const char *xpath,
|
2009-02-24 14:54:30 +00:00
|
|
|
xmlXPathContextPtr ctxt,
|
|
|
|
unsigned long long *value)
|
|
|
|
{
|
|
|
|
xmlXPathObjectPtr obj;
|
|
|
|
xmlNodePtr relnode;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if ((ctxt == NULL) || (xpath == NULL) || (value == NULL)) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("Invalid parameter to virXPathULong()"));
|
2012-03-22 11:33:35 +00:00
|
|
|
return -1;
|
2009-02-24 14:54:30 +00:00
|
|
|
}
|
|
|
|
relnode = ctxt->node;
|
|
|
|
obj = xmlXPathEval(BAD_CAST xpath, ctxt);
|
2009-09-29 09:07:01 +00:00
|
|
|
ctxt->node = relnode;
|
2009-02-24 14:54:30 +00:00
|
|
|
if ((obj != NULL) && (obj->type == XPATH_STRING) &&
|
|
|
|
(obj->stringval != NULL) && (obj->stringval[0] != 0)) {
|
2012-04-18 23:58:44 +00:00
|
|
|
if (virStrToLong_ull((char *) obj->stringval, NULL, 10, value) < 0)
|
2009-02-24 14:54:30 +00:00
|
|
|
ret = -2;
|
|
|
|
} else if ((obj != NULL) && (obj->type == XPATH_NUMBER) &&
|
|
|
|
(!(isnan(obj->floatval)))) {
|
|
|
|
*value = (unsigned long long) obj->floatval;
|
|
|
|
if (*value != obj->floatval) {
|
|
|
|
ret = -2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlXPathFreeObject(obj);
|
2012-03-22 11:33:35 +00:00
|
|
|
return ret;
|
2009-02-24 14:54:30 +00:00
|
|
|
}
|
|
|
|
|
2010-02-02 17:49:09 +00:00
|
|
|
/**
|
|
|
|
* virXPathULongLong:
|
|
|
|
* @xpath: the XPath string to evaluate
|
|
|
|
* @ctxt: an XPath context
|
|
|
|
* @value: the returned long long value
|
|
|
|
*
|
|
|
|
* Convenience function to evaluate an XPath number
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success in which case @value is set,
|
|
|
|
* or -1 if the XPath evaluation failed or -2 if the
|
|
|
|
* value doesn't have a long format.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virXPathLongLong(const char *xpath,
|
|
|
|
xmlXPathContextPtr ctxt,
|
|
|
|
long long *value)
|
|
|
|
{
|
|
|
|
xmlXPathObjectPtr obj;
|
|
|
|
xmlNodePtr relnode;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if ((ctxt == NULL) || (xpath == NULL) || (value == NULL)) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("Invalid parameter to virXPathLongLong()"));
|
2012-03-22 11:33:35 +00:00
|
|
|
return -1;
|
2010-02-02 17:49:09 +00:00
|
|
|
}
|
|
|
|
relnode = ctxt->node;
|
|
|
|
obj = xmlXPathEval(BAD_CAST xpath, ctxt);
|
|
|
|
ctxt->node = relnode;
|
|
|
|
if ((obj != NULL) && (obj->type == XPATH_STRING) &&
|
|
|
|
(obj->stringval != NULL) && (obj->stringval[0] != 0)) {
|
2012-04-18 23:58:44 +00:00
|
|
|
if (virStrToLong_ll((char *) obj->stringval, NULL, 10, value) < 0)
|
2010-02-02 17:49:09 +00:00
|
|
|
ret = -2;
|
|
|
|
} else if ((obj != NULL) && (obj->type == XPATH_NUMBER) &&
|
|
|
|
(!(isnan(obj->floatval)))) {
|
|
|
|
*value = (long long) obj->floatval;
|
|
|
|
if (*value != obj->floatval) {
|
|
|
|
ret = -2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlXPathFreeObject(obj);
|
2012-03-22 11:33:35 +00:00
|
|
|
return ret;
|
2010-02-02 17:49:09 +00:00
|
|
|
}
|
|
|
|
|
2008-07-11 16:23:36 +00:00
|
|
|
char *
|
|
|
|
virXMLPropString(xmlNodePtr node,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
return (char *)xmlGetProp(node, BAD_CAST name);
|
|
|
|
}
|
|
|
|
|
2007-04-06 12:28:24 +00:00
|
|
|
/**
|
|
|
|
* virXPathBoolean:
|
|
|
|
* @xpath: the XPath string to evaluate
|
|
|
|
* @ctxt: an XPath context
|
|
|
|
*
|
|
|
|
* Convenience function to evaluate an XPath boolean
|
|
|
|
*
|
|
|
|
* Returns 0 if false, 1 if true, or -1 if the evaluation failed.
|
|
|
|
*/
|
|
|
|
int
|
2010-02-04 21:52:34 +00:00
|
|
|
virXPathBoolean(const char *xpath,
|
2008-07-25 14:27:25 +00:00
|
|
|
xmlXPathContextPtr ctxt)
|
2007-10-24 14:22:25 +00:00
|
|
|
{
|
2007-04-06 12:28:24 +00:00
|
|
|
xmlXPathObjectPtr obj;
|
2008-07-09 08:35:09 +00:00
|
|
|
xmlNodePtr relnode;
|
2007-04-06 12:28:24 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ctxt == NULL) || (xpath == NULL)) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("Invalid parameter to virXPathBoolean()"));
|
2012-03-22 11:33:35 +00:00
|
|
|
return -1;
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
2008-07-09 08:35:09 +00:00
|
|
|
relnode = ctxt->node;
|
2007-04-06 12:28:24 +00:00
|
|
|
obj = xmlXPathEval(BAD_CAST xpath, ctxt);
|
2009-09-29 09:07:01 +00:00
|
|
|
ctxt->node = relnode;
|
2007-04-06 12:28:24 +00:00
|
|
|
if ((obj == NULL) || (obj->type != XPATH_BOOLEAN) ||
|
|
|
|
(obj->boolval < 0) || (obj->boolval > 1)) {
|
2007-10-24 14:22:25 +00:00
|
|
|
xmlXPathFreeObject(obj);
|
2012-03-22 11:33:35 +00:00
|
|
|
return -1;
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
|
|
|
ret = obj->boolval;
|
2007-10-24 14:22:25 +00:00
|
|
|
|
2007-04-06 12:28:24 +00:00
|
|
|
xmlXPathFreeObject(obj);
|
2012-03-22 11:33:35 +00:00
|
|
|
return ret;
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virXPathNode:
|
|
|
|
* @xpath: the XPath string to evaluate
|
|
|
|
* @ctxt: an XPath context
|
|
|
|
*
|
|
|
|
* Convenience function to evaluate an XPath node set and returning
|
|
|
|
* only one node, the first one in the set if any
|
|
|
|
*
|
|
|
|
* Returns a pointer to the node or NULL if the evaluation failed.
|
|
|
|
*/
|
|
|
|
xmlNodePtr
|
2010-02-04 21:52:34 +00:00
|
|
|
virXPathNode(const char *xpath,
|
2008-07-25 14:27:25 +00:00
|
|
|
xmlXPathContextPtr ctxt)
|
2007-10-24 14:22:25 +00:00
|
|
|
{
|
2007-04-06 12:28:24 +00:00
|
|
|
xmlXPathObjectPtr obj;
|
2008-07-09 08:35:09 +00:00
|
|
|
xmlNodePtr relnode;
|
2007-04-06 12:28:24 +00:00
|
|
|
xmlNodePtr ret;
|
|
|
|
|
|
|
|
if ((ctxt == NULL) || (xpath == NULL)) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("Invalid parameter to virXPathNode()"));
|
2012-03-22 11:33:35 +00:00
|
|
|
return NULL;
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
2008-07-09 08:35:09 +00:00
|
|
|
relnode = ctxt->node;
|
2007-04-06 12:28:24 +00:00
|
|
|
obj = xmlXPathEval(BAD_CAST xpath, ctxt);
|
2009-09-29 09:07:01 +00:00
|
|
|
ctxt->node = relnode;
|
2007-04-06 12:28:24 +00:00
|
|
|
if ((obj == NULL) || (obj->type != XPATH_NODESET) ||
|
|
|
|
(obj->nodesetval == NULL) || (obj->nodesetval->nodeNr <= 0) ||
|
2007-10-24 14:22:25 +00:00
|
|
|
(obj->nodesetval->nodeTab == NULL)) {
|
|
|
|
xmlXPathFreeObject(obj);
|
2012-03-22 11:33:35 +00:00
|
|
|
return NULL;
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
2007-10-24 14:22:25 +00:00
|
|
|
|
2007-04-06 12:28:24 +00:00
|
|
|
ret = obj->nodesetval->nodeTab[0];
|
|
|
|
xmlXPathFreeObject(obj);
|
2012-03-22 11:33:35 +00:00
|
|
|
return ret;
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
2007-10-24 14:22:25 +00:00
|
|
|
|
2007-04-06 12:28:24 +00:00
|
|
|
/**
|
|
|
|
* virXPathNodeSet:
|
|
|
|
* @xpath: the XPath string to evaluate
|
|
|
|
* @ctxt: an XPath context
|
|
|
|
* @list: the returned list of nodes (or NULL if only count matters)
|
|
|
|
*
|
|
|
|
* Convenience function to evaluate an XPath node set
|
|
|
|
*
|
|
|
|
* Returns the number of nodes found in which case @list is set (and
|
|
|
|
* must be freed) or -1 if the evaluation failed.
|
|
|
|
*/
|
|
|
|
int
|
2010-02-04 21:52:34 +00:00
|
|
|
virXPathNodeSet(const char *xpath,
|
2008-07-25 14:27:25 +00:00
|
|
|
xmlXPathContextPtr ctxt,
|
|
|
|
xmlNodePtr **list)
|
2007-10-24 14:22:25 +00:00
|
|
|
{
|
2007-04-06 12:28:24 +00:00
|
|
|
xmlXPathObjectPtr obj;
|
2008-07-09 08:35:09 +00:00
|
|
|
xmlNodePtr relnode;
|
2007-04-06 12:28:24 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ctxt == NULL) || (xpath == NULL)) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("Invalid parameter to virXPathNodeSet()"));
|
2012-03-22 11:33:35 +00:00
|
|
|
return -1;
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
2008-07-11 16:23:36 +00:00
|
|
|
|
|
|
|
if (list != NULL)
|
|
|
|
*list = NULL;
|
|
|
|
|
2008-07-09 08:35:09 +00:00
|
|
|
relnode = ctxt->node;
|
2007-04-06 12:28:24 +00:00
|
|
|
obj = xmlXPathEval(BAD_CAST xpath, ctxt);
|
2009-09-29 09:07:01 +00:00
|
|
|
ctxt->node = relnode;
|
2009-10-22 08:32:15 +00:00
|
|
|
if (obj == NULL)
|
2012-03-22 11:33:35 +00:00
|
|
|
return 0;
|
2011-05-12 19:45:22 +00:00
|
|
|
|
2009-10-22 08:32:15 +00:00
|
|
|
if (obj->type != XPATH_NODESET) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Incorrect xpath '%s'"), xpath);
|
2007-10-24 14:22:25 +00:00
|
|
|
xmlXPathFreeObject(obj);
|
2012-03-22 11:33:35 +00:00
|
|
|
return -1;
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
2011-05-12 19:45:22 +00:00
|
|
|
|
2009-10-22 08:32:15 +00:00
|
|
|
if ((obj->nodesetval == NULL) || (obj->nodesetval->nodeNr < 0)) {
|
|
|
|
xmlXPathFreeObject(obj);
|
2012-03-22 11:33:35 +00:00
|
|
|
return 0;
|
2009-10-22 08:32:15 +00:00
|
|
|
}
|
2007-10-24 14:22:25 +00:00
|
|
|
|
2007-04-06 12:28:24 +00:00
|
|
|
ret = obj->nodesetval->nodeNr;
|
2008-07-11 16:23:36 +00:00
|
|
|
if (list != NULL && ret) {
|
2008-05-29 19:20:22 +00:00
|
|
|
if (VIR_ALLOC_N(*list, ret) < 0) {
|
2010-02-04 18:19:08 +00:00
|
|
|
virReportOOMError();
|
2008-05-29 19:20:22 +00:00
|
|
|
ret = -1;
|
2007-10-24 14:22:25 +00:00
|
|
|
} else {
|
|
|
|
memcpy(*list, obj->nodesetval->nodeTab,
|
|
|
|
ret * sizeof(xmlNodePtr));
|
|
|
|
}
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
|
|
|
xmlXPathFreeObject(obj);
|
2012-03-22 11:33:35 +00:00
|
|
|
return ret;
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
2010-02-24 20:44:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* catchXMLError:
|
|
|
|
*
|
|
|
|
* Called from SAX on parsing errors in the XML.
|
2011-09-06 07:48:22 +00:00
|
|
|
*
|
|
|
|
* This version is heavily based on xmlParserPrintFileContextInternal from libxml2.
|
2010-02-24 20:44:47 +00:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
catchXMLError(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...)
|
|
|
|
{
|
|
|
|
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
|
|
|
|
|
2011-09-06 07:48:22 +00:00
|
|
|
const xmlChar *cur, *base;
|
|
|
|
unsigned int n, col; /* GCC warns if signed, because compared with sizeof() */
|
|
|
|
int domcode = VIR_FROM_XML;
|
|
|
|
|
|
|
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
|
|
|
char *contextstr = NULL;
|
|
|
|
char *pointerstr = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
/* conditions for error printing */
|
|
|
|
if (!ctxt ||
|
|
|
|
(virGetLastError() != NULL) ||
|
|
|
|
ctxt->input == NULL ||
|
|
|
|
ctxt->lastError.level != XML_ERR_FATAL ||
|
|
|
|
ctxt->lastError.message == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (ctxt->_private)
|
2010-02-24 20:44:47 +00:00
|
|
|
domcode = ((struct virParserData *) ctxt->_private)->domcode;
|
|
|
|
|
2011-09-06 07:48:22 +00:00
|
|
|
|
|
|
|
cur = ctxt->input->cur;
|
|
|
|
base = ctxt->input->base;
|
|
|
|
|
|
|
|
/* skip backwards over any end-of-lines */
|
|
|
|
while ((cur > base) && ((*(cur) == '\n') || (*(cur) == '\r'))) {
|
|
|
|
cur--;
|
2010-02-24 20:44:47 +00:00
|
|
|
}
|
|
|
|
|
2011-09-06 07:48:22 +00:00
|
|
|
/* search backwards for beginning-of-line (to max buff size) */
|
|
|
|
while ((cur > base) && (*(cur) != '\n') && (*(cur) != '\r'))
|
|
|
|
cur--;
|
|
|
|
if ((*(cur) == '\n') || (*(cur) == '\r')) cur++;
|
|
|
|
|
|
|
|
/* calculate the error position in terms of the current position */
|
|
|
|
col = ctxt->input->cur - cur;
|
|
|
|
|
|
|
|
/* search forward for end-of-line (to max buff size) */
|
|
|
|
/* copy selected text to our buffer */
|
|
|
|
while ((*cur != 0) && (*(cur) != '\n') && (*(cur) != '\r')) {
|
|
|
|
virBufferAddChar(&buf, *cur++);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create blank line with problem pointer */
|
|
|
|
contextstr = virBufferContentAndReset(&buf);
|
|
|
|
|
|
|
|
/* (leave buffer space for pointer + line terminator) */
|
|
|
|
for (n = 0; (n<col) && (contextstr[n] != 0); n++) {
|
|
|
|
if (contextstr[n] == '\t')
|
|
|
|
virBufferAddChar(&buf, '\t');
|
|
|
|
else
|
|
|
|
virBufferAddChar(&buf, '-');
|
|
|
|
}
|
|
|
|
|
|
|
|
virBufferAddChar(&buf, '^');
|
|
|
|
|
|
|
|
pointerstr = virBufferContentAndReset(&buf);
|
|
|
|
|
|
|
|
if (ctxt->lastError.file) {
|
|
|
|
virGenericReportError(domcode, VIR_ERR_XML_DETAIL,
|
|
|
|
_("%s:%d: %s%s\n%s"),
|
|
|
|
ctxt->lastError.file,
|
|
|
|
ctxt->lastError.line,
|
|
|
|
ctxt->lastError.message,
|
|
|
|
contextstr,
|
|
|
|
pointerstr);
|
|
|
|
} else {
|
|
|
|
virGenericReportError(domcode, VIR_ERR_XML_DETAIL,
|
|
|
|
_("at line %d: %s%s\n%s"),
|
|
|
|
ctxt->lastError.line,
|
|
|
|
ctxt->lastError.message,
|
|
|
|
contextstr,
|
|
|
|
pointerstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FREE(contextstr);
|
|
|
|
VIR_FREE(pointerstr);
|
|
|
|
}
|
2010-02-24 20:44:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* virXMLParseHelper:
|
|
|
|
* @domcode: error domain of the caller, usually VIR_FROM_THIS
|
|
|
|
* @filename: file to be parsed or NULL if string parsing is requested
|
|
|
|
* @xmlStr: XML string to be parsed in case filename is NULL
|
|
|
|
* @url: URL of XML document for string parser
|
2011-08-18 21:01:36 +00:00
|
|
|
* @ctxt: optional pointer to populate with new context pointer
|
2010-02-24 20:44:47 +00:00
|
|
|
*
|
|
|
|
* Parse XML document provided either as a file or a string. The function
|
|
|
|
* guarantees that the XML document contains a root element.
|
|
|
|
*
|
|
|
|
* Returns parsed XML document.
|
|
|
|
*/
|
|
|
|
xmlDocPtr
|
|
|
|
virXMLParseHelper(int domcode,
|
|
|
|
const char *filename,
|
|
|
|
const char *xmlStr,
|
2011-08-18 21:01:36 +00:00
|
|
|
const char *url,
|
|
|
|
xmlXPathContextPtr *ctxt)
|
2010-02-24 20:44:47 +00:00
|
|
|
{
|
|
|
|
struct virParserData private;
|
|
|
|
xmlParserCtxtPtr pctxt;
|
|
|
|
xmlDocPtr xml = NULL;
|
|
|
|
|
|
|
|
/* Set up a parser context so we can catch the details of XML errors. */
|
|
|
|
pctxt = xmlNewParserCtxt();
|
2011-08-18 21:01:36 +00:00
|
|
|
if (!pctxt || !pctxt->sax) {
|
|
|
|
virReportOOMError();
|
2010-02-24 20:44:47 +00:00
|
|
|
goto error;
|
2011-08-18 21:01:36 +00:00
|
|
|
}
|
2010-02-24 20:44:47 +00:00
|
|
|
|
|
|
|
private.domcode = domcode;
|
|
|
|
pctxt->_private = &private;
|
|
|
|
pctxt->sax->error = catchXMLError;
|
|
|
|
|
|
|
|
if (filename) {
|
|
|
|
xml = xmlCtxtReadFile(pctxt, filename, NULL,
|
|
|
|
XML_PARSE_NOENT | XML_PARSE_NONET |
|
|
|
|
XML_PARSE_NOWARNING);
|
|
|
|
} else {
|
|
|
|
xml = xmlCtxtReadDoc(pctxt, BAD_CAST xmlStr, url, NULL,
|
|
|
|
XML_PARSE_NOENT | XML_PARSE_NONET |
|
|
|
|
XML_PARSE_NOWARNING);
|
|
|
|
}
|
|
|
|
if (!xml)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (xmlDocGetRootElement(xml) == NULL) {
|
|
|
|
virGenericReportError(domcode, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("missing root element"));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2011-08-18 21:01:36 +00:00
|
|
|
if (ctxt) {
|
|
|
|
*ctxt = xmlXPathNewContext(xml);
|
|
|
|
if (!*ctxt) {
|
|
|
|
virReportOOMError();
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
(*ctxt)->node = xmlDocGetRootElement(xml);
|
|
|
|
}
|
|
|
|
|
2010-02-24 20:44:47 +00:00
|
|
|
cleanup:
|
|
|
|
xmlFreeParserCtxt(pctxt);
|
|
|
|
|
|
|
|
return xml;
|
|
|
|
|
|
|
|
error:
|
|
|
|
xmlFreeDoc(xml);
|
|
|
|
xml = NULL;
|
|
|
|
|
|
|
|
if (virGetLastError() == NULL) {
|
|
|
|
virGenericReportError(domcode, VIR_ERR_XML_ERROR,
|
|
|
|
"%s", _("failed to parse xml document"));
|
|
|
|
}
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2011-10-06 09:57:06 +00:00
|
|
|
|
2012-10-29 12:15:55 +00:00
|
|
|
const char *virXMLPickShellSafeComment(const char *str1, const char *str2)
|
|
|
|
{
|
|
|
|
if(str1 && !strpbrk(str1, "\r\t\n !\"#$&'()*;<>?[\\]^`{|}~") &&
|
|
|
|
!strstr(str1, "--"))
|
|
|
|
return str1;
|
|
|
|
if(str2 && !strpbrk(str2, "\r\t\n !\"#$&'()*;<>?[\\]^`{|}~") &&
|
|
|
|
!strstr(str2, "--"))
|
|
|
|
return str2;
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-10-06 09:57:06 +00:00
|
|
|
|
2012-01-27 17:35:09 +00:00
|
|
|
static int virXMLEmitWarning(int fd,
|
|
|
|
const char *name,
|
|
|
|
const char *cmd)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
const char *prologue = "<!--\n\
|
|
|
|
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE \n\
|
|
|
|
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:\n\
|
|
|
|
virsh ";
|
|
|
|
const char *epilogue = "\n\
|
|
|
|
or other application using the libvirt API.\n\
|
|
|
|
-->\n\n";
|
|
|
|
|
2012-10-29 12:15:55 +00:00
|
|
|
if (fd < 0 || !cmd) {
|
2012-01-27 17:35:09 +00:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen(prologue);
|
|
|
|
if (safewrite(fd, prologue, len) != len)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
len = strlen(cmd);
|
|
|
|
if (safewrite(fd, cmd, len) != len)
|
|
|
|
return -1;
|
|
|
|
|
2012-10-29 12:15:55 +00:00
|
|
|
if (name) {
|
2012-10-23 12:16:44 +00:00
|
|
|
if (safewrite(fd, " ", 1) != 1)
|
|
|
|
return -1;
|
2012-01-27 17:35:09 +00:00
|
|
|
|
2012-10-23 12:16:44 +00:00
|
|
|
len = strlen(name);
|
|
|
|
if (safewrite(fd, name, len) != len)
|
|
|
|
return -1;
|
|
|
|
}
|
2012-01-27 17:35:09 +00:00
|
|
|
|
|
|
|
len = strlen(epilogue);
|
|
|
|
if (safewrite(fd, epilogue, len) != len)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-27 18:07:21 +00:00
|
|
|
struct virXMLRewriteFileData {
|
2011-10-06 09:57:06 +00:00
|
|
|
const char *warnName;
|
|
|
|
const char *warnCommand;
|
|
|
|
const char *xml;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
virXMLRewriteFile(int fd, void *opaque)
|
|
|
|
{
|
2012-01-27 18:07:21 +00:00
|
|
|
struct virXMLRewriteFileData *data = opaque;
|
2011-10-06 09:57:06 +00:00
|
|
|
|
2012-10-29 12:15:55 +00:00
|
|
|
if (data->warnCommand) {
|
2012-01-27 17:35:09 +00:00
|
|
|
if (virXMLEmitWarning(fd, data->warnName, data->warnCommand) < 0)
|
2011-10-06 09:57:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (safewrite(fd, data->xml, strlen(data->xml)) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
virXMLSaveFile(const char *path,
|
|
|
|
const char *warnName,
|
|
|
|
const char *warnCommand,
|
|
|
|
const char *xml)
|
|
|
|
{
|
2012-01-27 18:07:21 +00:00
|
|
|
struct virXMLRewriteFileData data = { warnName, warnCommand, xml };
|
2011-10-06 09:57:06 +00:00
|
|
|
|
|
|
|
return virFileRewrite(path, S_IRUSR | S_IWUSR, virXMLRewriteFile, &data);
|
|
|
|
}
|
2011-12-07 22:06:18 +00:00
|
|
|
|
|
|
|
/* Returns the number of children of node, or -1 on error. */
|
|
|
|
long
|
|
|
|
virXMLChildElementCount(xmlNodePtr node)
|
|
|
|
{
|
|
|
|
long ret = 0;
|
|
|
|
xmlNodePtr cur = NULL;
|
|
|
|
|
|
|
|
/* xmlChildElementCount returns 0 on error, which isn't helpful;
|
|
|
|
* besides, it is not available in libxml2 2.6. */
|
|
|
|
if (!node || node->type != XML_ELEMENT_NODE)
|
|
|
|
return -1;
|
|
|
|
cur = node->children;
|
|
|
|
while (cur) {
|
|
|
|
if (cur->type == XML_ELEMENT_NODE)
|
|
|
|
ret++;
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|