lib: wholedisk - extend API, add test program

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2009-03-12 14:31:50 +01:00
parent 17c0248447
commit dfbc185e53
4 changed files with 41 additions and 16 deletions

View File

@ -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 */

4
lib/.gitignore vendored
View File

@ -1,3 +1 @@
test_blkdev
test_ismounted
test_pttype
test_*

View File

@ -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

View File

@ -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