chiark / gitweb /
delete vol_id and require util-linux-ng's blkid
[elogind.git] / extras / volume_id / lib / ntfs.c
index f63804d711f388e7cad9cdb7d2d241cc2a474333..f727634094916c08e1d831eb4653c35ff2b82976 100644 (file)
@@ -3,19 +3,24 @@
  *
  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
  *
- *     This program is free software; you can redistribute it and/or modify it
- *     under the terms of the GNU General Public License as published by the
- *     Free Software Foundation version 2 of the License.
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 #ifndef _GNU_SOURCE
 #define _GNU_SOURCE 1
 #endif
 
-#ifdef HAVE_CONFIG_H
-#  include <config.h>
-#endif
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -24,7 +29,7 @@
 #include <ctype.h>
 
 #include "libvolume_id.h"
-#include "util.h"
+#include "libvolume_id-private.h"
 
 static struct ntfs_super_block {
        uint8_t         jump[3];
@@ -92,6 +97,7 @@ static struct volume_info {
 
 int volume_id_probe_ntfs(struct volume_id *id, uint64_t off, uint64_t size)
 {
+       uint8_t volume_serial[8];
        unsigned int sector_size;
        unsigned int cluster_size;
        uint64_t mft_cluster;
@@ -105,7 +111,7 @@ int volume_id_probe_ntfs(struct volume_id *id, uint64_t off, uint64_t size)
        const uint8_t *buf;
        const uint8_t *val;
 
-       info("probing at offset 0x%llx", (unsigned long long) off);
+       info("probing at offset 0x%" PRIx64 "\n", off);
 
        ns = (struct ntfs_super_block *) volume_id_get_buffer(id, off, 0x200);
        if (ns == NULL)
@@ -114,7 +120,7 @@ int volume_id_probe_ntfs(struct volume_id *id, uint64_t off, uint64_t size)
        if (memcmp(ns->oem_id, "NTFS", 4) != 0)
                return -1;
 
-       volume_id_set_uuid(id, ns->volume_serial, 0, UUID_64BIT_LE);
+       memcpy(volume_serial, ns->volume_serial, sizeof(volume_serial));
 
        sector_size = le16_to_cpu(ns->bytes_per_sector);
        if (sector_size < 0x200)
@@ -130,12 +136,12 @@ int volume_id_probe_ntfs(struct volume_id *id, uint64_t off, uint64_t size)
        else
                mft_record_size = ns->cluster_per_mft_record * cluster_size;
 
-       dbg("sectorsize  0x%x", sector_size);
-       dbg("clustersize 0x%x", cluster_size);
-       dbg("mftcluster  %llu", (unsigned long long) mft_cluster);
-       dbg("mftoffset  0x%llx", (unsigned long long) mft_off);
-       dbg("cluster per mft_record  %i", ns->cluster_per_mft_record);
-       dbg("mft record size  %i", mft_record_size);
+       dbg("sectorsize  0x%x\n", sector_size);
+       dbg("clustersize 0x%x\n", cluster_size);
+       dbg("mftcluster  %" PRIu64 "\n", mft_cluster);
+       dbg("mftoffset  0x%" PRIx64 "\n", mft_off);
+       dbg("cluster per mft_record  %i\n", ns->cluster_per_mft_record);
+       dbg("mft record size  %i\n", mft_record_size);
 
        buf = volume_id_get_buffer(id, off + mft_off + (MFT_RECORD_VOLUME * mft_record_size),
                         mft_record_size);
@@ -143,12 +149,12 @@ int volume_id_probe_ntfs(struct volume_id *id, uint64_t off, uint64_t size)
                return -1;
 
        mftr = (struct master_file_table_record*) buf;
-       dbg("mftr->magic '%c%c%c%c'", mftr->magic[0], mftr->magic[1], mftr->magic[2], mftr->magic[3]);
+       dbg("mftr->magic '%c%c%c%c'\n", mftr->magic[0], mftr->magic[1], mftr->magic[2], mftr->magic[3]);
        if (memcmp(mftr->magic, "FILE", 4) != 0)
                return -1;
 
        attr_off = le16_to_cpu(mftr->attrs_offset);
-       dbg("file $Volume's attributes are at offset %i", attr_off);
+       dbg("file $Volume's attributes are at offset %i\n", attr_off);
 
        while (1) {
                attr = (struct file_attribute*) &buf[attr_off];
@@ -167,18 +173,18 @@ int volume_id_probe_ntfs(struct volume_id *id, uint64_t off, uint64_t size)
                if (attr_type == MFT_RECORD_ATTR_END)
                        break;
 
-               dbg("found attribute type 0x%x, len %i, at offset %i",
+               dbg("found attribute type 0x%x, len %i, at offset %i\n",
                    attr_type, attr_len, attr_off);
 
                if (attr_type == MFT_RECORD_ATTR_VOLUME_INFO) {
-                       dbg("found info, len %i", val_len);
+                       dbg("found info, len %i\n", val_len);
                        info = (struct volume_info*) (((uint8_t *) attr) + val_off);
                        snprintf(id->type_version, sizeof(id->type_version)-1,
                                 "%u.%u", info->major_ver, info->minor_ver);
                }
 
                if (attr_type == MFT_RECORD_ATTR_VOLUME_NAME) {
-                       dbg("found label, len %i", val_len);
+                       dbg("found label, len %i\n", val_len);
                        if (val_len > VOLUME_ID_LABEL_SIZE)
                                val_len = VOLUME_ID_LABEL_SIZE;
 
@@ -188,8 +194,10 @@ int volume_id_probe_ntfs(struct volume_id *id, uint64_t off, uint64_t size)
                }
        }
 
+       volume_id_set_uuid(id, volume_serial, 0, UUID_64BIT_LE);
        volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
        id->type = "ntfs";
-
+       /* we think this is ntfs, but we make sure no other signatures are found */
+       id->force_unique_result = 1;
        return 0;
 }