X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=daemon%2Fdevsparts.c;h=79da41b03dae4c127cd67df9fe1dc6930e2e375f;hb=119e67a92e74c3e657133c751a4f92d40924d787;hp=1781def3feb9ac876011328a490def77d2ff7b63;hpb=ffd4820ffe953b0583e3a9357e37d74bed3a2320;p=libguestfs.git diff --git a/daemon/devsparts.c b/daemon/devsparts.c index 1781def..79da41b 100644 --- a/daemon/devsparts.c +++ b/daemon/devsparts.c @@ -89,6 +89,7 @@ foreach_block_device (block_dev_func_t func) if(0 != errno) { reply_with_perror ("readdir: /sys/block"); free_stringslen(r, size); + closedir (dir); return NULL; } @@ -105,8 +106,9 @@ foreach_block_device (block_dev_func_t func) return NULL; } - /* Sort the devices */ - sort_strings (r, size); + /* Sort the devices. Note that r might be NULL if there are no devices. */ + if (r != NULL) + sort_strings (r, size); /* NULL terminate the list */ if (add_string (&r, &size, &alloc, NULL) == -1) { @@ -174,6 +176,7 @@ add_partitions(const char *device, if(0 != errno) { reply_with_perror ("readdir: %s", devdir); free_stringslen(*r, *size); + closedir (dir); return -1; } @@ -217,3 +220,28 @@ do_part_to_dev (const char *part) return r; } + +int +do_part_to_partnum (const char *part) +{ + int err = 1; + size_t n = strlen (part); + + while (n >= 1 && c_isdigit (part[n-1])) { + err = 0; + n--; + } + + if (err) { + reply_with_error ("device name is not a partition"); + return -1; + } + + int r; + if (sscanf (&part[n], "%d", &r) != 1) { + reply_with_error ("could not parse number"); + return -1; + } + + return r; +}