]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
fdisk: use libfdisk public API only
authorKarel Zak <kzak@redhat.com>
Fri, 15 Aug 2014 10:16:55 +0000 (12:16 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 15 Aug 2014 10:16:55 +0000 (12:16 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
disk-utils/cfdisk.c
disk-utils/fdisk-menu.c
disk-utils/fdisk.c

index fb6e1f6252ec580c42fe5c2e018994b2e81f3e99..88f9b48fa7159516f3173cb5c50cbd523f1e14f0 100644 (file)
@@ -1613,7 +1613,7 @@ static int ui_create_label(struct cfdisk *cf)
 
        while (fdisk_next_label(cf->cxt, &lb) == 0) {
                if (fdisk_label_is_disabled(lb) ||
-                   fdisk_label_is_labeltype(lb, FDISK_DISKLABEL_BSD))
+                   fdisk_label_get_type(lb) == FDISK_DISKLABEL_BSD)
                        continue;
                cm[i++].name = fdisk_label_get_name(lb);
        }
index 29bc0159354560aecfedda12bad3f297c2555360..2233322e576722284f5f7b2ee6b10d57b3770b4e 100644 (file)
@@ -247,6 +247,16 @@ static const struct menu_entry *next_menu_entry(
                        struct fdisk_context *cxt,
                        struct menu_context *mc)
 {
+       struct fdisk_label *lb = fdisk_get_label(cxt, NULL);
+       struct fdisk_context *parent = fdisk_get_parent(cxt);
+       unsigned int type = 0, pr_type = 0;
+
+       assert(cxt);
+
+       type = fdisk_label_get_type(lb);
+       if (parent)
+               pr_type = fdisk_label_get_type(fdisk_get_label(parent, NULL));
+
        while (mc->menu_idx < ARRAY_SIZE(menus)) {
                const struct menu *m = menus[mc->menu_idx];
                const struct menu_entry *e = &(m->entries[mc->entry_idx]);
@@ -258,11 +268,11 @@ static const struct menu_entry *next_menu_entry(
                /* no more entries */
                if (e->title == NULL ||
                /* menu wanted for specified labels only */
-                   (m->label && cxt->label && !(m->label & cxt->label->id)) ||
+                   (m->label && lb && !(m->label & type)) ||
                /* unwanted for nested PT */
-                   (m->nonested && cxt->parent) ||
+                   (m->nonested && parent) ||
                /* menu excluded for specified labels */
-                   (m->exclude && cxt->label && (m->exclude & cxt->label->id))) {
+                   (m->exclude && lb && (m->exclude & type))) {
                        mc->menu_idx++;
                        mc->entry_idx = 0;
                        continue;
@@ -273,16 +283,15 @@ static const struct menu_entry *next_menu_entry(
                 */
 
                /* excluded for the current label */
-               if ((e->exclude && cxt->label && e->exclude & cxt->label->id) ||
+               if ((e->exclude && lb && e->exclude & type) ||
                /* entry wanted for specified labels only */
-                   (e->label && cxt->label && !(e->label & cxt->label->id)) ||
+                   (e->label && lb && !(e->label & type)) ||
                /* exclude non-expert entries in expect mode */
                    (e->expert == 0 && fdisk_is_details(cxt)) ||
                /* nested only */
-                   (e->parent && (!cxt->parent || cxt->parent->label->id != e->parent)) ||
+                   (e->parent && (!parent || pr_type != e->parent)) ||
                /* exclude non-normal entries in normal mode */
                    (e->normal == 0 && !fdisk_is_details(cxt))) {
-
                        mc->entry_idx++;
                        continue;
                }
@@ -367,11 +376,15 @@ static int print_fdisk_menu(struct fdisk_context *cxt)
        }
        fputc('\n', stdout);
 
-       if (cxt->parent)
+       if (fdisk_get_parent(cxt)) {
+               struct fdisk_label *l = fdisk_get_label(cxt, NULL),
+                                  *p = fdisk_get_label(fdisk_get_parent(cxt), NULL);
+
                fdisk_info(cxt, _("You're editing nested '%s' partition table, "
                                  "primary partition table is '%s'."),
-                               cxt->label->name,
-                               cxt->parent->label->name);
+                               fdisk_label_get_name(l),
+                               fdisk_label_get_name(p));
+       }
 
        return 0;
 }
@@ -454,7 +467,7 @@ static int generic_menu_cb(struct fdisk_context **cxt0,
                rc = fdisk_write_disklabel(cxt);
                if (rc)
                        err(EXIT_FAILURE, _("failed to write disklabel"));
-               if (cxt->parent)
+               if (fdisk_get_parent(cxt))
                        break; /* nested PT, don't leave */
                fdisk_info(cxt, _("The partition table has been altered."));
                rc = fdisk_reread_partition_table(cxt);
@@ -526,8 +539,8 @@ static int generic_menu_cb(struct fdisk_context **cxt0,
                break;
        case 'r':
                /* return from nested BSD to DOS */
-               if (cxt->parent) {
-                       *cxt0 = cxt->parent;
+               if (fdisk_get_parent(cxt)) {
+                       *cxt0 = fdisk_get_parent(cxt);
 
                        fdisk_info(cxt, _("Leaving nested disklabel."));
                        fdisk_free_context(cxt);
@@ -666,8 +679,8 @@ static int dos_menu_cb(struct fdisk_context **cxt0,
                break;
        case 'M':
                /* return from nested MBR to GPT */
-               if (cxt->parent) {
-                       *cxt0 = cxt->parent;
+               if (fdisk_get_parent(cxt)) {
+                       *cxt0 = fdisk_get_parent(cxt);
 
                        fdisk_info(cxt, _("Leaving nested disklabel."));
                        fdisk_free_context(cxt);
@@ -825,15 +838,15 @@ static int geo_menu_cb(struct fdisk_context **cxt0,
 
        switch (ent->key) {
        case 'c':
-               rc =  fdisk_ask_number(cxt, 1, cxt->geom.cylinders,
+               rc =  fdisk_ask_number(cxt, 1, fdisk_get_geom_cylinders(cxt),
                                1048576, _("Number of cylinders"), &c);
                break;
        case 'h':
-               rc =  fdisk_ask_number(cxt, 1, cxt->geom.heads,
+               rc =  fdisk_ask_number(cxt, 1, fdisk_get_geom_heads(cxt),
                                256, _("Number of heads"), &h);
                break;
        case 's':
-               rc =  fdisk_ask_number(cxt, 1, cxt->geom.sectors,
+               rc =  fdisk_ask_number(cxt, 1, fdisk_get_geom_sectors(cxt),
                                63, _("Number of sectors"), &s);
                break;
        }
index 3b9431af3d85fb33b8b35cc93d95703004608f02..d817afaff4fa5e6fe5ecf73f99cb6a47be1a74d1 100644 (file)
@@ -59,7 +59,7 @@ int get_user_reply(struct fdisk_context *cxt, const char *prompt,
                fflush(stdout);
 
                if (!fgets(buf, bufsz, stdin)) {
-                       if (fdisk_label_is_changed(cxt->label)) {
+                       if (fdisk_label_is_changed(fdisk_get_label(cxt, NULL))) {
                                fprintf(stderr, _("\nDo you really want to quit? "));
 
                                if (fgets(buf, bufsz, stdin) && !rpmatch(buf))
@@ -478,7 +478,6 @@ void change_partition_type(struct fdisk_context *cxt)
        const char *old = NULL;
 
        assert(cxt);
-       assert(cxt->label);
 
        if (fdisk_ask_partnum(cxt, &i, FALSE))
                return;
@@ -511,34 +510,39 @@ void list_disk_geometry(struct fdisk_context *cxt)
 {
        char *id = NULL;
        struct fdisk_label *lb = fdisk_get_label(cxt, NULL);
-       uint64_t bytes = cxt->total_sectors * cxt->sector_size;
+       uint64_t bytes = fdisk_get_nsectors(cxt) * fdisk_get_sector_size(cxt);
        char *strsz = size_to_human_string(SIZE_SUFFIX_SPACE
                                           | SIZE_SUFFIX_3LETTER, bytes);
 
        fdisk_info(cxt, _("Disk %s: %s, %ju bytes, %ju sectors"),
-                       cxt->dev_path, strsz,
-                       bytes, (uintmax_t) cxt->total_sectors);
+                       fdisk_get_devname(cxt), strsz,
+                       bytes, (uintmax_t) fdisk_get_nsectors(cxt));
        free(strsz);
 
        if (fdisk_label_require_geometry(lb) || fdisk_use_cylinders(cxt))
                fdisk_info(cxt, _("Geometry: %d heads, %llu sectors/track, %llu cylinders"),
-                              cxt->geom.heads, cxt->geom.sectors, cxt->geom.cylinders);
+                              fdisk_get_geom_heads(cxt),
+                              fdisk_get_geom_sectors(cxt),
+                              fdisk_get_geom_cylinders(cxt));
 
        fdisk_info(cxt, _("Units: %s of %d * %ld = %ld bytes"),
               fdisk_get_unit(cxt, PLURAL),
               fdisk_get_units_per_sector(cxt),
-              cxt->sector_size,
-              fdisk_get_units_per_sector(cxt) * cxt->sector_size);
+              fdisk_get_sector_size(cxt),
+              fdisk_get_units_per_sector(cxt) * fdisk_get_sector_size(cxt));
 
        fdisk_info(cxt, _("Sector size (logical/physical): %lu bytes / %lu bytes"),
-                               cxt->sector_size, cxt->phy_sector_size);
+                               fdisk_get_sector_size(cxt),
+                               fdisk_get_physector_size(cxt));
        fdisk_info(cxt, _("I/O size (minimum/optimal): %lu bytes / %lu bytes"),
-                               cxt->min_io_size, cxt->io_size);
-       if (cxt->alignment_offset)
+                               fdisk_get_minimal_iosize(cxt),
+                               fdisk_get_optimal_iosize(cxt));
+       if (fdisk_get_alignment_offset(cxt))
                fdisk_info(cxt, _("Alignment offset: %lu bytes"),
-                               cxt->alignment_offset);
+                               fdisk_get_alignment_offset(cxt));
        if (fdisk_has_label(cxt))
-               fdisk_info(cxt, _("Disklabel type: %s"), cxt->label->name);
+               fdisk_info(cxt, _("Disklabel type: %s"),
+                               fdisk_label_get_name(lb));
 
        if (fdisk_get_disklabel_id(cxt, &id) == 0 && id)
                fdisk_info(cxt, _("Disk identifier: %s"), id);
@@ -549,6 +553,7 @@ void list_disklabel(struct fdisk_context *cxt)
        struct fdisk_table *tb = NULL;
        struct fdisk_partition *pa = NULL;
        struct fdisk_iter *itr = NULL;
+       struct fdisk_label *lb;
        struct libscols_table *out = NULL;
        const char *bold = NULL;
        int *ids = NULL;                /* IDs of fdisk_fields */
@@ -581,12 +586,15 @@ void list_disklabel(struct fdisk_context *cxt)
                bold = color_scheme_get_sequence("header", UL_COLOR_BOLD);
        }
 
+       lb = fdisk_get_label(cxt, NULL);
+       assert(lb);
+
        /* define output table columns */
        for (i = 0; i < nids; i++) {
                int fl = 0;
                struct libscols_column *co;
                const struct fdisk_field *field =
-                               fdisk_label_get_field(cxt->label, ids[i]);
+                               fdisk_label_get_field(lb, ids[i]);
                if (!field)
                        goto done;
                if (fdisk_field_is_number(field))
@@ -693,15 +701,19 @@ static void dump_buffer(off_t base, unsigned char *buf, size_t sz, int all)
 static void dump_blkdev(struct fdisk_context *cxt, const char *name,
                        off_t offset, size_t size, int all)
 {
+       int fd = fdisk_get_devfd(cxt);
+
        fdisk_info(cxt, _("\n%s: offset = %ju, size = %zu bytes."),
                        name, offset, size);
 
-       if (lseek(cxt->dev_fd, offset, SEEK_SET) == (off_t) -1)
+       assert(fd >= 0);
+
+       if (lseek(fd, offset, SEEK_SET) == (off_t) -1)
                fdisk_warn(cxt, _("cannot seek"));
        else {
                unsigned char *buf = xmalloc(size);
 
-               if (read_all(cxt->dev_fd, (char *) buf, size) != (ssize_t) size)
+               if (read_all(fd, (char *) buf, size) != (ssize_t) size)
                        fdisk_warn(cxt, _("cannot read"));
                else
                        dump_buffer(offset, buf, size, all);
@@ -714,9 +726,8 @@ void dump_firstsector(struct fdisk_context *cxt)
        int all = !isatty(STDOUT_FILENO);
 
        assert(cxt);
-       assert(cxt->label);
 
-       dump_blkdev(cxt, _("First sector"), 0, cxt->sector_size, all);
+       dump_blkdev(cxt, _("First sector"), 0, fdisk_get_sector_size(cxt), all);
 }
 
 void dump_disklabel(struct fdisk_context *cxt)
@@ -728,7 +739,6 @@ void dump_disklabel(struct fdisk_context *cxt)
        size_t size = 0;
 
        assert(cxt);
-       assert(cxt->label);
 
        while (fdisk_locate_disklabel(cxt, i++, &name, &offset, &size) == 0 && size)
                dump_blkdev(cxt, name, offset, size, all);