sync.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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. opts[M] = opts[S] = 0;
  178. for (t = 0; t < 2; t++) {
  179. if (chan->ops[t] & (OP_DELETE|OP_FLAGS)) {
  180. opts[t] |= OPEN_SETFLAGS;
  181. opts[1-t] |= OPEN_OLD;
  182. if (chan->ops[t] & OP_FLAGS)
  183. opts[1-t] |= OPEN_FLAGS;
  184. }
  185. if (chan->ops[t] & (OP_NEW|OP_RENEW)) {
  186. opts[t] |= OPEN_APPEND;
  187. if (chan->ops[t] & OP_RENEW)
  188. opts[1-t] |= OPEN_OLD;
  189. if (chan->ops[t] & OP_NEW)
  190. opts[1-t] |= OPEN_NEW;
  191. if (chan->ops[t] & OP_EXPUNGE)
  192. opts[1-t] |= OPEN_FLAGS;
  193. if (chan->stores[t]->max_size)
  194. opts[1-t] |= OPEN_SIZE;
  195. }
  196. if (chan->ops[t] & OP_EXPUNGE) {
  197. opts[t] |= OPEN_EXPUNGE;
  198. if (chan->stores[t]->trash) {
  199. if (!chan->stores[t]->trash_only_new)
  200. opts[t] |= OPEN_OLD;
  201. opts[t] |= OPEN_NEW|OPEN_FLAGS;
  202. } else if (chan->stores[1-t]->trash && chan->stores[1-t]->trash_remote_new)
  203. opts[t] |= OPEN_NEW|OPEN_FLAGS;
  204. }
  205. if (chan->ops[t] & OP_CREATE)
  206. opts[t] |= OPEN_CREATE;
  207. }
  208. if ((chan->ops[S] & (OP_NEW|OP_RENEW)) && chan->max_messages)
  209. opts[S] |= OPEN_OLD|OPEN_NEW|OPEN_FLAGS;
  210. for (t = 0; t < 2; t++) {
  211. ctx[t]->name =
  212. (!names[t] || (ctx[t]->conf->map_inbox && !strcmp( ctx[t]->conf->map_inbox, names[t] ))) ?
  213. "INBOX" : names[t];
  214. ctx[t]->uidvalidity = 0;
  215. driver[t] = ctx[t]->conf->driver;
  216. driver[t]->prepare_paths( ctx[t] );
  217. driver[t]->prepare_opts( ctx[t], opts[t] );
  218. }
  219. if (!strcmp( chan->sync_state ? chan->sync_state : global_sync_state, "*" )) {
  220. if (!ctx[S]->path) {
  221. fprintf( stderr, "Error: store '%s' does not support in-box sync state\n", chan->stores[S]->name );
  222. return SYNC_BAD(S);
  223. }
  224. nfasprintf( &dname, "%s/." EXE "state", ctx[S]->path );
  225. } else {
  226. csname = clean_strdup( ctx[S]->name );
  227. if (chan->sync_state)
  228. nfasprintf( &dname, "%s%s", chan->sync_state, csname );
  229. else {
  230. cmname = clean_strdup( ctx[M]->name );
  231. nfasprintf( &dname, "%s:%s:%s_:%s:%s", global_sync_state,
  232. chan->stores[M]->name, cmname, chan->stores[S]->name, csname );
  233. free( cmname );
  234. }
  235. free( csname );
  236. }
  237. nfasprintf( &jname, "%s.journal", dname );
  238. nfasprintf( &nname, "%s.new", dname );
  239. nfasprintf( &lname, "%s.lock", dname );
  240. muidval = suidval = smaxxuid = maxuid[M] = maxuid[S] = 0;
  241. memset( &lck, 0, sizeof(lck) );
  242. #if SEEK_SET != 0
  243. lck.l_whence = SEEK_SET;
  244. #endif
  245. #if F_WRLCK != 0
  246. lck.l_type = F_WRLCK;
  247. #endif
  248. if ((lfd = open( lname, O_WRONLY|O_CREAT, 0666 )) < 0) {
  249. if (errno != ENOENT) {
  250. lferr:
  251. fprintf( stderr, "Error: cannot create lock file %s: %s\n", lname, strerror(errno) );
  252. ret = SYNC_FAIL;
  253. goto bail2;
  254. }
  255. goto skiprd;
  256. }
  257. if (fcntl( lfd, F_SETLK, &lck )) {
  258. lckerr:
  259. fprintf( stderr, "Error: channel :%s:%s-:%s:%s is locked\n",
  260. chan->stores[M]->name, ctx[M]->name, chan->stores[S]->name, ctx[S]->name );
  261. ret = SYNC_FAIL;
  262. goto bail1;
  263. }
  264. if ((dfp = fopen( dname, "r" ))) {
  265. debug( "reading sync state %s ...\n", dname );
  266. if (!fgets( buf, sizeof(buf), dfp ) || !(t = strlen( buf )) || buf[t - 1] != '\n') {
  267. fprintf( stderr, "Error: incomplete sync state header in %s\n", dname );
  268. fclose( dfp );
  269. ret = SYNC_FAIL;
  270. goto bail;
  271. }
  272. if (sscanf( buf, "%d:%d %d:%d:%d", &muidval, &maxuid[M], &suidval, &smaxxuid, &maxuid[S]) != 5) {
  273. fprintf( stderr, "Error: invalid sync state header in %s\n", dname );
  274. fclose( dfp );
  275. ret = SYNC_FAIL;
  276. goto bail;
  277. }
  278. line = 1;
  279. while (fgets( buf, sizeof(buf), dfp )) {
  280. line++;
  281. if (!(t = strlen( buf )) || buf[t - 1] != '\n') {
  282. fprintf( stderr, "Error: incomplete sync state entry at %s:%d\n", dname, line );
  283. fclose( dfp );
  284. ret = SYNC_FAIL;
  285. goto bail;
  286. }
  287. fbuf[0] = 0;
  288. if (sscanf( buf, "%d %d %15s", &t1, &t2, fbuf ) < 2) {
  289. fprintf( stderr, "Error: invalid sync state entry at %s:%d\n", dname, line );
  290. fclose( dfp );
  291. ret = SYNC_FAIL;
  292. goto bail;
  293. }
  294. srec = nfmalloc( sizeof(*srec) );
  295. srec->uid[M] = t1;
  296. srec->uid[S] = t2;
  297. s = fbuf;
  298. if (*s == 'X') {
  299. s++;
  300. srec->status = S_EXPIRED;
  301. } else
  302. srec->status = 0;
  303. srec->flags = parse_flags( s );
  304. debug( " entry (%d,%d,%u,%s)\n", srec->uid[M], srec->uid[S], srec->flags, srec->status & S_EXPIRED ? "X" : "" );
  305. srec->msg[M] = srec->msg[S] = 0;
  306. srec->next = 0;
  307. *srecadd = srec;
  308. srecadd = &srec->next;
  309. }
  310. fclose( dfp );
  311. } else {
  312. if (errno != ENOENT) {
  313. fprintf( stderr, "Error: cannot read sync state %s\n", dname );
  314. ret = SYNC_FAIL;
  315. goto bail;
  316. }
  317. }
  318. if ((jfp = fopen( jname, "r" ))) {
  319. if (!stat( nname, &st )) {
  320. debug( "recovering journal ...\n" );
  321. line = 0;
  322. srec = recs;
  323. while (fgets( buf, sizeof(buf), jfp )) {
  324. line++;
  325. if (!(t = strlen( buf )) || buf[t - 1] != '\n') {
  326. fprintf( stderr, "Error: incomplete journal entry at %s:%d\n", jname, line );
  327. fclose( jfp );
  328. ret = SYNC_FAIL;
  329. goto bail;
  330. }
  331. if (buf[0] == '^')
  332. srec = recs;
  333. else {
  334. if (buf[0] == '(' || buf[0] == ')' ?
  335. (sscanf( buf + 2, "%d", &t1 ) != 1) :
  336. buf[0] == '-' || buf[0] == '|' ?
  337. (sscanf( buf + 2, "%d %d", &t1, &t2 ) != 2) :
  338. (sscanf( buf + 2, "%d %d %d", &t1, &t2, &t3 ) != 3))
  339. {
  340. fprintf( stderr, "Error: malformed journal entry at %s:%d\n", jname, line );
  341. fclose( jfp );
  342. ret = SYNC_FAIL;
  343. goto bail;
  344. }
  345. if (buf[0] == '(')
  346. maxuid[M] = t1;
  347. else if (buf[0] == ')')
  348. maxuid[S] = t1;
  349. else if (buf[0] == '|') {
  350. muidval = t1;
  351. suidval = t2;
  352. } else if (buf[0] == '+') {
  353. srec = nfmalloc( sizeof(*srec) );
  354. srec->uid[M] = t1;
  355. srec->uid[S] = t2;
  356. srec->flags = t3;
  357. debug( " new entry(%d,%d,%u)\n", t1, t2, t3 );
  358. srec->msg[M] = srec->msg[S] = 0;
  359. srec->status = 0;
  360. srec->next = 0;
  361. *srecadd = srec;
  362. srecadd = &srec->next;
  363. } else {
  364. for (; srec; srec = srec->next)
  365. if (srec->uid[M] == t1 && srec->uid[S] == t2)
  366. goto syncfnd;
  367. fprintf( stderr, "Error: journal entry at %s:%d refers to non-existing sync state entry\n", jname, line );
  368. fclose( jfp );
  369. ret = SYNC_FAIL;
  370. goto bail;
  371. syncfnd:
  372. debug( " entry(%d,%d,%u) ", srec->uid[M], srec->uid[S], srec->flags );
  373. switch (buf[0]) {
  374. case '-':
  375. debug( "killed\n" );
  376. srec->status = S_DEAD;
  377. break;
  378. case '<':
  379. debug( "master now %d\n", t3 );
  380. srec->uid[M] = t3;
  381. break;
  382. case '>':
  383. debug( "slave now %d\n", t3 );
  384. srec->uid[S] = t3;
  385. break;
  386. case '*':
  387. debug( "flags now %d\n", t3 );
  388. srec->flags = t3;
  389. break;
  390. case '~':
  391. debug( "expired now %d\n", t3 );
  392. if (t3) {
  393. if (smaxxuid < t2)
  394. smaxxuid = t2;
  395. srec->status |= S_EXPIRED;
  396. } else
  397. srec->status &= ~S_EXPIRED;
  398. break;
  399. default:
  400. fprintf( stderr, "Error: unrecognized journal entry at %s:%d\n", jname, line );
  401. fclose( jfp );
  402. ret = SYNC_FAIL;
  403. goto bail;
  404. }
  405. }
  406. }
  407. }
  408. }
  409. fclose( jfp );
  410. } else {
  411. if (errno != ENOENT) {
  412. fprintf( stderr, "Error: cannot read journal %s\n", jname );
  413. ret = SYNC_FAIL;
  414. goto bail;
  415. }
  416. }
  417. skiprd:
  418. if (ctx[S]->opts & OPEN_NEW)
  419. maxwuid = INT_MAX;
  420. else if (ctx[S]->opts & OPEN_OLD) {
  421. maxwuid = 0;
  422. for (srec = recs; srec; srec = srec->next)
  423. if (!(srec->status & S_DEAD) && srec->uid[S] > maxwuid)
  424. maxwuid = srec->uid[S];
  425. } else
  426. maxwuid = 0;
  427. info( "Selecting slave %s... ", ctx[S]->name );
  428. debug( maxwuid == INT_MAX ? "selecting slave [1,inf]\n" : "selecting slave [1,%d]\n", maxwuid );
  429. switch (driver[S]->select( ctx[S], (ctx[S]->opts & OPEN_OLD) ? 1 : maxuid[S] + 1, maxwuid, 0, 0 )) {
  430. case DRV_STORE_BAD: ret = SYNC_BAD(S); goto bail;
  431. case DRV_BOX_BAD: ret = SYNC_FAIL; goto bail;
  432. }
  433. info( "%d messages, %d recent\n", ctx[S]->count, ctx[S]->recent );
  434. findmsgs( recs, ctx, S );
  435. if (suidval && suidval != ctx[S]->uidvalidity) {
  436. fprintf( stderr, "Error: UIDVALIDITY of slave changed\n" );
  437. ret = SYNC_FAIL;
  438. goto bail;
  439. }
  440. s = strrchr( dname, '/' );
  441. *s = 0;
  442. mkdir( dname, 0700 );
  443. *s = '/';
  444. if (lfd < 0) {
  445. if ((lfd = open( lname, O_WRONLY|O_CREAT, 0666 )) < 0)
  446. goto lferr;
  447. if (fcntl( lfd, F_SETLK, &lck ))
  448. goto lckerr;
  449. }
  450. if (!(nfp = fopen( nname, "w" ))) {
  451. fprintf( stderr, "Error: cannot write new sync state %s\n", nname );
  452. ret = SYNC_FAIL;
  453. goto bail;
  454. }
  455. if (!(jfp = fopen( jname, "a" ))) {
  456. fprintf( stderr, "Error: cannot write journal %s\n", jname );
  457. fclose( nfp );
  458. ret = SYNC_FAIL;
  459. goto bail;
  460. }
  461. setlinebuf( jfp );
  462. mexcs = 0;
  463. nmexcs = rmexcs = 0;
  464. minwuid = INT_MAX;
  465. if (smaxxuid) {
  466. debug( "preparing master selection - max expired slave uid is %d\n", smaxxuid );
  467. for (srec = recs; srec; srec = srec->next) {
  468. if (srec->status & S_DEAD)
  469. continue;
  470. if (srec->status & S_EXPIRED) {
  471. if (!srec->uid[S] || ((ctx[S]->opts & OPEN_OLD) && !srec->msg[S])) {
  472. srec->status |= S_EXP_S;
  473. continue;
  474. }
  475. } else {
  476. if (smaxxuid >= srec->uid[S])
  477. continue;
  478. }
  479. if (minwuid > srec->uid[M])
  480. minwuid = srec->uid[M];
  481. }
  482. debug( " min non-orphaned master uid is %d\n", minwuid );
  483. Fprintf( jfp, "^\n" ); /* if any S_EXP_S */
  484. for (srec = recs; srec; srec = srec->next) {
  485. if (srec->status & S_DEAD)
  486. continue;
  487. if (srec->status & S_EXP_S) {
  488. if (minwuid > srec->uid[M] && maxuid[M] >= srec->uid[M]) {
  489. debug( " -> killing (%d,%d)\n", srec->uid[M], srec->uid[S] );
  490. srec->status = S_DEAD;
  491. Fprintf( jfp, "- %d %d\n", srec->uid[M], srec->uid[S] );
  492. } else if (srec->uid[S]) {
  493. debug( " -> orphaning (%d,[%d])\n", srec->uid[M], srec->uid[S] );
  494. Fprintf( jfp, "> %d %d 0\n", srec->uid[M], srec->uid[S] );
  495. srec->uid[S] = 0;
  496. }
  497. } else if (minwuid > srec->uid[M]) {
  498. if (srec->uid[S] < 0) {
  499. if (maxuid[M] >= srec->uid[M]) {
  500. debug( " -> killing (%d,%d)\n", srec->uid[M], srec->uid[S] );
  501. srec->status = S_DEAD;
  502. Fprintf( jfp, "- %d %d\n", srec->uid[M], srec->uid[S] );
  503. }
  504. } else if (srec->uid[M] > 0 && srec->uid[S] && (ctx[M]->opts & OPEN_OLD) &&
  505. (!(ctx[M]->opts & OPEN_NEW) || maxuid[M] >= srec->uid[M])) {
  506. if (nmexcs == rmexcs) {
  507. rmexcs = rmexcs * 2 + 100;
  508. mexcs = nfrealloc( mexcs, rmexcs * sizeof(int) );
  509. }
  510. mexcs[nmexcs++] = srec->uid[M];
  511. }
  512. }
  513. }
  514. debug( " exception list is:" );
  515. for (t = 0; t < nmexcs; t++)
  516. debug( " %d", mexcs[t] );
  517. debug( "\n" );
  518. } else if (ctx[M]->opts & OPEN_OLD)
  519. minwuid = 1;
  520. if (ctx[M]->opts & OPEN_NEW) {
  521. if (minwuid > maxuid[M] + 1)
  522. minwuid = maxuid[M] + 1;
  523. maxwuid = INT_MAX;
  524. } else if (ctx[M]->opts & OPEN_OLD) {
  525. maxwuid = 0;
  526. for (srec = recs; srec; srec = srec->next)
  527. if (!(srec->status & S_DEAD) && srec->uid[M] > maxwuid)
  528. maxwuid = srec->uid[M];
  529. } else
  530. maxwuid = 0;
  531. info( "Selecting master %s... ", ctx[M]->name );
  532. debug( maxwuid == INT_MAX ? "selecting master [%d,inf]\n" : "selecting master [%d,%d]\n", minwuid, maxwuid );
  533. switch (driver[M]->select( ctx[M], minwuid, maxwuid, mexcs, nmexcs )) {
  534. case DRV_STORE_BAD: ret = SYNC_BAD(M); goto finish;
  535. case DRV_BOX_BAD: ret = SYNC_FAIL; goto finish;
  536. }
  537. info( "%d messages, %d recent\n", ctx[M]->count, ctx[M]->recent );
  538. findmsgs( recs, ctx, M );
  539. if (muidval && muidval != ctx[M]->uidvalidity) {
  540. fprintf( stderr, "Error: UIDVALIDITY of master changed\n" );
  541. ret = SYNC_FAIL;
  542. goto finish;
  543. }
  544. if (!muidval || !suidval) {
  545. muidval = ctx[M]->uidvalidity;
  546. suidval = ctx[S]->uidvalidity;
  547. Fprintf( jfp, "| %d %d\n", muidval, suidval );
  548. }
  549. info( "Synchronizing...\n" );
  550. debug( "synchronizing old entries\n" );
  551. Fprintf( jfp, "^\n" );
  552. for (srec = recs; srec; srec = srec->next) {
  553. if (srec->status & S_DEAD)
  554. continue;
  555. debug( "pair (%d,%d)\n", srec->uid[M], srec->uid[S] );
  556. nom = !srec->msg[M] && (ctx[M]->opts & OPEN_OLD);
  557. nos = !srec->msg[S] && (ctx[S]->opts & OPEN_OLD);
  558. if (nom && nos) {
  559. debug( " vanished\n" );
  560. /* d.1) d.5) d.6) d.10) d.11) */
  561. srec->status = S_DEAD;
  562. Fprintf( jfp, "- %d %d\n", srec->uid[M], srec->uid[S] );
  563. } else {
  564. del[M] = nom && (srec->uid[M] > 0);
  565. del[S] = nos && (srec->uid[S] > 0);
  566. nflags = srec->flags;
  567. for (t = 0; t < 2; t++) {
  568. int uid, unex;
  569. unsigned char sflags, aflags, dflags, rflags;
  570. /* excludes (push) c.3) d.2) d.3) d.4) / (pull) b.3) d.7) d.8) d.9) */
  571. if (!srec->uid[t]) {
  572. /* b.1) / c.1) */
  573. debug( " no more %s\n", str_ms[t] );
  574. } else if (del[1-t]) {
  575. /* c.4) d.9) / b.4) d.4) */
  576. debug( " %s vanished\n", str_ms[1-t] );
  577. if (srec->msg[t] && srec->msg[t]->flags != nflags)
  578. info( "Info: conflicting changes in (%d,%d)\n", srec->uid[M], srec->uid[S] );
  579. if (chan->ops[t] & OP_DELETE) {
  580. debug( " -> %s delete\n", str_hl[t] );
  581. switch (driver[t]->set_flags( ctx[t], srec->msg[t], srec->uid[t], F_DELETED, 0 )) {
  582. case DRV_STORE_BAD: ret = SYNC_BAD(t); goto finish;
  583. case DRV_BOX_BAD: ret = SYNC_FAIL; goto finish;
  584. default: /* ok */ break;
  585. case DRV_OK:
  586. Fprintf( jfp, "%c %d %d 0\n", "><"[t], srec->uid[M], srec->uid[S] );
  587. srec->uid[1-t] = 0;
  588. }
  589. }
  590. } else if (!srec->msg[1-t])
  591. /* c.1) c.2) d.7) d.8) / b.1) b.2) d.2) d.3) */
  592. ;
  593. else if (srec->uid[t] < 0) {
  594. /* b.2) / c.2) */
  595. debug( " no %s yet\n", str_ms[t] );
  596. if (chan->ops[t] & OP_RENEW) {
  597. if ((chan->ops[t] & OP_EXPUNGE) && (srec->msg[1-t]->flags & F_DELETED)) {
  598. debug( " -> not %sing - would be expunged anyway\n", str_hl[t] );
  599. srec->msg[1-t]->status |= M_NOT_SYNCED;
  600. } else {
  601. if ((srec->msg[1-t]->flags & F_FLAGGED) || !chan->stores[t]->max_size || srec->msg[1-t]->size <= chan->stores[t]->max_size) {
  602. debug( " -> %sing it\n", str_hl[t] );
  603. msgdata.flags = srec->msg[1-t]->flags;
  604. switch (driver[1-t]->fetch_msg( ctx[1-t], srec->msg[1-t], &msgdata )) {
  605. case DRV_STORE_BAD: ret = SYNC_BAD(1-t); goto finish;
  606. case DRV_BOX_BAD: ret = SYNC_FAIL; goto finish;
  607. default: /* ok */ srec->msg[1-t]->status |= M_NOT_SYNCED; break;
  608. case DRV_OK:
  609. srec->msg[1-t]->flags = msgdata.flags;
  610. switch (driver[t]->store_msg( ctx[t], &msgdata, &uid )) {
  611. case DRV_STORE_BAD: ret = SYNC_BAD(t); goto finish;
  612. default: ret = SYNC_FAIL; goto finish;
  613. case DRV_OK:
  614. Fprintf( jfp, "%c %d %d %d\n", "<>"[t], srec->uid[M], srec->uid[S], uid );
  615. srec->uid[t] = uid;
  616. nflags = srec->msg[1-t]->flags;
  617. }
  618. }
  619. } else {
  620. debug( " -> not %sing - still too big\n", str_hl[t] );
  621. srec->msg[1-t]->status |= M_NOT_SYNCED;
  622. }
  623. }
  624. } else
  625. srec->msg[1-t]->status |= M_NOT_SYNCED;
  626. } else if (!del[t]) {
  627. /* a) & b.3) / c.3) */
  628. debug( " may %s\n", str_hl[t] );
  629. if (chan->ops[t] & OP_FLAGS) {
  630. debug( " -> %sing flags\n", str_hl[t] );
  631. sflags = srec->msg[1-t]->flags;
  632. aflags = sflags & ~nflags;
  633. dflags = ~sflags & nflags;
  634. unex = 0;
  635. if (srec->status & S_EXPIRED) {
  636. if (!t) {
  637. if ((aflags & ~F_DELETED) || dflags)
  638. info( "Info: Flags of expired message changed in (%d,%d)\n", srec->uid[M], srec->uid[S] );
  639. continue;
  640. } else {
  641. if ((sflags & F_FLAGGED) && !(sflags & F_DELETED)) {
  642. unex = 1;
  643. dflags |= F_DELETED;
  644. } else
  645. continue;
  646. }
  647. }
  648. if ((chan->ops[t] & OP_EXPUNGE) && (sflags & F_DELETED) &&
  649. (!ctx[t]->conf->trash || ctx[t]->conf->trash_only_new))
  650. {
  651. aflags &= F_DELETED;
  652. dflags = 0;
  653. }
  654. rflags = (nflags | aflags) & ~dflags;
  655. switch ((aflags | dflags) ? driver[t]->set_flags( ctx[t], srec->msg[t], srec->uid[t], aflags, dflags ) : DRV_OK) {
  656. case DRV_STORE_BAD: ret = SYNC_BAD(t); goto finish;
  657. case DRV_BOX_BAD: ret = SYNC_FAIL; goto finish;
  658. default: /* ok */ break;
  659. case DRV_OK:
  660. nflags = rflags;
  661. if (unex) {
  662. debug( "unexpiring pair(%d,%d)\n", srec->uid[M], srec->uid[S] );
  663. /* log last, so deletion can't be misinterpreted! */
  664. Fprintf( jfp, "~ %d %d 0\n", srec->uid[M], srec->uid[S] );
  665. srec->status &= ~S_EXPIRED;
  666. }
  667. }
  668. }
  669. } /* else b.4) / c.4) */
  670. }
  671. if (srec->flags != nflags) {
  672. debug( " updating flags (%u -> %u)\n", srec->flags, nflags );
  673. srec->flags = nflags;
  674. Fprintf( jfp, "* %d %d %u\n", srec->uid[M], srec->uid[S], nflags );
  675. }
  676. if (srec->msg[M] && (srec->msg[M]->flags & F_DELETED))
  677. srec->status |= S_DEL(M);
  678. if (srec->msg[S] && (srec->msg[S]->flags & F_DELETED))
  679. srec->status |= S_DEL(S);
  680. }
  681. }
  682. debug( "synchronizing new entries\n" );
  683. for (t = 0; t < 2; t++) {
  684. int nmsgs, uid;
  685. for (nmsgs = 0, tmsg = ctx[1-t]->msgs; tmsg; tmsg = tmsg->next)
  686. if (!(tmsg->status & M_PROCESSED)) {
  687. if (chan->ops[t] & OP_NEW) {
  688. debug( "new message %d on %s\n", tmsg->uid, str_ms[1-t] );
  689. if ((chan->ops[t] & OP_EXPUNGE) && (tmsg->flags & F_DELETED)) {
  690. debug( " not %sing - would be expunged anyway\n", str_hl[t] );
  691. tmsg->status |= M_NOT_SYNCED;
  692. } else {
  693. if ((tmsg->flags & F_FLAGGED) || !chan->stores[t]->max_size || tmsg->size <= chan->stores[t]->max_size) {
  694. debug( " %sing it\n", str_hl[t] );
  695. if (!nmsgs)
  696. info( t ? "Pulling new messages..." : "Pushing new messages..." );
  697. else
  698. infoc( '.' );
  699. nmsgs++;
  700. msgdata.flags = tmsg->flags;
  701. switch (driver[1-t]->fetch_msg( ctx[1-t], tmsg, &msgdata )) {
  702. case DRV_STORE_BAD: return SYNC_BAD(1-t);
  703. case DRV_BOX_BAD: return SYNC_FAIL;
  704. case DRV_MSG_BAD: /* ok */ tmsg->status |= M_NOT_SYNCED; continue;
  705. }
  706. tmsg->flags = msgdata.flags;
  707. switch (driver[t]->store_msg( ctx[t], &msgdata, &uid )) {
  708. case DRV_STORE_BAD: return SYNC_BAD(t);
  709. default: return SYNC_FAIL;
  710. case DRV_OK: break;
  711. }
  712. } else {
  713. debug( " not %sing - too big\n", str_hl[t] );
  714. tmsg->status |= M_NOT_SYNCED;
  715. uid = -1;
  716. }
  717. srec = nfmalloc( sizeof(*srec) );
  718. srec->uid[1-t] = tmsg->uid;
  719. srec->uid[t] = uid;
  720. srec->flags = tmsg->flags;
  721. srec->status = 0;
  722. srec->next = 0;
  723. *srecadd = srec;
  724. srecadd = &srec->next;
  725. Fprintf( jfp, "+ %d %d %u\n", srec->uid[M], srec->uid[S], srec->flags );
  726. if (maxuid[1-t] < tmsg->uid) {
  727. maxuid[1-t] = tmsg->uid;
  728. Fprintf( jfp, "%c %d\n", ")("[t], tmsg->uid );
  729. }
  730. }
  731. } else
  732. tmsg->status |= M_NOT_SYNCED;
  733. }
  734. if (nmsgs)
  735. info( " %d messages\n", nmsgs );
  736. }
  737. if ((chan->ops[S] & (OP_NEW|OP_RENEW)) && chan->max_messages) {
  738. debug( "expiring excess entries\n" );
  739. todel = ctx[S]->count - chan->max_messages;
  740. for (tmsg = ctx[S]->msgs; tmsg && todel > 0; tmsg = tmsg->next)
  741. if (!(tmsg->status & M_DEAD) && (tmsg->flags & F_DELETED))
  742. todel--;
  743. delt = 0;
  744. for (tmsg = ctx[S]->msgs; tmsg && todel > 0; tmsg = tmsg->next) {
  745. if ((tmsg->status & M_DEAD) || (tmsg->flags & F_DELETED))
  746. continue;
  747. if ((tmsg->flags & F_FLAGGED) || (tmsg->status & M_NOT_SYNCED)) /* add M_DESYNCED? */
  748. todel--;
  749. else if (!(tmsg->status & M_RECENT)) {
  750. tmsg->status |= M_EXPIRED;
  751. delt++;
  752. todel--;
  753. }
  754. }
  755. if (delt) {
  756. Fprintf( jfp, "^\n" );
  757. for (srec = recs; srec; srec = srec->next) {
  758. if (srec->status & (S_DEAD|S_EXPIRED))
  759. continue;
  760. if (srec->msg[S] && (srec->msg[S]->status & M_EXPIRED)) {
  761. debug( " expiring pair(%d,%d)\n", srec->uid[M], srec->uid[S] );
  762. /* log first, so deletion can't be misinterpreted! */
  763. Fprintf( jfp, "~ %d %d 1\n", srec->uid[M], srec->uid[S] );
  764. if (smaxxuid < srec->uid[S])
  765. smaxxuid = srec->uid[S];
  766. srec->status |= S_EXPIRED;
  767. switch (driver[S]->set_flags( ctx[S], srec->msg[S], 0, F_DELETED, 0 )) {
  768. case DRV_STORE_BAD: ret = SYNC_BAD(S); goto finish;
  769. case DRV_BOX_BAD: ret = SYNC_FAIL; goto finish;
  770. default: /* ok */ break;
  771. case DRV_OK: srec->status |= S_DEL(S);
  772. }
  773. }
  774. }
  775. }
  776. }
  777. /* Doing CLOSE here instead of EXPUNGE above saves network traffic.
  778. But it costs more server power for single-file formats. And it
  779. makes disk-full/quota-exceeded more probable. */
  780. for (t = 0; t < 2; t++) {
  781. ex[t] = 0;
  782. if (chan->ops[t] & OP_EXPUNGE) {
  783. info( "Expunging %s\n", str_ms[t] );
  784. debug( "expunging %s\n", str_ms[t] );
  785. for (tmsg = ctx[t]->msgs; tmsg; tmsg = tmsg->next)
  786. if (tmsg->flags & F_DELETED) {
  787. if (ctx[t]->conf->trash) {
  788. if (!ctx[t]->conf->trash_only_new || (tmsg->status & M_NOT_SYNCED)) {
  789. debug( " trashing message %d\n", tmsg->uid );
  790. switch (driver[t]->trash_msg( ctx[t], tmsg )) {
  791. case DRV_OK: break;
  792. case DRV_STORE_BAD: ret = SYNC_BAD(t); goto finish;
  793. default: ret = SYNC_FAIL; goto nexex;
  794. }
  795. } else
  796. debug( " not trashing message %d - not new\n", tmsg->uid );
  797. } else if (ctx[1-t]->conf->trash && ctx[1-t]->conf->trash_remote_new) {
  798. if (tmsg->status & M_NOT_SYNCED) {
  799. if (!ctx[1-t]->conf->max_size || tmsg->size <= ctx[1-t]->conf->max_size) {
  800. debug( " remote trashing message %d\n", tmsg->uid );
  801. msgdata.flags = tmsg->flags;
  802. switch (driver[t]->fetch_msg( ctx[t], tmsg, &msgdata )) {
  803. case DRV_OK: break;
  804. case DRV_STORE_BAD: ret = SYNC_BAD(t); goto finish;
  805. default: ret = SYNC_FAIL; goto nexex;
  806. }
  807. switch (driver[1-t]->store_msg( ctx[1-t], &msgdata, 0 )) {
  808. case DRV_OK: break;
  809. case DRV_STORE_BAD: ret = SYNC_BAD(1-t); goto finish;
  810. default: ret = SYNC_FAIL; goto nexex;
  811. }
  812. } else
  813. debug( " not remote trashing message %d - too big\n", tmsg->uid );
  814. } else
  815. debug( " not remote trashing message %d - not new\n", tmsg->uid );
  816. }
  817. }
  818. switch (driver[t]->close( ctx[t] )) {
  819. case DRV_OK: ex[t] = 1; break;
  820. case DRV_STORE_BAD: ret = SYNC_BAD(t); goto finish;
  821. default: break;
  822. }
  823. }
  824. nexex: ;
  825. }
  826. if (ex[M] || ex[S]) {
  827. /* This cleanup is not strictly necessary, as the next full sync
  828. would throw out the dead entries anyway. But ... */
  829. minwuid = INT_MAX;
  830. if (smaxxuid) {
  831. debug( "preparing entry purge - max expired slave uid is %d\n", smaxxuid );
  832. for (srec = recs; srec; srec = srec->next) {
  833. if (srec->status & S_DEAD)
  834. continue;
  835. if (!((srec->uid[S] <= 0 || ((srec->status & S_DEL(S)) && ex[S])) &&
  836. (srec->uid[M] <= 0 || ((srec->status & S_DEL(M)) && ex[M]) || (srec->status & S_EXPIRED))) &&
  837. smaxxuid < srec->uid[S] && minwuid > srec->uid[M])
  838. minwuid = srec->uid[M];
  839. }
  840. debug( " min non-orphaned master uid is %d\n", minwuid );
  841. }
  842. Fprintf( jfp, "^\n" );
  843. for (srec = recs; srec; srec = srec->next) {
  844. if (srec->status & S_DEAD)
  845. continue;
  846. if (srec->uid[S] <= 0 || ((srec->status & S_DEL(S)) && ex[S])) {
  847. if (srec->uid[M] <= 0 || ((srec->status & S_DEL(M)) && ex[M])) {
  848. debug( " -> killing (%d,%d)\n", srec->uid[M], srec->uid[S] );
  849. srec->status = S_DEAD;
  850. Fprintf( jfp, "- %d %d\n", srec->uid[M], srec->uid[S] );
  851. } else if (srec->status & S_EXPIRED) {
  852. if (maxuid[M] >= srec->uid[M] && minwuid > srec->uid[M]) {
  853. debug( " -> killing (%d,%d)\n", srec->uid[M], srec->uid[S] );
  854. srec->status = S_DEAD;
  855. Fprintf( jfp, "- %d %d\n", srec->uid[M], srec->uid[S] );
  856. } else if (srec->uid[S]) {
  857. debug( " -> orphaning (%d,[%d])\n", srec->uid[M], srec->uid[S] );
  858. Fprintf( jfp, "> %d %d 0\n", srec->uid[M], srec->uid[S] );
  859. srec->uid[S] = 0;
  860. }
  861. }
  862. }
  863. }
  864. }
  865. finish:
  866. Fprintf( nfp, "%d:%d %d:%d:%d\n", muidval, maxuid[M], suidval, smaxxuid, maxuid[S] );
  867. for (srec = recs; srec; srec = srec->next) {
  868. if (srec->status & S_DEAD)
  869. continue;
  870. make_flags( srec->flags, fbuf );
  871. Fprintf( nfp, "%d %d %s%s\n", srec->uid[M], srec->uid[S],
  872. srec->status & S_EXPIRED ? "X" : "", fbuf );
  873. }
  874. fclose( nfp );
  875. fclose( jfp );
  876. /* order is important! */
  877. rename( nname, dname );
  878. unlink( jname );
  879. bail:
  880. for (srec = recs; srec; srec = nsrec) {
  881. nsrec = srec->next;
  882. free( srec );
  883. }
  884. unlink( lname );
  885. bail1:
  886. close( lfd );
  887. bail2:
  888. free( lname );
  889. free( nname );
  890. free( jname );
  891. free( dname );
  892. return ret;
  893. }