Hex Artifact Content
Not logged in

Artifact d3ef278c076cf5d1f39d20fbfc5b4a485377b6a5:

File src/xfer.c part of check-in [dccd27c6da] - Added a global command line argument named -quiet. This will suppress some output (currently during sync) for those interfacing fossil with various development tools. It does not make eliminate all output, only the output that is meant for fancy display to the user on a console. You can now use this new flag anywhere by referencing if (!g.fQuiet) {...} by jeremy_c on 2009-12-14 23:18:54. Also file src/xfer.c part of check-in [76bc05d739] - merge with trunk by btheado on 2009-12-30 20:33:59.

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 53 65 6e 74 20 3d 20 30 3b 20 20 20 20 20  rdSent = 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 2a 2f 0a 20 20 69 6e  rds sent */.  in
6c10: 74 20 6e 43 61 72 64 52 63 76 64 20 3d 20 30 3b  t nCardRcvd = 0;
6c20: 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
6c30: 6f 66 20 63 61 72 64 73 20 72 65 63 65 69 76 65  of cards receive
6c40: 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 79 63 6c  d */.  int nCycl
6c50: 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f  e = 0;         /
6c60: 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 6f 75 6e  * Number of roun
6c70: 64 20 74 72 69 70 73 20 74 6f 20 74 68 65 20 73  d trips to the s
6c80: 65 72 76 65 72 20 2a 2f 0a 20 20 69 6e 74 20 73  erver */.  int s
6c90: 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ize;            
6ca0: 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 20     /* Size of a 
6cb0: 63 6f 6e 66 69 67 20 76 61 6c 75 65 20 2a 2f 0a  config value */.
6cc0: 20 20 69 6e 74 20 6e 46 69 6c 65 53 65 6e 64 20    int nFileSend 
6cd0: 3d 20 30 3b 0a 20 20 69 6e 74 20 6f 72 69 67 43  = 0;.  int origC
6ce0: 6f 6e 66 69 67 52 63 76 4d 61 73 6b 3b 20 20 2f  onfigRcvMask;  /
6cf0: 2a 20 4f 72 69 67 69 6e 61 6c 20 76 61 6c 75 65  * Original value
6d00: 20 6f 66 20 63 6f 6e 66 69 67 52 63 76 4d 61 73   of configRcvMas
6d10: 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 6c 65  k */.  int nFile
6d20: 52 65 63 76 3b 20 20 20 20 20 20 20 20 20 20 2f  Recv;          /
6d30: 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 69 6c 65  * Number of file
6d40: 73 20 72 65 63 65 69 76 65 64 20 2a 2f 0a 20 20  s received */.  
6d50: 69 6e 74 20 6d 78 50 68 61 6e 74 6f 6d 52 65 71  int mxPhantomReq
6d60: 20 3d 20 32 30 30 3b 20 2f 2a 20 4d 61 78 20 6e   = 200; /* Max n
6d70: 75 6d 62 65 72 20 6f 66 20 70 68 61 6e 74 6f 6d  umber of phantom
6d80: 73 20 74 6f 20 72 65 71 75 65 73 74 20 70 65 72  s to request per
6d90: 20 63 6f 6d 6d 20 2a 2f 0a 20 20 63 6f 6e 73 74   comm */.  const
6da0: 20 63 68 61 72 20 2a 7a 43 6f 6f 6b 69 65 3b 20   char *zCookie; 
6db0: 20 20 20 2f 2a 20 53 65 72 76 65 72 20 63 6f 6f     /* Server coo
6dc0: 6b 69 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 65  kie */.  int nSe
6dd0: 6e 74 2c 20 6e 52 63 76 64 3b 20 20 20 20 20 20  nt, nRcvd;      
6de0: 20 2f 2a 20 42 79 74 65 73 20 73 65 6e 74 20 61   /* Bytes sent a
6df0: 6e 64 20 72 65 63 65 69 76 65 64 20 28 61 66 74  nd received (aft
6e00: 65 72 20 63 6f 6d 70 72 65 73 73 69 6f 6e 29 20  er compression) 
6e10: 2a 2f 0a 20 20 42 6c 6f 62 20 73 65 6e 64 3b 20  */.  Blob send; 
6e20: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6e30: 54 65 78 74 20 77 65 20 61 72 65 20 73 65 6e 64  Text we are send
6e40: 69 6e 67 20 74 6f 20 74 68 65 20 73 65 72 76 65  ing to the serve
6e50: 72 20 2a 2f 0a 20 20 42 6c 6f 62 20 72 65 63 76  r */.  Blob recv
6e60: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
6e70: 2a 20 52 65 70 6c 79 20 77 65 20 67 6f 74 20 62  * Reply we got b
6e80: 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 73 65 72  ack from the ser
6e90: 76 65 72 20 2a 2f 0a 20 20 58 66 65 72 20 78 66  ver */.  Xfer xf
6ea0: 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  er;             
6eb0: 20 2f 2a 20 54 72 61 6e 73 66 65 72 20 64 61 74   /* Transfer dat
6ec0: 61 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  a */.  const cha
6ed0: 72 20 2a 7a 53 43 6f 64 65 20 3d 20 64 62 5f 67  r *zSCode = db_g
6ee0: 65 74 28 22 73 65 72 76 65 72 2d 63 6f 64 65 22  et("server-code"
6ef0: 2c 20 22 78 22 29 3b 0a 20 20 63 6f 6e 73 74 20  , "x");.  const 
6f00: 63 68 61 72 20 2a 7a 50 43 6f 64 65 20 3d 20 64  char *zPCode = d
6f10: 62 5f 67 65 74 28 22 70 72 6f 6a 65 63 74 2d 63  b_get("project-c
6f20: 6f 64 65 22 2c 20 30 29 3b 0a 0a 20 20 69 66 28  ode", 0);..  if(
6f30: 20 64 62 5f 67 65 74 5f 62 6f 6f 6c 65 61 6e 28   db_get_boolean(
6f40: 22 64 6f 6e 74 2d 70 75 73 68 22 2c 20 30 29 20  "dont-push", 0) 
6f50: 29 20 70 75 73 68 46 6c 61 67 20 3d 20 30 3b 0a  ) pushFlag = 0;.
6f60: 20 20 69 66 28 20 70 75 73 68 46 6c 61 67 20 2b    if( pushFlag +
6f70: 20 70 75 6c 6c 46 6c 61 67 20 2b 20 63 6c 6f 6e   pullFlag + clon
6f80: 65 46 6c 61 67 20 3d 3d 20 30 20 0a 20 20 20 20  eFlag == 0 .    
6f90: 20 26 26 20 63 6f 6e 66 69 67 52 63 76 4d 61 73   && configRcvMas
6fa0: 6b 3d 3d 30 20 26 26 20 63 6f 6e 66 69 67 53 65  k==0 && configSe
6fb0: 6e 64 4d 61 73 6b 3d 3d 30 20 29 20 72 65 74 75  ndMask==0 ) retu
6fc0: 72 6e 3b 0a 0a 20 20 74 72 61 6e 73 70 6f 72 74  rn;..  transport
6fd0: 5f 73 74 61 74 73 28 30 2c 20 30 2c 20 31 29 3b  _stats(0, 0, 1);
6fe0: 0a 20 20 73 6f 63 6b 65 74 5f 67 6c 6f 62 61 6c  .  socket_global
6ff0: 5f 69 6e 69 74 28 29 3b 0a 20 20 6d 65 6d 73 65  _init();.  memse
7000: 74 28 26 78 66 65 72 2c 20 30 2c 20 73 69 7a 65  t(&xfer, 0, size
7010: 6f 66 28 78 66 65 72 29 29 3b 0a 20 20 78 66 65  of(xfer));.  xfe
7020: 72 2e 70 49 6e 20 3d 20 26 72 65 63 76 3b 0a 20  r.pIn = &recv;. 
7030: 20 78 66 65 72 2e 70 4f 75 74 20 3d 20 26 73 65   xfer.pOut = &se
7040: 6e 64 3b 0a 20 20 78 66 65 72 2e 6d 78 53 65 6e  nd;.  xfer.mxSen
7050: 64 20 3d 20 64 62 5f 67 65 74 5f 69 6e 74 28 22  d = db_get_int("
7060: 6d 61 78 2d 75 70 6c 6f 61 64 22 2c 20 32 35 30  max-upload", 250
7070: 30 30 30 29 3b 0a 0a 20 20 61 73 73 65 72 74 28  000);..  assert(
7080: 20 70 75 73 68 46 6c 61 67 20 7c 20 70 75 6c 6c   pushFlag | pull
7090: 46 6c 61 67 20 7c 20 63 6c 6f 6e 65 46 6c 61 67  Flag | cloneFlag
70a0: 20 7c 20 63 6f 6e 66 69 67 52 63 76 4d 61 73 6b   | configRcvMask
70b0: 20 7c 20 63 6f 6e 66 69 67 53 65 6e 64 4d 61 73   | configSendMas
70c0: 6b 20 29 3b 0a 20 20 64 62 5f 62 65 67 69 6e 5f  k );.  db_begin_
70d0: 74 72 61 6e 73 61 63 74 69 6f 6e 28 29 3b 0a 20  transaction();. 
70e0: 20 64 62 5f 72 65 63 6f 72 64 5f 72 65 70 6f 73   db_record_repos
70f0: 69 74 6f 72 79 5f 66 69 6c 65 6e 61 6d 65 28 30  itory_filename(0
7100: 29 3b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78  );.  db_multi_ex
7110: 65 63 28 0a 20 20 20 20 22 43 52 45 41 54 45 20  ec(.    "CREATE 
7120: 54 45 4d 50 20 54 41 42 4c 45 20 6f 6e 72 65 6d  TEMP TABLE onrem
7130: 6f 74 65 28 72 69 64 20 49 4e 54 45 47 45 52 20  ote(rid INTEGER 
7140: 50 52 49 4d 41 52 59 20 4b 45 59 29 3b 22 0a 20  PRIMARY KEY);". 
7150: 20 29 3b 0a 20 20 62 6c 6f 62 61 72 72 61 79 5f   );.  blobarray_
7160: 7a 65 72 6f 28 78 66 65 72 2e 61 54 6f 6b 65 6e  zero(xfer.aToken
7170: 2c 20 63 6f 75 6e 74 28 78 66 65 72 2e 61 54 6f  , count(xfer.aTo
7180: 6b 65 6e 29 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65  ken));.  blob_ze
7190: 72 6f 28 26 73 65 6e 64 29 3b 0a 20 20 62 6c 6f  ro(&send);.  blo
71a0: 62 5f 7a 65 72 6f 28 26 72 65 63 76 29 3b 0a 20  b_zero(&recv);. 
71b0: 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 78 66 65 72   blob_zero(&xfer
71c0: 2e 65 72 72 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65  .err);.  blob_ze
71d0: 72 6f 28 26 78 66 65 72 2e 6c 69 6e 65 29 3b 0a  ro(&xfer.line);.
71e0: 20 20 6f 72 69 67 43 6f 6e 66 69 67 52 63 76 4d    origConfigRcvM
71f0: 61 73 6b 20 3d 20 63 6f 6e 66 69 67 52 63 76 4d  ask = configRcvM
7200: 61 73 6b 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  ask;..  /*.  ** 
7210: 41 6c 77 61 79 73 20 62 65 67 69 6e 20 77 69 74  Always begin wit
7220: 68 20 61 20 63 6c 6f 6e 65 2c 20 70 75 6c 6c 2c  h a clone, pull,
7230: 20 6f 72 20 70 75 73 68 20 6d 65 73 73 61 67 65   or push message
7240: 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6c 6f 6e  .  */.  if( clon
7250: 65 46 6c 61 67 20 29 7b 0a 20 20 20 20 62 6c 6f  eFlag ){.    blo
7260: 62 5f 61 70 70 65 6e 64 66 28 26 73 65 6e 64 2c  b_appendf(&send,
7270: 20 22 63 6c 6f 6e 65 5c 6e 22 29 3b 0a 20 20 20   "clone\n");.   
7280: 20 70 75 73 68 46 6c 61 67 20 3d 20 30 3b 0a 20   pushFlag = 0;. 
7290: 20 20 20 70 75 6c 6c 46 6c 61 67 20 3d 20 30 3b     pullFlag = 0;
72a0: 0a 20 20 20 20 6e 43 61 72 64 53 65 6e 74 2b 2b  .    nCardSent++
72b0: 3b 0a 20 20 20 20 2f 2a 20 54 42 44 3a 20 52 65  ;.    /* TBD: Re
72c0: 71 75 65 73 74 20 61 6c 6c 20 74 72 61 6e 73 66  quest all transf
72d0: 65 72 61 62 6c 65 20 63 6f 6e 66 69 67 75 72 61  erable configura
72e0: 74 69 6f 6e 20 76 61 6c 75 65 73 20 2a 2f 0a 20  tion values */. 
72f0: 20 7d 65 6c 73 65 20 69 66 28 20 70 75 6c 6c 46   }else if( pullF
7300: 6c 61 67 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f  lag ){.    blob_
7310: 61 70 70 65 6e 64 66 28 26 73 65 6e 64 2c 20 22  appendf(&send, "
7320: 70 75 6c 6c 20 25 73 20 25 73 5c 6e 22 2c 20 7a  pull %s %s\n", z
7330: 53 43 6f 64 65 2c 20 7a 50 43 6f 64 65 29 3b 0a  SCode, zPCode);.
7340: 20 20 20 20 6e 43 61 72 64 53 65 6e 74 2b 2b 3b      nCardSent++;
7350: 0a 20 20 7d 0a 20 20 69 66 28 20 70 75 73 68 46  .  }.  if( pushF
7360: 6c 61 67 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f  lag ){.    blob_
7370: 61 70 70 65 6e 64 66 28 26 73 65 6e 64 2c 20 22  appendf(&send, "
7380: 70 75 73 68 20 25 73 20 25 73 5c 6e 22 2c 20 7a  push %s %s\n", z
7390: 53 43 6f 64 65 2c 20 7a 50 43 6f 64 65 29 3b 0a  SCode, zPCode);.
73a0: 20 20 20 20 6e 43 61 72 64 53 65 6e 74 2b 2b 3b      nCardSent++;
73b0: 0a 20 20 7d 0a 20 20 6d 61 6e 69 66 65 73 74 5f  .  }.  manifest_
73c0: 63 72 6f 73 73 6c 69 6e 6b 5f 62 65 67 69 6e 28  crosslink_begin(
73d0: 29 3b 0a 20 20 70 72 69 6e 74 66 28 7a 4c 61 62  );.  printf(zLab
73e0: 65 6c 46 6f 72 6d 61 74 2c 20 22 22 2c 20 22 42  elFormat, "", "B
73f0: 79 74 65 73 22 2c 20 22 43 61 72 64 73 22 2c 20  ytes", "Cards", 
7400: 22 41 72 74 69 66 61 63 74 73 22 2c 20 22 44 65  "Artifacts", "De
7410: 6c 74 61 73 22 29 3b 0a 0a 20 20 77 68 69 6c 65  ltas");..  while
7420: 28 20 67 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20  ( go ){.    int 
7430: 6e 65 77 50 68 61 6e 74 6f 6d 20 3d 20 30 3b 0a  newPhantom = 0;.
7440: 20 20 20 20 63 68 61 72 20 2a 7a 52 61 6e 64 6f      char *zRando
7450: 6d 6e 65 73 73 3b 0a 0a 20 20 20 20 2f 2a 20 53  mness;..    /* S
7460: 65 6e 64 20 6d 61 6b 65 20 74 68 65 20 6d 6f 73  end make the mos
7470: 74 20 72 65 63 65 6e 74 6c 79 20 72 65 63 65 69  t recently recei
7480: 76 65 64 20 63 6f 6f 6b 69 65 2e 20 20 4c 65 74  ved cookie.  Let
7490: 20 74 68 65 20 73 65 72 76 65 72 0a 20 20 20 20   the server.    
74a0: 2a 2a 20 66 69 67 75 72 65 20 6f 75 74 20 69 66  ** figure out if
74b0: 20 74 68 69 73 20 69 73 20 61 20 63 6f 6f 6b 69   this is a cooki
74c0: 65 20 74 68 61 74 20 69 74 20 63 61 72 65 73 20  e that it cares 
74d0: 61 62 6f 75 74 2e 0a 20 20 20 20 2a 2f 0a 20 20  about..    */.  
74e0: 20 20 7a 43 6f 6f 6b 69 65 20 3d 20 64 62 5f 67    zCookie = db_g
74f0: 65 74 28 22 63 6f 6f 6b 69 65 22 2c 20 30 29 3b  et("cookie", 0);
7500: 0a 20 20 20 20 69 66 28 20 7a 43 6f 6f 6b 69 65  .    if( zCookie
7510: 20 29 7b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 61   ){.      blob_a
7520: 70 70 65 6e 64 66 28 26 73 65 6e 64 2c 20 22 63  ppendf(&send, "c
7530: 6f 6f 6b 69 65 20 25 73 5c 6e 22 2c 20 7a 43 6f  ookie %s\n", zCo
7540: 6f 6b 69 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20  okie);.    }.   
7550: 20 0a 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74   .    /* Generat
7560: 65 20 67 69 6d 6d 65 20 63 61 72 64 73 20 66 6f  e gimme cards fo
7570: 72 20 70 68 61 6e 74 6f 6d 73 20 61 6e 64 20 6c  r phantoms and l
7580: 65 61 66 20 63 61 72 64 73 0a 20 20 20 20 2a 2a  eaf cards.    **
7590: 20 66 6f 72 20 61 6c 6c 20 6c 65 61 76 65 73 2e   for all leaves.
75a0: 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
75b0: 70 75 6c 6c 46 6c 61 67 20 7c 7c 20 63 6c 6f 6e  pullFlag || clon
75c0: 65 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 72  eFlag ){.      r
75d0: 65 71 75 65 73 74 5f 70 68 61 6e 74 6f 6d 73 28  equest_phantoms(
75e0: 26 78 66 65 72 2c 20 6d 78 50 68 61 6e 74 6f 6d  &xfer, mxPhantom
75f0: 52 65 71 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Req);.    }.    
7600: 69 66 28 20 70 75 73 68 46 6c 61 67 20 29 7b 0a  if( pushFlag ){.
7610: 20 20 20 20 20 20 73 65 6e 64 5f 75 6e 73 65 6e        send_unsen
7620: 74 28 26 78 66 65 72 29 3b 0a 20 20 20 20 20 20  t(&xfer);.      
7630: 6e 43 61 72 64 53 65 6e 74 20 2b 3d 20 73 65 6e  nCardSent += sen
7640: 64 5f 75 6e 63 6c 75 73 74 65 72 65 64 28 26 78  d_unclustered(&x
7650: 66 65 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  fer);.    }..   
7660: 20 2f 2a 20 53 65 6e 64 20 63 6f 6e 66 69 67 75   /* Send configu
7670: 72 61 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72  ration parameter
7680: 20 72 65 71 75 65 73 74 73 20 2a 2f 0a 20 20 20   requests */.   
7690: 20 69 66 28 20 63 6f 6e 66 69 67 52 63 76 4d 61   if( configRcvMa
76a0: 73 6b 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73  sk ){.      cons
76b0: 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20  t char *zName;. 
76c0: 20 20 20 20 20 7a 4e 61 6d 65 20 3d 20 63 6f 6e       zName = con
76d0: 66 69 67 75 72 65 5f 66 69 72 73 74 5f 6e 61 6d  figure_first_nam
76e0: 65 28 63 6f 6e 66 69 67 52 63 76 4d 61 73 6b 29  e(configRcvMask)
76f0: 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 7a  ;.      while( z
7700: 4e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 20 20  Name ){.        
7710: 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 73 65  blob_appendf(&se
7720: 6e 64 2c 20 22 72 65 71 63 6f 6e 66 69 67 20 25  nd, "reqconfig %
7730: 73 5c 6e 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20  s\n", zName);.  
7740: 20 20 20 20 20 20 7a 4e 61 6d 65 20 3d 20 63 6f        zName = co
7750: 6e 66 69 67 75 72 65 5f 6e 65 78 74 5f 6e 61 6d  nfigure_next_nam
7760: 65 28 63 6f 6e 66 69 67 52 63 76 4d 61 73 6b 29  e(configRcvMask)
7770: 3b 0a 20 20 20 20 20 20 20 20 6e 43 61 72 64 53  ;.        nCardS
7780: 65 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20  ent++;.      }. 
7790: 20 20 20 20 20 69 66 28 20 63 6f 6e 66 69 67 52       if( configR
77a0: 63 76 4d 61 73 6b 20 26 20 28 43 4f 4e 46 49 47  cvMask & (CONFIG
77b0: 53 45 54 5f 55 53 45 52 7c 43 4f 4e 46 49 47 53  SET_USER|CONFIGS
77c0: 45 54 5f 54 4b 54 29 20 29 7b 0a 20 20 20 20 20  ET_TKT) ){.     
77d0: 20 20 20 63 6f 6e 66 69 67 75 72 65 5f 70 72 65     configure_pre
77e0: 70 61 72 65 5f 74 6f 5f 72 65 63 65 69 76 65 28  pare_to_receive(
77f0: 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  0);.      }.    
7800: 20 20 63 6f 6e 66 69 67 52 63 76 4d 61 73 6b 20    configRcvMask 
7810: 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  = 0;.    }..    
7820: 2f 2a 20 53 65 6e 64 20 63 6f 6e 66 69 67 75 72  /* Send configur
7830: 61 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 73  ation parameters
7840: 20 62 65 69 6e 67 20 70 75 73 68 65 64 20 2a 2f   being pushed */
7850: 0a 20 20 20 20 69 66 28 20 63 6f 6e 66 69 67 53  .    if( configS
7860: 65 6e 64 4d 61 73 6b 20 29 7b 0a 20 20 20 20 20  endMask ){.     
7870: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
7880: 6d 65 3b 0a 20 20 20 20 20 20 7a 4e 61 6d 65 20  me;.      zName 
7890: 3d 20 63 6f 6e 66 69 67 75 72 65 5f 66 69 72 73  = configure_firs
78a0: 74 5f 6e 61 6d 65 28 63 6f 6e 66 69 67 53 65 6e  t_name(configSen
78b0: 64 4d 61 73 6b 29 3b 0a 20 20 20 20 20 20 77 68  dMask);.      wh
78c0: 69 6c 65 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20  ile( zName ){.  
78d0: 20 20 20 20 20 20 73 65 6e 64 5f 63 6f 6e 66 69        send_confi
78e0: 67 5f 63 61 72 64 28 26 78 66 65 72 2c 20 7a 4e  g_card(&xfer, zN
78f0: 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 7a 4e  ame);.        zN
7900: 61 6d 65 20 3d 20 63 6f 6e 66 69 67 75 72 65 5f  ame = configure_
7910: 6e 65 78 74 5f 6e 61 6d 65 28 63 6f 6e 66 69 67  next_name(config
7920: 53 65 6e 64 4d 61 73 6b 29 3b 0a 20 20 20 20 20  SendMask);.     
7930: 20 20 20 6e 43 61 72 64 53 65 6e 74 2b 2b 3b 0a     nCardSent++;.
7940: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 6f        }.      co
7950: 6e 66 69 67 53 65 6e 64 4d 61 73 6b 20 3d 20 30  nfigSendMask = 0
7960: 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
7970: 41 70 70 65 6e 64 20 72 61 6e 64 6f 6d 6e 65 73  Append randomnes
7980: 73 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  s to the end of 
7990: 74 68 65 20 6d 65 73 73 61 67 65 20 2a 2f 0a 20  the message */. 
79a0: 20 20 20 7a 52 61 6e 64 6f 6d 6e 65 73 73 20 3d     zRandomness =
79b0: 20 64 62 5f 74 65 78 74 28 30 2c 20 22 53 45 4c   db_text(0, "SEL
79c0: 45 43 54 20 68 65 78 28 72 61 6e 64 6f 6d 62 6c  ECT hex(randombl
79d0: 6f 62 28 32 30 29 29 22 29 3b 0a 20 20 20 20 62  ob(20))");.    b
79e0: 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 73 65 6e  lob_appendf(&sen
79f0: 64 2c 20 22 23 20 25 73 5c 6e 22 2c 20 7a 52 61  d, "# %s\n", zRa
7a00: 6e 64 6f 6d 6e 65 73 73 29 3b 0a 20 20 20 20 66  ndomness);.    f
7a10: 72 65 65 28 7a 52 61 6e 64 6f 6d 6e 65 73 73 29  ree(zRandomness)
7a20: 3b 0a 0a 20 20 20 20 2f 2a 20 45 78 63 68 61 6e  ;..    /* Exchan
7a30: 67 65 20 6d 65 73 73 61 67 65 73 20 77 69 74 68  ge messages with
7a40: 20 74 68 65 20 73 65 72 76 65 72 20 2a 2f 0a 20   the server */. 
7a50: 20 20 20 6e 46 69 6c 65 53 65 6e 64 20 3d 20 78     nFileSend = x
7a60: 66 65 72 2e 6e 46 69 6c 65 53 65 6e 74 20 2b 20  fer.nFileSent + 
7a70: 78 66 65 72 2e 6e 44 65 6c 74 61 53 65 6e 74 3b  xfer.nDeltaSent;
7a80: 0a 20 20 20 20 70 72 69 6e 74 66 28 7a 56 61 6c  .    printf(zVal
7a90: 75 65 46 6f 72 6d 61 74 2c 20 22 53 65 6e 64 3a  ueFormat, "Send:
7aa0: 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 62  ",.            b
7ab0: 6c 6f 62 5f 73 69 7a 65 28 26 73 65 6e 64 29 2c  lob_size(&send),
7ac0: 20 6e 43 61 72 64 53 65 6e 74 2b 78 66 65 72 2e   nCardSent+xfer.
7ad0: 6e 47 69 6d 6d 65 53 65 6e 74 2b 78 66 65 72 2e  nGimmeSent+xfer.
7ae0: 6e 49 47 6f 74 53 65 6e 74 2c 0a 20 20 20 20 20  nIGotSent,.     
7af0: 20 20 20 20 20 20 20 78 66 65 72 2e 6e 46 69 6c         xfer.nFil
7b00: 65 53 65 6e 74 2c 20 78 66 65 72 2e 6e 44 65 6c  eSent, xfer.nDel
7b10: 74 61 53 65 6e 74 29 3b 0a 20 20 20 20 6e 43 61  taSent);.    nCa
7b20: 72 64 53 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20  rdSent = 0;.    
7b30: 6e 43 61 72 64 52 63 76 64 20 3d 20 30 3b 0a 20  nCardRcvd = 0;. 
7b40: 20 20 20 78 66 65 72 2e 6e 46 69 6c 65 53 65 6e     xfer.nFileSen
7b50: 74 20 3d 20 30 3b 0a 20 20 20 20 78 66 65 72 2e  t = 0;.    xfer.
7b60: 6e 44 65 6c 74 61 53 65 6e 74 20 3d 20 30 3b 0a  nDeltaSent = 0;.
7b70: 20 20 20 20 78 66 65 72 2e 6e 47 69 6d 6d 65 53      xfer.nGimmeS
7b80: 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20 66 66 6c  ent = 0;.    ffl
7b90: 75 73 68 28 73 74 64 6f 75 74 29 3b 0a 20 20 20  ush(stdout);.   
7ba0: 20 68 74 74 70 5f 65 78 63 68 61 6e 67 65 28 26   http_exchange(&
7bb0: 73 65 6e 64 2c 20 26 72 65 63 76 29 3b 0a 20 20  send, &recv);.  
7bc0: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 73 65    blob_reset(&se
7bd0: 6e 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 42 65 67  nd);..    /* Beg
7be0: 69 6e 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 20  in constructing 
7bf0: 74 68 65 20 6e 65 78 74 20 6d 65 73 73 61 67 65  the next message
7c00: 20 28 77 68 69 63 68 20 6d 69 67 68 74 20 6e 65   (which might ne
7c10: 76 65 72 20 62 65 0a 20 20 20 20 2a 2a 20 73 65  ver be.    ** se
7c20: 6e 74 29 20 62 79 20 62 65 67 69 6e 6e 69 6e 67  nt) by beginning
7c30: 20 77 69 74 68 20 74 68 65 20 70 75 6c 6c 20 6f   with the pull o
7c40: 72 20 70 75 73 68 20 63 61 72 64 73 0a 20 20 20  r push cards.   
7c50: 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 75 6c 6c   */.    if( pull
7c60: 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 62 6c  Flag ){.      bl
7c70: 6f 62 5f 61 70 70 65 6e 64 66 28 26 73 65 6e 64  ob_appendf(&send
7c80: 2c 20 22 70 75 6c 6c 20 25 73 20 25 73 5c 6e 22  , "pull %s %s\n"
7c90: 2c 20 7a 53 43 6f 64 65 2c 20 7a 50 43 6f 64 65  , zSCode, zPCode
7ca0: 29 3b 0a 20 20 20 20 20 20 6e 43 61 72 64 53 65  );.      nCardSe
7cb0: 6e 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  nt++;.    }.    
7cc0: 69 66 28 20 70 75 73 68 46 6c 61 67 20 29 7b 0a  if( pushFlag ){.
7cd0: 20 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e        blob_appen
7ce0: 64 66 28 26 73 65 6e 64 2c 20 22 70 75 73 68 20  df(&send, "push 
7cf0: 25 73 20 25 73 5c 6e 22 2c 20 7a 53 43 6f 64 65  %s %s\n", zSCode
7d00: 2c 20 7a 50 43 6f 64 65 29 3b 0a 20 20 20 20 20  , zPCode);.     
7d10: 20 6e 43 61 72 64 53 65 6e 74 2b 2b 3b 0a 20 20   nCardSent++;.  
7d20: 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 50 72 6f 63    }..    /* Proc
7d30: 65 73 73 20 74 68 65 20 72 65 70 6c 79 20 74 68  ess the reply th
7d40: 61 74 20 63 61 6d 65 20 62 61 63 6b 20 66 72 6f  at came back fro
7d50: 6d 20 74 68 65 20 73 65 72 76 65 72 20 2a 2f 0a  m the server */.
7d60: 20 20 20 20 77 68 69 6c 65 28 20 62 6c 6f 62 5f      while( blob_
7d70: 6c 69 6e 65 28 26 72 65 63 76 2c 20 26 78 66 65  line(&recv, &xfe
7d80: 72 2e 6c 69 6e 65 29 20 29 7b 0a 20 20 20 20 20  r.line) ){.     
7d90: 20 69 66 28 20 62 6c 6f 62 5f 62 75 66 66 65 72   if( blob_buffer
7da0: 28 26 78 66 65 72 2e 6c 69 6e 65 29 5b 30 5d 3d  (&xfer.line)[0]=
7db0: 3d 27 23 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='#' ){.        
7dc0: 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
7dd0: 7d 0a 20 20 20 20 20 20 78 66 65 72 2e 6e 54 6f  }.      xfer.nTo
7de0: 6b 65 6e 20 3d 20 62 6c 6f 62 5f 74 6f 6b 65 6e  ken = blob_token
7df0: 69 7a 65 28 26 78 66 65 72 2e 6c 69 6e 65 2c 20  ize(&xfer.line, 
7e00: 78 66 65 72 2e 61 54 6f 6b 65 6e 2c 20 63 6f 75  xfer.aToken, cou
7e10: 6e 74 28 78 66 65 72 2e 61 54 6f 6b 65 6e 29 29  nt(xfer.aToken))
7e20: 3b 0a 20 20 20 20 20 20 6e 43 61 72 64 52 63 76  ;.      nCardRcv
7e30: 64 2b 2b 3b 0a 20 20 20 20 20 20 69 66 20 28 21  d++;.      if (!
7e40: 67 2e 66 51 75 69 65 74 29 20 7b 0a 20 20 20 20  g.fQuiet) {.    
7e50: 20 20 20 20 70 72 69 6e 74 66 28 22 5c 72 25 64      printf("\r%d
7e60: 22 2c 20 6e 43 61 72 64 52 63 76 64 29 3b 0a 20  ", nCardRcvd);. 
7e70: 20 20 20 20 20 20 20 66 66 6c 75 73 68 28 73 74         fflush(st
7e80: 64 6f 75 74 29 3b 0a 20 20 20 20 20 20 7d 0a 0a  dout);.      }..
7e90: 20 20 20 20 20 20 2f 2a 20 20 20 66 69 6c 65 20        /*   file 
7ea0: 55 55 49 44 20 53 49 5a 45 20 5c 6e 20 43 4f 4e  UUID SIZE \n CON
7eb0: 54 45 4e 54 0a 20 20 20 20 20 20 2a 2a 20 20 20  TENT.      **   
7ec0: 66 69 6c 65 20 55 55 49 44 20 44 45 4c 54 41 53  file UUID DELTAS
7ed0: 52 43 20 53 49 5a 45 20 5c 6e 20 43 4f 4e 54 45  RC SIZE \n CONTE
7ee0: 4e 54 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  NT.      **.    
7ef0: 20 20 2a 2a 20 52 65 63 65 69 76 65 20 61 20 66    ** Receive a f
7f00: 69 6c 65 20 74 72 61 6e 73 6d 69 74 74 65 64 20  ile transmitted 
7f10: 66 72 6f 6d 20 74 68 65 20 73 65 72 76 65 72 2e  from the server.
7f20: 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
7f30: 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 78 66 65  if( blob_eq(&xfe
7f40: 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 22 66 69 6c  r.aToken[0],"fil
7f50: 65 22 29 20 29 7b 0a 20 20 20 20 20 20 20 20 78  e") ){.        x
7f60: 66 65 72 5f 61 63 63 65 70 74 5f 66 69 6c 65 28  fer_accept_file(
7f70: 26 78 66 65 72 29 3b 0a 20 20 20 20 20 20 7d 65  &xfer);.      }e
7f80: 6c 73 65 0a 0a 20 20 20 20 20 20 2f 2a 20 20 20  lse..      /*   
7f90: 67 69 6d 6d 65 20 55 55 49 44 0a 20 20 20 20 20  gimme UUID.     
7fa0: 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 53 65 72   **.      ** Ser
7fb0: 76 65 72 20 69 73 20 72 65 71 75 65 73 74 69 6e  ver is requestin
7fc0: 67 20 61 20 66 69 6c 65 2e 20 20 49 66 20 74 68  g a file.  If th
7fd0: 65 20 66 69 6c 65 20 69 73 20 61 20 6d 61 6e 69  e file is a mani
7fe0: 66 65 73 74 2c 20 61 73 73 75 6d 65 0a 20 20 20  fest, assume.   
7ff0: 20 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 73     ** that the s
8000: 65 72 76 65 72 20 77 69 6c 6c 20 61 6c 73 6f 20  erver will also 
8010: 77 61 6e 74 20 74 6f 20 6b 6e 6f 77 20 61 6c 6c  want to know all
8020: 20 6f 66 20 74 68 65 20 63 6f 6e 74 65 6e 74 20   of the content 
8030: 66 69 6c 65 73 0a 20 20 20 20 20 20 2a 2a 20 61  files.      ** a
8040: 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
8050: 68 65 20 6d 61 6e 69 66 65 73 74 20 61 6e 64 20  he manifest and 
8060: 73 65 6e 64 20 74 68 6f 73 65 20 74 6f 6f 2e 0a  send those too..
8070: 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
8080: 66 28 20 62 6c 6f 62 5f 65 71 28 26 78 66 65 72  f( blob_eq(&xfer
8090: 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 67 69 6d  .aToken[0], "gim
80a0: 6d 65 22 29 0a 20 20 20 20 20 20 20 26 26 20 78  me").       && x
80b0: 66 65 72 2e 6e 54 6f 6b 65 6e 3d 3d 32 0a 20 20  fer.nToken==2.  
80c0: 20 20 20 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f       && blob_is_
80d0: 75 75 69 64 28 26 78 66 65 72 2e 61 54 6f 6b 65  uuid(&xfer.aToke
80e0: 6e 5b 31 5d 29 0a 20 20 20 20 20 20 29 7b 0a 20  n[1]).      ){. 
80f0: 20 20 20 20 20 20 20 69 66 28 20 70 75 73 68 46         if( pushF
8100: 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20 20  lag ){.         
8110: 20 69 6e 74 20 72 69 64 20 3d 20 72 69 64 5f 66   int rid = rid_f
8120: 72 6f 6d 5f 75 75 69 64 28 26 78 66 65 72 2e 61  rom_uuid(&xfer.a
8130: 54 6f 6b 65 6e 5b 31 5d 2c 20 30 29 3b 0a 20 20  Token[1], 0);.  
8140: 20 20 20 20 20 20 20 20 69 66 28 20 72 69 64 20          if( rid 
8150: 29 20 73 65 6e 64 5f 66 69 6c 65 28 26 78 66 65  ) send_file(&xfe
8160: 72 2c 20 72 69 64 2c 20 26 78 66 65 72 2e 61 54  r, rid, &xfer.aT
8170: 6f 6b 65 6e 5b 31 5d 2c 20 30 29 3b 0a 20 20 20  oken[1], 0);.   
8180: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
8190: 73 65 0a 20 20 0a 20 20 20 20 20 20 2f 2a 20 20  se.  .      /*  
81a0: 20 69 67 6f 74 20 55 55 49 44 0a 20 20 20 20 20   igot UUID.     
81b0: 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 53 65 72   **.      ** Ser
81c0: 76 65 72 20 61 6e 6e 6f 75 6e 63 65 73 20 74 68  ver announces th
81d0: 61 74 20 69 74 20 68 61 73 20 61 20 70 61 72 74  at it has a part
81e0: 69 63 75 6c 61 72 20 66 69 6c 65 2e 20 20 49 66  icular file.  If
81f0: 20 74 68 69 73 20 69 73 0a 20 20 20 20 20 20 2a   this is.      *
8200: 2a 20 6e 6f 74 20 61 20 66 69 6c 65 20 74 68 61  * not a file tha
8210: 74 20 77 65 20 68 61 76 65 20 61 6e 64 20 77 65  t we have and we
8220: 20 61 72 65 20 70 75 6c 6c 69 6e 67 2c 20 74 68   are pulling, th
8230: 65 6e 20 63 72 65 61 74 65 20 61 0a 20 20 20 20  en create a.    
8240: 20 20 2a 2a 20 70 68 61 6e 74 6f 6d 20 74 6f 20    ** phantom to 
8250: 63 61 75 73 65 20 74 68 69 73 20 66 69 6c 65 20  cause this file 
8260: 74 6f 20 62 65 20 72 65 71 75 65 73 74 65 64 20  to be requested 
8270: 6f 6e 20 74 68 65 20 6e 65 78 74 20 63 79 63 6c  on the next cycl
8280: 65 2e 0a 20 20 20 20 20 20 2a 2a 20 41 6c 77 61  e..      ** Alwa
8290: 79 73 20 72 65 6d 65 6d 62 65 72 20 74 68 61 74  ys remember that
82a0: 20 74 68 65 20 73 65 72 76 65 72 20 68 61 73 20   the server has 
82b0: 74 68 69 73 20 66 69 6c 65 20 73 6f 20 74 68 61  this file so tha
82c0: 74 20 77 65 20 64 6f 0a 20 20 20 20 20 20 2a 2a  t we do.      **
82d0: 20 6e 6f 74 20 74 72 61 6e 73 6d 69 74 20 69 74   not transmit it
82e0: 20 62 79 20 61 63 63 69 64 65 6e 74 2e 0a 20 20   by accident..  
82f0: 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
8300: 20 78 66 65 72 2e 6e 54 6f 6b 65 6e 3d 3d 32 0a   xfer.nToken==2.
8310: 20 20 20 20 20 20 20 26 26 20 62 6c 6f 62 5f 65         && blob_e
8320: 71 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 30  q(&xfer.aToken[0
8330: 5d 2c 20 22 69 67 6f 74 22 29 0a 20 20 20 20 20  ], "igot").     
8340: 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69    && blob_is_uui
8350: 64 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31  d(&xfer.aToken[1
8360: 5d 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20  ]).      ){.    
8370: 20 20 20 20 69 6e 74 20 72 69 64 20 3d 20 72 69      int rid = ri
8380: 64 5f 66 72 6f 6d 5f 75 75 69 64 28 26 78 66 65  d_from_uuid(&xfe
8390: 72 2e 61 54 6f 6b 65 6e 5b 31 5d 2c 20 30 29 3b  r.aToken[1], 0);
83a0: 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 69 64  .        if( rid
83b0: 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  >0 ){.          
83c0: 63 6f 6e 74 65 6e 74 5f 6d 61 6b 65 5f 70 75 62  content_make_pub
83d0: 6c 69 63 28 72 69 64 29 3b 0a 20 20 20 20 20 20  lic(rid);.      
83e0: 20 20 7d 65 6c 73 65 20 69 66 28 20 70 75 6c 6c    }else if( pull
83f0: 46 6c 61 67 20 7c 7c 20 63 6c 6f 6e 65 46 6c 61  Flag || cloneFla
8400: 67 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  g ){.          r
8410: 69 64 20 3d 20 63 6f 6e 74 65 6e 74 5f 6e 65 77  id = content_new
8420: 28 62 6c 6f 62 5f 73 74 72 28 26 78 66 65 72 2e  (blob_str(&xfer.
8430: 61 54 6f 6b 65 6e 5b 31 5d 29 29 3b 0a 20 20 20  aToken[1]));.   
8440: 20 20 20 20 20 20 20 69 66 28 20 72 69 64 20 29         if( rid )
8450: 20 6e 65 77 50 68 61 6e 74 6f 6d 20 3d 20 31 3b   newPhantom = 1;
8460: 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
8470: 20 20 20 72 65 6d 6f 74 65 5f 68 61 73 28 72 69     remote_has(ri
8480: 64 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a  d);.      }else.
8490: 20 20 20 20 0a 20 20 20 20 20 20 0a 20 20 20 20      .      .    
84a0: 20 20 2f 2a 20 20 20 70 75 73 68 20 20 53 45 52    /*   push  SER
84b0: 56 45 52 43 4f 44 45 20 20 50 52 4f 44 55 43 54  VERCODE  PRODUCT
84c0: 43 4f 44 45 0a 20 20 20 20 20 20 2a 2a 0a 20 20  CODE.      **.  
84d0: 20 20 20 20 2a 2a 20 53 68 6f 75 6c 64 20 6f 6e      ** Should on
84e0: 6c 79 20 68 61 70 70 65 6e 20 69 6e 20 72 65 73  ly happen in res
84f0: 70 6f 6e 73 65 20 74 6f 20 61 20 63 6c 6f 6e 65  ponse to a clone
8500: 2e 20 20 54 68 69 73 20 6d 65 73 73 61 67 65 20  .  This message 
8510: 74 65 6c 6c 73 0a 20 20 20 20 20 20 2a 2a 20 74  tells.      ** t
8520: 68 65 20 63 6c 69 65 6e 74 20 77 68 61 74 20 70  he client what p
8530: 72 6f 64 75 63 74 20 74 6f 20 75 73 65 20 66 6f  roduct to use fo
8540: 72 20 74 68 65 20 6e 65 77 20 64 61 74 61 62 61  r the new databa
8550: 73 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  se..      */.   
8560: 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26     if( blob_eq(&
8570: 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 22  xfer.aToken[0],"
8580: 70 75 73 68 22 29 0a 20 20 20 20 20 20 20 26 26  push").       &&
8590: 20 78 66 65 72 2e 6e 54 6f 6b 65 6e 3d 3d 33 0a   xfer.nToken==3.
85a0: 20 20 20 20 20 20 20 26 26 20 63 6c 6f 6e 65 46         && cloneF
85b0: 6c 61 67 0a 20 20 20 20 20 20 20 26 26 20 62 6c  lag.       && bl
85c0: 6f 62 5f 69 73 5f 75 75 69 64 28 26 78 66 65 72  ob_is_uuid(&xfer
85d0: 2e 61 54 6f 6b 65 6e 5b 31 5d 29 0a 20 20 20 20  .aToken[1]).    
85e0: 20 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75 75     && blob_is_uu
85f0: 69 64 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b  id(&xfer.aToken[
8600: 32 5d 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20  2]).      ){.   
8610: 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71       if( blob_eq
8620: 5f 73 74 72 28 26 78 66 65 72 2e 61 54 6f 6b 65  _str(&xfer.aToke
8630: 6e 5b 31 5d 2c 20 7a 53 43 6f 64 65 2c 20 2d 31  n[1], zSCode, -1
8640: 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66  ) ){.          f
8650: 6f 73 73 69 6c 5f 66 61 74 61 6c 28 22 73 65 72  ossil_fatal("ser
8660: 76 65 72 20 6c 6f 6f 70 22 29 3b 0a 20 20 20 20  ver loop");.    
8670: 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
8680: 28 20 7a 50 43 6f 64 65 3d 3d 30 20 29 7b 0a 20  ( zPCode==0 ){. 
8690: 20 20 20 20 20 20 20 20 20 7a 50 43 6f 64 65 20           zPCode 
86a0: 3d 20 6d 70 72 69 6e 74 66 28 22 25 62 22 2c 20  = mprintf("%b", 
86b0: 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 32 5d 29  &xfer.aToken[2])
86c0: 3b 0a 20 20 20 20 20 20 20 20 20 20 64 62 5f 73  ;.          db_s
86d0: 65 74 28 22 70 72 6f 6a 65 63 74 2d 63 6f 64 65  et("project-code
86e0: 22 2c 20 7a 50 43 6f 64 65 2c 20 30 29 3b 0a 20  ", zPCode, 0);. 
86f0: 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
8700: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 73   blob_appendf(&s
8710: 65 6e 64 2c 20 22 63 6c 6f 6e 65 5c 6e 22 29 3b  end, "clone\n");
8720: 0a 20 20 20 20 20 20 20 20 6e 43 61 72 64 53 65  .        nCardSe
8730: 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73  nt++;.      }els
8740: 65 0a 20 20 20 20 20 20 0a 20 20 20 20 20 20 2f  e.      .      /
8750: 2a 20 20 20 63 6f 6e 66 69 67 20 4e 41 4d 45 20  *   config NAME 
8760: 53 49 5a 45 20 5c 6e 20 43 4f 4e 54 45 4e 54 0a  SIZE \n CONTENT.
8770: 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
8780: 2a 20 52 65 63 65 69 76 65 20 61 20 63 6f 6e 66  * Receive a conf
8790: 69 67 75 72 61 74 69 6f 6e 20 76 61 6c 75 65 20  iguration value 
87a0: 66 72 6f 6d 20 74 68 65 20 73 65 72 76 65 72 2e  from the server.
87b0: 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
87c0: 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 78 66 65  if( blob_eq(&xfe
87d0: 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 22 63 6f 6e  r.aToken[0],"con
87e0: 66 69 67 22 29 20 26 26 20 78 66 65 72 2e 6e 54  fig") && xfer.nT
87f0: 6f 6b 65 6e 3d 3d 33 0a 20 20 20 20 20 20 20 20  oken==3.        
8800: 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f 69 6e 74    && blob_is_int
8810: 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 32 5d  (&xfer.aToken[2]
8820: 2c 20 26 73 69 7a 65 29 20 29 7b 0a 20 20 20 20  , &size) ){.    
8830: 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
8840: 7a 4e 61 6d 65 20 3d 20 62 6c 6f 62 5f 73 74 72  zName = blob_str
8850: 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d  (&xfer.aToken[1]
8860: 29 3b 0a 20 20 20 20 20 20 20 20 42 6c 6f 62 20  );.        Blob 
8870: 63 6f 6e 74 65 6e 74 3b 0a 20 20 20 20 20 20 20  content;.       
8880: 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 63 6f 6e 74   blob_zero(&cont
8890: 65 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 62 6c  ent);.        bl
88a0: 6f 62 5f 65 78 74 72 61 63 74 28 78 66 65 72 2e  ob_extract(xfer.
88b0: 70 49 6e 2c 20 73 69 7a 65 2c 20 26 63 6f 6e 74  pIn, size, &cont
88c0: 65 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 67 2e  ent);.        g.
88d0: 6f 6b 41 64 6d 69 6e 20 3d 20 67 2e 6f 6b 52 64  okAdmin = g.okRd
88e0: 41 64 64 72 20 3d 20 31 3b 0a 20 20 20 20 20 20  Addr = 1;.      
88f0: 20 20 69 66 28 20 63 6f 6e 66 69 67 75 72 65 5f    if( configure_
8900: 69 73 5f 65 78 70 6f 72 74 61 62 6c 65 28 7a 4e  is_exportable(zN
8910: 61 6d 65 29 20 26 20 6f 72 69 67 43 6f 6e 66 69  ame) & origConfi
8920: 67 52 63 76 4d 61 73 6b 20 29 7b 0a 20 20 20 20  gRcvMask ){.    
8930: 20 20 20 20 20 20 69 66 28 20 7a 4e 61 6d 65 5b        if( zName[
8940: 30 5d 21 3d 27 40 27 20 29 7b 0a 20 20 20 20 20  0]!='@' ){.     
8950: 20 20 20 20 20 20 20 69 66 28 20 73 74 72 63 6d         if( strcm
8960: 70 28 7a 4e 61 6d 65 2c 20 22 6c 6f 67 6f 2d 69  p(zName, "logo-i
8970: 6d 61 67 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20  mage")==0 ){.   
8980: 20 20 20 20 20 20 20 20 20 20 20 53 74 6d 74 20             Stmt 
8990: 69 6e 73 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ins;.           
89a0: 20 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 69     db_prepare(&i
89b0: 6e 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ns,.            
89c0: 20 20 20 20 22 52 45 50 4c 41 43 45 20 49 4e 54      "REPLACE INT
89d0: 4f 20 63 6f 6e 66 69 67 28 6e 61 6d 65 2c 20 76  O config(name, v
89e0: 61 6c 75 65 29 20 56 41 4c 55 45 53 28 3a 6e 61  alue) VALUES(:na
89f0: 6d 65 2c 20 3a 76 61 6c 75 65 29 22 0a 20 20 20  me, :value)".   
8a00: 20 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20 20             );.  
8a10: 20 20 20 20 20 20 20 20 20 20 20 20 64 62 5f 62              db_b
8a20: 69 6e 64 5f 74 65 78 74 28 26 69 6e 73 2c 20 22  ind_text(&ins, "
8a30: 3a 6e 61 6d 65 22 2c 20 7a 4e 61 6d 65 29 3b 0a  :name", zName);.
8a40: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 62                db
8a50: 5f 62 69 6e 64 5f 62 6c 6f 62 28 26 69 6e 73 2c  _bind_blob(&ins,
8a60: 20 22 3a 76 61 6c 75 65 22 2c 20 26 63 6f 6e 74   ":value", &cont
8a70: 65 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ent);.          
8a80: 20 20 20 20 64 62 5f 73 74 65 70 28 26 69 6e 73      db_step(&ins
8a90: 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
8aa0: 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 69 6e   db_finalize(&in
8ab0: 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  s);.            
8ac0: 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
8ad0: 20 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78       db_multi_ex
8ae0: 65 63 28 0a 20 20 20 20 20 20 20 20 20 20 20 20  ec(.            
8af0: 20 20 20 20 20 20 22 52 45 50 4c 41 43 45 20 49        "REPLACE I
8b00: 4e 54 4f 20 63 6f 6e 66 69 67 28 6e 61 6d 65 2c  NTO config(name,
8b10: 76 61 6c 75 65 29 20 56 41 4c 55 45 53 28 25 51  value) VALUES(%Q
8b20: 2c 25 51 29 22 2c 0a 20 20 20 20 20 20 20 20 20  ,%Q)",.         
8b30: 20 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 2c 20           zName, 
8b40: 62 6c 6f 62 5f 73 74 72 28 26 63 6f 6e 74 65 6e  blob_str(&conten
8b50: 74 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t).             
8b60: 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   );.            
8b70: 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  }.          }els
8b80: 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f  e{.            /
8b90: 2a 20 4e 6f 74 69 63 65 20 74 68 61 74 20 77 65  * Notice that we
8ba0: 20 61 72 65 20 65 76 61 6c 75 61 74 69 6e 67 20   are evaluating 
8bb0: 61 72 62 69 74 72 61 72 79 20 53 51 4c 20 72 65  arbitrary SQL re
8bc0: 63 65 69 76 65 64 20 66 72 6f 6d 20 74 68 65 0a  ceived from the.
8bd0: 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73              ** s
8be0: 65 72 76 65 72 2e 20 20 42 75 74 20 74 68 69 73  erver.  But this
8bf0: 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e   can only happen
8c00: 20 69 66 20 77 65 20 68 61 76 65 20 73 70 65 63   if we have spec
8c10: 69 66 69 63 61 6c 6c 79 0a 20 20 20 20 20 20 20  ifically.       
8c20: 20 20 20 20 20 2a 2a 20 72 65 71 75 65 73 74 65       ** requeste
8c30: 64 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  d configuration 
8c40: 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d  information from
8c50: 20 74 68 65 20 73 65 72 76 65 72 2c 20 73 6f 0a   the server, so.
8c60: 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70              ** p
8c70: 72 65 73 75 6d 61 62 6c 79 20 74 68 65 20 6f 70  resumably the op
8c80: 65 72 61 74 6f 72 20 74 72 75 73 74 73 20 74 68  erator trusts th
8c90: 65 20 73 65 72 76 65 72 2e 0a 20 20 20 20 20 20  e server..      
8ca0: 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
8cb0: 20 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78       db_multi_ex
8cc0: 65 63 28 22 25 73 22 2c 20 62 6c 6f 62 5f 73 74  ec("%s", blob_st
8cd0: 72 28 26 63 6f 6e 74 65 6e 74 29 29 3b 0a 20 20  r(&content));.  
8ce0: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
8cf0: 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 43 61 72    }.        nCar
8d00: 64 53 65 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20  dSent++;.       
8d10: 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 63 6f 6e   blob_reset(&con
8d20: 74 65 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 62  tent);.        b
8d30: 6c 6f 62 5f 73 65 65 6b 28 78 66 65 72 2e 70 49  lob_seek(xfer.pI
8d40: 6e 2c 20 31 2c 20 42 4c 4f 42 5f 53 45 45 4b 5f  n, 1, BLOB_SEEK_
8d50: 43 55 52 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  CUR);.      }els
8d60: 65 0a 0a 20 20 20 20 20 20 0a 20 20 20 20 20 20  e..      .      
8d70: 2f 2a 20 20 20 20 63 6f 6f 6b 69 65 20 54 45 58  /*    cookie TEX
8d80: 54 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  T.      **.     
8d90: 20 2a 2a 20 54 68 65 20 73 65 72 76 65 72 20 6d   ** The server m
8da0: 69 67 68 74 20 69 6e 63 6c 75 64 65 20 61 20 63  ight include a c
8db0: 6f 6f 6b 69 65 20 69 6e 20 69 74 73 20 72 65 70  ookie in its rep
8dc0: 6c 79 2e 20 20 54 68 65 20 63 6c 69 65 6e 74 0a  ly.  The client.
8dd0: 20 20 20 20 20 20 2a 2a 20 73 68 6f 75 6c 64 20        ** should 
8de0: 72 65 6d 65 6d 62 65 72 20 74 68 69 73 20 63 6f  remember this co
8df0: 6f 6b 69 65 20 61 6e 64 20 73 65 6e 64 20 69 74  okie and send it
8e00: 20 62 61 63 6b 20 74 6f 20 74 68 65 20 73 65 72   back to the ser
8e10: 76 65 72 0a 20 20 20 20 20 20 2a 2a 20 69 6e 20  ver.      ** in 
8e20: 69 74 73 20 6e 65 78 74 20 71 75 65 72 79 2e 0a  its next query..
8e30: 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
8e40: 2a 20 45 61 63 68 20 63 6f 6f 6b 69 65 20 72 65  * Each cookie re
8e50: 63 65 69 76 65 64 20 6f 76 65 72 77 72 69 74 65  ceived overwrite
8e60: 73 20 74 68 65 20 70 72 69 6f 72 20 63 6f 6f 6b  s the prior cook
8e70: 69 65 20 66 72 6f 6d 20 74 68 65 0a 20 20 20 20  ie from the.    
8e80: 20 20 2a 2a 20 73 61 6d 65 20 73 65 72 76 65 72    ** same server
8e90: 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
8ea0: 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 78 66   if( blob_eq(&xf
8eb0: 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 63  er.aToken[0], "c
8ec0: 6f 6f 6b 69 65 22 29 20 26 26 20 78 66 65 72 2e  ookie") && xfer.
8ed0: 6e 54 6f 6b 65 6e 3d 3d 32 20 29 7b 0a 20 20 20  nToken==2 ){.   
8ee0: 20 20 20 20 20 64 62 5f 73 65 74 28 22 63 6f 6f       db_set("coo
8ef0: 6b 69 65 22 2c 20 62 6c 6f 62 5f 73 74 72 28 26  kie", blob_str(&
8f00: 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d 29 2c  xfer.aToken[1]),
8f10: 20 30 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65   0);.      }else
8f20: 0a 0a 20 20 20 20 20 20 2f 2a 20 20 20 6d 65 73  ..      /*   mes
8f30: 73 61 67 65 20 4d 45 53 53 41 47 45 0a 20 20 20  sage MESSAGE.   
8f40: 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 50     **.      ** P
8f50: 72 69 6e 74 20 61 20 6d 65 73 73 61 67 65 2e 20  rint a message. 
8f60: 20 53 69 6d 69 6c 61 72 20 74 6f 20 22 65 72 72   Similar to "err
8f70: 6f 72 22 20 62 75 74 20 64 6f 65 73 20 6e 6f 74  or" but does not
8f80: 20 73 74 6f 70 20 70 72 6f 63 65 73 73 69 6e 67   stop processing
8f90: 0a 20 20 20 20 20 20 2a 2f 20 20 20 20 20 20 20  .      */       
8fa0: 20 0a 20 20 20 20 20 20 69 66 28 20 62 6c 6f 62   .      if( blob
8fb0: 5f 65 71 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e  _eq(&xfer.aToken
8fc0: 5b 30 5d 2c 22 6d 65 73 73 61 67 65 22 29 20 26  [0],"message") &
8fd0: 26 20 78 66 65 72 2e 6e 54 6f 6b 65 6e 3d 3d 32  & xfer.nToken==2
8fe0: 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72   ){.        char
8ff0: 20 2a 7a 4d 73 67 20 3d 20 62 6c 6f 62 5f 74 65   *zMsg = blob_te
9000: 72 6d 69 6e 61 74 65 28 26 78 66 65 72 2e 61 54  rminate(&xfer.aT
9010: 6f 6b 65 6e 5b 31 5d 29 3b 0a 20 20 20 20 20 20  oken[1]);.      
9020: 20 20 64 65 66 6f 73 73 69 6c 69 7a 65 28 7a 4d    defossilize(zM
9030: 73 67 29 3b 0a 20 20 20 20 20 20 20 20 70 72 69  sg);.        pri
9040: 6e 74 66 28 22 5c 72 53 65 72 76 65 72 20 73 61  ntf("\rServer sa
9050: 79 73 3a 20 25 73 5c 6e 22 2c 20 7a 4d 73 67 29  ys: %s\n", zMsg)
9060: 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a 0a 20  ;.      }else.. 
9070: 20 20 20 20 20 2f 2a 20 20 20 65 72 72 6f 72 20       /*   error 
9080: 4d 45 53 53 41 47 45 0a 20 20 20 20 20 20 2a 2a  MESSAGE.      **
9090: 0a 20 20 20 20 20 20 2a 2a 20 52 65 70 6f 72 74  .      ** Report
90a0: 20 61 6e 20 65 72 72 6f 72 20 61 6e 64 20 61 62   an error and ab
90b0: 61 6e 64 6f 6e 20 74 68 65 20 73 79 6e 63 20 73  andon the sync s
90c0: 65 73 73 69 6f 6e 0a 20 20 20 20 20 20 2a 2f 20  ession.      */ 
90d0: 20 20 20 20 20 20 20 0a 20 20 20 20 20 20 69 66         .      if
90e0: 28 20 62 6c 6f 62 5f 65 71 28 26 78 66 65 72 2e  ( blob_eq(&xfer.
90f0: 61 54 6f 6b 65 6e 5b 30 5d 2c 22 65 72 72 6f 72  aToken[0],"error
9100: 22 29 20 26 26 20 78 66 65 72 2e 6e 54 6f 6b 65  ") && xfer.nToke
9110: 6e 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20 20 20  n==2 ){.        
9120: 63 68 61 72 20 2a 7a 4d 73 67 20 3d 20 62 6c 6f  char *zMsg = blo
9130: 62 5f 74 65 72 6d 69 6e 61 74 65 28 26 78 66 65  b_terminate(&xfe
9140: 72 2e 61 54 6f 6b 65 6e 5b 31 5d 29 3b 0a 20 20  r.aToken[1]);.  
9150: 20 20 20 20 20 20 64 65 66 6f 73 73 69 6c 69 7a        defossiliz
9160: 65 28 7a 4d 73 67 29 3b 0a 20 20 20 20 20 20 20  e(zMsg);.       
9170: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 78   blob_appendf(&x
9180: 66 65 72 2e 65 72 72 2c 20 22 73 65 72 76 65 72  fer.err, "server
9190: 20 73 61 79 73 3a 20 25 73 22 2c 20 7a 4d 73 67   says: %s", zMsg
91a0: 29 3b 0a 20 20 20 20 20 20 20 20 70 72 69 6e 74  );.        print
91b0: 66 28 22 53 65 72 76 65 72 20 45 72 72 6f 72 3a  f("Server Error:
91c0: 20 25 73 5c 6e 22 2c 20 7a 4d 73 67 29 3b 0a 20   %s\n", zMsg);. 
91d0: 20 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20 20       }else..    
91e0: 20 20 2f 2a 20 55 6e 6b 6e 6f 77 6e 20 6d 65 73    /* Unknown mes
91f0: 73 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 7b 0a  sage */.      {.
9200: 20 20 20 20 20 20 20 20 69 66 28 20 62 6c 6f 62          if( blob
9210: 5f 73 74 72 28 26 78 66 65 72 2e 61 54 6f 6b 65  _str(&xfer.aToke
9220: 6e 5b 30 5d 29 5b 30 5d 3d 3d 27 3c 27 20 29 7b  n[0])[0]=='<' ){
9230: 0a 20 20 20 20 20 20 20 20 20 20 66 6f 73 73 69  .          fossi
9240: 6c 5f 66 61 74 61 6c 28 0a 20 20 20 20 20 20 20  l_fatal(.       
9250: 20 20 20 20 20 22 73 65 72 76 65 72 20 72 65 70       "server rep
9260: 6c 69 65 73 20 77 69 74 68 20 48 54 4d 4c 20 69  lies with HTML i
9270: 6e 73 74 65 61 64 20 6f 66 20 66 6f 73 73 69 6c  nstead of fossil
9280: 20 73 79 6e 63 20 70 72 6f 74 6f 63 6f 6c 3a 5c   sync protocol:\
9290: 6e 25 62 22 2c 0a 20 20 20 20 20 20 20 20 20 20  n%b",.          
92a0: 20 20 26 72 65 63 76 0a 20 20 20 20 20 20 20 20    &recv.        
92b0: 20 20 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20    );.        }. 
92c0: 20 20 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 65         blob_appe
92d0: 6e 64 66 28 26 78 66 65 72 2e 65 72 72 2c 20 22  ndf(&xfer.err, "
92e0: 75 6e 6b 6e 6f 77 6e 20 63 6f 6d 6d 61 6e 64 3a  unknown command:
92f0: 20 25 62 22 2c 20 26 78 66 65 72 2e 61 54 6f 6b   %b", &xfer.aTok
9300: 65 6e 5b 30 5d 29 3b 0a 20 20 20 20 20 20 7d 0a  en[0]);.      }.
9310: 0a 20 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f  .      if( blob_
9320: 73 69 7a 65 28 26 78 66 65 72 2e 65 72 72 29 20  size(&xfer.err) 
9330: 29 7b 0a 20 20 20 20 20 20 20 20 66 6f 73 73 69  ){.        fossi
9340: 6c 5f 66 61 74 61 6c 28 22 25 62 22 2c 20 26 78  l_fatal("%b", &x
9350: 66 65 72 2e 65 72 72 29 3b 0a 20 20 20 20 20 20  fer.err);.      
9360: 7d 0a 20 20 20 20 20 20 62 6c 6f 62 61 72 72 61  }.      blobarra
9370: 79 5f 72 65 73 65 74 28 78 66 65 72 2e 61 54 6f  y_reset(xfer.aTo
9380: 6b 65 6e 2c 20 78 66 65 72 2e 6e 54 6f 6b 65 6e  ken, xfer.nToken
9390: 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 72 65  );.      blob_re
93a0: 73 65 74 28 26 78 66 65 72 2e 6c 69 6e 65 29 3b  set(&xfer.line);
93b0: 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6f  .    }.    if( o
93c0: 72 69 67 43 6f 6e 66 69 67 52 63 76 4d 61 73 6b  rigConfigRcvMask
93d0: 20 26 20 28 43 4f 4e 46 49 47 53 45 54 5f 54 4b   & (CONFIGSET_TK
93e0: 54 7c 43 4f 4e 46 49 47 53 45 54 5f 55 53 45 52  T|CONFIGSET_USER
93f0: 29 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 66 69  ) ){.      confi
9400: 67 75 72 65 5f 66 69 6e 61 6c 69 7a 65 5f 72 65  gure_finalize_re
9410: 63 65 69 76 65 28 29 3b 0a 20 20 20 20 7d 0a 20  ceive();.    }. 
9420: 20 20 20 6f 72 69 67 43 6f 6e 66 69 67 52 63 76     origConfigRcv
9430: 4d 61 73 6b 20 3d 20 30 3b 0a 20 20 20 20 69 66  Mask = 0;.    if
9440: 28 20 6e 43 61 72 64 52 63 76 64 3e 30 20 29 7b  ( nCardRcvd>0 ){
9450: 0a 20 20 20 20 20 20 70 72 69 6e 74 66 28 7a 56  .      printf(zV
9460: 61 6c 75 65 46 6f 72 6d 61 74 2c 20 22 52 65 63  alueFormat, "Rec
9470: 65 69 76 65 64 3a 22 2c 0a 20 20 20 20 20 20 20  eived:",.       
9480: 20 20 20 20 20 20 20 62 6c 6f 62 5f 73 69 7a 65         blob_size
9490: 28 26 72 65 63 76 29 2c 20 6e 43 61 72 64 52 63  (&recv), nCardRc
94a0: 76 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  vd,.            
94b0: 20 20 78 66 65 72 2e 6e 46 69 6c 65 52 63 76 64    xfer.nFileRcvd
94c0: 2c 20 78 66 65 72 2e 6e 44 65 6c 74 61 52 63 76  , xfer.nDeltaRcv
94d0: 64 20 2b 20 78 66 65 72 2e 6e 44 61 6e 67 6c 69  d + xfer.nDangli
94e0: 6e 67 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20  ngFile);.    }. 
94f0: 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 72     blob_reset(&r
9500: 65 63 76 29 3b 0a 20 20 20 20 6e 43 79 63 6c 65  ecv);.    nCycle
9510: 2b 2b 3b 0a 20 20 20 20 67 6f 20 3d 20 30 3b 0a  ++;.    go = 0;.
9520: 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 72 65  .    /* If we re
9530: 63 65 69 76 65 64 20 6f 6e 65 20 6f 72 20 6d 6f  ceived one or mo
9540: 72 65 20 66 69 6c 65 73 20 6f 6e 20 74 68 65 20  re files on the 
9550: 70 72 65 76 69 6f 75 73 20 65 78 63 68 61 6e 67  previous exchang
9560: 65 20 62 75 74 0a 20 20 20 20 2a 2a 20 74 68 65  e but.    ** the
9570: 72 65 20 61 72 65 20 73 74 69 6c 6c 20 70 68 61  re are still pha
9580: 6e 74 6f 6d 73 2c 20 74 68 65 6e 20 67 6f 20 61  ntoms, then go a
9590: 6e 6f 74 68 65 72 20 72 6f 75 6e 64 2e 0a 20 20  nother round..  
95a0: 20 20 2a 2f 0a 20 20 20 20 6e 46 69 6c 65 52 65    */.    nFileRe
95b0: 63 76 20 3d 20 78 66 65 72 2e 6e 46 69 6c 65 52  cv = xfer.nFileR
95c0: 63 76 64 20 2b 20 78 66 65 72 2e 6e 44 65 6c 74  cvd + xfer.nDelt
95d0: 61 52 63 76 64 20 2b 20 78 66 65 72 2e 6e 44 61  aRcvd + xfer.nDa
95e0: 6e 67 6c 69 6e 67 46 69 6c 65 3b 0a 20 20 20 20  nglingFile;.    
95f0: 69 66 28 20 28 6e 46 69 6c 65 52 65 63 76 3e 30  if( (nFileRecv>0
9600: 20 7c 7c 20 6e 65 77 50 68 61 6e 74 6f 6d 29 20   || newPhantom) 
9610: 26 26 20 64 62 5f 65 78 69 73 74 73 28 22 53 45  && db_exists("SE
9620: 4c 45 43 54 20 31 20 46 52 4f 4d 20 70 68 61 6e  LECT 1 FROM phan
9630: 74 6f 6d 22 29 20 29 7b 0a 20 20 20 20 20 20 67  tom") ){.      g
9640: 6f 20 3d 20 31 3b 0a 20 20 20 20 20 20 6d 78 50  o = 1;.      mxP
9650: 68 61 6e 74 6f 6d 52 65 71 20 3d 20 6e 46 69 6c  hantomReq = nFil
9660: 65 52 65 63 76 2a 32 3b 0a 20 20 20 20 20 20 69  eRecv*2;.      i
9670: 66 28 20 6d 78 50 68 61 6e 74 6f 6d 52 65 71 3c  f( mxPhantomReq<
9680: 32 30 30 20 29 20 6d 78 50 68 61 6e 74 6f 6d 52  200 ) mxPhantomR
9690: 65 71 20 3d 20 32 30 30 3b 0a 20 20 20 20 7d 0a  eq = 200;.    }.
96a0: 20 20 20 20 6e 43 61 72 64 52 63 76 64 20 3d 20      nCardRcvd = 
96b0: 30 3b 0a 20 20 20 20 78 66 65 72 2e 6e 46 69 6c  0;.    xfer.nFil
96c0: 65 52 63 76 64 20 3d 20 30 3b 0a 20 20 20 20 78  eRcvd = 0;.    x
96d0: 66 65 72 2e 6e 44 65 6c 74 61 52 63 76 64 20 3d  fer.nDeltaRcvd =
96e0: 20 30 3b 0a 20 20 20 20 78 66 65 72 2e 6e 44 61   0;.    xfer.nDa
96f0: 6e 67 6c 69 6e 67 46 69 6c 65 20 3d 20 30 3b 0a  nglingFile = 0;.
9700: 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 68 61  .    /* If we ha
9710: 76 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 66  ve one or more f
9720: 69 6c 65 73 20 71 75 65 75 65 64 20 74 6f 20 73  iles queued to s
9730: 65 6e 64 2c 20 74 68 65 6e 20 67 6f 0a 20 20 20  end, then go.   
9740: 20 2a 2a 20 61 6e 6f 74 68 65 72 20 72 6f 75 6e   ** another roun
9750: 64 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  d .    */.    if
9760: 28 20 78 66 65 72 2e 6e 46 69 6c 65 53 65 6e 74  ( xfer.nFileSent
9770: 2b 78 66 65 72 2e 6e 44 65 6c 74 61 53 65 6e 74  +xfer.nDeltaSent
9780: 3e 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 20 3d  >0 ){.      go =
9790: 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 3b 0a 20   1;.    }.  };. 
97a0: 20 74 72 61 6e 73 70 6f 72 74 5f 73 74 61 74 73   transport_stats
97b0: 28 26 6e 53 65 6e 74 2c 20 26 6e 52 63 76 64 2c  (&nSent, &nRcvd,
97c0: 20 31 29 3b 0a 20 20 70 72 69 6e 74 66 28 22 54   1);.  printf("T
97d0: 6f 74 61 6c 20 6e 65 74 77 6f 72 6b 20 74 72 61  otal network tra
97e0: 66 66 69 63 3a 20 25 64 20 62 79 74 65 73 20 73  ffic: %d bytes s
97f0: 65 6e 74 2c 20 25 64 20 62 79 74 65 73 20 72 65  ent, %d bytes re
9800: 63 65 69 76 65 64 5c 6e 22 2c 0a 20 20 20 20 20  ceived\n",.     
9810: 20 20 20 20 6e 53 65 6e 74 2c 20 6e 52 63 76 64      nSent, nRcvd
9820: 29 3b 0a 20 20 74 72 61 6e 73 70 6f 72 74 5f 63  );.  transport_c
9830: 6c 6f 73 65 28 29 3b 0a 20 20 73 6f 63 6b 65 74  lose();.  socket
9840: 5f 67 6c 6f 62 61 6c 5f 73 68 75 74 64 6f 77 6e  _global_shutdown
9850: 28 29 3b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65  ();.  db_multi_e
9860: 78 65 63 28 22 44 52 4f 50 20 54 41 42 4c 45 20  xec("DROP TABLE 
9870: 6f 6e 72 65 6d 6f 74 65 22 29 3b 0a 20 20 6d 61  onremote");.  ma
9880: 6e 69 66 65 73 74 5f 63 72 6f 73 73 6c 69 6e 6b  nifest_crosslink
9890: 5f 65 6e 64 28 29 3b 0a 20 20 64 62 5f 65 6e 64  _end();.  db_end
98a0: 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 30 29 3b  _transaction(0);
98b0: 0a 7d 0a                                         .}.