X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=blobdiff_plain;f=daemon%2Fdevsparts.c;h=1781def3feb9ac876011328a490def77d2ff7b63;hp=b89682c9548dc8573feab00159c78d7f65e62151;hb=ffd4820ffe953b0583e3a9357e37d74bed3a2320;hpb=98bc29fda446f5c667f4bc799161b3446bfc2ca1 diff --git a/daemon/devsparts.c b/daemon/devsparts.c index b89682c..1781def 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" @@ -40,7 +42,6 @@ foreach_block_device (block_dev_func_t func) int size = 0, alloc = 0; DIR *dir; - struct dirent *d; int err = 0; dir = opendir ("/sys/block"); @@ -49,15 +50,22 @@ foreach_block_device (block_dev_func_t func) return NULL; } - errno = 0; - while ((d = readdir (dir)) != NULL) { - 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) { + while(1) { + errno = 0; + struct dirent *d = readdir(dir); + if(NULL == d) break; + + 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. @@ -151,7 +159,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); @@ -185,21 +193,27 @@ do_list_partitions (void) return foreach_block_device(add_partitions); } -int -do_mkfs (char *fstype, char *device) +char * +do_part_to_dev (const char *part) { - char *err; - int r; + int err = 1; + size_t n = strlen (part); - IS_DEVICE (device, -1); + while (n >= 1 && c_isdigit (part[n-1])) { + err = 0; + n--; + } - r = command (NULL, &err, "/sbin/mkfs", "-t", fstype, device, NULL); - if (r == -1) { - reply_with_error ("mkfs: %s", err); - free (err); - return -1; + if (err) { + reply_with_error ("device name is not a partition"); + return NULL; } - free (err); - return 0; + char *r = strndup (part, n); + if (r == NULL) { + reply_with_perror ("strdup"); + return NULL; + } + + return r; }