sync.c 56 KB

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