chiark / gitweb /
volume_id: fat - accept empty FAT32 fsinfo signature
[elogind.git] / extras / volume_id / lib / volume_id.c
index 9b7d91d11da786ac2885f6628dd185e4870b053e..aaaab7ed747a5746be4610b8673c3b99c1a2a690 100644 (file)
@@ -83,6 +83,7 @@ static const struct prober prober_filesystem[] = {
        { volume_id_probe_squashfs, { "squashfs", } },
        { volume_id_probe_netware, { "netware", } },
        { volume_id_probe_oracleasm, { "oracleasm", } },
+       { volume_id_probe_btrfs, { "btrfs", } },
 };
 
 /* the user can overwrite this log function */
@@ -366,12 +367,14 @@ int volume_id_probe_raid(struct volume_id *id, uint64_t off, uint64_t size)
        if (!device_is_readable(id, off))
                return -1;
 
-       info("probing at offset 0x%llx, size 0x%llx\n",
-           (unsigned long long) off, (unsigned long long) size);
+       info("probing at offset 0x%" PRIx64 ", size 0x%" PRIx64 "\n", off, size);
 
-       for (i = 0; i < ARRAY_SIZE(prober_raid); i++)
-               if (prober_raid[i].prober(id, off, size) == 0)
+       for (i = 0; i < ARRAY_SIZE(prober_raid); i++) {
+               if (prober_raid[i].prober(id, off, size) == 0) {
+                       info("signature '%s' detected\n", id->type);
                        goto found;
+               }
+       }
        return -1;
 
 found:
@@ -400,14 +403,52 @@ int volume_id_probe_filesystem(struct volume_id *id, uint64_t off, uint64_t size
        if (!device_is_readable(id, off))
                return -1;
 
-       info("probing at offset 0x%llx, size 0x%llx\n",
-           (unsigned long long) off, (unsigned long long) size);
+       info("probing at offset 0x%" PRIx64 ", size 0x%" PRIx64 "\n", off, size);
+
+       /*
+        * We probe for all known filesystems to find conflicting signatures. If
+        * we find multiple matching signatures and one of the detected filesystem
+        * types claims that it can not co-exist with any other filesystem type,
+        * we do not return a probing result.
+        *
+        * We can not afford to mount a volume with the wrong filesystem code and
+        * possibly corrupt it. Linux ssytems have the problem of dozens of possible
+        * filesystem types, and volumes with left-over signatures from former
+        * filesystem types. Invalid signature need to be removed from the volume
+        * to make the filesystem detection successful.
+        *
+        * We do not want to read that many bytes from probed floppies, skip volumes
+        * smaller than a usual floppy disk.
+        */
+       if (size > 1440 * 1024) {
+               int found = 0;
+               int force_unique_result = 0;
+
+               for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++) {
+                       int match;
+
+                       match = (prober_filesystem[i].prober(id, off, size) == 0);
+                       if (match) {
+                               info("signature '%s' detected\n", id->type);
+                               if (id->force_unique_result)
+                                       force_unique_result = id->force_unique_result;
+                               if (found && force_unique_result) {
+                                       info("conflicting signatures found, skip results\n");
+                                       return -1;
+                               }
+                               found = 1;
+                       }
+               }
+       }
 
-       for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++)
-               if (prober_filesystem[i].prober(id, off, size) == 0)
+       /* return the first match */
+       for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++) {
+               if (prober_filesystem[i].prober(id, off, size) == 0) {
+                       info("signature '%s' detected\n", id->type);
                        goto found;
+               }
+       }
        return -1;
-
 found:
        /* If recognized, we free the allocated buffers */
        volume_id_free_buffer(id);
@@ -484,10 +525,9 @@ struct volume_id *volume_id_open_fd(int fd)
 {
        struct volume_id *id;
 
-       id = malloc(sizeof(struct volume_id));
+       id = calloc(1, sizeof(struct volume_id));
        if (id == NULL)
                return NULL;
-       memset(id, 0x00, sizeof(struct volume_id));
 
        id->fd = fd;