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>
|
2006-03-20 20:16:22 +00:00
|
|
|
* Copyright (C) 2002-2006 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
|
2006-02-09 17:44:22 +00:00
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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
|
|
|
|
2006-03-21 10:30:45 +00:00
|
|
|
const char *str_ms[] = { "master", "slave" }, *str_hl[] = { "push", "pull" };
|
2005-12-28 10:02:22 +00:00
|
|
|
|
2006-02-11 19:52:53 +00:00
|
|
|
void
|
|
|
|
Fclose( FILE *f )
|
|
|
|
{
|
|
|
|
if (fclose( f ) == EOF) {
|
|
|
|
perror( "cannot close file" );
|
|
|
|
exit( 1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2006-02-02 17:03:01 +00:00
|
|
|
#define S_DONE (1<<1)
|
2005-12-28 10:02:22 +00:00
|
|
|
#define S_DEL(ms) (1<<(2+(ms)))
|
2006-02-02 17:03:01 +00:00
|
|
|
#define S_EXPIRED (1<<4)
|
|
|
|
#define S_EXPIRE (1<<5)
|
|
|
|
#define S_NEXPIRE (1<<6)
|
|
|
|
#define S_EXP_S (1<<7)
|
|
|
|
|
|
|
|
#define mvBit(in,ib,ob) ((unsigned char)(((unsigned)in) * (ob) / (ib)))
|
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];
|
2006-02-02 17:03:01 +00:00
|
|
|
unsigned char status, flags, aflags[2], dflags[2];
|
2006-02-03 21:33:43 +00:00
|
|
|
char tuid[TUIDL];
|
2004-03-27 16:07:20 +00:00
|
|
|
} sync_rec_t;
|
|
|
|
|
2006-02-03 21:33:43 +00:00
|
|
|
static int
|
2006-02-12 11:42:46 +00:00
|
|
|
select_box( sync_rec_t *srecs, store_t *ctx[], int maxuid[], int uidval[], int t, int minwuid, int *mexcs, int nmexcs, FILE *jfp )
|
2005-12-28 19:17:40 +00:00
|
|
|
{
|
|
|
|
sync_rec_t *srec, *nsrec = 0;
|
|
|
|
message_t *msg;
|
|
|
|
const char *diag;
|
2006-02-12 11:42:46 +00:00
|
|
|
int uid, maxwuid;
|
2005-12-28 19:17:40 +00:00
|
|
|
char fbuf[16]; /* enlarge when support for keywords is added */
|
|
|
|
|
2006-02-12 11:42:46 +00:00
|
|
|
if (ctx[t]->opts & OPEN_NEW) {
|
|
|
|
if (minwuid > maxuid[t] + 1)
|
|
|
|
minwuid = maxuid[t] + 1;
|
|
|
|
maxwuid = INT_MAX;
|
|
|
|
} else if (ctx[t]->opts & OPEN_OLD) {
|
|
|
|
maxwuid = 0;
|
|
|
|
for (srec = srecs; srec; srec = srec->next)
|
|
|
|
if (!(srec->status & S_DEAD) && srec->uid[t] > maxwuid)
|
|
|
|
maxwuid = srec->uid[t];
|
|
|
|
} else
|
|
|
|
maxwuid = 0;
|
2006-03-19 11:29:12 +00:00
|
|
|
infon( "Selecting %s %s... ", str_ms[t], ctx[t]->name );
|
2006-02-12 11:42:46 +00:00
|
|
|
debug( maxwuid == INT_MAX ? "selecting %s [%d,inf]\n" : "selecting %s [%d,%d]\n", str_ms[t], minwuid, maxwuid );
|
|
|
|
switch (ctx[t]->conf->driver->select( ctx[t], minwuid, maxwuid, mexcs, nmexcs )) {
|
|
|
|
case DRV_STORE_BAD: return SYNC_BAD(t);
|
|
|
|
case DRV_BOX_BAD: return SYNC_FAIL;
|
|
|
|
}
|
|
|
|
if (uidval[t] && uidval[t] != ctx[t]->uidvalidity) {
|
2006-03-19 11:29:12 +00:00
|
|
|
error( "Error: UIDVALIDITY of %s changed (got %d, expected %d)\n", str_ms[t], ctx[t]->uidvalidity, uidval[t] );
|
2006-02-12 11:42:46 +00:00
|
|
|
return SYNC_FAIL;
|
|
|
|
}
|
|
|
|
info( "%d messages, %d recent\n", ctx[M]->count, ctx[M]->recent );
|
|
|
|
|
2006-02-03 21:33:43 +00:00
|
|
|
if (jfp) {
|
|
|
|
/*
|
|
|
|
* Alternatively, the TUIDs could be fetched into the messages and
|
|
|
|
* looked up here. This would make the search faster (probably) and
|
|
|
|
* save roundtrips. On the downside, quite some additional data would
|
|
|
|
* have to be fetched for every message and the IMAP driver would be
|
|
|
|
* more complicated. This is a corner case anyway, so why bother.
|
|
|
|
*/
|
|
|
|
debug( "finding previously copied messages\n" );
|
|
|
|
for (srec = srecs; srec; srec = srec->next) {
|
|
|
|
if (srec->status & S_DEAD)
|
|
|
|
continue;
|
|
|
|
if (srec->uid[t] == -2 && srec->tuid[0]) {
|
|
|
|
debug( " pair(%d,%d): lookup %s, TUID %." stringify(TUIDL) "s\n", srec->uid[M], srec->uid[S], str_ms[t], srec->tuid );
|
|
|
|
switch (ctx[t]->conf->driver->find_msg( ctx[t], srec->tuid, &uid )) {
|
|
|
|
case DRV_STORE_BAD: return SYNC_BAD(t);
|
|
|
|
case DRV_OK:
|
|
|
|
debug( " -> new UID %d\n", uid );
|
|
|
|
Fprintf( jfp, "%c %d %d %d\n", "<>"[t], srec->uid[M], srec->uid[S], uid );
|
|
|
|
srec->uid[t] = uid;
|
|
|
|
srec->tuid[0] = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
debug( " -> TUID lost\n" );
|
|
|
|
Fprintf( jfp, "& %d %d\n", srec->uid[M], srec->uid[S] );
|
|
|
|
srec->flags = 0;
|
|
|
|
srec->tuid[0] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mapping msg -> srec (this variant) is dog slow for new messages.
|
|
|
|
* Mapping srec -> msg is dog slow for deleted messages.
|
|
|
|
* One solution would be using binary search on an index array.
|
|
|
|
* msgs are already sorted by UID, srecs would have to be sorted by uid[t].
|
|
|
|
*/
|
|
|
|
debug( "matching messages against sync records\n" );
|
2005-12-28 19:17:40 +00:00
|
|
|
for (msg = ctx[t]->msgs; msg; msg = msg->next) {
|
|
|
|
uid = msg->uid;
|
2006-01-29 14:46:16 +00:00
|
|
|
if (DFlags & DEBUG) {
|
2005-12-28 19:17:40 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2006-01-30 10:26:04 +00:00
|
|
|
msg->srec = 0;
|
2005-12-28 19:17:40 +00:00
|
|
|
debug( "new\n" );
|
|
|
|
continue;
|
|
|
|
found:
|
2006-01-30 10:26:04 +00:00
|
|
|
msg->srec = srec;
|
2005-12-28 19:17:40 +00:00
|
|
|
srec->msg[t] = msg;
|
|
|
|
nsrec = srec->next;
|
|
|
|
debug( "pairs %5d %s\n", srec->uid[1-t], diag );
|
|
|
|
}
|
2006-02-03 21:33:43 +00:00
|
|
|
|
|
|
|
return SYNC_OK;
|
2005-12-28 19:17:40 +00:00
|
|
|
}
|
|
|
|
|
2006-02-03 21:33:43 +00:00
|
|
|
static int
|
|
|
|
copy_msg( store_t *ctx[], int t, message_t *tmsg, const char *tuid, int *uid )
|
|
|
|
{
|
|
|
|
msg_data_t msgdata;
|
|
|
|
char *fmap, *buf;
|
|
|
|
int i, len, extra, cra, crd, scr, tcr;
|
|
|
|
int start, sbreak = 0, ebreak = 0;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
msgdata.flags = tmsg->flags;
|
|
|
|
switch (ctx[1-t]->conf->driver->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: return SYNC_NOGOOD;
|
|
|
|
}
|
|
|
|
tmsg->flags = msgdata.flags;
|
|
|
|
|
|
|
|
scr = (ctx[1-t]->conf->driver->flags / DRV_CRLF) & 1;
|
|
|
|
tcr = (ctx[t]->conf->driver->flags / DRV_CRLF) & 1;
|
|
|
|
if (tuid || scr != tcr) {
|
|
|
|
fmap = msgdata.data;
|
|
|
|
len = msgdata.len;
|
|
|
|
cra = crd = 0;
|
|
|
|
if (scr > tcr)
|
|
|
|
crd = -1;
|
|
|
|
else if (scr < tcr)
|
2006-03-21 17:50:57 +00:00
|
|
|
cra = 1;
|
2006-02-03 21:33:43 +00:00
|
|
|
extra = 0, i = 0;
|
|
|
|
if (tuid) {
|
|
|
|
extra += 8 + TUIDL + 1 + tcr;
|
|
|
|
nloop:
|
|
|
|
start = i;
|
|
|
|
while (i < len) {
|
|
|
|
c = fmap[i++];
|
|
|
|
if (c == '\r')
|
|
|
|
extra += crd;
|
|
|
|
else if (c == '\n') {
|
|
|
|
extra += cra;
|
2006-03-21 17:50:57 +00:00
|
|
|
if (i - 1 - scr == start) {
|
|
|
|
sbreak = ebreak = i - 1 - scr;
|
2006-02-03 21:33:43 +00:00
|
|
|
goto oke;
|
|
|
|
}
|
|
|
|
if (!memcmp( fmap + start, "X-TUID: ", 8 )) {
|
|
|
|
extra -= (ebreak = i) - (sbreak = start);
|
|
|
|
goto oke;
|
|
|
|
}
|
|
|
|
goto nloop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* invalid message */
|
|
|
|
free( fmap );
|
|
|
|
return SYNC_NOGOOD;
|
|
|
|
}
|
|
|
|
oke:
|
|
|
|
if (cra || crd)
|
|
|
|
for (; i < len; i++) {
|
2006-02-03 23:43:52 +00:00
|
|
|
c = fmap[i];
|
2006-02-03 21:33:43 +00:00
|
|
|
if (c == '\r')
|
|
|
|
extra += crd;
|
|
|
|
else if (c == '\n')
|
|
|
|
extra += cra;
|
|
|
|
}
|
|
|
|
|
|
|
|
msgdata.len = len + extra;
|
|
|
|
buf = msgdata.data = nfmalloc( msgdata.len );
|
|
|
|
i = 0;
|
|
|
|
if (tuid) {
|
|
|
|
if (cra) {
|
|
|
|
for (; i < sbreak; i++) {
|
|
|
|
if (fmap[i] == '\n')
|
|
|
|
*buf++ = '\r';
|
|
|
|
*buf++ = fmap[i];
|
|
|
|
}
|
|
|
|
} else if (crd) {
|
|
|
|
for (; i < sbreak; i++)
|
|
|
|
if (fmap[i] != '\r')
|
|
|
|
*buf++ = fmap[i];
|
|
|
|
} else {
|
|
|
|
memcpy( buf, fmap, sbreak );
|
|
|
|
buf += sbreak;
|
|
|
|
}
|
|
|
|
memcpy( buf, "X-TUID: ", 8 );
|
|
|
|
buf += 8;
|
|
|
|
memcpy( buf, tuid, TUIDL );
|
|
|
|
buf += TUIDL;
|
|
|
|
if (tcr)
|
|
|
|
*buf++ = '\r';
|
|
|
|
*buf++ = '\n';
|
|
|
|
i = ebreak;
|
|
|
|
}
|
|
|
|
if (cra) {
|
|
|
|
for (; i < len; i++) {
|
|
|
|
if (fmap[i] == '\n')
|
|
|
|
*buf++ = '\r';
|
|
|
|
*buf++ = fmap[i];
|
|
|
|
}
|
|
|
|
} else if (crd) {
|
|
|
|
for (; i < len; i++)
|
|
|
|
if (fmap[i] != '\r')
|
|
|
|
*buf++ = fmap[i];
|
|
|
|
} else
|
|
|
|
memcpy( buf, fmap + i, len - i );
|
|
|
|
|
|
|
|
free( fmap );
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (ctx[t]->conf->driver->store_msg( ctx[t], &msgdata, uid )) {
|
|
|
|
case DRV_STORE_BAD: return SYNC_BAD(t);
|
|
|
|
case DRV_OK: return SYNC_OK;
|
|
|
|
default: return SYNC_FAIL;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2006-03-21 15:53:43 +00:00
|
|
|
typedef struct {
|
|
|
|
char *dname, *jname, *nname, *lname;
|
|
|
|
FILE *jfp, *nfp;
|
|
|
|
sync_rec_t *srecs, **srecadd, **osrecadd;
|
|
|
|
channel_conf_t *chan;
|
|
|
|
store_t *ctx[2];
|
|
|
|
driver_t *drv[2];
|
|
|
|
int state[2], ret;
|
|
|
|
int maxuid[2], uidval[2], smaxxuid, lfd;
|
|
|
|
} sync_vars_t;
|
|
|
|
|
|
|
|
#define ST_DID_EXPUNGE (1<<16)
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-02-03 21:33:43 +00:00
|
|
|
#define JOURNAL_VERSION "2"
|
2006-02-02 11:07:54 +00:00
|
|
|
|
2004-03-27 16:07:20 +00:00
|
|
|
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
|
|
|
{
|
2006-03-21 15:53:43 +00:00
|
|
|
sync_vars_t svars[1];
|
2005-12-28 19:17:40 +00:00
|
|
|
message_t *tmsg;
|
2006-03-21 15:53:43 +00:00
|
|
|
sync_rec_t *srec, *nsrec;
|
|
|
|
char *s, *cmname, *csname;
|
|
|
|
FILE *jfp;
|
|
|
|
int no[2], del[2], nex, minwuid, uid, nmsgs;
|
|
|
|
int todel, *mexcs, nmexcs, rmexcs;
|
|
|
|
int opts[2], line, t1, t2, t3, t;
|
2006-02-02 17:03:01 +00:00
|
|
|
unsigned char nflags, sflags, aflags, dflags;
|
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];
|
|
|
|
|
2006-03-21 15:53:43 +00:00
|
|
|
memset( svars, 0, sizeof(svars[0]) );
|
|
|
|
svars->ctx[0] = ctx[0];
|
|
|
|
svars->ctx[1] = ctx[1];
|
|
|
|
svars->chan = chan;
|
|
|
|
svars->srecadd = &svars->srecs;
|
2004-03-27 16:07:20 +00:00
|
|
|
|
2005-12-28 10:02:22 +00:00
|
|
|
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;
|
2006-03-21 15:53:43 +00:00
|
|
|
svars->drv[t] = ctx[t]->conf->driver;
|
|
|
|
svars->drv[t]->prepare_paths( ctx[t] );
|
2005-12-28 10:02:22 +00:00
|
|
|
}
|
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) {
|
2006-03-19 11:29:12 +00:00
|
|
|
error( "Error: store '%s' does not support in-box sync state\n", chan->stores[S]->name );
|
2005-12-28 10:02:22 +00:00
|
|
|
return SYNC_BAD(S);
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
2006-03-21 15:53:43 +00:00
|
|
|
nfasprintf( &svars->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)
|
2006-03-21 15:53:43 +00:00
|
|
|
nfasprintf( &svars->dname, "%s%s", chan->sync_state, csname );
|
2004-03-27 16:07:20 +00:00
|
|
|
else {
|
2005-12-28 10:02:22 +00:00
|
|
|
cmname = clean_strdup( ctx[M]->name );
|
2006-03-21 15:53:43 +00:00
|
|
|
nfasprintf( &svars->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 );
|
|
|
|
}
|
2006-03-21 15:53:43 +00:00
|
|
|
if (!(s = strrchr( svars->dname, '/' ))) {
|
|
|
|
error( "Error: invalid SyncState '%s'\n", svars->dname );
|
|
|
|
free( svars->dname );
|
2006-02-11 20:28:45 +00:00
|
|
|
return SYNC_BAD(S);
|
|
|
|
}
|
|
|
|
*s = 0;
|
2006-03-21 15:53:43 +00:00
|
|
|
if (mkdir( svars->dname, 0700 ) && errno != EEXIST) {
|
|
|
|
error( "Error: cannot create SyncState directory '%s': %s\n", svars->dname, strerror(errno) );
|
|
|
|
free( svars->dname );
|
2006-02-11 20:28:45 +00:00
|
|
|
return SYNC_BAD(S);
|
|
|
|
}
|
|
|
|
*s = '/';
|
2006-03-21 15:53:43 +00:00
|
|
|
nfasprintf( &svars->jname, "%s.journal", svars->dname );
|
|
|
|
nfasprintf( &svars->nname, "%s.new", svars->dname );
|
|
|
|
nfasprintf( &svars->lname, "%s.lock", svars->dname );
|
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
|
2006-03-21 15:53:43 +00:00
|
|
|
if ((svars->lfd = open( svars->lname, O_WRONLY|O_CREAT, 0666 )) < 0) {
|
|
|
|
error( "Error: cannot create lock file %s: %s\n", svars->lname, strerror(errno) );
|
|
|
|
svars->ret = SYNC_FAIL;
|
2006-02-11 20:28:45 +00:00
|
|
|
goto bail2;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
2006-03-21 15:53:43 +00:00
|
|
|
if (fcntl( svars->lfd, F_SETLK, &lck )) {
|
2006-03-19 11:29:12 +00:00
|
|
|
error( "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 );
|
2006-03-21 15:53:43 +00:00
|
|
|
svars->ret = SYNC_FAIL;
|
2004-03-27 16:07:20 +00:00
|
|
|
goto bail1;
|
|
|
|
}
|
2006-03-21 15:53:43 +00:00
|
|
|
if ((jfp = fopen( svars->dname, "r" ))) {
|
|
|
|
debug( "reading sync state %s ...\n", svars->dname );
|
|
|
|
if (!fgets( buf, sizeof(buf), jfp ) || !(t = strlen( buf )) || buf[t - 1] != '\n') {
|
|
|
|
error( "Error: incomplete sync state header in %s\n", svars->dname );
|
|
|
|
fclose( jfp );
|
|
|
|
svars->ret = SYNC_FAIL;
|
2004-11-13 09:19:36 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
2006-03-21 15:53:43 +00:00
|
|
|
if (sscanf( buf, "%d:%d %d:%d:%d", &svars->uidval[M], &svars->maxuid[M], &svars->uidval[S], &svars->smaxxuid, &svars->maxuid[S]) != 5) {
|
|
|
|
error( "Error: invalid sync state header in %s\n", svars->dname );
|
|
|
|
fclose( jfp );
|
|
|
|
svars->ret = SYNC_FAIL;
|
2004-03-27 16:07:20 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
2006-03-21 15:53:43 +00:00
|
|
|
line = 1;
|
|
|
|
while (fgets( buf, sizeof(buf), jfp )) {
|
|
|
|
line++;
|
2005-12-28 10:02:22 +00:00
|
|
|
if (!(t = strlen( buf )) || buf[t - 1] != '\n') {
|
2006-03-21 15:53:43 +00:00
|
|
|
error( "Error: incomplete sync state entry at %s:%d\n", svars->dname, line );
|
|
|
|
fclose( jfp );
|
|
|
|
svars->ret = SYNC_FAIL;
|
2004-11-13 09:19:36 +00:00
|
|
|
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) {
|
2006-03-21 15:53:43 +00:00
|
|
|
error( "Error: invalid sync state entry at %s:%d\n", svars->dname, line );
|
|
|
|
fclose( jfp );
|
|
|
|
svars->ret = SYNC_FAIL;
|
2004-03-27 16:07:20 +00:00
|
|
|
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++;
|
2006-02-02 17:03:01 +00:00
|
|
|
srec->status = S_EXPIRE | S_EXPIRED;
|
2004-03-27 16:07:20 +00:00
|
|
|
} 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;
|
2006-02-03 21:33:43 +00:00
|
|
|
srec->tuid[0] = 0;
|
2004-03-27 16:07:20 +00:00
|
|
|
srec->next = 0;
|
2006-03-21 15:53:43 +00:00
|
|
|
*svars->srecadd = srec;
|
|
|
|
svars->srecadd = &srec->next;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
2006-03-21 15:53:43 +00:00
|
|
|
fclose( jfp );
|
2004-03-27 16:07:20 +00:00
|
|
|
} else {
|
|
|
|
if (errno != ENOENT) {
|
2006-03-21 15:53:43 +00:00
|
|
|
error( "Error: cannot read sync state %s\n", svars->dname );
|
|
|
|
svars->ret = SYNC_FAIL;
|
2004-03-27 16:07:20 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
}
|
2006-02-11 20:28:45 +00:00
|
|
|
line = 0;
|
2006-03-21 15:53:43 +00:00
|
|
|
if ((jfp = fopen( svars->jname, "r" ))) {
|
|
|
|
if (!stat( svars->nname, &st ) && fgets( buf, sizeof(buf), jfp )) {
|
2004-03-27 16:07:20 +00:00
|
|
|
debug( "recovering journal ...\n" );
|
2006-02-02 11:07:54 +00:00
|
|
|
if (!(t = strlen( buf )) || buf[t - 1] != '\n') {
|
2006-03-21 15:53:43 +00:00
|
|
|
error( "Error: incomplete journal header in %s\n", svars->jname );
|
2006-02-02 11:07:54 +00:00
|
|
|
fclose( jfp );
|
2006-03-21 15:53:43 +00:00
|
|
|
svars->ret = SYNC_FAIL;
|
2006-02-02 11:07:54 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
if (memcmp( buf, JOURNAL_VERSION "\n", strlen(JOURNAL_VERSION) + 1 )) {
|
2006-03-19 11:29:12 +00:00
|
|
|
error( "Error: incompatible journal version "
|
2006-02-02 11:07:54 +00:00
|
|
|
"(got %.*s, expected " JOURNAL_VERSION ")\n", t - 1, buf );
|
|
|
|
fclose( jfp );
|
2006-03-21 15:53:43 +00:00
|
|
|
svars->ret = SYNC_FAIL;
|
2006-02-02 11:07:54 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
2006-02-02 10:44:19 +00:00
|
|
|
srec = 0;
|
2006-02-02 11:07:54 +00:00
|
|
|
line = 1;
|
2004-03-27 16:07:20 +00:00
|
|
|
while (fgets( buf, sizeof(buf), jfp )) {
|
|
|
|
line++;
|
2005-12-28 10:02:22 +00:00
|
|
|
if (!(t = strlen( buf )) || buf[t - 1] != '\n') {
|
2006-03-21 15:53:43 +00:00
|
|
|
error( "Error: incomplete journal entry at %s:%d\n", svars->jname, line );
|
2004-11-13 09:19:36 +00:00
|
|
|
fclose( jfp );
|
2006-03-21 15:53:43 +00:00
|
|
|
svars->ret = SYNC_FAIL;
|
2004-11-13 09:19:36 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
2006-02-03 21:33:43 +00:00
|
|
|
if (buf[0] == '#' ?
|
|
|
|
(t3 = 0, (sscanf( buf + 2, "%d %d %n", &t1, &t2, &t3 ) < 2) || !t3 || (t - t3 != TUIDL + 3)) :
|
|
|
|
buf[0] == '(' || buf[0] == ')' ?
|
2006-02-02 10:44:19 +00:00
|
|
|
(sscanf( buf + 2, "%d", &t1 ) != 1) :
|
2006-02-03 21:33:43 +00:00
|
|
|
buf[0] == '+' || buf[0] == '&' || buf[0] == '-' || buf[0] == '|' || buf[0] == '/' || buf[0] == '\\' ?
|
|
|
|
(sscanf( buf + 2, "%d %d", &t1, &t2 ) != 2) :
|
|
|
|
(sscanf( buf + 2, "%d %d %d", &t1, &t2, &t3 ) != 3))
|
2006-02-02 10:44:19 +00:00
|
|
|
{
|
2006-03-21 15:53:43 +00:00
|
|
|
error( "Error: malformed journal entry at %s:%d\n", svars->jname, line );
|
2006-02-02 10:44:19 +00:00
|
|
|
fclose( jfp );
|
2006-03-21 15:53:43 +00:00
|
|
|
svars->ret = SYNC_FAIL;
|
2006-02-02 10:44:19 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
if (buf[0] == '(')
|
2006-03-21 15:53:43 +00:00
|
|
|
svars->maxuid[M] = t1;
|
2006-02-02 10:44:19 +00:00
|
|
|
else if (buf[0] == ')')
|
2006-03-21 15:53:43 +00:00
|
|
|
svars->maxuid[S] = t1;
|
2006-02-02 10:44:19 +00:00
|
|
|
else if (buf[0] == '|') {
|
2006-03-21 15:53:43 +00:00
|
|
|
svars->uidval[M] = t1;
|
|
|
|
svars->uidval[S] = t2;
|
2006-02-02 10:44:19 +00:00
|
|
|
} else if (buf[0] == '+') {
|
|
|
|
srec = nfmalloc( sizeof(*srec) );
|
|
|
|
srec->uid[M] = t1;
|
|
|
|
srec->uid[S] = t2;
|
2006-02-03 21:33:43 +00:00
|
|
|
debug( " new entry(%d,%d)\n", t1, t2 );
|
2006-02-02 10:44:19 +00:00
|
|
|
srec->msg[M] = srec->msg[S] = 0;
|
|
|
|
srec->status = 0;
|
2006-02-03 21:33:43 +00:00
|
|
|
srec->flags = 0;
|
|
|
|
srec->tuid[0] = 0;
|
2006-02-02 10:44:19 +00:00
|
|
|
srec->next = 0;
|
2006-03-21 15:53:43 +00:00
|
|
|
*svars->srecadd = srec;
|
|
|
|
svars->srecadd = &srec->next;
|
2006-02-02 10:44:19 +00:00
|
|
|
} else {
|
|
|
|
for (nsrec = srec; srec; srec = srec->next)
|
|
|
|
if (srec->uid[M] == t1 && srec->uid[S] == t2)
|
|
|
|
goto syncfnd;
|
2006-03-21 15:53:43 +00:00
|
|
|
for (srec = svars->srecs; srec != nsrec; srec = srec->next)
|
2006-02-02 10:44:19 +00:00
|
|
|
if (srec->uid[M] == t1 && srec->uid[S] == t2)
|
|
|
|
goto syncfnd;
|
2006-03-21 15:53:43 +00:00
|
|
|
error( "Error: journal entry at %s:%d refers to non-existing sync state entry\n", svars->jname, line );
|
2006-02-02 10:44:19 +00:00
|
|
|
fclose( jfp );
|
2006-03-21 15:53:43 +00:00
|
|
|
svars->ret = SYNC_FAIL;
|
2006-02-02 10:44:19 +00:00
|
|
|
goto bail;
|
|
|
|
syncfnd:
|
2006-03-19 11:29:12 +00:00
|
|
|
debugn( " entry(%d,%d,%u) ", srec->uid[M], srec->uid[S], srec->flags );
|
2006-02-02 10:44:19 +00:00
|
|
|
switch (buf[0]) {
|
|
|
|
case '-':
|
|
|
|
debug( "killed\n" );
|
|
|
|
srec->status = S_DEAD;
|
|
|
|
break;
|
2006-02-03 21:33:43 +00:00
|
|
|
case '#':
|
|
|
|
debug( "TUID now %." stringify(TUIDL) "s\n", buf + t3 + 2 );
|
|
|
|
memcpy( srec->tuid, buf + t3 + 2, TUIDL );
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
debug( "TUID %." stringify(TUIDL) "s lost\n", srec->tuid );
|
|
|
|
srec->flags = 0;
|
|
|
|
srec->tuid[0] = 0;
|
|
|
|
break;
|
2006-02-02 10:44:19 +00:00
|
|
|
case '<':
|
|
|
|
debug( "master now %d\n", t3 );
|
|
|
|
srec->uid[M] = t3;
|
2006-02-03 21:33:43 +00:00
|
|
|
srec->tuid[0] = 0;
|
2006-02-02 10:44:19 +00:00
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
debug( "slave now %d\n", t3 );
|
|
|
|
srec->uid[S] = t3;
|
2006-02-03 21:33:43 +00:00
|
|
|
srec->tuid[0] = 0;
|
2006-02-02 10:44:19 +00:00
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
debug( "flags now %d\n", t3 );
|
2004-03-27 16:07:20 +00:00
|
|
|
srec->flags = t3;
|
2006-02-02 10:44:19 +00:00
|
|
|
break;
|
|
|
|
case '~':
|
2006-02-02 17:03:01 +00:00
|
|
|
debug( "expire now %d\n", t3 );
|
|
|
|
if (t3)
|
|
|
|
srec->status |= S_EXPIRE;
|
|
|
|
else
|
|
|
|
srec->status &= ~S_EXPIRE;
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
t3 = (srec->status & S_EXPIRED);
|
|
|
|
debug( "expire back to %d\n", t3 / S_EXPIRED );
|
|
|
|
if (t3)
|
|
|
|
srec->status |= S_EXPIRE;
|
|
|
|
else
|
|
|
|
srec->status &= ~S_EXPIRE;
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
t3 = (srec->status & S_EXPIRE);
|
|
|
|
debug( "expired now %d\n", t3 / S_EXPIRE );
|
2006-02-02 10:44:19 +00:00
|
|
|
if (t3) {
|
2006-03-21 15:53:43 +00:00
|
|
|
if (svars->smaxxuid < srec->uid[S])
|
|
|
|
svars->smaxxuid = srec->uid[S];
|
2006-02-02 10:44:19 +00:00
|
|
|
srec->status |= S_EXPIRED;
|
|
|
|
} else
|
|
|
|
srec->status &= ~S_EXPIRED;
|
|
|
|
break;
|
|
|
|
default:
|
2006-03-21 15:53:43 +00:00
|
|
|
error( "Error: unrecognized journal entry at %s:%d\n", svars->jname, line );
|
2004-03-27 16:07:20 +00:00
|
|
|
fclose( jfp );
|
2006-03-21 15:53:43 +00:00
|
|
|
svars->ret = SYNC_FAIL;
|
2004-03-27 16:07:20 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose( jfp );
|
|
|
|
} else {
|
|
|
|
if (errno != ENOENT) {
|
2006-03-21 15:53:43 +00:00
|
|
|
error( "Error: cannot read journal %s\n", svars->jname );
|
|
|
|
svars->ret = SYNC_FAIL;
|
2004-03-27 16:07:20 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
}
|
2006-03-21 15:53:43 +00:00
|
|
|
if (!(svars->nfp = fopen( svars->nname, "w" ))) {
|
|
|
|
error( "Error: cannot write new sync state %s\n", svars->nname );
|
|
|
|
svars->ret = SYNC_FAIL;
|
2006-02-11 20:28:45 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
2006-03-21 15:53:43 +00:00
|
|
|
if (!(svars->jfp = fopen( svars->jname, "a" ))) {
|
|
|
|
error( "Error: cannot write journal %s\n", svars->jname );
|
|
|
|
fclose( svars->nfp );
|
|
|
|
svars->ret = SYNC_FAIL;
|
2006-02-11 20:28:45 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
2006-03-21 15:53:43 +00:00
|
|
|
setlinebuf( svars->jfp );
|
2006-02-11 20:28:45 +00:00
|
|
|
if (!line)
|
2006-03-21 15:53:43 +00:00
|
|
|
Fprintf( svars->jfp, JOURNAL_VERSION "\n" );
|
2004-03-27 16:07:20 +00:00
|
|
|
|
2006-01-29 11:35: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;
|
2006-02-02 17:03:01 +00:00
|
|
|
if (line)
|
2006-03-21 15:53:43 +00:00
|
|
|
for (srec = svars->srecs; srec; srec = srec->next) {
|
2006-02-03 21:33:43 +00:00
|
|
|
if (srec->status & S_DEAD)
|
|
|
|
continue;
|
|
|
|
if ((mvBit(srec->status, S_EXPIRE, S_EXPIRED) ^ srec->status) & S_EXPIRED)
|
2006-02-02 17:03:01 +00:00
|
|
|
opts[S] |= OPEN_OLD|OPEN_FLAGS;
|
2006-02-03 21:33:43 +00:00
|
|
|
if (srec->tuid[0]) {
|
|
|
|
if (srec->uid[M] == -2)
|
|
|
|
opts[M] |= OPEN_OLD|OPEN_FIND;
|
|
|
|
else if (srec->uid[S] == -2)
|
|
|
|
opts[S] |= OPEN_OLD|OPEN_FIND;
|
2006-02-02 17:03:01 +00:00
|
|
|
}
|
2006-02-03 21:33:43 +00:00
|
|
|
}
|
2006-03-21 15:53:43 +00:00
|
|
|
svars->drv[M]->prepare_opts( ctx[M], opts[M] );
|
|
|
|
svars->drv[S]->prepare_opts( ctx[S], opts[S] );
|
2006-01-29 11:35:22 +00:00
|
|
|
|
2006-03-21 15:53:43 +00:00
|
|
|
if ((svars->ret = select_box( svars->srecs, svars->ctx, svars->maxuid, svars->uidval, S, (ctx[S]->opts & OPEN_OLD) ? 1 : INT_MAX, 0, 0, line ? svars->jfp : 0 )) != SYNC_OK)
|
2006-02-03 21:33:43 +00:00
|
|
|
goto finish;
|
|
|
|
|
2004-03-27 16:07:20 +00:00
|
|
|
mexcs = 0;
|
|
|
|
nmexcs = rmexcs = 0;
|
|
|
|
minwuid = INT_MAX;
|
2006-03-21 15:53:43 +00:00
|
|
|
if (svars->smaxxuid) {
|
|
|
|
debug( "preparing master selection - max expired slave uid is %d\n", svars->smaxxuid );
|
|
|
|
for (srec = svars->srecs; srec; srec = srec->next) {
|
2004-03-27 16:07:20 +00:00
|
|
|
if (srec->status & S_DEAD)
|
|
|
|
continue;
|
|
|
|
if (srec->status & S_EXPIRED) {
|
2006-03-21 15:53:43 +00:00
|
|
|
if (!srec->uid[S] || ((svars->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 {
|
2006-03-21 15:53:43 +00:00
|
|
|
if (svars->smaxxuid >= srec->uid[S])
|
2005-12-28 19:10:12 +00:00
|
|
|
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 );
|
2006-03-21 15:53:43 +00:00
|
|
|
for (srec = svars->srecs; srec; srec = srec->next) {
|
2004-03-27 16:07:20 +00:00
|
|
|
if (srec->status & S_DEAD)
|
|
|
|
continue;
|
2005-12-28 10:02:22 +00:00
|
|
|
if (srec->status & S_EXP_S) {
|
2006-03-21 15:53:43 +00:00
|
|
|
if (minwuid > srec->uid[M] && svars->maxuid[M] >= srec->uid[M]) {
|
2005-12-28 10:02:22 +00:00
|
|
|
debug( " -> killing (%d,%d)\n", srec->uid[M], srec->uid[S] );
|
2004-03-27 16:07:20 +00:00
|
|
|
srec->status = S_DEAD;
|
2006-03-21 15:53:43 +00:00
|
|
|
Fprintf( svars->jfp, "- %d %d\n", srec->uid[M], srec->uid[S] );
|
2005-12-28 10:02:22 +00:00
|
|
|
} else if (srec->uid[S]) {
|
|
|
|
debug( " -> orphaning (%d,[%d])\n", srec->uid[M], srec->uid[S] );
|
2006-03-21 15:53:43 +00:00
|
|
|
Fprintf( svars->jfp, "> %d %d 0\n", srec->uid[M], srec->uid[S] );
|
2005-12-28 10:02:22 +00:00
|
|
|
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) {
|
2006-03-21 15:53:43 +00:00
|
|
|
if (svars->maxuid[M] >= srec->uid[M]) {
|
2005-12-28 10:02:22 +00:00
|
|
|
debug( " -> killing (%d,%d)\n", srec->uid[M], srec->uid[S] );
|
2004-03-27 16:07:20 +00:00
|
|
|
srec->status = S_DEAD;
|
2006-03-21 15:53:43 +00:00
|
|
|
Fprintf( svars->jfp, "- %d %d\n", srec->uid[M], srec->uid[S] );
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
2006-03-21 15:53:43 +00:00
|
|
|
} else if (srec->uid[M] > 0 && srec->uid[S] && (svars->ctx[M]->opts & OPEN_OLD) &&
|
|
|
|
(!(svars->ctx[M]->opts & OPEN_NEW) || svars->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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-03-19 11:29:12 +00:00
|
|
|
debugn( " exception list is:" );
|
2005-12-28 10:02:22 +00:00
|
|
|
for (t = 0; t < nmexcs; t++)
|
2006-03-19 11:29:12 +00:00
|
|
|
debugn( " %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;
|
2006-03-21 15:53:43 +00:00
|
|
|
if ((svars->ret = select_box( svars->srecs, svars->ctx, svars->maxuid, svars->uidval, M, minwuid, mexcs, nmexcs, line ? svars->jfp : 0 )) != SYNC_OK)
|
2006-02-03 21:33:43 +00:00
|
|
|
goto finish;
|
2004-03-27 16:07:20 +00:00
|
|
|
|
2006-03-21 15:53:43 +00:00
|
|
|
if (!svars->uidval[M] || !svars->uidval[S]) {
|
|
|
|
svars->uidval[M] = svars->ctx[M]->uidvalidity;
|
|
|
|
svars->uidval[S] = svars->ctx[S]->uidvalidity;
|
|
|
|
Fprintf( svars->jfp, "| %d %d\n", svars->uidval[M], svars->uidval[S] );
|
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" );
|
2006-01-29 15:52:49 +00:00
|
|
|
|
|
|
|
debug( "synchronizing new entries\n" );
|
2006-03-21 15:53:43 +00:00
|
|
|
svars->osrecadd = svars->srecadd;
|
2006-01-29 15:52:49 +00:00
|
|
|
for (t = 0; t < 2; t++) {
|
2006-03-21 15:53:43 +00:00
|
|
|
for (nmsgs = 0, tmsg = svars->ctx[1-t]->msgs; tmsg; tmsg = tmsg->next)
|
|
|
|
if (tmsg->srec ? tmsg->srec->uid[t] < 0 && (tmsg->srec->uid[t] == -1 ? (svars->chan->ops[t] & OP_RENEW) : (svars->chan->ops[t] & OP_NEW)) : (svars->chan->ops[t] & OP_NEW)) {
|
2006-01-30 10:26:04 +00:00
|
|
|
debug( "new message %d on %s\n", tmsg->uid, str_ms[1-t] );
|
2006-03-21 15:53:43 +00:00
|
|
|
if ((svars->chan->ops[t] & OP_EXPUNGE) && (tmsg->flags & F_DELETED))
|
2006-02-03 21:33:43 +00:00
|
|
|
debug( " -> not %sing - would be expunged anyway\n", str_hl[t] );
|
2006-01-30 10:26:04 +00:00
|
|
|
else {
|
2006-02-03 21:33:43 +00:00
|
|
|
if (tmsg->srec) {
|
|
|
|
srec = tmsg->srec;
|
|
|
|
srec->status |= S_DONE;
|
|
|
|
debug( " -> pair(%d,%d) exists\n", srec->uid[M], srec->uid[S] );
|
|
|
|
} else {
|
|
|
|
srec = nfmalloc( sizeof(*srec) );
|
|
|
|
srec->next = 0;
|
2006-03-21 15:53:43 +00:00
|
|
|
*svars->srecadd = srec;
|
|
|
|
svars->srecadd = &srec->next;
|
2006-02-03 21:33:43 +00:00
|
|
|
srec->status = S_DONE;
|
|
|
|
srec->flags = 0;
|
|
|
|
srec->tuid[0] = 0;
|
|
|
|
srec->uid[1-t] = tmsg->uid;
|
|
|
|
srec->uid[t] = -2;
|
2006-03-21 15:53:43 +00:00
|
|
|
Fprintf( svars->jfp, "+ %d %d\n", srec->uid[M], srec->uid[S] );
|
2006-02-03 21:33:43 +00:00
|
|
|
debug( " -> pair(%d,%d) created\n", srec->uid[M], srec->uid[S] );
|
|
|
|
}
|
2006-03-21 15:53:43 +00:00
|
|
|
if ((tmsg->flags & F_FLAGGED) || !svars->chan->stores[t]->max_size || tmsg->size <= svars->chan->stores[t]->max_size) {
|
2006-01-30 10:26:04 +00:00
|
|
|
if (!nmsgs)
|
2006-03-19 11:29:12 +00:00
|
|
|
infon( t ? "Pulling new messages..." : "Pushing new messages..." );
|
2006-01-30 10:26:04 +00:00
|
|
|
else
|
|
|
|
infoc( '.' );
|
|
|
|
nmsgs++;
|
2006-02-03 21:33:43 +00:00
|
|
|
if (tmsg->flags) {
|
|
|
|
srec->flags = tmsg->flags;
|
2006-03-21 15:53:43 +00:00
|
|
|
Fprintf( svars->jfp, "* %d %d %u\n", srec->uid[M], srec->uid[S], srec->flags );
|
2006-02-03 21:33:43 +00:00
|
|
|
debug( " -> updated flags to %u\n", tmsg->flags );
|
2006-01-30 10:26:04 +00:00
|
|
|
}
|
2006-02-03 21:33:43 +00:00
|
|
|
for (t1 = 0; t1 < TUIDL; t1++) {
|
|
|
|
t2 = arc4_getbyte() & 0x3f;
|
|
|
|
srec->tuid[t1] = t2 < 26 ? t2 + 'A' : t2 < 52 ? t2 + 'a' - 26 : t2 < 62 ? t2 + '0' - 52 : t2 == 62 ? '+' : '/';
|
|
|
|
}
|
2006-03-21 15:53:43 +00:00
|
|
|
Fprintf( svars->jfp, "# %d %d %." stringify(TUIDL) "s\n", srec->uid[M], srec->uid[S], srec->tuid );
|
2006-02-03 21:33:43 +00:00
|
|
|
debug( " -> %sing message, TUID %." stringify(TUIDL) "s\n", str_hl[t], srec->tuid );
|
2006-03-21 15:53:43 +00:00
|
|
|
switch ((svars->ret = copy_msg( svars->ctx, t, tmsg, srec->tuid, &uid ))) {
|
2006-02-03 21:33:43 +00:00
|
|
|
case SYNC_OK: break;
|
|
|
|
case SYNC_NOGOOD:
|
|
|
|
/* The error is either transient or the message is gone. */
|
|
|
|
debug( " -> killing (%d,%d)\n", srec->uid[M], srec->uid[S] );
|
|
|
|
srec->status = S_DEAD;
|
2006-03-21 15:53:43 +00:00
|
|
|
Fprintf( svars->jfp, "- %d %d\n", srec->uid[M], srec->uid[S] );
|
2006-02-03 21:33:43 +00:00
|
|
|
continue;
|
|
|
|
default: goto finish;
|
2006-01-30 10:26:04 +00:00
|
|
|
}
|
2006-01-29 15:52:49 +00:00
|
|
|
} else {
|
2006-01-30 10:26:04 +00:00
|
|
|
if (tmsg->srec) {
|
|
|
|
debug( " -> not %sing - still too big\n", str_hl[t] );
|
|
|
|
continue;
|
2006-01-29 15:52:49 +00:00
|
|
|
}
|
2006-02-03 21:33:43 +00:00
|
|
|
debug( " -> not %sing - too big\n", str_hl[t] );
|
2006-01-30 10:26:04 +00:00
|
|
|
uid = -1;
|
|
|
|
}
|
2006-02-03 21:33:43 +00:00
|
|
|
if (srec->uid[t] != uid) {
|
|
|
|
debug( " -> new UID %d\n", uid );
|
2006-03-21 15:53:43 +00:00
|
|
|
Fprintf( svars->jfp, "%c %d %d %d\n", "<>"[t], srec->uid[M], srec->uid[S], uid );
|
2006-02-03 21:33:43 +00:00
|
|
|
srec->uid[t] = uid;
|
|
|
|
srec->tuid[0] = 0;
|
2006-01-30 10:26:04 +00:00
|
|
|
}
|
2006-02-03 21:33:43 +00:00
|
|
|
if (!tmsg->srec) {
|
2006-01-30 10:26:04 +00:00
|
|
|
tmsg->srec = srec;
|
2006-03-21 15:53:43 +00:00
|
|
|
if (svars->maxuid[1-t] < tmsg->uid) {
|
|
|
|
svars->maxuid[1-t] = tmsg->uid;
|
|
|
|
Fprintf( svars->jfp, "%c %d\n", ")("[t], tmsg->uid );
|
2006-01-29 15:52:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nmsgs)
|
|
|
|
info( " %d messages\n", nmsgs );
|
|
|
|
}
|
2006-02-03 21:33:43 +00:00
|
|
|
debug( "finding just copied messages\n" );
|
2006-03-21 15:53:43 +00:00
|
|
|
for (srec = svars->srecs; srec; srec = srec->next) {
|
2006-02-03 21:33:43 +00:00
|
|
|
if (srec->status & S_DEAD)
|
|
|
|
continue;
|
|
|
|
if (srec->tuid[0]) {
|
|
|
|
t = (srec->uid[M] == -2) ? M : S;
|
|
|
|
debug( " pair(%d,%d): lookup %s, TUID %." stringify(TUIDL) "s\n", srec->uid[M], srec->uid[S], str_ms[t], srec->tuid );
|
2006-03-21 15:53:43 +00:00
|
|
|
switch (svars->drv[t]->find_msg( svars->ctx[t], srec->tuid, &uid )) {
|
|
|
|
case DRV_STORE_BAD: svars->ret = SYNC_BAD(t); goto finish;
|
2006-02-03 21:33:43 +00:00
|
|
|
case DRV_OK:
|
|
|
|
debug( " -> new UID %d\n", uid );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
warn( "Warning: cannot find newly stored message %." stringify(TUIDL) "s on %s.\n", srec->tuid, str_ms[t] );
|
|
|
|
uid = 0;
|
|
|
|
break;
|
|
|
|
}
|
2006-03-21 15:53:43 +00:00
|
|
|
Fprintf( svars->jfp, "%c %d %d %d\n", "<>"[t], srec->uid[M], srec->uid[S], uid );
|
2006-02-03 21:33:43 +00:00
|
|
|
srec->uid[t] = uid;
|
|
|
|
srec->tuid[0] = 0;
|
|
|
|
}
|
|
|
|
}
|
2006-01-29 15:52:49 +00:00
|
|
|
|
2004-03-27 16:07:20 +00:00
|
|
|
debug( "synchronizing old entries\n" );
|
2006-03-21 15:53:43 +00:00
|
|
|
for (srec = svars->srecs; srec != *svars->osrecadd; srec = srec->next) {
|
2006-01-30 10:26:04 +00:00
|
|
|
if (srec->status & (S_DEAD|S_DONE))
|
2004-03-27 16:07:20 +00:00
|
|
|
continue;
|
2005-12-28 10:02:22 +00:00
|
|
|
debug( "pair (%d,%d)\n", srec->uid[M], srec->uid[S] );
|
2006-03-21 15:53:43 +00:00
|
|
|
no[M] = !srec->msg[M] && (svars->ctx[M]->opts & OPEN_OLD);
|
|
|
|
no[S] = !srec->msg[S] && (svars->ctx[S]->opts & OPEN_OLD);
|
|
|
|
if (no[M] && no[S]) {
|
2004-03-27 16:07:20 +00:00
|
|
|
debug( " vanished\n" );
|
|
|
|
/* d.1) d.5) d.6) d.10) d.11) */
|
|
|
|
srec->status = S_DEAD;
|
2006-03-21 15:53:43 +00:00
|
|
|
Fprintf( svars->jfp, "- %d %d\n", srec->uid[M], srec->uid[S] );
|
2004-03-27 16:07:20 +00:00
|
|
|
} else {
|
2006-03-21 15:53:43 +00:00
|
|
|
del[M] = no[M] && (srec->uid[M] > 0);
|
|
|
|
del[S] = no[S] && (srec->uid[S] > 0);
|
2004-03-27 16:07:20 +00:00
|
|
|
|
2005-12-28 10:02:22 +00:00
|
|
|
for (t = 0; t < 2; t++) {
|
2006-02-02 17:03:01 +00:00
|
|
|
srec->aflags[t] = srec->dflags[t] = 0;
|
|
|
|
if (srec->msg[t] && (srec->msg[t]->flags & F_DELETED))
|
|
|
|
srec->status |= S_DEL(t);
|
2005-12-28 10:02:22 +00:00
|
|
|
/* 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) */
|
2006-02-02 17:03:01 +00:00
|
|
|
if (srec->msg[t] && (srec->msg[t]->status & M_FLAGS) && srec->msg[t]->flags != srec->flags)
|
2005-12-28 10:02:22 +00:00
|
|
|
info( "Info: conflicting changes in (%d,%d)\n", srec->uid[M], srec->uid[S] );
|
2006-03-21 15:53:43 +00:00
|
|
|
if (svars->chan->ops[t] & OP_DELETE) {
|
2006-02-02 11:23:57 +00:00
|
|
|
debug( " %sing delete\n", str_hl[t] );
|
2006-03-21 15:53:43 +00:00
|
|
|
switch (svars->drv[t]->set_flags( svars->ctx[t], srec->msg[t], srec->uid[t], F_DELETED, 0 )) {
|
|
|
|
case DRV_STORE_BAD: svars->ret = SYNC_BAD(t); goto finish;
|
|
|
|
case DRV_BOX_BAD: svars->ret = SYNC_FAIL; goto finish;
|
2005-12-28 10:02:22 +00:00
|
|
|
default: /* ok */ break;
|
|
|
|
case DRV_OK:
|
2006-01-30 13:11:33 +00:00
|
|
|
srec->status |= S_DEL(t);
|
2006-03-21 15:53:43 +00:00
|
|
|
Fprintf( svars->jfp, "%c %d %d 0\n", "><"[t], srec->uid[M], srec->uid[S] );
|
2005-12-28 10:02:22 +00:00
|
|
|
srec->uid[1-t] = 0;
|
|
|
|
}
|
2006-02-02 11:23:57 +00:00
|
|
|
} else
|
|
|
|
debug( " not %sing delete\n", str_hl[t] );
|
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) */
|
|
|
|
;
|
2006-01-30 10:26:04 +00:00
|
|
|
else if (srec->uid[t] < 0)
|
2005-12-28 10:02:22 +00:00
|
|
|
/* b.2) / c.2) */
|
2006-03-21 15:53:43 +00:00
|
|
|
; /* handled as new messages (sort of) */
|
2006-01-30 10:26:04 +00:00
|
|
|
else if (!del[t]) {
|
2005-12-28 10:02:22 +00:00
|
|
|
/* a) & b.3) / c.3) */
|
2006-03-21 15:53:43 +00:00
|
|
|
if (svars->chan->ops[t] & OP_FLAGS) {
|
2005-12-28 19:17:40 +00:00
|
|
|
sflags = srec->msg[1-t]->flags;
|
2006-02-02 17:03:01 +00:00
|
|
|
if ((srec->status & (S_EXPIRE|S_EXPIRED)) && !t)
|
|
|
|
sflags &= ~F_DELETED;
|
|
|
|
srec->aflags[t] = sflags & ~srec->flags;
|
|
|
|
srec->dflags[t] = ~sflags & srec->flags;
|
2006-02-02 11:23:57 +00:00
|
|
|
if (DFlags & DEBUG) {
|
|
|
|
char afbuf[16], dfbuf[16]; /* enlarge when support for keywords is added */
|
2006-02-02 17:03:01 +00:00
|
|
|
make_flags( srec->aflags[t], afbuf );
|
|
|
|
make_flags( srec->dflags[t], dfbuf );
|
2006-02-02 11:23:57 +00:00
|
|
|
debug( " %sing flags: +%s -%s\n", str_hl[t], afbuf, dfbuf );
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
debug( " not %sing flags\n", str_hl[t] );
|
2005-12-28 10:02:22 +00:00
|
|
|
} /* else b.4) / c.4) */
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
2001-11-15 23:59:27 +00:00
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
|
2006-03-21 15:53:43 +00:00
|
|
|
if ((svars->chan->ops[S] & (OP_NEW|OP_RENEW|OP_FLAGS)) && svars->chan->max_messages) {
|
2006-02-02 17:03:01 +00:00
|
|
|
/* Flagged and not yet synced messages older than the first not
|
|
|
|
* expired message are not counted. */
|
2006-03-21 15:53:43 +00:00
|
|
|
todel = svars->ctx[S]->count - svars->chan->max_messages;
|
2006-02-02 17:03:01 +00:00
|
|
|
debug( "scheduling %d excess messages for expiration\n", todel );
|
2006-03-21 15:53:43 +00:00
|
|
|
for (tmsg = svars->ctx[S]->msgs; tmsg && todel > 0; tmsg = tmsg->next)
|
2006-02-02 17:03:01 +00:00
|
|
|
if (!(tmsg->status & M_DEAD) && (srec = tmsg->srec) &&
|
|
|
|
((tmsg->flags | srec->aflags[S]) & ~srec->dflags[S] & F_DELETED) &&
|
|
|
|
!(srec->status & (S_EXPIRE|S_EXPIRED)))
|
2004-03-27 16:07:20 +00:00
|
|
|
todel--;
|
2006-02-02 17:03:01 +00:00
|
|
|
debug( "%d non-deleted excess messages\n", todel );
|
2006-03-21 15:53:43 +00:00
|
|
|
for (tmsg = svars->ctx[S]->msgs; tmsg; tmsg = tmsg->next) {
|
2006-02-02 17:03:01 +00:00
|
|
|
if (tmsg->status & M_DEAD)
|
2004-03-27 16:07:20 +00:00
|
|
|
continue;
|
2006-02-02 17:03:01 +00:00
|
|
|
if (!(srec = tmsg->srec) || srec->uid[M] <= 0)
|
2004-03-27 16:07:20 +00:00
|
|
|
todel--;
|
2006-02-02 17:03:01 +00:00
|
|
|
else {
|
|
|
|
nflags = (tmsg->flags | srec->aflags[S]) & ~srec->dflags[S];
|
|
|
|
if (!(nflags & F_DELETED) || (srec->status & (S_EXPIRE|S_EXPIRED))) {
|
|
|
|
if (nflags & F_FLAGGED)
|
|
|
|
todel--;
|
|
|
|
else if (!(tmsg->status & M_RECENT) &&
|
|
|
|
(todel > 0 ||
|
|
|
|
((srec->status & (S_EXPIRE|S_EXPIRED)) == (S_EXPIRE|S_EXPIRED)) ||
|
|
|
|
((srec->status & (S_EXPIRE|S_EXPIRED)) && (tmsg->flags & F_DELETED)))) {
|
|
|
|
srec->status |= S_NEXPIRE;
|
|
|
|
debug( " pair(%d,%d)\n", srec->uid[M], srec->uid[S] );
|
|
|
|
todel--;
|
|
|
|
}
|
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
}
|
2006-02-02 17:03:01 +00:00
|
|
|
debug( "%d excess messages remain\n", todel );
|
2006-03-21 15:53:43 +00:00
|
|
|
for (srec = svars->srecs; srec; srec = srec->next) {
|
2006-02-02 17:03:01 +00:00
|
|
|
if ((srec->status & (S_DEAD|S_DONE)) || !srec->msg[S])
|
|
|
|
continue;
|
|
|
|
nex = (srec->status / S_NEXPIRE) & 1;
|
|
|
|
if (nex != ((srec->status / S_EXPIRED) & 1)) {
|
|
|
|
if (nex != ((srec->status / S_EXPIRE) & 1)) {
|
2006-03-21 15:53:43 +00:00
|
|
|
Fprintf( svars->jfp, "~ %d %d %d\n", srec->uid[M], srec->uid[S], nex );
|
2006-02-02 17:03:01 +00:00
|
|
|
debug( " pair(%d,%d): %d (pre)\n", srec->uid[M], srec->uid[S], nex );
|
|
|
|
srec->status = (srec->status & ~S_EXPIRE) | (nex * S_EXPIRE);
|
|
|
|
} else
|
|
|
|
debug( " pair(%d,%d): %d (pending)\n", srec->uid[M], srec->uid[S], nex );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-03-21 15:53:43 +00:00
|
|
|
|
2006-02-02 17:03:01 +00:00
|
|
|
debug( "synchronizing flags\n" );
|
2006-03-21 15:53:43 +00:00
|
|
|
for (srec = svars->srecs; srec != *svars->osrecadd; srec = srec->next) {
|
2006-02-02 17:03:01 +00:00
|
|
|
if (srec->status & (S_DEAD|S_DONE))
|
|
|
|
continue;
|
|
|
|
for (t = 0; t < 2; t++) {
|
|
|
|
aflags = srec->aflags[t];
|
|
|
|
dflags = srec->dflags[t];
|
2006-03-21 15:53:43 +00:00
|
|
|
if ((t == S) && ((mvBit(srec->status, S_EXPIRE, S_EXPIRED) ^ srec->status) & S_EXPIRED)) {
|
2006-02-02 17:03:01 +00:00
|
|
|
if (srec->status & S_NEXPIRE)
|
|
|
|
aflags |= F_DELETED;
|
|
|
|
else
|
|
|
|
dflags |= F_DELETED;
|
|
|
|
}
|
2006-03-21 15:53:43 +00:00
|
|
|
if ((svars->chan->ops[t] & OP_EXPUNGE) && (((srec->msg[t] ? srec->msg[t]->flags : 0) | aflags) & ~dflags & F_DELETED) &&
|
|
|
|
(!svars->ctx[t]->conf->trash || svars->ctx[t]->conf->trash_only_new))
|
2006-02-02 17:03:01 +00:00
|
|
|
{
|
|
|
|
srec->aflags[t] &= F_DELETED;
|
|
|
|
aflags &= F_DELETED;
|
|
|
|
srec->dflags[t] = dflags = 0;
|
|
|
|
}
|
|
|
|
if (srec->msg[t] && (srec->msg[t]->status & M_FLAGS)) {
|
|
|
|
aflags &= ~srec->msg[t]->flags;
|
|
|
|
dflags &= srec->msg[t]->flags;
|
|
|
|
}
|
2006-03-21 15:53:43 +00:00
|
|
|
switch ((aflags | dflags) ? svars->drv[t]->set_flags( svars->ctx[t], srec->msg[t], srec->uid[t], aflags, dflags ) : DRV_OK) {
|
|
|
|
case DRV_STORE_BAD: svars->ret = SYNC_BAD(t); goto finish;
|
|
|
|
case DRV_BOX_BAD: svars->ret = SYNC_FAIL; goto finish;
|
2006-02-02 17:03:01 +00:00
|
|
|
default: /* ok */ srec->aflags[t] = srec->dflags[t] = 0; break;
|
|
|
|
case DRV_OK:
|
|
|
|
if (aflags & F_DELETED)
|
|
|
|
srec->status |= S_DEL(t);
|
|
|
|
else if (dflags & F_DELETED)
|
|
|
|
srec->status &= ~S_DEL(t);
|
|
|
|
if (t) {
|
|
|
|
nex = (srec->status / S_NEXPIRE) & 1;
|
|
|
|
if (nex != ((srec->status / S_EXPIRED) & 1)) {
|
2006-03-21 15:53:43 +00:00
|
|
|
if (nex && (svars->smaxxuid < srec->uid[S]))
|
|
|
|
svars->smaxxuid = srec->uid[S];
|
|
|
|
Fprintf( svars->jfp, "/ %d %d\n", srec->uid[M], srec->uid[S] );
|
2006-02-02 17:03:01 +00:00
|
|
|
debug( " pair(%d,%d): expired %d (commit)\n", srec->uid[M], srec->uid[S], nex );
|
|
|
|
srec->status = (srec->status & ~S_EXPIRED) | (nex * S_EXPIRED);
|
|
|
|
} else if (nex != ((srec->status / S_EXPIRE) & 1)) {
|
2006-03-21 15:53:43 +00:00
|
|
|
Fprintf( svars->jfp, "\\ %d %d\n", srec->uid[M], srec->uid[S] );
|
2006-02-02 17:03:01 +00:00
|
|
|
debug( " pair(%d,%d): expire %d (cancel)\n", srec->uid[M], srec->uid[S], nex );
|
|
|
|
srec->status = (srec->status & ~S_EXPIRE) | (nex * S_EXPIRE);
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-12-21 18:16:44 +00:00
|
|
|
}
|
2006-02-02 17:03:01 +00:00
|
|
|
nflags = (srec->flags | srec->aflags[M] | srec->aflags[S]) & ~(srec->dflags[M] | srec->dflags[S]);
|
|
|
|
if (srec->flags != nflags) {
|
|
|
|
debug( " pair(%d,%d): updating flags (%u -> %u)\n", srec->uid[M], srec->uid[S], srec->flags, nflags );
|
|
|
|
srec->flags = nflags;
|
2006-03-21 15:53:43 +00:00
|
|
|
Fprintf( svars->jfp, "* %d %d %u\n", srec->uid[M], srec->uid[S], nflags );
|
2006-02-02 17:03:01 +00:00
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
2000-12-21 18:16:44 +00:00
|
|
|
|
2005-12-28 10:02:22 +00:00
|
|
|
for (t = 0; t < 2; t++) {
|
2006-03-21 17:40:31 +00:00
|
|
|
if (svars->chan->ops[t] & OP_EXPUNGE) {
|
|
|
|
if (svars->ctx[t]->conf->trash || (svars->ctx[1-t]->conf->trash && svars->ctx[1-t]->conf->trash_remote_new)) {
|
|
|
|
debug( "trashing in %s\n", str_ms[t] );
|
|
|
|
for (tmsg = svars->ctx[t]->msgs; tmsg; tmsg = tmsg->next)
|
|
|
|
if (tmsg->flags & F_DELETED) {
|
|
|
|
if (svars->ctx[t]->conf->trash) {
|
|
|
|
if (!svars->ctx[t]->conf->trash_only_new || !tmsg->srec || tmsg->srec->uid[1-t] < 0) {
|
|
|
|
debug( " trashing message %d\n", tmsg->uid );
|
|
|
|
switch (svars->drv[t]->trash_msg( svars->ctx[t], tmsg )) {
|
|
|
|
case DRV_OK: break;
|
|
|
|
case DRV_STORE_BAD: svars->ret = SYNC_BAD(t); goto finish;
|
|
|
|
default: svars->ret = SYNC_FAIL; goto nexex;
|
2005-12-28 10:02:22 +00:00
|
|
|
}
|
|
|
|
} else
|
2006-03-21 17:40:31 +00:00
|
|
|
debug( " not trashing message %d - not new\n", tmsg->uid );
|
|
|
|
} else {
|
|
|
|
if (!tmsg->srec || tmsg->srec->uid[1-t] < 0) {
|
|
|
|
if (!svars->ctx[1-t]->conf->max_size || tmsg->size <= svars->ctx[1-t]->conf->max_size) {
|
|
|
|
debug( " remote trashing message %d\n", tmsg->uid );
|
|
|
|
switch ((svars->ret = copy_msg( svars->ctx, 1 - t, tmsg, 0, 0 ))) {
|
|
|
|
case SYNC_OK: break;
|
|
|
|
case SYNC_NOGOOD: svars->ret = SYNC_FAIL; goto nexex;
|
|
|
|
case SYNC_FAIL: goto nexex;
|
|
|
|
default: goto finish;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
debug( " not remote trashing message %d - too big\n", tmsg->uid );
|
|
|
|
} else
|
|
|
|
debug( " not remote trashing message %d - not new\n", tmsg->uid );
|
|
|
|
}
|
2005-12-28 10:02:22 +00:00
|
|
|
}
|
2006-03-21 17:40:31 +00:00
|
|
|
}
|
2005-12-28 10:02:22 +00:00
|
|
|
|
2006-03-21 16:03:09 +00:00
|
|
|
info( "Expunging %s...\n", str_ms[t] );
|
|
|
|
debug( "expunging %s\n", str_ms[t] );
|
2006-03-21 15:53:43 +00:00
|
|
|
switch (svars->drv[t]->close( svars->ctx[t] )) {
|
|
|
|
case DRV_OK: svars->state[t] |= ST_DID_EXPUNGE; break;
|
|
|
|
case DRV_STORE_BAD: svars->ret = SYNC_BAD(t); goto finish;
|
2005-12-28 10:02:22 +00:00
|
|
|
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
|
|
|
}
|
2006-03-21 15:53:43 +00:00
|
|
|
if ((svars->state[M] | svars->state[S]) & ST_DID_EXPUNGE) {
|
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;
|
2006-03-21 15:53:43 +00:00
|
|
|
if (svars->smaxxuid) {
|
|
|
|
debug( "preparing entry purge - max expired slave uid is %d\n", svars->smaxxuid );
|
|
|
|
for (srec = svars->srecs; srec; srec = srec->next) {
|
2004-03-27 16:07:20 +00:00
|
|
|
if (srec->status & S_DEAD)
|
|
|
|
continue;
|
2006-03-21 15:53:43 +00:00
|
|
|
if (!((srec->uid[S] <= 0 || ((srec->status & S_DEL(S)) && (svars->state[S] & ST_DID_EXPUNGE))) &&
|
|
|
|
(srec->uid[M] <= 0 || ((srec->status & S_DEL(M)) && (svars->state[M] & ST_DID_EXPUNGE)) || (srec->status & S_EXPIRED))) &&
|
|
|
|
svars->smaxxuid < srec->uid[S] && 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 );
|
2002-01-16 19:47:28 +00:00
|
|
|
}
|
2000-12-21 18:16:44 +00:00
|
|
|
|
2006-03-21 15:53:43 +00:00
|
|
|
for (srec = svars->srecs; srec; srec = srec->next) {
|
2004-03-27 16:07:20 +00:00
|
|
|
if (srec->status & S_DEAD)
|
|
|
|
continue;
|
2006-03-21 15:53:43 +00:00
|
|
|
if (srec->uid[S] <= 0 || ((srec->status & S_DEL(S)) && (svars->state[S] & ST_DID_EXPUNGE))) {
|
|
|
|
if (srec->uid[M] <= 0 || ((srec->status & S_DEL(M)) && (svars->state[M] & ST_DID_EXPUNGE)) ||
|
|
|
|
((srec->status & S_EXPIRED) && svars->maxuid[M] >= srec->uid[M] && minwuid > srec->uid[M])) {
|
2005-12-28 10:02:22 +00:00
|
|
|
debug( " -> killing (%d,%d)\n", srec->uid[M], srec->uid[S] );
|
2004-03-27 16:07:20 +00:00
|
|
|
srec->status = S_DEAD;
|
2006-03-21 15:53:43 +00:00
|
|
|
Fprintf( svars->jfp, "- %d %d\n", srec->uid[M], srec->uid[S] );
|
2006-02-02 10:04:05 +00:00
|
|
|
} else if (srec->uid[S] > 0) {
|
|
|
|
debug( " -> orphaning (%d,[%d])\n", srec->uid[M], srec->uid[S] );
|
2006-03-21 15:53:43 +00:00
|
|
|
Fprintf( svars->jfp, "> %d %d 0\n", srec->uid[M], srec->uid[S] );
|
2006-02-02 10:04:05 +00:00
|
|
|
srec->uid[S] = 0;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
2006-03-21 15:53:43 +00:00
|
|
|
} else if (srec->uid[M] > 0 && ((srec->status & S_DEL(M)) && (svars->state[M] & ST_DID_EXPUNGE))) {
|
2006-02-02 10:04:05 +00:00
|
|
|
debug( " -> orphaning ([%d],%d)\n", srec->uid[M], srec->uid[S] );
|
2006-03-21 15:53:43 +00:00
|
|
|
Fprintf( svars->jfp, "< %d %d 0\n", srec->uid[M], srec->uid[S] );
|
2006-02-02 10:04:05 +00:00
|
|
|
srec->uid[M] = 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
|
|
|
|
2006-03-21 15:53:43 +00:00
|
|
|
Fprintf( svars->nfp, "%d:%d %d:%d:%d\n", svars->uidval[M], svars->maxuid[M], svars->uidval[S], svars->smaxxuid, svars->maxuid[S] );
|
|
|
|
for (srec = svars->srecs; srec; srec = srec->next) {
|
2004-03-27 16:07:20 +00:00
|
|
|
if (srec->status & S_DEAD)
|
|
|
|
continue;
|
|
|
|
make_flags( srec->flags, fbuf );
|
2006-03-21 15:53:43 +00:00
|
|
|
Fprintf( svars->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
|
|
|
|
2006-03-21 15:53:43 +00:00
|
|
|
Fclose( svars->nfp );
|
|
|
|
Fclose( svars->jfp );
|
2006-01-29 15:46:09 +00:00
|
|
|
if (!(DFlags & KEEPJOURNAL)) {
|
|
|
|
/* order is important! */
|
2006-03-21 15:53:43 +00:00
|
|
|
rename( svars->nname, svars->dname );
|
|
|
|
unlink( svars->jname );
|
2006-01-29 15:46:09 +00:00
|
|
|
}
|
2002-12-28 04:12:07 +00:00
|
|
|
|
2004-03-27 16:07:20 +00:00
|
|
|
bail:
|
2006-03-21 15:53:43 +00:00
|
|
|
for (srec = svars->srecs; srec; srec = nsrec) {
|
2004-03-27 16:07:20 +00:00
|
|
|
nsrec = srec->next;
|
|
|
|
free( srec );
|
|
|
|
}
|
2006-03-21 15:53:43 +00:00
|
|
|
unlink( svars->lname );
|
2004-03-27 16:07:20 +00:00
|
|
|
bail1:
|
2006-03-21 15:53:43 +00:00
|
|
|
close( svars->lfd );
|
2004-03-27 16:07:20 +00:00
|
|
|
bail2:
|
2006-03-21 15:53:43 +00:00
|
|
|
free( svars->lname );
|
|
|
|
free( svars->nname );
|
|
|
|
free( svars->jname );
|
|
|
|
free( svars->dname );
|
|
|
|
return svars->ret;
|
2006-02-11 20:02:06 +00:00
|
|
|
|
|
|
|
finish:
|
2006-03-21 15:53:43 +00:00
|
|
|
Fclose( svars->nfp );
|
|
|
|
Fclose( svars->jfp );
|
2006-02-11 20:02:06 +00:00
|
|
|
goto bail;
|
2000-12-20 21:41:21 +00:00
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
|