libfdisk: fix get_partition_unused_primary()
Was: Mentioned function returns -1 if adding of primary partition is impossible. Caller treats this value as size_t (res variable) and then compares it for negative values, totally ignoring errors. Becomes: Now function takes address to variable and fills it with partition number. Caller treats return value as int and use it appropriately. [kzak@redhat.com: - don't mix return code and partno at all] Signed-off-by: Boris Egorov <egorov@linux.com> Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
parent
1ce1a112c4
commit
44374b10d1
|
@ -186,24 +186,28 @@ static int is_cleared_partition(struct dos_partition *p)
|
||||||
}
|
}
|
||||||
|
|
||||||
static int get_partition_unused_primary(struct fdisk_context *cxt,
|
static int get_partition_unused_primary(struct fdisk_context *cxt,
|
||||||
struct fdisk_partition *pa)
|
struct fdisk_partition *pa,
|
||||||
|
size_t *partno)
|
||||||
{
|
{
|
||||||
size_t org = cxt->label->nparts_max, n;
|
size_t org, n;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
|
assert(cxt);
|
||||||
|
assert(cxt->label);
|
||||||
|
assert(partno);
|
||||||
|
|
||||||
|
org = cxt->label->nparts_max;
|
||||||
|
|
||||||
cxt->label->nparts_max = 4;
|
cxt->label->nparts_max = 4;
|
||||||
rc = fdisk_partition_next_partno(pa, cxt, &n);
|
rc = fdisk_partition_next_partno(pa, cxt, &n);
|
||||||
cxt->label->nparts_max = org;
|
cxt->label->nparts_max = org;
|
||||||
|
|
||||||
switch (rc) {
|
if (rc == 1) {
|
||||||
case 1:
|
|
||||||
fdisk_info(cxt, _("All primary partitions have been defined already."));
|
fdisk_info(cxt, _("All primary partitions have been defined already."));
|
||||||
return -1;
|
rc = -1;
|
||||||
case 0:
|
} else if (rc == 0)
|
||||||
return n;
|
*partno = n;
|
||||||
default:
|
return rc;
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int seek_sector(struct fdisk_context *cxt, sector_t secno)
|
static int seek_sector(struct fdisk_context *cxt, sector_t secno)
|
||||||
|
@ -1464,8 +1468,8 @@ static int dos_add_partition(struct fdisk_context *cxt,
|
||||||
fdisk_warnx(cxt, _("Extended partition already exists."));
|
fdisk_warnx(cxt, _("Extended partition already exists."));
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
res = get_partition_unused_primary(cxt, pa);
|
rc = get_partition_unused_primary(cxt, pa, &res);
|
||||||
if (res >= 0) {
|
if (rc == 0) {
|
||||||
rc = add_partition(cxt, res, pa);
|
rc = add_partition(cxt, res, pa);
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
@ -1473,8 +1477,8 @@ static int dos_add_partition(struct fdisk_context *cxt,
|
||||||
/* pa specifies start, but outside extended partition */
|
/* pa specifies start, but outside extended partition */
|
||||||
} else if (pa && fdisk_partition_has_start(pa) && l->ext_offset) {
|
} else if (pa && fdisk_partition_has_start(pa) && l->ext_offset) {
|
||||||
DBG(LABEL, ul_debug("DOS: pa template %p: add primary", pa));
|
DBG(LABEL, ul_debug("DOS: pa template %p: add primary", pa));
|
||||||
res = get_partition_unused_primary(cxt, pa);
|
rc = get_partition_unused_primary(cxt, pa, &res);
|
||||||
if (res >= 0) {
|
if (rc == 0) {
|
||||||
rc = add_partition(cxt, res, pa);
|
rc = add_partition(cxt, res, pa);
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
@ -1531,8 +1535,8 @@ static int dos_add_partition(struct fdisk_context *cxt,
|
||||||
} else if (cxt->label->nparts_max >= MAXIMUM_PARTS) {
|
} else if (cxt->label->nparts_max >= MAXIMUM_PARTS) {
|
||||||
fdisk_info(cxt, _("All logical partitions are in use. "
|
fdisk_info(cxt, _("All logical partitions are in use. "
|
||||||
"Adding a primary partition."));
|
"Adding a primary partition."));
|
||||||
res = get_partition_unused_primary(cxt, pa);
|
rc = get_partition_unused_primary(cxt, pa, &res);
|
||||||
if (res >= 0)
|
if (rc == 0)
|
||||||
rc = add_partition(cxt, res, pa);
|
rc = add_partition(cxt, res, pa);
|
||||||
} else {
|
} else {
|
||||||
char hint[BUFSIZ];
|
char hint[BUFSIZ];
|
||||||
|
@ -1541,8 +1545,8 @@ static int dos_add_partition(struct fdisk_context *cxt,
|
||||||
|
|
||||||
/* the default layout for scripts is to create primary partitions */
|
/* the default layout for scripts is to create primary partitions */
|
||||||
if (cxt->script) {
|
if (cxt->script) {
|
||||||
res = get_partition_unused_primary(cxt, pa);
|
rc = get_partition_unused_primary(cxt, pa, &res);
|
||||||
if (res >= 0)
|
if (rc == 0)
|
||||||
rc = add_partition(cxt, res, pa);
|
rc = add_partition(cxt, res, pa);
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
@ -1573,16 +1577,16 @@ static int dos_add_partition(struct fdisk_context *cxt,
|
||||||
fdisk_free_ask(ask);
|
fdisk_free_ask(ask);
|
||||||
|
|
||||||
if (c == 'p') {
|
if (c == 'p') {
|
||||||
res = get_partition_unused_primary(cxt, pa);
|
rc = get_partition_unused_primary(cxt, pa, &res);
|
||||||
if (res >= 0)
|
if (rc == 0)
|
||||||
rc = add_partition(cxt, res, pa);
|
rc = add_partition(cxt, res, pa);
|
||||||
goto done;
|
goto done;
|
||||||
} else if (c == 'l' && l->ext_offset) {
|
} else if (c == 'l' && l->ext_offset) {
|
||||||
rc = add_logical(cxt, pa, &res);
|
rc = add_logical(cxt, pa, &res);
|
||||||
goto done;
|
goto done;
|
||||||
} else if (c == 'e' && !l->ext_offset) {
|
} else if (c == 'e' && !l->ext_offset) {
|
||||||
res = get_partition_unused_primary(cxt, pa);
|
rc = get_partition_unused_primary(cxt, pa, &res);
|
||||||
if (res >= 0) {
|
if (rc == 0) {
|
||||||
struct fdisk_partition *xpa = NULL;
|
struct fdisk_partition *xpa = NULL;
|
||||||
struct fdisk_parttype *t;
|
struct fdisk_parttype *t;
|
||||||
|
|
||||||
|
@ -1607,7 +1611,7 @@ static int dos_add_partition(struct fdisk_context *cxt,
|
||||||
done:
|
done:
|
||||||
if (rc == 0) {
|
if (rc == 0) {
|
||||||
cxt->label->nparts_cur++;
|
cxt->label->nparts_cur++;
|
||||||
if (partno && res >= 0)
|
if (partno)
|
||||||
*partno = res;
|
*partno = res;
|
||||||
}
|
}
|
||||||
return rc;
|
return rc;
|
||||||
|
|
Loading…
Reference in New Issue