2004-03-27 16:07:20 +00:00
|
|
|
/*
|
|
|
|
* mbsync - mailbox synchronizer
|
2002-12-28 15:31:20 +00:00
|
|
|
* Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
|
2004-03-27 16:07:20 +00:00
|
|
|
* Copyright (C) 2002-2004 Oswald Buddenhagen <ossi@users.sf.net>
|
2000-12-20 21:41:21 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2002-10-30 02:31:20 +00:00
|
|
|
*
|
2004-03-27 16:07:20 +00:00
|
|
|
* As a special exception, mbsync may be linked with the OpenSSL library,
|
2002-10-30 02:31:20 +00:00
|
|
|
* despite that library's more restrictive license.
|
2000-12-20 21:41:21 +00:00
|
|
|
*/
|
|
|
|
|
2003-05-07 00:06:37 +00:00
|
|
|
#include "isync.h"
|
|
|
|
|
2000-12-20 21:41:21 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
2000-12-21 18:16:44 +00:00
|
|
|
#include <errno.h>
|
2001-01-11 10:13:47 +00:00
|
|
|
#include <sys/stat.h>
|
2000-12-20 21:41:21 +00:00
|
|
|
|
2005-12-28 10:02:22 +00:00
|
|
|
static const char *str_ms[] = { "master", "slave" }, *str_hl[] = { "push", "pull" };
|
|
|
|
|
2004-11-13 09:19:36 +00:00
|
|
|
void
|
|
|
|
Fprintf( FILE *f, const char *msg, ... )
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
va_start( va, msg );
|
|
|
|
r = vfprintf( f, msg, va );
|
|
|
|
va_end( va );
|
|
|
|
if (r < 0) {
|
|
|
|
perror( "cannot write file" );
|
|
|
|
exit( 1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-27 16:07:20 +00:00
|
|
|
static const char Flags[] = { 'D', 'F', 'R', 'S', 'T' };
|
2000-12-20 21:41:21 +00:00
|
|
|
|
2004-03-27 16:07:20 +00:00
|
|
|
static int
|
|
|
|
parse_flags( const char *buf )
|
2000-12-20 21:41:21 +00:00
|
|
|
{
|
2004-03-27 16:07:20 +00:00
|
|
|
unsigned flags, i, d;
|
|
|
|
|
|
|
|
for (flags = i = d = 0; i < as(Flags); i++)
|
|
|
|
if (buf[d] == Flags[i]) {
|
|
|
|
flags |= (1 << i);
|
|
|
|
d++;
|
|
|
|
}
|
|
|
|
return flags;
|
2000-12-20 21:41:21 +00:00
|
|
|
}
|
|
|
|
|
2002-01-16 21:43:58 +00:00
|
|
|
static int
|
2004-03-27 16:07:20 +00:00
|
|
|
make_flags( int flags, char *buf )
|
2002-01-16 19:47:28 +00:00
|
|
|
{
|
2004-03-27 16:07:20 +00:00
|
|
|
unsigned i, d;
|
|
|
|
|
|
|
|
for (i = d = 0; i < as(Flags); i++)
|
|
|
|
if (flags & (1 << i))
|
|
|
|
buf[d++] = Flags[i];
|
|
|
|
buf[d] = 0;
|
|
|
|
return d;
|
2002-01-16 19:47:28 +00:00
|
|
|
}
|
|
|
|
|
2004-03-27 16:07:20 +00:00
|
|
|
#define S_DEAD (1<<0)
|
|
|
|
#define S_EXPIRED (1<<1)
|
2005-12-28 10:02:22 +00:00
|
|
|
#define S_DEL(ms) (1<<(2+(ms)))
|
|
|
|
#define S_EXP_S (1<<4)
|
2004-03-27 16:07:20 +00:00
|
|
|
|
|
|
|
typedef struct sync_rec {
|
|
|
|
struct sync_rec *next;
|
|
|
|
/* string_list_t *keywords; */
|
2005-12-28 10:02:22 +00:00
|
|
|
int uid[2];
|
2005-12-28 19:17:40 +00:00
|
|
|
message_t *msg[2];
|
2004-03-27 16:07:20 +00:00
|
|
|
unsigned char flags, status;
|
|
|
|
} sync_rec_t;
|
|
|
|
|
2005-12-28 19:17:40 +00:00
|
|
|
static void
|
|
|
|
findmsgs( sync_rec_t *srecs, store_t *ctx[], int t )
|
|
|
|
{
|
|
|
|
sync_rec_t *srec, *nsrec = 0;
|
|
|
|
message_t *msg;
|
|
|
|
const char *diag;
|
|
|
|
int uid;
|
|
|
|
char fbuf[16]; /* enlarge when support for keywords is added */
|
|
|
|
|
|
|
|
for (msg = ctx[t]->msgs; msg; msg = msg->next) {
|
|
|
|
uid = msg->uid;
|
|
|
|
if (Debug) {
|
|
|
|
make_flags( msg->flags, fbuf );
|
|
|
|
printf( ctx[t]->opts & OPEN_SIZE ? " message %5d, %-4s, %6d: " : " message %5d, %-4s: ", uid, fbuf, msg->size );
|
|
|
|
}
|
|
|
|
for (srec = nsrec; srec; srec = srec->next) {
|
|
|
|
if (srec->status & S_DEAD)
|
|
|
|
continue;
|
|
|
|
if (srec->uid[t] == uid) {
|
|
|
|
diag = srec == nsrec ? "adjacently" : "after gap";
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (srec = srecs; srec != nsrec; srec = srec->next) {
|
|
|
|
if (srec->status & S_DEAD)
|
|
|
|
continue;
|
|
|
|
if (srec->uid[t] == uid) {
|
|
|
|
diag = "after reset";
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug( "new\n" );
|
|
|
|
continue;
|
|
|
|
found:
|
|
|
|
msg->status |= M_PROCESSED;
|
|
|
|
srec->msg[t] = msg;
|
|
|
|
nsrec = srec->next;
|
|
|
|
debug( "pairs %5d %s\n", srec->uid[1-t], diag );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-27 16:07:20 +00:00
|
|
|
|
|
|
|
/* cases:
|
|
|
|
a) both non-null
|
|
|
|
b) only master null
|
2005-12-28 10:02:22 +00:00
|
|
|
b.1) uid[M] 0
|
|
|
|
b.2) uid[M] -1
|
2004-03-27 16:07:20 +00:00
|
|
|
b.3) master not scanned
|
|
|
|
b.4) master gone
|
|
|
|
c) only slave null
|
2005-12-28 10:02:22 +00:00
|
|
|
c.1) uid[S] 0
|
|
|
|
c.2) uid[S] -1
|
2004-03-27 16:07:20 +00:00
|
|
|
c.3) slave not scanned
|
|
|
|
c.4) slave gone
|
|
|
|
d) both null
|
|
|
|
d.1) both gone
|
2005-12-28 10:02:22 +00:00
|
|
|
d.2) uid[M] 0, slave not scanned
|
|
|
|
d.3) uid[M] -1, slave not scanned
|
2004-03-27 16:07:20 +00:00
|
|
|
d.4) master gone, slave not scanned
|
2005-12-28 10:02:22 +00:00
|
|
|
d.5) uid[M] 0, slave gone
|
|
|
|
d.6) uid[M] -1, slave gone
|
|
|
|
d.7) uid[S] 0, master not scanned
|
|
|
|
d.8) uid[S] -1, master not scanned
|
2004-03-27 16:07:20 +00:00
|
|
|
d.9) slave gone, master not scanned
|
2005-12-28 10:02:22 +00:00
|
|
|
d.10) uid[S] 0, master gone
|
|
|
|
d.11) uid[S] -1, master gone
|
|
|
|
impossible cases: both uid[M] & uid[S] 0 or -1, both not scanned
|
2004-03-27 16:07:20 +00:00
|
|
|
*/
|
2001-01-11 10:13:47 +00:00
|
|
|
|
2004-03-27 16:07:20 +00:00
|
|
|
static char *
|
|
|
|
clean_strdup( const char *s )
|
|
|
|
{
|
|
|
|
char *cs;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
cs = nfstrdup( s );
|
|
|
|
for (i = 0; cs[i]; i++)
|
|
|
|
if (cs[i] == '/')
|
|
|
|
cs[i] = '!';
|
|
|
|
return cs;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-12-28 10:02:22 +00:00
|
|
|
sync_boxes( store_t *ctx[], const char *names[], channel_conf_t *chan )
|
2004-03-27 16:07:20 +00:00
|
|
|
{
|
2005-12-28 10:02:22 +00:00
|
|
|
driver_t *driver[2];
|
2005-12-28 19:17:40 +00:00
|
|
|
message_t *tmsg;
|
2004-03-27 16:07:20 +00:00
|
|
|
sync_rec_t *recs, *srec, **srecadd, *nsrec;
|
|
|
|
char *dname, *jname, *nname, *lname, *s, *cmname, *csname;
|
|
|
|
FILE *dfp, *jfp, *nfp;
|
2005-12-28 10:02:22 +00:00
|
|
|
int opts[2];
|
|
|
|
int nom, nos, del[2], ex[M];
|
|
|
|
int muidval, suidval, smaxxuid, maxuid[2], minwuid, maxwuid;
|
|
|
|
int t1, t2, t3, t;
|
|
|
|
int lfd, ret, line, todel, delt, *mexcs, nmexcs, rmexcs;
|
2004-03-27 16:07:20 +00:00
|
|
|
unsigned char nflags;
|
2005-12-28 10:02:22 +00:00
|
|
|
msg_data_t msgdata;
|
2004-03-27 16:07:20 +00:00
|
|
|
struct stat st;
|
|
|
|
struct flock lck;
|
|
|
|
char fbuf[16]; /* enlarge when support for keywords is added */
|
|
|
|
char buf[64];
|
|
|
|
|
|
|
|
ret = SYNC_OK;
|
|
|
|
recs = 0, srecadd = &recs;
|
|
|
|
|
2005-12-28 10:02:22 +00:00
|
|
|
opts[M] = opts[S] = 0;
|
|
|
|
for (t = 0; t < 2; t++) {
|
|
|
|
if (chan->ops[t] & (OP_DELETE|OP_FLAGS)) {
|
|
|
|
opts[t] |= OPEN_SETFLAGS;
|
|
|
|
opts[1-t] |= OPEN_OLD;
|
|
|
|
if (chan->ops[t] & OP_FLAGS)
|
|
|
|
opts[1-t] |= OPEN_FLAGS;
|
|
|
|
}
|
|
|
|
if (chan->ops[t] & (OP_NEW|OP_RENEW)) {
|
|
|
|
opts[t] |= OPEN_APPEND;
|
|
|
|
if (chan->ops[t] & OP_RENEW)
|
|
|
|
opts[1-t] |= OPEN_OLD;
|
|
|
|
if (chan->ops[t] & OP_NEW)
|
|
|
|
opts[1-t] |= OPEN_NEW;
|
|
|
|
if (chan->ops[t] & OP_EXPUNGE)
|
|
|
|
opts[1-t] |= OPEN_FLAGS;
|
|
|
|
if (chan->stores[t]->max_size)
|
|
|
|
opts[1-t] |= OPEN_SIZE;
|
|
|
|
}
|
|
|
|
if (chan->ops[t] & OP_EXPUNGE) {
|
|
|
|
opts[t] |= OPEN_EXPUNGE;
|
|
|
|
if (chan->stores[t]->trash) {
|
|
|
|
if (!chan->stores[t]->trash_only_new)
|
|
|
|
opts[t] |= OPEN_OLD;
|
|
|
|
opts[t] |= OPEN_NEW|OPEN_FLAGS;
|
|
|
|
} else if (chan->stores[1-t]->trash && chan->stores[1-t]->trash_remote_new)
|
|
|
|
opts[t] |= OPEN_NEW|OPEN_FLAGS;
|
|
|
|
}
|
|
|
|
if (chan->ops[t] & OP_CREATE)
|
|
|
|
opts[t] |= OPEN_CREATE;
|
|
|
|
}
|
|
|
|
if ((chan->ops[S] & (OP_NEW|OP_RENEW)) && chan->max_messages)
|
|
|
|
opts[S] |= OPEN_OLD|OPEN_NEW|OPEN_FLAGS;
|
|
|
|
|
|
|
|
for (t = 0; t < 2; t++) {
|
|
|
|
ctx[t]->name =
|
|
|
|
(!names[t] || (ctx[t]->conf->map_inbox && !strcmp( ctx[t]->conf->map_inbox, names[t] ))) ?
|
|
|
|
"INBOX" : names[t];
|
|
|
|
ctx[t]->uidvalidity = 0;
|
|
|
|
driver[t] = ctx[t]->conf->driver;
|
|
|
|
driver[t]->prepare( ctx[t], opts[t] );
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
|
|
|
|
if (!strcmp( chan->sync_state ? chan->sync_state : global_sync_state, "*" )) {
|
2005-12-28 10:02:22 +00:00
|
|
|
if (!ctx[S]->path) {
|
|
|
|
fprintf( stderr, "Error: store '%s' does not support in-box sync state\n", chan->stores[S]->name );
|
|
|
|
return SYNC_BAD(S);
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
2005-12-28 10:02:22 +00:00
|
|
|
nfasprintf( &dname, "%s/." EXE "state", ctx[S]->path );
|
2004-03-27 16:07:20 +00:00
|
|
|
} else {
|
2005-12-28 10:02:22 +00:00
|
|
|
csname = clean_strdup( ctx[S]->name );
|
2004-03-27 16:07:20 +00:00
|
|
|
if (chan->sync_state)
|
|
|
|
nfasprintf( &dname, "%s%s", chan->sync_state, csname );
|
|
|
|
else {
|
2005-12-28 10:02:22 +00:00
|
|
|
cmname = clean_strdup( ctx[M]->name );
|
2004-03-27 16:07:20 +00:00
|
|
|
nfasprintf( &dname, "%s:%s:%s_:%s:%s", global_sync_state,
|
2005-12-28 10:02:22 +00:00
|
|
|
chan->stores[M]->name, cmname, chan->stores[S]->name, csname );
|
2004-03-27 16:07:20 +00:00
|
|
|
free( cmname );
|
|
|
|
}
|
|
|
|
free( csname );
|
|
|
|
}
|
|
|
|
nfasprintf( &jname, "%s.journal", dname );
|
|
|
|
nfasprintf( &nname, "%s.new", dname );
|
|
|
|
nfasprintf( &lname, "%s.lock", dname );
|
2005-12-28 10:02:22 +00:00
|
|
|
muidval = suidval = smaxxuid = maxuid[M] = maxuid[S] = 0;
|
2004-03-27 16:07:20 +00:00
|
|
|
memset( &lck, 0, sizeof(lck) );
|
|
|
|
#if SEEK_SET != 0
|
|
|
|
lck.l_whence = SEEK_SET;
|
|
|
|
#endif
|
|
|
|
#if F_WRLCK != 0
|
|
|
|
lck.l_type = F_WRLCK;
|
|
|
|
#endif
|
|
|
|
if ((lfd = open( lname, O_WRONLY|O_CREAT, 0666 )) < 0) {
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
lferr:
|
|
|
|
fprintf( stderr, "Error: cannot create lock file %s: %s\n", lname, strerror(errno) );
|
|
|
|
ret = SYNC_FAIL;
|
|
|
|
goto bail2;
|
|
|
|
}
|
|
|
|
goto skiprd;
|
|
|
|
}
|
|
|
|
if (fcntl( lfd, F_SETLK, &lck )) {
|
|
|
|
lckerr:
|
|
|
|
fprintf( stderr, "Error: channel :%s:%s-:%s:%s is locked\n",
|
2005-12-28 10:02:22 +00:00
|
|
|
chan->stores[M]->name, ctx[M]->name, chan->stores[S]->name, ctx[S]->name );
|
2004-03-27 16:07:20 +00:00
|
|
|
ret = SYNC_FAIL;
|
|
|
|
goto bail1;
|
|
|
|
}
|
|
|
|
if ((dfp = fopen( dname, "r" ))) {
|
|
|
|
debug( "reading sync state %s ...\n", dname );
|
2005-12-28 10:02:22 +00:00
|
|
|
if (!fgets( buf, sizeof(buf), dfp ) || !(t = strlen( buf )) || buf[t - 1] != '\n') {
|
2004-11-13 09:19:36 +00:00
|
|
|
fprintf( stderr, "Error: incomplete sync state header in %s\n", dname );
|
|
|
|
fclose( dfp );
|
|
|
|
ret = SYNC_FAIL;
|
|
|
|
goto bail;
|
|
|
|
}
|
2005-12-28 10:02:22 +00:00
|
|
|
if (sscanf( buf, "%d:%d %d:%d:%d", &muidval, &maxuid[M], &suidval, &smaxxuid, &maxuid[S]) != 5) {
|
2004-03-27 16:07:20 +00:00
|
|
|
fprintf( stderr, "Error: invalid sync state header in %s\n", dname );
|
|
|
|
fclose( dfp );
|
|
|
|
ret = SYNC_FAIL;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
line = 1;
|
|
|
|
while (fgets( buf, sizeof(buf), dfp )) {
|
|
|
|
line++;
|
2005-12-28 10:02:22 +00:00
|
|
|
if (!(t = strlen( buf )) || buf[t - 1] != '\n') {
|
2004-11-13 09:19:36 +00:00
|
|
|
fprintf( stderr, "Error: incomplete sync state entry at %s:%d\n", dname, line );
|
|
|
|
fclose( dfp );
|
|
|
|
ret = SYNC_FAIL;
|
|
|
|
goto bail;
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
fbuf[0] = 0;
|
2004-11-13 09:19:36 +00:00
|
|
|
if (sscanf( buf, "%d %d %15s", &t1, &t2, fbuf ) < 2) {
|
2004-03-27 16:07:20 +00:00
|
|
|
fprintf( stderr, "Error: invalid sync state entry at %s:%d\n", dname, line );
|
|
|
|
fclose( dfp );
|
|
|
|
ret = SYNC_FAIL;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
srec = nfmalloc( sizeof(*srec) );
|
2005-12-28 10:02:22 +00:00
|
|
|
srec->uid[M] = t1;
|
|
|
|
srec->uid[S] = t2;
|
2004-03-27 16:07:20 +00:00
|
|
|
s = fbuf;
|
|
|
|
if (*s == 'X') {
|
|
|
|
s++;
|
|
|
|
srec->status = S_EXPIRED;
|
|
|
|
} else
|
|
|
|
srec->status = 0;
|
|
|
|
srec->flags = parse_flags( s );
|
2005-12-28 10:02:22 +00:00
|
|
|
debug( " entry (%d,%d,%u,%s)\n", srec->uid[M], srec->uid[S], srec->flags, srec->status & S_EXPIRED ? "X" : "" );
|
2005-12-28 19:17:40 +00:00
|
|
|
srec->msg[M] = srec->msg[S] = 0;
|
2004-03-27 16:07:20 +00:00
|
|
|
srec->next = 0;
|
|
|
|
*srecadd = srec;
|
|
|
|
srecadd = &srec->next;
|
|
|
|
}
|
|
|
|
fclose( dfp );
|
|
|
|
} else {
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
fprintf( stderr, "Error: cannot read sync state %s\n", dname );
|
|
|
|
ret = SYNC_FAIL;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((jfp = fopen( jname, "r" ))) {
|
|
|
|
if (!stat( nname, &st )) {
|
|
|
|
debug( "recovering journal ...\n" );
|
|
|
|
line = 0;
|
|
|
|
srec = recs;
|
|
|
|
while (fgets( buf, sizeof(buf), jfp )) {
|
|
|
|
line++;
|
2005-12-28 10:02:22 +00:00
|
|
|
if (!(t = strlen( buf )) || buf[t - 1] != '\n') {
|
2004-11-13 09:19:36 +00:00
|
|
|
fprintf( stderr, "Error: incomplete journal entry at %s:%d\n", jname, line );
|
|
|
|
fclose( jfp );
|
|
|
|
ret = SYNC_FAIL;
|
|
|
|
goto bail;
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
if (buf[0] == '^')
|
|
|
|
srec = recs;
|
|
|
|
else {
|
|
|
|
if (buf[0] == '(' || buf[0] == ')' ?
|
2004-11-13 09:19:36 +00:00
|
|
|
(sscanf( buf + 2, "%d", &t1 ) != 1) :
|
2004-03-27 16:07:20 +00:00
|
|
|
buf[0] == '-' || buf[0] == '|' ?
|
2004-11-13 09:19:36 +00:00
|
|
|
(sscanf( buf + 2, "%d %d", &t1, &t2 ) != 2) :
|
|
|
|
(sscanf( buf + 2, "%d %d %d", &t1, &t2, &t3 ) != 3))
|
2004-03-27 16:07:20 +00:00
|
|
|
{
|
|
|
|
fprintf( stderr, "Error: malformed journal entry at %s:%d\n", jname, line );
|
|
|
|
fclose( jfp );
|
|
|
|
ret = SYNC_FAIL;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
if (buf[0] == '(')
|
2005-12-28 10:02:22 +00:00
|
|
|
maxuid[M] = t1;
|
2004-03-27 16:07:20 +00:00
|
|
|
else if (buf[0] == ')')
|
2005-12-28 10:02:22 +00:00
|
|
|
maxuid[S] = t1;
|
2004-03-27 16:07:20 +00:00
|
|
|
else if (buf[0] == '|') {
|
|
|
|
muidval = t1;
|
|
|
|
suidval = t2;
|
|
|
|
} else if (buf[0] == '+') {
|
|
|
|
srec = nfmalloc( sizeof(*srec) );
|
2005-12-28 10:02:22 +00:00
|
|
|
srec->uid[M] = t1;
|
|
|
|
srec->uid[S] = t2;
|
2004-03-27 16:07:20 +00:00
|
|
|
srec->flags = t3;
|
|
|
|
debug( " new entry(%d,%d,%u)\n", t1, t2, t3 );
|
2005-12-28 19:17:40 +00:00
|
|
|
srec->msg[M] = srec->msg[S] = 0;
|
2004-03-27 16:07:20 +00:00
|
|
|
srec->status = 0;
|
|
|
|
srec->next = 0;
|
|
|
|
*srecadd = srec;
|
|
|
|
srecadd = &srec->next;
|
|
|
|
} else {
|
|
|
|
for (; srec; srec = srec->next)
|
2005-12-28 10:02:22 +00:00
|
|
|
if (srec->uid[M] == t1 && srec->uid[S] == t2)
|
2004-03-27 16:07:20 +00:00
|
|
|
goto syncfnd;
|
|
|
|
fprintf( stderr, "Error: journal entry at %s:%d refers to non-existing sync state entry\n", jname, line );
|
|
|
|
fclose( jfp );
|
|
|
|
ret = SYNC_FAIL;
|
|
|
|
goto bail;
|
|
|
|
syncfnd:
|
2005-12-28 10:02:22 +00:00
|
|
|
debug( " entry(%d,%d,%u) ", srec->uid[M], srec->uid[S], srec->flags );
|
2004-03-27 16:07:20 +00:00
|
|
|
switch (buf[0]) {
|
|
|
|
case '-':
|
|
|
|
debug( "killed\n" );
|
|
|
|
srec->status = S_DEAD;
|
|
|
|
break;
|
|
|
|
case '<':
|
|
|
|
debug( "master now %d\n", t3 );
|
2005-12-28 10:02:22 +00:00
|
|
|
srec->uid[M] = t3;
|
2004-03-27 16:07:20 +00:00
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
debug( "slave now %d\n", t3 );
|
2005-12-28 10:02:22 +00:00
|
|
|
srec->uid[S] = t3;
|
2004-03-27 16:07:20 +00:00
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
debug( "flags now %d\n", t3 );
|
|
|
|
srec->flags = t3;
|
|
|
|
break;
|
|
|
|
case '~':
|
|
|
|
debug( "expired now %d\n", t3 );
|
|
|
|
if (t3) {
|
|
|
|
if (smaxxuid < t2)
|
|
|
|
smaxxuid = t2;
|
|
|
|
srec->status |= S_EXPIRED;
|
|
|
|
} else
|
|
|
|
srec->status &= ~S_EXPIRED;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf( stderr, "Error: unrecognized journal entry at %s:%d\n", jname, line );
|
|
|
|
fclose( jfp );
|
|
|
|
ret = SYNC_FAIL;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose( jfp );
|
|
|
|
} else {
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
fprintf( stderr, "Error: cannot read journal %s\n", jname );
|
|
|
|
ret = SYNC_FAIL;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
skiprd:
|
|
|
|
|
2005-12-28 10:02:22 +00:00
|
|
|
if (ctx[S]->opts & OPEN_NEW)
|
2004-03-27 16:07:20 +00:00
|
|
|
maxwuid = INT_MAX;
|
2005-12-28 10:02:22 +00:00
|
|
|
else if (ctx[S]->opts & OPEN_OLD) {
|
2004-03-27 16:07:20 +00:00
|
|
|
maxwuid = 0;
|
|
|
|
for (srec = recs; srec; srec = srec->next)
|
2005-12-28 10:02:22 +00:00
|
|
|
if (!(srec->status & S_DEAD) && srec->uid[S] > maxwuid)
|
|
|
|
maxwuid = srec->uid[S];
|
2004-03-27 16:07:20 +00:00
|
|
|
} else
|
|
|
|
maxwuid = 0;
|
2005-12-28 10:02:22 +00:00
|
|
|
info( "Selecting slave %s... ", ctx[S]->name );
|
2004-03-27 16:07:20 +00:00
|
|
|
debug( "selecting slave [1,%d]\n", maxwuid );
|
2005-12-28 10:02:22 +00:00
|
|
|
switch (driver[S]->select( ctx[S], (ctx[S]->opts & OPEN_OLD) ? 1 : maxuid[S] + 1, maxwuid, 0, 0 )) {
|
|
|
|
case DRV_STORE_BAD: ret = SYNC_BAD(S); goto bail;
|
2004-03-27 16:07:20 +00:00
|
|
|
case DRV_BOX_BAD: ret = SYNC_FAIL; goto bail;
|
|
|
|
}
|
2005-12-28 10:02:22 +00:00
|
|
|
info( "%d messages, %d recent\n", ctx[S]->count, ctx[S]->recent );
|
2005-12-28 19:17:40 +00:00
|
|
|
findmsgs( recs, ctx, S );
|
2004-03-27 16:07:20 +00:00
|
|
|
|
2005-12-28 10:02:22 +00:00
|
|
|
if (suidval && suidval != ctx[S]->uidvalidity) {
|
2004-03-27 16:07:20 +00:00
|
|
|
fprintf( stderr, "Error: UIDVALIDITY of slave changed\n" );
|
|
|
|
ret = SYNC_FAIL;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = strrchr( dname, '/' );
|
|
|
|
*s = 0;
|
|
|
|
mkdir( dname, 0700 );
|
|
|
|
*s = '/';
|
|
|
|
if (lfd < 0) {
|
|
|
|
if ((lfd = open( lname, O_WRONLY|O_CREAT, 0666 )) < 0)
|
|
|
|
goto lferr;
|
|
|
|
if (fcntl( lfd, F_SETLK, &lck ))
|
|
|
|
goto lckerr;
|
2000-12-20 21:41:21 +00:00
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
if (!(nfp = fopen( nname, "w" ))) {
|
|
|
|
fprintf( stderr, "Error: cannot write new sync state %s\n", nname );
|
|
|
|
ret = SYNC_FAIL;
|
|
|
|
goto bail;
|
2001-01-11 10:13:47 +00:00
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
if (!(jfp = fopen( jname, "a" ))) {
|
|
|
|
fprintf( stderr, "Error: cannot write journal %s\n", jname );
|
|
|
|
fclose( nfp );
|
|
|
|
ret = SYNC_FAIL;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
setlinebuf( jfp );
|
|
|
|
|
|
|
|
mexcs = 0;
|
|
|
|
nmexcs = rmexcs = 0;
|
|
|
|
minwuid = INT_MAX;
|
|
|
|
if (smaxxuid) {
|
|
|
|
debug( "preparing master selection - max expired slave uid is %d\n", smaxxuid );
|
|
|
|
for (srec = recs; srec; srec = srec->next) {
|
|
|
|
if (srec->status & S_DEAD)
|
|
|
|
continue;
|
|
|
|
if (srec->status & S_EXPIRED) {
|
2005-12-28 19:17:40 +00:00
|
|
|
if (!srec->uid[S] || ((ctx[S]->opts & OPEN_OLD) && !srec->msg[S])) {
|
2005-12-28 10:02:22 +00:00
|
|
|
srec->status |= S_EXP_S;
|
2005-12-28 19:10:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (smaxxuid >= srec->uid[S])
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (minwuid > srec->uid[M])
|
2005-12-28 10:02:22 +00:00
|
|
|
minwuid = srec->uid[M];
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
debug( " min non-orphaned master uid is %d\n", minwuid );
|
2005-12-28 10:02:22 +00:00
|
|
|
Fprintf( jfp, "^\n" ); /* if any S_EXP_S */
|
2004-03-27 16:07:20 +00:00
|
|
|
for (srec = recs; srec; srec = srec->next) {
|
|
|
|
if (srec->status & S_DEAD)
|
|
|
|
continue;
|
2005-12-28 10:02:22 +00:00
|
|
|
if (srec->status & S_EXP_S) {
|
|
|
|
if (minwuid > srec->uid[M] && maxuid[M] >= srec->uid[M]) {
|
|
|
|
debug( " -> killing (%d,%d)\n", srec->uid[M], srec->uid[S] );
|
2004-03-27 16:07:20 +00:00
|
|
|
srec->status = S_DEAD;
|
2005-12-28 10:02:22 +00:00
|
|
|
Fprintf( jfp, "- %d %d\n", srec->uid[M], srec->uid[S] );
|
|
|
|
} else if (srec->uid[S]) {
|
|
|
|
debug( " -> orphaning (%d,[%d])\n", srec->uid[M], srec->uid[S] );
|
|
|
|
Fprintf( jfp, "> %d %d 0\n", srec->uid[M], srec->uid[S] );
|
|
|
|
srec->uid[S] = 0;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
2005-12-28 10:02:22 +00:00
|
|
|
} else if (minwuid > srec->uid[M]) {
|
|
|
|
if (srec->uid[S] < 0) {
|
|
|
|
if (maxuid[M] >= srec->uid[M]) {
|
|
|
|
debug( " -> killing (%d,%d)\n", srec->uid[M], srec->uid[S] );
|
2004-03-27 16:07:20 +00:00
|
|
|
srec->status = S_DEAD;
|
2005-12-28 10:02:22 +00:00
|
|
|
Fprintf( jfp, "- %d %d\n", srec->uid[M], srec->uid[S] );
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
2005-12-28 10:02:22 +00:00
|
|
|
} else if (srec->uid[M] > 0 && srec->uid[S] && (ctx[M]->opts & OPEN_OLD) &&
|
|
|
|
(!(ctx[M]->opts & OPEN_NEW) || maxuid[M] >= srec->uid[M])) {
|
2004-03-27 16:07:20 +00:00
|
|
|
if (nmexcs == rmexcs) {
|
|
|
|
rmexcs = rmexcs * 2 + 100;
|
|
|
|
mexcs = nfrealloc( mexcs, rmexcs * sizeof(int) );
|
|
|
|
}
|
2005-12-28 10:02:22 +00:00
|
|
|
mexcs[nmexcs++] = srec->uid[M];
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug( " exception list is:" );
|
2005-12-28 10:02:22 +00:00
|
|
|
for (t = 0; t < nmexcs; t++)
|
|
|
|
debug( " %d", mexcs[t] );
|
2004-03-27 16:07:20 +00:00
|
|
|
debug( "\n" );
|
2005-12-28 10:02:22 +00:00
|
|
|
} else if (ctx[M]->opts & OPEN_OLD)
|
2004-03-27 16:07:20 +00:00
|
|
|
minwuid = 1;
|
2005-12-28 10:02:22 +00:00
|
|
|
if (ctx[M]->opts & OPEN_NEW) {
|
|
|
|
if (minwuid > maxuid[M] + 1)
|
|
|
|
minwuid = maxuid[M] + 1;
|
2004-03-27 16:07:20 +00:00
|
|
|
maxwuid = INT_MAX;
|
2005-12-28 10:02:22 +00:00
|
|
|
} else if (ctx[M]->opts & OPEN_OLD) {
|
2004-03-27 16:07:20 +00:00
|
|
|
maxwuid = 0;
|
|
|
|
for (srec = recs; srec; srec = srec->next)
|
2005-12-28 10:02:22 +00:00
|
|
|
if (!(srec->status & S_DEAD) && srec->uid[M] > maxwuid)
|
|
|
|
maxwuid = srec->uid[M];
|
2004-03-27 16:07:20 +00:00
|
|
|
} else
|
|
|
|
maxwuid = 0;
|
2005-12-28 10:02:22 +00:00
|
|
|
info( "Selecting master %s... ", ctx[M]->name );
|
2004-03-27 16:07:20 +00:00
|
|
|
debug( "selecting master [%d,%d]\n", minwuid, maxwuid );
|
2005-12-28 10:02:22 +00:00
|
|
|
switch (driver[M]->select( ctx[M], minwuid, maxwuid, mexcs, nmexcs )) {
|
|
|
|
case DRV_STORE_BAD: ret = SYNC_BAD(M); goto finish;
|
2004-03-27 16:07:20 +00:00
|
|
|
case DRV_BOX_BAD: ret = SYNC_FAIL; goto finish;
|
|
|
|
}
|
2005-12-28 10:02:22 +00:00
|
|
|
info( "%d messages, %d recent\n", ctx[M]->count, ctx[M]->recent );
|
2005-12-28 19:17:40 +00:00
|
|
|
findmsgs( recs, ctx, M );
|
2001-01-11 10:13:47 +00:00
|
|
|
|
2005-12-28 10:02:22 +00:00
|
|
|
if (muidval && muidval != ctx[M]->uidvalidity) {
|
2004-03-27 16:07:20 +00:00
|
|
|
fprintf( stderr, "Error: UIDVALIDITY of master changed\n" );
|
|
|
|
ret = SYNC_FAIL;
|
|
|
|
goto finish;
|
2000-12-20 21:41:21 +00:00
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
|
|
|
|
if (!muidval || !suidval) {
|
2005-12-28 10:02:22 +00:00
|
|
|
muidval = ctx[M]->uidvalidity;
|
|
|
|
suidval = ctx[S]->uidvalidity;
|
2004-11-13 09:19:36 +00:00
|
|
|
Fprintf( jfp, "| %d %d\n", muidval, suidval );
|
2001-11-15 23:59:27 +00:00
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
|
2005-12-28 10:02:22 +00:00
|
|
|
info( "Synchronizing...\n" );
|
2004-03-27 16:07:20 +00:00
|
|
|
debug( "synchronizing old entries\n" );
|
2004-11-13 09:19:36 +00:00
|
|
|
Fprintf( jfp, "^\n" );
|
2004-03-27 16:07:20 +00:00
|
|
|
for (srec = recs; srec; srec = srec->next) {
|
|
|
|
if (srec->status & S_DEAD)
|
|
|
|
continue;
|
2005-12-28 10:02:22 +00:00
|
|
|
debug( "pair (%d,%d)\n", srec->uid[M], srec->uid[S] );
|
2005-12-28 19:17:40 +00:00
|
|
|
nom = !srec->msg[M] && (ctx[M]->opts & OPEN_OLD);
|
|
|
|
nos = !srec->msg[S] && (ctx[S]->opts & OPEN_OLD);
|
2004-03-27 16:07:20 +00:00
|
|
|
if (nom && nos) {
|
|
|
|
debug( " vanished\n" );
|
|
|
|
/* d.1) d.5) d.6) d.10) d.11) */
|
|
|
|
srec->status = S_DEAD;
|
2005-12-28 10:02:22 +00:00
|
|
|
Fprintf( jfp, "- %d %d\n", srec->uid[M], srec->uid[S] );
|
2004-03-27 16:07:20 +00:00
|
|
|
} else {
|
2005-12-28 10:02:22 +00:00
|
|
|
del[M] = nom && (srec->uid[M] > 0);
|
|
|
|
del[S] = nos && (srec->uid[S] > 0);
|
2004-03-27 16:07:20 +00:00
|
|
|
nflags = srec->flags;
|
|
|
|
|
2005-12-28 10:02:22 +00:00
|
|
|
for (t = 0; t < 2; t++) {
|
|
|
|
int uid, unex;
|
|
|
|
unsigned char sflags, aflags, dflags, rflags;
|
|
|
|
|
|
|
|
/* excludes (push) c.3) d.2) d.3) d.4) / (pull) b.3) d.7) d.8) d.9) */
|
|
|
|
if (!srec->uid[t]) {
|
|
|
|
/* b.1) / c.1) */
|
|
|
|
debug( " no more %s\n", str_ms[t] );
|
|
|
|
} else if (del[1-t]) {
|
|
|
|
/* c.4) d.9) / b.4) d.4) */
|
|
|
|
debug( " %s vanished\n", str_ms[1-t] );
|
2005-12-28 19:17:40 +00:00
|
|
|
if (srec->msg[t] && srec->msg[t]->flags != nflags)
|
2005-12-28 10:02:22 +00:00
|
|
|
info( "Info: conflicting changes in (%d,%d)\n", srec->uid[M], srec->uid[S] );
|
|
|
|
if (chan->ops[t] & OP_DELETE) {
|
|
|
|
debug( " -> %s delete\n", str_hl[t] );
|
2005-12-28 19:17:40 +00:00
|
|
|
switch (driver[t]->set_flags( ctx[t], srec->msg[t], srec->uid[t], F_DELETED, 0 )) {
|
2005-12-28 10:02:22 +00:00
|
|
|
case DRV_STORE_BAD: ret = SYNC_BAD(t); goto finish;
|
|
|
|
case DRV_BOX_BAD: ret = SYNC_FAIL; goto finish;
|
|
|
|
default: /* ok */ break;
|
|
|
|
case DRV_OK:
|
|
|
|
Fprintf( jfp, "%c %d %d 0\n", "><"[t], srec->uid[M], srec->uid[S] );
|
|
|
|
srec->uid[1-t] = 0;
|
|
|
|
}
|
|
|
|
}
|
2005-12-28 19:17:40 +00:00
|
|
|
} else if (!srec->msg[1-t])
|
2005-12-28 10:02:22 +00:00
|
|
|
/* c.1) c.2) d.7) d.8) / b.1) b.2) d.2) d.3) */
|
|
|
|
;
|
|
|
|
else if (srec->uid[t] < 0) {
|
|
|
|
/* b.2) / c.2) */
|
|
|
|
debug( " no %s yet\n", str_ms[t] );
|
|
|
|
if (chan->ops[t] & OP_RENEW) {
|
2005-12-28 19:17:40 +00:00
|
|
|
if ((chan->ops[t] & OP_EXPUNGE) && (srec->msg[1-t]->flags & F_DELETED)) {
|
2005-12-28 10:02:22 +00:00
|
|
|
debug( " -> not %sing - would be expunged anyway\n", str_hl[t] );
|
2005-12-28 19:17:40 +00:00
|
|
|
srec->msg[1-t]->status |= M_NOT_SYNCED;
|
2005-12-28 10:02:22 +00:00
|
|
|
} else {
|
2005-12-28 19:17:40 +00:00
|
|
|
if ((srec->msg[1-t]->flags & F_FLAGGED) || !chan->stores[t]->max_size || srec->msg[1-t]->size <= chan->stores[t]->max_size) {
|
2005-12-28 10:02:22 +00:00
|
|
|
debug( " -> %sing it\n", str_hl[t] );
|
2005-12-28 19:17:40 +00:00
|
|
|
msgdata.flags = srec->msg[1-t]->flags;
|
|
|
|
switch (driver[1-t]->fetch_msg( ctx[1-t], srec->msg[1-t], &msgdata )) {
|
2005-12-28 10:02:22 +00:00
|
|
|
case DRV_STORE_BAD: ret = SYNC_BAD(1-t); goto finish;
|
|
|
|
case DRV_BOX_BAD: ret = SYNC_FAIL; goto finish;
|
2005-12-28 19:17:40 +00:00
|
|
|
default: /* ok */ srec->msg[1-t]->status |= M_NOT_SYNCED; break;
|
2005-12-28 10:02:22 +00:00
|
|
|
case DRV_OK:
|
2005-12-28 19:17:40 +00:00
|
|
|
srec->msg[1-t]->flags = msgdata.flags;
|
2005-12-28 10:02:22 +00:00
|
|
|
switch (driver[t]->store_msg( ctx[t], &msgdata, &uid )) {
|
|
|
|
case DRV_STORE_BAD: ret = SYNC_BAD(t); goto finish;
|
|
|
|
default: ret = SYNC_FAIL; goto finish;
|
|
|
|
case DRV_OK:
|
|
|
|
Fprintf( jfp, "%c %d %d %d\n", "<>"[t], srec->uid[M], srec->uid[S], uid );
|
|
|
|
srec->uid[t] = uid;
|
2005-12-28 19:17:40 +00:00
|
|
|
nflags = srec->msg[1-t]->flags;
|
2005-12-28 10:02:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
debug( " -> not %sing - still too big\n", str_hl[t] );
|
2005-12-28 19:17:40 +00:00
|
|
|
srec->msg[1-t]->status |= M_NOT_SYNCED;
|
2005-12-28 10:02:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
2005-12-28 19:17:40 +00:00
|
|
|
srec->msg[1-t]->status |= M_NOT_SYNCED;
|
2005-12-28 10:02:22 +00:00
|
|
|
} else if (!del[t]) {
|
|
|
|
/* a) & b.3) / c.3) */
|
|
|
|
debug( " may %s\n", str_hl[t] );
|
|
|
|
if (chan->ops[t] & OP_FLAGS) {
|
|
|
|
debug( " -> %sing flags\n", str_hl[t] );
|
2005-12-28 19:17:40 +00:00
|
|
|
sflags = srec->msg[1-t]->flags;
|
2005-12-28 10:02:22 +00:00
|
|
|
aflags = sflags & ~nflags;
|
|
|
|
dflags = ~sflags & nflags;
|
|
|
|
unex = 0;
|
|
|
|
if (srec->status & S_EXPIRED) {
|
|
|
|
if (!t) {
|
2005-12-28 20:05:53 +00:00
|
|
|
if ((aflags & ~F_DELETED) || dflags)
|
2005-12-28 10:02:22 +00:00
|
|
|
info( "Info: Flags of expired message changed in (%d,%d)\n", srec->uid[M], srec->uid[S] );
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
if ((sflags & F_FLAGGED) && !(sflags & F_DELETED)) {
|
|
|
|
unex = 1;
|
|
|
|
dflags |= F_DELETED;
|
|
|
|
} else
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rflags = (nflags | aflags) & ~dflags;
|
|
|
|
if ((chan->ops[t] & OP_EXPUNGE) && (rflags & F_DELETED) &&
|
|
|
|
(!ctx[t]->conf->trash || ctx[t]->conf->trash_only_new))
|
|
|
|
{
|
|
|
|
aflags &= F_DELETED;
|
|
|
|
dflags = 0;
|
|
|
|
}
|
2005-12-28 19:17:40 +00:00
|
|
|
switch ((aflags | dflags) ? driver[t]->set_flags( ctx[t], srec->msg[t], srec->uid[t], aflags, dflags ) : DRV_OK) {
|
2005-12-28 10:02:22 +00:00
|
|
|
case DRV_STORE_BAD: ret = SYNC_BAD(t); goto finish;
|
|
|
|
case DRV_BOX_BAD: ret = SYNC_FAIL; goto finish;
|
|
|
|
default: /* ok */ break;
|
|
|
|
case DRV_OK:
|
|
|
|
nflags = rflags;
|
|
|
|
if (unex) {
|
|
|
|
debug( "unexpiring pair(%d,%d)\n", srec->uid[M], srec->uid[S] );
|
|
|
|
/* log last, so deletion can't be misinterpreted! */
|
|
|
|
Fprintf( jfp, "~ %d %d 0\n", srec->uid[M], srec->uid[S] );
|
|
|
|
srec->status &= ~S_EXPIRED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} /* else b.4) / c.4) */
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
|
|
|
|
if (srec->flags != nflags) {
|
|
|
|
debug( " updating flags (%u -> %u)\n", srec->flags, nflags );
|
|
|
|
srec->flags = nflags;
|
2005-12-28 10:02:22 +00:00
|
|
|
Fprintf( jfp, "* %d %d %u\n", srec->uid[M], srec->uid[S], nflags );
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
2005-12-28 19:17:40 +00:00
|
|
|
if (srec->msg[M] && (srec->msg[M]->flags & F_DELETED))
|
2005-12-28 10:02:22 +00:00
|
|
|
srec->status |= S_DEL(M);
|
2005-12-28 19:17:40 +00:00
|
|
|
if (srec->msg[S] && (srec->msg[S]->flags & F_DELETED))
|
2005-12-28 10:02:22 +00:00
|
|
|
srec->status |= S_DEL(S);
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
2001-11-15 23:59:27 +00:00
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
|
|
|
|
debug( "synchronizing new entries\n" );
|
2005-12-28 10:02:22 +00:00
|
|
|
for (t = 0; t < 2; t++) {
|
|
|
|
int nmsgs, uid;
|
|
|
|
|
|
|
|
for (nmsgs = 0, tmsg = ctx[1-t]->msgs; tmsg; tmsg = tmsg->next)
|
|
|
|
if (!(tmsg->status & M_PROCESSED)) {
|
|
|
|
if (chan->ops[t] & OP_NEW) {
|
|
|
|
debug( "new message %d on %s\n", tmsg->uid, str_ms[1-t] );
|
|
|
|
if ((chan->ops[t] & OP_EXPUNGE) && (tmsg->flags & F_DELETED)) {
|
|
|
|
debug( " not %sing - would be expunged anyway\n", str_hl[t] );
|
|
|
|
tmsg->status |= M_NOT_SYNCED;
|
|
|
|
} else {
|
|
|
|
if ((tmsg->flags & F_FLAGGED) || !chan->stores[t]->max_size || tmsg->size <= chan->stores[t]->max_size) {
|
|
|
|
debug( " %sing it\n", str_hl[t] );
|
|
|
|
if (!nmsgs)
|
|
|
|
info( t ? "Pulling new messages..." : "Pushing new messages..." );
|
|
|
|
else
|
|
|
|
infoc( '.' );
|
|
|
|
nmsgs++;
|
|
|
|
msgdata.flags = tmsg->flags;
|
|
|
|
switch (driver[1-t]->fetch_msg( ctx[1-t], tmsg, &msgdata )) {
|
|
|
|
case DRV_STORE_BAD: return SYNC_BAD(1-t);
|
|
|
|
case DRV_BOX_BAD: return SYNC_FAIL;
|
|
|
|
case DRV_MSG_BAD: /* ok */ tmsg->status |= M_NOT_SYNCED; continue;
|
|
|
|
}
|
|
|
|
tmsg->flags = msgdata.flags;
|
|
|
|
switch (driver[t]->store_msg( ctx[t], &msgdata, &uid )) {
|
|
|
|
case DRV_STORE_BAD: return SYNC_BAD(t);
|
|
|
|
default: return SYNC_FAIL;
|
|
|
|
case DRV_OK: break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
debug( " not %sing - too big\n", str_hl[t] );
|
|
|
|
tmsg->status |= M_NOT_SYNCED;
|
|
|
|
uid = -1;
|
|
|
|
}
|
|
|
|
srec = nfmalloc( sizeof(*srec) );
|
|
|
|
srec->uid[1-t] = tmsg->uid;
|
|
|
|
srec->uid[t] = uid;
|
|
|
|
srec->flags = tmsg->flags;
|
|
|
|
srec->status = 0;
|
|
|
|
srec->next = 0;
|
|
|
|
*srecadd = srec;
|
|
|
|
srecadd = &srec->next;
|
|
|
|
Fprintf( jfp, "+ %d %d %u\n", srec->uid[M], srec->uid[S], srec->flags );
|
|
|
|
if (maxuid[1-t] < tmsg->uid) {
|
|
|
|
maxuid[1-t] = tmsg->uid;
|
|
|
|
Fprintf( jfp, "%c %d\n", ")("[t], tmsg->uid );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
tmsg->status |= M_NOT_SYNCED;
|
|
|
|
}
|
|
|
|
if (nmsgs)
|
|
|
|
info( " %d messages\n", nmsgs );
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
|
2005-12-28 10:02:22 +00:00
|
|
|
if ((chan->ops[S] & (OP_NEW|OP_RENEW)) && chan->max_messages) {
|
2005-12-26 14:55:19 +00:00
|
|
|
debug( "expiring excess entries\n" );
|
2005-12-28 10:02:22 +00:00
|
|
|
todel = ctx[S]->count - chan->max_messages;
|
|
|
|
for (tmsg = ctx[S]->msgs; tmsg && todel > 0; tmsg = tmsg->next)
|
|
|
|
if (!(tmsg->status & M_DEAD) && (tmsg->flags & F_DELETED))
|
2004-03-27 16:07:20 +00:00
|
|
|
todel--;
|
|
|
|
delt = 0;
|
2005-12-28 10:02:22 +00:00
|
|
|
for (tmsg = ctx[S]->msgs; tmsg && todel > 0; tmsg = tmsg->next) {
|
|
|
|
if ((tmsg->status & M_DEAD) || (tmsg->flags & F_DELETED))
|
2004-03-27 16:07:20 +00:00
|
|
|
continue;
|
2005-12-28 10:02:22 +00:00
|
|
|
if ((tmsg->flags & F_FLAGGED) || (tmsg->status & M_NOT_SYNCED)) /* add M_DESYNCED? */
|
2004-03-27 16:07:20 +00:00
|
|
|
todel--;
|
2005-12-28 10:02:22 +00:00
|
|
|
else if (!(tmsg->status & M_RECENT)) {
|
|
|
|
tmsg->status |= M_EXPIRED;
|
2004-03-27 16:07:20 +00:00
|
|
|
delt++;
|
|
|
|
todel--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (delt) {
|
2004-11-13 09:19:36 +00:00
|
|
|
Fprintf( jfp, "^\n" );
|
2004-03-27 16:07:20 +00:00
|
|
|
for (srec = recs; srec; srec = srec->next) {
|
|
|
|
if (srec->status & (S_DEAD|S_EXPIRED))
|
|
|
|
continue;
|
2005-12-28 19:17:40 +00:00
|
|
|
if (srec->msg[S] && (srec->msg[S]->status & M_EXPIRED)) {
|
2005-12-28 10:02:22 +00:00
|
|
|
debug( " expiring pair(%d,%d)\n", srec->uid[M], srec->uid[S] );
|
2004-03-27 16:07:20 +00:00
|
|
|
/* log first, so deletion can't be misinterpreted! */
|
2005-12-28 10:02:22 +00:00
|
|
|
Fprintf( jfp, "~ %d %d 1\n", srec->uid[M], srec->uid[S] );
|
|
|
|
if (smaxxuid < srec->uid[S])
|
|
|
|
smaxxuid = srec->uid[S];
|
2004-03-27 16:07:20 +00:00
|
|
|
srec->status |= S_EXPIRED;
|
2005-12-28 19:17:40 +00:00
|
|
|
switch (driver[S]->set_flags( ctx[S], srec->msg[S], 0, F_DELETED, 0 )) {
|
2005-12-28 10:02:22 +00:00
|
|
|
case DRV_STORE_BAD: ret = SYNC_BAD(S); goto finish;
|
2004-03-27 16:07:20 +00:00
|
|
|
case DRV_BOX_BAD: ret = SYNC_FAIL; goto finish;
|
|
|
|
default: /* ok */ break;
|
2005-12-28 10:02:22 +00:00
|
|
|
case DRV_OK: srec->status |= S_DEL(S);
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-12-21 18:16:44 +00:00
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
2000-12-21 18:16:44 +00:00
|
|
|
|
2004-03-27 16:07:20 +00:00
|
|
|
/* Doing CLOSE here instead of EXPUNGE above saves network traffic.
|
|
|
|
But it costs more server power for single-file formats. And it
|
|
|
|
makes disk-full/quota-exceeded more probable. */
|
2005-12-28 10:02:22 +00:00
|
|
|
for (t = 0; t < 2; t++) {
|
|
|
|
ex[t] = 0;
|
|
|
|
if (chan->ops[t] & OP_EXPUNGE) {
|
|
|
|
info( "Expunging %s\n", str_ms[t] );
|
|
|
|
debug( "expunging %s\n", str_ms[t] );
|
|
|
|
for (tmsg = ctx[t]->msgs; tmsg; tmsg = tmsg->next)
|
|
|
|
if (tmsg->flags & F_DELETED) {
|
|
|
|
if (ctx[t]->conf->trash) {
|
|
|
|
if (!ctx[t]->conf->trash_only_new || (tmsg->status & M_NOT_SYNCED)) {
|
|
|
|
debug( " trashing message %d\n", tmsg->uid );
|
|
|
|
switch (driver[t]->trash_msg( ctx[t], tmsg )) {
|
|
|
|
case DRV_OK: break;
|
|
|
|
case DRV_STORE_BAD: ret = SYNC_BAD(t); goto finish;
|
|
|
|
default: ret = SYNC_FAIL; goto nexex;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
debug( " not trashing message %d - not new\n", tmsg->uid );
|
|
|
|
} else if (ctx[1-t]->conf->trash && ctx[1-t]->conf->trash_remote_new) {
|
|
|
|
if (tmsg->status & M_NOT_SYNCED) {
|
|
|
|
if (!ctx[1-t]->conf->max_size || tmsg->size <= ctx[1-t]->conf->max_size) {
|
|
|
|
debug( " remote trashing message %d\n", tmsg->uid );
|
|
|
|
msgdata.flags = tmsg->flags;
|
|
|
|
switch (driver[t]->fetch_msg( ctx[t], tmsg, &msgdata )) {
|
|
|
|
case DRV_OK: break;
|
|
|
|
case DRV_STORE_BAD: ret = SYNC_BAD(t); goto finish;
|
|
|
|
default: ret = SYNC_FAIL; goto nexex;
|
|
|
|
}
|
|
|
|
switch (driver[1-t]->store_msg( ctx[1-t], &msgdata, 0 )) {
|
|
|
|
case DRV_OK: break;
|
|
|
|
case DRV_STORE_BAD: ret = SYNC_BAD(1-t); goto finish;
|
|
|
|
default: ret = SYNC_FAIL; goto nexex;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
debug( " not remote trashing message %d - too big\n", tmsg->uid );
|
|
|
|
} else
|
|
|
|
debug( " not remote trashing message %d - not new\n", tmsg->uid );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (driver[t]->close( ctx[t] )) {
|
|
|
|
case DRV_OK: ex[t] = 1; break;
|
|
|
|
case DRV_STORE_BAD: ret = SYNC_BAD(t); goto finish;
|
|
|
|
default: break;
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
2005-12-28 10:02:22 +00:00
|
|
|
nexex: ;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
2005-12-28 10:02:22 +00:00
|
|
|
if (ex[M] || ex[S]) {
|
2004-03-27 16:07:20 +00:00
|
|
|
/* This cleanup is not strictly necessary, as the next full sync
|
|
|
|
would throw out the dead entries anyway. But ... */
|
|
|
|
|
|
|
|
minwuid = INT_MAX;
|
|
|
|
if (smaxxuid) {
|
|
|
|
debug( "preparing entry purge - max expired slave uid is %d\n", smaxxuid );
|
|
|
|
for (srec = recs; srec; srec = srec->next) {
|
|
|
|
if (srec->status & S_DEAD)
|
|
|
|
continue;
|
2005-12-28 10:02:22 +00:00
|
|
|
if (!((srec->uid[S] <= 0 || ((srec->status & S_DEL(S)) && ex[S])) &&
|
|
|
|
(srec->uid[M] <= 0 || ((srec->status & S_DEL(M)) && ex[M]) || (srec->status & S_EXPIRED))) &&
|
|
|
|
smaxxuid < srec->uid[S] && minwuid > srec->uid[M])
|
|
|
|
minwuid = srec->uid[M];
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
debug( " min non-orphaned master uid is %d\n", minwuid );
|
2002-01-16 19:47:28 +00:00
|
|
|
}
|
2000-12-21 18:16:44 +00:00
|
|
|
|
2004-11-13 09:19:36 +00:00
|
|
|
Fprintf( jfp, "^\n" );
|
2004-03-27 16:07:20 +00:00
|
|
|
for (srec = recs; srec; srec = srec->next) {
|
|
|
|
if (srec->status & S_DEAD)
|
|
|
|
continue;
|
2005-12-28 10:02:22 +00:00
|
|
|
if (srec->uid[S] <= 0 || ((srec->status & S_DEL(S)) && ex[S])) {
|
|
|
|
if (srec->uid[M] <= 0 || ((srec->status & S_DEL(M)) && ex[M])) {
|
|
|
|
debug( " -> killing (%d,%d)\n", srec->uid[M], srec->uid[S] );
|
2004-03-27 16:07:20 +00:00
|
|
|
srec->status = S_DEAD;
|
2005-12-28 10:02:22 +00:00
|
|
|
Fprintf( jfp, "- %d %d\n", srec->uid[M], srec->uid[S] );
|
2004-03-27 16:07:20 +00:00
|
|
|
} else if (srec->status & S_EXPIRED) {
|
2005-12-28 10:02:22 +00:00
|
|
|
if (maxuid[M] >= srec->uid[M] && minwuid > srec->uid[M]) {
|
|
|
|
debug( " -> killing (%d,%d)\n", srec->uid[M], srec->uid[S] );
|
2004-03-27 16:07:20 +00:00
|
|
|
srec->status = S_DEAD;
|
2005-12-28 10:02:22 +00:00
|
|
|
Fprintf( jfp, "- %d %d\n", srec->uid[M], srec->uid[S] );
|
|
|
|
} else if (srec->uid[S]) {
|
|
|
|
debug( " -> orphaning (%d,[%d])\n", srec->uid[M], srec->uid[S] );
|
|
|
|
Fprintf( jfp, "> %d %d 0\n", srec->uid[M], srec->uid[S] );
|
|
|
|
srec->uid[S] = 0;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-12-20 21:41:21 +00:00
|
|
|
}
|
2001-10-31 19:50:01 +00:00
|
|
|
|
2004-03-27 16:07:20 +00:00
|
|
|
finish:
|
2005-12-28 10:02:22 +00:00
|
|
|
Fprintf( nfp, "%d:%d %d:%d:%d\n", muidval, maxuid[M], suidval, smaxxuid, maxuid[S] );
|
2004-03-27 16:07:20 +00:00
|
|
|
for (srec = recs; srec; srec = srec->next) {
|
|
|
|
if (srec->status & S_DEAD)
|
|
|
|
continue;
|
|
|
|
make_flags( srec->flags, fbuf );
|
2005-12-28 10:02:22 +00:00
|
|
|
Fprintf( nfp, "%d %d %s%s\n", srec->uid[M], srec->uid[S],
|
2004-03-27 16:07:20 +00:00
|
|
|
srec->status & S_EXPIRED ? "X" : "", fbuf );
|
|
|
|
}
|
2000-12-20 21:41:21 +00:00
|
|
|
|
2004-03-27 16:07:20 +00:00
|
|
|
fclose( nfp );
|
|
|
|
fclose( jfp );
|
|
|
|
/* order is important! */
|
|
|
|
rename( nname, dname );
|
|
|
|
unlink( jname );
|
2002-12-28 04:12:07 +00:00
|
|
|
|
2004-03-27 16:07:20 +00:00
|
|
|
bail:
|
|
|
|
for (srec = recs; srec; srec = nsrec) {
|
|
|
|
nsrec = srec->next;
|
|
|
|
free( srec );
|
|
|
|
}
|
|
|
|
unlink( lname );
|
|
|
|
bail1:
|
|
|
|
close( lfd );
|
|
|
|
bail2:
|
|
|
|
free( lname );
|
|
|
|
free( nname );
|
|
|
|
free( jname );
|
|
|
|
free( dname );
|
|
|
|
return ret;
|
2000-12-20 21:41:21 +00:00
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
|