qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH 2/7] qemu-img: add iflag and oflag options to dd


From: Stefan Hajnoczi
Subject: Re: [Qemu-devel] [PATCH 2/7] qemu-img: add iflag and oflag options to dd
Date: Mon, 22 Aug 2016 09:26:52 -0400
User-agent: Mutt/1.6.2 (2016-07-01)

On Mon, Aug 22, 2016 at 09:55:12AM +0200, Reda Sallahi wrote:
> +static int img_dd_oflag(const char *arg,
> +                        struct DdIo *in, struct DdIo *out,
> +                        struct DdInfo *dd)
> +{
> +    const char *tok;
> +    char *str, *tmp;
> +    int ret = 0;
> +    const struct DdSymbols flags[] = {
> +        { "direct", C_DIRECT },
> +        { "dsync", C_DSYNC },
> +        { "sync", C_IOFLAG_SYNC },
> +        { "seek_bytes", C_SEEK_BYTES },
> +        { NULL, 0 }
> +    };
> +
> +    tmp = str = g_strdup(arg);
> +
> +    while (tmp != NULL && !ret) {
> +        tok = qemu_strsep(&tmp, ",");
> +        int j;
> +        for (j = 0; flags[j].name != NULL; j++) {
> +            if (!strcmp(tok, flags[j].name)) {
> +                out->flags |= flags[j].value;
> +                break;
> +            }
> +        }
> +        if (flags[j].name == NULL) {
> +            error_report("invalid output flag: '%s'", tok);
> +            ret = 1;
> +        }
> +    }
> +
> +    g_free(str);
> +    return ret;
> +}

img_dd_iflag()/img_dd_oflag() are duplicated code.  I suggest a single
helper function that parses a DdSymbols array into a DdIo->flags field.
Then you can pass in either the iflag or the oflag DdSymbols arrays.

static int img_dd_oflag(const char *arg,
                        struct DdIo *in, struct DdIo *out,
                        struct DdInfo *dd)
{
    return img_dd_xflag(arg, (DdSymbols[]){
        { "direct", C_DIRECT },
        { "dsync", C_DSYNC },
        ...
    }, in);
}

static int img_dd_oflag(const char *arg,
                        struct DdIo *in, struct DdIo *out,
                        struct DdInfo *dd)
{
    return img_dd_xflag(arg, (DdSymbols[]){
        { "direct", C_DIRECT },
        { "dsync", C_DSYNC },
        ...
    }, out);
}

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]