2017-06-02 00:44:06 +02:00
|
|
|
/**
|
|
|
|
* virsavecookie.c: Save cookie handling
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* 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 "virerror.h"
|
|
|
|
#include "virlog.h"
|
|
|
|
#include "virobject.h"
|
|
|
|
#include "virbuffer.h"
|
|
|
|
#include "virxml.h"
|
|
|
|
#include "virsavecookie.h"
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_CONF
|
|
|
|
|
|
|
|
VIR_LOG_INIT("conf.savecookie");
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virSaveCookieParse(xmlXPathContextPtr ctxt,
|
2021-03-11 08:16:13 +01:00
|
|
|
virObject **obj,
|
|
|
|
virSaveCookieCallbacks *saveCookie)
|
2017-06-02 00:44:06 +02:00
|
|
|
{
|
2020-07-28 21:47:48 +02:00
|
|
|
VIR_XPATH_NODE_AUTORESTORE(ctxt)
|
2017-06-02 00:44:06 +02:00
|
|
|
|
|
|
|
*obj = NULL;
|
|
|
|
|
2021-11-04 15:26:07 +01:00
|
|
|
if (!(ctxt->node = virXPathNode("./cookie", ctxt)))
|
2021-11-08 11:19:15 +01:00
|
|
|
return 0;
|
2017-06-02 00:44:06 +02:00
|
|
|
|
2022-09-23 15:03:19 +02:00
|
|
|
if (!saveCookie || !saveCookie->parse)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return saveCookie->parse(ctxt, obj);
|
2017-06-02 00:44:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virSaveCookieParseString(const char *xml,
|
2021-03-11 08:16:13 +01:00
|
|
|
virObject **obj,
|
|
|
|
virSaveCookieCallbacks *saveCookie)
|
2017-06-02 00:44:06 +02:00
|
|
|
{
|
2021-08-11 13:57:18 +02:00
|
|
|
g_autoptr(xmlDoc) doc = NULL;
|
2021-08-11 13:30:26 +02:00
|
|
|
g_autoptr(xmlXPathContext) ctxt = NULL;
|
2017-06-02 00:44:06 +02:00
|
|
|
|
|
|
|
*obj = NULL;
|
|
|
|
|
2022-09-23 15:03:19 +02:00
|
|
|
if (!xml || !saveCookie || !saveCookie->parse)
|
2021-08-17 12:17:31 +02:00
|
|
|
return 0;
|
2017-06-02 00:44:06 +02:00
|
|
|
|
2022-09-23 15:03:19 +02:00
|
|
|
if (!(doc = virXMLParse(NULL, xml, _("(save cookie)"), "cookie", &ctxt, NULL, false)))
|
2021-08-17 12:17:31 +02:00
|
|
|
return -1;
|
2017-06-02 00:44:06 +02:00
|
|
|
|
2022-09-23 15:03:19 +02:00
|
|
|
return saveCookie->parse(ctxt, obj);
|
2017-06-02 00:44:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2021-03-11 08:16:13 +01:00
|
|
|
virSaveCookieFormatBuf(virBuffer *buf,
|
|
|
|
virObject *obj,
|
|
|
|
virSaveCookieCallbacks *saveCookie)
|
2017-06-02 00:44:06 +02:00
|
|
|
{
|
|
|
|
if (!obj || !saveCookie || !saveCookie->format)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
virBufferAddLit(buf, "<cookie>\n");
|
|
|
|
virBufferAdjustIndent(buf, 2);
|
|
|
|
|
|
|
|
if (saveCookie->format(buf, obj) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
virBufferAdjustIndent(buf, -2);
|
|
|
|
virBufferAddLit(buf, "</cookie>\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *
|
2021-03-11 08:16:13 +01:00
|
|
|
virSaveCookieFormat(virObject *obj,
|
|
|
|
virSaveCookieCallbacks *saveCookie)
|
2017-06-02 00:44:06 +02:00
|
|
|
{
|
2020-07-02 22:19:01 -04:00
|
|
|
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
|
2017-06-02 00:44:06 +02:00
|
|
|
|
|
|
|
if (virSaveCookieFormatBuf(&buf, obj, saveCookie) < 0)
|
2020-07-02 23:19:26 -04:00
|
|
|
return NULL;
|
2017-06-02 00:44:06 +02:00
|
|
|
|
|
|
|
return virBufferContentAndReset(&buf);
|
|
|
|
}
|