int test_partnames(struct fdisk_test *ts, int argc, char *argv[]) { size_t i; const char *disk = argv[1]; for (i = 0; i < 5; i++) { char *p = fdisk_partname(disk, i + 1); if (p) printf("%zu: '%s'\n", i + 1, p); free(p); } return 0; }
/* * look for DOS partition usable for nested BSD partition table */ static int bsd_assign_dos_partition(struct fdisk_context *cxt) { struct fdisk_bsd_label *l = self_label(cxt); size_t i; for (i = 0; i < 4; i++) { sector_t ss; l->dos_part = fdisk_dos_get_partition(cxt->parent, i); if (!l->dos_part || !is_bsd_partition_type(l->dos_part->sys_ind)) continue; ss = dos_partition_get_start(l->dos_part); if (!ss) { fdisk_warnx(cxt, _("Partition %zd: has invalid starting " "sector 0."), i + 1); return -1; } if (cxt->parent->dev_path) { free(cxt->dev_path); cxt->dev_path = fdisk_partname( cxt->parent->dev_path, i + 1); } DBG(LABEL, ul_debug("partition %zu assigned to BSD", i + 1)); return 0; } fdisk_warnx(cxt, _("There is no *BSD partition on %s."), cxt->parent->dev_path); free(cxt->dev_path); cxt->dev_path = NULL; l->dos_part = NULL; return 1; }
static int sgi_list_table(struct fdisk_context *cxt) { struct tt *tb = NULL; struct sgi_disklabel *sgilabel = self_disklabel(cxt); struct sgi_device_parameter *sgiparam = &sgilabel->devparam; size_t i, used; char *p; int rc; if (fdisk_context_display_details(cxt)) fdisk_colon(cxt, _( "Label geometry: %d heads, %llu sectors\n" " %llu cylinders, %d physical cylinders\n" " %d extra sects/cyl, interleave %d:1\n"), cxt->geom.heads, cxt->geom.sectors, cxt->geom.cylinders, be16_to_cpu(sgiparam->pcylcount), (int) sgiparam->sparecyl, be16_to_cpu(sgiparam->ilfact)); /* * Partitions */ tb = tt_new_table(TT_FL_FREEDATA); if (!tb) return -ENOMEM; tt_define_column(tb, _("Pt#"), 3, TT_FL_RIGHT); tt_define_column(tb, _("Device"), 0.2, 0); tt_define_column(tb, _("Info"), 2, 0); tt_define_column(tb, _("Start"), 9, TT_FL_RIGHT); tt_define_column(tb, _("End"), 9, TT_FL_RIGHT); tt_define_column(tb, _("Sectors"), 9, TT_FL_RIGHT); tt_define_column(tb, _("Id"), 2, TT_FL_RIGHT); tt_define_column(tb, _("System"), 0.2, TT_FL_TRUNC); for (i = 0, used = 1; i < cxt->label->nparts_max; i++) { uint32_t start, len; struct fdisk_parttype *t; struct tt_line *ln; if (sgi_get_num_sectors(cxt, i) == 0) continue; ln = tt_add_line(tb, NULL); if (!ln) continue; start = sgi_get_start_sector(cxt, i); len = sgi_get_num_sectors(cxt, i); t = fdisk_get_partition_type(cxt, i); if (asprintf(&p, "%zu:", i + 1) > 0) tt_line_set_data(ln, 0, p); /* # */ p = fdisk_partname(cxt->dev_path, used++); if (p) tt_line_set_data(ln, 1, p); /* Device */ p = sgi_get_swappartition(cxt) == (int) i ? "swap" : sgi_get_bootpartition(cxt) == (int) i ? "boot" : NULL; if (p) tt_line_set_data(ln, 2, strdup(p)); /* Info */ if (asprintf(&p, "%ju", (uintmax_t) fdisk_scround(cxt, start)) > 0) tt_line_set_data(ln, 3, p); /* Start */ if (asprintf(&p, "%ju", (uintmax_t) fdisk_scround(cxt, start + len) - 1) > 0) tt_line_set_data(ln, 4, p); /* End */ if (asprintf(&p, "%ju", (uintmax_t) len) > 0) tt_line_set_data(ln, 5, p); /* Sectors*/ if (asprintf(&p, "%2x", t->type) > 0) tt_line_set_data(ln, 6, p); /* type ID */ if (t->name) tt_line_set_data(ln, 7, strdup(t->name)); /* type Name */ fdisk_free_parttype(t); } rc = fdisk_print_table(cxt, tb); tt_free_table(tb); if (rc) return rc; /* * Volumes */ tb = tt_new_table(TT_FL_FREEDATA); if (!tb) return -ENOMEM; tt_define_column(tb, _("#"), 3, TT_FL_RIGHT); tt_define_column(tb, _("Name"), 0.2, 0); tt_define_column(tb, _("Sector"), 2, TT_FL_RIGHT); tt_define_column(tb, _("Size"), 9, TT_FL_RIGHT); for (i = 0, used = 0; i < SGI_MAXVOLUMES; i++) { struct tt_line *ln; uint32_t start = be32_to_cpu(sgilabel->volume[i].block_num), len = be32_to_cpu(sgilabel->volume[i].num_bytes); if (!len) continue; ln = tt_add_line(tb, NULL); if (!ln) continue; if (asprintf(&p, "%zu:", i) > 0) tt_line_set_data(ln, 0, p); /* # */ if (*sgilabel->volume[i].name) tt_line_set_data(ln, 1, strndup((char *) sgilabel->volume[i].name, sizeof(sgilabel->volume[i].name))); /* Name */ if (asprintf(&p, "%ju", (uintmax_t) start) > 0) tt_line_set_data(ln, 2, p); /* Sector */ if (asprintf(&p, "%ju", (uintmax_t) len) > 0) tt_line_set_data(ln, 3, p); /* Size */ used++; } if (used) rc = fdisk_print_table(cxt, tb); tt_free_table(tb); fdisk_colon(cxt, _("Bootfile: %s"), sgilabel->boot_file); return rc; }
/** * fdisk_partition_to_string: * @pa: partition * @cxt: context * @id: field (FDISK_FIELD_*) * @data: returns string with allocated data * * Returns info about partition converted to printable string. * * For example * <informalexample> * <programlisting> * struct fdisk_parition *pa; * * fdisk_get_partition(cxt, 0, &pa); * fdisk_partition_to_string(pa, FDISK_FIELD_UUID, &data); * printf("first partition uuid: %s\n", data); * free(data); * fdisk_unref_partition(pa); * </programlisting> * </informalexample> * * returns UUID for the first partition. * * Returns: 0 on success, otherwise, a corresponding error. */ int fdisk_partition_to_string(struct fdisk_partition *pa, struct fdisk_context *cxt, int id, char **data) { char *p = NULL; int rc = 0; uint64_t x; if (!pa || !cxt || !data) return -EINVAL; switch (id) { case FDISK_FIELD_DEVICE: if (pa->freespace) p = strdup(_("Free space")); else if (fdisk_partition_has_partno(pa) && cxt->dev_path) { if (cxt->label->flags & FDISK_LABEL_FL_INCHARS_PARTNO) rc = asprintf(&p, "%c", (int) pa->partno + 'a'); else p = fdisk_partname(cxt->dev_path, pa->partno + 1); } break; case FDISK_FIELD_BOOT: p = fdisk_partition_is_bootable(pa) ? strdup("*") : NULL; break; case FDISK_FIELD_START: if (fdisk_partition_has_start(pa)) { x = fdisk_cround(cxt, pa->start); rc = pa->start_post ? asprintf(&p, "%ju%c", x, pa->start_post) : asprintf(&p, "%ju", x); } break; case FDISK_FIELD_END: if (fdisk_partition_has_end(pa)) { x = fdisk_cround(cxt, fdisk_partition_get_end(pa)); rc = pa->end_post ? asprintf(&p, "%ju%c", x, pa->end_post) : asprintf(&p, "%ju", x); } break; case FDISK_FIELD_SIZE: if (fdisk_partition_has_size(pa)) { uint64_t sz = pa->size * cxt->sector_size; 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; case FDISK_FIELD_CYLINDERS: rc = asprintf(&p, "%ju", (uintmax_t) fdisk_cround(cxt, fdisk_partition_has_size(pa) ? pa->size : 0)); break; case FDISK_FIELD_SECTORS: rc = asprintf(&p, "%ju", fdisk_partition_has_size(pa) ? (uintmax_t) pa->size : 0); break; case FDISK_FIELD_BSIZE: rc = asprintf(&p, "%ju", pa->bsize); break; case FDISK_FIELD_FSIZE: rc = asprintf(&p, "%ju", pa->fsize); break; case FDISK_FIELD_CPG: rc = asprintf(&p, "%ju", pa->cpg); break; case FDISK_FIELD_TYPE: p = pa->type && pa->type->name ? strdup(_(pa->type->name)) : NULL; break; case FDISK_FIELD_TYPEID: if (pa->type && fdisk_parttype_get_string(pa->type)) rc = asprintf(&p, "%s", fdisk_parttype_get_string(pa->type)); else if (pa->type) rc = asprintf(&p, "%x", fdisk_parttype_get_code(pa->type)); break; case FDISK_FIELD_UUID: p = pa->uuid && *pa->uuid? strdup(pa->uuid) : NULL; break; case FDISK_FIELD_NAME: p = pa->name && *pa->name ? strdup(pa->name) : NULL; break; case FDISK_FIELD_ATTR: p = pa->attrs && *pa->attrs ? strdup(pa->attrs) : NULL; break; case FDISK_FIELD_SADDR: p = pa->start_chs && *pa->start_chs ? strdup(pa->start_chs) : NULL; break; case FDISK_FIELD_EADDR: p = pa->end_chs && *pa->end_chs? strdup(pa->end_chs) : NULL; break; default: return -EINVAL; } if (rc < 0) { rc = -ENOMEM; free(p); p = NULL; } else if (rc > 0) rc = 0; *data = p; return rc; }
static int sun_list_disklabel(struct fdisk_context *cxt) { struct sun_disklabel *sunlabel; struct tt *tb = NULL; size_t i; int rc; assert(cxt); assert(cxt->label); assert(fdisk_is_disklabel(cxt, SUN)); sunlabel = self_disklabel(cxt); if (fdisk_context_display_details(cxt)) { fdisk_colon(cxt, _("Label geometry: %d rpm, %d alternate and %d physical cylinders,\n" " %d extra sects/cyl, interleave %d:1"), be16_to_cpu(sunlabel->rpm), be16_to_cpu(sunlabel->acyl), be16_to_cpu(sunlabel->pcyl), be16_to_cpu(sunlabel->apc), be16_to_cpu(sunlabel->intrlv)); fdisk_colon(cxt, _("Label ID: %s"), sunlabel->label_id); fdisk_colon(cxt, _("Volume ID: %s"), *sunlabel->vtoc.volume_id ? sunlabel->vtoc.volume_id : _("<none>")); } tb = tt_new_table(TT_FL_FREEDATA); if (!tb) return -ENOMEM; tt_define_column(tb, _("Device"), 0.2, 0); tt_define_column(tb, _("Flag"), 2, TT_FL_RIGHT); tt_define_column(tb, _("Start"), 9, TT_FL_RIGHT); tt_define_column(tb, _("End"), 9, TT_FL_RIGHT); /* TRANSLATORS: keep one blank space behind 'Blocks' */ tt_define_column(tb, _("Blocks "), 9, TT_FL_RIGHT); tt_define_column(tb, _("Id"), 2, TT_FL_RIGHT); tt_define_column(tb, _("System"), 0.2, TT_FL_TRUNC); for (i = 0 ; i < cxt->label->nparts_max; i++) { struct sun_partition *part = &sunlabel->partitions[i]; uint16_t flags = be16_to_cpu(sunlabel->vtoc.infos[i].flags); uint32_t start, len; struct fdisk_parttype *t; struct tt_line *ln; char *p; if (!part->num_sectors) continue; ln = tt_add_line(tb, NULL); if (!ln) continue; start = be32_to_cpu(part->start_cylinder) * cxt->geom.heads * cxt->geom.sectors; len = be32_to_cpu(part->num_sectors); t = fdisk_get_partition_type(cxt, i); p = fdisk_partname(cxt->dev_path, i + 1); if (p) tt_line_set_data(ln, 0, p); /* devname */ if ((flags & SUN_FLAG_UNMNT || flags & SUN_FLAG_RONLY) && asprintf(&p, "%c%c", flags & SUN_FLAG_UNMNT ? 'u' : ' ', flags & SUN_FLAG_RONLY ? 'r' : ' ') > 0) tt_line_set_data(ln, 1, p); /* flags */ if (asprintf(&p, "%ju", (uintmax_t) fdisk_scround(cxt, start)) > 0) tt_line_set_data(ln, 2, p); /* start */ if (asprintf(&p, "%ju", (uintmax_t) fdisk_scround(cxt, start + len - 1)) > 0) tt_line_set_data(ln, 3, p); /* end */ if (asprintf(&p, "%lu%c", (unsigned long) len / 2, len & 1 ? '+' : ' ') > 0) tt_line_set_data(ln, 4, p); /* blocks + flag */ if (asprintf(&p, "%2x", t->type) > 0) tt_line_set_data(ln, 5, p); /* type ID */ if (t->name) tt_line_set_data(ln, 6, strdup(t->name)); /* type Name */ fdisk_free_parttype(t); } rc = fdisk_print_table(cxt, tb); tt_free_table(tb); return rc; }