From: Karel Zak Date: Wed, 11 Mar 2009 13:00:21 +0000 (+0100) Subject: lib: add is_whole_disk() from fdisk code X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=929f243f79c4fdf3e89dad5134e53bf001598a5e;p=util-linux lib: add is_whole_disk() from fdisk code Signed-off-by: Karel Zak --- diff --git a/fdisk/Makefile.am b/fdisk/Makefile.am index b3b5d7e9..20906b52 100644 --- a/fdisk/Makefile.am +++ b/fdisk/Makefile.am @@ -3,7 +3,7 @@ include $(top_srcdir)/config/include-Makefile.am EXTRA_DIST = README.fdisk README.cfdisk sfdisk.examples partitiontype.c fdisk_common = i386_sys_types.c common.h gpt.c gpt.h \ - ../lib/blkdev.c + ../lib/blkdev.c ../lib/wholedisk.c if LINUX fdisk_common += ../lib/linux_version.c diff --git a/fdisk/common.h b/fdisk/common.h index 674e65f4..352b9a59 100644 --- a/fdisk/common.h +++ b/fdisk/common.h @@ -11,6 +11,5 @@ struct systypes { extern struct systypes i386_sys_types[]; extern char *partname(char *dev, int pno, int lth); -extern int is_probably_full_disk(char *name); #endif /* FDISK_COMMON_H */ diff --git a/fdisk/fdisk.c b/fdisk/fdisk.c index 5593503b..e3b6857f 100644 --- a/fdisk/fdisk.c +++ b/fdisk/fdisk.c @@ -26,6 +26,7 @@ #include "blkdev.h" #include "common.h" #include "fdisk.h" +#include "wholedisk.h" #include "fdisksunlabel.h" #include "fdisksgilabel.h" @@ -2568,7 +2569,7 @@ tryprocpt(void) { &ma, &mi, &sz, ptname) != 4) continue; snprintf(devname, sizeof(devname), "/dev/%s", ptname); - if (is_probably_full_disk(devname)) + if (is_whole_disk(devname)) try(devname, 0); } fclose(procpt); diff --git a/fdisk/partname.c b/fdisk/partname.c index 889e7f0b..1fe3087b 100644 --- a/fdisk/partname.c +++ b/fdisk/partname.c @@ -1,9 +1,7 @@ #include #include #include -#include -#include -#include + #include "blkdev.h" #include "pathnames.h" #include "common.h" @@ -48,28 +46,3 @@ partname(char *dev, int pno, int lth) { return bufp; } -int -is_probably_full_disk(char *name) { -#ifdef HDIO_GETGEO - struct hd_geometry geometry; - int fd, i = 0; - - fd = open(name, O_RDONLY); - if (fd >= 0) { - i = ioctl(fd, HDIO_GETGEO, &geometry); - close(fd); - } - if (i==0) - return (fd >= 0 && geometry.start == 0); -#endif - /* - * The "silly heuristic" is still sexy for us, because - * for example Xen doesn't implement HDIO_GETGEO for virtual - * block devices (/dev/xvda). - * - * -- kzak@redhat.com (23-Feb-2006) - */ - while (*name) - name++; - return !isdigit(name[-1]); -} diff --git a/fdisk/sfdisk.c b/fdisk/sfdisk.c index 533e729a..2cda03ce 100644 --- a/fdisk/sfdisk.c +++ b/fdisk/sfdisk.c @@ -50,7 +50,7 @@ #include "blkdev.h" #include "linux_version.h" #include "common.h" - +#include "wholedisk.h" #include "gpt.h" #define SIZE(a) (sizeof(a)/sizeof(a[0])) @@ -2443,7 +2443,7 @@ nextproc(FILE *procf) { &ma, &mi, &sz, ptname) != 4) continue; snprintf(devname, sizeof(devname), "/dev/%s", ptname); - if (!is_probably_full_disk(devname)) + if (!is_whole_disk(devname)) continue; return devname; } diff --git a/include/blkdev.h b/include/blkdev.h index 3cca79b9..9d9453bc 100644 --- a/include/blkdev.h +++ b/include/blkdev.h @@ -3,6 +3,8 @@ #include #include +#include +#include #define DEFAULT_SECTOR_SIZE 512 diff --git a/include/wholedisk.h b/include/wholedisk.h new file mode 100644 index 00000000..f367e401 --- /dev/null +++ b/include/wholedisk.h @@ -0,0 +1,7 @@ +#ifndef WHOLEDISK_H +#define WHOLEDISK_H + +extern int is_whole_disk(const char *name); + +#endif /* WHOLEDISK_H */ + diff --git a/lib/wholedisk.c b/lib/wholedisk.c new file mode 100644 index 00000000..eb6d432c --- /dev/null +++ b/lib/wholedisk.c @@ -0,0 +1,31 @@ + +#include + +#include "blkdev.h" +#include "wholedisk.h" + +int is_whole_disk(const char *name) +{ +#ifdef HDIO_GETGEO + struct hd_geometry geometry; + int fd, i = 0; + + fd = open(name, O_RDONLY); + if (fd >= 0) { + i = ioctl(fd, HDIO_GETGEO, &geometry); + close(fd); + } + if (i==0) + return (fd >= 0 && geometry.start == 0); +#endif + /* + * The "silly heuristic" is still sexy for us, because + * for example Xen doesn't implement HDIO_GETGEO for virtual + * block devices (/dev/xvda). + * + * -- kzak@redhat.com (23-Feb-2006) + */ + while (*name) + name++; + return !isdigit(name[-1]); +}