sync.c 56 KB

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