Skip to content

Commit

Permalink
global: Update mailbox_attribute_get API usage
Browse files Browse the repository at this point in the history
  • Loading branch information
cmouse authored and GitLab committed Aug 10, 2016
1 parent 9f37ef2 commit 66c8772
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 66 deletions.
5 changes: 1 addition & 4 deletions src/doveadm/doveadm-mail-mailbox-metadata.c
Expand Up @@ -139,7 +139,6 @@ cmd_mailbox_metadata_get_run(struct doveadm_mail_cmd_context *_ctx,
struct metadata_cmd_context *ctx = (struct metadata_cmd_context *)_ctx;
struct mail_namespace *ns;
struct mailbox *box;
struct mailbox_transaction_context *trans;
struct mail_attribute_value value;
int ret;

Expand All @@ -153,9 +152,8 @@ cmd_mailbox_metadata_get_run(struct doveadm_mail_cmd_context *_ctx,
mailbox_free(&box);
return -1;
}
trans = mailbox_transaction_begin(box, 0);

ret = mailbox_attribute_get_stream(trans, ctx->key_type, ctx->key, &value);
ret = mailbox_attribute_get_stream(box, ctx->key_type, ctx->key, &value);
if (ret < 0) {
i_error("Failed to get attribute: %s",
mailbox_get_last_error(box, NULL));
Expand All @@ -169,7 +167,6 @@ cmd_mailbox_metadata_get_run(struct doveadm_mail_cmd_context *_ctx,
doveadm_print(value.value);
}

(void)mailbox_transaction_commit(&trans);
mailbox_free(&box);
return ret;
}
Expand Down
4 changes: 2 additions & 2 deletions src/doveadm/dsync/dsync-mailbox-export.c
Expand Up @@ -545,7 +545,7 @@ dsync_mailbox_export_iter_next_nonexistent_attr(struct dsync_mailbox_exporter *e
continue;

/* lookup the value mainly to get its last_change value. */
if (mailbox_attribute_get_stream(exporter->trans, attr->type,
if (mailbox_attribute_get_stream(exporter->box, attr->type,
attr->key, &value) < 0) {
exporter->error = p_strdup_printf(exporter->pool,
"Mailbox attribute %s lookup failed: %s", attr->key,
Expand Down Expand Up @@ -597,7 +597,7 @@ dsync_mailbox_export_iter_next_attr(struct dsync_mailbox_exporter *exporter)
if (attr_change == NULL && !export_all_attrs)
continue;

if (mailbox_attribute_get_stream(exporter->trans,
if (mailbox_attribute_get_stream(exporter->box,
exporter->attr_type, key,
&value) < 0) {
exporter->error = p_strdup_printf(exporter->pool,
Expand Down
2 changes: 1 addition & 1 deletion src/doveadm/dsync/dsync-mailbox-import.c
Expand Up @@ -324,7 +324,7 @@ dsync_mailbox_import_lookup_attr(struct dsync_mailbox_importer *importer,

*attr_r = NULL;

if (mailbox_attribute_get_stream(importer->trans, type, key, &value) < 0) {
if (mailbox_attribute_get_stream(importer->box, type, key, &value) < 0) {
i_error("Mailbox %s: Failed to get attribute %s: %s",
mailbox_get_vname(importer->box), key,
mailbox_get_last_error(importer->box, &importer->mail_error));
Expand Down
8 changes: 2 additions & 6 deletions src/lib-imap-storage/imap-metadata.c
Expand Up @@ -162,9 +162,7 @@ int imap_metadata_get(struct imap_metadata_transaction *imtrans,
memset(value_r, 0, sizeof(*value_r));
if (!imap_metadata_entry2key(imtrans, entry, &type, &key))
return 0;
if (imap_metadata_get_mailbox_transaction(imtrans) < 0)
return -1;
return mailbox_attribute_get(imtrans->trans, type, key, value_r);
return mailbox_attribute_get(imtrans->box, type, key, value_r);
}

int imap_metadata_get_stream(struct imap_metadata_transaction *imtrans,
Expand All @@ -176,9 +174,7 @@ int imap_metadata_get_stream(struct imap_metadata_transaction *imtrans,
memset(value_r, 0, sizeof(*value_r));
if (!imap_metadata_entry2key(imtrans, entry, &type, &key))
return 0;
if (imap_metadata_get_mailbox_transaction(imtrans) < 0)
return -1;
return mailbox_attribute_get_stream(imtrans->trans, type, key, value_r);
return mailbox_attribute_get_stream(imtrans->box, type, key, value_r);
}

struct imap_metadata_iter {
Expand Down
59 changes: 43 additions & 16 deletions src/lib-imap-urlauth/imap-urlauth-backend.c
Expand Up @@ -13,13 +13,47 @@
#define IMAP_URLAUTH_KEY MAILBOX_ATTRIBUTE_PREFIX_DOVECOT"imap-urlauth"

static int
imap_urlauth_backend_trans_get_mailbox_key(struct mailbox_transaction_context *trans,
imap_urlauth_backend_trans_set_mailbox_key(struct mailbox *box,
unsigned char mailbox_key_r[IMAP_URLAUTH_KEY_LEN],
const char **error_r,
enum mail_error *error_code_r)
{
struct mail_attribute_value urlauth_key;
const char *mailbox_key_hex = NULL;
int ret;

if (mailbox_open(box) < 0) {
*error_r = mailbox_get_last_error(box, error_code_r);
return -1;
}

struct mailbox_transaction_context *t =
mailbox_transaction_begin(box, MAILBOX_TRANSACTION_FLAG_EXTERNAL);

/* create new key */
random_fill(mailbox_key_r, IMAP_URLAUTH_KEY_LEN);
mailbox_key_hex = binary_to_hex(mailbox_key_r,
IMAP_URLAUTH_KEY_LEN);
memset(&urlauth_key, 0, sizeof(urlauth_key));
urlauth_key.value = mailbox_key_hex;
ret = mailbox_attribute_set(t, MAIL_ATTRIBUTE_TYPE_PRIVATE,
IMAP_URLAUTH_KEY, &urlauth_key);

if (mailbox_transaction_commit(&t) < 0) {
*error_r = mailbox_get_last_error(box, error_code_r);
ret = -1;
}

return ret;
}

static int
imap_urlauth_backend_trans_get_mailbox_key(struct mailbox *box,
bool create,
unsigned char mailbox_key_r[IMAP_URLAUTH_KEY_LEN],
const char **error_r,
enum mail_error *error_code_r)
{
struct mailbox *box = mailbox_transaction_get_mailbox(trans);
struct mail_user *user = mail_storage_get_user(mailbox_get_storage(box));
struct mail_attribute_value urlauth_key;
const char *mailbox_key_hex = NULL;
Expand All @@ -29,7 +63,7 @@ imap_urlauth_backend_trans_get_mailbox_key(struct mailbox_transaction_context *t
*error_r = "Internal server error";
*error_code_r = MAIL_ERROR_TEMP;

ret = mailbox_attribute_get(trans, MAIL_ATTRIBUTE_TYPE_PRIVATE,
ret = mailbox_attribute_get(box, MAIL_ATTRIBUTE_TYPE_PRIVATE,
IMAP_URLAUTH_KEY, &urlauth_key);
if (ret < 0)
return -1;
Expand All @@ -43,14 +77,11 @@ imap_urlauth_backend_trans_get_mailbox_key(struct mailbox_transaction_context *t
if (!create)
return 0;

/* create new key */
random_fill(mailbox_key_r, IMAP_URLAUTH_KEY_LEN);
mailbox_key_hex = binary_to_hex(mailbox_key_r,
IMAP_URLAUTH_KEY_LEN);
memset(&urlauth_key, 0, sizeof(urlauth_key));
urlauth_key.value = mailbox_key_hex;
ret = mailbox_attribute_set(trans, MAIL_ATTRIBUTE_TYPE_PRIVATE,
IMAP_URLAUTH_KEY, &urlauth_key);
ret = imap_urlauth_backend_trans_set_mailbox_key(box,
mailbox_key_r,
error_r,
error_code_r);

if (ret < 0)
return -1;
if (user->mail_debug) {
Expand Down Expand Up @@ -78,13 +109,9 @@ int imap_urlauth_backend_get_mailbox_key(struct mailbox *box, bool create,
const char **error_r,
enum mail_error *error_code_r)
{
struct mailbox_transaction_context *t;
int ret;

t = mailbox_transaction_begin(box, MAILBOX_TRANSACTION_FLAG_EXTERNAL);
ret = imap_urlauth_backend_trans_get_mailbox_key(t, create, mailbox_key_r, error_r, error_code_r);
if (mailbox_transaction_commit(&t) < 0)
ret = -1;
ret = imap_urlauth_backend_trans_get_mailbox_key(box, create, mailbox_key_r, error_r, error_code_r);
return ret;
}

Expand Down
18 changes: 10 additions & 8 deletions src/plugins/acl/acl-attributes.c
Expand Up @@ -137,7 +137,8 @@ static int acl_have_attribute_rights(struct mailbox *box)

int acl_attribute_set(struct mailbox_transaction_context *t,
enum mail_attribute_type type, const char *key,
const struct mail_attribute_value *value)
const struct mail_attribute_value *value,
bool internal_attribute)
{
struct acl_mailbox *abox = ACL_CONTEXT(t->box);

Expand All @@ -146,21 +147,22 @@ int acl_attribute_set(struct mailbox_transaction_context *t,
if (strncmp(key, MAILBOX_ATTRIBUTE_PREFIX_ACL,
strlen(MAILBOX_ATTRIBUTE_PREFIX_ACL)) == 0)
return acl_attribute_update_acl(t, key, value);
return abox->module_ctx.super.attribute_set(t, type, key, value);
return abox->module_ctx.super.attribute_set(t, type, key, value, internal_attribute);
}

int acl_attribute_get(struct mailbox_transaction_context *t,
int acl_attribute_get(struct mailbox *box,
enum mail_attribute_type type, const char *key,
struct mail_attribute_value *value_r)
struct mail_attribute_value *value_r,
bool internal_attribute)
{
struct acl_mailbox *abox = ACL_CONTEXT(t->box);
struct acl_mailbox *abox = ACL_CONTEXT(box);

if (acl_have_attribute_rights(t->box) < 0)
if (acl_have_attribute_rights(box) < 0)
return -1;
if (strncmp(key, MAILBOX_ATTRIBUTE_PREFIX_ACL,
strlen(MAILBOX_ATTRIBUTE_PREFIX_ACL)) == 0)
return acl_attribute_get_acl(t->box, key, value_r);
return abox->module_ctx.super.attribute_get(t, type, key, value_r);
return acl_attribute_get_acl(box, key, value_r);
return abox->module_ctx.super.attribute_get(box, type, key, value_r, internal_attribute);
}

struct mailbox_attribute_iter *
Expand Down
8 changes: 5 additions & 3 deletions src/plugins/acl/acl-storage.h
Expand Up @@ -33,10 +33,12 @@ int acl_mailbox_update_acl(struct mailbox_transaction_context *t,

int acl_attribute_set(struct mailbox_transaction_context *t,
enum mail_attribute_type type, const char *key,
const struct mail_attribute_value *value);
int acl_attribute_get(struct mailbox_transaction_context *t,
const struct mail_attribute_value *value,
bool internal_attribute);
int acl_attribute_get(struct mailbox *box,
enum mail_attribute_type type, const char *key,
struct mail_attribute_value *value_r);
struct mail_attribute_value *value_r,
bool internal_attribute);
struct mailbox_attribute_iter *
acl_attribute_iter_init(struct mailbox *box, enum mail_attribute_type type,
const char *prefix);
Expand Down
35 changes: 9 additions & 26 deletions src/plugins/push-notification/push-notification-driver-ox.c
Expand Up @@ -141,7 +141,6 @@ static const char *push_notification_driver_ox_get_metadata
struct push_notification_driver_ox_config *dconfig = dtxn->duser->context;
struct mail_attribute_value attr;
struct mailbox *inbox;
struct mailbox_transaction_context *mctx = NULL;
struct mail_namespace *ns;
bool success = FALSE, use_existing_txn = FALSE;
int ret;
Expand All @@ -155,39 +154,23 @@ static const char *push_notification_driver_ox_get_metadata
/* Get canonical INBOX, where private server-level metadata is stored.
* See imap/cmd-getmetadata.c */
if ((dtxn->ptxn->t != NULL) && dtxn->ptxn->mbox->inbox_user) {
/* Use the currently open transaction. */
inbox = dtxn->ptxn->mbox;
mctx = dtxn->ptxn->t;
use_existing_txn = TRUE;
} else {
ns = mail_namespace_find_inbox(dtxn->ptxn->muser->namespaces);
inbox = mailbox_alloc(ns->list, "INBOX", MAILBOX_FLAG_READONLY);
if (mailbox_open(inbox) < 0) {
i_error(OX_LOG_LABEL "Skipped because unable to open INBOX: %s",
mailbox_get_last_error(inbox, NULL));
} else {
mctx = mailbox_transaction_begin(inbox, 0);
}
}

if (mctx != NULL) {
ret = mailbox_attribute_get(mctx, MAIL_ATTRIBUTE_TYPE_PRIVATE,
ret = mailbox_attribute_get(inbox, MAIL_ATTRIBUTE_TYPE_PRIVATE,
OX_METADATA_KEY, &attr);
if (ret < 0) {
i_error(OX_LOG_LABEL "Skipped because unable to get attribute: %s",
mailbox_get_last_error(inbox, NULL));
} else if (ret == 0) {
push_notification_driver_debug(OX_LOG_LABEL, dtxn->ptxn->muser,
"Skipped because not active (/private/"OX_METADATA_KEY" METADATA not set)");
} else {
success = TRUE;
}

if (!use_existing_txn && (mailbox_transaction_commit(&mctx) < 0)) {
i_error(OX_LOG_LABEL "Transaction commit failed: %s",
mailbox_get_last_error(inbox, NULL));
/* the commit doesn't matter though. */
}
if (ret < 0) {
i_error(OX_LOG_LABEL "Skipped because unable to get attribute: %s",
mailbox_get_last_error(inbox, NULL));
} else if (ret == 0) {
push_notification_driver_debug(OX_LOG_LABEL, dtxn->ptxn->muser,
"Skipped because not active (/private/"OX_METADATA_KEY" METADATA not set)");
} else {
success = TRUE;
}

if (!use_existing_txn) {
Expand Down

0 comments on commit 66c8772

Please sign in to comment.