virsh: Reject negative numbers in vshCommandOptULongLong

To follow the new semantics of the vshCommandOptToU* functions convert
this one to reject negative numbers too. To allow using -1 for "maximum"
semantics for the vol-*load two bandwidth functions that use this helper
introduce vshCommandOptULongLongWrap.
This commit is contained in:
Peter Krempa 2014-06-04 11:08:08 +02:00
parent 0e2d73051a
commit c62125395b
3 changed files with 47 additions and 15 deletions

View File

@ -677,12 +677,12 @@ cmdVolUpload(vshControl *ctl, const vshCmd *cmd)
const char *name = NULL;
unsigned long long offset = 0, length = 0;
if (vshCommandOptULongLong(cmd, "offset", &offset) < 0) {
if (vshCommandOptULongLongWrap(cmd, "offset", &offset) < 0) {
vshError(ctl, _("Unable to parse integer"));
return false;
}
if (vshCommandOptULongLong(cmd, "length", &length) < 0) {
if (vshCommandOptULongLongWrap(cmd, "length", &length) < 0) {
vshError(ctl, _("Unable to parse integer"));
return false;
}
@ -787,12 +787,12 @@ cmdVolDownload(vshControl *ctl, const vshCmd *cmd)
unsigned long long offset = 0, length = 0;
bool created = false;
if (vshCommandOptULongLong(cmd, "offset", &offset) < 0) {
if (vshCommandOptULongLongWrap(cmd, "offset", &offset) < 0) {
vshError(ctl, _("Unable to parse integer"));
return false;
}
if (vshCommandOptULongLong(cmd, "length", &length) < 0) {
if (vshCommandOptULongLongWrap(cmd, "length", &length) < 0) {
vshError(ctl, _("Unable to parse integer"));
return false;
}

View File

@ -1699,31 +1699,60 @@ vshCommandOptLongLong(const vshCmd *cmd, const char *name,
return 1;
}
static int
vshCommandOptULongLongInternal(const vshCmd *cmd,
const char *name,
unsigned long long *value,
bool wrap)
{
vshCmdOpt *arg;
int ret;
if ((ret = vshCommandOpt(cmd, name, &arg, true)) <= 0)
return ret;
if (wrap) {
if (virStrToLong_ull(arg->data, NULL, 10, value) < 0)
return -1;
} else {
if (virStrToLong_ullp(arg->data, NULL, 10, value) < 0)
return -1;
}
return 1;
}
/**
* vshCommandOptULongLong:
* @cmd command reference
* @name option name
* @value result
*
* Returns option as long long
* Returns option as long long, rejects negative numbers
* See vshCommandOptInt()
*/
int
vshCommandOptULongLong(const vshCmd *cmd, const char *name,
unsigned long long *value)
{
vshCmdOpt *arg;
int ret;
ret = vshCommandOpt(cmd, name, &arg, true);
if (ret <= 0)
return ret;
if (virStrToLong_ull(arg->data, NULL, 10, value) < 0)
return -1;
return 1;
return vshCommandOptULongLongInternal(cmd, name, value, false);
}
/**
* vshCommandOptULongLongWrap:
* @cmd command reference
* @name option name
* @value result
*
* Returns option as long long, wraps negative numbers to positive
* See vshCommandOptInt()
*/
int
vshCommandOptULongLongWrap(const vshCmd *cmd, const char *name,
unsigned long long *value)
{
return vshCommandOptULongLongInternal(cmd, name, value, true);
}
/**
* vshCommandOptScaledInt:

View File

@ -307,6 +307,9 @@ int vshCommandOptLongLong(const vshCmd *cmd, const char *name,
int vshCommandOptULongLong(const vshCmd *cmd, const char *name,
unsigned long long *value)
ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
int vshCommandOptULongLongWrap(const vshCmd *cmd, const char *name,
unsigned long long *value)
ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
int vshCommandOptScaledInt(const vshCmd *cmd, const char *name,
unsigned long long *value, int scale,
unsigned long long max)