summaryrefslogtreecommitdiffstats
path: root/libs/blkid/src/probe.c
diff options
context:
space:
mode:
authorKarel Zak2009-01-27 17:25:06 +0100
committerKarel Zak2009-02-11 23:35:25 +0100
commita2f01a1c0ba7e8d6f546133195ee759715ef42f7 (patch)
treeac4b51b12299821f80f731db6c62b10049296c34 /libs/blkid/src/probe.c
parentblkid: add lvm2 reg.test (diff)
downloadkernel-qcow2-util-linux-a2f01a1c0ba7e8d6f546133195ee759715ef42f7.tar.gz
kernel-qcow2-util-linux-a2f01a1c0ba7e8d6f546133195ee759715ef42f7.tar.xz
kernel-qcow2-util-linux-a2f01a1c0ba7e8d6f546133195ee759715ef42f7.zip
blkid: add blkid_do_safeprobe()
The function blkid_do_probe() is able to detect more filesystems on the device while(blkid_do_probe(pr) == 0) ... but in many cases we need only one exact answer, and we also need to be sure that there is not any other FS on the device. For example it's possible to create valid LUKS (or vfat, ...) header and valid linux swap header on the same device -- in such case the device can be interpreted (by mount/swapon) in two completely different ways. An ambivalent result is always error -- the library never returns such result. Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libs/blkid/src/probe.c')
-rw-r--r--libs/blkid/src/probe.c41
1 files changed, 40 insertions, 1 deletions
diff --git a/libs/blkid/src/probe.c b/libs/blkid/src/probe.c
index c9f88b058..c8731ded0 100644
--- a/libs/blkid/src/probe.c
+++ b/libs/blkid/src/probe.c
@@ -169,6 +169,7 @@ void blkid_reset_probe(blkid_probe pr)
memset(pr->buf, 0, pr->buf_max);
pr->buf_off = 0;
pr->buf_len = 0;
+ pr->idx = 0;
if (pr->sbbuf)
memset(pr->sbbuf, 0, BLKID_SB_BUFSIZ);
pr->sbbuf_len = 0;
@@ -448,7 +449,7 @@ int blkid_do_probe(blkid_probe pr)
DBG(DEBUG_LOWPROBE, printf("*** starting probing loop\n"));
- for (i = 0; i < ARRAY_SIZE(idinfos); i++) {
+ for ( ; i < ARRAY_SIZE(idinfos); i++) {
const struct blkid_idinfo *id;
const struct blkid_idmag *mag;
int hasmag = 0;
@@ -510,6 +511,44 @@ int blkid_do_probe(blkid_probe pr)
return 1;
}
+/*
+ * This is the same function as blkid_do_probe(), but returns only one result
+ * (cannot be used in while()) and checks for ambivalen results (more
+ * filesystems on the device) -- in such case returns -2.
+ */
+int blkid_do_safeprobe(blkid_probe pr)
+{
+ struct blkid_struct_probe first;
+ int count = 0;
+ int intol = 0;
+ int rc;
+
+ while ((rc = blkid_do_probe(pr)) == 0) {
+ if (!count) {
+ /* store the fist result */
+ memcpy(first.vals, pr->vals, sizeof(first.vals));
+ first.nvals = pr->nvals;
+ first.idx = pr->idx;
+ }
+ if (!(idinfos[pr->idx]->flags & BLKID_IDINFO_TOLERANT))
+ intol++;
+ count++;
+ }
+ if (rc < 0)
+ return rc; /* error */
+ if (count > 1 && intol)
+ return -2; /* error, ambivalent result (more FS) */
+ if (!count)
+ return 1; /* nothing detected */
+
+ /* restore the first result */
+ memcpy(pr->vals, first.vals, sizeof(first.vals));
+ pr->nvals = first.nvals;
+ pr->idx = first.idx;
+
+ return 0;
+}
+
int blkid_probe_numof_values(blkid_probe pr)
{
if (!pr)