fdisk: use libfdisk public API only
Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
parent
1753a234a1
commit
8eccde20dd
|
@ -1613,7 +1613,7 @@ static int ui_create_label(struct cfdisk *cf)
|
||||||
|
|
||||||
while (fdisk_next_label(cf->cxt, &lb) == 0) {
|
while (fdisk_next_label(cf->cxt, &lb) == 0) {
|
||||||
if (fdisk_label_is_disabled(lb) ||
|
if (fdisk_label_is_disabled(lb) ||
|
||||||
fdisk_label_is_labeltype(lb, FDISK_DISKLABEL_BSD))
|
fdisk_label_get_type(lb) == FDISK_DISKLABEL_BSD)
|
||||||
continue;
|
continue;
|
||||||
cm[i++].name = fdisk_label_get_name(lb);
|
cm[i++].name = fdisk_label_get_name(lb);
|
||||||
}
|
}
|
||||||
|
|
|
@ -247,6 +247,16 @@ static const struct menu_entry *next_menu_entry(
|
||||||
struct fdisk_context *cxt,
|
struct fdisk_context *cxt,
|
||||||
struct menu_context *mc)
|
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)) {
|
while (mc->menu_idx < ARRAY_SIZE(menus)) {
|
||||||
const struct menu *m = menus[mc->menu_idx];
|
const struct menu *m = menus[mc->menu_idx];
|
||||||
const struct menu_entry *e = &(m->entries[mc->entry_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 */
|
/* no more entries */
|
||||||
if (e->title == NULL ||
|
if (e->title == NULL ||
|
||||||
/* menu wanted for specified labels only */
|
/* 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 */
|
/* unwanted for nested PT */
|
||||||
(m->nonested && cxt->parent) ||
|
(m->nonested && parent) ||
|
||||||
/* menu excluded for specified labels */
|
/* menu excluded for specified labels */
|
||||||
(m->exclude && cxt->label && (m->exclude & cxt->label->id))) {
|
(m->exclude && lb && (m->exclude & type))) {
|
||||||
mc->menu_idx++;
|
mc->menu_idx++;
|
||||||
mc->entry_idx = 0;
|
mc->entry_idx = 0;
|
||||||
continue;
|
continue;
|
||||||
|
@ -273,16 +283,15 @@ static const struct menu_entry *next_menu_entry(
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* excluded for the current label */
|
/* 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 */
|
/* 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 */
|
/* exclude non-expert entries in expect mode */
|
||||||
(e->expert == 0 && fdisk_is_details(cxt)) ||
|
(e->expert == 0 && fdisk_is_details(cxt)) ||
|
||||||
/* nested only */
|
/* 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 */
|
/* exclude non-normal entries in normal mode */
|
||||||
(e->normal == 0 && !fdisk_is_details(cxt))) {
|
(e->normal == 0 && !fdisk_is_details(cxt))) {
|
||||||
|
|
||||||
mc->entry_idx++;
|
mc->entry_idx++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -367,11 +376,15 @@ static int print_fdisk_menu(struct fdisk_context *cxt)
|
||||||
}
|
}
|
||||||
fputc('\n', stdout);
|
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, "
|
fdisk_info(cxt, _("You're editing nested '%s' partition table, "
|
||||||
"primary partition table is '%s'."),
|
"primary partition table is '%s'."),
|
||||||
cxt->label->name,
|
fdisk_label_get_name(l),
|
||||||
cxt->parent->label->name);
|
fdisk_label_get_name(p));
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -454,7 +467,7 @@ static int generic_menu_cb(struct fdisk_context **cxt0,
|
||||||
rc = fdisk_write_disklabel(cxt);
|
rc = fdisk_write_disklabel(cxt);
|
||||||
if (rc)
|
if (rc)
|
||||||
err(EXIT_FAILURE, _("failed to write disklabel"));
|
err(EXIT_FAILURE, _("failed to write disklabel"));
|
||||||
if (cxt->parent)
|
if (fdisk_get_parent(cxt))
|
||||||
break; /* nested PT, don't leave */
|
break; /* nested PT, don't leave */
|
||||||
fdisk_info(cxt, _("The partition table has been altered."));
|
fdisk_info(cxt, _("The partition table has been altered."));
|
||||||
rc = fdisk_reread_partition_table(cxt);
|
rc = fdisk_reread_partition_table(cxt);
|
||||||
|
@ -526,8 +539,8 @@ static int generic_menu_cb(struct fdisk_context **cxt0,
|
||||||
break;
|
break;
|
||||||
case 'r':
|
case 'r':
|
||||||
/* return from nested BSD to DOS */
|
/* return from nested BSD to DOS */
|
||||||
if (cxt->parent) {
|
if (fdisk_get_parent(cxt)) {
|
||||||
*cxt0 = cxt->parent;
|
*cxt0 = fdisk_get_parent(cxt);
|
||||||
|
|
||||||
fdisk_info(cxt, _("Leaving nested disklabel."));
|
fdisk_info(cxt, _("Leaving nested disklabel."));
|
||||||
fdisk_free_context(cxt);
|
fdisk_free_context(cxt);
|
||||||
|
@ -666,8 +679,8 @@ static int dos_menu_cb(struct fdisk_context **cxt0,
|
||||||
break;
|
break;
|
||||||
case 'M':
|
case 'M':
|
||||||
/* return from nested MBR to GPT */
|
/* return from nested MBR to GPT */
|
||||||
if (cxt->parent) {
|
if (fdisk_get_parent(cxt)) {
|
||||||
*cxt0 = cxt->parent;
|
*cxt0 = fdisk_get_parent(cxt);
|
||||||
|
|
||||||
fdisk_info(cxt, _("Leaving nested disklabel."));
|
fdisk_info(cxt, _("Leaving nested disklabel."));
|
||||||
fdisk_free_context(cxt);
|
fdisk_free_context(cxt);
|
||||||
|
@ -825,15 +838,15 @@ static int geo_menu_cb(struct fdisk_context **cxt0,
|
||||||
|
|
||||||
switch (ent->key) {
|
switch (ent->key) {
|
||||||
case 'c':
|
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);
|
1048576, _("Number of cylinders"), &c);
|
||||||
break;
|
break;
|
||||||
case 'h':
|
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);
|
256, _("Number of heads"), &h);
|
||||||
break;
|
break;
|
||||||
case 's':
|
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);
|
63, _("Number of sectors"), &s);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,7 +59,7 @@ int get_user_reply(struct fdisk_context *cxt, const char *prompt,
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
|
|
||||||
if (!fgets(buf, bufsz, stdin)) {
|
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? "));
|
fprintf(stderr, _("\nDo you really want to quit? "));
|
||||||
|
|
||||||
if (fgets(buf, bufsz, stdin) && !rpmatch(buf))
|
if (fgets(buf, bufsz, stdin) && !rpmatch(buf))
|
||||||
|
@ -478,7 +478,6 @@ void change_partition_type(struct fdisk_context *cxt)
|
||||||
const char *old = NULL;
|
const char *old = NULL;
|
||||||
|
|
||||||
assert(cxt);
|
assert(cxt);
|
||||||
assert(cxt->label);
|
|
||||||
|
|
||||||
if (fdisk_ask_partnum(cxt, &i, FALSE))
|
if (fdisk_ask_partnum(cxt, &i, FALSE))
|
||||||
return;
|
return;
|
||||||
|
@ -511,34 +510,39 @@ void list_disk_geometry(struct fdisk_context *cxt)
|
||||||
{
|
{
|
||||||
char *id = NULL;
|
char *id = NULL;
|
||||||
struct fdisk_label *lb = fdisk_get_label(cxt, 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
|
char *strsz = size_to_human_string(SIZE_SUFFIX_SPACE
|
||||||
| SIZE_SUFFIX_3LETTER, bytes);
|
| SIZE_SUFFIX_3LETTER, bytes);
|
||||||
|
|
||||||
fdisk_info(cxt, _("Disk %s: %s, %ju bytes, %ju sectors"),
|
fdisk_info(cxt, _("Disk %s: %s, %ju bytes, %ju sectors"),
|
||||||
cxt->dev_path, strsz,
|
fdisk_get_devname(cxt), strsz,
|
||||||
bytes, (uintmax_t) cxt->total_sectors);
|
bytes, (uintmax_t) fdisk_get_nsectors(cxt));
|
||||||
free(strsz);
|
free(strsz);
|
||||||
|
|
||||||
if (fdisk_label_require_geometry(lb) || fdisk_use_cylinders(cxt))
|
if (fdisk_label_require_geometry(lb) || fdisk_use_cylinders(cxt))
|
||||||
fdisk_info(cxt, _("Geometry: %d heads, %llu sectors/track, %llu cylinders"),
|
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_info(cxt, _("Units: %s of %d * %ld = %ld bytes"),
|
||||||
fdisk_get_unit(cxt, PLURAL),
|
fdisk_get_unit(cxt, PLURAL),
|
||||||
fdisk_get_units_per_sector(cxt),
|
fdisk_get_units_per_sector(cxt),
|
||||||
cxt->sector_size,
|
fdisk_get_sector_size(cxt),
|
||||||
fdisk_get_units_per_sector(cxt) * cxt->sector_size);
|
fdisk_get_units_per_sector(cxt) * fdisk_get_sector_size(cxt));
|
||||||
|
|
||||||
fdisk_info(cxt, _("Sector size (logical/physical): %lu bytes / %lu bytes"),
|
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"),
|
fdisk_info(cxt, _("I/O size (minimum/optimal): %lu bytes / %lu bytes"),
|
||||||
cxt->min_io_size, cxt->io_size);
|
fdisk_get_minimal_iosize(cxt),
|
||||||
if (cxt->alignment_offset)
|
fdisk_get_optimal_iosize(cxt));
|
||||||
|
if (fdisk_get_alignment_offset(cxt))
|
||||||
fdisk_info(cxt, _("Alignment offset: %lu bytes"),
|
fdisk_info(cxt, _("Alignment offset: %lu bytes"),
|
||||||
cxt->alignment_offset);
|
fdisk_get_alignment_offset(cxt));
|
||||||
if (fdisk_has_label(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)
|
if (fdisk_get_disklabel_id(cxt, &id) == 0 && id)
|
||||||
fdisk_info(cxt, _("Disk identifier: %s"), 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_table *tb = NULL;
|
||||||
struct fdisk_partition *pa = NULL;
|
struct fdisk_partition *pa = NULL;
|
||||||
struct fdisk_iter *itr = NULL;
|
struct fdisk_iter *itr = NULL;
|
||||||
|
struct fdisk_label *lb;
|
||||||
struct libscols_table *out = NULL;
|
struct libscols_table *out = NULL;
|
||||||
const char *bold = NULL;
|
const char *bold = NULL;
|
||||||
int *ids = NULL; /* IDs of fdisk_fields */
|
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);
|
bold = color_scheme_get_sequence("header", UL_COLOR_BOLD);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lb = fdisk_get_label(cxt, NULL);
|
||||||
|
assert(lb);
|
||||||
|
|
||||||
/* define output table columns */
|
/* define output table columns */
|
||||||
for (i = 0; i < nids; i++) {
|
for (i = 0; i < nids; i++) {
|
||||||
int fl = 0;
|
int fl = 0;
|
||||||
struct libscols_column *co;
|
struct libscols_column *co;
|
||||||
const struct fdisk_field *field =
|
const struct fdisk_field *field =
|
||||||
fdisk_label_get_field(cxt->label, ids[i]);
|
fdisk_label_get_field(lb, ids[i]);
|
||||||
if (!field)
|
if (!field)
|
||||||
goto done;
|
goto done;
|
||||||
if (fdisk_field_is_number(field))
|
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,
|
static void dump_blkdev(struct fdisk_context *cxt, const char *name,
|
||||||
off_t offset, size_t size, int all)
|
off_t offset, size_t size, int all)
|
||||||
{
|
{
|
||||||
|
int fd = fdisk_get_devfd(cxt);
|
||||||
|
|
||||||
fdisk_info(cxt, _("\n%s: offset = %ju, size = %zu bytes."),
|
fdisk_info(cxt, _("\n%s: offset = %ju, size = %zu bytes."),
|
||||||
name, offset, size);
|
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"));
|
fdisk_warn(cxt, _("cannot seek"));
|
||||||
else {
|
else {
|
||||||
unsigned char *buf = xmalloc(size);
|
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"));
|
fdisk_warn(cxt, _("cannot read"));
|
||||||
else
|
else
|
||||||
dump_buffer(offset, buf, size, all);
|
dump_buffer(offset, buf, size, all);
|
||||||
|
@ -714,9 +726,8 @@ void dump_firstsector(struct fdisk_context *cxt)
|
||||||
int all = !isatty(STDOUT_FILENO);
|
int all = !isatty(STDOUT_FILENO);
|
||||||
|
|
||||||
assert(cxt);
|
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)
|
void dump_disklabel(struct fdisk_context *cxt)
|
||||||
|
@ -728,7 +739,6 @@ void dump_disklabel(struct fdisk_context *cxt)
|
||||||
size_t size = 0;
|
size_t size = 0;
|
||||||
|
|
||||||
assert(cxt);
|
assert(cxt);
|
||||||
assert(cxt->label);
|
|
||||||
|
|
||||||
while (fdisk_locate_disklabel(cxt, i++, &name, &offset, &size) == 0 && size)
|
while (fdisk_locate_disklabel(cxt, i++, &name, &offset, &size) == 0 && size)
|
||||||
dump_blkdev(cxt, name, offset, size, all);
|
dump_blkdev(cxt, name, offset, size, all);
|
||||||
|
|
Loading…
Reference in New Issue