Skip to content

Commit

Permalink
parted: introduce enum for whether parted has option -m
Browse files Browse the repository at this point in the history
Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com>

RWMJ: Use enum consistently, don't assign the result to int.
  • Loading branch information
chenhanxiao authored and rwmjones committed Mar 24, 2015
1 parent 52d1b0c commit af9aa2e
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions daemon/parted.c
Expand Up @@ -33,6 +33,13 @@ GUESTFSD_EXT_CMD(str_parted, parted);
GUESTFSD_EXT_CMD(str_sfdisk, sfdisk);
GUESTFSD_EXT_CMD(str_sgdisk, sgdisk);

enum parted_has_m_opt {
PARTED_INVALID = -1,
/* parted do not support -m option */
PARTED_OPT_NO_M = 0,
PARTED_OPT_HAS_M = 1,
};

/* Notes:
*
* Parted 1.9 sends error messages to stdout, hence use of the
Expand Down Expand Up @@ -317,10 +324,10 @@ get_table_field (const char *line, int n)
* print_partition_table below. Test for this option the first time
* this function is called.
*/
static int
static enum parted_has_m_opt
test_parted_m_opt (void)
{
static int result = -1;
static enum parted_has_m_opt result = PARTED_INVALID;

if (result >= 0)
return result;
Expand All @@ -334,20 +341,21 @@ test_parted_m_opt (void)
}

if (err && strstr (err, "invalid option -- m"))
result = 0;
result = PARTED_OPT_NO_M;
else
result = 1;
result = PARTED_OPT_HAS_M;
return result;
}

static char *
print_partition_table (const char *device, int parted_has_m_opt)
print_partition_table (const char *device,
enum parted_has_m_opt parted_has_m_opt)
{
char *out;
CLEANUP_FREE char *err = NULL;
int r;

if (parted_has_m_opt)
if (PARTED_OPT_HAS_M == parted_has_m_opt)
r = command (&out, &err, str_parted, "-m", "--", device,
"unit", "b",
"print", NULL);
Expand All @@ -369,15 +377,15 @@ print_partition_table (const char *device, int parted_has_m_opt)
char *
do_part_get_parttype (const char *device)
{
int parted_has_m_opt = test_parted_m_opt ();
if (parted_has_m_opt == -1)
enum parted_has_m_opt parted_has_m_opt = test_parted_m_opt ();
if (parted_has_m_opt == PARTED_INVALID)
return NULL;

CLEANUP_FREE char *out = print_partition_table (device, parted_has_m_opt);
if (!out)
return NULL;

if (parted_has_m_opt) {
if (PARTED_OPT_HAS_M == parted_has_m_opt) {
/* New-style parsing using the "machine-readable" format from
* 'parted -m'.
*/
Expand Down Expand Up @@ -451,8 +459,8 @@ do_part_get_parttype (const char *device)
guestfs_int_partition_list *
do_part_list (const char *device)
{
int parted_has_m_opt = test_parted_m_opt ();
if (parted_has_m_opt == -1)
enum parted_has_m_opt parted_has_m_opt = test_parted_m_opt ();
if (parted_has_m_opt == PARTED_INVALID)
return NULL;

CLEANUP_FREE char *out = print_partition_table (device, parted_has_m_opt);
Expand All @@ -466,7 +474,7 @@ do_part_list (const char *device)

guestfs_int_partition_list *r;

if (parted_has_m_opt) {
if (PARTED_OPT_HAS_M == parted_has_m_opt) {
/* New-style parsing using the "machine-readable" format from
* 'parted -m'.
*
Expand Down Expand Up @@ -577,8 +585,8 @@ do_part_get_bootable (const char *device, int partnum)
return -1;
}

int parted_has_m_opt = test_parted_m_opt ();
if (parted_has_m_opt == -1)
enum parted_has_m_opt parted_has_m_opt = test_parted_m_opt ();
if (parted_has_m_opt == PARTED_INVALID)
return -1;

CLEANUP_FREE char *out = print_partition_table (device, parted_has_m_opt);
Expand All @@ -590,7 +598,7 @@ do_part_get_bootable (const char *device, int partnum)
if (!lines)
return -1;

if (parted_has_m_opt) {
if (PARTED_OPT_HAS_M == parted_has_m_opt) {
/* New-style parsing using the "machine-readable" format from
* 'parted -m'.
*
Expand Down Expand Up @@ -964,15 +972,15 @@ do_part_get_name (const char *device, int partnum)
return NULL;

if (STREQ (parttype, "gpt")) {
int parted_has_m_opt = test_parted_m_opt ();
if (parted_has_m_opt == -1)
enum parted_has_m_opt parted_has_m_opt = test_parted_m_opt ();
if (parted_has_m_opt == PARTED_INVALID)
return NULL;

CLEANUP_FREE char *out = print_partition_table (device, parted_has_m_opt);
if (!out)
return NULL;

if (parted_has_m_opt) {
if (PARTED_OPT_HAS_M == parted_has_m_opt) {
/* New-style parsing using the "machine-readable" format from
* 'parted -m'.
*/
Expand Down

0 comments on commit af9aa2e

Please sign in to comment.