]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdiskL add API to print SIZE field in bytes
authorKarel Zak <kzak@redhat.com>
Mon, 19 Jan 2015 10:24:48 +0000 (11:24 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 19 Jan 2015 10:24:48 +0000 (11:24 +0100)
The patch also add --bytes to fdisk and fdisk.

Signed-off-by: Karel Zak <kzak@redhat.com>
disk-utils/fdisk.c
disk-utils/sfdisk.c
libfdisk/docs/libfdisk-sections.txt
libfdisk/src/context.c
libfdisk/src/fdiskP.h
libfdisk/src/libfdisk.h.in
libfdisk/src/libfdisk.sym
libfdisk/src/partition.c

index bc1a231e338a04979f5e6a8921dfc034b9e6dec4..6f20a310f51ec87ded3cbb99e364c0cdc15f0c7d 100644 (file)
@@ -648,6 +648,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
        fputs(_(" -t, --type <type>             recognize specified partition table type only\n"), out);
        fputs(_(" -u, --units[=<unit>]          display units: 'cylinders' or 'sectors' (default)\n"), out);
        fputs(_(" -s, --getsz                   display device size in 512-byte sectors [DEPRECATED]\n"), out);
+       fputs(_("     --bytes                   print SIZE in bytes rather than in human readable format\n"), out);
 
        fputs(USAGE_SEPARATOR, out);
        fputs(_(" -C, --cylinders <number>      specify the number of cylinders\n"), out);
@@ -677,8 +678,11 @@ int main(int argc, char **argv)
        int colormode = UL_COLORMODE_UNDEF;
        struct fdisk_context *cxt;
        char *outarg = NULL;
-
+       enum {
+               OPT_BYTES       = CHAR_MAX + 1
+       };
        static const struct option longopts[] = {
+               { "bytes",          no_argument,       NULL, OPT_BYTES },
                { "color",          optional_argument, NULL, 'L' },
                { "compatibility",  optional_argument, NULL, 'c' },
                { "cylinders",      required_argument, NULL, 'C' },
@@ -798,6 +802,9 @@ int main(int argc, char **argv)
                        return EXIT_SUCCESS;
                case 'h':
                        usage(stdout);
+               case OPT_BYTES:
+                       fdisk_set_size_unit(cxt, FDISK_SIZEUNIT_BYTES);
+                       break;
                default:
                        usage(stderr);
                }
index 499dd8f640d5d1067280b2dcc910c11bedfc4731..d609c55a6f0334781aa99a01a33e8687d63af450 100644 (file)
@@ -1335,6 +1335,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
 
        fputs(USAGE_OPTIONS, out);
        fputs(_(" -A, --append              append partitions to existing partition table\n"), out);
+       fputs(_("     --bytes               print SIZE in bytes rather than in human readable format\n"), out);
        fputs(_(" -b, --backup              backup partition table sectors (see -O)\n"), out);
        fputs(_(" -f, --force               disable all consistency checking\n"), out);
        fputs(_(" -o, --output <list>       output columns\n"), out);
@@ -1378,6 +1379,7 @@ int main(int argc, char *argv[])
                OPT_PARTLABEL,
                OPT_PARTTYPE,
                OPT_PARTATTRS,
+               OPT_BYTES
        };
 
        static const struct option longopts[] = {
@@ -1385,6 +1387,7 @@ int main(int argc, char *argv[])
                { "append",  no_argument,       NULL, 'A' },
                { "backup",  no_argument,       NULL, 'b' },
                { "backup-file", required_argument, NULL, 'O' },
+               { "bytes",   no_argument,       NULL, OPT_BYTES },
                { "dump",    no_argument,       NULL, 'd' },
                { "help",    no_argument,       NULL, 'h' },
                { "force",   no_argument,       NULL, 'f' },
@@ -1422,6 +1425,8 @@ int main(int argc, char *argv[])
        textdomain(PACKAGE);
        atexit(close_stdout);
 
+       sfdisk_init(sf);
+
        while ((c = getopt_long(argc, argv, "aAbcdfghlLo:O:nN:qsTu:vVX:Y:",
                                        longopts, &longidx)) != -1) {
                switch(c) {
@@ -1517,13 +1522,14 @@ int main(int argc, char *argv[])
                case OPT_NOREREAD:
                        sf->noreread = 1;
                        break;
-
+               case OPT_BYTES:
+                       fdisk_set_size_unit(sf->cxt, FDISK_SIZEUNIT_BYTES);
+                       break;
                default:
                        usage(stderr);
                }
        }
 
-       sfdisk_init(sf);
        if (outarg)
                init_fields(NULL, outarg, NULL);
 
index cd0f7238e011b9c96e1625d7175a0e2346273248..fdfeb8a4c8e8c55865458ab600cafdaa66703c48 100644 (file)
@@ -295,6 +295,7 @@ fdisk_get_optimal_iosize
 fdisk_get_parent
 fdisk_get_physector_size
 fdisk_get_sector_size
+fdisk_get_size_unit
 FDISK_PLURAL
 FDISK_SINGULAR
 fdisk_get_unit
@@ -309,6 +310,7 @@ fdisk_new_nested_context
 fdisk_ref_context
 fdisk_set_first_lba
 fdisk_set_last_lba
+sdisk_set_size_unit
 fdisk_set_unit
 fdisk_unref_context
 fdisk_use_cylinders
index 671c0ac7de4ebdda72a943288b69c2e6c04a93c8..94a0fb65953f756ad0bf27f44d9093383acfd371 100644 (file)
@@ -923,6 +923,35 @@ fdisk_sector_t fdisk_set_last_lba(struct fdisk_context *cxt, fdisk_sector_t lba)
        return 0;
 }
 
+/**
+ * fdisk_set_size_unit:
+ * @cxt: fdisk context
+ * @unit: FDISK_SIZEUNIT_*
+ *
+ * Sets unit for SIZE output field (see fdisk_partition_to_string()).
+ *
+ * Returns: 0 on success, <0 on error.
+ */
+int fdisk_set_size_unit(struct fdisk_context *cxt, int unit)
+{
+       assert(cxt);
+       cxt->sizeunit = unit;
+       return 0;
+}
+
+/**
+ * fdisk_get_size_unit:
+ * @cxt: fdisk context
+ *
+ * Gets unit for SIZE output field (see fdisk_partition_to_string()).
+ *
+ * Returns: unit
+ */
+int fdisk_get_size_units(struct fdisk_context *cxt)
+{
+       assert(cxt);
+       return cxt->sizeunit;
+}
 
 /**
  * fdisk_get_nsectors:
index b169a9ffb2a12035910bf9f96d1971567d34c219..59ab0c3393a7ca70f699ee4ce24d840934c08a4c 100644 (file)
@@ -351,6 +351,8 @@ struct fdisk_context {
                     display_details : 1,       /* expert display mode */
                     listonly : 1;              /* list partition, nothing else */
 
+       int sizeunit;                           /* SIZE fields, FDISK_SIZEUNIT_* */
+
        /* alignment */
        unsigned long grain;            /* alignment unit */
        fdisk_sector_t first_lba;               /* recommended begin of the first partition */
index d2d84f53935a4ae36aa33f82e3161810ebb3e625..d15d64f1c35196c465a52ef8eddf15c7628c89be 100644 (file)
@@ -204,6 +204,12 @@ unsigned int fdisk_get_geom_heads(struct fdisk_context *cxt);
 fdisk_sector_t fdisk_get_geom_sectors(struct fdisk_context *cxt);
 fdisk_sector_t fdisk_get_geom_cylinders(struct fdisk_context *cxt);
 
+enum {
+       FDISK_SIZEUNIT_HUMAN    = 0,    /* default, human readable {M,G,P,...} */
+       FDISK_SIZEUNIT_BYTES            /* bytes */
+};
+int fdisk_set_size_unit(struct fdisk_context *cxt, int unit);
+int fdisk_get_size_unit(struct fdisk_context *cxt);
 
 
 /* parttype.c */
index e8dbfd501b620f7bfb7d0c0170f85cd3fb9771ac..d8c4f66e72b9aa99e8e54f15ce09a1350f733563 100644 (file)
@@ -84,6 +84,7 @@ global:
        fdisk_get_physector_size;
        fdisk_get_script;
        fdisk_get_sector_size;
+       fdisk_get_size_unit;
        fdisk_get_unit;
        fdisk_get_units_per_sector;
        fdisk_gpt_is_hybrid;
@@ -204,6 +205,7 @@ global:
        fdisk_set_partition;
        fdisk_set_partition_type;
        fdisk_set_script;
+       fdisk_set_size_unit;
        fdisk_set_unit;
        fdisk_sgi_create_info;
        fdisk_sgi_set_bootfile;
index f6305c07deba73bc18a04d2975771163d23115ef..ba9f597a35d86066289ec8d9bfd605e5ff24e59b 100644 (file)
@@ -699,14 +699,21 @@ int fdisk_partition_to_string(struct fdisk_partition *pa,
                if (fdisk_partition_has_size(pa)) {
                        uint64_t sz = pa->size * cxt->sector_size;
 
-                       if (fdisk_is_details(cxt)) {
-                               rc = pa->size_post ?
-                                               asprintf(&p, "%ju%c", sz, pa->size_post) :
-                                               asprintf(&p, "%ju", sz);
-                       } else {
-                               p = size_to_human_string(SIZE_SUFFIX_1LETTER, sz);
-                               if (!p)
-                                       rc = -ENOMEM;
+                       switch (cxt->sizeunit) {
+                       case FDISK_SIZEUNIT_BYTES:
+                               rc = asprintf(&p, "%ju", sz);
+                               break;
+                       case FDISK_SIZEUNIT_HUMAN:
+                               if (fdisk_is_details(cxt))
+                                       rc = pa->size_post ?
+                                                       asprintf(&p, "%ju%c", sz, pa->size_post) :
+                                                       asprintf(&p, "%ju", sz);
+                               else {
+                                       p = size_to_human_string(SIZE_SUFFIX_1LETTER, sz);
+                                       if (!p)
+                                               rc = -ENOMEM;
+                               }
+                               break;
                        }
                }
                break;