Hex Artifact Content
Not logged in

Artifact b02eb5519cbcdf339bf5058826b272506c1f575d:

File src/xfer.c part of check-in [651c75c5b7] - merge trunk into creole by robert on 2009-10-05 10:08:05. Also file src/xfer.c part of check-in [469efd6a7c] - Disable the check of the "server-code" on a sync. This means that two repositories with the same "server-code" can sync with each other, which allows a repository to be copied using an ordinary file copy, and without having to run clone. by drh on 2009-10-01 14:47:54.

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 68 69 73 20 73 74 72  ../*.** This str
03e0: 75 63 74 75 72 65 20 68 6f 6c 64 73 20 69 6e 66  ucture holds inf
03f0: 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74  ormation about t
0400: 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 65  he current state
0410: 20 6f 66 20 65 69 74 68 65 72 0a 2a 2a 20 61 20   of either.** a 
0420: 63 6c 69 65 6e 74 20 6f 72 20 61 20 73 65 72 76  client or a serv
0430: 65 72 20 74 68 61 74 20 69 73 20 70 61 72 74 69  er that is parti
0440: 63 69 70 61 74 69 6e 67 20 69 6e 20 78 66 65 72  cipating in xfer
0450: 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
0460: 75 63 74 20 58 66 65 72 20 58 66 65 72 3b 0a 73  uct Xfer Xfer;.s
0470: 74 72 75 63 74 20 58 66 65 72 20 7b 0a 20 20 42  truct Xfer {.  B
0480: 6c 6f 62 20 2a 70 49 6e 3b 20 20 20 20 20 20 20  lob *pIn;       
0490: 20 20 20 2f 2a 20 49 6e 70 75 74 20 74 65 78 74     /* Input text
04a0: 20 66 72 6f 6d 20 74 68 65 20 6f 74 68 65 72 20   from the other 
04b0: 73 69 64 65 20 2a 2f 0a 20 20 42 6c 6f 62 20 2a  side */.  Blob *
04c0: 70 4f 75 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  pOut;         /*
04d0: 20 43 6f 6d 70 6f 73 65 20 6f 75 72 20 72 65 70   Compose our rep
04e0: 6c 79 20 68 65 72 65 20 2a 2f 0a 20 20 42 6c 6f  ly here */.  Blo
04f0: 62 20 6c 69 6e 65 3b 20 20 20 20 20 20 20 20 20  b line;         
0500: 20 2f 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20   /* The current 
0510: 6c 69 6e 65 20 6f 66 20 69 6e 70 75 74 20 2a 2f  line of input */
0520: 0a 20 20 42 6c 6f 62 20 61 54 6f 6b 65 6e 5b 35  .  Blob aToken[5
0530: 5d 3b 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69  ];     /* Tokeni
0540: 7a 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 6c  zed version of l
0550: 69 6e 65 20 2a 2f 0a 20 20 42 6c 6f 62 20 65 72  ine */.  Blob er
0560: 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  r;           /* 
0570: 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65  Error message te
0580: 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 6b  xt */.  int nTok
0590: 65 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  en;         /* N
05a0: 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20  umber of tokens 
05b0: 69 6e 20 6c 69 6e 65 20 2a 2f 0a 20 20 69 6e 74  in line */.  int
05c0: 20 6e 49 47 6f 74 53 65 6e 74 3b 20 20 20 20 20   nIGotSent;     
05d0: 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 22 69   /* Number of "i
05e0: 67 6f 74 22 20 63 61 72 64 73 20 73 65 6e 74 20  got" cards sent 
05f0: 2a 2f 0a 20 20 69 6e 74 20 6e 47 69 6d 6d 65 53  */.  int nGimmeS
0600: 65 6e 74 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62  ent;     /* Numb
0610: 65 72 20 6f 66 20 67 69 6d 6d 65 20 63 61 72 64  er of gimme card
0620: 73 20 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20  s sent */.  int 
0630: 6e 46 69 6c 65 53 65 6e 74 3b 20 20 20 20 20 20  nFileSent;      
0640: 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 69 6c  /* Number of fil
0650: 65 73 20 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74  es sent */.  int
0660: 20 6e 44 65 6c 74 61 53 65 6e 74 3b 20 20 20 20   nDeltaSent;    
0670: 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 65   /* Number of de
0680: 6c 74 61 73 20 73 65 6e 74 20 2a 2f 0a 20 20 69  ltas sent */.  i
0690: 6e 74 20 6e 46 69 6c 65 52 63 76 64 3b 20 20 20  nt nFileRcvd;   
06a0: 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
06b0: 66 69 6c 65 73 20 72 65 63 65 69 76 65 64 20 2a  files received *
06c0: 2f 0a 20 20 69 6e 74 20 6e 44 65 6c 74 61 52 63  /.  int nDeltaRc
06d0: 76 64 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  vd;     /* Numbe
06e0: 72 20 6f 66 20 64 65 6c 74 61 73 20 72 65 63 65  r of deltas rece
06f0: 69 76 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 44  ived */.  int nD
0700: 61 6e 67 6c 69 6e 67 46 69 6c 65 3b 20 20 2f 2a  anglingFile;  /*
0710: 20 4e 75 6d 62 65 72 20 6f 66 20 64 61 6e 67 6c   Number of dangl
0720: 69 6e 67 20 64 65 6c 74 61 73 20 72 65 63 65 69  ing deltas recei
0730: 76 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 53  ved */.  int mxS
0740: 65 6e 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  end;         /* 
0750: 53 74 6f 70 20 73 65 6e 64 69 6e 67 20 22 66 69  Stop sending "fi
0760: 6c 65 22 20 77 69 74 68 20 70 4f 75 74 20 72 65  le" with pOut re
0770: 61 63 68 65 73 20 74 68 69 73 20 73 69 7a 65 20  aches this size 
0780: 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68  */.};.../*.** Th
0790: 65 20 69 6e 70 75 74 20 62 6c 6f 62 20 63 6f 6e  e input blob con
07a0: 74 61 69 6e 73 20 61 20 55 55 49 44 2e 20 20 43  tains a UUID.  C
07b0: 6f 6e 76 65 72 74 20 69 74 20 69 6e 74 6f 20 61  onvert it into a
07c0: 20 72 65 63 6f 72 64 20 49 44 2e 0a 2a 2a 20 43   record ID..** C
07d0: 72 65 61 74 65 20 61 20 70 68 61 6e 74 6f 6d 20  reate a phantom 
07e0: 72 65 63 6f 72 64 20 69 66 20 6e 6f 20 70 72 69  record if no pri
07f0: 6f 72 20 72 65 63 6f 72 64 20 65 78 69 73 74 73  or record exists
0800: 20 61 6e 64 0a 2a 2a 20 70 68 61 6e 74 6f 6d 69   and.** phantomi
0810: 7a 65 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a  ze is true..**.*
0820: 2a 20 43 6f 6d 70 61 72 65 20 74 6f 20 75 75 69  * Compare to uui
0830: 64 5f 74 6f 5f 72 69 64 28 29 2e 20 20 54 68 69  d_to_rid().  Thi
0840: 73 20 72 6f 75 74 69 6e 65 20 74 61 6b 65 73 20  s routine takes 
0850: 61 20 62 6c 6f 62 20 61 72 67 75 6d 65 6e 74 0a  a blob argument.
0860: 2a 2a 20 61 6e 64 20 64 6f 65 73 20 6c 65 73 73  ** and does less
0870: 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 2e   error checking.
0880: 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
0890: 69 64 5f 66 72 6f 6d 5f 75 75 69 64 28 42 6c 6f  id_from_uuid(Blo
08a0: 62 20 2a 70 55 75 69 64 2c 20 69 6e 74 20 70 68  b *pUuid, int ph
08b0: 61 6e 74 6f 6d 69 7a 65 29 7b 0a 20 20 73 74 61  antomize){.  sta
08c0: 74 69 63 20 53 74 6d 74 20 71 3b 0a 20 20 69 6e  tic Stmt q;.  in
08d0: 74 20 72 69 64 3b 0a 0a 20 20 64 62 5f 73 74 61  t rid;..  db_sta
08e0: 74 69 63 5f 70 72 65 70 61 72 65 28 26 71 2c 20  tic_prepare(&q, 
08f0: 22 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f 4d  "SELECT rid FROM
0900: 20 62 6c 6f 62 20 57 48 45 52 45 20 75 75 69 64   blob WHERE uuid
0910: 3d 3a 75 75 69 64 22 29 3b 0a 20 20 64 62 5f 62  =:uuid");.  db_b
0920: 69 6e 64 5f 73 74 72 28 26 71 2c 20 22 3a 75 75  ind_str(&q, ":uu
0930: 69 64 22 2c 20 70 55 75 69 64 29 3b 0a 20 20 69  id", pUuid);.  i
0940: 66 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d  f( db_step(&q)==
0950: 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
0960: 20 20 72 69 64 20 3d 20 64 62 5f 63 6f 6c 75 6d    rid = db_colum
0970: 6e 5f 69 6e 74 28 26 71 2c 20 30 29 3b 0a 20 20  n_int(&q, 0);.  
0980: 7d 65 6c 73 65 7b 0a 20 20 20 20 72 69 64 20 3d  }else{.    rid =
0990: 20 30 3b 0a 20 20 7d 0a 20 20 64 62 5f 72 65 73   0;.  }.  db_res
09a0: 65 74 28 26 71 29 3b 0a 20 20 69 66 28 20 72 69  et(&q);.  if( ri
09b0: 64 3d 3d 30 20 26 26 20 70 68 61 6e 74 6f 6d 69  d==0 && phantomi
09c0: 7a 65 20 29 7b 0a 20 20 20 20 72 69 64 20 3d 20  ze ){.    rid = 
09d0: 63 6f 6e 74 65 6e 74 5f 6e 65 77 28 62 6c 6f 62  content_new(blob
09e0: 5f 73 74 72 28 70 55 75 69 64 29 29 3b 0a 20 20  _str(pUuid));.  
09f0: 7d 0a 20 20 72 65 74 75 72 6e 20 72 69 64 3b 0a  }.  return rid;.
0a00: 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 65 6d 62 65  }../*.** Remembe
0a10: 72 20 74 68 61 74 20 74 68 65 20 6f 74 68 65 72  r that the other
0a20: 20 73 69 64 65 20 6f 66 20 74 68 65 20 63 6f 6e   side of the con
0a30: 6e 65 63 74 69 6f 6e 20 61 6c 72 65 61 64 79 20  nection already 
0a40: 68 61 73 20 61 20 63 6f 70 79 0a 2a 2a 20 6f 66  has a copy.** of
0a50: 20 74 68 65 20 66 69 6c 65 20 72 69 64 2e 0a 2a   the file rid..*
0a60: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65  /.static void re
0a70: 6d 6f 74 65 5f 68 61 73 28 69 6e 74 20 72 69 64  mote_has(int rid
0a80: 29 7b 0a 20 20 69 66 28 20 72 69 64 20 29 20 64  ){.  if( rid ) d
0a90: 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22 49 4e  b_multi_exec("IN
0aa0: 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49  SERT OR IGNORE I
0ab0: 4e 54 4f 20 6f 6e 72 65 6d 6f 74 65 20 56 41 4c  NTO onremote VAL
0ac0: 55 45 53 28 25 64 29 22 2c 20 72 69 64 29 3b 0a  UES(%d)", rid);.
0ad0: 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 54 6f  }../*.** The aTo
0ae0: 6b 65 6e 5b 30 2e 2e 6e 54 6f 6b 65 6e 2d 31 5d  ken[0..nToken-1]
0af0: 20 62 6c 6f 62 20 61 72 72 61 79 20 69 73 20 61   blob array is a
0b00: 20 70 61 72 73 65 20 6f 66 20 61 20 22 66 69 6c   parse of a "fil
0b10: 65 22 20 6c 69 6e 65 20 0a 2a 2a 20 6d 65 73 73  e" line .** mess
0b20: 61 67 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69  age.  This routi
0b30: 6e 65 20 66 69 6e 69 73 68 65 73 20 70 61 72 73  ne finishes pars
0b40: 69 6e 67 20 74 68 61 74 20 6d 65 73 73 61 67 65  ing that message
0b50: 20 61 6e 64 20 64 6f 65 73 0a 2a 2a 20 61 20 72   and does.** a r
0b60: 65 63 6f 72 64 20 69 6e 73 65 72 74 20 6f 66 20  ecord insert of 
0b70: 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  the file..**.** 
0b80: 54 68 65 20 66 69 6c 65 20 6c 69 6e 65 20 69 73  The file line is
0b90: 20 69 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 66   in one of the f
0ba0: 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 66 6f 72  ollowing two for
0bb0: 6d 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 66  ms:.**.**      f
0bc0: 69 6c 65 20 55 55 49 44 20 53 49 5a 45 20 5c 6e  ile UUID SIZE \n
0bd0: 20 43 4f 4e 54 45 4e 54 0a 2a 2a 20 20 20 20 20   CONTENT.**     
0be0: 20 66 69 6c 65 20 55 55 49 44 20 44 45 4c 54 41   file UUID DELTA
0bf0: 53 52 43 20 53 49 5a 45 20 5c 6e 20 43 4f 4e 54  SRC SIZE \n CONT
0c00: 45 4e 54 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  ENT.**.** The co
0c10: 6e 74 65 6e 74 20 69 73 20 53 49 5a 45 20 62 79  ntent is SIZE by
0c20: 74 65 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  tes immediately 
0c30: 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 6e 65  following the ne
0c40: 77 6c 69 6e 65 2e 0a 2a 2a 20 49 66 20 44 45 4c  wline..** If DEL
0c50: 54 41 53 52 43 20 65 78 69 73 74 73 2c 20 74 68  TASRC exists, th
0c60: 65 6e 20 74 68 65 20 43 4f 4e 54 45 4e 54 20 69  en the CONTENT i
0c70: 73 20 61 20 64 65 6c 74 61 20 61 67 61 69 6e 73  s a delta agains
0c80: 74 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74  t the.** content
0c90: 20 6f 66 20 44 45 4c 54 41 53 52 43 2e 0a 2a 2a   of DELTASRC..**
0ca0: 0a 2a 2a 20 49 66 20 61 6e 79 20 65 72 72 6f 72  .** If any error
0cb0: 20 6f 63 63 75 72 73 2c 20 77 72 69 74 65 20 61   occurs, write a
0cc0: 20 6d 65 73 73 61 67 65 20 69 6e 74 6f 20 70 45   message into pE
0cd0: 72 72 20 77 68 69 63 68 20 68 61 73 20 61 6c 72  rr which has alr
0ce0: 65 61 64 79 0a 2a 2a 20 62 65 20 69 6e 69 74 69  eady.** be initi
0cf0: 61 6c 69 7a 65 64 20 74 6f 20 61 6e 20 65 6d 70  alized to an emp
0d00: 74 79 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a  ty string..**.**
0d10: 20 41 6e 79 20 61 72 74 69 66 61 63 74 20 73 75   Any artifact su
0d20: 63 63 65 73 73 66 75 6c 6c 79 20 72 65 63 65 69  ccessfully recei
0d30: 76 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74  ved by this rout
0d40: 69 6e 65 20 69 73 20 63 6f 6e 73 69 64 65 72 65  ine is considere
0d50: 64 20 74 6f 0a 2a 2a 20 62 65 20 70 75 62 6c 69  d to.** be publi
0d60: 63 20 61 6e 64 20 69 73 20 74 68 65 72 65 66 6f  c and is therefo
0d70: 72 65 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20  re removed from 
0d80: 74 68 65 20 22 70 72 69 76 61 74 65 22 20 74 61  the "private" ta
0d90: 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ble..*/.static v
0da0: 6f 69 64 20 78 66 65 72 5f 61 63 63 65 70 74 5f  oid xfer_accept_
0db0: 66 69 6c 65 28 58 66 65 72 20 2a 70 58 66 65 72  file(Xfer *pXfer
0dc0: 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 69 6e  ){.  int n;.  in
0dd0: 74 20 72 69 64 3b 0a 20 20 69 6e 74 20 73 72 63  t rid;.  int src
0de0: 69 64 20 3d 20 30 3b 0a 20 20 42 6c 6f 62 20 63  id = 0;.  Blob c
0df0: 6f 6e 74 65 6e 74 2c 20 68 61 73 68 3b 0a 20 20  ontent, hash;.  
0e00: 0a 20 20 69 66 28 20 70 58 66 65 72 2d 3e 6e 54  .  if( pXfer->nT
0e10: 6f 6b 65 6e 3c 33 20 0a 20 20 20 7c 7c 20 70 58  oken<3 .   || pX
0e20: 66 65 72 2d 3e 6e 54 6f 6b 65 6e 3e 34 0a 20 20  fer->nToken>4.  
0e30: 20 7c 7c 20 21 62 6c 6f 62 5f 69 73 5f 75 75 69   || !blob_is_uui
0e40: 64 28 26 70 58 66 65 72 2d 3e 61 54 6f 6b 65 6e  d(&pXfer->aToken
0e50: 5b 31 5d 29 0a 20 20 20 7c 7c 20 21 62 6c 6f 62  [1]).   || !blob
0e60: 5f 69 73 5f 69 6e 74 28 26 70 58 66 65 72 2d 3e  _is_int(&pXfer->
0e70: 61 54 6f 6b 65 6e 5b 70 58 66 65 72 2d 3e 6e 54  aToken[pXfer->nT
0e80: 6f 6b 65 6e 2d 31 5d 2c 20 26 6e 29 0a 20 20 20  oken-1], &n).   
0e90: 7c 7c 20 6e 3c 30 0a 20 20 20 7c 7c 20 28 70 58  || n<0.   || (pX
0ea0: 66 65 72 2d 3e 6e 54 6f 6b 65 6e 3d 3d 34 20 26  fer->nToken==4 &
0eb0: 26 20 21 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28  & !blob_is_uuid(
0ec0: 26 70 58 66 65 72 2d 3e 61 54 6f 6b 65 6e 5b 32  &pXfer->aToken[2
0ed0: 5d 29 29 0a 20 20 29 7b 0a 20 20 20 20 62 6c 6f  ])).  ){.    blo
0ee0: 62 5f 61 70 70 65 6e 64 66 28 26 70 58 66 65 72  b_appendf(&pXfer
0ef0: 2d 3e 65 72 72 2c 20 22 6d 61 6c 66 6f 72 6d 65  ->err, "malforme
0f00: 64 20 66 69 6c 65 20 6c 69 6e 65 22 29 3b 0a 20  d file line");. 
0f10: 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
0f20: 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 63 6f 6e 74   blob_zero(&cont
0f30: 65 6e 74 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72  ent);.  blob_zer
0f40: 6f 28 26 68 61 73 68 29 3b 0a 20 20 62 6c 6f 62  o(&hash);.  blob
0f50: 5f 65 78 74 72 61 63 74 28 70 58 66 65 72 2d 3e  _extract(pXfer->
0f60: 70 49 6e 2c 20 6e 2c 20 26 63 6f 6e 74 65 6e 74  pIn, n, &content
0f70: 29 3b 0a 20 20 69 66 28 20 75 75 69 64 5f 69 73  );.  if( uuid_is
0f80: 5f 73 68 75 6e 6e 65 64 28 62 6c 6f 62 5f 73 74  _shunned(blob_st
0f90: 72 28 26 70 58 66 65 72 2d 3e 61 54 6f 6b 65 6e  r(&pXfer->aToken
0fa0: 5b 31 5d 29 29 20 29 7b 0a 20 20 20 20 2f 2a 20  [1])) ){.    /* 
0fb0: 49 67 6e 6f 72 65 20 66 69 6c 65 73 20 74 68 61  Ignore files tha
0fc0: 74 20 68 61 76 65 20 62 65 65 6e 20 73 68 75 6e  t have been shun
0fd0: 6e 65 64 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  ned */.    retur
0fe0: 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 58 66  n;.  }.  if( pXf
0ff0: 65 72 2d 3e 6e 54 6f 6b 65 6e 3d 3d 34 20 29 7b  er->nToken==4 ){
1000: 0a 20 20 20 20 42 6c 6f 62 20 73 72 63 3b 0a 20  .    Blob src;. 
1010: 20 20 20 73 72 63 69 64 20 3d 20 72 69 64 5f 66     srcid = rid_f
1020: 72 6f 6d 5f 75 75 69 64 28 26 70 58 66 65 72 2d  rom_uuid(&pXfer-
1030: 3e 61 54 6f 6b 65 6e 5b 32 5d 2c 20 31 29 3b 0a  >aToken[2], 1);.
1040: 20 20 20 20 69 66 28 20 63 6f 6e 74 65 6e 74 5f      if( content_
1050: 67 65 74 28 73 72 63 69 64 2c 20 26 73 72 63 29  get(srcid, &src)
1060: 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 69 64  ==0 ){.      rid
1070: 20 3d 20 63 6f 6e 74 65 6e 74 5f 70 75 74 28 26   = content_put(&
1080: 63 6f 6e 74 65 6e 74 2c 20 62 6c 6f 62 5f 73 74  content, blob_st
1090: 72 28 26 70 58 66 65 72 2d 3e 61 54 6f 6b 65 6e  r(&pXfer->aToken
10a0: 5b 31 5d 29 2c 20 73 72 63 69 64 29 3b 0a 20 20  [1]), srcid);.  
10b0: 20 20 20 20 70 58 66 65 72 2d 3e 6e 44 61 6e 67      pXfer->nDang
10c0: 6c 69 6e 67 46 69 6c 65 2b 2b 3b 0a 20 20 20 20  lingFile++;.    
10d0: 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28    db_multi_exec(
10e0: 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 68 61  "DELETE FROM pha
10f0: 6e 74 6f 6d 20 57 48 45 52 45 20 72 69 64 3d 25  ntom WHERE rid=%
1100: 64 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 20 20  d", rid);.      
1110: 63 6f 6e 74 65 6e 74 5f 6d 61 6b 65 5f 70 75 62  content_make_pub
1120: 6c 69 63 28 72 69 64 29 3b 0a 20 20 20 20 20 20  lic(rid);.      
1130: 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
1140: 20 20 70 58 66 65 72 2d 3e 6e 44 65 6c 74 61 52    pXfer->nDeltaR
1150: 63 76 64 2b 2b 3b 0a 20 20 20 20 62 6c 6f 62 5f  cvd++;.    blob_
1160: 64 65 6c 74 61 5f 61 70 70 6c 79 28 26 73 72 63  delta_apply(&src
1170: 2c 20 26 63 6f 6e 74 65 6e 74 2c 20 26 63 6f 6e  , &content, &con
1180: 74 65 6e 74 29 3b 0a 20 20 20 20 62 6c 6f 62 5f  tent);.    blob_
1190: 72 65 73 65 74 28 26 73 72 63 29 3b 0a 20 20 7d  reset(&src);.  }
11a0: 65 6c 73 65 7b 0a 20 20 20 20 70 58 66 65 72 2d  else{.    pXfer-
11b0: 3e 6e 46 69 6c 65 52 63 76 64 2b 2b 3b 0a 20 20  >nFileRcvd++;.  
11c0: 7d 0a 20 20 73 68 61 31 73 75 6d 5f 62 6c 6f 62  }.  sha1sum_blob
11d0: 28 26 63 6f 6e 74 65 6e 74 2c 20 26 68 61 73 68  (&content, &hash
11e0: 29 3b 0a 20 20 69 66 28 20 21 62 6c 6f 62 5f 65  );.  if( !blob_e
11f0: 71 5f 73 74 72 28 26 70 58 66 65 72 2d 3e 61 54  q_str(&pXfer->aT
1200: 6f 6b 65 6e 5b 31 5d 2c 20 62 6c 6f 62 5f 73 74  oken[1], blob_st
1210: 72 28 26 68 61 73 68 29 2c 20 2d 31 29 20 29 7b  r(&hash), -1) ){
1220: 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64  .    blob_append
1230: 66 28 26 70 58 66 65 72 2d 3e 65 72 72 2c 20 22  f(&pXfer->err, "
1240: 63 6f 6e 74 65 6e 74 20 64 6f 65 73 20 6e 6f 74  content does not
1250: 20 6d 61 74 63 68 20 73 68 61 31 20 68 61 73 68   match sha1 hash
1260: 22 29 3b 0a 20 20 7d 0a 20 20 72 69 64 20 3d 20  ");.  }.  rid = 
1270: 63 6f 6e 74 65 6e 74 5f 70 75 74 28 26 63 6f 6e  content_put(&con
1280: 74 65 6e 74 2c 20 62 6c 6f 62 5f 73 74 72 28 26  tent, blob_str(&
1290: 68 61 73 68 29 2c 20 30 29 3b 0a 20 20 62 6c 6f  hash), 0);.  blo
12a0: 62 5f 72 65 73 65 74 28 26 68 61 73 68 29 3b 0a  b_reset(&hash);.
12b0: 20 20 69 66 28 20 72 69 64 3d 3d 30 20 29 7b 0a    if( rid==0 ){.
12c0: 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66      blob_appendf
12d0: 28 26 70 58 66 65 72 2d 3e 65 72 72 2c 20 22 25  (&pXfer->err, "%
12e0: 73 22 2c 20 67 2e 7a 45 72 72 4d 73 67 29 3b 0a  s", g.zErrMsg);.
12f0: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 6f 6e    }else{.    con
1300: 74 65 6e 74 5f 6d 61 6b 65 5f 70 75 62 6c 69 63  tent_make_public
1310: 28 72 69 64 29 3b 0a 20 20 20 20 6d 61 6e 69 66  (rid);.    manif
1320: 65 73 74 5f 63 72 6f 73 73 6c 69 6e 6b 28 72 69  est_crosslink(ri
1330: 64 2c 20 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20  d, &content);.  
1340: 7d 0a 20 20 72 65 6d 6f 74 65 5f 68 61 73 28 72  }.  remote_has(r
1350: 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72  id);.}../*.** Tr
1360: 79 20 74 6f 20 73 65 6e 64 20 61 20 66 69 6c 65  y to send a file
1370: 20 61 73 20 61 20 64 65 6c 74 61 20 61 67 61 69   as a delta agai
1380: 6e 73 74 20 69 74 73 20 70 61 72 65 6e 74 2e 0a  nst its parent..
1390: 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c  ** If successful
13a0: 2c 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  , return the num
13b0: 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
13c0: 74 68 65 20 64 65 6c 74 61 2e 0a 2a 2a 20 49 66  the delta..** If
13d0: 20 77 65 20 63 61 6e 6e 6f 74 20 67 65 6e 65 72   we cannot gener
13e0: 61 74 65 20 61 6e 20 61 70 70 72 6f 70 72 69 61  ate an appropria
13f0: 74 65 20 64 65 6c 74 61 2c 20 74 68 65 6e 20 73  te delta, then s
1400: 65 6e 64 0a 2a 2a 20 6e 6f 74 68 69 6e 67 20 61  end.** nothing a
1410: 6e 64 20 72 65 74 75 72 6e 20 7a 65 72 6f 2e 0a  nd return zero..
1420: 2a 2a 0a 2a 2a 20 4e 65 76 65 72 20 73 65 6e 64  **.** Never send
1430: 20 61 20 64 65 6c 74 61 20 61 67 61 69 6e 73 74   a delta against
1440: 20 61 20 70 72 69 76 61 74 65 20 61 72 74 69 66   a private artif
1450: 61 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  act..*/.static i
1460: 6e 74 20 73 65 6e 64 5f 64 65 6c 74 61 5f 70 61  nt send_delta_pa
1470: 72 65 6e 74 28 0a 20 20 58 66 65 72 20 2a 70 58  rent(.  Xfer *pX
1480: 66 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  fer,            
1490: 2f 2a 20 54 68 65 20 74 72 61 6e 73 66 65 72 20  /* The transfer 
14a0: 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74  context */.  int
14b0: 20 72 69 64 2c 20 20 20 20 20 20 20 20 20 20 20   rid,           
14c0: 20 20 20 20 20 2f 2a 20 72 65 63 6f 72 64 20 69       /* record i
14d0: 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 6f  d of the file to
14e0: 20 73 65 6e 64 20 2a 2f 0a 20 20 42 6c 6f 62 20   send */.  Blob 
14f0: 2a 70 43 6f 6e 74 65 6e 74 2c 20 20 20 20 20 20  *pContent,      
1500: 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 74 65 6e     /* The conten
1510: 74 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 6f  t of the file to
1520: 20 73 65 6e 64 20 2a 2f 0a 20 20 42 6c 6f 62 20   send */.  Blob 
1530: 2a 70 55 75 69 64 20 20 20 20 20 20 20 20 20 20  *pUuid          
1540: 20 20 20 2f 2a 20 54 68 65 20 55 55 49 44 20 6f     /* The UUID o
1550: 66 20 74 68 65 20 66 69 6c 65 20 74 6f 20 73 65  f the file to se
1560: 6e 64 20 2a 2f 0a 29 7b 0a 20 20 73 74 61 74 69  nd */.){.  stati
1570: 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 7a  c const char *az
1580: 51 75 65 72 79 5b 5d 20 3d 20 7b 0a 20 20 20 20  Query[] = {.    
1590: 22 53 45 4c 45 43 54 20 70 69 64 20 46 52 4f 4d  "SELECT pid FROM
15a0: 20 70 6c 69 6e 6b 20 78 22 0a 20 20 20 20 22 20   plink x".    " 
15b0: 57 48 45 52 45 20 63 69 64 3d 25 64 22 0a 20 20  WHERE cid=%d".  
15c0: 20 20 22 20 20 20 41 4e 44 20 4e 4f 54 20 45 58    "   AND NOT EX
15d0: 49 53 54 53 28 53 45 4c 45 43 54 20 31 20 46 52  ISTS(SELECT 1 FR
15e0: 4f 4d 20 70 68 61 6e 74 6f 6d 20 57 48 45 52 45  OM phantom WHERE
15f0: 20 72 69 64 3d 70 69 64 29 22 0a 20 20 20 20 22   rid=pid)".    "
1600: 20 20 20 41 4e 44 20 4e 4f 54 20 45 58 49 53 54     AND NOT EXIST
1610: 53 28 53 45 4c 45 43 54 20 31 20 46 52 4f 4d 20  S(SELECT 1 FROM 
1620: 70 6c 69 6e 6b 20 79 22 0a 20 20 20 20 20 20 20  plink y".       
1630: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
1640: 20 57 48 45 52 45 20 79 2e 70 69 64 3d 78 2e 63   WHERE y.pid=x.c
1650: 69 64 20 41 4e 44 20 79 2e 63 69 64 3d 78 2e 70  id AND y.cid=x.p
1660: 69 64 29 22 2c 0a 0a 20 20 20 20 22 53 45 4c 45  id)",..    "SELE
1670: 43 54 20 70 69 64 20 46 52 4f 4d 20 6d 6c 69 6e  CT pid FROM mlin
1680: 6b 20 78 22 0a 20 20 20 20 22 20 57 48 45 52 45  k x".    " WHERE
1690: 20 66 69 64 3d 25 64 22 0a 20 20 20 20 22 20 20   fid=%d".    "  
16a0: 20 41 4e 44 20 4e 4f 54 20 45 58 49 53 54 53 28   AND NOT EXISTS(
16b0: 53 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 70 68  SELECT 1 FROM ph
16c0: 61 6e 74 6f 6d 20 57 48 45 52 45 20 72 69 64 3d  antom WHERE rid=
16d0: 70 69 64 29 22 0a 20 20 20 20 22 20 20 20 41 4e  pid)".    "   AN
16e0: 44 20 4e 4f 54 20 45 58 49 53 54 53 28 53 45 4c  D NOT EXISTS(SEL
16f0: 45 43 54 20 31 20 46 52 4f 4d 20 6d 6c 69 6e 6b  ECT 1 FROM mlink
1700: 20 79 22 0a 20 20 20 20 20 20 20 20 20 20 20 20   y".            
1710: 20 20 20 20 20 20 20 20 20 22 20 20 57 48 45 52           "  WHER
1720: 45 20 79 2e 70 69 64 3d 78 2e 66 69 64 20 41 4e  E y.pid=x.fid AN
1730: 44 20 79 2e 66 69 64 3d 78 2e 70 69 64 29 22 0a  D y.fid=x.pid)".
1740: 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20    };.  int i;.  
1750: 42 6c 6f 62 20 73 72 63 2c 20 64 65 6c 74 61 3b  Blob src, delta;
1760: 0a 20 20 69 6e 74 20 73 69 7a 65 20 3d 20 30 3b  .  int size = 0;
1770: 0a 20 20 69 6e 74 20 73 72 63 49 64 20 3d 20 30  .  int srcId = 0
1780: 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 73 72  ;..  for(i=0; sr
1790: 63 49 64 3d 3d 30 20 26 26 20 69 3c 63 6f 75 6e  cId==0 && i<coun
17a0: 74 28 61 7a 51 75 65 72 79 29 3b 20 69 2b 2b 29  t(azQuery); i++)
17b0: 7b 0a 20 20 20 20 73 72 63 49 64 20 3d 20 64 62  {.    srcId = db
17c0: 5f 69 6e 74 28 30 2c 20 61 7a 51 75 65 72 79 5b  _int(0, azQuery[
17d0: 69 5d 2c 20 72 69 64 29 3b 0a 20 20 7d 0a 20 20  i], rid);.  }.  
17e0: 69 66 28 20 73 72 63 49 64 3e 30 20 26 26 20 21  if( srcId>0 && !
17f0: 63 6f 6e 74 65 6e 74 5f 69 73 5f 70 72 69 76 61  content_is_priva
1800: 74 65 28 73 72 63 49 64 29 20 26 26 20 63 6f 6e  te(srcId) && con
1810: 74 65 6e 74 5f 67 65 74 28 73 72 63 49 64 2c 20  tent_get(srcId, 
1820: 26 73 72 63 29 20 29 7b 0a 20 20 20 20 63 68 61  &src) ){.    cha
1830: 72 20 2a 7a 55 75 69 64 20 3d 20 64 62 5f 74 65  r *zUuid = db_te
1840: 78 74 28 30 2c 20 22 53 45 4c 45 43 54 20 75 75  xt(0, "SELECT uu
1850: 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45  id FROM blob WHE
1860: 52 45 20 72 69 64 3d 25 64 22 2c 20 73 72 63 49  RE rid=%d", srcI
1870: 64 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 64 65 6c  d);.    blob_del
1880: 74 61 5f 63 72 65 61 74 65 28 26 73 72 63 2c 20  ta_create(&src, 
1890: 70 43 6f 6e 74 65 6e 74 2c 20 26 64 65 6c 74 61  pContent, &delta
18a0: 29 3b 0a 20 20 20 20 73 69 7a 65 20 3d 20 62 6c  );.    size = bl
18b0: 6f 62 5f 73 69 7a 65 28 26 64 65 6c 74 61 29 3b  ob_size(&delta);
18c0: 0a 20 20 20 20 69 66 28 20 73 69 7a 65 3e 3d 62  .    if( size>=b
18d0: 6c 6f 62 5f 73 69 7a 65 28 70 43 6f 6e 74 65 6e  lob_size(pConten
18e0: 74 29 2d 35 30 20 29 7b 0a 20 20 20 20 20 20 73  t)-50 ){.      s
18f0: 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c  ize = 0;.    }el
1900: 73 65 20 69 66 28 20 75 75 69 64 5f 69 73 5f 73  se if( uuid_is_s
1910: 68 75 6e 6e 65 64 28 7a 55 75 69 64 29 20 29 7b  hunned(zUuid) ){
1920: 0a 20 20 20 20 20 20 73 69 7a 65 20 3d 20 30 3b  .      size = 0;
1930: 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1940: 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70    blob_appendf(p
1950: 58 66 65 72 2d 3e 70 4f 75 74 2c 20 22 66 69 6c  Xfer->pOut, "fil
1960: 65 20 25 62 20 25 73 20 25 64 5c 6e 22 2c 20 70  e %b %s %d\n", p
1970: 55 75 69 64 2c 20 7a 55 75 69 64 2c 20 73 69 7a  Uuid, zUuid, siz
1980: 65 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 61  e);.      blob_a
1990: 70 70 65 6e 64 28 70 58 66 65 72 2d 3e 70 4f 75  ppend(pXfer->pOu
19a0: 74 2c 20 62 6c 6f 62 5f 62 75 66 66 65 72 28 26  t, blob_buffer(&
19b0: 64 65 6c 74 61 29 2c 20 73 69 7a 65 29 3b 0a 20  delta), size);. 
19c0: 20 20 20 20 20 2f 2a 20 62 6c 6f 62 5f 61 70 70       /* blob_app
19d0: 65 6e 64 66 28 70 58 66 65 72 2d 3e 70 4f 75 74  endf(pXfer->pOut
19e0: 2c 20 22 5c 6e 22 2c 20 31 29 3b 20 2a 2f 0a 20  , "\n", 1); */. 
19f0: 20 20 20 7d 0a 20 20 20 20 62 6c 6f 62 5f 72 65     }.    blob_re
1a00: 73 65 74 28 26 64 65 6c 74 61 29 3b 0a 20 20 20  set(&delta);.   
1a10: 20 66 72 65 65 28 7a 55 75 69 64 29 3b 0a 20 20   free(zUuid);.  
1a20: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 73 72    blob_reset(&sr
1a30: 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  c);.  }.  return
1a40: 20 73 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   size;.}../*.** 
1a50: 54 72 79 20 74 6f 20 73 65 6e 64 20 61 20 66 69  Try to send a fi
1a60: 6c 65 20 61 73 20 61 20 6e 61 74 69 76 65 20 64  le as a native d
1a70: 65 6c 74 61 2e 20 20 0a 2a 2a 20 49 66 20 73 75  elta.  .** If su
1a80: 63 63 65 73 73 66 75 6c 2c 20 72 65 74 75 72 6e  ccessful, return
1a90: 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
1aa0: 79 74 65 73 20 69 6e 20 74 68 65 20 64 65 6c 74  ytes in the delt
1ab0: 61 2e 0a 2a 2a 20 49 66 20 77 65 20 63 61 6e 6e  a..** If we cann
1ac0: 6f 74 20 67 65 6e 65 72 61 74 65 20 61 6e 20 61  ot generate an a
1ad0: 70 70 72 6f 70 72 69 61 74 65 20 64 65 6c 74 61  ppropriate delta
1ae0: 2c 20 74 68 65 6e 20 73 65 6e 64 0a 2a 2a 20 6e  , then send.** n
1af0: 6f 74 68 69 6e 67 20 61 6e 64 20 72 65 74 75 72  othing and retur
1b00: 6e 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 4e 65  n zero..**.** Ne
1b10: 76 65 72 20 73 65 6e 64 20 61 20 64 65 6c 74 61  ver send a delta
1b20: 20 61 67 61 69 6e 73 74 20 61 20 70 72 69 76 61   against a priva
1b30: 74 65 20 61 72 74 69 66 61 63 74 2e 0a 2a 2f 0a  te artifact..*/.
1b40: 73 74 61 74 69 63 20 69 6e 74 20 73 65 6e 64 5f  static int send_
1b50: 64 65 6c 74 61 5f 6e 61 74 69 76 65 28 0a 20 20  delta_native(.  
1b60: 58 66 65 72 20 2a 70 58 66 65 72 2c 20 20 20 20  Xfer *pXfer,    
1b70: 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
1b80: 72 61 6e 73 66 65 72 20 63 6f 6e 74 65 78 74 20  ransfer context 
1b90: 2a 2f 0a 20 20 69 6e 74 20 72 69 64 2c 20 20 20  */.  int rid,   
1ba0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1bb0: 72 65 63 6f 72 64 20 69 64 20 6f 66 20 74 68 65  record id of the
1bc0: 20 66 69 6c 65 20 74 6f 20 73 65 6e 64 20 2a 2f   file to send */
1bd0: 0a 20 20 42 6c 6f 62 20 2a 70 55 75 69 64 20 20  .  Blob *pUuid  
1be0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1bf0: 65 20 55 55 49 44 20 6f 66 20 74 68 65 20 66 69  e UUID of the fi
1c00: 6c 65 20 74 6f 20 73 65 6e 64 20 2a 2f 0a 29 7b  le to send */.){
1c10: 0a 20 20 42 6c 6f 62 20 73 72 63 2c 20 64 65 6c  .  Blob src, del
1c20: 74 61 3b 0a 20 20 69 6e 74 20 73 69 7a 65 20 3d  ta;.  int size =
1c30: 20 30 3b 0a 20 20 69 6e 74 20 73 72 63 49 64 3b   0;.  int srcId;
1c40: 0a 0a 20 20 73 72 63 49 64 20 3d 20 64 62 5f 69  ..  srcId = db_i
1c50: 6e 74 28 30 2c 20 22 53 45 4c 45 43 54 20 73 72  nt(0, "SELECT sr
1c60: 63 69 64 20 46 52 4f 4d 20 64 65 6c 74 61 20 57  cid FROM delta W
1c70: 48 45 52 45 20 72 69 64 3d 25 64 22 2c 20 72 69  HERE rid=%d", ri
1c80: 64 29 3b 0a 20 20 69 66 28 20 73 72 63 49 64 3e  d);.  if( srcId>
1c90: 30 20 26 26 20 21 63 6f 6e 74 65 6e 74 5f 69 73  0 && !content_is
1ca0: 5f 70 72 69 76 61 74 65 28 73 72 63 49 64 29 20  _private(srcId) 
1cb0: 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 7a 65 72 6f  ){.    blob_zero
1cc0: 28 26 73 72 63 29 3b 0a 20 20 20 20 64 62 5f 62  (&src);.    db_b
1cd0: 6c 6f 62 28 26 73 72 63 2c 20 22 53 45 4c 45 43  lob(&src, "SELEC
1ce0: 54 20 75 75 69 64 20 46 52 4f 4d 20 62 6c 6f 62  T uuid FROM blob
1cf0: 20 57 48 45 52 45 20 72 69 64 3d 25 64 22 2c 20   WHERE rid=%d", 
1d00: 73 72 63 49 64 29 3b 0a 20 20 20 20 69 66 28 20  srcId);.    if( 
1d10: 75 75 69 64 5f 69 73 5f 73 68 75 6e 6e 65 64 28  uuid_is_shunned(
1d20: 62 6c 6f 62 5f 73 74 72 28 26 73 72 63 29 29 20  blob_str(&src)) 
1d30: 29 7b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 72 65  ){.      blob_re
1d40: 73 65 74 28 26 73 72 63 29 3b 0a 20 20 20 20 20  set(&src);.     
1d50: 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d   return 0;.    }
1d60: 0a 20 20 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26  .    blob_zero(&
1d70: 64 65 6c 74 61 29 3b 0a 20 20 20 20 64 62 5f 62  delta);.    db_b
1d80: 6c 6f 62 28 26 64 65 6c 74 61 2c 20 22 53 45 4c  lob(&delta, "SEL
1d90: 45 43 54 20 63 6f 6e 74 65 6e 74 20 46 52 4f 4d  ECT content FROM
1da0: 20 62 6c 6f 62 20 57 48 45 52 45 20 72 69 64 3d   blob WHERE rid=
1db0: 25 64 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 62  %d", rid);.    b
1dc0: 6c 6f 62 5f 75 6e 63 6f 6d 70 72 65 73 73 28 26  lob_uncompress(&
1dd0: 64 65 6c 74 61 2c 20 26 64 65 6c 74 61 29 3b 0a  delta, &delta);.
1de0: 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66      blob_appendf
1df0: 28 70 58 66 65 72 2d 3e 70 4f 75 74 2c 20 22 66  (pXfer->pOut, "f
1e00: 69 6c 65 20 25 62 20 25 62 20 25 64 5c 6e 22 2c  ile %b %b %d\n",
1e10: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1e20: 20 70 55 75 69 64 2c 20 26 73 72 63 2c 20 62 6c   pUuid, &src, bl
1e30: 6f 62 5f 73 69 7a 65 28 26 64 65 6c 74 61 29 29  ob_size(&delta))
1e40: 3b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e  ;.    blob_appen
1e50: 64 28 70 58 66 65 72 2d 3e 70 4f 75 74 2c 20 62  d(pXfer->pOut, b
1e60: 6c 6f 62 5f 62 75 66 66 65 72 28 26 64 65 6c 74  lob_buffer(&delt
1e70: 61 29 2c 20 62 6c 6f 62 5f 73 69 7a 65 28 26 64  a), blob_size(&d
1e80: 65 6c 74 61 29 29 3b 0a 20 20 20 20 73 69 7a 65  elta));.    size
1e90: 20 3d 20 62 6c 6f 62 5f 73 69 7a 65 28 26 64 65   = blob_size(&de
1ea0: 6c 74 61 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 72  lta);.    blob_r
1eb0: 65 73 65 74 28 26 64 65 6c 74 61 29 3b 0a 20 20  eset(&delta);.  
1ec0: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 73 72    blob_reset(&sr
1ed0: 63 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  c);.  }else{.   
1ee0: 20 73 69 7a 65 20 3d 20 30 3b 0a 20 20 7d 0a 20   size = 0;.  }. 
1ef0: 20 72 65 74 75 72 6e 20 73 69 7a 65 3b 0a 7d 0a   return size;.}.
1f00: 0a 2f 2a 0a 2a 2a 20 53 65 6e 64 20 74 68 65 20  ./*.** Send the 
1f10: 66 69 6c 65 20 69 64 65 6e 74 69 66 69 65 64 20  file identified 
1f20: 62 79 20 72 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  by rid..**.** Th
1f30: 65 20 70 55 75 69 64 20 63 61 6e 20 62 65 20 4e  e pUuid can be N
1f40: 55 4c 4c 20 69 6e 20 77 68 69 63 68 20 63 61 73  ULL in which cas
1f50: 65 20 74 68 65 20 63 6f 72 72 65 63 74 20 55 55  e the correct UU
1f60: 49 44 20 69 73 20 63 6f 6d 70 75 74 65 64 0a 2a  ID is computed.*
1f70: 2a 20 66 72 6f 6d 20 74 68 65 20 72 69 64 2e 0a  * from the rid..
1f80: 2a 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 73 65 6e  **.** Try to sen
1f90: 64 20 74 68 65 20 66 69 6c 65 20 61 73 20 61 20  d the file as a 
1fa0: 6e 61 74 69 76 65 20 64 65 6c 74 61 20 69 66 20  native delta if 
1fb0: 6e 61 74 69 76 65 44 65 6c 74 61 20 69 73 20 74  nativeDelta is t
1fc0: 72 75 65 2c 20 6f 72 0a 2a 2a 20 61 73 20 61 20  rue, or.** as a 
1fd0: 70 61 72 65 6e 74 20 64 65 6c 74 61 20 69 66 20  parent delta if 
1fe0: 6e 61 74 69 76 65 44 65 6c 74 61 20 69 73 20 66  nativeDelta is f
1ff0: 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 73  alse..**.** It s
2000: 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 20 74  hould never be t
2010: 68 65 20 63 61 73 65 20 74 68 61 74 20 72 69 64  he case that rid
2020: 20 69 73 20 61 20 70 72 69 76 61 74 65 20 61 72   is a private ar
2030: 74 69 66 61 63 74 2e 20 20 42 75 74 0a 2a 2a 20  tifact.  But.** 
2040: 61 73 20 61 20 70 72 65 63 61 75 74 69 6f 6e 2c  as a precaution,
2050: 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f   this routine do
2060: 65 73 20 63 68 65 63 6b 20 6f 6e 20 72 69 64 20  es check on rid 
2070: 61 6e 64 20 69 66 20 69 74 20 69 73 20 70 72 69  and if it is pri
2080: 76 61 74 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75  vate.** this rou
2090: 74 69 6e 65 20 62 65 63 6f 6d 65 73 20 61 20 6e  tine becomes a n
20a0: 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  o-op..*/.static 
20b0: 76 6f 69 64 20 73 65 6e 64 5f 66 69 6c 65 28 58  void send_file(X
20c0: 66 65 72 20 2a 70 58 66 65 72 2c 20 69 6e 74 20  fer *pXfer, int 
20d0: 72 69 64 2c 20 42 6c 6f 62 20 2a 70 55 75 69 64  rid, Blob *pUuid
20e0: 2c 20 69 6e 74 20 6e 61 74 69 76 65 44 65 6c 74  , int nativeDelt
20f0: 61 29 7b 0a 20 20 42 6c 6f 62 20 63 6f 6e 74 65  a){.  Blob conte
2100: 6e 74 2c 20 75 75 69 64 3b 0a 20 20 69 6e 74 20  nt, uuid;.  int 
2110: 73 69 7a 65 20 3d 20 30 3b 0a 0a 20 20 69 66 28  size = 0;..  if(
2120: 20 63 6f 6e 74 65 6e 74 5f 69 73 5f 70 72 69 76   content_is_priv
2130: 61 74 65 28 72 69 64 29 20 29 20 72 65 74 75 72  ate(rid) ) retur
2140: 6e 3b 0a 20 20 69 66 28 20 64 62 5f 65 78 69 73  n;.  if( db_exis
2150: 74 73 28 22 53 45 4c 45 43 54 20 31 20 46 52 4f  ts("SELECT 1 FRO
2160: 4d 20 6f 6e 72 65 6d 6f 74 65 20 57 48 45 52 45  M onremote WHERE
2170: 20 72 69 64 3d 25 64 22 2c 20 72 69 64 29 20 29   rid=%d", rid) )
2180: 7b 0a 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  {.     return;. 
2190: 20 7d 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26   }.  blob_zero(&
21a0: 75 75 69 64 29 3b 0a 20 20 64 62 5f 62 6c 6f 62  uuid);.  db_blob
21b0: 28 26 75 75 69 64 2c 20 22 53 45 4c 45 43 54 20  (&uuid, "SELECT 
21c0: 75 75 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57  uuid FROM blob W
21d0: 48 45 52 45 20 72 69 64 3d 25 64 20 41 4e 44 20  HERE rid=%d AND 
21e0: 73 69 7a 65 3e 3d 30 22 2c 20 72 69 64 29 3b 0a  size>=0", rid);.
21f0: 20 20 69 66 28 20 62 6c 6f 62 5f 73 69 7a 65 28    if( blob_size(
2200: 26 75 75 69 64 29 3d 3d 30 20 29 7b 0a 20 20 20  &uuid)==0 ){.   
2210: 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69   return;.  }.  i
2220: 66 28 20 70 55 75 69 64 20 29 7b 0a 20 20 20 20  f( pUuid ){.    
2230: 69 66 28 20 62 6c 6f 62 5f 63 6f 6d 70 61 72 65  if( blob_compare
2240: 28 70 55 75 69 64 2c 20 26 75 75 69 64 29 21 3d  (pUuid, &uuid)!=
2250: 30 20 29 7b 0a 20 20 20 20 20 20 62 6c 6f 62 5f  0 ){.      blob_
2260: 72 65 73 65 74 28 26 75 75 69 64 29 3b 0a 20 20  reset(&uuid);.  
2270: 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
2280: 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  }.  }else{.    p
2290: 55 75 69 64 20 3d 20 26 75 75 69 64 3b 0a 20 20  Uuid = &uuid;.  
22a0: 7d 0a 20 20 69 66 28 20 75 75 69 64 5f 69 73 5f  }.  if( uuid_is_
22b0: 73 68 75 6e 6e 65 64 28 62 6c 6f 62 5f 73 74 72  shunned(blob_str
22c0: 28 70 55 75 69 64 29 29 20 29 7b 0a 20 20 20 20  (pUuid)) ){.    
22d0: 62 6c 6f 62 5f 72 65 73 65 74 28 26 75 75 69 64  blob_reset(&uuid
22e0: 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
22f0: 20 7d 0a 20 20 69 66 28 20 70 58 66 65 72 2d 3e   }.  if( pXfer->
2300: 6d 78 53 65 6e 64 3c 3d 62 6c 6f 62 5f 73 69 7a  mxSend<=blob_siz
2310: 65 28 70 58 66 65 72 2d 3e 70 4f 75 74 29 20 29  e(pXfer->pOut) )
2320: 7b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e  {.    blob_appen
2330: 64 66 28 70 58 66 65 72 2d 3e 70 4f 75 74 2c 20  df(pXfer->pOut, 
2340: 22 69 67 6f 74 20 25 62 5c 6e 22 2c 20 70 55 75  "igot %b\n", pUu
2350: 69 64 29 3b 0a 20 20 20 20 70 58 66 65 72 2d 3e  id);.    pXfer->
2360: 6e 49 47 6f 74 53 65 6e 74 2b 2b 3b 0a 20 20 20  nIGotSent++;.   
2370: 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 75 75 69   blob_reset(&uui
2380: 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  d);.    return;.
2390: 20 20 7d 0a 20 20 69 66 28 20 6e 61 74 69 76 65    }.  if( native
23a0: 44 65 6c 74 61 20 29 7b 0a 20 20 20 20 73 69 7a  Delta ){.    siz
23b0: 65 20 3d 20 73 65 6e 64 5f 64 65 6c 74 61 5f 6e  e = send_delta_n
23c0: 61 74 69 76 65 28 70 58 66 65 72 2c 20 72 69 64  ative(pXfer, rid
23d0: 2c 20 70 55 75 69 64 29 3b 0a 20 20 20 20 69 66  , pUuid);.    if
23e0: 28 20 73 69 7a 65 20 29 7b 0a 20 20 20 20 20 20  ( size ){.      
23f0: 70 58 66 65 72 2d 3e 6e 44 65 6c 74 61 53 65 6e  pXfer->nDeltaSen
2400: 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  t++;.    }.  }. 
2410: 20 69 66 28 20 73 69 7a 65 3d 3d 30 20 29 7b 0a   if( size==0 ){.
2420: 20 20 20 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28      content_get(
2430: 72 69 64 2c 20 26 63 6f 6e 74 65 6e 74 29 3b 0a  rid, &content);.
2440: 0a 20 20 20 20 69 66 28 20 21 6e 61 74 69 76 65  .    if( !native
2450: 44 65 6c 74 61 20 26 26 20 62 6c 6f 62 5f 73 69  Delta && blob_si
2460: 7a 65 28 26 63 6f 6e 74 65 6e 74 29 3e 31 30 30  ze(&content)>100
2470: 20 29 7b 0a 20 20 20 20 20 20 73 69 7a 65 20 3d   ){.      size =
2480: 20 73 65 6e 64 5f 64 65 6c 74 61 5f 70 61 72 65   send_delta_pare
2490: 6e 74 28 70 58 66 65 72 2c 20 72 69 64 2c 20 26  nt(pXfer, rid, &
24a0: 63 6f 6e 74 65 6e 74 2c 20 70 55 75 69 64 29 3b  content, pUuid);
24b0: 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
24c0: 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ize==0 ){.      
24d0: 69 6e 74 20 73 69 7a 65 20 3d 20 62 6c 6f 62 5f  int size = blob_
24e0: 73 69 7a 65 28 26 63 6f 6e 74 65 6e 74 29 3b 0a  size(&content);.
24f0: 20 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e        blob_appen
2500: 64 66 28 70 58 66 65 72 2d 3e 70 4f 75 74 2c 20  df(pXfer->pOut, 
2510: 22 66 69 6c 65 20 25 62 20 25 64 5c 6e 22 2c 20  "file %b %d\n", 
2520: 70 55 75 69 64 2c 20 73 69 7a 65 29 3b 0a 20 20  pUuid, size);.  
2530: 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 28      blob_append(
2540: 70 58 66 65 72 2d 3e 70 4f 75 74 2c 20 62 6c 6f  pXfer->pOut, blo
2550: 62 5f 62 75 66 66 65 72 28 26 63 6f 6e 74 65 6e  b_buffer(&conten
2560: 74 29 2c 20 73 69 7a 65 29 3b 0a 20 20 20 20 20  t), size);.     
2570: 20 70 58 66 65 72 2d 3e 6e 46 69 6c 65 53 65 6e   pXfer->nFileSen
2580: 74 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  t++;.    }else{.
2590: 20 20 20 20 20 20 70 58 66 65 72 2d 3e 6e 44 65        pXfer->nDe
25a0: 6c 74 61 53 65 6e 74 2b 2b 3b 0a 20 20 20 20 7d  ltaSent++;.    }
25b0: 0a 20 20 7d 0a 20 20 72 65 6d 6f 74 65 5f 68 61  .  }.  remote_ha
25c0: 73 28 72 69 64 29 3b 0a 20 20 62 6c 6f 62 5f 72  s(rid);.  blob_r
25d0: 65 73 65 74 28 26 75 75 69 64 29 3b 0a 7d 0a 0a  eset(&uuid);.}..
25e0: 2f 2a 0a 2a 2a 20 53 65 6e 64 20 61 20 67 69 6d  /*.** Send a gim
25f0: 6d 65 20 6d 65 73 73 61 67 65 20 66 6f 72 20 65  me message for e
2600: 76 65 72 79 20 70 68 61 6e 74 6f 6d 2e 0a 2a 2a  very phantom..**
2610: 0a 2a 2a 20 49 74 20 73 68 6f 75 6c 64 20 6e 6f  .** It should no
2620: 74 20 62 65 20 70 6f 73 73 69 62 6c 65 20 74 6f  t be possible to
2630: 20 68 61 76 65 20 61 20 70 72 69 76 61 74 65 20   have a private 
2640: 70 68 61 6e 74 6f 6d 2e 20 20 42 75 74 20 6a 75  phantom.  But ju
2650: 73 74 20 74 6f 20 62 65 0a 2a 2a 20 73 75 72 65  st to be.** sure
2660: 2c 20 74 61 6b 65 20 63 61 72 65 20 6e 6f 74 20  , take care not 
2670: 74 6f 20 73 65 6e 64 20 61 6e 79 20 22 67 69 6d  to send any "gim
2680: 6d 65 22 20 6d 65 73 73 61 67 73 65 20 6f 6e 20  me" messagse on 
2690: 70 72 69 76 61 74 65 20 61 72 74 69 66 61 63 74  private artifact
26a0: 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  s..*/.static voi
26b0: 64 20 72 65 71 75 65 73 74 5f 70 68 61 6e 74 6f  d request_phanto
26c0: 6d 73 28 58 66 65 72 20 2a 70 58 66 65 72 2c 20  ms(Xfer *pXfer, 
26d0: 69 6e 74 20 6d 61 78 52 65 71 29 7b 0a 20 20 53  int maxReq){.  S
26e0: 74 6d 74 20 71 3b 0a 20 20 64 62 5f 70 72 65 70  tmt q;.  db_prep
26f0: 61 72 65 28 26 71 2c 20 0a 20 20 20 20 22 53 45  are(&q, .    "SE
2700: 4c 45 43 54 20 75 75 69 64 20 46 52 4f 4d 20 70  LECT uuid FROM p
2710: 68 61 6e 74 6f 6d 20 4a 4f 49 4e 20 62 6c 6f 62  hantom JOIN blob
2720: 20 55 53 49 4e 47 28 72 69 64 29 22 0a 20 20 20   USING(rid)".   
2730: 20 22 20 57 48 45 52 45 20 4e 4f 54 20 45 58 49   " WHERE NOT EXI
2740: 53 54 53 28 53 45 4c 45 43 54 20 31 20 46 52 4f  STS(SELECT 1 FRO
2750: 4d 20 73 68 75 6e 20 57 48 45 52 45 20 75 75 69  M shun WHERE uui
2760: 64 3d 62 6c 6f 62 2e 75 75 69 64 29 22 0a 20 20  d=blob.uuid)".  
2770: 20 20 22 20 20 20 41 4e 44 20 4e 4f 54 20 45 58    "   AND NOT EX
2780: 49 53 54 53 28 53 45 4c 45 43 54 20 31 20 46 52  ISTS(SELECT 1 FR
2790: 4f 4d 20 70 72 69 76 61 74 65 20 57 48 45 52 45  OM private WHERE
27a0: 20 72 69 64 3d 62 6c 6f 62 2e 72 69 64 29 22 0a   rid=blob.rid)".
27b0: 20 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 64 62    );.  while( db
27c0: 5f 73 74 65 70 28 26 71 29 3d 3d 53 51 4c 49 54  _step(&q)==SQLIT
27d0: 45 5f 52 4f 57 20 26 26 20 6d 61 78 52 65 71 2d  E_ROW && maxReq-
27e0: 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 63 6f 6e  - > 0 ){.    con
27f0: 73 74 20 63 68 61 72 20 2a 7a 55 75 69 64 20 3d  st char *zUuid =
2800: 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28   db_column_text(
2810: 26 71 2c 20 30 29 3b 0a 20 20 20 20 62 6c 6f 62  &q, 0);.    blob
2820: 5f 61 70 70 65 6e 64 66 28 70 58 66 65 72 2d 3e  _appendf(pXfer->
2830: 70 4f 75 74 2c 20 22 67 69 6d 6d 65 20 25 73 5c  pOut, "gimme %s\
2840: 6e 22 2c 20 7a 55 75 69 64 29 3b 0a 20 20 20 20  n", zUuid);.    
2850: 70 58 66 65 72 2d 3e 6e 47 69 6d 6d 65 53 65 6e  pXfer->nGimmeSen
2860: 74 2b 2b 3b 0a 20 20 7d 0a 20 20 64 62 5f 66 69  t++;.  }.  db_fi
2870: 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 7d 0a 0a 2f  nalize(&q);.}../
2880: 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 61 6e 20  *.** Compute an 
2890: 53 48 41 31 20 68 61 73 68 20 6f 6e 20 74 68 65  SHA1 hash on the
28a0: 20 74 61 69 6c 20 6f 66 20 70 4d 73 67 2e 20 20   tail of pMsg.  
28b0: 56 65 72 69 66 79 20 74 68 61 74 20 69 74 20 6d  Verify that it m
28c0: 61 74 63 68 65 73 20 74 68 65 0a 2a 2a 20 74 68  atches the.** th
28d0: 65 20 68 61 73 68 20 67 69 76 65 6e 20 69 6e 20  e hash given in 
28e0: 70 48 61 73 68 2e 20 20 52 65 74 75 72 6e 20 31  pHash.  Return 1
28f0: 20 6f 6e 20 61 20 73 75 63 63 65 73 73 66 75 6c   on a successful
2900: 20 6d 61 74 63 68 2e 20 20 52 65 74 75 72 6e 20   match.  Return 
2910: 30 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 69 73  0.** if there is
2920: 20 61 20 6d 69 73 6d 61 74 63 68 2e 0a 2a 2f 0a   a mismatch..*/.
2930: 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b  static int check
2940: 5f 74 61 69 6c 5f 68 61 73 68 28 42 6c 6f 62 20  _tail_hash(Blob 
2950: 2a 70 48 61 73 68 2c 20 42 6c 6f 62 20 2a 70 4d  *pHash, Blob *pM
2960: 73 67 29 7b 0a 20 20 42 6c 6f 62 20 74 61 69 6c  sg){.  Blob tail
2970: 3b 0a 20 20 42 6c 6f 62 20 68 32 3b 0a 20 20 69  ;.  Blob h2;.  i
2980: 6e 74 20 72 63 3b 0a 20 20 62 6c 6f 62 5f 74 61  nt rc;.  blob_ta
2990: 69 6c 28 70 4d 73 67 2c 20 26 74 61 69 6c 29 3b  il(pMsg, &tail);
29a0: 0a 20 20 73 68 61 31 73 75 6d 5f 62 6c 6f 62 28  .  sha1sum_blob(
29b0: 26 74 61 69 6c 2c 20 26 68 32 29 3b 0a 20 20 72  &tail, &h2);.  r
29c0: 63 20 3d 20 62 6c 6f 62 5f 63 6f 6d 70 61 72 65  c = blob_compare
29d0: 28 70 48 61 73 68 2c 20 26 68 32 29 3b 0a 20 20  (pHash, &h2);.  
29e0: 62 6c 6f 62 5f 72 65 73 65 74 28 26 68 32 29 3b  blob_reset(&h2);
29f0: 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 74  .  blob_reset(&t
2a00: 61 69 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ail);.  return r
2a10: 63 3d 3d 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  c==0;.}.../*.** 
2a20: 43 68 65 63 6b 20 74 68 65 20 73 69 67 6e 61 74  Check the signat
2a30: 75 72 65 20 6f 6e 20 61 6e 20 61 70 70 6c 69 63  ure on an applic
2a40: 61 74 69 6f 6e 2f 78 2d 66 6f 73 73 69 6c 20 70  ation/x-fossil p
2a50: 61 79 6c 6f 61 64 20 72 65 63 65 69 76 65 64 20  ayload received 
2a60: 62 79 0a 2a 2a 20 74 68 65 20 48 54 54 50 20 73  by.** the HTTP s
2a70: 65 72 76 65 72 2e 20 20 54 68 65 20 73 69 67 6e  erver.  The sign
2a80: 61 74 75 72 65 20 69 73 20 61 20 6c 69 6e 65 20  ature is a line 
2a90: 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
2aa0: 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   form:.**.**    
2ab0: 20 20 20 20 6c 6f 67 69 6e 20 4c 4f 47 49 4e 20      login LOGIN 
2ac0: 4e 4f 4e 43 45 20 53 49 47 4e 41 54 55 52 45 0a  NONCE SIGNATURE.
2ad0: 2a 2a 0a 2a 2a 20 54 68 65 20 4e 4f 4e 43 45 20  **.** The NONCE 
2ae0: 69 73 20 74 68 65 20 53 48 41 31 20 68 61 73 68  is the SHA1 hash
2af0: 20 6f 66 20 74 68 65 20 72 65 6d 61 69 6e 64 65   of the remainde
2b00: 72 20 6f 66 20 74 68 65 20 69 6e 70 75 74 2e 20  r of the input. 
2b10: 20 0a 2a 2a 20 53 49 47 4e 41 54 55 52 45 20 69   .** SIGNATURE i
2b20: 73 20 74 68 65 20 53 48 41 31 20 63 68 65 63 6b  s the SHA1 check
2b30: 73 75 6d 20 6f 66 20 74 68 65 20 4e 4f 4e 43 45  sum of the NONCE
2b40: 20 63 6f 6e 63 61 74 65 6e 61 74 65 64 20 0a 2a   concatenated .*
2b50: 2a 20 77 69 74 68 20 74 68 65 20 75 73 65 72 73  * with the users
2b60: 20 70 61 73 73 77 6f 72 64 2e 0a 2a 2a 0a 2a 2a   password..**.**
2b70: 20 54 68 65 20 70 61 72 61 6d 65 74 65 72 73 20   The parameters 
2b80: 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  to this routine 
2b90: 61 72 65 20 65 70 68 65 72 6d 65 72 61 6c 20 62  are ephermeral b
2ba0: 6c 6f 62 73 20 68 6f 6c 64 69 6e 67 20 74 68 65  lobs holding the
2bb0: 0a 2a 2a 20 4c 4f 47 49 4e 2c 20 4e 4f 4e 43 45  .** LOGIN, NONCE
2bc0: 20 61 6e 64 20 53 49 47 4e 41 54 55 52 45 2e 0a   and SIGNATURE..
2bd0: 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
2be0: 6e 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6c  ne attempts to l
2bf0: 6f 63 61 74 65 20 74 68 65 20 75 73 65 72 20 61  ocate the user a
2c00: 6e 64 20 76 65 72 69 66 79 20 74 68 65 20 73 69  nd verify the si
2c10: 67 6e 61 74 75 72 65 2e 0a 2a 2a 20 49 66 20 65  gnature..** If e
2c20: 76 65 72 79 74 68 69 6e 67 20 63 68 65 63 6b 73  verything checks
2c30: 20 6f 75 74 2c 20 74 68 65 20 55 53 45 52 2e 43   out, the USER.C
2c40: 41 50 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 74 68  AP column for th
2c50: 65 20 55 53 45 52 20 74 61 62 6c 65 0a 2a 2a 20  e USER table.** 
2c60: 69 73 20 63 6f 6e 73 75 6c 74 65 64 20 74 6f 20  is consulted to 
2c70: 73 65 74 20 70 72 69 76 69 6c 65 67 65 73 20 69  set privileges i
2c80: 6e 20 74 68 65 20 67 6c 6f 62 61 6c 20 67 20 76  n the global g v
2c90: 61 72 69 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49  ariable..**.** I
2ca0: 66 20 61 6e 79 74 68 69 6e 67 20 66 61 69 6c 73  f anything fails
2cb0: 20 74 6f 20 63 68 65 63 6b 20 6f 75 74 2c 20 6e   to check out, n
2cc0: 6f 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d 61  o changes are ma
2cd0: 64 65 20 74 6f 20 70 72 69 76 69 6c 65 67 65 73  de to privileges
2ce0: 2e 0a 2a 2a 0a 2a 2a 20 53 69 67 6e 61 74 75 72  ..**.** Signatur
2cf0: 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 6e 20  e generation on 
2d00: 74 68 65 20 63 6c 69 65 6e 74 20 73 69 64 65 20  the client side 
2d10: 69 73 20 68 61 6e 64 6c 65 64 20 62 79 20 74 68  is handled by th
2d20: 65 20 0a 2a 2a 20 68 74 74 70 5f 65 78 63 68 61  e .** http_excha
2d30: 6e 67 65 28 29 20 72 6f 75 74 69 6e 65 2e 0a 2a  nge() routine..*
2d40: 2f 0a 76 6f 69 64 20 63 68 65 63 6b 5f 6c 6f 67  /.void check_log
2d50: 69 6e 28 42 6c 6f 62 20 2a 70 4c 6f 67 69 6e 2c  in(Blob *pLogin,
2d60: 20 42 6c 6f 62 20 2a 70 4e 6f 6e 63 65 2c 20 42   Blob *pNonce, B
2d70: 6c 6f 62 20 2a 70 53 69 67 29 7b 0a 20 20 53 74  lob *pSig){.  St
2d80: 6d 74 20 71 3b 0a 20 20 69 6e 74 20 72 63 20 3d  mt q;.  int rc =
2d90: 20 2d 31 3b 0a 20 20 63 68 61 72 20 2a 7a 4c 6f   -1;.  char *zLo
2da0: 67 69 6e 20 3d 20 62 6c 6f 62 5f 74 65 72 6d 69  gin = blob_termi
2db0: 6e 61 74 65 28 70 4c 6f 67 69 6e 29 3b 0a 20 20  nate(pLogin);.  
2dc0: 64 65 66 6f 73 73 69 6c 69 7a 65 28 7a 4c 6f 67  defossilize(zLog
2dd0: 69 6e 29 3b 0a 0a 20 20 64 62 5f 70 72 65 70 61  in);..  db_prepa
2de0: 72 65 28 26 71 2c 0a 20 20 20 20 20 22 53 45 4c  re(&q,.     "SEL
2df0: 45 43 54 20 70 77 2c 20 63 61 70 2c 20 75 69 64  ECT pw, cap, uid
2e00: 20 46 52 4f 4d 20 75 73 65 72 22 0a 20 20 20 20   FROM user".    
2e10: 20 22 20 57 48 45 52 45 20 6c 6f 67 69 6e 3d 25   " WHERE login=%
2e20: 51 22 0a 20 20 20 20 20 22 20 20 20 41 4e 44 20  Q".     "   AND 
2e30: 6c 6f 67 69 6e 20 4e 4f 54 20 49 4e 20 28 27 61  login NOT IN ('a
2e40: 6e 6f 6e 79 6d 6f 75 73 27 2c 27 6e 6f 62 6f 64  nonymous','nobod
2e50: 79 27 2c 27 64 65 76 65 6c 6f 70 65 72 27 2c 27  y','developer','
2e60: 72 65 61 64 65 72 27 29 22 0a 20 20 20 20 20 22  reader')".     "
2e70: 20 20 20 41 4e 44 20 6c 65 6e 67 74 68 28 70 77     AND length(pw
2e80: 29 3e 30 22 2c 0a 20 20 20 20 20 7a 4c 6f 67 69  )>0",.     zLogi
2e90: 6e 0a 20 20 29 3b 0a 20 20 69 66 28 20 64 62 5f  n.  );.  if( db_
2ea0: 73 74 65 70 28 26 71 29 3d 3d 53 51 4c 49 54 45  step(&q)==SQLITE
2eb0: 5f 52 4f 57 20 29 7b 0a 20 20 20 20 42 6c 6f 62  _ROW ){.    Blob
2ec0: 20 70 77 2c 20 63 6f 6d 62 69 6e 65 64 2c 20 68   pw, combined, h
2ed0: 61 73 68 3b 0a 20 20 20 20 62 6c 6f 62 5f 7a 65  ash;.    blob_ze
2ee0: 72 6f 28 26 70 77 29 3b 0a 20 20 20 20 64 62 5f  ro(&pw);.    db_
2ef0: 65 70 68 65 6d 65 72 61 6c 5f 62 6c 6f 62 28 26  ephemeral_blob(&
2f00: 71 2c 20 30 2c 20 26 70 77 29 3b 0a 20 20 20 20  q, 0, &pw);.    
2f10: 62 6c 6f 62 5f 7a 65 72 6f 28 26 63 6f 6d 62 69  blob_zero(&combi
2f20: 6e 65 64 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 63  ned);.    blob_c
2f30: 6f 70 79 28 26 63 6f 6d 62 69 6e 65 64 2c 20 70  opy(&combined, p
2f40: 4e 6f 6e 63 65 29 3b 0a 20 20 20 20 62 6c 6f 62  Nonce);.    blob
2f50: 5f 61 70 70 65 6e 64 28 26 63 6f 6d 62 69 6e 65  _append(&combine
2f60: 64 2c 20 62 6c 6f 62 5f 62 75 66 66 65 72 28 26  d, blob_buffer(&
2f70: 70 77 29 2c 20 62 6c 6f 62 5f 73 69 7a 65 28 26  pw), blob_size(&
2f80: 70 77 29 29 3b 0a 20 20 20 20 2f 2a 20 43 47 49  pw));.    /* CGI
2f90: 44 45 42 55 47 28 28 22 70 72 65 73 69 67 3d 5b  DEBUG(("presig=[
2fa0: 25 73 5d 5c 6e 22 2c 20 62 6c 6f 62 5f 73 74 72  %s]\n", blob_str
2fb0: 28 26 63 6f 6d 62 69 6e 65 64 29 29 29 3b 20 2a  (&combined))); *
2fc0: 2f 0a 20 20 20 20 73 68 61 31 73 75 6d 5f 62 6c  /.    sha1sum_bl
2fd0: 6f 62 28 26 63 6f 6d 62 69 6e 65 64 2c 20 26 68  ob(&combined, &h
2fe0: 61 73 68 29 3b 0a 20 20 20 20 72 63 20 3d 20 62  ash);.    rc = b
2ff0: 6c 6f 62 5f 63 6f 6d 70 61 72 65 28 26 68 61 73  lob_compare(&has
3000: 68 2c 20 70 53 69 67 29 3b 0a 20 20 20 20 62 6c  h, pSig);.    bl
3010: 6f 62 5f 72 65 73 65 74 28 26 68 61 73 68 29 3b  ob_reset(&hash);
3020: 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28  .    blob_reset(
3030: 26 63 6f 6d 62 69 6e 65 64 29 3b 0a 20 20 20 20  &combined);.    
3040: 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20  if( rc==0 ){.   
3050: 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
3060: 43 61 70 3b 0a 20 20 20 20 20 20 7a 43 61 70 20  Cap;.      zCap 
3070: 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  = db_column_text
3080: 28 26 71 2c 20 31 29 3b 0a 20 20 20 20 20 20 6c  (&q, 1);.      l
3090: 6f 67 69 6e 5f 73 65 74 5f 63 61 70 61 62 69 6c  ogin_set_capabil
30a0: 69 74 69 65 73 28 7a 43 61 70 29 3b 0a 20 20 20  ities(zCap);.   
30b0: 20 20 20 67 2e 75 73 65 72 55 69 64 20 3d 20 64     g.userUid = d
30c0: 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c  b_column_int(&q,
30d0: 20 32 29 3b 0a 20 20 20 20 20 20 67 2e 7a 4c 6f   2);.      g.zLo
30e0: 67 69 6e 20 3d 20 6d 70 72 69 6e 74 66 28 22 25  gin = mprintf("%
30f0: 62 22 2c 20 70 4c 6f 67 69 6e 29 3b 0a 20 20 20  b", pLogin);.   
3100: 20 20 20 67 2e 7a 4e 6f 6e 63 65 20 3d 20 6d 70     g.zNonce = mp
3110: 72 69 6e 74 66 28 22 25 62 22 2c 20 70 4e 6f 6e  rintf("%b", pNon
3120: 63 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ce);.    }.  }. 
3130: 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29   db_finalize(&q)
3140: 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 30 20 29  ;..  if( rc==0 )
3150: 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20  {.    /* If the 
3160: 6c 6f 67 69 6e 20 77 61 73 20 73 75 63 63 65 73  login was succes
3170: 73 66 75 6c 2e 20 2a 2f 0a 20 20 20 20 6c 6f 67  sful. */.    log
3180: 69 6e 5f 73 65 74 5f 61 6e 6f 6e 5f 6e 6f 62 6f  in_set_anon_nobo
3190: 64 79 5f 63 61 70 61 62 69 6c 69 74 69 65 73 28  dy_capabilities(
31a0: 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
31b0: 53 65 6e 64 20 74 68 65 20 63 6f 6e 74 65 6e 74  Send the content
31c0: 20 6f 66 20 61 6c 6c 20 66 69 6c 65 73 20 69 6e   of all files in
31d0: 20 74 68 65 20 75 6e 73 65 6e 74 20 74 61 62 6c   the unsent tabl
31e0: 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73  e..**.** This is
31f0: 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 61 6e 20   really just an 
3200: 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 49  optimization.  I
3210: 66 20 79 6f 75 20 63 6c 65 61 72 20 74 68 65 0a  f you clear the.
3220: 2a 2a 20 75 6e 73 65 6e 74 20 74 61 62 6c 65 2c  ** unsent table,
3230: 20 61 6c 6c 20 74 68 65 20 72 69 67 68 74 20 66   all the right f
3240: 69 6c 65 73 20 77 69 6c 6c 20 73 74 69 6c 6c 20  iles will still 
3250: 67 65 74 20 74 72 61 6e 73 66 65 72 72 65 64 2e  get transferred.
3260: 0a 2a 2a 20 49 74 20 6a 75 73 74 20 6d 69 67 68  .** It just migh
3270: 74 20 72 65 71 75 69 72 65 20 61 6e 20 65 78 74  t require an ext
3280: 72 61 20 72 6f 75 6e 64 20 74 72 69 70 20 6f 72  ra round trip or
3290: 20 74 77 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   two..*/.static 
32a0: 76 6f 69 64 20 73 65 6e 64 5f 75 6e 73 65 6e 74  void send_unsent
32b0: 28 58 66 65 72 20 2a 70 58 66 65 72 29 7b 0a 20  (Xfer *pXfer){. 
32c0: 20 53 74 6d 74 20 71 3b 0a 20 20 64 62 5f 70 72   Stmt q;.  db_pr
32d0: 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c 45 43  epare(&q, "SELEC
32e0: 54 20 72 69 64 20 46 52 4f 4d 20 75 6e 73 65 6e  T rid FROM unsen
32f0: 74 20 45 58 43 45 50 54 20 53 45 4c 45 43 54 20  t EXCEPT SELECT 
3300: 72 69 64 20 46 52 4f 4d 20 70 72 69 76 61 74 65  rid FROM private
3310: 22 29 3b 0a 20 20 77 68 69 6c 65 28 20 64 62 5f  ");.  while( db_
3320: 73 74 65 70 28 26 71 29 3d 3d 53 51 4c 49 54 45  step(&q)==SQLITE
3330: 5f 52 4f 57 20 29 7b 0a 20 20 20 20 69 6e 74 20  _ROW ){.    int 
3340: 72 69 64 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f  rid = db_column_
3350: 69 6e 74 28 26 71 2c 20 30 29 3b 0a 20 20 20 20  int(&q, 0);.    
3360: 73 65 6e 64 5f 66 69 6c 65 28 70 58 66 65 72 2c  send_file(pXfer,
3370: 20 72 69 64 2c 20 30 2c 20 30 29 3b 0a 20 20 7d   rid, 0, 0);.  }
3380: 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26  .  db_finalize(&
3390: 71 29 3b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65  q);.  db_multi_e
33a0: 78 65 63 28 22 44 45 4c 45 54 45 20 46 52 4f 4d  xec("DELETE FROM
33b0: 20 75 6e 73 65 6e 74 22 29 3b 0a 7d 0a 0a 2f 2a   unsent");.}../*
33c0: 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65  .** Check to see
33d0: 20 69 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   if the number o
33e0: 66 20 75 6e 63 6c 75 73 74 65 72 65 64 20 65 6e  f unclustered en
33f0: 74 72 69 65 73 20 69 73 20 67 72 65 61 74 65 72  tries is greater
3400: 20 74 68 61 6e 0a 2a 2a 20 31 30 30 20 61 6e 64   than.** 100 and
3410: 20 69 66 20 69 74 20 69 73 2c 20 66 6f 72 6d 20   if it is, form 
3420: 61 20 6e 65 77 20 63 6c 75 73 74 65 72 2e 20 20  a new cluster.  
3430: 55 6e 63 6c 75 73 74 65 72 65 64 20 70 68 61 6e  Unclustered phan
3440: 74 6f 6d 73 20 64 6f 20 6e 6f 74 0a 2a 2a 20 63  toms do not.** c
3450: 6f 75 6e 74 20 74 6f 77 61 72 64 20 74 68 65 20  ount toward the 
3460: 31 30 30 20 74 6f 74 61 6c 2e 20 20 41 6e 64 20  100 total.  And 
3470: 70 68 61 6e 74 6f 6d 73 20 61 72 65 20 6e 65 76  phantoms are nev
3480: 65 72 20 61 64 64 65 64 20 74 6f 20 61 20 6e 65  er added to a ne
3490: 77 0a 2a 2a 20 63 6c 75 73 74 65 72 2e 0a 2a 2f  w.** cluster..*/
34a0: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 72 65  .static void cre
34b0: 61 74 65 5f 63 6c 75 73 74 65 72 28 76 6f 69 64  ate_cluster(void
34c0: 29 7b 0a 20 20 42 6c 6f 62 20 63 6c 75 73 74 65  ){.  Blob cluste
34d0: 72 2c 20 63 6b 73 75 6d 3b 0a 20 20 53 74 6d 74  r, cksum;.  Stmt
34e0: 20 71 3b 0a 20 20 69 6e 74 20 6e 55 6e 63 6c 3b   q;.  int nUncl;
34f0: 0a 0a 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64  ..  /* We should
3500: 20 6e 6f 74 20 65 76 65 72 20 67 65 74 20 61 6e   not ever get an
3510: 79 20 70 72 69 76 61 74 65 20 61 72 74 69 66 61  y private artifa
3520: 63 74 73 20 69 6e 20 74 68 65 20 75 6e 63 6c 75  cts in the unclu
3530: 73 74 65 72 65 64 20 74 61 62 6c 65 2e 0a 20 20  stered table..  
3540: 2a 2a 20 42 75 74 20 69 66 20 77 65 20 64 6f 20  ** But if we do 
3550: 28 62 65 63 61 75 73 65 20 6f 66 20 61 20 62 75  (because of a bu
3560: 67 29 20 6e 6f 77 20 69 73 20 61 20 67 6f 6f 64  g) now is a good
3570: 20 74 69 6d 65 20 74 6f 20 64 65 6c 65 74 65 20   time to delete 
3580: 74 68 65 6d 2e 20 2a 2f 0a 20 20 64 62 5f 6d 75  them. */.  db_mu
3590: 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 20 22 44  lti_exec(.    "D
35a0: 45 4c 45 54 45 20 46 52 4f 4d 20 75 6e 63 6c 75  ELETE FROM unclu
35b0: 73 74 65 72 65 64 20 57 48 45 52 45 20 72 69 64  stered WHERE rid
35c0: 20 49 4e 20 28 53 45 4c 45 43 54 20 72 69 64 20   IN (SELECT rid 
35d0: 46 52 4f 4d 20 70 72 69 76 61 74 65 29 22 0a 20  FROM private)". 
35e0: 20 29 3b 0a 0a 20 20 6e 55 6e 63 6c 20 3d 20 64   );..  nUncl = d
35f0: 62 5f 69 6e 74 28 30 2c 20 22 53 45 4c 45 43 54  b_int(0, "SELECT
3600: 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 75   count(*) FROM u
3610: 6e 63 6c 75 73 74 65 72 65 64 22 0a 20 20 20 20  nclustered".    
3620: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3630: 22 20 57 48 45 52 45 20 4e 4f 54 20 45 58 49 53  " WHERE NOT EXIS
3640: 54 53 28 53 45 4c 45 43 54 20 31 20 46 52 4f 4d  TS(SELECT 1 FROM
3650: 20 70 68 61 6e 74 6f 6d 22 0a 20 20 20 20 20 20   phantom".      
3660: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3670: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3680: 22 20 57 48 45 52 45 20 72 69 64 3d 75 6e 63 6c  " WHERE rid=uncl
3690: 75 73 74 65 72 65 64 2e 72 69 64 29 22 29 3b 0a  ustered.rid)");.
36a0: 20 20 69 66 28 20 6e 55 6e 63 6c 3c 31 30 30 20    if( nUncl<100 
36b0: 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  ){.    return;. 
36c0: 20 7d 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26   }.  blob_zero(&
36d0: 63 6c 75 73 74 65 72 29 3b 0a 20 20 64 62 5f 70  cluster);.  db_p
36e0: 72 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c 45  repare(&q, "SELE
36f0: 43 54 20 75 75 69 64 20 46 52 4f 4d 20 75 6e 63  CT uuid FROM unc
3700: 6c 75 73 74 65 72 65 64 2c 20 62 6c 6f 62 22 0a  lustered, blob".
3710: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3720: 20 22 20 57 48 45 52 45 20 4e 4f 54 20 45 58 49   " WHERE NOT EXI
3730: 53 54 53 28 53 45 4c 45 43 54 20 31 20 46 52 4f  STS(SELECT 1 FRO
3740: 4d 20 70 68 61 6e 74 6f 6d 22 0a 20 20 20 20 20  M phantom".     
3750: 20 20 20 20 20 20 20 20 20 20 20 20 22 20 20 20              "   
3760: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3770: 57 48 45 52 45 20 72 69 64 3d 75 6e 63 6c 75 73  WHERE rid=unclus
3780: 74 65 72 65 64 2e 72 69 64 29 22 0a 20 20 20 20  tered.rid)".    
3790: 20 20 20 20 20 20 20 20 20 20 20 20 20 22 20 20               "  
37a0: 20 41 4e 44 20 75 6e 63 6c 75 73 74 65 72 65 64   AND unclustered
37b0: 2e 72 69 64 3d 62 6c 6f 62 2e 72 69 64 22 0a 20  .rid=blob.rid". 
37c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37d0: 22 20 20 20 41 4e 44 20 4e 4f 54 20 45 58 49 53  "   AND NOT EXIS
37e0: 54 53 28 53 45 4c 45 43 54 20 31 20 46 52 4f 4d  TS(SELECT 1 FROM
37f0: 20 73 68 75 6e 20 57 48 45 52 45 20 75 75 69 64   shun WHERE uuid
3800: 3d 62 6c 6f 62 2e 75 75 69 64 29 22 0a 20 20 20  =blob.uuid)".   
3810: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 20                " 
3820: 4f 52 44 45 52 20 42 59 20 31 22 29 3b 0a 20 20  ORDER BY 1");.  
3830: 77 68 69 6c 65 28 20 64 62 5f 73 74 65 70 28 26  while( db_step(&
3840: 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  q)==SQLITE_ROW )
3850: 7b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e  {.    blob_appen
3860: 64 66 28 26 63 6c 75 73 74 65 72 2c 20 22 4d 20  df(&cluster, "M 
3870: 25 73 5c 6e 22 2c 20 64 62 5f 63 6f 6c 75 6d 6e  %s\n", db_column
3880: 5f 74 65 78 74 28 26 71 2c 20 30 29 29 3b 0a 20  _text(&q, 0));. 
3890: 20 7d 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65   }.  db_finalize
38a0: 28 26 71 29 3b 0a 20 20 6d 64 35 73 75 6d 5f 62  (&q);.  md5sum_b
38b0: 6c 6f 62 28 26 63 6c 75 73 74 65 72 2c 20 26 63  lob(&cluster, &c
38c0: 6b 73 75 6d 29 3b 0a 20 20 62 6c 6f 62 5f 61 70  ksum);.  blob_ap
38d0: 70 65 6e 64 66 28 26 63 6c 75 73 74 65 72 2c 20  pendf(&cluster, 
38e0: 22 5a 20 25 62 5c 6e 22 2c 20 26 63 6b 73 75 6d  "Z %b\n", &cksum
38f0: 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28  );.  blob_reset(
3900: 26 63 6b 73 75 6d 29 3b 0a 20 20 64 62 5f 6d 75  &cksum);.  db_mu
3910: 6c 74 69 5f 65 78 65 63 28 22 44 45 4c 45 54 45  lti_exec("DELETE
3920: 20 46 52 4f 4d 20 75 6e 63 6c 75 73 74 65 72 65   FROM unclustere
3930: 64 22 29 3b 0a 20 20 63 6f 6e 74 65 6e 74 5f 70  d");.  content_p
3940: 75 74 28 26 63 6c 75 73 74 65 72 2c 20 30 2c 20  ut(&cluster, 0, 
3950: 30 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74  0);.  blob_reset
3960: 28 26 63 6c 75 73 74 65 72 29 3b 0a 7d 0a 0a 2f  (&cluster);.}../
3970: 2a 0a 2a 2a 20 53 65 6e 64 20 61 6e 20 69 67 6f  *.** Send an igo
3980: 74 20 6d 65 73 73 61 67 65 20 66 6f 72 20 65 76  t message for ev
3990: 65 72 79 20 65 6e 74 72 79 20 69 6e 20 75 6e 63  ery entry in unc
39a0: 6c 75 73 74 65 72 65 64 20 74 61 62 6c 65 2e 0a  lustered table..
39b0: 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
39c0: 6d 62 65 72 20 6f 66 20 63 61 72 64 73 20 73 65  mber of cards se
39d0: 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  nt..*/.static in
39e0: 74 20 73 65 6e 64 5f 75 6e 63 6c 75 73 74 65 72  t send_uncluster
39f0: 65 64 28 58 66 65 72 20 2a 70 58 66 65 72 29 7b  ed(Xfer *pXfer){
3a00: 0a 20 20 53 74 6d 74 20 71 3b 0a 20 20 69 6e 74  .  Stmt q;.  int
3a10: 20 63 6e 74 20 3d 20 30 3b 0a 20 20 64 62 5f 70   cnt = 0;.  db_p
3a20: 72 65 70 61 72 65 28 26 71 2c 20 0a 20 20 20 20  repare(&q, .    
3a30: 22 53 45 4c 45 43 54 20 75 75 69 64 20 46 52 4f  "SELECT uuid FRO
3a40: 4d 20 75 6e 63 6c 75 73 74 65 72 65 64 20 4a 4f  M unclustered JO
3a50: 49 4e 20 62 6c 6f 62 20 55 53 49 4e 47 28 72 69  IN blob USING(ri
3a60: 64 29 22 0a 20 20 20 20 22 20 57 48 45 52 45 20  d)".    " WHERE 
3a70: 4e 4f 54 20 45 58 49 53 54 53 28 53 45 4c 45 43  NOT EXISTS(SELEC
3a80: 54 20 31 20 46 52 4f 4d 20 73 68 75 6e 20 57 48  T 1 FROM shun WH
3a90: 45 52 45 20 75 75 69 64 3d 62 6c 6f 62 2e 75 75  ERE uuid=blob.uu
3aa0: 69 64 29 22 0a 20 20 20 20 22 20 20 20 41 4e 44  id)".    "   AND
3ab0: 20 4e 4f 54 20 45 58 49 53 54 53 28 53 45 4c 45   NOT EXISTS(SELE
3ac0: 43 54 20 31 20 46 52 4f 4d 20 70 72 69 76 61 74  CT 1 FROM privat
3ad0: 65 20 57 48 45 52 45 20 72 69 64 3d 62 6c 6f 62  e WHERE rid=blob
3ae0: 2e 72 69 64 29 22 0a 20 20 29 3b 0a 20 20 77 68  .rid)".  );.  wh
3af0: 69 6c 65 28 20 64 62 5f 73 74 65 70 28 26 71 29  ile( db_step(&q)
3b00: 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a  ==SQLITE_ROW ){.
3b10: 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66      blob_appendf
3b20: 28 70 58 66 65 72 2d 3e 70 4f 75 74 2c 20 22 69  (pXfer->pOut, "i
3b30: 67 6f 74 20 25 73 5c 6e 22 2c 20 64 62 5f 63 6f  got %s\n", db_co
3b40: 6c 75 6d 6e 5f 74 65 78 74 28 26 71 2c 20 30 29  lumn_text(&q, 0)
3b50: 29 3b 0a 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20  );.    cnt++;.  
3b60: 7d 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28  }.  db_finalize(
3b70: 26 71 29 3b 0a 20 20 72 65 74 75 72 6e 20 63 6e  &q);.  return cn
3b80: 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 6e 64  t;.}../*.** Send
3b90: 20 61 6e 20 69 67 6f 74 20 6d 65 73 73 61 67 65   an igot message
3ba0: 20 66 6f 72 20 65 76 65 72 79 20 61 72 74 69 66   for every artif
3bb0: 61 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  act..*/.static v
3bc0: 6f 69 64 20 73 65 6e 64 5f 61 6c 6c 28 58 66 65  oid send_all(Xfe
3bd0: 72 20 2a 70 58 66 65 72 29 7b 0a 20 20 53 74 6d  r *pXfer){.  Stm
3be0: 74 20 71 3b 0a 20 20 64 62 5f 70 72 65 70 61 72  t q;.  db_prepar
3bf0: 65 28 26 71 2c 20 0a 20 20 20 20 22 53 45 4c 45  e(&q, .    "SELE
3c00: 43 54 20 75 75 69 64 20 46 52 4f 4d 20 62 6c 6f  CT uuid FROM blo
3c10: 62 20 22 0a 20 20 20 20 22 20 57 48 45 52 45 20  b ".    " WHERE 
3c20: 4e 4f 54 20 45 58 49 53 54 53 28 53 45 4c 45 43  NOT EXISTS(SELEC
3c30: 54 20 31 20 46 52 4f 4d 20 73 68 75 6e 20 57 48  T 1 FROM shun WH
3c40: 45 52 45 20 75 75 69 64 3d 62 6c 6f 62 2e 75 75  ERE uuid=blob.uu
3c50: 69 64 29 22 0a 20 20 20 20 22 20 20 20 41 4e 44  id)".    "   AND
3c60: 20 4e 4f 54 20 45 58 49 53 54 53 28 53 45 4c 45   NOT EXISTS(SELE
3c70: 43 54 20 31 20 46 52 4f 4d 20 70 72 69 76 61 74  CT 1 FROM privat
3c80: 65 20 57 48 45 52 45 20 72 69 64 3d 62 6c 6f 62  e WHERE rid=blob
3c90: 2e 72 69 64 29 22 0a 20 20 29 3b 0a 20 20 77 68  .rid)".  );.  wh
3ca0: 69 6c 65 28 20 64 62 5f 73 74 65 70 28 26 71 29  ile( db_step(&q)
3cb0: 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a  ==SQLITE_ROW ){.
3cc0: 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66      blob_appendf
3cd0: 28 70 58 66 65 72 2d 3e 70 4f 75 74 2c 20 22 69  (pXfer->pOut, "i
3ce0: 67 6f 74 20 25 73 5c 6e 22 2c 20 64 62 5f 63 6f  got %s\n", db_co
3cf0: 6c 75 6d 6e 5f 74 65 78 74 28 26 71 2c 20 30 29  lumn_text(&q, 0)
3d00: 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 66 69 6e 61  );.  }.  db_fina
3d10: 6c 69 7a 65 28 26 71 29 3b 0a 7d 0a 0a 2f 2a 0a  lize(&q);.}../*.
3d20: 2a 2a 20 53 65 6e 64 20 61 20 73 69 6e 67 6c 65  ** Send a single
3d30: 20 63 6f 6e 66 69 67 20 63 61 72 64 20 66 6f 72   config card for
3d40: 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 69   configuration i
3d50: 74 65 6d 20 7a 4e 61 6d 65 0a 2a 2f 0a 73 74 61  tem zName.*/.sta
3d60: 74 69 63 20 76 6f 69 64 20 73 65 6e 64 5f 63 6f  tic void send_co
3d70: 6e 66 69 67 5f 63 61 72 64 28 58 66 65 72 20 2a  nfig_card(Xfer *
3d80: 70 58 66 65 72 2c 20 63 6f 6e 73 74 20 63 68 61  pXfer, const cha
3d90: 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 69 66 28  r *zName){.  if(
3da0: 20 7a 4e 61 6d 65 5b 30 5d 21 3d 27 40 27 20 29   zName[0]!='@' )
3db0: 7b 0a 20 20 20 20 42 6c 6f 62 20 76 61 6c 3b 0a  {.    Blob val;.
3dc0: 20 20 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 76      blob_zero(&v
3dd0: 61 6c 29 3b 0a 20 20 20 20 64 62 5f 62 6c 6f 62  al);.    db_blob
3de0: 28 26 76 61 6c 2c 20 22 53 45 4c 45 43 54 20 76  (&val, "SELECT v
3df0: 61 6c 75 65 20 46 52 4f 4d 20 63 6f 6e 66 69 67  alue FROM config
3e00: 20 57 48 45 52 45 20 6e 61 6d 65 3d 25 51 22 2c   WHERE name=%Q",
3e10: 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28   zName);.    if(
3e20: 20 62 6c 6f 62 5f 73 69 7a 65 28 26 76 61 6c 29   blob_size(&val)
3e30: 3e 30 20 29 7b 0a 20 20 20 20 20 20 62 6c 6f 62  >0 ){.      blob
3e40: 5f 61 70 70 65 6e 64 66 28 70 58 66 65 72 2d 3e  _appendf(pXfer->
3e50: 70 4f 75 74 2c 20 22 63 6f 6e 66 69 67 20 25 73  pOut, "config %s
3e60: 20 25 64 5c 6e 22 2c 20 7a 4e 61 6d 65 2c 20 62   %d\n", zName, b
3e70: 6c 6f 62 5f 73 69 7a 65 28 26 76 61 6c 29 29 3b  lob_size(&val));
3e80: 0a 20 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 65  .      blob_appe
3e90: 6e 64 28 70 58 66 65 72 2d 3e 70 4f 75 74 2c 20  nd(pXfer->pOut, 
3ea0: 62 6c 6f 62 5f 62 75 66 66 65 72 28 26 76 61 6c  blob_buffer(&val
3eb0: 29 2c 20 62 6c 6f 62 5f 73 69 7a 65 28 26 76 61  ), blob_size(&va
3ec0: 6c 29 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f  l));.      blob_
3ed0: 72 65 73 65 74 28 26 76 61 6c 29 3b 0a 20 20 20  reset(&val);.   
3ee0: 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 28 70     blob_append(p
3ef0: 58 66 65 72 2d 3e 70 4f 75 74 2c 20 22 5c 6e 22  Xfer->pOut, "\n"
3f00: 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  , 1);.    }.  }e
3f10: 6c 73 65 7b 0a 20 20 20 20 42 6c 6f 62 20 63 6f  lse{.    Blob co
3f20: 6e 74 65 6e 74 3b 0a 20 20 20 20 62 6c 6f 62 5f  ntent;.    blob_
3f30: 7a 65 72 6f 28 26 63 6f 6e 74 65 6e 74 29 3b 0a  zero(&content);.
3f40: 20 20 20 20 63 6f 6e 66 69 67 75 72 65 5f 72 65      configure_re
3f50: 6e 64 65 72 5f 73 70 65 63 69 61 6c 5f 6e 61 6d  nder_special_nam
3f60: 65 28 7a 4e 61 6d 65 2c 20 26 63 6f 6e 74 65 6e  e(zName, &conten
3f70: 74 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70  t);.    blob_app
3f80: 65 6e 64 66 28 70 58 66 65 72 2d 3e 70 4f 75 74  endf(pXfer->pOut
3f90: 2c 20 22 63 6f 6e 66 69 67 20 25 73 20 25 64 5c  , "config %s %d\
3fa0: 6e 25 73 5c 6e 22 2c 20 7a 4e 61 6d 65 2c 0a 20  n%s\n", zName,. 
3fb0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3fc0: 62 6c 6f 62 5f 73 69 7a 65 28 26 63 6f 6e 74 65  blob_size(&conte
3fd0: 6e 74 29 2c 20 62 6c 6f 62 5f 73 74 72 28 26 63  nt), blob_str(&c
3fe0: 6f 6e 74 65 6e 74 29 29 3b 0a 20 20 20 20 62 6c  ontent));.    bl
3ff0: 6f 62 5f 72 65 73 65 74 28 26 63 6f 6e 74 65 6e  ob_reset(&conten
4000: 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a  t);.  }.}.../*.*
4010: 2a 20 49 66 20 74 68 69 73 20 76 61 72 69 61 62  * If this variab
4020: 6c 65 20 69 73 20 73 65 74 2c 20 64 69 73 61 62  le is set, disab
4030: 6c 65 20 6c 6f 67 69 6e 20 63 68 65 63 6b 73 2e  le login checks.
4040: 20 20 55 73 65 64 20 66 6f 72 20 64 65 62 75 67    Used for debug
4050: 67 69 6e 67 0a 2a 2a 20 6f 6e 6c 79 2e 0a 2a 2f  ging.** only..*/
4060: 0a 73 74 61 74 69 63 20 69 6e 74 20 64 69 73 61  .static int disa
4070: 62 6c 65 4c 6f 67 69 6e 20 3d 20 30 3b 0a 0a 2f  bleLogin = 0;../
4080: 2a 0a 2a 2a 20 54 68 65 20 43 47 49 2f 48 54 54  *.** The CGI/HTT
4090: 50 20 70 72 65 70 72 6f 63 65 73 73 6f 72 20 61  P preprocessor a
40a0: 6c 77 61 79 73 20 72 65 64 69 72 65 63 74 73 20  lways redirects 
40b0: 72 65 71 75 65 73 74 73 20 77 69 74 68 20 61 20  requests with a 
40c0: 63 6f 6e 74 65 6e 74 2d 74 79 70 65 0a 2a 2a 20  content-type.** 
40d0: 6f 66 20 61 70 70 6c 69 63 61 74 69 6f 6e 2f 78  of application/x
40e0: 2d 66 6f 73 73 69 6c 20 6f 72 20 61 70 70 6c 69  -fossil or appli
40f0: 63 61 74 69 6f 6e 2f 78 2d 66 6f 73 73 69 6c 2d  cation/x-fossil-
4100: 64 65 62 75 67 20 74 6f 20 74 68 69 73 20 70 61  debug to this pa
4110: 67 65 2c 0a 2a 2a 20 72 65 67 61 72 64 6c 65 73  ge,.** regardles
4120: 73 20 6f 66 20 77 68 61 74 20 70 61 74 68 20 77  s of what path w
4130: 61 73 20 73 70 65 63 69 66 69 65 64 20 69 6e 20  as specified in 
4140: 74 68 65 20 48 54 54 50 20 68 65 61 64 65 72 2e  the HTTP header.
4150: 20 20 54 68 69 73 20 61 6c 6c 6f 77 73 0a 2a 2a    This allows.**
4160: 20 63 6c 6f 6e 65 20 63 6c 69 65 6e 74 73 20 74   clone clients t
4170: 6f 20 73 70 65 63 69 66 79 20 61 20 55 52 4c 20  o specify a URL 
4180: 74 68 61 74 20 6f 6d 69 74 73 20 64 65 66 61 75  that omits defau
4190: 6c 74 20 70 61 74 68 6e 61 6d 65 73 2c 20 73 75  lt pathnames, su
41a0: 63 68 0a 2a 2a 20 61 73 20 22 68 74 74 70 3a 2f  ch.** as "http:/
41b0: 2f 66 6f 73 73 69 6c 2d 73 63 6d 2e 6d 6f 72 67  /fossil-scm.morg
41c0: 2f 22 20 69 6e 73 74 65 61 64 20 6f 66 20 22 68  /" instead of "h
41d0: 74 74 70 3a 2f 2f 66 6f 73 73 69 6c 2d 73 63 6d  ttp://fossil-scm
41e0: 2e 6f 72 67 2f 69 6e 64 65 78 2e 63 67 69 22 2e  .org/index.cgi".
41f0: 0a 2a 2a 0a 2a 2a 20 57 45 42 50 41 47 45 3a 20  .**.** WEBPAGE: 
4200: 78 66 65 72 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  xfer.**.** This 
4210: 69 73 20 74 68 65 20 74 72 61 6e 73 66 65 72 20  is the transfer 
4220: 68 61 6e 64 6c 65 72 20 6f 6e 20 74 68 65 20 73  handler on the s
4230: 65 72 76 65 72 20 73 69 64 65 2e 20 20 54 68 65  erver side.  The
4240: 20 74 72 61 6e 73 66 65 72 0a 2a 2a 20 6d 65 73   transfer.** mes
4250: 73 61 67 65 20 68 61 73 20 62 65 65 6e 20 75 6e  sage has been un
4260: 63 6f 6d 70 72 65 73 73 65 64 20 61 6e 64 20 70  compressed and p
4270: 6c 61 63 65 64 20 69 6e 20 74 68 65 20 67 2e 63  laced in the g.c
4280: 67 69 49 6e 20 62 6c 6f 62 2e 0a 2a 2a 20 50 72  giIn blob..** Pr
4290: 6f 63 65 73 73 20 74 68 69 73 20 6d 65 73 73 61  ocess this messa
42a0: 67 65 20 61 6e 64 20 66 6f 72 6d 20 61 6e 20 61  ge and form an a
42b0: 70 70 72 6f 70 72 69 61 74 65 20 72 65 70 6c 79  ppropriate reply
42c0: 2e 0a 2a 2f 0a 76 6f 69 64 20 70 61 67 65 5f 78  ..*/.void page_x
42d0: 66 65 72 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74  fer(void){.  int
42e0: 20 69 73 50 75 6c 6c 20 3d 20 30 3b 0a 20 20 69   isPull = 0;.  i
42f0: 6e 74 20 69 73 50 75 73 68 20 3d 20 30 3b 0a 20  nt isPush = 0;. 
4300: 20 69 6e 74 20 6e 45 72 72 20 3d 20 30 3b 0a 20   int nErr = 0;. 
4310: 20 58 66 65 72 20 78 66 65 72 3b 0a 20 20 69 6e   Xfer xfer;.  in
4320: 74 20 64 65 6c 74 61 46 6c 61 67 20 3d 20 30 3b  t deltaFlag = 0;
4330: 0a 20 20 69 6e 74 20 69 73 43 6c 6f 6e 65 20 3d  .  int isClone =
4340: 20 30 3b 0a 20 20 69 6e 74 20 6e 47 69 6d 6d 65   0;.  int nGimme
4350: 20 3d 20 30 3b 0a 20 20 69 6e 74 20 73 69 7a 65   = 0;.  int size
4360: 3b 0a 20 20 69 6e 74 20 72 65 63 76 43 6f 6e 66  ;.  int recvConf
4370: 69 67 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 73  ig = 0;..  if( s
4380: 74 72 63 6d 70 28 50 44 28 22 52 45 51 55 45 53  trcmp(PD("REQUES
4390: 54 5f 4d 45 54 48 4f 44 22 2c 22 50 4f 53 54 22  T_METHOD","POST"
43a0: 29 2c 22 50 4f 53 54 22 29 20 29 7b 0a 20 20 20  ),"POST") ){.   
43b0: 20 20 66 6f 73 73 69 6c 5f 72 65 64 69 72 65 63    fossil_redirec
43c0: 74 5f 68 6f 6d 65 28 29 3b 0a 20 20 7d 0a 20 20  t_home();.  }.  
43d0: 6d 65 6d 73 65 74 28 26 78 66 65 72 2c 20 30 2c  memset(&xfer, 0,
43e0: 20 73 69 7a 65 6f 66 28 78 66 65 72 29 29 3b 0a   sizeof(xfer));.
43f0: 20 20 62 6c 6f 62 61 72 72 61 79 5f 7a 65 72 6f    blobarray_zero
4400: 28 78 66 65 72 2e 61 54 6f 6b 65 6e 2c 20 63 6f  (xfer.aToken, co
4410: 75 6e 74 28 78 66 65 72 2e 61 54 6f 6b 65 6e 29  unt(xfer.aToken)
4420: 29 3b 0a 20 20 63 67 69 5f 73 65 74 5f 63 6f 6e  );.  cgi_set_con
4430: 74 65 6e 74 5f 74 79 70 65 28 67 2e 7a 43 6f 6e  tent_type(g.zCon
4440: 74 65 6e 74 54 79 70 65 29 3b 0a 20 20 62 6c 6f  tentType);.  blo
4450: 62 5f 7a 65 72 6f 28 26 78 66 65 72 2e 65 72 72  b_zero(&xfer.err
4460: 29 3b 0a 20 20 78 66 65 72 2e 70 49 6e 20 3d 20  );.  xfer.pIn = 
4470: 26 67 2e 63 67 69 49 6e 3b 0a 20 20 78 66 65 72  &g.cgiIn;.  xfer
4480: 2e 70 4f 75 74 20 3d 20 63 67 69 5f 6f 75 74 70  .pOut = cgi_outp
4490: 75 74 5f 62 6c 6f 62 28 29 3b 0a 20 20 78 66 65  ut_blob();.  xfe
44a0: 72 2e 6d 78 53 65 6e 64 20 3d 20 64 62 5f 67 65  r.mxSend = db_ge
44b0: 74 5f 69 6e 74 28 22 6d 61 78 2d 64 6f 77 6e 6c  t_int("max-downl
44c0: 6f 61 64 22 2c 20 35 30 30 30 30 30 30 29 3b 0a  oad", 5000000);.
44d0: 20 20 67 2e 78 66 65 72 50 61 6e 69 63 20 3d 20    g.xferPanic = 
44e0: 31 3b 0a 0a 20 20 64 62 5f 62 65 67 69 6e 5f 74  1;..  db_begin_t
44f0: 72 61 6e 73 61 63 74 69 6f 6e 28 29 3b 0a 20 20  ransaction();.  
4500: 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20  db_multi_exec(. 
4510: 20 20 20 20 22 43 52 45 41 54 45 20 54 45 4d 50      "CREATE TEMP
4520: 20 54 41 42 4c 45 20 6f 6e 72 65 6d 6f 74 65 28   TABLE onremote(
4530: 72 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d  rid INTEGER PRIM
4540: 41 52 59 20 4b 45 59 29 3b 22 0a 20 20 29 3b 0a  ARY KEY);".  );.
4550: 20 20 6d 61 6e 69 66 65 73 74 5f 63 72 6f 73 73    manifest_cross
4560: 6c 69 6e 6b 5f 62 65 67 69 6e 28 29 3b 0a 20 20  link_begin();.  
4570: 77 68 69 6c 65 28 20 62 6c 6f 62 5f 6c 69 6e 65  while( blob_line
4580: 28 78 66 65 72 2e 70 49 6e 2c 20 26 78 66 65 72  (xfer.pIn, &xfer
4590: 2e 6c 69 6e 65 29 20 29 7b 0a 20 20 20 20 69 66  .line) ){.    if
45a0: 28 20 62 6c 6f 62 5f 62 75 66 66 65 72 28 26 78  ( blob_buffer(&x
45b0: 66 65 72 2e 6c 69 6e 65 29 5b 30 5d 3d 3d 27 23  fer.line)[0]=='#
45c0: 27 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  ' ) continue;.  
45d0: 20 20 78 66 65 72 2e 6e 54 6f 6b 65 6e 20 3d 20    xfer.nToken = 
45e0: 62 6c 6f 62 5f 74 6f 6b 65 6e 69 7a 65 28 26 78  blob_tokenize(&x
45f0: 66 65 72 2e 6c 69 6e 65 2c 20 78 66 65 72 2e 61  fer.line, xfer.a
4600: 54 6f 6b 65 6e 2c 20 63 6f 75 6e 74 28 78 66 65  Token, count(xfe
4610: 72 2e 61 54 6f 6b 65 6e 29 29 3b 0a 0a 20 20 20  r.aToken));..   
4620: 20 2f 2a 20 20 20 66 69 6c 65 20 55 55 49 44 20   /*   file UUID 
4630: 53 49 5a 45 20 5c 6e 20 43 4f 4e 54 45 4e 54 0a  SIZE \n CONTENT.
4640: 20 20 20 20 2a 2a 20 20 20 66 69 6c 65 20 55 55      **   file UU
4650: 49 44 20 44 45 4c 54 41 53 52 43 20 53 49 5a 45  ID DELTASRC SIZE
4660: 20 5c 6e 20 43 4f 4e 54 45 4e 54 0a 20 20 20 20   \n CONTENT.    
4670: 2a 2a 0a 20 20 20 20 2a 2a 20 41 63 63 65 70 74  **.    ** Accept
4680: 20 61 20 66 69 6c 65 20 66 72 6f 6d 20 74 68 65   a file from the
4690: 20 63 6c 69 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a   client..    */.
46a0: 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28      if( blob_eq(
46b0: 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c  &xfer.aToken[0],
46c0: 20 22 66 69 6c 65 22 29 20 29 7b 0a 20 20 20 20   "file") ){.    
46d0: 20 20 69 66 28 20 21 69 73 50 75 73 68 20 29 7b    if( !isPush ){
46e0: 0a 20 20 20 20 20 20 20 20 63 67 69 5f 72 65 73  .        cgi_res
46f0: 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20  et_content();.  
4700: 20 20 20 20 20 20 40 20 65 72 72 6f 72 20 6e 6f        @ error no
4710: 74 5c 73 61 75 74 68 6f 72 69 7a 65 64 5c 73 74  t\sauthorized\st
4720: 6f 5c 73 77 72 69 74 65 0a 20 20 20 20 20 20 20  o\swrite.       
4730: 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20   nErr++;.       
4740: 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
4750: 20 20 20 20 20 20 78 66 65 72 5f 61 63 63 65 70        xfer_accep
4760: 74 5f 66 69 6c 65 28 26 78 66 65 72 29 3b 0a 20  t_file(&xfer);. 
4770: 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 73 69       if( blob_si
4780: 7a 65 28 26 78 66 65 72 2e 65 72 72 29 20 29 7b  ze(&xfer.err) ){
4790: 0a 20 20 20 20 20 20 20 20 63 67 69 5f 72 65 73  .        cgi_res
47a0: 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20  et_content();.  
47b0: 20 20 20 20 20 20 40 20 65 72 72 6f 72 20 25 54        @ error %T
47c0: 28 62 6c 6f 62 5f 73 74 72 28 26 78 66 65 72 2e  (blob_str(&xfer.
47d0: 65 72 72 29 29 0a 20 20 20 20 20 20 20 20 6e 45  err)).        nE
47e0: 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 62 72  rr++;.        br
47f0: 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
4800: 20 7d 65 6c 73 65 0a 0a 20 20 20 20 2f 2a 20 20   }else..    /*  
4810: 20 67 69 6d 6d 65 20 55 55 49 44 0a 20 20 20 20   gimme UUID.    
4820: 2a 2a 0a 20 20 20 20 2a 2a 20 43 6c 69 65 6e 74  **.    ** Client
4830: 20 69 73 20 72 65 71 75 65 73 74 69 6e 67 20 61   is requesting a
4840: 20 66 69 6c 65 2e 20 20 53 65 6e 64 20 69 74 2e   file.  Send it.
4850: 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
4860: 62 6c 6f 62 5f 65 71 28 26 78 66 65 72 2e 61 54  blob_eq(&xfer.aT
4870: 6f 6b 65 6e 5b 30 5d 2c 20 22 67 69 6d 6d 65 22  oken[0], "gimme"
4880: 29 0a 20 20 20 20 20 26 26 20 78 66 65 72 2e 6e  ).     && xfer.n
4890: 54 6f 6b 65 6e 3d 3d 32 0a 20 20 20 20 20 26 26  Token==2.     &&
48a0: 20 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26 78   blob_is_uuid(&x
48b0: 66 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d 29 0a 20  fer.aToken[1]). 
48c0: 20 20 20 29 7b 0a 20 20 20 20 20 20 6e 47 69 6d     ){.      nGim
48d0: 6d 65 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20  me++;.      if( 
48e0: 69 73 50 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20  isPull ){.      
48f0: 20 20 69 6e 74 20 72 69 64 20 3d 20 72 69 64 5f    int rid = rid_
4900: 66 72 6f 6d 5f 75 75 69 64 28 26 78 66 65 72 2e  from_uuid(&xfer.
4910: 61 54 6f 6b 65 6e 5b 31 5d 2c 20 30 29 3b 0a 20  aToken[1], 0);. 
4920: 20 20 20 20 20 20 20 69 66 28 20 72 69 64 20 29         if( rid )
4930: 7b 0a 20 20 20 20 20 20 20 20 20 20 73 65 6e 64  {.          send
4940: 5f 66 69 6c 65 28 26 78 66 65 72 2c 20 72 69 64  _file(&xfer, rid
4950: 2c 20 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31  , &xfer.aToken[1
4960: 5d 2c 20 64 65 6c 74 61 46 6c 61 67 29 3b 0a 20  ], deltaFlag);. 
4970: 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
4980: 0a 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20 20  .    }else..    
4990: 2f 2a 20 20 20 69 67 6f 74 20 55 55 49 44 0a 20  /*   igot UUID. 
49a0: 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 6c 69     **.    ** Cli
49b0: 65 6e 74 20 61 6e 6e 6f 75 6e 63 65 73 20 74 68  ent announces th
49c0: 61 74 20 69 74 20 68 61 73 20 61 20 70 61 72 74  at it has a part
49d0: 69 63 75 6c 61 72 20 66 69 6c 65 2e 0a 20 20 20  icular file..   
49e0: 20 2a 2f 0a 20 20 20 20 69 66 28 20 78 66 65 72   */.    if( xfer
49f0: 2e 6e 54 6f 6b 65 6e 3d 3d 32 0a 20 20 20 20 20  .nToken==2.     
4a00: 26 26 20 62 6c 6f 62 5f 65 71 28 26 78 66 65 72  && blob_eq(&xfer
4a10: 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 69 67 6f  .aToken[0], "igo
4a20: 74 22 29 0a 20 20 20 20 20 26 26 20 62 6c 6f 62  t").     && blob
4a30: 5f 69 73 5f 75 75 69 64 28 26 78 66 65 72 2e 61  _is_uuid(&xfer.a
4a40: 54 6f 6b 65 6e 5b 31 5d 29 0a 20 20 20 20 29 7b  Token[1]).    ){
4a50: 0a 20 20 20 20 20 20 69 66 28 20 69 73 50 75 73  .      if( isPus
4a60: 68 20 29 7b 0a 20 20 20 20 20 20 20 20 72 69 64  h ){.        rid
4a70: 5f 66 72 6f 6d 5f 75 75 69 64 28 26 78 66 65 72  _from_uuid(&xfer
4a80: 2e 61 54 6f 6b 65 6e 5b 31 5d 2c 20 31 29 3b 0a  .aToken[1], 1);.
4a90: 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
4aa0: 65 0a 20 20 0a 20 20 20 20 0a 20 20 20 20 2f 2a  e.  .    .    /*
4ab0: 20 20 20 20 70 75 6c 6c 20 20 53 45 52 56 45 52      pull  SERVER
4ac0: 43 4f 44 45 20 20 50 52 4f 4a 45 43 54 43 4f 44  CODE  PROJECTCOD
4ad0: 45 0a 20 20 20 20 2a 2a 20 20 20 20 70 75 73 68  E.    **    push
4ae0: 20 20 53 45 52 56 45 52 43 4f 44 45 20 20 50 52    SERVERCODE  PR
4af0: 4f 4a 45 43 54 43 4f 44 45 0a 20 20 20 20 2a 2a  OJECTCODE.    **
4b00: 0a 20 20 20 20 2a 2a 20 54 68 65 20 63 6c 69 65  .    ** The clie
4b10: 6e 74 20 77 61 6e 74 73 20 65 69 74 68 65 72 20  nt wants either 
4b20: 73 65 6e 64 20 6f 72 20 72 65 63 65 69 76 65 2e  send or receive.
4b30: 20 20 54 68 65 20 73 65 72 76 65 72 20 73 68 6f    The server sho
4b40: 75 6c 64 0a 20 20 20 20 2a 2a 20 76 65 72 69 66  uld.    ** verif
4b50: 79 20 74 68 61 74 20 74 68 65 20 70 72 6f 6a 65  y that the proje
4b60: 63 74 20 63 6f 64 65 20 6d 61 74 63 68 65 73 20  ct code matches 
4b70: 61 6e 64 20 74 68 61 74 20 74 68 65 20 73 65 72  and that the ser
4b80: 76 65 72 20 63 6f 64 65 0a 20 20 20 20 2a 2a 20  ver code.    ** 
4b90: 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 2e 0a  does not match..
4ba0: 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 78      */.    if( x
4bb0: 66 65 72 2e 6e 54 6f 6b 65 6e 3d 3d 33 0a 20 20  fer.nToken==3.  
4bc0: 20 20 20 26 26 20 28 62 6c 6f 62 5f 65 71 28 26     && (blob_eq(&
4bd0: 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20  xfer.aToken[0], 
4be0: 22 70 75 6c 6c 22 29 20 7c 7c 20 62 6c 6f 62 5f  "pull") || blob_
4bf0: 65 71 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b  eq(&xfer.aToken[
4c00: 30 5d 2c 20 22 70 75 73 68 22 29 29 0a 20 20 20  0], "push")).   
4c10: 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69    && blob_is_uui
4c20: 64 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31  d(&xfer.aToken[1
4c30: 5d 29 0a 20 20 20 20 20 26 26 20 62 6c 6f 62 5f  ]).     && blob_
4c40: 69 73 5f 75 75 69 64 28 26 78 66 65 72 2e 61 54  is_uuid(&xfer.aT
4c50: 6f 6b 65 6e 5b 32 5d 29 0a 20 20 20 20 29 7b 0a  oken[2]).    ){.
4c60: 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
4c70: 20 2a 7a 50 43 6f 64 65 3b 0a 0a 23 69 66 20 30   *zPCode;..#if 0
4c80: 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62  .      /* This b
4c90: 6c 6f 63 6b 20 63 68 65 63 6b 73 20 74 6f 20 73  lock checks to s
4ca0: 65 65 20 69 66 20 61 20 73 65 72 76 65 72 20 69  ee if a server i
4cb0: 73 20 74 72 79 69 6e 67 20 74 6f 20 73 79 6e 63  s trying to sync
4cc0: 20 77 69 74 68 20 69 74 73 65 6c 66 2e 0a 20 20   with itself..  
4cd0: 20 20 20 20 2a 2a 20 54 68 69 73 20 75 73 65 64      ** This used
4ce0: 20 74 6f 20 62 65 20 64 69 73 61 6c 6c 6f 77 65   to be disallowe
4cf0: 64 2c 20 62 75 74 20 49 20 63 61 6e 6e 6f 74 20  d, but I cannot 
4d00: 74 68 69 6e 6b 20 6f 66 20 61 6e 79 20 73 69 67  think of any sig
4d10: 6e 69 66 69 63 61 6e 74 0a 20 20 20 20 20 20 2a  nificant.      *
4d20: 2a 20 68 61 72 6d 2c 20 73 6f 20 49 20 68 61 76  * harm, so I hav
4d30: 65 20 64 69 73 61 62 6c 65 64 20 74 68 65 20 63  e disabled the c
4d40: 68 65 63 6b 2e 0a 20 20 20 20 20 20 2a 2a 0a 20  heck..      **. 
4d50: 20 20 20 20 20 2a 2a 20 57 69 74 68 20 74 68 69       ** With thi
4d60: 73 20 63 68 65 63 6b 20 64 69 73 61 62 6c 65 64  s check disabled
4d70: 2c 20 69 74 20 69 73 20 73 75 66 66 69 63 69 65  , it is sufficie
4d80: 6e 74 20 74 6f 20 63 6f 70 79 20 74 68 65 20 72  nt to copy the r
4d90: 65 70 6f 73 69 74 6f 72 79 0a 20 20 20 20 20 20  epository.      
4da0: 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20 4e 6f  ** database.  No
4db0: 20 6e 65 65 64 20 74 6f 20 72 75 6e 20 63 6c 6f   need to run clo
4dc0: 6e 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ne..      */.   
4dd0: 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
4de0: 53 43 6f 64 65 3b 0a 20 20 20 20 20 20 7a 53 43  SCode;.      zSC
4df0: 6f 64 65 20 3d 20 64 62 5f 67 65 74 28 22 73 65  ode = db_get("se
4e00: 72 76 65 72 2d 63 6f 64 65 22 2c 20 30 29 3b 0a  rver-code", 0);.
4e10: 20 20 20 20 20 20 69 66 28 20 7a 53 43 6f 64 65        if( zSCode
4e20: 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 66  ==0 ){.        f
4e30: 6f 73 73 69 6c 5f 70 61 6e 69 63 28 22 6d 69 73  ossil_panic("mis
4e40: 73 69 6e 67 20 73 65 72 76 65 72 20 63 6f 64 65  sing server code
4e50: 22 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ");.      }.    
4e60: 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 5f 73 74    if( blob_eq_st
4e70: 72 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31  r(&xfer.aToken[1
4e80: 5d 2c 20 7a 53 43 6f 64 65 2c 20 2d 31 29 20 29  ], zSCode, -1) )
4e90: 7b 0a 20 20 20 20 20 20 20 20 63 67 69 5f 72 65  {.        cgi_re
4ea0: 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a 20  set_content();. 
4eb0: 20 20 20 20 20 20 20 40 20 65 72 72 6f 72 20 73         @ error s
4ec0: 65 72 76 65 72 5c 73 6c 6f 6f 70 0a 20 20 20 20  erver\sloop.    
4ed0: 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20      nErr++;.    
4ee0: 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
4ef0: 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20   }.#endif..     
4f00: 20 7a 50 43 6f 64 65 20 3d 20 64 62 5f 67 65 74   zPCode = db_get
4f10: 28 22 70 72 6f 6a 65 63 74 2d 63 6f 64 65 22 2c  ("project-code",
4f20: 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a   0);.      if( z
4f30: 50 43 6f 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20  PCode==0 ){.    
4f40: 20 20 20 20 66 6f 73 73 69 6c 5f 70 61 6e 69 63      fossil_panic
4f50: 28 22 6d 69 73 73 69 6e 67 20 70 72 6f 6a 65 63  ("missing projec
4f60: 74 20 63 6f 64 65 22 29 3b 0a 20 20 20 20 20 20  t code");.      
4f70: 7d 0a 20 20 20 20 20 20 69 66 28 20 21 62 6c 6f  }.      if( !blo
4f80: 62 5f 65 71 5f 73 74 72 28 26 78 66 65 72 2e 61  b_eq_str(&xfer.a
4f90: 54 6f 6b 65 6e 5b 32 5d 2c 20 7a 50 43 6f 64 65  Token[2], zPCode
4fa0: 2c 20 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20  , -1) ){.       
4fb0: 20 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e 74 65   cgi_reset_conte
4fc0: 6e 74 28 29 3b 0a 20 20 20 20 20 20 20 20 40 20  nt();.        @ 
4fd0: 65 72 72 6f 72 20 77 72 6f 6e 67 5c 73 70 72 6f  error wrong\spro
4fe0: 6a 65 63 74 0a 20 20 20 20 20 20 20 20 6e 45 72  ject.        nEr
4ff0: 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 62 72 65  r++;.        bre
5000: 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
5010: 20 20 6c 6f 67 69 6e 5f 63 68 65 63 6b 5f 63 72    login_check_cr
5020: 65 64 65 6e 74 69 61 6c 73 28 29 3b 0a 20 20 20  edentials();.   
5030: 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26     if( blob_eq(&
5040: 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20  xfer.aToken[0], 
5050: 22 70 75 6c 6c 22 29 20 29 7b 0a 20 20 20 20 20  "pull") ){.     
5060: 20 20 20 69 66 28 20 21 67 2e 6f 6b 52 65 61 64     if( !g.okRead
5070: 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 67   ){.          cg
5080: 69 5f 72 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28  i_reset_content(
5090: 29 3b 0a 20 20 20 20 20 20 20 20 20 20 40 20 65  );.          @ e
50a0: 72 72 6f 72 20 6e 6f 74 5c 73 61 75 74 68 6f 72  rror not\sauthor
50b0: 69 7a 65 64 5c 73 74 6f 5c 73 72 65 61 64 0a 20  ized\sto\sread. 
50c0: 20 20 20 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b           nErr++;
50d0: 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
50e0: 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
50f0: 20 20 20 20 69 73 50 75 6c 6c 20 3d 20 31 3b 0a      isPull = 1;.
5100: 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
5110: 20 20 20 20 20 69 66 28 20 21 67 2e 6f 6b 57 72       if( !g.okWr
5120: 69 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ite ){.         
5130: 20 69 66 28 20 21 69 73 50 75 6c 6c 20 29 7b 0a   if( !isPull ){.
5140: 20 20 20 20 20 20 20 20 20 20 20 20 63 67 69 5f              cgi_
5150: 72 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b  reset_content();
5160: 0a 20 20 20 20 20 20 20 20 20 20 20 20 40 20 65  .            @ e
5170: 72 72 6f 72 20 6e 6f 74 5c 73 61 75 74 68 6f 72  rror not\sauthor
5180: 69 7a 65 64 5c 73 74 6f 5c 73 77 72 69 74 65 0a  ized\sto\swrite.
5190: 20 20 20 20 20 20 20 20 20 20 20 20 6e 45 72 72              nErr
51a0: 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65  ++;.          }e
51b0: 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
51c0: 20 40 20 6d 65 73 73 61 67 65 20 70 75 6c 6c 5c   @ message pull\
51d0: 73 6f 6e 6c 79 5c 73 2d 5c 73 6e 6f 74 5c 73 61  sonly\s-\snot\sa
51e0: 75 74 68 6f 72 69 7a 65 64 5c 73 74 6f 5c 73 70  uthorized\sto\sp
51f0: 75 73 68 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ush.          }.
5200: 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
5210: 20 20 20 20 20 20 20 20 20 69 73 50 75 73 68 20           isPush 
5220: 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 1;.        }. 
5230: 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
5240: 0a 0a 20 20 20 20 2f 2a 20 20 20 20 63 6c 6f 6e  ..    /*    clon
5250: 65 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  e.    **.    ** 
5260: 54 68 65 20 63 6c 69 65 6e 74 20 6b 6e 6f 77 73  The client knows
5270: 20 6e 6f 74 68 69 6e 67 2e 20 20 54 65 6c 6c 20   nothing.  Tell 
5280: 61 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  all..    */.    
5290: 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 78 66 65  if( blob_eq(&xfe
52a0: 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 63 6c  r.aToken[0], "cl
52b0: 6f 6e 65 22 29 20 29 7b 0a 20 20 20 20 20 20 6c  one") ){.      l
52c0: 6f 67 69 6e 5f 63 68 65 63 6b 5f 63 72 65 64 65  ogin_check_crede
52d0: 6e 74 69 61 6c 73 28 29 3b 0a 20 20 20 20 20 20  ntials();.      
52e0: 69 66 28 20 21 67 2e 6f 6b 43 6c 6f 6e 65 20 29  if( !g.okClone )
52f0: 7b 0a 20 20 20 20 20 20 20 20 63 67 69 5f 72 65  {.        cgi_re
5300: 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a 20  set_content();. 
5310: 20 20 20 20 20 20 20 40 20 65 72 72 6f 72 20 6e         @ error n
5320: 6f 74 5c 73 61 75 74 68 6f 72 69 7a 65 64 5c 73  ot\sauthorized\s
5330: 74 6f 5c 73 63 6c 6f 6e 65 0a 20 20 20 20 20 20  to\sclone.      
5340: 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20    nErr++;.      
5350: 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
5360: 0a 20 20 20 20 20 20 69 73 43 6c 6f 6e 65 20 3d  .      isClone =
5370: 20 31 3b 0a 20 20 20 20 20 20 69 73 50 75 6c 6c   1;.      isPull
5380: 20 3d 20 31 3b 0a 20 20 20 20 20 20 64 65 6c 74   = 1;.      delt
5390: 61 46 6c 61 67 20 3d 20 31 3b 0a 20 20 20 20 20  aFlag = 1;.     
53a0: 20 40 20 70 75 73 68 20 25 73 28 64 62 5f 67 65   @ push %s(db_ge
53b0: 74 28 22 73 65 72 76 65 72 2d 63 6f 64 65 22 2c  t("server-code",
53c0: 20 22 78 22 29 29 20 25 73 28 64 62 5f 67 65 74   "x")) %s(db_get
53d0: 28 22 70 72 6f 6a 65 63 74 2d 63 6f 64 65 22 2c  ("project-code",
53e0: 20 22 78 22 29 29 0a 20 20 20 20 7d 65 6c 73 65   "x")).    }else
53f0: 0a 0a 20 20 20 20 2f 2a 20 20 20 20 6c 6f 67 69  ..    /*    logi
5400: 6e 20 20 55 53 45 52 20 20 4e 4f 4e 43 45 20 20  n  USER  NONCE  
5410: 53 49 47 4e 41 54 55 52 45 0a 20 20 20 20 2a 2a  SIGNATURE.    **
5420: 0a 20 20 20 20 2a 2a 20 43 68 65 63 6b 20 66 6f  .    ** Check fo
5430: 72 20 61 20 76 61 6c 69 64 20 6c 6f 67 69 6e 2e  r a valid login.
5440: 20 20 54 68 69 73 20 68 61 73 20 74 6f 20 68 61    This has to ha
5450: 70 70 65 6e 20 62 65 66 6f 72 65 20 61 6e 79 74  ppen before anyt
5460: 68 69 6e 67 20 65 6c 73 65 2e 0a 20 20 20 20 2a  hing else..    *
5470: 2a 20 54 68 65 20 63 6c 69 65 6e 74 20 63 61 6e  * The client can
5480: 20 73 65 6e 64 20 6d 75 6c 74 69 70 6c 65 20 6c   send multiple l
5490: 6f 67 69 6e 73 2e 20 20 50 65 72 6d 69 73 73 69  ogins.  Permissi
54a0: 6f 6e 73 20 61 72 65 20 63 75 6d 75 6c 61 74 69  ons are cumulati
54b0: 76 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ve..    */.    i
54c0: 66 28 20 62 6c 6f 62 5f 65 71 28 26 78 66 65 72  f( blob_eq(&xfer
54d0: 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 6c 6f 67  .aToken[0], "log
54e0: 69 6e 22 29 0a 20 20 20 20 20 26 26 20 78 66 65  in").     && xfe
54f0: 72 2e 6e 54 6f 6b 65 6e 3d 3d 34 0a 20 20 20 20  r.nToken==4.    
5500: 29 7b 0a 20 20 20 20 20 20 69 66 28 20 64 69 73  ){.      if( dis
5510: 61 62 6c 65 4c 6f 67 69 6e 20 29 7b 0a 20 20 20  ableLogin ){.   
5520: 20 20 20 20 20 67 2e 6f 6b 52 65 61 64 20 3d 20       g.okRead = 
5530: 67 2e 6f 6b 57 72 69 74 65 20 3d 20 31 3b 0a 20  g.okWrite = 1;. 
5540: 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63       }else if( c
5550: 68 65 63 6b 5f 74 61 69 6c 5f 68 61 73 68 28 26  heck_tail_hash(&
5560: 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 32 5d 2c 20  xfer.aToken[2], 
5570: 78 66 65 72 2e 70 49 6e 29 20 29 7b 0a 20 20 20  xfer.pIn) ){.   
5580: 20 20 20 20 20 63 68 65 63 6b 5f 6c 6f 67 69 6e       check_login
5590: 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d  (&xfer.aToken[1]
55a0: 2c 20 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 32  , &xfer.aToken[2
55b0: 5d 2c 20 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b  ], &xfer.aToken[
55c0: 33 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  3]);.      }.   
55d0: 20 7d 65 6c 73 65 0a 20 20 20 20 0a 20 20 20 20   }else.    .    
55e0: 2f 2a 20 20 20 20 72 65 71 63 6f 6e 66 69 67 20  /*    reqconfig 
55f0: 20 4e 41 4d 45 0a 20 20 20 20 2a 2a 0a 20 20 20   NAME.    **.   
5600: 20 2a 2a 20 52 65 71 75 65 73 74 20 61 20 63 6f   ** Request a co
5610: 6e 66 69 67 75 72 61 74 69 6f 6e 20 76 61 6c 75  nfiguration valu
5620: 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  e.    */.    if(
5630: 20 62 6c 6f 62 5f 65 71 28 26 78 66 65 72 2e 61   blob_eq(&xfer.a
5640: 54 6f 6b 65 6e 5b 30 5d 2c 20 22 72 65 71 63 6f  Token[0], "reqco
5650: 6e 66 69 67 22 29 0a 20 20 20 20 20 26 26 20 78  nfig").     && x
5660: 66 65 72 2e 6e 54 6f 6b 65 6e 3d 3d 32 0a 20 20  fer.nToken==2.  
5670: 20 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 67    ){.      if( g
5680: 2e 6f 6b 52 65 61 64 20 29 7b 0a 20 20 20 20 20  .okRead ){.     
5690: 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d     char *zName =
56a0: 20 62 6c 6f 62 5f 73 74 72 28 26 78 66 65 72 2e   blob_str(&xfer.
56b0: 61 54 6f 6b 65 6e 5b 31 5d 29 3b 0a 20 20 20 20  aToken[1]);.    
56c0: 20 20 20 20 69 66 28 20 63 6f 6e 66 69 67 75 72      if( configur
56d0: 65 5f 69 73 5f 65 78 70 6f 72 74 61 62 6c 65 28  e_is_exportable(
56e0: 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20  zName) ){.      
56f0: 20 20 20 20 73 65 6e 64 5f 63 6f 6e 66 69 67 5f      send_config_
5700: 63 61 72 64 28 26 78 66 65 72 2c 20 7a 4e 61 6d  card(&xfer, zNam
5710: 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  e);.        }.  
5720: 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 0a      }.    }else.
5730: 20 20 20 20 0a 20 20 20 20 2f 2a 20 20 20 63 6f      .    /*   co
5740: 6e 66 69 67 20 4e 41 4d 45 20 53 49 5a 45 20 5c  nfig NAME SIZE \
5750: 6e 20 43 4f 4e 54 45 4e 54 0a 20 20 20 20 2a 2a  n CONTENT.    **
5760: 0a 20 20 20 20 2a 2a 20 52 65 63 65 69 76 65 20  .    ** Receive 
5770: 61 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  a configuration 
5780: 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 63  value from the c
5790: 6c 69 65 6e 74 2e 20 20 54 68 69 73 20 69 73 20  lient.  This is 
57a0: 6f 6e 6c 79 0a 20 20 20 20 2a 2a 20 70 65 72 6d  only.    ** perm
57b0: 69 74 74 65 64 20 66 6f 72 20 68 69 67 68 2d 70  itted for high-p
57c0: 72 69 76 69 6c 65 67 65 20 75 73 65 72 73 2e 0a  rivilege users..
57d0: 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 62      */.    if( b
57e0: 6c 6f 62 5f 65 71 28 26 78 66 65 72 2e 61 54 6f  lob_eq(&xfer.aTo
57f0: 6b 65 6e 5b 30 5d 2c 22 63 6f 6e 66 69 67 22 29  ken[0],"config")
5800: 20 26 26 20 78 66 65 72 2e 6e 54 6f 6b 65 6e 3d   && xfer.nToken=
5810: 3d 33 0a 20 20 20 20 20 20 20 20 26 26 20 62 6c  =3.        && bl
5820: 6f 62 5f 69 73 5f 69 6e 74 28 26 78 66 65 72 2e  ob_is_int(&xfer.
5830: 61 54 6f 6b 65 6e 5b 32 5d 2c 20 26 73 69 7a 65  aToken[2], &size
5840: 29 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74  ) ){.      const
5850: 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 62   char *zName = b
5860: 6c 6f 62 5f 73 74 72 28 26 78 66 65 72 2e 61 54  lob_str(&xfer.aT
5870: 6f 6b 65 6e 5b 31 5d 29 3b 0a 20 20 20 20 20 20  oken[1]);.      
5880: 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 3b 0a 20 20  Blob content;.  
5890: 20 20 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 63      blob_zero(&c
58a0: 6f 6e 74 65 6e 74 29 3b 0a 20 20 20 20 20 20 62  ontent);.      b
58b0: 6c 6f 62 5f 65 78 74 72 61 63 74 28 78 66 65 72  lob_extract(xfer
58c0: 2e 70 49 6e 2c 20 73 69 7a 65 2c 20 26 63 6f 6e  .pIn, size, &con
58d0: 74 65 6e 74 29 3b 0a 20 20 20 20 20 20 69 66 28  tent);.      if(
58e0: 20 21 67 2e 6f 6b 41 64 6d 69 6e 20 29 7b 0a 20   !g.okAdmin ){. 
58f0: 20 20 20 20 20 20 20 63 67 69 5f 72 65 73 65 74         cgi_reset
5900: 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 20 20  _content();.    
5910: 20 20 20 20 40 20 65 72 72 6f 72 20 6e 6f 74 5c      @ error not\
5920: 73 61 75 74 68 6f 72 69 7a 65 64 5c 73 74 6f 5c  sauthorized\sto\
5930: 73 70 75 73 68 5c 73 63 6f 6e 66 69 67 75 72 61  spush\sconfigura
5940: 74 69 6f 6e 0a 20 20 20 20 20 20 20 20 6e 45 72  tion.        nEr
5950: 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 62 72 65  r++;.        bre
5960: 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
5970: 20 20 69 66 28 20 7a 4e 61 6d 65 5b 30 5d 21 3d    if( zName[0]!=
5980: 27 40 27 20 29 7b 0a 20 20 20 20 20 20 20 20 69  '@' ){.        i
5990: 66 28 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c  f( strcmp(zName,
59a0: 20 22 6c 6f 67 6f 2d 69 6d 61 67 65 22 29 3d 3d   "logo-image")==
59b0: 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 53  0 ){.          S
59c0: 74 6d 74 20 69 6e 73 3b 0a 20 20 20 20 20 20 20  tmt ins;.       
59d0: 20 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 69     db_prepare(&i
59e0: 6e 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ns,.            
59f0: 22 52 45 50 4c 41 43 45 20 49 4e 54 4f 20 63 6f  "REPLACE INTO co
5a00: 6e 66 69 67 28 6e 61 6d 65 2c 20 76 61 6c 75 65  nfig(name, value
5a10: 29 20 56 41 4c 55 45 53 28 3a 6e 61 6d 65 2c 20  ) VALUES(:name, 
5a20: 3a 76 61 6c 75 65 29 22 0a 20 20 20 20 20 20 20  :value)".       
5a30: 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20     );.          
5a40: 64 62 5f 62 69 6e 64 5f 74 65 78 74 28 26 69 6e  db_bind_text(&in
5a50: 73 2c 20 22 3a 6e 61 6d 65 22 2c 20 7a 4e 61 6d  s, ":name", zNam
5a60: 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 64 62  e);.          db
5a70: 5f 62 69 6e 64 5f 62 6c 6f 62 28 26 69 6e 73 2c  _bind_blob(&ins,
5a80: 20 22 3a 76 61 6c 75 65 22 2c 20 26 63 6f 6e 74   ":value", &cont
5a90: 65 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ent);.          
5aa0: 64 62 5f 73 74 65 70 28 26 69 6e 73 29 3b 0a 20  db_step(&ins);. 
5ab0: 20 20 20 20 20 20 20 20 20 64 62 5f 66 69 6e 61           db_fina
5ac0: 6c 69 7a 65 28 26 69 6e 73 29 3b 0a 20 20 20 20  lize(&ins);.    
5ad0: 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
5ae0: 20 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78       db_multi_ex
5af0: 65 63 28 0a 20 20 20 20 20 20 20 20 20 20 20 20  ec(.            
5b00: 20 20 22 52 45 50 4c 41 43 45 20 49 4e 54 4f 20    "REPLACE INTO 
5b10: 63 6f 6e 66 69 67 28 6e 61 6d 65 2c 76 61 6c 75  config(name,valu
5b20: 65 29 20 56 41 4c 55 45 53 28 25 51 2c 25 51 29  e) VALUES(%Q,%Q)
5b30: 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
5b40: 20 7a 4e 61 6d 65 2c 20 62 6c 6f 62 5f 73 74 72   zName, blob_str
5b50: 28 26 63 6f 6e 74 65 6e 74 29 0a 20 20 20 20 20  (&content).     
5b60: 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20       );.        
5b70: 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  }.      }else{. 
5b80: 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 69 63 65         /* Notice
5b90: 20 74 68 61 74 20 77 65 20 61 72 65 20 65 76 61   that we are eva
5ba0: 6c 75 61 74 69 6e 67 20 61 72 62 69 74 72 61 72  luating arbitrar
5bb0: 79 20 53 51 4c 20 72 65 63 65 69 76 65 64 20 66  y SQL received f
5bc0: 72 6f 6d 20 74 68 65 0a 20 20 20 20 20 20 20 20  rom the.        
5bd0: 2a 2a 20 63 6c 69 65 6e 74 2e 20 20 42 75 74 20  ** client.  But 
5be0: 74 68 69 73 20 63 61 6e 20 6f 6e 6c 79 20 68 61  this can only ha
5bf0: 70 70 65 6e 20 69 66 20 74 68 65 20 63 6c 69 65  ppen if the clie
5c00: 6e 74 20 68 61 73 20 61 75 74 68 65 6e 74 69 63  nt has authentic
5c10: 61 74 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20  ated.        ** 
5c20: 61 73 20 61 6e 20 61 64 6d 69 6e 69 73 74 72 61  as an administra
5c30: 74 6f 72 2c 20 73 6f 20 70 72 65 73 75 6d 61 62  tor, so presumab
5c40: 6c 79 20 77 65 20 74 72 75 73 74 20 74 68 65 20  ly we trust the 
5c50: 63 6c 69 65 6e 74 20 61 74 20 74 68 69 73 0a 20  client at this. 
5c60: 20 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e 74 2e         ** point.
5c70: 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
5c80: 20 20 20 20 69 66 28 20 21 72 65 63 76 43 6f 6e      if( !recvCon
5c90: 66 69 67 20 29 7b 0a 20 20 20 20 20 20 20 20 20  fig ){.         
5ca0: 20 63 6f 6e 66 69 67 75 72 65 5f 70 72 65 70 61   configure_prepa
5cb0: 72 65 5f 74 6f 5f 72 65 63 65 69 76 65 28 30 29  re_to_receive(0)
5cc0: 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 63 76  ;.          recv
5cd0: 43 6f 6e 66 69 67 20 3d 20 31 3b 0a 20 20 20 20  Config = 1;.    
5ce0: 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 62      }.        db
5cf0: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22 25 73 22  _multi_exec("%s"
5d00: 2c 20 62 6c 6f 62 5f 73 74 72 28 26 63 6f 6e 74  , blob_str(&cont
5d10: 65 6e 74 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ent));.      }. 
5d20: 20 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28       blob_reset(
5d30: 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 20 20 20  &content);.     
5d40: 20 62 6c 6f 62 5f 73 65 65 6b 28 78 66 65 72 2e   blob_seek(xfer.
5d50: 70 49 6e 2c 20 31 2c 20 42 4c 4f 42 5f 53 45 45  pIn, 1, BLOB_SEE
5d60: 4b 5f 43 55 52 29 3b 0a 20 20 20 20 7d 65 6c 73  K_CUR);.    }els
5d70: 65 0a 0a 20 20 20 20 20 20 0a 0a 20 20 20 20 2f  e..      ..    /
5d80: 2a 20 20 20 20 63 6f 6f 6b 69 65 20 54 45 58 54  *    cookie TEXT
5d90: 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41  .    **.    ** A
5da0: 20 63 6f 6f 6b 69 65 20 63 6f 6e 74 61 69 6e 73   cookie contains
5db0: 20 61 20 61 72 62 69 74 72 61 72 79 2d 6c 65 6e   a arbitrary-len
5dc0: 67 74 68 20 61 72 67 75 6d 65 6e 74 20 74 68 61  gth argument tha
5dd0: 74 20 69 73 20 73 65 72 76 65 72 2d 64 65 66 69  t is server-defi
5de0: 6e 65 64 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20  ned..    ** The 
5df0: 61 72 67 75 6d 65 6e 74 20 6d 75 73 74 20 62 65  argument must be
5e00: 20 65 6e 63 6f 64 65 64 20 73 6f 20 61 73 20 6e   encoded so as n
5e10: 6f 74 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 6e  ot to contain an
5e20: 79 20 77 68 69 74 65 73 70 61 63 65 2e 0a 20 20  y whitespace..  
5e30: 20 20 2a 2a 20 54 68 65 20 73 65 72 76 65 72 20    ** The server 
5e40: 63 61 6e 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 73  can optionally s
5e50: 65 6e 64 20 61 20 63 6f 6f 6b 69 65 20 74 6f 20  end a cookie to 
5e60: 74 68 65 20 63 6c 69 65 6e 74 2e 20 20 54 68 65  the client.  The
5e70: 20 63 6c 69 65 6e 74 0a 20 20 20 20 2a 2a 20 6d   client.    ** m
5e80: 69 67 68 74 20 74 68 65 6e 20 72 65 74 75 72 6e  ight then return
5e90: 20 74 68 65 20 73 61 6d 65 20 63 6f 6f 6b 69 65   the same cookie
5ea0: 20 62 61 63 6b 20 74 6f 20 74 68 65 20 73 65 72   back to the ser
5eb0: 76 65 72 20 6f 6e 20 69 74 73 20 6e 65 78 74 0a  ver on its next.
5ec0: 20 20 20 20 2a 2a 20 63 6f 6d 6d 75 6e 69 63 61      ** communica
5ed0: 74 69 6f 6e 2e 20 20 54 68 65 20 63 6f 6f 6b 69  tion.  The cooki
5ee0: 65 20 6d 69 67 68 74 20 72 65 63 6f 72 64 20 69  e might record i
5ef0: 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 20  nformation that 
5f00: 68 65 6c 70 73 0a 20 20 20 20 2a 2a 20 74 68 65  helps.    ** the
5f10: 20 73 65 72 76 65 72 20 6f 70 74 69 6d 69 7a 65   server optimize
5f20: 20 61 20 70 75 73 68 20 6f 72 20 70 75 6c 6c 2e   a push or pull.
5f30: 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54  .    **.    ** T
5f40: 68 65 20 63 6c 69 65 6e 74 20 69 73 20 6e 6f 74  he client is not
5f50: 20 72 65 71 75 69 72 65 64 20 74 6f 20 72 65 74   required to ret
5f60: 75 72 6e 20 61 20 63 6f 6f 6b 69 65 2e 20 20 53  urn a cookie.  S
5f70: 6f 20 74 68 65 20 73 65 72 76 65 72 0a 20 20 20  o the server.   
5f80: 20 2a 2a 20 6d 75 73 74 20 6e 6f 74 20 64 65 70   ** must not dep
5f90: 65 6e 64 20 6f 6e 20 74 68 65 20 63 6f 6f 6b 69  end on the cooki
5fa0: 65 2e 20 20 54 68 65 20 63 6f 6f 6b 69 65 20 73  e.  The cookie s
5fb0: 68 6f 75 6c 64 20 62 65 20 61 6e 20 6f 70 74 69  hould be an opti
5fc0: 6d 69 7a 61 74 69 6f 6e 0a 20 20 20 20 2a 2a 20  mization.    ** 
5fd0: 6f 6e 6c 79 2e 20 20 54 68 65 20 63 6c 69 65 6e  only.  The clien
5fe0: 74 20 6d 69 67 68 74 20 61 6c 73 6f 20 73 65 6e  t might also sen
5ff0: 64 20 61 20 63 6f 6f 6b 69 65 20 74 68 61 74 20  d a cookie that 
6000: 63 61 6d 65 20 66 72 6f 6d 20 61 20 64 69 66 66  came from a diff
6010: 65 72 65 6e 74 0a 20 20 20 20 2a 2a 20 73 65 72  erent.    ** ser
6020: 76 65 72 2e 20 20 53 6f 20 74 68 65 20 73 65 72  ver.  So the ser
6030: 76 65 72 20 6d 75 73 74 20 62 65 20 70 72 65 70  ver must be prep
6040: 61 72 65 64 20 74 6f 20 64 69 73 74 69 6e 67 75  ared to distingu
6050: 69 73 68 20 69 74 73 20 6f 77 6e 20 63 6f 6f 6b  ish its own cook
6060: 69 65 0a 20 20 20 20 2a 2a 20 66 72 6f 6d 20 63  ie.    ** from c
6070: 6f 6f 6b 69 65 73 20 6f 72 69 67 69 6e 61 74 69  ookies originati
6080: 6e 67 20 66 72 6f 6d 20 6f 74 68 65 72 20 73 65  ng from other se
6090: 72 76 65 72 73 2e 20 20 54 68 65 20 63 6c 69 65  rvers.  The clie
60a0: 6e 74 20 6d 69 67 68 74 20 73 65 6e 64 0a 20 20  nt might send.  
60b0: 20 20 2a 2a 20 62 61 63 6b 20 73 65 76 65 72 61    ** back severa
60c0: 6c 20 64 69 66 66 65 72 65 6e 74 20 63 6f 6f 6b  l different cook
60d0: 69 65 73 20 74 6f 20 74 68 65 20 73 65 72 76 65  ies to the serve
60e0: 72 2e 20 20 54 68 65 20 73 65 72 76 65 72 20 73  r.  The server s
60f0: 68 6f 75 6c 64 20 62 65 0a 20 20 20 20 2a 2a 20  hould be.    ** 
6100: 70 72 65 70 61 72 65 64 20 74 6f 20 73 69 66 74  prepared to sift
6110: 20 74 68 72 6f 75 67 68 20 74 68 65 20 63 6f 6f   through the coo
6120: 6b 69 65 73 20 61 6e 64 20 70 69 63 6b 20 74 68  kies and pick th
6130: 65 20 6f 6e 65 20 74 68 61 74 20 69 74 20 77 61  e one that it wa
6140: 6e 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  nts..    */.    
6150: 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 78 66 65  if( blob_eq(&xfe
6160: 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 63 6f  r.aToken[0], "co
6170: 6f 6b 69 65 22 29 20 26 26 20 78 66 65 72 2e 6e  okie") && xfer.n
6180: 54 6f 6b 65 6e 3d 3d 32 20 29 7b 0a 20 20 20 20  Token==2 ){.    
6190: 20 20 2f 2a 20 50 72 6f 63 65 73 73 20 74 68 65    /* Process the
61a0: 20 63 6f 6f 6b 69 65 20 2a 2f 0a 20 20 20 20 7d   cookie */.    }
61b0: 65 6c 73 65 0a 0a 20 20 20 20 2f 2a 20 55 6e 6b  else..    /* Unk
61c0: 6e 6f 77 6e 20 6d 65 73 73 61 67 65 0a 20 20 20  nown message.   
61d0: 20 2a 2f 0a 20 20 20 20 7b 0a 20 20 20 20 20 20   */.    {.      
61e0: 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e 74 65 6e  cgi_reset_conten
61f0: 74 28 29 3b 0a 20 20 20 20 20 20 40 20 65 72 72  t();.      @ err
6200: 6f 72 20 62 61 64 5c 73 63 6f 6d 6d 61 6e 64 3a  or bad\scommand:
6210: 5c 73 25 46 28 62 6c 6f 62 5f 73 74 72 28 26 78  \s%F(blob_str(&x
6220: 66 65 72 2e 6c 69 6e 65 29 29 0a 20 20 20 20 7d  fer.line)).    }
6230: 0a 20 20 20 20 62 6c 6f 62 61 72 72 61 79 5f 72  .    blobarray_r
6240: 65 73 65 74 28 78 66 65 72 2e 61 54 6f 6b 65 6e  eset(xfer.aToken
6250: 2c 20 78 66 65 72 2e 6e 54 6f 6b 65 6e 29 3b 0a  , xfer.nToken);.
6260: 20 20 7d 0a 20 20 69 66 28 20 69 73 50 75 73 68    }.  if( isPush
6270: 20 29 7b 0a 20 20 20 20 72 65 71 75 65 73 74 5f   ){.    request_
6280: 70 68 61 6e 74 6f 6d 73 28 26 78 66 65 72 2c 20  phantoms(&xfer, 
6290: 35 30 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  500);.  }.  if( 
62a0: 69 73 43 6c 6f 6e 65 20 26 26 20 6e 47 69 6d 6d  isClone && nGimm
62b0: 65 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54  e==0 ){.    /* T
62c0: 68 65 20 69 6e 69 74 69 61 6c 20 22 63 6c 6f 6e  he initial "clon
62d0: 65 22 20 6d 65 73 73 61 67 65 20 66 72 6f 6d 20  e" message from 
62e0: 63 6c 69 65 6e 74 20 74 6f 20 73 65 72 76 65 72  client to server
62f0: 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 0a 20 20 20   contains no.   
6300: 20 2a 2a 20 22 67 69 6d 6d 65 22 20 63 61 72 64   ** "gimme" card
6310: 73 2e 20 4f 6e 20 74 68 61 74 20 69 6e 69 74 69  s. On that initi
6320: 61 6c 20 6d 65 73 73 61 67 65 2c 20 73 65 6e 64  al message, send
6330: 20 74 68 65 20 63 6c 69 65 6e 74 20 61 6e 20 22   the client an "
6340: 69 67 6f 74 22 0a 20 20 20 20 2a 2a 20 63 61 72  igot".    ** car
6350: 64 20 66 6f 72 20 65 76 65 72 79 20 61 72 74 69  d for every arti
6360: 66 61 63 74 20 63 75 72 72 65 6e 74 6c 79 20 69  fact currently i
6370: 6e 20 74 68 65 20 72 65 73 70 6f 73 69 74 6f 72  n the respositor
6380: 79 2e 20 20 54 68 69 73 20 77 69 6c 6c 0a 20 20  y.  This will.  
6390: 20 20 2a 2a 20 63 61 75 73 65 20 74 68 65 20 63    ** cause the c
63a0: 6c 69 65 6e 74 20 74 6f 20 63 72 65 61 74 65 20  lient to create 
63b0: 70 68 61 6e 74 6f 6d 73 20 66 6f 72 20 61 6c 6c  phantoms for all
63c0: 20 61 72 74 69 66 61 63 74 73 2c 20 77 68 69 63   artifacts, whic
63d0: 68 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 69 6e  h will.    ** in
63e0: 20 74 75 72 6e 20 6d 61 6b 65 20 73 75 72 65 20   turn make sure 
63f0: 74 68 61 74 20 74 68 65 20 65 6e 74 69 72 65 20  that the entire 
6400: 72 65 70 6f 73 69 74 6f 72 79 20 69 73 20 73 65  repository is se
6410: 6e 74 20 65 66 66 69 63 69 65 6e 74 6c 79 0a 20  nt efficiently. 
6420: 20 20 20 2a 2a 20 61 6e 64 20 65 78 70 65 64 69     ** and expedi
6430: 74 69 6f 75 73 6c 79 2e 0a 20 20 20 20 2a 2f 0a  tiously..    */.
6440: 20 20 20 20 73 65 6e 64 5f 61 6c 6c 28 26 78 66      send_all(&xf
6450: 65 72 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  er);.  }else if(
6460: 20 69 73 50 75 6c 6c 20 29 7b 0a 20 20 20 20 63   isPull ){.    c
6470: 72 65 61 74 65 5f 63 6c 75 73 74 65 72 28 29 3b  reate_cluster();
6480: 0a 20 20 20 20 73 65 6e 64 5f 75 6e 63 6c 75 73  .    send_unclus
6490: 74 65 72 65 64 28 26 78 66 65 72 29 3b 0a 20 20  tered(&xfer);.  
64a0: 7d 0a 20 20 69 66 28 20 72 65 63 76 43 6f 6e 66  }.  if( recvConf
64b0: 69 67 20 29 7b 0a 20 20 20 20 63 6f 6e 66 69 67  ig ){.    config
64c0: 75 72 65 5f 66 69 6e 61 6c 69 7a 65 5f 72 65 63  ure_finalize_rec
64d0: 65 69 76 65 28 29 3b 0a 20 20 7d 0a 20 20 6d 61  eive();.  }.  ma
64e0: 6e 69 66 65 73 74 5f 63 72 6f 73 73 6c 69 6e 6b  nifest_crosslink
64f0: 5f 65 6e 64 28 29 3b 0a 20 20 64 62 5f 65 6e 64  _end();.  db_end
6500: 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 30 29 3b  _transaction(0);
6510: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e  .}../*.** COMMAN
6520: 44 3a 20 74 65 73 74 2d 78 66 65 72 0a 2a 2a 0a  D: test-xfer.**.
6530: 2a 2a 20 54 68 69 73 20 63 6f 6d 6d 61 6e 64 20  ** This command 
6540: 69 73 20 75 73 65 64 20 66 6f 72 20 64 65 62 75  is used for debu
6550: 67 67 69 6e 67 20 74 68 65 20 73 65 72 76 65 72  gging the server
6560: 2e 20 20 54 68 65 72 65 20 69 73 20 61 20 73 69  .  There is a si
6570: 6e 67 6c 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  ngle.** argument
6580: 20 77 68 69 63 68 20 69 73 20 74 68 65 20 75 6e   which is the un
6590: 63 6f 6d 70 72 65 73 73 65 64 20 63 6f 6e 74 65  compressed conte
65a0: 6e 74 20 6f 66 20 61 6e 20 22 78 66 65 72 22 20  nt of an "xfer" 
65b0: 6d 65 73 73 61 67 65 0a 2a 2a 20 66 72 6f 6d 20  message.** from 
65c0: 63 6c 69 65 6e 74 20 74 6f 20 73 65 72 76 65 72  client to server
65d0: 2e 20 20 54 68 69 73 20 63 6f 6d 6d 61 6e 64 20  .  This command 
65e0: 69 6e 74 65 72 70 72 65 74 73 20 74 68 61 74 20  interprets that 
65f0: 6d 65 73 73 61 67 65 20 61 73 0a 2a 2a 20 69 66  message as.** if
6600: 20 68 61 64 20 62 65 65 6e 20 72 65 63 65 69 76   had been receiv
6610: 65 64 20 62 79 20 74 68 65 20 73 65 72 76 65 72  ed by the server
6620: 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 74 68 65 20 63  ..**.** On the c
6630: 6c 69 65 6e 74 20 73 69 64 65 2c 20 72 75 6e 3a  lient side, run:
6640: 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 66 6f 73 73  .**.**      foss
6650: 69 6c 20 70 75 73 68 20 68 74 74 70 3a 2f 2f 62  il push http://b
6660: 6f 67 75 73 2f 20 2d 2d 68 74 74 70 74 72 61 63  ogus/ --httptrac
6670: 65 0a 2a 2a 0a 2a 2a 20 4f 72 20 61 20 73 69 6d  e.**.** Or a sim
6680: 69 6c 61 72 20 63 6f 6d 6d 61 6e 64 20 74 6f 20  ilar command to 
6690: 70 72 6f 76 69 64 65 20 74 68 65 20 6f 75 74 70  provide the outp
66a0: 75 74 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74  ut.  The content
66b0: 20 6f 66 20 74 68 65 0a 2a 2a 20 6d 65 73 73 61   of the.** messa
66c0: 67 65 20 77 69 6c 6c 20 61 70 70 65 61 72 20 6f  ge will appear o
66d0: 6e 20 73 74 61 6e 64 61 72 64 20 6f 75 74 70 75  n standard outpu
66e0: 74 2e 20 20 43 61 70 74 75 72 65 20 74 68 69 73  t.  Capture this
66f0: 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 6e 74 6f   message.** into
6700: 20 61 20 66 69 6c 65 20 6e 61 6d 65 64 20 28 66   a file named (f
6710: 6f 72 20 65 78 61 6d 70 6c 65 29 20 6f 75 74 2e  or example) out.
6720: 74 78 74 2e 20 20 54 68 65 6e 20 72 75 6e 20 74  txt.  Then run t
6730: 68 65 0a 2a 2a 20 73 65 72 76 65 72 20 69 6e 20  he.** server in 
6740: 67 64 62 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 67  gdb:.**.**     g
6750: 64 62 20 66 6f 73 73 69 6c 0a 2a 2a 20 20 20 20  db fossil.**    
6760: 20 72 20 74 65 73 74 2d 78 66 65 72 20 6f 75 74   r test-xfer out
6770: 2e 74 78 74 0a 2a 2f 0a 76 6f 69 64 20 63 6d 64  .txt.*/.void cmd
6780: 5f 74 65 73 74 5f 78 66 65 72 28 76 6f 69 64 29  _test_xfer(void)
6790: 7b 0a 20 20 69 6e 74 20 6e 6f 74 55 73 65 64 3b  {.  int notUsed;
67a0: 0a 20 20 69 66 28 20 67 2e 61 72 67 63 21 3d 32  .  if( g.argc!=2
67b0: 20 26 26 20 67 2e 61 72 67 63 21 3d 33 20 29 7b   && g.argc!=3 ){
67c0: 0a 20 20 20 20 75 73 61 67 65 28 22 3f 4d 45 53  .    usage("?MES
67d0: 53 41 47 45 46 49 4c 45 3f 22 29 3b 0a 20 20 7d  SAGEFILE?");.  }
67e0: 0a 20 20 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69  .  db_must_be_wi
67f0: 74 68 69 6e 5f 74 72 65 65 28 29 3b 0a 20 20 62  thin_tree();.  b
6800: 6c 6f 62 5f 7a 65 72 6f 28 26 67 2e 63 67 69 49  lob_zero(&g.cgiI
6810: 6e 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 61 64 5f  n);.  blob_read_
6820: 66 72 6f 6d 5f 66 69 6c 65 28 26 67 2e 63 67 69  from_file(&g.cgi
6830: 49 6e 2c 20 67 2e 61 72 67 63 3d 3d 32 20 3f 20  In, g.argc==2 ? 
6840: 22 2d 22 20 3a 20 67 2e 61 72 67 76 5b 32 5d 29  "-" : g.argv[2])
6850: 3b 0a 20 20 64 69 73 61 62 6c 65 4c 6f 67 69 6e  ;.  disableLogin
6860: 20 3d 20 31 3b 0a 20 20 70 61 67 65 5f 78 66 65   = 1;.  page_xfe
6870: 72 28 29 3b 0a 20 20 70 72 69 6e 74 66 28 22 25  r();.  printf("%
6880: 73 5c 6e 22 2c 20 63 67 69 5f 65 78 74 72 61 63  s\n", cgi_extrac
6890: 74 5f 63 6f 6e 74 65 6e 74 28 26 6e 6f 74 55 73  t_content(&notUs
68a0: 65 64 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  ed));.}../*.** F
68b0: 6f 72 6d 61 74 20 73 74 72 69 6e 67 73 20 66 6f  ormat strings fo
68c0: 72 20 70 72 6f 67 72 65 73 73 20 72 65 70 6f 72  r progress repor
68d0: 74 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ting..*/.static 
68e0: 63 6f 6e 73 74 20 63 68 61 72 20 7a 4c 61 62 65  const char zLabe
68f0: 6c 46 6f 72 6d 61 74 5b 5d 20 3d 20 22 25 2d 31  lFormat[] = "%-1
6900: 30 73 20 25 31 30 73 20 25 31 30 73 20 25 31 30  0s %10s %10s %10
6910: 73 20 25 31 30 73 5c 6e 22 3b 0a 73 74 61 74 69  s %10s\n";.stati
6920: 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 56 61  c const char zVa
6930: 6c 75 65 46 6f 72 6d 61 74 5b 5d 20 3d 20 22 5c  lueFormat[] = "\
6940: 72 25 2d 31 30 73 20 25 31 30 64 20 25 31 30 64  r%-10s %10d %10d
6950: 20 25 31 30 64 20 25 31 30 64 5c 6e 22 3b 0a 0a   %10d %10d\n";..
6960: 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 6f 20 74  ./*.** Sync to t
6970: 68 65 20 68 6f 73 74 20 69 64 65 6e 74 69 66 69  he host identifi
6980: 65 64 20 69 6e 20 67 2e 75 72 6c 4e 61 6d 65 20  ed in g.urlName 
6990: 61 6e 64 20 67 2e 75 72 6c 50 61 74 68 2e 20 20  and g.urlPath.  
69a0: 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  This.** routine 
69b0: 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65  is called by the
69c0: 20 63 6c 69 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 52   client..**.** R
69d0: 65 63 6f 72 64 73 20 61 72 65 20 70 75 73 68 65  ecords are pushe
69e0: 64 20 74 6f 20 74 68 65 20 73 65 72 76 65 72 20  d to the server 
69f0: 69 66 20 70 75 73 68 46 6c 61 67 20 69 73 20 74  if pushFlag is t
6a00: 72 75 65 2e 20 20 52 65 63 6f 72 64 73 0a 2a 2a  rue.  Records.**
6a10: 20 61 72 65 20 70 75 6c 6c 65 64 20 69 66 20 70   are pulled if p
6a20: 75 6c 6c 46 6c 61 67 20 69 73 20 74 72 75 65 2e  ullFlag is true.
6a30: 20 20 41 20 66 75 6c 6c 20 73 79 6e 63 20 6f 63    A full sync oc
6a40: 63 75 72 73 20 69 66 20 62 6f 74 68 20 61 72 65  curs if both are
6a50: 0a 2a 2a 20 74 72 75 65 2e 0a 2a 2f 0a 76 6f 69  .** true..*/.voi
6a60: 64 20 63 6c 69 65 6e 74 5f 73 79 6e 63 28 0a 20  d client_sync(. 
6a70: 20 69 6e 74 20 70 75 73 68 46 6c 61 67 2c 20 20   int pushFlag,  
6a80: 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
6a90: 20 74 6f 20 64 6f 20 61 20 70 75 73 68 20 28 6f   to do a push (o
6aa0: 72 20 61 20 73 79 6e 63 29 20 2a 2f 0a 20 20 69  r a sync) */.  i
6ab0: 6e 74 20 70 75 6c 6c 46 6c 61 67 2c 20 20 20 20  nt pullFlag,    
6ac0: 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74         /* True t
6ad0: 6f 20 64 6f 20 61 20 70 75 6c 6c 20 28 6f 72 20  o do a pull (or 
6ae0: 61 20 73 79 6e 63 29 20 2a 2f 0a 20 20 69 6e 74  a sync) */.  int
6af0: 20 63 6c 6f 6e 65 46 6c 61 67 2c 20 20 20 20 20   cloneFlag,     
6b00: 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
6b10: 74 68 69 73 20 69 73 20 61 20 63 6c 6f 6e 65 20  this is a clone 
6b20: 2a 2f 0a 20 20 69 6e 74 20 63 6f 6e 66 69 67 52  */.  int configR
6b30: 63 76 4d 61 73 6b 2c 20 20 20 20 20 20 2f 2a 20  cvMask,      /* 
6b40: 52 65 63 65 69 76 65 20 74 68 65 73 65 20 63 6f  Receive these co
6b50: 6e 66 69 67 75 72 61 74 69 6f 6e 20 69 74 65 6d  nfiguration item
6b60: 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6e 66 69  s */.  int confi
6b70: 67 53 65 6e 64 4d 61 73 6b 20 20 20 20 20 20 2f  gSendMask      /
6b80: 2a 20 53 65 6e 64 20 74 68 65 73 65 20 63 6f 6e  * Send these con
6b90: 66 69 67 75 72 61 74 69 6f 6e 20 69 74 65 6d 73  figuration items
6ba0: 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 67 6f 20   */.){.  int go 
6bb0: 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 1;            
6bc0: 20 2f 2a 20 4c 6f 6f 70 20 75 6e 74 69 6c 20 7a   /* Loop until z
6bd0: 65 72 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 61  ero */.  int nCa
6be0: 72 64 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  rd = 0;         
6bf0: 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 61   /* Number of ca
6c00: 72 64 73 20 73 65 6e 74 20 6f 72 20 72 65 63 65  rds sent or rece
6c10: 69 76 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  ived */.  int nC
6c20: 79 63 6c 65 20 3d 20 30 3b 20 20 20 20 20 20 20  ycle = 0;       
6c30: 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72    /* Number of r
6c40: 6f 75 6e 64 20 74 72 69 70 73 20 74 6f 20 74 68  ound trips to th
6c50: 65 20 73 65 72 76 65 72 20 2a 2f 0a 20 20 69 6e  e server */.  in
6c60: 74 20 73 69 7a 65 3b 20 20 20 20 20 20 20 20 20  t size;         
6c70: 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
6c80: 20 61 20 63 6f 6e 66 69 67 20 76 61 6c 75 65 20   a config value 
6c90: 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 6c 65 53 65  */.  int nFileSe
6ca0: 6e 64 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6f 72  nd = 0;.  int or
6cb0: 69 67 43 6f 6e 66 69 67 52 63 76 4d 61 73 6b 3b  igConfigRcvMask;
6cc0: 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 76 61    /* Original va
6cd0: 6c 75 65 20 6f 66 20 63 6f 6e 66 69 67 52 63 76  lue of configRcv
6ce0: 4d 61 73 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 46  Mask */.  int nF
6cf0: 69 6c 65 52 65 63 76 3b 20 20 20 20 20 20 20 20  ileRecv;        
6d00: 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66    /* Number of f
6d10: 69 6c 65 73 20 72 65 63 65 69 76 65 64 20 2a 2f  iles received */
6d20: 0a 20 20 69 6e 74 20 6d 78 50 68 61 6e 74 6f 6d  .  int mxPhantom
6d30: 52 65 71 20 3d 20 32 30 30 3b 20 2f 2a 20 4d 61  Req = 200; /* Ma
6d40: 78 20 6e 75 6d 62 65 72 20 6f 66 20 70 68 61 6e  x number of phan
6d50: 74 6f 6d 73 20 74 6f 20 72 65 71 75 65 73 74 20  toms to request 
6d60: 70 65 72 20 63 6f 6d 6d 20 2a 2f 0a 20 20 63 6f  per comm */.  co
6d70: 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6f 6b 69  nst char *zCooki
6d80: 65 3b 20 20 20 20 2f 2a 20 53 65 72 76 65 72 20  e;    /* Server 
6d90: 63 6f 6f 6b 69 65 20 2a 2f 0a 20 20 69 6e 74 20  cookie */.  int 
6da0: 6e 53 65 6e 74 2c 20 6e 52 63 76 64 3b 20 20 20  nSent, nRcvd;   
6db0: 20 20 20 20 2f 2a 20 42 79 74 65 73 20 73 65 6e      /* Bytes sen
6dc0: 74 20 61 6e 64 20 72 65 63 65 69 76 65 64 20 28  t and received (
6dd0: 61 66 74 65 72 20 63 6f 6d 70 72 65 73 73 69 6f  after compressio
6de0: 6e 29 20 2a 2f 0a 20 20 42 6c 6f 62 20 73 65 6e  n) */.  Blob sen
6df0: 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
6e00: 2f 2a 20 54 65 78 74 20 77 65 20 61 72 65 20 73  /* Text we are s
6e10: 65 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 73 65  ending to the se
6e20: 72 76 65 72 20 2a 2f 0a 20 20 42 6c 6f 62 20 72  rver */.  Blob r
6e30: 65 63 76 3b 20 20 20 20 20 20 20 20 20 20 20 20  ecv;            
6e40: 20 20 2f 2a 20 52 65 70 6c 79 20 77 65 20 67 6f    /* Reply we go
6e50: 74 20 62 61 63 6b 20 66 72 6f 6d 20 74 68 65 20  t back from the 
6e60: 73 65 72 76 65 72 20 2a 2f 0a 20 20 58 66 65 72  server */.  Xfer
6e70: 20 78 66 65 72 3b 20 20 20 20 20 20 20 20 20 20   xfer;          
6e80: 20 20 20 20 2f 2a 20 54 72 61 6e 73 66 65 72 20      /* Transfer 
6e90: 64 61 74 61 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  data */.  const 
6ea0: 63 68 61 72 20 2a 7a 53 43 6f 64 65 20 3d 20 64  char *zSCode = d
6eb0: 62 5f 67 65 74 28 22 73 65 72 76 65 72 2d 63 6f  b_get("server-co
6ec0: 64 65 22 2c 20 22 78 22 29 3b 0a 20 20 63 6f 6e  de", "x");.  con
6ed0: 73 74 20 63 68 61 72 20 2a 7a 50 43 6f 64 65 20  st char *zPCode 
6ee0: 3d 20 64 62 5f 67 65 74 28 22 70 72 6f 6a 65 63  = db_get("projec
6ef0: 74 2d 63 6f 64 65 22 2c 20 30 29 3b 0a 0a 20 20  t-code", 0);..  
6f00: 69 66 28 20 64 62 5f 67 65 74 5f 62 6f 6f 6c 65  if( db_get_boole
6f10: 61 6e 28 22 64 6f 6e 74 2d 70 75 73 68 22 2c 20  an("dont-push", 
6f20: 30 29 20 29 20 70 75 73 68 46 6c 61 67 20 3d 20  0) ) pushFlag = 
6f30: 30 3b 0a 20 20 69 66 28 20 70 75 73 68 46 6c 61  0;.  if( pushFla
6f40: 67 20 2b 20 70 75 6c 6c 46 6c 61 67 20 2b 20 63  g + pullFlag + c
6f50: 6c 6f 6e 65 46 6c 61 67 20 3d 3d 20 30 20 0a 20  loneFlag == 0 . 
6f60: 20 20 20 20 26 26 20 63 6f 6e 66 69 67 52 63 76      && configRcv
6f70: 4d 61 73 6b 3d 3d 30 20 26 26 20 63 6f 6e 66 69  Mask==0 && confi
6f80: 67 53 65 6e 64 4d 61 73 6b 3d 3d 30 20 29 20 72  gSendMask==0 ) r
6f90: 65 74 75 72 6e 3b 0a 0a 20 20 74 72 61 6e 73 70  eturn;..  transp
6fa0: 6f 72 74 5f 73 74 61 74 73 28 30 2c 20 30 2c 20  ort_stats(0, 0, 
6fb0: 31 29 3b 0a 20 20 73 6f 63 6b 65 74 5f 67 6c 6f  1);.  socket_glo
6fc0: 62 61 6c 5f 69 6e 69 74 28 29 3b 0a 20 20 6d 65  bal_init();.  me
6fd0: 6d 73 65 74 28 26 78 66 65 72 2c 20 30 2c 20 73  mset(&xfer, 0, s
6fe0: 69 7a 65 6f 66 28 78 66 65 72 29 29 3b 0a 20 20  izeof(xfer));.  
6ff0: 78 66 65 72 2e 70 49 6e 20 3d 20 26 72 65 63 76  xfer.pIn = &recv
7000: 3b 0a 20 20 78 66 65 72 2e 70 4f 75 74 20 3d 20  ;.  xfer.pOut = 
7010: 26 73 65 6e 64 3b 0a 20 20 78 66 65 72 2e 6d 78  &send;.  xfer.mx
7020: 53 65 6e 64 20 3d 20 64 62 5f 67 65 74 5f 69 6e  Send = db_get_in
7030: 74 28 22 6d 61 78 2d 75 70 6c 6f 61 64 22 2c 20  t("max-upload", 
7040: 32 35 30 30 30 30 29 3b 0a 0a 20 20 61 73 73 65  250000);..  asse
7050: 72 74 28 20 70 75 73 68 46 6c 61 67 20 7c 20 70  rt( pushFlag | p
7060: 75 6c 6c 46 6c 61 67 20 7c 20 63 6c 6f 6e 65 46  ullFlag | cloneF
7070: 6c 61 67 20 7c 20 63 6f 6e 66 69 67 52 63 76 4d  lag | configRcvM
7080: 61 73 6b 20 7c 20 63 6f 6e 66 69 67 53 65 6e 64  ask | configSend
7090: 4d 61 73 6b 20 29 3b 0a 20 20 64 62 5f 62 65 67  Mask );.  db_beg
70a0: 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 29  in_transaction()
70b0: 3b 0a 20 20 64 62 5f 72 65 63 6f 72 64 5f 72 65  ;.  db_record_re
70c0: 70 6f 73 69 74 6f 72 79 5f 66 69 6c 65 6e 61 6d  pository_filenam
70d0: 65 28 30 29 3b 0a 20 20 64 62 5f 6d 75 6c 74 69  e(0);.  db_multi
70e0: 5f 65 78 65 63 28 0a 20 20 20 20 22 43 52 45 41  _exec(.    "CREA
70f0: 54 45 20 54 45 4d 50 20 54 41 42 4c 45 20 6f 6e  TE TEMP TABLE on
7100: 72 65 6d 6f 74 65 28 72 69 64 20 49 4e 54 45 47  remote(rid INTEG
7110: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 29 3b  ER PRIMARY KEY);
7120: 22 0a 20 20 29 3b 0a 20 20 62 6c 6f 62 61 72 72  ".  );.  blobarr
7130: 61 79 5f 7a 65 72 6f 28 78 66 65 72 2e 61 54 6f  ay_zero(xfer.aTo
7140: 6b 65 6e 2c 20 63 6f 75 6e 74 28 78 66 65 72 2e  ken, count(xfer.
7150: 61 54 6f 6b 65 6e 29 29 3b 0a 20 20 62 6c 6f 62  aToken));.  blob
7160: 5f 7a 65 72 6f 28 26 73 65 6e 64 29 3b 0a 20 20  _zero(&send);.  
7170: 62 6c 6f 62 5f 7a 65 72 6f 28 26 72 65 63 76 29  blob_zero(&recv)
7180: 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 78  ;.  blob_zero(&x
7190: 66 65 72 2e 65 72 72 29 3b 0a 20 20 62 6c 6f 62  fer.err);.  blob
71a0: 5f 7a 65 72 6f 28 26 78 66 65 72 2e 6c 69 6e 65  _zero(&xfer.line
71b0: 29 3b 0a 20 20 6f 72 69 67 43 6f 6e 66 69 67 52  );.  origConfigR
71c0: 63 76 4d 61 73 6b 20 3d 20 63 6f 6e 66 69 67 52  cvMask = configR
71d0: 63 76 4d 61 73 6b 3b 0a 0a 20 20 2f 2a 0a 20 20  cvMask;..  /*.  
71e0: 2a 2a 20 41 6c 77 61 79 73 20 62 65 67 69 6e 20  ** Always begin 
71f0: 77 69 74 68 20 61 20 63 6c 6f 6e 65 2c 20 70 75  with a clone, pu
7200: 6c 6c 2c 20 6f 72 20 70 75 73 68 20 6d 65 73 73  ll, or push mess
7210: 61 67 65 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63  age.  */.  if( c
7220: 6c 6f 6e 65 46 6c 61 67 20 29 7b 0a 20 20 20 20  loneFlag ){.    
7230: 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 73 65  blob_appendf(&se
7240: 6e 64 2c 20 22 63 6c 6f 6e 65 5c 6e 22 29 3b 0a  nd, "clone\n");.
7250: 20 20 20 20 70 75 73 68 46 6c 61 67 20 3d 20 30      pushFlag = 0
7260: 3b 0a 20 20 20 20 70 75 6c 6c 46 6c 61 67 20 3d  ;.    pullFlag =
7270: 20 30 3b 0a 20 20 20 20 6e 43 61 72 64 2b 2b 3b   0;.    nCard++;
7280: 0a 20 20 20 20 2f 2a 20 54 42 44 3a 20 52 65 71  .    /* TBD: Req
7290: 75 65 73 74 20 61 6c 6c 20 74 72 61 6e 73 66 65  uest all transfe
72a0: 72 61 62 6c 65 20 63 6f 6e 66 69 67 75 72 61 74  rable configurat
72b0: 69 6f 6e 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20  ion values */.  
72c0: 7d 65 6c 73 65 20 69 66 28 20 70 75 6c 6c 46 6c  }else if( pullFl
72d0: 61 67 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 61  ag ){.    blob_a
72e0: 70 70 65 6e 64 66 28 26 73 65 6e 64 2c 20 22 70  ppendf(&send, "p
72f0: 75 6c 6c 20 25 73 20 25 73 5c 6e 22 2c 20 7a 53  ull %s %s\n", zS
7300: 43 6f 64 65 2c 20 7a 50 43 6f 64 65 29 3b 0a 20  Code, zPCode);. 
7310: 20 20 20 6e 43 61 72 64 2b 2b 3b 0a 20 20 7d 0a     nCard++;.  }.
7320: 20 20 69 66 28 20 70 75 73 68 46 6c 61 67 20 29    if( pushFlag )
7330: 7b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e  {.    blob_appen
7340: 64 66 28 26 73 65 6e 64 2c 20 22 70 75 73 68 20  df(&send, "push 
7350: 25 73 20 25 73 5c 6e 22 2c 20 7a 53 43 6f 64 65  %s %s\n", zSCode
7360: 2c 20 7a 50 43 6f 64 65 29 3b 0a 20 20 20 20 6e  , zPCode);.    n
7370: 43 61 72 64 2b 2b 3b 0a 20 20 7d 0a 20 20 6d 61  Card++;.  }.  ma
7380: 6e 69 66 65 73 74 5f 63 72 6f 73 73 6c 69 6e 6b  nifest_crosslink
7390: 5f 62 65 67 69 6e 28 29 3b 0a 20 20 70 72 69 6e  _begin();.  prin
73a0: 74 66 28 7a 4c 61 62 65 6c 46 6f 72 6d 61 74 2c  tf(zLabelFormat,
73b0: 20 22 22 2c 20 22 42 79 74 65 73 22 2c 20 22 43   "", "Bytes", "C
73c0: 61 72 64 73 22 2c 20 22 41 72 74 69 66 61 63 74  ards", "Artifact
73d0: 73 22 2c 20 22 44 65 6c 74 61 73 22 29 3b 0a 0a  s", "Deltas");..
73e0: 20 20 77 68 69 6c 65 28 20 67 6f 20 29 7b 0a 20    while( go ){. 
73f0: 20 20 20 69 6e 74 20 6e 65 77 50 68 61 6e 74 6f     int newPhanto
7400: 6d 20 3d 20 30 3b 0a 20 20 20 20 63 68 61 72 20  m = 0;.    char 
7410: 2a 7a 52 61 6e 64 6f 6d 6e 65 73 73 3b 0a 0a 20  *zRandomness;.. 
7420: 20 20 20 2f 2a 20 53 65 6e 64 20 6d 61 6b 65 20     /* Send make 
7430: 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c  the most recentl
7440: 79 20 72 65 63 65 69 76 65 64 20 63 6f 6f 6b 69  y received cooki
7450: 65 2e 20 20 4c 65 74 20 74 68 65 20 73 65 72 76  e.  Let the serv
7460: 65 72 0a 20 20 20 20 2a 2a 20 66 69 67 75 72 65  er.    ** figure
7470: 20 6f 75 74 20 69 66 20 74 68 69 73 20 69 73 20   out if this is 
7480: 61 20 63 6f 6f 6b 69 65 20 74 68 61 74 20 69 74  a cookie that it
7490: 20 63 61 72 65 73 20 61 62 6f 75 74 2e 0a 20 20   cares about..  
74a0: 20 20 2a 2f 0a 20 20 20 20 7a 43 6f 6f 6b 69 65    */.    zCookie
74b0: 20 3d 20 64 62 5f 67 65 74 28 22 63 6f 6f 6b 69   = db_get("cooki
74c0: 65 22 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  e", 0);.    if( 
74d0: 7a 43 6f 6f 6b 69 65 20 29 7b 0a 20 20 20 20 20  zCookie ){.     
74e0: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 73   blob_appendf(&s
74f0: 65 6e 64 2c 20 22 63 6f 6f 6b 69 65 20 25 73 5c  end, "cookie %s\
7500: 6e 22 2c 20 7a 43 6f 6f 6b 69 65 29 3b 0a 20 20  n", zCookie);.  
7510: 20 20 7d 0a 20 20 20 20 0a 20 20 20 20 2f 2a 20    }.    .    /* 
7520: 47 65 6e 65 72 61 74 65 20 67 69 6d 6d 65 20 63  Generate gimme c
7530: 61 72 64 73 20 66 6f 72 20 70 68 61 6e 74 6f 6d  ards for phantom
7540: 73 20 61 6e 64 20 6c 65 61 66 20 63 61 72 64 73  s and leaf cards
7550: 0a 20 20 20 20 2a 2a 20 66 6f 72 20 61 6c 6c 20  .    ** for all 
7560: 6c 65 61 76 65 73 2e 0a 20 20 20 20 2a 2f 0a 20  leaves..    */. 
7570: 20 20 20 69 66 28 20 70 75 6c 6c 46 6c 61 67 20     if( pullFlag 
7580: 7c 7c 20 63 6c 6f 6e 65 46 6c 61 67 20 29 7b 0a  || cloneFlag ){.
7590: 20 20 20 20 20 20 72 65 71 75 65 73 74 5f 70 68        request_ph
75a0: 61 6e 74 6f 6d 73 28 26 78 66 65 72 2c 20 6d 78  antoms(&xfer, mx
75b0: 50 68 61 6e 74 6f 6d 52 65 71 29 3b 0a 20 20 20  PhantomReq);.   
75c0: 20 7d 0a 20 20 20 20 69 66 28 20 70 75 73 68 46   }.    if( pushF
75d0: 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 73 65 6e  lag ){.      sen
75e0: 64 5f 75 6e 73 65 6e 74 28 26 78 66 65 72 29 3b  d_unsent(&xfer);
75f0: 0a 20 20 20 20 20 20 6e 43 61 72 64 20 2b 3d 20  .      nCard += 
7600: 73 65 6e 64 5f 75 6e 63 6c 75 73 74 65 72 65 64  send_unclustered
7610: 28 26 78 66 65 72 29 3b 0a 20 20 20 20 7d 0a 0a  (&xfer);.    }..
7620: 20 20 20 20 2f 2a 20 53 65 6e 64 20 63 6f 6e 66      /* Send conf
7630: 69 67 75 72 61 74 69 6f 6e 20 70 61 72 61 6d 65  iguration parame
7640: 74 65 72 20 72 65 71 75 65 73 74 73 20 2a 2f 0a  ter requests */.
7650: 20 20 20 20 69 66 28 20 63 6f 6e 66 69 67 52 63      if( configRc
7660: 76 4d 61 73 6b 20 29 7b 0a 20 20 20 20 20 20 63  vMask ){.      c
7670: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
7680: 3b 0a 20 20 20 20 20 20 7a 4e 61 6d 65 20 3d 20  ;.      zName = 
7690: 63 6f 6e 66 69 67 75 72 65 5f 66 69 72 73 74 5f  configure_first_
76a0: 6e 61 6d 65 28 63 6f 6e 66 69 67 52 63 76 4d 61  name(configRcvMa
76b0: 73 6b 29 3b 0a 20 20 20 20 20 20 77 68 69 6c 65  sk);.      while
76c0: 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 20  ( zName ){.     
76d0: 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28     blob_appendf(
76e0: 26 73 65 6e 64 2c 20 22 72 65 71 63 6f 6e 66 69  &send, "reqconfi
76f0: 67 20 25 73 5c 6e 22 2c 20 7a 4e 61 6d 65 29 3b  g %s\n", zName);
7700: 0a 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 20 3d  .        zName =
7710: 20 63 6f 6e 66 69 67 75 72 65 5f 6e 65 78 74 5f   configure_next_
7720: 6e 61 6d 65 28 63 6f 6e 66 69 67 52 63 76 4d 61  name(configRcvMa
7730: 73 6b 29 3b 0a 20 20 20 20 20 20 20 20 6e 43 61  sk);.        nCa
7740: 72 64 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rd++;.      }.  
7750: 20 20 20 20 69 66 28 20 63 6f 6e 66 69 67 52 63      if( configRc
7760: 76 4d 61 73 6b 20 26 20 28 43 4f 4e 46 49 47 53  vMask & (CONFIGS
7770: 45 54 5f 55 53 45 52 7c 43 4f 4e 46 49 47 53 45  ET_USER|CONFIGSE
7780: 54 5f 54 4b 54 29 20 29 7b 0a 20 20 20 20 20 20  T_TKT) ){.      
7790: 20 20 63 6f 6e 66 69 67 75 72 65 5f 70 72 65 70    configure_prep
77a0: 61 72 65 5f 74 6f 5f 72 65 63 65 69 76 65 28 30  are_to_receive(0
77b0: 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
77c0: 20 63 6f 6e 66 69 67 52 63 76 4d 61 73 6b 20 3d   configRcvMask =
77d0: 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f   0;.    }..    /
77e0: 2a 20 53 65 6e 64 20 63 6f 6e 66 69 67 75 72 61  * Send configura
77f0: 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 73 20  tion parameters 
7800: 62 65 69 6e 67 20 70 75 73 68 65 64 20 2a 2f 0a  being pushed */.
7810: 20 20 20 20 69 66 28 20 63 6f 6e 66 69 67 53 65      if( configSe
7820: 6e 64 4d 61 73 6b 20 29 7b 0a 20 20 20 20 20 20  ndMask ){.      
7830: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
7840: 65 3b 0a 20 20 20 20 20 20 7a 4e 61 6d 65 20 3d  e;.      zName =
7850: 20 63 6f 6e 66 69 67 75 72 65 5f 66 69 72 73 74   configure_first
7860: 5f 6e 61 6d 65 28 63 6f 6e 66 69 67 53 65 6e 64  _name(configSend
7870: 4d 61 73 6b 29 3b 0a 20 20 20 20 20 20 77 68 69  Mask);.      whi
7880: 6c 65 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 20  le( zName ){.   
7890: 20 20 20 20 20 73 65 6e 64 5f 63 6f 6e 66 69 67       send_config
78a0: 5f 63 61 72 64 28 26 78 66 65 72 2c 20 7a 4e 61  _card(&xfer, zNa
78b0: 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 7a 4e 61  me);.        zNa
78c0: 6d 65 20 3d 20 63 6f 6e 66 69 67 75 72 65 5f 6e  me = configure_n
78d0: 65 78 74 5f 6e 61 6d 65 28 63 6f 6e 66 69 67 53  ext_name(configS
78e0: 65 6e 64 4d 61 73 6b 29 3b 0a 20 20 20 20 20 20  endMask);.      
78f0: 20 20 6e 43 61 72 64 2b 2b 3b 0a 20 20 20 20 20    nCard++;.     
7900: 20 7d 0a 20 20 20 20 20 20 63 6f 6e 66 69 67 53   }.      configS
7910: 65 6e 64 4d 61 73 6b 20 3d 20 30 3b 0a 20 20 20  endMask = 0;.   
7920: 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 70 70 65 6e   }..    /* Appen
7930: 64 20 72 61 6e 64 6f 6d 6e 65 73 73 20 74 6f 20  d randomness to 
7940: 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d  the end of the m
7950: 65 73 73 61 67 65 20 2a 2f 0a 20 20 20 20 7a 52  essage */.    zR
7960: 61 6e 64 6f 6d 6e 65 73 73 20 3d 20 64 62 5f 74  andomness = db_t
7970: 65 78 74 28 30 2c 20 22 53 45 4c 45 43 54 20 68  ext(0, "SELECT h
7980: 65 78 28 72 61 6e 64 6f 6d 62 6c 6f 62 28 32 30  ex(randomblob(20
7990: 29 29 22 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 61  ))");.    blob_a
79a0: 70 70 65 6e 64 66 28 26 73 65 6e 64 2c 20 22 23  ppendf(&send, "#
79b0: 20 25 73 5c 6e 22 2c 20 7a 52 61 6e 64 6f 6d 6e   %s\n", zRandomn
79c0: 65 73 73 29 3b 0a 20 20 20 20 66 72 65 65 28 7a  ess);.    free(z
79d0: 52 61 6e 64 6f 6d 6e 65 73 73 29 3b 0a 0a 20 20  Randomness);..  
79e0: 20 20 2f 2a 20 45 78 63 68 61 6e 67 65 20 6d 65    /* Exchange me
79f0: 73 73 61 67 65 73 20 77 69 74 68 20 74 68 65 20  ssages with the 
7a00: 73 65 72 76 65 72 20 2a 2f 0a 20 20 20 20 6e 46  server */.    nF
7a10: 69 6c 65 53 65 6e 64 20 3d 20 78 66 65 72 2e 6e  ileSend = xfer.n
7a20: 46 69 6c 65 53 65 6e 74 20 2b 20 78 66 65 72 2e  FileSent + xfer.
7a30: 6e 44 65 6c 74 61 53 65 6e 74 3b 0a 20 20 20 20  nDeltaSent;.    
7a40: 70 72 69 6e 74 66 28 7a 56 61 6c 75 65 46 6f 72  printf(zValueFor
7a50: 6d 61 74 2c 20 22 53 65 6e 64 3a 22 2c 0a 20 20  mat, "Send:",.  
7a60: 20 20 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 73            blob_s
7a70: 69 7a 65 28 26 73 65 6e 64 29 2c 20 6e 43 61 72  ize(&send), nCar
7a80: 64 2b 78 66 65 72 2e 6e 47 69 6d 6d 65 53 65 6e  d+xfer.nGimmeSen
7a90: 74 2b 78 66 65 72 2e 6e 49 47 6f 74 53 65 6e 74  t+xfer.nIGotSent
7aa0: 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 78 66  ,.            xf
7ab0: 65 72 2e 6e 46 69 6c 65 53 65 6e 74 2c 20 78 66  er.nFileSent, xf
7ac0: 65 72 2e 6e 44 65 6c 74 61 53 65 6e 74 29 3b 0a  er.nDeltaSent);.
7ad0: 20 20 20 20 6e 43 61 72 64 20 3d 20 30 3b 0a 20      nCard = 0;. 
7ae0: 20 20 20 78 66 65 72 2e 6e 46 69 6c 65 53 65 6e     xfer.nFileSen
7af0: 74 20 3d 20 30 3b 0a 20 20 20 20 78 66 65 72 2e  t = 0;.    xfer.
7b00: 6e 44 65 6c 74 61 53 65 6e 74 20 3d 20 30 3b 0a  nDeltaSent = 0;.
7b10: 20 20 20 20 78 66 65 72 2e 6e 47 69 6d 6d 65 53      xfer.nGimmeS
7b20: 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20 66 66 6c  ent = 0;.    ffl
7b30: 75 73 68 28 73 74 64 6f 75 74 29 3b 0a 20 20 20  ush(stdout);.   
7b40: 20 68 74 74 70 5f 65 78 63 68 61 6e 67 65 28 26   http_exchange(&
7b50: 73 65 6e 64 2c 20 26 72 65 63 76 29 3b 0a 20 20  send, &recv);.  
7b60: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 73 65    blob_reset(&se
7b70: 6e 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 42 65 67  nd);..    /* Beg
7b80: 69 6e 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 20  in constructing 
7b90: 74 68 65 20 6e 65 78 74 20 6d 65 73 73 61 67 65  the next message
7ba0: 20 28 77 68 69 63 68 20 6d 69 67 68 74 20 6e 65   (which might ne
7bb0: 76 65 72 20 62 65 0a 20 20 20 20 2a 2a 20 73 65  ver be.    ** se
7bc0: 6e 74 29 20 62 79 20 62 65 67 69 6e 6e 69 6e 67  nt) by beginning
7bd0: 20 77 69 74 68 20 74 68 65 20 70 75 6c 6c 20 6f   with the pull o
7be0: 72 20 70 75 73 68 20 63 61 72 64 73 0a 20 20 20  r push cards.   
7bf0: 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 75 6c 6c   */.    if( pull
7c00: 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 62 6c  Flag ){.      bl
7c10: 6f 62 5f 61 70 70 65 6e 64 66 28 26 73 65 6e 64  ob_appendf(&send
7c20: 2c 20 22 70 75 6c 6c 20 25 73 20 25 73 5c 6e 22  , "pull %s %s\n"
7c30: 2c 20 7a 53 43 6f 64 65 2c 20 7a 50 43 6f 64 65  , zSCode, zPCode
7c40: 29 3b 0a 20 20 20 20 20 20 6e 43 61 72 64 2b 2b  );.      nCard++
7c50: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
7c60: 70 75 73 68 46 6c 61 67 20 29 7b 0a 20 20 20 20  pushFlag ){.    
7c70: 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26    blob_appendf(&
7c80: 73 65 6e 64 2c 20 22 70 75 73 68 20 25 73 20 25  send, "push %s %
7c90: 73 5c 6e 22 2c 20 7a 53 43 6f 64 65 2c 20 7a 50  s\n", zSCode, zP
7ca0: 43 6f 64 65 29 3b 0a 20 20 20 20 20 20 6e 43 61  Code);.      nCa
7cb0: 72 64 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  rd++;.    }..   
7cc0: 20 2f 2a 20 50 72 6f 63 65 73 73 20 74 68 65 20   /* Process the 
7cd0: 72 65 70 6c 79 20 74 68 61 74 20 63 61 6d 65 20  reply that came 
7ce0: 62 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 73 65  back from the se
7cf0: 72 76 65 72 20 2a 2f 0a 20 20 20 20 77 68 69 6c  rver */.    whil
7d00: 65 28 20 62 6c 6f 62 5f 6c 69 6e 65 28 26 72 65  e( blob_line(&re
7d10: 63 76 2c 20 26 78 66 65 72 2e 6c 69 6e 65 29 20  cv, &xfer.line) 
7d20: 29 7b 0a 20 20 20 20 20 20 69 66 28 20 62 6c 6f  ){.      if( blo
7d30: 62 5f 62 75 66 66 65 72 28 26 78 66 65 72 2e 6c  b_buffer(&xfer.l
7d40: 69 6e 65 29 5b 30 5d 3d 3d 27 23 27 20 29 7b 0a  ine)[0]=='#' ){.
7d50: 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65          continue
7d60: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
7d70: 78 66 65 72 2e 6e 54 6f 6b 65 6e 20 3d 20 62 6c  xfer.nToken = bl
7d80: 6f 62 5f 74 6f 6b 65 6e 69 7a 65 28 26 78 66 65  ob_tokenize(&xfe
7d90: 72 2e 6c 69 6e 65 2c 20 78 66 65 72 2e 61 54 6f  r.line, xfer.aTo
7da0: 6b 65 6e 2c 20 63 6f 75 6e 74 28 78 66 65 72 2e  ken, count(xfer.
7db0: 61 54 6f 6b 65 6e 29 29 3b 0a 20 20 20 20 20 20  aToken));.      
7dc0: 6e 43 61 72 64 2b 2b 3b 0a 20 20 20 20 20 20 70  nCard++;.      p
7dd0: 72 69 6e 74 66 28 22 5c 72 25 64 22 2c 20 6e 43  rintf("\r%d", nC
7de0: 61 72 64 29 3b 0a 20 20 20 20 20 20 66 66 6c 75  ard);.      fflu
7df0: 73 68 28 73 74 64 6f 75 74 29 3b 0a 0a 20 20 20  sh(stdout);..   
7e00: 20 20 20 2f 2a 20 20 20 66 69 6c 65 20 55 55 49     /*   file UUI
7e10: 44 20 53 49 5a 45 20 5c 6e 20 43 4f 4e 54 45 4e  D SIZE \n CONTEN
7e20: 54 0a 20 20 20 20 20 20 2a 2a 20 20 20 66 69 6c  T.      **   fil
7e30: 65 20 55 55 49 44 20 44 45 4c 54 41 53 52 43 20  e UUID DELTASRC 
7e40: 53 49 5a 45 20 5c 6e 20 43 4f 4e 54 45 4e 54 0a  SIZE \n CONTENT.
7e50: 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
7e60: 2a 20 52 65 63 65 69 76 65 20 61 20 66 69 6c 65  * Receive a file
7e70: 20 74 72 61 6e 73 6d 69 74 74 65 64 20 66 72 6f   transmitted fro
7e80: 6d 20 74 68 65 20 73 65 72 76 65 72 2e 0a 20 20  m the server..  
7e90: 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
7ea0: 20 62 6c 6f 62 5f 65 71 28 26 78 66 65 72 2e 61   blob_eq(&xfer.a
7eb0: 54 6f 6b 65 6e 5b 30 5d 2c 22 66 69 6c 65 22 29  Token[0],"file")
7ec0: 20 29 7b 0a 20 20 20 20 20 20 20 20 78 66 65 72   ){.        xfer
7ed0: 5f 61 63 63 65 70 74 5f 66 69 6c 65 28 26 78 66  _accept_file(&xf
7ee0: 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  er);.      }else
7ef0: 0a 0a 20 20 20 20 20 20 2f 2a 20 20 20 67 69 6d  ..      /*   gim
7f00: 6d 65 20 55 55 49 44 0a 20 20 20 20 20 20 2a 2a  me UUID.      **
7f10: 0a 20 20 20 20 20 20 2a 2a 20 53 65 72 76 65 72  .      ** Server
7f20: 20 69 73 20 72 65 71 75 65 73 74 69 6e 67 20 61   is requesting a
7f30: 20 66 69 6c 65 2e 20 20 49 66 20 74 68 65 20 66   file.  If the f
7f40: 69 6c 65 20 69 73 20 61 20 6d 61 6e 69 66 65 73  ile is a manifes
7f50: 74 2c 20 61 73 73 75 6d 65 0a 20 20 20 20 20 20  t, assume.      
7f60: 2a 2a 20 74 68 61 74 20 74 68 65 20 73 65 72 76  ** that the serv
7f70: 65 72 20 77 69 6c 6c 20 61 6c 73 6f 20 77 61 6e  er will also wan
7f80: 74 20 74 6f 20 6b 6e 6f 77 20 61 6c 6c 20 6f 66  t to know all of
7f90: 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 66 69 6c   the content fil
7fa0: 65 73 0a 20 20 20 20 20 20 2a 2a 20 61 73 73 6f  es.      ** asso
7fb0: 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
7fc0: 6d 61 6e 69 66 65 73 74 20 61 6e 64 20 73 65 6e  manifest and sen
7fd0: 64 20 74 68 6f 73 65 20 74 6f 6f 2e 0a 20 20 20  d those too..   
7fe0: 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
7ff0: 62 6c 6f 62 5f 65 71 28 26 78 66 65 72 2e 61 54  blob_eq(&xfer.aT
8000: 6f 6b 65 6e 5b 30 5d 2c 20 22 67 69 6d 6d 65 22  oken[0], "gimme"
8010: 29 0a 20 20 20 20 20 20 20 26 26 20 78 66 65 72  ).       && xfer
8020: 2e 6e 54 6f 6b 65 6e 3d 3d 32 0a 20 20 20 20 20  .nToken==2.     
8030: 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69    && blob_is_uui
8040: 64 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31  d(&xfer.aToken[1
8050: 5d 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20  ]).      ){.    
8060: 20 20 20 20 69 66 28 20 70 75 73 68 46 6c 61 67      if( pushFlag
8070: 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e   ){.          in
8080: 74 20 72 69 64 20 3d 20 72 69 64 5f 66 72 6f 6d  t rid = rid_from
8090: 5f 75 75 69 64 28 26 78 66 65 72 2e 61 54 6f 6b  _uuid(&xfer.aTok
80a0: 65 6e 5b 31 5d 2c 20 30 29 3b 0a 20 20 20 20 20  en[1], 0);.     
80b0: 20 20 20 20 20 69 66 28 20 72 69 64 20 29 20 73       if( rid ) s
80c0: 65 6e 64 5f 66 69 6c 65 28 26 78 66 65 72 2c 20  end_file(&xfer, 
80d0: 72 69 64 2c 20 26 78 66 65 72 2e 61 54 6f 6b 65  rid, &xfer.aToke
80e0: 6e 5b 31 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20  n[1], 0);.      
80f0: 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a    }.      }else.
8100: 20 20 0a 20 20 20 20 20 20 2f 2a 20 20 20 69 67    .      /*   ig
8110: 6f 74 20 55 55 49 44 0a 20 20 20 20 20 20 2a 2a  ot UUID.      **
8120: 0a 20 20 20 20 20 20 2a 2a 20 53 65 72 76 65 72  .      ** Server
8130: 20 61 6e 6e 6f 75 6e 63 65 73 20 74 68 61 74 20   announces that 
8140: 69 74 20 68 61 73 20 61 20 70 61 72 74 69 63 75  it has a particu
8150: 6c 61 72 20 66 69 6c 65 2e 20 20 49 66 20 74 68  lar file.  If th
8160: 69 73 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 6e  is is.      ** n
8170: 6f 74 20 61 20 66 69 6c 65 20 74 68 61 74 20 77  ot a file that w
8180: 65 20 68 61 76 65 20 61 6e 64 20 77 65 20 61 72  e have and we ar
8190: 65 20 70 75 6c 6c 69 6e 67 2c 20 74 68 65 6e 20  e pulling, then 
81a0: 63 72 65 61 74 65 20 61 0a 20 20 20 20 20 20 2a  create a.      *
81b0: 2a 20 70 68 61 6e 74 6f 6d 20 74 6f 20 63 61 75  * phantom to cau
81c0: 73 65 20 74 68 69 73 20 66 69 6c 65 20 74 6f 20  se this file to 
81d0: 62 65 20 72 65 71 75 65 73 74 65 64 20 6f 6e 20  be requested on 
81e0: 74 68 65 20 6e 65 78 74 20 63 79 63 6c 65 2e 0a  the next cycle..
81f0: 20 20 20 20 20 20 2a 2a 20 41 6c 77 61 79 73 20        ** Always 
8200: 72 65 6d 65 6d 62 65 72 20 74 68 61 74 20 74 68  remember that th
8210: 65 20 73 65 72 76 65 72 20 68 61 73 20 74 68 69  e server has thi
8220: 73 20 66 69 6c 65 20 73 6f 20 74 68 61 74 20 77  s file so that w
8230: 65 20 64 6f 0a 20 20 20 20 20 20 2a 2a 20 6e 6f  e do.      ** no
8240: 74 20 74 72 61 6e 73 6d 69 74 20 69 74 20 62 79  t transmit it by
8250: 20 61 63 63 69 64 65 6e 74 2e 0a 20 20 20 20 20   accident..     
8260: 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 78 66   */.      if( xf
8270: 65 72 2e 6e 54 6f 6b 65 6e 3d 3d 32 0a 20 20 20  er.nToken==2.   
8280: 20 20 20 20 26 26 20 62 6c 6f 62 5f 65 71 28 26      && blob_eq(&
8290: 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20  xfer.aToken[0], 
82a0: 22 69 67 6f 74 22 29 0a 20 20 20 20 20 20 20 26  "igot").       &
82b0: 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26  & blob_is_uuid(&
82c0: 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d 29 0a  xfer.aToken[1]).
82d0: 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
82e0: 20 69 6e 74 20 72 69 64 20 3d 20 72 69 64 5f 66   int rid = rid_f
82f0: 72 6f 6d 5f 75 75 69 64 28 26 78 66 65 72 2e 61  rom_uuid(&xfer.a
8300: 54 6f 6b 65 6e 5b 31 5d 2c 20 30 29 3b 0a 20 20  Token[1], 0);.  
8310: 20 20 20 20 20 20 69 66 28 20 72 69 64 3e 30 20        if( rid>0 
8320: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e  ){.          con
8330: 74 65 6e 74 5f 6d 61 6b 65 5f 70 75 62 6c 69 63  tent_make_public
8340: 28 72 69 64 29 3b 0a 20 20 20 20 20 20 20 20 7d  (rid);.        }
8350: 65 6c 73 65 20 69 66 28 20 70 75 6c 6c 46 6c 61  else if( pullFla
8360: 67 20 7c 7c 20 63 6c 6f 6e 65 46 6c 61 67 20 29  g || cloneFlag )
8370: 7b 0a 20 20 20 20 20 20 20 20 20 20 72 69 64 20  {.          rid 
8380: 3d 20 63 6f 6e 74 65 6e 74 5f 6e 65 77 28 62 6c  = content_new(bl
8390: 6f 62 5f 73 74 72 28 26 78 66 65 72 2e 61 54 6f  ob_str(&xfer.aTo
83a0: 6b 65 6e 5b 31 5d 29 29 3b 0a 20 20 20 20 20 20  ken[1]));.      
83b0: 20 20 20 20 69 66 28 20 72 69 64 20 29 20 6e 65      if( rid ) ne
83c0: 77 50 68 61 6e 74 6f 6d 20 3d 20 31 3b 0a 20 20  wPhantom = 1;.  
83d0: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
83e0: 72 65 6d 6f 74 65 5f 68 61 73 28 72 69 64 29 3b  remote_has(rid);
83f0: 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a 20 20 20  .      }else.   
8400: 20 0a 20 20 20 20 20 20 0a 20 20 20 20 20 20 2f   .      .      /
8410: 2a 20 20 20 70 75 73 68 20 20 53 45 52 56 45 52  *   push  SERVER
8420: 43 4f 44 45 20 20 50 52 4f 44 55 43 54 43 4f 44  CODE  PRODUCTCOD
8430: 45 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  E.      **.     
8440: 20 2a 2a 20 53 68 6f 75 6c 64 20 6f 6e 6c 79 20   ** Should only 
8450: 68 61 70 70 65 6e 20 69 6e 20 72 65 73 70 6f 6e  happen in respon
8460: 73 65 20 74 6f 20 61 20 63 6c 6f 6e 65 2e 20 20  se to a clone.  
8470: 54 68 69 73 20 6d 65 73 73 61 67 65 20 74 65 6c  This message tel
8480: 6c 73 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  ls.      ** the 
8490: 63 6c 69 65 6e 74 20 77 68 61 74 20 70 72 6f 64  client what prod
84a0: 75 63 74 20 74 6f 20 75 73 65 20 66 6f 72 20 74  uct to use for t
84b0: 68 65 20 6e 65 77 20 64 61 74 61 62 61 73 65 2e  he new database.
84c0: 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
84d0: 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 78 66 65  if( blob_eq(&xfe
84e0: 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 22 70 75 73  r.aToken[0],"pus
84f0: 68 22 29 0a 20 20 20 20 20 20 20 26 26 20 78 66  h").       && xf
8500: 65 72 2e 6e 54 6f 6b 65 6e 3d 3d 33 0a 20 20 20  er.nToken==3.   
8510: 20 20 20 20 26 26 20 63 6c 6f 6e 65 46 6c 61 67      && cloneFlag
8520: 0a 20 20 20 20 20 20 20 26 26 20 62 6c 6f 62 5f  .       && blob_
8530: 69 73 5f 75 75 69 64 28 26 78 66 65 72 2e 61 54  is_uuid(&xfer.aT
8540: 6f 6b 65 6e 5b 31 5d 29 0a 20 20 20 20 20 20 20  oken[1]).       
8550: 26 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28  && blob_is_uuid(
8560: 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 32 5d 29  &xfer.aToken[2])
8570: 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20  .      ){.      
8580: 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 5f 73 74    if( blob_eq_st
8590: 72 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31  r(&xfer.aToken[1
85a0: 5d 2c 20 7a 53 43 6f 64 65 2c 20 2d 31 29 20 29  ], zSCode, -1) )
85b0: 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 73 73  {.          foss
85c0: 69 6c 5f 66 61 74 61 6c 28 22 73 65 72 76 65 72  il_fatal("server
85d0: 20 6c 6f 6f 70 22 29 3b 0a 20 20 20 20 20 20 20   loop");.       
85e0: 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a   }.        if( z
85f0: 50 43 6f 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20  PCode==0 ){.    
8600: 20 20 20 20 20 20 7a 50 43 6f 64 65 20 3d 20 6d        zPCode = m
8610: 70 72 69 6e 74 66 28 22 25 62 22 2c 20 26 78 66  printf("%b", &xf
8620: 65 72 2e 61 54 6f 6b 65 6e 5b 32 5d 29 3b 0a 20  er.aToken[2]);. 
8630: 20 20 20 20 20 20 20 20 20 64 62 5f 73 65 74 28           db_set(
8640: 22 70 72 6f 6a 65 63 74 2d 63 6f 64 65 22 2c 20  "project-code", 
8650: 7a 50 43 6f 64 65 2c 20 30 29 3b 0a 20 20 20 20  zPCode, 0);.    
8660: 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 6c      }.        bl
8670: 6f 62 5f 61 70 70 65 6e 64 66 28 26 73 65 6e 64  ob_appendf(&send
8680: 2c 20 22 63 6c 6f 6e 65 5c 6e 22 29 3b 0a 20 20  , "clone\n");.  
8690: 20 20 20 20 20 20 6e 43 61 72 64 2b 2b 3b 0a 20        nCard++;. 
86a0: 20 20 20 20 20 7d 65 6c 73 65 0a 20 20 20 20 20       }else.     
86b0: 20 0a 20 20 20 20 20 20 2f 2a 20 20 20 63 6f 6e   .      /*   con
86c0: 66 69 67 20 4e 41 4d 45 20 53 49 5a 45 20 5c 6e  fig NAME SIZE \n
86d0: 20 43 4f 4e 54 45 4e 54 0a 20 20 20 20 20 20 2a   CONTENT.      *
86e0: 2a 0a 20 20 20 20 20 20 2a 2a 20 52 65 63 65 69  *.      ** Recei
86f0: 76 65 20 61 20 63 6f 6e 66 69 67 75 72 61 74 69  ve a configurati
8700: 6f 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68  on value from th
8710: 65 20 73 65 72 76 65 72 2e 0a 20 20 20 20 20 20  e server..      
8720: 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 62 6c 6f  */.      if( blo
8730: 62 5f 65 71 28 26 78 66 65 72 2e 61 54 6f 6b 65  b_eq(&xfer.aToke
8740: 6e 5b 30 5d 2c 22 63 6f 6e 66 69 67 22 29 20 26  n[0],"config") &
8750: 26 20 78 66 65 72 2e 6e 54 6f 6b 65 6e 3d 3d 33  & xfer.nToken==3
8760: 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 62 6c  .          && bl
8770: 6f 62 5f 69 73 5f 69 6e 74 28 26 78 66 65 72 2e  ob_is_int(&xfer.
8780: 61 54 6f 6b 65 6e 5b 32 5d 2c 20 26 73 69 7a 65  aToken[2], &size
8790: 29 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e  ) ){.        con
87a0: 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d  st char *zName =
87b0: 20 62 6c 6f 62 5f 73 74 72 28 26 78 66 65 72 2e   blob_str(&xfer.
87c0: 61 54 6f 6b 65 6e 5b 31 5d 29 3b 0a 20 20 20 20  aToken[1]);.    
87d0: 20 20 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e 74      Blob content
87e0: 3b 0a 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 7a  ;.        blob_z
87f0: 65 72 6f 28 26 63 6f 6e 74 65 6e 74 29 3b 0a 20  ero(&content);. 
8800: 20 20 20 20 20 20 20 62 6c 6f 62 5f 65 78 74 72         blob_extr
8810: 61 63 74 28 78 66 65 72 2e 70 49 6e 2c 20 73 69  act(xfer.pIn, si
8820: 7a 65 2c 20 26 63 6f 6e 74 65 6e 74 29 3b 0a 20  ze, &content);. 
8830: 20 20 20 20 20 20 20 67 2e 6f 6b 41 64 6d 69 6e         g.okAdmin
8840: 20 3d 20 67 2e 6f 6b 52 64 41 64 64 72 20 3d 20   = g.okRdAddr = 
8850: 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63  1;.        if( c
8860: 6f 6e 66 69 67 75 72 65 5f 69 73 5f 65 78 70 6f  onfigure_is_expo
8870: 72 74 61 62 6c 65 28 7a 4e 61 6d 65 29 20 26 20  rtable(zName) & 
8880: 6f 72 69 67 43 6f 6e 66 69 67 52 63 76 4d 61 73  origConfigRcvMas
8890: 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  k ){.          i
88a0: 66 28 20 7a 4e 61 6d 65 5b 30 5d 21 3d 27 40 27  f( zName[0]!='@'
88b0: 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
88c0: 69 66 28 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65  if( strcmp(zName
88d0: 2c 20 22 6c 6f 67 6f 2d 69 6d 61 67 65 22 29 3d  , "logo-image")=
88e0: 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
88f0: 20 20 20 20 53 74 6d 74 20 69 6e 73 3b 0a 20 20      Stmt ins;.  
8900: 20 20 20 20 20 20 20 20 20 20 20 20 64 62 5f 70              db_p
8910: 72 65 70 61 72 65 28 26 69 6e 73 2c 0a 20 20 20  repare(&ins,.   
8920: 20 20 20 20 20 20 20 20 20 20 20 20 20 22 52 45               "RE
8930: 50 4c 41 43 45 20 49 4e 54 4f 20 63 6f 6e 66 69  PLACE INTO confi
8940: 67 28 6e 61 6d 65 2c 20 76 61 6c 75 65 29 20 56  g(name, value) V
8950: 41 4c 55 45 53 28 3a 6e 61 6d 65 2c 20 3a 76 61  ALUES(:name, :va
8960: 6c 75 65 29 22 0a 20 20 20 20 20 20 20 20 20 20  lue)".          
8970: 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 20      );.         
8980: 20 20 20 20 20 64 62 5f 62 69 6e 64 5f 74 65 78       db_bind_tex
8990: 74 28 26 69 6e 73 2c 20 22 3a 6e 61 6d 65 22 2c  t(&ins, ":name",
89a0: 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20   zName);.       
89b0: 20 20 20 20 20 20 20 64 62 5f 62 69 6e 64 5f 62         db_bind_b
89c0: 6c 6f 62 28 26 69 6e 73 2c 20 22 3a 76 61 6c 75  lob(&ins, ":valu
89d0: 65 22 2c 20 26 63 6f 6e 74 65 6e 74 29 3b 0a 20  e", &content);. 
89e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 64 62 5f               db_
89f0: 73 74 65 70 28 26 69 6e 73 29 3b 0a 20 20 20 20  step(&ins);.    
8a00: 20 20 20 20 20 20 20 20 20 20 64 62 5f 66 69 6e            db_fin
8a10: 61 6c 69 7a 65 28 26 69 6e 73 29 3b 0a 20 20 20  alize(&ins);.   
8a20: 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a           }else{.
8a30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 62                db
8a40: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20  _multi_exec(.   
8a50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
8a60: 52 45 50 4c 41 43 45 20 49 4e 54 4f 20 63 6f 6e  REPLACE INTO con
8a70: 66 69 67 28 6e 61 6d 65 2c 76 61 6c 75 65 29 20  fig(name,value) 
8a80: 56 41 4c 55 45 53 28 25 51 2c 25 51 29 22 2c 0a  VALUES(%Q,%Q)",.
8a90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8aa0: 20 20 7a 4e 61 6d 65 2c 20 62 6c 6f 62 5f 73 74    zName, blob_st
8ab0: 72 28 26 63 6f 6e 74 65 6e 74 29 0a 20 20 20 20  r(&content).    
8ac0: 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20            );.   
8ad0: 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
8ae0: 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
8af0: 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 69 63          /* Notic
8b00: 65 20 74 68 61 74 20 77 65 20 61 72 65 20 65 76  e that we are ev
8b10: 61 6c 75 61 74 69 6e 67 20 61 72 62 69 74 72 61  aluating arbitra
8b20: 72 79 20 53 51 4c 20 72 65 63 65 69 76 65 64 20  ry SQL received 
8b30: 66 72 6f 6d 20 74 68 65 0a 20 20 20 20 20 20 20  from the.       
8b40: 20 20 20 20 20 2a 2a 20 73 65 72 76 65 72 2e 20       ** server. 
8b50: 20 42 75 74 20 74 68 69 73 20 63 61 6e 20 6f 6e   But this can on
8b60: 6c 79 20 68 61 70 70 65 6e 20 69 66 20 77 65 20  ly happen if we 
8b70: 68 61 76 65 20 73 70 65 63 69 66 69 63 61 6c 6c  have specificall
8b80: 79 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a  y.            **
8b90: 20 72 65 71 75 65 73 74 65 64 20 63 6f 6e 66 69   requested confi
8ba0: 67 75 72 61 74 69 6f 6e 20 69 6e 66 6f 72 6d 61  guration informa
8bb0: 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 73 65  tion from the se
8bc0: 72 76 65 72 2c 20 73 6f 0a 20 20 20 20 20 20 20  rver, so.       
8bd0: 20 20 20 20 20 2a 2a 20 70 72 65 73 75 6d 61 62       ** presumab
8be0: 6c 79 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20  ly the operator 
8bf0: 74 72 75 73 74 73 20 74 68 65 20 73 65 72 76 65  trusts the serve
8c00: 72 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  r..            *
8c10: 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 62  /.            db
8c20: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22 25 73 22  _multi_exec("%s"
8c30: 2c 20 62 6c 6f 62 5f 73 74 72 28 26 63 6f 6e 74  , blob_str(&cont
8c40: 65 6e 74 29 29 3b 0a 20 20 20 20 20 20 20 20 20  ent));.         
8c50: 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
8c60: 20 20 20 20 20 6e 43 61 72 64 2b 2b 3b 0a 20 20       nCard++;.  
8c70: 20 20 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74        blob_reset
8c80: 28 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 20 20  (&content);.    
8c90: 20 20 20 20 62 6c 6f 62 5f 73 65 65 6b 28 78 66      blob_seek(xf
8ca0: 65 72 2e 70 49 6e 2c 20 31 2c 20 42 4c 4f 42 5f  er.pIn, 1, BLOB_
8cb0: 53 45 45 4b 5f 43 55 52 29 3b 0a 20 20 20 20 20  SEEK_CUR);.     
8cc0: 20 7d 65 6c 73 65 0a 0a 20 20 20 20 20 20 0a 20   }else..      . 
8cd0: 20 20 20 20 20 2f 2a 20 20 20 20 63 6f 6f 6b 69       /*    cooki
8ce0: 65 20 54 45 58 54 0a 20 20 20 20 20 20 2a 2a 0a  e TEXT.      **.
8cf0: 20 20 20 20 20 20 2a 2a 20 54 68 65 20 73 65 72        ** The ser
8d00: 76 65 72 20 6d 69 67 68 74 20 69 6e 63 6c 75 64  ver might includ
8d10: 65 20 61 20 63 6f 6f 6b 69 65 20 69 6e 20 69 74  e a cookie in it
8d20: 73 20 72 65 70 6c 79 2e 20 20 54 68 65 20 63 6c  s reply.  The cl
8d30: 69 65 6e 74 0a 20 20 20 20 20 20 2a 2a 20 73 68  ient.      ** sh
8d40: 6f 75 6c 64 20 72 65 6d 65 6d 62 65 72 20 74 68  ould remember th
8d50: 69 73 20 63 6f 6f 6b 69 65 20 61 6e 64 20 73 65  is cookie and se
8d60: 6e 64 20 69 74 20 62 61 63 6b 20 74 6f 20 74 68  nd it back to th
8d70: 65 20 73 65 72 76 65 72 0a 20 20 20 20 20 20 2a  e server.      *
8d80: 2a 20 69 6e 20 69 74 73 20 6e 65 78 74 20 71 75  * in its next qu
8d90: 65 72 79 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20  ery..      **.  
8da0: 20 20 20 20 2a 2a 20 45 61 63 68 20 63 6f 6f 6b      ** Each cook
8db0: 69 65 20 72 65 63 65 69 76 65 64 20 6f 76 65 72  ie received over
8dc0: 77 72 69 74 65 73 20 74 68 65 20 70 72 69 6f 72  writes the prior
8dd0: 20 63 6f 6f 6b 69 65 20 66 72 6f 6d 20 74 68 65   cookie from the
8de0: 0a 20 20 20 20 20 20 2a 2a 20 73 61 6d 65 20 73  .      ** same s
8df0: 65 72 76 65 72 2e 0a 20 20 20 20 20 20 2a 2f 0a  erver..      */.
8e00: 20 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65        if( blob_e
8e10: 71 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 30  q(&xfer.aToken[0
8e20: 5d 2c 20 22 63 6f 6f 6b 69 65 22 29 20 26 26 20  ], "cookie") && 
8e30: 78 66 65 72 2e 6e 54 6f 6b 65 6e 3d 3d 32 20 29  xfer.nToken==2 )
8e40: 7b 0a 20 20 20 20 20 20 20 20 64 62 5f 73 65 74  {.        db_set
8e50: 28 22 63 6f 6f 6b 69 65 22 2c 20 62 6c 6f 62 5f  ("cookie", blob_
8e60: 73 74 72 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e  str(&xfer.aToken
8e70: 5b 31 5d 29 2c 20 30 29 3b 0a 20 20 20 20 20 20  [1]), 0);.      
8e80: 7d 65 6c 73 65 0a 0a 20 20 20 20 20 20 2f 2a 20  }else..      /* 
8e90: 20 20 6d 65 73 73 61 67 65 20 4d 45 53 53 41 47    message MESSAG
8ea0: 45 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  E.      **.     
8eb0: 20 2a 2a 20 50 72 69 6e 74 20 61 20 6d 65 73 73   ** Print a mess
8ec0: 61 67 65 2e 20 20 53 69 6d 69 6c 61 72 20 74 6f  age.  Similar to
8ed0: 20 22 65 72 72 6f 72 22 20 62 75 74 20 64 6f 65   "error" but doe
8ee0: 73 20 6e 6f 74 20 73 74 6f 70 20 70 72 6f 63 65  s not stop proce
8ef0: 73 73 69 6e 67 0a 20 20 20 20 20 20 2a 2f 20 20  ssing.      */  
8f00: 20 20 20 20 20 20 0a 20 20 20 20 20 20 69 66 28        .      if(
8f10: 20 62 6c 6f 62 5f 65 71 28 26 78 66 65 72 2e 61   blob_eq(&xfer.a
8f20: 54 6f 6b 65 6e 5b 30 5d 2c 22 6d 65 73 73 61 67  Token[0],"messag
8f30: 65 22 29 20 26 26 20 78 66 65 72 2e 6e 54 6f 6b  e") && xfer.nTok
8f40: 65 6e 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20 20  en==2 ){.       
8f50: 20 63 68 61 72 20 2a 7a 4d 73 67 20 3d 20 62 6c   char *zMsg = bl
8f60: 6f 62 5f 74 65 72 6d 69 6e 61 74 65 28 26 78 66  ob_terminate(&xf
8f70: 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d 29 3b 0a 20  er.aToken[1]);. 
8f80: 20 20 20 20 20 20 20 64 65 66 6f 73 73 69 6c 69         defossili
8f90: 7a 65 28 7a 4d 73 67 29 3b 0a 20 20 20 20 20 20  ze(zMsg);.      
8fa0: 20 20 70 72 69 6e 74 66 28 22 53 65 72 76 65 72    printf("Server
8fb0: 20 73 61 79 73 3a 20 25 73 5c 6e 22 2c 20 7a 4d   says: %s\n", zM
8fc0: 73 67 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  sg);.      }else
8fd0: 0a 0a 20 20 20 20 20 20 2f 2a 20 20 20 65 72 72  ..      /*   err
8fe0: 6f 72 20 4d 45 53 53 41 47 45 0a 20 20 20 20 20  or MESSAGE.     
8ff0: 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 52 65 70   **.      ** Rep
9000: 6f 72 74 20 61 6e 20 65 72 72 6f 72 20 61 6e 64  ort an error and
9010: 20 61 62 61 6e 64 6f 6e 20 74 68 65 20 73 79 6e   abandon the syn
9020: 63 20 73 65 73 73 69 6f 6e 0a 20 20 20 20 20 20  c session.      
9030: 2a 2f 20 20 20 20 20 20 20 20 0a 20 20 20 20 20  */        .     
9040: 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 78 66   if( blob_eq(&xf
9050: 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 22 65 72  er.aToken[0],"er
9060: 72 6f 72 22 29 20 26 26 20 78 66 65 72 2e 6e 54  ror") && xfer.nT
9070: 6f 6b 65 6e 3d 3d 32 20 29 7b 0a 20 20 20 20 20  oken==2 ){.     
9080: 20 20 20 63 68 61 72 20 2a 7a 4d 73 67 20 3d 20     char *zMsg = 
9090: 62 6c 6f 62 5f 74 65 72 6d 69 6e 61 74 65 28 26  blob_terminate(&
90a0: 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d 29 3b  xfer.aToken[1]);
90b0: 0a 20 20 20 20 20 20 20 20 64 65 66 6f 73 73 69  .        defossi
90c0: 6c 69 7a 65 28 7a 4d 73 67 29 3b 0a 20 20 20 20  lize(zMsg);.    
90d0: 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66      blob_appendf
90e0: 28 26 78 66 65 72 2e 65 72 72 2c 20 22 73 65 72  (&xfer.err, "ser
90f0: 76 65 72 20 73 61 79 73 3a 20 25 73 22 2c 20 7a  ver says: %s", z
9100: 4d 73 67 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  Msg);.      }els
9110: 65 0a 0a 20 20 20 20 20 20 2f 2a 20 55 6e 6b 6e  e..      /* Unkn
9120: 6f 77 6e 20 6d 65 73 73 61 67 65 20 2a 2f 0a 20  own message */. 
9130: 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 69       {.        i
9140: 66 28 20 62 6c 6f 62 5f 73 74 72 28 26 78 66 65  f( blob_str(&xfe
9150: 72 2e 61 54 6f 6b 65 6e 5b 30 5d 29 5b 30 5d 3d  r.aToken[0])[0]=
9160: 3d 27 3c 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='<' ){.        
9170: 20 20 66 6f 73 73 69 6c 5f 66 61 74 61 6c 28 0a    fossil_fatal(.
9180: 20 20 20 20 20 20 20 20 20 20 20 20 22 73 65 72              "ser
9190: 76 65 72 20 72 65 70 6c 69 65 73 20 77 69 74 68  ver replies with
91a0: 20 48 54 4d 4c 20 69 6e 73 74 65 61 64 20 6f 66   HTML instead of
91b0: 20 66 6f 73 73 69 6c 20 73 79 6e 63 20 70 72 6f   fossil sync pro
91c0: 74 6f 63 6f 6c 3a 5c 6e 25 62 22 2c 0a 20 20 20  tocol:\n%b",.   
91d0: 20 20 20 20 20 20 20 20 20 26 72 65 63 76 0a 20           &recv. 
91e0: 20 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20           );.    
91f0: 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 6c      }.        bl
9200: 6f 62 5f 61 70 70 65 6e 64 66 28 26 78 66 65 72  ob_appendf(&xfer
9210: 2e 65 72 72 2c 20 22 75 6e 6b 6e 6f 77 6e 20 63  .err, "unknown c
9220: 6f 6d 6d 61 6e 64 3a 20 25 62 22 2c 20 26 78 66  ommand: %b", &xf
9230: 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d 29 3b 0a 20  er.aToken[0]);. 
9240: 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66       }..      if
9250: 28 20 62 6c 6f 62 5f 73 69 7a 65 28 26 78 66 65  ( blob_size(&xfe
9260: 72 2e 65 72 72 29 20 29 7b 0a 20 20 20 20 20 20  r.err) ){.      
9270: 20 20 66 6f 73 73 69 6c 5f 66 61 74 61 6c 28 22    fossil_fatal("
9280: 25 62 22 2c 20 26 78 66 65 72 2e 65 72 72 29 3b  %b", &xfer.err);
9290: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
92a0: 6c 6f 62 61 72 72 61 79 5f 72 65 73 65 74 28 78  lobarray_reset(x
92b0: 66 65 72 2e 61 54 6f 6b 65 6e 2c 20 78 66 65 72  fer.aToken, xfer
92c0: 2e 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20  .nToken);.      
92d0: 62 6c 6f 62 5f 72 65 73 65 74 28 26 78 66 65 72  blob_reset(&xfer
92e0: 2e 6c 69 6e 65 29 3b 0a 20 20 20 20 7d 0a 20 20  .line);.    }.  
92f0: 20 20 69 66 28 20 6f 72 69 67 43 6f 6e 66 69 67    if( origConfig
9300: 52 63 76 4d 61 73 6b 20 26 20 28 43 4f 4e 46 49  RcvMask & (CONFI
9310: 47 53 45 54 5f 54 4b 54 7c 43 4f 4e 46 49 47 53  GSET_TKT|CONFIGS
9320: 45 54 5f 55 53 45 52 29 20 29 7b 0a 20 20 20 20  ET_USER) ){.    
9330: 20 20 63 6f 6e 66 69 67 75 72 65 5f 66 69 6e 61    configure_fina
9340: 6c 69 7a 65 5f 72 65 63 65 69 76 65 28 29 3b 0a  lize_receive();.
9350: 20 20 20 20 7d 0a 20 20 20 20 6f 72 69 67 43 6f      }.    origCo
9360: 6e 66 69 67 52 63 76 4d 61 73 6b 20 3d 20 30 3b  nfigRcvMask = 0;
9370: 0a 20 20 20 20 70 72 69 6e 74 66 28 7a 56 61 6c  .    printf(zVal
9380: 75 65 46 6f 72 6d 61 74 2c 20 22 52 65 63 65 69  ueFormat, "Recei
9390: 76 65 64 3a 22 2c 0a 20 20 20 20 20 20 20 20 20  ved:",.         
93a0: 20 20 20 62 6c 6f 62 5f 73 69 7a 65 28 26 72 65     blob_size(&re
93b0: 63 76 29 2c 20 6e 43 61 72 64 2c 0a 20 20 20 20  cv), nCard,.    
93c0: 20 20 20 20 20 20 20 20 78 66 65 72 2e 6e 46 69          xfer.nFi
93d0: 6c 65 52 63 76 64 2c 20 78 66 65 72 2e 6e 44 65  leRcvd, xfer.nDe
93e0: 6c 74 61 52 63 76 64 20 2b 20 78 66 65 72 2e 6e  ltaRcvd + xfer.n
93f0: 44 61 6e 67 6c 69 6e 67 46 69 6c 65 29 3b 0a 20  DanglingFile);. 
9400: 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 72     blob_reset(&r
9410: 65 63 76 29 3b 0a 20 20 20 20 6e 43 79 63 6c 65  ecv);.    nCycle
9420: 2b 2b 3b 0a 20 20 20 20 67 6f 20 3d 20 30 3b 0a  ++;.    go = 0;.
9430: 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 72 65  .    /* If we re
9440: 63 65 69 76 65 64 20 6f 6e 65 20 6f 72 20 6d 6f  ceived one or mo
9450: 72 65 20 66 69 6c 65 73 20 6f 6e 20 74 68 65 20  re files on the 
9460: 70 72 65 76 69 6f 75 73 20 65 78 63 68 61 6e 67  previous exchang
9470: 65 20 62 75 74 0a 20 20 20 20 2a 2a 20 74 68 65  e but.    ** the
9480: 72 65 20 61 72 65 20 73 74 69 6c 6c 20 70 68 61  re are still pha
9490: 6e 74 6f 6d 73 2c 20 74 68 65 6e 20 67 6f 20 61  ntoms, then go a
94a0: 6e 6f 74 68 65 72 20 72 6f 75 6e 64 2e 0a 20 20  nother round..  
94b0: 20 20 2a 2f 0a 20 20 20 20 6e 46 69 6c 65 52 65    */.    nFileRe
94c0: 63 76 20 3d 20 78 66 65 72 2e 6e 46 69 6c 65 52  cv = xfer.nFileR
94d0: 63 76 64 20 2b 20 78 66 65 72 2e 6e 44 65 6c 74  cvd + xfer.nDelt
94e0: 61 52 63 76 64 20 2b 20 78 66 65 72 2e 6e 44 61  aRcvd + xfer.nDa
94f0: 6e 67 6c 69 6e 67 46 69 6c 65 3b 0a 20 20 20 20  nglingFile;.    
9500: 69 66 28 20 28 6e 46 69 6c 65 52 65 63 76 3e 30  if( (nFileRecv>0
9510: 20 7c 7c 20 6e 65 77 50 68 61 6e 74 6f 6d 29 20   || newPhantom) 
9520: 26 26 20 64 62 5f 65 78 69 73 74 73 28 22 53 45  && db_exists("SE
9530: 4c 45 43 54 20 31 20 46 52 4f 4d 20 70 68 61 6e  LECT 1 FROM phan
9540: 74 6f 6d 22 29 20 29 7b 0a 20 20 20 20 20 20 67  tom") ){.      g
9550: 6f 20 3d 20 31 3b 0a 20 20 20 20 20 20 6d 78 50  o = 1;.      mxP
9560: 68 61 6e 74 6f 6d 52 65 71 20 3d 20 6e 46 69 6c  hantomReq = nFil
9570: 65 52 65 63 76 2a 32 3b 0a 20 20 20 20 20 20 69  eRecv*2;.      i
9580: 66 28 20 6d 78 50 68 61 6e 74 6f 6d 52 65 71 3c  f( mxPhantomReq<
9590: 32 30 30 20 29 20 6d 78 50 68 61 6e 74 6f 6d 52  200 ) mxPhantomR
95a0: 65 71 20 3d 20 32 30 30 3b 0a 20 20 20 20 7d 0a  eq = 200;.    }.
95b0: 20 20 20 20 6e 43 61 72 64 20 3d 20 30 3b 0a 20      nCard = 0;. 
95c0: 20 20 20 78 66 65 72 2e 6e 46 69 6c 65 52 63 76     xfer.nFileRcv
95d0: 64 20 3d 20 30 3b 0a 20 20 20 20 78 66 65 72 2e  d = 0;.    xfer.
95e0: 6e 44 65 6c 74 61 52 63 76 64 20 3d 20 30 3b 0a  nDeltaRcvd = 0;.
95f0: 20 20 20 20 78 66 65 72 2e 6e 44 61 6e 67 6c 69      xfer.nDangli
9600: 6e 67 46 69 6c 65 20 3d 20 30 3b 0a 0a 20 20 20  ngFile = 0;..   
9610: 20 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 6f   /* If we have o
9620: 6e 65 20 6f 72 20 6d 6f 72 65 20 66 69 6c 65 73  ne or more files
9630: 20 71 75 65 75 65 64 20 74 6f 20 73 65 6e 64 2c   queued to send,
9640: 20 74 68 65 6e 20 67 6f 0a 20 20 20 20 2a 2a 20   then go.    ** 
9650: 61 6e 6f 74 68 65 72 20 72 6f 75 6e 64 20 0a 20  another round . 
9660: 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 78 66     */.    if( xf
9670: 65 72 2e 6e 46 69 6c 65 53 65 6e 74 2b 78 66 65  er.nFileSent+xfe
9680: 72 2e 6e 44 65 6c 74 61 53 65 6e 74 3e 30 20 29  r.nDeltaSent>0 )
9690: 7b 0a 20 20 20 20 20 20 67 6f 20 3d 20 31 3b 0a  {.      go = 1;.
96a0: 20 20 20 20 7d 0a 20 20 7d 3b 0a 20 20 74 72 61      }.  };.  tra
96b0: 6e 73 70 6f 72 74 5f 73 74 61 74 73 28 26 6e 53  nsport_stats(&nS
96c0: 65 6e 74 2c 20 26 6e 52 63 76 64 2c 20 31 29 3b  ent, &nRcvd, 1);
96d0: 0a 20 20 70 72 69 6e 74 66 28 22 54 6f 74 61 6c  .  printf("Total
96e0: 20 6e 65 74 77 6f 72 6b 20 74 72 61 66 66 69 63   network traffic
96f0: 3a 20 25 64 20 62 79 74 65 73 20 73 65 6e 74 2c  : %d bytes sent,
9700: 20 25 64 20 62 79 74 65 73 20 72 65 63 65 69 76   %d bytes receiv
9710: 65 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20  ed\n",.         
9720: 6e 53 65 6e 74 2c 20 6e 52 63 76 64 29 3b 0a 20  nSent, nRcvd);. 
9730: 20 74 72 61 6e 73 70 6f 72 74 5f 63 6c 6f 73 65   transport_close
9740: 28 29 3b 0a 20 20 73 6f 63 6b 65 74 5f 67 6c 6f  ();.  socket_glo
9750: 62 61 6c 5f 73 68 75 74 64 6f 77 6e 28 29 3b 0a  bal_shutdown();.
9760: 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28    db_multi_exec(
9770: 22 44 52 4f 50 20 54 41 42 4c 45 20 6f 6e 72 65  "DROP TABLE onre
9780: 6d 6f 74 65 22 29 3b 0a 20 20 6d 61 6e 69 66 65  mote");.  manife
9790: 73 74 5f 63 72 6f 73 73 6c 69 6e 6b 5f 65 6e 64  st_crosslink_end
97a0: 28 29 3b 0a 20 20 64 62 5f 65 6e 64 5f 74 72 61  ();.  db_end_tra
97b0: 6e 73 61 63 74 69 6f 6e 28 30 29 3b 0a 7d 0a     nsaction(0);.}.