conf.c 22.6 KB
Newer Older
D
Daniel Veillard 已提交
1 2 3
/**
 * conf.c: parser for a subset of the Python encoded Xen configuration files
 *
4
 * Copyright (C) 2006, 2007 Red Hat, Inc.
D
Daniel Veillard 已提交
5 6 7 8 9 10
 *
 * See COPYING.LIB for the License of this software
 *
 * Daniel Veillard <veillard@redhat.com>
 */

11 12
#include "config.h"

D
Daniel Veillard 已提交
13 14 15 16 17 18 19 20 21
#include <string.h>

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "internal.h"
22
#include "buf.h"
D
Daniel Veillard 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
#include "conf.h"

/************************************************************************
 *									*
 *	Structures and macros used by the mini parser			*
 *									*
 ************************************************************************/

typedef struct _virConfParserCtxt virConfParserCtxt;
typedef virConfParserCtxt *virConfParserCtxtPtr;

struct _virConfParserCtxt {
    const char* filename;
    const char* base;
    const char* cur;
    const char *end;
    int line;

    virConfPtr conf;
};

#define CUR (*ctxt->cur)
#define NEXT if (ctxt->cur < ctxt->end) ctxt->cur++;
#define IS_EOL(c) (((c) == '\n') || ((c) == '\r'))
#define IS_BLANK(c) (((c) == ' ') || ((c) == '\n') || ((c) == '\r') ||	\
                     ((c) == '\t'))
#define SKIP_BLANKS {while ((ctxt->cur < ctxt->end) && (IS_BLANK(CUR))){\
			   if (CUR == '\n') ctxt->line++;		\
			   ctxt->cur++;}}
#define IS_SPACE(c) (((c) == ' ') || ((c) == '\t'))
#define SKIP_SPACES {while ((ctxt->cur < ctxt->end) && (IS_SPACE(CUR)))	\
			   ctxt->cur++;}
#define IS_CHAR(c) ((((c) >= 'a') && ((c) <= 'z')) ||			\
                    (((c) >= 'A') && ((c) <= 'Z')))
#define IS_DIGIT(c) (((c) >= '0') && ((c) <= '9'))

/************************************************************************
 *									*
 *		Structures used by configuration data			*
 *									*
 ************************************************************************/

typedef struct _virConfEntry virConfEntry;
typedef virConfEntry *virConfEntryPtr;

struct _virConfEntry {
    virConfEntryPtr next;
    char* name;
    char* comment;
    virConfValuePtr value;
};

struct _virConf {
    const char* filename;
    virConfEntryPtr entries;
};

/**
 * virConfError:
 * @conf: the configuration if available
 * @error: the error number
 * @info: extra information string
 * @line: line for the error
 *
 * Handle an error at the xend daemon interface
 */
static void
virConfError(virConfPtr conf ATTRIBUTE_UNUSED,
             virErrorNumber error, const char *info, int line)
{
    const char *errmsg;

    if (error == VIR_ERR_OK)
        return;

    errmsg = __virErrorMsg(error, info);
99
    __virRaiseError(NULL, NULL, NULL, VIR_FROM_CONF, error, VIR_ERR_ERROR,
D
Daniel Veillard 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
                    errmsg, info, NULL, line, 0, errmsg, info, line);
}


/************************************************************************
 *									*
 *		Structures allocations and deallocations		*
 *									*
 ************************************************************************/
static void virConfFreeValue(virConfValuePtr val);

/**
 * virConfFreeList:
 * @list: the list to free
 *
 * Free a list
 */
static void
virConfFreeList(virConfValuePtr list)
{
    virConfValuePtr next;

    while (list != NULL) {
        next = list->next;
124 125 126
        list->next = NULL;
        virConfFreeValue(list);
        list = next;
D
Daniel Veillard 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140
    }
}

/**
 * virConfFreeValue:
 * @val: the value to free
 *
 * Free a value
 */
static void
virConfFreeValue(virConfValuePtr val)
{
    if (val == NULL)
        return;
141 142
    if (val->type == VIR_CONF_STRING &&
        val->str != NULL)
D
Daniel Veillard 已提交
143
        free(val->str);
144 145
    if (val->type == VIR_CONF_LIST &&
        val->list != NULL)
D
Daniel Veillard 已提交
146 147 148 149
        virConfFreeList(val->list);
    free(val);
}

150 151
virConfPtr
__virConfNew(void)
D
Daniel Veillard 已提交
152 153 154
{
    virConfPtr ret;

155
    ret = calloc(1, sizeof(*ret));
D
Daniel Veillard 已提交
156
    if (ret == NULL) {
157
        virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocating configuration"), 0);
D
Daniel Veillard 已提交
158 159
        return(NULL);
    }
160 161 162 163
    ret->filename = NULL;

    return(ret);
}
D
Daniel Veillard 已提交
164

165 166 167 168 169 170 171 172 173 174 175 176 177 178
/**
 * virConfCreate:
 * @filename: the name to report errors
 *
 * Create a configuration internal structure
 *
 * Returns a pointer or NULL in case of error.
 */
static virConfPtr
virConfCreate(const char *filename)
{
    virConfPtr ret = virConfNew();
    if (ret)
        ret->filename = filename;
D
Daniel Veillard 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    return(ret);
}

/**
 * virConfAddEntry:
 * @conf: the conf structure
 * @name: name of the entry or NULL for comment
 * @value: the value if any
 * @comm: extra comment for that entry if any
 *
 * add one entry to the conf, the parameters are included in the conf
 * if successful and freed on virConfFree()
 *
 * Returns a pointer to the entry or NULL in case of failure
 */
static virConfEntryPtr
virConfAddEntry(virConfPtr conf, char *name, virConfValuePtr value, char *comm)
{
    virConfEntryPtr ret, prev;

    if (conf == NULL)
        return(NULL);
    if ((comm == NULL) && (name == NULL))
        return(NULL);
    
204
    ret = calloc(1, sizeof(*ret));
D
Daniel Veillard 已提交
205
    if (ret == NULL) {
206
        virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocating configuration"), 0);
D
Daniel Veillard 已提交
207 208
        return(NULL);
    }
209

D
Daniel Veillard 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
    ret->name = name;
    ret->value = value;
    ret->comment = comm;

    if (conf->entries == NULL) {
        conf->entries = ret;
    } else {
        prev = conf->entries;
	while (prev->next != NULL)
	    prev = prev->next;
	prev->next = ret;
    }
    return(ret);
}

/************************************************************************
 *									*
 *			Serialization					*
 *									*
 ************************************************************************/

/**
 * virConfSaveValue:
 * @buf: output buffer
 * @val: a value
 *
 * Serialize the value to the buffer
 *
 * Returns 0 in case of success, -1 in case of error.
 */
static int
virConfSaveValue(virBufferPtr buf, virConfValuePtr val)
{
    if (val == NULL)
        return(-1);
    switch (val->type) {
        case VIR_CONF_NONE:
	    return(-1);
	case VIR_CONF_LONG:
	    virBufferVSprintf(buf, "%ld", val->l);
	    break;
	case VIR_CONF_STRING:
	    if (strchr(val->str, '\n') != NULL) {
		virBufferVSprintf(buf, "\"\"\"%s\"\"\"", val->str);
	    } else if (strchr(val->str, '"') == NULL) {
		virBufferVSprintf(buf, "\"%s\"", val->str);
	    } else if (strchr(val->str, '\'') == NULL) {
		virBufferVSprintf(buf, "'%s'", val->str);
	    } else {
		virBufferVSprintf(buf, "\"\"\"%s\"\"\"", val->str);
	    }
	    break;
	case VIR_CONF_LIST: {
	    virConfValuePtr cur;

	    cur = val->list;
	    virBufferAdd(buf, "[ ", 2);
	    if (cur != NULL) {
	        virConfSaveValue(buf, cur);
		cur = cur->next;
		while (cur != NULL) {
		    virBufferAdd(buf, ", ", 2);
		    virConfSaveValue(buf, cur);
		    cur = cur->next;
		}
	    }
	    virBufferAdd(buf, " ]", 2);
	    break;
	}
	default:
	    return(-1);
    }
    return(0);
}

/**
 * virConfSaveEntry:
 * @buf: output buffer
 * @cur: a conf entry
 *
 * Serialize the entry to the buffer
 *
 * Returns 0 in case of success, -1 in case of error.
 */
static int
virConfSaveEntry(virBufferPtr buf, virConfEntryPtr cur)
{
    if (cur->name != NULL) {
        virBufferAdd(buf, cur->name, -1);
	virBufferAdd(buf, " = ", 3);
	virConfSaveValue(buf, cur->value);
	if (cur->comment != NULL) {
	    virBufferAdd(buf, " #", 2);
	    virBufferAdd(buf, cur->comment, -1);
	}
    } else if (cur->comment != NULL) {
	virBufferAdd(buf, "#", 1);
	virBufferAdd(buf, cur->comment, -1);
    }
    virBufferAdd(buf, "\n", 1);
    return(0);
}

/************************************************************************
 *									*
 *			The parser core					*
 *									*
 ************************************************************************/

/**
 * virConfParseLong:
 * @ctxt: the parsing context
 * @val: the result
 *
 * Parse one long int value
 *
 * Returns 0 in case of success and -1 in case of error
 */
static int
virConfParseLong(virConfParserCtxtPtr ctxt, long *val)
{
    long l = 0;
    int neg = 0;

    if (CUR == '-') {
        neg = 1;
	NEXT;
    } else if (CUR == '+') {
        NEXT;
    }
    if ((ctxt->cur >= ctxt->end) || (!IS_DIGIT(CUR))) {
341
        virConfError(NULL, VIR_ERR_CONF_SYNTAX, _("unterminated number"),
D
Daniel Veillard 已提交
342 343 344 345 346 347 348
		     ctxt->line);
	return(-1);
    }
    while ((ctxt->cur < ctxt->end) && (IS_DIGIT(CUR))) {
        l = l * 10 + (CUR - '0');
	NEXT;
    }
349 350
    if (neg)
        l = -l;
D
Daniel Veillard 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
    *val = l;
    return(0);
}

/**
 * virConfParseString:
 * @ctxt: the parsing context
 *
 * Parse one string
 *
 * Returns a pointer to the string or NULL in case of error
 */
static char *
virConfParseString(virConfParserCtxtPtr ctxt)
{
    const char *base;
    char *ret = NULL;

    if (CUR == '\'') {
        NEXT;
	base = ctxt->cur;
	while ((ctxt->cur < ctxt->end) && (CUR != '\'') && (!IS_EOL(CUR)))
	    NEXT;
	if (CUR != '\'') {
375
	    virConfError(NULL, VIR_ERR_CONF_SYNTAX, _("unterminated string"),
D
Daniel Veillard 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
			 ctxt->line);
	    return(NULL);
	}
	ret = strndup(base, ctxt->cur - base);
	NEXT;
    } else if ((ctxt->cur + 6 < ctxt->end) && (ctxt->cur[0] == '"') &&
               (ctxt->cur[1] == '"') && (ctxt->cur[2] == '"')) {
	ctxt->cur += 3;
	base = ctxt->cur;
	while ((ctxt->cur + 2 < ctxt->end) && (ctxt->cur[0] == '"') &&
	       (ctxt->cur[1] == '"') && (ctxt->cur[2] == '"')) {
	       if (CUR == '\n') ctxt->line++;
	       NEXT;
	}
	if ((ctxt->cur[0] != '"') || (ctxt->cur[1] != '"') ||
	    (ctxt->cur[2] != '"')) {
392
	    virConfError(NULL, VIR_ERR_CONF_SYNTAX, _("unterminated string"),
D
Daniel Veillard 已提交
393 394 395 396 397 398 399 400 401 402 403
			 ctxt->line);
	    return(NULL);
	}
	ret = strndup(base, ctxt->cur - base);
	ctxt->cur += 3;
    } else if (CUR == '"') {
        NEXT;
	base = ctxt->cur;
	while ((ctxt->cur < ctxt->end) && (CUR != '"') && (!IS_EOL(CUR)))
	    NEXT;
	if (CUR != '"') {
404
	    virConfError(NULL, VIR_ERR_CONF_SYNTAX, _("unterminated string"),
D
Daniel Veillard 已提交
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
			 ctxt->line);
	    return(NULL);
	}
	ret = strndup(base, ctxt->cur - base);
	NEXT;
    }
    return(ret);
}

/**
 * virConfParseValue:
 * @ctxt: the parsing context
 *
 * Parse one value
 *
 * Returns a pointer to the value or NULL in case of error
 */
static virConfValuePtr
virConfParseValue(virConfParserCtxtPtr ctxt)
{
    virConfValuePtr ret, lst = NULL, tmp, prev;
    virConfType type = VIR_CONF_NONE;
    char *str = NULL;
    long  l = 0;

    SKIP_SPACES;
    if (ctxt->cur >= ctxt->end) {
432
        virConfError(NULL, VIR_ERR_CONF_SYNTAX, _("expecting a value"),
D
Daniel Veillard 已提交
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
	             ctxt->line);
	return(NULL);
    }
    if ((CUR == '"') || (CUR == '\'')) {
        type = VIR_CONF_STRING;
        str = virConfParseString(ctxt);
	if (str == NULL)
	    return(NULL);
    } else if (CUR == '[') {
        type = VIR_CONF_LIST;
        NEXT;
	SKIP_BLANKS;
	if ((ctxt->cur < ctxt->end) && (CUR != ']')) {
	    lst = virConfParseValue(ctxt);
	    SKIP_BLANKS;
	}
	while ((ctxt->cur < ctxt->end) && (CUR != ']')) {
	    if (CUR != ',') {
		virConfError(NULL, VIR_ERR_CONF_SYNTAX,
452
		             _("expecting a separator in list"), ctxt->line);
D
Daniel Veillard 已提交
453 454 455 456 457
	        virConfFreeList(lst);
		return(NULL);
	    }
	    NEXT;
	    SKIP_BLANKS;
458 459 460
	    if (CUR == ']') {
	        break;
	    }
D
Daniel Veillard 已提交
461 462 463 464 465 466 467 468 469 470 471 472 473 474
	    tmp = virConfParseValue(ctxt);
	    if (tmp == NULL) {
	        virConfFreeList(lst);
		return(NULL);
	    }
	    prev = lst;
	    while (prev->next != NULL) prev = prev->next;
	    prev->next = tmp;
	    SKIP_BLANKS;
	}
	if (CUR == ']') {
	    NEXT;
	} else {
	    virConfError(NULL, VIR_ERR_CONF_SYNTAX,
475
			 _("list is not closed with ] "), ctxt->line);
D
Daniel Veillard 已提交
476 477 478 479 480 481 482 483 484
	    virConfFreeList(lst);
	    return(NULL);
	}
    } else if (IS_DIGIT(CUR) || (CUR == '-') || (CUR == '+')) {
        if (virConfParseLong(ctxt, &l) < 0) {
	    return(NULL);
	}
        type = VIR_CONF_LONG;
    } else {
485
        virConfError(NULL, VIR_ERR_CONF_SYNTAX, _("expecting a value"),
D
Daniel Veillard 已提交
486 487 488
	             ctxt->line);
	return(NULL);
    }
489
    ret = calloc(1, sizeof(*ret));
D
Daniel Veillard 已提交
490
    if (ret == NULL) {
491
        virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocating configuration"), 0);
D
Daniel Veillard 已提交
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
	if (str != NULL)
	    free(str);
        return(NULL);
    }
    ret->type = type;
    ret->l = l;
    ret->str = str;
    ret->list = lst;
    return(ret);
}

/**
 * virConfParseName:
 * @ctxt: the parsing context
 *
 * Parse one name
 *
 * Returns a copy of the new string, NULL in case of error
 */
static char *
virConfParseName(virConfParserCtxtPtr ctxt)
{
    const char *base;
    char *ret;

    SKIP_SPACES;
    base = ctxt->cur;
    /* TODO: probably need encoding support and UTF-8 parsing ! */
    if (!IS_CHAR(CUR)) {
521
        virConfError(NULL, VIR_ERR_CONF_SYNTAX, _("expecting a name"), ctxt->line);
D
Daniel Veillard 已提交
522 523
	return(NULL);
    }
524
    while ((ctxt->cur < ctxt->end) && ((IS_CHAR(CUR)) || (IS_DIGIT(CUR)) || (CUR == '_')))
D
Daniel Veillard 已提交
525 526 527
        NEXT;
    ret = strndup(base, ctxt->cur - base);
    if (ret == NULL) {
528
        virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocating configuration"),
D
Daniel Veillard 已提交
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
	             ctxt->line);
        return(NULL);
    }
    return(ret);
}

/**
 * virConfParseComment:
 * @ctxt: the parsing context
 *
 * Parse one standalone comment in the configuration file
 *
 * Returns 0 in case of success and -1 in case of error
 */
static int
virConfParseComment(virConfParserCtxtPtr ctxt)
{
    const char *base;
    char *comm;

    if (CUR != '#')
        return(-1);
    NEXT;
    base = ctxt->cur;
    while ((ctxt->cur < ctxt->end) && (!IS_EOL(CUR))) NEXT;
    comm = strndup(base, ctxt->cur - base);
    if (comm == NULL) {
556
        virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocating configuration"),
D
Daniel Veillard 已提交
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
	             ctxt->line);
        return(-1);
    }
    virConfAddEntry(ctxt->conf, NULL, NULL, comm);
    return(0);
}

/**
 * virConfParseSeparator:
 * @ctxt: the parsing context
 *
 * Parse one separator between statement if not at the end.
 *
 * Returns 0 in case of success and -1 in case of error
 */
static int
virConfParseSeparator(virConfParserCtxtPtr ctxt)
{
    SKIP_SPACES;
    if (ctxt->cur >= ctxt->end)
	return(0);
    if (IS_EOL(CUR)) {
	SKIP_BLANKS
    } else if (CUR == ';') {
	NEXT;
	SKIP_BLANKS;
    } else {
584
        virConfError(NULL, VIR_ERR_CONF_SYNTAX, _("expecting a separator"),
D
Daniel Veillard 已提交
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
		     ctxt->line);
	return(-1);
    }
    return(0);
}

/**
 * virConfParseStatement:
 * @ctxt: the parsing context
 *
 * Parse one statement in the conf file
 *
 * Returns 0 in case of success and -1 in case of error
 */
static int
virConfParseStatement(virConfParserCtxtPtr ctxt)
{
    const char *base;
    char *name;
    virConfValuePtr value;
    char *comm = NULL;

    SKIP_BLANKS;
    if (CUR == '#') {
        return(virConfParseComment(ctxt));
    }
    name = virConfParseName(ctxt);
    if (name == NULL)
        return(-1);
    SKIP_SPACES;
    if (CUR != '=') {
616
        virConfError(NULL, VIR_ERR_CONF_SYNTAX, _("expecting an assignment"),
D
Daniel Veillard 已提交
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
	             ctxt->line);
        return(-1);
    }
    NEXT;
    SKIP_SPACES;
    value = virConfParseValue(ctxt);
    if (value == NULL) {
        free(name);
	return(-1);
    }
    SKIP_SPACES;
    if (CUR == '#') {
	NEXT;
	base = ctxt->cur;
	while ((ctxt->cur < ctxt->end) && (!IS_EOL(CUR))) NEXT;
	comm = strndup(base, ctxt->cur - base);
	if (comm == NULL) {
634
	    virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocating configuration"),
D
Daniel Veillard 已提交
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
	                 ctxt->line);
	    free(name);
	    virConfFreeValue(value);
	    return(-1);
	}
    }
    if (virConfAddEntry(ctxt->conf, name, value, comm) == NULL) {
        free(name);
	virConfFreeValue(value);
	if (comm != NULL)
	    free(comm);
	return(-1);
    }
    return(0);
}

/**
 * virConfParse:
 * @filename: the name to report errors
 * @content: the configuration content in memory
 * @len: the length in bytes
 *
 * Parse the subset of the Python language needed to handle simple
 * Xen configuration files.
 *
 * Returns an handle to lookup settings or NULL if it failed to
 *         read or parse the file, use virConfFree() to free the data.
 */
static virConfPtr
virConfParse(const char *filename, const char *content, int len) {
    virConfParserCtxt ctxt;

    ctxt.filename = filename;
    ctxt.base = ctxt.cur = content;
    ctxt.end = content + len - 1;
    ctxt.line = 1;

    ctxt.conf = virConfCreate(filename);
    if (ctxt.conf == NULL)
        return(NULL);

    while (ctxt.cur < ctxt.end) {
        if (virConfParseStatement(&ctxt) < 0)
	    goto error;
	if (virConfParseSeparator(&ctxt) < 0)
	    goto error;
    }

    return(ctxt.conf);

error:
    virConfFree(ctxt.conf);
    return(NULL);
}

/************************************************************************
 *									*
 *			The module entry points				*
 *									*
 ************************************************************************/

/**
697
 * __virConfReadFile:
D
Daniel Veillard 已提交
698 699 700 701 702 703 704 705
 * @filename: the path to the configuration file.
 *
 * Reads a configuration file.
 *
 * Returns an handle to lookup settings or NULL if it failed to
 *         read or parse the file, use virConfFree() to free the data.
 */
virConfPtr
706
__virConfReadFile(const char *filename)
D
Daniel Veillard 已提交
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
{
    char content[4096];
    int fd;
    int len;

    if (filename == NULL) {
        virConfError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__, 0);
        return(NULL);
    }
    fd = open(filename, O_RDONLY);
    if (fd < 0) {
        virConfError(NULL, VIR_ERR_OPEN_FAILED, filename, 0);
	return(NULL);
    }
    len = read(fd, content, sizeof(content));
    close(fd);
    if (len <= 0) {
        virConfError(NULL, VIR_ERR_READ_FAILED, filename, 0);
	return(NULL);
    }
    return(virConfParse(filename, content, len));
}

/**
731
 * __virConfReadMem:
D
Daniel Veillard 已提交
732 733 734 735 736 737 738 739 740 741
 * @memory: pointer to the content of the configuration file
 * @len: lenght in byte
 *
 * Reads a configuration file loaded in memory. The string can be
 * zero terminated in which case @len can be 0
 *
 * Returns an handle to lookup settings or NULL if it failed to
 *         parse the content, use virConfFree() to free the data.
 */
virConfPtr
742
__virConfReadMem(const char *memory, int len)
D
Daniel Veillard 已提交
743 744 745 746 747 748 749 750 751 752 753 754
{
    if ((memory == NULL) || (len < 0)) {
        virConfError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__, 0);
        return(NULL);
    }
    if (len == 0)
        len = strlen(memory);

    return(virConfParse("memory conf", memory, len));
}

/**
755
 * __virConfFree:
D
Daniel Veillard 已提交
756 757 758 759 760 761 762
 * @conf: a configuration file handle
 *
 * Frees all data associated to the handle
 *
 * Returns 0 in case of success, -1 in case of error.
 */
int
763
__virConfFree(virConfPtr conf)
D
Daniel Veillard 已提交
764
{
765
    virConfEntryPtr tmp;
D
Daniel Veillard 已提交
766 767
    if (conf == NULL) {
        virConfError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__, 0);
768 769 770 771 772 773 774 775 776 777 778 779 780
        return(-1);
    }

    tmp = conf->entries;
    while (tmp) {
        virConfEntryPtr next;
        free(tmp->name);
        virConfFreeValue(tmp->value);
        if (tmp->comment)
            free(tmp->comment);
        next = tmp->next;
        free(tmp);
        tmp = next;
D
Daniel Veillard 已提交
781 782 783 784 785 786
    }
    free(conf);
    return(0);
}

/**
787
 * __virConfGetValue:
D
Daniel Veillard 已提交
788 789 790 791 792 793 794 795 796
 * @conf: a configuration file handle
 * @entry: the name of the entry
 *
 * Lookup the value associated to this entry in the configuration file
 *
 * Returns a pointer to the value or NULL if the lookup failed, the data 
 *         associated will be freed when virConfFree() is called
 */
virConfValuePtr
797
__virConfGetValue(virConfPtr conf, const char *setting)
D
Daniel Veillard 已提交
798
{
799 800 801 802 803 804 805 806 807
    virConfEntryPtr cur;

    cur = conf->entries;
    while (cur != NULL) {
        if ((cur->name != NULL) && (!strcmp(cur->name, setting)))
	    return(cur->value);
        cur = cur->next;
    }
    return(NULL);
D
Daniel Veillard 已提交
808 809
}

810
/**
811
 * __virConfSetValue:
812 813 814 815 816 817 818 819 820 821 822
 * @conf: a configuration file handle
 * @entry: the name of the entry
 * @value: the new configuration value
 *
 * Set (or replace) the value associated to this entry in the configuration
 * file. The passed in 'value' will be owned by the conf object upon return
 * of this method, even in case of error. It should not be referenced again
 * by the caller.
 *
 * Returns 0 on success, or -1 on failure.
 */
823 824 825 826 827
int
__virConfSetValue (virConfPtr conf,
                  const char *setting,
                  virConfValuePtr value)
{
828 829 830 831 832 833 834 835 836 837
    virConfEntryPtr cur, prev = NULL;

    cur = conf->entries;
    while (cur != NULL) {
        if ((cur->name != NULL) && (!strcmp(cur->name, setting))) {
            break;
        }
        prev = cur;
        cur = cur->next;
    }
838

839
    if (!cur) {
840
        if (!(cur = malloc(sizeof(*cur)))) {
841 842 843 844 845 846 847 848 849 850 851
            virConfFreeValue(value);
            return (-1);
        }
        cur->comment = NULL;
        if (!(cur->name = strdup(setting))) {
            virConfFreeValue(value);
            free(cur);
            return (-1);
        }
        cur->value = value;
        if (prev) {
852
            cur->next = prev->next;
853 854
            prev->next = cur;
        } else {
855
            cur->next = conf->entries;
856 857 858 859 860 861 862 863 864 865 866 867
            conf->entries = cur;
        }
    } else {
        if (cur->value) {
            virConfFreeValue(cur->value);
        }
        cur->value = value;
    }
    return (0);
}


D
Daniel Veillard 已提交
868
/**
869
 * __virConfWriteFile:
D
Daniel Veillard 已提交
870 871 872 873 874 875 876 877
 * @filename: the path to the configuration file.
 * @conf: the conf
 *
 * Writes a configuration file back to a file.
 *
 * Returns the number of bytes written or -1 in case of error.
 */
int
878
__virConfWriteFile(const char *filename, virConfPtr conf)
D
Daniel Veillard 已提交
879 880 881 882 883 884 885 886 887 888
{
    virBufferPtr buf;
    virConfEntryPtr cur;
    int ret;
    int fd;

    if (conf == NULL)
        return(-1);

    buf = virBufferNew(500);
889 890
    if (buf == NULL) {
        virConfError(NULL, VIR_ERR_NO_MEMORY, _("failed to allocate buffer"), 0);
D
Daniel Veillard 已提交
891
        return(-1);
892
    }
D
Daniel Veillard 已提交
893 894 895 896 897 898 899 900 901

    cur = conf->entries;
    while (cur != NULL) {
        virConfSaveEntry(buf, cur);
	cur = cur->next;
    }
    
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR );
    if (fd < 0) {
902
        virConfError(NULL, VIR_ERR_WRITE_FAILED, _("failed to open file"), 0);
D
Daniel Veillard 已提交
903 904 905 906 907 908 909
        ret = -1;
	goto error;
    }

    ret = write(fd, buf->content, buf->use);
    close(fd);
    if (ret != (int) buf->use) {
910
        virConfError(NULL, VIR_ERR_WRITE_FAILED, _("failed to save content"), 0);
D
Daniel Veillard 已提交
911 912 913 914 915 916 917 918 919
        ret = -1;
	goto error;
    }
error:
    virBufferFree(buf);
    return(ret);
}

/**
920
 * __virConfWriteMem:
D
Daniel Veillard 已提交
921
 * @memory: pointer to the memory to store the config file
922
 * @len: pointer to the length in bytes of the store, on output the size
D
Daniel Veillard 已提交
923 924 925 926 927 928 929 930 931 932
 * @conf: the conf
 *
 * Writes a configuration file back to a memory area. @len is an IN/OUT
 * parameter, it indicates the size available in bytes, and on output the
 * size required for the configuration file (even if the call fails due to
 * insufficient space).
 *
 * Returns the number of bytes written or -1 in case of error.
 */
int
933
__virConfWriteMem(char *memory, int *len, virConfPtr conf)
D
Daniel Veillard 已提交
934 935 936 937 938 939 940 941 942
{
    virBufferPtr buf;
    virConfEntryPtr cur;
    int ret;

    if ((memory == NULL) || (len == NULL) || (*len <= 0) || (conf == NULL))
        return(-1);

    buf = virBufferNew(500);
943 944
    if (buf == NULL) {
        virConfError(NULL, VIR_ERR_NO_MEMORY, _("failed to allocate buffer"), 0);
D
Daniel Veillard 已提交
945
        return(-1);
946
    }
D
Daniel Veillard 已提交
947 948 949 950

    cur = conf->entries;
    while (cur != NULL) {
        virConfSaveEntry(buf, cur);
951
        cur = cur->next;
D
Daniel Veillard 已提交
952 953 954 955
    }
    
    if ((int) buf->use >= *len) {
        *len = buf->use;
956 957
        ret = -1;
        goto error;
D
Daniel Veillard 已提交
958 959 960
    }
    memcpy(memory, buf->content, buf->use);
    ret = buf->use;
961
    *len = buf->use;
D
Daniel Veillard 已提交
962 963 964 965
error:
    virBufferFree(buf);
    return(ret);
}
966 967 968 969 970 971 972 973 974 975


/*
 * Local variables:
 *  indent-tabs-mode: nil
 *  c-indent-level: 4
 *  c-basic-offset: 4
 *  tab-width: 4
 * End:
 */