X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=daemon%2Fdevsparts.c;h=52a6d304d5afb6a1e45c33eb9ab25d723b59a46f;hb=50c54d2814b13d7d51f659600e93565dc6921b28;hp=95e4a6828e6d1767c9147afbe39ed41a4eb57783;hpb=5c31f6126ba4ea3e9056c34c300f6f5e332ab997;p=libguestfs.git diff --git a/daemon/devsparts.c b/daemon/devsparts.c index 95e4a68..52a6d30 100644 --- a/daemon/devsparts.c +++ b/daemon/devsparts.c @@ -26,6 +26,8 @@ #include #include +#include "c-ctype.h" + #include "daemon.h" #include "actions.h" @@ -87,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; } @@ -103,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) { @@ -172,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; } @@ -190,3 +195,28 @@ do_list_partitions (void) { return foreach_block_device(add_partitions); } + +char * +do_part_to_dev (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 NULL; + } + + char *r = strndup (part, n); + if (r == NULL) { + reply_with_perror ("strdup"); + return NULL; + } + + return r; +}