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>
|
2011-04-10 17:25:46 +00:00
|
|
|
* Copyright (C) 2002-2006,2010-2012 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
|
2011-04-10 17:34:36 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
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>
|
2006-03-21 20:03:21 +00:00
|
|
|
#include <stddef.h>
|
2000-12-20 21:41:21 +00:00
|
|
|
#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
|
|
|
|
2012-09-15 11:25:50 +00:00
|
|
|
#ifndef _POSIX_SYNCHRONIZED_IO
|
|
|
|
# define fdatasync fsync
|
|
|
|
#endif
|
|
|
|
|
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
|
2012-09-15 11:25:50 +00:00
|
|
|
Fclose( FILE *f, int safe )
|
2006-02-11 19:52:53 +00:00
|
|
|
{
|
2012-09-15 13:15:22 +00:00
|
|
|
if ((safe && (fflush( f ) || (FSyncLevel >= FSYNC_NORMAL && fdatasync( fileno( f ) )))) || fclose( f ) == EOF) {
|
2011-04-10 13:32:25 +00:00
|
|
|
sys_error( "Error: cannot close file. Disk full?" );
|
2006-02-11 19:52:53 +00:00
|
|
|
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) {
|
2011-04-10 13:32:25 +00:00
|
|
|
sys_error( "Error: cannot write file. Disk full?" );
|
2004-11-13 09:19:36 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2006-03-21 20:03:21 +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)
|
2011-04-10 11:06:07 +00:00
|
|
|
#define S_FIND (1<<8)
|
2006-02-02 17:03:01 +00:00
|
|
|
|
|
|
|
#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;
|
|
|
|
|
|
|
|
|
|
|
|
/* 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 {
|
2006-03-21 20:03:21 +00:00
|
|
|
int t[2];
|
|
|
|
void (*cb)( int sts, void *aux ), *aux;
|
2006-03-21 15:53:43 +00:00
|
|
|
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];
|
2012-08-26 11:36:12 +00:00
|
|
|
int state[2], ref_count, nsrecs, ret, lfd;
|
2006-03-21 20:03:21 +00:00
|
|
|
int new_total[2], new_done[2];
|
|
|
|
int flags_total[2], flags_done[2];
|
|
|
|
int trash_total[2], trash_done[2];
|
2011-04-03 16:21:46 +00:00
|
|
|
int maxuid[2]; /* highest UID that was already propagated */
|
|
|
|
int uidval[2]; /* UID validity value */
|
2011-04-10 11:06:07 +00:00
|
|
|
int uidnext[2]; /* next expected UID; TUID lookup makes sense only for lower UIDs */
|
2011-04-03 16:21:46 +00:00
|
|
|
int smaxxuid; /* highest expired UID on slave */
|
2006-03-21 15:53:43 +00:00
|
|
|
} sync_vars_t;
|
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
static void sync_ref( sync_vars_t *svars ) { ++svars->ref_count; }
|
|
|
|
static int sync_deref( sync_vars_t *svars );
|
|
|
|
static int deref_check_cancel( sync_vars_t *svars );
|
|
|
|
static int check_cancel( sync_vars_t *svars );
|
|
|
|
|
|
|
|
#define DRIVER_CALL_RET(call) \
|
|
|
|
do { \
|
|
|
|
sync_ref( svars ); \
|
|
|
|
svars->drv[t]->call; \
|
|
|
|
return deref_check_cancel( svars ); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define DRIVER_CALL(call) \
|
|
|
|
do { \
|
|
|
|
sync_ref( svars ); \
|
|
|
|
svars->drv[t]->call; \
|
|
|
|
if (deref_check_cancel( svars )) \
|
|
|
|
return; \
|
|
|
|
} while (0)
|
|
|
|
|
2006-03-21 20:03:21 +00:00
|
|
|
#define AUX &svars->t[t]
|
2012-06-17 12:52:46 +00:00
|
|
|
#define DECL_SVARS \
|
|
|
|
int t; \
|
|
|
|
sync_vars_t *svars
|
|
|
|
#define INIT_SVARS(aux) \
|
|
|
|
t = *(int *)aux; \
|
|
|
|
svars = (sync_vars_t *)(((char *)(&((int *)aux)[-t])) - offsetof(sync_vars_t, t))
|
|
|
|
#define DECL_INIT_SVARS(aux) \
|
2006-03-21 20:03:21 +00:00
|
|
|
int t = *(int *)aux; \
|
2012-06-17 12:52:46 +00:00
|
|
|
sync_vars_t *svars = (sync_vars_t *)(((char *)(&((int *)aux)[-t])) - offsetof(sync_vars_t, t))
|
2006-03-21 20:03:21 +00:00
|
|
|
|
|
|
|
/* operation dependencies:
|
|
|
|
select(S): -
|
2011-04-10 11:06:07 +00:00
|
|
|
select(M): select(S) | -
|
|
|
|
new(M), new(S), flags(M): select(M) & select(S)
|
2006-03-21 20:03:21 +00:00
|
|
|
flags(S): count(new(S))
|
|
|
|
find_new(x): new(x)
|
|
|
|
trash(x): flags(x)
|
|
|
|
close(x): trash(x) & find_new(x) // with expunge
|
|
|
|
cleanup: close(M) & close(S)
|
|
|
|
*/
|
|
|
|
|
2011-04-10 11:06:07 +00:00
|
|
|
#define ST_LOADED (1<<0)
|
2006-03-21 20:03:21 +00:00
|
|
|
#define ST_SENT_NEW (1<<1)
|
2011-04-10 11:06:07 +00:00
|
|
|
#define ST_FOUND_NEW (1<<2)
|
2006-03-21 20:03:21 +00:00
|
|
|
#define ST_SENT_FLAGS (1<<3)
|
|
|
|
#define ST_SENT_TRASH (1<<4)
|
|
|
|
#define ST_CLOSED (1<<5)
|
2011-04-03 14:29:18 +00:00
|
|
|
#define ST_SENT_CANCEL (1<<6)
|
|
|
|
#define ST_CANCELED (1<<7)
|
2011-07-23 14:06:32 +00:00
|
|
|
#define ST_SELECTED (1<<8)
|
2006-03-21 20:03:21 +00:00
|
|
|
|
2006-03-21 15:53:43 +00:00
|
|
|
#define ST_DID_EXPUNGE (1<<16)
|
|
|
|
|
2006-03-21 20:03:21 +00:00
|
|
|
|
2011-04-10 11:06:07 +00:00
|
|
|
static void
|
|
|
|
match_tuids( sync_vars_t *svars, int t )
|
|
|
|
{
|
|
|
|
sync_rec_t *srec;
|
|
|
|
message_t *tmsg, *ntmsg = 0;
|
|
|
|
const char *diag;
|
|
|
|
|
|
|
|
for (srec = svars->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 );
|
|
|
|
for (tmsg = ntmsg; tmsg; tmsg = tmsg->next) {
|
|
|
|
if (tmsg->status & M_DEAD)
|
|
|
|
continue;
|
|
|
|
if (tmsg->tuid[0] && !memcmp( tmsg->tuid, srec->tuid, TUIDL )) {
|
|
|
|
diag = (tmsg == ntmsg) ? "adjacently" : "after gap";
|
|
|
|
goto mfound;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (tmsg = svars->ctx[t]->msgs; tmsg != ntmsg; tmsg = tmsg->next) {
|
|
|
|
if (tmsg->status & M_DEAD)
|
|
|
|
continue;
|
|
|
|
if (tmsg->tuid[0] && !memcmp( tmsg->tuid, srec->tuid, TUIDL )) {
|
|
|
|
diag = "after reset";
|
|
|
|
goto mfound;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug( " -> TUID lost\n" );
|
|
|
|
Fprintf( svars->jfp, "& %d %d\n", srec->uid[M], srec->uid[S] );
|
|
|
|
srec->flags = 0;
|
|
|
|
srec->tuid[0] = 0;
|
|
|
|
continue;
|
|
|
|
mfound:
|
|
|
|
debug( " -> new UID %d %s\n", tmsg->uid, diag );
|
|
|
|
Fprintf( svars->jfp, "%c %d %d %d\n", "<>"[t], srec->uid[M], srec->uid[S], tmsg->uid );
|
|
|
|
tmsg->srec = srec;
|
|
|
|
ntmsg = tmsg->next;
|
|
|
|
srec->uid[t] = tmsg->uid;
|
|
|
|
srec->tuid[0] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-21 20:03:21 +00:00
|
|
|
typedef struct copy_vars {
|
2012-07-29 21:14:48 +00:00
|
|
|
void (*cb)( int sts, int uid, struct copy_vars *vars );
|
2006-03-21 20:03:21 +00:00
|
|
|
void *aux;
|
|
|
|
sync_rec_t *srec; /* also ->tuid */
|
|
|
|
message_t *msg;
|
|
|
|
msg_data_t data;
|
|
|
|
} copy_vars_t;
|
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
static void msg_fetched( int sts, void *aux );
|
2006-03-21 20:03:21 +00:00
|
|
|
|
2006-12-09 10:39:30 +00:00
|
|
|
static int
|
2006-03-21 20:03:21 +00:00
|
|
|
copy_msg( copy_vars_t *vars )
|
|
|
|
{
|
2012-06-17 12:52:46 +00:00
|
|
|
DECL_INIT_SVARS(vars->aux);
|
2006-03-21 20:03:21 +00:00
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
t ^= 1;
|
2006-03-21 20:03:21 +00:00
|
|
|
vars->data.flags = vars->msg->flags;
|
2012-07-29 21:14:48 +00:00
|
|
|
DRIVER_CALL_RET(fetch_msg( svars->ctx[t], vars->msg, &vars->data, msg_fetched, vars ));
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
static void msg_stored( int sts, int uid, void *aux );
|
2006-03-21 20:03:21 +00:00
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
static void
|
2006-03-21 20:03:21 +00:00
|
|
|
msg_fetched( int sts, void *aux )
|
|
|
|
{
|
|
|
|
copy_vars_t *vars = (copy_vars_t *)aux;
|
2012-06-17 12:52:46 +00:00
|
|
|
DECL_SVARS;
|
2006-03-21 20:03:21 +00:00
|
|
|
char *fmap, *buf;
|
2011-03-27 08:52:47 +00:00
|
|
|
int i, len, extra, scr, tcr, lcrs, hcrs, bcrs, lines;
|
2006-03-21 20:03:21 +00:00
|
|
|
int start, sbreak = 0, ebreak = 0;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
switch (sts) {
|
|
|
|
case DRV_OK:
|
2012-06-17 12:52:46 +00:00
|
|
|
INIT_SVARS(vars->aux);
|
2012-07-29 21:15:12 +00:00
|
|
|
if (check_cancel( svars )) {
|
|
|
|
free( vars->data.data );
|
|
|
|
vars->cb( SYNC_CANCELED, 0, vars );
|
|
|
|
return;
|
|
|
|
}
|
2012-06-17 12:52:46 +00:00
|
|
|
|
2006-03-21 20:03:21 +00:00
|
|
|
vars->msg->flags = vars->data.flags;
|
|
|
|
|
|
|
|
scr = (svars->drv[1-t]->flags / DRV_CRLF) & 1;
|
|
|
|
tcr = (svars->drv[t]->flags / DRV_CRLF) & 1;
|
|
|
|
if (vars->srec || scr != tcr) {
|
|
|
|
fmap = vars->data.data;
|
|
|
|
len = vars->data.len;
|
2011-03-27 08:52:47 +00:00
|
|
|
extra = lines = hcrs = bcrs = i = 0;
|
2006-03-21 20:03:21 +00:00
|
|
|
if (vars->srec) {
|
|
|
|
nloop:
|
|
|
|
start = i;
|
2010-02-06 09:34:41 +00:00
|
|
|
lcrs = 0;
|
2006-03-21 20:03:21 +00:00
|
|
|
while (i < len) {
|
|
|
|
c = fmap[i++];
|
|
|
|
if (c == '\r')
|
2010-02-06 09:34:41 +00:00
|
|
|
lcrs++;
|
2006-03-21 20:03:21 +00:00
|
|
|
else if (c == '\n') {
|
|
|
|
if (!memcmp( fmap + start, "X-TUID: ", 8 )) {
|
2010-02-06 09:34:41 +00:00
|
|
|
extra = (sbreak = start) - (ebreak = i);
|
2006-03-21 20:03:21 +00:00
|
|
|
goto oke;
|
|
|
|
}
|
2010-02-06 09:34:41 +00:00
|
|
|
lines++;
|
2011-03-27 08:52:47 +00:00
|
|
|
hcrs += lcrs;
|
2010-02-06 09:34:41 +00:00
|
|
|
if (i - lcrs - 1 == start) {
|
|
|
|
sbreak = ebreak = start;
|
2006-08-10 07:01:02 +00:00
|
|
|
goto oke;
|
|
|
|
}
|
2006-03-21 20:03:21 +00:00
|
|
|
goto nloop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* invalid message */
|
2010-02-06 09:32:10 +00:00
|
|
|
warn( "Warning: message %d from %s has incomplete header.\n",
|
|
|
|
vars->msg->uid, str_ms[1-t] );
|
2006-03-21 20:03:21 +00:00
|
|
|
free( fmap );
|
2012-07-29 21:14:48 +00:00
|
|
|
vars->cb( SYNC_NOGOOD, 0, vars );
|
|
|
|
return;
|
2010-02-06 09:34:41 +00:00
|
|
|
oke:
|
2011-03-27 09:01:22 +00:00
|
|
|
extra += 8 + TUIDL + 1 + (tcr && (!scr || hcrs));
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
2010-02-06 09:34:41 +00:00
|
|
|
if (tcr != scr) {
|
2006-03-21 20:03:21 +00:00
|
|
|
for (; i < len; i++) {
|
|
|
|
c = fmap[i];
|
|
|
|
if (c == '\r')
|
2011-03-27 08:52:47 +00:00
|
|
|
bcrs++;
|
2006-03-21 20:03:21 +00:00
|
|
|
else if (c == '\n')
|
2010-02-06 09:34:41 +00:00
|
|
|
lines++;
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
2011-03-27 08:52:47 +00:00
|
|
|
extra -= hcrs + bcrs;
|
2010-02-06 09:34:41 +00:00
|
|
|
if (tcr)
|
|
|
|
extra += lines;
|
|
|
|
}
|
2006-03-21 20:03:21 +00:00
|
|
|
|
|
|
|
vars->data.len = len + extra;
|
|
|
|
buf = vars->data.data = nfmalloc( vars->data.len );
|
|
|
|
i = 0;
|
|
|
|
if (vars->srec) {
|
2010-02-06 09:34:41 +00:00
|
|
|
if (tcr != scr) {
|
|
|
|
if (tcr) {
|
|
|
|
for (; i < sbreak; i++)
|
|
|
|
if ((c = fmap[i]) != '\r') {
|
|
|
|
if (c == '\n')
|
|
|
|
*buf++ = '\r';
|
|
|
|
*buf++ = c;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (; i < sbreak; i++)
|
|
|
|
if ((c = fmap[i]) != '\r')
|
|
|
|
*buf++ = c;
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
memcpy( buf, fmap, sbreak );
|
|
|
|
buf += sbreak;
|
|
|
|
}
|
2010-02-06 09:34:41 +00:00
|
|
|
|
2006-03-21 20:03:21 +00:00
|
|
|
memcpy( buf, "X-TUID: ", 8 );
|
|
|
|
buf += 8;
|
|
|
|
memcpy( buf, vars->srec->tuid, TUIDL );
|
|
|
|
buf += TUIDL;
|
2011-03-27 09:01:22 +00:00
|
|
|
if (tcr && (!scr || hcrs))
|
2006-03-21 20:03:21 +00:00
|
|
|
*buf++ = '\r';
|
|
|
|
*buf++ = '\n';
|
|
|
|
i = ebreak;
|
|
|
|
}
|
2010-02-06 09:34:41 +00:00
|
|
|
if (tcr != scr) {
|
|
|
|
if (tcr) {
|
|
|
|
for (; i < len; i++)
|
|
|
|
if ((c = fmap[i]) != '\r') {
|
|
|
|
if (c == '\n')
|
|
|
|
*buf++ = '\r';
|
|
|
|
*buf++ = c;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (; i < len; i++)
|
|
|
|
if ((c = fmap[i]) != '\r')
|
|
|
|
*buf++ = c;
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
memcpy( buf, fmap + i, len - i );
|
|
|
|
|
|
|
|
free( fmap );
|
|
|
|
}
|
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
svars->drv[t]->store_msg( svars->ctx[t], &vars->data, !vars->srec, msg_stored, vars );
|
|
|
|
break;
|
2006-03-21 20:03:21 +00:00
|
|
|
case DRV_CANCELED:
|
2012-07-29 21:14:48 +00:00
|
|
|
vars->cb( SYNC_CANCELED, 0, vars );
|
|
|
|
break;
|
2006-03-21 20:03:21 +00:00
|
|
|
case DRV_MSG_BAD:
|
2012-07-29 21:14:48 +00:00
|
|
|
vars->cb( SYNC_NOGOOD, 0, vars );
|
|
|
|
break;
|
2006-03-21 20:03:21 +00:00
|
|
|
default:
|
2012-07-29 21:14:48 +00:00
|
|
|
vars->cb( SYNC_FAIL, 0, vars );
|
|
|
|
break;
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
static void
|
2006-03-21 20:03:21 +00:00
|
|
|
msg_stored( int sts, int uid, void *aux )
|
|
|
|
{
|
|
|
|
copy_vars_t *vars = (copy_vars_t *)aux;
|
2012-06-17 12:52:46 +00:00
|
|
|
DECL_SVARS;
|
2006-03-21 20:03:21 +00:00
|
|
|
|
|
|
|
switch (sts) {
|
|
|
|
case DRV_OK:
|
2012-07-29 21:14:48 +00:00
|
|
|
vars->cb( SYNC_OK, uid, vars );
|
|
|
|
break;
|
2006-03-21 20:03:21 +00:00
|
|
|
case DRV_CANCELED:
|
2012-07-29 21:14:48 +00:00
|
|
|
vars->cb( SYNC_CANCELED, 0, vars );
|
|
|
|
break;
|
2010-02-06 09:32:10 +00:00
|
|
|
case DRV_MSG_BAD:
|
2012-06-17 12:52:46 +00:00
|
|
|
INIT_SVARS(vars->aux);
|
|
|
|
(void)svars;
|
2010-02-06 09:32:10 +00:00
|
|
|
warn( "Warning: %s refuses to store message %d from %s.\n",
|
|
|
|
str_ms[t], vars->msg->uid, str_ms[1-t] );
|
2012-07-29 21:14:48 +00:00
|
|
|
vars->cb( SYNC_NOGOOD, 0, vars );
|
|
|
|
break;
|
2006-03-21 20:03:21 +00:00
|
|
|
default:
|
2012-07-29 21:14:48 +00:00
|
|
|
vars->cb( SYNC_FAIL, 0, vars );
|
|
|
|
break;
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
stats( sync_vars_t *svars )
|
|
|
|
{
|
|
|
|
char buf[2][64];
|
|
|
|
char *cs;
|
|
|
|
int t, l;
|
|
|
|
static int cols = -1;
|
|
|
|
|
|
|
|
if (cols < 0 && (!(cs = getenv( "COLUMNS" )) || !(cols = atoi( cs ) / 2)))
|
|
|
|
cols = 36;
|
|
|
|
if (!(DFlags & QUIET)) {
|
|
|
|
for (t = 0; t < 2; t++) {
|
2011-04-10 11:06:07 +00:00
|
|
|
l = sprintf( buf[t], "+%d/%d *%d/%d #%d/%d",
|
2006-03-21 20:03:21 +00:00
|
|
|
svars->new_done[t], svars->new_total[t],
|
|
|
|
svars->flags_done[t], svars->flags_total[t],
|
|
|
|
svars->trash_done[t], svars->trash_total[t] );
|
|
|
|
if (l > cols)
|
|
|
|
buf[t][cols - 1] = '~';
|
|
|
|
}
|
2012-09-01 15:21:32 +00:00
|
|
|
infon( "\v\rM: %.*s S: %.*s", cols, buf[0], cols, buf[1] );
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void sync_bail( sync_vars_t *svars );
|
|
|
|
static void sync_bail1( sync_vars_t *svars );
|
|
|
|
static void sync_bail2( sync_vars_t *svars );
|
2012-08-18 11:58:14 +00:00
|
|
|
static void sync_bail3( sync_vars_t *svars );
|
2012-07-22 15:32:32 +00:00
|
|
|
static void cancel_done( void *aux );
|
2006-03-21 20:03:21 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
cancel_sync( sync_vars_t *svars )
|
|
|
|
{
|
|
|
|
int t;
|
|
|
|
|
2012-07-22 15:46:54 +00:00
|
|
|
for (t = 0; t < 2; t++) {
|
|
|
|
int other_state = svars->state[1-t];
|
2012-07-22 15:32:32 +00:00
|
|
|
if (svars->ret & SYNC_BAD(t)) {
|
|
|
|
cancel_done( AUX );
|
2011-04-03 14:29:18 +00:00
|
|
|
} else if (!(svars->state[t] & ST_SENT_CANCEL)) {
|
|
|
|
/* ignore subsequent failures from in-flight commands */
|
|
|
|
svars->state[t] |= ST_SENT_CANCEL;
|
2006-03-21 20:03:21 +00:00
|
|
|
svars->drv[t]->cancel( svars->ctx[t], cancel_done, AUX );
|
2012-07-22 15:32:32 +00:00
|
|
|
}
|
2012-07-22 15:46:54 +00:00
|
|
|
if (other_state & ST_CANCELED)
|
|
|
|
break;
|
|
|
|
}
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-07-22 15:32:32 +00:00
|
|
|
cancel_done( void *aux )
|
2006-03-21 20:03:21 +00:00
|
|
|
{
|
2012-06-17 12:52:46 +00:00
|
|
|
DECL_INIT_SVARS(aux);
|
2006-03-21 20:03:21 +00:00
|
|
|
|
|
|
|
svars->state[t] |= ST_CANCELED;
|
|
|
|
if (svars->state[1-t] & ST_CANCELED) {
|
2011-07-23 14:06:32 +00:00
|
|
|
if (svars->lfd) {
|
2012-09-15 11:25:50 +00:00
|
|
|
Fclose( svars->nfp, 0 );
|
|
|
|
Fclose( svars->jfp, 0 );
|
2011-07-23 14:06:32 +00:00
|
|
|
sync_bail( svars );
|
|
|
|
} else {
|
|
|
|
sync_bail2( svars );
|
|
|
|
}
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-15 10:55:04 +00:00
|
|
|
static void
|
|
|
|
store_bad( void *aux )
|
|
|
|
{
|
|
|
|
DECL_INIT_SVARS(aux);
|
|
|
|
|
|
|
|
svars->drv[t]->cancel_store( svars->ctx[t] );
|
|
|
|
svars->ret |= SYNC_BAD(t);
|
|
|
|
cancel_sync( svars );
|
|
|
|
}
|
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
static int
|
|
|
|
deref_check_cancel( sync_vars_t *svars )
|
|
|
|
{
|
|
|
|
if (sync_deref( svars ))
|
|
|
|
return -1;
|
|
|
|
return check_cancel( svars );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
check_cancel( sync_vars_t *svars )
|
|
|
|
{
|
|
|
|
return (svars->state[M] | svars->state[S]) & (ST_SENT_CANCEL | ST_CANCELED);
|
|
|
|
}
|
2006-03-21 20:03:21 +00:00
|
|
|
|
|
|
|
static int
|
2012-06-17 12:52:46 +00:00
|
|
|
check_ret( int sts, void *aux )
|
2006-03-21 20:03:21 +00:00
|
|
|
{
|
2012-06-17 12:52:46 +00:00
|
|
|
DECL_SVARS;
|
|
|
|
|
2012-07-29 21:15:12 +00:00
|
|
|
if (sts == DRV_CANCELED)
|
2006-03-21 20:03:21 +00:00
|
|
|
return 1;
|
2012-07-29 21:15:12 +00:00
|
|
|
INIT_SVARS(aux);
|
|
|
|
if (sts == DRV_BOX_BAD) {
|
2006-03-21 20:03:21 +00:00
|
|
|
svars->ret |= SYNC_FAIL;
|
|
|
|
cancel_sync( svars );
|
|
|
|
return 1;
|
|
|
|
}
|
2012-07-29 21:15:12 +00:00
|
|
|
return check_cancel( svars );
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
|
|
|
|
2012-06-17 12:52:46 +00:00
|
|
|
#define SVARS_CHECK_RET \
|
|
|
|
DECL_SVARS; \
|
|
|
|
if (check_ret( sts, aux )) \
|
2012-07-29 21:14:48 +00:00
|
|
|
return; \
|
2012-06-17 12:52:46 +00:00
|
|
|
INIT_SVARS(aux)
|
|
|
|
|
|
|
|
#define SVARS_CHECK_RET_VARS(type) \
|
|
|
|
type *vars = (type *)aux; \
|
|
|
|
DECL_SVARS; \
|
|
|
|
if (check_ret( sts, vars->aux )) { \
|
|
|
|
free( vars ); \
|
2012-07-29 21:14:48 +00:00
|
|
|
return; \
|
2012-06-17 12:52:46 +00:00
|
|
|
} \
|
|
|
|
INIT_SVARS(vars->aux)
|
|
|
|
|
|
|
|
#define SVARS_CHECK_CANCEL_RET \
|
|
|
|
DECL_SVARS; \
|
|
|
|
if (sts == SYNC_CANCELED) { \
|
|
|
|
free( vars ); \
|
2012-07-29 21:14:48 +00:00
|
|
|
return; \
|
2012-06-17 12:52:46 +00:00
|
|
|
} \
|
|
|
|
INIT_SVARS(vars->aux)
|
2006-03-21 20:03:21 +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;
|
|
|
|
}
|
|
|
|
|
2006-03-21 20:03:21 +00:00
|
|
|
|
2006-02-03 21:33:43 +00:00
|
|
|
#define JOURNAL_VERSION "2"
|
2006-02-02 11:07:54 +00:00
|
|
|
|
2011-07-23 14:06:32 +00:00
|
|
|
static void box_selected( int sts, void *aux );
|
2006-03-21 20:03:21 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
sync_boxes( store_t *ctx[], const char *names[], channel_conf_t *chan,
|
|
|
|
void (*cb)( int sts, void *aux ), void *aux )
|
2004-03-27 16:07:20 +00:00
|
|
|
{
|
2006-03-21 20:03:21 +00:00
|
|
|
sync_vars_t *svars;
|
2011-07-23 14:06:32 +00:00
|
|
|
int t;
|
2004-03-27 16:07:20 +00:00
|
|
|
|
2006-03-21 20:03:21 +00:00
|
|
|
svars = nfcalloc( sizeof(*svars) );
|
|
|
|
svars->t[1] = 1;
|
2012-07-29 21:14:48 +00:00
|
|
|
svars->ref_count = 1;
|
2006-03-21 20:03:21 +00:00
|
|
|
svars->cb = cb;
|
|
|
|
svars->aux = aux;
|
2006-03-21 15:53:43 +00:00
|
|
|
svars->ctx[0] = ctx[0];
|
|
|
|
svars->ctx[1] = ctx[1];
|
|
|
|
svars->chan = chan;
|
2008-08-23 07:54:00 +00:00
|
|
|
svars->uidval[0] = svars->uidval[1] = -1;
|
2006-03-21 15:53:43 +00:00
|
|
|
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++) {
|
2012-08-18 11:58:14 +00:00
|
|
|
ctx[t]->orig_name =
|
2005-12-28 10:02:22 +00:00
|
|
|
(!names[t] || (ctx[t]->conf->map_inbox && !strcmp( ctx[t]->conf->map_inbox, names[t] ))) ?
|
|
|
|
"INBOX" : names[t];
|
2012-08-18 11:58:14 +00:00
|
|
|
ctx[t]->name = nfstrdup( ctx[t]->orig_name );
|
|
|
|
if (ctx[t]->conf->flat_delim && map_name( ctx[t]->name, '/', ctx[t]->conf->flat_delim ) < 0) {
|
|
|
|
error( "Error: canonical mailbox name '%s' contains flattened hierarchy delimiter\n", ctx[t]->name );
|
|
|
|
svars->ret = SYNC_FAIL;
|
|
|
|
sync_bail3( svars );
|
|
|
|
return;
|
|
|
|
}
|
2008-08-23 07:54:00 +00:00
|
|
|
ctx[t]->uidvalidity = -1;
|
2012-07-15 10:55:04 +00:00
|
|
|
set_bad_callback( ctx[t], store_bad, AUX );
|
2006-03-21 15:53:43 +00:00
|
|
|
svars->drv[t] = ctx[t]->conf->driver;
|
2012-08-25 13:34:26 +00:00
|
|
|
}
|
|
|
|
/* Both boxes must be fully set up at this point, so that error exit paths
|
|
|
|
* don't run into uninitialized variables. */
|
|
|
|
for (t = 0; t < 2; t++) {
|
2012-08-18 11:58:14 +00:00
|
|
|
info( "Selecting %s %s...\n", str_ms[t], ctx[t]->orig_name );
|
2011-07-23 14:06:32 +00:00
|
|
|
DRIVER_CALL(select( ctx[t], (chan->ops[t] & OP_CREATE) != 0, box_selected, AUX ));
|
2005-12-28 10:02:22 +00:00
|
|
|
}
|
2011-07-23 14:06:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int load_box( sync_vars_t *svars, int t, int minwuid, int *mexcs, int nmexcs );
|
2004-03-27 16:07:20 +00:00
|
|
|
|
2011-07-23 14:06:32 +00:00
|
|
|
static void
|
|
|
|
box_selected( int sts, void *aux )
|
|
|
|
{
|
|
|
|
DECL_SVARS;
|
|
|
|
sync_rec_t *srec, *nsrec;
|
|
|
|
char *s, *cmname, *csname;
|
|
|
|
store_t *ctx[2];
|
|
|
|
channel_conf_t *chan;
|
|
|
|
FILE *jfp;
|
|
|
|
int opts[2], line, t1, t2, t3;
|
|
|
|
struct stat st;
|
|
|
|
struct flock lck;
|
|
|
|
char fbuf[16]; /* enlarge when support for keywords is added */
|
2011-04-10 11:06:07 +00:00
|
|
|
char buf[128], buf1[64], buf2[64];
|
2011-07-23 14:06:32 +00:00
|
|
|
|
|
|
|
if (check_ret( sts, aux ))
|
|
|
|
return;
|
|
|
|
INIT_SVARS(aux);
|
|
|
|
ctx[0] = svars->ctx[0];
|
|
|
|
ctx[1] = svars->ctx[1];
|
|
|
|
svars->state[t] |= ST_SELECTED;
|
|
|
|
if (!(svars->state[1-t] & ST_SELECTED))
|
|
|
|
return;
|
|
|
|
|
|
|
|
chan = svars->chan;
|
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 );
|
2011-04-10 07:58:41 +00:00
|
|
|
sbail:
|
2012-08-18 10:48:08 +00:00
|
|
|
svars->ret = SYNC_FAIL;
|
2011-07-23 14:06:32 +00:00
|
|
|
sync_bail2( svars );
|
2006-03-21 20:03:21 +00:00
|
|
|
return;
|
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 );
|
2011-07-24 18:27:09 +00:00
|
|
|
if (!(s = strrchr( svars->dname, '/' ))) {
|
2011-04-10 13:32:25 +00:00
|
|
|
error( "Error: invalid SyncState location '%s'\n", svars->dname );
|
2011-07-24 18:27:09 +00:00
|
|
|
goto sbail;
|
|
|
|
}
|
|
|
|
*s = 0;
|
|
|
|
if (mkdir( svars->dname, 0700 ) && errno != EEXIST) {
|
2011-04-10 13:32:25 +00:00
|
|
|
sys_error( "Error: cannot create SyncState directory '%s'", svars->dname );
|
2011-07-24 18:27:09 +00:00
|
|
|
goto sbail;
|
|
|
|
}
|
|
|
|
*s = '/';
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
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) {
|
2011-04-10 13:32:25 +00:00
|
|
|
sys_error( "Error: cannot create lock file %s", svars->lname );
|
2006-03-21 15:53:43 +00:00
|
|
|
svars->ret = SYNC_FAIL;
|
2006-03-21 20:03:21 +00:00
|
|
|
sync_bail2( svars );
|
|
|
|
return;
|
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",
|
2012-08-18 11:58:14 +00:00
|
|
|
chan->stores[M]->name, ctx[M]->orig_name, chan->stores[S]->name, ctx[S]->orig_name );
|
2006-03-21 15:53:43 +00:00
|
|
|
svars->ret = SYNC_FAIL;
|
2006-03-21 20:03:21 +00:00
|
|
|
sync_bail1( svars );
|
|
|
|
return;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
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 );
|
2011-04-10 07:58:41 +00:00
|
|
|
jbail:
|
2006-03-21 15:53:43 +00:00
|
|
|
fclose( jfp );
|
2011-04-10 07:58:41 +00:00
|
|
|
bail:
|
2006-03-21 15:53:43 +00:00
|
|
|
svars->ret = SYNC_FAIL;
|
2006-03-21 20:03:21 +00:00
|
|
|
sync_bail( svars );
|
|
|
|
return;
|
2004-11-13 09:19:36 +00:00
|
|
|
}
|
2011-04-10 11:06:07 +00:00
|
|
|
if (sscanf( buf, "%63s %63s", buf1, buf2 ) != 2 ||
|
|
|
|
sscanf( buf1, "%d:%d:%d", &svars->uidval[M], &svars->maxuid[M], &svars->uidnext[M] ) < 2 ||
|
|
|
|
sscanf( buf2, "%d:%d:%d:%d", &svars->uidval[S], &svars->smaxxuid, &svars->maxuid[S], &svars->uidnext[S] ) < 3) {
|
2006-03-21 15:53:43 +00:00
|
|
|
error( "Error: invalid sync state header in %s\n", svars->dname );
|
2011-04-10 07:58:41 +00:00
|
|
|
goto jbail;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
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 );
|
2011-04-10 07:58:41 +00:00
|
|
|
goto jbail;
|
2004-11-13 09:19:36 +00:00
|
|
|
}
|
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 );
|
2011-04-10 07:58:41 +00:00
|
|
|
goto jbail;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
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;
|
2012-08-26 11:36:12 +00:00
|
|
|
svars->nsrecs++;
|
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 );
|
2011-04-10 07:58:41 +00:00
|
|
|
goto bail;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
}
|
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 );
|
2011-04-10 07:58:41 +00:00
|
|
|
goto jbail;
|
2006-02-02 11:07:54 +00:00
|
|
|
}
|
|
|
|
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 );
|
2011-04-10 07:58:41 +00:00
|
|
|
goto jbail;
|
2006-02-02 11:07:54 +00:00
|
|
|
}
|
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 );
|
2011-04-10 07:58:41 +00:00
|
|
|
goto jbail;
|
2004-11-13 09:19:36 +00:00
|
|
|
}
|
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)) :
|
2011-04-10 11:06:07 +00:00
|
|
|
buf[0] == '(' || buf[0] == ')' || 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 );
|
2011-04-10 07:58:41 +00:00
|
|
|
goto jbail;
|
2006-02-02 10:44:19 +00:00
|
|
|
}
|
|
|
|
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;
|
2011-04-10 11:06:07 +00:00
|
|
|
else if (buf[0] == '{')
|
|
|
|
svars->uidnext[M] = t1;
|
|
|
|
else if (buf[0] == '}')
|
|
|
|
svars->uidnext[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;
|
2012-08-26 11:36:12 +00:00
|
|
|
svars->nsrecs++;
|
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 );
|
2011-04-10 07:58:41 +00:00
|
|
|
goto jbail;
|
2006-02-02 10:44:19 +00:00
|
|
|
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 );
|
2011-04-10 07:58:41 +00:00
|
|
|
goto jbail;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose( jfp );
|
|
|
|
} else {
|
|
|
|
if (errno != ENOENT) {
|
2006-03-21 15:53:43 +00:00
|
|
|
error( "Error: cannot read journal %s\n", svars->jname );
|
2011-04-10 07:58:41 +00:00
|
|
|
goto bail;
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
|
|
|
}
|
2011-07-23 14:06:32 +00:00
|
|
|
|
|
|
|
t1 = 0;
|
|
|
|
for (t = 0; t < 2; t++)
|
|
|
|
if (svars->uidval[t] >= 0 && svars->uidval[t] != ctx[t]->uidvalidity) {
|
|
|
|
error( "Error: UIDVALIDITY of %s changed (got %d, expected %d)\n",
|
|
|
|
str_ms[t], ctx[t]->uidvalidity, svars->uidval[t] );
|
|
|
|
t1++;
|
|
|
|
}
|
|
|
|
if (t1)
|
|
|
|
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 );
|
2011-04-10 07:58:41 +00:00
|
|
|
goto bail;
|
2006-02-11 20:28:45 +00:00
|
|
|
}
|
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 );
|
2011-04-10 07:58:41 +00:00
|
|
|
goto bail;
|
2006-02-11 20:28:45 +00:00
|
|
|
}
|
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[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)
|
2011-04-10 11:06:07 +00:00
|
|
|
opts[M] |= OPEN_NEW|OPEN_FIND, svars->state[M] |= S_FIND;
|
2006-02-03 21:33:43 +00:00
|
|
|
else if (srec->uid[S] == -2)
|
2011-04-10 11:06:07 +00:00
|
|
|
opts[S] |= OPEN_NEW|OPEN_FIND, svars->state[S] |= S_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
|
|
|
|
2011-07-23 14:06:32 +00:00
|
|
|
if (!svars->smaxxuid && load_box( svars, M, (ctx[M]->opts & OPEN_OLD) ? 1 : INT_MAX, 0, 0 ))
|
2006-12-09 10:39:30 +00:00
|
|
|
return;
|
2011-07-23 14:06:32 +00:00
|
|
|
load_box( svars, S, (ctx[S]->opts & OPEN_OLD) ? 1 : INT_MAX, 0, 0 );
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
|
|
|
|
2011-07-23 14:06:32 +00:00
|
|
|
static void box_loaded( int sts, void *aux );
|
2006-03-21 20:03:21 +00:00
|
|
|
|
2006-12-09 10:39:30 +00:00
|
|
|
static int
|
2011-07-23 14:06:32 +00:00
|
|
|
load_box( sync_vars_t *svars, int t, int minwuid, int *mexcs, int nmexcs )
|
2006-03-21 20:03:21 +00:00
|
|
|
{
|
|
|
|
sync_rec_t *srec;
|
|
|
|
int maxwuid;
|
|
|
|
|
|
|
|
if (svars->ctx[t]->opts & OPEN_NEW) {
|
|
|
|
if (minwuid > svars->maxuid[t] + 1)
|
|
|
|
minwuid = svars->maxuid[t] + 1;
|
|
|
|
maxwuid = INT_MAX;
|
|
|
|
} else if (svars->ctx[t]->opts & OPEN_OLD) {
|
|
|
|
maxwuid = 0;
|
|
|
|
for (srec = svars->srecs; srec; srec = srec->next)
|
|
|
|
if (!(srec->status & S_DEAD) && srec->uid[t] > maxwuid)
|
|
|
|
maxwuid = srec->uid[t];
|
|
|
|
} else
|
|
|
|
maxwuid = 0;
|
2011-07-23 14:06:32 +00:00
|
|
|
info( "Loading %s...\n", str_ms[t] );
|
|
|
|
debug( maxwuid == INT_MAX ? "loading %s [%d,inf]\n" : "loading %s [%d,%d]\n", str_ms[t], minwuid, maxwuid );
|
2011-04-10 11:06:07 +00:00
|
|
|
DRIVER_CALL_RET(load( svars->ctx[t], minwuid, maxwuid, svars->uidnext[t], mexcs, nmexcs, box_loaded, AUX ));
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
void *aux;
|
|
|
|
sync_rec_t *srec;
|
|
|
|
int aflags, dflags;
|
|
|
|
} flag_vars_t;
|
|
|
|
|
2012-08-26 11:36:12 +00:00
|
|
|
typedef struct {
|
|
|
|
int uid;
|
|
|
|
sync_rec_t *srec;
|
|
|
|
} sync_rec_map_t;
|
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
static void flags_set_del( int sts, void *aux );
|
|
|
|
static void flags_set_sync( int sts, void *aux );
|
2006-03-21 20:03:21 +00:00
|
|
|
static void flags_set_sync_p2( sync_vars_t *svars, sync_rec_t *srec, int t );
|
2006-12-09 10:39:30 +00:00
|
|
|
static int msgs_flags_set( sync_vars_t *svars, int t );
|
2012-07-29 21:14:48 +00:00
|
|
|
static void msg_copied( int sts, int uid, copy_vars_t *vars );
|
2006-03-21 20:03:21 +00:00
|
|
|
static void msg_copied_p2( sync_vars_t *svars, sync_rec_t *srec, int t, message_t *tmsg, int uid );
|
2012-07-29 21:14:48 +00:00
|
|
|
static void msgs_copied( sync_vars_t *svars, int t );
|
2006-03-21 20:03:21 +00:00
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
static void
|
2011-04-10 11:06:07 +00:00
|
|
|
box_loaded( int sts, void *aux )
|
2006-03-21 20:03:21 +00:00
|
|
|
{
|
2011-04-10 11:06:07 +00:00
|
|
|
DECL_SVARS;
|
2012-08-26 11:36:12 +00:00
|
|
|
sync_rec_t *srec;
|
|
|
|
sync_rec_map_t *srecmap;
|
2006-03-21 20:03:21 +00:00
|
|
|
message_t *tmsg;
|
|
|
|
copy_vars_t *cv;
|
|
|
|
flag_vars_t *fv;
|
2012-07-07 15:19:17 +00:00
|
|
|
int uid, minwuid, *mexcs, nmexcs, rmexcs, no[2], del[2], todel, t1, t2;
|
2006-03-21 20:03:21 +00:00
|
|
|
int sflags, nflags, aflags, dflags, nex;
|
2012-08-26 11:36:12 +00:00
|
|
|
unsigned hashsz, idx;
|
2006-03-21 20:03:21 +00:00
|
|
|
char fbuf[16]; /* enlarge when support for keywords is added */
|
|
|
|
|
2011-04-10 11:06:07 +00:00
|
|
|
if (check_ret( sts, aux ))
|
2012-07-29 21:14:48 +00:00
|
|
|
return;
|
2011-04-10 11:06:07 +00:00
|
|
|
INIT_SVARS(aux);
|
|
|
|
svars->state[t] |= ST_LOADED;
|
|
|
|
info( "%s: %d messages, %d recent\n", str_ms[t], svars->ctx[t]->count, svars->ctx[t]->recent );
|
|
|
|
|
|
|
|
if (svars->state[t] & S_FIND) {
|
|
|
|
svars->state[t] &= ~S_FIND;
|
|
|
|
debug( "matching previously copied messages on %s\n", str_ms[t] );
|
|
|
|
match_tuids( svars, t );
|
|
|
|
}
|
|
|
|
Fprintf( svars->jfp, "%c %d\n", "{}"[t], svars->ctx[t]->uidnext );
|
2006-03-21 20:03:21 +00:00
|
|
|
|
2011-04-03 09:29:33 +00:00
|
|
|
debug( "matching messages on %s against sync records\n", str_ms[t] );
|
2012-08-26 11:36:12 +00:00
|
|
|
hashsz = bucketsForSize( svars->nsrecs * 3 );
|
|
|
|
srecmap = nfcalloc( hashsz * sizeof(*srecmap) );
|
|
|
|
for (srec = svars->srecs; srec; srec = srec->next) {
|
|
|
|
if (srec->status & S_DEAD)
|
|
|
|
continue;
|
|
|
|
uid = srec->uid[t];
|
|
|
|
idx = (unsigned)((unsigned)uid * 1103515245U) % hashsz;
|
|
|
|
while (srecmap[idx].uid)
|
|
|
|
if (++idx == hashsz)
|
|
|
|
idx = 0;
|
|
|
|
srecmap[idx].uid = uid;
|
|
|
|
srecmap[idx].srec = srec;
|
|
|
|
}
|
2006-03-21 20:03:21 +00:00
|
|
|
for (tmsg = svars->ctx[t]->msgs; tmsg; tmsg = tmsg->next) {
|
2011-04-10 11:06:07 +00:00
|
|
|
if (tmsg->srec) /* found by TUID */
|
|
|
|
continue;
|
2006-03-21 20:03:21 +00:00
|
|
|
uid = tmsg->uid;
|
|
|
|
if (DFlags & DEBUG) {
|
|
|
|
make_flags( tmsg->flags, fbuf );
|
2012-07-07 15:19:17 +00:00
|
|
|
printf( svars->ctx[t]->opts & OPEN_SIZE ? " message %5d, %-4s, %6lu: " : " message %5d, %-4s: ", uid, fbuf, tmsg->size );
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
2012-08-26 11:36:12 +00:00
|
|
|
idx = (unsigned)((unsigned)uid * 1103515245U) % hashsz;
|
|
|
|
while (srecmap[idx].uid) {
|
|
|
|
if (srecmap[idx].uid == uid) {
|
|
|
|
srec = srecmap[idx].srec;
|
2006-03-21 20:03:21 +00:00
|
|
|
goto found;
|
|
|
|
}
|
2012-08-26 11:36:12 +00:00
|
|
|
if (++idx == hashsz)
|
|
|
|
idx = 0;
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
|
|
|
tmsg->srec = 0;
|
|
|
|
debug( "new\n" );
|
|
|
|
continue;
|
|
|
|
found:
|
|
|
|
tmsg->srec = srec;
|
|
|
|
srec->msg[t] = tmsg;
|
2012-08-26 11:36:12 +00:00
|
|
|
debug( "pairs %5d\n", srec->uid[1-t] );
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
2012-08-26 11:36:12 +00:00
|
|
|
free( srecmap );
|
2006-03-21 20:03:21 +00:00
|
|
|
|
|
|
|
if ((t == S) && svars->smaxxuid) {
|
2006-03-21 15:53:43 +00:00
|
|
|
debug( "preparing master selection - max expired slave uid is %d\n", svars->smaxxuid );
|
2006-03-21 20:03:21 +00:00
|
|
|
mexcs = 0;
|
|
|
|
nmexcs = rmexcs = 0;
|
|
|
|
minwuid = INT_MAX;
|
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;
|
|
|
|
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" );
|
2011-07-23 14:06:32 +00:00
|
|
|
load_box( svars, M, minwuid, mexcs, nmexcs );
|
2012-07-29 21:14:48 +00:00
|
|
|
return;
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
|
|
|
|
2011-04-10 11:06:07 +00:00
|
|
|
if (!(svars->state[1-t] & ST_LOADED))
|
2012-07-29 21:14:48 +00:00
|
|
|
return;
|
2004-03-27 16:07:20 +00:00
|
|
|
|
2008-08-23 07:54:00 +00:00
|
|
|
if (svars->uidval[M] < 0 || svars->uidval[S] < 0) {
|
2006-03-21 15:53:43 +00:00
|
|
|
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++) {
|
2012-07-07 15:19:17 +00:00
|
|
|
for (tmsg = svars->ctx[1-t]->msgs; tmsg; tmsg = tmsg->next)
|
2006-03-21 15:53:43 +00:00
|
|
|
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;
|
2012-08-26 11:36:12 +00:00
|
|
|
svars->nsrecs++;
|
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-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 20:03:21 +00:00
|
|
|
svars->new_total[t]++;
|
|
|
|
stats( svars );
|
|
|
|
cv = nfmalloc( sizeof(*cv) );
|
|
|
|
cv->cb = msg_copied;
|
|
|
|
cv->aux = AUX;
|
|
|
|
cv->srec = srec;
|
|
|
|
cv->msg = tmsg;
|
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 );
|
2012-09-15 13:15:22 +00:00
|
|
|
if (FSyncLevel >= FSYNC_THOROUGH)
|
|
|
|
fdatasync( fileno( svars->jfp ) );
|
2006-02-03 21:33:43 +00:00
|
|
|
debug( " -> %sing message, TUID %." stringify(TUIDL) "s\n", str_hl[t], srec->tuid );
|
2006-12-09 10:39:30 +00:00
|
|
|
if (copy_msg( cv ))
|
2012-07-29 21:14:48 +00:00
|
|
|
return;
|
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-03-21 20:03:21 +00:00
|
|
|
msg_copied_p2( svars, srec, t, tmsg, -1 );
|
2006-01-29 15:52:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-03-21 20:03:21 +00:00
|
|
|
svars->state[t] |= ST_SENT_NEW;
|
2012-07-29 21:14:48 +00:00
|
|
|
msgs_copied( svars, t );
|
2006-02-03 21:33:43 +00:00
|
|
|
}
|
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 20:03:21 +00:00
|
|
|
svars->flags_total[t]++;
|
|
|
|
stats( svars );
|
|
|
|
fv = nfmalloc( sizeof(*fv) );
|
|
|
|
fv->aux = AUX;
|
|
|
|
fv->srec = srec;
|
2012-07-29 21:14:48 +00:00
|
|
|
DRIVER_CALL(set_flags( svars->ctx[t], srec->msg[t], srec->uid[t], F_DELETED, 0, flags_set_del, fv ));
|
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 20:03:21 +00:00
|
|
|
todel = svars->ctx[S]->count + svars->new_total[S] - 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--;
|
2006-05-27 12:43:03 +00:00
|
|
|
else if ((!(tmsg->status & M_RECENT) || (tmsg->flags & F_SEEN)) &&
|
2006-02-02 17:03:01 +00:00
|
|
|
(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 20:03:21 +00:00
|
|
|
if (aflags | dflags) {
|
|
|
|
svars->flags_total[t]++;
|
|
|
|
stats( svars );
|
|
|
|
fv = nfmalloc( sizeof(*fv) );
|
|
|
|
fv->aux = AUX;
|
|
|
|
fv->srec = srec;
|
|
|
|
fv->aflags = aflags;
|
|
|
|
fv->dflags = dflags;
|
2012-07-29 21:14:48 +00:00
|
|
|
DRIVER_CALL(set_flags( svars->ctx[t], srec->msg[t], srec->uid[t], aflags, dflags, flags_set_sync, fv ));
|
2006-03-21 20:03:21 +00:00
|
|
|
} else
|
|
|
|
flags_set_sync_p2( svars, srec, t );
|
2000-12-21 18:16:44 +00:00
|
|
|
}
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
|
|
|
for (t = 0; t < 2; t++) {
|
|
|
|
svars->drv[t]->commit( svars->ctx[t] );
|
|
|
|
svars->state[t] |= ST_SENT_FLAGS;
|
2006-12-09 10:39:30 +00:00
|
|
|
if (msgs_flags_set( svars, t ))
|
2012-07-29 21:14:48 +00:00
|
|
|
return;
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
static void
|
2006-03-21 20:03:21 +00:00
|
|
|
msg_copied( int sts, int uid, copy_vars_t *vars )
|
|
|
|
{
|
2012-06-17 12:52:46 +00:00
|
|
|
SVARS_CHECK_CANCEL_RET;
|
2006-03-21 20:03:21 +00:00
|
|
|
switch (sts) {
|
|
|
|
case SYNC_OK:
|
2011-04-10 11:06:07 +00:00
|
|
|
if (uid < 0)
|
|
|
|
svars->state[t] |= S_FIND;
|
2006-03-21 20:03:21 +00:00
|
|
|
msg_copied_p2( svars, vars->srec, t, vars->msg, uid );
|
|
|
|
break;
|
|
|
|
case SYNC_NOGOOD:
|
|
|
|
debug( " -> killing (%d,%d)\n", vars->srec->uid[M], vars->srec->uid[S] );
|
|
|
|
vars->srec->status = S_DEAD;
|
|
|
|
Fprintf( svars->jfp, "- %d %d\n", vars->srec->uid[M], vars->srec->uid[S] );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cancel_sync( svars );
|
|
|
|
free( vars );
|
2012-07-29 21:14:48 +00:00
|
|
|
return;
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
|
|
|
free( vars );
|
|
|
|
svars->new_done[t]++;
|
|
|
|
stats( svars );
|
2012-07-29 21:14:48 +00:00
|
|
|
msgs_copied( svars, t );
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
msg_copied_p2( sync_vars_t *svars, sync_rec_t *srec, int t, message_t *tmsg, int uid )
|
|
|
|
{
|
|
|
|
if (srec->uid[t] != uid) {
|
|
|
|
debug( " -> new UID %d\n", uid );
|
|
|
|
Fprintf( svars->jfp, "%c %d %d %d\n", "<>"[t], srec->uid[M], srec->uid[S], uid );
|
|
|
|
srec->uid[t] = uid;
|
|
|
|
srec->tuid[0] = 0;
|
|
|
|
}
|
|
|
|
if (!tmsg->srec) {
|
|
|
|
tmsg->srec = srec;
|
|
|
|
if (svars->maxuid[1-t] < tmsg->uid) {
|
|
|
|
svars->maxuid[1-t] = tmsg->uid;
|
|
|
|
Fprintf( svars->jfp, "%c %d\n", ")("[t], tmsg->uid );
|
2006-02-02 17:03:01 +00:00
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
2000-12-21 18:16:44 +00:00
|
|
|
|
2011-04-10 11:06:07 +00:00
|
|
|
static void msgs_found_new( int sts, void *aux );
|
|
|
|
static void msgs_new_done( sync_vars_t *svars, int t );
|
2012-07-29 21:14:48 +00:00
|
|
|
static void sync_close( sync_vars_t *svars, int t );
|
2005-12-28 10:02:22 +00:00
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
static void
|
2006-03-21 20:03:21 +00:00
|
|
|
msgs_copied( sync_vars_t *svars, int t )
|
|
|
|
{
|
|
|
|
if (!(svars->state[t] & ST_SENT_NEW) || svars->new_done[t] < svars->new_total[t])
|
2012-07-29 21:14:48 +00:00
|
|
|
return;
|
2006-03-21 20:03:21 +00:00
|
|
|
|
2011-04-10 11:06:07 +00:00
|
|
|
if (svars->state[t] & S_FIND) {
|
|
|
|
debug( "finding just copied messages on %s\n", str_ms[t] );
|
|
|
|
svars->drv[t]->find_new_msgs( svars->ctx[t], msgs_found_new, AUX );
|
|
|
|
} else {
|
|
|
|
msgs_new_done( svars, t );
|
2004-03-27 16:07:20 +00:00
|
|
|
}
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
static void
|
2011-04-10 11:06:07 +00:00
|
|
|
msgs_found_new( int sts, void *aux )
|
2006-03-21 20:03:21 +00:00
|
|
|
{
|
2011-04-10 11:06:07 +00:00
|
|
|
SVARS_CHECK_RET;
|
2006-03-21 20:03:21 +00:00
|
|
|
switch (sts) {
|
|
|
|
case DRV_OK:
|
2011-04-10 11:06:07 +00:00
|
|
|
debug( "matching just copied messages on %s\n", str_ms[t] );
|
2006-03-21 20:03:21 +00:00
|
|
|
break;
|
|
|
|
default:
|
2011-04-10 11:06:07 +00:00
|
|
|
warn( "Warning: cannot find newly stored messages on %s.\n", str_ms[t] );
|
2006-03-21 20:03:21 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-04-10 11:06:07 +00:00
|
|
|
match_tuids( svars, t );
|
|
|
|
msgs_new_done( svars, t );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
msgs_new_done( sync_vars_t *svars, int t )
|
|
|
|
{
|
|
|
|
Fprintf( svars->jfp, "%c %d\n", "{}"[t], svars->ctx[t]->uidnext );
|
|
|
|
svars->state[t] |= ST_FOUND_NEW;
|
2012-07-29 21:14:48 +00:00
|
|
|
sync_close( svars, t );
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
static void
|
2006-03-21 20:03:21 +00:00
|
|
|
flags_set_del( int sts, void *aux )
|
|
|
|
{
|
2012-06-17 12:52:46 +00:00
|
|
|
SVARS_CHECK_RET_VARS(flag_vars_t);
|
2006-03-21 20:03:21 +00:00
|
|
|
switch (sts) {
|
|
|
|
case DRV_OK:
|
|
|
|
vars->srec->status |= S_DEL(t);
|
|
|
|
Fprintf( svars->jfp, "%c %d %d 0\n", "><"[t], vars->srec->uid[M], vars->srec->uid[S] );
|
|
|
|
vars->srec->uid[1-t] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free( vars );
|
|
|
|
svars->flags_done[t]++;
|
|
|
|
stats( svars );
|
2012-07-29 21:14:48 +00:00
|
|
|
msgs_flags_set( svars, t );
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
static void
|
2006-03-21 20:03:21 +00:00
|
|
|
flags_set_sync( int sts, void *aux )
|
|
|
|
{
|
2012-06-17 12:52:46 +00:00
|
|
|
SVARS_CHECK_RET_VARS(flag_vars_t);
|
2006-03-21 20:03:21 +00:00
|
|
|
switch (sts) {
|
|
|
|
case DRV_OK:
|
|
|
|
if (vars->aflags & F_DELETED)
|
|
|
|
vars->srec->status |= S_DEL(t);
|
|
|
|
else if (vars->dflags & F_DELETED)
|
|
|
|
vars->srec->status &= ~S_DEL(t);
|
|
|
|
flags_set_sync_p2( svars, vars->srec, t );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free( vars );
|
|
|
|
svars->flags_done[t]++;
|
|
|
|
stats( svars );
|
2012-07-29 21:14:48 +00:00
|
|
|
msgs_flags_set( svars, t );
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
flags_set_sync_p2( sync_vars_t *svars, sync_rec_t *srec, int t )
|
|
|
|
{
|
|
|
|
int nflags, nex;
|
|
|
|
|
|
|
|
nflags = (srec->flags | srec->aflags[t]) & ~srec->dflags[t];
|
|
|
|
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;
|
|
|
|
Fprintf( svars->jfp, "* %d %d %u\n", srec->uid[M], srec->uid[S], nflags );
|
|
|
|
}
|
|
|
|
if (t == S) {
|
|
|
|
nex = (srec->status / S_NEXPIRE) & 1;
|
|
|
|
if (nex != ((srec->status / S_EXPIRED) & 1)) {
|
|
|
|
if (nex && (svars->smaxxuid < srec->uid[S]))
|
|
|
|
svars->smaxxuid = srec->uid[S];
|
|
|
|
Fprintf( svars->jfp, "/ %d %d\n", srec->uid[M], srec->uid[S] );
|
|
|
|
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)) {
|
|
|
|
Fprintf( svars->jfp, "\\ %d %d\n", srec->uid[M], srec->uid[S] );
|
|
|
|
debug( " pair(%d,%d): expire %d (cancel)\n", srec->uid[M], srec->uid[S], nex );
|
|
|
|
srec->status = (srec->status & ~S_EXPIRE) | (nex * S_EXPIRE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
static void msg_trashed( int sts, void *aux );
|
|
|
|
static void msg_rtrashed( int sts, int uid, copy_vars_t *vars );
|
2006-03-21 20:03:21 +00:00
|
|
|
|
2006-12-09 10:39:30 +00:00
|
|
|
static int
|
2006-03-21 20:03:21 +00:00
|
|
|
msgs_flags_set( sync_vars_t *svars, int t )
|
|
|
|
{
|
|
|
|
message_t *tmsg;
|
|
|
|
copy_vars_t *cv;
|
|
|
|
|
|
|
|
if (!(svars->state[t] & ST_SENT_FLAGS) || svars->flags_done[t] < svars->flags_total[t])
|
2006-12-09 10:39:30 +00:00
|
|
|
return 0;
|
2006-03-21 20:03:21 +00:00
|
|
|
|
|
|
|
if ((svars->chan->ops[t] & OP_EXPUNGE) &&
|
|
|
|
(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( "%s: trashing message %d\n", str_ms[t], tmsg->uid );
|
|
|
|
svars->trash_total[t]++;
|
|
|
|
stats( svars );
|
2012-07-29 21:14:48 +00:00
|
|
|
sync_ref( svars );
|
|
|
|
svars->drv[t]->trash_msg( svars->ctx[t], tmsg, msg_trashed, AUX );
|
|
|
|
if (deref_check_cancel( svars ))
|
|
|
|
return -1;
|
2006-03-21 20:03:21 +00:00
|
|
|
} else
|
|
|
|
debug( "%s: not trashing message %d - not new\n", str_ms[t], 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( "%s: remote trashing message %d\n", str_ms[t], tmsg->uid );
|
|
|
|
svars->trash_total[t]++;
|
|
|
|
stats( svars );
|
|
|
|
cv = nfmalloc( sizeof(*cv) );
|
|
|
|
cv->cb = msg_rtrashed;
|
|
|
|
cv->aux = AUX;
|
|
|
|
cv->srec = 0;
|
|
|
|
cv->msg = tmsg;
|
2006-12-09 10:39:30 +00:00
|
|
|
if (copy_msg( cv ))
|
2012-07-29 21:14:48 +00:00
|
|
|
return -1;
|
2006-03-21 20:03:21 +00:00
|
|
|
} else
|
|
|
|
debug( "%s: not remote trashing message %d - too big\n", str_ms[t], tmsg->uid );
|
|
|
|
} else
|
|
|
|
debug( "%s: not remote trashing message %d - not new\n", str_ms[t], tmsg->uid );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
svars->state[t] |= ST_SENT_TRASH;
|
2012-07-29 21:14:48 +00:00
|
|
|
sync_close( svars, t );
|
|
|
|
return 0;
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
static void
|
2006-03-21 20:03:21 +00:00
|
|
|
msg_trashed( int sts, void *aux )
|
|
|
|
{
|
2012-06-17 12:52:46 +00:00
|
|
|
DECL_SVARS;
|
2006-03-21 20:03:21 +00:00
|
|
|
|
|
|
|
if (sts == DRV_MSG_BAD)
|
|
|
|
sts = DRV_BOX_BAD;
|
2012-06-17 12:52:46 +00:00
|
|
|
if (check_ret( sts, aux ))
|
2012-07-29 21:14:48 +00:00
|
|
|
return;
|
2012-06-17 12:52:46 +00:00
|
|
|
INIT_SVARS(aux);
|
2006-03-21 20:03:21 +00:00
|
|
|
svars->trash_done[t]++;
|
|
|
|
stats( svars );
|
2012-07-29 21:14:48 +00:00
|
|
|
sync_close( svars, t );
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
static void
|
2006-03-21 20:03:21 +00:00
|
|
|
msg_rtrashed( int sts, int uid, copy_vars_t *vars )
|
|
|
|
{
|
2012-06-17 12:52:46 +00:00
|
|
|
SVARS_CHECK_CANCEL_RET;
|
2006-03-21 20:03:21 +00:00
|
|
|
(void)uid;
|
|
|
|
switch (sts) {
|
|
|
|
case SYNC_OK:
|
|
|
|
case SYNC_NOGOOD: /* the message is gone or heavily busted */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cancel_sync( svars );
|
|
|
|
free( vars );
|
2012-07-29 21:14:48 +00:00
|
|
|
return;
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
|
|
|
free( vars );
|
|
|
|
svars->trash_done[t]++;
|
|
|
|
stats( svars );
|
2012-07-29 21:14:48 +00:00
|
|
|
sync_close( svars, t );
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
static void box_closed( int sts, void *aux );
|
2006-03-21 20:03:21 +00:00
|
|
|
static void box_closed_p2( sync_vars_t *svars, int t );
|
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
static void
|
2006-03-21 20:03:21 +00:00
|
|
|
sync_close( sync_vars_t *svars, int t )
|
|
|
|
{
|
2011-04-10 11:06:07 +00:00
|
|
|
if ((~svars->state[t] & (ST_FOUND_NEW|ST_SENT_TRASH)) ||
|
2006-03-21 20:03:21 +00:00
|
|
|
svars->trash_done[t] < svars->trash_total[t])
|
2012-07-29 21:14:48 +00:00
|
|
|
return;
|
2006-03-21 20:03:21 +00:00
|
|
|
|
|
|
|
if ((svars->chan->ops[t] & OP_EXPUNGE) /*&& !(svars->state[t] & ST_TRASH_BAD)*/) {
|
|
|
|
debug( "expunging %s\n", str_ms[t] );
|
2012-07-29 21:14:48 +00:00
|
|
|
svars->drv[t]->close( svars->ctx[t], box_closed, AUX );
|
|
|
|
} else {
|
|
|
|
box_closed_p2( svars, t );
|
2006-12-09 10:39:30 +00:00
|
|
|
}
|
2006-03-21 20:03:21 +00:00
|
|
|
}
|
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
static void
|
2006-03-21 20:03:21 +00:00
|
|
|
box_closed( int sts, void *aux )
|
|
|
|
{
|
2012-06-17 12:52:46 +00:00
|
|
|
SVARS_CHECK_RET;
|
2006-03-21 20:03:21 +00:00
|
|
|
svars->state[t] |= ST_DID_EXPUNGE;
|
|
|
|
box_closed_p2( svars, t );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
box_closed_p2( sync_vars_t *svars, int t )
|
|
|
|
{
|
|
|
|
sync_rec_t *srec;
|
|
|
|
int minwuid;
|
|
|
|
char fbuf[16]; /* enlarge when support for keywords is added */
|
|
|
|
|
|
|
|
svars->state[t] |= ST_CLOSED;
|
|
|
|
if (!(svars->state[1-t] & ST_CLOSED))
|
|
|
|
return;
|
|
|
|
|
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
|
|
|
|
2011-04-10 11:06:07 +00:00
|
|
|
Fprintf( svars->nfp, "%d:%d:%d %d:%d:%d:%d\n",
|
|
|
|
svars->uidval[M], svars->maxuid[M], svars->ctx[M]->uidnext,
|
|
|
|
svars->uidval[S], svars->smaxxuid, svars->maxuid[S], svars->ctx[S]->uidnext );
|
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;
|
|
|
|
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
|
|
|
|
2012-09-15 11:25:50 +00:00
|
|
|
Fclose( svars->nfp, 1 );
|
|
|
|
Fclose( svars->jfp, 0 );
|
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
|
|
|
|
2006-03-21 20:03:21 +00:00
|
|
|
sync_bail( svars );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sync_bail( sync_vars_t *svars )
|
|
|
|
{
|
|
|
|
sync_rec_t *srec, *nsrec;
|
|
|
|
|
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 );
|
2006-03-21 20:03:21 +00:00
|
|
|
sync_bail1( svars );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sync_bail1( sync_vars_t *svars )
|
|
|
|
{
|
2006-03-21 15:53:43 +00:00
|
|
|
close( svars->lfd );
|
2006-03-21 20:03:21 +00:00
|
|
|
sync_bail2( svars );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sync_bail2( sync_vars_t *svars )
|
|
|
|
{
|
2006-03-21 15:53:43 +00:00
|
|
|
free( svars->lname );
|
|
|
|
free( svars->nname );
|
|
|
|
free( svars->jname );
|
|
|
|
free( svars->dname );
|
2011-04-11 08:45:46 +00:00
|
|
|
flushn();
|
2012-08-18 11:58:14 +00:00
|
|
|
sync_bail3( svars );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sync_bail3( sync_vars_t *svars )
|
|
|
|
{
|
|
|
|
free( svars->ctx[M]->name );
|
|
|
|
free( svars->ctx[S]->name );
|
2012-07-29 21:14:48 +00:00
|
|
|
sync_deref( svars );
|
2000-12-20 21:41:21 +00:00
|
|
|
}
|
2004-03-27 16:07:20 +00:00
|
|
|
|
2012-07-29 21:14:48 +00:00
|
|
|
static int sync_deref( sync_vars_t *svars )
|
|
|
|
{
|
|
|
|
if (!--svars->ref_count) {
|
|
|
|
void (*cb)( int sts, void *aux ) = svars->cb;
|
|
|
|
void *aux = svars->aux;
|
|
|
|
int ret = svars->ret;
|
|
|
|
free( svars );
|
|
|
|
cb( ret, aux );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|