storage_conf.c 40.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/*
 * storage_conf.c: config handling for storage driver
 *
 * Copyright (C) 2006-2008 Red Hat, Inc.
 * Copyright (C) 2006-2008 Daniel P. Berrange
 *
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <libxml/uri.h>

#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>

41
#include "internal.h"
42 43 44 45 46 47
#include "storage_conf.h"
#include "storage_backend.h"
#include "xml.h"
#include "uuid.h"
#include "buf.h"
#include "util.h"
48
#include "memory.h"
49 50 51 52 53 54

#define virStorageLog(msg...) fprintf(stderr, msg)

void
virStorageVolDefFree(virStorageVolDefPtr def) {
    int i;
55 56 57 58

    if (!def)
        return;

59 60
    VIR_FREE(def->name);
    VIR_FREE(def->key);
61 62

    for (i = 0 ; i < def->source.nextent ; i++) {
63
        VIR_FREE(def->source.extents[i].path);
64
    }
65
    VIR_FREE(def->source.extents);
66

67 68 69
    VIR_FREE(def->target.path);
    VIR_FREE(def->target.perms.label);
    VIR_FREE(def);
70 71 72
}

void
73
virStoragePoolSourceFree(virStoragePoolSourcePtr source) {
74 75
    int i;

76
    if (!source)
77 78
        return;

79 80 81 82
    VIR_FREE(source->host.name);
    for (i = 0 ; i < source->ndevice ; i++) {
        VIR_FREE(source->devices[i].freeExtents);
        VIR_FREE(source->devices[i].path);
83
    }
84 85 86
    VIR_FREE(source->devices);
    VIR_FREE(source->dir);
    VIR_FREE(source->name);
87

88 89 90
    if (source->authType == VIR_STORAGE_POOL_AUTH_CHAP) {
        VIR_FREE(source->auth.chap.login);
        VIR_FREE(source->auth.chap.passwd);
91
    }
92 93 94 95 96 97 98 99 100 101
}

void
virStoragePoolDefFree(virStoragePoolDefPtr def) {
    if (!def)
        return;

    VIR_FREE(def->name);

    virStoragePoolSourceFree(&def->source);
102

103 104 105
    VIR_FREE(def->target.path);
    VIR_FREE(def->target.perms.label);
    VIR_FREE(def);
106 107 108 109 110
}


void
virStoragePoolObjFree(virStoragePoolObjPtr obj) {
111 112 113 114 115
    if (!obj)
        return;

    virStoragePoolDefFree(obj->def);
    virStoragePoolDefFree(obj->newDef);
116

117 118 119
    VIR_FREE(obj->configFile);
    VIR_FREE(obj->autostartLink);
    VIR_FREE(obj);
120 121
}

122 123 124 125 126 127 128 129 130
void virStoragePoolObjListFree(virStoragePoolObjListPtr pools)
{
    unsigned int i;
    for (i = 0 ; i < pools->count ; i++)
        virStoragePoolObjFree(pools->objs[i]);
    VIR_FREE(pools->objs);
    pools->count = 0;
}

131
void
132
virStoragePoolObjRemove(virStoragePoolObjListPtr pools,
133 134
                        virStoragePoolObjPtr pool)
{
135
    unsigned int i;
136

137 138 139
    for (i = 0 ; i < pools->count ; i++) {
        if (pools->objs[i] == pool) {
            virStoragePoolObjFree(pools->objs[i]);
140

141 142 143
            if (i < (pools->count - 1))
                memmove(pools->objs + i, pools->objs + i + 1,
                        sizeof(*(pools->objs)) * (pools->count - (i + 1)));
144

145 146 147 148
            if (VIR_REALLOC_N(pools->objs, pools->count - 1) < 0) {
                ; /* Failure to reduce memory allocation isn't fatal */
            }
            pools->count--;
149

150 151 152
            break;
        }
    }
153 154 155 156 157 158 159
}


static int
virStoragePoolDefParseAuthChap(virConnectPtr conn,
                               xmlXPathContextPtr ctxt,
                               virStoragePoolAuthChapPtr auth) {
160
    auth->login = virXPathString(conn, "string(/pool/source/auth/@login)", ctxt);
161 162
    if (auth->login == NULL) {
        virStorageReportError(conn, VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
163
                              "%s", _("missing auth host attribute"));
164 165 166
        return -1;
    }

167
    auth->passwd = virXPathString(conn, "string(/pool/source/auth/@passwd)", ctxt);
168 169
    if (auth->passwd == NULL) {
        virStorageReportError(conn, VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
170
                              "%s", _("missing auth passwd attribute"));
171 172 173 174 175 176 177 178 179 180 181 182 183 184
        return -1;
    }

    return 0;
}


static int
virStoragePoolDefParsePerms(virConnectPtr conn,
                            xmlXPathContextPtr ctxt,
                            virStoragePermsPtr perms) {
    char *mode;
    long v;

185
    mode = virXPathString(conn, "string(/pool/permissions/mode)", ctxt);
186 187 188 189 190 191 192
    if (!mode) {
        perms->mode = 0700;
    } else {
        char *end;
        perms->mode = strtol(mode, &end, 8);
        if (*end || perms->mode < 0 || perms->mode > 0777) {
            virStorageReportError(conn, VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
193
                                  "%s", _("malformed octal mode"));
194 195 196 197
            return -1;
        }
    }

198
    if (virXPathNode(conn, "/pool/permissions/owner", ctxt) == NULL) {
199 200
        perms->uid = getuid();
    } else {
201
        if (virXPathLong(conn, "number(/pool/permissions/owner)", ctxt, &v) < 0) {
202
            virStorageReportError(conn, VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
203
                                  "%s", _("malformed owner element"));
204 205 206 207 208
            return -1;
        }
        perms->uid = (int)v;
    }

209
    if (virXPathNode(conn, "/pool/permissions/group", ctxt) == NULL) {
D
Daniel P. Berrange 已提交
210
        perms->gid = getgid();
211
    } else {
212
        if (virXPathLong(conn, "number(/pool/permissions/group)", ctxt, &v) < 0) {
213
            virStorageReportError(conn, VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
214
                                  "%s", _("malformed group element"));
215 216 217 218 219 220
            return -1;
        }
        perms->gid = (int)v;
    }

    /* NB, we're ignoring missing labels here - they'll simply inherit */
221
    perms->label = virXPathString(conn, "string(/pool/permissions/label)", ctxt);
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236

    return 0;
}


static virStoragePoolDefPtr
virStoragePoolDefParseDoc(virConnectPtr conn,
                          xmlXPathContextPtr ctxt,
                          xmlNodePtr root) {
    virStorageBackendPoolOptionsPtr options;
    virStoragePoolDefPtr ret;
    xmlChar *type = NULL;
    char *uuid = NULL;
    char *authType = NULL;

237 238 239
    if (VIR_ALLOC(ret) < 0) {
        virStorageReportError(conn, VIR_ERR_NO_MEMORY,
                              "%s", _("cannot allocate storage pool"));
240
        return NULL;
241
    }
242 243 244

    if (STRNEQ((const char *)root->name, "pool")) {
        virStorageReportError(conn, VIR_ERR_XML_ERROR,
245
                          "%s", _("unknown root elementi for storage pool"));
246 247 248 249 250 251 252 253 254 255 256 257 258
        goto cleanup;
    }

    type = xmlGetProp(root, BAD_CAST "type");
    if ((ret->type = virStorageBackendFromString((const char *)type)) < 0)
        goto cleanup;
    xmlFree(type);
    type = NULL;

    if ((options = virStorageBackendPoolOptionsForType(ret->type)) == NULL) {
        goto cleanup;
    }

259 260 261 262 263
    ret->name = virXPathString(conn, "string(/pool/name)", ctxt);
    if (ret->name == NULL &&
        options->flags & VIR_STORAGE_BACKEND_POOL_SOURCE_NAME)
        ret->name = virXPathString(conn, "string(/pool/source/name)", ctxt);
    if (ret->name == NULL) {
264
        virStorageReportError(conn, VIR_ERR_XML_ERROR,
265
                              "%s", _("missing pool source name element"));
266 267 268
        goto cleanup;
    }

269
    uuid = virXPathString(conn, "string(/pool/uuid)", ctxt);
270 271 272
    if (uuid == NULL) {
        if (virUUIDGenerate(ret->uuid) < 0) {
            virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
273
                                  "%s", _("unable to generate uuid"));
274 275 276 277 278
            goto cleanup;
        }
    } else {
        if (virUUIDParse(uuid, ret->uuid) < 0) {
            virStorageReportError(conn, VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
279
                                  "%s", _("malformed uuid element"));
280 281
            goto cleanup;
        }
282
        VIR_FREE(uuid);
283 284 285
    }

    if (options->formatFromString) {
286
        char *format = virXPathString(conn, "string(/pool/source/format/@type)", ctxt);
287 288 289 290 291 292
        if (format == NULL)
            ret->source.format = options->defaultFormat;
        else
            ret->source.format = options->formatFromString(format);

        if (ret->source.format < 0) {
293 294
            virStorageReportError(conn, VIR_ERR_XML_ERROR,
                                  _("unknown pool format type %s"), format);
295
            VIR_FREE(format);
296 297
            goto cleanup;
        }
298
        VIR_FREE(format);
299 300 301
    }

    if (options->flags & VIR_STORAGE_BACKEND_POOL_SOURCE_HOST) {
302
        if ((ret->source.host.name = virXPathString(conn, "string(/pool/source/host/@name)", ctxt)) == NULL) {
303
            virStorageReportError(conn, VIR_ERR_XML_ERROR,
304
                             "%s", _("missing storage pool source host name"));
305 306 307 308 309 310 311
            goto cleanup;
        }
    }
    if (options->flags & VIR_STORAGE_BACKEND_POOL_SOURCE_DEVICE) {
        xmlNodePtr *nodeset = NULL;
        int nsource, i;

312
        if ((nsource = virXPathNodeSet(conn, "/pool/source/device", ctxt, &nodeset)) < 0) {
313
            virStorageReportError(conn, VIR_ERR_XML_ERROR,
314
                        "%s", _("cannot extract storage pool source devices"));
315 316
            goto cleanup;
        }
317 318
        if (VIR_ALLOC_N(ret->source.devices, nsource) < 0) {
            VIR_FREE(nodeset);
J
Jim Meyering 已提交
319
            virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("device"));
320 321 322 323 324
            goto cleanup;
        }
        for (i = 0 ; i < nsource ; i++) {
            xmlChar *path = xmlGetProp(nodeset[i], BAD_CAST "path");
            if (path == NULL) {
325
                VIR_FREE(nodeset);
326
                virStorageReportError(conn, VIR_ERR_XML_ERROR,
327
                        "%s", _("missing storage pool source device path"));
328 329 330 331
                goto cleanup;
            }
            ret->source.devices[i].path = (char *)path;
        }
332
        VIR_FREE(nodeset);
333 334 335
        ret->source.ndevice = nsource;
    }
    if (options->flags & VIR_STORAGE_BACKEND_POOL_SOURCE_DIR) {
336
        if ((ret->source.dir = virXPathString(conn, "string(/pool/source/dir/@path)", ctxt)) == NULL) {
337
            virStorageReportError(conn, VIR_ERR_XML_ERROR,
338
                                "%s", _("missing storage pool source path"));
339 340 341
            goto cleanup;
        }
    }
342 343 344 345 346 347
    if (options->flags & VIR_STORAGE_BACKEND_POOL_SOURCE_NAME) {
        ret->source.name = virXPathString(conn, "string(/pool/source/name)",
                                          ctxt);
        if (ret->source.name == NULL) {
            /* source name defaults to pool name */
            ret->source.name = strdup(ret->name);
348 349 350 351 352
            if (ret->source.name == NULL) {
                virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s",
                                      _("pool name"));
                goto cleanup;
            }
353 354
        }
    }
355 356


357
    authType = virXPathString(conn, "string(/pool/source/auth/@type)", ctxt);
358 359 360 361 362 363 364 365 366
    if (authType == NULL) {
        ret->source.authType = VIR_STORAGE_POOL_AUTH_NONE;
    } else {
        if (STREQ(authType, "chap")) {
            ret->source.authType = VIR_STORAGE_POOL_AUTH_CHAP;
        } else {
            virStorageReportError(conn, VIR_ERR_XML_ERROR,
                                  _("unknown auth type '%s'"),
                                  (const char *)authType);
367
            VIR_FREE(authType);
368 369
            goto cleanup;
        }
370
        VIR_FREE(authType);
371 372 373 374 375 376 377
    }

    if (ret->source.authType == VIR_STORAGE_POOL_AUTH_CHAP) {
        if (virStoragePoolDefParseAuthChap(conn, ctxt, &ret->source.auth.chap) < 0)
            goto cleanup;
    }

378
    if ((ret->target.path = virXPathString(conn, "string(/pool/target/path)", ctxt)) == NULL) {
379
        virStorageReportError(conn, VIR_ERR_XML_ERROR,
380
                              "%s", _("missing storage pool target path"));
381 382 383 384 385 386 387 388 389
        goto cleanup;
    }

    if (virStoragePoolDefParsePerms(conn, ctxt, &ret->target.perms) < 0)
        goto cleanup;

    return ret;

 cleanup:
390
    VIR_FREE(uuid);
391
    xmlFree(type);
392 393 394 395
    virStoragePoolDefFree(ret);
    return NULL;
}

396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
/* Called from SAX on parsing errors in the XML. */
static void
catchXMLError (void *ctx, const char *msg ATTRIBUTE_UNUSED, ...)
{
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;

    if (ctxt) {
        virConnectPtr conn = ctxt->_private;

        if (conn &&
            conn->err.code == VIR_ERR_NONE &&
            ctxt->lastError.level == XML_ERR_FATAL &&
            ctxt->lastError.message != NULL) {
            virStorageReportError (conn, VIR_ERR_XML_DETAIL,
                                   _("at line %d: %s"),
                                   ctxt->lastError.line,
                                   ctxt->lastError.message);
        }
    }
}

417 418 419 420 421
virStoragePoolDefPtr
virStoragePoolDefParse(virConnectPtr conn,
                       const char *xmlStr,
                       const char *filename) {
    virStoragePoolDefPtr ret = NULL;
422
    xmlParserCtxtPtr pctxt;
423 424 425 426
    xmlDocPtr xml = NULL;
    xmlNodePtr node = NULL;
    xmlXPathContextPtr ctxt = NULL;

427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
    /* Set up a parser context so we can catch the details of XML errors. */
    pctxt = xmlNewParserCtxt ();
    if (!pctxt || !pctxt->sax)
        goto cleanup;
    pctxt->sax->error = catchXMLError;
    pctxt->_private = conn;

    if (conn) virResetError (&conn->err);
    xml = xmlCtxtReadDoc (pctxt, BAD_CAST xmlStr,
                          filename ? filename : "storage.xml", NULL,
                          XML_PARSE_NOENT | XML_PARSE_NONET |
                          XML_PARSE_NOWARNING);
    if (!xml) {
        if (conn && conn->err.code == VIR_ERR_NONE)
              virStorageReportError(conn, VIR_ERR_XML_ERROR,
D
Daniel P. Berrange 已提交
442
                                    "%s",_("failed to parse xml document"));
443 444 445 446 447 448
        goto cleanup;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
        virStorageReportError(conn, VIR_ERR_NO_MEMORY,
J
Jim Meyering 已提交
449
                              "%s", _("xmlXPathContext"));
450 451 452 453 454 455
        goto cleanup;
    }

    node = xmlDocGetRootElement(xml);
    if (node == NULL) {
        virStorageReportError(conn, VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
456
                              "%s", _("missing root element"));
457 458 459 460 461
        goto cleanup;
    }

    ret = virStoragePoolDefParseDoc(conn, ctxt, node);

462
    xmlFreeParserCtxt (pctxt);
463 464 465 466 467 468
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(xml);

    return ret;

 cleanup:
469
    xmlFreeParserCtxt (pctxt);
470 471 472 473 474
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(xml);
    return NULL;
}

475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 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 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
static int
virStoragePoolSourceFormat(virConnectPtr conn,
                           virBufferPtr buf,
                           virStorageBackendPoolOptionsPtr options,
                           virStoragePoolSourcePtr src)
{
    int i, j;

    virBufferAddLit(buf,"  <source>\n");
    if ((options->flags & VIR_STORAGE_BACKEND_POOL_SOURCE_HOST) &&
        src->host.name)
        virBufferVSprintf(buf,"    <host name='%s'/>\n", src->host.name);

    if ((options->flags & VIR_STORAGE_BACKEND_POOL_SOURCE_DEVICE) &&
        src->ndevice) {
        for (i = 0 ; i < src->ndevice ; i++) {
            if (src->devices[i].nfreeExtent) {
                virBufferVSprintf(buf,"    <device path='%s'>\n",
                                  src->devices[i].path);
                for (j = 0 ; j < src->devices[i].nfreeExtent ; j++) {
                    virBufferVSprintf(buf, "    <freeExtent start='%llu' end='%llu'/>\n",
                                      src->devices[i].freeExtents[j].start,
                                      src->devices[i].freeExtents[j].end);
                }
                virBufferAddLit(buf,"    </device>\n");
            }
            else
                virBufferVSprintf(buf, "    <device path='%s'/>\n",
                                  src->devices[i].path);
        }
    }
    if ((options->flags & VIR_STORAGE_BACKEND_POOL_SOURCE_DIR) &&
        src->dir)
        virBufferVSprintf(buf,"    <dir path='%s'/>\n", src->dir);
    if ((options->flags & VIR_STORAGE_BACKEND_POOL_SOURCE_ADAPTER) &&
        src->adapter)
        virBufferVSprintf(buf,"    <adapter name='%s'/>\n", src->adapter);
    if ((options->flags & VIR_STORAGE_BACKEND_POOL_SOURCE_NAME) &&
        src->name)
        virBufferVSprintf(buf,"    <name>%s</name>\n", src->name);

    if (options->formatToString) {
        const char *format = (options->formatToString)(src->format);
        if (!format) {
            virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
                                  _("unknown pool format number %d"),
                                  src->format);
            return -1;
        }
        virBufferVSprintf(buf,"    <format type='%s'/>\n", format);
    }


    if (src->authType == VIR_STORAGE_POOL_AUTH_CHAP)
        virBufferVSprintf(buf,"    <auth type='chap' login='%s' passwd='%s'>\n",
                          src->auth.chap.login,
                          src->auth.chap.passwd);
    virBufferAddLit(buf,"  </source>\n");

    return 0;
}

537 538 539 540 541

char *
virStoragePoolDefFormat(virConnectPtr conn,
                        virStoragePoolDefPtr def) {
    virStorageBackendPoolOptionsPtr options;
542
    virBuffer buf = VIR_BUFFER_INITIALIZER;
543 544 545 546 547 548 549 550 551 552
    const char *type;
    char uuid[VIR_UUID_STRING_BUFLEN];

    options = virStorageBackendPoolOptionsForType(def->type);
    if (options == NULL)
        return NULL;

    type = virStorageBackendToString(def->type);
    if (!type) {
        virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
553
                              "%s", _("unexpected pool type"));
554 555
        goto cleanup;
    }
556 557
    virBufferVSprintf(&buf, "<pool type='%s'>\n", type);
    virBufferVSprintf(&buf,"  <name>%s</name>\n", def->name);
558 559

    virUUIDFormat(def->uuid, uuid);
560
    virBufferVSprintf(&buf,"  <uuid>%s</uuid>\n", uuid);
561

562 563 564 565 566 567
    virBufferVSprintf(&buf,"  <capacity>%llu</capacity>\n",
                      def->capacity);
    virBufferVSprintf(&buf,"  <allocation>%llu</allocation>\n",
                      def->allocation);
    virBufferVSprintf(&buf,"  <available>%llu</available>\n",
                      def->available);
568

569 570
    if (virStoragePoolSourceFormat(conn, &buf, options, &def->source) < 0)
        goto cleanup;
571

572
    virBufferAddLit(&buf,"  <target>\n");
573

574 575
    if (def->target.path)
        virBufferVSprintf(&buf,"    <path>%s</path>\n", def->target.path);
576

577 578 579 580 581 582 583
    virBufferAddLit(&buf,"    <permissions>\n");
    virBufferVSprintf(&buf,"      <mode>0%o</mode>\n",
                      def->target.perms.mode);
    virBufferVSprintf(&buf,"      <owner>%d</owner>\n",
                      def->target.perms.uid);
    virBufferVSprintf(&buf,"      <group>%d</group>\n",
                      def->target.perms.gid);
584

585 586 587
    if (def->target.perms.label)
        virBufferVSprintf(&buf,"      <label>%s</label>\n",
                          def->target.perms.label);
588

589 590 591
    virBufferAddLit(&buf,"    </permissions>\n");
    virBufferAddLit(&buf,"  </target>\n");
    virBufferAddLit(&buf,"</pool>\n");
592

593
    if (virBufferError(&buf))
594 595
        goto no_memory;

596
    return virBufferContentAndReset(&buf);
597 598

 no_memory:
J
Jim Meyering 已提交
599
    virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("xml"));
600
 cleanup:
601
    free(virBufferContentAndReset(&buf));
602 603 604 605 606 607 608 609 610 611 612
    return NULL;
}


static int
virStorageVolDefParsePerms(virConnectPtr conn,
                           xmlXPathContextPtr ctxt,
                           virStoragePermsPtr perms) {
    char *mode;
    long v;

613
    mode = virXPathString(conn, "string(/volume/permissions/mode)", ctxt);
614 615 616 617 618 619 620
    if (!mode) {
        perms->mode = 0600;
    } else {
        char *end = NULL;
        perms->mode = strtol(mode, &end, 8);
        if (*end || perms->mode < 0 || perms->mode > 0777) {
            virStorageReportError(conn, VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
621
                                  "%s", _("malformed octal mode"));
622 623 624 625
            return -1;
        }
    }

626
    if (virXPathNode(conn, "/volume/permissions/owner", ctxt) == NULL) {
627 628
        perms->uid = getuid();
    } else {
629
        if (virXPathLong(conn, "number(/volume/permissions/owner)", ctxt, &v) < 0) {
630
            virStorageReportError(conn, VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
631
                                  "%s", _("missing owner element"));
632 633 634 635
            return -1;
        }
        perms->uid = (int)v;
    }
636
    if (virXPathNode(conn, "/volume/permissions/group", ctxt) == NULL) {
637 638
        perms->gid = getgid();
    } else {
639
        if (virXPathLong(conn, "number(/volume/permissions/group)", ctxt, &v) < 0) {
640
            virStorageReportError(conn, VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
641
                                  "%s", _("missing owner element"));
642 643 644 645 646 647
            return -1;
        }
        perms->gid = (int)v;
    }

    /* NB, we're ignoring missing labels here - they'll simply inherit */
648
    perms->label = virXPathString(conn, "string(/volume/permissions/label)", ctxt);
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 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711

    return 0;
}


static int
virStorageSize(virConnectPtr conn,
               const char *unit,
               const char *val,
               unsigned long long *ret) {
    unsigned long long mult;
    char *end;

    if (!unit) {
        mult = 1;
    } else {
        switch (unit[0]) {
        case 'k':
        case 'K':
            mult = 1024ull;
            break;

        case 'm':
        case 'M':
            mult = 1024ull * 1024ull;
            break;

        case 'g':
        case 'G':
            mult = 1024ull * 1024ull * 1024ull;
            break;

        case 't':
        case 'T':
            mult = 1024ull * 1024ull * 1024ull * 1024ull;
            break;

        case 'p':
        case 'P':
            mult = 1024ull * 1024ull * 1024ull * 1024ull * 1024ull;
            break;

        case 'y':
        case 'Y':
            mult = 1024ull * 1024ull * 1024ull * 1024ull * 1024ull *
                1024ull;
            break;

        case 'z':
        case 'Z':
            mult = 1024ull * 1024ull * 1024ull * 1024ull * 1024ull *
                1024ull * 1024ull;
            break;

        default:
            virStorageReportError(conn, VIR_ERR_XML_ERROR,
                                  _("unknown size units '%s'"), unit);
            return -1;
        }
    }

    if (virStrToLong_ull (val, &end, 10, ret) < 0) {
        virStorageReportError(conn, VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
712
                              "%s", _("malformed capacity element"));
713 714 715 716
        return -1;
    }
    if (*ret > (ULLONG_MAX / mult)) {
        virStorageReportError(conn, VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
717
                              "%s", _("capacity element value too large"));
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
            return -1;
    }

    *ret *= mult;

    return 0;
}

static virStorageVolDefPtr
virStorageVolDefParseDoc(virConnectPtr conn,
                         virStoragePoolDefPtr pool,
                         xmlXPathContextPtr ctxt,
                         xmlNodePtr root) {
    virStorageVolDefPtr ret;
    virStorageBackendVolOptionsPtr options;
    char *allocation = NULL;
    char *capacity = NULL;
    char *unit = NULL;

    options = virStorageBackendVolOptionsForType(pool->type);
    if (options == NULL)
        return NULL;

741 742 743
    if (VIR_ALLOC(ret) < 0) {
        virStorageReportError(conn, VIR_ERR_NO_MEMORY,
                              "%s", _("cannot allocate storage vol"));
744
        return NULL;
745
    }
746 747 748

    if (STRNEQ((const char *)root->name, "volume")) {
        virStorageReportError(conn, VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
749
                              "%s", _("unknown root element"));
750 751 752
        goto cleanup;
    }

753
    ret->name = virXPathString(conn, "string(/volume/name)", ctxt);
754 755
    if (ret->name == NULL) {
        virStorageReportError(conn, VIR_ERR_XML_ERROR,
756
                              "%s", _("missing volume name element"));
757 758 759
        goto cleanup;
    }

R
Richard W.M. Jones 已提交
760
    /* Auto-generated so deliberately ignore */
761
    /*ret->key = virXPathString(conn, "string(/volume/key)", ctxt);*/
762

763 764
    capacity = virXPathString(conn, "string(/volume/capacity)", ctxt);
    unit = virXPathString(conn, "string(/volume/capacity/@unit)", ctxt);
765 766
    if (capacity == NULL) {
        virStorageReportError(conn, VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
767
                              "%s", _("missing capacity element"));
768 769 770 771
        goto cleanup;
    }
    if (virStorageSize(conn, unit, capacity, &ret->capacity) < 0)
        goto cleanup;
772 773
    VIR_FREE(capacity);
    VIR_FREE(unit);
774

775
    allocation = virXPathString(conn, "string(/volume/allocation)", ctxt);
776
    if (allocation) {
777
        unit = virXPathString(conn, "string(/volume/allocation/@unit)", ctxt);
778 779
        if (virStorageSize(conn, unit, allocation, &ret->allocation) < 0)
            goto cleanup;
780 781
        VIR_FREE(allocation);
        VIR_FREE(unit);
782 783 784 785
    } else {
        ret->allocation = ret->capacity;
    }

786
    ret->target.path = virXPathString(conn, "string(/volume/target/path)", ctxt);
787
    if (options->formatFromString) {
788
        char *format = virXPathString(conn, "string(/volume/target/format/@type)", ctxt);
789
        if ((ret->target.format = (options->formatFromString)(format)) < 0) {
790 791
            virStorageReportError(conn, VIR_ERR_XML_ERROR,
                                  _("unknown volume format type %s"), format);
792
            VIR_FREE(format);
793 794
            goto cleanup;
        }
795
        VIR_FREE(format);
796 797 798 799 800 801 802 803
    }

    if (virStorageVolDefParsePerms(conn, ctxt, &ret->target.perms) < 0)
        goto cleanup;

    return ret;

 cleanup:
804 805 806
    VIR_FREE(allocation);
    VIR_FREE(capacity);
    VIR_FREE(unit);
807 808 809 810 811 812 813 814 815 816 817
    virStorageVolDefFree(ret);
    return NULL;
}


virStorageVolDefPtr
virStorageVolDefParse(virConnectPtr conn,
                      virStoragePoolDefPtr pool,
                      const char *xmlStr,
                      const char *filename) {
    virStorageVolDefPtr ret = NULL;
818
    xmlParserCtxtPtr pctxt;
819 820 821 822
    xmlDocPtr xml = NULL;
    xmlNodePtr node = NULL;
    xmlXPathContextPtr ctxt = NULL;

823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
    /* Set up a parser context so we can catch the details of XML errors. */
    pctxt = xmlNewParserCtxt ();
    if (!pctxt || !pctxt->sax)
        goto cleanup;
    pctxt->sax->error = catchXMLError;
    pctxt->_private = conn;

    if (conn) virResetError (&conn->err);
    xml = xmlCtxtReadDoc (pctxt, BAD_CAST xmlStr,
                          filename ? filename : "storage.xml", NULL,
                          XML_PARSE_NOENT | XML_PARSE_NONET |
                          XML_PARSE_NOWARNING);
    if (!xml) {
        if (conn && conn->err.code == VIR_ERR_NONE)
              virStorageReportError(conn, VIR_ERR_XML_ERROR,
D
Daniel P. Berrange 已提交
838
                                    "%s", _("failed to parse xml document"));
839 840 841 842 843 844
        goto cleanup;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
        virStorageReportError(conn, VIR_ERR_NO_MEMORY,
J
Jim Meyering 已提交
845
                              "%s", _("xmlXPathContext"));
846 847 848 849 850 851
        goto cleanup;
    }

    node = xmlDocGetRootElement(xml);
    if (node == NULL) {
        virStorageReportError(conn, VIR_ERR_XML_ERROR,
J
Jim Meyering 已提交
852
                              "%s", _("missing root element"));
853 854 855 856 857
        goto cleanup;
    }

    ret = virStorageVolDefParseDoc(conn, pool, ctxt, node);

858
    xmlFreeParserCtxt (pctxt);
859 860 861 862 863 864
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(xml);

    return ret;

 cleanup:
865
    xmlFreeParserCtxt (pctxt);
866 867 868 869 870 871 872 873 874 875 876 877
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(xml);
    return NULL;
}



char *
virStorageVolDefFormat(virConnectPtr conn,
                       virStoragePoolDefPtr pool,
                       virStorageVolDefPtr def) {
    virStorageBackendVolOptionsPtr options;
878
    virBuffer buf = VIR_BUFFER_INITIALIZER;
879
    char *tmp;
880 881 882 883 884

    options = virStorageBackendVolOptionsForType(pool->type);
    if (options == NULL)
        return NULL;

885 886 887 888
    virBufferAddLit(&buf, "<volume>\n");
    virBufferVSprintf(&buf,"  <name>%s</name>\n", def->name);
    virBufferVSprintf(&buf,"  <key>%s</key>\n", def->key);
    virBufferAddLit(&buf, "  <source>\n");
889 890 891 892 893 894 895 896

    if (def->source.nextent) {
        int i;
        const char *thispath = NULL;
        for (i = 0 ; i < def->source.nextent ; i++) {
            if (thispath == NULL ||
                STRNEQ(thispath, def->source.extents[i].path)) {
                if (thispath != NULL)
897 898 899 900
                    virBufferAddLit(&buf, "    </device>\n");

                virBufferVSprintf(&buf, "    <device path='%s'>\n",
                                  def->source.extents[i].path);
901 902
            }

903 904 905 906
            virBufferVSprintf(&buf,
                              "      <extent start='%llu' end='%llu'/>\n",
                              def->source.extents[i].start,
                              def->source.extents[i].end);
907 908 909
            thispath = def->source.extents[i].path;
        }
        if (thispath != NULL)
910
            virBufferAddLit(&buf, "    </device>\n");
911
    }
912
    virBufferAddLit(&buf, "  </source>\n");
913

914 915 916 917
    virBufferVSprintf(&buf,"  <capacity>%llu</capacity>\n",
                      def->capacity);
    virBufferVSprintf(&buf,"  <allocation>%llu</allocation>\n",
                      def->allocation);
918

919
    virBufferAddLit(&buf, "  <target>\n");
920

921 922
    if (def->target.path)
        virBufferVSprintf(&buf,"    <path>%s</path>\n", def->target.path);
923 924

    if (options->formatToString) {
925
        const char *format = (options->formatToString)(def->target.format);
926 927 928 929
        if (!format) {
            virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
                                  _("unknown volume format number %d"),
                                  def->target.format);
930
            goto cleanup;
931
        }
932
        virBufferVSprintf(&buf,"    <format type='%s'/>\n", format);
933 934
    }

935 936 937 938 939 940 941
    virBufferAddLit(&buf,"    <permissions>\n");
    virBufferVSprintf(&buf,"      <mode>0%o</mode>\n",
                      def->target.perms.mode);
    virBufferVSprintf(&buf,"      <owner>%d</owner>\n",
                      def->target.perms.uid);
    virBufferVSprintf(&buf,"      <group>%d</group>\n",
                      def->target.perms.gid);
942 943


944 945 946 947 948 949 950
    if (def->target.perms.label)
        virBufferVSprintf(&buf,"      <label>%s</label>\n",
                          def->target.perms.label);

    virBufferAddLit(&buf,"    </permissions>\n");
    virBufferAddLit(&buf, "  </target>\n");
    virBufferAddLit(&buf,"</volume>\n");
951

952
    if (virBufferError(&buf))
953 954
        goto no_memory;

955
    return virBufferContentAndReset(&buf);
956 957

 no_memory:
J
Jim Meyering 已提交
958
    virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("xml"));
959
 cleanup:
960 961
    tmp = virBufferContentAndReset(&buf);
    VIR_FREE(tmp);
962 963 964 965 966
    return NULL;
}


virStoragePoolObjPtr
967
virStoragePoolObjFindByUUID(virStoragePoolObjListPtr pools,
968
                            const unsigned char *uuid) {
969
    unsigned int i;
970

971 972 973
    for (i = 0 ; i < pools->count ; i++)
        if (!memcmp(pools->objs[i]->def->uuid, uuid, VIR_UUID_BUFLEN))
            return pools->objs[i];
974 975 976 977 978

    return NULL;
}

virStoragePoolObjPtr
979
virStoragePoolObjFindByName(virStoragePoolObjListPtr pools,
980
                            const char *name) {
981
    unsigned int i;
982

983 984 985
    for (i = 0 ; i < pools->count ; i++)
        if (STREQ(pools->objs[i]->def->name, name))
            return pools->objs[i];
986 987 988 989 990 991 992

    return NULL;
}

void
virStoragePoolObjClearVols(virStoragePoolObjPtr pool)
{
993 994 995 996 997 998
    unsigned int i;
    for (i = 0 ; i < pool->volumes.count ; i++)
        virStorageVolDefFree(pool->volumes.objs[i]);

    VIR_FREE(pool->volumes.objs);
    pool->volumes.count = 0;
999 1000 1001 1002 1003
}

virStorageVolDefPtr
virStorageVolDefFindByKey(virStoragePoolObjPtr pool,
                          const char *key) {
1004
    unsigned int i;
1005

1006 1007 1008
    for (i = 0 ; i < pool->volumes.count ; i++)
        if (STREQ(pool->volumes.objs[i]->key, key))
            return pool->volumes.objs[i];
1009 1010 1011 1012 1013 1014 1015

    return NULL;
}

virStorageVolDefPtr
virStorageVolDefFindByPath(virStoragePoolObjPtr pool,
                           const char *path) {
1016
    unsigned int i;
1017

1018 1019 1020
    for (i = 0 ; i < pool->volumes.count ; i++)
        if (STREQ(pool->volumes.objs[i]->target.path, path))
            return pool->volumes.objs[i];
1021 1022 1023 1024 1025 1026 1027

    return NULL;
}

virStorageVolDefPtr
virStorageVolDefFindByName(virStoragePoolObjPtr pool,
                           const char *name) {
1028
    unsigned int i;
1029

1030 1031 1032
    for (i = 0 ; i < pool->volumes.count ; i++)
        if (STREQ(pool->volumes.objs[i]->name, name))
            return pool->volumes.objs[i];
1033 1034 1035 1036 1037 1038

    return NULL;
}

virStoragePoolObjPtr
virStoragePoolObjAssignDef(virConnectPtr conn,
1039
                           virStoragePoolObjListPtr pools,
1040 1041 1042
                           virStoragePoolDefPtr def) {
    virStoragePoolObjPtr pool;

1043
    if ((pool = virStoragePoolObjFindByName(pools, def->name))) {
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
        if (!virStoragePoolObjIsActive(pool)) {
            virStoragePoolDefFree(pool->def);
            pool->def = def;
        } else {
            if (pool->newDef)
                virStoragePoolDefFree(pool->newDef);
            pool->newDef = def;
        }
        return pool;
    }

1055
    if (VIR_ALLOC(pool) < 0) {
1056
        virStorageReportError(conn, VIR_ERR_NO_MEMORY,
J
Jim Meyering 已提交
1057
                              "%s", _("pool"));
1058 1059 1060 1061 1062 1063
        return NULL;
    }

    pool->active = 0;
    pool->def = def;

1064 1065 1066 1067 1068 1069 1070
    if (VIR_REALLOC_N(pools->objs, pools->count+1) < 0) {
        pool->def = NULL;
        virStoragePoolObjFree(pool);
        virStorageReportError(conn, VIR_ERR_NO_MEMORY, NULL);
        return NULL;
    }
    pools->objs[pools->count++] = pool;
1071 1072 1073 1074 1075

    return pool;
}

static virStoragePoolObjPtr
1076 1077
virStoragePoolObjLoad(virConnectPtr conn,
                      virStoragePoolObjListPtr pools,
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
                      const char *file,
                      const char *path,
                      const char *xml,
                      const char *autostartLink) {
    virStoragePoolDefPtr def;
    virStoragePoolObjPtr pool;

    if (!(def = virStoragePoolDefParse(NULL, xml, file))) {
        virErrorPtr err = virGetLastError();
        virStorageLog("Error parsing storage pool config '%s' : %s",
1088
                      path, err ? err->message : NULL);
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
        return NULL;
    }

    if (!virFileMatchesNameSuffix(file, def->name, ".xml")) {
        virStorageLog("Storage pool config filename '%s' does not match pool name '%s'",
                      path, def->name);
        virStoragePoolDefFree(def);
        return NULL;
    }

1099
    if (!(pool = virStoragePoolObjAssignDef(conn, pools, def))) {
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
        virStorageLog("Failed to load storage pool config '%s': out of memory", path);
        virStoragePoolDefFree(def);
        return NULL;
    }

    pool->configFile = strdup(path);
    if (pool->configFile == NULL) {
        virStorageLog("Failed to load storage pool config '%s': out of memory", path);
        virStoragePoolDefFree(def);
        return NULL;
    }
    pool->autostartLink = strdup(autostartLink);
    if (pool->autostartLink == NULL) {
        virStorageLog("Failed to load storage pool config '%s': out of memory", path);
        virStoragePoolDefFree(def);
        return NULL;
    }

    pool->autostart = virFileLinkPointsTo(pool->autostartLink,
                                          pool->configFile);

    return pool;
}


int
1126 1127 1128 1129
virStoragePoolLoadAllConfigs(virConnectPtr conn,
                             virStoragePoolObjListPtr pools,
                             const char *configDir,
                             const char *autostartDir) {
1130 1131 1132
    DIR *dir;
    struct dirent *entry;

1133
    if (!(dir = opendir(configDir))) {
1134 1135 1136
        if (errno == ENOENT)
            return 0;
        virStorageLog("Failed to open dir '%s': %s",
1137
                      configDir, strerror(errno));
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
        return -1;
    }

    while ((entry = readdir(dir))) {
        char *xml = NULL;
        char path[PATH_MAX];
        char autostartLink[PATH_MAX];

        if (entry->d_name[0] == '.')
            continue;

        if (!virFileHasSuffix(entry->d_name, ".xml"))
            continue;

1152
        if (virFileBuildPath(configDir, entry->d_name,
1153 1154
                             NULL, path, PATH_MAX) < 0) {
            virStorageLog("Config filename '%s/%s' is too long",
1155
                          configDir, entry->d_name);
1156 1157 1158
            continue;
        }

1159
        if (virFileBuildPath(autostartDir, entry->d_name,
1160 1161
                             NULL, autostartLink, PATH_MAX) < 0) {
            virStorageLog("Autostart link path '%s/%s' is too long",
1162
                          autostartDir, entry->d_name);
1163 1164 1165 1166 1167 1168
            continue;
        }

        if (virFileReadAll(path, 8192, &xml) < 0)
            continue;

1169
        virStoragePoolObjLoad(conn, pools, entry->d_name, path, xml, autostartLink);
1170

1171
        VIR_FREE(xml);
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
    }

    closedir(dir);

    return 0;
}

int
virStoragePoolObjSaveDef(virConnectPtr conn,
                         virStorageDriverStatePtr driver,
                         virStoragePoolObjPtr pool,
                         virStoragePoolDefPtr def) {
    char *xml;
    int fd = -1, ret = -1;
    ssize_t towrite;

    if (!pool->configFile) {
        int err;
        char path[PATH_MAX];

        if ((err = virFileMakePath(driver->configDir))) {
            virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
                                  _("cannot create config directory %s: %s"),
                                  driver->configDir, strerror(err));
            return -1;
        }

        if (virFileBuildPath(driver->configDir, def->name, ".xml",
                             path, sizeof(path)) < 0) {
            virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1202
                                  "%s", _("cannot construct config file path"));
1203 1204 1205 1206
            return -1;
        }
        if (!(pool->configFile = strdup(path))) {
            virStorageReportError(conn, VIR_ERR_NO_MEMORY,
J
Jim Meyering 已提交
1207
                                  "%s", _("configFile"));
1208 1209 1210 1211 1212 1213
            return -1;
        }

        if (virFileBuildPath(driver->autostartDir, def->name, ".xml",
                             path, sizeof(path)) < 0) {
            virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1214 1215
                                  "%s", _("cannot construct "
                                          "autostart link path"));
1216
            VIR_FREE(pool->configFile);
1217 1218 1219 1220
            return -1;
        }
        if (!(pool->autostartLink = strdup(path))) {
            virStorageReportError(conn, VIR_ERR_NO_MEMORY,
J
Jim Meyering 已提交
1221
                                  "%s", _("config file"));
1222
            VIR_FREE(pool->configFile);
1223 1224 1225 1226 1227 1228
            return -1;
        }
    }

    if (!(xml = virStoragePoolDefFormat(conn, def))) {
        virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
1229
                              "%s", _("failed to generate XML"));
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
        return -1;
    }

    if ((fd = open(pool->configFile,
                   O_WRONLY | O_CREAT | O_TRUNC,
                   S_IRUSR | S_IWUSR )) < 0) {
        virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
                              _("cannot create config file %s: %s"),
                              pool->configFile, strerror(errno));
        goto cleanup;
    }

    towrite = strlen(xml);
    if (safewrite(fd, xml, towrite) != towrite) {
        virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
                              _("cannot write config file %s: %s"),
                              pool->configFile, strerror(errno));
        goto cleanup;
    }

    if (close(fd) < 0) {
        virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
                              _("cannot save config file %s: %s"),
                              pool->configFile, strerror(errno));
        goto cleanup;
    }

    ret = 0;

 cleanup:
    if (fd != -1)
        close(fd);

1263
    VIR_FREE(xml);
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285

    return ret;
}

int
virStoragePoolObjDeleteDef(virConnectPtr conn,
                           virStoragePoolObjPtr pool) {
    if (!pool->configFile) {
        virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
                              _("no config file for %s"), pool->def->name);
        return -1;
    }

    if (unlink(pool->configFile) < 0) {
        virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
                              _("cannot remove config for %s"),
                              pool->def->name);
        return -1;
    }

    return 0;
}
1286

1287
char *virStoragePoolSourceListFormat(virConnectPtr conn,
1288 1289
                                     virStoragePoolSourceListPtr def)
{
1290
    virStorageBackendPoolOptionsPtr options;
1291
    virBuffer buf = VIR_BUFFER_INITIALIZER;
1292 1293
    const char *type;
    int i;
1294

1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
    options = virStorageBackendPoolOptionsForType(def->type);
    if (options == NULL)
        return NULL;

    type = virStorageBackendToString(def->type);
    if (!type) {
        virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
                              "%s", _("unexpected pool type"));
        goto cleanup;
    }

    virBufferAddLit(&buf, "<sources>\n");
1307 1308

    for (i = 0; i < def->nsources; i++) {
1309
        virStoragePoolSourceFormat(conn, &buf, options, &def->sources[i]);
1310 1311
    }

1312 1313 1314 1315
    virBufferAddLit(&buf, "</sources>\n");

    if (virBufferError(&buf))
        goto no_memory;
1316 1317

    return virBufferContentAndReset(&buf);
1318 1319 1320 1321 1322 1323

 no_memory:
    virStorageReportError(conn, VIR_ERR_NO_MEMORY, NULL);
 cleanup:
    free(virBufferContentAndReset(&buf));
    return NULL;
1324
}