Hex Artifact Content
Not logged in

Artifact 04b08e89d93300592abad44257337daf2901c9fc:

File src/xfer.c part of check-in [4c072728b4] - Add a quick hack that disables delta compression on xfer. This gives people a chance to synchronize the previous fix. We will eventually remove the hack. by drh on 2007-08-09 03:47:28.

0000: 2f 2a 0a 2a 2a 20 43 6f 70 79 72 69 67 68 74 20  /*.** Copyright 
0010: 28 63 29 20 32 30 30 37 20 44 2e 20 52 69 63 68  (c) 2007 D. Rich
0020: 61 72 64 20 48 69 70 70 0a 2a 2a 0a 2a 2a 20 54  ard Hipp.**.** T
0030: 68 69 73 20 70 72 6f 67 72 61 6d 20 69 73 20 66  his program is f
0040: 72 65 65 20 73 6f 66 74 77 61 72 65 3b 20 79 6f  ree software; yo
0050: 75 20 63 61 6e 20 72 65 64 69 73 74 72 69 62 75  u can redistribu
0060: 74 65 20 69 74 20 61 6e 64 2f 6f 72 0a 2a 2a 20  te it and/or.** 
0070: 6d 6f 64 69 66 79 20 69 74 20 75 6e 64 65 72 20  modify it under 
0080: 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65  the terms of the
0090: 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 50 75 62   GNU General Pub
00a0: 6c 69 63 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 76  lic.** License v
00b0: 65 72 73 69 6f 6e 20 32 20 61 73 20 70 75 62 6c  ersion 2 as publ
00c0: 69 73 68 65 64 20 62 79 20 74 68 65 20 46 72 65  ished by the Fre
00d0: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64  e Software Found
00e0: 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ation..**.** Thi
00f0: 73 20 70 72 6f 67 72 61 6d 20 69 73 20 64 69 73  s program is dis
0100: 74 72 69 62 75 74 65 64 20 69 6e 20 74 68 65 20  tributed in the 
0110: 68 6f 70 65 20 74 68 61 74 20 69 74 20 77 69 6c  hope that it wil
0120: 6c 20 62 65 20 75 73 65 66 75 6c 2c 0a 2a 2a 20  l be useful,.** 
0130: 62 75 74 20 57 49 54 48 4f 55 54 20 41 4e 59 20  but WITHOUT ANY 
0140: 57 41 52 52 41 4e 54 59 3b 20 77 69 74 68 6f 75  WARRANTY; withou
0150: 74 20 65 76 65 6e 20 74 68 65 20 69 6d 70 6c 69  t even the impli
0160: 65 64 20 77 61 72 72 61 6e 74 79 20 6f 66 0a 2a  ed warranty of.*
0170: 2a 20 4d 45 52 43 48 41 4e 54 41 42 49 4c 49 54  * MERCHANTABILIT
0180: 59 20 6f 72 20 46 49 54 4e 45 53 53 20 46 4f 52  Y or FITNESS FOR
0190: 20 41 20 50 41 52 54 49 43 55 4c 41 52 20 50 55   A PARTICULAR PU
01a0: 52 50 4f 53 45 2e 20 20 53 65 65 20 74 68 65 20  RPOSE.  See the 
01b0: 47 4e 55 0a 2a 2a 20 47 65 6e 65 72 61 6c 20 50  GNU.** General P
01c0: 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 20 66 6f  ublic License fo
01d0: 72 20 6d 6f 72 65 20 64 65 74 61 69 6c 73 2e 0a  r more details..
01e0: 2a 2a 20 0a 2a 2a 20 59 6f 75 20 73 68 6f 75 6c  ** .** You shoul
01f0: 64 20 68 61 76 65 20 72 65 63 65 69 76 65 64 20  d have received 
0200: 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 47 4e  a copy of the GN
0210: 55 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69 63  U General Public
0220: 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 61 6c 6f 6e  .** License alon
0230: 67 20 77 69 74 68 20 74 68 69 73 20 6c 69 62 72  g with this libr
0240: 61 72 79 3b 20 69 66 20 6e 6f 74 2c 20 77 72 69  ary; if not, wri
0250: 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 46 72 65  te to the.** Fre
0260: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64  e Software Found
0270: 61 74 69 6f 6e 2c 20 49 6e 63 2e 2c 20 35 39 20  ation, Inc., 59 
0280: 54 65 6d 70 6c 65 20 50 6c 61 63 65 20 2d 20 53  Temple Place - S
0290: 75 69 74 65 20 33 33 30 2c 0a 2a 2a 20 42 6f 73  uite 330,.** Bos
02a0: 74 6f 6e 2c 20 4d 41 20 20 30 32 31 31 31 2d 31  ton, MA  02111-1
02b0: 33 30 37 2c 20 55 53 41 2e 0a 2a 2a 0a 2a 2a 20  307, USA..**.** 
02c0: 41 75 74 68 6f 72 20 63 6f 6e 74 61 63 74 20 69  Author contact i
02d0: 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a 20 20  nformation:.**  
02e0: 20 64 72 68 40 68 77 61 63 69 2e 63 6f 6d 0a 2a   drh@hwaci.com.*
02f0: 2a 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 68  *   http://www.h
0300: 77 61 63 69 2e 63 6f 6d 2f 64 72 68 2f 0a 2a 2a  waci.com/drh/.**
0310: 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
0320: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0330: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0340: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0350: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0360: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
0370: 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74   contains code t
0380: 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  o implement the 
0390: 66 69 6c 65 20 74 72 61 6e 73 66 65 72 20 70 72  file transfer pr
03a0: 6f 74 6f 63 6f 6c 2e 0a 2a 2f 0a 23 69 6e 63 6c  otocol..*/.#incl
03b0: 75 64 65 20 22 63 6f 6e 66 69 67 2e 68 22 0a 23  ude "config.h".#
03c0: 69 6e 63 6c 75 64 65 20 22 78 66 65 72 2e 68 22  include "xfer.h"
03d0: 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 6c  ../*.** Try to l
03e0: 6f 63 61 74 65 20 61 20 72 65 63 6f 72 64 20 74  ocate a record t
03f0: 68 61 74 20 69 73 20 73 69 6d 69 6c 61 72 20 74  hat is similar t
0400: 6f 20 72 69 64 20 61 6e 64 20 69 73 20 61 20 6c  o rid and is a l
0410: 69 6b 65 6c 79 0a 2a 2a 20 63 61 6e 64 69 64 61  ikely.** candida
0420: 74 65 20 66 6f 72 20 64 65 6c 74 61 20 61 67 61  te for delta aga
0430: 69 6e 73 74 20 72 69 64 2e 20 20 54 68 65 20 73  inst rid.  The s
0440: 69 6d 69 6c 61 72 20 72 65 63 6f 72 64 20 6d 75  imilar record mu
0450: 73 74 20 62 65 0a 2a 2a 20 72 65 66 65 72 65 6e  st be.** referen
0460: 63 65 64 20 69 6e 20 74 68 65 20 6f 6e 72 65 6d  ced in the onrem
0470: 6f 74 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  ote table..**.**
0480: 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 74 65   Return the inte
0490: 67 65 72 20 72 65 63 6f 72 64 20 49 44 20 6f 66  ger record ID of
04a0: 20 74 68 65 20 73 69 6d 69 6c 61 72 20 72 65 63   the similar rec
04b0: 6f 72 64 2e 20 20 4f 72 20 72 65 74 75 72 6e 0a  ord.  Or return.
04c0: 2a 2a 20 30 20 69 66 20 6e 6f 6e 65 20 69 73 20  ** 0 if none is 
04d0: 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  found..*/.static
04e0: 20 69 6e 74 20 73 69 6d 69 6c 61 72 5f 72 65 63   int similar_rec
04f0: 6f 72 64 28 69 6e 74 20 72 69 64 2c 20 69 6e 74  ord(int rid, int
0500: 20 74 72 61 63 65 46 6c 61 67 29 7b 0a 20 20 69   traceFlag){.  i
0510: 6e 74 20 69 6e 43 6e 74 2c 20 6f 75 74 43 6e 74  nt inCnt, outCnt
0520: 3b 0a 20 20 53 74 6d 74 20 71 3b 0a 20 20 69 6e  ;.  Stmt q;.  in
0530: 74 20 71 75 65 75 65 5b 31 30 30 5d 3b 0a 0a 72  t queue[100];..r
0540: 65 74 75 72 6e 20 30 3b 0a 0a 20 20 64 62 5f 70  eturn 0;..  db_p
0550: 72 65 70 61 72 65 28 26 71 2c 0a 20 20 20 20 20  repare(&q,.     
0560: 20 22 53 45 4c 45 43 54 20 73 72 63 69 64 2c 20   "SELECT srcid, 
0570: 45 58 49 53 54 53 28 53 45 4c 45 43 54 20 31 20  EXISTS(SELECT 1 
0580: 46 52 4f 4d 20 6f 6e 72 65 6d 6f 74 65 20 57 48  FROM onremote WH
0590: 45 52 45 20 72 69 64 3d 73 72 63 69 64 29 22 0a  ERE rid=srcid)".
05a0: 20 20 20 20 20 20 22 20 20 46 52 4f 4d 20 64 65        "  FROM de
05b0: 6c 74 61 22 0a 20 20 20 20 20 20 22 20 57 48 45  lta".      " WHE
05c0: 52 45 20 72 69 64 3d 3a 78 22 0a 20 20 20 20 20  RE rid=:x".     
05d0: 20 22 20 55 4e 49 4f 4e 20 41 4c 4c 20 22 0a 20   " UNION ALL ". 
05e0: 20 20 20 20 20 22 53 45 4c 45 43 54 20 72 69 64       "SELECT rid
05f0: 2c 20 45 58 49 53 54 53 28 53 45 4c 45 43 54 20  , EXISTS(SELECT 
0600: 31 20 46 52 4f 4d 20 6f 6e 72 65 6d 6f 74 65 20  1 FROM onremote 
0610: 57 48 45 52 45 20 72 69 64 3d 64 65 6c 74 61 2e  WHERE rid=delta.
0620: 72 69 64 29 22 0a 20 20 20 20 20 20 22 20 20 46  rid)".      "  F
0630: 52 4f 4d 20 64 65 6c 74 61 22 0a 20 20 20 20 20  ROM delta".     
0640: 20 22 20 57 48 45 52 45 20 73 72 63 69 64 3d 3a   " WHERE srcid=:
0650: 78 22 0a 20 20 29 3b 0a 20 20 71 75 65 75 65 5b  x".  );.  queue[
0660: 30 5d 20 3d 20 72 69 64 3b 0a 20 20 69 6e 43 6e  0] = rid;.  inCn
0670: 74 20 3d 20 31 3b 0a 20 20 6f 75 74 43 6e 74 20  t = 1;.  outCnt 
0680: 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 6f 75  = 0;.  while( ou
0690: 74 43 6e 74 3c 69 6e 43 6e 74 20 29 7b 0a 20 20  tCnt<inCnt ){.  
06a0: 20 20 69 6e 74 20 78 69 64 20 3d 20 71 75 65 75    int xid = queu
06b0: 65 5b 6f 75 74 43 6e 74 25 36 34 5d 3b 0a 20 20  e[outCnt%64];.  
06c0: 20 20 6f 75 74 43 6e 74 2b 2b 3b 0a 20 20 20 20    outCnt++;.    
06d0: 64 62 5f 62 69 6e 64 5f 69 6e 74 28 26 71 2c 20  db_bind_int(&q, 
06e0: 22 3a 78 22 2c 20 78 69 64 29 3b 0a 20 20 20 20  ":x", xid);.    
06f0: 69 66 28 20 74 72 61 63 65 46 6c 61 67 20 29 20  if( traceFlag ) 
0700: 70 72 69 6e 74 66 28 22 78 69 64 3d 25 64 5c 6e  printf("xid=%d\n
0710: 22 2c 20 78 69 64 29 3b 0a 20 20 20 20 77 68 69  ", xid);.    whi
0720: 6c 65 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d  le( db_step(&q)=
0730: 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20  =SQLITE_ROW ){. 
0740: 20 20 20 20 20 69 6e 74 20 6e 69 64 20 3d 20 64       int nid = d
0750: 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c  b_column_int(&q,
0760: 20 30 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 68   0);.      int h
0770: 69 74 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69  it = db_column_i
0780: 6e 74 28 26 71 2c 20 31 29 3b 0a 20 20 20 20 20  nt(&q, 1);.     
0790: 20 69 66 28 20 74 72 61 63 65 46 6c 61 67 20 29   if( traceFlag )
07a0: 20 70 72 69 6e 74 66 28 22 6e 69 64 3d 25 64 20   printf("nid=%d 
07b0: 68 69 74 3d 25 64 5c 6e 22 2c 20 6e 69 64 2c 20  hit=%d\n", nid, 
07c0: 68 69 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  hit);.      if( 
07d0: 68 69 74 20 20 29 7b 0a 20 20 20 20 20 20 20 20  hit  ){.        
07e0: 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b  db_finalize(&q);
07f0: 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
0800: 6e 69 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  nid;.      }.   
0810: 20 20 20 69 66 28 20 69 6e 43 6e 74 3c 73 69 7a     if( inCnt<siz
0820: 65 6f 66 28 71 75 65 75 65 29 2f 73 69 7a 65 6f  eof(queue)/sizeo
0830: 66 28 71 75 65 75 65 5b 30 5d 29 20 29 7b 0a 20  f(queue[0]) ){. 
0840: 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20         int i;.  
0850: 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
0860: 3c 69 6e 43 6e 74 20 26 26 20 71 75 65 75 65 5b  <inCnt && queue[
0870: 69 5d 21 3d 6e 69 64 3b 20 69 2b 2b 29 7b 7d 0a  i]!=nid; i++){}.
0880: 20 20 20 20 20 20 20 20 69 66 28 20 69 3e 3d 69          if( i>=i
0890: 6e 43 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20  nCnt ){.        
08a0: 20 20 71 75 65 75 65 5b 69 6e 43 6e 74 2b 2b 5d    queue[inCnt++]
08b0: 20 3d 20 6e 69 64 3b 0a 20 20 20 20 20 20 20 20   = nid;.        
08c0: 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
08d0: 20 20 20 20 64 62 5f 72 65 73 65 74 28 26 71 29      db_reset(&q)
08e0: 3b 0a 20 20 7d 0a 20 20 64 62 5f 66 69 6e 61 6c  ;.  }.  db_final
08f0: 69 7a 65 28 26 71 29 3b 0a 20 20 72 65 74 75 72  ize(&q);.  retur
0900: 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f  n 0;.}../*.** CO
0910: 4d 4d 41 4e 44 3a 20 74 65 73 74 2d 73 69 6d 69  MMAND: test-simi
0920: 6c 61 72 2d 72 65 63 6f 72 64 0a 2a 2f 0a 76 6f  lar-record.*/.vo
0930: 69 64 20 74 65 73 74 5f 73 69 6d 69 6c 61 72 5f  id test_similar_
0940: 72 65 63 6f 72 64 28 76 6f 69 64 29 7b 0a 20 20  record(void){.  
0950: 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 67 2e 61  int i;.  if( g.a
0960: 72 67 63 3c 34 20 29 7b 0a 20 20 20 20 75 73 61  rgc<4 ){.    usa
0970: 67 65 28 22 53 52 43 20 4f 4e 52 45 4d 4f 54 45  ge("SRC ONREMOTE
0980: 2e 2e 2e 22 29 3b 0a 20 20 7d 0a 20 20 64 62 5f  ...");.  }.  db_
0990: 6d 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74  must_be_within_t
09a0: 72 65 65 28 29 3b 0a 20 20 64 62 5f 6d 75 6c 74  ree();.  db_mult
09b0: 69 5f 65 78 65 63 28 0a 20 20 20 20 22 43 52 45  i_exec(.    "CRE
09c0: 41 54 45 20 54 45 4d 50 20 54 41 42 4c 45 20 6f  ATE TEMP TABLE o
09d0: 6e 72 65 6d 6f 74 65 28 72 69 64 20 49 4e 54 45  nremote(rid INTE
09e0: 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 29  GER PRIMARY KEY)
09f0: 3b 22 0a 20 20 29 3b 0a 20 20 66 6f 72 28 69 3d  ;".  );.  for(i=
0a00: 33 3b 20 69 3c 67 2e 61 72 67 63 3b 20 69 2b 2b  3; i<g.argc; i++
0a10: 29 7b 0a 20 20 20 20 69 6e 74 20 72 69 64 20 3d  ){.    int rid =
0a20: 20 6e 61 6d 65 5f 74 6f 5f 72 69 64 28 67 2e 61   name_to_rid(g.a
0a30: 72 67 76 5b 69 5d 29 3b 0a 20 20 20 20 70 72 69  rgv[i]);.    pri
0a40: 6e 74 66 28 22 25 73 20 2d 3e 20 25 64 5c 6e 22  ntf("%s -> %d\n"
0a50: 2c 20 67 2e 61 72 67 76 5b 69 5d 2c 20 72 69 64  , g.argv[i], rid
0a60: 29 3b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f  );.    db_multi_
0a70: 65 78 65 63 28 22 49 4e 53 45 52 54 20 49 4e 54  exec("INSERT INT
0a80: 4f 20 6f 6e 72 65 6d 6f 74 65 20 56 41 4c 55 45  O onremote VALUE
0a90: 53 28 25 64 29 22 2c 20 72 69 64 29 3b 0a 20 20  S(%d)", rid);.  
0aa0: 7d 0a 20 20 70 72 69 6e 74 66 28 22 73 69 6d 69  }.  printf("simi
0ab0: 6c 61 72 3a 20 25 64 5c 6e 22 2c 20 73 69 6d 69  lar: %d\n", simi
0ac0: 6c 61 72 5f 72 65 63 6f 72 64 28 6e 61 6d 65 5f  lar_record(name_
0ad0: 74 6f 5f 72 69 64 28 67 2e 61 72 67 76 5b 32 5d  to_rid(g.argv[2]
0ae0: 29 2c 20 31 29 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  ), 1));.}.../*.*
0af0: 2a 20 54 68 65 20 61 54 6f 6b 65 6e 5b 30 2e 2e  * The aToken[0..
0b00: 6e 54 6f 6b 65 6e 2d 31 5d 20 62 6c 6f 62 20 61  nToken-1] blob a
0b10: 72 72 61 79 20 69 73 20 61 20 70 61 72 73 65 20  rray is a parse 
0b20: 6f 66 20 61 20 22 66 69 6c 65 22 20 6c 69 6e 65  of a "file" line
0b30: 20 0a 2a 2a 20 6d 65 73 73 61 67 65 2e 20 20 54   .** message.  T
0b40: 68 69 73 20 72 6f 75 74 69 6e 65 20 66 69 6e 69  his routine fini
0b50: 73 68 65 73 20 70 61 72 73 69 6e 67 20 74 68 61  shes parsing tha
0b60: 74 20 6d 65 73 73 61 67 65 20 61 6e 64 20 64 6f  t message and do
0b70: 65 73 0a 2a 2a 20 61 20 72 65 63 6f 72 64 20 69  es.** a record i
0b80: 6e 73 65 72 74 20 6f 66 20 74 68 65 20 66 69 6c  nsert of the fil
0b90: 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c  e..**.** The fil
0ba0: 65 20 6c 69 6e 65 20 69 73 20 69 6e 20 6f 6e 65  e line is in one
0bb0: 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
0bc0: 67 20 74 77 6f 20 66 6f 72 6d 73 3a 0a 2a 2a 0a  g two forms:.**.
0bd0: 2a 2a 20 20 20 20 20 20 66 69 6c 65 20 55 55 49  **      file UUI
0be0: 44 20 53 49 5a 45 20 5c 6e 20 43 4f 4e 54 45 4e  D SIZE \n CONTEN
0bf0: 54 0a 2a 2a 20 20 20 20 20 20 66 69 6c 65 20 55  T.**      file U
0c00: 55 49 44 20 44 45 4c 54 41 53 52 43 20 53 49 5a  UID DELTASRC SIZ
0c10: 45 20 5c 6e 20 43 4f 4e 54 45 4e 54 0a 2a 2a 0a  E \n CONTENT.**.
0c20: 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 69  ** The content i
0c30: 73 20 53 49 5a 45 20 62 79 74 65 73 20 69 6d 6d  s SIZE bytes imm
0c40: 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69  ediately followi
0c50: 6e 67 20 74 68 65 20 6e 65 77 6c 69 6e 65 2e 0a  ng the newline..
0c60: 2a 2a 20 49 66 20 44 45 4c 54 41 53 52 43 20 65  ** If DELTASRC e
0c70: 78 69 73 74 73 2c 20 74 68 65 6e 20 74 68 65 20  xists, then the 
0c80: 43 4f 4e 54 45 4e 54 20 69 73 20 61 20 64 65 6c  CONTENT is a del
0c90: 74 61 20 61 67 61 69 6e 73 74 20 74 68 65 0a 2a  ta against the.*
0ca0: 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 44 45 4c  * content of DEL
0cb0: 54 41 53 52 43 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  TASRC..**.** If 
0cc0: 61 6e 79 20 65 72 72 6f 72 20 6f 63 63 75 72 73  any error occurs
0cd0: 2c 20 77 72 69 74 65 20 61 20 6d 65 73 73 61 67  , write a messag
0ce0: 65 20 69 6e 74 6f 20 70 45 72 72 20 77 68 69 63  e into pErr whic
0cf0: 68 20 68 61 73 20 61 6c 72 65 61 64 79 0a 2a 2a  h has already.**
0d00: 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20   be initialized 
0d10: 74 6f 20 61 6e 20 65 6d 70 74 79 20 73 74 72 69  to an empty stri
0d20: 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ng..*/.static vo
0d30: 69 64 20 78 66 65 72 5f 61 63 63 65 70 74 5f 66  id xfer_accept_f
0d40: 69 6c 65 28 42 6c 6f 62 20 2a 70 49 6e 2c 20 42  ile(Blob *pIn, B
0d50: 6c 6f 62 20 2a 61 54 6f 6b 65 6e 2c 20 69 6e 74  lob *aToken, int
0d60: 20 6e 54 6f 6b 65 6e 2c 20 42 6c 6f 62 20 2a 70   nToken, Blob *p
0d70: 45 72 72 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20  Err){.  int n;. 
0d80: 20 69 6e 74 20 72 69 64 3b 0a 20 20 42 6c 6f 62   int rid;.  Blob
0d90: 20 63 6f 6e 74 65 6e 74 2c 20 68 61 73 68 3b 0a   content, hash;.
0da0: 20 20 0a 20 20 69 66 28 20 6e 54 6f 6b 65 6e 3c    .  if( nToken<
0db0: 33 20 7c 7c 20 6e 54 6f 6b 65 6e 3e 34 20 7c 7c  3 || nToken>4 ||
0dc0: 20 21 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26   !blob_is_uuid(&
0dd0: 61 54 6f 6b 65 6e 5b 31 5d 29 0a 20 20 20 20 20  aToken[1]).     
0de0: 20 20 7c 7c 20 21 62 6c 6f 62 5f 69 73 5f 69 6e    || !blob_is_in
0df0: 74 28 26 61 54 6f 6b 65 6e 5b 6e 54 6f 6b 65 6e  t(&aToken[nToken
0e00: 2d 31 5d 2c 20 26 6e 29 20 7c 7c 20 6e 3c 3d 30  -1], &n) || n<=0
0e10: 0a 20 20 20 20 20 20 20 7c 7c 20 28 6e 54 6f 6b  .       || (nTok
0e20: 65 6e 3d 3d 34 20 26 26 20 21 62 6c 6f 62 5f 69  en==4 && !blob_i
0e30: 73 5f 75 75 69 64 28 26 61 54 6f 6b 65 6e 5b 32  s_uuid(&aToken[2
0e40: 5d 29 29 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f  ])) ){.    blob_
0e50: 61 70 70 65 6e 64 66 28 70 45 72 72 2c 20 22 6d  appendf(pErr, "m
0e60: 61 6c 66 6f 72 6d 65 64 20 66 69 6c 65 20 6c 69  alformed file li
0e70: 6e 65 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ne");.    return
0e80: 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f 7a 65 72  ;.  }.  blob_zer
0e90: 6f 28 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 62  o(&content);.  b
0ea0: 6c 6f 62 5f 7a 65 72 6f 28 26 68 61 73 68 29 3b  lob_zero(&hash);
0eb0: 0a 20 20 62 6c 6f 62 5f 65 78 74 72 61 63 74 28  .  blob_extract(
0ec0: 70 49 6e 2c 20 6e 2c 20 26 63 6f 6e 74 65 6e 74  pIn, n, &content
0ed0: 29 3b 0a 20 20 69 66 28 20 6e 54 6f 6b 65 6e 3d  );.  if( nToken=
0ee0: 3d 34 20 29 7b 0a 20 20 20 20 42 6c 6f 62 20 73  =4 ){.    Blob s
0ef0: 72 63 3b 0a 20 20 20 20 69 6e 74 20 73 72 63 69  rc;.    int srci
0f00: 64 20 3d 20 64 62 5f 69 6e 74 28 30 2c 20 22 53  d = db_int(0, "S
0f10: 45 4c 45 43 54 20 72 69 64 20 46 52 4f 4d 20 62  ELECT rid FROM b
0f20: 6c 6f 62 20 57 48 45 52 45 20 75 75 69 64 3d 25  lob WHERE uuid=%
0f30: 42 22 2c 20 26 61 54 6f 6b 65 6e 5b 32 5d 29 3b  B", &aToken[2]);
0f40: 0a 20 20 20 20 69 66 28 20 73 72 63 69 64 3d 3d  .    if( srcid==
0f50: 30 20 29 7b 0a 20 20 20 20 20 20 62 6c 6f 62 5f  0 ){.      blob_
0f60: 61 70 70 65 6e 64 66 28 70 45 72 72 2c 20 22 75  appendf(pErr, "u
0f70: 6e 6b 6e 6f 77 6e 20 64 65 6c 74 61 20 73 6f 75  nknown delta sou
0f80: 72 63 65 3a 20 25 62 22 2c 20 26 61 54 6f 6b 65  rce: %b", &aToke
0f90: 6e 5b 32 5d 29 3b 0a 20 20 20 20 20 20 72 65 74  n[2]);.      ret
0fa0: 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  urn;.    }.    c
0fb0: 6f 6e 74 65 6e 74 5f 67 65 74 28 73 72 63 69 64  ontent_get(srcid
0fc0: 2c 20 26 73 72 63 29 3b 0a 20 20 20 20 62 6c 6f  , &src);.    blo
0fd0: 62 5f 64 65 6c 74 61 5f 61 70 70 6c 79 28 26 73  b_delta_apply(&s
0fe0: 72 63 2c 20 26 63 6f 6e 74 65 6e 74 2c 20 26 63  rc, &content, &c
0ff0: 6f 6e 74 65 6e 74 29 3b 0a 20 20 20 20 62 6c 6f  ontent);.    blo
1000: 62 5f 72 65 73 65 74 28 26 73 72 63 29 3b 0a 20  b_reset(&src);. 
1010: 20 7d 0a 20 20 73 68 61 31 73 75 6d 5f 62 6c 6f   }.  sha1sum_blo
1020: 62 28 26 63 6f 6e 74 65 6e 74 2c 20 26 68 61 73  b(&content, &has
1030: 68 29 3b 0a 20 20 69 66 28 20 21 62 6c 6f 62 5f  h);.  if( !blob_
1040: 65 71 5f 73 74 72 28 26 61 54 6f 6b 65 6e 5b 31  eq_str(&aToken[1
1050: 5d 2c 20 62 6c 6f 62 5f 73 74 72 28 26 68 61 73  ], blob_str(&has
1060: 68 29 2c 20 2d 31 29 20 29 7b 0a 20 20 20 20 62  h), -1) ){.    b
1070: 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70 45 72 72  lob_appendf(pErr
1080: 2c 20 22 63 6f 6e 74 65 6e 74 20 64 6f 65 73 20  , "content does 
1090: 6e 6f 74 20 6d 61 74 63 68 20 73 68 61 31 20 68  not match sha1 h
10a0: 61 73 68 22 29 3b 0a 20 20 7d 0a 20 20 62 6c 6f  ash");.  }.  blo
10b0: 62 5f 72 65 73 65 74 28 26 68 61 73 68 29 3b 0a  b_reset(&hash);.
10c0: 20 20 72 69 64 20 3d 20 63 6f 6e 74 65 6e 74 5f    rid = content_
10d0: 70 75 74 28 26 63 6f 6e 74 65 6e 74 2c 20 30 29  put(&content, 0)
10e0: 3b 0a 20 20 6d 61 6e 69 66 65 73 74 5f 63 72 6f  ;.  manifest_cro
10f0: 73 73 6c 69 6e 6b 28 72 69 64 2c 20 26 63 6f 6e  sslink(rid, &con
1100: 74 65 6e 74 29 3b 0a 20 20 69 66 28 20 72 69 64  tent);.  if( rid
1110: 3d 3d 30 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f  ==0 ){.    blob_
1120: 61 70 70 65 6e 64 66 28 70 45 72 72 2c 20 22 25  appendf(pErr, "%
1130: 73 22 2c 20 67 2e 7a 45 72 72 4d 73 67 29 3b 0a  s", g.zErrMsg);.
1140: 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 6e    }.}../*.** Sen
1150: 64 20 74 68 65 20 66 69 6c 65 20 69 64 65 6e 74  d the file ident
1160: 69 66 69 65 64 20 62 79 20 72 69 64 2e 0a 2a 2a  ified by rid..**
1170: 0a 2a 2a 20 49 66 20 70 4f 75 74 20 69 73 20 6e  .** If pOut is n
1180: 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 70  ot NULL, then ap
1190: 70 65 6e 64 20 74 68 65 20 74 65 78 74 20 6f 66  pend the text of
11a0: 20 74 68 65 20 73 65 6e 64 20 6d 65 73 73 61 67   the send messag
11b0: 65 0a 2a 2a 20 74 6f 20 70 4f 75 74 2e 20 20 4f  e.** to pOut.  O
11c0: 74 68 65 72 77 69 73 65 2c 20 61 70 70 65 6e 64  therwise, append
11d0: 20 74 68 65 20 74 65 78 74 20 74 6f 20 74 68 65   the text to the
11e0: 20 43 47 49 20 6f 75 74 70 75 74 2e 0a 2a 2f 0a   CGI output..*/.
11f0: 73 74 61 74 69 63 20 69 6e 74 20 73 65 6e 64 5f  static int send_
1200: 66 69 6c 65 28 69 6e 74 20 72 69 64 2c 20 42 6c  file(int rid, Bl
1210: 6f 62 20 2a 70 4f 75 74 29 7b 0a 20 20 42 6c 6f  ob *pOut){.  Blo
1220: 62 20 63 6f 6e 74 65 6e 74 2c 20 75 75 69 64 3b  b content, uuid;
1230: 0a 20 20 69 6e 74 20 73 69 7a 65 3b 0a 20 20 69  .  int size;.  i
1240: 6e 74 20 73 72 63 69 64 3b 0a 0a 0a 20 20 62 6c  nt srcid;...  bl
1250: 6f 62 5f 7a 65 72 6f 28 26 75 75 69 64 29 3b 0a  ob_zero(&uuid);.
1260: 20 20 64 62 5f 62 6c 6f 62 28 26 75 75 69 64 2c    db_blob(&uuid,
1270: 20 22 53 45 4c 45 43 54 20 75 75 69 64 20 46 52   "SELECT uuid FR
1280: 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 72 69  OM blob WHERE ri
1290: 64 3d 25 64 20 41 4e 44 20 73 69 7a 65 3e 3d 30  d=%d AND size>=0
12a0: 22 2c 20 72 69 64 29 3b 0a 20 20 69 66 28 20 62  ", rid);.  if( b
12b0: 6c 6f 62 5f 73 69 7a 65 28 26 75 75 69 64 29 3d  lob_size(&uuid)=
12c0: 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
12d0: 20 30 3b 0a 20 20 7d 0a 20 20 63 6f 6e 74 65 6e   0;.  }.  conten
12e0: 74 5f 67 65 74 28 72 69 64 2c 20 26 63 6f 6e 74  t_get(rid, &cont
12f0: 65 6e 74 29 3b 0a 0a 20 20 73 72 63 69 64 20 3d  ent);..  srcid =
1300: 20 73 69 6d 69 6c 61 72 5f 72 65 63 6f 72 64 28   similar_record(
1310: 72 69 64 2c 20 30 29 3b 0a 20 20 69 66 28 20 73  rid, 0);.  if( s
1320: 72 63 69 64 20 29 7b 0a 20 20 20 20 42 6c 6f 62  rcid ){.    Blob
1330: 20 73 72 63 2c 20 64 65 6c 74 61 3b 0a 20 20 20   src, delta;.   
1340: 20 42 6c 6f 62 20 73 72 63 75 75 69 64 3b 0a 20   Blob srcuuid;. 
1350: 20 20 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28 73     content_get(s
1360: 72 63 69 64 2c 20 26 73 72 63 29 3b 0a 20 20 20  rcid, &src);.   
1370: 20 62 6c 6f 62 5f 64 65 6c 74 61 5f 63 72 65 61   blob_delta_crea
1380: 74 65 28 26 73 72 63 2c 20 26 63 6f 6e 74 65 6e  te(&src, &conten
1390: 74 2c 20 26 64 65 6c 74 61 29 3b 0a 20 20 20 20  t, &delta);.    
13a0: 62 6c 6f 62 5f 72 65 73 65 74 28 26 73 72 63 29  blob_reset(&src)
13b0: 3b 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74  ;.    blob_reset
13c0: 28 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 20 20  (&content);.    
13d0: 62 6c 6f 62 5f 7a 65 72 6f 28 26 73 72 63 75 75  blob_zero(&srcuu
13e0: 69 64 29 3b 0a 20 20 20 20 64 62 5f 62 6c 6f 62  id);.    db_blob
13f0: 28 26 73 72 63 75 75 69 64 2c 20 22 53 45 4c 45  (&srcuuid, "SELE
1400: 43 54 20 75 75 69 64 20 46 52 4f 4d 20 62 6c 6f  CT uuid FROM blo
1410: 62 20 57 48 45 52 45 20 72 69 64 3d 25 64 22 2c  b WHERE rid=%d",
1420: 20 73 72 63 69 64 29 3b 0a 20 20 20 20 73 69 7a   srcid);.    siz
1430: 65 20 3d 20 62 6c 6f 62 5f 73 69 7a 65 28 26 64  e = blob_size(&d
1440: 65 6c 74 61 29 3b 0a 20 20 20 20 69 66 28 20 70  elta);.    if( p
1450: 4f 75 74 20 29 7b 0a 20 20 20 20 20 20 62 6c 6f  Out ){.      blo
1460: 62 5f 61 70 70 65 6e 64 66 28 70 4f 75 74 2c 20  b_appendf(pOut, 
1470: 22 66 69 6c 65 20 25 62 20 25 62 20 25 64 5c 6e  "file %b %b %d\n
1480: 22 2c 20 26 75 75 69 64 2c 20 26 73 72 63 75 75  ", &uuid, &srcuu
1490: 69 64 2c 20 73 69 7a 65 29 3b 0a 20 20 20 20 20  id, size);.     
14a0: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 28 70 4f 75   blob_append(pOu
14b0: 74 2c 20 62 6c 6f 62 5f 62 75 66 66 65 72 28 26  t, blob_buffer(&
14c0: 64 65 6c 74 61 29 2c 20 73 69 7a 65 29 3b 0a 20  delta), size);. 
14d0: 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
14e0: 63 67 69 5f 70 72 69 6e 74 66 28 22 66 69 6c 65  cgi_printf("file
14f0: 20 25 62 20 25 62 20 25 64 5c 6e 22 2c 20 26 75   %b %b %d\n", &u
1500: 75 69 64 2c 20 26 73 72 63 75 75 69 64 2c 20 73  uid, &srcuuid, s
1510: 69 7a 65 29 3b 0a 20 20 20 20 20 20 63 67 69 5f  ize);.      cgi_
1520: 61 70 70 65 6e 64 5f 63 6f 6e 74 65 6e 74 28 62  append_content(b
1530: 6c 6f 62 5f 62 75 66 66 65 72 28 26 64 65 6c 74  lob_buffer(&delt
1540: 61 29 2c 20 73 69 7a 65 29 3b 0a 20 20 20 20 7d  a), size);.    }
1550: 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28  .    blob_reset(
1560: 26 64 65 6c 74 61 29 3b 0a 20 20 20 20 62 6c 6f  &delta);.    blo
1570: 62 5f 72 65 73 65 74 28 26 73 72 63 75 75 69 64  b_reset(&srcuuid
1580: 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 65  );.    blob_rese
1590: 74 28 26 75 75 69 64 29 3b 0a 20 20 7d 65 6c 73  t(&uuid);.  }els
15a0: 65 7b 0a 20 20 20 20 73 69 7a 65 20 3d 20 62 6c  e{.    size = bl
15b0: 6f 62 5f 73 69 7a 65 28 26 63 6f 6e 74 65 6e 74  ob_size(&content
15c0: 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 75 74 20  );.    if( pOut 
15d0: 29 7b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 61 70  ){.      blob_ap
15e0: 70 65 6e 64 66 28 70 4f 75 74 2c 20 22 66 69 6c  pendf(pOut, "fil
15f0: 65 20 25 62 20 25 64 5c 6e 22 2c 20 26 75 75 69  e %b %d\n", &uui
1600: 64 2c 20 73 69 7a 65 29 3b 0a 20 20 20 20 20 20  d, size);.      
1610: 62 6c 6f 62 5f 61 70 70 65 6e 64 28 70 4f 75 74  blob_append(pOut
1620: 2c 20 62 6c 6f 62 5f 62 75 66 66 65 72 28 26 63  , blob_buffer(&c
1630: 6f 6e 74 65 6e 74 29 2c 20 73 69 7a 65 29 3b 0a  ontent), size);.
1640: 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1650: 20 63 67 69 5f 70 72 69 6e 74 66 28 22 66 69 6c   cgi_printf("fil
1660: 65 20 25 62 20 25 64 5c 6e 22 2c 20 26 75 75 69  e %b %d\n", &uui
1670: 64 2c 20 73 69 7a 65 29 3b 0a 20 20 20 20 20 20  d, size);.      
1680: 63 67 69 5f 61 70 70 65 6e 64 5f 63 6f 6e 74 65  cgi_append_conte
1690: 6e 74 28 62 6c 6f 62 5f 62 75 66 66 65 72 28 26  nt(blob_buffer(&
16a0: 63 6f 6e 74 65 6e 74 29 2c 20 73 69 7a 65 29 3b  content), size);
16b0: 0a 20 20 20 20 7d 0a 20 20 20 20 62 6c 6f 62 5f  .    }.    blob_
16c0: 72 65 73 65 74 28 26 63 6f 6e 74 65 6e 74 29 3b  reset(&content);
16d0: 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28  .    blob_reset(
16e0: 26 75 75 69 64 29 3b 0a 20 20 7d 0a 20 20 64 62  &uuid);.  }.  db
16f0: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22 49 4e 53  _multi_exec("INS
1700: 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e  ERT OR IGNORE IN
1710: 54 4f 20 6f 6e 72 65 6d 6f 74 65 20 56 41 4c 55  TO onremote VALU
1720: 45 53 28 25 64 29 22 2c 20 72 69 64 29 3b 0a 20  ES(%d)", rid);. 
1730: 20 72 65 74 75 72 6e 20 73 69 7a 65 3b 0a 7d 0a   return size;.}.
1740: 0a 0a 2f 2a 0a 2a 2a 20 53 65 6e 64 20 61 6c 6c  ../*.** Send all
1750: 20 70 65 6e 64 69 6e 67 20 66 69 6c 65 73 2e 0a   pending files..
1760: 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65  */.static int se
1770: 6e 64 5f 61 6c 6c 5f 70 65 6e 64 69 6e 67 28 42  nd_all_pending(B
1780: 6c 6f 62 20 2a 70 4f 75 74 29 7b 0a 20 20 69 6e  lob *pOut){.  in
1790: 74 20 69 52 69 64 53 65 6e 74 20 3d 20 30 3b 0a  t iRidSent = 0;.
17a0: 20 20 69 6e 74 20 73 65 6e 74 20 3d 20 30 3b 0a    int sent = 0;.
17b0: 20 20 69 6e 74 20 6e 53 65 6e 74 20 3d 20 30 3b    int nSent = 0;
17c0: 0a 20 20 69 6e 74 20 6d 61 78 53 69 7a 65 20 3d  .  int maxSize =
17d0: 20 64 62 5f 67 65 74 5f 69 6e 74 28 22 68 74 74   db_get_int("htt
17e0: 70 2d 6d 73 67 2d 73 69 7a 65 22 2c 20 35 30 30  p-msg-size", 500
17f0: 30 30 30 29 3b 0a 20 20 53 74 6d 74 20 71 3b 0a  000);.  Stmt q;.
1800: 0a 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 71  .  db_prepare(&q
1810: 2c 20 22 53 45 4c 45 43 54 20 72 69 64 20 46 52  , "SELECT rid FR
1820: 4f 4d 20 70 65 6e 64 69 6e 67 20 4f 52 44 45 52  OM pending ORDER
1830: 20 42 59 20 72 69 64 22 29 3b 0a 20 20 77 68 69   BY rid");.  whi
1840: 6c 65 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d  le( db_step(&q)=
1850: 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20  =SQLITE_ROW ){. 
1860: 20 20 20 69 6e 74 20 72 69 64 20 3d 20 64 62 5f     int rid = db_
1870: 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 30  column_int(&q, 0
1880: 29 3b 0a 20 20 20 20 69 66 28 20 73 65 6e 74 3c  );.    if( sent<
1890: 6d 61 78 53 69 7a 65 20 29 7b 0a 20 20 20 20 20  maxSize ){.     
18a0: 20 73 65 6e 74 20 2b 3d 20 73 65 6e 64 5f 66 69   sent += send_fi
18b0: 6c 65 28 72 69 64 2c 20 70 4f 75 74 29 3b 0a 20  le(rid, pOut);. 
18c0: 20 20 20 20 20 6e 53 65 6e 74 2b 2b 3b 0a 20 20       nSent++;.  
18d0: 20 20 20 20 69 52 69 64 53 65 6e 74 20 3d 20 72      iRidSent = r
18e0: 69 64 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  id;.    }else{. 
18f0: 20 20 20 20 20 63 68 61 72 20 2a 7a 55 75 69 64       char *zUuid
1900: 20 3d 20 64 62 5f 74 65 78 74 28 30 2c 0a 20 20   = db_text(0,.  
1910: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1920: 20 20 20 20 22 53 45 4c 45 43 54 20 75 75 69 64      "SELECT uuid
1930: 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45   FROM blob WHERE
1940: 20 72 69 64 3d 25 64 20 41 4e 44 20 73 69 7a 65   rid=%d AND size
1950: 3e 3d 30 22 2c 20 72 69 64 29 3b 0a 20 20 20 20  >=0", rid);.    
1960: 20 20 69 66 28 20 7a 55 75 69 64 20 29 7b 0a 20    if( zUuid ){. 
1970: 20 20 20 20 20 20 20 69 66 28 20 70 4f 75 74 20         if( pOut 
1980: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62 6c 6f  ){.          blo
1990: 62 5f 61 70 70 65 6e 64 66 28 70 4f 75 74 2c 20  b_appendf(pOut, 
19a0: 22 69 67 6f 74 20 25 73 5c 6e 22 2c 20 7a 55 75  "igot %s\n", zUu
19b0: 69 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  id);.        }el
19c0: 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 63 67  se{.          cg
19d0: 69 5f 70 72 69 6e 74 66 28 22 69 67 6f 74 20 25  i_printf("igot %
19e0: 73 5c 6e 22 2c 20 7a 55 75 69 64 29 3b 0a 20 20  s\n", zUuid);.  
19f0: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1a00: 66 72 65 65 28 7a 55 75 69 64 29 3b 0a 20 20 20  free(zUuid);.   
1a10: 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
1a20: 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29   db_finalize(&q)
1a30: 3b 0a 20 20 0a 20 20 2f 2a 20 44 65 6c 65 74 65  ;.  .  /* Delete
1a40: 20 74 68 65 20 27 70 65 6e 64 69 6e 67 27 20 72   the 'pending' r
1a50: 65 63 6f 72 64 73 20 66 6f 72 20 61 6c 6c 20 66  ecords for all f
1a60: 69 6c 65 73 20 6a 75 73 74 20 73 65 6e 74 2e 20  iles just sent. 
1a70: 4f 74 68 65 72 77 69 73 65 2c 0a 20 20 2a 2a 20  Otherwise,.  ** 
1a80: 77 65 20 63 61 6e 20 77 69 6e 64 20 75 70 20 73  we can wind up s
1a90: 65 6e 64 69 6e 67 20 73 6f 6d 65 20 66 69 6c 65  ending some file
1aa0: 73 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65  s more than once
1ab0: 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 53 65  ..  */.  if( nSe
1ac0: 6e 74 3e 30 20 29 7b 0a 20 20 20 20 64 62 5f 6d  nt>0 ){.    db_m
1ad0: 75 6c 74 69 5f 65 78 65 63 28 22 44 45 4c 45 54  ulti_exec("DELET
1ae0: 45 20 46 52 4f 4d 20 70 65 6e 64 69 6e 67 20 57  E FROM pending W
1af0: 48 45 52 45 20 72 69 64 20 3c 3d 20 25 64 22 2c  HERE rid <= %d",
1b00: 20 69 52 69 64 53 65 6e 74 29 3b 0a 20 20 7d 0a   iRidSent);.  }.
1b10: 0a 20 20 72 65 74 75 72 6e 20 6e 53 65 6e 74 3b  .  return nSent;
1b20: 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b  .}.../*.** Check
1b30: 20 74 68 65 20 73 69 67 6e 61 74 75 72 65 20 6f   the signature o
1b40: 6e 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e  n an application
1b50: 2f 78 2d 66 6f 73 73 69 6c 20 70 61 79 6c 6f 61  /x-fossil payloa
1b60: 64 20 72 65 63 65 69 76 65 64 20 62 79 0a 2a 2a  d received by.**
1b70: 20 74 68 65 20 48 54 54 50 20 73 65 72 76 65 72   the HTTP server
1b80: 2e 20 20 54 68 65 20 73 69 67 6e 61 74 75 72 65  .  The signature
1b90: 20 69 73 20 61 20 6c 69 6e 65 20 6f 66 20 74 68   is a line of th
1ba0: 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d  e following form
1bb0: 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 6c  :.**.**        l
1bc0: 6f 67 69 6e 20 4c 4f 47 49 4e 20 4e 4f 4e 43 45  ogin LOGIN NONCE
1bd0: 20 53 49 47 4e 41 54 55 52 45 0a 2a 2a 0a 2a 2a   SIGNATURE.**.**
1be0: 20 54 68 65 20 4e 4f 4e 43 45 20 69 73 20 61 20   The NONCE is a 
1bf0: 72 61 6e 64 6f 6d 20 73 74 72 69 6e 67 2e 20 20  random string.  
1c00: 54 68 65 20 73 65 72 76 65 72 20 77 69 6c 6c 20  The server will 
1c10: 6e 65 76 65 72 20 61 63 63 65 70 74 20 61 0a 2a  never accept a.*
1c20: 2a 20 72 65 70 65 61 74 20 4e 4f 4e 43 45 2e 20  * repeat NONCE. 
1c30: 20 53 49 47 4e 41 54 55 52 45 20 69 73 20 74 68   SIGNATURE is th
1c40: 65 20 53 48 41 31 20 63 68 65 63 6b 73 75 6d 20  e SHA1 checksum 
1c50: 6f 66 20 74 68 65 20 4e 4f 4e 43 45 20 0a 2a 2a  of the NONCE .**
1c60: 20 63 6f 6e 63 61 74 65 6e 61 74 65 64 20 77 69   concatenated wi
1c70: 74 68 20 74 68 65 20 75 73 65 72 73 20 70 61 73  th the users pas
1c80: 73 77 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  sword..**.** The
1c90: 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74   parameters to t
1ca0: 68 69 73 20 72 6f 75 74 69 6e 65 20 61 72 65 20  his routine are 
1cb0: 65 70 68 65 72 6d 65 72 61 6c 20 62 6c 6f 62 73  ephermeral blobs
1cc0: 20 68 6f 6c 64 69 6e 67 20 74 68 65 0a 2a 2a 20   holding the.** 
1cd0: 4c 4f 47 49 4e 2c 20 4e 4f 4e 43 45 20 61 6e 64  LOGIN, NONCE and
1ce0: 20 53 49 47 4e 41 54 55 52 45 2e 0a 2a 2a 0a 2a   SIGNATURE..**.*
1cf0: 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61  * This routine a
1d00: 74 74 65 6d 70 74 73 20 74 6f 20 6c 6f 63 61 74  ttempts to locat
1d10: 65 20 74 68 65 20 75 73 65 72 20 61 6e 64 20 76  e the user and v
1d20: 65 72 69 66 79 20 74 68 65 20 73 69 67 6e 61 74  erify the signat
1d30: 75 72 65 2e 0a 2a 2a 20 49 66 20 65 76 65 72 79  ure..** If every
1d40: 74 68 69 6e 67 20 63 68 65 63 6b 73 20 6f 75 74  thing checks out
1d50: 2c 20 74 68 65 20 55 53 45 52 2e 43 41 50 20 63  , the USER.CAP c
1d60: 6f 6c 75 6d 6e 20 66 6f 72 20 74 68 65 20 55 53  olumn for the US
1d70: 45 52 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20 63  ER table.** is c
1d80: 6f 6e 73 75 6c 74 65 64 20 74 6f 20 73 65 74 20  onsulted to set 
1d90: 70 72 69 76 69 6c 65 67 65 73 20 69 6e 20 74 68  privileges in th
1da0: 65 20 67 6c 6f 62 61 6c 20 67 20 76 61 72 69 61  e global g varia
1db0: 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ble..**.** If an
1dc0: 79 74 68 69 6e 67 20 66 61 69 6c 73 20 74 6f 20  ything fails to 
1dd0: 63 68 65 63 6b 20 6f 75 74 2c 20 6e 6f 20 63 68  check out, no ch
1de0: 61 6e 67 65 73 20 61 72 65 20 6d 61 64 65 20 74  anges are made t
1df0: 6f 20 70 72 69 76 69 6c 65 67 65 73 2e 0a 2a 2a  o privileges..**
1e00: 0a 2a 2a 20 53 69 67 6e 61 74 75 72 65 20 67 65  .** Signature ge
1e10: 6e 65 72 61 74 69 6f 6e 20 6f 6e 20 74 68 65 20  neration on the 
1e20: 63 6c 69 65 6e 74 20 73 69 64 65 20 69 73 20 68  client side is h
1e30: 61 6e 64 6c 65 64 20 62 79 20 74 68 65 20 0a 2a  andled by the .*
1e40: 2a 20 68 74 74 70 5f 65 78 63 68 61 6e 67 65 28  * http_exchange(
1e50: 29 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 76 6f  ) routine..*/.vo
1e60: 69 64 20 63 68 65 63 6b 5f 6c 6f 67 69 6e 28 42  id check_login(B
1e70: 6c 6f 62 20 2a 70 4c 6f 67 69 6e 2c 20 42 6c 6f  lob *pLogin, Blo
1e80: 62 20 2a 70 4e 6f 6e 63 65 2c 20 42 6c 6f 62 20  b *pNonce, Blob 
1e90: 2a 70 53 69 67 29 7b 0a 20 20 53 74 6d 74 20 71  *pSig){.  Stmt q
1ea0: 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 69  ;.  int rc;..  i
1eb0: 66 28 20 64 62 5f 65 78 69 73 74 73 28 22 53 45  f( db_exists("SE
1ec0: 4c 45 43 54 20 31 20 46 52 4f 4d 20 72 63 76 66  LECT 1 FROM rcvf
1ed0: 72 6f 6d 20 57 48 45 52 45 20 6e 6f 6e 63 65 3d  rom WHERE nonce=
1ee0: 25 42 22 2c 20 70 4e 6f 6e 63 65 29 20 29 7b 0a  %B", pNonce) ){.
1ef0: 20 20 20 20 72 65 74 75 72 6e 3b 20 20 2f 2a 20      return;  /* 
1f00: 4e 65 76 65 72 20 61 63 63 65 70 74 20 61 20 72  Never accept a r
1f10: 65 70 65 61 74 65 64 20 6e 6f 6e 63 65 20 2a 2f  epeated nonce */
1f20: 0a 20 20 7d 0a 20 20 64 62 5f 70 72 65 70 61 72  .  }.  db_prepar
1f30: 65 28 26 71 2c 20 22 53 45 4c 45 43 54 20 70 77  e(&q, "SELECT pw
1f40: 2c 20 63 61 70 2c 20 75 69 64 20 46 52 4f 4d 20  , cap, uid FROM 
1f50: 75 73 65 72 20 57 48 45 52 45 20 6c 6f 67 69 6e  user WHERE login
1f60: 3d 25 42 22 2c 20 70 4c 6f 67 69 6e 29 3b 0a 20  =%B", pLogin);. 
1f70: 20 69 66 28 20 64 62 5f 73 74 65 70 28 26 71 29   if( db_step(&q)
1f80: 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a  ==SQLITE_ROW ){.
1f90: 20 20 20 20 42 6c 6f 62 20 70 77 2c 20 63 6f 6d      Blob pw, com
1fa0: 62 69 6e 65 64 2c 20 68 61 73 68 3b 0a 20 20 20  bined, hash;.   
1fb0: 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 70 77 29 3b   blob_zero(&pw);
1fc0: 0a 20 20 20 20 64 62 5f 65 70 68 65 6d 65 72 61  .    db_ephemera
1fd0: 6c 5f 62 6c 6f 62 28 26 71 2c 20 30 2c 20 26 70  l_blob(&q, 0, &p
1fe0: 77 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 7a 65 72  w);.    blob_zer
1ff0: 6f 28 26 63 6f 6d 62 69 6e 65 64 29 3b 0a 20 20  o(&combined);.  
2000: 20 20 62 6c 6f 62 5f 63 6f 70 79 28 26 63 6f 6d    blob_copy(&com
2010: 62 69 6e 65 64 2c 20 70 4e 6f 6e 63 65 29 3b 0a  bined, pNonce);.
2020: 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 28      blob_append(
2030: 26 63 6f 6d 62 69 6e 65 64 2c 20 62 6c 6f 62 5f  &combined, blob_
2040: 62 75 66 66 65 72 28 26 70 77 29 2c 20 62 6c 6f  buffer(&pw), blo
2050: 62 5f 73 69 7a 65 28 26 70 77 29 29 3b 0a 20 20  b_size(&pw));.  
2060: 20 20 2f 2a 20 43 47 49 44 45 42 55 47 28 28 22    /* CGIDEBUG(("
2070: 70 72 65 73 69 67 3d 5b 25 73 5d 5c 6e 22 2c 20  presig=[%s]\n", 
2080: 62 6c 6f 62 5f 73 74 72 28 26 63 6f 6d 62 69 6e  blob_str(&combin
2090: 65 64 29 29 29 3b 20 2a 2f 0a 20 20 20 20 73 68  ed))); */.    sh
20a0: 61 31 73 75 6d 5f 62 6c 6f 62 28 26 63 6f 6d 62  a1sum_blob(&comb
20b0: 69 6e 65 64 2c 20 26 68 61 73 68 29 3b 0a 20 20  ined, &hash);.  
20c0: 20 20 72 63 20 3d 20 62 6c 6f 62 5f 63 6f 6d 70    rc = blob_comp
20d0: 61 72 65 28 26 68 61 73 68 2c 20 70 53 69 67 29  are(&hash, pSig)
20e0: 3b 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74  ;.    blob_reset
20f0: 28 26 68 61 73 68 29 3b 0a 20 20 20 20 62 6c 6f  (&hash);.    blo
2100: 62 5f 72 65 73 65 74 28 26 63 6f 6d 62 69 6e 65  b_reset(&combine
2110: 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  d);.    if( rc==
2120: 30 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74  0 ){.      const
2130: 20 63 68 61 72 20 2a 7a 43 61 70 3b 0a 20 20 20   char *zCap;.   
2140: 20 20 20 7a 43 61 70 20 3d 20 64 62 5f 63 6f 6c     zCap = db_col
2150: 75 6d 6e 5f 74 65 78 74 28 26 71 2c 20 31 29 3b  umn_text(&q, 1);
2160: 0a 20 20 20 20 20 20 6c 6f 67 69 6e 5f 73 65 74  .      login_set
2170: 5f 63 61 70 61 62 69 6c 69 74 69 65 73 28 7a 43  _capabilities(zC
2180: 61 70 29 3b 0a 20 20 20 20 20 20 67 2e 75 73 65  ap);.      g.use
2190: 72 55 69 64 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e  rUid = db_column
21a0: 5f 69 6e 74 28 26 71 2c 20 32 29 3b 0a 20 20 20  _int(&q, 2);.   
21b0: 20 20 20 67 2e 7a 4c 6f 67 69 6e 20 3d 20 6d 70     g.zLogin = mp
21c0: 72 69 6e 74 66 28 22 25 62 22 2c 20 70 4c 6f 67  rintf("%b", pLog
21d0: 69 6e 29 3b 0a 20 20 20 20 20 20 67 2e 7a 4e 6f  in);.      g.zNo
21e0: 6e 63 65 20 3d 20 6d 70 72 69 6e 74 66 28 22 25  nce = mprintf("%
21f0: 62 22 2c 20 70 4e 6f 6e 63 65 29 3b 0a 20 20 20  b", pNonce);.   
2200: 20 7d 0a 20 20 7d 0a 20 20 64 62 5f 72 65 73 65   }.  }.  db_rese
2210: 74 28 26 71 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  t(&q);.}.../*.**
2220: 20 49 66 20 74 68 69 73 20 76 61 72 69 61 62 6c   If this variabl
2230: 65 20 69 73 20 73 65 74 2c 20 64 69 73 61 62 6c  e is set, disabl
2240: 65 20 6c 6f 67 69 6e 20 63 68 65 63 6b 73 2e 20  e login checks. 
2250: 20 55 73 65 64 20 66 6f 72 20 64 65 62 75 67 67   Used for debugg
2260: 69 6e 67 0a 2a 2a 20 6f 6e 6c 79 2e 0a 2a 2f 0a  ing.** only..*/.
2270: 73 74 61 74 69 63 20 69 6e 74 20 64 69 73 61 62  static int disab
2280: 6c 65 4c 6f 67 69 6e 20 3d 20 30 3b 0a 0a 2f 2a  leLogin = 0;../*
2290: 0a 2a 2a 20 57 45 42 50 41 47 45 3a 20 78 66 65  .** WEBPAGE: xfe
22a0: 72 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  r.**.** This is 
22b0: 74 68 65 20 74 72 61 6e 73 66 65 72 20 68 61 6e  the transfer han
22c0: 64 6c 65 72 20 6f 6e 20 74 68 65 20 73 65 72 76  dler on the serv
22d0: 65 72 20 73 69 64 65 2e 20 20 54 68 65 20 74 72  er side.  The tr
22e0: 61 6e 73 66 65 72 0a 2a 2a 20 6d 65 73 73 61 67  ansfer.** messag
22f0: 65 20 68 61 73 20 62 65 65 6e 20 75 6e 63 6f 6d  e has been uncom
2300: 70 72 65 73 73 65 64 20 61 6e 64 20 70 6c 61 63  pressed and plac
2310: 65 64 20 69 6e 20 74 68 65 20 67 2e 63 67 69 49  ed in the g.cgiI
2320: 6e 20 62 6c 6f 62 2e 0a 2a 2a 20 50 72 6f 63 65  n blob..** Proce
2330: 73 73 20 74 68 69 73 20 6d 65 73 73 61 67 65 20  ss this message 
2340: 61 6e 64 20 66 6f 72 6d 20 61 6e 20 61 70 70 72  and form an appr
2350: 6f 70 72 69 61 74 65 20 72 65 70 6c 79 2e 0a 2a  opriate reply..*
2360: 2f 0a 76 6f 69 64 20 70 61 67 65 5f 78 66 65 72  /.void page_xfer
2370: 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 6e 54  (void){.  int nT
2380: 6f 6b 65 6e 3b 0a 20 20 69 6e 74 20 69 73 50 75  oken;.  int isPu
2390: 6c 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 73  ll = 0;.  int is
23a0: 50 75 73 68 20 3d 20 30 3b 0a 20 20 69 6e 74 20  Push = 0;.  int 
23b0: 6e 45 72 72 20 3d 20 30 3b 0a 20 20 42 6c 6f 62  nErr = 0;.  Blob
23c0: 20 6c 69 6e 65 2c 20 65 72 72 6d 73 67 2c 20 61   line, errmsg, a
23d0: 54 6f 6b 65 6e 5b 35 5d 3b 0a 0a 20 20 64 62 5f  Token[5];..  db_
23e0: 62 65 67 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f  begin_transactio
23f0: 6e 28 29 3b 0a 20 20 62 6c 6f 62 61 72 72 61 79  n();.  blobarray
2400: 5f 7a 65 72 6f 28 61 54 6f 6b 65 6e 2c 20 63 6f  _zero(aToken, co
2410: 75 6e 74 28 61 54 6f 6b 65 6e 29 29 3b 0a 20 20  unt(aToken));.  
2420: 63 67 69 5f 73 65 74 5f 63 6f 6e 74 65 6e 74 5f  cgi_set_content_
2430: 74 79 70 65 28 67 2e 7a 43 6f 6e 74 65 6e 74 54  type(g.zContentT
2440: 79 70 65 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72  ype);.  blob_zer
2450: 6f 28 26 65 72 72 6d 73 67 29 3b 0a 20 20 64 62  o(&errmsg);.  db
2460: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20  _multi_exec(.   
2470: 20 20 22 43 52 45 41 54 45 20 54 45 4d 50 20 54    "CREATE TEMP T
2480: 41 42 4c 45 20 6f 6e 72 65 6d 6f 74 65 28 72 69  ABLE onremote(ri
2490: 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  d INTEGER PRIMAR
24a0: 59 20 4b 45 59 29 3b 22 20 2f 2a 20 43 6c 69 65  Y KEY);" /* Clie
24b0: 6e 74 20 68 61 73 20 2a 2f 0a 20 20 20 20 20 22  nt has */.     "
24c0: 43 52 45 41 54 45 20 54 45 4d 50 20 54 41 42 4c  CREATE TEMP TABL
24d0: 45 20 70 65 6e 64 69 6e 67 28 72 69 64 20 49 4e  E pending(rid IN
24e0: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
24f0: 59 29 3b 22 20 20 2f 2a 20 43 6c 69 65 6e 74 20  Y);"  /* Client 
2500: 6e 65 65 64 73 20 2a 2f 0a 20 20 29 3b 0a 20 20  needs */.  );.  
2510: 77 68 69 6c 65 28 20 62 6c 6f 62 5f 6c 69 6e 65  while( blob_line
2520: 28 26 67 2e 63 67 69 49 6e 2c 20 26 6c 69 6e 65  (&g.cgiIn, &line
2530: 29 20 29 7b 0a 20 20 20 20 6e 54 6f 6b 65 6e 20  ) ){.    nToken 
2540: 3d 20 62 6c 6f 62 5f 74 6f 6b 65 6e 69 7a 65 28  = blob_tokenize(
2550: 26 6c 69 6e 65 2c 20 61 54 6f 6b 65 6e 2c 20 63  &line, aToken, c
2560: 6f 75 6e 74 28 61 54 6f 6b 65 6e 29 29 3b 0a 0a  ount(aToken));..
2570: 20 20 20 20 2f 2a 20 20 20 66 69 6c 65 20 55 55      /*   file UU
2580: 49 44 20 53 49 5a 45 20 5c 6e 20 43 4f 4e 54 45  ID SIZE \n CONTE
2590: 4e 54 0a 20 20 20 20 2a 2a 20 20 20 66 69 6c 65  NT.    **   file
25a0: 20 55 55 49 44 20 44 45 4c 54 41 53 52 43 20 53   UUID DELTASRC S
25b0: 49 5a 45 20 5c 6e 20 43 4f 4e 54 45 4e 54 0a 20  IZE \n CONTENT. 
25c0: 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 63 63     **.    ** Acc
25d0: 65 70 74 20 61 20 66 69 6c 65 20 66 72 6f 6d 20  ept a file from 
25e0: 74 68 65 20 63 6c 69 65 6e 74 2e 0a 20 20 20 20  the client..    
25f0: 2a 2f 0a 20 20 20 20 69 66 28 20 62 6c 6f 62 5f  */.    if( blob_
2600: 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22  eq(&aToken[0], "
2610: 66 69 6c 65 22 29 20 26 26 20 6e 54 6f 6b 65 6e  file") && nToken
2620: 3e 3d 33 20 26 26 20 6e 54 6f 6b 65 6e 3c 3d 34  >=3 && nToken<=4
2630: 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 69   ){.      if( !i
2640: 73 50 75 73 68 20 29 7b 0a 20 20 20 20 20 20 20  sPush ){.       
2650: 20 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e 74 65   cgi_reset_conte
2660: 6e 74 28 29 3b 0a 20 20 20 20 20 20 20 20 40 20  nt();.        @ 
2670: 65 72 72 6f 72 20 6e 6f 74 5c 73 61 75 74 68 6f  error not\sautho
2680: 72 69 7a 65 64 5c 73 74 6f 5c 73 77 72 69 74 65  rized\sto\swrite
2690: 0a 20 20 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b  .        nErr++;
26a0: 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
26b0: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 78 66        }.      xf
26c0: 65 72 5f 61 63 63 65 70 74 5f 66 69 6c 65 28 26  er_accept_file(&
26d0: 67 2e 63 67 69 49 6e 2c 20 61 54 6f 6b 65 6e 2c  g.cgiIn, aToken,
26e0: 20 6e 54 6f 6b 65 6e 2c 20 26 65 72 72 6d 73 67   nToken, &errmsg
26f0: 29 3b 0a 20 20 20 20 20 20 69 66 28 20 62 6c 6f  );.      if( blo
2700: 62 5f 73 69 7a 65 28 26 65 72 72 6d 73 67 29 20  b_size(&errmsg) 
2710: 29 7b 0a 20 20 20 20 20 20 20 20 63 67 69 5f 72  ){.        cgi_r
2720: 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a  eset_content();.
2730: 20 20 20 20 20 20 20 20 40 20 65 72 72 6f 72 20          @ error 
2740: 25 54 28 62 6c 6f 62 5f 73 74 72 28 26 65 72 72  %T(blob_str(&err
2750: 6d 73 67 29 29 0a 20 20 20 20 20 20 20 20 6e 45  msg)).        nE
2760: 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 62 72  rr++;.        br
2770: 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
2780: 20 7d 65 6c 73 65 0a 0a 20 20 20 20 2f 2a 20 20   }else..    /*  
2790: 20 67 69 6d 6d 65 20 55 55 49 44 0a 20 20 20 20   gimme UUID.    
27a0: 2a 2a 0a 20 20 20 20 2a 2a 20 43 6c 69 65 6e 74  **.    ** Client
27b0: 20 69 73 20 72 65 71 75 65 73 74 69 6e 67 20 61   is requesting a
27c0: 20 66 69 6c 65 0a 20 20 20 20 2a 2f 0a 20 20 20   file.    */.   
27d0: 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 61 54   if( blob_eq(&aT
27e0: 6f 6b 65 6e 5b 30 5d 2c 20 22 67 69 6d 6d 65 22  oken[0], "gimme"
27f0: 29 20 26 26 20 6e 54 6f 6b 65 6e 3d 3d 32 20 26  ) && nToken==2 &
2800: 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26  & blob_is_uuid(&
2810: 61 54 6f 6b 65 6e 5b 31 5d 29 20 29 7b 0a 20 20  aToken[1]) ){.  
2820: 20 20 20 20 69 66 28 20 69 73 50 75 6c 6c 20 29      if( isPull )
2830: 7b 0a 20 20 20 20 20 20 20 20 64 62 5f 6d 75 6c  {.        db_mul
2840: 74 69 5f 65 78 65 63 28 0a 20 20 20 20 20 20 20  ti_exec(.       
2850: 20 20 20 22 49 4e 53 45 52 54 20 4f 52 20 49 47     "INSERT OR IG
2860: 4e 4f 52 45 20 49 4e 54 4f 20 70 65 6e 64 69 6e  NORE INTO pendin
2870: 67 28 72 69 64 29 20 22 0a 20 20 20 20 20 20 20  g(rid) ".       
2880: 20 20 20 22 53 45 4c 45 43 54 20 72 69 64 20 46     "SELECT rid F
2890: 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 75  ROM blob WHERE u
28a0: 75 69 64 3d 25 42 20 41 4e 44 20 73 69 7a 65 3e  uid=%B AND size>
28b0: 3d 30 22 2c 20 26 61 54 6f 6b 65 6e 5b 31 5d 0a  =0", &aToken[1].
28c0: 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20          );.     
28d0: 20 7d 0a 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20   }.    }else..  
28e0: 20 20 2f 2a 20 20 20 69 67 6f 74 20 55 55 49 44    /*   igot UUID
28f0: 0a 20 20 20 20 2a 2a 20 20 20 6c 65 61 66 20 55  .    **   leaf U
2900: 55 49 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  UID.    **.    *
2910: 2a 20 43 6c 69 65 6e 74 20 61 6e 6e 6f 75 6e 63  * Client announc
2920: 65 73 20 74 68 61 74 20 69 74 20 68 61 73 20 61  es that it has a
2930: 20 70 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65   particular file
2940: 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
2950: 6e 54 6f 6b 65 6e 3d 3d 32 0a 20 20 20 20 20 20  nToken==2.      
2960: 20 20 20 20 26 26 20 28 62 6c 6f 62 5f 65 71 28      && (blob_eq(
2970: 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 69 67 6f  &aToken[0], "igo
2980: 74 22 29 20 7c 7c 20 62 6c 6f 62 5f 65 71 28 26  t") || blob_eq(&
2990: 61 54 6f 6b 65 6e 5b 30 5d 2c 22 6c 65 61 66 22  aToken[0],"leaf"
29a0: 29 29 0a 20 20 20 20 20 20 20 20 20 20 26 26 20  )).          && 
29b0: 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26 61 54  blob_is_uuid(&aT
29c0: 6f 6b 65 6e 5b 31 5d 29 20 29 7b 0a 20 20 20 20  oken[1]) ){.    
29d0: 20 20 69 66 28 20 69 73 50 75 6c 6c 20 7c 7c 20    if( isPull || 
29e0: 69 73 50 75 73 68 20 29 7b 0a 20 20 20 20 20 20  isPush ){.      
29f0: 20 20 69 6e 74 20 72 69 64 20 3d 20 64 62 5f 69    int rid = db_i
2a00: 6e 74 28 30 2c 20 22 53 45 4c 45 43 54 20 72 69  nt(0, "SELECT ri
2a10: 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52  d FROM blob WHER
2a20: 45 20 75 75 69 64 3d 25 42 22 2c 20 26 61 54 6f  E uuid=%B", &aTo
2a30: 6b 65 6e 5b 31 5d 29 3b 0a 20 20 20 20 20 20 20  ken[1]);.       
2a40: 20 69 66 28 20 72 69 64 3e 30 20 29 7b 0a 20 20   if( rid>0 ){.  
2a50: 20 20 20 20 20 20 20 20 64 62 5f 6d 75 6c 74 69          db_multi
2a60: 5f 65 78 65 63 28 0a 20 20 20 20 20 20 20 20 20  _exec(.         
2a70: 20 20 20 22 49 4e 53 45 52 54 20 4f 52 20 49 47     "INSERT OR IG
2a80: 4e 4f 52 45 20 49 4e 54 4f 20 6f 6e 72 65 6d 6f  NORE INTO onremo
2a90: 74 65 28 72 69 64 29 20 56 41 4c 55 45 53 28 25  te(rid) VALUES(%
2aa0: 64 29 22 2c 20 72 69 64 0a 20 20 20 20 20 20 20  d)", rid.       
2ab0: 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20     );.          
2ac0: 69 66 28 20 69 73 50 75 6c 6c 20 26 26 20 62 6c  if( isPull && bl
2ad0: 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d  ob_eq(&aToken[0]
2ae0: 2c 20 22 6c 65 61 66 22 29 20 29 7b 0a 20 20 20  , "leaf") ){.   
2af0: 20 20 20 20 20 20 20 20 20 64 62 5f 6d 75 6c 74           db_mult
2b00: 69 5f 65 78 65 63 28 0a 20 20 20 20 20 20 20 20  i_exec(.        
2b10: 20 20 20 20 20 20 22 49 4e 53 45 52 54 20 4f 52        "INSERT OR
2b20: 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 70 65 6e   IGNORE INTO pen
2b30: 64 69 6e 67 28 72 69 64 29 20 22 0a 20 20 20 20  ding(rid) ".    
2b40: 20 20 20 20 20 20 20 20 20 20 22 53 45 4c 45 43            "SELEC
2b50: 54 20 63 69 64 20 46 52 4f 4d 20 70 6c 69 6e 6b  T cid FROM plink
2b60: 20 57 48 45 52 45 20 70 69 64 3d 25 64 22 2c 20   WHERE pid=%d", 
2b70: 72 69 64 0a 20 20 20 20 20 20 20 20 20 20 20 20  rid.            
2b80: 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
2b90: 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
2ba0: 20 69 73 50 75 73 68 20 29 7b 0a 20 20 20 20 20   isPush ){.     
2bb0: 20 20 20 20 20 63 6f 6e 74 65 6e 74 5f 70 75 74       content_put
2bc0: 28 30 2c 20 62 6c 6f 62 5f 73 74 72 28 26 61 54  (0, blob_str(&aT
2bd0: 6f 6b 65 6e 5b 31 5d 29 29 3b 0a 20 20 20 20 20  oken[1]));.     
2be0: 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
2bf0: 20 7d 65 6c 73 65 0a 0a 20 20 20 20 2f 2a 20 20   }else..    /*  
2c00: 20 20 70 75 6c 6c 20 20 53 45 52 56 45 52 43 4f    pull  SERVERCO
2c10: 44 45 20 20 50 52 4f 4a 45 43 54 43 4f 44 45 0a  DE  PROJECTCODE.
2c20: 20 20 20 20 2a 2a 20 20 20 20 70 75 73 68 20 20      **    push  
2c30: 53 45 52 56 45 52 43 4f 44 45 20 20 50 52 4f 4a  SERVERCODE  PROJ
2c40: 45 43 54 43 4f 44 45 0a 20 20 20 20 2a 2a 0a 20  ECTCODE.    **. 
2c50: 20 20 20 2a 2a 20 54 68 65 20 63 6c 69 65 6e 74     ** The client
2c60: 20 77 61 6e 74 73 20 65 69 74 68 65 72 20 73 65   wants either se
2c70: 6e 64 20 6f 72 20 72 65 63 65 69 76 65 0a 20 20  nd or receive.  
2c80: 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 54 6f    */.    if( nTo
2c90: 6b 65 6e 3d 3d 33 0a 20 20 20 20 20 20 20 20 20  ken==3.         
2ca0: 20 20 20 20 20 20 26 26 20 28 62 6c 6f 62 5f 65        && (blob_e
2cb0: 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 70  q(&aToken[0], "p
2cc0: 75 6c 6c 22 29 20 7c 7c 20 62 6c 6f 62 5f 65 71  ull") || blob_eq
2cd0: 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 70 75  (&aToken[0], "pu
2ce0: 73 68 22 29 29 0a 20 20 20 20 20 20 20 20 20 20  sh")).          
2cf0: 20 20 20 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f       && blob_is_
2d00: 75 75 69 64 28 26 61 54 6f 6b 65 6e 5b 31 5d 29  uuid(&aToken[1])
2d10: 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69 64   && blob_is_uuid
2d20: 28 26 61 54 6f 6b 65 6e 5b 32 5d 29 20 29 7b 0a  (&aToken[2]) ){.
2d30: 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
2d40: 20 2a 7a 53 43 6f 64 65 3b 0a 20 20 20 20 20 20   *zSCode;.      
2d50: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 43 6f  const char *zPCo
2d60: 64 65 3b 0a 0a 20 20 20 20 20 20 7a 53 43 6f 64  de;..      zSCod
2d70: 65 20 3d 20 64 62 5f 67 65 74 28 22 73 65 72 76  e = db_get("serv
2d80: 65 72 2d 63 6f 64 65 22 2c 20 30 29 3b 0a 20 20  er-code", 0);.  
2d90: 20 20 20 20 69 66 28 20 7a 53 43 6f 64 65 3d 3d      if( zSCode==
2da0: 30 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6f 73  0 ){.        fos
2db0: 73 69 6c 5f 70 61 6e 69 63 28 22 6d 69 73 73 69  sil_panic("missi
2dc0: 6e 67 20 73 65 72 76 65 72 20 63 6f 64 65 22 29  ng server code")
2dd0: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2de0: 69 66 28 20 62 6c 6f 62 5f 65 71 5f 73 74 72 28  if( blob_eq_str(
2df0: 26 61 54 6f 6b 65 6e 5b 31 5d 2c 20 7a 53 43 6f  &aToken[1], zSCo
2e00: 64 65 2c 20 2d 31 29 20 29 7b 0a 20 20 20 20 20  de, -1) ){.     
2e10: 20 20 20 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e     cgi_reset_con
2e20: 74 65 6e 74 28 29 3b 0a 20 20 20 20 20 20 20 20  tent();.        
2e30: 40 20 65 72 72 6f 72 20 73 65 72 76 65 72 5c 73  @ error server\s
2e40: 6c 6f 6f 70 0a 20 20 20 20 20 20 20 20 6e 45 72  loop.        nEr
2e50: 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 62 72 65  r++;.        bre
2e60: 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
2e70: 20 20 7a 50 43 6f 64 65 20 3d 20 64 62 5f 67 65    zPCode = db_ge
2e80: 74 28 22 70 72 6f 6a 65 63 74 2d 63 6f 64 65 22  t("project-code"
2e90: 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , 0);.      if( 
2ea0: 7a 50 43 6f 64 65 3d 3d 30 20 29 7b 0a 20 20 20  zPCode==0 ){.   
2eb0: 20 20 20 20 20 66 6f 73 73 69 6c 5f 70 61 6e 69       fossil_pani
2ec0: 63 28 22 6d 69 73 73 69 6e 67 20 70 72 6f 6a 65  c("missing proje
2ed0: 63 74 20 63 6f 64 65 22 29 3b 0a 20 20 20 20 20  ct code");.     
2ee0: 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 62 6c   }.      if( !bl
2ef0: 6f 62 5f 65 71 5f 73 74 72 28 26 61 54 6f 6b 65  ob_eq_str(&aToke
2f00: 6e 5b 32 5d 2c 20 7a 50 43 6f 64 65 2c 20 2d 31  n[2], zPCode, -1
2f10: 29 20 29 7b 0a 20 20 20 20 20 20 20 20 63 67 69  ) ){.        cgi
2f20: 5f 72 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29  _reset_content()
2f30: 3b 0a 20 20 20 20 20 20 20 20 40 20 65 72 72 6f  ;.        @ erro
2f40: 72 20 77 72 6f 6e 67 5c 73 70 72 6f 6a 65 63 74  r wrong\sproject
2f50: 0a 20 20 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b  .        nErr++;
2f60: 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2f70: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6c 6f        }.      lo
2f80: 67 69 6e 5f 63 68 65 63 6b 5f 63 72 65 64 65 6e  gin_check_creden
2f90: 74 69 61 6c 73 28 29 3b 0a 20 20 20 20 20 20 69  tials();.      i
2fa0: 66 28 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b  f( blob_eq(&aTok
2fb0: 65 6e 5b 30 5d 2c 20 22 70 75 6c 6c 22 29 20 29  en[0], "pull") )
2fc0: 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 67  {.        if( !g
2fd0: 2e 6f 6b 52 65 61 64 20 29 7b 0a 20 20 20 20 20  .okRead ){.     
2fe0: 20 20 20 20 20 63 67 69 5f 72 65 73 65 74 5f 63       cgi_reset_c
2ff0: 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 20 20 20 20  ontent();.      
3000: 20 20 20 20 40 20 65 72 72 6f 72 20 6e 6f 74 5c      @ error not\
3010: 73 61 75 74 68 6f 72 69 7a 65 64 5c 73 74 6f 5c  sauthorized\sto\
3020: 73 72 65 61 64 0a 20 20 20 20 20 20 20 20 20 20  sread.          
3030: 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20  nErr++;.        
3040: 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
3050: 20 7d 0a 20 20 20 20 20 20 20 20 69 73 50 75 6c   }.        isPul
3060: 6c 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c  l = 1;.      }el
3070: 73 65 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  se{.        if( 
3080: 21 67 2e 6f 6b 57 72 69 74 65 20 29 7b 0a 20 20  !g.okWrite ){.  
3090: 20 20 20 20 20 20 20 20 63 67 69 5f 72 65 73 65          cgi_rese
30a0: 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 20  t_content();.   
30b0: 20 20 20 20 20 20 20 40 20 65 72 72 6f 72 20 6e         @ error n
30c0: 6f 74 5c 73 61 75 74 68 6f 72 69 7a 65 64 5c 73  ot\sauthorized\s
30d0: 74 6f 5c 73 77 72 69 74 65 0a 20 20 20 20 20 20  to\swrite.      
30e0: 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20      nErr++;.    
30f0: 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3100: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
3110: 73 50 75 73 68 20 3d 20 31 3b 0a 0a 20 20 20 20  sPush = 1;..    
3120: 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 0a 0a 20    }.    }else.. 
3130: 20 20 20 2f 2a 20 20 20 20 63 6c 6f 6e 65 0a 20     /*    clone. 
3140: 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65     **.    ** The
3150: 20 63 6c 69 65 6e 74 20 6b 6e 6f 77 73 20 6e 6f   client knows no
3160: 74 68 69 6e 67 2e 20 20 54 65 6c 6c 20 61 6c 6c  thing.  Tell all
3170: 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
3180: 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e   blob_eq(&aToken
3190: 5b 30 5d 2c 20 22 63 6c 6f 6e 65 22 29 20 29 7b  [0], "clone") ){
31a0: 0a 20 20 20 20 20 20 6c 6f 67 69 6e 5f 63 68 65  .      login_che
31b0: 63 6b 5f 63 72 65 64 65 6e 74 69 61 6c 73 28 29  ck_credentials()
31c0: 3b 0a 20 20 20 20 20 20 69 66 28 20 21 67 2e 6f  ;.      if( !g.o
31d0: 6b 52 65 61 64 20 29 7b 0a 20 20 20 20 20 20 20  kRead ){.       
31e0: 20 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e 74 65   cgi_reset_conte
31f0: 6e 74 28 29 3b 0a 20 20 20 20 20 20 20 20 40 20  nt();.        @ 
3200: 65 72 72 6f 72 20 6e 6f 74 5c 73 61 75 74 68 6f  error not\sautho
3210: 72 69 7a 65 64 5c 73 74 6f 5c 73 72 65 61 64 0a  rized\sto\sread.
3220: 20 20 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a          nErr++;.
3230: 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3240: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 73 50       }.      isP
3250: 75 6c 6c 20 3d 20 31 3b 0a 20 20 20 20 20 20 40  ull = 1;.      @
3260: 20 70 75 73 68 20 25 73 28 64 62 5f 67 65 74 28   push %s(db_get(
3270: 22 73 65 72 76 65 72 2d 63 6f 64 65 22 2c 20 22  "server-code", "
3280: 78 22 29 29 20 25 73 28 64 62 5f 67 65 74 28 22  x")) %s(db_get("
3290: 70 72 6f 6a 65 63 74 2d 63 6f 64 65 22 2c 20 22  project-code", "
32a0: 78 22 29 29 0a 20 20 20 20 20 20 64 62 5f 6d 75  x")).      db_mu
32b0: 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 20 20 20  lti_exec(.      
32c0: 20 20 22 49 4e 53 45 52 54 20 4f 52 20 49 47 4e    "INSERT OR IGN
32d0: 4f 52 45 20 49 4e 54 4f 20 70 65 6e 64 69 6e 67  ORE INTO pending
32e0: 28 72 69 64 29 20 22 0a 20 20 20 20 20 20 20 20  (rid) ".        
32f0: 22 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f 4d  "SELECT rid FROM
3300: 20 62 6c 6f 62 20 57 48 45 52 45 20 73 69 7a 65   blob WHERE size
3310: 3e 3d 30 22 0a 20 20 20 20 20 20 29 3b 0a 20 20  >=0".      );.  
3320: 20 20 7d 65 6c 73 65 0a 0a 20 20 20 20 2f 2a 20    }else..    /* 
3330: 20 20 20 6c 6f 67 69 6e 20 20 55 53 45 52 20 20     login  USER  
3340: 4e 4f 4e 43 45 20 20 53 49 47 4e 41 54 55 52 45  NONCE  SIGNATURE
3350: 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43  .    **.    ** C
3360: 68 65 63 6b 20 66 6f 72 20 61 20 76 61 6c 69 64  heck for a valid
3370: 20 6c 6f 67 69 6e 2e 20 20 54 68 69 73 20 68 61   login.  This ha
3380: 73 20 74 6f 20 68 61 70 70 65 6e 20 62 65 66 6f  s to happen befo
3390: 72 65 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65  re anything else
33a0: 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
33b0: 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e   blob_eq(&aToken
33c0: 5b 30 5d 2c 20 22 6c 6f 67 69 6e 22 29 20 26 26  [0], "login") &&
33d0: 20 6e 54 6f 6b 65 6e 3d 3d 34 20 29 7b 0a 20 20   nToken==4 ){.  
33e0: 20 20 20 20 69 66 28 20 64 69 73 61 62 6c 65 4c      if( disableL
33f0: 6f 67 69 6e 20 29 7b 0a 20 20 20 20 20 20 20 20  ogin ){.        
3400: 67 2e 6f 6b 52 65 61 64 20 3d 20 67 2e 6f 6b 57  g.okRead = g.okW
3410: 72 69 74 65 20 3d 20 31 3b 0a 20 20 20 20 20 20  rite = 1;.      
3420: 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63  }else{.        c
3430: 68 65 63 6b 5f 6c 6f 67 69 6e 28 26 61 54 6f 6b  heck_login(&aTok
3440: 65 6e 5b 31 5d 2c 20 26 61 54 6f 6b 65 6e 5b 32  en[1], &aToken[2
3450: 5d 2c 20 26 61 54 6f 6b 65 6e 5b 33 5d 29 3b 0a  ], &aToken[3]);.
3460: 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
3470: 65 0a 0a 20 20 20 20 2f 2a 20 55 6e 6b 6e 6f 77  e..    /* Unknow
3480: 6e 20 6d 65 73 73 61 67 65 0a 20 20 20 20 2a 2f  n message.    */
3490: 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 63 67 69  .    {.      cgi
34a0: 5f 72 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29  _reset_content()
34b0: 3b 0a 20 20 20 20 20 20 40 20 65 72 72 6f 72 20  ;.      @ error 
34c0: 62 61 64 5c 73 63 6f 6d 6d 61 6e 64 3a 5c 73 25  bad\scommand:\s%
34d0: 46 28 62 6c 6f 62 5f 73 74 72 28 26 6c 69 6e 65  F(blob_str(&line
34e0: 29 29 0a 20 20 20 20 7d 0a 20 20 20 20 62 6c 6f  )).    }.    blo
34f0: 62 61 72 72 61 79 5f 72 65 73 65 74 28 61 54 6f  barray_reset(aTo
3500: 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29 3b 0a 20 20  ken, nToken);.  
3510: 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 69 6e 70 75  }..  /* The inpu
3520: 74 20 6d 65 73 73 61 67 65 20 68 61 73 20 6e 6f  t message has no
3530: 77 20 62 65 65 6e 20 70 72 6f 63 65 73 73 65 64  w been processed
3540: 2e 20 20 47 65 6e 65 72 61 74 65 20 61 20 72 65  .  Generate a re
3550: 70 6c 79 2e 20 2a 2f 0a 20 20 69 66 28 20 69 73  ply. */.  if( is
3560: 50 75 73 68 20 29 7b 0a 20 20 20 20 53 74 6d 74  Push ){.    Stmt
3570: 20 71 3b 0a 20 20 20 20 64 62 5f 70 72 65 70 61   q;.    db_prepa
3580: 72 65 28 26 71 2c 20 22 53 45 4c 45 43 54 20 75  re(&q, "SELECT u
3590: 75 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48  uid FROM blob WH
35a0: 45 52 45 20 73 69 7a 65 3c 30 22 29 3b 0a 20 20  ERE size<0");.  
35b0: 20 20 77 68 69 6c 65 28 20 64 62 5f 73 74 65 70    while( db_step
35c0: 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  (&q)==SQLITE_ROW
35d0: 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20   ){.      const 
35e0: 63 68 61 72 20 2a 7a 55 75 69 64 20 3d 20 64 62  char *zUuid = db
35f0: 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 26 71 2c  _column_text(&q,
3600: 20 30 29 3b 0a 20 20 20 20 20 20 40 20 67 69 6d   0);.      @ gim
3610: 6d 65 20 25 73 28 7a 55 75 69 64 29 0a 20 20 20  me %s(zUuid).   
3620: 20 7d 0a 20 20 20 20 64 62 5f 66 69 6e 61 6c 69   }.    db_finali
3630: 7a 65 28 26 71 29 3b 0a 20 20 7d 0a 20 20 69 66  ze(&q);.  }.  if
3640: 28 20 69 73 50 75 6c 6c 20 29 7b 0a 20 20 20 20  ( isPull ){.    
3650: 73 65 6e 64 5f 61 6c 6c 5f 70 65 6e 64 69 6e 67  send_all_pending
3660: 28 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69  (0);.  }.  if( i
3670: 73 50 75 73 68 20 7c 7c 20 69 73 50 75 6c 6c 20  sPush || isPull 
3680: 29 7b 0a 20 20 20 20 2f 2a 20 41 6c 77 61 79 73  ){.    /* Always
3690: 20 73 65 6e 64 20 6f 75 72 20 6c 65 61 76 65 73   send our leaves
36a0: 20 2a 2f 0a 20 20 20 20 53 74 6d 74 20 71 3b 0a   */.    Stmt q;.
36b0: 20 20 20 20 64 62 5f 70 72 65 70 61 72 65 28 26      db_prepare(&
36c0: 71 2c 20 0a 20 20 20 20 20 20 20 22 53 45 4c 45  q, .       "SELE
36d0: 43 54 20 75 75 69 64 20 46 52 4f 4d 20 62 6c 6f  CT uuid FROM blo
36e0: 62 20 57 48 45 52 45 20 72 69 64 20 49 4e 22 0a  b WHERE rid IN".
36f0: 20 20 20 20 20 20 20 22 20 20 28 53 45 4c 45 43         "  (SELEC
3700: 54 20 63 69 64 20 46 52 4f 4d 20 70 6c 69 6e 6b  T cid FROM plink
3710: 20 45 58 43 45 50 54 20 53 45 4c 45 43 54 20 70   EXCEPT SELECT p
3720: 69 64 20 46 52 4f 4d 20 70 6c 69 6e 6b 29 22 0a  id FROM plink)".
3730: 20 20 20 20 29 3b 0a 20 20 20 20 77 68 69 6c 65      );.    while
3740: 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53  ( db_step(&q)==S
3750: 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20  QLITE_ROW ){.   
3760: 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
3770: 55 75 69 64 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e  Uuid = db_column
3780: 5f 74 65 78 74 28 26 71 2c 20 30 29 3b 0a 20 20  _text(&q, 0);.  
3790: 20 20 20 20 40 20 6c 65 61 66 20 25 73 28 7a 55      @ leaf %s(zU
37a0: 75 69 64 29 0a 20 20 20 20 7d 0a 20 20 20 20 64  uid).    }.    d
37b0: 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a  b_finalize(&q);.
37c0: 20 20 7d 0a 0a 20 20 64 62 5f 65 6e 64 5f 74 72    }..  db_end_tr
37d0: 61 6e 73 61 63 74 69 6f 6e 28 30 29 3b 0a 7d 0a  ansaction(0);.}.
37e0: 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20  ./*.** COMMAND: 
37f0: 74 65 73 74 2d 78 66 65 72 0a 2a 2a 0a 2a 2a 20  test-xfer.**.** 
3800: 54 68 69 73 20 63 6f 6d 6d 61 6e 64 20 69 73 20  This command is 
3810: 75 73 65 64 20 66 6f 72 20 64 65 62 75 67 67 69  used for debuggi
3820: 6e 67 20 74 68 65 20 73 65 72 76 65 72 2e 20 20  ng the server.  
3830: 54 68 65 72 65 20 69 73 20 61 20 73 69 6e 67 6c  There is a singl
3840: 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 77 68  e.** argument wh
3850: 69 63 68 20 69 73 20 74 68 65 20 75 6e 63 6f 6d  ich is the uncom
3860: 70 72 65 73 73 65 64 20 63 6f 6e 74 65 6e 74 20  pressed content 
3870: 6f 66 20 61 6e 20 22 78 66 65 72 22 20 6d 65 73  of an "xfer" mes
3880: 73 61 67 65 0a 2a 2a 20 66 72 6f 6d 20 63 6c 69  sage.** from cli
3890: 65 6e 74 20 74 6f 20 73 65 72 76 65 72 2e 20 20  ent to server.  
38a0: 54 68 69 73 20 63 6f 6d 6d 61 6e 64 20 69 6e 74  This command int
38b0: 65 72 70 72 65 74 73 20 74 68 61 74 20 6d 65 73  erprets that mes
38c0: 73 61 67 65 20 61 73 0a 2a 2a 20 69 66 20 68 61  sage as.** if ha
38d0: 64 20 62 65 65 6e 20 72 65 63 65 69 76 65 64 20  d been received 
38e0: 62 79 20 74 68 65 20 73 65 72 76 65 72 2e 0a 2a  by the server..*
38f0: 2a 0a 2a 2a 20 4f 6e 20 74 68 65 20 63 6c 69 65  *.** On the clie
3900: 6e 74 20 73 69 64 65 2c 20 72 75 6e 3a 0a 2a 2a  nt side, run:.**
3910: 0a 2a 2a 20 20 20 20 20 20 66 6f 73 73 69 6c 20  .**      fossil 
3920: 70 75 73 68 20 68 74 74 70 3a 2f 2f 62 6f 67 75  push http://bogu
3930: 73 2f 20 2d 2d 68 74 74 70 74 72 61 63 65 0a 2a  s/ --httptrace.*
3940: 2a 0a 2a 2a 20 4f 72 20 61 20 73 69 6d 69 6c 61  *.** Or a simila
3950: 72 20 63 6f 6d 6d 61 6e 64 20 74 6f 20 70 72 6f  r command to pro
3960: 76 69 64 65 20 74 68 65 20 6f 75 74 70 75 74 2e  vide the output.
3970: 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66    The content of
3980: 20 74 68 65 0a 2a 2a 20 6d 65 73 73 61 67 65 20   the.** message 
3990: 77 69 6c 6c 20 61 70 70 65 61 72 20 6f 6e 20 73  will appear on s
39a0: 74 61 6e 64 61 72 64 20 6f 75 74 70 75 74 2e 20  tandard output. 
39b0: 20 43 61 70 74 75 72 65 20 74 68 69 73 20 6d 65   Capture this me
39c0: 73 73 61 67 65 0a 2a 2a 20 69 6e 74 6f 20 61 20  ssage.** into a 
39d0: 66 69 6c 65 20 6e 61 6d 65 64 20 28 66 6f 72 20  file named (for 
39e0: 65 78 61 6d 70 6c 65 29 20 6f 75 74 2e 74 78 74  example) out.txt
39f0: 2e 20 20 54 68 65 6e 20 72 75 6e 20 74 68 65 0a  .  Then run the.
3a00: 2a 2a 20 73 65 72 76 65 72 20 69 6e 20 67 64 62  ** server in gdb
3a10: 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 67 64 62 20  :.**.**     gdb 
3a20: 66 6f 73 73 69 6c 0a 2a 2a 20 20 20 20 20 72 20  fossil.**     r 
3a30: 74 65 73 74 2d 78 66 65 72 20 6f 75 74 2e 74 78  test-xfer out.tx
3a40: 74 0a 2a 2f 0a 76 6f 69 64 20 63 6d 64 5f 74 65  t.*/.void cmd_te
3a50: 73 74 5f 78 66 65 72 28 76 6f 69 64 29 7b 0a 20  st_xfer(void){. 
3a60: 20 69 6e 74 20 6e 6f 74 55 73 65 64 3b 0a 20 20   int notUsed;.  
3a70: 69 66 28 20 67 2e 61 72 67 63 21 3d 32 20 26 26  if( g.argc!=2 &&
3a80: 20 67 2e 61 72 67 63 21 3d 33 20 29 7b 0a 20 20   g.argc!=3 ){.  
3a90: 20 20 75 73 61 67 65 28 22 3f 4d 45 53 53 41 47    usage("?MESSAG
3aa0: 45 46 49 4c 45 3f 22 29 3b 0a 20 20 7d 0a 20 20  EFILE?");.  }.  
3ab0: 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68 69  db_must_be_withi
3ac0: 6e 5f 74 72 65 65 28 29 3b 0a 20 20 62 6c 6f 62  n_tree();.  blob
3ad0: 5f 7a 65 72 6f 28 26 67 2e 63 67 69 49 6e 29 3b  _zero(&g.cgiIn);
3ae0: 0a 20 20 62 6c 6f 62 5f 72 65 61 64 5f 66 72 6f  .  blob_read_fro
3af0: 6d 5f 66 69 6c 65 28 26 67 2e 63 67 69 49 6e 2c  m_file(&g.cgiIn,
3b00: 20 67 2e 61 72 67 63 3d 3d 32 20 3f 20 22 2d 22   g.argc==2 ? "-"
3b10: 20 3a 20 67 2e 61 72 67 76 5b 32 5d 29 3b 0a 20   : g.argv[2]);. 
3b20: 20 64 69 73 61 62 6c 65 4c 6f 67 69 6e 20 3d 20   disableLogin = 
3b30: 31 3b 0a 20 20 70 61 67 65 5f 78 66 65 72 28 29  1;.  page_xfer()
3b40: 3b 0a 20 20 70 72 69 6e 74 66 28 22 25 73 5c 6e  ;.  printf("%s\n
3b50: 22 2c 20 63 67 69 5f 65 78 74 72 61 63 74 5f 63  ", cgi_extract_c
3b60: 6f 6e 74 65 6e 74 28 26 6e 6f 74 55 73 65 64 29  ontent(&notUsed)
3b70: 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e  );.}.../*.** Syn
3b80: 63 20 74 6f 20 74 68 65 20 68 6f 73 74 20 69 64  c to the host id
3b90: 65 6e 74 69 66 69 65 64 20 69 6e 20 67 2e 75 72  entified in g.ur
3ba0: 6c 4e 61 6d 65 20 61 6e 64 20 67 2e 75 72 6c 50  lName and g.urlP
3bb0: 61 74 68 2e 20 20 54 68 69 73 0a 2a 2a 20 72 6f  ath.  This.** ro
3bc0: 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
3bd0: 62 79 20 74 68 65 20 63 6c 69 65 6e 74 2e 0a 2a  by the client..*
3be0: 2a 0a 2a 2a 20 52 65 63 6f 72 64 73 20 61 72 65  *.** Records are
3bf0: 20 70 75 73 68 65 64 20 74 6f 20 74 68 65 20 73   pushed to the s
3c00: 65 72 76 65 72 20 69 66 20 70 75 73 68 46 6c 61  erver if pushFla
3c10: 67 20 69 73 20 74 72 75 65 2e 20 20 52 65 63 6f  g is true.  Reco
3c20: 72 64 73 0a 2a 2a 20 61 72 65 20 70 75 6c 6c 65  rds.** are pulle
3c30: 64 20 69 66 20 70 75 6c 6c 46 6c 61 67 20 69 73  d if pullFlag is
3c40: 20 74 72 75 65 2e 20 20 41 20 66 75 6c 6c 20 73   true.  A full s
3c50: 79 6e 63 20 6f 63 63 75 72 73 20 69 66 20 62 6f  ync occurs if bo
3c60: 74 68 20 61 72 65 0a 2a 2a 20 74 72 75 65 2e 0a  th are.** true..
3c70: 2a 2f 0a 76 6f 69 64 20 63 6c 69 65 6e 74 5f 73  */.void client_s
3c80: 79 6e 63 28 69 6e 74 20 70 75 73 68 46 6c 61 67  ync(int pushFlag
3c90: 2c 20 69 6e 74 20 70 75 6c 6c 46 6c 61 67 2c 20  , int pullFlag, 
3ca0: 69 6e 74 20 63 6c 6f 6e 65 46 6c 61 67 29 7b 0a  int cloneFlag){.
3cb0: 20 20 69 6e 74 20 67 6f 20 3d 20 31 3b 20 20 20    int go = 1;   
3cc0: 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 75 6e 74       /* Loop unt
3cd0: 69 6c 20 7a 65 72 6f 20 2a 2f 0a 20 20 69 6e 74  il zero */.  int
3ce0: 20 6e 54 6f 6b 65 6e 3b 0a 20 20 63 6f 6e 73 74   nToken;.  const
3cf0: 20 63 68 61 72 20 2a 7a 53 43 6f 64 65 20 3d 20   char *zSCode = 
3d00: 64 62 5f 67 65 74 28 22 73 65 72 76 65 72 2d 63  db_get("server-c
3d10: 6f 64 65 22 2c 20 22 78 22 29 3b 0a 20 20 63 6f  ode", "x");.  co
3d20: 6e 73 74 20 63 68 61 72 20 2a 7a 50 43 6f 64 65  nst char *zPCode
3d30: 20 3d 20 64 62 5f 67 65 74 28 22 70 72 6f 6a 65   = db_get("proje
3d40: 63 74 2d 63 6f 64 65 22 2c 20 30 29 3b 0a 20 20  ct-code", 0);.  
3d50: 69 6e 74 20 6e 46 69 6c 65 20 3d 20 30 3b 0a 20  int nFile = 0;. 
3d60: 20 69 6e 74 20 6e 4d 73 67 20 3d 20 30 3b 0a 20   int nMsg = 0;. 
3d70: 20 69 6e 74 20 6e 52 65 71 20 3d 20 30 3b 0a 20   int nReq = 0;. 
3d80: 20 69 6e 74 20 6e 46 69 6c 65 53 65 6e 64 3b 0a   int nFileSend;.
3d90: 20 20 69 6e 74 20 6e 4e 6f 46 69 6c 65 43 79 63    int nNoFileCyc
3da0: 6c 65 20 3d 20 30 3b 0a 20 20 42 6c 6f 62 20 73  le = 0;.  Blob s
3db0: 65 6e 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  end;        /* T
3dc0: 65 78 74 20 77 65 20 61 72 65 20 73 65 6e 64 69  ext we are sendi
3dd0: 6e 67 20 74 6f 20 74 68 65 20 73 65 72 76 65 72  ng to the server
3de0: 20 2a 2f 0a 20 20 42 6c 6f 62 20 72 65 63 76 3b   */.  Blob recv;
3df0: 20 20 20 20 20 20 20 20 2f 2a 20 52 65 70 6c 79          /* Reply
3e00: 20 77 65 20 67 6f 74 20 62 61 63 6b 20 66 72 6f   we got back fro
3e10: 6d 20 74 68 65 20 73 65 72 76 65 72 20 2a 2f 0a  m the server */.
3e20: 20 20 42 6c 6f 62 20 6c 69 6e 65 3b 20 20 20 20    Blob line;    
3e30: 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20      /* A single 
3e40: 6c 69 6e 65 20 6f 66 20 74 68 65 20 72 65 70 6c  line of the repl
3e50: 79 20 2a 2f 0a 20 20 42 6c 6f 62 20 61 54 6f 6b  y */.  Blob aTok
3e60: 65 6e 5b 35 5d 3b 20 20 20 2f 2a 20 41 20 74 6f  en[5];   /* A to
3e70: 6b 65 6e 69 7a 61 74 69 6f 6e 20 6f 66 20 6c 69  kenization of li
3e80: 6e 65 20 2a 2f 0a 20 20 42 6c 6f 62 20 65 72 72  ne */.  Blob err
3e90: 6d 73 67 3b 20 20 20 20 20 20 2f 2a 20 45 72 72  msg;      /* Err
3ea0: 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f 0a 0a 20  or message */.. 
3eb0: 20 61 73 73 65 72 74 28 20 70 75 73 68 46 6c 61   assert( pushFla
3ec0: 67 20 7c 7c 20 70 75 6c 6c 46 6c 61 67 20 7c 7c  g || pullFlag ||
3ed0: 20 63 6c 6f 6e 65 46 6c 61 67 20 29 3b 0a 20 20   cloneFlag );.  
3ee0: 61 73 73 65 72 74 28 20 21 67 2e 75 72 6c 49 73  assert( !g.urlIs
3ef0: 46 69 6c 65 20 29 3b 20 20 20 20 20 20 20 20 20  File );         
3f00: 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20 77 6f   /* This only wo
3f10: 72 6b 73 20 66 6f 72 20 6e 65 74 77 6f 72 6b 69  rks for networki
3f20: 6e 67 20 2a 2f 0a 0a 20 20 64 62 5f 62 65 67 69  ng */..  db_begi
3f30: 6e 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 29 3b  n_transaction();
3f40: 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63  .  db_multi_exec
3f50: 28 0a 20 20 20 20 2f 2a 20 52 65 63 6f 72 64 73  (.    /* Records
3f60: 20 77 68 69 63 68 20 77 65 20 6b 6e 6f 77 20 74   which we know t
3f70: 68 65 20 6f 74 68 65 72 20 73 69 64 65 20 61 6c  he other side al
3f80: 73 6f 20 68 61 73 20 2a 2f 0a 20 20 20 20 22 43  so has */.    "C
3f90: 52 45 41 54 45 20 54 45 4d 50 20 54 41 42 4c 45  REATE TEMP TABLE
3fa0: 20 6f 6e 72 65 6d 6f 74 65 28 72 69 64 20 49 4e   onremote(rid IN
3fb0: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
3fc0: 59 29 3b 22 0a 20 20 20 20 2f 2a 20 52 65 63 6f  Y);".    /* Reco
3fd0: 72 64 73 20 77 65 20 6b 6e 6f 77 20 74 68 65 20  rds we know the 
3fe0: 6f 74 68 65 72 20 73 69 64 65 20 6e 65 65 64 73  other side needs
3ff0: 20 2a 2f 0a 20 20 20 20 22 43 52 45 41 54 45 20   */.    "CREATE 
4000: 54 45 4d 50 20 54 41 42 4c 45 20 70 65 6e 64 69  TEMP TABLE pendi
4010: 6e 67 28 72 69 64 20 49 4e 54 45 47 45 52 20 50  ng(rid INTEGER P
4020: 52 49 4d 41 52 59 20 4b 45 59 29 3b 22 0a 20 20  RIMARY KEY);".  
4030: 29 3b 0a 20 20 62 6c 6f 62 61 72 72 61 79 5f 7a  );.  blobarray_z
4040: 65 72 6f 28 61 54 6f 6b 65 6e 2c 20 63 6f 75 6e  ero(aToken, coun
4050: 74 28 61 54 6f 6b 65 6e 29 29 3b 0a 20 20 62 6c  t(aToken));.  bl
4060: 6f 62 5f 7a 65 72 6f 28 26 73 65 6e 64 29 3b 0a  ob_zero(&send);.
4070: 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 72 65 63    blob_zero(&rec
4080: 76 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28  v);.  blob_zero(
4090: 26 65 72 72 6d 73 67 29 3b 0a 0a 0a 20 20 77 68  &errmsg);...  wh
40a0: 69 6c 65 28 20 67 6f 20 29 7b 0a 20 20 20 20 67  ile( go ){.    g
40b0: 6f 20 3d 20 30 3b 0a 20 20 20 20 6e 46 69 6c 65  o = 0;.    nFile
40c0: 20 3d 20 6e 52 65 71 20 3d 20 6e 4d 73 67 20 3d   = nReq = nMsg =
40d0: 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 47 65 6e 65   0;..    /* Gene
40e0: 72 61 74 65 20 61 20 72 65 71 75 65 73 74 20 74  rate a request t
40f0: 6f 20 62 65 20 73 65 6e 74 20 74 6f 20 74 68 65  o be sent to the
4100: 20 73 65 72 76 65 72 2e 0a 20 20 20 20 2a 2a 20   server..    ** 
4110: 41 6c 77 61 79 73 20 62 65 67 69 6e 20 77 69 74  Always begin wit
4120: 68 20 61 20 63 6c 6f 6e 65 2c 20 70 75 6c 6c 2c  h a clone, pull,
4130: 20 6f 72 20 70 75 73 68 20 6d 65 73 73 61 67 65   or push message
4140: 0a 20 20 20 20 2a 2f 0a 20 20 20 20 0a 20 20 20  .    */.    .   
4150: 20 69 66 28 20 63 6c 6f 6e 65 46 6c 61 67 20 29   if( cloneFlag )
4160: 7b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 61 70 70  {.      blob_app
4170: 65 6e 64 66 28 26 73 65 6e 64 2c 20 22 63 6c 6f  endf(&send, "clo
4180: 6e 65 5c 6e 22 29 3b 0a 20 20 20 20 20 20 70 75  ne\n");.      pu
4190: 73 68 46 6c 61 67 20 3d 20 30 3b 0a 20 20 20 20  shFlag = 0;.    
41a0: 20 20 70 75 6c 6c 46 6c 61 67 20 3d 20 30 3b 0a    pullFlag = 0;.
41b0: 20 20 20 20 20 20 6e 4d 73 67 2b 2b 3b 0a 20 20        nMsg++;.  
41c0: 20 20 7d 65 6c 73 65 20 69 66 28 20 70 75 6c 6c    }else if( pull
41d0: 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 62 6c  Flag ){.      bl
41e0: 6f 62 5f 61 70 70 65 6e 64 66 28 26 73 65 6e 64  ob_appendf(&send
41f0: 2c 20 22 70 75 6c 6c 20 25 73 20 25 73 5c 6e 22  , "pull %s %s\n"
4200: 2c 20 7a 53 43 6f 64 65 2c 20 7a 50 43 6f 64 65  , zSCode, zPCode
4210: 29 3b 0a 20 20 20 20 20 20 6e 4d 73 67 2b 2b 3b  );.      nMsg++;
4220: 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
4230: 75 73 68 46 6c 61 67 20 29 7b 0a 20 20 20 20 20  ushFlag ){.     
4240: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 73   blob_appendf(&s
4250: 65 6e 64 2c 20 22 70 75 73 68 20 25 73 20 25 73  end, "push %s %s
4260: 5c 6e 22 2c 20 7a 53 43 6f 64 65 2c 20 7a 50 43  \n", zSCode, zPC
4270: 6f 64 65 29 3b 0a 20 20 20 20 20 20 6e 4d 73 67  ode);.      nMsg
4280: 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69  ++;.    }..    i
4290: 66 28 20 70 75 6c 6c 46 6c 61 67 20 29 7b 0a 20  f( pullFlag ){. 
42a0: 20 20 20 20 20 2f 2a 20 53 65 6e 64 20 67 69 6d       /* Send gim
42b0: 6d 65 20 6d 65 73 73 61 67 65 20 66 6f 72 20 65  me message for e
42c0: 76 65 72 79 20 70 68 61 6e 74 6f 6d 20 74 68 61  very phantom tha
42d0: 74 20 77 65 20 68 6f 6c 64 2e 0a 20 20 20 20 20  t we hold..     
42e0: 20 2a 2f 0a 20 20 20 20 20 20 53 74 6d 74 20 71   */.      Stmt q
42f0: 3b 0a 20 20 20 20 20 20 64 62 5f 70 72 65 70 61  ;.      db_prepa
4300: 72 65 28 26 71 2c 20 22 53 45 4c 45 43 54 20 75  re(&q, "SELECT u
4310: 75 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48  uid FROM blob WH
4320: 45 52 45 20 73 69 7a 65 3c 30 22 29 3b 0a 20 20  ERE size<0");.  
4330: 20 20 20 20 77 68 69 6c 65 28 20 64 62 5f 73 74      while( db_st
4340: 65 70 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52  ep(&q)==SQLITE_R
4350: 4f 57 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f  OW ){.        co
4360: 6e 73 74 20 63 68 61 72 20 2a 7a 55 75 69 64 20  nst char *zUuid 
4370: 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  = db_column_text
4380: 28 26 71 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  (&q, 0);.       
4390: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 73   blob_appendf(&s
43a0: 65 6e 64 2c 22 67 69 6d 6d 65 20 25 73 5c 6e 22  end,"gimme %s\n"
43b0: 2c 20 7a 55 75 69 64 29 3b 0a 20 20 20 20 20 20  , zUuid);.      
43c0: 20 20 6e 52 65 71 2b 2b 3b 0a 20 20 20 20 20 20    nReq++;.      
43d0: 7d 0a 20 20 20 20 20 20 64 62 5f 66 69 6e 61 6c  }.      db_final
43e0: 69 7a 65 28 26 71 29 3b 0a 20 20 20 20 7d 0a 0a  ize(&q);.    }..
43f0: 20 20 20 20 69 66 28 20 70 75 73 68 46 6c 61 67      if( pushFlag
4400: 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53 65 6e   ){.      /* Sen
4410: 64 20 74 68 65 20 73 65 72 76 65 72 20 61 6e 79  d the server any
4420: 20 66 69 6c 65 73 20 74 68 61 74 20 74 68 65 20   files that the 
4430: 73 65 72 76 65 72 20 68 61 73 20 72 65 71 75 65  server has reque
4440: 73 74 65 64 20 2a 2f 0a 20 20 20 20 20 20 6e 46  sted */.      nF
4450: 69 6c 65 20 2b 3d 20 73 65 6e 64 5f 61 6c 6c 5f  ile += send_all_
4460: 70 65 6e 64 69 6e 67 28 26 73 65 6e 64 29 3b 0a  pending(&send);.
4470: 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 70      }..    if( p
4480: 75 6c 6c 46 6c 61 67 20 7c 7c 20 70 75 73 68 46  ullFlag || pushF
4490: 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  lag ){.      /* 
44a0: 41 6c 77 61 79 73 20 73 65 6e 64 20 6f 75 72 20  Always send our 
44b0: 6c 65 61 76 65 73 20 2a 2f 0a 20 20 20 20 20 20  leaves */.      
44c0: 53 74 6d 74 20 71 3b 0a 20 20 20 20 20 20 64 62  Stmt q;.      db
44d0: 5f 70 72 65 70 61 72 65 28 26 71 2c 20 0a 20 20  _prepare(&q, .  
44e0: 20 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 75         "SELECT u
44f0: 75 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48  uid FROM blob WH
4500: 45 52 45 20 72 69 64 20 49 4e 22 0a 20 20 20 20  ERE rid IN".    
4510: 20 20 20 20 20 22 20 20 28 53 45 4c 45 43 54 20       "  (SELECT 
4520: 63 69 64 20 46 52 4f 4d 20 70 6c 69 6e 6b 20 45  cid FROM plink E
4530: 58 43 45 50 54 20 53 45 4c 45 43 54 20 70 69 64  XCEPT SELECT pid
4540: 20 46 52 4f 4d 20 70 6c 69 6e 6b 29 22 0a 20 20   FROM plink)".  
4550: 20 20 20 20 29 3b 0a 20 20 20 20 20 20 77 68 69      );.      whi
4560: 6c 65 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d  le( db_step(&q)=
4570: 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20  =SQLITE_ROW ){. 
4580: 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
4590: 72 20 2a 7a 55 75 69 64 20 3d 20 64 62 5f 63 6f  r *zUuid = db_co
45a0: 6c 75 6d 6e 5f 74 65 78 74 28 26 71 2c 20 30 29  lumn_text(&q, 0)
45b0: 3b 0a 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 61  ;.        blob_a
45c0: 70 70 65 6e 64 66 28 26 73 65 6e 64 2c 20 22 6c  ppendf(&send, "l
45d0: 65 61 66 20 25 73 5c 6e 22 2c 20 7a 55 75 69 64  eaf %s\n", zUuid
45e0: 29 3b 0a 20 20 20 20 20 20 20 20 6e 4d 73 67 2b  );.        nMsg+
45f0: 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  +;.      }.     
4600: 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29   db_finalize(&q)
4610: 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
4620: 45 78 63 68 61 6e 67 65 20 6d 65 73 73 61 67 65  Exchange message
4630: 73 20 77 69 74 68 20 74 68 65 20 73 65 72 76 65  s with the serve
4640: 72 20 2a 2f 0a 20 20 20 20 70 72 69 6e 74 66 28  r */.    printf(
4650: 22 53 65 6e 64 3a 20 20 20 20 20 20 25 33 64 20  "Send:      %3d 
4660: 66 69 6c 65 73 2c 20 25 33 64 20 72 65 71 75 65  files, %3d reque
4670: 73 74 73 2c 20 25 33 64 20 6f 74 68 65 72 20 6d  sts, %3d other m
4680: 73 67 73 2c 20 25 38 64 20 62 79 74 65 73 5c 6e  sgs, %8d bytes\n
4690: 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e  ",.            n
46a0: 46 69 6c 65 2c 20 6e 52 65 71 2c 20 6e 4d 73 67  File, nReq, nMsg
46b0: 2c 20 62 6c 6f 62 5f 73 69 7a 65 28 26 73 65 6e  , blob_size(&sen
46c0: 64 29 29 3b 0a 20 20 20 20 6e 46 69 6c 65 53 65  d));.    nFileSe
46d0: 6e 64 20 3d 20 6e 46 69 6c 65 3b 0a 20 20 20 20  nd = nFile;.    
46e0: 6e 46 69 6c 65 20 3d 20 6e 52 65 71 20 3d 20 6e  nFile = nReq = n
46f0: 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 68 74 74  Msg = 0;.    htt
4700: 70 5f 65 78 63 68 61 6e 67 65 28 26 73 65 6e 64  p_exchange(&send
4710: 2c 20 26 72 65 63 76 29 3b 0a 20 20 20 20 62 6c  , &recv);.    bl
4720: 6f 62 5f 72 65 73 65 74 28 26 73 65 6e 64 29 3b  ob_reset(&send);
4730: 0a 0a 20 20 20 20 2f 2a 20 50 72 6f 63 65 73 73  ..    /* Process
4740: 20 74 68 65 20 72 65 70 6c 79 20 74 68 61 74 20   the reply that 
4750: 63 61 6d 65 20 62 61 63 6b 20 66 72 6f 6d 20 74  came back from t
4760: 68 65 20 73 65 72 76 65 72 20 2a 2f 0a 20 20 20  he server */.   
4770: 20 77 68 69 6c 65 28 20 62 6c 6f 62 5f 6c 69 6e   while( blob_lin
4780: 65 28 26 72 65 63 76 2c 20 26 6c 69 6e 65 29 20  e(&recv, &line) 
4790: 29 7b 0a 20 20 20 20 20 20 6e 54 6f 6b 65 6e 20  ){.      nToken 
47a0: 3d 20 62 6c 6f 62 5f 74 6f 6b 65 6e 69 7a 65 28  = blob_tokenize(
47b0: 26 6c 69 6e 65 2c 20 61 54 6f 6b 65 6e 2c 20 63  &line, aToken, c
47c0: 6f 75 6e 74 28 61 54 6f 6b 65 6e 29 29 3b 0a 0a  ount(aToken));..
47d0: 20 20 20 20 20 20 2f 2a 20 20 20 66 69 6c 65 20        /*   file 
47e0: 55 55 49 44 20 53 49 5a 45 20 5c 6e 20 43 4f 4e  UUID SIZE \n CON
47f0: 54 45 4e 54 0a 20 20 20 20 20 20 2a 2a 20 20 20  TENT.      **   
4800: 66 69 6c 65 20 55 55 49 44 20 44 45 4c 54 41 53  file UUID DELTAS
4810: 52 43 20 53 49 5a 45 20 5c 6e 20 43 4f 4e 54 45  RC SIZE \n CONTE
4820: 4e 54 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  NT.      **.    
4830: 20 20 2a 2a 20 52 65 63 65 69 76 65 20 61 20 66    ** Receive a f
4840: 69 6c 65 20 74 72 61 6e 73 6d 69 74 74 65 64 20  ile transmitted 
4850: 66 72 6f 6d 20 74 68 65 20 6f 74 68 65 72 20 73  from the other s
4860: 69 64 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ide.      */.   
4870: 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26     if( blob_eq(&
4880: 61 54 6f 6b 65 6e 5b 30 5d 2c 22 66 69 6c 65 22  aToken[0],"file"
4890: 29 20 29 7b 0a 20 20 20 20 20 20 20 20 78 66 65  ) ){.        xfe
48a0: 72 5f 61 63 63 65 70 74 5f 66 69 6c 65 28 26 72  r_accept_file(&r
48b0: 65 63 76 2c 20 61 54 6f 6b 65 6e 2c 20 6e 54 6f  ecv, aToken, nTo
48c0: 6b 65 6e 2c 20 26 65 72 72 6d 73 67 29 3b 0a 20  ken, &errmsg);. 
48d0: 20 20 20 20 20 20 20 6e 46 69 6c 65 2b 2b 3b 0a         nFile++;.
48e0: 20 20 20 20 20 20 20 20 67 6f 20 3d 20 31 3b 0a          go = 1;.
48f0: 20 20 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20        }else..   
4900: 20 20 20 2f 2a 20 20 20 67 69 6d 6d 65 20 55 55     /*   gimme UU
4910: 49 44 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  ID.      **.    
4920: 20 20 2a 2a 20 53 65 72 76 65 72 20 69 73 20 72    ** Server is r
4930: 65 71 75 65 73 74 69 6e 67 20 61 20 66 69 6c 65  equesting a file
4940: 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
4950: 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f  if( blob_eq(&aTo
4960: 6b 65 6e 5b 30 5d 2c 20 22 67 69 6d 6d 65 22 29  ken[0], "gimme")
4970: 20 26 26 20 6e 54 6f 6b 65 6e 3d 3d 32 0a 20 20   && nToken==2.  
4980: 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20               && 
4990: 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26 61 54  blob_is_uuid(&aT
49a0: 6f 6b 65 6e 5b 31 5d 29 20 29 7b 0a 20 20 20 20  oken[1]) ){.    
49b0: 20 20 20 20 6e 52 65 71 2b 2b 3b 0a 20 20 20 20      nReq++;.    
49c0: 20 20 20 20 69 66 28 20 70 75 73 68 46 6c 61 67      if( pushFlag
49d0: 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62   ){.          db
49e0: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20  _multi_exec(.   
49f0: 20 20 20 20 20 20 20 20 20 22 49 4e 53 45 52 54           "INSERT
4a00: 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20   OR IGNORE INTO 
4a10: 70 65 6e 64 69 6e 67 28 72 69 64 29 20 22 0a 20  pending(rid) ". 
4a20: 20 20 20 20 20 20 20 20 20 20 20 22 53 45 4c 45             "SELE
4a30: 43 54 20 72 69 64 20 46 52 4f 4d 20 62 6c 6f 62  CT rid FROM blob
4a40: 20 57 48 45 52 45 20 75 75 69 64 3d 25 42 20 41   WHERE uuid=%B A
4a50: 4e 44 20 73 69 7a 65 3e 3d 30 22 2c 20 26 61 54  ND size>=0", &aT
4a60: 6f 6b 65 6e 5b 31 5d 0a 20 20 20 20 20 20 20 20  oken[1].        
4a70: 20 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67    );.          g
4a80: 6f 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  o = 1;.        }
4a90: 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a 20 20 0a  .      }else.  .
4aa0: 20 20 20 20 20 20 2f 2a 20 20 20 69 67 6f 74 20        /*   igot 
4ab0: 55 55 49 44 0a 20 20 20 20 20 20 2a 2a 20 20 20  UUID.      **   
4ac0: 6c 65 61 66 20 55 55 49 44 0a 20 20 20 20 20 20  leaf UUID.      
4ad0: 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 53 65 72 76  **.      ** Serv
4ae0: 65 72 20 70 72 6f 63 6c 61 69 6d 73 20 74 68 61  er proclaims tha
4af0: 74 20 69 74 20 68 61 73 20 61 20 70 61 72 74 69  t it has a parti
4b00: 63 75 6c 61 72 20 66 69 6c 65 2e 20 20 41 20 6c  cular file.  A l
4b10: 65 61 66 20 6d 65 73 73 61 67 65 0a 20 20 20 20  eaf message.    
4b20: 20 20 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 20    ** means that 
4b30: 74 68 65 20 66 69 6c 65 20 69 73 20 61 20 6c 65  the file is a le
4b40: 61 66 20 6d 61 6e 69 66 65 73 74 20 6f 6e 20 74  af manifest on t
4b50: 68 65 20 73 65 72 76 65 72 2e 0a 20 20 20 20 20  he server..     
4b60: 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 6e 54   */.      if( nT
4b70: 6f 6b 65 6e 3d 3d 32 0a 20 20 20 20 20 20 20 20  oken==2.        
4b80: 20 20 20 20 20 20 26 26 20 28 62 6c 6f 62 5f 65        && (blob_e
4b90: 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 69  q(&aToken[0], "i
4ba0: 67 6f 74 22 29 20 7c 7c 20 62 6c 6f 62 5f 65 71  got") || blob_eq
4bb0: 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 6c 65  (&aToken[0], "le
4bc0: 61 66 22 29 29 0a 20 20 20 20 20 20 20 20 20 20  af")).          
4bd0: 20 20 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75      && blob_is_u
4be0: 75 69 64 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 20  uid(&aToken[1]) 
4bf0: 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72  ){.        int r
4c00: 69 64 20 3d 20 64 62 5f 69 6e 74 28 30 2c 20 22  id = db_int(0, "
4c10: 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f 4d 20  SELECT rid FROM 
4c20: 62 6c 6f 62 20 57 48 45 52 45 20 75 75 69 64 3d  blob WHERE uuid=
4c30: 25 42 22 2c 20 26 61 54 6f 6b 65 6e 5b 31 5d 29  %B", &aToken[1])
4c40: 3b 0a 20 20 20 20 20 20 20 20 6e 4d 73 67 2b 2b  ;.        nMsg++
4c50: 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 69  ;.        if( ri
4c60: 64 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  d>0 ){.         
4c70: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a   db_multi_exec(.
4c80: 20 20 20 20 20 20 20 20 20 20 20 20 22 49 4e 53              "INS
4c90: 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e  ERT OR IGNORE IN
4ca0: 54 4f 20 6f 6e 72 65 6d 6f 74 65 28 72 69 64 29  TO onremote(rid)
4cb0: 20 56 41 4c 55 45 53 28 25 64 29 22 2c 20 72 69   VALUES(%d)", ri
4cc0: 64 0a 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20  d.          );. 
4cd0: 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20           /* Add 
4ce0: 74 6f 20 74 68 65 20 70 65 6e 64 69 6e 67 20 73  to the pending s
4cf0: 65 74 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20  et all children 
4d00: 6f 66 20 74 68 65 20 73 65 72 76 65 72 27 73 20  of the server's 
4d10: 6c 65 61 76 65 73 20 2a 2f 0a 20 20 20 20 20 20  leaves */.      
4d20: 20 20 20 20 69 66 28 20 70 75 73 68 46 6c 61 67      if( pushFlag
4d30: 20 26 26 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f   && blob_eq(&aTo
4d40: 6b 65 6e 5b 30 5d 2c 20 22 6c 65 61 66 22 29 20  ken[0], "leaf") 
4d50: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 64  ){.            d
4d60: 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20  b_multi_exec(.  
4d70: 20 20 20 20 20 20 20 20 20 20 20 20 22 49 4e 53              "INS
4d80: 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e  ERT OR IGNORE IN
4d90: 54 4f 20 70 65 6e 64 69 6e 67 28 72 69 64 29 20  TO pending(rid) 
4da0: 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ".              
4db0: 22 53 45 4c 45 43 54 20 63 69 64 20 46 52 4f 4d  "SELECT cid FROM
4dc0: 20 70 6c 69 6e 6b 20 57 48 45 52 45 20 70 69 64   plink WHERE pid
4dd0: 3d 25 64 22 2c 20 72 69 64 0a 20 20 20 20 20 20  =%d", rid.      
4de0: 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20        );.       
4df0: 20 20 20 20 20 69 66 28 20 64 62 5f 63 68 61 6e       if( db_chan
4e00: 67 65 73 28 29 3e 30 20 29 7b 0a 20 20 20 20 20  ges()>0 ){.     
4e10: 20 20 20 20 20 20 20 20 20 67 6f 20 3d 20 31 3b           go = 1;
4e20: 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
4e30: 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
4e40: 20 20 20 20 20 69 66 28 20 70 75 6c 6c 46 6c 61       if( pullFla
4e50: 67 20 26 26 20 21 67 6f 20 26 26 20 0a 20 20 20  g && !go && .   
4e60: 20 20 20 20 20 20 20 20 20 20 20 64 62 5f 65 78             db_ex
4e70: 69 73 74 73 28 22 53 45 4c 45 43 54 20 31 20 46  ists("SELECT 1 F
4e80: 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 72  ROM blob WHERE r
4e90: 69 64 3d 25 64 20 41 4e 44 20 73 69 7a 65 3c 30  id=%d AND size<0
4ea0: 22 2c 20 72 69 64 29 20 29 7b 0a 20 20 20 20 20  ", rid) ){.     
4eb0: 20 20 20 20 20 20 20 67 6f 20 3d 20 31 3b 0a 20         go = 1;. 
4ec0: 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
4ed0: 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 75 6c     }else if( pul
4ee0: 6c 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20  lFlag ){.       
4ef0: 20 20 20 67 6f 20 3d 20 31 3b 0a 20 20 20 20 20     go = 1;.     
4f00: 20 20 20 20 20 63 6f 6e 74 65 6e 74 5f 70 75 74       content_put
4f10: 28 30 2c 20 62 6c 6f 62 5f 73 74 72 28 26 61 54  (0, blob_str(&aT
4f20: 6f 6b 65 6e 5b 31 5d 29 29 3b 0a 20 20 20 20 20  oken[1]));.     
4f30: 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
4f40: 0a 20 20 0a 20 20 20 20 20 20 2f 2a 20 20 20 70  .  .      /*   p
4f50: 75 73 68 20 20 53 45 52 56 45 52 43 4f 44 45 20  ush  SERVERCODE 
4f60: 20 50 52 4f 44 55 43 54 43 4f 44 45 0a 20 20 20   PRODUCTCODE.   
4f70: 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 53     **.      ** S
4f80: 68 6f 75 6c 64 20 6f 6e 6c 79 20 68 61 70 70 65  hould only happe
4f90: 6e 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f  n in response to
4fa0: 20 61 20 63 6c 6f 6e 65 2e 0a 20 20 20 20 20 20   a clone..      
4fb0: 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 62 6c 6f  */.      if( blo
4fc0: 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c  b_eq(&aToken[0],
4fd0: 22 70 75 73 68 22 29 20 26 26 20 6e 54 6f 6b 65  "push") && nToke
4fe0: 6e 3d 3d 33 20 26 26 20 63 6c 6f 6e 65 46 6c 61  n==3 && cloneFla
4ff0: 67 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g.              
5000: 26 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28  && blob_is_uuid(
5010: 26 61 54 6f 6b 65 6e 5b 31 5d 29 20 26 26 20 62  &aToken[1]) && b
5020: 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26 61 54 6f  lob_is_uuid(&aTo
5030: 6b 65 6e 5b 32 5d 29 20 29 7b 0a 0a 20 20 20 20  ken[2]) ){..    
5040: 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 5f      if( blob_eq_
5050: 73 74 72 28 26 61 54 6f 6b 65 6e 5b 31 5d 2c 20  str(&aToken[1], 
5060: 7a 53 43 6f 64 65 2c 20 2d 31 29 20 29 7b 0a 20  zSCode, -1) ){. 
5070: 20 20 20 20 20 20 20 20 20 66 6f 73 73 69 6c 5f           fossil_
5080: 66 61 74 61 6c 28 22 73 65 72 76 65 72 20 6c 6f  fatal("server lo
5090: 6f 70 22 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  op");.        }.
50a0: 20 20 20 20 20 20 20 20 6e 4d 73 67 2b 2b 3b 0a          nMsg++;.
50b0: 20 20 20 20 20 20 20 20 69 66 28 20 7a 50 43 6f          if( zPCo
50c0: 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  de==0 ){.       
50d0: 20 20 20 7a 50 43 6f 64 65 20 3d 20 6d 70 72 69     zPCode = mpri
50e0: 6e 74 66 28 22 25 62 22 2c 20 26 61 54 6f 6b 65  ntf("%b", &aToke
50f0: 6e 5b 32 5d 29 3b 0a 20 20 20 20 20 20 20 20 20  n[2]);.         
5100: 20 64 62 5f 73 65 74 28 22 70 72 6f 6a 65 63 74   db_set("project
5110: 2d 63 6f 64 65 22 2c 20 7a 50 43 6f 64 65 29 3b  -code", zPCode);
5120: 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
5130: 20 20 20 63 6c 6f 6e 65 46 6c 61 67 20 3d 20 30     cloneFlag = 0
5140: 3b 0a 20 20 20 20 20 20 20 20 70 75 6c 6c 46 6c  ;.        pullFl
5150: 61 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 65  ag = 1;.      }e
5160: 6c 73 65 0a 0a 20 20 20 20 20 20 2f 2a 20 20 20  lse..      /*   
5170: 65 72 72 6f 72 20 4d 45 53 53 41 47 45 0a 20 20  error MESSAGE.  
5180: 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
5190: 52 65 70 6f 72 74 20 61 6e 20 65 72 72 6f 72 0a  Report an error.
51a0: 20 20 20 20 20 20 2a 2f 20 20 20 20 20 20 20 20        */        
51b0: 0a 20 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f  .      if( blob_
51c0: 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 22 65  eq(&aToken[0],"e
51d0: 72 72 6f 72 22 29 20 26 26 20 6e 54 6f 6b 65 6e  rror") && nToken
51e0: 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20 20 20 63  ==2 ){.        c
51f0: 68 61 72 20 2a 7a 4d 73 67 20 3d 20 62 6c 6f 62  har *zMsg = blob
5200: 5f 74 65 72 6d 69 6e 61 74 65 28 26 61 54 6f 6b  _terminate(&aTok
5210: 65 6e 5b 31 5d 29 3b 0a 20 20 20 20 20 20 20 20  en[1]);.        
5220: 64 65 66 6f 73 73 69 6c 69 7a 65 28 7a 4d 73 67  defossilize(zMsg
5230: 29 3b 0a 20 20 20 20 20 20 20 20 62 6c 6f 62 5f  );.        blob_
5240: 61 70 70 65 6e 64 66 28 26 65 72 72 6d 73 67 2c  appendf(&errmsg,
5250: 20 22 73 65 72 76 65 72 20 73 61 79 73 3a 20 25   "server says: %
5260: 73 22 2c 20 7a 4d 73 67 29 3b 0a 20 20 20 20 20  s", zMsg);.     
5270: 20 7d 65 6c 73 65 0a 0a 20 20 20 20 20 20 2f 2a   }else..      /*
5280: 20 55 6e 6b 6e 6f 77 6e 20 6d 65 73 73 61 67 65   Unknown message
5290: 20 2a 2f 0a 20 20 20 20 20 20 7b 0a 20 20 20 20   */.      {.    
52a0: 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66      blob_appendf
52b0: 28 26 65 72 72 6d 73 67 2c 20 22 75 6e 6b 6e 6f  (&errmsg, "unkno
52c0: 77 6e 20 63 6f 6d 6d 61 6e 64 3a 20 25 62 22 2c  wn command: %b",
52d0: 20 26 61 54 6f 6b 65 6e 5b 30 5d 29 3b 0a 20 20   &aToken[0]);.  
52e0: 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28      }..      if(
52f0: 20 62 6c 6f 62 5f 73 69 7a 65 28 26 65 72 72 6d   blob_size(&errm
5300: 73 67 29 20 29 7b 0a 20 20 20 20 20 20 20 20 66  sg) ){.        f
5310: 6f 73 73 69 6c 5f 66 61 74 61 6c 28 22 25 62 22  ossil_fatal("%b"
5320: 2c 20 26 65 72 72 6d 73 67 29 3b 0a 20 20 20 20  , &errmsg);.    
5330: 20 20 7d 0a 20 20 20 20 20 20 62 6c 6f 62 61 72    }.      blobar
5340: 72 61 79 5f 72 65 73 65 74 28 61 54 6f 6b 65 6e  ray_reset(aToken
5350: 2c 20 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 7d  , nToken);.    }
5360: 0a 20 20 20 20 70 72 69 6e 74 66 28 22 52 65 63  .    printf("Rec
5370: 65 69 76 65 64 3a 20 20 25 33 64 20 66 69 6c 65  eived:  %3d file
5380: 73 2c 20 25 33 64 20 72 65 71 75 65 73 74 73 2c  s, %3d requests,
5390: 20 25 33 64 20 6f 74 68 65 72 20 6d 73 67 73 2c   %3d other msgs,
53a0: 20 25 38 64 20 62 79 74 65 73 5c 6e 22 2c 0a 20   %8d bytes\n",. 
53b0: 20 20 20 20 20 20 20 20 20 20 20 6e 46 69 6c 65             nFile
53c0: 2c 20 6e 52 65 71 2c 20 6e 4d 73 67 2c 20 62 6c  , nReq, nMsg, bl
53d0: 6f 62 5f 73 69 7a 65 28 26 72 65 63 76 29 29 3b  ob_size(&recv));
53e0: 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28  .    blob_reset(
53f0: 26 72 65 63 76 29 3b 0a 20 20 20 20 69 66 28 20  &recv);.    if( 
5400: 6e 46 69 6c 65 53 65 6e 64 20 2b 20 6e 46 69 6c  nFileSend + nFil
5410: 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e 4e  e==0 ){.      nN
5420: 6f 46 69 6c 65 43 79 63 6c 65 2b 2b 3b 0a 20 20  oFileCycle++;.  
5430: 20 20 20 20 69 66 28 20 6e 4e 6f 46 69 6c 65 43      if( nNoFileC
5440: 79 63 6c 65 3e 31 20 29 7b 0a 20 20 20 20 20 20  ycle>1 ){.      
5450: 20 20 67 6f 20 3d 20 30 3b 0a 20 20 20 20 20 20    go = 0;.      
5460: 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
5470: 20 20 20 6e 4e 6f 46 69 6c 65 43 79 63 6c 65 20     nNoFileCycle 
5480: 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e  = 0;.    }.    n
5490: 46 69 6c 65 20 3d 20 6e 52 65 71 20 3d 20 6e 4d  File = nReq = nM
54a0: 73 67 20 3d 20 30 3b 0a 20 20 7d 3b 0a 20 20 68  sg = 0;.  };.  h
54b0: 74 74 70 5f 63 6c 6f 73 65 28 29 3b 0a 20 20 64  ttp_close();.  d
54c0: 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f  b_end_transactio
54d0: 6e 28 30 29 3b 0a 20 20 64 62 5f 6d 75 6c 74 69  n(0);.  db_multi
54e0: 5f 65 78 65 63 28 0a 20 20 20 20 22 44 52 4f 50  _exec(.    "DROP
54f0: 20 54 41 42 4c 45 20 6f 6e 72 65 6d 6f 74 65 3b   TABLE onremote;
5500: 22 0a 20 20 20 20 22 44 52 4f 50 20 54 41 42 4c  ".    "DROP TABL
5510: 45 20 70 65 6e 64 69 6e 67 3b 22 0a 20 20 29 3b  E pending;".  );
5520: 0a 7d 0a                                         .}.