2004-03-27 16:07:20 +00:00
|
|
|
/*
|
|
|
|
* mbsync - mailbox synchronizer
|
|
|
|
* Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
|
2013-04-20 14:57:16 +00:00
|
|
|
* Copyright (C) 2002-2006,2010-2013 Oswald Buddenhagen <ossi@users.sf.net>
|
2004-03-27 16:07:20 +00:00
|
|
|
* Copyright (C) 2004 Theodore Y. Ts'o <tytso@mit.edu>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2011-04-10 17:34:36 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2004-03-27 16:07:20 +00:00
|
|
|
*
|
|
|
|
* As a special exception, mbsync may be linked with the OpenSSL library,
|
|
|
|
* despite that library's more restrictive license.
|
|
|
|
*/
|
|
|
|
|
2013-12-08 19:46:40 +00:00
|
|
|
#include "driver.h"
|
2004-03-27 16:07:20 +00:00
|
|
|
|
2011-04-10 11:06:07 +00:00
|
|
|
#include <assert.h>
|
2004-03-27 16:07:20 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <time.h>
|
2013-07-28 13:55:13 +00:00
|
|
|
#include <utime.h>
|
2004-03-27 16:07:20 +00:00
|
|
|
|
2004-09-20 11:31:55 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
# define LEGACY_FLOCK 1
|
|
|
|
#endif
|
2004-03-27 16:07:20 +00:00
|
|
|
|
2014-01-02 18:36:45 +00:00
|
|
|
#if !defined(_POSIX_SYNCHRONIZED_IO) || _POSIX_SYNCHRONIZED_IO <= 0
|
2012-09-09 10:18:14 +00:00
|
|
|
# define fdatasync fsync
|
|
|
|
#endif
|
|
|
|
|
2004-03-27 16:07:20 +00:00
|
|
|
#ifdef USE_DB
|
|
|
|
#include <db.h>
|
|
|
|
#endif /* USE_DB */
|
|
|
|
|
|
|
|
typedef struct maildir_store_conf {
|
|
|
|
store_conf_t gen;
|
|
|
|
char *inbox;
|
|
|
|
#ifdef USE_DB
|
|
|
|
int alt_map;
|
|
|
|
#endif /* USE_DB */
|
2014-10-25 15:30:57 +00:00
|
|
|
char info_delimiter;
|
|
|
|
char *info_prefix, *info_stop; /* precalculated from info_delimiter */
|
2004-03-27 16:07:20 +00:00
|
|
|
} maildir_store_conf_t;
|
|
|
|
|
|
|
|
typedef struct maildir_message {
|
|
|
|
message_t gen;
|
|
|
|
char *base;
|
|
|
|
} maildir_message_t;
|
|
|
|
|
|
|
|
typedef struct maildir_store {
|
|
|
|
store_t gen;
|
2015-03-26 13:28:06 +00:00
|
|
|
int uvfd, uvok, nuid, is_inbox, fresh[3];
|
2011-04-10 11:06:07 +00:00
|
|
|
int minuid, maxuid, newuid, nexcs, *excs;
|
2012-08-11 15:11:57 +00:00
|
|
|
char *trash;
|
2004-03-27 16:07:20 +00:00
|
|
|
#ifdef USE_DB
|
|
|
|
DB *db;
|
2015-01-01 18:25:17 +00:00
|
|
|
char *usedb;
|
2004-03-27 16:07:20 +00:00
|
|
|
#endif /* USE_DB */
|
2015-01-01 12:24:30 +00:00
|
|
|
wakeup_t lcktmr;
|
2004-03-27 16:07:20 +00:00
|
|
|
} maildir_store_t;
|
|
|
|
|
|
|
|
#ifdef USE_DB
|
|
|
|
static DBT key, value; /* no need to be reentrant, and this saves lots of memset()s */
|
|
|
|
#endif /* USE_DB */
|
|
|
|
static struct flock lck;
|
|
|
|
|
|
|
|
static int MaildirCount;
|
|
|
|
|
2015-03-23 07:42:51 +00:00
|
|
|
static void ATTR_PRINTFLIKE(1, 2)
|
|
|
|
debug( const char *msg, ... )
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
va_start( va, msg );
|
|
|
|
vdebug( DEBUG_SYNC, msg, va );
|
|
|
|
va_end( va );
|
|
|
|
}
|
|
|
|
|
2004-03-27 16:07:20 +00:00
|
|
|
static const char Flags[] = { 'D', 'F', 'R', 'S', 'T' };
|
|
|
|
|
2014-12-07 12:19:30 +00:00
|
|
|
static uchar
|
2014-10-25 15:30:57 +00:00
|
|
|
maildir_parse_flags( const char *info_prefix, const char *base )
|
2004-03-27 16:07:20 +00:00
|
|
|
{
|
|
|
|
const char *s;
|
2014-12-07 12:19:30 +00:00
|
|
|
uint i;
|
|
|
|
uchar flags;
|
2004-03-27 16:07:20 +00:00
|
|
|
|
|
|
|
flags = 0;
|
2014-10-25 15:30:57 +00:00
|
|
|
if ((s = strstr( base, info_prefix )))
|
2004-03-27 16:07:20 +00:00
|
|
|
for (s += 3, i = 0; i < as(Flags); i++)
|
|
|
|
if (strchr( s, Flags[i] ))
|
|
|
|
flags |= (1 << i);
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2012-08-11 16:34:46 +00:00
|
|
|
static char *
|
|
|
|
maildir_join_path( const char *prefix, const char *box )
|
|
|
|
{
|
|
|
|
char *out, *p;
|
|
|
|
int pl, bl, n;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
pl = strlen( prefix );
|
|
|
|
for (bl = 0, n = 0; (c = box[bl]); bl++)
|
|
|
|
if (c == '/')
|
|
|
|
n++;
|
|
|
|
out = nfmalloc( pl + bl + n + 1 );
|
|
|
|
memcpy( out, prefix, pl );
|
|
|
|
p = out + pl;
|
|
|
|
while ((c = *box++)) {
|
|
|
|
*p++ = c;
|
|
|
|
if (c == '/')
|
|
|
|
*p++ = '.';
|
|
|
|
}
|
|
|
|
*p = 0;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2014-10-04 10:16:10 +00:00
|
|
|
static int
|
2015-01-03 23:39:06 +00:00
|
|
|
maildir_validate_path( store_conf_t *conf )
|
2004-03-27 16:07:20 +00:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
2014-10-04 10:16:10 +00:00
|
|
|
if (!conf->path) {
|
|
|
|
error( "Maildir error: store '%s' has no Path\n", conf->name );
|
2015-01-03 23:39:06 +00:00
|
|
|
conf->failed = FAIL_FINAL;
|
2014-10-04 10:16:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
if (stat( conf->path, &st ) || !S_ISDIR(st.st_mode)) {
|
2011-04-10 13:32:25 +00:00
|
|
|
error( "Maildir error: cannot open store '%s'\n", conf->path );
|
2015-01-03 23:39:06 +00:00
|
|
|
conf->failed = FAIL_FINAL;
|
2014-10-04 10:16:10 +00:00
|
|
|
return -1;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
2014-10-04 10:16:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-01 12:24:30 +00:00
|
|
|
static void lcktmr_timeout( void *aux );
|
|
|
|
|
2014-10-04 10:16:10 +00:00
|
|
|
static void
|
|
|
|
maildir_open_store( store_conf_t *conf, const char *label ATTR_UNUSED,
|
|
|
|
void (*cb)( store_t *ctx, void *aux ), void *aux )
|
|
|
|
{
|
|
|
|
maildir_store_t *ctx;
|
|
|
|
|
2004-03-27 16:07:20 +00:00
|
|
|
ctx = nfcalloc( sizeof(*ctx) );
|
|
|
|
ctx->gen.conf = conf;
|
|
|
|
ctx->uvfd = -1;
|
2015-01-01 12:24:30 +00:00
|
|
|
init_wakeup( &ctx->lcktmr, lcktmr_timeout, ctx );
|
2014-10-04 10:16:10 +00:00
|
|
|
if (conf->trash) {
|
|
|
|
if (maildir_validate_path( conf ) < 0) {
|
|
|
|
free( ctx );
|
|
|
|
cb( 0, aux );
|
|
|
|
return;
|
|
|
|
}
|
2012-08-11 16:34:46 +00:00
|
|
|
ctx->trash = maildir_join_path( conf->path, conf->trash );
|
2014-10-04 10:16:10 +00:00
|
|
|
}
|
2006-03-21 20:03:21 +00:00
|
|
|
cb( &ctx->gen, aux );
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_maildir_messages( message_t *msg )
|
|
|
|
{
|
|
|
|
message_t *tmsg;
|
|
|
|
|
|
|
|
for (; (tmsg = msg); msg = tmsg) {
|
|
|
|
tmsg = msg->next;
|
|
|
|
free( ((maildir_message_t *)msg)->base );
|
|
|
|
free( msg );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
maildir_cleanup( store_t *gctx )
|
|
|
|
{
|
|
|
|
maildir_store_t *ctx = (maildir_store_t *)gctx;
|
|
|
|
|
|
|
|
free_maildir_messages( gctx->msgs );
|
|
|
|
#ifdef USE_DB
|
|
|
|
if (ctx->db)
|
|
|
|
ctx->db->close( ctx->db, 0 );
|
2015-01-01 18:25:17 +00:00
|
|
|
free( ctx->usedb );
|
2004-03-27 16:07:20 +00:00
|
|
|
#endif /* USE_DB */
|
2010-11-14 15:44:50 +00:00
|
|
|
free( gctx->path );
|
|
|
|
free( ctx->excs );
|
2004-03-27 16:07:20 +00:00
|
|
|
if (ctx->uvfd >= 0)
|
|
|
|
close( ctx->uvfd );
|
2015-01-01 12:24:30 +00:00
|
|
|
conf_wakeup( &ctx->lcktmr, -1 );
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-03-20 19:38:20 +00:00
|
|
|
maildir_disown_store( store_t *gctx )
|
2004-03-27 16:07:20 +00:00
|
|
|
{
|
2015-01-01 12:24:30 +00:00
|
|
|
maildir_store_t *ctx = (maildir_store_t *)gctx;
|
|
|
|
|
2004-03-27 16:07:20 +00:00
|
|
|
maildir_cleanup( gctx );
|
2015-01-01 12:24:30 +00:00
|
|
|
wipe_wakeup( &ctx->lcktmr );
|
|
|
|
free( ctx->trash );
|
2006-03-20 19:27:38 +00:00
|
|
|
free_string_list( gctx->boxes );
|
2004-03-27 16:07:20 +00:00
|
|
|
free( gctx );
|
|
|
|
}
|
|
|
|
|
2006-03-20 19:38:20 +00:00
|
|
|
static void
|
|
|
|
maildir_cleanup_drv( void )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-07-15 10:55:04 +00:00
|
|
|
static void
|
|
|
|
maildir_invoke_bad_callback( store_t *ctx )
|
|
|
|
{
|
|
|
|
ctx->bad_callback( ctx->bad_callback_aux );
|
|
|
|
}
|
|
|
|
|
2013-04-01 10:20:03 +00:00
|
|
|
static int maildir_list_inbox( store_t *gctx, int *flags );
|
2012-08-11 16:34:46 +00:00
|
|
|
|
|
|
|
static int
|
2014-10-04 15:07:17 +00:00
|
|
|
maildir_list_recurse( store_t *gctx, int isBox, int *flags, const char *inbox, int inboxLen,
|
2012-08-11 16:34:46 +00:00
|
|
|
char *path, int pathLen, char *name, int nameLen )
|
2004-03-27 16:07:20 +00:00
|
|
|
{
|
|
|
|
DIR *dir;
|
2015-01-17 14:27:08 +00:00
|
|
|
int pl, nl;
|
2004-03-27 16:07:20 +00:00
|
|
|
struct dirent *de;
|
2012-08-11 16:34:46 +00:00
|
|
|
struct stat st;
|
2004-03-27 16:07:20 +00:00
|
|
|
|
2012-08-11 16:34:46 +00:00
|
|
|
if (isBox) {
|
2013-12-08 09:44:50 +00:00
|
|
|
path[pathLen++] = '/';
|
2015-01-17 14:27:08 +00:00
|
|
|
if (isBox > 1 ||
|
|
|
|
(nfsnprintf( path + pathLen, _POSIX_PATH_MAX - pathLen, "cur" ),
|
|
|
|
!stat( path, &st ) && S_ISDIR(st.st_mode)))
|
2013-04-13 17:25:20 +00:00
|
|
|
add_string_list( &gctx->boxes, name );
|
2012-08-11 16:34:46 +00:00
|
|
|
path[pathLen] = 0;
|
|
|
|
name[nameLen++] = '/';
|
|
|
|
}
|
|
|
|
if (!(dir = opendir( path ))) {
|
2015-01-17 14:27:08 +00:00
|
|
|
if (isBox && (errno == ENOENT || errno == ENOTDIR))
|
|
|
|
return 0;
|
2012-08-11 16:34:46 +00:00
|
|
|
sys_error( "Maildir error: cannot list %s", path );
|
|
|
|
return -1;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
while ((de = readdir( dir ))) {
|
2012-08-11 16:34:46 +00:00
|
|
|
const char *ent = de->d_name;
|
|
|
|
pl = pathLen + nfsnprintf( path + pathLen, _POSIX_PATH_MAX - pathLen, "%s", ent );
|
2014-10-04 15:07:17 +00:00
|
|
|
if (inbox && equals( path, pl, inbox, inboxLen )) {
|
2014-04-12 10:46:36 +00:00
|
|
|
if (maildir_list_inbox( gctx, flags ) < 0) {
|
|
|
|
closedir( dir );
|
2012-08-11 16:34:46 +00:00
|
|
|
return -1;
|
2014-04-12 10:46:36 +00:00
|
|
|
}
|
2012-08-11 16:34:46 +00:00
|
|
|
} else {
|
|
|
|
if (*ent == '.') {
|
|
|
|
if (!isBox)
|
|
|
|
continue;
|
2013-12-08 09:44:50 +00:00
|
|
|
if (!ent[1] || ent[1] == '.')
|
|
|
|
continue;
|
2012-08-11 16:34:46 +00:00
|
|
|
ent++;
|
|
|
|
} else {
|
|
|
|
if (isBox)
|
|
|
|
continue;
|
2014-10-04 15:07:17 +00:00
|
|
|
if (!nameLen && equals( ent, -1, "INBOX", 5 )) {
|
2013-12-08 09:48:14 +00:00
|
|
|
path[pathLen] = 0;
|
|
|
|
warn( "Maildir warning: ignoring INBOX in %s\n", path );
|
|
|
|
continue;
|
|
|
|
}
|
2012-08-11 16:34:46 +00:00
|
|
|
}
|
|
|
|
nl = nameLen + nfsnprintf( name + nameLen, _POSIX_PATH_MAX - nameLen, "%s", ent );
|
2014-10-04 15:07:17 +00:00
|
|
|
if (maildir_list_recurse( gctx, 1, flags, inbox, inboxLen, path, pl, name, nl ) < 0) {
|
2014-04-12 10:46:36 +00:00
|
|
|
closedir( dir );
|
2012-08-11 16:34:46 +00:00
|
|
|
return -1;
|
2014-04-12 10:46:36 +00:00
|
|
|
}
|
2011-06-02 17:21:19 +00:00
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
closedir (dir);
|
2012-08-11 16:34:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-04-01 10:20:03 +00:00
|
|
|
maildir_list_inbox( store_t *gctx, int *flags )
|
2012-08-11 16:34:46 +00:00
|
|
|
{
|
|
|
|
char path[_POSIX_PATH_MAX], name[_POSIX_PATH_MAX];
|
|
|
|
|
2013-04-01 10:20:03 +00:00
|
|
|
*flags &= ~LIST_INBOX;
|
|
|
|
return maildir_list_recurse(
|
2014-10-04 15:07:17 +00:00
|
|
|
gctx, 2, flags, 0, 0,
|
2013-04-01 10:20:03 +00:00
|
|
|
path, nfsnprintf( path, _POSIX_PATH_MAX, "%s", ((maildir_store_conf_t *)gctx->conf)->inbox ),
|
|
|
|
name, nfsnprintf( name, _POSIX_PATH_MAX, "INBOX" ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
maildir_list_path( store_t *gctx, int *flags )
|
|
|
|
{
|
2014-10-04 15:07:17 +00:00
|
|
|
const char *inbox = ((maildir_store_conf_t *)gctx->conf)->inbox;
|
2013-04-01 10:20:03 +00:00
|
|
|
char path[_POSIX_PATH_MAX], name[_POSIX_PATH_MAX];
|
|
|
|
|
2014-10-04 10:16:10 +00:00
|
|
|
if (maildir_validate_path( gctx->conf ) < 0)
|
|
|
|
return -1;
|
2013-04-01 10:20:03 +00:00
|
|
|
return maildir_list_recurse(
|
2014-10-04 15:07:17 +00:00
|
|
|
gctx, 0, flags, inbox, strlen( inbox ),
|
2013-04-01 10:20:03 +00:00
|
|
|
path, nfsnprintf( path, _POSIX_PATH_MAX, "%s", gctx->conf->path ),
|
|
|
|
name, 0 );
|
2012-08-11 16:34:46 +00:00
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
|
2012-08-11 16:34:46 +00:00
|
|
|
static void
|
2014-12-27 21:13:24 +00:00
|
|
|
maildir_list_store( store_t *gctx, int flags,
|
|
|
|
void (*cb)( int sts, void *aux ), void *aux )
|
2012-08-11 16:34:46 +00:00
|
|
|
{
|
2013-04-01 10:20:03 +00:00
|
|
|
if (((flags & LIST_PATH) && maildir_list_path( gctx, &flags ) < 0) ||
|
|
|
|
((flags & LIST_INBOX) && maildir_list_inbox( gctx, &flags ) < 0)) {
|
2012-08-11 16:34:46 +00:00
|
|
|
maildir_invoke_bad_callback( gctx );
|
|
|
|
cb( DRV_CANCELED, aux );
|
|
|
|
} else {
|
|
|
|
cb( DRV_OK, aux );
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *subdirs[] = { "cur", "new", "tmp" };
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
char *base;
|
|
|
|
int size;
|
2014-12-07 12:19:30 +00:00
|
|
|
uint uid:31, recent:1;
|
2006-02-03 21:33:43 +00:00
|
|
|
char tuid[TUIDL];
|
2004-03-27 16:07:20 +00:00
|
|
|
} msg_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
msg_t *ents;
|
|
|
|
int nents, nalloc;
|
|
|
|
} msglist_t;
|
|
|
|
|
|
|
|
static void
|
|
|
|
maildir_free_scan( msglist_t *msglist )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (msglist->ents) {
|
|
|
|
for (i = 0; i < msglist->nents; i++)
|
2010-11-14 15:44:50 +00:00
|
|
|
free( msglist->ents[i].base );
|
2004-03-27 16:07:20 +00:00
|
|
|
free( msglist->ents );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define _24_HOURS (3600 * 24)
|
|
|
|
|
|
|
|
static int
|
2014-12-29 01:00:18 +00:00
|
|
|
maildir_clear_tmp( char *buf, int bufsz, int bl )
|
2004-03-27 16:07:20 +00:00
|
|
|
{
|
|
|
|
DIR *dirp;
|
|
|
|
struct dirent *entry;
|
|
|
|
time_t now;
|
2014-12-29 01:00:18 +00:00
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
memcpy( buf + bl, "tmp/", 5 );
|
|
|
|
bl += 4;
|
|
|
|
if (!(dirp = opendir( buf ))) {
|
|
|
|
sys_error( "Maildir error: cannot list %s", buf );
|
|
|
|
return DRV_BOX_BAD;
|
|
|
|
}
|
|
|
|
time( &now );
|
|
|
|
while ((entry = readdir( dirp ))) {
|
|
|
|
nfsnprintf( buf + bl, bufsz - bl, "%s", entry->d_name );
|
|
|
|
if (stat( buf, &st )) {
|
|
|
|
if (errno != ENOENT)
|
|
|
|
sys_error( "Maildir error: cannot access %s", buf );
|
|
|
|
} else if (S_ISREG(st.st_mode) && now - st.st_ctime >= _24_HOURS) {
|
|
|
|
/* This should happen infrequently enough that it won't be
|
|
|
|
* bothersome to the user to display when it occurs.
|
|
|
|
*/
|
2015-03-23 07:42:51 +00:00
|
|
|
notice( "Maildir notice: removing stale file %s\n", buf );
|
2014-12-29 01:00:18 +00:00
|
|
|
if (unlink( buf ) && errno != ENOENT)
|
|
|
|
sys_error( "Maildir error: cannot remove %s", buf );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir( dirp );
|
|
|
|
return DRV_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-01-17 14:27:08 +00:00
|
|
|
make_box_dir( char *buf, int bl )
|
2014-12-29 01:00:18 +00:00
|
|
|
{
|
|
|
|
char *p;
|
2015-01-17 14:27:08 +00:00
|
|
|
|
|
|
|
if (!mkdir( buf, 0700 ) || errno == EEXIST)
|
|
|
|
return 0;
|
|
|
|
p = memrchr( buf, '/', bl - 1 );
|
|
|
|
if (*(p + 1) != '.') {
|
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*p = 0;
|
|
|
|
if (make_box_dir( buf, (int)(p - buf) ))
|
|
|
|
return -1;
|
|
|
|
*p = '/';
|
|
|
|
return mkdir( buf, 0700 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
maildir_validate( const char *box, int create, maildir_store_t *ctx )
|
|
|
|
{
|
2012-08-11 16:34:46 +00:00
|
|
|
int i, bl, ret;
|
2004-03-27 16:07:20 +00:00
|
|
|
struct stat st;
|
|
|
|
char buf[_POSIX_PATH_MAX];
|
|
|
|
|
2012-08-11 15:11:57 +00:00
|
|
|
bl = nfsnprintf( buf, sizeof(buf) - 4, "%s/", box );
|
2004-03-27 16:07:20 +00:00
|
|
|
if (stat( buf, &st )) {
|
2014-12-29 01:01:23 +00:00
|
|
|
if (errno != ENOENT) {
|
|
|
|
sys_error( "Maildir error: cannot access mailbox '%s'", box );
|
2004-03-27 16:07:20 +00:00
|
|
|
return DRV_BOX_BAD;
|
|
|
|
}
|
2014-12-29 01:01:23 +00:00
|
|
|
if (!create)
|
|
|
|
return DRV_BOX_BAD;
|
2015-01-17 14:27:08 +00:00
|
|
|
if (make_box_dir( buf, bl )) {
|
2014-12-29 01:01:23 +00:00
|
|
|
sys_error( "Maildir error: cannot create mailbox '%s'", box );
|
2015-01-03 23:39:06 +00:00
|
|
|
ctx->gen.conf->failed = FAIL_FINAL;
|
2014-12-29 01:01:23 +00:00
|
|
|
maildir_invoke_bad_callback( &ctx->gen );
|
|
|
|
return DRV_CANCELED;
|
|
|
|
}
|
|
|
|
} else if (!S_ISDIR(st.st_mode)) {
|
|
|
|
notdir:
|
|
|
|
error( "Maildir error: '%s' is no valid mailbox\n", box );
|
|
|
|
return DRV_BOX_BAD;
|
|
|
|
}
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
memcpy( buf + bl, subdirs[i], 4 );
|
|
|
|
if (stat( buf, &st )) {
|
|
|
|
/* We always create new/ and tmp/ if they are missing. cur/ is the presence indicator. */
|
|
|
|
if (!i && !create)
|
|
|
|
return DRV_BOX_BAD;
|
|
|
|
if (mkdir( buf, 0700 )) {
|
|
|
|
sys_error( "Maildir error: cannot create directory %s", buf );
|
2011-07-24 18:26:10 +00:00
|
|
|
return DRV_BOX_BAD;
|
|
|
|
}
|
2014-12-29 01:01:23 +00:00
|
|
|
ctx->fresh[i] = 1;
|
|
|
|
} else if (!S_ISDIR(st.st_mode)) {
|
|
|
|
goto notdir;
|
|
|
|
} else {
|
|
|
|
if (i == 2) {
|
|
|
|
if ((ret = maildir_clear_tmp( buf, sizeof(buf), bl )) != DRV_OK)
|
|
|
|
return ret;
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return DRV_OK;
|
|
|
|
}
|
|
|
|
|
2004-05-23 16:39:24 +00:00
|
|
|
#ifdef USE_DB
|
|
|
|
static void
|
2014-10-25 15:30:57 +00:00
|
|
|
make_key( const char *info_stop, DBT *tkey, char *name )
|
2004-05-23 16:39:24 +00:00
|
|
|
{
|
2014-10-25 15:30:57 +00:00
|
|
|
char *u = strpbrk( name, info_stop );
|
2004-05-23 16:39:24 +00:00
|
|
|
tkey->data = name;
|
|
|
|
tkey->size = u ? (size_t)(u - name) : strlen( name );
|
|
|
|
}
|
|
|
|
#endif /* USE_DB */
|
|
|
|
|
|
|
|
static int
|
2015-01-01 17:00:41 +00:00
|
|
|
maildir_store_uidval( maildir_store_t *ctx )
|
2004-05-23 16:39:24 +00:00
|
|
|
{
|
|
|
|
int n;
|
2015-01-01 18:18:27 +00:00
|
|
|
#ifdef USE_DB
|
|
|
|
int ret, uv[2];
|
|
|
|
#endif
|
2004-05-23 16:39:24 +00:00
|
|
|
char buf[128];
|
|
|
|
|
2015-01-01 18:18:27 +00:00
|
|
|
#ifdef USE_DB
|
|
|
|
if (ctx->db) {
|
|
|
|
key.data = (void *)"UIDVALIDITY";
|
|
|
|
key.size = 11;
|
|
|
|
uv[0] = ctx->gen.uidvalidity;
|
|
|
|
uv[1] = ctx->nuid;
|
|
|
|
value.data = uv;
|
|
|
|
value.size = sizeof(uv);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if ((ret = ctx->db->sync( ctx->db, 0 ))) {
|
|
|
|
ctx->db->err( ctx->db, ret, "Maildir error: db->sync()" );
|
|
|
|
return DRV_BOX_BAD;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif /* USE_DB */
|
|
|
|
{
|
|
|
|
n = sprintf( buf, "%d\n%d\n", ctx->gen.uidvalidity, ctx->nuid );
|
|
|
|
lseek( ctx->uvfd, 0, SEEK_SET );
|
|
|
|
if (write( ctx->uvfd, buf, n ) != n || ftruncate( ctx->uvfd, n ) || (UseFSync && fdatasync( ctx->uvfd ))) {
|
|
|
|
error( "Maildir error: cannot write UIDVALIDITY.\n" );
|
|
|
|
return DRV_BOX_BAD;
|
|
|
|
}
|
2004-05-23 16:39:24 +00:00
|
|
|
}
|
2015-01-01 12:24:30 +00:00
|
|
|
conf_wakeup( &ctx->lcktmr, 2 );
|
2004-05-23 16:39:24 +00:00
|
|
|
return DRV_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-01-01 17:00:41 +00:00
|
|
|
maildir_init_uidval( maildir_store_t *ctx )
|
2004-05-23 16:39:24 +00:00
|
|
|
{
|
|
|
|
ctx->gen.uidvalidity = time( 0 );
|
|
|
|
ctx->nuid = 0;
|
|
|
|
ctx->uvok = 0;
|
|
|
|
#ifdef USE_DB
|
|
|
|
if (ctx->db) {
|
2006-10-24 17:37:57 +00:00
|
|
|
u_int32_t count;
|
|
|
|
ctx->db->truncate( ctx->db, 0, &count, 0 );
|
2004-05-23 16:39:24 +00:00
|
|
|
}
|
|
|
|
#endif /* USE_DB */
|
2015-01-01 17:00:41 +00:00
|
|
|
return maildir_store_uidval( ctx );
|
2004-05-23 16:39:24 +00:00
|
|
|
}
|
|
|
|
|
2011-04-10 13:32:25 +00:00
|
|
|
static int
|
2015-01-01 17:00:41 +00:00
|
|
|
maildir_init_uidval_new( maildir_store_t *ctx )
|
2011-04-10 13:32:25 +00:00
|
|
|
{
|
2015-03-23 07:42:51 +00:00
|
|
|
notice( "Maildir notice: no UIDVALIDITY, creating new.\n" );
|
2015-01-01 17:00:41 +00:00
|
|
|
return maildir_init_uidval( ctx );
|
2011-04-10 13:32:25 +00:00
|
|
|
}
|
|
|
|
|
2004-03-27 16:07:20 +00:00
|
|
|
static int
|
|
|
|
maildir_uidval_lock( maildir_store_t *ctx )
|
|
|
|
{
|
|
|
|
int n;
|
2015-01-01 18:25:17 +00:00
|
|
|
#ifdef USE_DB
|
|
|
|
int ret;
|
|
|
|
struct stat st;
|
|
|
|
#endif
|
2004-03-27 16:07:20 +00:00
|
|
|
char buf[128];
|
|
|
|
|
2015-02-15 10:38:26 +00:00
|
|
|
if (pending_wakeup( &ctx->lcktmr )) {
|
2015-01-01 12:24:30 +00:00
|
|
|
/* The unlock timer is active, so we are obviously already locked. */
|
|
|
|
return DRV_OK;
|
|
|
|
}
|
2004-09-20 11:31:55 +00:00
|
|
|
#ifdef LEGACY_FLOCK
|
2004-03-27 16:07:20 +00:00
|
|
|
/* This is legacy only */
|
|
|
|
if (flock( ctx->uvfd, LOCK_EX ) < 0) {
|
2006-03-19 11:29:12 +00:00
|
|
|
error( "Maildir error: cannot flock UIDVALIDITY.\n" );
|
2004-03-27 16:07:20 +00:00
|
|
|
return DRV_BOX_BAD;
|
|
|
|
}
|
2004-09-20 11:31:55 +00:00
|
|
|
#endif
|
2004-03-27 16:07:20 +00:00
|
|
|
/* This (theoretically) works over NFS. Let's hope nobody else did
|
|
|
|
the same in the opposite order, as we'd deadlock then. */
|
|
|
|
#if SEEK_SET != 0
|
|
|
|
lck.l_whence = SEEK_SET;
|
|
|
|
#endif
|
|
|
|
lck.l_type = F_WRLCK;
|
|
|
|
if (fcntl( ctx->uvfd, F_SETLKW, &lck )) {
|
2006-03-19 11:29:12 +00:00
|
|
|
error( "Maildir error: cannot fcntl lock UIDVALIDITY.\n" );
|
2004-03-27 16:07:20 +00:00
|
|
|
return DRV_BOX_BAD;
|
|
|
|
}
|
2015-01-01 18:25:17 +00:00
|
|
|
|
|
|
|
#ifdef USE_DB
|
|
|
|
if (ctx->usedb) {
|
|
|
|
if (fstat( ctx->uvfd, &st )) {
|
|
|
|
sys_error( "Maildir error: cannot fstat UID database" );
|
2011-05-03 07:42:27 +00:00
|
|
|
return DRV_BOX_BAD;
|
|
|
|
}
|
2015-01-01 18:25:17 +00:00
|
|
|
if (db_create( &ctx->db, 0, 0 )) {
|
|
|
|
fputs( "Maildir error: db_create() failed\n", stderr );
|
|
|
|
return DRV_BOX_BAD;
|
|
|
|
}
|
|
|
|
if ((ret = (ctx->db->open)( ctx->db, 0, ctx->usedb, 0, DB_HASH,
|
|
|
|
st.st_size ? 0 : DB_CREATE | DB_TRUNCATE, 0 ))) {
|
|
|
|
ctx->db->err( ctx->db, ret, "Maildir error: db->open(%s)", ctx->usedb );
|
|
|
|
return DRV_BOX_BAD;
|
|
|
|
}
|
|
|
|
key.data = (void *)"UIDVALIDITY";
|
|
|
|
key.size = 11;
|
|
|
|
if ((ret = ctx->db->get( ctx->db, 0, &key, &value, 0 ))) {
|
|
|
|
if (ret != DB_NOTFOUND) {
|
|
|
|
ctx->db->err( ctx->db, ret, "Maildir error: db->get()" );
|
|
|
|
return DRV_BOX_BAD;
|
|
|
|
}
|
|
|
|
return maildir_init_uidval_new( ctx );
|
|
|
|
}
|
|
|
|
ctx->gen.uidvalidity = ((int *)value.data)[0];
|
|
|
|
ctx->nuid = ((int *)value.data)[1];
|
2004-03-27 16:07:20 +00:00
|
|
|
} else
|
2015-01-01 18:25:17 +00:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
lseek( ctx->uvfd, 0, SEEK_SET );
|
|
|
|
if ((n = read( ctx->uvfd, buf, sizeof(buf) - 1 )) <= 0 ||
|
|
|
|
(buf[n] = 0, sscanf( buf, "%d\n%d", &ctx->gen.uidvalidity, &ctx->nuid ) != 2)) {
|
|
|
|
#if 1
|
|
|
|
/* In a generic driver, resetting the UID validity would be the right thing.
|
|
|
|
* But this would mess up the sync state completely. So better bail out and
|
|
|
|
* give the user a chance to fix the mailbox. */
|
|
|
|
if (n) {
|
|
|
|
error( "Maildir error: cannot read UIDVALIDITY.\n" );
|
|
|
|
return DRV_BOX_BAD;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return maildir_init_uidval_new( ctx );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx->uvok = 1;
|
2015-01-01 12:24:30 +00:00
|
|
|
conf_wakeup( &ctx->lcktmr, 2 );
|
2004-03-27 16:07:20 +00:00
|
|
|
return DRV_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
maildir_uidval_unlock( maildir_store_t *ctx )
|
|
|
|
{
|
2015-01-01 18:25:17 +00:00
|
|
|
#ifdef USE_DB
|
|
|
|
if (ctx->db) {
|
|
|
|
ctx->db->close( ctx->db, 0 );
|
|
|
|
ctx->db = 0;
|
|
|
|
}
|
|
|
|
#endif /* USE_DB */
|
2004-03-27 16:07:20 +00:00
|
|
|
lck.l_type = F_UNLCK;
|
|
|
|
fcntl( ctx->uvfd, F_SETLK, &lck );
|
2004-09-20 11:31:55 +00:00
|
|
|
#ifdef LEGACY_FLOCK
|
2004-03-27 16:07:20 +00:00
|
|
|
/* This is legacy only */
|
|
|
|
flock( ctx->uvfd, LOCK_UN );
|
2004-09-20 11:31:55 +00:00
|
|
|
#endif
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
|
2015-01-01 12:24:30 +00:00
|
|
|
static void
|
|
|
|
lcktmr_timeout( void *aux )
|
|
|
|
{
|
|
|
|
maildir_uidval_unlock( (maildir_store_t *)aux );
|
|
|
|
}
|
|
|
|
|
2004-03-27 16:07:20 +00:00
|
|
|
static int
|
|
|
|
maildir_obtain_uid( maildir_store_t *ctx, int *uid )
|
|
|
|
{
|
2015-01-01 15:49:23 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ret = maildir_uidval_lock( ctx )) != DRV_OK)
|
|
|
|
return ret;
|
2004-03-27 16:07:20 +00:00
|
|
|
*uid = ++ctx->nuid;
|
2015-01-01 17:00:41 +00:00
|
|
|
return maildir_store_uidval( ctx );
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
|
2015-01-01 18:18:27 +00:00
|
|
|
#ifdef USE_DB
|
|
|
|
static int
|
|
|
|
maildir_set_uid( maildir_store_t *ctx, const char *name, int *uid )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2015-01-01 18:25:17 +00:00
|
|
|
if ((ret = maildir_uidval_lock( ctx )) != DRV_OK)
|
|
|
|
return ret;
|
2015-01-01 18:18:27 +00:00
|
|
|
*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
|
|
|
|
|
2004-03-27 16:07:20 +00:00
|
|
|
static int
|
|
|
|
maildir_compare( const void *l, const void *r )
|
|
|
|
{
|
|
|
|
msg_t *lm = (msg_t *)l, *rm = (msg_t *)r;
|
|
|
|
char *ldot, *rdot, *ldot2, *rdot2, *lseq, *rseq;
|
|
|
|
int ret, llen, rlen;
|
|
|
|
|
|
|
|
if ((ret = lm->uid - rm->uid))
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* No UID, so sort by arrival date. We should not do this, but we rely
|
|
|
|
on the suggested unique file name scheme - we have no choice. */
|
|
|
|
/* The first field are always the seconds. Alphabetical sort should be
|
|
|
|
faster than numeric. */
|
|
|
|
if (!(ldot = strchr( lm->base, '.' )) || !(rdot = strchr( rm->base, '.' )))
|
|
|
|
goto stronly; /* Should never happen ... */
|
|
|
|
llen = ldot - lm->base, rlen = rdot - rm->base;
|
|
|
|
/* The shorter number is smaller. Really. This won't trigger with any
|
|
|
|
mail created after Sep 9 2001 anyway. */
|
|
|
|
if ((ret = llen - rlen))
|
|
|
|
return ret;
|
|
|
|
if ((ret = memcmp( lm->base, rm->base, llen )))
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ldot++, rdot++;
|
|
|
|
|
|
|
|
if ((llen = strtol( ldot, &ldot2, 10 ))) {
|
|
|
|
if (!(rlen = strtol( rdot, &rdot2, 10 )))
|
|
|
|
goto stronly; /* Comparing apples to oranges ... */
|
|
|
|
/* Classical PID specs */
|
|
|
|
if ((ret = llen - rlen)) {
|
|
|
|
retpid:
|
|
|
|
/* Handle PID wraparound. This works only on systems
|
|
|
|
where PIDs are not reused too fast */
|
|
|
|
if (ret > 20000 || ret < -20000)
|
|
|
|
ret = -ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return (*ldot2 != '_' ? 0 : atoi( ldot2 + 1 )) -
|
|
|
|
(*rdot2 != '_' ? 0 : atoi( rdot2 + 1 ));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(ldot2 = strchr( ldot, '.' )) || !(rdot2 = strchr( rdot, '.' )))
|
|
|
|
goto stronly; /* Should never happen ... */
|
|
|
|
llen = ldot2 - ldot, rlen = rdot2 - rdot;
|
|
|
|
|
|
|
|
if (((lseq = memchr( ldot, '#', llen )) && (rseq = memchr( rdot, '#', rlen ))) ||
|
|
|
|
((lseq = memchr( ldot, 'M', llen )) && (rseq = memchr( rdot, 'M', rlen ))))
|
|
|
|
return atoi( lseq + 1 ) - atoi( rseq + 1 );
|
|
|
|
|
|
|
|
if ((lseq = memchr( ldot, 'P', llen )) && (rseq = memchr( rdot, 'P', rlen ))) {
|
|
|
|
if ((ret = atoi( lseq + 1 ) - atoi( rseq + 1 )))
|
|
|
|
goto retpid;
|
|
|
|
if ((lseq = memchr( ldot, 'Q', llen )) && (rseq = memchr( rdot, 'Q', rlen )))
|
|
|
|
return atoi( lseq + 1 ) - atoi( rseq + 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
stronly:
|
|
|
|
/* Fall-back, so the sort order is defined at all */
|
|
|
|
return strcmp( lm->base, rm->base );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
maildir_scan( maildir_store_t *ctx, msglist_t *msglist )
|
|
|
|
{
|
2014-10-25 15:30:57 +00:00
|
|
|
maildir_store_conf_t *conf = (maildir_store_conf_t *)ctx->gen.conf;
|
2004-03-27 16:07:20 +00:00
|
|
|
DIR *d;
|
2006-02-03 21:33:43 +00:00
|
|
|
FILE *f;
|
2004-03-27 16:07:20 +00:00
|
|
|
struct dirent *e;
|
|
|
|
const char *u, *ru;
|
|
|
|
#ifdef USE_DB
|
|
|
|
DB *tdb;
|
|
|
|
DBC *dbc;
|
|
|
|
#endif /* USE_DB */
|
|
|
|
msg_t *entry;
|
2006-10-24 17:37:57 +00:00
|
|
|
int i, j, uid, bl, fnl, ret;
|
2012-08-25 13:29:16 +00:00
|
|
|
time_t now, stamps[2];
|
2004-03-27 16:07:20 +00:00
|
|
|
struct stat st;
|
|
|
|
char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX];
|
|
|
|
|
|
|
|
again:
|
|
|
|
msglist->ents = 0;
|
|
|
|
msglist->nents = msglist->nalloc = 0;
|
|
|
|
ctx->gen.count = ctx->gen.recent = 0;
|
|
|
|
if (ctx->uvok || ctx->maxuid == INT_MAX) {
|
|
|
|
#ifdef USE_DB
|
2015-01-01 18:25:17 +00:00
|
|
|
if (ctx->usedb) {
|
2004-03-27 16:07:20 +00:00
|
|
|
if (db_create( &tdb, 0, 0 )) {
|
|
|
|
fputs( "Maildir error: db_create() failed\n", stderr );
|
|
|
|
return DRV_BOX_BAD;
|
|
|
|
}
|
2008-02-23 09:01:51 +00:00
|
|
|
if ((tdb->open)( tdb, 0, 0, 0, DB_HASH, DB_CREATE, 0 )) {
|
2004-03-27 16:07:20 +00:00
|
|
|
fputs( "Maildir error: tdb->open() failed\n", stderr );
|
2011-05-22 15:53:28 +00:00
|
|
|
bork:
|
2004-03-27 16:07:20 +00:00
|
|
|
tdb->close( tdb, 0 );
|
|
|
|
return DRV_BOX_BAD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* USE_DB */
|
|
|
|
bl = nfsnprintf( buf, sizeof(buf) - 4, "%s/", ctx->gen.path );
|
2012-08-25 13:29:16 +00:00
|
|
|
restat:
|
|
|
|
now = time( 0 );
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
memcpy( buf + bl, subdirs[i], 4 );
|
|
|
|
if (stat( buf, &st )) {
|
|
|
|
sys_error( "Maildir error: cannot stat %s", buf );
|
|
|
|
goto dfail;
|
|
|
|
}
|
2014-12-29 01:01:23 +00:00
|
|
|
if (st.st_mtime == now && !(DFlags & ZERODELAY) && !ctx->fresh[i]) {
|
2012-08-25 13:29:16 +00:00
|
|
|
/* If the modification happened during this second, we wouldn't be able to
|
|
|
|
* tell if there were further modifications during this second. So wait.
|
|
|
|
* This has the nice side effect that we wait for "batches" of changes to
|
|
|
|
* complete. On the downside, it can potentially block indefinitely. */
|
2015-03-23 07:42:51 +00:00
|
|
|
notice( "Maildir notice: sleeping due to recent directory modification.\n" );
|
2012-08-25 13:29:16 +00:00
|
|
|
sleep( 1 ); /* FIXME: should make this async */
|
|
|
|
goto restat;
|
|
|
|
}
|
|
|
|
stamps[i] = st.st_mtime;
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
memcpy( buf + bl, subdirs[i], 4 );
|
|
|
|
if (!(d = opendir( buf ))) {
|
2011-04-10 13:32:25 +00:00
|
|
|
sys_error( "Maildir error: cannot list %s", buf );
|
2012-08-25 13:29:16 +00:00
|
|
|
rfail:
|
2004-03-27 16:07:20 +00:00
|
|
|
maildir_free_scan( msglist );
|
2012-08-25 13:29:16 +00:00
|
|
|
dfail:
|
2004-03-27 16:07:20 +00:00
|
|
|
#ifdef USE_DB
|
2015-01-01 18:25:17 +00:00
|
|
|
if (ctx->usedb)
|
2004-03-27 16:07:20 +00:00
|
|
|
tdb->close( tdb, 0 );
|
|
|
|
#endif /* USE_DB */
|
|
|
|
return DRV_BOX_BAD;
|
|
|
|
}
|
|
|
|
while ((e = readdir( d ))) {
|
|
|
|
if (*e->d_name == '.')
|
|
|
|
continue;
|
|
|
|
ctx->gen.count++;
|
|
|
|
ctx->gen.recent += i;
|
|
|
|
#ifdef USE_DB
|
2015-01-01 18:25:17 +00:00
|
|
|
if (ctx->usedb) {
|
|
|
|
if (maildir_uidval_lock( ctx ) != DRV_OK)
|
|
|
|
goto mbork;
|
2014-10-25 15:30:57 +00:00
|
|
|
make_key( conf->info_stop, &key, e->d_name );
|
2004-03-27 16:07:20 +00:00
|
|
|
if ((ret = ctx->db->get( ctx->db, 0, &key, &value, 0 ))) {
|
|
|
|
if (ret != DB_NOTFOUND) {
|
|
|
|
ctx->db->err( ctx->db, ret, "Maildir error: db->get()" );
|
2011-05-22 15:53:28 +00:00
|
|
|
mbork:
|
|
|
|
maildir_free_scan( msglist );
|
2014-04-12 10:46:36 +00:00
|
|
|
closedir( d );
|
2004-03-27 16:07:20 +00:00
|
|
|
goto bork;
|
|
|
|
}
|
|
|
|
uid = INT_MAX;
|
|
|
|
} else {
|
|
|
|
value.size = 0;
|
|
|
|
if ((ret = tdb->put( tdb, 0, &key, &value, 0 ))) {
|
|
|
|
tdb->err( tdb, ret, "Maildir error: tdb->put()" );
|
2011-05-22 15:53:28 +00:00
|
|
|
goto mbork;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
uid = *(int *)value.data;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif /* USE_DB */
|
|
|
|
{
|
|
|
|
uid = (ctx->uvok && (u = strstr( e->d_name, ",U=" ))) ? atoi( u + 3 ) : 0;
|
|
|
|
if (!uid)
|
|
|
|
uid = INT_MAX;
|
|
|
|
}
|
|
|
|
if (uid <= ctx->maxuid) {
|
|
|
|
if (uid < ctx->minuid) {
|
|
|
|
for (j = 0; j < ctx->nexcs; j++)
|
|
|
|
if (ctx->excs[j] == uid)
|
|
|
|
goto oke;
|
|
|
|
continue;
|
|
|
|
oke: ;
|
|
|
|
}
|
|
|
|
if (msglist->nalloc == msglist->nents) {
|
|
|
|
msglist->nalloc = msglist->nalloc * 2 + 100;
|
|
|
|
msglist->ents = nfrealloc( msglist->ents, msglist->nalloc * sizeof(msg_t) );
|
|
|
|
}
|
|
|
|
entry = &msglist->ents[msglist->nents++];
|
|
|
|
entry->base = nfstrdup( e->d_name );
|
|
|
|
entry->uid = uid;
|
|
|
|
entry->recent = i;
|
|
|
|
entry->size = 0;
|
2006-02-03 21:33:43 +00:00
|
|
|
entry->tuid[0] = 0;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir( d );
|
|
|
|
}
|
2012-08-25 13:29:16 +00:00
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
memcpy( buf + bl, subdirs[i], 4 );
|
|
|
|
if (stat( buf, &st )) {
|
|
|
|
sys_error( "Maildir error: cannot re-stat %s", buf );
|
|
|
|
goto rfail;
|
|
|
|
}
|
|
|
|
if (st.st_mtime != stamps[i]) {
|
|
|
|
/* Somebody messed with the mailbox since we started listing it. */
|
|
|
|
#ifdef USE_DB
|
2015-01-01 18:25:17 +00:00
|
|
|
if (ctx->usedb)
|
2012-08-25 13:29:16 +00:00
|
|
|
tdb->close( tdb, 0 );
|
|
|
|
#endif /* USE_DB */
|
|
|
|
maildir_free_scan( msglist );
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
#ifdef USE_DB
|
2015-01-01 18:25:17 +00:00
|
|
|
if (ctx->usedb) {
|
|
|
|
if (maildir_uidval_lock( ctx ) != DRV_OK)
|
|
|
|
;
|
|
|
|
else if ((ret = ctx->db->cursor( ctx->db, 0, &dbc, 0 )))
|
2004-03-27 16:07:20 +00:00
|
|
|
ctx->db->err( ctx->db, ret, "Maildir error: db->cursor()" );
|
|
|
|
else {
|
|
|
|
for (;;) {
|
|
|
|
if ((ret = dbc->c_get( dbc, &key, &value, DB_NEXT ))) {
|
|
|
|
if (ret != DB_NOTFOUND)
|
|
|
|
ctx->db->err( ctx->db, ret, "Maildir error: db->c_get()" );
|
|
|
|
break;
|
|
|
|
}
|
2014-10-04 15:07:17 +00:00
|
|
|
if (!equals( key.data, key.size, "UIDVALIDITY", 11 ) &&
|
2004-03-27 16:07:20 +00:00
|
|
|
(ret = tdb->get( tdb, 0, &key, &value, 0 ))) {
|
|
|
|
if (ret != DB_NOTFOUND) {
|
|
|
|
tdb->err( tdb, ret, "Maildir error: tdb->get()" );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((ret = dbc->c_del( dbc, 0 ))) {
|
|
|
|
ctx->db->err( ctx->db, ret, "Maildir error: db->c_del()" );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dbc->c_close( dbc );
|
|
|
|
}
|
|
|
|
tdb->close( tdb, 0 );
|
|
|
|
}
|
|
|
|
#endif /* USE_DB */
|
|
|
|
qsort( msglist->ents, msglist->nents, sizeof(msg_t), maildir_compare );
|
|
|
|
for (uid = i = 0; i < msglist->nents; i++) {
|
|
|
|
entry = &msglist->ents[i];
|
|
|
|
if (entry->uid != INT_MAX) {
|
|
|
|
if (uid == entry->uid) {
|
2011-05-03 07:42:27 +00:00
|
|
|
#if 1
|
|
|
|
/* See comment in maildir_uidval_lock() why this is fatal. */
|
|
|
|
error( "Maildir error: duplicate UID %d.\n", uid );
|
|
|
|
maildir_free_scan( msglist );
|
|
|
|
return DRV_BOX_BAD;
|
|
|
|
#else
|
2015-03-23 07:42:51 +00:00
|
|
|
notice( "Maildir notice: duplicate UID; changing UIDVALIDITY.\n");
|
2011-04-10 13:32:25 +00:00
|
|
|
if ((ret = maildir_init_uid( ctx )) != DRV_OK) {
|
2004-05-23 16:39:24 +00:00
|
|
|
maildir_free_scan( msglist );
|
|
|
|
return ret;
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
maildir_free_scan( msglist );
|
|
|
|
goto again;
|
2011-05-03 07:42:27 +00:00
|
|
|
#endif
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
uid = entry->uid;
|
2014-06-28 09:58:26 +00:00
|
|
|
if (uid > ctx->nuid) {
|
|
|
|
/* In principle, we could just warn and top up nuid. However, getting into this
|
|
|
|
* situation might indicate some serious trouble, so let's not make it worse. */
|
|
|
|
error( "Maildir error: UID %d is beyond highest assigned UID %d.\n", uid, ctx->nuid );
|
|
|
|
maildir_free_scan( msglist );
|
|
|
|
return DRV_BOX_BAD;
|
|
|
|
}
|
2011-04-10 11:06:07 +00:00
|
|
|
if ((ctx->gen.opts & OPEN_SIZE) || ((ctx->gen.opts & OPEN_FIND) && uid >= ctx->newuid))
|
2004-03-27 16:07:20 +00:00
|
|
|
nfsnprintf( buf + bl, sizeof(buf) - bl, "%s/%s", subdirs[entry->recent], entry->base );
|
|
|
|
#ifdef USE_DB
|
2015-01-01 18:25:17 +00:00
|
|
|
} else if (ctx->usedb) {
|
2004-03-27 16:07:20 +00:00
|
|
|
if ((ret = maildir_set_uid( ctx, entry->base, &uid )) != DRV_OK) {
|
|
|
|
maildir_free_scan( msglist );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
entry->uid = uid;
|
2011-04-10 11:06:07 +00:00
|
|
|
if ((ctx->gen.opts & OPEN_SIZE) || ((ctx->gen.opts & OPEN_FIND) && uid >= ctx->newuid))
|
2004-03-27 16:07:20 +00:00
|
|
|
nfsnprintf( buf + bl, sizeof(buf) - bl, "%s/%s", subdirs[entry->recent], entry->base );
|
|
|
|
#endif /* USE_DB */
|
|
|
|
} else {
|
|
|
|
if ((ret = maildir_obtain_uid( ctx, &uid )) != DRV_OK) {
|
|
|
|
maildir_free_scan( msglist );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
entry->uid = uid;
|
|
|
|
if ((u = strstr( entry->base, ",U=" )))
|
2014-12-07 12:19:30 +00:00
|
|
|
for (ru = u + 3; isdigit( (uchar)*ru ); ru++);
|
2004-03-27 16:07:20 +00:00
|
|
|
else
|
2014-10-25 15:30:57 +00:00
|
|
|
u = ru = strchr( entry->base, conf->info_delimiter );
|
2006-07-31 05:30:46 +00:00
|
|
|
fnl = (u ?
|
2012-07-07 15:19:17 +00:00
|
|
|
nfsnprintf( buf + bl, sizeof(buf) - bl, "%s/%.*s,U=%d%s", subdirs[entry->recent], (int)(u - entry->base), entry->base, uid, ru ) :
|
2006-07-31 05:30:46 +00:00
|
|
|
nfsnprintf( buf + bl, sizeof(buf) - bl, "%s/%s,U=%d", subdirs[entry->recent], entry->base, uid ))
|
|
|
|
+ 1 - 4;
|
2004-03-27 16:07:20 +00:00
|
|
|
memcpy( nbuf, buf, bl + 4 );
|
|
|
|
nfsnprintf( nbuf + bl + 4, sizeof(nbuf) - bl - 4, "%s", entry->base );
|
|
|
|
if (rename( nbuf, buf )) {
|
|
|
|
if (errno != ENOENT) {
|
2011-04-10 13:32:25 +00:00
|
|
|
sys_error( "Maildir error: cannot rename %s to %s", nbuf, buf );
|
|
|
|
fail:
|
2004-03-27 16:07:20 +00:00
|
|
|
maildir_free_scan( msglist );
|
|
|
|
return DRV_BOX_BAD;
|
|
|
|
}
|
2011-04-10 13:32:25 +00:00
|
|
|
retry:
|
2004-03-27 16:07:20 +00:00
|
|
|
maildir_free_scan( msglist );
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
free( entry->base );
|
|
|
|
entry->base = nfmalloc( fnl );
|
|
|
|
memcpy( entry->base, buf + bl + 4, fnl );
|
|
|
|
}
|
|
|
|
if (ctx->gen.opts & OPEN_SIZE) {
|
2011-04-10 13:32:25 +00:00
|
|
|
if (stat( buf, &st )) {
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
sys_error( "Maildir error: cannot stat %s", buf );
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
goto retry;
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
entry->size = st.st_size;
|
|
|
|
}
|
2011-04-10 11:06:07 +00:00
|
|
|
if ((ctx->gen.opts & OPEN_FIND) && uid >= ctx->newuid) {
|
2011-04-10 13:32:25 +00:00
|
|
|
if (!(f = fopen( buf, "r" ))) {
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
sys_error( "Maildir error: cannot open %s", buf );
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
goto retry;
|
|
|
|
}
|
2006-02-03 21:33:43 +00:00
|
|
|
while (fgets( nbuf, sizeof(nbuf), f )) {
|
|
|
|
if (!nbuf[0] || nbuf[0] == '\n')
|
|
|
|
break;
|
2014-10-04 15:07:17 +00:00
|
|
|
if (starts_with( nbuf, -1, "X-TUID: ", 8 ) && nbuf[8 + TUIDL] == '\n') {
|
2006-02-03 21:33:43 +00:00
|
|
|
memcpy( entry->tuid, nbuf + 8, TUIDL );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose( f );
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
2004-05-23 16:39:24 +00:00
|
|
|
ctx->uvok = 1;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
return DRV_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
maildir_init_msg( maildir_store_t *ctx, maildir_message_t *msg, msg_t *entry )
|
|
|
|
{
|
|
|
|
msg->base = entry->base;
|
|
|
|
entry->base = 0; /* prevent deletion */
|
|
|
|
msg->gen.size = entry->size;
|
2011-04-10 11:06:07 +00:00
|
|
|
msg->gen.srec = 0;
|
|
|
|
strncpy( msg->gen.tuid, entry->tuid, TUIDL );
|
2004-03-27 16:07:20 +00:00
|
|
|
if (entry->recent)
|
|
|
|
msg->gen.status |= M_RECENT;
|
|
|
|
if (ctx->gen.opts & OPEN_FLAGS) {
|
|
|
|
msg->gen.status |= M_FLAGS;
|
2014-10-25 15:30:57 +00:00
|
|
|
msg->gen.flags = maildir_parse_flags( ((maildir_store_conf_t *)ctx->gen.conf)->info_prefix, msg->base );
|
2004-03-27 16:07:20 +00:00
|
|
|
} else
|
|
|
|
msg->gen.flags = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
maildir_app_msg( maildir_store_t *ctx, message_t ***msgapp, msg_t *entry )
|
|
|
|
{
|
|
|
|
maildir_message_t *msg = nfmalloc( sizeof(*msg) );
|
|
|
|
msg->gen.next = **msgapp;
|
|
|
|
**msgapp = &msg->gen;
|
|
|
|
*msgapp = &msg->gen.next;
|
|
|
|
msg->gen.uid = entry->uid;
|
|
|
|
msg->gen.status = 0;
|
|
|
|
maildir_init_msg( ctx, msg, entry );
|
|
|
|
}
|
|
|
|
|
2014-12-27 22:39:55 +00:00
|
|
|
static int
|
|
|
|
maildir_select_box( store_t *gctx, const char *name )
|
2004-03-27 16:07:20 +00:00
|
|
|
{
|
|
|
|
maildir_store_t *ctx = (maildir_store_t *)gctx;
|
|
|
|
|
|
|
|
maildir_cleanup( gctx );
|
|
|
|
gctx->msgs = 0;
|
2011-05-22 15:23:23 +00:00
|
|
|
ctx->excs = 0;
|
2004-03-27 16:07:20 +00:00
|
|
|
ctx->uvfd = -1;
|
|
|
|
#ifdef USE_DB
|
|
|
|
ctx->db = 0;
|
2015-01-01 18:25:17 +00:00
|
|
|
ctx->usedb = 0;
|
2004-03-27 16:07:20 +00:00
|
|
|
#endif /* USE_DB */
|
2014-12-29 01:01:23 +00:00
|
|
|
ctx->fresh[0] = ctx->fresh[1] = 0;
|
2014-10-04 16:26:10 +00:00
|
|
|
if (starts_with( name, -1, "INBOX", 5 ) && (!name[5] || name[5] == '/')) {
|
|
|
|
gctx->path = maildir_join_path( ((maildir_store_conf_t *)gctx->conf)->inbox, name + 5 );
|
2015-03-26 13:28:06 +00:00
|
|
|
ctx->is_inbox = !name[5];
|
2014-10-04 10:16:10 +00:00
|
|
|
} else {
|
|
|
|
if (maildir_validate_path( gctx->conf ) < 0) {
|
2014-12-27 22:39:55 +00:00
|
|
|
gctx->path = 0;
|
|
|
|
return DRV_CANCELED;
|
2014-10-04 10:16:10 +00:00
|
|
|
}
|
2014-10-04 16:26:10 +00:00
|
|
|
gctx->path = maildir_join_path( gctx->conf->path, name );
|
2015-03-26 13:28:06 +00:00
|
|
|
ctx->is_inbox = 0;
|
2014-10-04 10:16:10 +00:00
|
|
|
}
|
2014-12-27 22:39:55 +00:00
|
|
|
return DRV_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-12-29 00:42:17 +00:00
|
|
|
maildir_open_box( store_t *gctx,
|
2014-12-27 22:39:55 +00:00
|
|
|
void (*cb)( int sts, void *aux ), void *aux )
|
|
|
|
{
|
|
|
|
maildir_store_t *ctx = (maildir_store_t *)gctx;
|
|
|
|
int ret;
|
|
|
|
char uvpath[_POSIX_PATH_MAX];
|
2004-03-27 16:07:20 +00:00
|
|
|
|
2015-03-26 13:28:06 +00:00
|
|
|
if ((ret = maildir_validate( gctx->path, ctx->is_inbox, ctx )) != DRV_OK)
|
2015-01-01 12:24:30 +00:00
|
|
|
goto bail;
|
2004-03-27 16:07:20 +00:00
|
|
|
|
|
|
|
nfsnprintf( uvpath, sizeof(uvpath), "%s/.uidvalidity", gctx->path );
|
|
|
|
#ifndef USE_DB
|
|
|
|
if ((ctx->uvfd = open( uvpath, O_RDWR|O_CREAT, 0600 )) < 0) {
|
2011-04-10 13:32:25 +00:00
|
|
|
sys_error( "Maildir error: cannot write %s", uvpath );
|
2012-07-29 21:14:48 +00:00
|
|
|
cb( DRV_BOX_BAD, aux );
|
|
|
|
return;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
#else
|
2015-01-01 18:25:17 +00:00
|
|
|
ctx->usedb = 0;
|
2004-03-27 16:07:20 +00:00
|
|
|
if ((ctx->uvfd = open( uvpath, O_RDWR, 0600 )) < 0) {
|
|
|
|
nfsnprintf( uvpath, sizeof(uvpath), "%s/.isyncuidmap.db", gctx->path );
|
|
|
|
if ((ctx->uvfd = open( uvpath, O_RDWR, 0600 )) < 0) {
|
|
|
|
if (((maildir_store_conf_t *)gctx->conf)->alt_map) {
|
|
|
|
if ((ctx->uvfd = open( uvpath, O_RDWR|O_CREAT, 0600 )) >= 0)
|
|
|
|
goto dbok;
|
|
|
|
} else {
|
|
|
|
nfsnprintf( uvpath, sizeof(uvpath), "%s/.uidvalidity", gctx->path );
|
|
|
|
if ((ctx->uvfd = open( uvpath, O_RDWR|O_CREAT, 0600 )) >= 0)
|
|
|
|
goto fnok;
|
|
|
|
}
|
2011-04-10 13:32:25 +00:00
|
|
|
sys_error( "Maildir error: cannot write %s", uvpath );
|
2012-07-29 21:14:48 +00:00
|
|
|
cb( DRV_BOX_BAD, aux );
|
|
|
|
return;
|
2004-03-27 16:07:20 +00:00
|
|
|
} else {
|
2015-01-01 18:25:17 +00:00
|
|
|
dbok:
|
|
|
|
ctx->usedb = nfstrdup( uvpath );
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fnok:
|
|
|
|
#endif /* USE_DB */
|
2015-01-01 12:24:30 +00:00
|
|
|
ret = maildir_uidval_lock( ctx );
|
2011-07-23 14:06:32 +00:00
|
|
|
|
2015-01-01 12:24:30 +00:00
|
|
|
bail:
|
|
|
|
cb( ret, aux );
|
2011-07-23 14:06:32 +00:00
|
|
|
}
|
|
|
|
|
2014-12-29 00:42:17 +00:00
|
|
|
static void
|
|
|
|
maildir_create_box( store_t *gctx,
|
|
|
|
void (*cb)( int sts, void *aux ), void *aux )
|
|
|
|
{
|
|
|
|
cb( maildir_validate( gctx->path, 1, (maildir_store_t *)gctx ), aux );
|
|
|
|
}
|
|
|
|
|
2014-12-29 01:08:48 +00:00
|
|
|
static int
|
|
|
|
maildir_confirm_box_empty( store_t *gctx )
|
|
|
|
{
|
|
|
|
maildir_store_t *ctx = (maildir_store_t *)gctx;
|
|
|
|
msglist_t msglist;
|
|
|
|
|
|
|
|
ctx->nexcs = ctx->minuid = ctx->maxuid = ctx->newuid = 0;
|
|
|
|
|
|
|
|
if (maildir_scan( ctx, &msglist ) != DRV_OK)
|
|
|
|
return DRV_BOX_BAD;
|
|
|
|
maildir_free_scan( &msglist );
|
|
|
|
return gctx->count ? DRV_BOX_BAD : DRV_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
maildir_delete_box( store_t *gctx,
|
|
|
|
void (*cb)( int sts, void *aux ), void *aux )
|
|
|
|
{
|
|
|
|
int i, bl, ret = DRV_OK;
|
|
|
|
struct stat st;
|
|
|
|
char buf[_POSIX_PATH_MAX];
|
|
|
|
|
|
|
|
bl = nfsnprintf( buf, sizeof(buf) - 4, "%s/", gctx->path );
|
|
|
|
if (stat( buf, &st )) {
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
sys_error( "Maildir error: cannot access mailbox '%s'", gctx->path );
|
|
|
|
ret = DRV_BOX_BAD;
|
|
|
|
}
|
|
|
|
} else if (!S_ISDIR(st.st_mode)) {
|
|
|
|
error( "Maildir error: '%s' is no valid mailbox\n", gctx->path );
|
|
|
|
ret = DRV_BOX_BAD;
|
|
|
|
} else if ((ret = maildir_clear_tmp( buf, sizeof(buf), bl )) == DRV_OK) {
|
|
|
|
nfsnprintf( buf + bl, sizeof(buf) - bl, ".uidvalidity" );
|
|
|
|
if (unlink( buf ) && errno != ENOENT)
|
|
|
|
goto badrm;
|
|
|
|
#ifdef USE_DB
|
|
|
|
nfsnprintf( buf + bl, sizeof(buf) - bl, ".isyncuidmap.db" );
|
|
|
|
if (unlink( buf ) && errno != ENOENT)
|
|
|
|
goto badrm;
|
|
|
|
#endif
|
|
|
|
/* We delete cur/ last, as it is the indicator for a present mailbox.
|
|
|
|
* That way an interrupted operation can be resumed. */
|
|
|
|
for (i = 3; --i >= 0; ) {
|
|
|
|
memcpy( buf + bl, subdirs[i], 4 );
|
|
|
|
if (rmdir( buf ) && errno != ENOENT) {
|
|
|
|
badrm:
|
|
|
|
sys_error( "Maildir error: cannot remove '%s'", buf );
|
|
|
|
ret = DRV_BOX_BAD;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cb( ret, aux );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
maildir_finish_delete_box( store_t *gctx )
|
|
|
|
{
|
|
|
|
/* Subfolders are not deleted; the deleted folder is only "stripped of its mailboxness".
|
|
|
|
* Consequently, the rmdir may legitimately fail. This behavior follows the IMAP spec. */
|
|
|
|
if (rmdir( gctx->path ) && errno != ENOENT && errno != ENOTEMPTY) {
|
|
|
|
sys_error( "Maildir warning: cannot remove '%s'", gctx->path );
|
|
|
|
return DRV_BOX_BAD;
|
|
|
|
}
|
|
|
|
return DRV_OK;
|
|
|
|
}
|
|
|
|
|
2011-07-23 14:06:32 +00:00
|
|
|
static void
|
2014-12-27 21:13:24 +00:00
|
|
|
maildir_prepare_load_box( store_t *gctx, int opts )
|
2011-07-23 14:06:32 +00:00
|
|
|
{
|
|
|
|
if (opts & OPEN_SETFLAGS)
|
|
|
|
opts |= OPEN_OLD;
|
|
|
|
if (opts & OPEN_EXPUNGE)
|
|
|
|
opts |= OPEN_OLD|OPEN_NEW|OPEN_FLAGS;
|
|
|
|
gctx->opts = opts;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-12-27 21:13:24 +00:00
|
|
|
maildir_load_box( store_t *gctx, int minuid, int maxuid, int newuid, int *excs, int nexcs,
|
|
|
|
void (*cb)( int sts, void *aux ), void *aux )
|
2011-07-23 14:06:32 +00:00
|
|
|
{
|
|
|
|
maildir_store_t *ctx = (maildir_store_t *)gctx;
|
|
|
|
message_t **msgapp;
|
|
|
|
msglist_t msglist;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ctx->minuid = minuid;
|
|
|
|
ctx->maxuid = maxuid;
|
2011-04-10 11:06:07 +00:00
|
|
|
ctx->newuid = newuid;
|
2011-07-23 14:06:32 +00:00
|
|
|
ctx->excs = nfrealloc( excs, nexcs * sizeof(int) );
|
|
|
|
ctx->nexcs = nexcs;
|
2004-03-27 16:07:20 +00:00
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
if (maildir_scan( ctx, &msglist ) != DRV_OK) {
|
|
|
|
cb( DRV_BOX_BAD, aux );
|
|
|
|
return;
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
msgapp = &ctx->gen.msgs;
|
|
|
|
for (i = 0; i < msglist.nents; i++)
|
|
|
|
maildir_app_msg( ctx, &msgapp, msglist.ents + i );
|
|
|
|
maildir_free_scan( &msglist );
|
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
cb( DRV_OK, aux );
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
maildir_rescan( maildir_store_t *ctx )
|
|
|
|
{
|
|
|
|
message_t **msgapp;
|
|
|
|
maildir_message_t *msg;
|
|
|
|
msglist_t msglist;
|
|
|
|
int i;
|
|
|
|
|
2014-12-29 01:01:23 +00:00
|
|
|
ctx->fresh[0] = ctx->fresh[1] = 0;
|
2004-03-27 16:07:20 +00:00
|
|
|
if (maildir_scan( ctx, &msglist ) != DRV_OK)
|
|
|
|
return DRV_BOX_BAD;
|
|
|
|
for (msgapp = &ctx->gen.msgs, i = 0;
|
|
|
|
(msg = (maildir_message_t *)*msgapp) || i < msglist.nents; )
|
|
|
|
{
|
2004-09-08 14:52:22 +00:00
|
|
|
if (!msg) {
|
|
|
|
#if 0
|
|
|
|
debug( "adding new message %d\n", msglist.ents[i].uid );
|
|
|
|
maildir_app_msg( ctx, &msgapp, msglist.ents + i );
|
|
|
|
#else
|
2004-03-27 16:07:20 +00:00
|
|
|
debug( "ignoring new message %d\n", msglist.ents[i].uid );
|
2004-09-08 14:52:22 +00:00
|
|
|
#endif
|
|
|
|
i++;
|
|
|
|
} else if (i >= msglist.nents) {
|
|
|
|
debug( "purging deleted message %d\n", msg->gen.uid );
|
|
|
|
msg->gen.status = M_DEAD;
|
|
|
|
msgapp = &msg->gen.next;
|
|
|
|
} else if (msglist.ents[i].uid < msg->gen.uid) {
|
|
|
|
/* this should not happen, actually */
|
|
|
|
#if 0
|
|
|
|
debug( "adding new message %d\n", msglist.ents[i].uid );
|
|
|
|
maildir_app_msg( ctx, &msgapp, msglist.ents + i );
|
|
|
|
#else
|
|
|
|
debug( "ignoring new message %d\n", msglist.ents[i].uid );
|
|
|
|
#endif
|
2004-03-27 16:07:20 +00:00
|
|
|
i++;
|
2004-09-08 14:52:22 +00:00
|
|
|
} else if (msglist.ents[i].uid > msg->gen.uid) {
|
2004-03-27 16:07:20 +00:00
|
|
|
debug( "purging deleted message %d\n", msg->gen.uid );
|
|
|
|
msg->gen.status = M_DEAD;
|
|
|
|
msgapp = &msg->gen.next;
|
|
|
|
} else {
|
|
|
|
debug( "updating message %d\n", msg->gen.uid );
|
|
|
|
msg->gen.status &= ~(M_FLAGS|M_RECENT);
|
|
|
|
free( msg->base );
|
|
|
|
maildir_init_msg( ctx, msg, msglist.ents + i );
|
|
|
|
i++, msgapp = &msg->gen.next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
maildir_free_scan( &msglist );
|
|
|
|
return DRV_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-04-10 13:32:25 +00:00
|
|
|
maildir_again( maildir_store_t *ctx, maildir_message_t *msg,
|
|
|
|
const char *err, const char *fn, const char *fn2 )
|
2004-03-27 16:07:20 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (errno != ENOENT) {
|
2011-04-10 13:32:25 +00:00
|
|
|
sys_error( err, fn, fn2 );
|
2004-03-27 16:07:20 +00:00
|
|
|
return DRV_BOX_BAD;
|
|
|
|
}
|
|
|
|
if ((ret = maildir_rescan( ctx )) != DRV_OK)
|
|
|
|
return ret;
|
2005-03-28 10:26:39 +00:00
|
|
|
return (msg->gen.status & M_DEAD) ? DRV_MSG_BAD : DRV_OK;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
static void
|
2006-03-21 20:03:21 +00:00
|
|
|
maildir_fetch_msg( store_t *gctx, message_t *gmsg, msg_data_t *data,
|
2012-07-29 21:14:48 +00:00
|
|
|
void (*cb)( int sts, void *aux ), void *aux )
|
2004-03-27 16:07:20 +00:00
|
|
|
{
|
|
|
|
maildir_store_t *ctx = (maildir_store_t *)gctx;
|
|
|
|
maildir_message_t *msg = (maildir_message_t *)gmsg;
|
|
|
|
int fd, ret;
|
|
|
|
struct stat st;
|
|
|
|
char buf[_POSIX_PATH_MAX];
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
nfsnprintf( buf, sizeof(buf), "%s/%s/%s", gctx->path, subdirs[gmsg->status & M_RECENT], msg->base );
|
|
|
|
if ((fd = open( buf, O_RDONLY )) >= 0)
|
|
|
|
break;
|
2011-04-10 13:32:25 +00:00
|
|
|
if ((ret = maildir_again( ctx, msg, "Cannot open %s", buf, 0 )) != DRV_OK) {
|
2012-07-29 21:14:48 +00:00
|
|
|
cb( ret, aux );
|
|
|
|
return;
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
fstat( fd, &st );
|
|
|
|
data->len = st.st_size;
|
2013-07-28 13:55:13 +00:00
|
|
|
if (data->date == -1)
|
|
|
|
data->date = st.st_mtime;
|
2004-03-27 16:07:20 +00:00
|
|
|
data->data = nfmalloc( data->len );
|
|
|
|
if (read( fd, data->data, data->len ) != data->len) {
|
2011-04-10 13:32:25 +00:00
|
|
|
sys_error( "Maildir error: cannot read %s", buf );
|
2004-03-27 16:07:20 +00:00
|
|
|
close( fd );
|
2012-07-29 21:14:48 +00:00
|
|
|
cb( DRV_MSG_BAD, aux );
|
|
|
|
return;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
close( fd );
|
|
|
|
if (!(gmsg->status & M_FLAGS))
|
2014-10-25 15:30:57 +00:00
|
|
|
data->flags = maildir_parse_flags( ((maildir_store_conf_t *)gctx->conf)->info_prefix, msg->base );
|
2012-07-29 21:14:48 +00:00
|
|
|
cb( DRV_OK, aux );
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2014-10-25 15:30:57 +00:00
|
|
|
maildir_make_flags( char info_delimiter, int flags, char *buf )
|
2004-03-27 16:07:20 +00:00
|
|
|
{
|
2014-12-07 12:19:30 +00:00
|
|
|
uint i, d;
|
2004-03-27 16:07:20 +00:00
|
|
|
|
2014-10-25 15:30:57 +00:00
|
|
|
buf[0] = info_delimiter;
|
2004-03-27 16:07:20 +00:00
|
|
|
buf[1] = '2';
|
|
|
|
buf[2] = ',';
|
|
|
|
for (d = 3, i = 0; i < as(Flags); i++)
|
|
|
|
if (flags & (1 << i))
|
|
|
|
buf[d++] = Flags[i];
|
|
|
|
buf[d] = 0;
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
static void
|
2006-03-21 20:03:21 +00:00
|
|
|
maildir_store_msg( store_t *gctx, msg_data_t *data, int to_trash,
|
2012-07-29 21:14:48 +00:00
|
|
|
void (*cb)( int sts, int uid, void *aux ), void *aux )
|
2004-03-27 16:07:20 +00:00
|
|
|
{
|
|
|
|
maildir_store_t *ctx = (maildir_store_t *)gctx;
|
2012-08-11 15:11:57 +00:00
|
|
|
const char *box;
|
2006-03-21 20:03:21 +00:00
|
|
|
int ret, fd, bl, uid;
|
2004-03-27 16:07:20 +00:00
|
|
|
char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX], fbuf[NUM_FLAGS + 3], base[128];
|
|
|
|
|
2013-03-23 14:07:47 +00:00
|
|
|
bl = nfsnprintf( base, sizeof(base), "%ld.%d_%d.%s", (long)time( 0 ), Pid, ++MaildirCount, Hostname );
|
2006-03-21 20:03:21 +00:00
|
|
|
if (!to_trash) {
|
2004-03-27 16:07:20 +00:00
|
|
|
#ifdef USE_DB
|
2015-01-01 18:25:17 +00:00
|
|
|
if (ctx->usedb) {
|
2006-03-21 20:03:21 +00:00
|
|
|
if ((ret = maildir_set_uid( ctx, base, &uid )) != DRV_OK) {
|
2004-03-27 16:07:20 +00:00
|
|
|
free( data->data );
|
2012-07-29 21:14:48 +00:00
|
|
|
cb( ret, 0, aux );
|
|
|
|
return;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif /* USE_DB */
|
|
|
|
{
|
2015-01-01 15:49:23 +00:00
|
|
|
if ((ret = maildir_obtain_uid( ctx, &uid )) != DRV_OK) {
|
2011-05-22 15:22:24 +00:00
|
|
|
free( data->data );
|
2012-07-29 21:14:48 +00:00
|
|
|
cb( ret, 0, aux );
|
|
|
|
return;
|
|
|
|
}
|
2006-03-21 20:03:21 +00:00
|
|
|
nfsnprintf( base + bl, sizeof(base) - bl, ",U=%d", uid );
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
2012-08-11 15:11:57 +00:00
|
|
|
box = gctx->path;
|
2004-03-27 16:07:20 +00:00
|
|
|
} else {
|
2015-04-13 07:17:56 +00:00
|
|
|
uid = 0;
|
2012-08-11 15:11:57 +00:00
|
|
|
box = ctx->trash;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
|
2014-10-25 15:30:57 +00:00
|
|
|
maildir_make_flags( ((maildir_store_conf_t *)gctx->conf)->info_delimiter, data->flags, fbuf );
|
2012-08-11 15:11:57 +00:00
|
|
|
nfsnprintf( buf, sizeof(buf), "%s/tmp/%s%s", box, base, fbuf );
|
2004-03-27 16:07:20 +00:00
|
|
|
if ((fd = open( buf, O_WRONLY|O_CREAT|O_EXCL, 0600 )) < 0) {
|
2012-08-11 15:05:22 +00:00
|
|
|
if (errno != ENOENT || !to_trash) {
|
2011-04-10 13:32:25 +00:00
|
|
|
sys_error( "Maildir error: cannot create %s", buf );
|
2004-03-27 16:07:20 +00:00
|
|
|
free( data->data );
|
2012-07-29 21:14:48 +00:00
|
|
|
cb( DRV_BOX_BAD, 0, aux );
|
|
|
|
return;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
2012-08-11 15:11:57 +00:00
|
|
|
if ((ret = maildir_validate( box, 1, ctx )) != DRV_OK) {
|
2004-03-27 16:07:20 +00:00
|
|
|
free( data->data );
|
2012-07-29 21:14:48 +00:00
|
|
|
cb( ret, 0, aux );
|
|
|
|
return;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
if ((fd = open( buf, O_WRONLY|O_CREAT|O_EXCL, 0600 )) < 0) {
|
2011-04-10 13:32:25 +00:00
|
|
|
sys_error( "Maildir error: cannot create %s", buf );
|
2004-03-27 16:07:20 +00:00
|
|
|
free( data->data );
|
2012-07-29 21:14:48 +00:00
|
|
|
cb( DRV_BOX_BAD, 0, aux );
|
|
|
|
return;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = write( fd, data->data, data->len );
|
|
|
|
free( data->data );
|
2013-11-02 20:42:34 +00:00
|
|
|
if (ret != data->len || (UseFSync && (ret = fsync( fd )))) {
|
2004-03-27 16:07:20 +00:00
|
|
|
if (ret < 0)
|
2011-04-10 13:32:25 +00:00
|
|
|
sys_error( "Maildir error: cannot write %s", buf );
|
2004-03-27 16:07:20 +00:00
|
|
|
else
|
2011-04-10 13:32:25 +00:00
|
|
|
error( "Maildir error: cannot write %s. Disk full?\n", buf );
|
2004-03-27 16:07:20 +00:00
|
|
|
close( fd );
|
2012-07-29 21:14:48 +00:00
|
|
|
cb( DRV_BOX_BAD, 0, aux );
|
|
|
|
return;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
2011-07-23 10:49:08 +00:00
|
|
|
if (close( fd ) < 0) {
|
|
|
|
/* Quota exceeded may cause this. */
|
2011-04-10 13:32:25 +00:00
|
|
|
sys_error( "Maildir error: cannot write %s", buf );
|
2012-07-29 21:14:48 +00:00
|
|
|
cb( DRV_BOX_BAD, 0, aux );
|
|
|
|
return;
|
2011-07-23 10:49:08 +00:00
|
|
|
}
|
2013-07-28 13:55:13 +00:00
|
|
|
|
|
|
|
if (data->date) {
|
|
|
|
/* Set atime and mtime according to INTERNALDATE or mtime of source message */
|
|
|
|
struct utimbuf utimebuf;
|
|
|
|
utimebuf.actime = utimebuf.modtime = data->date;
|
|
|
|
if (utime( buf, &utimebuf ) < 0) {
|
|
|
|
sys_error( "Maildir error: cannot set times for %s", buf );
|
|
|
|
cb( DRV_BOX_BAD, 0, aux );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-27 12:44:13 +00:00
|
|
|
/* Moving seen messages to cur/ is strictly speaking incorrect, but makes mutt happy. */
|
2012-08-11 15:11:57 +00:00
|
|
|
nfsnprintf( nbuf, sizeof(nbuf), "%s/%s/%s%s", box, subdirs[!(data->flags & F_SEEN)], base, fbuf );
|
2004-03-27 16:07:20 +00:00
|
|
|
if (rename( buf, nbuf )) {
|
2011-04-10 13:32:25 +00:00
|
|
|
sys_error( "Maildir error: cannot rename %s to %s", buf, nbuf );
|
2012-07-29 21:14:48 +00:00
|
|
|
cb( DRV_BOX_BAD, 0, aux );
|
|
|
|
return;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
2012-07-29 21:14:48 +00:00
|
|
|
cb( DRV_OK, uid, aux );
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
static void
|
2015-01-11 13:29:19 +00:00
|
|
|
maildir_find_new_msgs( store_t *gctx ATTR_UNUSED, int newuid ATTR_UNUSED,
|
2011-04-10 11:06:07 +00:00
|
|
|
void (*cb)( int sts, void *aux ) ATTR_UNUSED, void *aux ATTR_UNUSED )
|
2006-02-03 21:33:43 +00:00
|
|
|
{
|
2011-04-10 11:06:07 +00:00
|
|
|
assert( !"maildir_find_new_msgs is not supposed to be called" );
|
2006-02-03 21:33:43 +00:00
|
|
|
}
|
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
static void
|
2014-12-27 21:13:24 +00:00
|
|
|
maildir_set_msg_flags( store_t *gctx, message_t *gmsg, int uid ATTR_UNUSED, int add, int del,
|
|
|
|
void (*cb)( int sts, void *aux ), void *aux )
|
2004-03-27 16:07:20 +00:00
|
|
|
{
|
2014-10-25 15:30:57 +00:00
|
|
|
maildir_store_conf_t *conf = (maildir_store_conf_t *)gctx->conf;
|
2004-03-27 16:07:20 +00:00
|
|
|
maildir_store_t *ctx = (maildir_store_t *)gctx;
|
|
|
|
maildir_message_t *msg = (maildir_message_t *)gmsg;
|
|
|
|
char *s, *p;
|
2014-12-07 12:19:30 +00:00
|
|
|
uint i;
|
2004-03-27 16:07:20 +00:00
|
|
|
int j, ret, ol, fl, bbl, bl, tl;
|
|
|
|
char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX];
|
|
|
|
|
|
|
|
bbl = nfsnprintf( buf, sizeof(buf), "%s/", gctx->path );
|
2005-09-29 21:07:20 +00:00
|
|
|
memcpy( nbuf, gctx->path, bbl - 1 );
|
|
|
|
memcpy( nbuf + bbl - 1, "/cur/", 5 );
|
2004-03-27 16:07:20 +00:00
|
|
|
for (;;) {
|
|
|
|
bl = bbl + nfsnprintf( buf + bbl, sizeof(buf) - bbl, "%s/", subdirs[gmsg->status & M_RECENT] );
|
|
|
|
ol = strlen( msg->base );
|
|
|
|
if ((int)sizeof(buf) - bl < ol + 3 + NUM_FLAGS)
|
|
|
|
oob();
|
|
|
|
memcpy( buf + bl, msg->base, ol + 1 );
|
2005-09-29 21:07:20 +00:00
|
|
|
memcpy( nbuf + bl, msg->base, ol + 1 );
|
2014-10-25 15:30:57 +00:00
|
|
|
if ((s = strstr( nbuf + bl, conf->info_prefix ))) {
|
2004-03-27 16:07:20 +00:00
|
|
|
s += 3;
|
|
|
|
fl = ol - (s - (nbuf + bl));
|
|
|
|
for (i = 0; i < as(Flags); i++) {
|
|
|
|
if ((p = strchr( s, Flags[i] ))) {
|
|
|
|
if (del & (1 << i)) {
|
2015-04-03 20:38:46 +00:00
|
|
|
memmove( p, p + 1, fl - (p - s) );
|
2004-03-27 16:07:20 +00:00
|
|
|
fl--;
|
|
|
|
}
|
|
|
|
} else if (add & (1 << i)) {
|
|
|
|
for (j = 0; j < fl && Flags[i] > s[j]; j++);
|
|
|
|
fl++;
|
|
|
|
memmove( s + j + 1, s + j, fl - j );
|
|
|
|
s[j] = Flags[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tl = ol + 3 + fl;
|
|
|
|
} else {
|
2014-10-25 15:30:57 +00:00
|
|
|
tl = ol + maildir_make_flags( conf->info_delimiter, msg->gen.flags, nbuf + bl + ol );
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
if (!rename( buf, nbuf ))
|
|
|
|
break;
|
2011-04-10 13:32:25 +00:00
|
|
|
if ((ret = maildir_again( ctx, msg, "Maildir error: cannot rename %s to %s", buf, nbuf )) != DRV_OK) {
|
2012-07-29 21:14:48 +00:00
|
|
|
cb( ret, aux );
|
|
|
|
return;
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
free( msg->base );
|
|
|
|
msg->base = nfmalloc( tl + 1 );
|
|
|
|
memcpy( msg->base, nbuf + bl, tl + 1 );
|
|
|
|
msg->gen.flags |= add;
|
|
|
|
msg->gen.flags &= ~del;
|
2005-09-29 21:07:20 +00:00
|
|
|
gmsg->status &= ~M_RECENT;
|
2004-03-27 16:07:20 +00:00
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
cb( DRV_OK, aux );
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_DB
|
|
|
|
static int
|
|
|
|
maildir_purge_msg( maildir_store_t *ctx, const char *name )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2015-01-01 18:25:17 +00:00
|
|
|
if ((ret = maildir_uidval_lock( ctx )) != DRV_OK)
|
|
|
|
return ret;
|
2014-10-25 15:30:57 +00:00
|
|
|
make_key( ((maildir_store_conf_t *)ctx->gen.conf)->info_stop, &key, (char *)name );
|
2004-03-27 16:07:20 +00:00
|
|
|
if ((ret = ctx->db->del( ctx->db, 0, &key, 0 ))) {
|
|
|
|
ctx->db->err( ctx->db, ret, "Maildir error: db->del()" );
|
|
|
|
return DRV_BOX_BAD;
|
|
|
|
}
|
|
|
|
return DRV_OK;
|
|
|
|
}
|
|
|
|
#endif /* USE_DB */
|
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
static void
|
2006-03-21 20:03:21 +00:00
|
|
|
maildir_trash_msg( store_t *gctx, message_t *gmsg,
|
2012-07-29 21:14:48 +00:00
|
|
|
void (*cb)( int sts, void *aux ), void *aux )
|
2004-03-27 16:07:20 +00:00
|
|
|
{
|
|
|
|
maildir_store_t *ctx = (maildir_store_t *)gctx;
|
|
|
|
maildir_message_t *msg = (maildir_message_t *)gmsg;
|
|
|
|
char *s;
|
|
|
|
int ret;
|
|
|
|
struct stat st;
|
|
|
|
char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX];
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
nfsnprintf( buf, sizeof(buf), "%s/%s/%s", gctx->path, subdirs[gmsg->status & M_RECENT], msg->base );
|
2014-10-25 15:30:57 +00:00
|
|
|
s = strstr( msg->base, ((maildir_store_conf_t *)gctx->conf)->info_prefix );
|
2012-08-11 15:11:57 +00:00
|
|
|
nfsnprintf( nbuf, sizeof(nbuf), "%s/%s/%ld.%d_%d.%s%s", ctx->trash,
|
2013-03-23 14:07:47 +00:00
|
|
|
subdirs[gmsg->status & M_RECENT], (long)time( 0 ), Pid, ++MaildirCount, Hostname, s ? s : "" );
|
2004-03-27 16:07:20 +00:00
|
|
|
if (!rename( buf, nbuf ))
|
|
|
|
break;
|
|
|
|
if (!stat( buf, &st )) {
|
2012-08-11 15:11:57 +00:00
|
|
|
if ((ret = maildir_validate( ctx->trash, 1, ctx )) != DRV_OK) {
|
2012-07-29 21:14:48 +00:00
|
|
|
cb( ret, aux );
|
|
|
|
return;
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
if (!rename( buf, nbuf ))
|
|
|
|
break;
|
2004-05-23 16:39:24 +00:00
|
|
|
if (errno != ENOENT) {
|
2011-04-10 13:32:25 +00:00
|
|
|
sys_error( "Maildir error: cannot move %s to %s", buf, nbuf );
|
2012-07-29 21:14:48 +00:00
|
|
|
cb( DRV_BOX_BAD, aux );
|
|
|
|
return;
|
2004-05-23 16:39:24 +00:00
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
2011-04-10 13:32:25 +00:00
|
|
|
if ((ret = maildir_again( ctx, msg, "Maildir error: cannot move %s to %s", buf, nbuf )) != DRV_OK) {
|
2012-07-29 21:14:48 +00:00
|
|
|
cb( ret, aux );
|
|
|
|
return;
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
gmsg->status |= M_DEAD;
|
|
|
|
gctx->count--;
|
|
|
|
|
|
|
|
#ifdef USE_DB
|
2015-01-01 18:25:17 +00:00
|
|
|
if (ctx->usedb) {
|
2012-07-29 21:14:48 +00:00
|
|
|
cb( maildir_purge_msg( ctx, msg->base ), aux );
|
|
|
|
return;
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
#endif /* USE_DB */
|
2012-07-29 21:14:48 +00:00
|
|
|
cb( DRV_OK, aux );
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
static void
|
2014-12-27 21:13:24 +00:00
|
|
|
maildir_close_box( store_t *gctx,
|
|
|
|
void (*cb)( int sts, void *aux ), void *aux )
|
2004-03-27 16:07:20 +00:00
|
|
|
{
|
|
|
|
#ifdef USE_DB
|
|
|
|
maildir_store_t *ctx = (maildir_store_t *)gctx;
|
|
|
|
#endif /* USE_DB */
|
|
|
|
message_t *msg;
|
|
|
|
int basel, retry, ret;
|
|
|
|
char buf[_POSIX_PATH_MAX];
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
retry = 0;
|
|
|
|
basel = nfsnprintf( buf, sizeof(buf), "%s/", gctx->path );
|
|
|
|
for (msg = gctx->msgs; msg; msg = msg->next)
|
|
|
|
if (!(msg->status & M_DEAD) && (msg->flags & F_DELETED)) {
|
|
|
|
nfsnprintf( buf + basel, sizeof(buf) - basel, "%s/%s", subdirs[msg->status & M_RECENT], ((maildir_message_t *)msg)->base );
|
|
|
|
if (unlink( buf )) {
|
|
|
|
if (errno == ENOENT)
|
|
|
|
retry = 1;
|
|
|
|
else
|
2011-04-10 13:32:25 +00:00
|
|
|
sys_error( "Maildir error: cannot remove %s", buf );
|
2004-03-27 16:07:20 +00:00
|
|
|
} else {
|
|
|
|
msg->status |= M_DEAD;
|
|
|
|
gctx->count--;
|
|
|
|
#ifdef USE_DB
|
2012-07-29 21:14:48 +00:00
|
|
|
if (ctx->db && (ret = maildir_purge_msg( ctx, ((maildir_message_t *)msg)->base )) != DRV_OK) {
|
|
|
|
cb( ret, aux );
|
|
|
|
return;
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
#endif /* USE_DB */
|
|
|
|
}
|
|
|
|
}
|
2012-07-29 21:14:48 +00:00
|
|
|
if (!retry) {
|
|
|
|
cb( DRV_OK, aux );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((ret = maildir_rescan( (maildir_store_t *)gctx )) != DRV_OK) {
|
|
|
|
cb( ret, aux );
|
|
|
|
return;
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-21 20:03:21 +00:00
|
|
|
static void
|
2014-12-27 21:13:24 +00:00
|
|
|
maildir_cancel_cmds( store_t *gctx ATTR_UNUSED,
|
|
|
|
void (*cb)( void *aux ), void *aux )
|
2006-03-21 20:03:21 +00:00
|
|
|
{
|
2012-07-22 15:32:32 +00:00
|
|
|
cb( aux );
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-12-27 21:13:24 +00:00
|
|
|
maildir_commit_cmds( store_t *gctx )
|
2004-03-27 16:07:20 +00:00
|
|
|
{
|
|
|
|
(void) gctx;
|
|
|
|
}
|
|
|
|
|
2015-02-15 17:13:05 +00:00
|
|
|
static int
|
|
|
|
maildir_memory_usage( store_t *gctx ATTR_UNUSED )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-03-27 16:07:20 +00:00
|
|
|
static int
|
2012-09-15 09:46:42 +00:00
|
|
|
maildir_parse_store( conffile_t *cfg, store_conf_t **storep )
|
2004-03-27 16:07:20 +00:00
|
|
|
{
|
|
|
|
maildir_store_conf_t *store;
|
|
|
|
|
|
|
|
if (strcasecmp( "MaildirStore", cfg->cmd ))
|
|
|
|
return 0;
|
|
|
|
store = nfcalloc( sizeof(*store) );
|
2014-10-25 15:30:57 +00:00
|
|
|
store->info_delimiter = FieldDelimiter;
|
2004-03-27 16:07:20 +00:00
|
|
|
store->gen.driver = &maildir_driver;
|
|
|
|
store->gen.name = nfstrdup( cfg->val );
|
|
|
|
|
|
|
|
while (getcline( cfg ) && cfg->cmd)
|
|
|
|
if (!strcasecmp( "Inbox", cfg->cmd ))
|
|
|
|
store->inbox = expand_strdup( cfg->val );
|
|
|
|
else if (!strcasecmp( "Path", cfg->cmd ))
|
|
|
|
store->gen.path = expand_strdup( cfg->val );
|
|
|
|
#ifdef USE_DB
|
|
|
|
else if (!strcasecmp( "AltMap", cfg->cmd ))
|
|
|
|
store->alt_map = parse_bool( cfg );
|
|
|
|
#endif /* USE_DB */
|
2014-10-25 15:30:57 +00:00
|
|
|
else if (!strcasecmp( "InfoDelimiter", cfg->cmd )) {
|
|
|
|
if (strlen( cfg->val ) != 1) {
|
|
|
|
error( "%s:%d: Info delimiter must be exactly one character long\n", cfg->file, cfg->line );
|
|
|
|
cfg->err = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
store->info_delimiter = cfg->val[0];
|
|
|
|
if (!ispunct( store->info_delimiter )) {
|
|
|
|
error( "%s:%d: Info delimiter must be a punctuation character\n", cfg->file, cfg->line );
|
|
|
|
cfg->err = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else
|
2012-09-15 09:46:42 +00:00
|
|
|
parse_generic_store( &store->gen, cfg );
|
2004-03-27 16:07:20 +00:00
|
|
|
if (!store->inbox)
|
|
|
|
store->inbox = expand_strdup( "~/Maildir" );
|
2014-10-25 15:30:57 +00:00
|
|
|
nfasprintf( &store->info_prefix, "%c2,", store->info_delimiter );
|
|
|
|
nfasprintf( &store->info_stop, "%c,", store->info_delimiter );
|
2004-03-27 16:07:20 +00:00
|
|
|
*storep = &store->gen;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct driver maildir_driver = {
|
2010-02-06 09:34:41 +00:00
|
|
|
0, /* XXX DRV_CRLF? */
|
2004-03-27 16:07:20 +00:00
|
|
|
maildir_parse_store,
|
2006-03-20 19:38:20 +00:00
|
|
|
maildir_cleanup_drv,
|
2004-03-27 16:07:20 +00:00
|
|
|
maildir_open_store,
|
2006-03-20 19:38:20 +00:00
|
|
|
maildir_disown_store,
|
|
|
|
maildir_disown_store, /* _cancel_, but it's the same */
|
2014-12-27 21:13:24 +00:00
|
|
|
maildir_list_store,
|
|
|
|
maildir_select_box,
|
2014-12-29 00:42:17 +00:00
|
|
|
maildir_create_box,
|
2014-12-27 22:39:55 +00:00
|
|
|
maildir_open_box,
|
2014-12-29 01:08:48 +00:00
|
|
|
maildir_confirm_box_empty,
|
|
|
|
maildir_delete_box,
|
|
|
|
maildir_finish_delete_box,
|
2014-12-27 21:13:24 +00:00
|
|
|
maildir_prepare_load_box,
|
|
|
|
maildir_load_box,
|
2004-03-27 16:07:20 +00:00
|
|
|
maildir_fetch_msg,
|
|
|
|
maildir_store_msg,
|
2011-04-10 11:06:07 +00:00
|
|
|
maildir_find_new_msgs,
|
2014-12-27 21:13:24 +00:00
|
|
|
maildir_set_msg_flags,
|
2004-03-27 16:07:20 +00:00
|
|
|
maildir_trash_msg,
|
2014-12-27 21:13:24 +00:00
|
|
|
maildir_close_box,
|
|
|
|
maildir_cancel_cmds,
|
|
|
|
maildir_commit_cmds,
|
2015-02-15 17:13:05 +00:00
|
|
|
maildir_memory_usage,
|
2004-03-27 16:07:20 +00:00
|
|
|
};
|