libfdisk: final parttype API cleanup
Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
parent
5ab3760071
commit
a745611d69
|
@ -1509,9 +1509,9 @@ static struct fdisk_parttype *ui_get_parttype(struct cfdisk *cf,
|
|||
{
|
||||
struct cfdisk_menuitem *d, *cm;
|
||||
size_t i = 0, nitems, idx = 0;
|
||||
struct fdisk_parttype *types, *t = NULL;
|
||||
struct fdisk_parttype *t = NULL;
|
||||
struct fdisk_label *lb;
|
||||
int has_typestr = 0;
|
||||
int codetypes = 0;
|
||||
|
||||
DBG(UI, ul_debug("asking for parttype."));
|
||||
|
||||
|
@ -1519,27 +1519,28 @@ static struct fdisk_parttype *ui_get_parttype(struct cfdisk *cf,
|
|||
|
||||
/* create cfdisk menu according to label types, note that the
|
||||
* last cm[] item has to be empty -- so nitems + 1 */
|
||||
if (fdisk_label_get_parttypes(lb, &types, &nitems) || !nitems)
|
||||
nitems = fdisk_label_get_nparttypes(lb);
|
||||
if (!nitems)
|
||||
return NULL;
|
||||
|
||||
cm = xcalloc(nitems + 1, sizeof(struct cfdisk_menuitem));
|
||||
if (!cm)
|
||||
return NULL;
|
||||
|
||||
has_typestr = fdisk_label_is_parttype_string(lb);
|
||||
codetypes = fdisk_label_has_code_parttypes(lb);
|
||||
|
||||
for (i = 0; i < nitems; i++) {
|
||||
struct fdisk_parttype *x = &types[i];
|
||||
const struct fdisk_parttype *x = fdisk_label_get_parttype(lb, i);
|
||||
char *name;
|
||||
|
||||
if (!x || !x->name)
|
||||
continue;
|
||||
cm[i].userdata = x;
|
||||
if (!has_typestr)
|
||||
xasprintf(&name, "%2x %s", x->type, _(x->name));
|
||||
cm[i].userdata = (void *) x;
|
||||
if (codetypes)
|
||||
xasprintf(&name, "%2x %s",
|
||||
fdisk_parttype_get_code(x),
|
||||
_(fdisk_parttype_get_name(x)));
|
||||
else {
|
||||
name = (char *) _(x->name);
|
||||
cm[i].desc = x->typestr;
|
||||
name = (char *) _(fdisk_parttype_get_name(x));
|
||||
cm[i].desc = fdisk_parttype_get_string(x);
|
||||
}
|
||||
cm[i].name = name;
|
||||
if (x == cur)
|
||||
|
@ -1579,7 +1580,7 @@ static struct fdisk_parttype *ui_get_parttype(struct cfdisk *cf,
|
|||
|
||||
done:
|
||||
menu_pop(cf);
|
||||
if (!has_typestr) {
|
||||
if (codetypes) {
|
||||
for (i = 0; i < nitems; i++)
|
||||
free((char *) cm[i].name);
|
||||
}
|
||||
|
|
|
@ -354,12 +354,15 @@ int ask_callback(struct fdisk_context *cxt, struct fdisk_ask *ask,
|
|||
struct fdisk_parttype *ask_partition_type(struct fdisk_context *cxt)
|
||||
{
|
||||
const char *q;
|
||||
struct fdisk_label *lb = fdisk_get_label(cxt, NULL);
|
||||
struct fdisk_label *lb;
|
||||
|
||||
if (!cxt || !cxt->label || !cxt->label->nparttypes)
|
||||
assert(cxt);
|
||||
lb = fdisk_get_label(cxt, NULL);
|
||||
|
||||
if (!lb)
|
||||
return NULL;
|
||||
|
||||
q = fdisk_label_is_parttype_string(lb) ?
|
||||
q = fdisk_label_has_code_parttypes(lb) ?
|
||||
_("Partition type (type L to list all types): ") :
|
||||
_("Hex code (type L to list all codes): ");
|
||||
do {
|
||||
|
@ -372,7 +375,7 @@ struct fdisk_parttype *ask_partition_type(struct fdisk_context *cxt)
|
|||
if (buf[1] == '\0' && toupper(*buf) == 'L')
|
||||
list_partition_types(cxt);
|
||||
else if (*buf)
|
||||
return fdisk_parse_parttype(cxt, buf);
|
||||
return fdisk_label_parse_parttype(lb, buf);
|
||||
} while (1);
|
||||
|
||||
return NULL;
|
||||
|
@ -380,16 +383,18 @@ struct fdisk_parttype *ask_partition_type(struct fdisk_context *cxt)
|
|||
|
||||
void list_partition_types(struct fdisk_context *cxt)
|
||||
{
|
||||
struct fdisk_parttype *types;
|
||||
size_t ntypes = 0;
|
||||
struct fdisk_label *lb = fdisk_get_label(cxt, NULL);
|
||||
|
||||
if (!cxt || !cxt->label || !cxt->label->parttypes)
|
||||
assert(cxt);
|
||||
lb = fdisk_get_label(cxt, NULL);
|
||||
if (!lb)
|
||||
return;
|
||||
ntypes = fdisk_label_get_nparttypes(lb);
|
||||
if (!ntypes)
|
||||
return;
|
||||
|
||||
types = cxt->label->parttypes;
|
||||
ntypes = cxt->label->nparttypes;
|
||||
|
||||
if (types[0].typestr == NULL) {
|
||||
if (fdisk_label_has_code_parttypes(lb)) {
|
||||
/*
|
||||
* Prints in 4 columns in format <hex> <name>
|
||||
*/
|
||||
|
@ -397,8 +402,6 @@ void list_partition_types(struct fdisk_context *cxt)
|
|||
int i;
|
||||
|
||||
size = ntypes;
|
||||
if (types[ntypes - 1].name == NULL)
|
||||
size--;
|
||||
|
||||
for (i = 3; i >= 0; i--)
|
||||
last[3 - i] = done += (size + i - done) / (i + 1);
|
||||
|
@ -408,16 +411,19 @@ void list_partition_types(struct fdisk_context *cxt)
|
|||
#define NAME_WIDTH 15
|
||||
char name[NAME_WIDTH * MB_LEN_MAX];
|
||||
size_t width = NAME_WIDTH;
|
||||
struct fdisk_parttype *t = &types[next];
|
||||
const struct fdisk_parttype *t = fdisk_label_get_parttype(lb, next);
|
||||
size_t ret;
|
||||
|
||||
if (t->name) {
|
||||
printf("%c%2x ", i ? ' ' : '\n', t->type);
|
||||
ret = mbsalign(_(t->name), name, sizeof(name),
|
||||
&width, MBS_ALIGN_LEFT, 0);
|
||||
printf("%c%2x ", i ? ' ' : '\n',
|
||||
fdisk_parttype_get_code(t));
|
||||
ret = mbsalign(_(fdisk_parttype_get_name(t)),
|
||||
name, sizeof(name),
|
||||
&width, MBS_ALIGN_LEFT, 0);
|
||||
|
||||
if (ret == (size_t)-1 || ret >= sizeof(name))
|
||||
printf("%-15.15s", _(t->name));
|
||||
printf("%-15.15s",
|
||||
_(fdisk_parttype_get_name(t)));
|
||||
else
|
||||
fputs(name, stdout);
|
||||
}
|
||||
|
@ -433,13 +439,13 @@ void list_partition_types(struct fdisk_context *cxt)
|
|||
/*
|
||||
* Prints 1 column in format <idx> <name> <typestr>
|
||||
*/
|
||||
struct fdisk_parttype *t;
|
||||
size_t i;
|
||||
|
||||
for (i = 0, t = types; t && i < ntypes; t++, i++) {
|
||||
if (t->name)
|
||||
printf("%3zu %-30s %s\n", i + 1,
|
||||
t->name, t->typestr);
|
||||
for (i = 0; i < ntypes; i++) {
|
||||
const struct fdisk_parttype *t = fdisk_label_get_parttype(lb, i);
|
||||
printf("%3zu %-30s %s\n", i + 1,
|
||||
fdisk_parttype_get_name(t),
|
||||
fdisk_parttype_get_string(t));
|
||||
}
|
||||
}
|
||||
putchar('\n');
|
||||
|
@ -492,7 +498,7 @@ void change_partition_type(struct fdisk_context *cxt)
|
|||
if (fdisk_set_partition_type(cxt, i, t) == 0)
|
||||
fdisk_sinfo(cxt, FDISK_INFO_SUCCESS,
|
||||
_("Changed type of partition '%s' to '%s'."),
|
||||
old, t ? t->name : _("Unknown"));
|
||||
old, t ? fdisk_parttype_get_name(t) : _("Unknown"));
|
||||
else
|
||||
fdisk_info(cxt,
|
||||
_("Type of partition %zu is unchanged: %s."),
|
||||
|
|
|
@ -110,7 +110,7 @@ static struct fdisk_parttype *bsd_partition_parttype(
|
|||
struct bsd_partition *p)
|
||||
{
|
||||
struct fdisk_parttype *t
|
||||
= fdisk_get_parttype_from_code(cxt, p->p_fstype);
|
||||
= fdisk_label_get_parttype_from_code(cxt->label, p->p_fstype);
|
||||
return t ? : fdisk_new_unknown_parttype(p->p_fstype, NULL);
|
||||
}
|
||||
|
||||
|
@ -838,14 +838,14 @@ static int bsd_set_parttype(
|
|||
struct bsd_partition *p;
|
||||
struct bsd_disklabel *d = self_disklabel(cxt);
|
||||
|
||||
if (partnum >= d->d_npartitions || !t || t->type > UINT8_MAX)
|
||||
if (partnum >= d->d_npartitions || !t || t->code > UINT8_MAX)
|
||||
return -EINVAL;
|
||||
|
||||
p = &d->d_partitions[partnum];
|
||||
if (t->type == p->p_fstype)
|
||||
if (t->code == p->p_fstype)
|
||||
return 0;
|
||||
|
||||
p->p_fstype = t->type;
|
||||
p->p_fstype = t->code;
|
||||
fdisk_label_set_changed(cxt->label, 1);
|
||||
return 0;
|
||||
}
|
||||
|
@ -912,7 +912,7 @@ struct fdisk_label *fdisk_new_bsd_label(struct fdisk_context *cxt)
|
|||
lb->id = FDISK_DISKLABEL_BSD;
|
||||
lb->op = &bsd_operations;
|
||||
lb->parttypes = bsd_fstypes;
|
||||
lb->nparttypes = ARRAY_SIZE(bsd_fstypes);
|
||||
lb->nparttypes = ARRAY_SIZE(bsd_fstypes) - 1;
|
||||
|
||||
lb->fields = bsd_fields;
|
||||
lb->nfields = ARRAY_SIZE(bsd_fields);
|
||||
|
|
|
@ -128,7 +128,7 @@ static struct fdisk_parttype *dos_partition_parttype(
|
|||
struct dos_partition *p)
|
||||
{
|
||||
struct fdisk_parttype *t
|
||||
= fdisk_get_parttype_from_code(cxt, p->sys_ind);
|
||||
= fdisk_label_get_parttype_from_code(cxt->label, p->sys_ind);
|
||||
return t ? : fdisk_new_unknown_parttype(p->sys_ind, NULL);
|
||||
}
|
||||
|
||||
|
@ -811,7 +811,8 @@ static void set_partition(struct fdisk_context *cxt,
|
|||
dos_partition_set_size(p, stop - start + 1);
|
||||
|
||||
if (!doext) {
|
||||
struct fdisk_parttype *t = fdisk_get_parttype_from_code(cxt, sysid);
|
||||
struct fdisk_parttype *t =
|
||||
fdisk_label_get_parttype_from_code(cxt->label, sysid);
|
||||
fdisk_info_new_partition(cxt, i + 1, start, stop, t);
|
||||
}
|
||||
if (is_dos_compatible(cxt) && (start/(cxt->geom.sectors*cxt->geom.heads) > 1023))
|
||||
|
@ -930,7 +931,7 @@ static int add_partition(struct fdisk_context *cxt, size_t n,
|
|||
|
||||
DBG(LABEL, ul_debug("DOS: adding partition %zu", n));
|
||||
|
||||
sys = pa && pa->type ? pa->type->type : MBR_LINUX_DATA_PARTITION;
|
||||
sys = pa && pa->type ? pa->type->code : MBR_LINUX_DATA_PARTITION;
|
||||
|
||||
if (is_used_partition(p)) {
|
||||
fdisk_warnx(cxt, _("Partition %zu is already defined. "
|
||||
|
@ -1451,7 +1452,7 @@ static int dos_add_partition(struct fdisk_context *cxt,
|
|||
struct fdisk_partition xpa = { .type = NULL };
|
||||
struct fdisk_parttype *t;
|
||||
|
||||
t = fdisk_get_parttype_from_code(cxt,
|
||||
t = fdisk_label_get_parttype_from_code(cxt->label,
|
||||
MBR_DOS_EXTENDED_PARTITION);
|
||||
if (!pa)
|
||||
pa = &xpa;
|
||||
|
@ -1587,28 +1588,28 @@ static int dos_set_parttype(
|
|||
assert(cxt->label);
|
||||
assert(fdisk_is_label(cxt, DOS));
|
||||
|
||||
if (partnum >= cxt->label->nparts_max || !t || t->type > UINT8_MAX)
|
||||
if (partnum >= cxt->label->nparts_max || !t || t->code > UINT8_MAX)
|
||||
return -EINVAL;
|
||||
|
||||
p = self_partition(cxt, partnum);
|
||||
if (t->type == p->sys_ind)
|
||||
if (t->code == p->sys_ind)
|
||||
return 0;
|
||||
|
||||
if (IS_EXTENDED(p->sys_ind) || IS_EXTENDED(t->type)) {
|
||||
if (IS_EXTENDED(p->sys_ind) || IS_EXTENDED(t->code)) {
|
||||
fdisk_warnx(cxt, _("You cannot change a partition into an "
|
||||
"extended one or vice versa. Delete it first."));
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (is_dos_partition(t->type) || is_dos_partition(p->sys_ind))
|
||||
if (is_dos_partition(t->code) || is_dos_partition(p->sys_ind))
|
||||
fdisk_info(cxt, _("If you have created or modified any DOS 6.x "
|
||||
"partitions, please see the fdisk documentation for additional "
|
||||
"information."));
|
||||
|
||||
if (!t->type)
|
||||
if (!t->code)
|
||||
fdisk_warnx(cxt, _("Type 0 means free space to many systems. "
|
||||
"Having partitions of type 0 is probably unwise."));
|
||||
p->sys_ind = t->type;
|
||||
p->sys_ind = t->code;
|
||||
|
||||
partition_set_changed(cxt, partnum, 1);
|
||||
return 0;
|
||||
|
@ -2046,7 +2047,7 @@ struct fdisk_label *fdisk_new_dos_label(struct fdisk_context *cxt)
|
|||
lb->id = FDISK_DISKLABEL_DOS;
|
||||
lb->op = &dos_operations;
|
||||
lb->parttypes = dos_parttypes;
|
||||
lb->nparttypes = ARRAY_SIZE(dos_parttypes);
|
||||
lb->nparttypes = ARRAY_SIZE(dos_parttypes) - 1;
|
||||
lb->fields = dos_fields;
|
||||
lb->nfields = ARRAY_SIZE(dos_fields);
|
||||
|
||||
|
|
|
@ -94,7 +94,7 @@ struct fdisk_iter {
|
|||
* Partition types
|
||||
*/
|
||||
struct fdisk_parttype {
|
||||
unsigned int type; /* type as number or zero */
|
||||
unsigned int code; /* type as number or zero */
|
||||
const char *name; /* description */
|
||||
char *typestr; /* type as string or NULL */
|
||||
|
||||
|
|
|
@ -321,7 +321,7 @@ static struct fdisk_parttype *gpt_partition_parttype(
|
|||
char str[37];
|
||||
|
||||
guid_to_string(&e->type, str);
|
||||
t = fdisk_get_parttype_from_string(cxt, str);
|
||||
t = fdisk_label_get_parttype_from_string(cxt->label, str);
|
||||
return t ? : fdisk_new_unknown_parttype(0, str);
|
||||
}
|
||||
|
||||
|
|
|
@ -53,43 +53,6 @@ const char *fdisk_label_get_name(struct fdisk_label *lb)
|
|||
return lb ? lb->name : NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* fdisk_label_get_parttypes:
|
||||
* @lb: label
|
||||
* @types: returns array with supported partition types
|
||||
* @ntypes: returns number of types
|
||||
*
|
||||
* Returns: 0 on success, <0 on error.
|
||||
*/
|
||||
int fdisk_label_get_parttypes(struct fdisk_label *lb,
|
||||
struct fdisk_parttype **types,
|
||||
size_t *ntypes)
|
||||
{
|
||||
if (!lb)
|
||||
return -EINVAL;
|
||||
if (types)
|
||||
*types = lb->parttypes;
|
||||
if (ntypes)
|
||||
*ntypes = lb->nparttypes;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* fdisk_label_is_parttype_string:
|
||||
* @lb: label
|
||||
*
|
||||
* Returns: 1 if the label uses strings as partition type
|
||||
* identifiers (e.g. GPT UUIDS) or 0.
|
||||
*/
|
||||
int fdisk_label_is_parttype_string(struct fdisk_label *lb)
|
||||
{
|
||||
assert(lb);
|
||||
|
||||
if (lb->parttypes && lb->parttypes[0].typestr)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* fdisk_label_require_geometry:
|
||||
* @lb: label
|
||||
|
|
|
@ -121,16 +121,27 @@ sector_t fdisk_get_nsectors(struct fdisk_context *cxt);
|
|||
const char *fdisk_get_devname(struct fdisk_context *cxt);
|
||||
|
||||
/* parttype.c */
|
||||
extern struct fdisk_parttype *fdisk_get_parttype_from_code(struct fdisk_context *cxt,
|
||||
unsigned int code);
|
||||
extern struct fdisk_parttype *fdisk_get_parttype_from_string(struct fdisk_context *cxt,
|
||||
const char *str);
|
||||
extern struct fdisk_parttype *fdisk_parse_parttype(struct fdisk_context *cxt, const char *str);
|
||||
const struct fdisk_parttype *fdisk_label_get_parttype(struct fdisk_label *lb, size_t n);
|
||||
size_t fdisk_label_get_nparttypes(struct fdisk_label *lb);
|
||||
|
||||
extern struct fdisk_parttype *fdisk_new_unknown_parttype(unsigned int type, const char *typestr);
|
||||
extern void fdisk_free_parttype(struct fdisk_parttype *type);
|
||||
int fdisk_label_has_code_parttypes(struct fdisk_label *lb);
|
||||
struct fdisk_parttype *fdisk_label_get_parttype_from_code(
|
||||
struct fdisk_label *lb,
|
||||
unsigned int code);
|
||||
|
||||
extern int fdisk_is_parttype_string(struct fdisk_context *cxt);
|
||||
struct fdisk_parttype *fdisk_label_get_parttype_from_string(
|
||||
struct fdisk_label *lb,
|
||||
const char *str);
|
||||
struct fdisk_parttype *fdisk_new_unknown_parttype(unsigned int code,
|
||||
const char *typestr);
|
||||
struct fdisk_parttype *fdisk_label_parse_parttype(
|
||||
struct fdisk_label *lb,
|
||||
const char *str);
|
||||
void fdisk_free_parttype(struct fdisk_parttype *t);
|
||||
|
||||
const char *fdisk_parttype_get_string(const struct fdisk_parttype *t);
|
||||
unsigned int fdisk_parttype_get_code(const struct fdisk_parttype *t);
|
||||
const char *fdisk_parttype_get_name(const struct fdisk_parttype *t);
|
||||
|
||||
/* label.c */
|
||||
enum {
|
||||
|
@ -179,11 +190,6 @@ extern int fdisk_delete_partition(struct fdisk_context *cxt, size_t partnum);
|
|||
extern int fdisk_set_partition_type(struct fdisk_context *cxt, size_t partnum,
|
||||
struct fdisk_parttype *t);
|
||||
|
||||
extern int fdisk_label_get_parttypes(struct fdisk_label *lb,
|
||||
struct fdisk_parttype **types,
|
||||
size_t *ntypes);
|
||||
extern int fdisk_label_is_parttype_string(struct fdisk_label *lb);
|
||||
|
||||
extern int fdisk_label_get_fields_ids(
|
||||
struct fdisk_label *lb,
|
||||
struct fdisk_context *cxt,
|
||||
|
|
|
@ -389,10 +389,10 @@ int fdisk_partition_to_string(struct fdisk_partition *pa,
|
|||
p = pa->type && pa->type->name ? strdup(pa->type->name) : NULL;
|
||||
break;
|
||||
case FDISK_FIELD_TYPEID:
|
||||
if (pa->type && pa->type->typestr)
|
||||
rc = asprintf(&p, "%s", pa->type->typestr);
|
||||
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", pa->type->type);
|
||||
rc = asprintf(&p, "%x", fdisk_parttype_get_code(pa->type));
|
||||
break;
|
||||
case FDISK_FIELD_UUID:
|
||||
p = pa->uuid ? strdup(pa->uuid) : NULL;
|
||||
|
|
|
@ -5,68 +5,117 @@
|
|||
|
||||
#include "fdiskP.h"
|
||||
|
||||
|
||||
/**
|
||||
* fdisk_get_parttype_from_code:
|
||||
* @cxt: fdisk context
|
||||
* fdisk_label_get_nparttypes:
|
||||
* @lb: label
|
||||
*
|
||||
* Returns: number of types supported by label.
|
||||
*/
|
||||
size_t fdisk_label_get_nparttypes(struct fdisk_label *lb)
|
||||
{
|
||||
if (!lb)
|
||||
return 0;
|
||||
return lb->nparttypes;
|
||||
}
|
||||
|
||||
/**
|
||||
* fdisk_label_get_parttype:
|
||||
* @lb: label
|
||||
* @n: number
|
||||
*
|
||||
* Returns: return parttype
|
||||
*/
|
||||
const struct fdisk_parttype *fdisk_label_get_parttype(struct fdisk_label *lb, size_t n)
|
||||
{
|
||||
if (!lb || n >= lb->nparttypes)
|
||||
return NULL;
|
||||
return &lb->parttypes[n];
|
||||
}
|
||||
|
||||
/**
|
||||
* fdisk_label_has_code_parttypes:
|
||||
* @lb: label
|
||||
*
|
||||
* Returns: 1 if the label uses code as partition type
|
||||
* identifiers (e.g. MBR) or 0.
|
||||
*/
|
||||
int fdisk_label_has_code_parttypes(struct fdisk_label *lb)
|
||||
{
|
||||
assert(lb);
|
||||
|
||||
if (lb->parttypes && lb->parttypes[0].typestr)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* fdisk_label_get_parttype_from_code:
|
||||
* @lb: label
|
||||
* @code: code to search for
|
||||
*
|
||||
* Search in lable-specific table of supported partition types by code.
|
||||
*
|
||||
* Returns partition type or NULL upon failure or invalid @code.
|
||||
* Returns: partition type or NULL upon failure or invalid @code.
|
||||
*/
|
||||
struct fdisk_parttype *fdisk_get_parttype_from_code(
|
||||
struct fdisk_context *cxt,
|
||||
struct fdisk_parttype *fdisk_label_get_parttype_from_code(
|
||||
struct fdisk_label *lb,
|
||||
unsigned int code)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (!cxt->label->nparttypes)
|
||||
assert(lb);
|
||||
|
||||
if (!lb->nparttypes)
|
||||
return NULL;
|
||||
|
||||
for (i = 0; i < cxt->label->nparttypes; i++)
|
||||
if (cxt->label->parttypes[i].type == code)
|
||||
return &cxt->label->parttypes[i];
|
||||
for (i = 0; i < lb->nparttypes; i++)
|
||||
if (lb->parttypes[i].code == code)
|
||||
return &lb->parttypes[i];
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* fdisk_get_parttype_from_string:
|
||||
* @cxt: fdisk context
|
||||
* fdisk_label_get_parttype_from_string:
|
||||
* @lb: label
|
||||
* @str: string to search for
|
||||
*
|
||||
* Search in lable-specific table of supported partition types by typestr.
|
||||
*
|
||||
* Returns partition type or NULL upon failure or invalid @str.
|
||||
* Returns: partition type or NULL upon failure or invalid @str.
|
||||
*/
|
||||
struct fdisk_parttype *fdisk_get_parttype_from_string(
|
||||
struct fdisk_context *cxt,
|
||||
struct fdisk_parttype *fdisk_label_get_parttype_from_string(
|
||||
struct fdisk_label *lb,
|
||||
const char *str)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (!cxt->label->nparttypes)
|
||||
assert(lb);
|
||||
|
||||
if (!lb->nparttypes)
|
||||
return NULL;
|
||||
|
||||
for (i = 0; i < cxt->label->nparttypes; i++)
|
||||
if (cxt->label->parttypes[i].typestr
|
||||
&&strcasecmp(cxt->label->parttypes[i].typestr, str) == 0)
|
||||
return &cxt->label->parttypes[i];
|
||||
for (i = 0; i < lb->nparttypes; i++)
|
||||
if (lb->parttypes[i].typestr
|
||||
&& strcasecmp(lb->parttypes[i].typestr, str) == 0)
|
||||
return &lb->parttypes[i];
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* fdisk_new_unknown_parttype:
|
||||
* @type: type as number
|
||||
* @code: type as number
|
||||
* @typestr: type as string
|
||||
|
||||
* Allocates new 'unknown' partition type. Use fdisk_free_parttype() to
|
||||
* deallocate.
|
||||
*
|
||||
* Returns newly allocated partition type, or NULL upon failure.
|
||||
* Returns: newly allocated partition type, or NULL upon failure.
|
||||
*/
|
||||
struct fdisk_parttype *fdisk_new_unknown_parttype(unsigned int type,
|
||||
struct fdisk_parttype *fdisk_new_unknown_parttype(unsigned int code,
|
||||
const char *typestr)
|
||||
{
|
||||
struct fdisk_parttype *t;
|
||||
|
@ -83,7 +132,7 @@ struct fdisk_parttype *fdisk_new_unknown_parttype(unsigned int type,
|
|||
}
|
||||
}
|
||||
t->name = _("unknown");
|
||||
t->type = type;
|
||||
t->code = code;
|
||||
t->flags |= FDISK_PARTTYPE_UNKNOWN | FDISK_PARTTYPE_ALLOCATED;
|
||||
|
||||
DBG(PARTTYPE, ul_debugobj(t, "allocated new unknown type"));
|
||||
|
@ -91,28 +140,30 @@ struct fdisk_parttype *fdisk_new_unknown_parttype(unsigned int type,
|
|||
}
|
||||
|
||||
/**
|
||||
* fdisk_parse_parttype:
|
||||
* @cxt: fdisk context
|
||||
* fdisk_label_parse_parttype:
|
||||
* @lb: label
|
||||
* @str: string to parse from
|
||||
*
|
||||
* Returns pointer to static table of the partition types, or newly allocated
|
||||
* Returns: pointer to static table of the partition types, or newly allocated
|
||||
* partition type for unknown types. It's safe to call fdisk_free_parttype()
|
||||
* for all results.
|
||||
*/
|
||||
struct fdisk_parttype *fdisk_parse_parttype(
|
||||
struct fdisk_context *cxt,
|
||||
struct fdisk_parttype *fdisk_label_parse_parttype(
|
||||
struct fdisk_label *lb,
|
||||
const char *str)
|
||||
{
|
||||
struct fdisk_parttype *types, *ret;
|
||||
unsigned int code = 0;
|
||||
char *typestr = NULL, *end = NULL;
|
||||
|
||||
if (!cxt->label->nparttypes)
|
||||
assert(lb);
|
||||
|
||||
if (!lb->nparttypes)
|
||||
return NULL;
|
||||
|
||||
DBG(CXT, ul_debugobj(cxt, "parsing '%s' partition type", str));
|
||||
DBG(LABEL, ul_debugobj(lb, "parsing '%s' partition type", str));
|
||||
|
||||
types = cxt->label->parttypes;
|
||||
types = lb->parttypes;
|
||||
|
||||
if (types[0].typestr == NULL && isxdigit(*str)) {
|
||||
|
||||
|
@ -120,17 +171,17 @@ struct fdisk_parttype *fdisk_parse_parttype(
|
|||
code = strtol(str, &end, 16);
|
||||
|
||||
if (errno || *end != '\0') {
|
||||
DBG(CXT, ul_debugobj(cxt, "parsing failed: %m"));
|
||||
DBG(LABEL, ul_debugobj(lb, "parsing failed: %m"));
|
||||
return NULL;
|
||||
}
|
||||
ret = fdisk_get_parttype_from_code(cxt, code);
|
||||
ret = fdisk_label_get_parttype_from_code(lb, code);
|
||||
if (ret)
|
||||
goto done;
|
||||
} else {
|
||||
int i;
|
||||
|
||||
/* maybe specified by type string (e.g. UUID) */
|
||||
ret = fdisk_get_parttype_from_string(cxt, str);
|
||||
ret = fdisk_label_get_parttype_from_string(lb, str);
|
||||
if (ret)
|
||||
goto done;
|
||||
|
||||
|
@ -138,7 +189,7 @@ struct fdisk_parttype *fdisk_parse_parttype(
|
|||
errno = 0;
|
||||
i = strtol(str, &end, 0);
|
||||
if (errno == 0 && *end == '\0' && i > 0
|
||||
&& i - 1 < (int) cxt->label->nparttypes) {
|
||||
&& i - 1 < (int) lb->nparttypes) {
|
||||
ret = &types[i - 1];
|
||||
goto done;
|
||||
}
|
||||
|
@ -146,7 +197,7 @@ struct fdisk_parttype *fdisk_parse_parttype(
|
|||
|
||||
ret = fdisk_new_unknown_parttype(code, typestr);
|
||||
done:
|
||||
DBG(PARTTYPE, ul_debugobj(ret, "returns '%s' partition type", ret->name));
|
||||
DBG(PARTTYPE, ul_debugobj(ret, "returns parsed '%s' partition type", ret->name));
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -165,4 +216,23 @@ void fdisk_free_parttype(struct fdisk_parttype *t)
|
|||
}
|
||||
}
|
||||
|
||||
const char *fdisk_parttype_get_string(const struct fdisk_parttype *t)
|
||||
{
|
||||
assert(t);
|
||||
return t->typestr && *t->typestr ? t->typestr : NULL;
|
||||
}
|
||||
|
||||
unsigned int fdisk_parttype_get_code(const struct fdisk_parttype *t)
|
||||
{
|
||||
assert(t);
|
||||
return t->code;
|
||||
}
|
||||
|
||||
const char *fdisk_parttype_get_name(const struct fdisk_parttype *t)
|
||||
{
|
||||
assert(t);
|
||||
return t->name;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -308,7 +308,7 @@ static struct fdisk_parttype *sgi_get_parttype(struct fdisk_context *cxt, size_t
|
|||
if (n >= cxt->label->nparts_max)
|
||||
return NULL;
|
||||
|
||||
t = fdisk_get_parttype_from_code(cxt, sgi_get_sysid(cxt, n));
|
||||
t = fdisk_label_get_parttype_from_code(cxt->label, sgi_get_sysid(cxt, n));
|
||||
return t ? : fdisk_new_unknown_parttype(sgi_get_sysid(cxt, n), NULL);
|
||||
}
|
||||
|
||||
|
@ -329,7 +329,7 @@ static int sgi_get_partition(struct fdisk_context *cxt, size_t n, struct fdisk_p
|
|||
pa->start = start;
|
||||
pa->end = start + len - (len ? 1 : 0);
|
||||
|
||||
if (pa->type && pa->type->type == SGI_TYPE_ENTIRE_DISK)
|
||||
if (pa->type && pa->type->code == SGI_TYPE_ENTIRE_DISK)
|
||||
pa->wholedisk = 1;
|
||||
|
||||
pa->attrs = sgi_get_swappartition(cxt) == (int) n ? "swap" :
|
||||
|
@ -707,7 +707,8 @@ static int sgi_set_partition(struct fdisk_context *cxt, size_t i,
|
|||
if (sgi_gaps(cxt) < 0) /* rebuild freelist */
|
||||
fdisk_warnx(cxt, _("Partition overlap on the disk."));
|
||||
if (length) {
|
||||
struct fdisk_parttype *t = fdisk_get_parttype_from_code(cxt, sys);
|
||||
struct fdisk_parttype *t =
|
||||
fdisk_label_get_parttype_from_code(cxt->label, sys);
|
||||
fdisk_info_new_partition(cxt, i + 1, start, start + length, t);
|
||||
}
|
||||
|
||||
|
@ -764,7 +765,7 @@ static int sgi_add_partition(struct fdisk_context *cxt,
|
|||
char mesg[256];
|
||||
unsigned int first = 0, last = 0;
|
||||
struct fdisk_ask *ask;
|
||||
int sys = pa && pa->type ? pa->type->type : SGI_TYPE_XFS;
|
||||
int sys = pa && pa->type ? pa->type->code : SGI_TYPE_XFS;
|
||||
int rc;
|
||||
size_t n;
|
||||
|
||||
|
@ -1003,7 +1004,7 @@ static int sgi_set_parttype(struct fdisk_context *cxt,
|
|||
{
|
||||
struct sgi_disklabel *sgilabel;
|
||||
|
||||
if (i >= cxt->label->nparts_max || !t || t->type > UINT32_MAX)
|
||||
if (i >= cxt->label->nparts_max || !t || t->code > UINT32_MAX)
|
||||
return -EINVAL;
|
||||
|
||||
if (sgi_get_num_sectors(cxt, i) == 0) /* caught already before, ... */ {
|
||||
|
@ -1011,13 +1012,13 @@ static int sgi_set_parttype(struct fdisk_context *cxt,
|
|||
return -EINVAL;
|
||||
}
|
||||
|
||||
if ((i == 10 && t->type != SGI_TYPE_ENTIRE_DISK)
|
||||
|| (i == 8 && t->type != 0))
|
||||
if ((i == 10 && t->code != SGI_TYPE_ENTIRE_DISK)
|
||||
|| (i == 8 && t->code != 0))
|
||||
fdisk_info(cxt, _("Consider leaving partition 9 as volume header (0), "
|
||||
"and partition 11 as entire volume (6), "
|
||||
"as IRIX expects it."));
|
||||
|
||||
if (((t->type != SGI_TYPE_ENTIRE_DISK) && (t->type != SGI_TYPE_VOLHDR))
|
||||
if (((t->code != SGI_TYPE_ENTIRE_DISK) && (t->code != SGI_TYPE_VOLHDR))
|
||||
&& (sgi_get_start_sector(cxt, i) < 1)) {
|
||||
int yes = 0;
|
||||
fdisk_ask_yesno(cxt,
|
||||
|
@ -1031,7 +1032,7 @@ static int sgi_set_parttype(struct fdisk_context *cxt,
|
|||
}
|
||||
|
||||
sgilabel = self_disklabel(cxt);
|
||||
sgilabel->partitions[i].type = cpu_to_be32(t->type);
|
||||
sgilabel->partitions[i].type = cpu_to_be32(t->code);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -1129,7 +1130,7 @@ struct fdisk_label *fdisk_new_sgi_label(struct fdisk_context *cxt)
|
|||
lb->id = FDISK_DISKLABEL_SGI;
|
||||
lb->op = &sgi_operations;
|
||||
lb->parttypes = sgi_parttypes;
|
||||
lb->nparttypes = ARRAY_SIZE(sgi_parttypes);
|
||||
lb->nparttypes = ARRAY_SIZE(sgi_parttypes) - 1;
|
||||
lb->fields = sgi_fields;
|
||||
lb->nfields = ARRAY_SIZE(sgi_fields);
|
||||
|
||||
|
|
|
@ -72,7 +72,8 @@ static void set_sun_partition(struct fdisk_context *cxt, size_t i,
|
|||
uint32_t start,uint32_t stop, uint16_t sysid)
|
||||
{
|
||||
struct sun_disklabel *sunlabel = self_disklabel(cxt);
|
||||
struct fdisk_parttype *t = fdisk_get_parttype_from_code(cxt, sysid);
|
||||
struct fdisk_parttype *t =
|
||||
fdisk_label_get_parttype_from_code(cxt->label, sysid);
|
||||
|
||||
sunlabel->vtoc.infos[i].id = cpu_to_be16(sysid);
|
||||
sunlabel->vtoc.infos[i].flags = cpu_to_be16(0);
|
||||
|
@ -481,7 +482,7 @@ static int sun_add_partition(
|
|||
struct sun_info *info;
|
||||
uint32_t start, stop, stop2;
|
||||
int whole_disk = 0;
|
||||
int sys = pa && pa->type ? pa->type->type : SUN_TAG_LINUX_NATIVE;
|
||||
int sys = pa && pa->type ? pa->type->code : SUN_TAG_LINUX_NATIVE;
|
||||
int rc;
|
||||
size_t n;
|
||||
|
||||
|
@ -753,7 +754,8 @@ static struct fdisk_parttype *sun_get_parttype(
|
|||
if (n >= cxt->label->nparts_max)
|
||||
return NULL;
|
||||
|
||||
t = fdisk_get_parttype_from_code(cxt, be16_to_cpu(sunlabel->vtoc.infos[n].id));
|
||||
t = fdisk_label_get_parttype_from_code(cxt->label,
|
||||
be16_to_cpu(sunlabel->vtoc.infos[n].id));
|
||||
return t ? : fdisk_new_unknown_parttype(be16_to_cpu(sunlabel->vtoc.infos[n].id), NULL);
|
||||
}
|
||||
|
||||
|
@ -786,7 +788,7 @@ static int sun_get_partition(struct fdisk_context *cxt, size_t n,
|
|||
len = be32_to_cpu(part->num_sectors);
|
||||
|
||||
pa->type = sun_get_parttype(cxt, n);
|
||||
if (pa->type && pa->type->type == SUN_TAG_WHOLEDISK)
|
||||
if (pa->type && pa->type->code == SUN_TAG_WHOLEDISK)
|
||||
pa->wholedisk = 1;
|
||||
|
||||
if (flags & SUN_FLAG_UNMNT || flags & SUN_FLAG_RONLY) {
|
||||
|
@ -929,17 +931,17 @@ static int sun_set_parttype(
|
|||
|
||||
sunlabel = self_disklabel(cxt);
|
||||
|
||||
if (i >= cxt->label->nparts_max || !t || t->type > UINT16_MAX)
|
||||
if (i >= cxt->label->nparts_max || !t || t->code > UINT16_MAX)
|
||||
return -EINVAL;
|
||||
|
||||
if (i == 2 && t->type != SUN_TAG_WHOLEDISK)
|
||||
if (i == 2 && t->code != SUN_TAG_WHOLEDISK)
|
||||
fdisk_info(cxt, _("Consider leaving partition 3 as Whole disk (5),\n"
|
||||
"as SunOS/Solaris expects it and even Linux likes it.\n"));
|
||||
|
||||
part = &sunlabel->partitions[i];
|
||||
info = &sunlabel->vtoc.infos[i];
|
||||
|
||||
if (t->type == SUN_TAG_LINUX_SWAP && !part->start_cylinder) {
|
||||
if (t->code == SUN_TAG_LINUX_SWAP && !part->start_cylinder) {
|
||||
int yes, rc;
|
||||
rc = fdisk_ask_yesno(cxt,
|
||||
_("It is highly recommended that the partition at offset 0\n"
|
||||
|
@ -952,7 +954,7 @@ static int sun_set_parttype(
|
|||
return 1;
|
||||
}
|
||||
|
||||
switch (t->type) {
|
||||
switch (t->code) {
|
||||
case SUN_TAG_SWAP:
|
||||
case SUN_TAG_LINUX_SWAP:
|
||||
/* swaps are not mountable by default */
|
||||
|
@ -964,7 +966,7 @@ static int sun_set_parttype(
|
|||
info->flags &= ~cpu_to_be16(SUN_FLAG_UNMNT);
|
||||
break;
|
||||
}
|
||||
info->id = cpu_to_be16(t->type);
|
||||
info->id = cpu_to_be16(t->code);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -1045,7 +1047,7 @@ struct fdisk_label *fdisk_new_sun_label(struct fdisk_context *cxt)
|
|||
lb->id = FDISK_DISKLABEL_SUN;
|
||||
lb->op = &sun_operations;
|
||||
lb->parttypes = sun_parttypes;
|
||||
lb->nparttypes = ARRAY_SIZE(sun_parttypes);
|
||||
lb->nparttypes = ARRAY_SIZE(sun_parttypes) - 1;
|
||||
lb->fields = sun_fields;
|
||||
lb->nfields = ARRAY_SIZE(sun_fields);
|
||||
lb->flags |= FDISK_LABEL_FL_REQUIRE_GEOMETRY;
|
||||
|
|
Loading…
Reference in New Issue