[PATCH] New API: is_whole_device
--- daemon/devsparts.c | 27 +++++++++++++++++++++++++++ generator/actions.ml | 17 +++++++++++++++++ src/MAX_PROC_NR | 2 +- 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/daemon/devsparts.c b/daemon/devsparts.c index 9a2ce9a..1939f2a 100644 --- a/daemon/devsparts.c +++ b/daemon/devsparts.c @@ -245,6 +245,33 @@ do_part_to_partnum (const char *part) } int +do_is_whole_device (const char *device) +{ + #define PREFIX "/dev/" + + /* Shouldn't be possible because we already sanity checked our input */ + if (!STRPREFIX (device, PREFIX)) return 0; + + CLEANUP_FREE char *devpath = NULL; + if (asprintf (&devpath, "/sys/block/%s/device", + device + strlen (PREFIX)) == -1) + { + reply_with_perror ("asprintf"); + return -1; + } + + struct stat statbuf; + if (stat (devpath, &statbuf) == -1) { + if (errno == ENOENT || errno == ENOTDIR) return 0; + + reply_with_perror ("stat"); + return -1; + } + + return 1; +} + +int do_device_index (const char *device) { size_t i; diff --git a/generator/actions.ml b/generator/actions.ml index 96702f7..711f97e 100644 --- a/generator/actions.ml +++ b/generator/actions.ml @@ -10704,6 +10704,23 @@ Rename a file to a new place on the same filesystem. This is the same as the Linux L<rename(2)> system call. In most cases you are better to use C<guestfs_mv> instead." }; + { defaults with + name = "is_whole_device"; + style = RBool "flag", [Device "device"], []; + proc_nr = Some 395; + tests = [ + InitEmpty, Always, TestOutputTrue ( + [["is_whole_device"; "/dev/sda"]]); + InitPartition, Always, TestOutputFalse ( + [["is_whole_device"; "/dev/sda1"]]); + InitBasicFSonLVM, Always, TestOutputFalse ( + [["is_whole_device"; "/dev/VG/LV"]]); + ]; + shortdesc = "test if a device is a whole device"; + longdesc = "\ +This returns C<true> if and only if C<device> refers to a whole block +device. That is, not a partition or a logical device." }; + ] (* Non-API meta-commands available only in guestfish. diff --git a/src/MAX_PROC_NR b/src/MAX_PROC_NR index 0ca45a0..e537bfe 100644 --- a/src/MAX_PROC_NR +++ b/src/MAX_PROC_NR @@ -1 +1 @@ -394 +395 -- 1.8.1
On Thu, Feb 07, 2013 at 02:24:10PM +0000, Matthew Booth wrote:
--- daemon/devsparts.c | 27 +++++++++++++++++++++++++++ generator/actions.ml | 17 +++++++++++++++++ src/MAX_PROC_NR | 2 +- 3 files changed, 45 insertions(+), 1 deletion(-)
diff --git a/daemon/devsparts.c b/daemon/devsparts.c index 9a2ce9a..1939f2a 100644 --- a/daemon/devsparts.c +++ b/daemon/devsparts.c @@ -245,6 +245,33 @@ do_part_to_partnum (const char *part) }
int +do_is_whole_device (const char *device) +{ + #define PREFIX "/dev/" + + /* Shouldn't be possible because we already sanity checked our input */ + if (!STRPREFIX (device, PREFIX)) return 0;
There's no need for this test.
+ CLEANUP_FREE char *devpath = NULL; + if (asprintf (&devpath, "/sys/block/%s/device", + device + strlen (PREFIX)) == -1) + { + reply_with_perror ("asprintf"); + return -1; + } + + struct stat statbuf; + if (stat (devpath, &statbuf) == -1) { + if (errno == ENOENT || errno == ENOTDIR) return 0; + + reply_with_perror ("stat"); + return -1; + } + + return 1; +}
This test seems fine. Is it worth adding S_ISDIR (statbuf.st_mode)? Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 100 libraries supported. http://fedoraproject.org/wiki/MinGW
On Fri, 2013-02-08 at 09:42 +0000, Richard W.M. Jones wrote:
On Thu, Feb 07, 2013 at 02:24:10PM +0000, Matthew Booth wrote:
--- daemon/devsparts.c | 27 +++++++++++++++++++++++++++ generator/actions.ml | 17 +++++++++++++++++ src/MAX_PROC_NR | 2 +- 3 files changed, 45 insertions(+), 1 deletion(-)
diff --git a/daemon/devsparts.c b/daemon/devsparts.c index 9a2ce9a..1939f2a 100644 --- a/daemon/devsparts.c +++ b/daemon/devsparts.c @@ -245,6 +245,33 @@ do_part_to_partnum (const char *part) }
int +do_is_whole_device (const char *device) +{ + #define PREFIX "/dev/" + + /* Shouldn't be possible because we already sanity checked our input */ + if (!STRPREFIX (device, PREFIX)) return 0;
There's no need for this test.
+ CLEANUP_FREE char *devpath = NULL; + if (asprintf (&devpath, "/sys/block/%s/device", + device + strlen (PREFIX)) == -1) + { + reply_with_perror ("asprintf"); + return -1; + } + + struct stat statbuf; + if (stat (devpath, &statbuf) == -1) { + if (errno == ENOENT || errno == ENOTDIR) return 0; + + reply_with_perror ("stat"); + return -1; + } + + return 1; +}
This test seems fine. Is it worth adding S_ISDIR (statbuf.st_mode)?
It's actually a symlink. I'll add that and also a comment describing what it's looking for, then push it. Matt
participants (2)
-
Matthew Booth -
Richard W.M. Jones