mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-12 07:42:56 +00:00
util: buffer: Remove error handling internals
Now that there are no errors reported and tracked in virBuffer, remove all the internals which were used to track them. Signed-off-by: Peter Krempa <pkrempa@redhat.com> Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
parent
244f906b16
commit
418aa809fd
@ -23,26 +23,11 @@
|
|||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
|
||||||
#include "virbuffer.h"
|
#include "virbuffer.h"
|
||||||
#include "virerror.h"
|
|
||||||
#include "virstring.h"
|
#include "virstring.h"
|
||||||
#include "viralloc.h"
|
#include "viralloc.h"
|
||||||
|
|
||||||
#define VIR_FROM_THIS VIR_FROM_NONE
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
||||||
|
|
||||||
/**
|
|
||||||
* virBufferFail
|
|
||||||
* @buf: the buffer
|
|
||||||
* @error: which error occurred (errno value, or -1 for usage)
|
|
||||||
*
|
|
||||||
* Mark the buffer as failed, free the content and set the error flag.
|
|
||||||
*/
|
|
||||||
static void
|
|
||||||
virBufferSetError(virBufferPtr buf, int error)
|
|
||||||
{
|
|
||||||
virBufferFreeAndReset(buf);
|
|
||||||
buf->error = error;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* virBufferAdjustIndent:
|
* virBufferAdjustIndent:
|
||||||
* @buf: the buffer
|
* @buf: the buffer
|
||||||
@ -58,7 +43,7 @@ virBufferSetError(virBufferPtr buf, int error)
|
|||||||
void
|
void
|
||||||
virBufferAdjustIndent(virBufferPtr buf, int indent)
|
virBufferAdjustIndent(virBufferPtr buf, int indent)
|
||||||
{
|
{
|
||||||
if (!buf || buf->error)
|
if (!buf)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (indent > 0) {
|
if (indent > 0) {
|
||||||
@ -88,7 +73,7 @@ virBufferAdjustIndent(virBufferPtr buf, int indent)
|
|||||||
void
|
void
|
||||||
virBufferSetIndent(virBufferPtr buf, int indent)
|
virBufferSetIndent(virBufferPtr buf, int indent)
|
||||||
{
|
{
|
||||||
if (!buf || buf->error)
|
if (!buf)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
buf->indent = indent;
|
buf->indent = indent;
|
||||||
@ -171,7 +156,7 @@ virBufferApplyIndent(virBufferPtr buf)
|
|||||||
void
|
void
|
||||||
virBufferAdd(virBufferPtr buf, const char *str, int len)
|
virBufferAdd(virBufferPtr buf, const char *str, int len)
|
||||||
{
|
{
|
||||||
if (!str || !buf || buf->error || (len == 0 && buf->indent == 0))
|
if (!str || !buf || (len == 0 && buf->indent == 0))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
virBufferInitialize(buf);
|
virBufferInitialize(buf);
|
||||||
@ -203,12 +188,6 @@ virBufferAddBuffer(virBufferPtr buf, virBufferPtr toadd)
|
|||||||
if (!buf)
|
if (!buf)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
if (buf->error || toadd->error) {
|
|
||||||
if (!buf->error)
|
|
||||||
virBufferSetError(buf, toadd->error);
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
virBufferInitialize(buf);
|
virBufferInitialize(buf);
|
||||||
g_string_append_len(buf->str, toadd->str->str, toadd->str->len);
|
g_string_append_len(buf->str, toadd->str->str, toadd->str->len);
|
||||||
|
|
||||||
@ -243,7 +222,7 @@ virBufferAddChar(virBufferPtr buf, char c)
|
|||||||
const char *
|
const char *
|
||||||
virBufferCurrentContent(virBufferPtr buf)
|
virBufferCurrentContent(virBufferPtr buf)
|
||||||
{
|
{
|
||||||
if (!buf || buf->error)
|
if (!buf)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (!buf->str ||
|
if (!buf->str ||
|
||||||
@ -307,12 +286,9 @@ void virBufferFreeAndReset(virBufferPtr buf)
|
|||||||
* Return positive errno value or -1 on usage error, 0 if normal
|
* Return positive errno value or -1 on usage error, 0 if normal
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
virBufferError(const virBuffer *buf)
|
virBufferError(const virBuffer *buf G_GNUC_UNUSED)
|
||||||
{
|
{
|
||||||
if (buf == NULL)
|
return 0;
|
||||||
return -1;
|
|
||||||
|
|
||||||
return buf->error;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -324,25 +300,9 @@ virBufferError(const virBuffer *buf)
|
|||||||
* Return -1 if an error has been reported, 0 otherwise.
|
* Return -1 if an error has been reported, 0 otherwise.
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
virBufferCheckErrorInternal(const virBuffer *buf,
|
virBufferCheckErrorInternal(const virBuffer *buf G_GNUC_UNUSED)
|
||||||
int domcode,
|
|
||||||
const char *filename,
|
|
||||||
const char *funcname,
|
|
||||||
size_t linenr)
|
|
||||||
{
|
{
|
||||||
if (buf->error == 0)
|
return 0;
|
||||||
return 0;
|
|
||||||
|
|
||||||
if (buf->error == ENOMEM) {
|
|
||||||
virReportOOMErrorFull(domcode, filename, funcname, linenr);
|
|
||||||
errno = ENOMEM;
|
|
||||||
} else {
|
|
||||||
virReportErrorHelper(domcode, VIR_ERR_INTERNAL_ERROR, filename,
|
|
||||||
funcname, linenr, "%s",
|
|
||||||
_("Invalid buffer API usage"));
|
|
||||||
errno = EINVAL;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -391,9 +351,6 @@ virBufferVasprintf(virBufferPtr buf, const char *format, va_list argptr)
|
|||||||
if ((format == NULL) || (buf == NULL))
|
if ((format == NULL) || (buf == NULL))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (buf->error)
|
|
||||||
return;
|
|
||||||
|
|
||||||
virBufferInitialize(buf);
|
virBufferInitialize(buf);
|
||||||
virBufferApplyIndent(buf);
|
virBufferApplyIndent(buf);
|
||||||
|
|
||||||
@ -430,9 +387,6 @@ virBufferEscapeString(virBufferPtr buf, const char *format, const char *str)
|
|||||||
if ((format == NULL) || (buf == NULL) || (str == NULL))
|
if ((format == NULL) || (buf == NULL) || (str == NULL))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (buf->error)
|
|
||||||
return;
|
|
||||||
|
|
||||||
len = strlen(str);
|
len = strlen(str);
|
||||||
if (strcspn(str, forbidden_characters) == len) {
|
if (strcspn(str, forbidden_characters) == len) {
|
||||||
virBufferAsprintf(buf, format, str);
|
virBufferAsprintf(buf, format, str);
|
||||||
@ -575,9 +529,6 @@ virBufferEscape(virBufferPtr buf, char escape, const char *toescape,
|
|||||||
if ((format == NULL) || (buf == NULL) || (str == NULL))
|
if ((format == NULL) || (buf == NULL) || (str == NULL))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (buf->error)
|
|
||||||
return;
|
|
||||||
|
|
||||||
len = strlen(str);
|
len = strlen(str);
|
||||||
if (strcspn(str, toescape) == len) {
|
if (strcspn(str, toescape) == len) {
|
||||||
virBufferAsprintf(buf, format, str);
|
virBufferAsprintf(buf, format, str);
|
||||||
@ -615,9 +566,6 @@ virBufferURIEncodeString(virBufferPtr buf, const char *str)
|
|||||||
if ((buf == NULL) || (str == NULL))
|
if ((buf == NULL) || (str == NULL))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (buf->error)
|
|
||||||
return;
|
|
||||||
|
|
||||||
virBufferInitialize(buf);
|
virBufferInitialize(buf);
|
||||||
virBufferApplyIndent(buf);
|
virBufferApplyIndent(buf);
|
||||||
|
|
||||||
@ -643,9 +591,6 @@ virBufferEscapeShell(virBufferPtr buf, const char *str)
|
|||||||
if ((buf == NULL) || (str == NULL))
|
if ((buf == NULL) || (str == NULL))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (buf->error)
|
|
||||||
return;
|
|
||||||
|
|
||||||
/* Only quote if str includes shell metacharacters. */
|
/* Only quote if str includes shell metacharacters. */
|
||||||
if (*str && !strpbrk(str, "\r\t\n !\"#$&'()*;<>?[\\]^`{|}~")) {
|
if (*str && !strpbrk(str, "\r\t\n !\"#$&'()*;<>?[\\]^`{|}~")) {
|
||||||
virBufferAdd(buf, str, -1);
|
virBufferAdd(buf, str, -1);
|
||||||
@ -693,9 +638,6 @@ virBufferStrcatVArgs(virBufferPtr buf,
|
|||||||
{
|
{
|
||||||
char *str;
|
char *str;
|
||||||
|
|
||||||
if (buf->error)
|
|
||||||
return;
|
|
||||||
|
|
||||||
while ((str = va_arg(ap, char *)) != NULL)
|
while ((str = va_arg(ap, char *)) != NULL)
|
||||||
virBufferAdd(buf, str, -1);
|
virBufferAdd(buf, str, -1);
|
||||||
}
|
}
|
||||||
@ -737,7 +679,7 @@ virBufferTrim(virBufferPtr buf, const char *str, int len)
|
|||||||
{
|
{
|
||||||
size_t len2 = 0;
|
size_t len2 = 0;
|
||||||
|
|
||||||
if (!buf || buf->error || !buf->str)
|
if (!buf || !buf->str)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!str && len < 0)
|
if (!str && len < 0)
|
||||||
@ -775,7 +717,7 @@ virBufferAddStr(virBufferPtr buf,
|
|||||||
{
|
{
|
||||||
const char *end;
|
const char *end;
|
||||||
|
|
||||||
if (!buf || !str || buf->error)
|
if (!buf || !str)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
while (*str) {
|
while (*str) {
|
||||||
|
@ -33,11 +33,10 @@
|
|||||||
typedef struct _virBuffer virBuffer;
|
typedef struct _virBuffer virBuffer;
|
||||||
typedef virBuffer *virBufferPtr;
|
typedef virBuffer *virBufferPtr;
|
||||||
|
|
||||||
#define VIR_BUFFER_INITIALIZER { NULL, 0, 0 }
|
#define VIR_BUFFER_INITIALIZER { NULL, 0 }
|
||||||
|
|
||||||
struct _virBuffer {
|
struct _virBuffer {
|
||||||
GString *str;
|
GString *str;
|
||||||
int error; /* errno value, or -1 for usage error */
|
|
||||||
int indent;
|
int indent;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -45,11 +44,7 @@ const char *virBufferCurrentContent(virBufferPtr buf);
|
|||||||
char *virBufferContentAndReset(virBufferPtr buf);
|
char *virBufferContentAndReset(virBufferPtr buf);
|
||||||
void virBufferFreeAndReset(virBufferPtr buf);
|
void virBufferFreeAndReset(virBufferPtr buf);
|
||||||
int virBufferError(const virBuffer *buf);
|
int virBufferError(const virBuffer *buf);
|
||||||
int virBufferCheckErrorInternal(const virBuffer *buf,
|
int virBufferCheckErrorInternal(const virBuffer *buf)
|
||||||
int domcode,
|
|
||||||
const char *filename,
|
|
||||||
const char *funcname,
|
|
||||||
size_t linenr)
|
|
||||||
ATTRIBUTE_NONNULL(1);
|
ATTRIBUTE_NONNULL(1);
|
||||||
|
|
||||||
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(virBuffer, virBufferFreeAndReset);
|
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(virBuffer, virBufferFreeAndReset);
|
||||||
@ -63,8 +58,8 @@ G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(virBuffer, virBufferFreeAndReset);
|
|||||||
* and -1 is returned.
|
* and -1 is returned.
|
||||||
*/
|
*/
|
||||||
#define virBufferCheckError(buf) \
|
#define virBufferCheckError(buf) \
|
||||||
virBufferCheckErrorInternal(buf, VIR_FROM_THIS, __FILE__, __FUNCTION__, \
|
virBufferCheckErrorInternal(buf)
|
||||||
__LINE__)
|
|
||||||
size_t virBufferUse(const virBuffer *buf);
|
size_t virBufferUse(const virBuffer *buf);
|
||||||
void virBufferAdd(virBufferPtr buf, const char *str, int len);
|
void virBufferAdd(virBufferPtr buf, const char *str, int len);
|
||||||
void virBufferAddBuffer(virBufferPtr buf, virBufferPtr toadd);
|
void virBufferAddBuffer(virBufferPtr buf, virBufferPtr toadd);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user