factor out jFprintf()

This commit is contained in:
Oswald Buddenhagen 2017-04-02 15:24:03 +02:00
parent 1fdf793a3f
commit c3350753b0

View File

@ -75,20 +75,27 @@ Fclose( FILE *f, int safe )
} }
void void
Fprintf( FILE *f, const char *msg, ... ) vFprintf( FILE *f, const char *msg, va_list va )
{ {
int r; int r;
va_list va;
va_start( va, msg );
r = vfprintf( f, msg, va ); r = vfprintf( f, msg, va );
va_end( va );
if (r < 0) { if (r < 0) {
sys_error( "Error: cannot write file" ); sys_error( "Error: cannot write file" );
exit( 1 ); exit( 1 );
} }
} }
void
Fprintf( FILE *f, const char *msg, ... )
{
va_list va;
va_start( va, msg );
vFprintf( f, msg, va );
va_end( va );
}
static const char Flags[] = { 'D', 'F', 'R', 'S', 'T' }; static const char Flags[] = { 'D', 'F', 'R', 'S', 'T' };
@ -228,6 +235,16 @@ static int check_cancel( sync_vars_t *svars );
#define ST_SENDING_NEW (1<<15) #define ST_SENDING_NEW (1<<15)
void
jFprintf( sync_vars_t *svars, const char *msg, ... )
{
va_list va;
va_start( va, msg );
vFprintf( svars->jfp, msg, va );
va_end( va );
}
static void static void
match_tuids( sync_vars_t *svars, int t ) match_tuids( sync_vars_t *svars, int t )
{ {
@ -258,14 +275,14 @@ match_tuids( sync_vars_t *svars, int t )
} }
} }
debug( " -> TUID lost\n" ); debug( " -> TUID lost\n" );
Fprintf( svars->jfp, "& %d %d\n", srec->uid[M], srec->uid[S] ); jFprintf( svars, "& %d %d\n", srec->uid[M], srec->uid[S] );
srec->flags = 0; srec->flags = 0;
srec->tuid[0] = 0; srec->tuid[0] = 0;
num_lost++; num_lost++;
continue; continue;
mfound: mfound:
debug( " -> new UID %d %s\n", tmsg->uid, diag ); 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 ); jFprintf( svars, "%c %d %d %d\n", "<>"[t], srec->uid[M], srec->uid[S], tmsg->uid );
tmsg->srec = srec; tmsg->srec = srec;
srec->msg[t] = tmsg; srec->msg[t] = tmsg;
ntmsg = tmsg->next; ntmsg = tmsg->next;
@ -1191,7 +1208,7 @@ box_opened2( sync_vars_t *svars, int t )
} }
setlinebuf( svars->jfp ); setlinebuf( svars->jfp );
if (!svars->replayed) if (!svars->replayed)
Fprintf( svars->jfp, JOURNAL_VERSION "\n" ); jFprintf( svars, JOURNAL_VERSION "\n" );
opts[M] = opts[S] = 0; opts[M] = opts[S] = 0;
if (fails) if (fails)
@ -1478,7 +1495,7 @@ box_loaded( int sts, void *aux )
if (svars->uidval[M] < 0 || svars->uidval[S] < 0) { if (svars->uidval[M] < 0 || svars->uidval[S] < 0) {
svars->uidval[M] = svars->ctx[M]->uidvalidity; svars->uidval[M] = svars->ctx[M]->uidvalidity;
svars->uidval[S] = svars->ctx[S]->uidvalidity; svars->uidval[S] = svars->ctx[S]->uidvalidity;
Fprintf( svars->jfp, "| %d %d\n", svars->uidval[M], svars->uidval[S] ); jFprintf( svars, "| %d %d\n", svars->uidval[M], svars->uidval[S] );
} }
info( "Synchronizing...\n" ); info( "Synchronizing...\n" );
@ -1494,7 +1511,7 @@ box_loaded( int sts, void *aux )
debug( " vanished\n" ); debug( " vanished\n" );
/* d.1) d.5) d.6) d.10) d.11) */ /* d.1) d.5) d.6) d.10) d.11) */
srec->status = S_DEAD; srec->status = S_DEAD;
Fprintf( svars->jfp, "- %d %d\n", srec->uid[M], srec->uid[S] ); jFprintf( svars, "- %d %d\n", srec->uid[M], srec->uid[S] );
} else { } else {
del[M] = no[M] && (srec->uid[M] > 0); del[M] = no[M] && (srec->uid[M] > 0);
del[S] = no[S] && (srec->uid[S] > 0); del[S] = no[S] && (srec->uid[S] > 0);
@ -1512,7 +1529,7 @@ box_loaded( int sts, void *aux )
if ((t == M) && (srec->status & (S_EXPIRE|S_EXPIRED))) { if ((t == M) && (srec->status & (S_EXPIRE|S_EXPIRED))) {
/* Don't propagate deletion resulting from expiration. */ /* Don't propagate deletion resulting from expiration. */
debug( " slave expired, orphaning master\n" ); debug( " slave expired, orphaning master\n" );
Fprintf( svars->jfp, "> %d %d 0\n", srec->uid[M], srec->uid[S] ); jFprintf( svars, "> %d %d 0\n", srec->uid[M], srec->uid[S] );
srec->uid[S] = 0; srec->uid[S] = 0;
} else { } else {
if (srec->msg[t] && (srec->msg[t]->status & M_FLAGS) && srec->msg[t]->flags != srec->flags) if (srec->msg[t] && (srec->msg[t]->status & M_FLAGS) && srec->msg[t]->flags != srec->flags)
@ -1592,7 +1609,7 @@ box_loaded( int sts, void *aux )
tmsg->srec = srec; tmsg->srec = srec;
if (svars->newmaxuid[1-t] < tmsg->uid) if (svars->newmaxuid[1-t] < tmsg->uid)
svars->newmaxuid[1-t] = tmsg->uid; svars->newmaxuid[1-t] = tmsg->uid;
Fprintf( svars->jfp, "+ %d %d\n", srec->uid[M], srec->uid[S] ); jFprintf( svars, "+ %d %d\n", srec->uid[M], srec->uid[S] );
debug( " -> pair(%d,%d) created\n", srec->uid[M], srec->uid[S] ); debug( " -> pair(%d,%d) created\n", srec->uid[M], srec->uid[S] );
} }
if (svars->maxuid[1-t] < tmsg->uid) { if (svars->maxuid[1-t] < tmsg->uid) {
@ -1604,14 +1621,14 @@ box_loaded( int sts, void *aux )
if ((tmsg->flags & F_FLAGGED) || tmsg->size <= svars->chan->stores[t]->max_size) { if ((tmsg->flags & F_FLAGGED) || tmsg->size <= svars->chan->stores[t]->max_size) {
if (tmsg->flags) { if (tmsg->flags) {
srec->flags = tmsg->flags; srec->flags = tmsg->flags;
Fprintf( svars->jfp, "* %d %d %u\n", srec->uid[M], srec->uid[S], srec->flags ); jFprintf( svars, "* %d %d %u\n", srec->uid[M], srec->uid[S], srec->flags );
debug( " -> updated flags to %u\n", tmsg->flags ); debug( " -> updated flags to %u\n", tmsg->flags );
} }
for (t1 = 0; t1 < TUIDL; t1++) { for (t1 = 0; t1 < TUIDL; t1++) {
t2 = arc4_getbyte() & 0x3f; t2 = arc4_getbyte() & 0x3f;
srec->tuid[t1] = t2 < 26 ? t2 + 'A' : t2 < 52 ? t2 + 'a' - 26 : t2 < 62 ? t2 + '0' - 52 : t2 == 62 ? '+' : '/'; srec->tuid[t1] = t2 < 26 ? t2 + 'A' : t2 < 52 ? t2 + 'a' - 26 : t2 < 62 ? t2 + '0' - 52 : t2 == 62 ? '+' : '/';
} }
Fprintf( svars->jfp, "# %d %d %." stringify(TUIDL) "s\n", srec->uid[M], srec->uid[S], srec->tuid ); jFprintf( svars, "# %d %d %." stringify(TUIDL) "s\n", srec->uid[M], srec->uid[S], srec->tuid );
debug( " -> %sing message, TUID %." stringify(TUIDL) "s\n", str_hl[t], srec->tuid ); debug( " -> %sing message, TUID %." stringify(TUIDL) "s\n", str_hl[t], srec->tuid );
} else { } else {
if (srec->uid[t] == -1) { if (srec->uid[t] == -1) {
@ -1714,7 +1731,7 @@ box_loaded( int sts, void *aux )
/* The record needs a state change ... */ /* The record needs a state change ... */
if (nex != ((srec->status / S_EXPIRE) & 1)) { if (nex != ((srec->status / S_EXPIRE) & 1)) {
/* ... and we need to start a transaction. */ /* ... and we need to start a transaction. */
Fprintf( svars->jfp, "~ %d %d %d\n", srec->uid[M], srec->uid[S], nex ); jFprintf( svars, "~ %d %d %d\n", srec->uid[M], srec->uid[S], nex );
debug( " pair(%d,%d): %d (pre)\n", srec->uid[M], srec->uid[S], nex ); debug( " pair(%d,%d): %d (pre)\n", srec->uid[M], srec->uid[S], nex );
srec->status = (srec->status & ~S_EXPIRE) | (nex * S_EXPIRE); srec->status = (srec->status & ~S_EXPIRE) | (nex * S_EXPIRE);
} else { } else {
@ -1727,7 +1744,7 @@ box_loaded( int sts, void *aux )
} }
} else { } else {
if (srec->status & S_NEXPIRE) { if (srec->status & S_NEXPIRE) {
Fprintf( svars->jfp, "- %d %d\n", srec->uid[M], srec->uid[S] ); jFprintf( svars, "- %d %d\n", srec->uid[M], srec->uid[S] );
debug( " pair(%d,%d): 1 (abort)\n", srec->uid[M], srec->uid[S] ); debug( " pair(%d,%d): 1 (abort)\n", srec->uid[M], srec->uid[S] );
srec->msg[M]->srec = 0; srec->msg[M]->srec = 0;
srec->status = S_DEAD; srec->status = S_DEAD;
@ -1802,7 +1819,7 @@ box_loaded( int sts, void *aux )
fdatasync( fileno( svars->jfp ) ); fdatasync( fileno( svars->jfp ) );
for (t = 0; t < 2; t++) { for (t = 0; t < 2; t++) {
svars->newuid[t] = svars->ctx[t]->uidnext; svars->newuid[t] = svars->ctx[t]->uidnext;
Fprintf( svars->jfp, "%c %d\n", "{}"[t], svars->newuid[t] ); jFprintf( svars, "%c %d\n", "{}"[t], svars->newuid[t] );
svars->new_msgs[t] = svars->ctx[1-t]->msgs; svars->new_msgs[t] = svars->ctx[1-t]->msgs;
msgs_copied( svars, t ); msgs_copied( svars, t );
if (check_cancel( svars )) if (check_cancel( svars ))
@ -1826,7 +1843,7 @@ msg_copied( int sts, int uid, copy_vars_t *vars )
case SYNC_NOGOOD: case SYNC_NOGOOD:
debug( " -> killing (%d,%d)\n", vars->srec->uid[M], vars->srec->uid[S] ); debug( " -> killing (%d,%d)\n", vars->srec->uid[M], vars->srec->uid[S] );
vars->srec->status = S_DEAD; vars->srec->status = S_DEAD;
Fprintf( svars->jfp, "- %d %d\n", vars->srec->uid[M], vars->srec->uid[S] ); jFprintf( svars, "- %d %d\n", vars->srec->uid[M], vars->srec->uid[S] );
break; break;
default: default:
cancel_sync( svars ); cancel_sync( svars );
@ -1852,7 +1869,7 @@ msg_copied_p2( sync_vars_t *svars, sync_rec_t *srec, int t, int uid )
* - -1 when not actually storing a message */ * - -1 when not actually storing a message */
if (srec->uid[t] != uid) { if (srec->uid[t] != uid) {
debug( " -> new UID %d on %s\n", uid, str_ms[t] ); debug( " -> new UID %d on %s\n", uid, str_ms[t] );
Fprintf( svars->jfp, "%c %d %d %d\n", "<>"[t], srec->uid[M], srec->uid[S], uid ); jFprintf( svars, "%c %d %d %d\n", "<>"[t], srec->uid[M], srec->uid[S], uid );
srec->uid[t] = uid; srec->uid[t] = uid;
srec->tuid[0] = 0; srec->tuid[0] = 0;
} }
@ -1863,7 +1880,7 @@ msg_copied_p2( sync_vars_t *svars, sync_rec_t *srec, int t, int uid )
svars->mmaxxuid = INT_MAX; svars->mmaxxuid = INT_MAX;
if (svars->smaxxuid < srec->uid[S] - 1) { if (svars->smaxxuid < srec->uid[S] - 1) {
svars->smaxxuid = srec->uid[S] - 1; svars->smaxxuid = srec->uid[S] - 1;
Fprintf( svars->jfp, "! %d\n", svars->smaxxuid ); jFprintf( svars, "! %d\n", svars->smaxxuid );
} }
} }
} }
@ -1912,7 +1929,7 @@ msgs_copied( sync_vars_t *svars, int t )
if (svars->new_pending[t]) if (svars->new_pending[t])
goto out; goto out;
Fprintf( svars->jfp, "%c %d\n", ")("[t], svars->maxuid[1-t] ); jFprintf( svars, "%c %d\n", ")("[t], svars->maxuid[1-t] );
sync_close( svars, 1-t ); sync_close( svars, 1-t );
if (check_cancel( svars )) if (check_cancel( svars ))
goto out; goto out;
@ -1976,25 +1993,25 @@ flags_set_p2( sync_vars_t *svars, sync_rec_t *srec, int t )
{ {
if (srec->status & S_DELETE) { if (srec->status & S_DELETE) {
debug( " pair(%d,%d): resetting %s UID\n", srec->uid[M], srec->uid[S], str_ms[1-t] ); debug( " pair(%d,%d): resetting %s UID\n", srec->uid[M], srec->uid[S], str_ms[1-t] );
Fprintf( svars->jfp, "%c %d %d 0\n", "><"[t], srec->uid[M], srec->uid[S] ); jFprintf( svars, "%c %d %d 0\n", "><"[t], srec->uid[M], srec->uid[S] );
srec->uid[1-t] = 0; srec->uid[1-t] = 0;
} else { } else {
int nflags = (srec->flags | srec->aflags[t]) & ~srec->dflags[t]; int nflags = (srec->flags | srec->aflags[t]) & ~srec->dflags[t];
if (srec->flags != nflags) { if (srec->flags != nflags) {
debug( " pair(%d,%d): updating flags (%u -> %u; %sed)\n", srec->uid[M], srec->uid[S], srec->flags, nflags, str_hl[t] ); debug( " pair(%d,%d): updating flags (%u -> %u; %sed)\n", srec->uid[M], srec->uid[S], srec->flags, nflags, str_hl[t] );
srec->flags = nflags; srec->flags = nflags;
Fprintf( svars->jfp, "* %d %d %u\n", srec->uid[M], srec->uid[S], nflags ); jFprintf( svars, "* %d %d %u\n", srec->uid[M], srec->uid[S], nflags );
} }
if (t == S) { if (t == S) {
uint nex = (srec->status / S_NEXPIRE) & 1; uint nex = (srec->status / S_NEXPIRE) & 1;
if (nex != ((srec->status / S_EXPIRED) & 1)) { if (nex != ((srec->status / S_EXPIRED) & 1)) {
if (nex && (svars->smaxxuid < srec->uid[S])) if (nex && (svars->smaxxuid < srec->uid[S]))
svars->smaxxuid = srec->uid[S]; svars->smaxxuid = srec->uid[S];
Fprintf( svars->jfp, "/ %d %d\n", srec->uid[M], srec->uid[S] ); jFprintf( svars, "/ %d %d\n", srec->uid[M], srec->uid[S] );
debug( " pair(%d,%d): expired %d (commit)\n", srec->uid[M], srec->uid[S], nex ); debug( " pair(%d,%d): expired %d (commit)\n", srec->uid[M], srec->uid[S], nex );
srec->status = (srec->status & ~S_EXPIRED) | (nex * S_EXPIRED); srec->status = (srec->status & ~S_EXPIRED) | (nex * S_EXPIRED);
} else if (nex != ((srec->status / S_EXPIRE) & 1)) { } else if (nex != ((srec->status / S_EXPIRE) & 1)) {
Fprintf( svars->jfp, "\\ %d %d\n", srec->uid[M], srec->uid[S] ); jFprintf( svars, "\\ %d %d\n", srec->uid[M], srec->uid[S] );
debug( " pair(%d,%d): expire %d (cancel)\n", srec->uid[M], srec->uid[S], nex ); debug( " pair(%d,%d): expire %d (cancel)\n", srec->uid[M], srec->uid[S], nex );
srec->status = (srec->status & ~S_EXPIRE) | (nex * S_EXPIRE); srec->status = (srec->status & ~S_EXPIRE) | (nex * S_EXPIRE);
} }
@ -2083,7 +2100,7 @@ msg_trashed( int sts, void *aux )
return; return;
INIT_SVARS(vars->aux); INIT_SVARS(vars->aux);
debug( " -> trashed %s %d\n", str_ms[t], vars->msg->uid ); debug( " -> trashed %s %d\n", str_ms[t], vars->msg->uid );
Fprintf( svars->jfp, "%c %d\n", "[]"[t], vars->msg->uid ); jFprintf( svars, "%c %d\n", "[]"[t], vars->msg->uid );
free( vars ); free( vars );
trash_done[t]++; trash_done[t]++;
stats(); stats();
@ -2106,7 +2123,7 @@ msg_rtrashed( int sts, int uid ATTR_UNUSED, copy_vars_t *vars )
} }
t ^= 1; t ^= 1;
debug( " -> remote trashed %s %d\n", str_ms[t], vars->msg->uid ); debug( " -> remote trashed %s %d\n", str_ms[t], vars->msg->uid );
Fprintf( svars->jfp, "%c %d\n", "[]"[t], vars->msg->uid ); jFprintf( svars, "%c %d\n", "[]"[t], vars->msg->uid );
free( vars ); free( vars );
trash_done[t]++; trash_done[t]++;
stats(); stats();
@ -2179,15 +2196,15 @@ box_closed_p2( sync_vars_t *svars, int t )
((srec->status & S_EXPIRED) && svars->maxuid[M] >= srec->uid[M] && minwuid > srec->uid[M])) { ((srec->status & S_EXPIRED) && svars->maxuid[M] >= srec->uid[M] && minwuid > srec->uid[M])) {
debug( " -> killing (%d,%d)\n", srec->uid[M], srec->uid[S] ); debug( " -> killing (%d,%d)\n", srec->uid[M], srec->uid[S] );
srec->status = S_DEAD; srec->status = S_DEAD;
Fprintf( svars->jfp, "- %d %d\n", srec->uid[M], srec->uid[S] ); jFprintf( svars, "- %d %d\n", srec->uid[M], srec->uid[S] );
} else if (srec->uid[S] > 0) { } else if (srec->uid[S] > 0) {
debug( " -> orphaning (%d,[%d])\n", srec->uid[M], srec->uid[S] ); debug( " -> orphaning (%d,[%d])\n", srec->uid[M], srec->uid[S] );
Fprintf( svars->jfp, "> %d %d 0\n", srec->uid[M], srec->uid[S] ); jFprintf( svars, "> %d %d 0\n", srec->uid[M], srec->uid[S] );
srec->uid[S] = 0; srec->uid[S] = 0;
} }
} else if (srec->uid[M] > 0 && ((srec->status & S_DEL(M)) && (svars->state[M] & ST_DID_EXPUNGE))) { } else if (srec->uid[M] > 0 && ((srec->status & S_DEL(M)) && (svars->state[M] & ST_DID_EXPUNGE))) {
debug( " -> orphaning ([%d],%d)\n", srec->uid[M], srec->uid[S] ); debug( " -> orphaning ([%d],%d)\n", srec->uid[M], srec->uid[S] );
Fprintf( svars->jfp, "< %d %d 0\n", srec->uid[M], srec->uid[S] ); jFprintf( svars, "< %d %d 0\n", srec->uid[M], srec->uid[S] );
srec->uid[M] = 0; srec->uid[M] = 0;
} }
} }