mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-08-26 18:41:17 +00:00
src/conf.c: Improved error messages in XM conf module.
This commit is contained in:
parent
422311ff32
commit
7076981f50
@ -1,3 +1,7 @@
|
|||||||
|
Wed Sep 17 15:08:00 BST 2008 Richard W.M. Jones <rjones@redhat.com>
|
||||||
|
|
||||||
|
* src/conf.c: Improved error messages in XM conf module.
|
||||||
|
|
||||||
Wed Sep 17 15:03:00 BST 2008 Richard W.M. Jones <rjones@redhat.com>
|
Wed Sep 17 15:03:00 BST 2008 Richard W.M. Jones <rjones@redhat.com>
|
||||||
|
|
||||||
* libvirt.spec.in: Specfile enhancements for minimal
|
* libvirt.spec.in: Specfile enhancements for minimal
|
||||||
|
90
src/conf.c
90
src/conf.c
@ -79,25 +79,34 @@ struct _virConf {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* virConfError:
|
* virConfError:
|
||||||
* @conf: the configuration if available
|
* @ctxt: the parser context if available or NULL
|
||||||
* @error: the error number
|
* @error: the error number
|
||||||
* @info: extra information string
|
* @info: extra information string
|
||||||
* @line: line for the error
|
|
||||||
*
|
*
|
||||||
* Handle an error at the xend daemon interface
|
* Handle an error at the xend daemon interface
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
virConfError(virConfPtr conf ATTRIBUTE_UNUSED,
|
virConfError(virConfParserCtxtPtr ctxt,
|
||||||
virErrorNumber error, const char *info, int line)
|
virErrorNumber error, const char *info)
|
||||||
{
|
{
|
||||||
const char *errmsg;
|
const char *format;
|
||||||
|
|
||||||
if (error == VIR_ERR_OK)
|
if (error == VIR_ERR_OK)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
errmsg = __virErrorMsg(error, info);
|
/* Construct the string 'filename:line: info' if we have that. */
|
||||||
__virRaiseError(NULL, NULL, NULL, VIR_FROM_CONF, error, VIR_ERR_ERROR,
|
if (ctxt && ctxt->filename) {
|
||||||
errmsg, info, NULL, line, 0, errmsg, info, line);
|
__virRaiseError(NULL, NULL, NULL, VIR_FROM_CONF, error, VIR_ERR_ERROR,
|
||||||
|
info, ctxt->filename, NULL,
|
||||||
|
ctxt->line, 0,
|
||||||
|
"%s:%d: %s", ctxt->filename, ctxt->line, info);
|
||||||
|
} else {
|
||||||
|
format = __virErrorMsg(error, info);
|
||||||
|
__virRaiseError(NULL, NULL, NULL, VIR_FROM_CONF, error, VIR_ERR_ERROR,
|
||||||
|
info, NULL, NULL,
|
||||||
|
ctxt ? ctxt->line : 0, 0,
|
||||||
|
format, info);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -152,7 +161,7 @@ __virConfNew(void)
|
|||||||
virConfPtr ret;
|
virConfPtr ret;
|
||||||
|
|
||||||
if (VIR_ALLOC(ret) < 0) {
|
if (VIR_ALLOC(ret) < 0) {
|
||||||
virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocating configuration"), 0);
|
virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocating configuration"));
|
||||||
return(NULL);
|
return(NULL);
|
||||||
}
|
}
|
||||||
ret->filename = NULL;
|
ret->filename = NULL;
|
||||||
@ -200,7 +209,7 @@ virConfAddEntry(virConfPtr conf, char *name, virConfValuePtr value, char *comm)
|
|||||||
return(NULL);
|
return(NULL);
|
||||||
|
|
||||||
if (VIR_ALLOC(ret) < 0) {
|
if (VIR_ALLOC(ret) < 0) {
|
||||||
virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocating configuration"), 0);
|
virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocating configuration"));
|
||||||
return(NULL);
|
return(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -335,8 +344,7 @@ virConfParseLong(virConfParserCtxtPtr ctxt, long *val)
|
|||||||
NEXT;
|
NEXT;
|
||||||
}
|
}
|
||||||
if ((ctxt->cur >= ctxt->end) || (!c_isdigit(CUR))) {
|
if ((ctxt->cur >= ctxt->end) || (!c_isdigit(CUR))) {
|
||||||
virConfError(NULL, VIR_ERR_CONF_SYNTAX, _("unterminated number"),
|
virConfError(ctxt, VIR_ERR_CONF_SYNTAX, _("unterminated number"));
|
||||||
ctxt->line);
|
|
||||||
return(-1);
|
return(-1);
|
||||||
}
|
}
|
||||||
while ((ctxt->cur < ctxt->end) && (c_isdigit(CUR))) {
|
while ((ctxt->cur < ctxt->end) && (c_isdigit(CUR))) {
|
||||||
@ -369,8 +377,7 @@ virConfParseString(virConfParserCtxtPtr ctxt)
|
|||||||
while ((ctxt->cur < ctxt->end) && (CUR != '\'') && (!IS_EOL(CUR)))
|
while ((ctxt->cur < ctxt->end) && (CUR != '\'') && (!IS_EOL(CUR)))
|
||||||
NEXT;
|
NEXT;
|
||||||
if (CUR != '\'') {
|
if (CUR != '\'') {
|
||||||
virConfError(NULL, VIR_ERR_CONF_SYNTAX, _("unterminated string"),
|
virConfError(ctxt, VIR_ERR_CONF_SYNTAX, _("unterminated string"));
|
||||||
ctxt->line);
|
|
||||||
return(NULL);
|
return(NULL);
|
||||||
}
|
}
|
||||||
ret = strndup(base, ctxt->cur - base);
|
ret = strndup(base, ctxt->cur - base);
|
||||||
@ -386,8 +393,7 @@ virConfParseString(virConfParserCtxtPtr ctxt)
|
|||||||
}
|
}
|
||||||
if ((ctxt->cur[0] != '"') || (ctxt->cur[1] != '"') ||
|
if ((ctxt->cur[0] != '"') || (ctxt->cur[1] != '"') ||
|
||||||
(ctxt->cur[2] != '"')) {
|
(ctxt->cur[2] != '"')) {
|
||||||
virConfError(NULL, VIR_ERR_CONF_SYNTAX, _("unterminated string"),
|
virConfError(ctxt, VIR_ERR_CONF_SYNTAX, _("unterminated string"));
|
||||||
ctxt->line);
|
|
||||||
return(NULL);
|
return(NULL);
|
||||||
}
|
}
|
||||||
ret = strndup(base, ctxt->cur - base);
|
ret = strndup(base, ctxt->cur - base);
|
||||||
@ -398,8 +404,7 @@ virConfParseString(virConfParserCtxtPtr ctxt)
|
|||||||
while ((ctxt->cur < ctxt->end) && (CUR != '"') && (!IS_EOL(CUR)))
|
while ((ctxt->cur < ctxt->end) && (CUR != '"') && (!IS_EOL(CUR)))
|
||||||
NEXT;
|
NEXT;
|
||||||
if (CUR != '"') {
|
if (CUR != '"') {
|
||||||
virConfError(NULL, VIR_ERR_CONF_SYNTAX, _("unterminated string"),
|
virConfError(ctxt, VIR_ERR_CONF_SYNTAX, _("unterminated string"));
|
||||||
ctxt->line);
|
|
||||||
return(NULL);
|
return(NULL);
|
||||||
}
|
}
|
||||||
ret = strndup(base, ctxt->cur - base);
|
ret = strndup(base, ctxt->cur - base);
|
||||||
@ -426,8 +431,7 @@ virConfParseValue(virConfParserCtxtPtr ctxt)
|
|||||||
|
|
||||||
SKIP_BLANKS;
|
SKIP_BLANKS;
|
||||||
if (ctxt->cur >= ctxt->end) {
|
if (ctxt->cur >= ctxt->end) {
|
||||||
virConfError(NULL, VIR_ERR_CONF_SYNTAX, _("expecting a value"),
|
virConfError(ctxt, VIR_ERR_CONF_SYNTAX, _("expecting a value"));
|
||||||
ctxt->line);
|
|
||||||
return(NULL);
|
return(NULL);
|
||||||
}
|
}
|
||||||
if ((CUR == '"') || (CUR == '\'')) {
|
if ((CUR == '"') || (CUR == '\'')) {
|
||||||
@ -446,8 +450,8 @@ virConfParseValue(virConfParserCtxtPtr ctxt)
|
|||||||
}
|
}
|
||||||
while ((ctxt->cur < ctxt->end) && (CUR != ']')) {
|
while ((ctxt->cur < ctxt->end) && (CUR != ']')) {
|
||||||
if (CUR != ',') {
|
if (CUR != ',') {
|
||||||
virConfError(NULL, VIR_ERR_CONF_SYNTAX,
|
virConfError(ctxt, VIR_ERR_CONF_SYNTAX,
|
||||||
_("expecting a separator in list"), ctxt->line);
|
_("expecting a separator in list"));
|
||||||
virConfFreeList(lst);
|
virConfFreeList(lst);
|
||||||
return(NULL);
|
return(NULL);
|
||||||
}
|
}
|
||||||
@ -469,8 +473,8 @@ virConfParseValue(virConfParserCtxtPtr ctxt)
|
|||||||
if (CUR == ']') {
|
if (CUR == ']') {
|
||||||
NEXT;
|
NEXT;
|
||||||
} else {
|
} else {
|
||||||
virConfError(NULL, VIR_ERR_CONF_SYNTAX,
|
virConfError(ctxt, VIR_ERR_CONF_SYNTAX,
|
||||||
_("list is not closed with ]"), ctxt->line);
|
_("list is not closed with ]"));
|
||||||
virConfFreeList(lst);
|
virConfFreeList(lst);
|
||||||
return(NULL);
|
return(NULL);
|
||||||
}
|
}
|
||||||
@ -480,12 +484,11 @@ virConfParseValue(virConfParserCtxtPtr ctxt)
|
|||||||
}
|
}
|
||||||
type = VIR_CONF_LONG;
|
type = VIR_CONF_LONG;
|
||||||
} else {
|
} else {
|
||||||
virConfError(NULL, VIR_ERR_CONF_SYNTAX, _("expecting a value"),
|
virConfError(ctxt, VIR_ERR_CONF_SYNTAX, _("expecting a value"));
|
||||||
ctxt->line);
|
|
||||||
return(NULL);
|
return(NULL);
|
||||||
}
|
}
|
||||||
if (VIR_ALLOC(ret) < 0) {
|
if (VIR_ALLOC(ret) < 0) {
|
||||||
virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocating configuration"), 0);
|
virConfError(ctxt, VIR_ERR_NO_MEMORY, _("allocating configuration"));
|
||||||
virConfFreeList(lst);
|
virConfFreeList(lst);
|
||||||
VIR_FREE(str);
|
VIR_FREE(str);
|
||||||
return(NULL);
|
return(NULL);
|
||||||
@ -515,15 +518,14 @@ virConfParseName(virConfParserCtxtPtr ctxt)
|
|||||||
base = ctxt->cur;
|
base = ctxt->cur;
|
||||||
/* TODO: probably need encoding support and UTF-8 parsing ! */
|
/* TODO: probably need encoding support and UTF-8 parsing ! */
|
||||||
if (!c_isalpha(CUR)) {
|
if (!c_isalpha(CUR)) {
|
||||||
virConfError(NULL, VIR_ERR_CONF_SYNTAX, _("expecting a name"), ctxt->line);
|
virConfError(ctxt, VIR_ERR_CONF_SYNTAX, _("expecting a name"));
|
||||||
return(NULL);
|
return(NULL);
|
||||||
}
|
}
|
||||||
while ((ctxt->cur < ctxt->end) && (c_isalnum(CUR) || (CUR == '_')))
|
while ((ctxt->cur < ctxt->end) && (c_isalnum(CUR) || (CUR == '_')))
|
||||||
NEXT;
|
NEXT;
|
||||||
ret = strndup(base, ctxt->cur - base);
|
ret = strndup(base, ctxt->cur - base);
|
||||||
if (ret == NULL) {
|
if (ret == NULL) {
|
||||||
virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocating configuration"),
|
virConfError(ctxt, VIR_ERR_NO_MEMORY, _("allocating configuration"));
|
||||||
ctxt->line);
|
|
||||||
return(NULL);
|
return(NULL);
|
||||||
}
|
}
|
||||||
return(ret);
|
return(ret);
|
||||||
@ -550,8 +552,7 @@ virConfParseComment(virConfParserCtxtPtr ctxt)
|
|||||||
while ((ctxt->cur < ctxt->end) && (!IS_EOL(CUR))) NEXT;
|
while ((ctxt->cur < ctxt->end) && (!IS_EOL(CUR))) NEXT;
|
||||||
comm = strndup(base, ctxt->cur - base);
|
comm = strndup(base, ctxt->cur - base);
|
||||||
if (comm == NULL) {
|
if (comm == NULL) {
|
||||||
virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocating configuration"),
|
virConfError(ctxt, VIR_ERR_NO_MEMORY, _("allocating configuration"));
|
||||||
ctxt->line);
|
|
||||||
return(-1);
|
return(-1);
|
||||||
}
|
}
|
||||||
virConfAddEntry(ctxt->conf, NULL, NULL, comm);
|
virConfAddEntry(ctxt->conf, NULL, NULL, comm);
|
||||||
@ -578,8 +579,7 @@ virConfParseSeparator(virConfParserCtxtPtr ctxt)
|
|||||||
NEXT;
|
NEXT;
|
||||||
SKIP_BLANKS_AND_EOL;
|
SKIP_BLANKS_AND_EOL;
|
||||||
} else {
|
} else {
|
||||||
virConfError(NULL, VIR_ERR_CONF_SYNTAX, _("expecting a separator"),
|
virConfError(ctxt, VIR_ERR_CONF_SYNTAX, _("expecting a separator"));
|
||||||
ctxt->line);
|
|
||||||
return(-1);
|
return(-1);
|
||||||
}
|
}
|
||||||
return(0);
|
return(0);
|
||||||
@ -610,8 +610,7 @@ virConfParseStatement(virConfParserCtxtPtr ctxt)
|
|||||||
return(-1);
|
return(-1);
|
||||||
SKIP_BLANKS;
|
SKIP_BLANKS;
|
||||||
if (CUR != '=') {
|
if (CUR != '=') {
|
||||||
virConfError(NULL, VIR_ERR_CONF_SYNTAX, _("expecting an assignment"),
|
virConfError(ctxt, VIR_ERR_CONF_SYNTAX, _("expecting an assignment"));
|
||||||
ctxt->line);
|
|
||||||
return(-1);
|
return(-1);
|
||||||
}
|
}
|
||||||
NEXT;
|
NEXT;
|
||||||
@ -628,8 +627,7 @@ virConfParseStatement(virConfParserCtxtPtr ctxt)
|
|||||||
while ((ctxt->cur < ctxt->end) && (!IS_EOL(CUR))) NEXT;
|
while ((ctxt->cur < ctxt->end) && (!IS_EOL(CUR))) NEXT;
|
||||||
comm = strndup(base, ctxt->cur - base);
|
comm = strndup(base, ctxt->cur - base);
|
||||||
if (comm == NULL) {
|
if (comm == NULL) {
|
||||||
virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocating configuration"),
|
virConfError(ctxt, VIR_ERR_NO_MEMORY, _("allocating configuration"));
|
||||||
ctxt->line);
|
|
||||||
VIR_FREE(name);
|
VIR_FREE(name);
|
||||||
virConfFreeValue(value);
|
virConfFreeValue(value);
|
||||||
return(-1);
|
return(-1);
|
||||||
@ -709,12 +707,12 @@ __virConfReadFile(const char *filename)
|
|||||||
virConfPtr conf;
|
virConfPtr conf;
|
||||||
|
|
||||||
if (filename == NULL) {
|
if (filename == NULL) {
|
||||||
virConfError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__, 0);
|
virConfError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
|
||||||
return(NULL);
|
return(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((len = virFileReadAll(filename, MAX_CONFIG_FILE_SIZE, &content)) < 0) {
|
if ((len = virFileReadAll(filename, MAX_CONFIG_FILE_SIZE, &content)) < 0) {
|
||||||
virConfError(NULL, VIR_ERR_OPEN_FAILED, filename, 0);
|
virConfError(NULL, VIR_ERR_OPEN_FAILED, filename);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -740,7 +738,7 @@ virConfPtr
|
|||||||
__virConfReadMem(const char *memory, int len)
|
__virConfReadMem(const char *memory, int len)
|
||||||
{
|
{
|
||||||
if ((memory == NULL) || (len < 0)) {
|
if ((memory == NULL) || (len < 0)) {
|
||||||
virConfError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__, 0);
|
virConfError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
|
||||||
return(NULL);
|
return(NULL);
|
||||||
}
|
}
|
||||||
if (len == 0)
|
if (len == 0)
|
||||||
@ -762,7 +760,7 @@ __virConfFree(virConfPtr conf)
|
|||||||
{
|
{
|
||||||
virConfEntryPtr tmp;
|
virConfEntryPtr tmp;
|
||||||
if (conf == NULL) {
|
if (conf == NULL) {
|
||||||
virConfError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__, 0);
|
virConfError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
|
||||||
return(-1);
|
return(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -891,14 +889,14 @@ __virConfWriteFile(const char *filename, virConfPtr conf)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (virBufferError(&buf)) {
|
if (virBufferError(&buf)) {
|
||||||
virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocate buffer"), 0);
|
virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocate buffer"));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR );
|
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR );
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
char *tmp = virBufferContentAndReset(&buf);
|
char *tmp = virBufferContentAndReset(&buf);
|
||||||
virConfError(NULL, VIR_ERR_WRITE_FAILED, _("failed to open file"), 0);
|
virConfError(NULL, VIR_ERR_WRITE_FAILED, _("failed to open file"));
|
||||||
VIR_FREE(tmp);
|
VIR_FREE(tmp);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -909,7 +907,7 @@ __virConfWriteFile(const char *filename, virConfPtr conf)
|
|||||||
VIR_FREE(content);
|
VIR_FREE(content);
|
||||||
close(fd);
|
close(fd);
|
||||||
if (ret != (int)use) {
|
if (ret != (int)use) {
|
||||||
virConfError(NULL, VIR_ERR_WRITE_FAILED, _("failed to save content"), 0);
|
virConfError(NULL, VIR_ERR_WRITE_FAILED, _("failed to save content"));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -947,7 +945,7 @@ __virConfWriteMem(char *memory, int *len, virConfPtr conf)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (virBufferError(&buf)) {
|
if (virBufferError(&buf)) {
|
||||||
virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocate buffer"), 0);
|
virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocate buffer"));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user