]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: improve conversion to string
authorKarel Zak <kzak@redhat.com>
Tue, 26 Nov 2013 14:28:03 +0000 (15:28 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 11 Mar 2014 10:35:12 +0000 (11:35 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
libfdisk/src/fdiskP.h
libfdisk/src/label.c
libfdisk/src/libfdisk.h
libfdisk/src/partition.c

index 16ce843775e076d8434dfee631fb61d4b25f4b82..152e88b5f47439d38ca16560ee644f0295f1b5a4 100644 (file)
@@ -130,15 +130,26 @@ struct fdisk_partition {
        struct fdisk_context    *cxt;
 
        size_t          partno;
+
        uint64_t        start;
        uint64_t        end;
        uint64_t        size;
+
        char            *name;
        char            *uuid;
        char            *attrs;
 
        struct fdisk_parttype   *type;
 
+       /* private fields */
+       char            start_post;
+       char            end_post;
+       char            size_post;
+       uint64_t        fsize;
+       uint64_t        bsize;
+       uint64_t        cpg;
+
+
        unsigned int    nested : 1,             /* logical partition */
                        used   : 1,             /* partition used */
                        endrel : 1;             /* end is specified as relative number */
index 35916ebe00928368dded8bc8a2cce738ba55fbda..b7c2964c0211806e4cff884f8b714b112d8c687f 100644 (file)
@@ -200,6 +200,7 @@ int fdisk_get_partition(struct fdisk_context *cxt, size_t partno,
 
        fdisk_reset_partition(pa);
        pa->cxt = cxt;
+       pa->partno = partno;
 
        rc = cxt->label->op->get_part(cxt, partno, pa);
        if (rc == 0 && fdisk_partition_is_used(pa))
@@ -303,7 +304,7 @@ int fdisk_list_partitions(struct fdisk_context *cxt, int *cols, size_t ncols)
 
                rc = fdisk_get_partition(cxt, i, pa);
                if (rc)
-                       goto done;
+                       continue;
                if (!fdisk_partition_is_used(pa))
                        continue;
                ln = tt_add_line(tb, NULL);
@@ -319,13 +320,15 @@ int fdisk_list_partitions(struct fdisk_context *cxt, int *cols, size_t ncols)
                                continue;
                        rc = fdisk_partition_to_string(pa, col->id, &data);
                        if (rc)
-                               goto done;
+                               continue;
                        tt_line_set_data(ln, j, data);
                }
        }
 
        if (!tt_is_empty(tb))
                rc = fdisk_print_table(cxt, tb);
+       else
+               DBG(LABEL, dbgprint("table empty, not list"));
 done:
        if (org != cols)
                free(cols);
index 9ef42c47caab9620b921cba3ad4a4f30ab6c3508..84b94e883f6107531f988a6f2cac1d7062cc584e 100644 (file)
@@ -121,7 +121,10 @@ enum {
        FDISK_COL_TYPE,
        FDISK_COL_UUID,
        FDISK_COL_NAME,
-       FDISK_COL_ATTR
+       FDISK_COL_ATTR,
+       FDISK_COL_FSIZE,
+       FDISK_COL_BSIZE,
+       FDISK_COL_CPG
 };
 
 extern int fdisk_require_geometry(struct fdisk_context *cxt);
index 91d1bff29381caaacfe47066a8eca8f1db1c0280..12ac435fa9b791d5994dd030765d98a0ddff1d68 100644 (file)
@@ -203,26 +203,41 @@ int fdisk_partition_to_string(struct fdisk_partition *pa,
 
        switch (id) {
        case FDISK_COL_DEVICE:
-               p = fdisk_partname(pa->cxt->dev_path, pa->partno + 1);
+               if (pa->cxt->label->flags & FDISK_LABEL_FL_INCHARS_PARTNO)
+                       rc = asprintf(&p, "%c", (int) pa->partno + 'a');
+               else
+                       p = fdisk_partname(pa->cxt->dev_path, pa->partno + 1);
                break;
        case FDISK_COL_START:
-               if (asprintf(&p, "%ju", pa->start) < 0)
-                       rc = -ENOMEM;
+               rc = pa->start_post ?
+                               asprintf(&p, "%ju%c", pa->start, pa->start_post) :
+                               asprintf(&p, "%ju", pa->start);
                break;
        case FDISK_COL_END:
-               if (asprintf(&p, "%ju", pa->end) < 0)
-                       rc = -ENOMEM;
+               rc = pa->end_post ?
+                               asprintf(&p, "%ju%c", pa->end, pa->end_post) :
+                               asprintf(&p, "%ju", pa->end);
                break;
        case FDISK_COL_SIZE:
                if (fdisk_context_display_details(pa->cxt)) {
-                       if (asprintf(&p, "%ju", pa->size))
-                               rc = -ENOMEM;
+                       rc = pa->size_post ?
+                                       asprintf(&p, "%ju%c", pa->size, pa->size_post) :
+                                       asprintf(&p, "%ju", pa->size);
                } else {
                        p = size_to_human_string(SIZE_SUFFIX_1LETTER, pa->size);
                        if (!p)
                                rc = -ENOMEM;
                }
                break;
+       case FDISK_COL_BSIZE:
+               rc = asprintf(&p, "%ju", pa->bsize);
+               break;
+       case FDISK_COL_FSIZE:
+               rc = asprintf(&p, "%ju", pa->fsize);
+               break;
+       case FDISK_COL_CPG:
+               rc = asprintf(&p, "%ju", pa->cpg);
+               break;
        case FDISK_COL_TYPE:
                p = pa->type && pa->type->name ? strdup(pa->type->name) : NULL;
                break;
@@ -239,6 +254,11 @@ int fdisk_partition_to_string(struct fdisk_partition *pa,
                return -EINVAL;
        }
 
+       if (rc < 0)
+               rc = -ENOMEM;
+       else if (rc > 0)
+               rc = 0;
+
        if (data)
                *data = p;
        return rc;