From 47412f137f2b09dbc2b44b78ba84c0b348e3f402 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Tue, 25 Oct 2011 12:46:05 +0100 Subject: [PATCH] New API: part-to-partnum This converts a partition device name (eg. /dev/sda1) to a partition number (eg. 1). This is useful in conjunction with the parted APIs that mostly take a disk device + partnum. --- daemon/devsparts.c | 25 +++++++++++++++++++++++++ generator/generator_actions.ml | 18 +++++++++++++++++- src/MAX_PROC_NR | 2 +- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/daemon/devsparts.c b/daemon/devsparts.c index 52a6d30..79da41b 100644 --- a/daemon/devsparts.c +++ b/daemon/devsparts.c @@ -220,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; +} diff --git a/generator/generator_actions.ml b/generator/generator_actions.ml index e8a32ac..ce3382f 100644 --- a/generator/generator_actions.ml +++ b/generator/generator_actions.ml @@ -5850,7 +5850,9 @@ removes the partition number, returning the device name (eg. \"/dev/sdb\"). The named partition must exist, for example as a string returned -from C."); +from C. + +See also C."); ("upload_offset", (RErr, [FileIn "filename"; Dev_or_Path "remotefilename"; Int64 "offset"], []), 273, [Progress], (let md5 = Digest.to_hex (Digest.file "COPYING.LIB") in @@ -6211,6 +6213,20 @@ file C. The C and optional C parameters have the same meaning as in C."); + ("part_to_partnum", (RInt "partnum", [Device "partition"], []), 293, [], + [InitPartition, Always, TestOutputInt ( + [["part_to_partnum"; "/dev/sda1"]], 1); + InitEmpty, Always, TestLastFail ( + [["part_to_partnum"; "/dev/sda"]])], + "convert partition name to partition number", + "\ +This function takes a partition name (eg. \"/dev/sdb1\") and +returns the partition number (eg. C<1>). + +The named partition must exist, for example as a string returned +from C. + +See also C."); ] let all_functions = non_daemon_functions @ daemon_functions diff --git a/src/MAX_PROC_NR b/src/MAX_PROC_NR index f20bd4e..4438e30 100644 --- a/src/MAX_PROC_NR +++ b/src/MAX_PROC_NR @@ -1 +1 @@ -292 +293 -- 1.8.3.1