From dfbc185e532bf2e2dc192081d6fddd0b58f9089e Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 12 Mar 2009 14:31:50 +0100 Subject: [PATCH] lib: wholedisk - extend API, add test program Signed-off-by: Karel Zak --- include/wholedisk.h | 1 + lib/.gitignore | 4 +--- lib/Makefile.am | 9 ++++----- lib/wholedisk.c | 43 +++++++++++++++++++++++++++++++++++-------- 4 files changed, 41 insertions(+), 16 deletions(-) diff --git a/include/wholedisk.h b/include/wholedisk.h index f367e4010..251479e76 100644 --- a/include/wholedisk.h +++ b/include/wholedisk.h @@ -2,6 +2,7 @@ #define WHOLEDISK_H extern int is_whole_disk(const char *name); +extern int is_whole_disk_fd(int fd, const char *name); #endif /* WHOLEDISK_H */ diff --git a/lib/.gitignore b/lib/.gitignore index 0be0fd86c..145f5d746 100644 --- a/lib/.gitignore +++ b/lib/.gitignore @@ -1,3 +1 @@ -test_blkdev -test_ismounted -test_pttype +test_* diff --git a/lib/Makefile.am b/lib/Makefile.am index f813f6855..0b6480699 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -1,16 +1,15 @@ include $(top_srcdir)/config/include-Makefile.am -noinst_PROGRAMS = test_blkdev test_ismounted test_pttype +AM_CPPFLAGS += -DTEST_PROGRAM + +noinst_PROGRAMS = test_blkdev test_ismounted test_pttype test_wholedisk test_blkdev_SOURCES = blkdev.c test_ismounted_SOURCES = ismounted.c test_pttype_SOURCES = pttype.c +test_wholedisk_SOURCES = wholedisk.c if LINUX test_blkdev_SOURCES += linux_version.c endif -test_blkdev_CFLAGS = -DTEST_PROGRAM -test_ismounted_CFLAGS = -DTEST_PROGRAM -test_pttype_CFLAGS = -DTEST_PROGRAM - diff --git a/lib/wholedisk.c b/lib/wholedisk.c index eb6d432ce..35f143d28 100644 --- a/lib/wholedisk.c +++ b/lib/wholedisk.c @@ -1,22 +1,21 @@ +#include +#include #include #include "blkdev.h" #include "wholedisk.h" -int is_whole_disk(const char *name) +int is_whole_disk_fd(int fd, const char *name) { #ifdef HDIO_GETGEO struct hd_geometry geometry; - int fd, i = 0; + int i = 0; - fd = open(name, O_RDONLY); - if (fd >= 0) { + if (fd != -1) i = ioctl(fd, HDIO_GETGEO, &geometry); - close(fd); - } - if (i==0) - return (fd >= 0 && geometry.start == 0); + if (i == 0) + return geometry.start == 0; #endif /* * The "silly heuristic" is still sexy for us, because @@ -29,3 +28,31 @@ int is_whole_disk(const char *name) name++; return !isdigit(name[-1]); } + +int is_whole_disk(const char *name) +{ + int fd = -1, res = 0; +#ifdef HDIO_GETGEO + fd = open(name, O_RDONLY); + if (fd != -1) +#endif + res = is_whole_disk_fd(fd, name); + + if (fd != -1) + close(fd); + return res; +} + +#ifdef TEST_PROGRAM +int main(int argc, char **argv) +{ + if (argc < 2) { + fprintf(stderr, "usage: %s \n", argv[0]); + exit(EXIT_FAILURE); + } + + printf("%s: is%s whole disk\n", argv[1], + is_whole_disk(argv[1]) ? "" : " NOT"); + exit(EXIT_SUCCESS); +} +#endif