]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: remove label specific get_freespace
authorKarel Zak <kzak@redhat.com>
Tue, 4 Feb 2014 19:50:02 +0000 (20:50 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 11 Mar 2014 10:35:14 +0000 (11:35 +0100)
 - it seems we can use improved nested<->container relationship
   rather than implement DOS specific function

Signed-off-by: Karel Zak <kzak@redhat.com>
libfdisk/src/dos.c
libfdisk/src/fdiskP.h
libfdisk/src/libfdisk.h
libfdisk/src/partition.c
libfdisk/src/table.c

index 5d01b3fbebb7a82b72c61ed73cd54476c8c1ed1d..7f8e40cc1fe49d73503b1ac418466017a15e6db5 100644 (file)
@@ -1630,12 +1630,14 @@ static int dos_get_partition(struct fdisk_context *cxt, size_t n,
        struct dos_partition *p;
        struct pte *pe;
        unsigned int psects;
+       struct fdisk_dos_label *lb;
 
        assert(cxt);
        assert(pa);
        assert(cxt->label);
        assert(fdisk_is_disklabel(cxt, DOS));
 
+       lb = self_label(cxt);
        pe = self_pte(cxt, n);
        p = pe->pt_entry;
        pa->used = !is_cleared_partition(p);
@@ -1649,11 +1651,10 @@ static int dos_get_partition(struct fdisk_context *cxt, size_t n,
        pa->start = get_abs_partition_start(pe);
        pa->end = get_abs_partition_start(pe) + psects - (psects ? 1 : 0);
        pa->size = psects;
+       pa->container = n == lb->ext_index;
 
-       if (n >= 4) {
-               pa->parent_partno = self_label(cxt)->ext_index;
-               pa->nested = 1;
-       }
+       if (n >= 4)
+               pa->parent_partno = lb->ext_index;
 
        if (asprintf(&pa->attrs, "%02x", p->boot_ind) < 0)
                return -ENOMEM;
index 899a043b0acbf9f912a401f9b5692fe95450c2fe..6d067fbf5e9b4c345722555f136d15686be4b050 100644 (file)
@@ -187,8 +187,8 @@ struct fdisk_partition {
        unsigned int    partno_follow_default : 1,      /* use default partno */
                        start_follow_default : 1,       /* use default start */
                        end_follow_default : 1,         /* use default end */
-                       freespace : 1,          /* dthis is not partition, this is free space */
-                       nested : 1,             /* logical partition */
+                       freespace : 1,          /* this is free space */
+                       container : 1,          /* container partition (e.g. extended partition) */
                        used   : 1;             /* partition already used */
 };
 
@@ -253,8 +253,6 @@ struct fdisk_label_operations {
        int (*get_part)(struct fdisk_context *cxt,
                                                size_t n,
                                                struct fdisk_partition *pa);
-       /* add all gaps to table */
-       int (*get_freespace)(struct fdisk_context *cxt, struct fdisk_table *tb);
 
        int (*part_toggle_flag)(struct fdisk_context *cxt, size_t i, unsigned long flag);
 
index 726296c3203645a68e9e9e9b46430b84db4df3b1..ec6b87a1c6c7bdb57f02a3190e3ec78897065768 100644 (file)
@@ -205,8 +205,8 @@ extern const char *fdisk_partition_get_name(struct fdisk_partition *pa);
 extern int fdisk_partition_set_uuid(struct fdisk_partition *pa, const char *uuid);
 extern const char *fdisk_partition_get_uuid(struct fdisk_partition *pa);
 extern const char *fdisk_partition_get_attrs(struct fdisk_partition *pa);
-extern int fdisk_partition_set_nested(struct fdisk_partition *pa, int nested);
 extern int fdisk_partition_is_nested(struct fdisk_partition *pa);
+extern int fdisk_partition_is_container(struct fdisk_partition *pa);
 extern int fdisk_partition_get_parent(struct fdisk_partition *pa, size_t *parent);
 extern int fdisk_partition_is_used(struct fdisk_partition *pa);
 extern int fdisk_partition_to_string(struct fdisk_partition *pa,
index 20993ba3ab298b4b0839d64898f00ff51291a660..81594842c8bcb89484b47071b12fba1f08ddfd0c 100644 (file)
@@ -11,6 +11,7 @@ struct fdisk_partition *fdisk_new_partition(void)
        pa->refcount = 1;
        INIT_LIST_HEAD(&pa->parts);
        pa->partno = FDISK_EMPTY_PARTNO;
+       pa->parent_partno = FDISK_EMPTY_PARTNO;
        DBG(PART, dbgprint("new %p", pa));
        return pa;
 }
@@ -29,6 +30,7 @@ void fdisk_reset_partition(struct fdisk_partition *pa)
        free(pa->attrs);
        memset(pa, 0, sizeof(*pa));
        pa->partno = FDISK_EMPTY_PARTNO;
+       pa->parent_partno = FDISK_EMPTY_PARTNO;
        pa->refcount = ref;
        INIT_LIST_HEAD(&pa->parts);
 }
@@ -205,18 +207,14 @@ const char *fdisk_partition_get_attrs(struct fdisk_partition *pa)
        return pa ? pa->attrs : NULL;
 }
 
-/* nested partition means logical (within extended partition) */
-int fdisk_partition_set_nested(struct fdisk_partition *pa, int nested)
+int fdisk_partition_is_nested(struct fdisk_partition *pa)
 {
-       if (!pa)
-               return -EINVAL;
-       pa->nested = nested ? 1 : 0;
-       return 0;
+       return pa && pa->parent_partno != FDISK_EMPTY_PARTNO;
 }
 
-int fdisk_partition_is_nested(struct fdisk_partition *pa)
+int fdisk_partition_is_container(struct fdisk_partition *pa)
 {
-       return pa && pa->nested;
+       return pa && pa->container;
 }
 
 int fdisk_partition_get_parent(struct fdisk_partition *pa, size_t *parent)
index 6b8c6e3a34971c38e61a653019e3caa6dbc12b04..88e199cbf2f7057731d6080d6d9f0d9c5241031f 100644 (file)
@@ -315,11 +315,11 @@ static int table_add_freespace(
                        struct fdisk_table *tb,
                        uint64_t start,
                        uint64_t end,
-                       int dosort,
-                       struct fdisk_partition **res)
+                       struct fdisk_partition *parent)
 {
-       struct fdisk_partition *pa;
-       int rc = 0;
+       struct fdisk_partition *pa, *x, *real_parent = NULL, *best = NULL;
+       struct fdisk_iter itr;
+       int rc;
 
        assert(tb);
 
@@ -334,21 +334,33 @@ static int table_add_freespace(
        pa->end = end;
        pa->size = pa->end - pa->start + 1ULL;
 
-       if (!dosort)
-               rc = fdisk_table_add_partition(tb, pa);
-       else {
-               struct fdisk_partition *x, *best = NULL;
-               struct fdisk_iter itr;
+       fdisk_reset_iter(&itr, FDISK_ITER_FORWARD);
+
+       if (parent) {
+               pa->parent_partno = parent->partno;
 
-               fdisk_reset_iter(&itr, FDISK_ITER_FORWARD);
                while (fdisk_table_next_partition(tb, &itr, &x) == 0) {
-                       if (x->end < pa->start && (!best || best->end < x->end))
-                               best = x;
+                       if (x->partno == parent->partno) {
+                               real_parent = x;
+                               break;
+                       }
+               }
+               if (!real_parent) {
+                       DBG(LABEL, dbgprint("not found freespace parent (partno=%ju)",
+                                       parent->partno));
+                       fdisk_reset_iter(&itr, FDISK_ITER_FORWARD);
                }
-               rc = table_insert_partition(tb, best, pa);
        }
-       if (res)
-               *res = pa;
+
+       while (fdisk_table_next_partition(tb, &itr, &x) == 0) {
+               if (x->end < pa->start && (!best || best->end < x->end))
+                       best = x;
+       }
+
+       if (!best && real_parent)
+               best = real_parent;
+       rc = table_insert_partition(tb, best, pa);
+
        fdisk_unref_partition(pa);
        return rc;
 }
@@ -366,12 +378,11 @@ static int table_add_freespace(
  */
 int fdisk_get_freespaces(struct fdisk_context *cxt, struct fdisk_table **tb)
 {
-       int dosort, rc = 0;
+       int rc = 0;
        uint64_t last, grain;
        struct fdisk_table *parts = NULL;
-       struct fdisk_partition *pa;
+       struct fdisk_partition *pa, *cont = NULL;
        struct fdisk_iter itr;
-       size_t cont = FDISK_EMPTY_PARTNO;
 
        DBG(LABEL, dbgprint("get freespace"));
 
@@ -379,11 +390,6 @@ int fdisk_get_freespaces(struct fdisk_context *cxt, struct fdisk_table **tb)
                return -EINVAL;
        if (!*tb && !(*tb = fdisk_new_table()))
                return -ENOMEM;
-
-       /* label specific way */
-       if (cxt->label->op->get_freespace)
-               return cxt->label->op->get_freespace(cxt, *tb);
-
        /* generic way */
        rc = fdisk_get_partitions(cxt, &parts);
        if (rc)
@@ -391,24 +397,21 @@ int fdisk_get_freespaces(struct fdisk_context *cxt, struct fdisk_table **tb)
 
        fdisk_table_sort_partitions(parts, fdisk_partition_cmp_start);
        fdisk_reset_iter(&itr, FDISK_ITER_FORWARD);
-
-       dosort = !fdisk_table_is_empty(*tb);
        last = cxt->first_lba;
        grain = cxt->grain > cxt->sector_size ? cxt->grain / cxt->sector_size : 1;
 
        /* analyze gaps between partitions */
        while (fdisk_table_next_partition(parts, &itr, &pa) == 0) {
-               if (pa->nested)
-                       cont = (int) pa->parent_partno;
-               if (!pa->used || pa->nested)
+               if (!pa->used || fdisk_partition_is_nested(pa))
                        continue;
+               if (fdisk_partition_is_container(pa))
+                       cont = pa;
                DBG(LABEL, dbgprint("freespace analyze: partno=%zu, start=%ju, end=%ju",
                                        pa->partno, pa->start, pa->end));
                if (last + grain < pa->start) {
                        rc = table_add_freespace(cxt, *tb,
                                last + (last > cxt->first_lba ? 1 : 0),
-                               pa->start - 1,
-                               dosort, NULL);
+                               pa->start - 1, NULL);
                }
                last = pa->end;
        }
@@ -417,56 +420,39 @@ int fdisk_get_freespaces(struct fdisk_context *cxt, struct fdisk_table **tb)
        if (rc == 0 && last + grain < cxt->total_sectors - 1)
                rc = table_add_freespace(cxt, *tb,
                        last + (last > cxt->first_lba ? 1 : 0),
-                       cxt->last_lba,
-                       dosort, NULL);
+                       cxt->last_lba, NULL);
 
        /* add gaps between logical partitions */
-       if (cont != FDISK_EMPTY_PARTNO) {
+       if (cont) {
                uint64_t x;
-               struct fdisk_partition *fr;
-               struct fdisk_partition *parent =
-                               fdisk_table_get_partition(parts, cont);
-               if (!parent)
-                       goto done;
-               last = fdisk_partition_get_start(parent) + cxt->first_lba;
+
+               last = fdisk_partition_get_start(cont) + cxt->first_lba;
 
                DBG(LABEL, dbgprint("check container freespace last=%ju, "
                                    "grain=%ju, partno=%zu, start=%ju, end=%ju",
-                                   last, grain, parent->partno, parent->start,
-                                   parent->end));
+                                   last, grain, cont->partno, cont->start,
+                                   cont->end));
 
                fdisk_reset_iter(&itr, FDISK_ITER_FORWARD);
 
                while (fdisk_table_next_partition(parts, &itr, &pa) == 0) {
                        uint64_t lastfree;
 
-                       if (!pa->used || !pa->nested)
+                       if (!pa->used || !fdisk_partition_is_nested(pa))
                                continue;
                        lastfree = pa->start - 1 - cxt->first_lba;
-                       if (last + grain < lastfree) {
+                       if (last + grain < lastfree)
                                rc = table_add_freespace(cxt, *tb,
-                                               last + grain, lastfree,
-                                               dosort, &fr);
-                               if (rc == 0 && fr) {
-                                       fr->parent_partno = parent->partno;
-                                       fr->nested = 1;
-                               }
-                       }
+                                               last + grain, lastfree, cont);
                        last = pa->end;
                }
 
                /* free-space remaining in extended partition */
-               x = fdisk_partition_get_start(parent)
-                                       + fdisk_partition_get_size(parent) - 1;
-               if (last + grain < x) {
+               x = fdisk_partition_get_start(cont)
+                                       + fdisk_partition_get_size(cont) - 1;
+               if (last + grain < x)
                        rc = table_add_freespace(cxt, *tb,
-                                       last + grain, x - 1,
-                                       dosort, &fr);
-                       if (rc == 0 && fr) {
-                               fr->parent_partno = parent->partno;
-                               fr->nested = 1;
-                       }
-               }
+                                       last + grain, x - 1, cont);
        }
 
 done: