summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorKarel Zak2009-03-12 14:31:50 +0100
committerKarel Zak2009-03-12 14:31:50 +0100
commitdfbc185e532bf2e2dc192081d6fddd0b58f9089e (patch)
tree12e1866a8871a113ffc5273fb0cfec4ea802f95e /lib
parentlib: pttype - extend the API to work with file descriptors (diff)
downloadkernel-qcow2-util-linux-dfbc185e532bf2e2dc192081d6fddd0b58f9089e.tar.gz
kernel-qcow2-util-linux-dfbc185e532bf2e2dc192081d6fddd0b58f9089e.tar.xz
kernel-qcow2-util-linux-dfbc185e532bf2e2dc192081d6fddd0b58f9089e.zip
lib: wholedisk - extend API, add test program
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/.gitignore4
-rw-r--r--lib/Makefile.am9
-rw-r--r--lib/wholedisk.c43
3 files changed, 40 insertions, 16 deletions
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 <stdio.h>
+#include <stdlib.h>
#include <ctype.h>
#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 <device>\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