debuggers.hg

changeset 22710:2c487c02d8d7

libxl: Factorize function libxl_device_disk_list

This patch adds function libxl_append_disk_list_of_type to get disks
parameter of one backend type.

Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
Committed-by: Ian Jackson <ian.jackson@eu.citrix.com>
author Anthony PERARD <anthony.perard@citrix.com>
date Thu Jan 06 18:03:11 2011 +0000 (2011-01-06)
parents 4e120cb427f4
children e18d9af29d2a
files tools/libxl/libxl.c
line diff
     1.1 --- a/tools/libxl/libxl.c	Thu Jan 06 17:37:00 2011 +0000
     1.2 +++ b/tools/libxl/libxl.c	Thu Jan 06 18:03:11 2011 +0000
     1.3 @@ -2495,61 +2495,62 @@ int libxl_device_vkb_hard_shutdown(libxl
     1.4      return ERROR_NI;
     1.5  }
     1.6  
     1.7 -libxl_device_disk *libxl_device_disk_list(libxl_ctx *ctx, uint32_t domid, int *num)
     1.8 +static unsigned int libxl_append_disk_list_of_type(libxl_ctx *ctx,
     1.9 +                                                   uint32_t domid,
    1.10 +                                                   const char *type,
    1.11 +                                                   libxl_device_disk **disks,
    1.12 +                                                   unsigned int *ndisks)
    1.13  {
    1.14      libxl__gc gc = LIBXL_INIT_GC(ctx);
    1.15 -    char *be_path_tap, *be_path_vbd;
    1.16 -    libxl_device_disk *dend, *disks, *ret = NULL;
    1.17 -    char **b, **l = NULL;
    1.18 -    unsigned int numl, len;
    1.19 -    char *type;
    1.20 -
    1.21 -    be_path_vbd = libxl__sprintf(&gc, "%s/backend/vbd/%d", libxl__xs_get_dompath(&gc, 0), domid);
    1.22 -    be_path_tap = libxl__sprintf(&gc, "%s/backend/tap/%d", libxl__xs_get_dompath(&gc, 0), domid);
    1.23 -
    1.24 -    b = l = libxl__xs_directory(&gc, XBT_NULL, be_path_vbd, &numl);
    1.25 -    if (l) {
    1.26 -        ret = realloc(ret, sizeof(libxl_device_disk) * numl);
    1.27 -        disks = ret;
    1.28 -        *num = numl;
    1.29 -        dend = ret + *num;
    1.30 -        for (; disks < dend; ++disks, ++l) {
    1.31 -            disks->backend_domid = 0;
    1.32 -            disks->domid = domid;
    1.33 -            disks->physpath = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "%s/%s/params", be_path_vbd, *l), &len);
    1.34 -            libxl_string_to_phystype(ctx, libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/type", be_path_vbd, *l)), &(disks->phystype));
    1.35 -            disks->virtpath = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "%s/%s/dev", be_path_vbd, *l), &len);
    1.36 -            disks->unpluggable = atoi(libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/removable", be_path_vbd, *l)));
    1.37 -            if (!strcmp(libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/mode", be_path_vbd, *l)), "w"))
    1.38 -                disks->readwrite = 1;
    1.39 +    char *be_path = NULL;
    1.40 +    char **dir = NULL;
    1.41 +    unsigned int n = 0, len = 0;
    1.42 +    libxl_device_disk *pdisk = NULL, *pdisk_end = NULL;
    1.43 +    char *physpath_tmp = NULL;
    1.44 +
    1.45 +    be_path = libxl__sprintf(&gc, "%s/backend/%s/%d",
    1.46 +                             libxl__xs_get_dompath(&gc, 0), type, domid);
    1.47 +    dir = libxl__xs_directory(&gc, XBT_NULL, be_path, &n);
    1.48 +    if (dir) {
    1.49 +        *disks = realloc(*disks, sizeof (libxl_device_disk) * (*ndisks + n));
    1.50 +        pdisk = *disks + *ndisks;
    1.51 +        *ndisks += n;
    1.52 +        pdisk_end = *disks + *ndisks;
    1.53 +        for (; pdisk < pdisk_end; pdisk++, dir++) {
    1.54 +            pdisk->backend_domid = 0;
    1.55 +            pdisk->domid = domid;
    1.56 +            physpath_tmp = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "%s/%s/params", be_path, *dir), &len);
    1.57 +            if (strchr(physpath_tmp, ':')) {
    1.58 +                pdisk->physpath = strdup(strchr(physpath_tmp, ':') + 1);
    1.59 +                free(physpath_tmp);
    1.60 +            } else {
    1.61 +                pdisk->physpath = physpath_tmp;
    1.62 +            }
    1.63 +            libxl_string_to_phystype(ctx, libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/type", be_path, *dir)), &(pdisk->phystype));
    1.64 +            pdisk->virtpath = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "%s/%s/dev", be_path, *dir), &len);
    1.65 +            pdisk->unpluggable = atoi(libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/removable", be_path, *dir)));
    1.66 +            if (!strcmp(libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/mode", be_path, *dir)), "w"))
    1.67 +                pdisk->readwrite = 1;
    1.68              else
    1.69 -                disks->readwrite = 0;
    1.70 -            type = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/device-type", libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/frontend", be_path_vbd, *l))));
    1.71 -            disks->is_cdrom = !strcmp(type, "cdrom");
    1.72 +                pdisk->readwrite = 0;
    1.73 +            type = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/device-type", libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/frontend", be_path, *dir))));
    1.74 +            pdisk->is_cdrom = !strcmp(type, "cdrom");
    1.75          }
    1.76      }
    1.77 -    b = l = libxl__xs_directory(&gc, XBT_NULL, be_path_tap, &numl);
    1.78 -    if (l) {
    1.79 -        ret = realloc(ret, sizeof(libxl_device_disk) * (*num + numl));
    1.80 -        disks = ret + *num;
    1.81 -        *num += numl;
    1.82 -        for (dend = ret + *num; disks < dend; ++disks, ++l) {
    1.83 -            disks->backend_domid = 0;
    1.84 -            disks->domid = domid;
    1.85 -            disks->physpath = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "%s/%s/params", be_path_tap, *l), &len);
    1.86 -            libxl_string_to_phystype(ctx, libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/type", be_path_tap, *l)), &(disks->phystype));
    1.87 -            disks->virtpath = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "%s/%s/dev", be_path_tap, *l), &len);
    1.88 -            disks->unpluggable = atoi(libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/removable", be_path_tap, *l)));
    1.89 -            if (!strcmp(libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/mode", be_path_tap, *l)), "w"))
    1.90 -                disks->readwrite = 1;
    1.91 -            else
    1.92 -                disks->readwrite = 0;
    1.93 -            type = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/device-type", libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/frontend", be_path_tap, *l))));
    1.94 -            disks->is_cdrom = !strcmp(type, "cdrom");
    1.95 -        }
    1.96 -    }
    1.97 +
    1.98      libxl__free_all(&gc);
    1.99 -    return ret;
   1.100 +    return n;
   1.101 +}
   1.102 +
   1.103 +libxl_device_disk *libxl_device_disk_list(libxl_ctx *ctx, uint32_t domid, int *num)
   1.104 +{
   1.105 +    libxl_device_disk *disks = NULL;
   1.106 +    unsigned int ndisks = 0;
   1.107 +
   1.108 +    *num = libxl_append_disk_list_of_type(ctx, domid, "vbd", &disks, &ndisks);
   1.109 +    *num += libxl_append_disk_list_of_type(ctx, domid, "tap", &disks, &ndisks);
   1.110 +
   1.111 +    return disks;
   1.112  }
   1.113  
   1.114  int libxl_device_disk_getinfo(libxl_ctx *ctx, uint32_t domid,