Merge branch 'upstream' into tizen
[platform/upstream/cryptsetup.git] / lib / utils_blkid.c
index 0e3e7fd..5a848a1 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * blkid probe utilities
  *
- * Copyright (C) 2018-2020 Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2018-2023 Red Hat, Inc. All rights reserved.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -19,6 +19,7 @@
  */
 
 #include <errno.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -72,14 +73,20 @@ void blk_set_chains_for_full_print(struct blkid_handle *h)
        blk_set_chains_for_wipes(h);
 }
 
+void blk_set_chains_for_superblocks(struct blkid_handle *h)
+{
+#ifdef HAVE_BLKID
+       blkid_probe_enable_superblocks(h->pr, 1);
+       blkid_probe_set_superblocks_flags(h->pr, BLKID_SUBLKS_TYPE);
+#endif
+}
+
 void blk_set_chains_for_fast_detection(struct blkid_handle *h)
 {
 #ifdef HAVE_BLKID
        blkid_probe_enable_partitions(h->pr, 1);
        blkid_probe_set_partitions_flags(h->pr, 0);
-
-       blkid_probe_enable_superblocks(h->pr, 1);
-       blkid_probe_set_superblocks_flags(h->pr, BLKID_SUBLKS_TYPE);
+       blk_set_chains_for_superblocks(h);
 #endif
 }
 
@@ -135,16 +142,34 @@ int blk_init_by_fd(struct blkid_handle **h, int fd)
        return r;
 }
 
-int blk_superblocks_filter_luks(struct blkid_handle *h)
-{
-       int r = -ENOTSUP;
 #ifdef HAVE_BLKID
+static int blk_superblocks_luks(struct blkid_handle *h, bool enable)
+{
        char luks[] = "crypto_LUKS";
        char *luks_filter[] = {
                luks,
                NULL
        };
-       r = blkid_probe_filter_superblocks_type(h->pr, BLKID_FLTR_NOTIN, luks_filter);
+       return blkid_probe_filter_superblocks_type(h->pr,
+                       enable ? BLKID_FLTR_ONLYIN : BLKID_FLTR_NOTIN,
+                       luks_filter);
+}
+#endif
+
+int blk_superblocks_filter_luks(struct blkid_handle *h)
+{
+       int r = -ENOTSUP;
+#ifdef HAVE_BLKID
+       r = blk_superblocks_luks(h, false);
+#endif
+       return r;
+}
+
+int blk_superblocks_only_luks(struct blkid_handle *h)
+{
+       int r = -ENOTSUP;
+#ifdef HAVE_BLKID
+       r = blk_superblocks_luks(h, true);
 #endif
        return r;
 }
@@ -308,16 +333,15 @@ int blk_supported(void)
        return r;
 }
 
-off_t blk_get_offset(struct blkid_handle *h)
+unsigned blk_get_block_size(struct blkid_handle *h)
 {
-       off_t offset_value = -1;
+       unsigned block_size = 0;
 #ifdef HAVE_BLKID
-       const char *offset;
-       if (blk_is_superblock(h)) {
-               if (!blkid_probe_lookup_value(h->pr, "SBMAGIC_OFFSET", &offset, NULL))
-                       offset_value = strtoll(offset, NULL, 10);
-       } else if (blk_is_partition(h) && !blkid_probe_lookup_value(h->pr, "PTMAGIC_OFFSET", &offset, NULL))
-               offset_value = strtoll(offset, NULL, 10);
+       const char *data;
+       if (!blk_is_superblock(h) || !blkid_probe_has_value(h->pr, "BLOCK_SIZE") ||
+           blkid_probe_lookup_value(h->pr, "BLOCK_SIZE", &data, NULL) ||
+           sscanf(data, "%u", &block_size) != 1)
+               block_size = 0;
 #endif
-       return offset_value;
+       return block_size;
 }