]> err.no Git - util-linux/commitdiff
blkid: set size for non-blkdevs, add blkid_probe_strcpy_uuid()
authorKarel Zak <kzak@redhat.com>
Thu, 22 Jan 2009 00:06:57 +0000 (01:06 +0100)
committerKarel Zak <kzak@redhat.com>
Wed, 11 Feb 2009 22:35:23 +0000 (23:35 +0100)
- fix blkid_probe_set_device() to work for non-blkdevs (e.g. file images),
  in such case we need to set the size of data from stat->st_size

- add blkid_probe_strcpy_uuid() for filesystems where UUID is stored as a string
  (e.g. DDF raid, luks, ...)

Signed-off-by: Karel Zak <kzak@redhat.com>
libs/blkid/src/blkidP.h
libs/blkid/src/probe.c

index 774f6c4d7be941598e16ea59b9cd76c59241ec71..2631966a119432b9247ad54d29f399e1d141c731 100644 (file)
@@ -285,11 +285,11 @@ extern int blkid_probe_set_utf8label(blkid_probe pr, unsigned char *label,
 extern int blkid_probe_sprintf_uuid(blkid_probe pr, unsigned char *uuid,
                 size_t len, const char *fmt, ...)
                __attribute__ ((format (printf, 4, 5)));
+extern int blkid_probe_strncpy_uuid(blkid_probe pr, unsigned char *str, size_t len);
 
 extern int blkid_probe_set_uuid(blkid_probe pr, unsigned char *uuid);
 extern int blkid_probe_set_uuid_as(blkid_probe pr, unsigned char *uuid, const char *name);
 
-
 #define BLKID_ENC_UTF16BE      0
 #define BLKID_ENC_UTF16LE      1
 
index 5a4fad746cb7537d337fc1ff2bb39bd6f97db0bf..5271938871b993c3f2d63d14611f50abd0d975a0 100644 (file)
@@ -256,11 +256,23 @@ int blkid_probe_set_device(blkid_probe pr, int fd,
 
        pr->fd = fd;
        pr->off = off;
+       pr->size = 0;
 
-       if (!size)
-               blkdev_get_size(fd, (unsigned long long *) &pr->size);
-       else
+       if (size)
                pr->size = size;
+       else {
+               struct stat sb;
+
+               if (fstat(fd, &sb))
+                       return -1;
+
+               if (S_ISBLK(sb.st_mode))
+                       blkdev_get_size(fd, (unsigned long long *) &pr->size);
+               else
+                       pr->size = sb.st_size;
+       }
+       if (!pr->size)
+               return -1;
 
        /* read SB to test if the device is readable */
        if (!blkid_probe_get_buffer(pr, 0, 0x200)) {
@@ -269,7 +281,8 @@ int blkid_probe_set_device(blkid_probe pr, int fd,
                return -1;
        }
 
-       DBG(DEBUG_LOWPROBE, printf("a new device prepared for low-probing\n"));
+       DBG(DEBUG_LOWPROBE, printf("ready for low-probing, offset=%zd, size=%zd\n",
+                               pr->off, pr->size));
        pr->idx = 0;
        return 0;
 }
@@ -467,7 +480,7 @@ int blkid_do_probe(blkid_probe pr)
                        mag++;
                }
 
-               if (mag && mag->magic == NULL)
+               if (id->magics && id->magics[0].magic)
                        /* magic string(s) defined, but not found */
                        continue;
 
@@ -712,22 +725,7 @@ int blkid_probe_sprintf_uuid(blkid_probe pr, unsigned char *uuid,
                return 0;
 
        va_start(ap, fmt);
-
-       if (!strcmp(fmt, "%s")) {
-               struct blkid_prval *v = NULL;
-               const char *str = va_arg(ap, char *);
-
-               if (str && *str)
-                       v = blkid_probe_assign_value(pr, "UUID");
-               if (v) {
-                       strncpy((char *) v->data, str, BLKID_PROBVAL_BUFSIZ);
-                       v->data[BLKID_PROBVAL_BUFSIZ - 1] = '\0';
-                       v->len = strlen((char *) v->data);
-                       rc = 0;
-               }
-       } else
-               rc = blkid_probe_vsprintf_value(pr, "UUID", fmt, ap);
-
+       rc = blkid_probe_vsprintf_value(pr, "UUID", fmt, ap);
        va_end(ap);
 
        /* convert to lower case (..be paranoid) */
@@ -742,6 +740,34 @@ int blkid_probe_sprintf_uuid(blkid_probe pr, unsigned char *uuid,
        return rc;
 }
 
+/* function to set UUIDs that are in suberblocks stored as strings */
+int blkid_probe_strncpy_uuid(blkid_probe pr, unsigned char *str, size_t len)
+{
+       struct blkid_prval *v;
+
+       if (str == NULL || *str == '\0')
+               return -1;
+       if (!len)
+               len = strlen((char *) str);
+       if (len > BLKID_PROBVAL_BUFSIZ)
+               len = BLKID_PROBVAL_BUFSIZ;
+
+       if ((pr->probreq & BLKID_PROBREQ_UUIDRAW) &&
+           blkid_probe_set_value(pr, "UUID_RAW", str, len) < 0)
+               return -1;
+       if (!(pr->probreq & BLKID_PROBREQ_UUID))
+               return 0;
+
+       v = blkid_probe_assign_value(pr, "UUID");
+       if (v) {
+               memcpy((char *) v->data, str, len);
+               v->data[len - 1] = '\0';
+               v->len = len;
+               return 0;
+       }
+       return -1;
+}
+
 /* default _set_uuid function to set DCE UUIDs */
 int blkid_probe_set_uuid_as(blkid_probe pr, unsigned char *uuid, const char *name)
 {