]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: add info about display units to context
authorKarel Zak <kzak@redhat.com>
Mon, 11 Feb 2013 13:48:18 +0000 (14:48 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 11 Mar 2013 12:00:57 +0000 (13:00 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
libfdisk/src/context.c
libfdisk/src/fdiskP.h
libfdisk/src/libfdisk.h

index 4a2faf97d616c860212cc05734caa1ad708cb1a9..352f03ac35a7b83f6c8f24e5ed80fd313f9d7458 100644 (file)
@@ -201,3 +201,58 @@ int fdisk_context_set_ask(struct fdisk_context *cxt,
        cxt->ask_data = data;
        return 0;
 }
+
+
+/*
+ * @str: "cylinder" or "sector".
+ *
+ * This is pure shit, unfortunately for example Sun addresses begin of the
+ * partition by cylinders...
+ */
+int fdisk_context_set_unit(struct fdisk_context *cxt, const char *str)
+{
+       assert(cxt);
+
+       cxt->display_in_cyl_units = 0;
+
+       if (!str)
+               return 0;
+
+       if (strcmp(str, "cylinder") == 0 || strcmp(str, "cylinders") == 0)
+               cxt->display_in_cyl_units = 1;
+
+       else if (strcmp(str, "sector") == 0 || strcmp(str, "sectors") == 0)
+               cxt->display_in_cyl_units = 0;
+
+       DBG(CONTEXT, dbgprint("display unit: %s", fdisk_context_get_unit(cxt, 0)));
+       return 0;
+}
+
+const char *fdisk_context_get_unit(struct fdisk_context *cxt, int n)
+{
+       assert(cxt);
+
+       if (fdisk_context_use_cylinders(cxt))
+               return P_("cylinder", "cylinders", n);
+       return P_("sector", "sectors", n);
+}
+
+/* Returns 1 if user wants to display in cylinders. */
+int fdisk_context_use_cylinders(struct fdisk_context *cxt)
+{
+       assert(cxt);
+       return cxt->display_in_cyl_units == 1;
+}
+
+/* Returns number of "units" per sector, default is 1 if display unit is sector.
+ */
+unsigned int fdisk_context_get_units_per_sector(struct fdisk_context *cxt)
+{
+       assert(cxt);
+
+       if (fdisk_context_use_cylinders(cxt)) {
+               assert(cxt->geom.heads);
+               return cxt->geom.heads * cxt->geom.sectors;
+       }
+       return 1;
+}
index ab41740865651527fb00a839ce753a3e48f02722..4c66a5a330e33ee8e9949cce59fff4252b44d081 100644 (file)
@@ -254,6 +254,8 @@ struct fdisk_context {
        unsigned long sector_size;      /* logical size */
        unsigned long alignment_offset;
 
+       int display_in_cyl_units;       /* for obscure labels */
+
        /* alignment */
        unsigned long grain;            /* alignment unit */
        sector_t first_lba;             /* recommended begin of the first partition */
index f34d487d1076198a15241dbc652fa1024df291f1..f60b3eb553ab07d75745402da90d5daa9d380027 100644 (file)
@@ -82,6 +82,11 @@ extern struct fdisk_label *fdisk_context_get_label(struct fdisk_context *cxt,
 extern int fdisk_context_switch_label(struct fdisk_context *cxt,
                                const char *name);
 
+extern int fdisk_context_set_unit(struct fdisk_context *cxt, const char *str);
+extern const char *fdisk_context_get_unit(struct fdisk_context *cxt, int n);
+extern int fdisk_context_use_cylinders(struct fdisk_context *cxt);
+extern unsigned int fdisk_context_get_units_per_sector(struct fdisk_context *cxt);
+
 /* parttype.c */
 extern struct fdisk_parttype *fdisk_get_parttype_from_code(struct fdisk_context *cxt,
                                 unsigned int code);