make struct imap_cmd_refcounted_state "abstract"

take the callback out of it, so it can be individualized.
so far, this only increases code size ...
This commit is contained in:
Oswald Buddenhagen 2017-03-24 16:56:43 +01:00
parent 96b5ae8360
commit 39247197f9

View File

@ -183,8 +183,6 @@ typedef struct {
} imap_cmd_find_new_t; } imap_cmd_find_new_t;
typedef struct { typedef struct {
void (*callback)( int sts, void *aux );
void *callback_aux;
int ref_count; int ref_count;
int ret_val; int ret_val;
} imap_cmd_refcounted_state_t; } imap_cmd_refcounted_state_t;
@ -559,16 +557,19 @@ imap_done_simple_msg( imap_store_t *ctx ATTR_UNUSED,
} }
static imap_cmd_refcounted_state_t * static imap_cmd_refcounted_state_t *
imap_refcounted_new_state( void (*cb)( int, void * ), void *aux ) imap_refcounted_new_state( int sz )
{ {
imap_cmd_refcounted_state_t *sts = nfmalloc( sizeof(*sts) ); imap_cmd_refcounted_state_t *sts = nfmalloc( sz );
sts->callback = cb;
sts->callback_aux = aux;
sts->ref_count = 1; /* so forced sync does not cause an early exit */ sts->ref_count = 1; /* so forced sync does not cause an early exit */
sts->ret_val = DRV_OK; sts->ret_val = DRV_OK;
return sts; return sts;
} }
#define INIT_REFCOUNTED_STATE(type, sts, cb, aux) \
type *sts = (type *)imap_refcounted_new_state( sizeof(type) ); \
sts->callback = cb; \
sts->callback_aux = aux;
static imap_cmd_t * static imap_cmd_t *
imap_refcounted_new_cmd( imap_cmd_refcounted_state_t *sts ) imap_refcounted_new_cmd( imap_cmd_refcounted_state_t *sts )
{ {
@ -578,20 +579,21 @@ imap_refcounted_new_cmd( imap_cmd_refcounted_state_t *sts )
return &cmd->gen; return &cmd->gen;
} }
static void #define DONE_REFCOUNTED_STATE(sts) \
imap_refcounted_done( imap_cmd_refcounted_state_t *sts ) if (!--sts->gen.ref_count) { \
{ sts->callback( sts->gen.ret_val, sts->callback_aux ); \
if (!--sts->ref_count) { free( sts ); \
sts->callback( sts->ret_val, sts->callback_aux );
free( sts );
} }
#define DONE_REFCOUNTED_STATE_ARGS(sts, ...) \
if (!--sts->gen.ref_count) { \
sts->callback( sts->gen.ret_val, __VA_ARGS__, sts->callback_aux ); \
free( sts ); \
} }
static void static void
imap_refcounted_done_box( imap_store_t *ctx ATTR_UNUSED, imap_cmd_t *cmd, int response ) transform_refcounted_box_response( imap_cmd_refcounted_state_t *sts, int response )
{ {
imap_cmd_refcounted_state_t *sts = ((imap_cmd_refcounted_t *)cmd)->state;
switch (response) { switch (response) {
case RESP_CANCEL: case RESP_CANCEL:
sts->ret_val = DRV_CANCELED; sts->ret_val = DRV_CANCELED;
@ -601,7 +603,6 @@ imap_refcounted_done_box( imap_store_t *ctx ATTR_UNUSED, imap_cmd_t *cmd, int re
sts->ret_val = DRV_BOX_BAD; sts->ret_val = DRV_BOX_BAD;
break; break;
} }
imap_refcounted_done( sts );
} }
static const char * static const char *
@ -2473,7 +2474,14 @@ imap_set_range( imap_range_t *ranges, int *nranges, int low_flags, int high_flag
ranges[r].flags |= (ranges[r].last <= maxlow) ? low_flags : high_flags; ranges[r].flags |= (ranges[r].last <= maxlow) ? low_flags : high_flags;
} }
static void imap_submit_load( imap_store_t *, const char *, int, imap_cmd_refcounted_state_t * ); typedef struct {
imap_cmd_refcounted_state_t gen;
void (*callback)( int sts, void *aux );
void *callback_aux;
} imap_load_box_state_t;
static void imap_submit_load( imap_store_t *, const char *, int, imap_load_box_state_t * );
static void imap_submit_load_p3( imap_load_box_state_t * );
static void static void
imap_load_box( store_t *gctx, int minuid, int maxuid, int newuid, int seenuid, int_array_t excs, imap_load_box( store_t *gctx, int minuid, int maxuid, int newuid, int seenuid, int_array_t excs,
@ -2487,8 +2495,7 @@ imap_load_box( store_t *gctx, int minuid, int maxuid, int newuid, int seenuid, i
free( excs.data ); free( excs.data );
cb( DRV_OK, aux ); cb( DRV_OK, aux );
} else { } else {
imap_cmd_refcounted_state_t *sts = imap_refcounted_new_state( cb, aux ); INIT_REFCOUNTED_STATE(imap_load_box_state_t, sts, cb, aux)
for (i = 0; i < excs.size; ) { for (i = 0; i < excs.size; ) {
for (bl = 0; i < excs.size && bl < 960; i++) { for (bl = 0; i < excs.size && bl < 960; i++) {
if (bl) if (bl)
@ -2522,14 +2529,16 @@ imap_load_box( store_t *gctx, int minuid, int maxuid, int newuid, int seenuid, i
} }
} }
free( excs.data ); free( excs.data );
imap_refcounted_done( sts ); imap_submit_load_p3( sts );
} }
} }
static void imap_submit_load_p2( imap_store_t *, imap_cmd_t *, int );
static void static void
imap_submit_load( imap_store_t *ctx, const char *buf, int flags, imap_cmd_refcounted_state_t *sts ) imap_submit_load( imap_store_t *ctx, const char *buf, int flags, imap_load_box_state_t *sts )
{ {
imap_exec( ctx, imap_refcounted_new_cmd( sts ), imap_refcounted_done_box, imap_exec( ctx, imap_refcounted_new_cmd( &sts->gen ), imap_submit_load_p2,
"UID FETCH %s (UID%s%s%s%s%s%s%s)", buf, "UID FETCH %s (UID%s%s%s%s%s%s%s)", buf,
(ctx->opts & OPEN_FLAGS) ? " FLAGS" : "", (ctx->opts & OPEN_FLAGS) ? " FLAGS" : "",
(flags & WantSize) ? " RFC822.SIZE" : "", (flags & WantSize) ? " RFC822.SIZE" : "",
@ -2540,6 +2549,21 @@ imap_submit_load( imap_store_t *ctx, const char *buf, int flags, imap_cmd_refcou
(flags & (WantTuids | WantMsgids)) ? ")]" : ""); (flags & (WantTuids | WantMsgids)) ? ")]" : "");
} }
static void
imap_submit_load_p2( imap_store_t *ctx ATTR_UNUSED, imap_cmd_t *cmd, int response )
{
imap_load_box_state_t *sts = (imap_load_box_state_t *)((imap_cmd_refcounted_t *)cmd)->state;
transform_refcounted_box_response( &sts->gen, response );
imap_submit_load_p3( sts );
}
static void
imap_submit_load_p3( imap_load_box_state_t *sts )
{
DONE_REFCOUNTED_STATE(sts)
}
/******************* imap_fetch_msg *******************/ /******************* imap_fetch_msg *******************/
static void imap_fetch_msg_p2( imap_store_t *, imap_cmd_t *, int ); static void imap_fetch_msg_p2( imap_store_t *, imap_cmd_t *, int );
@ -2574,8 +2598,6 @@ imap_fetch_msg_p2( imap_store_t *ctx, imap_cmd_t *gcmd, int response )
/******************* imap_set_msg_flags *******************/ /******************* imap_set_msg_flags *******************/
static void imap_set_flags_p2( imap_store_t *, imap_cmd_t *, int );
static int static int
imap_make_flags( int flags, char *buf ) imap_make_flags( int flags, char *buf )
{ {
@ -2594,14 +2616,23 @@ imap_make_flags( int flags, char *buf )
return d; return d;
} }
typedef struct {
imap_cmd_refcounted_state_t gen;
void (*callback)( int sts, void *aux );
void *callback_aux;
} imap_set_msg_flags_state_t;
static void imap_set_flags_p2( imap_store_t *, imap_cmd_t *, int );
static void imap_set_flags_p3( imap_set_msg_flags_state_t * );
static void static void
imap_flags_helper( imap_store_t *ctx, int uid, char what, int flags, imap_flags_helper( imap_store_t *ctx, int uid, char what, int flags,
imap_cmd_refcounted_state_t *sts ) imap_set_msg_flags_state_t *sts )
{ {
char buf[256]; char buf[256];
buf[imap_make_flags( flags, buf )] = 0; buf[imap_make_flags( flags, buf )] = 0;
imap_exec( ctx, imap_refcounted_new_cmd( sts ), imap_set_flags_p2, imap_exec( ctx, imap_refcounted_new_cmd( &sts->gen ), imap_set_flags_p2,
"UID STORE %d %cFLAGS.SILENT %s", uid, what, buf ); "UID STORE %d %cFLAGS.SILENT %s", uid, what, buf );
} }
@ -2619,12 +2650,12 @@ imap_set_msg_flags( store_t *gctx, message_t *msg, int uid, int add, int del,
msg->flags &= ~del; msg->flags &= ~del;
} }
if (add || del) { if (add || del) {
imap_cmd_refcounted_state_t *sts = imap_refcounted_new_state( cb, aux ); INIT_REFCOUNTED_STATE(imap_set_msg_flags_state_t, sts, cb, aux)
if (add) if (add)
imap_flags_helper( ctx, uid, '+', add, sts ); imap_flags_helper( ctx, uid, '+', add, sts );
if (del) if (del)
imap_flags_helper( ctx, uid, '-', del, sts ); imap_flags_helper( ctx, uid, '-', del, sts );
imap_refcounted_done( sts ); imap_set_flags_p3( sts );
} else { } else {
cb( DRV_OK, aux ); cb( DRV_OK, aux );
} }
@ -2633,21 +2664,36 @@ imap_set_msg_flags( store_t *gctx, message_t *msg, int uid, int add, int del,
static void static void
imap_set_flags_p2( imap_store_t *ctx ATTR_UNUSED, imap_cmd_t *cmd, int response ) imap_set_flags_p2( imap_store_t *ctx ATTR_UNUSED, imap_cmd_t *cmd, int response )
{ {
imap_cmd_refcounted_state_t *sts = ((imap_cmd_refcounted_t *)cmd)->state; imap_set_msg_flags_state_t *sts = (imap_set_msg_flags_state_t *)((imap_cmd_refcounted_t *)cmd)->state;
switch (response) { switch (response) {
case RESP_CANCEL: case RESP_CANCEL:
sts->ret_val = DRV_CANCELED; sts->gen.ret_val = DRV_CANCELED;
break; break;
case RESP_NO: case RESP_NO:
if (sts->ret_val == DRV_OK) /* Don't override cancelation. */ if (sts->gen.ret_val == DRV_OK) /* Don't override cancelation. */
sts->ret_val = DRV_MSG_BAD; sts->gen.ret_val = DRV_MSG_BAD;
break; break;
} }
imap_refcounted_done( sts ); imap_set_flags_p3( sts );
}
static void
imap_set_flags_p3( imap_set_msg_flags_state_t *sts )
{
DONE_REFCOUNTED_STATE(sts)
} }
/******************* imap_close_box *******************/ /******************* imap_close_box *******************/
typedef struct {
imap_cmd_refcounted_state_t gen;
void (*callback)( int sts, void *aux );
void *callback_aux;
} imap_expunge_state_t;
static void imap_close_box_p2( imap_store_t *, imap_cmd_t *, int );
static void imap_close_box_p3( imap_expunge_state_t * );
static void static void
imap_close_box( store_t *gctx, imap_close_box( store_t *gctx,
void (*cb)( int sts, void *aux ), void *aux ) void (*cb)( int sts, void *aux ), void *aux )
@ -2655,7 +2701,7 @@ imap_close_box( store_t *gctx,
imap_store_t *ctx = (imap_store_t *)gctx; imap_store_t *ctx = (imap_store_t *)gctx;
if (ctx->gen.conf->trash && CAP(UIDPLUS)) { if (ctx->gen.conf->trash && CAP(UIDPLUS)) {
imap_cmd_refcounted_state_t *sts = imap_refcounted_new_state( cb, aux ); INIT_REFCOUNTED_STATE(imap_expunge_state_t, sts, cb, aux)
message_t *msg, *fmsg, *nmsg; message_t *msg, *fmsg, *nmsg;
int bl; int bl;
char buf[1000]; char buf[1000];
@ -2674,10 +2720,10 @@ imap_close_box( store_t *gctx,
} }
if (!bl) if (!bl)
break; break;
imap_exec( ctx, imap_refcounted_new_cmd( sts ), imap_refcounted_done_box, imap_exec( ctx, imap_refcounted_new_cmd( &sts->gen ), imap_close_box_p2,
"UID EXPUNGE %s", buf ); "UID EXPUNGE %s", buf );
} }
imap_refcounted_done( sts ); imap_close_box_p3( sts );
} else { } else {
/* This is inherently racy: it may cause messages which other clients /* This is inherently racy: it may cause messages which other clients
* marked as deleted to be expunged without being trashed. */ * marked as deleted to be expunged without being trashed. */
@ -2687,6 +2733,21 @@ imap_close_box( store_t *gctx,
} }
} }
static void
imap_close_box_p2( imap_store_t *ctx ATTR_UNUSED, imap_cmd_t *cmd, int response )
{
imap_expunge_state_t *sts = (imap_expunge_state_t *)((imap_cmd_refcounted_t *)cmd)->state;
transform_refcounted_box_response( &sts->gen, response );
imap_close_box_p3( sts );
}
static void
imap_close_box_p3( imap_expunge_state_t *sts )
{
DONE_REFCOUNTED_STATE(sts)
}
/******************* imap_trash_msg *******************/ /******************* imap_trash_msg *******************/
static void static void
@ -2831,12 +2892,21 @@ imap_find_new_msgs_p3( imap_store_t *ctx, imap_cmd_t *gcmd, int response )
/******************* imap_list_store *******************/ /******************* imap_list_store *******************/
typedef struct {
imap_cmd_refcounted_state_t gen;
void (*callback)( int sts, void *aux );
void *callback_aux;
} imap_list_store_state_t;
static void imap_list_store_p2( imap_store_t *, imap_cmd_t *, int );
static void imap_list_store_p3( imap_list_store_state_t * );
static void static void
imap_list_store( store_t *gctx, int flags, imap_list_store( store_t *gctx, int flags,
void (*cb)( int sts, void *aux ), void *aux ) void (*cb)( int sts, void *aux ), void *aux )
{ {
imap_store_t *ctx = (imap_store_t *)gctx; imap_store_t *ctx = (imap_store_t *)gctx;
imap_cmd_refcounted_state_t *sts = imap_refcounted_new_state( cb, aux ); INIT_REFCOUNTED_STATE(imap_list_store_state_t, sts, cb, aux)
// ctx->prefix may be empty, "INBOX.", or something else. // ctx->prefix may be empty, "INBOX.", or something else.
// 'flags' may be LIST_INBOX, LIST_PATH (or LIST_PATH_MAYBE), or both. 'listed' // 'flags' may be LIST_INBOX, LIST_PATH (or LIST_PATH_MAYBE), or both. 'listed'
@ -2858,17 +2928,32 @@ imap_list_store( store_t *gctx, int flags,
ctx->gen.listed |= LIST_PATH; ctx->gen.listed |= LIST_PATH;
if (pfx_is_empty) if (pfx_is_empty)
ctx->gen.listed |= LIST_INBOX; ctx->gen.listed |= LIST_INBOX;
imap_exec( ctx, imap_refcounted_new_cmd( sts ), imap_refcounted_done_box, imap_exec( ctx, imap_refcounted_new_cmd( &sts->gen ), imap_list_store_p2,
"LIST \"\" \"%\\s*\"", ctx->prefix ); "LIST \"\" \"%\\s*\"", ctx->prefix );
} }
if (((flags & LIST_INBOX) || pfx_is_inbox) && !pfx_is_empty && !(ctx->gen.listed & LIST_INBOX)) { if (((flags & LIST_INBOX) || pfx_is_inbox) && !pfx_is_empty && !(ctx->gen.listed & LIST_INBOX)) {
ctx->gen.listed |= LIST_INBOX; ctx->gen.listed |= LIST_INBOX;
if (pfx_is_inbox) if (pfx_is_inbox)
ctx->gen.listed |= LIST_PATH; ctx->gen.listed |= LIST_PATH;
imap_exec( ctx, imap_refcounted_new_cmd( sts ), imap_refcounted_done_box, imap_exec( ctx, imap_refcounted_new_cmd( &sts->gen ), imap_list_store_p2,
"LIST \"\" INBOX*" ); "LIST \"\" INBOX*" );
} }
imap_refcounted_done( sts ); imap_list_store_p3( sts );
}
static void
imap_list_store_p2( imap_store_t *ctx ATTR_UNUSED, imap_cmd_t *cmd, int response )
{
imap_list_store_state_t *sts = (imap_list_store_state_t *)((imap_cmd_refcounted_t *)cmd)->state;
transform_refcounted_box_response( &sts->gen, response );
imap_list_store_p3( sts );
}
static void
imap_list_store_p3( imap_list_store_state_t *sts )
{
DONE_REFCOUNTED_STATE(sts)
} }
/******************* imap_cancel_cmds *******************/ /******************* imap_cancel_cmds *******************/