X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=daemon%2Fdevsparts.c;h=79da41b03dae4c127cd67df9fe1dc6930e2e375f;hb=e2249b7ce1dd0a2f8f110e0e47aca397185a6373;hp=e9c5e8f761db72e44d8e35ab862997adad718d63;hpb=2b8019e56cf40fe8e8223b4e57cfd590495e8e60;p=libguestfs.git diff --git a/daemon/devsparts.c b/daemon/devsparts.c index e9c5e8f..79da41b 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" @@ -53,13 +55,17 @@ foreach_block_device (block_dev_func_t func) struct dirent *d = readdir(dir); if(NULL == d) break; - if (strncmp (d->d_name, "sd", 2) == 0 || - strncmp (d->d_name, "hd", 2) == 0 || - strncmp (d->d_name, "vd", 2) == 0 || - strncmp (d->d_name, "sr", 2) == 0) { + if (STREQLEN (d->d_name, "sd", 2) || + STREQLEN (d->d_name, "hd", 2) || + STREQLEN (d->d_name, "vd", 2) || + STREQLEN (d->d_name, "sr", 2)) { char dev_path[256]; snprintf (dev_path, sizeof dev_path, "/dev/%s", d->d_name); + /* Ignore the root device. */ + if (is_root_device (dev_path)) + continue; + /* RHBZ#514505: Some versions of qemu <= 0.10 add a * CD-ROM device even though we didn't request it. Try to * detect this by seeing if the device contains media. @@ -83,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; } @@ -99,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) { @@ -153,7 +161,7 @@ add_partitions(const char *device, errno = 0; struct dirent *d; while ((d = readdir (dir)) != NULL) { - if (strncmp (d->d_name, device, strlen (device)) == 0) { + if (STREQLEN (d->d_name, device, strlen (device))) { char part[256]; snprintf (part, sizeof part, "/dev/%s", d->d_name); @@ -168,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; } @@ -187,21 +196,52 @@ 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; +} + int -do_mkfs (char *fstype, char *device) +do_part_to_partnum (const char *part) { - char *err; - int r; + int err = 1; + size_t n = strlen (part); + + while (n >= 1 && c_isdigit (part[n-1])) { + err = 0; + n--; + } - IS_DEVICE (device, -1); + if (err) { + reply_with_error ("device name is not a partition"); + return -1; + } - r = command (NULL, &err, "/sbin/mkfs", "-t", fstype, device, NULL); - if (r == -1) { - reply_with_error ("mkfs: %s", err); - free (err); + int r; + if (sscanf (&part[n], "%d", &r) != 1) { + reply_with_error ("could not parse number"); return -1; } - free (err); - return 0; + return r; }