sync.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. /*
  2. * mbsync - mailbox synchronizer
  3. * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
  4. * Copyright (C) 2002-2004 Oswald Buddenhagen <ossi@users.sf.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * As a special exception, mbsync may be linked with the OpenSSL library,
  21. * despite that library's more restrictive license.
  22. */
  23. #include "isync.h"
  24. #include <stdio.h>
  25. #include <limits.h>
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include <time.h>
  29. #include <fcntl.h>
  30. #include <string.h>
  31. #include <errno.h>
  32. #include <sys/stat.h>
  33. static const char *str_ms[] = { "master", "slave" }, *str_hl[] = { "push", "pull" };
  34. void
  35. Fprintf( FILE *f, const char *msg, ... )
  36. {
  37. int r;
  38. va_list va;
  39. va_start( va, msg );
  40. r = vfprintf( f, msg, va );
  41. va_end( va );
  42. if (r < 0) {
  43. perror( "cannot write file" );
  44. exit( 1 );
  45. }
  46. }
  47. static const char Flags[] = { 'D', 'F', 'R', 'S', 'T' };
  48. static int
  49. parse_flags( const char *buf )
  50. {
  51. unsigned flags, i, d;
  52. for (flags = i = d = 0; i < as(Flags); i++)
  53. if (buf[d] == Flags[i]) {
  54. flags |= (1 << i);
  55. d++;
  56. }
  57. return flags;
  58. }
  59. static int
  60. make_flags( int flags, char *buf )
  61. {
  62. unsigned i, d;
  63. for (i = d = 0; i < as(Flags); i++)
  64. if (flags & (1 << i))
  65. buf[d++] = Flags[i];
  66. buf[d] = 0;
  67. return d;
  68. }
  69. #define S_DEAD (1<<0)
  70. #define S_EXPIRED (1<<1)
  71. #define S_DEL(ms) (1<<(2+(ms)))
  72. #define S_EXP_S (1<<4)
  73. typedef struct sync_rec {
  74. struct sync_rec *next;
  75. /* string_list_t *keywords; */
  76. int uid[2];
  77. message_t *msg[2];
  78. unsigned char flags, status;
  79. } sync_rec_t;
  80. static void
  81. findmsgs( sync_rec_t *srecs, store_t *ctx[], int t )
  82. {
  83. sync_rec_t *srec, *nsrec = 0;
  84. message_t *msg;
  85. const char *diag;
  86. int uid;
  87. char fbuf[16]; /* enlarge when support for keywords is added */
  88. for (msg = ctx[t]->msgs; msg; msg = msg->next) {
  89. uid = msg->uid;
  90. if (Debug) {
  91. make_flags( msg->flags, fbuf );
  92. printf( ctx[t]->opts & OPEN_SIZE ? " message %5d, %-4s, %6d: " : " message %5d, %-4s: ", uid, fbuf, msg->size );
  93. }
  94. for (srec = nsrec; srec; srec = srec->next) {
  95. if (srec->status & S_DEAD)
  96. continue;
  97. if (srec->uid[t] == uid) {
  98. diag = srec == nsrec ? "adjacently" : "after gap";
  99. goto found;
  100. }
  101. }
  102. for (srec = srecs; srec != nsrec; srec = srec->next) {
  103. if (srec->status & S_DEAD)
  104. continue;
  105. if (srec->uid[t] == uid) {
  106. diag = "after reset";
  107. goto found;
  108. }
  109. }
  110. debug( "new\n" );
  111. continue;
  112. found:
  113. msg->status |= M_PROCESSED;
  114. srec->msg[t] = msg;
  115. nsrec = srec->next;
  116. debug( "pairs %5d %s\n", srec->uid[1-t], diag );
  117. }
  118. }
  119. /* cases:
  120. a) both non-null
  121. b) only master null
  122. b.1) uid[M] 0
  123. b.2) uid[M] -1
  124. b.3) master not scanned
  125. b.4) master gone
  126. c) only slave null
  127. c.1) uid[S] 0
  128. c.2) uid[S] -1
  129. c.3) slave not scanned
  130. c.4) slave gone
  131. d) both null
  132. d.1) both gone
  133. d.2) uid[M] 0, slave not scanned
  134. d.3) uid[M] -1, slave not scanned
  135. d.4) master gone, slave not scanned
  136. d.5) uid[M] 0, slave gone
  137. d.6) uid[M] -1, slave gone
  138. d.7) uid[S] 0, master not scanned
  139. d.8) uid[S] -1, master not scanned
  140. d.9) slave gone, master not scanned
  141. d.10) uid[S] 0, master gone
  142. d.11) uid[S] -1, master gone
  143. impossible cases: both uid[M] & uid[S] 0 or -1, both not scanned
  144. */
  145. static char *
  146. clean_strdup( const char *s )
  147. {
  148. char *cs;
  149. int i;
  150. cs = nfstrdup( s );
  151. for (i = 0; cs[i]; i++)
  152. if (cs[i] == '/')
  153. cs[i] = '!';
  154. return cs;
  155. }
  156. int
  157. sync_boxes( store_t *ctx[], const char *names[], channel_conf_t *chan )
  158. {
  159. driver_t *driver[2];
  160. message_t *tmsg;
  161. sync_rec_t *recs, *srec, **srecadd, *nsrec;
  162. char *dname, *jname, *nname, *lname, *s, *cmname, *csname;
  163. FILE *dfp, *jfp, *nfp;
  164. int opts[2];
  165. int nom, nos, del[2], ex[M];
  166. int muidval, suidval, smaxxuid, maxuid[2], minwuid, maxwuid;
  167. int t1, t2, t3, t;
  168. int lfd, ret, line, todel, delt, *mexcs, nmexcs, rmexcs;
  169. unsigned char nflags;
  170. msg_data_t msgdata;
  171. struct stat st;
  172. struct flock lck;
  173. char fbuf[16]; /* enlarge when support for keywords is added */
  174. char buf[64];
  175. ret = SYNC_OK;
  176. recs = 0, srecadd = &recs;
  177. for (t = 0; t < 2; t++) {
  178. ctx[t]->name =
  179. (!names[t] || (ctx[t]->conf->map_inbox && !strcmp( ctx[t]->conf->map_inbox, names[t] ))) ?
  180. "INBOX" : names[t];
  181. ctx[t]->uidvalidity = 0;
  182. driver[t] = ctx[t]->conf->driver;
  183. driver[t]->prepare_paths( ctx[t] );
  184. }
  185. if (!strcmp( chan->sync_state ? chan->sync_state : global_sync_state, "*" )) {
  186. if (!ctx[S]->path) {
  187. fprintf( stderr, "Error: store '%s' does not support in-box sync state\n", chan->stores[S]->name );
  188. return SYNC_BAD(S);
  189. }
  190. nfasprintf( &dname, "%s/." EXE "state", ctx[S]->path );
  191. } else {
  192. csname = clean_strdup( ctx[S]->name );
  193. if (chan->sync_state)
  194. nfasprintf( &dname, "%s%s", chan->sync_state, csname );
  195. else {
  196. cmname = clean_strdup( ctx[M]->name );
  197. nfasprintf( &dname, "%s:%s:%s_:%s:%s", global_sync_state,
  198. chan->stores[M]->name, cmname, chan->stores[S]->name, csname );
  199. free( cmname );
  200. }
  201. free( csname );
  202. }
  203. nfasprintf( &jname, "%s.journal", dname );
  204. nfasprintf( &nname, "%s.new", dname );
  205. nfasprintf( &lname, "%s.lock", dname );
  206. muidval = suidval = smaxxuid = maxuid[M] = maxuid[S] = 0;
  207. memset( &lck, 0, sizeof(lck) );
  208. #if SEEK_SET != 0
  209. lck.l_whence = SEEK_SET;
  210. #endif
  211. #if F_WRLCK != 0
  212. lck.l_type = F_WRLCK;
  213. #endif
  214. if ((lfd = open( lname, O_WRONLY|O_CREAT, 0666 )) < 0) {
  215. if (errno != ENOENT) {
  216. lferr:
  217. fprintf( stderr, "Error: cannot create lock file %s: %s\n", lname, strerror(errno) );
  218. ret = SYNC_FAIL;
  219. goto bail2;
  220. }
  221. goto skiprd;
  222. }
  223. if (fcntl( lfd, F_SETLK, &lck )) {
  224. lckerr:
  225. fprintf( stderr, "Error: channel :%s:%s-:%s:%s is locked\n",
  226. chan->stores[M]->name, ctx[M]->name, chan->stores[S]->name, ctx[S]->name );
  227. ret = SYNC_FAIL;
  228. goto bail1;
  229. }
  230. if ((dfp = fopen( dname, "r" ))) {
  231. debug( "reading sync state %s ...\n", dname );
  232. if (!fgets( buf, sizeof(buf), dfp ) || !(t = strlen( buf )) || buf[t - 1] != '\n') {
  233. fprintf( stderr, "Error: incomplete sync state header in %s\n", dname );
  234. fclose( dfp );
  235. ret = SYNC_FAIL;
  236. goto bail;
  237. }
  238. if (sscanf( buf, "%d:%d %d:%d:%d", &muidval, &maxuid[M], &suidval, &smaxxuid, &maxuid[S]) != 5) {
  239. fprintf( stderr, "Error: invalid sync state header in %s\n", dname );
  240. fclose( dfp );
  241. ret = SYNC_FAIL;
  242. goto bail;
  243. }
  244. line = 1;
  245. while (fgets( buf, sizeof(buf), dfp )) {
  246. line++;
  247. if (!(t = strlen( buf )) || buf[t - 1] != '\n') {
  248. fprintf( stderr, "Error: incomplete sync state entry at %s:%d\n", dname, line );
  249. fclose( dfp );
  250. ret = SYNC_FAIL;
  251. goto bail;
  252. }
  253. fbuf[0] = 0;
  254. if (sscanf( buf, "%d %d %15s", &t1, &t2, fbuf ) < 2) {
  255. fprintf( stderr, "Error: invalid sync state entry at %s:%d\n", dname, line );
  256. fclose( dfp );
  257. ret = SYNC_FAIL;
  258. goto bail;
  259. }
  260. srec = nfmalloc( sizeof(*srec) );
  261. srec->uid[M] = t1;
  262. srec->uid[S] = t2;
  263. s = fbuf;
  264. if (*s == 'X') {
  265. s++;
  266. srec->status = S_EXPIRED;
  267. } else
  268. srec->status = 0;
  269. srec->flags = parse_flags( s );
  270. debug( " entry (%d,%d,%u,%s)\n", srec->uid[M], srec->uid[S], srec->flags, srec->status & S_EXPIRED ? "X" : "" );
  271. srec->msg[M] = srec->msg[S] = 0;
  272. srec->next = 0;
  273. *srecadd = srec;
  274. srecadd = &srec->next;
  275. }
  276. fclose( dfp );
  277. } else {
  278. if (errno != ENOENT) {
  279. fprintf( stderr, "Error: cannot read sync state %s\n", dname );
  280. ret = SYNC_FAIL;
  281. goto bail;
  282. }
  283. }
  284. if ((jfp = fopen( jname, "r" ))) {
  285. if (!stat( nname, &st )) {
  286. debug( "recovering journal ...\n" );
  287. line = 0;
  288. srec = recs;
  289. while (fgets( buf, sizeof(buf), jfp )) {
  290. line++;
  291. if (!(t = strlen( buf )) || buf[t - 1] != '\n') {
  292. fprintf( stderr, "Error: incomplete journal entry at %s:%d\n", jname, line );
  293. fclose( jfp );
  294. ret = SYNC_FAIL;
  295. goto bail;
  296. }
  297. if (buf[0] == '^')
  298. srec = recs;
  299. else {
  300. if (buf[0] == '(' || buf[0] == ')' ?
  301. (sscanf( buf + 2, "%d", &t1 ) != 1) :
  302. buf[0] == '-' || buf[0] == '|' ?
  303. (sscanf( buf + 2, "%d %d", &t1, &t2 ) != 2) :
  304. (sscanf( buf + 2, "%d %d %d", &t1, &t2, &t3 ) != 3))
  305. {
  306. fprintf( stderr, "Error: malformed journal entry at %s:%d\n", jname, line );
  307. fclose( jfp );
  308. ret = SYNC_FAIL;
  309. goto bail;
  310. }
  311. if (buf[0] == '(')
  312. maxuid[M] = t1;
  313. else if (buf[0] == ')')
  314. maxuid[S] = t1;
  315. else if (buf[0] == '|') {
  316. muidval = t1;
  317. suidval = t2;
  318. } else if (buf[0] == '+') {
  319. srec = nfmalloc( sizeof(*srec) );
  320. srec->uid[M] = t1;
  321. srec->uid[S] = t2;
  322. srec->flags = t3;
  323. debug( " new entry(%d,%d,%u)\n", t1, t2, t3 );
  324. srec->msg[M] = srec->msg[S] = 0;
  325. srec->status = 0;
  326. srec->next = 0;
  327. *srecadd = srec;
  328. srecadd = &srec->next;
  329. } else {
  330. for (; srec; srec = srec->next)
  331. if (srec->uid[M] == t1 && srec->uid[S] == t2)
  332. goto syncfnd;
  333. fprintf( stderr, "Error: journal entry at %s:%d refers to non-existing sync state entry\n", jname, line );
  334. fclose( jfp );
  335. ret = SYNC_FAIL;
  336. goto bail;
  337. syncfnd:
  338. debug( " entry(%d,%d,%u) ", srec->uid[M], srec->uid[S], srec->flags );
  339. switch (buf[0]) {
  340. case '-':
  341. debug( "killed\n" );
  342. srec->status = S_DEAD;
  343. break;
  344. case '<':
  345. debug( "master now %d\n", t3 );
  346. srec->uid[M] = t3;
  347. break;
  348. case '>':
  349. debug( "slave now %d\n", t3 );
  350. srec->uid[S] = t3;
  351. break;
  352. case '*':
  353. debug( "flags now %d\n", t3 );
  354. srec->flags = t3;
  355. break;
  356. case '~':
  357. debug( "expired now %d\n", t3 );
  358. if (t3) {
  359. if (smaxxuid < t2)
  360. smaxxuid = t2;
  361. srec->status |= S_EXPIRED;
  362. } else
  363. srec->status &= ~S_EXPIRED;
  364. break;
  365. default:
  366. fprintf( stderr, "Error: unrecognized journal entry at %s:%d\n", jname, line );
  367. fclose( jfp );
  368. ret = SYNC_FAIL;
  369. goto bail;
  370. }
  371. }
  372. }
  373. }
  374. }
  375. fclose( jfp );
  376. } else {
  377. if (errno != ENOENT) {
  378. fprintf( stderr, "Error: cannot read journal %s\n", jname );
  379. ret = SYNC_FAIL;
  380. goto bail;
  381. }
  382. }
  383. skiprd:
  384. opts[M] = opts[S] = 0;
  385. for (t = 0; t < 2; t++) {
  386. if (chan->ops[t] & (OP_DELETE|OP_FLAGS)) {
  387. opts[t] |= OPEN_SETFLAGS;
  388. opts[1-t] |= OPEN_OLD;
  389. if (chan->ops[t] & OP_FLAGS)
  390. opts[1-t] |= OPEN_FLAGS;
  391. }
  392. if (chan->ops[t] & (OP_NEW|OP_RENEW)) {
  393. opts[t] |= OPEN_APPEND;
  394. if (chan->ops[t] & OP_RENEW)
  395. opts[1-t] |= OPEN_OLD;
  396. if (chan->ops[t] & OP_NEW)
  397. opts[1-t] |= OPEN_NEW;
  398. if (chan->ops[t] & OP_EXPUNGE)
  399. opts[1-t] |= OPEN_FLAGS;
  400. if (chan->stores[t]->max_size)
  401. opts[1-t] |= OPEN_SIZE;
  402. }
  403. if (chan->ops[t] & OP_EXPUNGE) {
  404. opts[t] |= OPEN_EXPUNGE;
  405. if (chan->stores[t]->trash) {
  406. if (!chan->stores[t]->trash_only_new)
  407. opts[t] |= OPEN_OLD;
  408. opts[t] |= OPEN_NEW|OPEN_FLAGS;
  409. } else if (chan->stores[1-t]->trash && chan->stores[1-t]->trash_remote_new)
  410. opts[t] |= OPEN_NEW|OPEN_FLAGS;
  411. }
  412. if (chan->ops[t] & OP_CREATE)
  413. opts[t] |= OPEN_CREATE;
  414. }
  415. if ((chan->ops[S] & (OP_NEW|OP_RENEW)) && chan->max_messages)
  416. opts[S] |= OPEN_OLD|OPEN_NEW|OPEN_FLAGS;
  417. driver[M]->prepare_opts( ctx[M], opts[M] );
  418. driver[S]->prepare_opts( ctx[S], opts[S] );
  419. if (ctx[S]->opts & OPEN_NEW)
  420. maxwuid = INT_MAX;
  421. else if (ctx[S]->opts & OPEN_OLD) {
  422. maxwuid = 0;
  423. for (srec = recs; srec; srec = srec->next)
  424. if (!(srec->status & S_DEAD) && srec->uid[S] > maxwuid)
  425. maxwuid = srec->uid[S];
  426. } else
  427. maxwuid = 0;
  428. info( "Selecting slave %s... ", ctx[S]->name );
  429. debug( maxwuid == INT_MAX ? "selecting slave [1,inf]\n" : "selecting slave [1,%d]\n", maxwuid );
  430. switch (driver[S]->select( ctx[S], (ctx[S]->opts & OPEN_OLD) ? 1 : maxuid[S] + 1, maxwuid, 0, 0 )) {
  431. case DRV_STORE_BAD: ret = SYNC_BAD(S); goto bail;
  432. case DRV_BOX_BAD: ret = SYNC_FAIL; goto bail;
  433. }
  434. info( "%d messages, %d recent\n", ctx[S]->count, ctx[S]->recent );
  435. findmsgs( recs, ctx, S );
  436. if (suidval && suidval != ctx[S]->uidvalidity) {
  437. fprintf( stderr, "Error: UIDVALIDITY of slave changed\n" );
  438. ret = SYNC_FAIL;
  439. goto bail;
  440. }
  441. s = strrchr( dname, '/' );
  442. *s = 0;
  443. mkdir( dname, 0700 );
  444. *s = '/';
  445. if (lfd < 0) {
  446. if ((lfd = open( lname, O_WRONLY|O_CREAT, 0666 )) < 0)
  447. goto lferr;
  448. if (fcntl( lfd, F_SETLK, &lck ))
  449. goto lckerr;
  450. }
  451. if (!(nfp = fopen( nname, "w" ))) {
  452. fprintf( stderr, "Error: cannot write new sync state %s\n", nname );
  453. ret = SYNC_FAIL;
  454. goto bail;
  455. }
  456. if (!(jfp = fopen( jname, "a" ))) {
  457. fprintf( stderr, "Error: cannot write journal %s\n", jname );
  458. fclose( nfp );
  459. ret = SYNC_FAIL;
  460. goto bail;
  461. }
  462. setlinebuf( jfp );
  463. mexcs = 0;
  464. nmexcs = rmexcs = 0;
  465. minwuid = INT_MAX;
  466. if (smaxxuid) {
  467. debug( "preparing master selection - max expired slave uid is %d\n", smaxxuid );
  468. for (srec = recs; srec; srec = srec->next) {
  469. if (srec->status & S_DEAD)
  470. continue;
  471. if (srec->status & S_EXPIRED) {
  472. if (!srec->uid[S] || ((ctx[S]->opts & OPEN_OLD) && !srec->msg[S])) {
  473. srec->status |= S_EXP_S;
  474. continue;
  475. }
  476. } else {
  477. if (smaxxuid >= srec->uid[S])
  478. continue;
  479. }
  480. if (minwuid > srec->uid[M])
  481. minwuid = srec->uid[M];
  482. }
  483. debug( " min non-orphaned master uid is %d\n", minwuid );
  484. Fprintf( jfp, "^\n" ); /* if any S_EXP_S */
  485. for (srec = recs; srec; srec = srec->next) {
  486. if (srec->status & S_DEAD)
  487. continue;
  488. if (srec->status & S_EXP_S) {
  489. if (minwuid > srec->uid[M] && maxuid[M] >= srec->uid[M]) {
  490. debug( " -> killing (%d,%d)\n", srec->uid[M], srec->uid[S] );
  491. srec->status = S_DEAD;
  492. Fprintf( jfp, "- %d %d\n", srec->uid[M], srec->uid[S] );
  493. } else if (srec->uid[S]) {
  494. debug( " -> orphaning (%d,[%d])\n", srec->uid[M], srec->uid[S] );
  495. Fprintf( jfp, "> %d %d 0\n", srec->uid[M], srec->uid[S] );
  496. srec->uid[S] = 0;
  497. }
  498. } else if (minwuid > srec->uid[M]) {
  499. if (srec->uid[S] < 0) {
  500. if (maxuid[M] >= srec->uid[M]) {
  501. debug( " -> killing (%d,%d)\n", srec->uid[M], srec->uid[S] );
  502. srec->status = S_DEAD;
  503. Fprintf( jfp, "- %d %d\n", srec->uid[M], srec->uid[S] );
  504. }
  505. } else if (srec->uid[M] > 0 && srec->uid[S] && (ctx[M]->opts & OPEN_OLD) &&
  506. (!(ctx[M]->opts & OPEN_NEW) || maxuid[M] >= srec->uid[M])) {
  507. if (nmexcs == rmexcs) {
  508. rmexcs = rmexcs * 2 + 100;
  509. mexcs = nfrealloc( mexcs, rmexcs * sizeof(int) );
  510. }
  511. mexcs[nmexcs++] = srec->uid[M];
  512. }
  513. }
  514. }
  515. debug( " exception list is:" );
  516. for (t = 0; t < nmexcs; t++)
  517. debug( " %d", mexcs[t] );
  518. debug( "\n" );
  519. } else if (ctx[M]->opts & OPEN_OLD)
  520. minwuid = 1;
  521. if (ctx[M]->opts & OPEN_NEW) {
  522. if (minwuid > maxuid[M] + 1)
  523. minwuid = maxuid[M] + 1;
  524. maxwuid = INT_MAX;
  525. } else if (ctx[M]->opts & OPEN_OLD) {
  526. maxwuid = 0;
  527. for (srec = recs; srec; srec = srec->next)
  528. if (!(srec->status & S_DEAD) && srec->uid[M] > maxwuid)
  529. maxwuid = srec->uid[M];
  530. } else
  531. maxwuid = 0;
  532. info( "Selecting master %s... ", ctx[M]->name );
  533. debug( maxwuid == INT_MAX ? "selecting master [%d,inf]\n" : "selecting master [%d,%d]\n", minwuid, maxwuid );
  534. switch (driver[M]->select( ctx[M], minwuid, maxwuid, mexcs, nmexcs )) {
  535. case DRV_STORE_BAD: ret = SYNC_BAD(M); goto finish;
  536. case DRV_BOX_BAD: ret = SYNC_FAIL; goto finish;
  537. }
  538. info( "%d messages, %d recent\n", ctx[M]->count, ctx[M]->recent );
  539. findmsgs( recs, ctx, M );
  540. if (muidval && muidval != ctx[M]->uidvalidity) {
  541. fprintf( stderr, "Error: UIDVALIDITY of master changed\n" );
  542. ret = SYNC_FAIL;
  543. goto finish;
  544. }
  545. if (!muidval || !suidval) {
  546. muidval = ctx[M]->uidvalidity;
  547. suidval = ctx[S]->uidvalidity;
  548. Fprintf( jfp, "| %d %d\n", muidval, suidval );
  549. }
  550. info( "Synchronizing...\n" );
  551. debug( "synchronizing old entries\n" );
  552. Fprintf( jfp, "^\n" );
  553. for (srec = recs; srec; srec = srec->next) {
  554. if (srec->status & S_DEAD)
  555. continue;
  556. debug( "pair (%d,%d)\n", srec->uid[M], srec->uid[S] );
  557. nom = !srec->msg[M] && (ctx[M]->opts & OPEN_OLD);
  558. nos = !srec->msg[S] && (ctx[S]->opts & OPEN_OLD);
  559. if (nom && nos) {
  560. debug( " vanished\n" );
  561. /* d.1) d.5) d.6) d.10) d.11) */
  562. srec->status = S_DEAD;
  563. Fprintf( jfp, "- %d %d\n", srec->uid[M], srec->uid[S] );
  564. } else {
  565. del[M] = nom && (srec->uid[M] > 0);
  566. del[S] = nos && (srec->uid[S] > 0);
  567. nflags = srec->flags;
  568. for (t = 0; t < 2; t++) {
  569. int uid, unex;
  570. unsigned char sflags, aflags, dflags, rflags;
  571. /* excludes (push) c.3) d.2) d.3) d.4) / (pull) b.3) d.7) d.8) d.9) */
  572. if (!srec->uid[t]) {
  573. /* b.1) / c.1) */
  574. debug( " no more %s\n", str_ms[t] );
  575. } else if (del[1-t]) {
  576. /* c.4) d.9) / b.4) d.4) */
  577. debug( " %s vanished\n", str_ms[1-t] );
  578. if (srec->msg[t] && srec->msg[t]->flags != nflags)
  579. info( "Info: conflicting changes in (%d,%d)\n", srec->uid[M], srec->uid[S] );
  580. if (chan->ops[t] & OP_DELETE) {
  581. debug( " -> %s delete\n", str_hl[t] );
  582. switch (driver[t]->set_flags( ctx[t], srec->msg[t], srec->uid[t], F_DELETED, 0 )) {
  583. case DRV_STORE_BAD: ret = SYNC_BAD(t); goto finish;
  584. case DRV_BOX_BAD: ret = SYNC_FAIL; goto finish;
  585. default: /* ok */ break;
  586. case DRV_OK:
  587. Fprintf( jfp, "%c %d %d 0\n", "><"[t], srec->uid[M], srec->uid[S] );
  588. srec->uid[1-t] = 0;
  589. }
  590. }
  591. } else if (!srec->msg[1-t])
  592. /* c.1) c.2) d.7) d.8) / b.1) b.2) d.2) d.3) */
  593. ;
  594. else if (srec->uid[t] < 0) {
  595. /* b.2) / c.2) */
  596. debug( " no %s yet\n", str_ms[t] );
  597. if (chan->ops[t] & OP_RENEW) {
  598. if ((chan->ops[t] & OP_EXPUNGE) && (srec->msg[1-t]->flags & F_DELETED)) {
  599. debug( " -> not %sing - would be expunged anyway\n", str_hl[t] );
  600. srec->msg[1-t]->status |= M_NOT_SYNCED;
  601. } else {
  602. if ((srec->msg[1-t]->flags & F_FLAGGED) || !chan->stores[t]->max_size || srec->msg[1-t]->size <= chan->stores[t]->max_size) {
  603. debug( " -> %sing it\n", str_hl[t] );
  604. msgdata.flags = srec->msg[1-t]->flags;
  605. switch (driver[1-t]->fetch_msg( ctx[1-t], srec->msg[1-t], &msgdata )) {
  606. case DRV_STORE_BAD: ret = SYNC_BAD(1-t); goto finish;
  607. case DRV_BOX_BAD: ret = SYNC_FAIL; goto finish;
  608. default: /* ok */ srec->msg[1-t]->status |= M_NOT_SYNCED; break;
  609. case DRV_OK:
  610. srec->msg[1-t]->flags = msgdata.flags;
  611. switch (driver[t]->store_msg( ctx[t], &msgdata, &uid )) {
  612. case DRV_STORE_BAD: ret = SYNC_BAD(t); goto finish;
  613. default: ret = SYNC_FAIL; goto finish;
  614. case DRV_OK:
  615. Fprintf( jfp, "%c %d %d %d\n", "<>"[t], srec->uid[M], srec->uid[S], uid );
  616. srec->uid[t] = uid;
  617. nflags = srec->msg[1-t]->flags;
  618. }
  619. }
  620. } else {
  621. debug( " -> not %sing - still too big\n", str_hl[t] );
  622. srec->msg[1-t]->status |= M_NOT_SYNCED;
  623. }
  624. }
  625. } else
  626. srec->msg[1-t]->status |= M_NOT_SYNCED;
  627. } else if (!del[t]) {
  628. /* a) & b.3) / c.3) */
  629. debug( " may %s\n", str_hl[t] );
  630. if (chan->ops[t] & OP_FLAGS) {
  631. debug( " -> %sing flags\n", str_hl[t] );
  632. sflags = srec->msg[1-t]->flags;
  633. aflags = sflags & ~nflags;
  634. dflags = ~sflags & nflags;
  635. unex = 0;
  636. if (srec->status & S_EXPIRED) {
  637. if (!t) {
  638. if ((aflags & ~F_DELETED) || dflags)
  639. info( "Info: Flags of expired message changed in (%d,%d)\n", srec->uid[M], srec->uid[S] );
  640. continue;
  641. } else {
  642. if ((sflags & F_FLAGGED) && !(sflags & F_DELETED)) {
  643. unex = 1;
  644. dflags |= F_DELETED;
  645. } else
  646. continue;
  647. }
  648. }
  649. if ((chan->ops[t] & OP_EXPUNGE) && (sflags & F_DELETED) &&
  650. (!ctx[t]->conf->trash || ctx[t]->conf->trash_only_new))
  651. {
  652. aflags &= F_DELETED;
  653. dflags = 0;
  654. }
  655. rflags = (nflags | aflags) & ~dflags;
  656. switch ((aflags | dflags) ? driver[t]->set_flags( ctx[t], srec->msg[t], srec->uid[t], aflags, dflags ) : DRV_OK) {
  657. case DRV_STORE_BAD: ret = SYNC_BAD(t); goto finish;
  658. case DRV_BOX_BAD: ret = SYNC_FAIL; goto finish;
  659. default: /* ok */ break;
  660. case DRV_OK:
  661. nflags = rflags;
  662. if (unex) {
  663. debug( "unexpiring pair(%d,%d)\n", srec->uid[M], srec->uid[S] );
  664. /* log last, so deletion can't be misinterpreted! */
  665. Fprintf( jfp, "~ %d %d 0\n", srec->uid[M], srec->uid[S] );
  666. srec->status &= ~S_EXPIRED;
  667. }
  668. }
  669. }
  670. } /* else b.4) / c.4) */
  671. }
  672. if (srec->flags != nflags) {
  673. debug( " updating flags (%u -> %u)\n", srec->flags, nflags );
  674. srec->flags = nflags;
  675. Fprintf( jfp, "* %d %d %u\n", srec->uid[M], srec->uid[S], nflags );
  676. }
  677. if (srec->msg[M] && (srec->msg[M]->flags & F_DELETED))
  678. srec->status |= S_DEL(M);
  679. if (srec->msg[S] && (srec->msg[S]->flags & F_DELETED))
  680. srec->status |= S_DEL(S);
  681. }
  682. }
  683. debug( "synchronizing new entries\n" );
  684. for (t = 0; t < 2; t++) {
  685. int nmsgs, uid;
  686. for (nmsgs = 0, tmsg = ctx[1-t]->msgs; tmsg; tmsg = tmsg->next)
  687. if (!(tmsg->status & M_PROCESSED)) {
  688. if (chan->ops[t] & OP_NEW) {
  689. debug( "new message %d on %s\n", tmsg->uid, str_ms[1-t] );
  690. if ((chan->ops[t] & OP_EXPUNGE) && (tmsg->flags & F_DELETED)) {
  691. debug( " not %sing - would be expunged anyway\n", str_hl[t] );
  692. tmsg->status |= M_NOT_SYNCED;
  693. } else {
  694. if ((tmsg->flags & F_FLAGGED) || !chan->stores[t]->max_size || tmsg->size <= chan->stores[t]->max_size) {
  695. debug( " %sing it\n", str_hl[t] );
  696. if (!nmsgs)
  697. info( t ? "Pulling new messages..." : "Pushing new messages..." );
  698. else
  699. infoc( '.' );
  700. nmsgs++;
  701. msgdata.flags = tmsg->flags;
  702. switch (driver[1-t]->fetch_msg( ctx[1-t], tmsg, &msgdata )) {
  703. case DRV_STORE_BAD: return SYNC_BAD(1-t);
  704. case DRV_BOX_BAD: return SYNC_FAIL;
  705. case DRV_MSG_BAD: /* ok */ tmsg->status |= M_NOT_SYNCED; continue;
  706. }
  707. tmsg->flags = msgdata.flags;
  708. switch (driver[t]->store_msg( ctx[t], &msgdata, &uid )) {
  709. case DRV_STORE_BAD: return SYNC_BAD(t);
  710. default: return SYNC_FAIL;
  711. case DRV_OK: break;
  712. }
  713. } else {
  714. debug( " not %sing - too big\n", str_hl[t] );
  715. tmsg->status |= M_NOT_SYNCED;
  716. uid = -1;
  717. }
  718. srec = nfmalloc( sizeof(*srec) );
  719. srec->uid[1-t] = tmsg->uid;
  720. srec->uid[t] = uid;
  721. srec->flags = tmsg->flags;
  722. srec->status = 0;
  723. srec->next = 0;
  724. *srecadd = srec;
  725. srecadd = &srec->next;
  726. Fprintf( jfp, "+ %d %d %u\n", srec->uid[M], srec->uid[S], srec->flags );
  727. if (maxuid[1-t] < tmsg->uid) {
  728. maxuid[1-t] = tmsg->uid;
  729. Fprintf( jfp, "%c %d\n", ")("[t], tmsg->uid );
  730. }
  731. }
  732. } else
  733. tmsg->status |= M_NOT_SYNCED;
  734. }
  735. if (nmsgs)
  736. info( " %d messages\n", nmsgs );
  737. }
  738. if ((chan->ops[S] & (OP_NEW|OP_RENEW)) && chan->max_messages) {
  739. debug( "expiring excess entries\n" );
  740. todel = ctx[S]->count - chan->max_messages;
  741. for (tmsg = ctx[S]->msgs; tmsg && todel > 0; tmsg = tmsg->next)
  742. if (!(tmsg->status & M_DEAD) && (tmsg->flags & F_DELETED))
  743. todel--;
  744. delt = 0;
  745. for (tmsg = ctx[S]->msgs; tmsg && todel > 0; tmsg = tmsg->next) {
  746. if ((tmsg->status & M_DEAD) || (tmsg->flags & F_DELETED))
  747. continue;
  748. if ((tmsg->flags & F_FLAGGED) || (tmsg->status & M_NOT_SYNCED)) /* add M_DESYNCED? */
  749. todel--;
  750. else if (!(tmsg->status & M_RECENT)) {
  751. tmsg->status |= M_EXPIRED;
  752. delt++;
  753. todel--;
  754. }
  755. }
  756. if (delt) {
  757. Fprintf( jfp, "^\n" );
  758. for (srec = recs; srec; srec = srec->next) {
  759. if (srec->status & (S_DEAD|S_EXPIRED))
  760. continue;
  761. if (srec->msg[S] && (srec->msg[S]->status & M_EXPIRED)) {
  762. debug( " expiring pair(%d,%d)\n", srec->uid[M], srec->uid[S] );
  763. /* log first, so deletion can't be misinterpreted! */
  764. Fprintf( jfp, "~ %d %d 1\n", srec->uid[M], srec->uid[S] );
  765. if (smaxxuid < srec->uid[S])
  766. smaxxuid = srec->uid[S];
  767. srec->status |= S_EXPIRED;
  768. switch (driver[S]->set_flags( ctx[S], srec->msg[S], 0, F_DELETED, 0 )) {
  769. case DRV_STORE_BAD: ret = SYNC_BAD(S); goto finish;
  770. case DRV_BOX_BAD: ret = SYNC_FAIL; goto finish;
  771. default: /* ok */ break;
  772. case DRV_OK: srec->status |= S_DEL(S);
  773. }
  774. }
  775. }
  776. }
  777. }
  778. /* Doing CLOSE here instead of EXPUNGE above saves network traffic.
  779. But it costs more server power for single-file formats. And it
  780. makes disk-full/quota-exceeded more probable. */
  781. for (t = 0; t < 2; t++) {
  782. ex[t] = 0;
  783. if (chan->ops[t] & OP_EXPUNGE) {
  784. info( "Expunging %s\n", str_ms[t] );
  785. debug( "expunging %s\n", str_ms[t] );
  786. for (tmsg = ctx[t]->msgs; tmsg; tmsg = tmsg->next)
  787. if (tmsg->flags & F_DELETED) {
  788. if (ctx[t]->conf->trash) {
  789. if (!ctx[t]->conf->trash_only_new || (tmsg->status & M_NOT_SYNCED)) {
  790. debug( " trashing message %d\n", tmsg->uid );
  791. switch (driver[t]->trash_msg( ctx[t], tmsg )) {
  792. case DRV_OK: break;
  793. case DRV_STORE_BAD: ret = SYNC_BAD(t); goto finish;
  794. default: ret = SYNC_FAIL; goto nexex;
  795. }
  796. } else
  797. debug( " not trashing message %d - not new\n", tmsg->uid );
  798. } else if (ctx[1-t]->conf->trash && ctx[1-t]->conf->trash_remote_new) {
  799. if (tmsg->status & M_NOT_SYNCED) {
  800. if (!ctx[1-t]->conf->max_size || tmsg->size <= ctx[1-t]->conf->max_size) {
  801. debug( " remote trashing message %d\n", tmsg->uid );
  802. msgdata.flags = tmsg->flags;
  803. switch (driver[t]->fetch_msg( ctx[t], tmsg, &msgdata )) {
  804. case DRV_OK: break;
  805. case DRV_STORE_BAD: ret = SYNC_BAD(t); goto finish;
  806. default: ret = SYNC_FAIL; goto nexex;
  807. }
  808. switch (driver[1-t]->store_msg( ctx[1-t], &msgdata, 0 )) {
  809. case DRV_OK: break;
  810. case DRV_STORE_BAD: ret = SYNC_BAD(1-t); goto finish;
  811. default: ret = SYNC_FAIL; goto nexex;
  812. }
  813. } else
  814. debug( " not remote trashing message %d - too big\n", tmsg->uid );
  815. } else
  816. debug( " not remote trashing message %d - not new\n", tmsg->uid );
  817. }
  818. }
  819. switch (driver[t]->close( ctx[t] )) {
  820. case DRV_OK: ex[t] = 1; break;
  821. case DRV_STORE_BAD: ret = SYNC_BAD(t); goto finish;
  822. default: break;
  823. }
  824. }
  825. nexex: ;
  826. }
  827. if (ex[M] || ex[S]) {
  828. /* This cleanup is not strictly necessary, as the next full sync
  829. would throw out the dead entries anyway. But ... */
  830. minwuid = INT_MAX;
  831. if (smaxxuid) {
  832. debug( "preparing entry purge - max expired slave uid is %d\n", smaxxuid );
  833. for (srec = recs; srec; srec = srec->next) {
  834. if (srec->status & S_DEAD)
  835. continue;
  836. if (!((srec->uid[S] <= 0 || ((srec->status & S_DEL(S)) && ex[S])) &&
  837. (srec->uid[M] <= 0 || ((srec->status & S_DEL(M)) && ex[M]) || (srec->status & S_EXPIRED))) &&
  838. smaxxuid < srec->uid[S] && minwuid > srec->uid[M])
  839. minwuid = srec->uid[M];
  840. }
  841. debug( " min non-orphaned master uid is %d\n", minwuid );
  842. }
  843. Fprintf( jfp, "^\n" );
  844. for (srec = recs; srec; srec = srec->next) {
  845. if (srec->status & S_DEAD)
  846. continue;
  847. if (srec->uid[S] <= 0 || ((srec->status & S_DEL(S)) && ex[S])) {
  848. if (srec->uid[M] <= 0 || ((srec->status & S_DEL(M)) && ex[M])) {
  849. debug( " -> killing (%d,%d)\n", srec->uid[M], srec->uid[S] );
  850. srec->status = S_DEAD;
  851. Fprintf( jfp, "- %d %d\n", srec->uid[M], srec->uid[S] );
  852. } else if (srec->status & S_EXPIRED) {
  853. if (maxuid[M] >= srec->uid[M] && minwuid > srec->uid[M]) {
  854. debug( " -> killing (%d,%d)\n", srec->uid[M], srec->uid[S] );
  855. srec->status = S_DEAD;
  856. Fprintf( jfp, "- %d %d\n", srec->uid[M], srec->uid[S] );
  857. } else if (srec->uid[S]) {
  858. debug( " -> orphaning (%d,[%d])\n", srec->uid[M], srec->uid[S] );
  859. Fprintf( jfp, "> %d %d 0\n", srec->uid[M], srec->uid[S] );
  860. srec->uid[S] = 0;
  861. }
  862. }
  863. }
  864. }
  865. }
  866. finish:
  867. Fprintf( nfp, "%d:%d %d:%d:%d\n", muidval, maxuid[M], suidval, smaxxuid, maxuid[S] );
  868. for (srec = recs; srec; srec = srec->next) {
  869. if (srec->status & S_DEAD)
  870. continue;
  871. make_flags( srec->flags, fbuf );
  872. Fprintf( nfp, "%d %d %s%s\n", srec->uid[M], srec->uid[S],
  873. srec->status & S_EXPIRED ? "X" : "", fbuf );
  874. }
  875. fclose( nfp );
  876. fclose( jfp );
  877. /* order is important! */
  878. rename( nname, dname );
  879. unlink( jname );
  880. bail:
  881. for (srec = recs; srec; srec = nsrec) {
  882. nsrec = srec->next;
  883. free( srec );
  884. }
  885. unlink( lname );
  886. bail1:
  887. close( lfd );
  888. bail2:
  889. free( lname );
  890. free( nname );
  891. free( jname );
  892. free( dname );
  893. return ret;
  894. }