refactor maildir_set_uid() and maildir_store_uidval()

the latter now handles both the db and the plaintext file, so the former
can make use of it.
This commit is contained in:
Oswald Buddenhagen 2015-01-01 19:18:27 +01:00
parent f61efdbb9d
commit 4da89af7be

View File

@ -451,14 +451,19 @@ make_key( const char *info_stop, DBT *tkey, char *name )
tkey->data = name; tkey->data = name;
tkey->size = u ? (size_t)(u - name) : strlen( name ); tkey->size = u ? (size_t)(u - name) : strlen( name );
} }
#endif /* USE_DB */
static int static int
maildir_set_uid( maildir_store_t *ctx, const char *name, int *uid ) maildir_store_uidval( maildir_store_t *ctx )
{ {
int n;
#ifdef USE_DB
int ret, uv[2]; int ret, uv[2];
#endif
char buf[128];
if (uid) #ifdef USE_DB
*uid = ++ctx->nuid; if (ctx->db) {
key.data = (void *)"UIDVALIDITY"; key.data = (void *)"UIDVALIDITY";
key.size = 11; key.size = 11;
uv[0] = ctx->gen.uidvalidity; uv[0] = ctx->gen.uidvalidity;
@ -466,37 +471,23 @@ maildir_set_uid( maildir_store_t *ctx, const char *name, int *uid )
value.data = uv; value.data = uv;
value.size = sizeof(uv); value.size = sizeof(uv);
if ((ret = ctx->db->put( ctx->db, 0, &key, &value, 0 ))) { if ((ret = ctx->db->put( ctx->db, 0, &key, &value, 0 ))) {
tbork:
ctx->db->err( ctx->db, ret, "Maildir error: db->put()" ); ctx->db->err( ctx->db, ret, "Maildir error: db->put()" );
return DRV_BOX_BAD; return DRV_BOX_BAD;
} }
if (uid) {
make_key( ((maildir_store_conf_t *)ctx->gen.conf)->info_stop, &key, (char *)name );
value.data = uid;
value.size = sizeof(*uid);
if ((ret = ctx->db->put( ctx->db, 0, &key, &value, 0 )))
goto tbork;
}
if ((ret = ctx->db->sync( ctx->db, 0 ))) { if ((ret = ctx->db->sync( ctx->db, 0 ))) {
ctx->db->err( ctx->db, ret, "Maildir error: db->sync()" ); ctx->db->err( ctx->db, ret, "Maildir error: db->sync()" );
return DRV_BOX_BAD; return DRV_BOX_BAD;
} }
return DRV_OK; } else
}
#endif /* USE_DB */ #endif /* USE_DB */
static int
maildir_store_uidval( maildir_store_t *ctx )
{ {
int n;
char buf[128];
n = sprintf( buf, "%d\n%d\n", ctx->gen.uidvalidity, ctx->nuid ); n = sprintf( buf, "%d\n%d\n", ctx->gen.uidvalidity, ctx->nuid );
lseek( ctx->uvfd, 0, SEEK_SET ); lseek( ctx->uvfd, 0, SEEK_SET );
if (write( ctx->uvfd, buf, n ) != n || ftruncate( ctx->uvfd, n ) || (UseFSync && fdatasync( ctx->uvfd ))) { if (write( ctx->uvfd, buf, n ) != n || ftruncate( ctx->uvfd, n ) || (UseFSync && fdatasync( ctx->uvfd ))) {
error( "Maildir error: cannot write UIDVALIDITY.\n" ); error( "Maildir error: cannot write UIDVALIDITY.\n" );
return DRV_BOX_BAD; return DRV_BOX_BAD;
} }
}
conf_wakeup( &ctx->lcktmr, 2 ); conf_wakeup( &ctx->lcktmr, 2 );
return DRV_OK; return DRV_OK;
} }
@ -511,7 +502,6 @@ maildir_init_uidval( maildir_store_t *ctx )
if (ctx->db) { if (ctx->db) {
u_int32_t count; u_int32_t count;
ctx->db->truncate( ctx->db, 0, &count, 0 ); ctx->db->truncate( ctx->db, 0, &count, 0 );
return maildir_set_uid( ctx, 0, 0 );
} }
#endif /* USE_DB */ #endif /* USE_DB */
return maildir_store_uidval( ctx ); return maildir_store_uidval( ctx );
@ -598,6 +588,25 @@ maildir_obtain_uid( maildir_store_t *ctx, int *uid )
return maildir_store_uidval( ctx ); return maildir_store_uidval( ctx );
} }
#ifdef USE_DB
static int
maildir_set_uid( maildir_store_t *ctx, const char *name, int *uid )
{
int ret;
*uid = ++ctx->nuid;
make_key( ((maildir_store_conf_t *)ctx->gen.conf)->info_stop, &key, (char *)name );
value.data = uid;
value.size = sizeof(*uid);
if ((ret = ctx->db->put( ctx->db, 0, &key, &value, 0 ))) {
ctx->db->err( ctx->db, ret, "Maildir error: db->put()" );
return DRV_BOX_BAD;
}
return maildir_store_uidval( ctx );
}
#endif
static int static int
maildir_compare( const void *l, const void *r ) maildir_compare( const void *l, const void *r )
{ {