Browse Source

de-duplicate error paths

makes the code more compact. yay for gotos.
wip/maildir-uid-dupes-test
Oswald Buddenhagen 14 years ago
parent
commit
7bab2d6d94
  1. 72
      src/sync.c

72
src/sync.c

@ -498,6 +498,7 @@ sync_boxes( store_t *ctx[], const char *names[], channel_conf_t *chan,
if (!strcmp( chan->sync_state ? chan->sync_state : global_sync_state, "*" )) { if (!strcmp( chan->sync_state ? chan->sync_state : global_sync_state, "*" )) {
if (!ctx[S]->path) { if (!ctx[S]->path) {
error( "Error: store '%s' does not support in-box sync state\n", chan->stores[S]->name ); error( "Error: store '%s' does not support in-box sync state\n", chan->stores[S]->name );
sbail:
free( svars ); free( svars );
cb( SYNC_BAD(S), aux ); cb( SYNC_BAD(S), aux );
return; return;
@ -518,17 +519,13 @@ sync_boxes( store_t *ctx[], const char *names[], channel_conf_t *chan,
if (!(s = strrchr( svars->dname, '/' ))) { if (!(s = strrchr( svars->dname, '/' ))) {
error( "Error: invalid SyncState '%s'\n", svars->dname ); error( "Error: invalid SyncState '%s'\n", svars->dname );
free( svars->dname ); free( svars->dname );
free( svars ); goto sbail;
cb( SYNC_BAD(S), aux );
return;
} }
*s = 0; *s = 0;
if (mkdir( svars->dname, 0700 ) && errno != EEXIST) { if (mkdir( svars->dname, 0700 ) && errno != EEXIST) {
error( "Error: cannot create SyncState directory '%s': %s\n", svars->dname, strerror(errno) ); error( "Error: cannot create SyncState directory '%s': %s\n", svars->dname, strerror(errno) );
free( svars->dname ); free( svars->dname );
free( svars ); goto sbail;
cb( SYNC_BAD(S), aux );
return;
} }
*s = '/'; *s = '/';
nfasprintf( &svars->jname, "%s.journal", svars->dname ); nfasprintf( &svars->jname, "%s.journal", svars->dname );
@ -558,35 +555,28 @@ sync_boxes( store_t *ctx[], const char *names[], channel_conf_t *chan,
debug( "reading sync state %s ...\n", svars->dname ); debug( "reading sync state %s ...\n", svars->dname );
if (!fgets( buf, sizeof(buf), jfp ) || !(t = strlen( buf )) || buf[t - 1] != '\n') { if (!fgets( buf, sizeof(buf), jfp ) || !(t = strlen( buf )) || buf[t - 1] != '\n') {
error( "Error: incomplete sync state header in %s\n", svars->dname ); error( "Error: incomplete sync state header in %s\n", svars->dname );
jbail:
fclose( jfp ); fclose( jfp );
bail:
svars->ret = SYNC_FAIL; svars->ret = SYNC_FAIL;
sync_bail( svars ); sync_bail( svars );
return; return;
} }
if (sscanf( buf, "%d:%d %d:%d:%d", &svars->uidval[M], &svars->maxuid[M], &svars->uidval[S], &svars->smaxxuid, &svars->maxuid[S]) != 5) { if (sscanf( buf, "%d:%d %d:%d:%d", &svars->uidval[M], &svars->maxuid[M], &svars->uidval[S], &svars->smaxxuid, &svars->maxuid[S]) != 5) {
error( "Error: invalid sync state header in %s\n", svars->dname ); error( "Error: invalid sync state header in %s\n", svars->dname );
fclose( jfp ); goto jbail;
svars->ret = SYNC_FAIL;
sync_bail( svars );
return;
} }
line = 1; line = 1;
while (fgets( buf, sizeof(buf), jfp )) { while (fgets( buf, sizeof(buf), jfp )) {
line++; line++;
if (!(t = strlen( buf )) || buf[t - 1] != '\n') { if (!(t = strlen( buf )) || buf[t - 1] != '\n') {
error( "Error: incomplete sync state entry at %s:%d\n", svars->dname, line ); error( "Error: incomplete sync state entry at %s:%d\n", svars->dname, line );
fclose( jfp ); goto jbail;
svars->ret = SYNC_FAIL;
sync_bail( svars );
return;
} }
fbuf[0] = 0; fbuf[0] = 0;
if (sscanf( buf, "%d %d %15s", &t1, &t2, fbuf ) < 2) { if (sscanf( buf, "%d %d %15s", &t1, &t2, fbuf ) < 2) {
error( "Error: invalid sync state entry at %s:%d\n", svars->dname, line ); error( "Error: invalid sync state entry at %s:%d\n", svars->dname, line );
fclose( jfp ); goto jbail;
svars->ret = SYNC_FAIL;
sync_bail( svars );
return;
} }
srec = nfmalloc( sizeof(*srec) ); srec = nfmalloc( sizeof(*srec) );
srec->uid[M] = t1; srec->uid[M] = t1;
@ -609,9 +599,7 @@ sync_boxes( store_t *ctx[], const char *names[], channel_conf_t *chan,
} else { } else {
if (errno != ENOENT) { if (errno != ENOENT) {
error( "Error: cannot read sync state %s\n", svars->dname ); error( "Error: cannot read sync state %s\n", svars->dname );
svars->ret = SYNC_FAIL; goto bail;
sync_bail( svars );
return;
} }
} }
line = 0; line = 0;
@ -620,18 +608,12 @@ sync_boxes( store_t *ctx[], const char *names[], channel_conf_t *chan,
debug( "recovering journal ...\n" ); debug( "recovering journal ...\n" );
if (!(t = strlen( buf )) || buf[t - 1] != '\n') { if (!(t = strlen( buf )) || buf[t - 1] != '\n') {
error( "Error: incomplete journal header in %s\n", svars->jname ); error( "Error: incomplete journal header in %s\n", svars->jname );
fclose( jfp ); goto jbail;
svars->ret = SYNC_FAIL;
sync_bail( svars );
return;
} }
if (memcmp( buf, JOURNAL_VERSION "\n", strlen(JOURNAL_VERSION) + 1 )) { if (memcmp( buf, JOURNAL_VERSION "\n", strlen(JOURNAL_VERSION) + 1 )) {
error( "Error: incompatible journal version " error( "Error: incompatible journal version "
"(got %.*s, expected " JOURNAL_VERSION ")\n", t - 1, buf ); "(got %.*s, expected " JOURNAL_VERSION ")\n", t - 1, buf );
fclose( jfp ); goto jbail;
svars->ret = SYNC_FAIL;
sync_bail( svars );
return;
} }
srec = 0; srec = 0;
line = 1; line = 1;
@ -639,10 +621,7 @@ sync_boxes( store_t *ctx[], const char *names[], channel_conf_t *chan,
line++; line++;
if (!(t = strlen( buf )) || buf[t - 1] != '\n') { if (!(t = strlen( buf )) || buf[t - 1] != '\n') {
error( "Error: incomplete journal entry at %s:%d\n", svars->jname, line ); error( "Error: incomplete journal entry at %s:%d\n", svars->jname, line );
fclose( jfp ); goto jbail;
svars->ret = SYNC_FAIL;
sync_bail( svars );
return;
} }
if (buf[0] == '#' ? if (buf[0] == '#' ?
(t3 = 0, (sscanf( buf + 2, "%d %d %n", &t1, &t2, &t3 ) < 2) || !t3 || (t - t3 != TUIDL + 3)) : (t3 = 0, (sscanf( buf + 2, "%d %d %n", &t1, &t2, &t3 ) < 2) || !t3 || (t - t3 != TUIDL + 3)) :
@ -653,10 +632,7 @@ sync_boxes( store_t *ctx[], const char *names[], channel_conf_t *chan,
(sscanf( buf + 2, "%d %d %d", &t1, &t2, &t3 ) != 3)) (sscanf( buf + 2, "%d %d %d", &t1, &t2, &t3 ) != 3))
{ {
error( "Error: malformed journal entry at %s:%d\n", svars->jname, line ); error( "Error: malformed journal entry at %s:%d\n", svars->jname, line );
fclose( jfp ); goto jbail;
svars->ret = SYNC_FAIL;
sync_bail( svars );
return;
} }
if (buf[0] == '(') if (buf[0] == '(')
svars->maxuid[M] = t1; svars->maxuid[M] = t1;
@ -685,10 +661,7 @@ sync_boxes( store_t *ctx[], const char *names[], channel_conf_t *chan,
if (srec->uid[M] == t1 && srec->uid[S] == t2) if (srec->uid[M] == t1 && srec->uid[S] == t2)
goto syncfnd; goto syncfnd;
error( "Error: journal entry at %s:%d refers to non-existing sync state entry\n", svars->jname, line ); error( "Error: journal entry at %s:%d refers to non-existing sync state entry\n", svars->jname, line );
fclose( jfp ); goto jbail;
svars->ret = SYNC_FAIL;
sync_bail( svars );
return;
syncfnd: syncfnd:
debugn( " entry(%d,%d,%u) ", srec->uid[M], srec->uid[S], srec->flags ); debugn( " entry(%d,%d,%u) ", srec->uid[M], srec->uid[S], srec->flags );
switch (buf[0]) { switch (buf[0]) {
@ -746,10 +719,7 @@ sync_boxes( store_t *ctx[], const char *names[], channel_conf_t *chan,
break; break;
default: default:
error( "Error: unrecognized journal entry at %s:%d\n", svars->jname, line ); error( "Error: unrecognized journal entry at %s:%d\n", svars->jname, line );
fclose( jfp ); goto jbail;
svars->ret = SYNC_FAIL;
sync_bail( svars );
return;
} }
} }
} }
@ -758,23 +728,17 @@ sync_boxes( store_t *ctx[], const char *names[], channel_conf_t *chan,
} else { } else {
if (errno != ENOENT) { if (errno != ENOENT) {
error( "Error: cannot read journal %s\n", svars->jname ); error( "Error: cannot read journal %s\n", svars->jname );
svars->ret = SYNC_FAIL; goto bail;
sync_bail( svars );
return;
} }
} }
if (!(svars->nfp = fopen( svars->nname, "w" ))) { if (!(svars->nfp = fopen( svars->nname, "w" ))) {
error( "Error: cannot write new sync state %s\n", svars->nname ); error( "Error: cannot write new sync state %s\n", svars->nname );
svars->ret = SYNC_FAIL; goto bail;
sync_bail( svars );
return;
} }
if (!(svars->jfp = fopen( svars->jname, "a" ))) { if (!(svars->jfp = fopen( svars->jname, "a" ))) {
error( "Error: cannot write journal %s\n", svars->jname ); error( "Error: cannot write journal %s\n", svars->jname );
fclose( svars->nfp ); fclose( svars->nfp );
svars->ret = SYNC_FAIL; goto bail;
sync_bail( svars );
return;
} }
setlinebuf( svars->jfp ); setlinebuf( svars->jfp );
if (!line) if (!line)

Loading…
Cancel
Save