sync.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940
  1. /*
  2. * mbsync - mailbox synchronizer
  3. * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
  4. * Copyright (C) 2002-2006,2010-2013 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, see <http://www.gnu.org/licenses/>.
  18. *
  19. * As a special exception, mbsync may be linked with the OpenSSL library,
  20. * despite that library's more restrictive license.
  21. */
  22. #include "sync.h"
  23. #include <assert.h>
  24. #include <stdio.h>
  25. #include <limits.h>
  26. #include <stdlib.h>
  27. #include <stddef.h>
  28. #include <unistd.h>
  29. #include <time.h>
  30. #include <fcntl.h>
  31. #include <ctype.h>
  32. #include <string.h>
  33. #include <errno.h>
  34. #include <sys/stat.h>
  35. #if !defined(_POSIX_SYNCHRONIZED_IO) || _POSIX_SYNCHRONIZED_IO <= 0
  36. # define fdatasync fsync
  37. #endif
  38. channel_conf_t global_conf;
  39. channel_conf_t *channels;
  40. group_conf_t *groups;
  41. const char *str_ms[] = { "master", "slave" }, *str_hl[] = { "push", "pull" };
  42. void
  43. Fclose( FILE *f, int safe )
  44. {
  45. if ((safe && (fflush( f ) || (UseFSync && fdatasync( fileno( f ) )))) || fclose( f ) == EOF) {
  46. sys_error( "Error: cannot close file" );
  47. exit( 1 );
  48. }
  49. }
  50. void
  51. Fprintf( FILE *f, const char *msg, ... )
  52. {
  53. int r;
  54. va_list va;
  55. va_start( va, msg );
  56. r = vfprintf( f, msg, va );
  57. va_end( va );
  58. if (r < 0) {
  59. sys_error( "Error: cannot write file" );
  60. exit( 1 );
  61. }
  62. }
  63. static const char Flags[] = { 'D', 'F', 'R', 'S', 'T' };
  64. static int
  65. parse_flags( const char *buf )
  66. {
  67. uint flags, i, d;
  68. for (flags = i = d = 0; i < as(Flags); i++)
  69. if (buf[d] == Flags[i]) {
  70. flags |= (1 << i);
  71. d++;
  72. }
  73. return flags;
  74. }
  75. static int
  76. make_flags( int flags, char *buf )
  77. {
  78. uint i, d;
  79. for (i = d = 0; i < as(Flags); i++)
  80. if (flags & (1 << i))
  81. buf[d++] = Flags[i];
  82. buf[d] = 0;
  83. return d;
  84. }
  85. #define S_DEAD (1<<0) /* ephemeral: the entry was killed and should be ignored */
  86. #define S_DEL(ms) (1<<(2+(ms))) /* ephemeral: m/s message would be subject to expunge */
  87. #define S_EXPIRED (1<<4) /* the entry is expired (slave message removal confirmed) */
  88. #define S_EXPIRE (1<<5) /* the entry is being expired (slave message removal scheduled) */
  89. #define S_NEXPIRE (1<<6) /* temporary: new expiration state */
  90. #define S_DELETE (1<<7) /* ephemeral: flags propagation is a deletion */
  91. #define mvBit(in,ib,ob) ((uchar)(((uint)in) * (ob) / (ib)))
  92. typedef struct sync_rec {
  93. struct sync_rec *next;
  94. /* string_list_t *keywords; */
  95. int uid[2]; /* -2 = pending (use tuid), -1 = skipped (too big), 0 = expired */
  96. message_t *msg[2];
  97. uchar status, flags, aflags[2], dflags[2];
  98. char tuid[TUIDL];
  99. } sync_rec_t;
  100. /* cases:
  101. a) both non-null
  102. b) only master null
  103. b.1) uid[M] 0
  104. b.2) uid[M] -1
  105. b.3) master not scanned
  106. b.4) master gone
  107. c) only slave null
  108. c.1) uid[S] 0
  109. c.2) uid[S] -1
  110. c.3) slave not scanned
  111. c.4) slave gone
  112. d) both null
  113. d.1) both gone
  114. d.2) uid[M] 0, slave not scanned
  115. d.3) uid[M] -1, slave not scanned
  116. d.4) master gone, slave not scanned
  117. d.5) uid[M] 0, slave gone
  118. d.6) uid[M] -1, slave gone
  119. d.7) uid[S] 0, master not scanned
  120. d.8) uid[S] -1, master not scanned
  121. d.9) slave gone, master not scanned
  122. d.10) uid[S] 0, master gone
  123. d.11) uid[S] -1, master gone
  124. impossible cases: both uid[M] & uid[S] 0 or -1, both not scanned
  125. */
  126. typedef struct {
  127. int t[2];
  128. void (*cb)( int sts, void *aux ), *aux;
  129. char *dname, *jname, *nname, *lname, *box_name[2];
  130. FILE *jfp, *nfp;
  131. sync_rec_t *srecs, **srecadd;
  132. channel_conf_t *chan;
  133. store_t *ctx[2];
  134. driver_t *drv[2];
  135. const char *orig_name[2];
  136. int state[2], ref_count, nsrecs, ret, lfd;
  137. int new_total[2], new_done[2];
  138. int flags_total[2], flags_done[2];
  139. int trash_total[2], trash_done[2];
  140. int maxuid[2]; /* highest UID that was already propagated */
  141. int newmaxuid[2]; /* highest UID that is currently being propagated */
  142. int uidval[2]; /* UID validity value */
  143. int newuid[2]; /* TUID lookup makes sense only for UIDs >= this */
  144. int mmaxxuid; /* highest expired UID on master during new message propagation */
  145. int smaxxuid; /* highest expired UID on slave */
  146. } sync_vars_t;
  147. static void sync_ref( sync_vars_t *svars ) { ++svars->ref_count; }
  148. static void sync_deref( sync_vars_t *svars );
  149. static int check_cancel( sync_vars_t *svars );
  150. #define AUX &svars->t[t]
  151. #define INV_AUX &svars->t[1-t]
  152. #define DECL_SVARS \
  153. int t; \
  154. sync_vars_t *svars
  155. #define INIT_SVARS(aux) \
  156. t = *(int *)aux; \
  157. svars = (sync_vars_t *)(((char *)(&((int *)aux)[-t])) - offsetof(sync_vars_t, t))
  158. #define DECL_INIT_SVARS(aux) \
  159. int t = *(int *)aux; \
  160. sync_vars_t *svars = (sync_vars_t *)(((char *)(&((int *)aux)[-t])) - offsetof(sync_vars_t, t))
  161. /* operation dependencies:
  162. select(x): -
  163. load(x): select(x)
  164. new(M), new(S), flags(M), flags(S): load(M) & load(S)
  165. find_new(x): new(x)
  166. trash(x): flags(x)
  167. close(x): trash(x) & find_new(x) & new(!x) // with expunge
  168. cleanup: close(M) & close(S)
  169. */
  170. #define ST_LOADED (1<<0)
  171. #define ST_FIND_OLD (1<<1)
  172. #define ST_SENT_NEW (1<<2)
  173. #define ST_FIND_NEW (1<<3)
  174. #define ST_FOUND_NEW (1<<4)
  175. #define ST_SENT_FLAGS (1<<5)
  176. #define ST_SENT_TRASH (1<<6)
  177. #define ST_CLOSED (1<<7)
  178. #define ST_SENT_CANCEL (1<<8)
  179. #define ST_CANCELED (1<<9)
  180. #define ST_SELECTED (1<<10)
  181. #define ST_DID_EXPUNGE (1<<11)
  182. #define ST_CLOSING (1<<12)
  183. static void
  184. match_tuids( sync_vars_t *svars, int t )
  185. {
  186. sync_rec_t *srec;
  187. message_t *tmsg, *ntmsg = 0;
  188. const char *diag;
  189. int num_lost = 0;
  190. for (srec = svars->srecs; srec; srec = srec->next) {
  191. if (srec->status & S_DEAD)
  192. continue;
  193. if (srec->uid[t] == -2 && srec->tuid[0]) {
  194. debug( " pair(%d,%d): lookup %s, TUID %." stringify(TUIDL) "s\n", srec->uid[M], srec->uid[S], str_ms[t], srec->tuid );
  195. for (tmsg = ntmsg; tmsg; tmsg = tmsg->next) {
  196. if (tmsg->status & M_DEAD)
  197. continue;
  198. if (tmsg->tuid[0] && !memcmp( tmsg->tuid, srec->tuid, TUIDL )) {
  199. diag = (tmsg == ntmsg) ? "adjacently" : "after gap";
  200. goto mfound;
  201. }
  202. }
  203. for (tmsg = svars->ctx[t]->msgs; tmsg != ntmsg; tmsg = tmsg->next) {
  204. if (tmsg->status & M_DEAD)
  205. continue;
  206. if (tmsg->tuid[0] && !memcmp( tmsg->tuid, srec->tuid, TUIDL )) {
  207. diag = "after reset";
  208. goto mfound;
  209. }
  210. }
  211. debug( " -> TUID lost\n" );
  212. Fprintf( svars->jfp, "& %d %d\n", srec->uid[M], srec->uid[S] );
  213. srec->flags = 0;
  214. srec->tuid[0] = 0;
  215. num_lost++;
  216. continue;
  217. mfound:
  218. debug( " -> new UID %d %s\n", tmsg->uid, diag );
  219. Fprintf( svars->jfp, "%c %d %d %d\n", "<>"[t], srec->uid[M], srec->uid[S], tmsg->uid );
  220. tmsg->srec = srec;
  221. srec->msg[t] = tmsg;
  222. ntmsg = tmsg->next;
  223. srec->uid[t] = tmsg->uid;
  224. srec->tuid[0] = 0;
  225. }
  226. }
  227. if (num_lost)
  228. warn( "Warning: lost track of %d %sed message(s)\n", num_lost, str_hl[t] );
  229. }
  230. typedef struct copy_vars {
  231. void (*cb)( int sts, int uid, struct copy_vars *vars );
  232. void *aux;
  233. sync_rec_t *srec; /* also ->tuid */
  234. message_t *msg;
  235. msg_data_t data;
  236. } copy_vars_t;
  237. static void msg_fetched( int sts, void *aux );
  238. static void
  239. copy_msg( copy_vars_t *vars )
  240. {
  241. DECL_INIT_SVARS(vars->aux);
  242. t ^= 1;
  243. vars->data.flags = vars->msg->flags;
  244. vars->data.date = svars->chan->use_internal_date ? -1 : 0;
  245. svars->drv[t]->fetch_msg( svars->ctx[t], vars->msg, &vars->data, msg_fetched, vars );
  246. }
  247. static void msg_stored( int sts, int uid, void *aux );
  248. static void
  249. msg_fetched( int sts, void *aux )
  250. {
  251. copy_vars_t *vars = (copy_vars_t *)aux;
  252. DECL_SVARS;
  253. char *fmap, *buf;
  254. int i, len, extra, scr, tcr, lcrs, hcrs, bcrs, lines;
  255. int start, sbreak = 0, ebreak = 0;
  256. char c;
  257. switch (sts) {
  258. case DRV_OK:
  259. INIT_SVARS(vars->aux);
  260. if (check_cancel( svars )) {
  261. free( vars->data.data );
  262. vars->cb( SYNC_CANCELED, 0, vars );
  263. return;
  264. }
  265. vars->msg->flags = vars->data.flags;
  266. scr = (svars->drv[1-t]->flags / DRV_CRLF) & 1;
  267. tcr = (svars->drv[t]->flags / DRV_CRLF) & 1;
  268. if (vars->srec || scr != tcr) {
  269. fmap = vars->data.data;
  270. len = vars->data.len;
  271. extra = lines = hcrs = bcrs = i = 0;
  272. if (vars->srec) {
  273. nloop:
  274. start = i;
  275. lcrs = 0;
  276. while (i < len) {
  277. c = fmap[i++];
  278. if (c == '\r')
  279. lcrs++;
  280. else if (c == '\n') {
  281. if (starts_with( fmap + start, len - start, "X-TUID: ", 8 )) {
  282. extra = (sbreak = start) - (ebreak = i);
  283. goto oke;
  284. }
  285. lines++;
  286. hcrs += lcrs;
  287. if (i - lcrs - 1 == start) {
  288. sbreak = ebreak = start;
  289. goto oke;
  290. }
  291. goto nloop;
  292. }
  293. }
  294. /* invalid message */
  295. warn( "Warning: message %d from %s has incomplete header.\n",
  296. vars->msg->uid, str_ms[1-t] );
  297. free( fmap );
  298. vars->cb( SYNC_NOGOOD, 0, vars );
  299. return;
  300. oke:
  301. extra += 8 + TUIDL + 1 + (tcr && (!scr || hcrs));
  302. }
  303. if (tcr != scr) {
  304. for (; i < len; i++) {
  305. c = fmap[i];
  306. if (c == '\r')
  307. bcrs++;
  308. else if (c == '\n')
  309. lines++;
  310. }
  311. extra -= hcrs + bcrs;
  312. if (tcr)
  313. extra += lines;
  314. }
  315. vars->data.len = len + extra;
  316. buf = vars->data.data = nfmalloc( vars->data.len );
  317. i = 0;
  318. if (vars->srec) {
  319. if (tcr != scr) {
  320. if (tcr) {
  321. for (; i < sbreak; i++)
  322. if ((c = fmap[i]) != '\r') {
  323. if (c == '\n')
  324. *buf++ = '\r';
  325. *buf++ = c;
  326. }
  327. } else {
  328. for (; i < sbreak; i++)
  329. if ((c = fmap[i]) != '\r')
  330. *buf++ = c;
  331. }
  332. } else {
  333. memcpy( buf, fmap, sbreak );
  334. buf += sbreak;
  335. }
  336. memcpy( buf, "X-TUID: ", 8 );
  337. buf += 8;
  338. memcpy( buf, vars->srec->tuid, TUIDL );
  339. buf += TUIDL;
  340. if (tcr && (!scr || hcrs))
  341. *buf++ = '\r';
  342. *buf++ = '\n';
  343. i = ebreak;
  344. }
  345. if (tcr != scr) {
  346. if (tcr) {
  347. for (; i < len; i++)
  348. if ((c = fmap[i]) != '\r') {
  349. if (c == '\n')
  350. *buf++ = '\r';
  351. *buf++ = c;
  352. }
  353. } else {
  354. for (; i < len; i++)
  355. if ((c = fmap[i]) != '\r')
  356. *buf++ = c;
  357. }
  358. } else
  359. memcpy( buf, fmap + i, len - i );
  360. free( fmap );
  361. }
  362. svars->drv[t]->store_msg( svars->ctx[t], &vars->data, !vars->srec, msg_stored, vars );
  363. break;
  364. case DRV_CANCELED:
  365. vars->cb( SYNC_CANCELED, 0, vars );
  366. break;
  367. case DRV_MSG_BAD:
  368. vars->cb( SYNC_NOGOOD, 0, vars );
  369. break;
  370. default:
  371. vars->cb( SYNC_FAIL, 0, vars );
  372. break;
  373. }
  374. }
  375. static void
  376. msg_stored( int sts, int uid, void *aux )
  377. {
  378. copy_vars_t *vars = (copy_vars_t *)aux;
  379. DECL_SVARS;
  380. switch (sts) {
  381. case DRV_OK:
  382. vars->cb( SYNC_OK, uid, vars );
  383. break;
  384. case DRV_CANCELED:
  385. vars->cb( SYNC_CANCELED, 0, vars );
  386. break;
  387. case DRV_MSG_BAD:
  388. INIT_SVARS(vars->aux);
  389. (void)svars;
  390. warn( "Warning: %s refuses to store message %d from %s.\n",
  391. str_ms[t], vars->msg->uid, str_ms[1-t] );
  392. vars->cb( SYNC_NOGOOD, 0, vars );
  393. break;
  394. default:
  395. vars->cb( SYNC_FAIL, 0, vars );
  396. break;
  397. }
  398. }
  399. static void
  400. stats( sync_vars_t *svars )
  401. {
  402. char buf[2][64];
  403. char *cs;
  404. int t, l;
  405. static int cols = -1;
  406. if (cols < 0 && (!(cs = getenv( "COLUMNS" )) || !(cols = atoi( cs ) / 2)))
  407. cols = 36;
  408. if (!(DFlags & QUIET)) {
  409. for (t = 0; t < 2; t++) {
  410. l = sprintf( buf[t], "+%d/%d *%d/%d #%d/%d",
  411. svars->new_done[t], svars->new_total[t],
  412. svars->flags_done[t], svars->flags_total[t],
  413. svars->trash_done[t], svars->trash_total[t] );
  414. if (l > cols)
  415. buf[t][cols - 1] = '~';
  416. }
  417. infon( "\v\rM: %.*s S: %.*s", cols, buf[0], cols, buf[1] );
  418. }
  419. }
  420. static void sync_bail( sync_vars_t *svars );
  421. static void sync_bail1( sync_vars_t *svars );
  422. static void sync_bail2( sync_vars_t *svars );
  423. static void sync_bail3( sync_vars_t *svars );
  424. static void cancel_done( void *aux );
  425. static void
  426. cancel_sync( sync_vars_t *svars )
  427. {
  428. int t;
  429. for (t = 0; t < 2; t++) {
  430. int other_state = svars->state[1-t];
  431. if (svars->ret & SYNC_BAD(t)) {
  432. cancel_done( AUX );
  433. } else if (!(svars->state[t] & ST_SENT_CANCEL)) {
  434. /* ignore subsequent failures from in-flight commands */
  435. svars->state[t] |= ST_SENT_CANCEL;
  436. svars->drv[t]->cancel_cmds( svars->ctx[t], cancel_done, AUX );
  437. }
  438. if (other_state & ST_CANCELED)
  439. break;
  440. }
  441. }
  442. static void
  443. cancel_done( void *aux )
  444. {
  445. DECL_INIT_SVARS(aux);
  446. svars->state[t] |= ST_CANCELED;
  447. if (svars->state[1-t] & ST_CANCELED) {
  448. if (svars->lfd >= 0) {
  449. Fclose( svars->nfp, 0 );
  450. Fclose( svars->jfp, 0 );
  451. sync_bail( svars );
  452. } else {
  453. /* Early failure during box selection. */
  454. sync_bail2( svars );
  455. }
  456. }
  457. }
  458. static void
  459. store_bad( void *aux )
  460. {
  461. DECL_INIT_SVARS(aux);
  462. svars->drv[t]->cancel_store( svars->ctx[t] );
  463. svars->ret |= SYNC_BAD(t);
  464. cancel_sync( svars );
  465. }
  466. static int
  467. check_cancel( sync_vars_t *svars )
  468. {
  469. return (svars->state[M] | svars->state[S]) & (ST_SENT_CANCEL | ST_CANCELED);
  470. }
  471. static int
  472. check_ret( int sts, void *aux )
  473. {
  474. DECL_SVARS;
  475. if (sts == DRV_CANCELED)
  476. return 1;
  477. INIT_SVARS(aux);
  478. if (sts == DRV_BOX_BAD) {
  479. svars->ret |= SYNC_FAIL;
  480. cancel_sync( svars );
  481. return 1;
  482. }
  483. return check_cancel( svars );
  484. }
  485. #define SVARS_CHECK_RET \
  486. DECL_SVARS; \
  487. if (check_ret( sts, aux )) \
  488. return; \
  489. INIT_SVARS(aux)
  490. #define SVARS_CHECK_RET_VARS(type) \
  491. type *vars = (type *)aux; \
  492. DECL_SVARS; \
  493. if (check_ret( sts, vars->aux )) { \
  494. free( vars ); \
  495. return; \
  496. } \
  497. INIT_SVARS(vars->aux)
  498. #define SVARS_CHECK_CANCEL_RET \
  499. DECL_SVARS; \
  500. if (sts == SYNC_CANCELED) { \
  501. free( vars ); \
  502. return; \
  503. } \
  504. INIT_SVARS(vars->aux)
  505. static char *
  506. clean_strdup( const char *s )
  507. {
  508. char *cs;
  509. int i;
  510. cs = nfstrdup( s );
  511. for (i = 0; cs[i]; i++)
  512. if (cs[i] == '/')
  513. cs[i] = '!';
  514. return cs;
  515. }
  516. #define JOURNAL_VERSION "2"
  517. static void box_selected( int sts, void *aux );
  518. void
  519. sync_boxes( store_t *ctx[], const char *names[], channel_conf_t *chan,
  520. void (*cb)( int sts, void *aux ), void *aux )
  521. {
  522. sync_vars_t *svars;
  523. int t;
  524. svars = nfcalloc( sizeof(*svars) );
  525. svars->t[1] = 1;
  526. svars->ref_count = 1;
  527. svars->cb = cb;
  528. svars->aux = aux;
  529. svars->ctx[0] = ctx[0];
  530. svars->ctx[1] = ctx[1];
  531. svars->chan = chan;
  532. svars->uidval[0] = svars->uidval[1] = -1;
  533. svars->srecadd = &svars->srecs;
  534. for (t = 0; t < 2; t++) {
  535. svars->orig_name[t] =
  536. (!names[t] || (ctx[t]->conf->map_inbox && !strcmp( ctx[t]->conf->map_inbox, names[t] ))) ?
  537. "INBOX" : names[t];
  538. if (!ctx[t]->conf->flat_delim) {
  539. svars->box_name[t] = nfstrdup( svars->orig_name[t] );
  540. } else if (map_name( svars->orig_name[t], &svars->box_name[t], 0, "/", ctx[t]->conf->flat_delim ) < 0) {
  541. error( "Error: canonical mailbox name '%s' contains flattened hierarchy delimiter\n", svars->orig_name[t] );
  542. svars->ret = SYNC_FAIL;
  543. sync_bail3( svars );
  544. return;
  545. }
  546. ctx[t]->uidvalidity = -1;
  547. set_bad_callback( ctx[t], store_bad, AUX );
  548. svars->drv[t] = ctx[t]->conf->driver;
  549. }
  550. /* Both boxes must be fully set up at this point, so that error exit paths
  551. * don't run into uninitialized variables. */
  552. sync_ref( svars );
  553. for (t = 0; t < 2; t++) {
  554. info( "Selecting %s %s...\n", str_ms[t], svars->orig_name[t] );
  555. svars->drv[t]->select_box( ctx[t], svars->box_name[t], (chan->ops[t] & OP_CREATE) != 0, box_selected, AUX );
  556. if (check_cancel( svars ))
  557. break;
  558. }
  559. sync_deref( svars );
  560. }
  561. static void load_box( sync_vars_t *svars, int t, int minwuid, int *mexcs, int nmexcs );
  562. static void
  563. box_selected( int sts, void *aux )
  564. {
  565. DECL_SVARS;
  566. sync_rec_t *srec, *nsrec;
  567. char *s, *cmname, *csname;
  568. store_t *ctx[2];
  569. channel_conf_t *chan;
  570. FILE *jfp;
  571. int opts[2], line, t1, t2, t3;
  572. int *mexcs, nmexcs, rmexcs, minwuid;
  573. struct stat st;
  574. struct flock lck;
  575. char fbuf[16]; /* enlarge when support for keywords is added */
  576. char buf[128], buf1[64], buf2[64];
  577. if (check_ret( sts, aux ))
  578. return;
  579. INIT_SVARS(aux);
  580. ctx[0] = svars->ctx[0];
  581. ctx[1] = svars->ctx[1];
  582. svars->state[t] |= ST_SELECTED;
  583. if (!(svars->state[1-t] & ST_SELECTED))
  584. return;
  585. chan = svars->chan;
  586. if (!strcmp( chan->sync_state ? chan->sync_state : global_conf.sync_state, "*" )) {
  587. if (!ctx[S]->path) {
  588. error( "Error: store '%s' does not support in-box sync state\n", chan->stores[S]->name );
  589. sbail:
  590. svars->ret = SYNC_FAIL;
  591. sync_bail2( svars );
  592. return;
  593. }
  594. nfasprintf( &svars->dname, "%s/." EXE "state", ctx[S]->path );
  595. } else {
  596. csname = clean_strdup( svars->box_name[S] );
  597. if (chan->sync_state)
  598. nfasprintf( &svars->dname, "%s%s", chan->sync_state, csname );
  599. else {
  600. char c = FieldDelimiter;
  601. cmname = clean_strdup( svars->box_name[M] );
  602. nfasprintf( &svars->dname, "%s%c%s%c%s_%c%s%c%s", global_conf.sync_state,
  603. c, chan->stores[M]->name, c, cmname, c, chan->stores[S]->name, c, csname );
  604. free( cmname );
  605. }
  606. free( csname );
  607. if (!(s = strrchr( svars->dname, '/' ))) {
  608. error( "Error: invalid SyncState location '%s'\n", svars->dname );
  609. goto sbail;
  610. }
  611. *s = 0;
  612. if (mkdir( svars->dname, 0700 ) && errno != EEXIST) {
  613. sys_error( "Error: cannot create SyncState directory '%s'", svars->dname );
  614. goto sbail;
  615. }
  616. *s = '/';
  617. }
  618. nfasprintf( &svars->jname, "%s.journal", svars->dname );
  619. nfasprintf( &svars->nname, "%s.new", svars->dname );
  620. nfasprintf( &svars->lname, "%s.lock", svars->dname );
  621. memset( &lck, 0, sizeof(lck) );
  622. #if SEEK_SET != 0
  623. lck.l_whence = SEEK_SET;
  624. #endif
  625. #if F_WRLCK != 0
  626. lck.l_type = F_WRLCK;
  627. #endif
  628. if ((svars->lfd = open( svars->lname, O_WRONLY|O_CREAT, 0666 )) < 0) {
  629. sys_error( "Error: cannot create lock file %s", svars->lname );
  630. svars->ret = SYNC_FAIL;
  631. sync_bail2( svars );
  632. return;
  633. }
  634. if (fcntl( svars->lfd, F_SETLK, &lck )) {
  635. error( "Error: channel :%s:%s-:%s:%s is locked\n",
  636. chan->stores[M]->name, svars->orig_name[M], chan->stores[S]->name, svars->orig_name[S] );
  637. svars->ret = SYNC_FAIL;
  638. sync_bail1( svars );
  639. return;
  640. }
  641. if ((jfp = fopen( svars->dname, "r" ))) {
  642. debug( "reading sync state %s ...\n", svars->dname );
  643. line = 0;
  644. while (fgets( buf, sizeof(buf), jfp )) {
  645. line++;
  646. if (!(t = strlen( buf )) || buf[t - 1] != '\n') {
  647. error( "Error: incomplete sync state header entry at %s:%d\n", svars->dname, line );
  648. jbail:
  649. fclose( jfp );
  650. bail:
  651. svars->ret = SYNC_FAIL;
  652. sync_bail( svars );
  653. return;
  654. }
  655. if (t == 1)
  656. goto gothdr;
  657. if (line == 1 && isdigit( buf[0] )) {
  658. if (sscanf( buf, "%63s %63s", buf1, buf2 ) != 2 ||
  659. sscanf( buf1, "%d:%d", &svars->uidval[M], &svars->maxuid[M] ) < 2 ||
  660. sscanf( buf2, "%d:%d:%d", &svars->uidval[S], &svars->smaxxuid, &svars->maxuid[S] ) < 3) {
  661. error( "Error: invalid sync state header in %s\n", svars->dname );
  662. goto jbail;
  663. }
  664. goto gothdr;
  665. }
  666. if (sscanf( buf, "%63s %d", buf1, &t1 ) != 2) {
  667. error( "Error: malformed sync state header entry at %s:%d\n", svars->dname, line );
  668. goto jbail;
  669. }
  670. if (!strcmp( buf1, "MasterUidValidity" ))
  671. svars->uidval[M] = t1;
  672. else if (!strcmp( buf1, "SlaveUidValidity" ))
  673. svars->uidval[S] = t1;
  674. else if (!strcmp( buf1, "MaxPulledUid" ))
  675. svars->maxuid[M] = t1;
  676. else if (!strcmp( buf1, "MaxPushedUid" ))
  677. svars->maxuid[S] = t1;
  678. else if (!strcmp( buf1, "MaxExpiredSlaveUid" ))
  679. svars->smaxxuid = t1;
  680. else {
  681. error( "Error: unrecognized sync state header entry at %s:%d\n", svars->dname, line );
  682. goto jbail;
  683. }
  684. }
  685. error( "Error: unterminated sync state header in %s\n", svars->dname );
  686. goto jbail;
  687. gothdr:
  688. while (fgets( buf, sizeof(buf), jfp )) {
  689. line++;
  690. if (!(t = strlen( buf )) || buf[t - 1] != '\n') {
  691. error( "Error: incomplete sync state entry at %s:%d\n", svars->dname, line );
  692. goto jbail;
  693. }
  694. fbuf[0] = 0;
  695. if (sscanf( buf, "%d %d %15s", &t1, &t2, fbuf ) < 2) {
  696. error( "Error: invalid sync state entry at %s:%d\n", svars->dname, line );
  697. goto jbail;
  698. }
  699. srec = nfmalloc( sizeof(*srec) );
  700. srec->uid[M] = t1;
  701. srec->uid[S] = t2;
  702. s = fbuf;
  703. if (*s == 'X') {
  704. s++;
  705. srec->status = S_EXPIRE | S_EXPIRED;
  706. } else
  707. srec->status = 0;
  708. srec->flags = parse_flags( s );
  709. debug( " entry (%d,%d,%u,%s)\n", srec->uid[M], srec->uid[S], srec->flags, srec->status & S_EXPIRED ? "X" : "" );
  710. srec->msg[M] = srec->msg[S] = 0;
  711. srec->tuid[0] = 0;
  712. srec->next = 0;
  713. *svars->srecadd = srec;
  714. svars->srecadd = &srec->next;
  715. svars->nsrecs++;
  716. }
  717. fclose( jfp );
  718. } else {
  719. if (errno != ENOENT) {
  720. sys_error( "Error: cannot read sync state %s", svars->dname );
  721. goto bail;
  722. }
  723. }
  724. svars->newmaxuid[M] = svars->maxuid[M];
  725. svars->newmaxuid[S] = svars->maxuid[S];
  726. svars->mmaxxuid = INT_MAX;
  727. line = 0;
  728. if ((jfp = fopen( svars->jname, "r" ))) {
  729. if (!stat( svars->nname, &st ) && fgets( buf, sizeof(buf), jfp )) {
  730. debug( "recovering journal ...\n" );
  731. if (!(t = strlen( buf )) || buf[t - 1] != '\n') {
  732. error( "Error: incomplete journal header in %s\n", svars->jname );
  733. goto jbail;
  734. }
  735. if (!equals( buf, t, JOURNAL_VERSION "\n", strlen(JOURNAL_VERSION) + 1 )) {
  736. error( "Error: incompatible journal version "
  737. "(got %.*s, expected " JOURNAL_VERSION ")\n", t - 1, buf );
  738. goto jbail;
  739. }
  740. srec = 0;
  741. line = 1;
  742. while (fgets( buf, sizeof(buf), jfp )) {
  743. line++;
  744. if (!(t = strlen( buf )) || buf[t - 1] != '\n') {
  745. error( "Error: incomplete journal entry at %s:%d\n", svars->jname, line );
  746. goto jbail;
  747. }
  748. if (buf[0] == '#' ?
  749. (t3 = 0, (sscanf( buf + 2, "%d %d %n", &t1, &t2, &t3 ) < 2) || !t3 || (t - t3 != TUIDL + 3)) :
  750. buf[0] == '(' || buf[0] == ')' || buf[0] == '{' || buf[0] == '}' || buf[0] == '!' ?
  751. (sscanf( buf + 2, "%d", &t1 ) != 1) :
  752. buf[0] == '+' || buf[0] == '&' || buf[0] == '-' || buf[0] == '|' || buf[0] == '/' || buf[0] == '\\' ?
  753. (sscanf( buf + 2, "%d %d", &t1, &t2 ) != 2) :
  754. (sscanf( buf + 2, "%d %d %d", &t1, &t2, &t3 ) != 3))
  755. {
  756. error( "Error: malformed journal entry at %s:%d\n", svars->jname, line );
  757. goto jbail;
  758. }
  759. if (buf[0] == '(')
  760. svars->maxuid[M] = t1;
  761. else if (buf[0] == ')')
  762. svars->maxuid[S] = t1;
  763. else if (buf[0] == '{')
  764. svars->newuid[M] = t1;
  765. else if (buf[0] == '}')
  766. svars->newuid[S] = t1;
  767. else if (buf[0] == '!')
  768. svars->smaxxuid = t1;
  769. else if (buf[0] == '|') {
  770. svars->uidval[M] = t1;
  771. svars->uidval[S] = t2;
  772. } else if (buf[0] == '+') {
  773. srec = nfmalloc( sizeof(*srec) );
  774. srec->uid[M] = t1;
  775. srec->uid[S] = t2;
  776. if (svars->newmaxuid[M] < t1)
  777. svars->newmaxuid[M] = t1;
  778. if (svars->newmaxuid[S] < t2)
  779. svars->newmaxuid[S] = t2;
  780. debug( " new entry(%d,%d)\n", t1, t2 );
  781. srec->msg[M] = srec->msg[S] = 0;
  782. srec->status = 0;
  783. srec->flags = 0;
  784. srec->tuid[0] = 0;
  785. srec->next = 0;
  786. *svars->srecadd = srec;
  787. svars->srecadd = &srec->next;
  788. svars->nsrecs++;
  789. } else {
  790. for (nsrec = srec; srec; srec = srec->next)
  791. if (srec->uid[M] == t1 && srec->uid[S] == t2)
  792. goto syncfnd;
  793. for (srec = svars->srecs; srec != nsrec; srec = srec->next)
  794. if (srec->uid[M] == t1 && srec->uid[S] == t2)
  795. goto syncfnd;
  796. error( "Error: journal entry at %s:%d refers to non-existing sync state entry\n", svars->jname, line );
  797. goto jbail;
  798. syncfnd:
  799. debugn( " entry(%d,%d,%u) ", srec->uid[M], srec->uid[S], srec->flags );
  800. switch (buf[0]) {
  801. case '-':
  802. debug( "killed\n" );
  803. if (srec->msg[M])
  804. srec->msg[M]->srec = 0;
  805. srec->status = S_DEAD;
  806. break;
  807. case '#':
  808. debug( "TUID now %." stringify(TUIDL) "s\n", buf + t3 + 2 );
  809. memcpy( srec->tuid, buf + t3 + 2, TUIDL );
  810. break;
  811. case '&':
  812. debug( "TUID %." stringify(TUIDL) "s lost\n", srec->tuid );
  813. srec->flags = 0;
  814. srec->tuid[0] = 0;
  815. break;
  816. case '<':
  817. debug( "master now %d\n", t3 );
  818. srec->uid[M] = t3;
  819. srec->tuid[0] = 0;
  820. break;
  821. case '>':
  822. debug( "slave now %d\n", t3 );
  823. srec->uid[S] = t3;
  824. srec->tuid[0] = 0;
  825. break;
  826. case '*':
  827. debug( "flags now %d\n", t3 );
  828. srec->flags = t3;
  829. break;
  830. case '~':
  831. debug( "expire now %d\n", t3 );
  832. if (t3)
  833. srec->status |= S_EXPIRE;
  834. else
  835. srec->status &= ~S_EXPIRE;
  836. break;
  837. case '\\':
  838. t3 = (srec->status & S_EXPIRED);
  839. debug( "expire back to %d\n", t3 / S_EXPIRED );
  840. if (t3)
  841. srec->status |= S_EXPIRE;
  842. else
  843. srec->status &= ~S_EXPIRE;
  844. break;
  845. case '/':
  846. t3 = (srec->status & S_EXPIRE);
  847. debug( "expired now %d\n", t3 / S_EXPIRE );
  848. if (t3) {
  849. if (svars->smaxxuid < srec->uid[S])
  850. svars->smaxxuid = srec->uid[S];
  851. srec->status |= S_EXPIRED;
  852. } else
  853. srec->status &= ~S_EXPIRED;
  854. break;
  855. default:
  856. error( "Error: unrecognized journal entry at %s:%d\n", svars->jname, line );
  857. goto jbail;
  858. }
  859. }
  860. }
  861. }
  862. fclose( jfp );
  863. } else {
  864. if (errno != ENOENT) {
  865. sys_error( "Error: cannot read journal %s", svars->jname );
  866. goto bail;
  867. }
  868. }
  869. t1 = 0;
  870. for (t = 0; t < 2; t++)
  871. if (svars->uidval[t] >= 0 && svars->uidval[t] != ctx[t]->uidvalidity) {
  872. error( "Error: UIDVALIDITY of %s changed (got %d, expected %d)\n",
  873. str_ms[t], ctx[t]->uidvalidity, svars->uidval[t] );
  874. t1++;
  875. }
  876. if (t1)
  877. goto bail;
  878. if (!(svars->nfp = fopen( svars->nname, "w" ))) {
  879. sys_error( "Error: cannot create new sync state %s", svars->nname );
  880. goto bail;
  881. }
  882. if (!(svars->jfp = fopen( svars->jname, "a" ))) {
  883. sys_error( "Error: cannot create journal %s", svars->jname );
  884. fclose( svars->nfp );
  885. goto bail;
  886. }
  887. setlinebuf( svars->jfp );
  888. if (!line)
  889. Fprintf( svars->jfp, JOURNAL_VERSION "\n" );
  890. opts[M] = opts[S] = 0;
  891. for (t = 0; t < 2; t++) {
  892. if (chan->ops[t] & (OP_DELETE|OP_FLAGS)) {
  893. opts[t] |= OPEN_SETFLAGS;
  894. opts[1-t] |= OPEN_OLD;
  895. if (chan->ops[t] & OP_FLAGS)
  896. opts[1-t] |= OPEN_FLAGS;
  897. }
  898. if (chan->ops[t] & (OP_NEW|OP_RENEW)) {
  899. opts[t] |= OPEN_APPEND;
  900. if (chan->ops[t] & OP_RENEW)
  901. opts[1-t] |= OPEN_OLD;
  902. if (chan->ops[t] & OP_NEW)
  903. opts[1-t] |= OPEN_NEW;
  904. if (chan->ops[t] & OP_EXPUNGE)
  905. opts[1-t] |= OPEN_FLAGS;
  906. if (chan->stores[t]->max_size != INT_MAX)
  907. opts[1-t] |= OPEN_SIZE;
  908. }
  909. if (chan->ops[t] & OP_EXPUNGE) {
  910. opts[t] |= OPEN_EXPUNGE;
  911. if (chan->stores[t]->trash) {
  912. if (!chan->stores[t]->trash_only_new)
  913. opts[t] |= OPEN_OLD;
  914. opts[t] |= OPEN_NEW|OPEN_FLAGS;
  915. } else if (chan->stores[1-t]->trash && chan->stores[1-t]->trash_remote_new)
  916. opts[t] |= OPEN_NEW|OPEN_FLAGS;
  917. }
  918. }
  919. if ((chan->ops[S] & (OP_NEW|OP_RENEW|OP_FLAGS)) && chan->max_messages)
  920. opts[S] |= OPEN_OLD|OPEN_NEW|OPEN_FLAGS;
  921. if (line)
  922. for (srec = svars->srecs; srec; srec = srec->next) {
  923. if (srec->status & S_DEAD)
  924. continue;
  925. if (srec->tuid[0]) {
  926. if (srec->uid[M] == -2)
  927. opts[M] |= OPEN_NEW|OPEN_FIND, svars->state[M] |= ST_FIND_OLD;
  928. else if (srec->uid[S] == -2)
  929. opts[S] |= OPEN_NEW|OPEN_FIND, svars->state[S] |= ST_FIND_OLD;
  930. else
  931. assert( !"sync record with stray TUID" );
  932. }
  933. }
  934. svars->drv[M]->prepare_load_box( ctx[M], opts[M] );
  935. svars->drv[S]->prepare_load_box( ctx[S], opts[S] );
  936. mexcs = 0;
  937. nmexcs = rmexcs = 0;
  938. if (svars->ctx[M]->opts & OPEN_OLD) {
  939. if (chan->max_messages) {
  940. /* When messages have been expired on the slave, the master fetch is split into
  941. * two ranges: The bulk fetch which corresponds with the most recent messages, and an
  942. * exception list of messages which would have been expired if they weren't important. */
  943. debug( "preparing master selection - max expired slave uid is %d\n", svars->smaxxuid );
  944. /* First, find out the lower bound for the bulk fetch. */
  945. minwuid = INT_MAX;
  946. for (srec = svars->srecs; srec; srec = srec->next) {
  947. if ((srec->status & S_DEAD) || srec->uid[M] <= 0)
  948. continue;
  949. if (srec->status & S_EXPIRED) {
  950. if (!srec->uid[S]) {
  951. /* The expired message was already gone. */
  952. continue;
  953. }
  954. /* The expired message was not expunged yet, so re-examine it.
  955. * This will happen en masse, so just extend the bulk fetch. */
  956. } else {
  957. if (svars->smaxxuid >= srec->uid[S]) {
  958. /* The non-expired message is in the generally expired range, so don't
  959. * make it contribute to the bulk fetch. */
  960. continue;
  961. }
  962. /* Usual non-expired message. */
  963. }
  964. if (minwuid > srec->uid[M])
  965. minwuid = srec->uid[M];
  966. }
  967. debug( " min non-orphaned master uid is %d\n", minwuid );
  968. /* Next, calculate the exception fetch. */
  969. for (srec = svars->srecs; srec; srec = srec->next) {
  970. if (srec->status & S_DEAD)
  971. continue;
  972. if (srec->uid[M] > 0 && srec->uid[S] > 0 && minwuid > srec->uid[M] &&
  973. (!(svars->ctx[M]->opts & OPEN_NEW) || svars->maxuid[M] >= srec->uid[M])) {
  974. /* The pair is alive, but outside the bulk range. */
  975. if (nmexcs == rmexcs) {
  976. rmexcs = rmexcs * 2 + 100;
  977. mexcs = nfrealloc( mexcs, rmexcs * sizeof(int) );
  978. }
  979. mexcs[nmexcs++] = srec->uid[M];
  980. }
  981. }
  982. debugn( " exception list is:" );
  983. for (t = 0; t < nmexcs; t++)
  984. debugn( " %d", mexcs[t] );
  985. debug( "\n" );
  986. } else {
  987. minwuid = 1;
  988. }
  989. } else {
  990. minwuid = INT_MAX;
  991. }
  992. sync_ref( svars );
  993. load_box( svars, M, minwuid, mexcs, nmexcs );
  994. if (!check_cancel( svars ))
  995. load_box( svars, S, (ctx[S]->opts & OPEN_OLD) ? 1 : INT_MAX, 0, 0 );
  996. sync_deref( svars );
  997. }
  998. static void box_loaded( int sts, void *aux );
  999. static void
  1000. load_box( sync_vars_t *svars, int t, int minwuid, int *mexcs, int nmexcs )
  1001. {
  1002. sync_rec_t *srec;
  1003. int maxwuid;
  1004. if (svars->ctx[t]->opts & OPEN_NEW) {
  1005. if (minwuid > svars->maxuid[t] + 1)
  1006. minwuid = svars->maxuid[t] + 1;
  1007. maxwuid = INT_MAX;
  1008. } else if (svars->ctx[t]->opts & OPEN_OLD) {
  1009. maxwuid = 0;
  1010. for (srec = svars->srecs; srec; srec = srec->next)
  1011. if (!(srec->status & S_DEAD) && srec->uid[t] > maxwuid)
  1012. maxwuid = srec->uid[t];
  1013. } else
  1014. maxwuid = 0;
  1015. info( "Loading %s...\n", str_ms[t] );
  1016. debug( maxwuid == INT_MAX ? "loading %s [%d,inf]\n" : "loading %s [%d,%d]\n", str_ms[t], minwuid, maxwuid );
  1017. svars->drv[t]->load_box( svars->ctx[t], minwuid, maxwuid, svars->newuid[t], mexcs, nmexcs, box_loaded, AUX );
  1018. }
  1019. typedef struct {
  1020. void *aux;
  1021. sync_rec_t *srec;
  1022. int aflags, dflags;
  1023. } flag_vars_t;
  1024. typedef struct {
  1025. int uid;
  1026. sync_rec_t *srec;
  1027. } sync_rec_map_t;
  1028. static void flags_set( int sts, void *aux );
  1029. static void flags_set_p2( sync_vars_t *svars, sync_rec_t *srec, int t );
  1030. static void msgs_flags_set( sync_vars_t *svars, int t );
  1031. static void msg_copied( int sts, int uid, copy_vars_t *vars );
  1032. static void msg_copied_p2( sync_vars_t *svars, sync_rec_t *srec, int t, int uid );
  1033. static void msgs_copied( sync_vars_t *svars, int t );
  1034. static void
  1035. box_loaded( int sts, void *aux )
  1036. {
  1037. DECL_SVARS;
  1038. sync_rec_t *srec;
  1039. sync_rec_map_t *srecmap;
  1040. message_t *tmsg;
  1041. copy_vars_t *cv;
  1042. flag_vars_t *fv;
  1043. int uid, no[2], del[2], alive, todel, t1, t2;
  1044. int sflags, nflags, aflags, dflags, nex;
  1045. uint hashsz, idx;
  1046. char fbuf[16]; /* enlarge when support for keywords is added */
  1047. if (check_ret( sts, aux ))
  1048. return;
  1049. INIT_SVARS(aux);
  1050. svars->state[t] |= ST_LOADED;
  1051. info( "%s: %d messages, %d recent\n", str_ms[t], svars->ctx[t]->count, svars->ctx[t]->recent );
  1052. if (svars->state[t] & ST_FIND_OLD) {
  1053. debug( "matching previously copied messages on %s\n", str_ms[t] );
  1054. match_tuids( svars, t );
  1055. }
  1056. debug( "matching messages on %s against sync records\n", str_ms[t] );
  1057. hashsz = bucketsForSize( svars->nsrecs * 3 );
  1058. srecmap = nfcalloc( hashsz * sizeof(*srecmap) );
  1059. for (srec = svars->srecs; srec; srec = srec->next) {
  1060. if (srec->status & S_DEAD)
  1061. continue;
  1062. uid = srec->uid[t];
  1063. idx = (uint)((uint)uid * 1103515245U) % hashsz;
  1064. while (srecmap[idx].uid)
  1065. if (++idx == hashsz)
  1066. idx = 0;
  1067. srecmap[idx].uid = uid;
  1068. srecmap[idx].srec = srec;
  1069. }
  1070. for (tmsg = svars->ctx[t]->msgs; tmsg; tmsg = tmsg->next) {
  1071. if (tmsg->srec) /* found by TUID */
  1072. continue;
  1073. uid = tmsg->uid;
  1074. if (DFlags & DEBUG) {
  1075. make_flags( tmsg->flags, fbuf );
  1076. printf( svars->ctx[t]->opts & OPEN_SIZE ? " message %5d, %-4s, %6lu: " : " message %5d, %-4s: ", uid, fbuf, tmsg->size );
  1077. }
  1078. idx = (uint)((uint)uid * 1103515245U) % hashsz;
  1079. while (srecmap[idx].uid) {
  1080. if (srecmap[idx].uid == uid) {
  1081. srec = srecmap[idx].srec;
  1082. goto found;
  1083. }
  1084. if (++idx == hashsz)
  1085. idx = 0;
  1086. }
  1087. debug( "new\n" );
  1088. continue;
  1089. found:
  1090. tmsg->srec = srec;
  1091. srec->msg[t] = tmsg;
  1092. debug( "pairs %5d\n", srec->uid[1-t] );
  1093. }
  1094. free( srecmap );
  1095. if (!(svars->state[1-t] & ST_LOADED))
  1096. return;
  1097. if (svars->uidval[M] < 0 || svars->uidval[S] < 0) {
  1098. svars->uidval[M] = svars->ctx[M]->uidvalidity;
  1099. svars->uidval[S] = svars->ctx[S]->uidvalidity;
  1100. Fprintf( svars->jfp, "| %d %d\n", svars->uidval[M], svars->uidval[S] );
  1101. }
  1102. info( "Synchronizing...\n" );
  1103. debug( "synchronizing old entries\n" );
  1104. for (srec = svars->srecs; srec; srec = srec->next) {
  1105. if (srec->status & S_DEAD)
  1106. continue;
  1107. debug( "pair (%d,%d)\n", srec->uid[M], srec->uid[S] );
  1108. no[M] = !srec->msg[M] && (svars->ctx[M]->opts & OPEN_OLD);
  1109. no[S] = !srec->msg[S] && (svars->ctx[S]->opts & OPEN_OLD);
  1110. if (no[M] && no[S]) {
  1111. debug( " vanished\n" );
  1112. /* d.1) d.5) d.6) d.10) d.11) */
  1113. srec->status = S_DEAD;
  1114. Fprintf( svars->jfp, "- %d %d\n", srec->uid[M], srec->uid[S] );
  1115. } else {
  1116. del[M] = no[M] && (srec->uid[M] > 0);
  1117. del[S] = no[S] && (srec->uid[S] > 0);
  1118. for (t = 0; t < 2; t++) {
  1119. srec->aflags[t] = srec->dflags[t] = 0;
  1120. if (srec->msg[t] && (srec->msg[t]->flags & F_DELETED))
  1121. srec->status |= S_DEL(t);
  1122. /* excludes (push) c.3) d.2) d.3) d.4) / (pull) b.3) d.7) d.8) d.9) */
  1123. if (!srec->uid[t]) {
  1124. /* b.1) / c.1) */
  1125. debug( " no more %s\n", str_ms[t] );
  1126. } else if (del[1-t]) {
  1127. /* c.4) d.9) / b.4) d.4) */
  1128. if ((t == M) && (srec->status & (S_EXPIRE|S_EXPIRED))) {
  1129. /* Don't propagate deletion resulting from expiration. */
  1130. debug( " slave expired, orphaning master\n" );
  1131. Fprintf( svars->jfp, "> %d %d 0\n", srec->uid[M], srec->uid[S] );
  1132. srec->uid[S] = 0;
  1133. } else {
  1134. if (srec->msg[t] && (srec->msg[t]->status & M_FLAGS) && srec->msg[t]->flags != srec->flags)
  1135. info( "Info: conflicting changes in (%d,%d)\n", srec->uid[M], srec->uid[S] );
  1136. if (svars->chan->ops[t] & OP_DELETE) {
  1137. debug( " %sing delete\n", str_hl[t] );
  1138. srec->aflags[t] = F_DELETED;
  1139. srec->status |= S_DELETE;
  1140. } else {
  1141. debug( " not %sing delete\n", str_hl[t] );
  1142. }
  1143. }
  1144. } else if (!srec->msg[1-t])
  1145. /* c.1) c.2) d.7) d.8) / b.1) b.2) d.2) d.3) */
  1146. ;
  1147. else if (srec->uid[t] < 0)
  1148. /* b.2) / c.2) */
  1149. ; /* handled as new messages (sort of) */
  1150. else if (!del[t]) {
  1151. /* a) & b.3) / c.3) */
  1152. if (svars->chan->ops[t] & OP_FLAGS) {
  1153. sflags = srec->msg[1-t]->flags;
  1154. if ((t == M) && (srec->status & (S_EXPIRE|S_EXPIRED))) {
  1155. /* Don't propagate deletion resulting from expiration. */
  1156. debug( " slave expiring\n" );
  1157. sflags &= ~F_DELETED;
  1158. }
  1159. srec->aflags[t] = sflags & ~srec->flags;
  1160. srec->dflags[t] = ~sflags & srec->flags;
  1161. if (DFlags & DEBUG) {
  1162. char afbuf[16], dfbuf[16]; /* enlarge when support for keywords is added */
  1163. make_flags( srec->aflags[t], afbuf );
  1164. make_flags( srec->dflags[t], dfbuf );
  1165. debug( " %sing flags: +%s -%s\n", str_hl[t], afbuf, dfbuf );
  1166. }
  1167. } else
  1168. debug( " not %sing flags\n", str_hl[t] );
  1169. } /* else b.4) / c.4) */
  1170. }
  1171. }
  1172. }
  1173. debug( "synchronizing new entries\n" );
  1174. for (t = 0; t < 2; t++) {
  1175. for (tmsg = svars->ctx[1-t]->msgs; tmsg; tmsg = tmsg->next) {
  1176. /* If we have a srec:
  1177. * - message is old (> 0) or expired (0) => ignore
  1178. * - message was skipped (-1) => ReNew
  1179. * - message was attempted, but failed (-2) => New
  1180. * If new have no srec, the message is always New. If messages were previously ignored
  1181. * due to being excessive, they would now appear to be newer than the messages that
  1182. * got actually synced, so make sure to look only at the newest ones. As some messages
  1183. * may be already propagated before an interruption, and maxuid logging is delayed,
  1184. * we need to track the newmaxuid separately. */
  1185. srec = tmsg->srec;
  1186. if (srec ? srec->uid[t] < 0 && (svars->chan->ops[t] & (srec->uid[t] == -1 ? OP_RENEW : OP_NEW))
  1187. : svars->newmaxuid[1-t] < tmsg->uid && (svars->chan->ops[t] & OP_NEW)) {
  1188. debug( "new message %d on %s\n", tmsg->uid, str_ms[1-t] );
  1189. if ((svars->chan->ops[t] & OP_EXPUNGE) && (tmsg->flags & F_DELETED)) {
  1190. debug( " -> not %sing - would be expunged anyway\n", str_hl[t] );
  1191. } else {
  1192. if (srec) {
  1193. debug( " -> pair(%d,%d) exists\n", srec->uid[M], srec->uid[S] );
  1194. } else {
  1195. srec = nfmalloc( sizeof(*srec) );
  1196. srec->next = 0;
  1197. *svars->srecadd = srec;
  1198. svars->srecadd = &srec->next;
  1199. svars->nsrecs++;
  1200. srec->status = 0;
  1201. srec->flags = 0;
  1202. srec->tuid[0] = 0;
  1203. srec->uid[1-t] = tmsg->uid;
  1204. srec->uid[t] = -2;
  1205. srec->msg[1-t] = tmsg;
  1206. srec->msg[t] = 0;
  1207. tmsg->srec = srec;
  1208. if (svars->newmaxuid[1-t] < tmsg->uid)
  1209. svars->newmaxuid[1-t] = tmsg->uid;
  1210. Fprintf( svars->jfp, "+ %d %d\n", srec->uid[M], srec->uid[S] );
  1211. debug( " -> pair(%d,%d) created\n", srec->uid[M], srec->uid[S] );
  1212. }
  1213. if (svars->maxuid[1-t] < tmsg->uid) {
  1214. /* We do this here for simplicity. However, logging must be delayed until
  1215. * all messages were propagated, as skipped messages could otherwise be
  1216. * logged before the propagation of messages with lower UIDs completes. */
  1217. svars->maxuid[1-t] = tmsg->uid;
  1218. }
  1219. if ((tmsg->flags & F_FLAGGED) || tmsg->size <= svars->chan->stores[t]->max_size) {
  1220. if (tmsg->flags) {
  1221. srec->flags = tmsg->flags;
  1222. Fprintf( svars->jfp, "* %d %d %u\n", srec->uid[M], srec->uid[S], srec->flags );
  1223. debug( " -> updated flags to %u\n", tmsg->flags );
  1224. }
  1225. for (t1 = 0; t1 < TUIDL; t1++) {
  1226. t2 = arc4_getbyte() & 0x3f;
  1227. srec->tuid[t1] = t2 < 26 ? t2 + 'A' : t2 < 52 ? t2 + 'a' - 26 : t2 < 62 ? t2 + '0' - 52 : t2 == 62 ? '+' : '/';
  1228. }
  1229. Fprintf( svars->jfp, "# %d %d %." stringify(TUIDL) "s\n", srec->uid[M], srec->uid[S], srec->tuid );
  1230. debug( " -> %sing message, TUID %." stringify(TUIDL) "s\n", str_hl[t], srec->tuid );
  1231. } else {
  1232. if (srec->uid[t] == -1) {
  1233. debug( " -> not %sing - still too big\n", str_hl[t] );
  1234. } else {
  1235. debug( " -> not %sing - too big\n", str_hl[t] );
  1236. msg_copied_p2( svars, srec, t, -1 );
  1237. }
  1238. }
  1239. }
  1240. }
  1241. }
  1242. }
  1243. if ((svars->chan->ops[S] & (OP_NEW|OP_RENEW|OP_FLAGS)) && svars->chan->max_messages) {
  1244. /* Note: When this branch is entered, we have loaded all slave messages. */
  1245. /* Expire excess messages. Important (flagged, unread, or unpropagated) messages
  1246. * older than the first not expired message are not counted towards the total. */
  1247. debug( "preparing message expiration\n" );
  1248. alive = 0;
  1249. for (tmsg = svars->ctx[S]->msgs; tmsg; tmsg = tmsg->next) {
  1250. if (tmsg->status & M_DEAD)
  1251. continue;
  1252. if ((srec = tmsg->srec) && srec->uid[M] > 0 &&
  1253. ((tmsg->flags | srec->aflags[S]) & ~srec->dflags[S] & F_DELETED) &&
  1254. !(srec->status & (S_EXPIRE|S_EXPIRED))) {
  1255. /* Message was not propagated yet, or is deleted. */
  1256. } else {
  1257. alive++;
  1258. }
  1259. }
  1260. for (tmsg = svars->ctx[M]->msgs; tmsg; tmsg = tmsg->next) {
  1261. if ((srec = tmsg->srec) && srec->tuid[0] && !(tmsg->flags & F_DELETED))
  1262. alive++;
  1263. }
  1264. todel = alive - svars->chan->max_messages;
  1265. debug( "%d alive messages, %d excess - expiring\n", alive, todel );
  1266. alive = 0;
  1267. for (tmsg = svars->ctx[S]->msgs; tmsg; tmsg = tmsg->next) {
  1268. if (tmsg->status & M_DEAD)
  1269. continue;
  1270. if (!(srec = tmsg->srec) || srec->uid[M] <= 0) {
  1271. /* We did not push the message, so it must be kept. */
  1272. debug( " message %d unpropagated\n", tmsg->uid );
  1273. todel--;
  1274. } else {
  1275. nflags = (tmsg->flags | srec->aflags[S]) & ~srec->dflags[S];
  1276. if (!(nflags & F_DELETED) || (srec->status & (S_EXPIRE|S_EXPIRED))) {
  1277. /* The message is not deleted, or is already (being) expired. */
  1278. if ((nflags & F_FLAGGED) || !((nflags & F_SEEN) || ((void)(todel > 0 && alive++), svars->chan->expire_unread > 0))) {
  1279. /* Important messages are always kept. */
  1280. debug( " old pair(%d,%d) important\n", srec->uid[M], srec->uid[S] );
  1281. todel--;
  1282. } else if (todel > 0 ||
  1283. ((srec->status & (S_EXPIRE|S_EXPIRED)) == (S_EXPIRE|S_EXPIRED)) ||
  1284. ((srec->status & (S_EXPIRE|S_EXPIRED)) && (tmsg->flags & F_DELETED))) {
  1285. /* The message is excess or was already (being) expired. */
  1286. srec->status |= S_NEXPIRE;
  1287. debug( " old pair(%d,%d) expired\n", srec->uid[M], srec->uid[S] );
  1288. todel--;
  1289. }
  1290. }
  1291. }
  1292. }
  1293. for (tmsg = svars->ctx[M]->msgs; tmsg; tmsg = tmsg->next) {
  1294. if ((srec = tmsg->srec) && srec->tuid[0]) {
  1295. nflags = tmsg->flags;
  1296. if (!(nflags & F_DELETED)) {
  1297. if ((nflags & F_FLAGGED) || !((nflags & F_SEEN) || ((void)(todel > 0 && alive++), svars->chan->expire_unread > 0))) {
  1298. /* Important messages are always fetched. */
  1299. debug( " new pair(%d,%d) important\n", srec->uid[M], srec->uid[S] );
  1300. todel--;
  1301. } else if (todel > 0) {
  1302. /* The message is excess. */
  1303. srec->status |= S_NEXPIRE;
  1304. debug( " new pair(%d,%d) expired\n", srec->uid[M], srec->uid[S] );
  1305. svars->mmaxxuid = srec->uid[M];
  1306. todel--;
  1307. }
  1308. }
  1309. }
  1310. }
  1311. debug( "%d excess messages remain\n", todel );
  1312. if (svars->chan->expire_unread < 0 && (uint)alive * 2 > svars->chan->max_messages) {
  1313. error( "%s: %d unread messages in excess of MaxMessages (%d).\n"
  1314. "Please set ExpireUnread to decide outcome. Skipping mailbox.\n",
  1315. svars->orig_name[S], alive, svars->chan->max_messages );
  1316. svars->ret |= SYNC_FAIL;
  1317. cancel_sync( svars );
  1318. return;
  1319. }
  1320. for (srec = svars->srecs; srec; srec = srec->next) {
  1321. if (srec->status & S_DEAD)
  1322. continue;
  1323. if (!srec->tuid[0]) {
  1324. if (!srec->msg[S])
  1325. continue;
  1326. nex = (srec->status / S_NEXPIRE) & 1;
  1327. if (nex != ((srec->status / S_EXPIRED) & 1)) {
  1328. /* The record needs a state change ... */
  1329. if (nex != ((srec->status / S_EXPIRE) & 1)) {
  1330. /* ... and we need to start a transaction. */
  1331. Fprintf( svars->jfp, "~ %d %d %d\n", srec->uid[M], srec->uid[S], nex );
  1332. debug( " pair(%d,%d): %d (pre)\n", srec->uid[M], srec->uid[S], nex );
  1333. srec->status = (srec->status & ~S_EXPIRE) | (nex * S_EXPIRE);
  1334. } else {
  1335. /* ... but the "right" transaction is already pending. */
  1336. debug( " pair(%d,%d): %d (pending)\n", srec->uid[M], srec->uid[S], nex );
  1337. }
  1338. } else {
  1339. /* Note: the "wrong" transaction may be pending here,
  1340. * e.g.: S_NEXPIRE = 0, S_EXPIRE = 1, S_EXPIRED = 0. */
  1341. }
  1342. } else {
  1343. if (srec->status & S_NEXPIRE) {
  1344. Fprintf( svars->jfp, "- %d %d\n", srec->uid[M], srec->uid[S] );
  1345. debug( " pair(%d,%d): 1 (abort)\n", srec->uid[M], srec->uid[S] );
  1346. srec->msg[M]->srec = 0;
  1347. srec->status = S_DEAD;
  1348. }
  1349. }
  1350. }
  1351. }
  1352. sync_ref( svars );
  1353. debug( "synchronizing flags\n" );
  1354. for (srec = svars->srecs; srec; srec = srec->next) {
  1355. if ((srec->status & S_DEAD) || srec->uid[M] <= 0 || srec->uid[S] <= 0)
  1356. continue;
  1357. for (t = 0; t < 2; t++) {
  1358. aflags = srec->aflags[t];
  1359. dflags = srec->dflags[t];
  1360. if (srec->status & S_DELETE) {
  1361. if (!aflags) {
  1362. /* This deletion propagation goes the other way round. */
  1363. continue;
  1364. }
  1365. } else {
  1366. /* The trigger is an expiration transaction being ongoing ... */
  1367. if ((t == S) && ((mvBit(srec->status, S_EXPIRE, S_EXPIRED) ^ srec->status) & S_EXPIRED)) {
  1368. /* ... but the actual action derives from the wanted state. */
  1369. if (srec->status & S_NEXPIRE)
  1370. aflags |= F_DELETED;
  1371. else
  1372. dflags |= F_DELETED;
  1373. }
  1374. }
  1375. if ((svars->chan->ops[t] & OP_EXPUNGE) && (((srec->msg[t] ? srec->msg[t]->flags : 0) | aflags) & ~dflags & F_DELETED) &&
  1376. (!svars->ctx[t]->conf->trash || svars->ctx[t]->conf->trash_only_new))
  1377. {
  1378. /* If the message is going to be expunged, don't propagate anything but the deletion. */
  1379. srec->aflags[t] &= F_DELETED;
  1380. aflags &= F_DELETED;
  1381. srec->dflags[t] = dflags = 0;
  1382. }
  1383. if (srec->msg[t] && (srec->msg[t]->status & M_FLAGS)) {
  1384. /* If we know the target message's state, optimize away non-changes. */
  1385. aflags &= ~srec->msg[t]->flags;
  1386. dflags &= srec->msg[t]->flags;
  1387. }
  1388. if (aflags | dflags) {
  1389. svars->flags_total[t]++;
  1390. stats( svars );
  1391. fv = nfmalloc( sizeof(*fv) );
  1392. fv->aux = AUX;
  1393. fv->srec = srec;
  1394. fv->aflags = aflags;
  1395. fv->dflags = dflags;
  1396. svars->drv[t]->set_msg_flags( svars->ctx[t], srec->msg[t], srec->uid[t], aflags, dflags, flags_set, fv );
  1397. if (check_cancel( svars ))
  1398. goto out;
  1399. } else
  1400. flags_set_p2( svars, srec, t );
  1401. }
  1402. }
  1403. for (t = 0; t < 2; t++) {
  1404. svars->drv[t]->commit_cmds( svars->ctx[t] );
  1405. svars->state[t] |= ST_SENT_FLAGS;
  1406. msgs_flags_set( svars, t );
  1407. if (check_cancel( svars ))
  1408. goto out;
  1409. }
  1410. debug( "propagating new messages\n" );
  1411. if (UseFSync)
  1412. fdatasync( fileno( svars->jfp ) );
  1413. for (t = 0; t < 2; t++) {
  1414. svars->newuid[t] = svars->ctx[t]->uidnext;
  1415. Fprintf( svars->jfp, "%c %d\n", "{}"[t], svars->newuid[t] );
  1416. for (tmsg = svars->ctx[1-t]->msgs; tmsg; tmsg = tmsg->next) {
  1417. if ((srec = tmsg->srec) && srec->tuid[0]) {
  1418. svars->new_total[t]++;
  1419. stats( svars );
  1420. cv = nfmalloc( sizeof(*cv) );
  1421. cv->cb = msg_copied;
  1422. cv->aux = AUX;
  1423. cv->srec = srec;
  1424. cv->msg = tmsg;
  1425. copy_msg( cv );
  1426. if (check_cancel( svars ))
  1427. goto out;
  1428. }
  1429. }
  1430. svars->state[t] |= ST_SENT_NEW;
  1431. msgs_copied( svars, t );
  1432. if (check_cancel( svars ))
  1433. goto out;
  1434. }
  1435. out:
  1436. sync_deref( svars );
  1437. }
  1438. static void
  1439. msg_copied( int sts, int uid, copy_vars_t *vars )
  1440. {
  1441. SVARS_CHECK_CANCEL_RET;
  1442. switch (sts) {
  1443. case SYNC_OK:
  1444. if (uid < 0)
  1445. svars->state[t] |= ST_FIND_NEW;
  1446. msg_copied_p2( svars, vars->srec, t, uid );
  1447. break;
  1448. case SYNC_NOGOOD:
  1449. debug( " -> killing (%d,%d)\n", vars->srec->uid[M], vars->srec->uid[S] );
  1450. vars->srec->status = S_DEAD;
  1451. Fprintf( svars->jfp, "- %d %d\n", vars->srec->uid[M], vars->srec->uid[S] );
  1452. break;
  1453. default:
  1454. cancel_sync( svars );
  1455. free( vars );
  1456. return;
  1457. }
  1458. free( vars );
  1459. svars->new_done[t]++;
  1460. stats( svars );
  1461. msgs_copied( svars, t );
  1462. }
  1463. static void
  1464. msg_copied_p2( sync_vars_t *svars, sync_rec_t *srec, int t, int uid )
  1465. {
  1466. /* Possible previous UIDs:
  1467. * - -2 when the entry is new
  1468. * - -1 when re-newing an entry
  1469. * Possible new UIDs:
  1470. * - a real UID when storing a message to a UIDPLUS mailbox
  1471. * - -2 when storing a message to a dumb mailbox
  1472. * - -1 when not actually storing a message */
  1473. if (srec->uid[t] != uid) {
  1474. debug( " -> new UID %d on %s\n", uid, str_ms[t] );
  1475. Fprintf( svars->jfp, "%c %d %d %d\n", "<>"[t], srec->uid[M], srec->uid[S], uid );
  1476. srec->uid[t] = uid;
  1477. srec->tuid[0] = 0;
  1478. }
  1479. if (t == S && svars->mmaxxuid < srec->uid[M]) {
  1480. /* If we have so many new messages that some of them are instantly expired,
  1481. * but some are still propagated because they are important, we need to
  1482. * ensure explicitly that the bulk fetch limit is upped. */
  1483. svars->mmaxxuid = INT_MAX;
  1484. if (svars->smaxxuid < srec->uid[S] - 1) {
  1485. svars->smaxxuid = srec->uid[S] - 1;
  1486. Fprintf( svars->jfp, "! %d\n", svars->smaxxuid );
  1487. }
  1488. }
  1489. }
  1490. static void msgs_found_new( int sts, void *aux );
  1491. static void msgs_new_done( sync_vars_t *svars, int t );
  1492. static void sync_close( sync_vars_t *svars, int t );
  1493. static void
  1494. msgs_copied( sync_vars_t *svars, int t )
  1495. {
  1496. if (!(svars->state[t] & ST_SENT_NEW) || svars->new_done[t] < svars->new_total[t])
  1497. return;
  1498. sync_ref( svars );
  1499. Fprintf( svars->jfp, "%c %d\n", ")("[t], svars->maxuid[1-t] );
  1500. sync_close( svars, 1-t );
  1501. if (check_cancel( svars ))
  1502. goto out;
  1503. if (svars->state[t] & ST_FIND_NEW) {
  1504. debug( "finding just copied messages on %s\n", str_ms[t] );
  1505. svars->drv[t]->find_new_msgs( svars->ctx[t], svars->newuid[t], msgs_found_new, AUX );
  1506. } else {
  1507. msgs_new_done( svars, t );
  1508. }
  1509. out:
  1510. sync_deref( svars );
  1511. }
  1512. static void
  1513. msgs_found_new( int sts, void *aux )
  1514. {
  1515. SVARS_CHECK_RET;
  1516. switch (sts) {
  1517. case DRV_OK:
  1518. debug( "matching just copied messages on %s\n", str_ms[t] );
  1519. break;
  1520. default:
  1521. warn( "Warning: cannot find newly stored messages on %s.\n", str_ms[t] );
  1522. break;
  1523. }
  1524. match_tuids( svars, t );
  1525. msgs_new_done( svars, t );
  1526. }
  1527. static void
  1528. msgs_new_done( sync_vars_t *svars, int t )
  1529. {
  1530. svars->state[t] |= ST_FOUND_NEW;
  1531. sync_close( svars, t );
  1532. }
  1533. static void
  1534. flags_set( int sts, void *aux )
  1535. {
  1536. SVARS_CHECK_RET_VARS(flag_vars_t);
  1537. switch (sts) {
  1538. case DRV_OK:
  1539. if (vars->aflags & F_DELETED)
  1540. vars->srec->status |= S_DEL(t);
  1541. else if (vars->dflags & F_DELETED)
  1542. vars->srec->status &= ~S_DEL(t);
  1543. flags_set_p2( svars, vars->srec, t );
  1544. break;
  1545. }
  1546. free( vars );
  1547. svars->flags_done[t]++;
  1548. stats( svars );
  1549. msgs_flags_set( svars, t );
  1550. }
  1551. static void
  1552. flags_set_p2( sync_vars_t *svars, sync_rec_t *srec, int t )
  1553. {
  1554. if (srec->status & S_DELETE) {
  1555. debug( " pair(%d,%d): resetting %s UID\n", srec->uid[M], srec->uid[S], str_ms[1-t] );
  1556. Fprintf( svars->jfp, "%c %d %d 0\n", "><"[t], srec->uid[M], srec->uid[S] );
  1557. srec->uid[1-t] = 0;
  1558. } else {
  1559. int nflags = (srec->flags | srec->aflags[t]) & ~srec->dflags[t];
  1560. if (srec->flags != nflags) {
  1561. debug( " pair(%d,%d): updating flags (%u -> %u; %sed)\n", srec->uid[M], srec->uid[S], srec->flags, nflags, str_hl[t] );
  1562. srec->flags = nflags;
  1563. Fprintf( svars->jfp, "* %d %d %u\n", srec->uid[M], srec->uid[S], nflags );
  1564. }
  1565. if (t == S) {
  1566. int nex = (srec->status / S_NEXPIRE) & 1;
  1567. if (nex != ((srec->status / S_EXPIRED) & 1)) {
  1568. if (nex && (svars->smaxxuid < srec->uid[S]))
  1569. svars->smaxxuid = srec->uid[S];
  1570. Fprintf( svars->jfp, "/ %d %d\n", srec->uid[M], srec->uid[S] );
  1571. debug( " pair(%d,%d): expired %d (commit)\n", srec->uid[M], srec->uid[S], nex );
  1572. srec->status = (srec->status & ~S_EXPIRED) | (nex * S_EXPIRED);
  1573. } else if (nex != ((srec->status / S_EXPIRE) & 1)) {
  1574. Fprintf( svars->jfp, "\\ %d %d\n", srec->uid[M], srec->uid[S] );
  1575. debug( " pair(%d,%d): expire %d (cancel)\n", srec->uid[M], srec->uid[S], nex );
  1576. srec->status = (srec->status & ~S_EXPIRE) | (nex * S_EXPIRE);
  1577. }
  1578. }
  1579. }
  1580. }
  1581. static void msg_trashed( int sts, void *aux );
  1582. static void msg_rtrashed( int sts, int uid, copy_vars_t *vars );
  1583. static void
  1584. msgs_flags_set( sync_vars_t *svars, int t )
  1585. {
  1586. message_t *tmsg;
  1587. copy_vars_t *cv;
  1588. if (!(svars->state[t] & ST_SENT_FLAGS) || svars->flags_done[t] < svars->flags_total[t])
  1589. return;
  1590. sync_ref( svars );
  1591. if ((svars->chan->ops[t] & OP_EXPUNGE) &&
  1592. (svars->ctx[t]->conf->trash || (svars->ctx[1-t]->conf->trash && svars->ctx[1-t]->conf->trash_remote_new))) {
  1593. debug( "trashing in %s\n", str_ms[t] );
  1594. for (tmsg = svars->ctx[t]->msgs; tmsg; tmsg = tmsg->next)
  1595. if ((tmsg->flags & F_DELETED) && (t == M || !tmsg->srec || !(tmsg->srec->status & (S_EXPIRE|S_EXPIRED)))) {
  1596. if (svars->ctx[t]->conf->trash) {
  1597. if (!svars->ctx[t]->conf->trash_only_new || !tmsg->srec || tmsg->srec->uid[1-t] < 0) {
  1598. debug( "%s: trashing message %d\n", str_ms[t], tmsg->uid );
  1599. svars->trash_total[t]++;
  1600. stats( svars );
  1601. svars->drv[t]->trash_msg( svars->ctx[t], tmsg, msg_trashed, AUX );
  1602. if (check_cancel( svars ))
  1603. goto out;
  1604. } else
  1605. debug( "%s: not trashing message %d - not new\n", str_ms[t], tmsg->uid );
  1606. } else {
  1607. if (!tmsg->srec || tmsg->srec->uid[1-t] < 0) {
  1608. if (tmsg->size <= svars->ctx[1-t]->conf->max_size) {
  1609. debug( "%s: remote trashing message %d\n", str_ms[t], tmsg->uid );
  1610. svars->trash_total[t]++;
  1611. stats( svars );
  1612. cv = nfmalloc( sizeof(*cv) );
  1613. cv->cb = msg_rtrashed;
  1614. cv->aux = INV_AUX;
  1615. cv->srec = 0;
  1616. cv->msg = tmsg;
  1617. copy_msg( cv );
  1618. if (check_cancel( svars ))
  1619. goto out;
  1620. } else
  1621. debug( "%s: not remote trashing message %d - too big\n", str_ms[t], tmsg->uid );
  1622. } else
  1623. debug( "%s: not remote trashing message %d - not new\n", str_ms[t], tmsg->uid );
  1624. }
  1625. }
  1626. }
  1627. svars->state[t] |= ST_SENT_TRASH;
  1628. sync_close( svars, t );
  1629. out:
  1630. sync_deref( svars );
  1631. }
  1632. static void
  1633. msg_trashed( int sts, void *aux )
  1634. {
  1635. DECL_SVARS;
  1636. if (sts == DRV_MSG_BAD)
  1637. sts = DRV_BOX_BAD;
  1638. if (check_ret( sts, aux ))
  1639. return;
  1640. INIT_SVARS(aux);
  1641. svars->trash_done[t]++;
  1642. stats( svars );
  1643. sync_close( svars, t );
  1644. }
  1645. static void
  1646. msg_rtrashed( int sts, int uid ATTR_UNUSED, copy_vars_t *vars )
  1647. {
  1648. SVARS_CHECK_CANCEL_RET;
  1649. switch (sts) {
  1650. case SYNC_OK:
  1651. case SYNC_NOGOOD: /* the message is gone or heavily busted */
  1652. break;
  1653. default:
  1654. cancel_sync( svars );
  1655. free( vars );
  1656. return;
  1657. }
  1658. free( vars );
  1659. t ^= 1;
  1660. svars->trash_done[t]++;
  1661. stats( svars );
  1662. sync_close( svars, t );
  1663. }
  1664. static void box_closed( int sts, void *aux );
  1665. static void box_closed_p2( sync_vars_t *svars, int t );
  1666. static void
  1667. sync_close( sync_vars_t *svars, int t )
  1668. {
  1669. if ((~svars->state[t] & (ST_FOUND_NEW|ST_SENT_TRASH)) || svars->trash_done[t] < svars->trash_total[t] ||
  1670. !(svars->state[1-t] & ST_SENT_NEW) || svars->new_done[1-t] < svars->new_total[1-t])
  1671. return;
  1672. if (svars->state[t] & ST_CLOSING)
  1673. return;
  1674. svars->state[t] |= ST_CLOSING;
  1675. if ((svars->chan->ops[t] & OP_EXPUNGE) /*&& !(svars->state[t] & ST_TRASH_BAD)*/) {
  1676. debug( "expunging %s\n", str_ms[t] );
  1677. svars->drv[t]->close_box( svars->ctx[t], box_closed, AUX );
  1678. } else {
  1679. box_closed_p2( svars, t );
  1680. }
  1681. }
  1682. static void
  1683. box_closed( int sts, void *aux )
  1684. {
  1685. SVARS_CHECK_RET;
  1686. svars->state[t] |= ST_DID_EXPUNGE;
  1687. box_closed_p2( svars, t );
  1688. }
  1689. static void
  1690. box_closed_p2( sync_vars_t *svars, int t )
  1691. {
  1692. sync_rec_t *srec;
  1693. int minwuid;
  1694. char fbuf[16]; /* enlarge when support for keywords is added */
  1695. svars->state[t] |= ST_CLOSED;
  1696. if (!(svars->state[1-t] & ST_CLOSED))
  1697. return;
  1698. if (((svars->state[M] | svars->state[S]) & ST_DID_EXPUNGE) || svars->chan->max_messages) {
  1699. debug( "purging obsolete entries\n" );
  1700. minwuid = INT_MAX;
  1701. if (svars->chan->max_messages) {
  1702. debug( " max expired slave uid is %d\n", svars->smaxxuid );
  1703. for (srec = svars->srecs; srec; srec = srec->next) {
  1704. if (srec->status & S_DEAD)
  1705. continue;
  1706. if (!((srec->uid[S] <= 0 || ((srec->status & S_DEL(S)) && (svars->state[S] & ST_DID_EXPUNGE))) &&
  1707. (srec->uid[M] <= 0 || ((srec->status & S_DEL(M)) && (svars->state[M] & ST_DID_EXPUNGE)) || (srec->status & S_EXPIRED))) &&
  1708. svars->smaxxuid < srec->uid[S] && minwuid > srec->uid[M])
  1709. minwuid = srec->uid[M];
  1710. }
  1711. debug( " min non-orphaned master uid is %d\n", minwuid );
  1712. }
  1713. for (srec = svars->srecs; srec; srec = srec->next) {
  1714. if (srec->status & S_DEAD)
  1715. continue;
  1716. if (srec->uid[S] <= 0 || ((srec->status & S_DEL(S)) && (svars->state[S] & ST_DID_EXPUNGE))) {
  1717. if (srec->uid[M] <= 0 || ((srec->status & S_DEL(M)) && (svars->state[M] & ST_DID_EXPUNGE)) ||
  1718. ((srec->status & S_EXPIRED) && svars->maxuid[M] >= srec->uid[M] && minwuid > srec->uid[M])) {
  1719. debug( " -> killing (%d,%d)\n", srec->uid[M], srec->uid[S] );
  1720. srec->status = S_DEAD;
  1721. Fprintf( svars->jfp, "- %d %d\n", srec->uid[M], srec->uid[S] );
  1722. } else if (srec->uid[S] > 0) {
  1723. debug( " -> orphaning (%d,[%d])\n", srec->uid[M], srec->uid[S] );
  1724. Fprintf( svars->jfp, "> %d %d 0\n", srec->uid[M], srec->uid[S] );
  1725. srec->uid[S] = 0;
  1726. }
  1727. } else if (srec->uid[M] > 0 && ((srec->status & S_DEL(M)) && (svars->state[M] & ST_DID_EXPUNGE))) {
  1728. debug( " -> orphaning ([%d],%d)\n", srec->uid[M], srec->uid[S] );
  1729. Fprintf( svars->jfp, "< %d %d 0\n", srec->uid[M], srec->uid[S] );
  1730. srec->uid[M] = 0;
  1731. }
  1732. }
  1733. }
  1734. Fprintf( svars->nfp,
  1735. "MasterUidValidity %d\nSlaveUidValidity %d\nMaxPulledUid %d\nMaxPushedUid %d\n",
  1736. svars->uidval[M], svars->uidval[S], svars->maxuid[M], svars->maxuid[S] );
  1737. if (svars->smaxxuid)
  1738. Fprintf( svars->nfp, "MaxExpiredSlaveUid %d\n", svars->smaxxuid );
  1739. Fprintf( svars->nfp, "\n" );
  1740. for (srec = svars->srecs; srec; srec = srec->next) {
  1741. if (srec->status & S_DEAD)
  1742. continue;
  1743. make_flags( srec->flags, fbuf );
  1744. Fprintf( svars->nfp, "%d %d %s%s\n", srec->uid[M], srec->uid[S],
  1745. srec->status & S_EXPIRED ? "X" : "", fbuf );
  1746. }
  1747. Fclose( svars->nfp, 1 );
  1748. Fclose( svars->jfp, 0 );
  1749. if (!(DFlags & KEEPJOURNAL)) {
  1750. /* order is important! */
  1751. if (rename( svars->nname, svars->dname ))
  1752. warn( "Warning: cannot commit sync state %s\n", svars->dname );
  1753. else if (unlink( svars->jname ))
  1754. warn( "Warning: cannot delete journal %s\n", svars->jname );
  1755. }
  1756. sync_bail( svars );
  1757. }
  1758. static void
  1759. sync_bail( sync_vars_t *svars )
  1760. {
  1761. sync_rec_t *srec, *nsrec;
  1762. for (srec = svars->srecs; srec; srec = nsrec) {
  1763. nsrec = srec->next;
  1764. free( srec );
  1765. }
  1766. unlink( svars->lname );
  1767. sync_bail1( svars );
  1768. }
  1769. static void
  1770. sync_bail1( sync_vars_t *svars )
  1771. {
  1772. close( svars->lfd );
  1773. sync_bail2( svars );
  1774. }
  1775. static void
  1776. sync_bail2( sync_vars_t *svars )
  1777. {
  1778. free( svars->lname );
  1779. free( svars->nname );
  1780. free( svars->jname );
  1781. free( svars->dname );
  1782. flushn();
  1783. sync_bail3( svars );
  1784. }
  1785. static void
  1786. sync_bail3( sync_vars_t *svars )
  1787. {
  1788. free( svars->box_name[M] );
  1789. free( svars->box_name[S] );
  1790. sync_deref( svars );
  1791. }
  1792. static void
  1793. sync_deref( sync_vars_t *svars )
  1794. {
  1795. if (!--svars->ref_count) {
  1796. void (*cb)( int sts, void *aux ) = svars->cb;
  1797. void *aux = svars->aux;
  1798. int ret = svars->ret;
  1799. free( svars );
  1800. cb( ret, aux );
  1801. }
  1802. }