sync.c 49 KB

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