From: Richard Jones Date: Wed, 15 Sep 2010 15:39:36 +0000 (+0100) Subject: New API: part-to-dev: Convert partition name to device name. X-Git-Tag: 1.5.15~2 X-Git-Url: http://git.annexia.org/?a=commitdiff_plain;h=ffd4820ffe953b0583e3a9357e37d74bed3a2320;p=libguestfs.git New API: part-to-dev: Convert partition name to device name. This adds a formal API for going from a partition to the containing device, eg. /dev/sda1 -> /dev/sda --- diff --git a/daemon/devsparts.c b/daemon/devsparts.c index 95e4a68..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" @@ -190,3 +192,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; +} diff --git a/generator/generator_actions.ml b/generator/generator_actions.ml index 014687d..52c2b7f 100644 --- a/generator/generator_actions.ml +++ b/generator/generator_actions.ml @@ -5064,6 +5064,20 @@ with the given C name. See also C."); + ("part_to_dev", (RString "device", [Device "partition"]), 272, [], + [InitPartition, Always, TestOutputDevice ( + [["part_to_dev"; "/dev/sda1"]], "/dev/sda"); + InitEmpty, Always, TestLastFail ( + [["part_to_dev"; "/dev/sda"]])], + "convert partition name to device name", + "\ +This function takes a partition name (eg. \"/dev/sdb1\") and +removes the partition number, returning the device name +(eg. \"/dev/sdb\"). + +The named partition must exist, for example as a string returned +from C."); + ] let all_functions = non_daemon_functions @ daemon_functions diff --git a/src/MAX_PROC_NR b/src/MAX_PROC_NR index e6a4f00..31e9cf9 100644 --- a/src/MAX_PROC_NR +++ b/src/MAX_PROC_NR @@ -1 +1 @@ -271 +272