unsigned int i;
uint64_t value, limit;
size_t prefix_len, orig_len = str_len(str);
- int ret = 0;
+ enum quota_get_result ret = QUOTA_GET_RESULT_UNLIMITED;
str_append(str, "* QUOTA ");
name = imap_quota_root_get_name(user, owner, root);
list = quota_root_get_resources(root);
for (i = 0; *list != NULL; list++) {
ret = quota_get_resource(root, "", *list, &value, &limit);
- if (ret < 0)
+ if (ret == QUOTA_GET_RESULT_INTERNAL_ERROR)
break;
- if (ret > 0) {
+ if (ret == QUOTA_GET_RESULT_LIMITED) {
if (i > 0)
str_append_c(str, ' ');
str_printfa(str, "%s %llu %llu", *list,
} else {
str_append(str, ")\r\n");
}
- return ret < 0 ? -1 : 0;
+ return ret == QUOTA_GET_RESULT_INTERNAL_ERROR ? -1 : 0;
}
static bool cmd_getquotaroot(struct client_command_context *cmd)
struct quota_root *root;
uint64_t bytes_value, count_value, limit;
const char *error;
- int ret_bytes, ret_count;
+ enum quota_get_result ret_bytes, ret_count;
/* we'll clone the first quota root */
iter = quota_root_iter_init(box);
/* get new values first */
ret_bytes = quota_get_resource(root, "", QUOTA_NAME_STORAGE_BYTES,
&bytes_value, &limit);
- if (ret_bytes < 0) {
+ if (ret_bytes == QUOTA_GET_RESULT_INTERNAL_ERROR) {
i_error("quota_clone_plugin: Failed to lookup current quota bytes");
return;
}
ret_count = quota_get_resource(root, "", QUOTA_NAME_MESSAGES,
&count_value, &limit);
- if (ret_count < 0) {
+ if (ret_count == QUOTA_GET_RESULT_INTERNAL_ERROR) {
i_error("quota_clone_plugin: Failed to lookup current quota count");
return;
}
- if (ret_bytes == 0 && ret_count == 0) {
- /* quota isn't enabled - no point in updating it */
+ if (ret_bytes == QUOTA_GET_RESULT_UNKNOWN_RESOURCE &&
+ ret_count == QUOTA_GET_RESULT_UNKNOWN_RESOURCE) {
+ /* quota resources don't exist - no point in updating it */
return;
}
the special case of ret_count changing between 1 and 0. Note that
ret_count==1 also when quota is unlimited. */
trans = dict_transaction_begin(quser->dict);
- if (ret_bytes > 0) {
+ if (ret_bytes == QUOTA_GET_RESULT_LIMITED ||
+ ret_bytes == QUOTA_GET_RESULT_UNLIMITED) {
dict_set(trans, DICT_QUOTA_CLONE_BYTES_PATH,
t_strdup_printf("%llu", (unsigned long long)bytes_value));
}
- if (ret_count > 0) {
+ if (ret_count == QUOTA_GET_RESULT_LIMITED ||
+ ret_count == QUOTA_GET_RESULT_UNLIMITED) {
dict_set(trans, DICT_QUOTA_CLONE_COUNT_PATH,
t_strdup_printf("%llu", (unsigned long long)count_value));
}
{
const char *const *res;
uint64_t value, limit;
- int ret;
+ enum quota_get_result ret;
res = quota_root_get_resources(root);
for (; *res != NULL; res++) {
ret = quota_get_resource(root, "", *res, &value, &limit);
doveadm_print(root->set->name);
doveadm_print(*res);
- if (ret > 0) {
+ if (ret == QUOTA_GET_RESULT_LIMITED) {
doveadm_print_num(value);
doveadm_print_num(limit);
if (limit > 0)
doveadm_print_num(value*100 / limit);
else
doveadm_print("0");
- } else if (ret == 0) {
+ } else if (ret == QUOTA_GET_RESULT_UNLIMITED) {
doveadm_print_num(value);
doveadm_print("-");
doveadm_print("0");
struct mail_namespace *ns);
const char *const *(*get_resources)(struct quota_root *root);
+ /* Returns 1 if value was returned, 0 if resource name doesn't exist,
+ -1 if internal error. */
int (*get_resource)(struct quota_root *root,
const char *name, uint64_t *value_r);
return root->hidden;
}
-int quota_get_resource(struct quota_root *root, const char *mailbox_name,
- const char *name, uint64_t *value_r, uint64_t *limit_r)
+enum quota_get_result
+quota_get_resource(struct quota_root *root, const char *mailbox_name,
+ const char *name, uint64_t *value_r, uint64_t *limit_r)
{
uint64_t bytes_limit, count_limit;
bool ignored, kilobytes = FALSE;
/* Get the value first. This call may also update quota limits if
they're defined externally. */
ret = root->backend.v.get_resource(root, name, value_r);
- if (ret <= 0)
- return ret;
+ if (ret < 0)
+ return QUOTA_GET_RESULT_INTERNAL_ERROR;
+ if (ret == 0)
+ return QUOTA_GET_RESULT_UNKNOWN_RESOURCE;
if (quota_root_get_rule_limits(root, mailbox_name,
&bytes_limit, &count_limit,
&ignored) < 0)
- return -1;
+ return QUOTA_GET_RESULT_INTERNAL_ERROR;
if (strcmp(name, QUOTA_NAME_STORAGE_BYTES) == 0)
*limit_r = bytes_limit;
*value_r = (*value_r + 1023) / 1024;
*limit_r = (*limit_r + 1023) / 1024;
}
- return *limit_r == 0 ? 0 : 1;
+ return *limit_r == 0 ? QUOTA_GET_RESULT_UNLIMITED : QUOTA_GET_RESULT_LIMITED;
}
int quota_set_resource(struct quota_root *root, const char *name,
unsigned int i, count;
uint64_t bytes_limit, count_limit, current, limit, diff;
bool use_grace, ignored;
- int ret;
+ enum quota_get_result ret;
if (ctx->limits_set)
return 0;
ret = quota_get_resource(roots[i], mailbox_name,
QUOTA_NAME_STORAGE_BYTES,
¤t, &limit);
- if (ret > 0) {
+ if (ret == QUOTA_GET_RESULT_LIMITED) {
if (limit <= current) {
/* over quota */
ctx->bytes_ceil = 0;
if (ctx->bytes_ceil > diff)
ctx->bytes_ceil = diff;
}
- } else if (ret < 0) {
+ } else if (ret == QUOTA_GET_RESULT_INTERNAL_ERROR) {
ctx->failed = TRUE;
return -1;
}
ret = quota_get_resource(roots[i], mailbox_name,
QUOTA_NAME_MESSAGES,
¤t, &limit);
- if (ret > 0) {
+ if (ret == QUOTA_GET_RESULT_LIMITED) {
if (limit <= current) {
/* over quota */
ctx->count_ceil = 0;
if (ctx->count_ceil > diff)
ctx->count_ceil = diff;
}
- } else if (ret < 0) {
+ } else if (ret == QUOTA_GET_RESULT_INTERNAL_ERROR) {
ctx->failed = TRUE;
return -1;
}
return;
if (quota_get_resource(root, "", QUOTA_NAME_STORAGE_BYTES,
- &bytes_current, &bytes_limit) < 0)
+ &bytes_current, &bytes_limit) == QUOTA_GET_RESULT_INTERNAL_ERROR)
return;
if (quota_get_resource(root, "", QUOTA_NAME_MESSAGES,
- &count_current, &count_limit) < 0)
+ &count_current, &count_limit) == QUOTA_GET_RESULT_INTERNAL_ERROR)
return;
if (ctx->bytes_used > 0 && bytes_current < (uint64_t)ctx->bytes_used)
uint64_t value, limit;
bool cur_overquota = FALSE;
bool quota_over_status;
- int ret;
+ enum quota_get_result ret;
if (root->quota_over_flag_checked)
return;
resources = quota_root_get_resources(root);
for (i = 0; resources[i] != NULL; i++) {
ret = quota_get_resource(root, "", resources[i], &value, &limit);
- if (ret < 0) {
+ if (ret == QUOTA_GET_RESULT_INTERNAL_ERROR) {
/* can't reliably verify this */
if (root->quota->set->debug) {
i_debug("quota: Quota %s lookup failed - can't verify quota_over_flag",
(unsigned long long)value,
(unsigned long long)limit);
}
- if (ret > 0 && value >= limit)
+ if (ret == QUOTA_GET_RESULT_LIMITED && value >= limit)
cur_overquota = TRUE;
}
if (root->quota->set->debug) {
QUOTA_ALLOC_RESULT_OVER_QUOTA_LIMIT,
};
+enum quota_get_result {
+ /* Quota limit exists and was returned successfully */
+ QUOTA_GET_RESULT_LIMITED,
+ /* Quota is unlimited, but its value was returned */
+ QUOTA_GET_RESULT_UNLIMITED,
+ /* Quota resource name doesn't exist */
+ QUOTA_GET_RESULT_UNKNOWN_RESOURCE,
+ /* Internal error */
+ QUOTA_GET_RESULT_INTERNAL_ERROR = -1,
+};
+
const char *quota_alloc_result_errstr(enum quota_alloc_result res,
struct quota_transaction_context *qt);
/* Returns 1 if values were successfully returned, 0 if resource name doesn't
exist or isn't enabled, -1 if error. */
-int quota_get_resource(struct quota_root *root, const char *mailbox_name,
- const char *name, uint64_t *value_r, uint64_t *limit_r);
+enum quota_get_result
+quota_get_resource(struct quota_root *root, const char *mailbox_name,
+ const char *name, uint64_t *value_r, uint64_t *limit_r);
/* Returns 0 if OK, -1 if error (eg. permission denied, invalid name). */
int quota_set_resource(struct quota_root *root, const char *name,
uint64_t value, const char **error_r);