QemuOpts: Convert qemu_opt_set_bool() to Error, fix its use

Return the Error object instead of reporting it with
qerror_report_err().

Change callers that assume the function can't fail to pass
&error_abort, so that should the assumption ever break, it'll break
noisily.

Turns out all callers outside its unit test assume that.  We could
drop the Error ** argument, but that would make the interface less
regular, so don't.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
Markus Armbruster 2015-02-12 16:37:44 +01:00
parent c5c6d7f81a
commit cccb7967bd
6 changed files with 19 additions and 18 deletions

View File

@ -737,15 +737,15 @@ DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
/* Specific options take precedence */ /* Specific options take precedence */
if (!qemu_opt_get(all_opts, "cache.writeback")) { if (!qemu_opt_get(all_opts, "cache.writeback")) {
qemu_opt_set_bool(all_opts, "cache.writeback", qemu_opt_set_bool(all_opts, "cache.writeback",
!!(flags & BDRV_O_CACHE_WB)); !!(flags & BDRV_O_CACHE_WB), &error_abort);
} }
if (!qemu_opt_get(all_opts, "cache.direct")) { if (!qemu_opt_get(all_opts, "cache.direct")) {
qemu_opt_set_bool(all_opts, "cache.direct", qemu_opt_set_bool(all_opts, "cache.direct",
!!(flags & BDRV_O_NOCACHE)); !!(flags & BDRV_O_NOCACHE), &error_abort);
} }
if (!qemu_opt_get(all_opts, "cache.no-flush")) { if (!qemu_opt_get(all_opts, "cache.no-flush")) {
qemu_opt_set_bool(all_opts, "cache.no-flush", qemu_opt_set_bool(all_opts, "cache.no-flush",
!!(flags & BDRV_O_NO_FLUSH)); !!(flags & BDRV_O_NO_FLUSH), &error_abort);
} }
qemu_opt_unset(all_opts, "cache"); qemu_opt_unset(all_opts, "cache");
} }

View File

@ -97,7 +97,8 @@ int qemu_opt_unset(QemuOpts *opts, const char *name);
int qemu_opt_set(QemuOpts *opts, const char *name, const char *value); int qemu_opt_set(QemuOpts *opts, const char *name, const char *value);
void qemu_opt_set_err(QemuOpts *opts, const char *name, const char *value, void qemu_opt_set_err(QemuOpts *opts, const char *name, const char *value,
Error **errp); Error **errp);
int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val); void qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val,
Error **errp);
int qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val); int qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val);
typedef int (*qemu_opt_loopfunc)(const char *name, const char *value, void *opaque); typedef int (*qemu_opt_loopfunc)(const char *name, const char *value, void *opaque);
int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque, int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,

View File

@ -169,10 +169,10 @@ static void test_qemu_opt_get(void)
static void test_qemu_opt_get_bool(void) static void test_qemu_opt_get_bool(void)
{ {
Error *err = NULL;
QemuOptsList *list; QemuOptsList *list;
QemuOpts *opts; QemuOpts *opts;
bool opt; bool opt;
int ret;
list = qemu_find_opts("opts_list_02"); list = qemu_find_opts("opts_list_02");
g_assert(list != NULL); g_assert(list != NULL);
@ -192,16 +192,16 @@ static void test_qemu_opt_get_bool(void)
opt = qemu_opt_get_bool(opts, "bool1", false); opt = qemu_opt_get_bool(opts, "bool1", false);
g_assert(opt == false); g_assert(opt == false);
ret = qemu_opt_set_bool(opts, "bool1", true); qemu_opt_set_bool(opts, "bool1", true, &err);
g_assert(ret == 0); g_assert(!err);
/* now we have set bool1, should know about it */ /* now we have set bool1, should know about it */
opt = qemu_opt_get_bool(opts, "bool1", false); opt = qemu_opt_get_bool(opts, "bool1", false);
g_assert(opt == true); g_assert(opt == true);
/* having reset the value, opt should be the reset one not defval */ /* having reset the value, opt should be the reset one not defval */
ret = qemu_opt_set_bool(opts, "bool1", false); qemu_opt_set_bool(opts, "bool1", false, &err);
g_assert(ret == 0); g_assert(!err);
opt = qemu_opt_get_bool(opts, "bool1", true); opt = qemu_opt_get_bool(opts, "bool1", true);
g_assert(opt == false); g_assert(opt == false);

View File

@ -568,7 +568,8 @@ void qemu_opt_set_err(QemuOpts *opts, const char *name, const char *value,
opt_set(opts, name, value, false, errp); opt_set(opts, name, value, false, errp);
} }
int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val) void qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val,
Error **errp)
{ {
QemuOpt *opt; QemuOpt *opt;
const QemuOptDesc *desc = opts->list->desc; const QemuOptDesc *desc = opts->list->desc;
@ -576,9 +577,9 @@ int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val)
opt = g_malloc0(sizeof(*opt)); opt = g_malloc0(sizeof(*opt));
opt->desc = find_desc_by_name(desc, name); opt->desc = find_desc_by_name(desc, name);
if (!opt->desc && !opts_accepts_any(opts)) { if (!opt->desc && !opts_accepts_any(opts)) {
qerror_report(QERR_INVALID_PARAMETER, name); error_set(errp, QERR_INVALID_PARAMETER, name);
g_free(opt); g_free(opt);
return -1; return;
} }
opt->name = g_strdup(name); opt->name = g_strdup(name);
@ -586,8 +587,6 @@ int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val)
opt->value.boolean = !!val; opt->value.boolean = !!val;
opt->str = g_strdup(val ? "on" : "off"); opt->str = g_strdup(val ? "on" : "off");
QTAILQ_INSERT_TAIL(&opts->head, opt, next); QTAILQ_INSERT_TAIL(&opts->head, opt, next);
return 0;
} }
int qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val) int qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val)

View File

@ -580,8 +580,8 @@ static void inet_addr_to_opts(QemuOpts *opts, const InetSocketAddress *addr)
bool ipv6 = addr->ipv6 || !addr->has_ipv6; bool ipv6 = addr->ipv6 || !addr->has_ipv6;
if (!ipv4 || !ipv6) { if (!ipv4 || !ipv6) {
qemu_opt_set_bool(opts, "ipv4", ipv4); qemu_opt_set_bool(opts, "ipv4", ipv4, &error_abort);
qemu_opt_set_bool(opts, "ipv6", ipv6); qemu_opt_set_bool(opts, "ipv6", ipv6, &error_abort);
} }
if (addr->has_to) { if (addr->has_to) {
char to[20]; char to[20];

5
vl.c
View File

@ -2222,7 +2222,7 @@ static void monitor_parse(const char *optarg, const char *mode, bool pretty)
} }
qemu_opt_set(opts, "mode", mode); qemu_opt_set(opts, "mode", mode);
qemu_opt_set(opts, "chardev", label); qemu_opt_set(opts, "chardev", label);
qemu_opt_set_bool(opts, "pretty", pretty); qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
if (def) if (def)
qemu_opt_set(opts, "default", "on"); qemu_opt_set(opts, "default", "on");
monitor_device_index++; monitor_device_index++;
@ -3287,7 +3287,8 @@ int main(int argc, char **argv, char **envp)
} }
qemu_opt_set_bool(fsdev, "readonly", qemu_opt_set_bool(fsdev, "readonly",
qemu_opt_get_bool(opts, "readonly", 0)); qemu_opt_get_bool(opts, "readonly", 0),
&error_abort);
device = qemu_opts_create(qemu_find_opts("device"), NULL, 0, device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
&error_abort); &error_abort);
qemu_opt_set(device, "driver", "virtio-9p-pci"); qemu_opt_set(device, "driver", "virtio-9p-pci");