2004-03-27 16:07:20 +00:00
|
|
|
/*
|
|
|
|
* mbsync - mailbox synchronizer
|
|
|
|
* Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
|
2011-04-10 17:25:46 +00:00
|
|
|
* Copyright (C) 2002-2006,2010-2012 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "isync.h"
|
|
|
|
|
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>
|
|
|
|
|
|
|
|
#define USE_DB 1
|
2004-09-20 11:31:55 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
# define LEGACY_FLOCK 1
|
|
|
|
#endif
|
2004-03-27 16:07:20 +00:00
|
|
|
|
2012-09-09 10:18:14 +00:00
|
|
|
#ifndef _POSIX_SYNCHRONIZED_IO
|
|
|
|
# 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 */
|
|
|
|
} maildir_store_conf_t;
|
|
|
|
|
|
|
|
typedef struct maildir_message {
|
|
|
|
message_t gen;
|
|
|
|
char *base;
|
|
|
|
} maildir_message_t;
|
|
|
|
|
|
|
|
typedef struct maildir_store {
|
|
|
|
store_t gen;
|
|
|
|
int uvfd, uvok, nuid;
|
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;
|
|
|
|
#endif /* USE_DB */
|
|
|
|
} 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;
|
|
|
|
|
|
|
|
static const char Flags[] = { 'D', 'F', 'R', 'S', 'T' };
|
|
|
|
|
|
|
|
static unsigned char
|
|
|
|
maildir_parse_flags( const char *base )
|
|
|
|
{
|
|
|
|
const char *s;
|
|
|
|
unsigned i;
|
|
|
|
unsigned char flags;
|
|
|
|
|
|
|
|
flags = 0;
|
|
|
|
if ((s = strstr( base, ":2," )))
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-03-21 20:03:21 +00:00
|
|
|
static void
|
|
|
|
maildir_open_store( store_conf_t *conf,
|
|
|
|
void (*cb)( store_t *ctx, void *aux ), void *aux )
|
2004-03-27 16:07:20 +00:00
|
|
|
{
|
|
|
|
maildir_store_t *ctx;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
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 );
|
2006-03-21 20:03:21 +00:00
|
|
|
cb( 0, aux );
|
|
|
|
return;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
ctx = nfcalloc( sizeof(*ctx) );
|
|
|
|
ctx->gen.conf = conf;
|
|
|
|
ctx->uvfd = -1;
|
2012-08-11 16:34:46 +00:00
|
|
|
if (conf->trash)
|
|
|
|
ctx->trash = maildir_join_path( conf->path, conf->trash );
|
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 );
|
|
|
|
#endif /* USE_DB */
|
2012-08-11 15:11:57 +00:00
|
|
|
free( ctx->trash );
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-03-20 19:38:20 +00:00
|
|
|
maildir_disown_store( store_t *gctx )
|
2004-03-27 16:07:20 +00:00
|
|
|
{
|
|
|
|
maildir_cleanup( gctx );
|
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 store_t *
|
|
|
|
maildir_own_store( store_conf_t *conf )
|
|
|
|
{
|
|
|
|
(void)conf;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2012-08-11 16:34:46 +00:00
|
|
|
static int maildir_list_part( store_t *gctx, int doInbox, int *flags );
|
|
|
|
|
|
|
|
static int
|
|
|
|
maildir_list_recurse( store_t *gctx, int isBox, int *flags, const char *inbox,
|
|
|
|
char *path, int pathLen, char *name, int nameLen )
|
2004-03-27 16:07:20 +00:00
|
|
|
{
|
|
|
|
DIR *dir;
|
2012-08-11 16:34:46 +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) {
|
|
|
|
nfsnprintf( path + pathLen, _POSIX_PATH_MAX - pathLen, "/cur" );
|
|
|
|
if (stat( path, &st ) || !S_ISDIR(st.st_mode))
|
|
|
|
return 0;
|
|
|
|
path[pathLen] = 0;
|
|
|
|
add_string_list( &gctx->boxes, name );
|
|
|
|
name[nameLen++] = '/';
|
|
|
|
}
|
|
|
|
if (!(dir = opendir( path ))) {
|
|
|
|
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 );
|
|
|
|
if (inbox && !memcmp( path, inbox, pl ) && !inbox[pl]) {
|
|
|
|
if (maildir_list_part( gctx, 1, flags ) < 0)
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
if (!memcmp( ent, "INBOX", 6 )) {
|
|
|
|
path[pathLen] = 0;
|
|
|
|
warn( "Maildir warning: ignoring INBOX in %s\n", path );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*ent == '.') {
|
|
|
|
if (!isBox)
|
|
|
|
continue;
|
|
|
|
ent++;
|
|
|
|
} else {
|
|
|
|
if (isBox)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
nl = nameLen + nfsnprintf( name + nameLen, _POSIX_PATH_MAX - nameLen, "%s", ent );
|
|
|
|
if (maildir_list_recurse( gctx, 1, flags, inbox, path, pl, name, nl ) < 0)
|
|
|
|
return -1;
|
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
|
|
|
|
maildir_list_part( store_t *gctx, int doInbox, int *flags )
|
|
|
|
{
|
|
|
|
int pl, nl;
|
|
|
|
const char *inbox = ((maildir_store_conf_t *)gctx->conf)->inbox;
|
|
|
|
char path[_POSIX_PATH_MAX], name[_POSIX_PATH_MAX];
|
|
|
|
|
|
|
|
if (doInbox) {
|
|
|
|
*flags &= ~LIST_INBOX;
|
|
|
|
pl = nfsnprintf( path, _POSIX_PATH_MAX, "%s", inbox );
|
|
|
|
nl = nfsnprintf( name, _POSIX_PATH_MAX, "INBOX" );
|
|
|
|
return maildir_list_recurse( gctx, 1, flags, 0, path, pl, name, nl );
|
|
|
|
} else {
|
|
|
|
pl = nfsnprintf( path, _POSIX_PATH_MAX, "%s", gctx->conf->path );
|
|
|
|
return maildir_list_recurse( gctx, 0, flags, inbox, path, pl, name, 0 );
|
|
|
|
}
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
|
2012-08-11 16:34:46 +00:00
|
|
|
static void
|
|
|
|
maildir_list( store_t *gctx, int flags,
|
|
|
|
void (*cb)( int sts, void *aux ), void *aux )
|
|
|
|
{
|
|
|
|
if (((flags & LIST_PATH) && maildir_list_part( gctx, 0, &flags ) < 0) ||
|
|
|
|
((flags & LIST_INBOX) && maildir_list_part( gctx, 1, &flags ) < 0)) {
|
|
|
|
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;
|
|
|
|
unsigned 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
|
2012-08-11 15:11:57 +00:00
|
|
|
maildir_validate( const char *box, int create, maildir_store_t *ctx )
|
2004-03-27 16:07:20 +00:00
|
|
|
{
|
|
|
|
DIR *dirp;
|
|
|
|
struct dirent *entry;
|
2012-08-11 16:34:46 +00:00
|
|
|
char *p;
|
2004-03-27 16:07:20 +00:00
|
|
|
time_t now;
|
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 )) {
|
|
|
|
if (errno == ENOENT) {
|
|
|
|
if (create) {
|
2012-08-11 16:34:46 +00:00
|
|
|
p = memrchr( buf, '/', bl - 1 );
|
|
|
|
if (*(p + 1) == '.') {
|
|
|
|
*p = 0;
|
|
|
|
if ((ret = maildir_validate( buf, 1, ctx )) != DRV_OK)
|
|
|
|
return ret;
|
|
|
|
*p = '/';
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
if (mkdir( buf, 0700 )) {
|
2011-04-10 13:32:25 +00:00
|
|
|
sys_error( "Maildir error: cannot create mailbox '%s'", buf );
|
2012-07-15 10:55:04 +00:00
|
|
|
maildir_invoke_bad_callback( &ctx->gen );
|
|
|
|
return DRV_CANCELED;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
memcpy( buf + bl, subdirs[i], 4 );
|
|
|
|
if (mkdir( buf, 0700 )) {
|
2011-04-10 13:32:25 +00:00
|
|
|
sys_error( "Maildir error: cannot create directory %s", buf );
|
2004-03-27 16:07:20 +00:00
|
|
|
return DRV_BOX_BAD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2006-03-19 11:29:12 +00:00
|
|
|
error( "Maildir error: mailbox '%s' does not exist\n", buf );
|
2004-03-27 16:07:20 +00:00
|
|
|
return DRV_BOX_BAD;
|
|
|
|
}
|
|
|
|
} else {
|
2011-04-10 13:32:25 +00:00
|
|
|
sys_error( "Maildir error: cannot access mailbox '%s'", buf );
|
2004-03-27 16:07:20 +00:00
|
|
|
return DRV_BOX_BAD;
|
|
|
|
}
|
|
|
|
} else {
|
2011-07-24 18:26:10 +00:00
|
|
|
for (i = 0; i < 3; i++) {
|
2004-03-27 16:07:20 +00:00
|
|
|
memcpy( buf + bl, subdirs[i], 4 );
|
2011-07-24 18:26:10 +00:00
|
|
|
if (stat( buf, &st ) || !S_ISDIR(st.st_mode)) {
|
|
|
|
error( "Maildir error: '%.*s' is no valid mailbox\n", bl, buf );
|
|
|
|
return DRV_BOX_BAD;
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
memcpy( buf + bl, "tmp/", 5 );
|
|
|
|
bl += 4;
|
|
|
|
if (!(dirp = opendir( buf ))) {
|
2011-04-10 13:32:25 +00:00
|
|
|
sys_error( "Maildir error: cannot list %s", buf );
|
2004-03-27 16:07:20 +00:00
|
|
|
return DRV_BOX_BAD;
|
|
|
|
}
|
|
|
|
time( &now );
|
|
|
|
while ((entry = readdir( dirp ))) {
|
|
|
|
nfsnprintf( buf + bl, sizeof(buf) - bl, "%s", entry->d_name );
|
2011-03-27 18:39:53 +00:00
|
|
|
if (stat( buf, &st )) {
|
|
|
|
if (errno != ENOENT)
|
2011-04-10 13:32:25 +00:00
|
|
|
sys_error( "Maildir error: cannot access %s", buf );
|
2011-03-27 18:39:53 +00:00
|
|
|
} else if (S_ISREG(st.st_mode) && now - st.st_ctime >= _24_HOURS) {
|
2004-03-27 16:07:20 +00:00
|
|
|
/* this should happen infrequently enough that it won't be
|
|
|
|
* bothersome to the user to display when it occurs.
|
|
|
|
*/
|
|
|
|
info( "Maildir notice: removing stale file %s\n", buf );
|
2011-03-27 18:39:53 +00:00
|
|
|
if (unlink( buf ) && errno != ENOENT)
|
2011-04-10 13:32:25 +00:00
|
|
|
sys_error( "Maildir error: cannot remove %s", buf );
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir( dirp );
|
|
|
|
}
|
|
|
|
return DRV_OK;
|
|
|
|
}
|
|
|
|
|
2004-05-23 16:39:24 +00:00
|
|
|
#ifdef USE_DB
|
|
|
|
static void
|
|
|
|
make_key( DBT *tkey, char *name )
|
|
|
|
{
|
|
|
|
char *u = strpbrk( name, ":," );
|
|
|
|
tkey->data = name;
|
|
|
|
tkey->size = u ? (size_t)(u - name) : strlen( name );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
maildir_set_uid( maildir_store_t *ctx, const char *name, int *uid )
|
|
|
|
{
|
|
|
|
int ret, uv[2];
|
|
|
|
|
|
|
|
if (uid)
|
|
|
|
*uid = ++ctx->nuid;
|
|
|
|
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 ))) {
|
|
|
|
tbork:
|
|
|
|
ctx->db->err( ctx->db, ret, "Maildir error: db->put()" );
|
|
|
|
return DRV_BOX_BAD;
|
|
|
|
}
|
|
|
|
if (uid) {
|
|
|
|
make_key( &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 ))) {
|
|
|
|
ctx->db->err( ctx->db, ret, "Maildir error: db->sync()" );
|
2011-05-22 15:53:28 +00:00
|
|
|
return DRV_BOX_BAD;
|
2004-05-23 16:39:24 +00:00
|
|
|
}
|
|
|
|
return DRV_OK;
|
|
|
|
}
|
|
|
|
#endif /* USE_DB */
|
|
|
|
|
|
|
|
static int
|
|
|
|
maildir_store_uid( maildir_store_t *ctx )
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
char buf[128];
|
|
|
|
|
|
|
|
n = sprintf( buf, "%d\n%d\n", ctx->gen.uidvalidity, ctx->nuid );
|
|
|
|
lseek( ctx->uvfd, 0, SEEK_SET );
|
2012-09-15 13:15:22 +00:00
|
|
|
if (write( ctx->uvfd, buf, n ) != n || ftruncate( ctx->uvfd, n ) || (FSyncLevel >= FSYNC_NORMAL && fdatasync( ctx->uvfd ))) {
|
2006-03-19 11:29:12 +00:00
|
|
|
error( "Maildir error: cannot write UIDVALIDITY.\n" );
|
2004-05-23 16:39:24 +00:00
|
|
|
return DRV_BOX_BAD;
|
|
|
|
}
|
|
|
|
return DRV_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-04-10 13:32:25 +00:00
|
|
|
maildir_init_uid( 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
|
|
|
return maildir_set_uid( ctx, 0, 0 );
|
|
|
|
}
|
|
|
|
#endif /* USE_DB */
|
|
|
|
return maildir_store_uid( ctx );
|
|
|
|
}
|
|
|
|
|
2011-04-10 13:32:25 +00:00
|
|
|
static int
|
|
|
|
maildir_init_uid_new( maildir_store_t *ctx )
|
|
|
|
{
|
|
|
|
info( "Maildir notice: no UIDVALIDITY, creating new.\n" );
|
|
|
|
return maildir_init_uid( ctx );
|
|
|
|
}
|
|
|
|
|
2004-03-27 16:07:20 +00:00
|
|
|
static int
|
|
|
|
maildir_uidval_lock( maildir_store_t *ctx )
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
char buf[128];
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
lseek( ctx->uvfd, 0, SEEK_SET );
|
|
|
|
if ((n = read( ctx->uvfd, buf, sizeof(buf) )) <= 0 ||
|
|
|
|
(buf[n] = 0, sscanf( buf, "%d\n%d", &ctx->gen.uidvalidity, &ctx->nuid ) != 2)) {
|
2011-05-03 07:42:27 +00:00
|
|
|
#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
|
2011-04-10 13:32:25 +00:00
|
|
|
return maildir_init_uid_new( ctx );
|
2004-03-27 16:07:20 +00:00
|
|
|
} else
|
|
|
|
ctx->uvok = 1;
|
|
|
|
return DRV_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
maildir_uidval_unlock( maildir_store_t *ctx )
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
maildir_obtain_uid( maildir_store_t *ctx, int *uid )
|
|
|
|
{
|
|
|
|
*uid = ++ctx->nuid;
|
2004-05-23 16:39:24 +00:00
|
|
|
return maildir_store_uid( ctx );
|
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 )
|
|
|
|
{
|
|
|
|
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];
|
|
|
|
|
|
|
|
#ifdef USE_DB
|
|
|
|
if (!ctx->db)
|
|
|
|
#endif /* USE_DB */
|
|
|
|
if ((ret = maildir_uidval_lock( ctx )) != DRV_OK)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
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
|
|
|
|
if (ctx->db) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (st.st_mtime == now && !(DFlags & ZERODELAY)) {
|
|
|
|
/* 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. */
|
|
|
|
info( "Maildir notice: sleeping due to recent directory modification.\n" );
|
|
|
|
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
|
|
|
|
if (ctx->db)
|
|
|
|
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
|
|
|
|
if (ctx->db) {
|
|
|
|
make_key( &key, e->d_name );
|
|
|
|
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 );
|
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
|
|
|
|
if (ctx->db)
|
|
|
|
tdb->close( tdb, 0 );
|
|
|
|
#endif /* USE_DB */
|
|
|
|
maildir_free_scan( msglist );
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
#ifdef USE_DB
|
|
|
|
if (ctx->db) {
|
|
|
|
if ((ret = ctx->db->cursor( ctx->db, 0, &dbc, 0 )))
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if ((key.size != 11 || memcmp( key.data, "UIDVALIDITY", 11 )) &&
|
|
|
|
(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
|
2011-04-10 13:32:25 +00:00
|
|
|
info( "Maildir notice: duplicate UID; changing UIDVALIDITY.\n");
|
|
|
|
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;
|
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
|
|
|
|
} else if (ctx->db) {
|
|
|
|
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=" )))
|
|
|
|
for (ru = u + 3; isdigit( (unsigned char)*ru ); ru++);
|
|
|
|
else
|
|
|
|
u = ru = strchr( entry->base, ':' );
|
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;
|
|
|
|
if (!memcmp( nbuf, "X-TUID: ", 8 ) && nbuf[8 + TUIDL] == '\n') {
|
|
|
|
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
|
|
|
}
|
|
|
|
#ifdef USE_DB
|
|
|
|
if (!ctx->db)
|
|
|
|
#endif /* ! USE_DB */
|
|
|
|
maildir_uidval_unlock( ctx );
|
|
|
|
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;
|
|
|
|
msg->gen.flags = maildir_parse_flags( msg->base );
|
|
|
|
} 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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-07-23 14:06:32 +00:00
|
|
|
maildir_select( store_t *gctx, int create,
|
|
|
|
void (*cb)( int sts, void *aux ), void *aux )
|
2004-03-27 16:07:20 +00:00
|
|
|
{
|
|
|
|
maildir_store_t *ctx = (maildir_store_t *)gctx;
|
2011-07-23 14:06:32 +00:00
|
|
|
int ret;
|
2011-07-25 07:25:09 +00:00
|
|
|
#ifdef USE_DB
|
|
|
|
struct stat st;
|
|
|
|
#endif /* USE_DB */
|
2011-07-23 14:06:32 +00:00
|
|
|
char uvpath[_POSIX_PATH_MAX];
|
2004-03-27 16:07:20 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
#endif /* USE_DB */
|
2012-08-11 16:34:46 +00:00
|
|
|
gctx->path =
|
|
|
|
(!memcmp( gctx->name, "INBOX", 5 ) && (!gctx->name[5] || gctx->name[5] == '/')) ?
|
|
|
|
maildir_join_path( ((maildir_store_conf_t *)gctx->conf)->inbox, gctx->name + 5 ) :
|
|
|
|
maildir_join_path( gctx->conf->path, gctx->name );
|
2004-03-27 16:07:20 +00:00
|
|
|
|
2012-08-11 15:11:57 +00:00
|
|
|
if ((ret = maildir_validate( gctx->path, create, ctx )) != DRV_OK) {
|
2012-07-29 21:14:48 +00:00
|
|
|
cb( ret, aux );
|
|
|
|
return;
|
|
|
|
}
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
dbok:
|
|
|
|
#if SEEK_SET != 0
|
|
|
|
lck.l_whence = SEEK_SET;
|
|
|
|
#endif
|
|
|
|
lck.l_type = F_WRLCK;
|
|
|
|
if (fcntl( ctx->uvfd, F_SETLKW, &lck )) {
|
2011-04-10 13:32:25 +00:00
|
|
|
sys_error( "Maildir error: cannot lock %s", uvpath );
|
2012-07-29 21:14:48 +00:00
|
|
|
cb( DRV_BOX_BAD, aux );
|
|
|
|
return;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
2011-07-25 07:25:09 +00:00
|
|
|
if (fstat( ctx->uvfd, &st )) {
|
|
|
|
sys_error( "Maildir error: cannot stat %s", uvpath );
|
|
|
|
cb( DRV_BOX_BAD, aux );
|
|
|
|
return;
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
if (db_create( &ctx->db, 0, 0 )) {
|
|
|
|
fputs( "Maildir error: db_create() failed\n", stderr );
|
2011-05-22 15:53:28 +00:00
|
|
|
cb( DRV_BOX_BAD, aux );
|
|
|
|
return;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
2011-07-25 07:25:09 +00:00
|
|
|
if ((ret = (ctx->db->open)( ctx->db, 0, uvpath, 0, DB_HASH,
|
|
|
|
st.st_size ? 0 : DB_CREATE | DB_TRUNCATE, 0 ))) {
|
2004-03-27 16:07:20 +00:00
|
|
|
ctx->db->err( ctx->db, ret, "Maildir error: db->open(%s)", uvpath );
|
2011-05-22 15:53:28 +00:00
|
|
|
cb( DRV_BOX_BAD, aux );
|
|
|
|
return;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
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()" );
|
2011-05-22 15:53:28 +00:00
|
|
|
cb( DRV_BOX_BAD, aux );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (maildir_init_uid_new( ctx ) != DRV_OK) {
|
|
|
|
cb( DRV_BOX_BAD, aux );
|
|
|
|
return;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ctx->gen.uidvalidity = ((int *)value.data)[0];
|
|
|
|
ctx->nuid = ((int *)value.data)[1];
|
|
|
|
ctx->uvok = 1;
|
|
|
|
}
|
2011-07-23 14:06:32 +00:00
|
|
|
cb( DRV_OK, aux );
|
|
|
|
return;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
fnok:
|
|
|
|
#endif /* USE_DB */
|
2011-07-23 14:06:32 +00:00
|
|
|
if ((ret = maildir_uidval_lock( ctx )) != DRV_OK) {
|
|
|
|
cb( ret, aux );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
maildir_uidval_unlock( ctx );
|
|
|
|
|
|
|
|
cb( DRV_OK, aux );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
maildir_prepare_opts( store_t *gctx, int opts )
|
|
|
|
{
|
|
|
|
if (opts & OPEN_SETFLAGS)
|
|
|
|
opts |= OPEN_OLD;
|
|
|
|
if (opts & OPEN_EXPUNGE)
|
|
|
|
opts |= OPEN_OLD|OPEN_NEW|OPEN_FLAGS;
|
|
|
|
gctx->opts = opts;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-04-10 11:06:07 +00:00
|
|
|
maildir_load( store_t *gctx, int minuid, int maxuid, int newuid, int *excs, int nexcs,
|
2011-07-23 14:06:32 +00:00
|
|
|
void (*cb)( int sts, void *aux ), void *aux )
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (maildir_scan( ctx, &msglist ) != DRV_OK)
|
|
|
|
return DRV_BOX_BAD;
|
|
|
|
ctx->gen.recent = 0;
|
|
|
|
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;
|
|
|
|
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))
|
|
|
|
data->flags = maildir_parse_flags( msg->base );
|
2012-07-29 21:14:48 +00:00
|
|
|
cb( DRV_OK, aux );
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
maildir_make_flags( int flags, char *buf )
|
|
|
|
{
|
|
|
|
unsigned i, d;
|
|
|
|
|
|
|
|
buf[0] = ':';
|
|
|
|
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
|
|
|
|
if (ctx->db) {
|
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 */
|
|
|
|
{
|
|
|
|
if ((ret = maildir_uidval_lock( ctx )) != DRV_OK ||
|
2012-07-29 21:14:48 +00:00
|
|
|
(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;
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
maildir_uidval_unlock( ctx );
|
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 {
|
2012-08-11 15:11:57 +00:00
|
|
|
box = ctx->trash;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
maildir_make_flags( 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 );
|
2012-09-15 13:15:22 +00:00
|
|
|
if (ret != data->len || ((FSyncLevel >= FSYNC_NORMAL) && (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
|
|
|
}
|
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
|
2011-04-10 11:06:07 +00:00
|
|
|
maildir_find_new_msgs( store_t *gctx ATTR_UNUSED,
|
|
|
|
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
|
2006-03-21 20:03:21 +00:00
|
|
|
maildir_set_flags( store_t *gctx, message_t *gmsg, int uid, int add, int del,
|
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, *p;
|
|
|
|
unsigned i;
|
|
|
|
int j, ret, ol, fl, bbl, bl, tl;
|
|
|
|
char buf[_POSIX_PATH_MAX], nbuf[_POSIX_PATH_MAX];
|
|
|
|
|
|
|
|
(void) uid;
|
|
|
|
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 );
|
2004-03-27 16:07:20 +00:00
|
|
|
if ((s = strstr( nbuf + bl, ":2," ))) {
|
|
|
|
s += 3;
|
|
|
|
fl = ol - (s - (nbuf + bl));
|
|
|
|
for (i = 0; i < as(Flags); i++) {
|
|
|
|
if ((p = strchr( s, Flags[i] ))) {
|
|
|
|
if (del & (1 << i)) {
|
|
|
|
memcpy( p, p + 1, fl - (p - s) );
|
|
|
|
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 {
|
|
|
|
tl = ol + maildir_make_flags( msg->gen.flags, nbuf + bl + ol );
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
|
|
|
make_key( &key, (char *)name );
|
|
|
|
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 );
|
|
|
|
s = strstr( msg->base, ":2," );
|
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
|
2012-07-29 21:14:48 +00:00
|
|
|
if (ctx->db) {
|
|
|
|
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
|
2006-03-21 20:03:21 +00:00
|
|
|
maildir_close( store_t *gctx,
|
2012-07-29 21:14:48 +00:00
|
|
|
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
|
|
|
|
maildir_cancel( store_t *gctx,
|
2012-07-22 15:32:32 +00:00
|
|
|
void (*cb)( void *aux ), void *aux )
|
2006-03-21 20:03:21 +00:00
|
|
|
{
|
|
|
|
(void)gctx;
|
2012-07-22 15:32:32 +00:00
|
|
|
cb( aux );
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
maildir_commit( store_t *gctx )
|
2004-03-27 16:07:20 +00:00
|
|
|
{
|
|
|
|
(void) gctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) );
|
|
|
|
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 */
|
|
|
|
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" );
|
|
|
|
*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_own_store,
|
|
|
|
maildir_disown_store, /* _cancel_, but it's the same */
|
2004-03-27 16:07:20 +00:00
|
|
|
maildir_list,
|
2006-01-29 11:22:45 +00:00
|
|
|
maildir_prepare_opts,
|
2004-03-27 16:07:20 +00:00
|
|
|
maildir_select,
|
2011-07-23 14:06:32 +00:00
|
|
|
maildir_load,
|
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,
|
2004-03-27 16:07:20 +00:00
|
|
|
maildir_set_flags,
|
|
|
|
maildir_trash_msg,
|
2006-03-21 20:03:21 +00:00
|
|
|
maildir_close,
|
|
|
|
maildir_cancel,
|
|
|
|
maildir_commit,
|
2004-03-27 16:07:20 +00:00
|
|
|
};
|