Hex Artifact Content
Not logged in

Artifact c1b226ecc251ca45ebdc41b62466c082e5b1cdc3:

File src/xfer.c part of check-in [64c58fd64f] - Remove the "Dangling" column from the sync status display. by drh on 2008-05-18 17:33:25.

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 23 69 66 20 49 4e 54 45 52 46 41 43 45 0a  ..#if INTERFACE.
03e0: 2f 2a 0a 2a 2a 20 43 6f 6e 66 69 67 75 72 61 74  /*.** Configurat
03f0: 69 6f 6e 20 74 72 61 6e 73 66 65 72 73 20 6f 63  ion transfers oc
0400: 63 75 72 20 69 6e 20 67 72 6f 75 70 73 2e 20 20  cur in groups.  
0410: 54 68 65 73 65 20 61 72 65 20 74 68 65 20 61 6c  These are the al
0420: 6c 6f 77 65 64 0a 2a 2a 20 67 72 6f 75 70 69 6e  lowed.** groupin
0430: 67 73 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43  gs:.*/.#define C
0440: 4f 4e 46 49 47 53 45 54 5f 53 4b 49 4e 20 20 20  ONFIGSET_SKIN   
0450: 30 78 30 30 30 30 30 31 20 20 20 20 20 2f 2a 20  0x000001     /* 
0460: 57 57 57 20 69 6e 74 65 72 66 61 63 65 20 61 70  WWW interface ap
0470: 70 65 61 72 61 6e 63 65 20 2a 2f 0a 23 64 65 66  pearance */.#def
0480: 69 6e 65 20 43 4f 4e 46 49 47 53 45 54 5f 54 4b  ine CONFIGSET_TK
0490: 54 20 20 20 20 30 78 30 30 30 30 30 32 20 20 20  T    0x000002   
04a0: 20 20 2f 2a 20 54 69 63 6b 65 74 20 63 6f 6e 66    /* Ticket conf
04b0: 69 67 75 72 61 74 69 6f 6e 20 2a 2f 0a 23 64 65  iguration */.#de
04c0: 66 69 6e 65 20 43 4f 4e 46 49 47 53 45 54 5f 50  fine CONFIGSET_P
04d0: 52 4f 4a 20 20 20 30 78 30 30 30 30 30 34 20 20  ROJ   0x000004  
04e0: 20 20 20 2f 2a 20 50 72 6f 6a 65 63 74 20 6e 61     /* Project na
04f0: 6d 65 20 2a 2f 0a 0a 23 65 6e 64 69 66 20 2f 2a  me */..#endif /*
0500: 20 49 4e 54 45 52 46 41 43 45 20 2a 2f 0a 0a 2f   INTERFACE */../
0510: 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
0520: 6e 67 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20  ng is a list of 
0530: 73 65 74 74 69 6e 67 73 20 74 68 61 74 20 77 65  settings that we
0540: 20 61 72 65 20 77 69 6c 6c 69 6e 67 20 74 6f 0a   are willing to.
0550: 2a 2a 20 74 72 61 6e 73 66 65 72 2e 0a 2a 2f 0a  ** transfer..*/.
0560: 73 74 61 74 69 63 20 73 74 72 75 63 74 20 7b 0a  static struct {.
0570: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
0580: 61 6d 65 3b 20 20 20 2f 2a 20 4e 61 6d 65 20 6f  ame;   /* Name o
0590: 66 20 74 68 65 20 63 6f 6e 66 69 67 75 72 61 74  f the configurat
05a0: 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 2a 2f  ion parameter */
05b0: 0a 20 20 69 6e 74 20 67 72 6f 75 70 4d 61 73 6b  .  int groupMask
05c0: 3b 20 20 20 20 20 20 20 2f 2a 20 57 68 69 63 68  ;       /* Which
05d0: 20 63 6f 6e 66 69 67 20 67 72 6f 75 70 73 20 69   config groups i
05e0: 73 20 69 74 20 70 61 72 74 20 6f 66 20 2a 2f 0a  s it part of */.
05f0: 7d 20 61 53 61 66 65 43 6f 6e 66 69 67 5b 5d 20  } aSafeConfig[] 
0600: 3d 20 7b 0a 20 20 7b 20 22 63 73 73 22 2c 20 20  = {.  { "css",  
0610: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
0620: 20 43 4f 4e 46 49 47 53 45 54 5f 53 4b 49 4e 20   CONFIGSET_SKIN 
0630: 7d 2c 0a 20 20 7b 20 22 68 65 61 64 65 72 22 2c  },.  { "header",
0640: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
0650: 43 4f 4e 46 49 47 53 45 54 5f 53 4b 49 4e 20 7d  CONFIGSET_SKIN }
0660: 2c 0a 20 20 7b 20 22 66 6f 6f 74 65 72 22 2c 20  ,.  { "footer", 
0670: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 43                 C
0680: 4f 4e 46 49 47 53 45 54 5f 53 4b 49 4e 20 7d 2c  ONFIGSET_SKIN },
0690: 0a 20 20 7b 20 22 70 72 6f 6a 65 63 74 2d 6e 61  .  { "project-na
06a0: 6d 65 22 2c 20 20 20 20 20 20 20 20 20 20 43 4f  me",          CO
06b0: 4e 46 49 47 53 45 54 5f 50 52 4f 4a 20 7d 2c 0a  NFIGSET_PROJ },.
06c0: 20 20 7b 20 22 70 72 6f 6a 65 63 74 2d 64 65 73    { "project-des
06d0: 63 72 69 70 74 69 6f 6e 22 2c 20 20 20 43 4f 4e  cription",   CON
06e0: 46 49 47 53 45 54 5f 50 52 4f 4a 20 7d 2c 0a 20  FIGSET_PROJ },. 
06f0: 20 7b 20 22 69 6e 64 65 78 2d 70 61 67 65 22 2c   { "index-page",
0700: 20 20 20 20 20 20 20 20 20 20 20 20 43 4f 4e 46              CONF
0710: 49 47 53 45 54 5f 53 4b 49 4e 20 7d 2c 0a 20 20  IGSET_SKIN },.  
0720: 7b 20 22 74 69 6d 65 6c 69 6e 65 2d 62 6c 6f 63  { "timeline-bloc
0730: 6b 2d 6d 61 72 6b 75 70 22 2c 20 43 4f 4e 46 49  k-markup", CONFI
0740: 47 53 45 54 5f 53 4b 49 4e 20 7d 2c 0a 20 20 7b  GSET_SKIN },.  {
0750: 20 22 74 69 6d 65 6c 69 6e 65 2d 6d 61 78 2d 63   "timeline-max-c
0760: 6f 6d 6d 65 6e 74 22 2c 20 20 43 4f 4e 46 49 47  omment",  CONFIG
0770: 53 45 54 5f 53 4b 49 4e 20 7d 2c 0a 7d 3b 0a 0a  SET_SKIN },.};..
0780: 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63  /*.** This struc
0790: 74 75 72 65 20 68 6f 6c 64 73 20 69 6e 66 6f 72  ture holds infor
07a0: 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65  mation about the
07b0: 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f   current state o
07c0: 66 20 65 69 74 68 65 72 0a 2a 2a 20 61 20 63 6c  f either.** a cl
07d0: 69 65 6e 74 20 6f 72 20 61 20 73 65 72 76 65 72  ient or a server
07e0: 20 74 68 61 74 20 69 73 20 70 61 72 74 69 63 69   that is partici
07f0: 70 61 74 69 6e 67 20 69 6e 20 78 66 65 72 2e 0a  pating in xfer..
0800: 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
0810: 74 20 58 66 65 72 20 58 66 65 72 3b 0a 73 74 72  t Xfer Xfer;.str
0820: 75 63 74 20 58 66 65 72 20 7b 0a 20 20 42 6c 6f  uct Xfer {.  Blo
0830: 62 20 2a 70 49 6e 3b 20 20 20 20 20 20 20 20 20  b *pIn;         
0840: 20 2f 2a 20 49 6e 70 75 74 20 74 65 78 74 20 66   /* Input text f
0850: 72 6f 6d 20 74 68 65 20 6f 74 68 65 72 20 73 69  rom the other si
0860: 64 65 20 2a 2f 0a 20 20 42 6c 6f 62 20 2a 70 4f  de */.  Blob *pO
0870: 75 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43  ut;         /* C
0880: 6f 6d 70 6f 73 65 20 6f 75 72 20 72 65 70 6c 79  ompose our reply
0890: 20 68 65 72 65 20 2a 2f 0a 20 20 42 6c 6f 62 20   here */.  Blob 
08a0: 6c 69 6e 65 3b 20 20 20 20 20 20 20 20 20 20 2f  line;          /
08b0: 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20 6c 69  * The current li
08c0: 6e 65 20 6f 66 20 69 6e 70 75 74 20 2a 2f 0a 20  ne of input */. 
08d0: 20 42 6c 6f 62 20 61 54 6f 6b 65 6e 5b 35 5d 3b   Blob aToken[5];
08e0: 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65       /* Tokenize
08f0: 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 6c 69 6e  d version of lin
0900: 65 20 2a 2f 0a 20 20 42 6c 6f 62 20 65 72 72 3b  e */.  Blob err;
0910: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72             /* Er
0920: 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78 74  ror message text
0930: 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e   */.  int nToken
0940: 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  ;         /* Num
0950: 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e  ber of tokens in
0960: 20 6c 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 6e   line */.  int n
0970: 49 47 6f 74 53 65 6e 74 3b 20 20 20 20 20 20 2f  IGotSent;      /
0980: 2a 20 4e 75 6d 62 65 72 20 6f 66 20 22 69 67 6f  * Number of "igo
0990: 74 22 20 63 61 72 64 73 20 73 65 6e 74 20 2a 2f  t" cards sent */
09a0: 0a 20 20 69 6e 74 20 6e 47 69 6d 6d 65 53 65 6e  .  int nGimmeSen
09b0: 74 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  t;     /* Number
09c0: 20 6f 66 20 67 69 6d 6d 65 20 63 61 72 64 73 20   of gimme cards 
09d0: 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 46  sent */.  int nF
09e0: 69 6c 65 53 65 6e 74 3b 20 20 20 20 20 20 2f 2a  ileSent;      /*
09f0: 20 4e 75 6d 62 65 72 20 6f 66 20 66 69 6c 65 73   Number of files
0a00: 20 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e   sent */.  int n
0a10: 44 65 6c 74 61 53 65 6e 74 3b 20 20 20 20 20 2f  DeltaSent;     /
0a20: 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 65 6c 74  * Number of delt
0a30: 61 73 20 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74  as sent */.  int
0a40: 20 6e 46 69 6c 65 52 63 76 64 3b 20 20 20 20 20   nFileRcvd;     
0a50: 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 69   /* Number of fi
0a60: 6c 65 73 20 72 65 63 65 69 76 65 64 20 2a 2f 0a  les received */.
0a70: 20 20 69 6e 74 20 6e 44 65 6c 74 61 52 63 76 64    int nDeltaRcvd
0a80: 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20  ;     /* Number 
0a90: 6f 66 20 64 65 6c 74 61 73 20 72 65 63 65 69 76  of deltas receiv
0aa0: 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 61 6e  ed */.  int nDan
0ab0: 67 6c 69 6e 67 46 69 6c 65 3b 20 20 2f 2a 20 4e  glingFile;  /* N
0ac0: 75 6d 62 65 72 20 6f 66 20 64 61 6e 67 6c 69 6e  umber of danglin
0ad0: 67 20 64 65 6c 74 61 73 20 72 65 63 65 69 76 65  g deltas receive
0ae0: 64 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 53 65 6e  d */.  int mxSen
0af0: 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74  d;         /* St
0b00: 6f 70 20 73 65 6e 64 69 6e 67 20 22 66 69 6c 65  op sending "file
0b10: 22 20 77 69 74 68 20 70 4f 75 74 20 72 65 61 63  " with pOut reac
0b20: 68 65 73 20 74 68 69 73 20 73 69 7a 65 20 2a 2f  hes this size */
0b30: 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  .};.../*.** The 
0b40: 69 6e 70 75 74 20 62 6c 6f 62 20 63 6f 6e 74 61  input blob conta
0b50: 69 6e 73 20 61 20 55 55 49 44 2e 20 20 43 6f 6e  ins a UUID.  Con
0b60: 76 65 72 74 20 69 74 20 69 6e 74 6f 20 61 20 72  vert it into a r
0b70: 65 63 6f 72 64 20 49 44 2e 0a 2a 2a 20 43 72 65  ecord ID..** Cre
0b80: 61 74 65 20 61 20 70 68 61 6e 74 6f 6d 20 72 65  ate a phantom re
0b90: 63 6f 72 64 20 69 66 20 6e 6f 20 70 72 69 6f 72  cord if no prior
0ba0: 20 72 65 63 6f 72 64 20 65 78 69 73 74 73 20 61   record exists a
0bb0: 6e 64 0a 2a 2a 20 70 68 61 6e 74 6f 6d 69 7a 65  nd.** phantomize
0bc0: 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20   is true..**.** 
0bd0: 43 6f 6d 70 61 72 65 20 74 6f 20 75 75 69 64 5f  Compare to uuid_
0be0: 74 6f 5f 72 69 64 28 29 2e 20 20 54 68 69 73 20  to_rid().  This 
0bf0: 72 6f 75 74 69 6e 65 20 74 61 6b 65 73 20 61 20  routine takes a 
0c00: 62 6c 6f 62 20 61 72 67 75 6d 65 6e 74 0a 2a 2a  blob argument.**
0c10: 20 61 6e 64 20 64 6f 65 73 20 6c 65 73 73 20 65   and does less e
0c20: 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 2e 0a 2a  rror checking..*
0c30: 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 69 64  /.static int rid
0c40: 5f 66 72 6f 6d 5f 75 75 69 64 28 42 6c 6f 62 20  _from_uuid(Blob 
0c50: 2a 70 55 75 69 64 2c 20 69 6e 74 20 70 68 61 6e  *pUuid, int phan
0c60: 74 6f 6d 69 7a 65 29 7b 0a 20 20 73 74 61 74 69  tomize){.  stati
0c70: 63 20 53 74 6d 74 20 71 3b 0a 20 20 69 6e 74 20  c Stmt q;.  int 
0c80: 72 69 64 3b 0a 0a 20 20 64 62 5f 73 74 61 74 69  rid;..  db_stati
0c90: 63 5f 70 72 65 70 61 72 65 28 26 71 2c 20 22 53  c_prepare(&q, "S
0ca0: 45 4c 45 43 54 20 72 69 64 20 46 52 4f 4d 20 62  ELECT rid FROM b
0cb0: 6c 6f 62 20 57 48 45 52 45 20 75 75 69 64 3d 3a  lob WHERE uuid=:
0cc0: 75 75 69 64 22 29 3b 0a 20 20 64 62 5f 62 69 6e  uuid");.  db_bin
0cd0: 64 5f 73 74 72 28 26 71 2c 20 22 3a 75 75 69 64  d_str(&q, ":uuid
0ce0: 22 2c 20 70 55 75 69 64 29 3b 0a 20 20 69 66 28  ", pUuid);.  if(
0cf0: 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53 51   db_step(&q)==SQ
0d00: 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20  LITE_ROW ){.    
0d10: 72 69 64 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f  rid = db_column_
0d20: 69 6e 74 28 26 71 2c 20 30 29 3b 0a 20 20 7d 65  int(&q, 0);.  }e
0d30: 6c 73 65 7b 0a 20 20 20 20 72 69 64 20 3d 20 30  lse{.    rid = 0
0d40: 3b 0a 20 20 7d 0a 20 20 64 62 5f 72 65 73 65 74  ;.  }.  db_reset
0d50: 28 26 71 29 3b 0a 20 20 69 66 28 20 72 69 64 3d  (&q);.  if( rid=
0d60: 3d 30 20 26 26 20 70 68 61 6e 74 6f 6d 69 7a 65  =0 && phantomize
0d70: 20 29 7b 0a 20 20 20 20 72 69 64 20 3d 20 63 6f   ){.    rid = co
0d80: 6e 74 65 6e 74 5f 6e 65 77 28 62 6c 6f 62 5f 73  ntent_new(blob_s
0d90: 74 72 28 70 55 75 69 64 29 29 3b 0a 20 20 7d 0a  tr(pUuid));.  }.
0da0: 20 20 72 65 74 75 72 6e 20 72 69 64 3b 0a 7d 0a    return rid;.}.
0db0: 0a 2f 2a 0a 2a 2a 20 52 65 6d 65 6d 62 65 72 20  ./*.** Remember 
0dc0: 74 68 61 74 20 74 68 65 20 6f 74 68 65 72 20 73  that the other s
0dd0: 69 64 65 20 6f 66 20 74 68 65 20 63 6f 6e 6e 65  ide of the conne
0de0: 63 74 69 6f 6e 20 61 6c 72 65 61 64 79 20 68 61  ction already ha
0df0: 73 20 61 20 63 6f 70 79 0a 2a 2a 20 6f 66 20 74  s a copy.** of t
0e00: 68 65 20 66 69 6c 65 20 72 69 64 2e 0a 2a 2f 0a  he file rid..*/.
0e10: 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6d 6f  static void remo
0e20: 74 65 5f 68 61 73 28 69 6e 74 20 72 69 64 29 7b  te_has(int rid){
0e30: 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63  .  db_multi_exec
0e40: 28 22 49 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f  ("INSERT OR IGNO
0e50: 52 45 20 49 4e 54 4f 20 6f 6e 72 65 6d 6f 74 65  RE INTO onremote
0e60: 20 56 41 4c 55 45 53 28 25 64 29 22 2c 20 72 69   VALUES(%d)", ri
0e70: 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  d);.}../*.** The
0e80: 20 61 54 6f 6b 65 6e 5b 30 2e 2e 6e 54 6f 6b 65   aToken[0..nToke
0e90: 6e 2d 31 5d 20 62 6c 6f 62 20 61 72 72 61 79 20  n-1] blob array 
0ea0: 69 73 20 61 20 70 61 72 73 65 20 6f 66 20 61 20  is a parse of a 
0eb0: 22 66 69 6c 65 22 20 6c 69 6e 65 20 0a 2a 2a 20  "file" line .** 
0ec0: 6d 65 73 73 61 67 65 2e 20 20 54 68 69 73 20 72  message.  This r
0ed0: 6f 75 74 69 6e 65 20 66 69 6e 69 73 68 65 73 20  outine finishes 
0ee0: 70 61 72 73 69 6e 67 20 74 68 61 74 20 6d 65 73  parsing that mes
0ef0: 73 61 67 65 20 61 6e 64 20 64 6f 65 73 0a 2a 2a  sage and does.**
0f00: 20 61 20 72 65 63 6f 72 64 20 69 6e 73 65 72 74   a record insert
0f10: 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a   of the file..**
0f20: 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 6c 69 6e  .** The file lin
0f30: 65 20 69 73 20 69 6e 20 6f 6e 65 20 6f 66 20 74  e is in one of t
0f40: 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f  he following two
0f50: 20 66 6f 72 6d 73 3a 0a 2a 2a 0a 2a 2a 20 20 20   forms:.**.**   
0f60: 20 20 20 66 69 6c 65 20 55 55 49 44 20 53 49 5a     file UUID SIZ
0f70: 45 20 5c 6e 20 43 4f 4e 54 45 4e 54 0a 2a 2a 20  E \n CONTENT.** 
0f80: 20 20 20 20 20 66 69 6c 65 20 55 55 49 44 20 44       file UUID D
0f90: 45 4c 54 41 53 52 43 20 53 49 5a 45 20 5c 6e 20  ELTASRC SIZE \n 
0fa0: 43 4f 4e 54 45 4e 54 0a 2a 2a 0a 2a 2a 20 54 68  CONTENT.**.** Th
0fb0: 65 20 63 6f 6e 74 65 6e 74 20 69 73 20 53 49 5a  e content is SIZ
0fc0: 45 20 62 79 74 65 73 20 69 6d 6d 65 64 69 61 74  E bytes immediat
0fd0: 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68  ely following th
0fe0: 65 20 6e 65 77 6c 69 6e 65 2e 0a 2a 2a 20 49 66  e newline..** If
0ff0: 20 44 45 4c 54 41 53 52 43 20 65 78 69 73 74 73   DELTASRC exists
1000: 2c 20 74 68 65 6e 20 74 68 65 20 43 4f 4e 54 45  , then the CONTE
1010: 4e 54 20 69 73 20 61 20 64 65 6c 74 61 20 61 67  NT is a delta ag
1020: 61 69 6e 73 74 20 74 68 65 0a 2a 2a 20 63 6f 6e  ainst the.** con
1030: 74 65 6e 74 20 6f 66 20 44 45 4c 54 41 53 52 43  tent of DELTASRC
1040: 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 65  ..**.** If any e
1050: 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 77 72 69  rror occurs, wri
1060: 74 65 20 61 20 6d 65 73 73 61 67 65 20 69 6e 74  te a message int
1070: 6f 20 70 45 72 72 20 77 68 69 63 68 20 68 61 73  o pErr which has
1080: 20 61 6c 72 65 61 64 79 0a 2a 2a 20 62 65 20 69   already.** be i
1090: 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 61 6e  nitialized to an
10a0: 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2e 0a 2a   empty string..*
10b0: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 78 66  /.static void xf
10c0: 65 72 5f 61 63 63 65 70 74 5f 66 69 6c 65 28 58  er_accept_file(X
10d0: 66 65 72 20 2a 70 58 66 65 72 29 7b 0a 20 20 69  fer *pXfer){.  i
10e0: 6e 74 20 6e 3b 0a 20 20 69 6e 74 20 72 69 64 3b  nt n;.  int rid;
10f0: 0a 20 20 69 6e 74 20 73 72 63 69 64 20 3d 20 30  .  int srcid = 0
1100: 3b 0a 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e 74  ;.  Blob content
1110: 2c 20 68 61 73 68 3b 0a 20 20 0a 20 20 69 66 28  , hash;.  .  if(
1120: 20 70 58 66 65 72 2d 3e 6e 54 6f 6b 65 6e 3c 33   pXfer->nToken<3
1130: 20 0a 20 20 20 7c 7c 20 70 58 66 65 72 2d 3e 6e   .   || pXfer->n
1140: 54 6f 6b 65 6e 3e 34 0a 20 20 20 7c 7c 20 21 62  Token>4.   || !b
1150: 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26 70 58 66  lob_is_uuid(&pXf
1160: 65 72 2d 3e 61 54 6f 6b 65 6e 5b 31 5d 29 0a 20  er->aToken[1]). 
1170: 20 20 7c 7c 20 21 62 6c 6f 62 5f 69 73 5f 69 6e    || !blob_is_in
1180: 74 28 26 70 58 66 65 72 2d 3e 61 54 6f 6b 65 6e  t(&pXfer->aToken
1190: 5b 70 58 66 65 72 2d 3e 6e 54 6f 6b 65 6e 2d 31  [pXfer->nToken-1
11a0: 5d 2c 20 26 6e 29 0a 20 20 20 7c 7c 20 6e 3c 30  ], &n).   || n<0
11b0: 0a 20 20 20 7c 7c 20 28 70 58 66 65 72 2d 3e 6e  .   || (pXfer->n
11c0: 54 6f 6b 65 6e 3d 3d 34 20 26 26 20 21 62 6c 6f  Token==4 && !blo
11d0: 62 5f 69 73 5f 75 75 69 64 28 26 70 58 66 65 72  b_is_uuid(&pXfer
11e0: 2d 3e 61 54 6f 6b 65 6e 5b 32 5d 29 29 0a 20 20  ->aToken[2])).  
11f0: 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65  ){.    blob_appe
1200: 6e 64 66 28 26 70 58 66 65 72 2d 3e 65 72 72 2c  ndf(&pXfer->err,
1210: 20 22 6d 61 6c 66 6f 72 6d 65 64 20 66 69 6c 65   "malformed file
1220: 20 6c 69 6e 65 22 29 3b 0a 20 20 20 20 72 65 74   line");.    ret
1230: 75 72 6e 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f  urn;.  }.  blob_
1240: 7a 65 72 6f 28 26 63 6f 6e 74 65 6e 74 29 3b 0a  zero(&content);.
1250: 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 68 61 73    blob_zero(&has
1260: 68 29 3b 0a 20 20 62 6c 6f 62 5f 65 78 74 72 61  h);.  blob_extra
1270: 63 74 28 70 58 66 65 72 2d 3e 70 49 6e 2c 20 6e  ct(pXfer->pIn, n
1280: 2c 20 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 69  , &content);.  i
1290: 66 28 20 64 62 5f 65 78 69 73 74 73 28 22 53 45  f( db_exists("SE
12a0: 4c 45 43 54 20 31 20 46 52 4f 4d 20 73 68 75 6e  LECT 1 FROM shun
12b0: 20 57 48 45 52 45 20 75 75 69 64 3d 25 42 22 2c   WHERE uuid=%B",
12c0: 20 26 70 58 66 65 72 2d 3e 61 54 6f 6b 65 6e 5b   &pXfer->aToken[
12d0: 31 5d 29 20 29 7b 0a 20 20 20 20 2f 2a 20 49 67  1]) ){.    /* Ig
12e0: 6e 6f 72 65 20 66 69 6c 65 73 20 74 68 61 74 20  nore files that 
12f0: 68 61 76 65 20 62 65 65 6e 20 73 68 75 6e 6e 65  have been shunne
1300: 64 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 3b  d */.    return;
1310: 0a 20 20 7d 0a 20 20 69 66 28 20 70 58 66 65 72  .  }.  if( pXfer
1320: 2d 3e 6e 54 6f 6b 65 6e 3d 3d 34 20 29 7b 0a 20  ->nToken==4 ){. 
1330: 20 20 20 42 6c 6f 62 20 73 72 63 3b 0a 20 20 20     Blob src;.   
1340: 20 73 72 63 69 64 20 3d 20 72 69 64 5f 66 72 6f   srcid = rid_fro
1350: 6d 5f 75 75 69 64 28 26 70 58 66 65 72 2d 3e 61  m_uuid(&pXfer->a
1360: 54 6f 6b 65 6e 5b 32 5d 2c 20 31 29 3b 0a 20 20  Token[2], 1);.  
1370: 20 20 69 66 28 20 63 6f 6e 74 65 6e 74 5f 67 65    if( content_ge
1380: 74 28 73 72 63 69 64 2c 20 26 73 72 63 29 3d 3d  t(srcid, &src)==
1390: 30 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 74 65  0 ){.      conte
13a0: 6e 74 5f 70 75 74 28 26 63 6f 6e 74 65 6e 74 2c  nt_put(&content,
13b0: 20 62 6c 6f 62 5f 73 74 72 28 26 70 58 66 65 72   blob_str(&pXfer
13c0: 2d 3e 61 54 6f 6b 65 6e 5b 31 5d 29 2c 20 73 72  ->aToken[1]), sr
13d0: 63 69 64 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62  cid);.      blob
13e0: 5f 61 70 70 65 6e 64 66 28 70 58 66 65 72 2d 3e  _appendf(pXfer->
13f0: 70 4f 75 74 2c 20 22 67 69 6d 6d 65 20 25 62 5c  pOut, "gimme %b\
1400: 6e 22 2c 20 26 70 58 66 65 72 2d 3e 61 54 6f 6b  n", &pXfer->aTok
1410: 65 6e 5b 32 5d 29 3b 0a 20 20 20 20 20 20 70 58  en[2]);.      pX
1420: 66 65 72 2d 3e 6e 47 69 6d 6d 65 53 65 6e 74 2b  fer->nGimmeSent+
1430: 2b 3b 0a 20 20 20 20 20 20 70 58 66 65 72 2d 3e  +;.      pXfer->
1440: 6e 44 61 6e 67 6c 69 6e 67 46 69 6c 65 2b 2b 3b  nDanglingFile++;
1450: 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
1460: 20 20 20 7d 0a 20 20 20 20 70 58 66 65 72 2d 3e     }.    pXfer->
1470: 6e 44 65 6c 74 61 52 63 76 64 2b 2b 3b 0a 20 20  nDeltaRcvd++;.  
1480: 20 20 62 6c 6f 62 5f 64 65 6c 74 61 5f 61 70 70    blob_delta_app
1490: 6c 79 28 26 73 72 63 2c 20 26 63 6f 6e 74 65 6e  ly(&src, &conten
14a0: 74 2c 20 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20  t, &content);.  
14b0: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 73 72    blob_reset(&sr
14c0: 63 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  c);.  }else{.   
14d0: 20 70 58 66 65 72 2d 3e 6e 46 69 6c 65 52 63 76   pXfer->nFileRcv
14e0: 64 2b 2b 3b 0a 20 20 7d 0a 20 20 73 68 61 31 73  d++;.  }.  sha1s
14f0: 75 6d 5f 62 6c 6f 62 28 26 63 6f 6e 74 65 6e 74  um_blob(&content
1500: 2c 20 26 68 61 73 68 29 3b 0a 20 20 69 66 28 20  , &hash);.  if( 
1510: 21 62 6c 6f 62 5f 65 71 5f 73 74 72 28 26 70 58  !blob_eq_str(&pX
1520: 66 65 72 2d 3e 61 54 6f 6b 65 6e 5b 31 5d 2c 20  fer->aToken[1], 
1530: 62 6c 6f 62 5f 73 74 72 28 26 68 61 73 68 29 2c  blob_str(&hash),
1540: 20 2d 31 29 20 29 7b 0a 20 20 20 20 62 6c 6f 62   -1) ){.    blob
1550: 5f 61 70 70 65 6e 64 66 28 26 70 58 66 65 72 2d  _appendf(&pXfer-
1560: 3e 65 72 72 2c 20 22 63 6f 6e 74 65 6e 74 20 64  >err, "content d
1570: 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 73 68  oes not match sh
1580: 61 31 20 68 61 73 68 22 29 3b 0a 20 20 7d 0a 20  a1 hash");.  }. 
1590: 20 72 69 64 20 3d 20 63 6f 6e 74 65 6e 74 5f 70   rid = content_p
15a0: 75 74 28 26 63 6f 6e 74 65 6e 74 2c 20 62 6c 6f  ut(&content, blo
15b0: 62 5f 73 74 72 28 26 68 61 73 68 29 2c 20 30 29  b_str(&hash), 0)
15c0: 3b 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26  ;.  blob_reset(&
15d0: 68 61 73 68 29 3b 0a 20 20 69 66 28 20 72 69 64  hash);.  if( rid
15e0: 3d 3d 30 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f  ==0 ){.    blob_
15f0: 61 70 70 65 6e 64 66 28 26 70 58 66 65 72 2d 3e  appendf(&pXfer->
1600: 65 72 72 2c 20 22 25 73 22 2c 20 67 2e 7a 45 72  err, "%s", g.zEr
1610: 72 4d 73 67 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  rMsg);.  }else{.
1620: 20 20 20 20 2f 2a 20 64 62 5f 6d 75 6c 74 69 5f      /* db_multi_
1630: 65 78 65 63 28 22 44 45 4c 45 54 45 20 46 52 4f  exec("DELETE FRO
1640: 4d 20 70 68 61 6e 74 6f 6d 20 57 48 45 52 45 20  M phantom WHERE 
1650: 72 69 64 3d 25 64 22 2c 20 72 69 64 29 3b 20 2a  rid=%d", rid); *
1660: 2f 0a 20 20 20 20 6d 61 6e 69 66 65 73 74 5f 63  /.    manifest_c
1670: 72 6f 73 73 6c 69 6e 6b 28 72 69 64 2c 20 26 63  rosslink(rid, &c
1680: 6f 6e 74 65 6e 74 29 3b 0a 20 20 7d 0a 20 20 72  ontent);.  }.  r
1690: 65 6d 6f 74 65 5f 68 61 73 28 72 69 64 29 3b 0a  emote_has(rid);.
16a0: 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20  }../*.** Try to 
16b0: 73 65 6e 64 20 61 20 66 69 6c 65 20 61 73 20 61  send a file as a
16c0: 20 64 65 6c 74 61 20 61 67 61 69 6e 73 74 20 69   delta against i
16d0: 74 73 20 70 61 72 65 6e 74 2e 0a 2a 2a 20 49 66  ts parent..** If
16e0: 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72 65 74   successful, ret
16f0: 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
1700: 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 64  f bytes in the d
1710: 65 6c 74 61 2e 0a 2a 2a 20 49 66 20 77 65 20 63  elta..** If we c
1720: 61 6e 6e 6f 74 20 67 65 6e 65 72 61 74 65 20 61  annot generate a
1730: 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 64 65  n appropriate de
1740: 6c 74 61 2c 20 74 68 65 6e 20 73 65 6e 64 0a 2a  lta, then send.*
1750: 2a 20 6e 6f 74 68 69 6e 67 20 61 6e 64 20 72 65  * nothing and re
1760: 74 75 72 6e 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74  turn zero..*/.st
1770: 61 74 69 63 20 69 6e 74 20 73 65 6e 64 5f 64 65  atic int send_de
1780: 6c 74 61 5f 70 61 72 65 6e 74 28 0a 20 20 58 66  lta_parent(.  Xf
1790: 65 72 20 2a 70 58 66 65 72 2c 20 20 20 20 20 20  er *pXfer,      
17a0: 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 72 61        /* The tra
17b0: 6e 73 66 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  nsfer context */
17c0: 0a 20 20 69 6e 74 20 72 69 64 2c 20 20 20 20 20  .  int rid,     
17d0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 72 65             /* re
17e0: 63 6f 72 64 20 69 64 20 6f 66 20 74 68 65 20 66  cord id of the f
17f0: 69 6c 65 20 74 6f 20 73 65 6e 64 20 2a 2f 0a 20  ile to send */. 
1800: 20 42 6c 6f 62 20 2a 70 43 6f 6e 74 65 6e 74 2c   Blob *pContent,
1810: 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1820: 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 66  content of the f
1830: 69 6c 65 20 74 6f 20 73 65 6e 64 20 2a 2f 0a 20  ile to send */. 
1840: 20 42 6c 6f 62 20 2a 70 55 75 69 64 20 20 20 20   Blob *pUuid    
1850: 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1860: 55 55 49 44 20 6f 66 20 74 68 65 20 66 69 6c 65  UUID of the file
1870: 20 74 6f 20 73 65 6e 64 20 2a 2f 0a 29 7b 0a 20   to send */.){. 
1880: 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68   static const ch
1890: 61 72 20 2a 61 7a 51 75 65 72 79 5b 5d 20 3d 20  ar *azQuery[] = 
18a0: 7b 0a 20 20 20 20 22 53 45 4c 45 43 54 20 70 69  {.    "SELECT pi
18b0: 64 20 46 52 4f 4d 20 70 6c 69 6e 6b 20 78 22 0a  d FROM plink x".
18c0: 20 20 20 20 22 20 57 48 45 52 45 20 63 69 64 3d      " WHERE cid=
18d0: 25 64 22 0a 20 20 20 20 22 20 20 20 41 4e 44 20  %d".    "   AND 
18e0: 4e 4f 54 20 45 58 49 53 54 53 28 53 45 4c 45 43  NOT EXISTS(SELEC
18f0: 54 20 31 20 46 52 4f 4d 20 70 68 61 6e 74 6f 6d  T 1 FROM phantom
1900: 20 57 48 45 52 45 20 72 69 64 3d 70 69 64 29 22   WHERE rid=pid)"
1910: 0a 20 20 20 20 22 20 20 20 41 4e 44 20 4e 4f 54  .    "   AND NOT
1920: 20 45 58 49 53 54 53 28 53 45 4c 45 43 54 20 31   EXISTS(SELECT 1
1930: 20 46 52 4f 4d 20 70 6c 69 6e 6b 20 79 22 0a 20   FROM plink y". 
1940: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1950: 20 20 20 20 20 22 20 57 48 45 52 45 20 79 2e 70       " WHERE y.p
1960: 69 64 3d 78 2e 63 69 64 20 41 4e 44 20 79 2e 63  id=x.cid AND y.c
1970: 69 64 3d 78 2e 70 69 64 29 22 2c 0a 0a 20 20 20  id=x.pid)",..   
1980: 20 22 53 45 4c 45 43 54 20 70 69 64 20 46 52 4f   "SELECT pid FRO
1990: 4d 20 6d 6c 69 6e 6b 20 78 22 0a 20 20 20 20 22  M mlink x".    "
19a0: 20 57 48 45 52 45 20 66 69 64 3d 25 64 22 0a 20   WHERE fid=%d". 
19b0: 20 20 20 22 20 20 20 41 4e 44 20 4e 4f 54 20 45     "   AND NOT E
19c0: 58 49 53 54 53 28 53 45 4c 45 43 54 20 31 20 46  XISTS(SELECT 1 F
19d0: 52 4f 4d 20 70 68 61 6e 74 6f 6d 20 57 48 45 52  ROM phantom WHER
19e0: 45 20 72 69 64 3d 70 69 64 29 22 0a 20 20 20 20  E rid=pid)".    
19f0: 22 20 20 20 41 4e 44 20 4e 4f 54 20 45 58 49 53  "   AND NOT EXIS
1a00: 54 53 28 53 45 4c 45 43 54 20 31 20 46 52 4f 4d  TS(SELECT 1 FROM
1a10: 20 6d 6c 69 6e 6b 20 79 22 0a 20 20 20 20 20 20   mlink y".      
1a20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
1a30: 20 20 57 48 45 52 45 20 79 2e 70 69 64 3d 78 2e    WHERE y.pid=x.
1a40: 66 69 64 20 41 4e 44 20 79 2e 66 69 64 3d 78 2e  fid AND y.fid=x.
1a50: 70 69 64 29 22 0a 20 20 7d 3b 0a 20 20 69 6e 74  pid)".  };.  int
1a60: 20 69 3b 0a 20 20 42 6c 6f 62 20 73 72 63 2c 20   i;.  Blob src, 
1a70: 64 65 6c 74 61 3b 0a 20 20 69 6e 74 20 73 69 7a  delta;.  int siz
1a80: 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 73 72 63  e = 0;.  int src
1a90: 49 64 20 3d 20 30 3b 0a 0a 20 20 66 6f 72 28 69  Id = 0;..  for(i
1aa0: 3d 30 3b 20 73 72 63 49 64 3d 3d 30 20 26 26 20  =0; srcId==0 && 
1ab0: 69 3c 63 6f 75 6e 74 28 61 7a 51 75 65 72 79 29  i<count(azQuery)
1ac0: 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 72 63 49  ; i++){.    srcI
1ad0: 64 20 3d 20 64 62 5f 69 6e 74 28 30 2c 20 61 7a  d = db_int(0, az
1ae0: 51 75 65 72 79 5b 69 5d 2c 20 72 69 64 29 3b 0a  Query[i], rid);.
1af0: 20 20 7d 0a 20 20 69 66 28 20 73 72 63 49 64 3e    }.  if( srcId>
1b00: 30 20 26 26 20 63 6f 6e 74 65 6e 74 5f 67 65 74  0 && content_get
1b10: 28 73 72 63 49 64 2c 20 26 73 72 63 29 20 29 7b  (srcId, &src) ){
1b20: 0a 20 20 20 20 63 68 61 72 20 2a 7a 55 75 69 64  .    char *zUuid
1b30: 20 3d 20 64 62 5f 74 65 78 74 28 30 2c 20 22 53   = db_text(0, "S
1b40: 45 4c 45 43 54 20 75 75 69 64 20 46 52 4f 4d 20  ELECT uuid FROM 
1b50: 62 6c 6f 62 20 57 48 45 52 45 20 72 69 64 3d 25  blob WHERE rid=%
1b60: 64 22 2c 20 73 72 63 49 64 29 3b 0a 20 20 20 20  d", srcId);.    
1b70: 62 6c 6f 62 5f 64 65 6c 74 61 5f 63 72 65 61 74  blob_delta_creat
1b80: 65 28 26 73 72 63 2c 20 70 43 6f 6e 74 65 6e 74  e(&src, pContent
1b90: 2c 20 26 64 65 6c 74 61 29 3b 0a 20 20 20 20 73  , &delta);.    s
1ba0: 69 7a 65 20 3d 20 62 6c 6f 62 5f 73 69 7a 65 28  ize = blob_size(
1bb0: 26 64 65 6c 74 61 29 3b 0a 20 20 20 20 69 66 28  &delta);.    if(
1bc0: 20 73 69 7a 65 3e 3d 62 6c 6f 62 5f 73 69 7a 65   size>=blob_size
1bd0: 28 70 43 6f 6e 74 65 6e 74 29 2d 35 30 20 29 7b  (pContent)-50 ){
1be0: 0a 20 20 20 20 20 20 73 69 7a 65 20 3d 20 30 3b  .      size = 0;
1bf0: 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1c00: 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70    blob_appendf(p
1c10: 58 66 65 72 2d 3e 70 4f 75 74 2c 20 22 66 69 6c  Xfer->pOut, "fil
1c20: 65 20 25 62 20 25 73 20 25 64 5c 6e 22 2c 20 70  e %b %s %d\n", p
1c30: 55 75 69 64 2c 20 7a 55 75 69 64 2c 20 73 69 7a  Uuid, zUuid, siz
1c40: 65 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 61  e);.      blob_a
1c50: 70 70 65 6e 64 28 70 58 66 65 72 2d 3e 70 4f 75  ppend(pXfer->pOu
1c60: 74 2c 20 62 6c 6f 62 5f 62 75 66 66 65 72 28 26  t, blob_buffer(&
1c70: 64 65 6c 74 61 29 2c 20 73 69 7a 65 29 3b 0a 20  delta), size);. 
1c80: 20 20 20 20 20 2f 2a 20 62 6c 6f 62 5f 61 70 70       /* blob_app
1c90: 65 6e 64 66 28 70 58 66 65 72 2d 3e 70 4f 75 74  endf(pXfer->pOut
1ca0: 2c 20 22 5c 6e 22 2c 20 31 29 3b 20 2a 2f 0a 20  , "\n", 1); */. 
1cb0: 20 20 20 7d 0a 20 20 20 20 62 6c 6f 62 5f 72 65     }.    blob_re
1cc0: 73 65 74 28 26 64 65 6c 74 61 29 3b 0a 20 20 20  set(&delta);.   
1cd0: 20 66 72 65 65 28 7a 55 75 69 64 29 3b 0a 20 20   free(zUuid);.  
1ce0: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 73 72    blob_reset(&sr
1cf0: 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  c);.  }.  return
1d00: 20 73 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   size;.}../*.** 
1d10: 54 72 79 20 74 6f 20 73 65 6e 64 20 61 20 66 69  Try to send a fi
1d20: 6c 65 20 61 73 20 61 20 6e 61 74 69 76 65 20 64  le as a native d
1d30: 65 6c 74 61 2e 20 20 0a 2a 2a 20 49 66 20 73 75  elta.  .** If su
1d40: 63 63 65 73 73 66 75 6c 2c 20 72 65 74 75 72 6e  ccessful, return
1d50: 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
1d60: 79 74 65 73 20 69 6e 20 74 68 65 20 64 65 6c 74  ytes in the delt
1d70: 61 2e 0a 2a 2a 20 49 66 20 77 65 20 63 61 6e 6e  a..** If we cann
1d80: 6f 74 20 67 65 6e 65 72 61 74 65 20 61 6e 20 61  ot generate an a
1d90: 70 70 72 6f 70 72 69 61 74 65 20 64 65 6c 74 61  ppropriate delta
1da0: 2c 20 74 68 65 6e 20 73 65 6e 64 0a 2a 2a 20 6e  , then send.** n
1db0: 6f 74 68 69 6e 67 20 61 6e 64 20 72 65 74 75 72  othing and retur
1dc0: 6e 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69  n zero..*/.stati
1dd0: 63 20 69 6e 74 20 73 65 6e 64 5f 64 65 6c 74 61  c int send_delta
1de0: 5f 6e 61 74 69 76 65 28 0a 20 20 58 66 65 72 20  _native(.  Xfer 
1df0: 2a 70 58 66 65 72 2c 20 20 20 20 20 20 20 20 20  *pXfer,         
1e00: 20 20 20 2f 2a 20 54 68 65 20 74 72 61 6e 73 66     /* The transf
1e10: 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
1e20: 69 6e 74 20 72 69 64 2c 20 20 20 20 20 20 20 20  int rid,        
1e30: 20 20 20 20 20 20 20 20 2f 2a 20 72 65 63 6f 72          /* recor
1e40: 64 20 69 64 20 6f 66 20 74 68 65 20 66 69 6c 65  d id of the file
1e50: 20 74 6f 20 73 65 6e 64 20 2a 2f 0a 20 20 42 6c   to send */.  Bl
1e60: 6f 62 20 2a 70 55 75 69 64 20 20 20 20 20 20 20  ob *pUuid       
1e70: 20 20 20 20 20 20 2f 2a 20 54 68 65 20 55 55 49        /* The UUI
1e80: 44 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 6f  D of the file to
1e90: 20 73 65 6e 64 20 2a 2f 0a 29 7b 0a 20 20 42 6c   send */.){.  Bl
1ea0: 6f 62 20 73 72 63 2c 20 64 65 6c 74 61 3b 0a 20  ob src, delta;. 
1eb0: 20 69 6e 74 20 73 69 7a 65 20 3d 20 30 3b 0a 20   int size = 0;. 
1ec0: 20 69 6e 74 20 73 72 63 49 64 3b 0a 0a 20 20 73   int srcId;..  s
1ed0: 72 63 49 64 20 3d 20 64 62 5f 69 6e 74 28 30 2c  rcId = db_int(0,
1ee0: 20 22 53 45 4c 45 43 54 20 73 72 63 69 64 20 46   "SELECT srcid F
1ef0: 52 4f 4d 20 64 65 6c 74 61 20 57 48 45 52 45 20  ROM delta WHERE 
1f00: 72 69 64 3d 25 64 22 2c 20 72 69 64 29 3b 0a 20  rid=%d", rid);. 
1f10: 20 69 66 28 20 73 72 63 49 64 3e 30 20 29 7b 0a   if( srcId>0 ){.
1f20: 20 20 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 64      blob_zero(&d
1f30: 65 6c 74 61 29 3b 0a 20 20 20 20 64 62 5f 62 6c  elta);.    db_bl
1f40: 6f 62 28 26 64 65 6c 74 61 2c 20 22 53 45 4c 45  ob(&delta, "SELE
1f50: 43 54 20 63 6f 6e 74 65 6e 74 20 46 52 4f 4d 20  CT content FROM 
1f60: 62 6c 6f 62 20 57 48 45 52 45 20 72 69 64 3d 25  blob WHERE rid=%
1f70: 64 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 62 6c  d", rid);.    bl
1f80: 6f 62 5f 75 6e 63 6f 6d 70 72 65 73 73 28 26 64  ob_uncompress(&d
1f90: 65 6c 74 61 2c 20 26 64 65 6c 74 61 29 3b 0a 20  elta, &delta);. 
1fa0: 20 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 73 72     blob_zero(&sr
1fb0: 63 29 3b 0a 20 20 20 20 64 62 5f 62 6c 6f 62 28  c);.    db_blob(
1fc0: 26 73 72 63 2c 20 22 53 45 4c 45 43 54 20 75 75  &src, "SELECT uu
1fd0: 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45  id FROM blob WHE
1fe0: 52 45 20 72 69 64 3d 25 64 22 2c 20 73 72 63 49  RE rid=%d", srcI
1ff0: 64 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70  d);.    blob_app
2000: 65 6e 64 66 28 70 58 66 65 72 2d 3e 70 4f 75 74  endf(pXfer->pOut
2010: 2c 20 22 66 69 6c 65 20 25 62 20 25 62 20 25 64  , "file %b %b %d
2020: 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
2030: 20 20 20 20 20 70 55 75 69 64 2c 20 26 73 72 63       pUuid, &src
2040: 2c 20 62 6c 6f 62 5f 73 69 7a 65 28 26 64 65 6c  , blob_size(&del
2050: 74 61 29 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 61  ta));.    blob_a
2060: 70 70 65 6e 64 28 70 58 66 65 72 2d 3e 70 4f 75  ppend(pXfer->pOu
2070: 74 2c 20 62 6c 6f 62 5f 62 75 66 66 65 72 28 26  t, blob_buffer(&
2080: 64 65 6c 74 61 29 2c 20 62 6c 6f 62 5f 73 69 7a  delta), blob_siz
2090: 65 28 26 64 65 6c 74 61 29 29 3b 0a 20 20 20 20  e(&delta));.    
20a0: 73 69 7a 65 20 3d 20 62 6c 6f 62 5f 73 69 7a 65  size = blob_size
20b0: 28 26 64 65 6c 74 61 29 3b 0a 20 20 20 20 62 6c  (&delta);.    bl
20c0: 6f 62 5f 72 65 73 65 74 28 26 64 65 6c 74 61 29  ob_reset(&delta)
20d0: 3b 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74  ;.    blob_reset
20e0: 28 26 73 72 63 29 3b 0a 20 20 7d 65 6c 73 65 7b  (&src);.  }else{
20f0: 0a 20 20 20 20 73 69 7a 65 20 3d 20 30 3b 0a 20  .    size = 0;. 
2100: 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 69 7a 65   }.  return size
2110: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 6e 64 20  ;.}../*.** Send 
2120: 74 68 65 20 66 69 6c 65 20 69 64 65 6e 74 69 66  the file identif
2130: 69 65 64 20 62 79 20 72 69 64 2e 0a 2a 2a 0a 2a  ied by rid..**.*
2140: 2a 20 54 68 65 20 70 55 75 69 64 20 63 61 6e 20  * The pUuid can 
2150: 62 65 20 4e 55 4c 4c 20 69 6e 20 77 68 69 63 68  be NULL in which
2160: 20 63 61 73 65 20 74 68 65 20 63 6f 72 72 65 63   case the correc
2170: 74 20 55 55 49 44 20 69 73 20 63 6f 6d 70 75 74  t UUID is comput
2180: 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 72  ed.** from the r
2190: 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 72 79 20 74 6f  id..**.** Try to
21a0: 20 73 65 6e 64 20 74 68 65 20 66 69 6c 65 20 61   send the file a
21b0: 73 20 61 20 6e 61 74 69 76 65 20 64 65 6c 74 61  s a native delta
21c0: 20 69 66 20 6e 61 74 69 76 65 44 65 6c 74 61 20   if nativeDelta 
21d0: 69 73 20 74 72 75 65 2c 20 6f 72 0a 2a 2a 20 61  is true, or.** a
21e0: 73 20 61 20 70 61 72 65 6e 74 20 64 65 6c 74 61  s a parent delta
21f0: 20 69 66 20 6e 61 74 69 76 65 44 65 6c 74 61 20   if nativeDelta 
2200: 69 73 20 66 61 6c 73 65 2e 0a 2a 2f 0a 73 74 61  is false..*/.sta
2210: 74 69 63 20 76 6f 69 64 20 73 65 6e 64 5f 66 69  tic void send_fi
2220: 6c 65 28 58 66 65 72 20 2a 70 58 66 65 72 2c 20  le(Xfer *pXfer, 
2230: 69 6e 74 20 72 69 64 2c 20 42 6c 6f 62 20 2a 70  int rid, Blob *p
2240: 55 75 69 64 2c 20 69 6e 74 20 6e 61 74 69 76 65  Uuid, int native
2250: 44 65 6c 74 61 29 7b 0a 20 20 42 6c 6f 62 20 63  Delta){.  Blob c
2260: 6f 6e 74 65 6e 74 2c 20 75 75 69 64 3b 0a 20 20  ontent, uuid;.  
2270: 69 6e 74 20 73 69 7a 65 20 3d 20 30 3b 0a 0a 20  int size = 0;.. 
2280: 20 69 66 28 20 64 62 5f 65 78 69 73 74 73 28 22   if( db_exists("
2290: 53 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 6f 6e  SELECT 1 FROM on
22a0: 72 65 6d 6f 74 65 20 57 48 45 52 45 20 72 69 64  remote WHERE rid
22b0: 3d 25 64 22 2c 20 72 69 64 29 20 29 7b 0a 20 20  =%d", rid) ){.  
22c0: 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
22d0: 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 75 75 69 64   blob_zero(&uuid
22e0: 29 3b 0a 20 20 69 66 28 20 70 55 75 69 64 3d 3d  );.  if( pUuid==
22f0: 30 20 29 7b 0a 20 20 20 20 64 62 5f 62 6c 6f 62  0 ){.    db_blob
2300: 28 26 75 75 69 64 2c 20 22 53 45 4c 45 43 54 20  (&uuid, "SELECT 
2310: 75 75 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57  uuid FROM blob W
2320: 48 45 52 45 20 72 69 64 3d 25 64 20 41 4e 44 20  HERE rid=%d AND 
2330: 73 69 7a 65 3e 3d 30 22 2c 20 72 69 64 29 3b 0a  size>=0", rid);.
2340: 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 73 69 7a      if( blob_siz
2350: 65 28 26 75 75 69 64 29 3d 3d 30 20 29 7b 0a 20  e(&uuid)==0 ){. 
2360: 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
2370: 20 7d 0a 20 20 20 20 70 55 75 69 64 20 3d 20 26   }.    pUuid = &
2380: 75 75 69 64 3b 0a 20 20 7d 0a 20 20 69 66 28 20  uuid;.  }.  if( 
2390: 70 58 66 65 72 2d 3e 6d 78 53 65 6e 64 3c 3d 62  pXfer->mxSend<=b
23a0: 6c 6f 62 5f 73 69 7a 65 28 70 58 66 65 72 2d 3e  lob_size(pXfer->
23b0: 70 4f 75 74 29 20 29 7b 0a 20 20 20 20 62 6c 6f  pOut) ){.    blo
23c0: 62 5f 61 70 70 65 6e 64 66 28 70 58 66 65 72 2d  b_appendf(pXfer-
23d0: 3e 70 4f 75 74 2c 20 22 69 67 6f 74 20 25 62 5c  >pOut, "igot %b\
23e0: 6e 22 2c 20 70 55 75 69 64 29 3b 0a 20 20 20 20  n", pUuid);.    
23f0: 70 58 66 65 72 2d 3e 6e 49 47 6f 74 53 65 6e 74  pXfer->nIGotSent
2400: 2b 2b 3b 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73  ++;.    blob_res
2410: 65 74 28 26 75 75 69 64 29 3b 0a 20 20 20 20 72  et(&uuid);.    r
2420: 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28  eturn;.  }.  if(
2430: 20 6e 61 74 69 76 65 44 65 6c 74 61 20 29 7b 0a   nativeDelta ){.
2440: 20 20 20 20 73 69 7a 65 20 3d 20 73 65 6e 64 5f      size = send_
2450: 64 65 6c 74 61 5f 6e 61 74 69 76 65 28 70 58 66  delta_native(pXf
2460: 65 72 2c 20 72 69 64 2c 20 70 55 75 69 64 29 3b  er, rid, pUuid);
2470: 0a 20 20 20 20 69 66 28 20 73 69 7a 65 20 29 7b  .    if( size ){
2480: 0a 20 20 20 20 20 20 70 58 66 65 72 2d 3e 6e 44  .      pXfer->nD
2490: 65 6c 74 61 53 65 6e 74 2b 2b 3b 0a 20 20 20 20  eltaSent++;.    
24a0: 7d 0a 20 20 7d 0a 20 20 69 66 28 20 73 69 7a 65  }.  }.  if( size
24b0: 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6f 6e 74 65  ==0 ){.    conte
24c0: 6e 74 5f 67 65 74 28 72 69 64 2c 20 26 63 6f 6e  nt_get(rid, &con
24d0: 74 65 6e 74 29 3b 0a 0a 20 20 20 20 69 66 28 20  tent);..    if( 
24e0: 21 6e 61 74 69 76 65 44 65 6c 74 61 20 26 26 20  !nativeDelta && 
24f0: 62 6c 6f 62 5f 73 69 7a 65 28 26 63 6f 6e 74 65  blob_size(&conte
2500: 6e 74 29 3e 31 30 30 20 29 7b 0a 20 20 20 20 20  nt)>100 ){.     
2510: 20 73 69 7a 65 20 3d 20 73 65 6e 64 5f 64 65 6c   size = send_del
2520: 74 61 5f 70 61 72 65 6e 74 28 70 58 66 65 72 2c  ta_parent(pXfer,
2530: 20 72 69 64 2c 20 26 63 6f 6e 74 65 6e 74 2c 20   rid, &content, 
2540: 70 55 75 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20  pUuid);.    }.  
2550: 20 20 69 66 28 20 73 69 7a 65 3d 3d 30 20 29 7b    if( size==0 ){
2560: 0a 20 20 20 20 20 20 69 6e 74 20 73 69 7a 65 20  .      int size 
2570: 3d 20 62 6c 6f 62 5f 73 69 7a 65 28 26 63 6f 6e  = blob_size(&con
2580: 74 65 6e 74 29 3b 0a 20 20 20 20 20 20 62 6c 6f  tent);.      blo
2590: 62 5f 61 70 70 65 6e 64 66 28 70 58 66 65 72 2d  b_appendf(pXfer-
25a0: 3e 70 4f 75 74 2c 20 22 66 69 6c 65 20 25 62 20  >pOut, "file %b 
25b0: 25 64 5c 6e 22 2c 20 70 55 75 69 64 2c 20 73 69  %d\n", pUuid, si
25c0: 7a 65 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f  ze);.      blob_
25d0: 61 70 70 65 6e 64 28 70 58 66 65 72 2d 3e 70 4f  append(pXfer->pO
25e0: 75 74 2c 20 62 6c 6f 62 5f 62 75 66 66 65 72 28  ut, blob_buffer(
25f0: 26 63 6f 6e 74 65 6e 74 29 2c 20 73 69 7a 65 29  &content), size)
2600: 3b 0a 20 20 20 20 20 20 70 58 66 65 72 2d 3e 6e  ;.      pXfer->n
2610: 46 69 6c 65 53 65 6e 74 2b 2b 3b 0a 20 20 20 20  FileSent++;.    
2620: 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 58 66  }else{.      pXf
2630: 65 72 2d 3e 6e 44 65 6c 74 61 53 65 6e 74 2b 2b  er->nDeltaSent++
2640: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
2650: 6d 6f 74 65 5f 68 61 73 28 72 69 64 29 3b 0a 20  mote_has(rid);. 
2660: 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 75 75 69   blob_reset(&uui
2670: 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 6e  d);.}../*.** Sen
2680: 64 20 61 20 67 69 6d 6d 65 20 6d 65 73 73 61 67  d a gimme messag
2690: 65 20 66 6f 72 20 65 76 65 72 79 20 70 68 61 6e  e for every phan
26a0: 74 6f 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  tom..*/.static v
26b0: 6f 69 64 20 72 65 71 75 65 73 74 5f 70 68 61 6e  oid request_phan
26c0: 74 6f 6d 73 28 58 66 65 72 20 2a 70 58 66 65 72  toms(Xfer *pXfer
26d0: 2c 20 69 6e 74 20 6d 61 78 52 65 71 29 7b 0a 20  , int maxReq){. 
26e0: 20 53 74 6d 74 20 71 3b 0a 20 20 64 62 5f 70 72   Stmt q;.  db_pr
26f0: 65 70 61 72 65 28 26 71 2c 20 0a 20 20 20 20 22  epare(&q, .    "
2700: 53 45 4c 45 43 54 20 75 75 69 64 20 46 52 4f 4d  SELECT uuid FROM
2710: 20 70 68 61 6e 74 6f 6d 20 4a 4f 49 4e 20 62 6c   phantom JOIN bl
2720: 6f 62 20 55 53 49 4e 47 28 72 69 64 29 22 0a 20  ob USING(rid)". 
2730: 20 20 20 22 20 57 48 45 52 45 20 4e 4f 54 20 45     " WHERE NOT E
2740: 58 49 53 54 53 28 53 45 4c 45 43 54 20 31 20 46  XISTS(SELECT 1 F
2750: 52 4f 4d 20 73 68 75 6e 20 57 48 45 52 45 20 75  ROM shun WHERE u
2760: 75 69 64 3d 62 6c 6f 62 2e 75 75 69 64 29 22 0a  uid=blob.uuid)".
2770: 20 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 64 62    );.  while( db
2780: 5f 73 74 65 70 28 26 71 29 3d 3d 53 51 4c 49 54  _step(&q)==SQLIT
2790: 45 5f 52 4f 57 20 26 26 20 6d 61 78 52 65 71 2d  E_ROW && maxReq-
27a0: 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 63 6f 6e  - > 0 ){.    con
27b0: 73 74 20 63 68 61 72 20 2a 7a 55 75 69 64 20 3d  st char *zUuid =
27c0: 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28   db_column_text(
27d0: 26 71 2c 20 30 29 3b 0a 20 20 20 20 62 6c 6f 62  &q, 0);.    blob
27e0: 5f 61 70 70 65 6e 64 66 28 70 58 66 65 72 2d 3e  _appendf(pXfer->
27f0: 70 4f 75 74 2c 20 22 67 69 6d 6d 65 20 25 73 5c  pOut, "gimme %s\
2800: 6e 22 2c 20 7a 55 75 69 64 29 3b 0a 20 20 20 20  n", zUuid);.    
2810: 70 58 66 65 72 2d 3e 6e 47 69 6d 6d 65 53 65 6e  pXfer->nGimmeSen
2820: 74 2b 2b 3b 0a 20 20 7d 0a 20 20 64 62 5f 66 69  t++;.  }.  db_fi
2830: 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 7d 0a 0a 2f  nalize(&q);.}../
2840: 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 61 6e 20  *.** Compute an 
2850: 53 48 41 31 20 68 61 73 68 20 6f 6e 20 74 68 65  SHA1 hash on the
2860: 20 74 61 69 6c 20 6f 66 20 70 4d 73 67 2e 20 20   tail of pMsg.  
2870: 56 65 72 69 66 79 20 74 68 61 74 20 69 74 20 6d  Verify that it m
2880: 61 74 63 68 65 73 20 74 68 65 0a 2a 2a 20 74 68  atches the.** th
2890: 65 20 68 61 73 68 20 67 69 76 65 6e 20 69 6e 20  e hash given in 
28a0: 70 48 61 73 68 2e 20 20 52 65 74 75 72 6e 20 31  pHash.  Return 1
28b0: 20 6f 6e 20 61 20 73 75 63 63 65 73 73 66 75 6c   on a successful
28c0: 20 6d 61 74 63 68 2e 20 20 52 65 74 75 72 6e 20   match.  Return 
28d0: 30 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 69 73  0.** if there is
28e0: 20 61 20 6d 69 73 6d 61 74 63 68 2e 0a 2a 2f 0a   a mismatch..*/.
28f0: 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b  static int check
2900: 5f 74 61 69 6c 5f 68 61 73 68 28 42 6c 6f 62 20  _tail_hash(Blob 
2910: 2a 70 48 61 73 68 2c 20 42 6c 6f 62 20 2a 70 4d  *pHash, Blob *pM
2920: 73 67 29 7b 0a 20 20 42 6c 6f 62 20 74 61 69 6c  sg){.  Blob tail
2930: 3b 0a 20 20 42 6c 6f 62 20 68 32 3b 0a 20 20 69  ;.  Blob h2;.  i
2940: 6e 74 20 72 63 3b 0a 20 20 62 6c 6f 62 5f 74 61  nt rc;.  blob_ta
2950: 69 6c 28 70 4d 73 67 2c 20 26 74 61 69 6c 29 3b  il(pMsg, &tail);
2960: 0a 20 20 73 68 61 31 73 75 6d 5f 62 6c 6f 62 28  .  sha1sum_blob(
2970: 26 74 61 69 6c 2c 20 26 68 32 29 3b 0a 20 20 72  &tail, &h2);.  r
2980: 63 20 3d 20 62 6c 6f 62 5f 63 6f 6d 70 61 72 65  c = blob_compare
2990: 28 70 48 61 73 68 2c 20 26 68 32 29 3b 0a 20 20  (pHash, &h2);.  
29a0: 62 6c 6f 62 5f 72 65 73 65 74 28 26 68 32 29 3b  blob_reset(&h2);
29b0: 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 74  .  blob_reset(&t
29c0: 61 69 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ail);.  return r
29d0: 63 3d 3d 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  c==0;.}.../*.** 
29e0: 43 68 65 63 6b 20 74 68 65 20 73 69 67 6e 61 74  Check the signat
29f0: 75 72 65 20 6f 6e 20 61 6e 20 61 70 70 6c 69 63  ure on an applic
2a00: 61 74 69 6f 6e 2f 78 2d 66 6f 73 73 69 6c 20 70  ation/x-fossil p
2a10: 61 79 6c 6f 61 64 20 72 65 63 65 69 76 65 64 20  ayload received 
2a20: 62 79 0a 2a 2a 20 74 68 65 20 48 54 54 50 20 73  by.** the HTTP s
2a30: 65 72 76 65 72 2e 20 20 54 68 65 20 73 69 67 6e  erver.  The sign
2a40: 61 74 75 72 65 20 69 73 20 61 20 6c 69 6e 65 20  ature is a line 
2a50: 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
2a60: 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   form:.**.**    
2a70: 20 20 20 20 6c 6f 67 69 6e 20 4c 4f 47 49 4e 20      login LOGIN 
2a80: 4e 4f 4e 43 45 20 53 49 47 4e 41 54 55 52 45 0a  NONCE SIGNATURE.
2a90: 2a 2a 0a 2a 2a 20 54 68 65 20 4e 4f 4e 43 45 20  **.** The NONCE 
2aa0: 69 73 20 74 68 65 20 53 48 41 31 20 68 61 73 68  is the SHA1 hash
2ab0: 20 6f 66 20 74 68 65 20 72 65 6d 61 69 6e 64 65   of the remainde
2ac0: 72 20 6f 66 20 74 68 65 20 69 6e 70 75 74 2e 20  r of the input. 
2ad0: 20 0a 2a 2a 20 53 49 47 4e 41 54 55 52 45 20 69   .** SIGNATURE i
2ae0: 73 20 74 68 65 20 53 48 41 31 20 63 68 65 63 6b  s the SHA1 check
2af0: 73 75 6d 20 6f 66 20 74 68 65 20 4e 4f 4e 43 45  sum of the NONCE
2b00: 20 63 6f 6e 63 61 74 65 6e 61 74 65 64 20 0a 2a   concatenated .*
2b10: 2a 20 77 69 74 68 20 74 68 65 20 75 73 65 72 73  * with the users
2b20: 20 70 61 73 73 77 6f 72 64 2e 0a 2a 2a 0a 2a 2a   password..**.**
2b30: 20 54 68 65 20 70 61 72 61 6d 65 74 65 72 73 20   The parameters 
2b40: 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  to this routine 
2b50: 61 72 65 20 65 70 68 65 72 6d 65 72 61 6c 20 62  are ephermeral b
2b60: 6c 6f 62 73 20 68 6f 6c 64 69 6e 67 20 74 68 65  lobs holding the
2b70: 0a 2a 2a 20 4c 4f 47 49 4e 2c 20 4e 4f 4e 43 45  .** LOGIN, NONCE
2b80: 20 61 6e 64 20 53 49 47 4e 41 54 55 52 45 2e 0a   and SIGNATURE..
2b90: 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
2ba0: 6e 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6c  ne attempts to l
2bb0: 6f 63 61 74 65 20 74 68 65 20 75 73 65 72 20 61  ocate the user a
2bc0: 6e 64 20 76 65 72 69 66 79 20 74 68 65 20 73 69  nd verify the si
2bd0: 67 6e 61 74 75 72 65 2e 0a 2a 2a 20 49 66 20 65  gnature..** If e
2be0: 76 65 72 79 74 68 69 6e 67 20 63 68 65 63 6b 73  verything checks
2bf0: 20 6f 75 74 2c 20 74 68 65 20 55 53 45 52 2e 43   out, the USER.C
2c00: 41 50 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 74 68  AP column for th
2c10: 65 20 55 53 45 52 20 74 61 62 6c 65 0a 2a 2a 20  e USER table.** 
2c20: 69 73 20 63 6f 6e 73 75 6c 74 65 64 20 74 6f 20  is consulted to 
2c30: 73 65 74 20 70 72 69 76 69 6c 65 67 65 73 20 69  set privileges i
2c40: 6e 20 74 68 65 20 67 6c 6f 62 61 6c 20 67 20 76  n the global g v
2c50: 61 72 69 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49  ariable..**.** I
2c60: 66 20 61 6e 79 74 68 69 6e 67 20 66 61 69 6c 73  f anything fails
2c70: 20 74 6f 20 63 68 65 63 6b 20 6f 75 74 2c 20 6e   to check out, n
2c80: 6f 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d 61  o changes are ma
2c90: 64 65 20 74 6f 20 70 72 69 76 69 6c 65 67 65 73  de to privileges
2ca0: 2e 0a 2a 2a 0a 2a 2a 20 53 69 67 6e 61 74 75 72  ..**.** Signatur
2cb0: 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 6e 20  e generation on 
2cc0: 74 68 65 20 63 6c 69 65 6e 74 20 73 69 64 65 20  the client side 
2cd0: 69 73 20 68 61 6e 64 6c 65 64 20 62 79 20 74 68  is handled by th
2ce0: 65 20 0a 2a 2a 20 68 74 74 70 5f 65 78 63 68 61  e .** http_excha
2cf0: 6e 67 65 28 29 20 72 6f 75 74 69 6e 65 2e 0a 2a  nge() routine..*
2d00: 2f 0a 76 6f 69 64 20 63 68 65 63 6b 5f 6c 6f 67  /.void check_log
2d10: 69 6e 28 42 6c 6f 62 20 2a 70 4c 6f 67 69 6e 2c  in(Blob *pLogin,
2d20: 20 42 6c 6f 62 20 2a 70 4e 6f 6e 63 65 2c 20 42   Blob *pNonce, B
2d30: 6c 6f 62 20 2a 70 53 69 67 29 7b 0a 20 20 53 74  lob *pSig){.  St
2d40: 6d 74 20 71 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  mt q;.  int rc;.
2d50: 0a 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 71  .  db_prepare(&q
2d60: 2c 20 22 53 45 4c 45 43 54 20 70 77 2c 20 63 61  , "SELECT pw, ca
2d70: 70 2c 20 75 69 64 20 46 52 4f 4d 20 75 73 65 72  p, uid FROM user
2d80: 20 57 48 45 52 45 20 6c 6f 67 69 6e 3d 25 42 22   WHERE login=%B"
2d90: 2c 20 70 4c 6f 67 69 6e 29 3b 0a 20 20 69 66 28  , pLogin);.  if(
2da0: 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53 51   db_step(&q)==SQ
2db0: 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20  LITE_ROW ){.    
2dc0: 42 6c 6f 62 20 70 77 2c 20 63 6f 6d 62 69 6e 65  Blob pw, combine
2dd0: 64 2c 20 68 61 73 68 3b 0a 20 20 20 20 62 6c 6f  d, hash;.    blo
2de0: 62 5f 7a 65 72 6f 28 26 70 77 29 3b 0a 20 20 20  b_zero(&pw);.   
2df0: 20 64 62 5f 65 70 68 65 6d 65 72 61 6c 5f 62 6c   db_ephemeral_bl
2e00: 6f 62 28 26 71 2c 20 30 2c 20 26 70 77 29 3b 0a  ob(&q, 0, &pw);.
2e10: 20 20 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 63      blob_zero(&c
2e20: 6f 6d 62 69 6e 65 64 29 3b 0a 20 20 20 20 62 6c  ombined);.    bl
2e30: 6f 62 5f 63 6f 70 79 28 26 63 6f 6d 62 69 6e 65  ob_copy(&combine
2e40: 64 2c 20 70 4e 6f 6e 63 65 29 3b 0a 20 20 20 20  d, pNonce);.    
2e50: 62 6c 6f 62 5f 61 70 70 65 6e 64 28 26 63 6f 6d  blob_append(&com
2e60: 62 69 6e 65 64 2c 20 62 6c 6f 62 5f 62 75 66 66  bined, blob_buff
2e70: 65 72 28 26 70 77 29 2c 20 62 6c 6f 62 5f 73 69  er(&pw), blob_si
2e80: 7a 65 28 26 70 77 29 29 3b 0a 20 20 20 20 2f 2a  ze(&pw));.    /*
2e90: 20 43 47 49 44 45 42 55 47 28 28 22 70 72 65 73   CGIDEBUG(("pres
2ea0: 69 67 3d 5b 25 73 5d 5c 6e 22 2c 20 62 6c 6f 62  ig=[%s]\n", blob
2eb0: 5f 73 74 72 28 26 63 6f 6d 62 69 6e 65 64 29 29  _str(&combined))
2ec0: 29 3b 20 2a 2f 0a 20 20 20 20 73 68 61 31 73 75  ); */.    sha1su
2ed0: 6d 5f 62 6c 6f 62 28 26 63 6f 6d 62 69 6e 65 64  m_blob(&combined
2ee0: 2c 20 26 68 61 73 68 29 3b 0a 20 20 20 20 72 63  , &hash);.    rc
2ef0: 20 3d 20 62 6c 6f 62 5f 63 6f 6d 70 61 72 65 28   = blob_compare(
2f00: 26 68 61 73 68 2c 20 70 53 69 67 29 3b 0a 20 20  &hash, pSig);.  
2f10: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 68 61    blob_reset(&ha
2f20: 73 68 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 72 65  sh);.    blob_re
2f30: 73 65 74 28 26 63 6f 6d 62 69 6e 65 64 29 3b 0a  set(&combined);.
2f40: 20 20 20 20 69 66 28 20 72 63 3d 3d 30 20 29 7b      if( rc==0 ){
2f50: 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
2f60: 72 20 2a 7a 43 61 70 3b 0a 20 20 20 20 20 20 7a  r *zCap;.      z
2f70: 43 61 70 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f  Cap = db_column_
2f80: 74 65 78 74 28 26 71 2c 20 31 29 3b 0a 20 20 20  text(&q, 1);.   
2f90: 20 20 20 6c 6f 67 69 6e 5f 73 65 74 5f 63 61 70     login_set_cap
2fa0: 61 62 69 6c 69 74 69 65 73 28 7a 43 61 70 29 3b  abilities(zCap);
2fb0: 0a 20 20 20 20 20 20 67 2e 75 73 65 72 55 69 64  .      g.userUid
2fc0: 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74   = db_column_int
2fd0: 28 26 71 2c 20 32 29 3b 0a 20 20 20 20 20 20 67  (&q, 2);.      g
2fe0: 2e 7a 4c 6f 67 69 6e 20 3d 20 6d 70 72 69 6e 74  .zLogin = mprint
2ff0: 66 28 22 25 62 22 2c 20 70 4c 6f 67 69 6e 29 3b  f("%b", pLogin);
3000: 0a 20 20 20 20 20 20 67 2e 7a 4e 6f 6e 63 65 20  .      g.zNonce 
3010: 3d 20 6d 70 72 69 6e 74 66 28 22 25 62 22 2c 20  = mprintf("%b", 
3020: 70 4e 6f 6e 63 65 29 3b 0a 20 20 20 20 7d 0a 20  pNonce);.    }. 
3030: 20 7d 0a 20 20 64 62 5f 72 65 73 65 74 28 26 71   }.  db_reset(&q
3040: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 6e 64  );.}../*.** Send
3050: 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   the content of 
3060: 61 6c 6c 20 66 69 6c 65 73 20 69 6e 20 74 68 65  all files in the
3070: 20 75 6e 73 65 6e 74 20 74 61 62 6c 65 2e 0a 2a   unsent table..*
3080: 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 72 65 61  *.** This is rea
3090: 6c 6c 79 20 6a 75 73 74 20 61 6e 20 6f 70 74 69  lly just an opti
30a0: 6d 69 7a 61 74 69 6f 6e 2e 20 20 49 66 20 79 6f  mization.  If yo
30b0: 75 20 63 6c 65 61 72 20 74 68 65 0a 2a 2a 20 75  u clear the.** u
30c0: 6e 73 65 6e 74 20 74 61 62 6c 65 2c 20 61 6c 6c  nsent table, all
30d0: 20 74 68 65 20 72 69 67 68 74 20 66 69 6c 65 73   the right files
30e0: 20 77 69 6c 6c 20 73 74 69 6c 6c 20 67 65 74 20   will still get 
30f0: 74 72 61 6e 73 66 65 72 72 65 64 2e 0a 2a 2a 20  transferred..** 
3100: 49 74 20 6a 75 73 74 20 6d 69 67 68 74 20 72 65  It just might re
3110: 71 75 69 72 65 20 61 6e 20 65 78 74 72 61 20 72  quire an extra r
3120: 6f 75 6e 64 20 74 72 69 70 20 6f 72 20 74 77 6f  ound trip or two
3130: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
3140: 20 73 65 6e 64 5f 75 6e 73 65 6e 74 28 58 66 65   send_unsent(Xfe
3150: 72 20 2a 70 58 66 65 72 29 7b 0a 20 20 53 74 6d  r *pXfer){.  Stm
3160: 74 20 71 3b 0a 20 20 64 62 5f 70 72 65 70 61 72  t q;.  db_prepar
3170: 65 28 26 71 2c 20 22 53 45 4c 45 43 54 20 72 69  e(&q, "SELECT ri
3180: 64 20 46 52 4f 4d 20 75 6e 73 65 6e 74 22 29 3b  d FROM unsent");
3190: 0a 20 20 77 68 69 6c 65 28 20 64 62 5f 73 74 65  .  while( db_ste
31a0: 70 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f  p(&q)==SQLITE_RO
31b0: 57 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 69 64  W ){.    int rid
31c0: 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74   = db_column_int
31d0: 28 26 71 2c 20 30 29 3b 0a 20 20 20 20 73 65 6e  (&q, 0);.    sen
31e0: 64 5f 66 69 6c 65 28 70 58 66 65 72 2c 20 72 69  d_file(pXfer, ri
31f0: 64 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  d, 0, 0);.  }.  
3200: 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b  db_finalize(&q);
3210: 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63  .  db_multi_exec
3220: 28 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 75 6e  ("DELETE FROM un
3230: 73 65 6e 74 22 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  sent");.}../*.**
3240: 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   Check to see if
3250: 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 75   the number of u
3260: 6e 63 6c 75 73 74 65 72 65 64 20 65 6e 74 72 69  nclustered entri
3270: 65 73 20 69 73 20 67 72 65 61 74 65 72 20 74 68  es is greater th
3280: 61 6e 0a 2a 2a 20 31 30 30 20 61 6e 64 20 69 66  an.** 100 and if
3290: 20 69 74 20 69 73 2c 20 66 6f 72 6d 20 61 20 6e   it is, form a n
32a0: 65 77 20 63 6c 75 73 74 65 72 2e 20 20 55 6e 63  ew cluster.  Unc
32b0: 6c 75 73 74 65 72 65 64 20 70 68 61 6e 74 6f 6d  lustered phantom
32c0: 73 20 64 6f 20 6e 6f 74 0a 2a 2a 20 63 6f 75 6e  s do not.** coun
32d0: 74 20 74 6f 77 61 72 64 20 74 68 65 20 31 30 30  t toward the 100
32e0: 20 74 6f 74 61 6c 2e 20 20 41 6e 64 20 70 68 61   total.  And pha
32f0: 6e 74 6f 6d 73 20 61 72 65 20 6e 65 76 65 72 20  ntoms are never 
3300: 61 64 64 65 64 20 74 6f 20 61 20 6e 65 77 0a 2a  added to a new.*
3310: 2a 20 63 6c 75 73 74 65 72 2e 0a 2a 2f 0a 73 74  * cluster..*/.st
3320: 61 74 69 63 20 76 6f 69 64 20 63 72 65 61 74 65  atic void create
3330: 5f 63 6c 75 73 74 65 72 28 76 6f 69 64 29 7b 0a  _cluster(void){.
3340: 20 20 42 6c 6f 62 20 63 6c 75 73 74 65 72 2c 20    Blob cluster, 
3350: 63 6b 73 75 6d 3b 0a 20 20 53 74 6d 74 20 71 3b  cksum;.  Stmt q;
3360: 0a 20 20 69 6e 74 20 6e 55 6e 63 6c 3b 0a 20 20  .  int nUncl;.  
3370: 6e 55 6e 63 6c 20 3d 20 64 62 5f 69 6e 74 28 30  nUncl = db_int(0
3380: 2c 20 22 53 45 4c 45 43 54 20 63 6f 75 6e 74 28  , "SELECT count(
3390: 2a 29 20 46 52 4f 4d 20 75 6e 63 6c 75 73 74 65  *) FROM uncluste
33a0: 72 65 64 22 0a 20 20 20 20 20 20 20 20 20 20 20  red".           
33b0: 20 20 20 20 20 20 20 20 20 22 20 57 48 45 52 45           " WHERE
33c0: 20 4e 4f 54 20 45 58 49 53 54 53 28 53 45 4c 45   NOT EXISTS(SELE
33d0: 43 54 20 31 20 46 52 4f 4d 20 70 68 61 6e 74 6f  CT 1 FROM phanto
33e0: 6d 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  m".             
33f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3400: 20 20 20 20 20 20 20 20 20 22 20 57 48 45 52 45           " WHERE
3410: 20 72 69 64 3d 75 6e 63 6c 75 73 74 65 72 65 64   rid=unclustered
3420: 2e 72 69 64 29 22 29 3b 0a 20 20 69 66 28 20 6e  .rid)");.  if( n
3430: 55 6e 63 6c 3c 31 30 30 20 29 7b 0a 20 20 20 20  Uncl<100 ){.    
3440: 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 62 6c  return;.  }.  bl
3450: 6f 62 5f 7a 65 72 6f 28 26 63 6c 75 73 74 65 72  ob_zero(&cluster
3460: 29 3b 0a 20 20 64 62 5f 70 72 65 70 61 72 65 28  );.  db_prepare(
3470: 26 71 2c 20 22 53 45 4c 45 43 54 20 75 75 69 64  &q, "SELECT uuid
3480: 20 46 52 4f 4d 20 75 6e 63 6c 75 73 74 65 72 65   FROM unclustere
3490: 64 2c 20 62 6c 6f 62 22 0a 20 20 20 20 20 20 20  d, blob".       
34a0: 20 20 20 20 20 20 20 20 20 20 22 20 57 48 45 52            " WHER
34b0: 45 20 4e 4f 54 20 45 58 49 53 54 53 28 53 45 4c  E NOT EXISTS(SEL
34c0: 45 43 54 20 31 20 46 52 4f 4d 20 70 68 61 6e 74  ECT 1 FROM phant
34d0: 6f 6d 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  om".            
34e0: 20 20 20 20 20 22 20 20 20 20 20 20 20 20 20 20       "          
34f0: 20 20 20 20 20 20 20 20 20 57 48 45 52 45 20 72           WHERE r
3500: 69 64 21 3d 75 6e 63 6c 75 73 74 65 72 65 64 2e  id!=unclustered.
3510: 72 69 64 29 22 0a 20 20 20 20 20 20 20 20 20 20  rid)".          
3520: 20 20 20 20 20 20 20 22 20 20 20 41 4e 44 20 75         "   AND u
3530: 6e 63 6c 75 73 74 65 72 65 64 2e 72 69 64 3d 62  nclustered.rid=b
3540: 6c 6f 62 2e 72 69 64 22 0a 20 20 20 20 20 20 20  lob.rid".       
3550: 20 20 20 20 20 20 20 20 20 20 22 20 4f 52 44 45            " ORDE
3560: 52 20 42 59 20 31 22 29 3b 0a 20 20 77 68 69 6c  R BY 1");.  whil
3570: 65 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d  e( db_step(&q)==
3580: 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
3590: 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26    blob_appendf(&
35a0: 63 6c 75 73 74 65 72 2c 20 22 4d 20 25 73 5c 6e  cluster, "M %s\n
35b0: 22 2c 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78  ", db_column_tex
35c0: 74 28 26 71 2c 20 30 29 29 3b 0a 20 20 7d 0a 20  t(&q, 0));.  }. 
35d0: 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29   db_finalize(&q)
35e0: 3b 0a 20 20 6d 64 35 73 75 6d 5f 62 6c 6f 62 28  ;.  md5sum_blob(
35f0: 26 63 6c 75 73 74 65 72 2c 20 26 63 6b 73 75 6d  &cluster, &cksum
3600: 29 3b 0a 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64  );.  blob_append
3610: 66 28 26 63 6c 75 73 74 65 72 2c 20 22 5a 20 25  f(&cluster, "Z %
3620: 62 5c 6e 22 2c 20 26 63 6b 73 75 6d 29 3b 0a 20  b\n", &cksum);. 
3630: 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 63 6b 73   blob_reset(&cks
3640: 75 6d 29 3b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f  um);.  db_multi_
3650: 65 78 65 63 28 22 44 45 4c 45 54 45 20 46 52 4f  exec("DELETE FRO
3660: 4d 20 75 6e 63 6c 75 73 74 65 72 65 64 22 29 3b  M unclustered");
3670: 0a 20 20 63 6f 6e 74 65 6e 74 5f 70 75 74 28 26  .  content_put(&
3680: 63 6c 75 73 74 65 72 2c 20 30 2c 20 30 29 3b 0a  cluster, 0, 0);.
3690: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 63 6c    blob_reset(&cl
36a0: 75 73 74 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  uster);.}../*.**
36b0: 20 53 65 6e 64 20 61 6e 20 69 67 6f 74 20 6d 65   Send an igot me
36c0: 73 73 61 67 65 20 66 6f 72 20 65 76 65 72 79 20  ssage for every 
36d0: 65 6e 74 72 79 20 69 6e 20 75 6e 63 6c 75 73 74  entry in unclust
36e0: 65 72 65 64 20 74 61 62 6c 65 2e 0a 2a 2a 20 52  ered table..** R
36f0: 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
3700: 20 6f 66 20 63 61 72 64 73 20 73 65 6e 74 2e 0a   of cards sent..
3710: 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65  */.static int se
3720: 6e 64 5f 75 6e 63 6c 75 73 74 65 72 65 64 28 58  nd_unclustered(X
3730: 66 65 72 20 2a 70 58 66 65 72 29 7b 0a 20 20 53  fer *pXfer){.  S
3740: 74 6d 74 20 71 3b 0a 20 20 69 6e 74 20 63 6e 74  tmt q;.  int cnt
3750: 20 3d 20 30 3b 0a 20 20 64 62 5f 70 72 65 70 61   = 0;.  db_prepa
3760: 72 65 28 26 71 2c 20 0a 20 20 20 20 22 53 45 4c  re(&q, .    "SEL
3770: 45 43 54 20 75 75 69 64 20 46 52 4f 4d 20 75 6e  ECT uuid FROM un
3780: 63 6c 75 73 74 65 72 65 64 20 4a 4f 49 4e 20 62  clustered JOIN b
3790: 6c 6f 62 20 55 53 49 4e 47 28 72 69 64 29 22 0a  lob USING(rid)".
37a0: 20 20 20 20 22 20 57 48 45 52 45 20 4e 4f 54 20      " WHERE NOT 
37b0: 45 58 49 53 54 53 28 53 45 4c 45 43 54 20 31 20  EXISTS(SELECT 1 
37c0: 46 52 4f 4d 20 73 68 75 6e 20 57 48 45 52 45 20  FROM shun WHERE 
37d0: 75 75 69 64 3d 62 6c 6f 62 2e 75 75 69 64 29 22  uuid=blob.uuid)"
37e0: 0a 20 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 64  .  );.  while( d
37f0: 62 5f 73 74 65 70 28 26 71 29 3d 3d 53 51 4c 49  b_step(&q)==SQLI
3800: 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 62 6c  TE_ROW ){.    bl
3810: 6f 62 5f 61 70 70 65 6e 64 66 28 70 58 66 65 72  ob_appendf(pXfer
3820: 2d 3e 70 4f 75 74 2c 20 22 69 67 6f 74 20 25 73  ->pOut, "igot %s
3830: 5c 6e 22 2c 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74  \n", db_column_t
3840: 65 78 74 28 26 71 2c 20 30 29 29 3b 0a 20 20 20  ext(&q, 0));.   
3850: 20 63 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 20 64 62   cnt++;.  }.  db
3860: 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 20  _finalize(&q);. 
3870: 20 72 65 74 75 72 6e 20 63 6e 74 3b 0a 7d 0a 0a   return cnt;.}..
3880: 2f 2a 0a 2a 2a 20 53 65 6e 64 20 61 6e 20 69 67  /*.** Send an ig
3890: 6f 74 20 6d 65 73 73 61 67 65 20 66 6f 72 20 65  ot message for e
38a0: 76 65 72 79 20 61 72 74 69 66 61 63 74 2e 0a 2a  very artifact..*
38b0: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65  /.static void se
38c0: 6e 64 5f 61 6c 6c 28 58 66 65 72 20 2a 70 58 66  nd_all(Xfer *pXf
38d0: 65 72 29 7b 0a 20 20 53 74 6d 74 20 71 3b 0a 20  er){.  Stmt q;. 
38e0: 20 64 62 5f 70 72 65 70 61 72 65 28 26 71 2c 20   db_prepare(&q, 
38f0: 22 53 45 4c 45 43 54 20 75 75 69 64 20 46 52 4f  "SELECT uuid FRO
3900: 4d 20 62 6c 6f 62 22 29 3b 0a 20 20 77 68 69 6c  M blob");.  whil
3910: 65 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d  e( db_step(&q)==
3920: 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
3930: 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70    blob_appendf(p
3940: 58 66 65 72 2d 3e 70 4f 75 74 2c 20 22 69 67 6f  Xfer->pOut, "igo
3950: 74 20 25 73 5c 6e 22 2c 20 64 62 5f 63 6f 6c 75  t %s\n", db_colu
3960: 6d 6e 5f 74 65 78 74 28 26 71 2c 20 30 29 29 3b  mn_text(&q, 0));
3970: 0a 20 20 7d 0a 20 20 64 62 5f 66 69 6e 61 6c 69  .  }.  db_finali
3980: 7a 65 28 26 71 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ze(&q);.}../*.**
3990: 20 49 66 20 74 68 69 73 20 76 61 72 69 61 62 6c   If this variabl
39a0: 65 20 69 73 20 73 65 74 2c 20 64 69 73 61 62 6c  e is set, disabl
39b0: 65 20 6c 6f 67 69 6e 20 63 68 65 63 6b 73 2e 20  e login checks. 
39c0: 20 55 73 65 64 20 66 6f 72 20 64 65 62 75 67 67   Used for debugg
39d0: 69 6e 67 0a 2a 2a 20 6f 6e 6c 79 2e 0a 2a 2f 0a  ing.** only..*/.
39e0: 73 74 61 74 69 63 20 69 6e 74 20 64 69 73 61 62  static int disab
39f0: 6c 65 4c 6f 67 69 6e 20 3d 20 30 3b 0a 0a 2f 2a  leLogin = 0;../*
3a00: 0a 2a 2a 20 57 45 42 50 41 47 45 3a 20 78 66 65  .** WEBPAGE: xfe
3a10: 72 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  r.**.** This is 
3a20: 74 68 65 20 74 72 61 6e 73 66 65 72 20 68 61 6e  the transfer han
3a30: 64 6c 65 72 20 6f 6e 20 74 68 65 20 73 65 72 76  dler on the serv
3a40: 65 72 20 73 69 64 65 2e 20 20 54 68 65 20 74 72  er side.  The tr
3a50: 61 6e 73 66 65 72 0a 2a 2a 20 6d 65 73 73 61 67  ansfer.** messag
3a60: 65 20 68 61 73 20 62 65 65 6e 20 75 6e 63 6f 6d  e has been uncom
3a70: 70 72 65 73 73 65 64 20 61 6e 64 20 70 6c 61 63  pressed and plac
3a80: 65 64 20 69 6e 20 74 68 65 20 67 2e 63 67 69 49  ed in the g.cgiI
3a90: 6e 20 62 6c 6f 62 2e 0a 2a 2a 20 50 72 6f 63 65  n blob..** Proce
3aa0: 73 73 20 74 68 69 73 20 6d 65 73 73 61 67 65 20  ss this message 
3ab0: 61 6e 64 20 66 6f 72 6d 20 61 6e 20 61 70 70 72  and form an appr
3ac0: 6f 70 72 69 61 74 65 20 72 65 70 6c 79 2e 0a 2a  opriate reply..*
3ad0: 2f 0a 76 6f 69 64 20 70 61 67 65 5f 78 66 65 72  /.void page_xfer
3ae0: 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 69 73  (void){.  int is
3af0: 50 75 6c 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 20  Pull = 0;.  int 
3b00: 69 73 50 75 73 68 20 3d 20 30 3b 0a 20 20 69 6e  isPush = 0;.  in
3b10: 74 20 6e 45 72 72 20 3d 20 30 3b 0a 20 20 58 66  t nErr = 0;.  Xf
3b20: 65 72 20 78 66 65 72 3b 0a 20 20 69 6e 74 20 64  er xfer;.  int d
3b30: 65 6c 74 61 46 6c 61 67 20 3d 20 30 3b 0a 20 20  eltaFlag = 0;.  
3b40: 69 6e 74 20 69 73 43 6c 6f 6e 65 20 3d 20 30 3b  int isClone = 0;
3b50: 0a 20 20 69 6e 74 20 6e 47 69 6d 6d 65 20 3d 20  .  int nGimme = 
3b60: 30 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 26 78 66  0;..  memset(&xf
3b70: 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 78 66  er, 0, sizeof(xf
3b80: 65 72 29 29 3b 0a 20 20 62 6c 6f 62 61 72 72 61  er));.  blobarra
3b90: 79 5f 7a 65 72 6f 28 78 66 65 72 2e 61 54 6f 6b  y_zero(xfer.aTok
3ba0: 65 6e 2c 20 63 6f 75 6e 74 28 78 66 65 72 2e 61  en, count(xfer.a
3bb0: 54 6f 6b 65 6e 29 29 3b 0a 20 20 63 67 69 5f 73  Token));.  cgi_s
3bc0: 65 74 5f 63 6f 6e 74 65 6e 74 5f 74 79 70 65 28  et_content_type(
3bd0: 67 2e 7a 43 6f 6e 74 65 6e 74 54 79 70 65 29 3b  g.zContentType);
3be0: 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 78 66  .  blob_zero(&xf
3bf0: 65 72 2e 65 72 72 29 3b 0a 20 20 78 66 65 72 2e  er.err);.  xfer.
3c00: 70 49 6e 20 3d 20 26 67 2e 63 67 69 49 6e 3b 0a  pIn = &g.cgiIn;.
3c10: 20 20 78 66 65 72 2e 70 4f 75 74 20 3d 20 63 67    xfer.pOut = cg
3c20: 69 5f 6f 75 74 70 75 74 5f 62 6c 6f 62 28 29 3b  i_output_blob();
3c30: 0a 20 20 78 66 65 72 2e 6d 78 53 65 6e 64 20 3d  .  xfer.mxSend =
3c40: 20 64 62 5f 67 65 74 5f 69 6e 74 28 22 6d 61 78   db_get_int("max
3c50: 2d 64 6f 77 6e 6c 6f 61 64 22 2c 20 35 30 30 30  -download", 5000
3c60: 30 30 30 29 3b 0a 0a 20 20 64 62 5f 62 65 67 69  000);..  db_begi
3c70: 6e 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 29 3b  n_transaction();
3c80: 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63  .  db_multi_exec
3c90: 28 0a 20 20 20 20 20 22 43 52 45 41 54 45 20 54  (.     "CREATE T
3ca0: 45 4d 50 20 54 41 42 4c 45 20 6f 6e 72 65 6d 6f  EMP TABLE onremo
3cb0: 74 65 28 72 69 64 20 49 4e 54 45 47 45 52 20 50  te(rid INTEGER P
3cc0: 52 49 4d 41 52 59 20 4b 45 59 29 3b 22 0a 20 20  RIMARY KEY);".  
3cd0: 29 3b 0a 20 20 77 68 69 6c 65 28 20 62 6c 6f 62  );.  while( blob
3ce0: 5f 6c 69 6e 65 28 78 66 65 72 2e 70 49 6e 2c 20  _line(xfer.pIn, 
3cf0: 26 78 66 65 72 2e 6c 69 6e 65 29 20 29 7b 0a 20  &xfer.line) ){. 
3d00: 20 20 20 78 66 65 72 2e 6e 54 6f 6b 65 6e 20 3d     xfer.nToken =
3d10: 20 62 6c 6f 62 5f 74 6f 6b 65 6e 69 7a 65 28 26   blob_tokenize(&
3d20: 78 66 65 72 2e 6c 69 6e 65 2c 20 78 66 65 72 2e  xfer.line, xfer.
3d30: 61 54 6f 6b 65 6e 2c 20 63 6f 75 6e 74 28 78 66  aToken, count(xf
3d40: 65 72 2e 61 54 6f 6b 65 6e 29 29 3b 0a 0a 20 20  er.aToken));..  
3d50: 20 20 2f 2a 20 20 20 66 69 6c 65 20 55 55 49 44    /*   file UUID
3d60: 20 53 49 5a 45 20 5c 6e 20 43 4f 4e 54 45 4e 54   SIZE \n CONTENT
3d70: 0a 20 20 20 20 2a 2a 20 20 20 66 69 6c 65 20 55  .    **   file U
3d80: 55 49 44 20 44 45 4c 54 41 53 52 43 20 53 49 5a  UID DELTASRC SIZ
3d90: 45 20 5c 6e 20 43 4f 4e 54 45 4e 54 0a 20 20 20  E \n CONTENT.   
3da0: 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 63 63 65 70   **.    ** Accep
3db0: 74 20 61 20 66 69 6c 65 20 66 72 6f 6d 20 74 68  t a file from th
3dc0: 65 20 63 6c 69 65 6e 74 2e 0a 20 20 20 20 2a 2f  e client..    */
3dd0: 0a 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71  .    if( blob_eq
3de0: 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d  (&xfer.aToken[0]
3df0: 2c 20 22 66 69 6c 65 22 29 20 29 7b 0a 20 20 20  , "file") ){.   
3e00: 20 20 20 69 66 28 20 21 69 73 50 75 73 68 20 29     if( !isPush )
3e10: 7b 0a 20 20 20 20 20 20 20 20 63 67 69 5f 72 65  {.        cgi_re
3e20: 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a 20  set_content();. 
3e30: 20 20 20 20 20 20 20 40 20 65 72 72 6f 72 20 6e         @ error n
3e40: 6f 74 5c 73 61 75 74 68 6f 72 69 7a 65 64 5c 73  ot\sauthorized\s
3e50: 74 6f 5c 73 77 72 69 74 65 0a 20 20 20 20 20 20  to\swrite.      
3e60: 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20    nErr++;.      
3e70: 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
3e80: 0a 20 20 20 20 20 20 78 66 65 72 5f 61 63 63 65  .      xfer_acce
3e90: 70 74 5f 66 69 6c 65 28 26 78 66 65 72 29 3b 0a  pt_file(&xfer);.
3ea0: 20 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 73        if( blob_s
3eb0: 69 7a 65 28 26 78 66 65 72 2e 65 72 72 29 20 29  ize(&xfer.err) )
3ec0: 7b 0a 20 20 20 20 20 20 20 20 63 67 69 5f 72 65  {.        cgi_re
3ed0: 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a 20  set_content();. 
3ee0: 20 20 20 20 20 20 20 40 20 65 72 72 6f 72 20 25         @ error %
3ef0: 54 28 62 6c 6f 62 5f 73 74 72 28 26 78 66 65 72  T(blob_str(&xfer
3f00: 2e 65 72 72 29 29 0a 20 20 20 20 20 20 20 20 6e  .err)).        n
3f10: 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 62  Err++;.        b
3f20: 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
3f30: 20 20 7d 65 6c 73 65 0a 0a 20 20 20 20 2f 2a 20    }else..    /* 
3f40: 20 20 67 69 6d 6d 65 20 55 55 49 44 0a 20 20 20    gimme UUID.   
3f50: 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 6c 69 65 6e   **.    ** Clien
3f60: 74 20 69 73 20 72 65 71 75 65 73 74 69 6e 67 20  t is requesting 
3f70: 61 20 66 69 6c 65 2e 20 20 53 65 6e 64 20 69 74  a file.  Send it
3f80: 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
3f90: 20 62 6c 6f 62 5f 65 71 28 26 78 66 65 72 2e 61   blob_eq(&xfer.a
3fa0: 54 6f 6b 65 6e 5b 30 5d 2c 20 22 67 69 6d 6d 65  Token[0], "gimme
3fb0: 22 29 0a 20 20 20 20 20 26 26 20 78 66 65 72 2e  ").     && xfer.
3fc0: 6e 54 6f 6b 65 6e 3d 3d 32 0a 20 20 20 20 20 26  nToken==2.     &
3fd0: 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26  & blob_is_uuid(&
3fe0: 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d 29 0a  xfer.aToken[1]).
3ff0: 20 20 20 20 29 7b 0a 20 20 20 20 20 20 6e 47 69      ){.      nGi
4000: 6d 6d 65 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28  mme++;.      if(
4010: 20 69 73 50 75 6c 6c 20 29 7b 0a 20 20 20 20 20   isPull ){.     
4020: 20 20 20 69 6e 74 20 72 69 64 20 3d 20 72 69 64     int rid = rid
4030: 5f 66 72 6f 6d 5f 75 75 69 64 28 26 78 66 65 72  _from_uuid(&xfer
4040: 2e 61 54 6f 6b 65 6e 5b 31 5d 2c 20 30 29 3b 0a  .aToken[1], 0);.
4050: 20 20 20 20 20 20 20 20 69 66 28 20 72 69 64 20          if( rid 
4060: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 65 6e  ){.          sen
4070: 64 5f 66 69 6c 65 28 26 78 66 65 72 2c 20 72 69  d_file(&xfer, ri
4080: 64 2c 20 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b  d, &xfer.aToken[
4090: 31 5d 2c 20 64 65 6c 74 61 46 6c 61 67 29 3b 0a  1], deltaFlag);.
40a0: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
40b0: 7d 0a 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20  }.    }else..   
40c0: 20 2f 2a 20 20 20 69 67 6f 74 20 55 55 49 44 0a   /*   igot UUID.
40d0: 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 6c      **.    ** Cl
40e0: 69 65 6e 74 20 61 6e 6e 6f 75 6e 63 65 73 20 74  ient announces t
40f0: 68 61 74 20 69 74 20 68 61 73 20 61 20 70 61 72  hat it has a par
4100: 74 69 63 75 6c 61 72 20 66 69 6c 65 2e 0a 20 20  ticular file..  
4110: 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 78 66 65    */.    if( xfe
4120: 72 2e 6e 54 6f 6b 65 6e 3d 3d 32 0a 20 20 20 20  r.nToken==2.    
4130: 20 26 26 20 62 6c 6f 62 5f 65 71 28 26 78 66 65   && blob_eq(&xfe
4140: 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 69 67  r.aToken[0], "ig
4150: 6f 74 22 29 0a 20 20 20 20 20 26 26 20 62 6c 6f  ot").     && blo
4160: 62 5f 69 73 5f 75 75 69 64 28 26 78 66 65 72 2e  b_is_uuid(&xfer.
4170: 61 54 6f 6b 65 6e 5b 31 5d 29 0a 20 20 20 20 29  aToken[1]).    )
4180: 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73 50 75  {.      if( isPu
4190: 73 68 20 29 7b 0a 20 20 20 20 20 20 20 20 72 69  sh ){.        ri
41a0: 64 5f 66 72 6f 6d 5f 75 75 69 64 28 26 78 66 65  d_from_uuid(&xfe
41b0: 72 2e 61 54 6f 6b 65 6e 5b 31 5d 2c 20 31 29 3b  r.aToken[1], 1);
41c0: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
41d0: 73 65 0a 20 20 0a 20 20 20 20 0a 20 20 20 20 2f  se.  .    .    /
41e0: 2a 20 20 20 20 70 75 6c 6c 20 20 53 45 52 56 45  *    pull  SERVE
41f0: 52 43 4f 44 45 20 20 50 52 4f 4a 45 43 54 43 4f  RCODE  PROJECTCO
4200: 44 45 0a 20 20 20 20 2a 2a 20 20 20 20 70 75 73  DE.    **    pus
4210: 68 20 20 53 45 52 56 45 52 43 4f 44 45 20 20 50  h  SERVERCODE  P
4220: 52 4f 4a 45 43 54 43 4f 44 45 0a 20 20 20 20 2a  ROJECTCODE.    *
4230: 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 63 6c 69  *.    ** The cli
4240: 65 6e 74 20 77 61 6e 74 73 20 65 69 74 68 65 72  ent wants either
4250: 20 73 65 6e 64 20 6f 72 20 72 65 63 65 69 76 65   send or receive
4260: 2e 20 20 54 68 65 20 73 65 72 76 65 72 20 73 68  .  The server sh
4270: 6f 75 6c 64 0a 20 20 20 20 2a 2a 20 76 65 72 69  ould.    ** veri
4280: 66 79 20 74 68 61 74 20 74 68 65 20 70 72 6f 6a  fy that the proj
4290: 65 63 74 20 63 6f 64 65 20 6d 61 74 63 68 65 73  ect code matches
42a0: 20 61 6e 64 20 74 68 61 74 20 74 68 65 20 73 65   and that the se
42b0: 72 76 65 72 20 63 6f 64 65 0a 20 20 20 20 2a 2a  rver code.    **
42c0: 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 2e   does not match.
42d0: 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
42e0: 78 66 65 72 2e 6e 54 6f 6b 65 6e 3d 3d 33 0a 20  xfer.nToken==3. 
42f0: 20 20 20 20 26 26 20 28 62 6c 6f 62 5f 65 71 28      && (blob_eq(
4300: 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c  &xfer.aToken[0],
4310: 20 22 70 75 6c 6c 22 29 20 7c 7c 20 62 6c 6f 62   "pull") || blob
4320: 5f 65 71 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e  _eq(&xfer.aToken
4330: 5b 30 5d 2c 20 22 70 75 73 68 22 29 29 0a 20 20  [0], "push")).  
4340: 20 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75 75     && blob_is_uu
4350: 69 64 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b  id(&xfer.aToken[
4360: 31 5d 29 0a 20 20 20 20 20 26 26 20 62 6c 6f 62  1]).     && blob
4370: 5f 69 73 5f 75 75 69 64 28 26 78 66 65 72 2e 61  _is_uuid(&xfer.a
4380: 54 6f 6b 65 6e 5b 32 5d 29 0a 20 20 20 20 29 7b  Token[2]).    ){
4390: 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
43a0: 72 20 2a 7a 53 43 6f 64 65 3b 0a 20 20 20 20 20  r *zSCode;.     
43b0: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 43   const char *zPC
43c0: 6f 64 65 3b 0a 0a 20 20 20 20 20 20 7a 53 43 6f  ode;..      zSCo
43d0: 64 65 20 3d 20 64 62 5f 67 65 74 28 22 73 65 72  de = db_get("ser
43e0: 76 65 72 2d 63 6f 64 65 22 2c 20 30 29 3b 0a 20  ver-code", 0);. 
43f0: 20 20 20 20 20 69 66 28 20 7a 53 43 6f 64 65 3d       if( zSCode=
4400: 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6f  =0 ){.        fo
4410: 73 73 69 6c 5f 70 61 6e 69 63 28 22 6d 69 73 73  ssil_panic("miss
4420: 69 6e 67 20 73 65 72 76 65 72 20 63 6f 64 65 22  ing server code"
4430: 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
4440: 20 69 66 28 20 62 6c 6f 62 5f 65 71 5f 73 74 72   if( blob_eq_str
4450: 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d  (&xfer.aToken[1]
4460: 2c 20 7a 53 43 6f 64 65 2c 20 2d 31 29 20 29 7b  , zSCode, -1) ){
4470: 0a 20 20 20 20 20 20 20 20 63 67 69 5f 72 65 73  .        cgi_res
4480: 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20  et_content();.  
4490: 20 20 20 20 20 20 40 20 65 72 72 6f 72 20 73 65        @ error se
44a0: 72 76 65 72 5c 73 6c 6f 6f 70 0a 20 20 20 20 20  rver\sloop.     
44b0: 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20     nErr++;.     
44c0: 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
44d0: 7d 0a 20 20 20 20 20 20 7a 50 43 6f 64 65 20 3d  }.      zPCode =
44e0: 20 64 62 5f 67 65 74 28 22 70 72 6f 6a 65 63 74   db_get("project
44f0: 2d 63 6f 64 65 22 2c 20 30 29 3b 0a 20 20 20 20  -code", 0);.    
4500: 20 20 69 66 28 20 7a 50 43 6f 64 65 3d 3d 30 20    if( zPCode==0 
4510: 29 7b 0a 20 20 20 20 20 20 20 20 66 6f 73 73 69  ){.        fossi
4520: 6c 5f 70 61 6e 69 63 28 22 6d 69 73 73 69 6e 67  l_panic("missing
4530: 20 70 72 6f 6a 65 63 74 20 63 6f 64 65 22 29 3b   project code");
4540: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
4550: 66 28 20 21 62 6c 6f 62 5f 65 71 5f 73 74 72 28  f( !blob_eq_str(
4560: 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 32 5d 2c  &xfer.aToken[2],
4570: 20 7a 50 43 6f 64 65 2c 20 2d 31 29 20 29 7b 0a   zPCode, -1) ){.
4580: 20 20 20 20 20 20 20 20 63 67 69 5f 72 65 73 65          cgi_rese
4590: 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 20  t_content();.   
45a0: 20 20 20 20 20 40 20 65 72 72 6f 72 20 77 72 6f       @ error wro
45b0: 6e 67 5c 73 70 72 6f 6a 65 63 74 0a 20 20 20 20  ng\sproject.    
45c0: 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20      nErr++;.    
45d0: 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
45e0: 20 7d 0a 20 20 20 20 20 20 6c 6f 67 69 6e 5f 63   }.      login_c
45f0: 68 65 63 6b 5f 63 72 65 64 65 6e 74 69 61 6c 73  heck_credentials
4600: 28 29 3b 0a 20 20 20 20 20 20 69 66 28 20 62 6c  ();.      if( bl
4610: 6f 62 5f 65 71 28 26 78 66 65 72 2e 61 54 6f 6b  ob_eq(&xfer.aTok
4620: 65 6e 5b 30 5d 2c 20 22 70 75 6c 6c 22 29 20 29  en[0], "pull") )
4630: 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 67  {.        if( !g
4640: 2e 6f 6b 52 65 61 64 20 29 7b 0a 20 20 20 20 20  .okRead ){.     
4650: 20 20 20 20 20 63 67 69 5f 72 65 73 65 74 5f 63       cgi_reset_c
4660: 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 20 20 20 20  ontent();.      
4670: 20 20 20 20 40 20 65 72 72 6f 72 20 6e 6f 74 5c      @ error not\
4680: 73 61 75 74 68 6f 72 69 7a 65 64 5c 73 74 6f 5c  sauthorized\sto\
4690: 73 72 65 61 64 0a 20 20 20 20 20 20 20 20 20 20  sread.          
46a0: 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20  nErr++;.        
46b0: 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
46c0: 20 7d 0a 20 20 20 20 20 20 20 20 69 73 50 75 6c   }.        isPul
46d0: 6c 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c  l = 1;.      }el
46e0: 73 65 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  se{.        if( 
46f0: 21 67 2e 6f 6b 57 72 69 74 65 20 29 7b 0a 20 20  !g.okWrite ){.  
4700: 20 20 20 20 20 20 20 20 69 66 28 20 21 69 73 50          if( !isP
4710: 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ull ){.         
4720: 20 20 20 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e     cgi_reset_con
4730: 74 65 6e 74 28 29 3b 0a 20 20 20 20 20 20 20 20  tent();.        
4740: 20 20 20 20 40 20 65 72 72 6f 72 20 6e 6f 74 5c      @ error not\
4750: 73 61 75 74 68 6f 72 69 7a 65 64 5c 73 74 6f 5c  sauthorized\sto\
4760: 73 77 72 69 74 65 0a 20 20 20 20 20 20 20 20 20  swrite.         
4770: 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20     nErr++;.     
4780: 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
4790: 20 20 20 20 20 20 20 20 40 20 6d 65 73 73 61 67          @ messag
47a0: 65 20 70 75 6c 6c 5c 73 6f 6e 6c 79 5c 73 2d 5c  e pull\sonly\s-\
47b0: 73 6e 6f 74 5c 73 61 75 74 68 6f 72 69 7a 65 64  snot\sauthorized
47c0: 5c 73 74 6f 5c 73 70 75 73 68 0a 20 20 20 20 20  \sto\spush.     
47d0: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
47e0: 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
47f0: 69 73 50 75 73 68 20 3d 20 31 3b 0a 20 20 20 20  isPush = 1;.    
4800: 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
4810: 20 20 7d 65 6c 73 65 0a 0a 20 20 20 20 2f 2a 20    }else..    /* 
4820: 20 20 20 63 6c 6f 6e 65 0a 20 20 20 20 2a 2a 0a     clone.    **.
4830: 20 20 20 20 2a 2a 20 54 68 65 20 63 6c 69 65 6e      ** The clien
4840: 74 20 6b 6e 6f 77 73 20 6e 6f 74 68 69 6e 67 2e  t knows nothing.
4850: 20 20 54 65 6c 6c 20 61 6c 6c 2e 0a 20 20 20 20    Tell all..    
4860: 2a 2f 0a 20 20 20 20 69 66 28 20 62 6c 6f 62 5f  */.    if( blob_
4870: 65 71 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b  eq(&xfer.aToken[
4880: 30 5d 2c 20 22 63 6c 6f 6e 65 22 29 20 29 7b 0a  0], "clone") ){.
4890: 20 20 20 20 20 20 6c 6f 67 69 6e 5f 63 68 65 63        login_chec
48a0: 6b 5f 63 72 65 64 65 6e 74 69 61 6c 73 28 29 3b  k_credentials();
48b0: 0a 20 20 20 20 20 20 69 66 28 20 21 67 2e 6f 6b  .      if( !g.ok
48c0: 43 6c 6f 6e 65 20 29 7b 0a 20 20 20 20 20 20 20  Clone ){.       
48d0: 20 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e 74 65   cgi_reset_conte
48e0: 6e 74 28 29 3b 0a 20 20 20 20 20 20 20 20 40 20  nt();.        @ 
48f0: 65 72 72 6f 72 20 6e 6f 74 5c 73 61 75 74 68 6f  error not\sautho
4900: 72 69 7a 65 64 5c 73 74 6f 5c 73 63 6c 6f 6e 65  rized\sto\sclone
4910: 0a 20 20 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b  .        nErr++;
4920: 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
4930: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 73        }.      is
4940: 43 6c 6f 6e 65 20 3d 20 31 3b 0a 20 20 20 20 20  Clone = 1;.     
4950: 20 69 73 50 75 6c 6c 20 3d 20 31 3b 0a 20 20 20   isPull = 1;.   
4960: 20 20 20 64 65 6c 74 61 46 6c 61 67 20 3d 20 31     deltaFlag = 1
4970: 3b 0a 20 20 20 20 20 20 40 20 70 75 73 68 20 25  ;.      @ push %
4980: 73 28 64 62 5f 67 65 74 28 22 73 65 72 76 65 72  s(db_get("server
4990: 2d 63 6f 64 65 22 2c 20 22 78 22 29 29 20 25 73  -code", "x")) %s
49a0: 28 64 62 5f 67 65 74 28 22 70 72 6f 6a 65 63 74  (db_get("project
49b0: 2d 63 6f 64 65 22 2c 20 22 78 22 29 29 0a 20 20  -code", "x")).  
49c0: 20 20 7d 65 6c 73 65 0a 0a 20 20 20 20 2f 2a 20    }else..    /* 
49d0: 20 20 20 6c 6f 67 69 6e 20 20 55 53 45 52 20 20     login  USER  
49e0: 4e 4f 4e 43 45 20 20 53 49 47 4e 41 54 55 52 45  NONCE  SIGNATURE
49f0: 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43  .    **.    ** C
4a00: 68 65 63 6b 20 66 6f 72 20 61 20 76 61 6c 69 64  heck for a valid
4a10: 20 6c 6f 67 69 6e 2e 20 20 54 68 69 73 20 68 61   login.  This ha
4a20: 73 20 74 6f 20 68 61 70 70 65 6e 20 62 65 66 6f  s to happen befo
4a30: 72 65 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65  re anything else
4a40: 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 63 6c 69  ..    ** The cli
4a50: 65 6e 74 20 63 61 6e 20 73 65 6e 64 20 6d 75 6c  ent can send mul
4a60: 74 69 70 6c 65 20 6c 6f 67 69 6e 73 2e 20 20 50  tiple logins.  P
4a70: 65 72 6d 69 73 73 69 6f 6e 73 20 61 72 65 20 63  ermissions are c
4a80: 75 6d 75 6c 61 74 69 76 65 2e 0a 20 20 20 20 2a  umulative..    *
4a90: 2f 0a 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65  /.    if( blob_e
4aa0: 71 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 30  q(&xfer.aToken[0
4ab0: 5d 2c 20 22 6c 6f 67 69 6e 22 29 0a 20 20 20 20  ], "login").    
4ac0: 20 26 26 20 78 66 65 72 2e 6e 54 6f 6b 65 6e 3d   && xfer.nToken=
4ad0: 3d 34 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  =4.    ){.      
4ae0: 69 66 28 20 64 69 73 61 62 6c 65 4c 6f 67 69 6e  if( disableLogin
4af0: 20 29 7b 0a 20 20 20 20 20 20 20 20 67 2e 6f 6b   ){.        g.ok
4b00: 52 65 61 64 20 3d 20 67 2e 6f 6b 57 72 69 74 65  Read = g.okWrite
4b10: 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73   = 1;.      }els
4b20: 65 20 69 66 28 20 63 68 65 63 6b 5f 74 61 69 6c  e if( check_tail
4b30: 5f 68 61 73 68 28 26 78 66 65 72 2e 61 54 6f 6b  _hash(&xfer.aTok
4b40: 65 6e 5b 32 5d 2c 20 78 66 65 72 2e 70 49 6e 29  en[2], xfer.pIn)
4b50: 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63   ){.        chec
4b60: 6b 5f 6c 6f 67 69 6e 28 26 78 66 65 72 2e 61 54  k_login(&xfer.aT
4b70: 6f 6b 65 6e 5b 31 5d 2c 20 26 78 66 65 72 2e 61  oken[1], &xfer.a
4b80: 54 6f 6b 65 6e 5b 32 5d 2c 20 26 78 66 65 72 2e  Token[2], &xfer.
4b90: 61 54 6f 6b 65 6e 5b 33 5d 29 3b 0a 20 20 20 20  aToken[3]);.    
4ba0: 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 0a 20 20    }.    }else.  
4bb0: 20 20 0a 20 20 20 20 2f 2a 20 20 20 20 72 65 71    .    /*    req
4bc0: 63 6f 6e 66 69 67 20 20 4e 41 4d 45 0a 20 20 20  config  NAME.   
4bd0: 20 2a 2a 0a 20 20 20 20 2a 2a 20 52 65 71 75 65   **.    ** Reque
4be0: 73 74 20 61 20 63 6f 6e 66 69 67 75 72 61 74 69  st a configurati
4bf0: 6f 6e 20 76 61 6c 75 65 0a 20 20 20 20 2a 2f 0a  on value.    */.
4c00: 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28      if( blob_eq(
4c10: 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c  &xfer.aToken[0],
4c20: 20 22 72 65 71 63 6f 6e 66 69 67 22 29 0a 20 20   "reqconfig").  
4c30: 20 20 20 26 26 20 78 66 65 72 2e 6e 54 6f 6b 65     && xfer.nToke
4c40: 6e 3d 3d 32 0a 20 20 20 20 29 7b 0a 20 20 20 20  n==2.    ){.    
4c50: 20 20 69 66 28 20 67 2e 6f 6b 52 65 61 64 20 29    if( g.okRead )
4c60: 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a  {.        char *
4c70: 7a 4e 61 6d 65 20 3d 20 62 6c 6f 62 5f 73 74 72  zName = blob_str
4c80: 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d  (&xfer.aToken[1]
4c90: 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  );.        int i
4ca0: 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d  ;.        for(i=
4cb0: 30 3b 20 69 3c 63 6f 75 6e 74 28 61 53 61 66 65  0; i<count(aSafe
4cc0: 43 6f 6e 66 69 67 29 3b 20 69 2b 2b 29 7b 0a 20  Config); i++){. 
4cd0: 20 20 20 20 20 20 20 20 20 69 66 28 20 73 74 72           if( str
4ce0: 63 6d 70 28 61 53 61 66 65 43 6f 6e 66 69 67 5b  cmp(aSafeConfig[
4cf0: 69 5d 2e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29  i].zName, zName)
4d00: 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
4d10: 20 20 20 63 68 61 72 20 2a 7a 56 61 6c 75 65 20     char *zValue 
4d20: 3d 20 64 62 5f 67 65 74 28 7a 4e 61 6d 65 2c 20  = db_get(zName, 
4d30: 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  0);.            
4d40: 69 66 28 20 7a 56 61 6c 75 65 20 29 7b 0a 20 20  if( zValue ){.  
4d50: 20 20 20 20 20 20 20 20 20 20 20 20 62 6c 6f 62              blob
4d60: 5f 61 70 70 65 6e 64 66 28 78 66 65 72 2e 70 4f  _appendf(xfer.pO
4d70: 75 74 2c 20 22 63 6f 6e 66 69 67 20 25 73 20 25  ut, "config %s %
4d80: 64 5c 6e 25 73 5c 6e 22 2c 20 7a 4e 61 6d 65 2c  d\n%s\n", zName,
4d90: 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
4da0: 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 72               str
4db0: 6c 65 6e 28 7a 56 61 6c 75 65 29 2c 20 7a 56 61  len(zValue), zVa
4dc0: 6c 75 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  lue);.          
4dd0: 20 20 20 20 66 72 65 65 28 7a 56 61 6c 75 65 29      free(zValue)
4de0: 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
4df0: 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
4e00: 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  k;.          }. 
4e10: 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
4e20: 0a 20 20 20 20 7d 65 6c 73 65 0a 20 20 20 20 0a  .    }else.    .
4e30: 20 20 20 20 2f 2a 20 20 20 20 63 6f 6f 6b 69 65      /*    cookie
4e40: 20 54 45 58 54 0a 20 20 20 20 2a 2a 0a 20 20 20   TEXT.    **.   
4e50: 20 2a 2a 20 41 20 63 6f 6f 6b 69 65 20 63 6f 6e   ** A cookie con
4e60: 74 61 69 6e 73 20 61 20 61 72 62 69 74 72 61 72  tains a arbitrar
4e70: 79 2d 6c 65 6e 67 74 68 20 61 72 67 75 6d 65 6e  y-length argumen
4e80: 74 20 74 68 61 74 20 69 73 20 73 65 72 76 65 72  t that is server
4e90: 2d 64 65 66 69 6e 65 64 2e 0a 20 20 20 20 2a 2a  -defined..    **
4ea0: 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 6d 75   The argument mu
4eb0: 73 74 20 62 65 20 65 6e 63 6f 64 65 64 20 73 6f  st be encoded so
4ec0: 20 61 73 20 6e 6f 74 20 74 6f 20 63 6f 6e 74 61   as not to conta
4ed0: 69 6e 20 61 6e 79 20 77 68 69 74 65 73 70 61 63  in any whitespac
4ee0: 65 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 65  e..    ** The se
4ef0: 72 76 65 72 20 63 61 6e 20 6f 70 74 69 6f 6e 61  rver can optiona
4f00: 6c 6c 79 20 73 65 6e 64 20 61 20 63 6f 6f 6b 69  lly send a cooki
4f10: 65 20 74 6f 20 74 68 65 20 63 6c 69 65 6e 74 2e  e to the client.
4f20: 20 20 54 68 65 20 63 6c 69 65 6e 74 0a 20 20 20    The client.   
4f30: 20 2a 2a 20 6d 69 67 68 74 20 74 68 65 6e 20 72   ** might then r
4f40: 65 74 75 72 6e 20 74 68 65 20 73 61 6d 65 20 63  eturn the same c
4f50: 6f 6f 6b 69 65 20 62 61 63 6b 20 74 6f 20 74 68  ookie back to th
4f60: 65 20 73 65 72 76 65 72 20 6f 6e 20 69 74 73 20  e server on its 
4f70: 6e 65 78 74 0a 20 20 20 20 2a 2a 20 63 6f 6d 6d  next.    ** comm
4f80: 75 6e 69 63 61 74 69 6f 6e 2e 20 20 54 68 65 20  unication.  The 
4f90: 63 6f 6f 6b 69 65 20 6d 69 67 68 74 20 72 65 63  cookie might rec
4fa0: 6f 72 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ord information 
4fb0: 74 68 61 74 20 68 65 6c 70 73 0a 20 20 20 20 2a  that helps.    *
4fc0: 2a 20 74 68 65 20 73 65 72 76 65 72 20 6f 70 74  * the server opt
4fd0: 69 6d 69 7a 65 20 61 20 70 75 73 68 20 6f 72 20  imize a push or 
4fe0: 70 75 6c 6c 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  pull..    **.   
4ff0: 20 2a 2a 20 54 68 65 20 63 6c 69 65 6e 74 20 69   ** The client i
5000: 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74  s not required t
5010: 6f 20 72 65 74 75 72 6e 20 61 20 63 6f 6f 6b 69  o return a cooki
5020: 65 2e 20 20 53 6f 20 74 68 65 20 73 65 72 76 65  e.  So the serve
5030: 72 0a 20 20 20 20 2a 2a 20 6d 75 73 74 20 6e 6f  r.    ** must no
5040: 74 20 64 65 70 65 6e 64 20 6f 6e 20 74 68 65 20  t depend on the 
5050: 63 6f 6f 6b 69 65 2e 20 20 54 68 65 20 63 6f 6f  cookie.  The coo
5060: 6b 69 65 20 73 68 6f 75 6c 64 20 62 65 20 61 6e  kie should be an
5070: 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 0a 20 20   optimization.  
5080: 20 20 2a 2a 20 6f 6e 6c 79 2e 20 20 54 68 65 20    ** only.  The 
5090: 63 6c 69 65 6e 74 20 6d 69 67 68 74 20 61 6c 73  client might als
50a0: 6f 20 73 65 6e 64 20 61 20 63 6f 6f 6b 69 65 20  o send a cookie 
50b0: 74 68 61 74 20 63 61 6d 65 20 66 72 6f 6d 20 61  that came from a
50c0: 20 64 69 66 66 65 72 65 6e 74 0a 20 20 20 20 2a   different.    *
50d0: 2a 20 73 65 72 76 65 72 2e 20 20 53 6f 20 74 68  * server.  So th
50e0: 65 20 73 65 72 76 65 72 20 6d 75 73 74 20 62 65  e server must be
50f0: 20 70 72 65 70 61 72 65 64 20 74 6f 20 64 69 73   prepared to dis
5100: 74 69 6e 67 75 69 73 68 20 69 74 73 20 6f 77 6e  tinguish its own
5110: 20 63 6f 6f 6b 69 65 0a 20 20 20 20 2a 2a 20 66   cookie.    ** f
5120: 72 6f 6d 20 63 6f 6f 6b 69 65 73 20 6f 72 69 67  rom cookies orig
5130: 69 6e 61 74 69 6e 67 20 66 72 6f 6d 20 6f 74 68  inating from oth
5140: 65 72 20 73 65 72 76 65 72 73 2e 20 20 54 68 65  er servers.  The
5150: 20 63 6c 69 65 6e 74 20 6d 69 67 68 74 20 73 65   client might se
5160: 6e 64 0a 20 20 20 20 2a 2a 20 62 61 63 6b 20 73  nd.    ** back s
5170: 65 76 65 72 61 6c 20 64 69 66 66 65 72 65 6e 74  everal different
5180: 20 63 6f 6f 6b 69 65 73 20 74 6f 20 74 68 65 20   cookies to the 
5190: 73 65 72 76 65 72 2e 20 20 54 68 65 20 73 65 72  server.  The ser
51a0: 76 65 72 20 73 68 6f 75 6c 64 20 62 65 0a 20 20  ver should be.  
51b0: 20 20 2a 2a 20 70 72 65 70 61 72 65 64 20 74 6f    ** prepared to
51c0: 20 73 69 66 74 20 74 68 72 6f 75 67 68 20 74 68   sift through th
51d0: 65 20 63 6f 6f 6b 69 65 73 20 61 6e 64 20 70 69  e cookies and pi
51e0: 63 6b 20 74 68 65 20 6f 6e 65 20 74 68 61 74 20  ck the one that 
51f0: 69 74 20 77 61 6e 74 73 2e 0a 20 20 20 20 2a 2f  it wants..    */
5200: 0a 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71  .    if( blob_eq
5210: 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d  (&xfer.aToken[0]
5220: 2c 20 22 63 6f 6f 6b 69 65 22 29 20 26 26 20 78  , "cookie") && x
5230: 66 65 72 2e 6e 54 6f 6b 65 6e 3d 3d 32 20 29 7b  fer.nToken==2 ){
5240: 0a 20 20 20 20 20 20 2f 2a 20 50 72 6f 63 65 73  .      /* Proces
5250: 73 20 74 68 65 20 63 6f 6f 6b 69 65 20 2a 2f 0a  s the cookie */.
5260: 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20 20 2f      }else..    /
5270: 2a 20 55 6e 6b 6e 6f 77 6e 20 6d 65 73 73 61 67  * Unknown messag
5280: 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7b 0a 20  e.    */.    {. 
5290: 20 20 20 20 20 63 67 69 5f 72 65 73 65 74 5f 63       cgi_reset_c
52a0: 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 20 20 20 20  ontent();.      
52b0: 40 20 65 72 72 6f 72 20 62 61 64 5c 73 63 6f 6d  @ error bad\scom
52c0: 6d 61 6e 64 3a 5c 73 25 46 28 62 6c 6f 62 5f 73  mand:\s%F(blob_s
52d0: 74 72 28 26 78 66 65 72 2e 6c 69 6e 65 29 29 0a  tr(&xfer.line)).
52e0: 20 20 20 20 7d 0a 20 20 20 20 62 6c 6f 62 61 72      }.    blobar
52f0: 72 61 79 5f 72 65 73 65 74 28 78 66 65 72 2e 61  ray_reset(xfer.a
5300: 54 6f 6b 65 6e 2c 20 78 66 65 72 2e 6e 54 6f 6b  Token, xfer.nTok
5310: 65 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69  en);.  }.  if( i
5320: 73 50 75 73 68 20 29 7b 0a 20 20 20 20 72 65 71  sPush ){.    req
5330: 75 65 73 74 5f 70 68 61 6e 74 6f 6d 73 28 26 78  uest_phantoms(&x
5340: 66 65 72 2c 20 35 30 30 29 3b 0a 20 20 7d 0a 20  fer, 500);.  }. 
5350: 20 69 66 28 20 69 73 43 6c 6f 6e 65 20 26 26 20   if( isClone && 
5360: 6e 47 69 6d 6d 65 3d 3d 30 20 29 7b 0a 20 20 20  nGimme==0 ){.   
5370: 20 2f 2a 20 54 68 65 20 69 6e 69 74 69 61 6c 20   /* The initial 
5380: 22 63 6c 6f 6e 65 22 20 6d 65 73 73 61 67 65 20  "clone" message 
5390: 66 72 6f 6d 20 63 6c 69 65 6e 74 20 74 6f 20 73  from client to s
53a0: 65 72 76 65 72 20 63 6f 6e 74 61 69 6e 73 20 6e  erver contains n
53b0: 6f 0a 20 20 20 20 2a 2a 20 22 67 69 6d 6d 65 22  o.    ** "gimme"
53c0: 20 63 61 72 64 73 2e 20 4f 6e 20 74 68 61 74 20   cards. On that 
53d0: 69 6e 69 74 69 61 6c 20 6d 65 73 73 61 67 65 2c  initial message,
53e0: 20 73 65 6e 64 20 74 68 65 20 63 6c 69 65 6e 74   send the client
53f0: 20 61 6e 20 22 69 67 6f 74 22 0a 20 20 20 20 2a   an "igot".    *
5400: 2a 20 63 61 72 64 20 66 6f 72 20 65 76 65 72 79  * card for every
5410: 20 61 72 74 69 66 61 63 74 20 63 75 72 72 65 6e   artifact curren
5420: 74 6c 79 20 69 6e 20 74 68 65 20 72 65 73 70 6f  tly in the respo
5430: 73 69 74 6f 72 79 2e 20 20 54 68 69 73 20 77 69  sitory.  This wi
5440: 6c 6c 0a 20 20 20 20 2a 2a 20 63 61 75 73 65 20  ll.    ** cause 
5450: 74 68 65 20 63 6c 69 65 6e 74 20 74 6f 20 63 72  the client to cr
5460: 65 61 74 65 20 70 68 61 6e 74 6f 6d 73 20 66 6f  eate phantoms fo
5470: 72 20 61 6c 6c 20 61 72 74 69 66 61 63 74 73 2c  r all artifacts,
5480: 20 77 68 69 63 68 20 77 69 6c 6c 0a 20 20 20 20   which will.    
5490: 2a 2a 20 69 6e 20 74 75 72 6e 20 6d 61 6b 65 20  ** in turn make 
54a0: 73 75 72 65 20 74 68 61 74 20 74 68 65 20 65 6e  sure that the en
54b0: 74 69 72 65 20 72 65 70 6f 73 69 74 6f 72 79 20  tire repository 
54c0: 69 73 20 73 65 6e 74 20 65 66 66 69 63 69 65 6e  is sent efficien
54d0: 74 6c 79 0a 20 20 20 20 2a 2a 20 61 6e 64 20 65  tly.    ** and e
54e0: 78 70 65 64 69 74 69 6f 75 73 6c 79 2e 0a 20 20  xpeditiously..  
54f0: 20 20 2a 2f 0a 20 20 20 20 73 65 6e 64 5f 61 6c    */.    send_al
5500: 6c 28 26 78 66 65 72 29 3b 0a 20 20 7d 65 6c 73  l(&xfer);.  }els
5510: 65 20 69 66 28 20 69 73 50 75 6c 6c 20 29 7b 0a  e if( isPull ){.
5520: 20 20 20 20 63 72 65 61 74 65 5f 63 6c 75 73 74      create_clust
5530: 65 72 28 29 3b 0a 20 20 20 20 73 65 6e 64 5f 75  er();.    send_u
5540: 6e 63 6c 75 73 74 65 72 65 64 28 26 78 66 65 72  nclustered(&xfer
5550: 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 65 6e 64 5f  );.  }.  db_end_
5560: 74 72 61 6e 73 61 63 74 69 6f 6e 28 30 29 3b 0a  transaction(0);.
5570: 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44  }../*.** COMMAND
5580: 3a 20 74 65 73 74 2d 78 66 65 72 0a 2a 2a 0a 2a  : test-xfer.**.*
5590: 2a 20 54 68 69 73 20 63 6f 6d 6d 61 6e 64 20 69  * This command i
55a0: 73 20 75 73 65 64 20 66 6f 72 20 64 65 62 75 67  s used for debug
55b0: 67 69 6e 67 20 74 68 65 20 73 65 72 76 65 72 2e  ging the server.
55c0: 20 20 54 68 65 72 65 20 69 73 20 61 20 73 69 6e    There is a sin
55d0: 67 6c 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20  gle.** argument 
55e0: 77 68 69 63 68 20 69 73 20 74 68 65 20 75 6e 63  which is the unc
55f0: 6f 6d 70 72 65 73 73 65 64 20 63 6f 6e 74 65 6e  ompressed conten
5600: 74 20 6f 66 20 61 6e 20 22 78 66 65 72 22 20 6d  t of an "xfer" m
5610: 65 73 73 61 67 65 0a 2a 2a 20 66 72 6f 6d 20 63  essage.** from c
5620: 6c 69 65 6e 74 20 74 6f 20 73 65 72 76 65 72 2e  lient to server.
5630: 20 20 54 68 69 73 20 63 6f 6d 6d 61 6e 64 20 69    This command i
5640: 6e 74 65 72 70 72 65 74 73 20 74 68 61 74 20 6d  nterprets that m
5650: 65 73 73 61 67 65 20 61 73 0a 2a 2a 20 69 66 20  essage as.** if 
5660: 68 61 64 20 62 65 65 6e 20 72 65 63 65 69 76 65  had been receive
5670: 64 20 62 79 20 74 68 65 20 73 65 72 76 65 72 2e  d by the server.
5680: 0a 2a 2a 0a 2a 2a 20 4f 6e 20 74 68 65 20 63 6c  .**.** On the cl
5690: 69 65 6e 74 20 73 69 64 65 2c 20 72 75 6e 3a 0a  ient side, run:.
56a0: 2a 2a 0a 2a 2a 20 20 20 20 20 20 66 6f 73 73 69  **.**      fossi
56b0: 6c 20 70 75 73 68 20 68 74 74 70 3a 2f 2f 62 6f  l push http://bo
56c0: 67 75 73 2f 20 2d 2d 68 74 74 70 74 72 61 63 65  gus/ --httptrace
56d0: 0a 2a 2a 0a 2a 2a 20 4f 72 20 61 20 73 69 6d 69  .**.** Or a simi
56e0: 6c 61 72 20 63 6f 6d 6d 61 6e 64 20 74 6f 20 70  lar command to p
56f0: 72 6f 76 69 64 65 20 74 68 65 20 6f 75 74 70 75  rovide the outpu
5700: 74 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20  t.  The content 
5710: 6f 66 20 74 68 65 0a 2a 2a 20 6d 65 73 73 61 67  of the.** messag
5720: 65 20 77 69 6c 6c 20 61 70 70 65 61 72 20 6f 6e  e will appear on
5730: 20 73 74 61 6e 64 61 72 64 20 6f 75 74 70 75 74   standard output
5740: 2e 20 20 43 61 70 74 75 72 65 20 74 68 69 73 20  .  Capture this 
5750: 6d 65 73 73 61 67 65 0a 2a 2a 20 69 6e 74 6f 20  message.** into 
5760: 61 20 66 69 6c 65 20 6e 61 6d 65 64 20 28 66 6f  a file named (fo
5770: 72 20 65 78 61 6d 70 6c 65 29 20 6f 75 74 2e 74  r example) out.t
5780: 78 74 2e 20 20 54 68 65 6e 20 72 75 6e 20 74 68  xt.  Then run th
5790: 65 0a 2a 2a 20 73 65 72 76 65 72 20 69 6e 20 67  e.** server in g
57a0: 64 62 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 67 64  db:.**.**     gd
57b0: 62 20 66 6f 73 73 69 6c 0a 2a 2a 20 20 20 20 20  b fossil.**     
57c0: 72 20 74 65 73 74 2d 78 66 65 72 20 6f 75 74 2e  r test-xfer out.
57d0: 74 78 74 0a 2a 2f 0a 76 6f 69 64 20 63 6d 64 5f  txt.*/.void cmd_
57e0: 74 65 73 74 5f 78 66 65 72 28 76 6f 69 64 29 7b  test_xfer(void){
57f0: 0a 20 20 69 6e 74 20 6e 6f 74 55 73 65 64 3b 0a  .  int notUsed;.
5800: 20 20 69 66 28 20 67 2e 61 72 67 63 21 3d 32 20    if( g.argc!=2 
5810: 26 26 20 67 2e 61 72 67 63 21 3d 33 20 29 7b 0a  && g.argc!=3 ){.
5820: 20 20 20 20 75 73 61 67 65 28 22 3f 4d 45 53 53      usage("?MESS
5830: 41 47 45 46 49 4c 45 3f 22 29 3b 0a 20 20 7d 0a  AGEFILE?");.  }.
5840: 20 20 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74    db_must_be_wit
5850: 68 69 6e 5f 74 72 65 65 28 29 3b 0a 20 20 62 6c  hin_tree();.  bl
5860: 6f 62 5f 7a 65 72 6f 28 26 67 2e 63 67 69 49 6e  ob_zero(&g.cgiIn
5870: 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 61 64 5f 66  );.  blob_read_f
5880: 72 6f 6d 5f 66 69 6c 65 28 26 67 2e 63 67 69 49  rom_file(&g.cgiI
5890: 6e 2c 20 67 2e 61 72 67 63 3d 3d 32 20 3f 20 22  n, g.argc==2 ? "
58a0: 2d 22 20 3a 20 67 2e 61 72 67 76 5b 32 5d 29 3b  -" : g.argv[2]);
58b0: 0a 20 20 64 69 73 61 62 6c 65 4c 6f 67 69 6e 20  .  disableLogin 
58c0: 3d 20 31 3b 0a 20 20 70 61 67 65 5f 78 66 65 72  = 1;.  page_xfer
58d0: 28 29 3b 0a 20 20 70 72 69 6e 74 66 28 22 25 73  ();.  printf("%s
58e0: 5c 6e 22 2c 20 63 67 69 5f 65 78 74 72 61 63 74  \n", cgi_extract
58f0: 5f 63 6f 6e 74 65 6e 74 28 26 6e 6f 74 55 73 65  _content(&notUse
5900: 64 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 6f  d));.}../*.** Fo
5910: 72 6d 61 74 20 73 74 72 69 6e 67 73 20 66 6f 72  rmat strings for
5920: 20 70 72 6f 67 72 65 73 73 20 72 65 70 6f 72 74   progress report
5930: 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  ing..*/.static c
5940: 6f 6e 73 74 20 63 68 61 72 20 7a 4c 61 62 65 6c  onst char zLabel
5950: 46 6f 72 6d 61 74 5b 5d 20 3d 20 22 25 2d 31 30  Format[] = "%-10
5960: 73 20 25 31 30 73 20 25 31 30 73 20 25 31 30 73  s %10s %10s %10s
5970: 20 25 31 30 73 5c 6e 22 3b 0a 73 74 61 74 69 63   %10s\n";.static
5980: 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 56 61 6c   const char zVal
5990: 75 65 46 6f 72 6d 61 74 5b 5d 20 3d 20 22 5c 72  ueFormat[] = "\r
59a0: 25 2d 31 30 73 20 25 31 30 64 20 25 31 30 64 20  %-10s %10d %10d 
59b0: 25 31 30 64 20 25 31 30 64 5c 6e 22 3b 0a 0a 0a  %10d %10d\n";...
59c0: 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 6f 20 74 68  /*.** Sync to th
59d0: 65 20 68 6f 73 74 20 69 64 65 6e 74 69 66 69 65  e host identifie
59e0: 64 20 69 6e 20 67 2e 75 72 6c 4e 61 6d 65 20 61  d in g.urlName a
59f0: 6e 64 20 67 2e 75 72 6c 50 61 74 68 2e 20 20 54  nd g.urlPath.  T
5a00: 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69  his.** routine i
5a10: 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20  s called by the 
5a20: 63 6c 69 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 52 65  client..**.** Re
5a30: 63 6f 72 64 73 20 61 72 65 20 70 75 73 68 65 64  cords are pushed
5a40: 20 74 6f 20 74 68 65 20 73 65 72 76 65 72 20 69   to the server i
5a50: 66 20 70 75 73 68 46 6c 61 67 20 69 73 20 74 72  f pushFlag is tr
5a60: 75 65 2e 20 20 52 65 63 6f 72 64 73 0a 2a 2a 20  ue.  Records.** 
5a70: 61 72 65 20 70 75 6c 6c 65 64 20 69 66 20 70 75  are pulled if pu
5a80: 6c 6c 46 6c 61 67 20 69 73 20 74 72 75 65 2e 20  llFlag is true. 
5a90: 20 41 20 66 75 6c 6c 20 73 79 6e 63 20 6f 63 63   A full sync occ
5aa0: 75 72 73 20 69 66 20 62 6f 74 68 20 61 72 65 0a  urs if both are.
5ab0: 2a 2a 20 74 72 75 65 2e 0a 2a 2f 0a 76 6f 69 64  ** true..*/.void
5ac0: 20 63 6c 69 65 6e 74 5f 73 79 6e 63 28 69 6e 74   client_sync(int
5ad0: 20 70 75 73 68 46 6c 61 67 2c 20 69 6e 74 20 70   pushFlag, int p
5ae0: 75 6c 6c 46 6c 61 67 2c 20 69 6e 74 20 63 6c 6f  ullFlag, int clo
5af0: 6e 65 46 6c 61 67 29 7b 0a 20 20 69 6e 74 20 67  neFlag){.  int g
5b00: 6f 20 3d 20 31 3b 20 20 20 20 20 20 20 20 2f 2a  o = 1;        /*
5b10: 20 4c 6f 6f 70 20 75 6e 74 69 6c 20 7a 65 72 6f   Loop until zero
5b20: 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
5b30: 20 2a 7a 53 43 6f 64 65 20 3d 20 64 62 5f 67 65   *zSCode = db_ge
5b40: 74 28 22 73 65 72 76 65 72 2d 63 6f 64 65 22 2c  t("server-code",
5b50: 20 22 78 22 29 3b 0a 20 20 63 6f 6e 73 74 20 63   "x");.  const c
5b60: 68 61 72 20 2a 7a 50 43 6f 64 65 20 3d 20 64 62  har *zPCode = db
5b70: 5f 67 65 74 28 22 70 72 6f 6a 65 63 74 2d 63 6f  _get("project-co
5b80: 64 65 22 2c 20 30 29 3b 0a 20 20 69 6e 74 20 6e  de", 0);.  int n
5b90: 43 61 72 64 20 3d 20 30 3b 20 20 20 20 20 20 20  Card = 0;       
5ba0: 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
5bb0: 61 72 64 73 20 73 65 6e 74 20 6f 72 20 72 65 63  ards sent or rec
5bc0: 65 69 76 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  eived */.  int n
5bd0: 43 79 63 6c 65 20 3d 20 30 3b 20 20 20 20 20 20  Cycle = 0;      
5be0: 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72    /* Number of r
5bf0: 6f 75 6e 64 20 74 72 69 70 73 20 74 6f 20 74 68  ound trips to th
5c00: 65 20 73 65 72 76 65 72 20 2a 2f 0a 20 20 69 6e  e server */.  in
5c10: 74 20 6e 46 69 6c 65 53 65 6e 64 20 3d 20 30 3b  t nFileSend = 0;
5c20: 0a 20 20 69 6e 74 20 6e 46 69 6c 65 52 65 63 76  .  int nFileRecv
5c30: 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  ;          /* Nu
5c40: 6d 62 65 72 20 6f 66 20 66 69 6c 65 73 20 72 65  mber of files re
5c50: 63 65 69 76 65 64 20 2a 2f 0a 20 20 69 6e 74 20  ceived */.  int 
5c60: 6d 78 50 68 61 6e 74 6f 6d 52 65 71 20 3d 20 32  mxPhantomReq = 2
5c70: 30 30 3b 20 2f 2a 20 4d 61 78 20 6e 75 6d 62 65  00; /* Max numbe
5c80: 72 20 6f 66 20 70 68 61 6e 74 6f 6d 73 20 74 6f  r of phantoms to
5c90: 20 72 65 71 75 65 73 74 20 70 65 72 20 63 6f 6d   request per com
5ca0: 6d 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  m */.  const cha
5cb0: 72 20 2a 7a 43 6f 6f 6b 69 65 3b 20 20 20 20 2f  r *zCookie;    /
5cc0: 2a 20 53 65 72 76 65 72 20 63 6f 6f 6b 69 65 20  * Server cookie 
5cd0: 2a 2f 0a 20 20 42 6c 6f 62 20 73 65 6e 64 3b 20  */.  Blob send; 
5ce0: 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 77         /* Text w
5cf0: 65 20 61 72 65 20 73 65 6e 64 69 6e 67 20 74 6f  e are sending to
5d00: 20 74 68 65 20 73 65 72 76 65 72 20 2a 2f 0a 20   the server */. 
5d10: 20 42 6c 6f 62 20 72 65 63 76 3b 20 20 20 20 20   Blob recv;     
5d20: 20 20 20 2f 2a 20 52 65 70 6c 79 20 77 65 20 67     /* Reply we g
5d30: 6f 74 20 62 61 63 6b 20 66 72 6f 6d 20 74 68 65  ot back from the
5d40: 20 73 65 72 76 65 72 20 2a 2f 0a 20 20 58 66 65   server */.  Xfe
5d50: 72 20 78 66 65 72 3b 20 20 20 20 20 20 20 20 2f  r xfer;        /
5d60: 2a 20 54 72 61 6e 73 66 65 72 20 64 61 74 61 20  * Transfer data 
5d70: 2a 2f 0a 0a 20 20 6d 65 6d 73 65 74 28 26 78 66  */..  memset(&xf
5d80: 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 78 66  er, 0, sizeof(xf
5d90: 65 72 29 29 3b 0a 20 20 78 66 65 72 2e 70 49 6e  er));.  xfer.pIn
5da0: 20 3d 20 26 72 65 63 76 3b 0a 20 20 78 66 65 72   = &recv;.  xfer
5db0: 2e 70 4f 75 74 20 3d 20 26 73 65 6e 64 3b 0a 20  .pOut = &send;. 
5dc0: 20 78 66 65 72 2e 6d 78 53 65 6e 64 20 3d 20 64   xfer.mxSend = d
5dd0: 62 5f 67 65 74 5f 69 6e 74 28 22 6d 61 78 2d 75  b_get_int("max-u
5de0: 70 6c 6f 61 64 22 2c 20 32 35 30 30 30 30 29 3b  pload", 250000);
5df0: 0a 0a 20 20 61 73 73 65 72 74 28 20 70 75 73 68  ..  assert( push
5e00: 46 6c 61 67 20 7c 7c 20 70 75 6c 6c 46 6c 61 67  Flag || pullFlag
5e10: 20 7c 7c 20 63 6c 6f 6e 65 46 6c 61 67 20 29 3b   || cloneFlag );
5e20: 0a 20 20 61 73 73 65 72 74 28 20 21 67 2e 75 72  .  assert( !g.ur
5e30: 6c 49 73 46 69 6c 65 20 29 3b 20 20 20 20 20 20  lIsFile );      
5e40: 20 20 20 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79      /* This only
5e50: 20 77 6f 72 6b 73 20 66 6f 72 20 6e 65 74 77 6f   works for netwo
5e60: 72 6b 69 6e 67 20 2a 2f 0a 0a 20 20 64 62 5f 62  rking */..  db_b
5e70: 65 67 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f 6e  egin_transaction
5e80: 28 29 3b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65  ();.  db_multi_e
5e90: 78 65 63 28 0a 20 20 20 20 22 43 52 45 41 54 45  xec(.    "CREATE
5ea0: 20 54 45 4d 50 20 54 41 42 4c 45 20 6f 6e 72 65   TEMP TABLE onre
5eb0: 6d 6f 74 65 28 72 69 64 20 49 4e 54 45 47 45 52  mote(rid INTEGER
5ec0: 20 50 52 49 4d 41 52 59 20 4b 45 59 29 3b 22 0a   PRIMARY KEY);".
5ed0: 20 20 29 3b 0a 20 20 62 6c 6f 62 61 72 72 61 79    );.  blobarray
5ee0: 5f 7a 65 72 6f 28 78 66 65 72 2e 61 54 6f 6b 65  _zero(xfer.aToke
5ef0: 6e 2c 20 63 6f 75 6e 74 28 78 66 65 72 2e 61 54  n, count(xfer.aT
5f00: 6f 6b 65 6e 29 29 3b 0a 20 20 62 6c 6f 62 5f 7a  oken));.  blob_z
5f10: 65 72 6f 28 26 73 65 6e 64 29 3b 0a 20 20 62 6c  ero(&send);.  bl
5f20: 6f 62 5f 7a 65 72 6f 28 26 72 65 63 76 29 3b 0a  ob_zero(&recv);.
5f30: 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 78 66 65    blob_zero(&xfe
5f40: 72 2e 65 72 72 29 3b 0a 20 20 62 6c 6f 62 5f 7a  r.err);.  blob_z
5f50: 65 72 6f 28 26 78 66 65 72 2e 6c 69 6e 65 29 3b  ero(&xfer.line);
5f60: 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 77 61  ..  /*.  ** Alwa
5f70: 79 73 20 62 65 67 69 6e 20 77 69 74 68 20 61 20  ys begin with a 
5f80: 63 6c 6f 6e 65 2c 20 70 75 6c 6c 2c 20 6f 72 20  clone, pull, or 
5f90: 70 75 73 68 20 6d 65 73 73 61 67 65 0a 20 20 2a  push message.  *
5fa0: 2f 0a 20 20 69 66 28 20 63 6c 6f 6e 65 46 6c 61  /.  if( cloneFla
5fb0: 67 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 61 70  g ){.    blob_ap
5fc0: 70 65 6e 64 66 28 26 73 65 6e 64 2c 20 22 63 6c  pendf(&send, "cl
5fd0: 6f 6e 65 5c 6e 22 29 3b 0a 20 20 20 20 70 75 73  one\n");.    pus
5fe0: 68 46 6c 61 67 20 3d 20 30 3b 0a 20 20 20 20 70  hFlag = 0;.    p
5ff0: 75 6c 6c 46 6c 61 67 20 3d 20 30 3b 0a 20 20 20  ullFlag = 0;.   
6000: 20 6e 43 61 72 64 2b 2b 3b 0a 20 20 20 20 2f 2a   nCard++;.    /*
6010: 20 54 42 44 3a 20 52 65 71 75 65 73 74 20 61 6c   TBD: Request al
6020: 6c 20 74 72 61 6e 73 66 65 72 61 62 6c 65 20 63  l transferable c
6030: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 76 61 6c  onfiguration val
6040: 75 65 73 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69  ues */.  }else i
6050: 66 28 20 70 75 6c 6c 46 6c 61 67 20 29 7b 0a 20  f( pullFlag ){. 
6060: 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28     blob_appendf(
6070: 26 73 65 6e 64 2c 20 22 70 75 6c 6c 20 25 73 20  &send, "pull %s 
6080: 25 73 5c 6e 22 2c 20 7a 53 43 6f 64 65 2c 20 7a  %s\n", zSCode, z
6090: 50 43 6f 64 65 29 3b 0a 20 20 20 20 6e 43 61 72  PCode);.    nCar
60a0: 64 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  d++;.  }.  if( p
60b0: 75 73 68 46 6c 61 67 20 29 7b 0a 20 20 20 20 62  ushFlag ){.    b
60c0: 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 73 65 6e  lob_appendf(&sen
60d0: 64 2c 20 22 70 75 73 68 20 25 73 20 25 73 5c 6e  d, "push %s %s\n
60e0: 22 2c 20 7a 53 43 6f 64 65 2c 20 7a 50 43 6f 64  ", zSCode, zPCod
60f0: 65 29 3b 0a 20 20 20 20 6e 43 61 72 64 2b 2b 3b  e);.    nCard++;
6100: 0a 20 20 7d 0a 20 20 70 72 69 6e 74 66 28 7a 4c  .  }.  printf(zL
6110: 61 62 65 6c 46 6f 72 6d 61 74 2c 20 22 22 2c 20  abelFormat, "", 
6120: 22 42 79 74 65 73 22 2c 20 22 43 61 72 64 73 22  "Bytes", "Cards"
6130: 2c 20 22 41 72 74 69 66 61 63 74 73 22 2c 20 22  , "Artifacts", "
6140: 44 65 6c 74 61 73 22 29 3b 0a 0a 20 20 77 68 69  Deltas");..  whi
6150: 6c 65 28 20 67 6f 20 29 7b 0a 20 20 20 20 69 6e  le( go ){.    in
6160: 74 20 6e 65 77 50 68 61 6e 74 6f 6d 20 3d 20 30  t newPhantom = 0
6170: 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 6e 64 20 6d  ;..    /* Send m
6180: 61 6b 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63  ake the most rec
6190: 65 6e 74 6c 79 20 72 65 63 65 69 76 65 64 20 63  ently received c
61a0: 6f 6f 6b 69 65 2e 20 20 4c 65 74 20 74 68 65 20  ookie.  Let the 
61b0: 73 65 72 76 65 72 0a 20 20 20 20 2a 2a 20 66 69  server.    ** fi
61c0: 67 75 72 65 20 6f 75 74 20 69 66 20 74 68 69 73  gure out if this
61d0: 20 69 73 20 61 20 63 6f 6f 6b 69 65 20 74 68 61   is a cookie tha
61e0: 74 20 69 74 20 63 61 72 65 73 20 61 62 6f 75 74  t it cares about
61f0: 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a 43 6f  ..    */.    zCo
6200: 6f 6b 69 65 20 3d 20 64 62 5f 67 65 74 28 22 63  okie = db_get("c
6210: 6f 6f 6b 69 65 22 2c 20 30 29 3b 0a 20 20 20 20  ookie", 0);.    
6220: 69 66 28 20 7a 43 6f 6f 6b 69 65 20 29 7b 0a 20  if( zCookie ){. 
6230: 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64       blob_append
6240: 66 28 26 73 65 6e 64 2c 20 22 63 6f 6f 6b 69 65  f(&send, "cookie
6250: 20 25 73 5c 6e 22 2c 20 7a 43 6f 6f 6b 69 65 29   %s\n", zCookie)
6260: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 20  ;.    }.    .   
6270: 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 67 69 6d   /* Generate gim
6280: 6d 65 20 63 61 72 64 73 20 66 6f 72 20 70 68 61  me cards for pha
6290: 6e 74 6f 6d 73 20 61 6e 64 20 6c 65 61 66 20 63  ntoms and leaf c
62a0: 61 72 64 73 0a 20 20 20 20 2a 2a 20 66 6f 72 20  ards.    ** for 
62b0: 61 6c 6c 20 6c 65 61 76 65 73 2e 0a 20 20 20 20  all leaves..    
62c0: 2a 2f 0a 20 20 20 20 69 66 28 20 70 75 6c 6c 46  */.    if( pullF
62d0: 6c 61 67 20 7c 7c 20 63 6c 6f 6e 65 46 6c 61 67  lag || cloneFlag
62e0: 20 29 7b 0a 20 20 20 20 20 20 72 65 71 75 65 73   ){.      reques
62f0: 74 5f 70 68 61 6e 74 6f 6d 73 28 26 78 66 65 72  t_phantoms(&xfer
6300: 2c 20 6d 78 50 68 61 6e 74 6f 6d 52 65 71 29 3b  , mxPhantomReq);
6310: 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
6320: 75 73 68 46 6c 61 67 20 29 7b 0a 20 20 20 20 20  ushFlag ){.     
6330: 20 73 65 6e 64 5f 75 6e 73 65 6e 74 28 26 78 66   send_unsent(&xf
6340: 65 72 29 3b 0a 20 20 20 20 20 20 6e 43 61 72 64  er);.      nCard
6350: 20 2b 3d 20 73 65 6e 64 5f 75 6e 63 6c 75 73 74   += send_unclust
6360: 65 72 65 64 28 26 78 66 65 72 29 3b 0a 20 20 20  ered(&xfer);.   
6370: 20 7d 0a 0a 20 20 20 20 2f 2a 20 45 78 63 68 61   }..    /* Excha
6380: 6e 67 65 20 6d 65 73 73 61 67 65 73 20 77 69 74  nge messages wit
6390: 68 20 74 68 65 20 73 65 72 76 65 72 20 2a 2f 0a  h the server */.
63a0: 20 20 20 20 6e 46 69 6c 65 53 65 6e 64 20 3d 20      nFileSend = 
63b0: 78 66 65 72 2e 6e 46 69 6c 65 53 65 6e 74 20 2b  xfer.nFileSent +
63c0: 20 78 66 65 72 2e 6e 44 65 6c 74 61 53 65 6e 74   xfer.nDeltaSent
63d0: 3b 0a 20 20 20 20 70 72 69 6e 74 66 28 7a 56 61  ;.    printf(zVa
63e0: 6c 75 65 46 6f 72 6d 61 74 2c 20 22 53 65 6e 64  lueFormat, "Send
63f0: 3a 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  :",.            
6400: 62 6c 6f 62 5f 73 69 7a 65 28 26 73 65 6e 64 29  blob_size(&send)
6410: 2c 20 6e 43 61 72 64 2b 78 66 65 72 2e 6e 47 69  , nCard+xfer.nGi
6420: 6d 6d 65 53 65 6e 74 2b 78 66 65 72 2e 6e 49 47  mmeSent+xfer.nIG
6430: 6f 74 53 65 6e 74 2c 0a 20 20 20 20 20 20 20 20  otSent,.        
6440: 20 20 20 20 78 66 65 72 2e 6e 46 69 6c 65 53 65      xfer.nFileSe
6450: 6e 74 2c 20 78 66 65 72 2e 6e 44 65 6c 74 61 53  nt, xfer.nDeltaS
6460: 65 6e 74 29 3b 0a 23 69 66 20 30 0a 20 20 20 20  ent);.#if 0.    
6470: 70 72 69 6e 74 66 28 22 53 65 6e 74 3a 20 20 20  printf("Sent:   
6480: 20 20 20 25 31 30 64 20 62 79 74 65 73 2c 20 25     %10d bytes, %
6490: 35 64 20 63 61 72 64 73 2c 20 25 35 64 20 66 69  5d cards, %5d fi
64a0: 6c 65 73 20 28 25 64 2b 25 64 29 5c 6e 22 2c 0a  les (%d+%d)\n",.
64b0: 20 20 20 20 20 20 20 20 20 20 20 20 62 6c 6f 62              blob
64c0: 5f 73 69 7a 65 28 26 73 65 6e 64 29 2c 20 6e 43  _size(&send), nC
64d0: 61 72 64 2b 78 66 65 72 2e 6e 47 69 6d 6d 65 53  ard+xfer.nGimmeS
64e0: 65 6e 74 2b 78 66 65 72 2e 6e 49 47 6f 74 53 65  ent+xfer.nIGotSe
64f0: 6e 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  nt,.            
6500: 6e 46 69 6c 65 53 65 6e 64 2c 20 78 66 65 72 2e  nFileSend, xfer.
6510: 6e 46 69 6c 65 53 65 6e 74 2c 20 78 66 65 72 2e  nFileSent, xfer.
6520: 6e 44 65 6c 74 61 53 65 6e 74 29 3b 0a 23 65 6e  nDeltaSent);.#en
6530: 64 69 66 0a 20 20 20 20 6e 43 61 72 64 20 3d 20  dif.    nCard = 
6540: 30 3b 0a 20 20 20 20 78 66 65 72 2e 6e 46 69 6c  0;.    xfer.nFil
6550: 65 53 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20 78  eSent = 0;.    x
6560: 66 65 72 2e 6e 44 65 6c 74 61 53 65 6e 74 20 3d  fer.nDeltaSent =
6570: 20 30 3b 0a 20 20 20 20 78 66 65 72 2e 6e 47 69   0;.    xfer.nGi
6580: 6d 6d 65 53 65 6e 74 20 3d 20 30 3b 0a 20 20 20  mmeSent = 0;.   
6590: 20 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 3b   fflush(stdout);
65a0: 0a 20 20 20 20 68 74 74 70 5f 65 78 63 68 61 6e  .    http_exchan
65b0: 67 65 28 26 73 65 6e 64 2c 20 26 72 65 63 76 29  ge(&send, &recv)
65c0: 3b 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74  ;.    blob_reset
65d0: 28 26 73 65 6e 64 29 3b 0a 0a 20 20 20 20 2f 2a  (&send);..    /*
65e0: 20 42 65 67 69 6e 20 63 6f 6e 73 74 72 75 63 74   Begin construct
65f0: 69 6e 67 20 74 68 65 20 6e 65 78 74 20 6d 65 73  ing the next mes
6600: 73 61 67 65 20 28 77 68 69 63 68 20 6d 69 67 68  sage (which migh
6610: 74 20 6e 65 76 65 72 20 62 65 0a 20 20 20 20 2a  t never be.    *
6620: 2a 20 73 65 6e 74 29 20 62 79 20 62 65 67 69 6e  * sent) by begin
6630: 6e 69 6e 67 20 77 69 74 68 20 74 68 65 20 70 75  ning with the pu
6640: 6c 6c 20 6f 72 20 70 75 73 68 20 63 61 72 64 73  ll or push cards
6650: 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
6660: 70 75 6c 6c 46 6c 61 67 20 29 7b 0a 20 20 20 20  pullFlag ){.    
6670: 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26    blob_appendf(&
6680: 73 65 6e 64 2c 20 22 70 75 6c 6c 20 25 73 20 25  send, "pull %s %
6690: 73 5c 6e 22 2c 20 7a 53 43 6f 64 65 2c 20 7a 50  s\n", zSCode, zP
66a0: 43 6f 64 65 29 3b 0a 20 20 20 20 20 20 6e 43 61  Code);.      nCa
66b0: 72 64 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rd++;.    }.    
66c0: 69 66 28 20 70 75 73 68 46 6c 61 67 20 29 7b 0a  if( pushFlag ){.
66d0: 20 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e        blob_appen
66e0: 64 66 28 26 73 65 6e 64 2c 20 22 70 75 73 68 20  df(&send, "push 
66f0: 25 73 20 25 73 5c 6e 22 2c 20 7a 53 43 6f 64 65  %s %s\n", zSCode
6700: 2c 20 7a 50 43 6f 64 65 29 3b 0a 20 20 20 20 20  , zPCode);.     
6710: 20 6e 43 61 72 64 2b 2b 3b 0a 20 20 20 20 7d 0a   nCard++;.    }.
6720: 0a 20 20 20 20 2f 2a 20 50 72 6f 63 65 73 73 20  .    /* Process 
6730: 74 68 65 20 72 65 70 6c 79 20 74 68 61 74 20 63  the reply that c
6740: 61 6d 65 20 62 61 63 6b 20 66 72 6f 6d 20 74 68  ame back from th
6750: 65 20 73 65 72 76 65 72 20 2a 2f 0a 20 20 20 20  e server */.    
6760: 77 68 69 6c 65 28 20 62 6c 6f 62 5f 6c 69 6e 65  while( blob_line
6770: 28 26 72 65 63 76 2c 20 26 78 66 65 72 2e 6c 69  (&recv, &xfer.li
6780: 6e 65 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ne) ){.      if(
6790: 20 62 6c 6f 62 5f 62 75 66 66 65 72 28 26 78 66   blob_buffer(&xf
67a0: 65 72 2e 6c 69 6e 65 29 5b 30 5d 3d 3d 27 23 27  er.line)[0]=='#'
67b0: 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74   ){.        cont
67c0: 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  inue;.      }.  
67d0: 20 20 20 20 78 66 65 72 2e 6e 54 6f 6b 65 6e 20      xfer.nToken 
67e0: 3d 20 62 6c 6f 62 5f 74 6f 6b 65 6e 69 7a 65 28  = blob_tokenize(
67f0: 26 78 66 65 72 2e 6c 69 6e 65 2c 20 78 66 65 72  &xfer.line, xfer
6800: 2e 61 54 6f 6b 65 6e 2c 20 63 6f 75 6e 74 28 78  .aToken, count(x
6810: 66 65 72 2e 61 54 6f 6b 65 6e 29 29 3b 0a 20 20  fer.aToken));.  
6820: 20 20 20 20 6e 43 61 72 64 2b 2b 3b 0a 20 20 20      nCard++;.   
6830: 20 20 20 70 72 69 6e 74 66 28 22 5c 72 25 64 22     printf("\r%d"
6840: 2c 20 6e 43 61 72 64 29 3b 0a 20 20 20 20 20 20  , nCard);.      
6850: 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 3b 0a  fflush(stdout);.
6860: 0a 20 20 20 20 20 20 2f 2a 20 20 20 66 69 6c 65  .      /*   file
6870: 20 55 55 49 44 20 53 49 5a 45 20 5c 6e 20 43 4f   UUID SIZE \n CO
6880: 4e 54 45 4e 54 0a 20 20 20 20 20 20 2a 2a 20 20  NTENT.      **  
6890: 20 66 69 6c 65 20 55 55 49 44 20 44 45 4c 54 41   file UUID DELTA
68a0: 53 52 43 20 53 49 5a 45 20 5c 6e 20 43 4f 4e 54  SRC SIZE \n CONT
68b0: 45 4e 54 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  ENT.      **.   
68c0: 20 20 20 2a 2a 20 52 65 63 65 69 76 65 20 61 20     ** Receive a 
68d0: 66 69 6c 65 20 74 72 61 6e 73 6d 69 74 74 65 64  file transmitted
68e0: 20 66 72 6f 6d 20 74 68 65 20 73 65 72 76 65 72   from the server
68f0: 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
6900: 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 78 66   if( blob_eq(&xf
6910: 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 22 66 69  er.aToken[0],"fi
6920: 6c 65 22 29 20 29 7b 0a 20 20 20 20 20 20 20 20  le") ){.        
6930: 78 66 65 72 5f 61 63 63 65 70 74 5f 66 69 6c 65  xfer_accept_file
6940: 28 26 78 66 65 72 29 3b 0a 20 20 20 20 20 20 7d  (&xfer);.      }
6950: 65 6c 73 65 0a 0a 20 20 20 20 20 20 2f 2a 20 20  else..      /*  
6960: 20 67 69 6d 6d 65 20 55 55 49 44 0a 20 20 20 20   gimme UUID.    
6970: 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 53 65    **.      ** Se
6980: 72 76 65 72 20 69 73 20 72 65 71 75 65 73 74 69  rver is requesti
6990: 6e 67 20 61 20 66 69 6c 65 2e 20 20 49 66 20 74  ng a file.  If t
69a0: 68 65 20 66 69 6c 65 20 69 73 20 61 20 6d 61 6e  he file is a man
69b0: 69 66 65 73 74 2c 20 61 73 73 75 6d 65 0a 20 20  ifest, assume.  
69c0: 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20      ** that the 
69d0: 73 65 72 76 65 72 20 77 69 6c 6c 20 61 6c 73 6f  server will also
69e0: 20 77 61 6e 74 20 74 6f 20 6b 6e 6f 77 20 61 6c   want to know al
69f0: 6c 20 6f 66 20 74 68 65 20 63 6f 6e 74 65 6e 74  l of the content
6a00: 20 66 69 6c 65 73 0a 20 20 20 20 20 20 2a 2a 20   files.      ** 
6a10: 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
6a20: 74 68 65 20 6d 61 6e 69 66 65 73 74 20 61 6e 64  the manifest and
6a30: 20 73 65 6e 64 20 74 68 6f 73 65 20 74 6f 6f 2e   send those too.
6a40: 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
6a50: 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 78 66 65  if( blob_eq(&xfe
6a60: 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 67 69  r.aToken[0], "gi
6a70: 6d 6d 65 22 29 0a 20 20 20 20 20 20 20 26 26 20  mme").       && 
6a80: 78 66 65 72 2e 6e 54 6f 6b 65 6e 3d 3d 32 0a 20  xfer.nToken==2. 
6a90: 20 20 20 20 20 20 26 26 20 62 6c 6f 62 5f 69 73        && blob_is
6aa0: 5f 75 75 69 64 28 26 78 66 65 72 2e 61 54 6f 6b  _uuid(&xfer.aTok
6ab0: 65 6e 5b 31 5d 29 0a 20 20 20 20 20 20 29 7b 0a  en[1]).      ){.
6ac0: 20 20 20 20 20 20 20 20 69 66 28 20 70 75 73 68          if( push
6ad0: 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20  Flag ){.        
6ae0: 20 20 69 6e 74 20 72 69 64 20 3d 20 72 69 64 5f    int rid = rid_
6af0: 66 72 6f 6d 5f 75 75 69 64 28 26 78 66 65 72 2e  from_uuid(&xfer.
6b00: 61 54 6f 6b 65 6e 5b 31 5d 2c 20 30 29 3b 0a 20  aToken[1], 0);. 
6b10: 20 20 20 20 20 20 20 20 20 73 65 6e 64 5f 66 69           send_fi
6b20: 6c 65 28 26 78 66 65 72 2c 20 72 69 64 2c 20 26  le(&xfer, rid, &
6b30: 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d 2c 20  xfer.aToken[1], 
6b40: 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  0);.        }.  
6b50: 20 20 20 20 7d 65 6c 73 65 0a 20 20 0a 20 20 20      }else.  .   
6b60: 20 20 20 2f 2a 20 20 20 69 67 6f 74 20 55 55 49     /*   igot UUI
6b70: 44 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  D.      **.     
6b80: 20 2a 2a 20 53 65 72 76 65 72 20 61 6e 6e 6f 75   ** Server annou
6b90: 6e 63 65 73 20 74 68 61 74 20 69 74 20 68 61 73  nces that it has
6ba0: 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 66 69   a particular fi
6bb0: 6c 65 2e 20 20 49 66 20 74 68 69 73 20 69 73 0a  le.  If this is.
6bc0: 20 20 20 20 20 20 2a 2a 20 6e 6f 74 20 61 20 66        ** not a f
6bd0: 69 6c 65 20 74 68 61 74 20 77 65 20 68 61 76 65  ile that we have
6be0: 20 61 6e 64 20 77 65 20 61 72 65 20 70 75 6c 6c   and we are pull
6bf0: 69 6e 67 2c 20 74 68 65 6e 20 63 72 65 61 74 65  ing, then create
6c00: 20 61 0a 20 20 20 20 20 20 2a 2a 20 70 68 61 6e   a.      ** phan
6c10: 74 6f 6d 20 74 6f 20 63 61 75 73 65 20 74 68 69  tom to cause thi
6c20: 73 20 66 69 6c 65 20 74 6f 20 62 65 20 72 65 71  s file to be req
6c30: 75 65 73 74 65 64 20 6f 6e 20 74 68 65 20 6e 65  uested on the ne
6c40: 78 74 20 63 79 63 6c 65 2e 0a 20 20 20 20 20 20  xt cycle..      
6c50: 2a 2a 20 41 6c 77 61 79 73 20 72 65 6d 65 6d 62  ** Always rememb
6c60: 65 72 20 74 68 61 74 20 74 68 65 20 73 65 72 76  er that the serv
6c70: 65 72 20 68 61 73 20 74 68 69 73 20 66 69 6c 65  er has this file
6c80: 20 73 6f 20 74 68 61 74 20 77 65 20 64 6f 0a 20   so that we do. 
6c90: 20 20 20 20 20 2a 2a 20 6e 6f 74 20 74 72 61 6e       ** not tran
6ca0: 73 6d 69 74 20 69 74 20 62 79 20 61 63 63 69 64  smit it by accid
6cb0: 65 6e 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ent..      */.  
6cc0: 20 20 20 20 69 66 28 20 78 66 65 72 2e 6e 54 6f      if( xfer.nTo
6cd0: 6b 65 6e 3d 3d 32 0a 20 20 20 20 20 20 20 26 26  ken==2.       &&
6ce0: 20 62 6c 6f 62 5f 65 71 28 26 78 66 65 72 2e 61   blob_eq(&xfer.a
6cf0: 54 6f 6b 65 6e 5b 30 5d 2c 20 22 69 67 6f 74 22  Token[0], "igot"
6d00: 29 0a 20 20 20 20 20 20 20 26 26 20 62 6c 6f 62  ).       && blob
6d10: 5f 69 73 5f 75 75 69 64 28 26 78 66 65 72 2e 61  _is_uuid(&xfer.a
6d20: 54 6f 6b 65 6e 5b 31 5d 29 0a 20 20 20 20 20 20  Token[1]).      
6d30: 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72  ){.        int r
6d40: 69 64 20 3d 20 72 69 64 5f 66 72 6f 6d 5f 75 75  id = rid_from_uu
6d50: 69 64 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b  id(&xfer.aToken[
6d60: 31 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  1], 0);.        
6d70: 69 66 28 20 72 69 64 3d 3d 30 20 26 26 20 28 70  if( rid==0 && (p
6d80: 75 6c 6c 46 6c 61 67 20 7c 7c 20 63 6c 6f 6e 65  ullFlag || clone
6d90: 46 6c 61 67 29 20 29 7b 0a 20 20 20 20 20 20 20  Flag) ){.       
6da0: 20 20 20 72 69 64 20 3d 20 63 6f 6e 74 65 6e 74     rid = content
6db0: 5f 6e 65 77 28 62 6c 6f 62 5f 73 74 72 28 26 78  _new(blob_str(&x
6dc0: 66 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d 29 29 3b  fer.aToken[1]));
6dd0: 0a 20 20 20 20 20 20 20 20 20 20 6e 65 77 50 68  .          newPh
6de0: 61 6e 74 6f 6d 20 3d 20 31 3b 0a 20 20 20 20 20  antom = 1;.     
6df0: 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 65 6d     }.        rem
6e00: 6f 74 65 5f 68 61 73 28 72 69 64 29 3b 0a 20 20  ote_has(rid);.  
6e10: 20 20 20 20 7d 65 6c 73 65 0a 20 20 20 20 0a 20      }else.    . 
6e20: 20 20 20 20 20 0a 20 20 20 20 20 20 2f 2a 20 20       .      /*  
6e30: 20 70 75 73 68 20 20 53 45 52 56 45 52 43 4f 44   push  SERVERCOD
6e40: 45 20 20 50 52 4f 44 55 43 54 43 4f 44 45 0a 20  E  PRODUCTCODE. 
6e50: 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
6e60: 20 53 68 6f 75 6c 64 20 6f 6e 6c 79 20 68 61 70   Should only hap
6e70: 70 65 6e 20 69 6e 20 72 65 73 70 6f 6e 73 65 20  pen in response 
6e80: 74 6f 20 61 20 63 6c 6f 6e 65 2e 20 20 54 68 69  to a clone.  Thi
6e90: 73 20 6d 65 73 73 61 67 65 20 74 65 6c 6c 73 0a  s message tells.
6ea0: 20 20 20 20 20 20 2a 2a 20 74 68 65 20 63 6c 69        ** the cli
6eb0: 65 6e 74 20 77 68 61 74 20 70 72 6f 64 75 63 74  ent what product
6ec0: 20 74 6f 20 75 73 65 20 66 6f 72 20 74 68 65 20   to use for the 
6ed0: 6e 65 77 20 64 61 74 61 62 61 73 65 2e 0a 20 20  new database..  
6ee0: 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
6ef0: 20 62 6c 6f 62 5f 65 71 28 26 78 66 65 72 2e 61   blob_eq(&xfer.a
6f00: 54 6f 6b 65 6e 5b 30 5d 2c 22 70 75 73 68 22 29  Token[0],"push")
6f10: 0a 20 20 20 20 20 20 20 26 26 20 78 66 65 72 2e  .       && xfer.
6f20: 6e 54 6f 6b 65 6e 3d 3d 33 0a 20 20 20 20 20 20  nToken==3.      
6f30: 20 26 26 20 63 6c 6f 6e 65 46 6c 61 67 0a 20 20   && cloneFlag.  
6f40: 20 20 20 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f       && blob_is_
6f50: 75 75 69 64 28 26 78 66 65 72 2e 61 54 6f 6b 65  uuid(&xfer.aToke
6f60: 6e 5b 31 5d 29 0a 20 20 20 20 20 20 20 26 26 20  n[1]).       && 
6f70: 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26 78 66  blob_is_uuid(&xf
6f80: 65 72 2e 61 54 6f 6b 65 6e 5b 32 5d 29 0a 20 20  er.aToken[2]).  
6f90: 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 69      ){.        i
6fa0: 66 28 20 62 6c 6f 62 5f 65 71 5f 73 74 72 28 26  f( blob_eq_str(&
6fb0: 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d 2c 20  xfer.aToken[1], 
6fc0: 7a 53 43 6f 64 65 2c 20 2d 31 29 20 29 7b 0a 20  zSCode, -1) ){. 
6fd0: 20 20 20 20 20 20 20 20 20 66 6f 73 73 69 6c 5f           fossil_
6fe0: 66 61 74 61 6c 28 22 73 65 72 76 65 72 20 6c 6f  fatal("server lo
6ff0: 6f 70 22 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  op");.        }.
7000: 20 20 20 20 20 20 20 20 69 66 28 20 7a 50 43 6f          if( zPCo
7010: 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  de==0 ){.       
7020: 20 20 20 7a 50 43 6f 64 65 20 3d 20 6d 70 72 69     zPCode = mpri
7030: 6e 74 66 28 22 25 62 22 2c 20 26 78 66 65 72 2e  ntf("%b", &xfer.
7040: 61 54 6f 6b 65 6e 5b 32 5d 29 3b 0a 20 20 20 20  aToken[2]);.    
7050: 20 20 20 20 20 20 64 62 5f 73 65 74 28 22 70 72        db_set("pr
7060: 6f 6a 65 63 74 2d 63 6f 64 65 22 2c 20 7a 50 43  oject-code", zPC
7070: 6f 64 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  ode, 0);.       
7080: 20 7d 0a 20 20 20 20 20 20 20 20 62 6c 6f 62 5f   }.        blob_
7090: 61 70 70 65 6e 64 66 28 26 73 65 6e 64 2c 20 22  appendf(&send, "
70a0: 63 6c 6f 6e 65 5c 6e 22 29 3b 0a 20 20 20 20 20  clone\n");.     
70b0: 20 20 20 6e 43 61 72 64 2b 2b 3b 0a 20 20 20 20     nCard++;.    
70c0: 20 20 7d 65 6c 73 65 0a 20 20 20 20 20 20 0a 20    }else.      . 
70d0: 20 20 20 20 20 2f 2a 20 20 20 63 6f 6e 66 69 67       /*   config
70e0: 20 4e 41 4d 45 20 53 49 5a 45 20 5c 6e 20 43 4f   NAME SIZE \n CO
70f0: 4e 54 45 4e 54 0a 20 20 20 20 20 20 2a 2a 0a 20  NTENT.      **. 
7100: 20 20 20 20 20 2a 2a 20 52 65 63 65 69 76 65 20       ** Receive 
7110: 61 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  a configuration 
7120: 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 73  value from the s
7130: 65 72 76 65 72 2e 0a 20 20 20 20 20 20 2a 2f 0a  erver..      */.
7140: 20 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65        if( blob_e
7150: 71 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 30  q(&xfer.aToken[0
7160: 5d 2c 22 63 6f 6e 66 69 67 22 29 20 29 7b 0a 20  ],"config") ){. 
7170: 20 20 20 20 20 20 20 2f 2a 20 54 42 44 3a 20 45         /* TBD: E
7180: 78 74 72 61 63 74 20 6e 61 6d 65 20 61 6e 64 20  xtract name and 
7190: 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 20 20 20  content */.     
71a0: 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73     /* Check to s
71b0: 65 65 20 69 66 20 63 6f 6e 66 69 67 75 72 61 74  ee if configurat
71c0: 69 6f 6e 20 6e 61 6d 65 20 69 73 20 61 75 74 68  ion name is auth
71d0: 6f 72 69 7a 65 64 20 2a 2f 0a 20 20 20 20 20 20  orized */.      
71e0: 20 20 2f 2a 20 53 74 6f 72 65 20 63 6f 6e 74 65    /* Store conte
71f0: 6e 74 20 69 6e 20 74 68 65 20 63 6f 6e 66 69 67  nt in the config
7200: 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20   table */.      
7210: 7d 65 6c 73 65 0a 0a 20 20 20 20 20 20 0a 20 20  }else..      .  
7220: 20 20 20 20 2f 2a 20 20 20 20 63 6f 6f 6b 69 65      /*    cookie
7230: 20 54 45 58 54 0a 20 20 20 20 20 20 2a 2a 0a 20   TEXT.      **. 
7240: 20 20 20 20 20 2a 2a 20 54 68 65 20 73 65 72 76       ** The serv
7250: 65 72 20 6d 69 67 68 74 20 69 6e 63 6c 75 64 65  er might include
7260: 20 61 20 63 6f 6f 6b 69 65 20 69 6e 20 69 74 73   a cookie in its
7270: 20 72 65 70 6c 79 2e 20 20 54 68 65 20 63 6c 69   reply.  The cli
7280: 65 6e 74 0a 20 20 20 20 20 20 2a 2a 20 73 68 6f  ent.      ** sho
7290: 75 6c 64 20 72 65 6d 65 6d 62 65 72 20 74 68 69  uld remember thi
72a0: 73 20 63 6f 6f 6b 69 65 20 61 6e 64 20 73 65 6e  s cookie and sen
72b0: 64 20 69 74 20 62 61 63 6b 20 74 6f 20 74 68 65  d it back to the
72c0: 20 73 65 72 76 65 72 0a 20 20 20 20 20 20 2a 2a   server.      **
72d0: 20 69 6e 20 69 74 73 20 6e 65 78 74 20 71 75 65   in its next que
72e0: 72 79 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  ry..      **.   
72f0: 20 20 20 2a 2a 20 45 61 63 68 20 63 6f 6f 6b 69     ** Each cooki
7300: 65 20 72 65 63 65 69 76 65 64 20 6f 76 65 72 77  e received overw
7310: 72 69 74 65 73 20 74 68 65 20 70 72 69 6f 72 20  rites the prior 
7320: 63 6f 6f 6b 69 65 20 66 72 6f 6d 20 74 68 65 0a  cookie from the.
7330: 20 20 20 20 20 20 2a 2a 20 73 61 6d 65 20 73 65        ** same se
7340: 72 76 65 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  rver..      */. 
7350: 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71       if( blob_eq
7360: 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d  (&xfer.aToken[0]
7370: 2c 20 22 63 6f 6f 6b 69 65 22 29 20 26 26 20 78  , "cookie") && x
7380: 66 65 72 2e 6e 54 6f 6b 65 6e 3d 3d 32 20 29 7b  fer.nToken==2 ){
7390: 0a 20 20 20 20 20 20 20 20 64 62 5f 73 65 74 28  .        db_set(
73a0: 22 63 6f 6f 6b 69 65 22 2c 20 62 6c 6f 62 5f 73  "cookie", blob_s
73b0: 74 72 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b  tr(&xfer.aToken[
73c0: 31 5d 29 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d  1]), 0);.      }
73d0: 65 6c 73 65 0a 0a 20 20 20 20 20 20 2f 2a 20 20  else..      /*  
73e0: 20 6d 65 73 73 61 67 65 20 4d 45 53 53 41 47 45   message MESSAGE
73f0: 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
7400: 2a 2a 20 50 72 69 6e 74 20 61 20 6d 65 73 73 61  ** Print a messa
7410: 67 65 2e 20 20 53 69 6d 69 6c 61 72 20 74 6f 20  ge.  Similar to 
7420: 22 65 72 72 6f 72 22 20 62 75 74 20 64 6f 65 73  "error" but does
7430: 20 6e 6f 74 20 73 74 6f 70 20 70 72 6f 63 65 73   not stop proces
7440: 73 69 6e 67 0a 20 20 20 20 20 20 2a 2f 20 20 20  sing.      */   
7450: 20 20 20 20 20 0a 20 20 20 20 20 20 69 66 28 20       .      if( 
7460: 62 6c 6f 62 5f 65 71 28 26 78 66 65 72 2e 61 54  blob_eq(&xfer.aT
7470: 6f 6b 65 6e 5b 30 5d 2c 22 6d 65 73 73 61 67 65  oken[0],"message
7480: 22 29 20 26 26 20 78 66 65 72 2e 6e 54 6f 6b 65  ") && xfer.nToke
7490: 6e 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20 20 20  n==2 ){.        
74a0: 63 68 61 72 20 2a 7a 4d 73 67 20 3d 20 62 6c 6f  char *zMsg = blo
74b0: 62 5f 74 65 72 6d 69 6e 61 74 65 28 26 78 66 65  b_terminate(&xfe
74c0: 72 2e 61 54 6f 6b 65 6e 5b 31 5d 29 3b 0a 20 20  r.aToken[1]);.  
74d0: 20 20 20 20 20 20 64 65 66 6f 73 73 69 6c 69 7a        defossiliz
74e0: 65 28 7a 4d 73 67 29 3b 0a 20 20 20 20 20 20 20  e(zMsg);.       
74f0: 20 70 72 69 6e 74 66 28 22 53 65 72 76 65 72 20   printf("Server 
7500: 73 61 79 73 3a 20 25 73 5c 6e 22 2c 20 7a 4d 73  says: %s\n", zMs
7510: 67 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a  g);.      }else.
7520: 0a 20 20 20 20 20 20 2f 2a 20 20 20 65 72 72 6f  .      /*   erro
7530: 72 20 4d 45 53 53 41 47 45 0a 20 20 20 20 20 20  r MESSAGE.      
7540: 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 52 65 70 6f  **.      ** Repo
7550: 72 74 20 61 6e 20 65 72 72 6f 72 20 61 6e 64 20  rt an error and 
7560: 61 62 61 6e 64 6f 6e 20 74 68 65 20 73 79 6e 63  abandon the sync
7570: 20 73 65 73 73 69 6f 6e 0a 20 20 20 20 20 20 2a   session.      *
7580: 2f 20 20 20 20 20 20 20 20 0a 20 20 20 20 20 20  /        .      
7590: 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 78 66 65  if( blob_eq(&xfe
75a0: 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 22 65 72 72  r.aToken[0],"err
75b0: 6f 72 22 29 20 26 26 20 78 66 65 72 2e 6e 54 6f  or") && xfer.nTo
75c0: 6b 65 6e 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20  ken==2 ){.      
75d0: 20 20 63 68 61 72 20 2a 7a 4d 73 67 20 3d 20 62    char *zMsg = b
75e0: 6c 6f 62 5f 74 65 72 6d 69 6e 61 74 65 28 26 78  lob_terminate(&x
75f0: 66 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d 29 3b 0a  fer.aToken[1]);.
7600: 20 20 20 20 20 20 20 20 64 65 66 6f 73 73 69 6c          defossil
7610: 69 7a 65 28 7a 4d 73 67 29 3b 0a 20 20 20 20 20  ize(zMsg);.     
7620: 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28     blob_appendf(
7630: 26 78 66 65 72 2e 65 72 72 2c 20 22 73 65 72 76  &xfer.err, "serv
7640: 65 72 20 73 61 79 73 3a 20 25 73 22 2c 20 7a 4d  er says: %s", zM
7650: 73 67 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  sg);.      }else
7660: 0a 0a 20 20 20 20 20 20 2f 2a 20 55 6e 6b 6e 6f  ..      /* Unkno
7670: 77 6e 20 6d 65 73 73 61 67 65 20 2a 2f 0a 20 20  wn message */.  
7680: 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 62 6c      {.        bl
7690: 6f 62 5f 61 70 70 65 6e 64 66 28 26 78 66 65 72  ob_appendf(&xfer
76a0: 2e 65 72 72 2c 20 22 75 6e 6b 6e 6f 77 6e 20 63  .err, "unknown c
76b0: 6f 6d 6d 61 6e 64 3a 20 25 62 22 2c 20 26 78 66  ommand: %b", &xf
76c0: 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d 29 3b 0a 20  er.aToken[0]);. 
76d0: 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66       }..      if
76e0: 28 20 62 6c 6f 62 5f 73 69 7a 65 28 26 78 66 65  ( blob_size(&xfe
76f0: 72 2e 65 72 72 29 20 29 7b 0a 20 20 20 20 20 20  r.err) ){.      
7700: 20 20 66 6f 73 73 69 6c 5f 66 61 74 61 6c 28 22    fossil_fatal("
7710: 25 62 22 2c 20 26 78 66 65 72 2e 65 72 72 29 3b  %b", &xfer.err);
7720: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
7730: 6c 6f 62 61 72 72 61 79 5f 72 65 73 65 74 28 78  lobarray_reset(x
7740: 66 65 72 2e 61 54 6f 6b 65 6e 2c 20 78 66 65 72  fer.aToken, xfer
7750: 2e 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20  .nToken);.      
7760: 62 6c 6f 62 5f 72 65 73 65 74 28 26 78 66 65 72  blob_reset(&xfer
7770: 2e 6c 69 6e 65 29 3b 0a 20 20 20 20 7d 0a 20 20  .line);.    }.  
7780: 20 20 70 72 69 6e 74 66 28 7a 56 61 6c 75 65 46    printf(zValueF
7790: 6f 72 6d 61 74 2c 20 22 52 65 63 65 69 76 65 64  ormat, "Received
77a0: 3a 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  :",.            
77b0: 62 6c 6f 62 5f 73 69 7a 65 28 26 72 65 63 76 29  blob_size(&recv)
77c0: 2c 20 6e 43 61 72 64 2c 0a 20 20 20 20 20 20 20  , nCard,.       
77d0: 20 20 20 20 20 78 66 65 72 2e 6e 46 69 6c 65 52       xfer.nFileR
77e0: 63 76 64 2c 20 78 66 65 72 2e 6e 44 65 6c 74 61  cvd, xfer.nDelta
77f0: 52 63 76 64 20 2b 20 78 66 65 72 2e 6e 44 61 6e  Rcvd + xfer.nDan
7800: 67 6c 69 6e 67 46 69 6c 65 29 3b 0a 20 20 20 20  glingFile);.    
7810: 62 6c 6f 62 5f 72 65 73 65 74 28 26 72 65 63 76  blob_reset(&recv
7820: 29 3b 0a 20 20 20 20 6e 43 79 63 6c 65 2b 2b 3b  );.    nCycle++;
7830: 0a 20 20 20 20 67 6f 20 3d 20 30 3b 0a 0a 20 20  .    go = 0;..  
7840: 20 20 2f 2a 20 49 66 20 77 65 20 72 65 63 65 69    /* If we recei
7850: 76 65 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  ved one or more 
7860: 66 69 6c 65 73 20 6f 6e 20 74 68 65 20 70 72 65  files on the pre
7870: 76 69 6f 75 73 20 65 78 63 68 61 6e 67 65 20 62  vious exchange b
7880: 75 74 0a 20 20 20 20 2a 2a 20 74 68 65 72 65 20  ut.    ** there 
7890: 61 72 65 20 73 74 69 6c 6c 20 70 68 61 6e 74 6f  are still phanto
78a0: 6d 73 2c 20 74 68 65 6e 20 67 6f 20 61 6e 6f 74  ms, then go anot
78b0: 68 65 72 20 72 6f 75 6e 64 2e 0a 20 20 20 20 2a  her round..    *
78c0: 2f 0a 20 20 20 20 6e 46 69 6c 65 52 65 63 76 20  /.    nFileRecv 
78d0: 3d 20 78 66 65 72 2e 6e 46 69 6c 65 52 63 76 64  = xfer.nFileRcvd
78e0: 20 2b 20 78 66 65 72 2e 6e 44 65 6c 74 61 52 63   + xfer.nDeltaRc
78f0: 76 64 20 2b 20 78 66 65 72 2e 6e 44 61 6e 67 6c  vd + xfer.nDangl
7900: 69 6e 67 46 69 6c 65 3b 0a 20 20 20 20 69 66 28  ingFile;.    if(
7910: 20 28 6e 46 69 6c 65 52 65 63 76 3e 30 20 7c 7c   (nFileRecv>0 ||
7920: 20 6e 65 77 50 68 61 6e 74 6f 6d 29 20 26 26 20   newPhantom) && 
7930: 64 62 5f 65 78 69 73 74 73 28 22 53 45 4c 45 43  db_exists("SELEC
7940: 54 20 31 20 46 52 4f 4d 20 70 68 61 6e 74 6f 6d  T 1 FROM phantom
7950: 22 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 20 3d  ") ){.      go =
7960: 20 31 3b 0a 20 20 20 20 20 20 6d 78 50 68 61 6e   1;.      mxPhan
7970: 74 6f 6d 52 65 71 20 3d 20 6e 46 69 6c 65 52 65  tomReq = nFileRe
7980: 63 76 2a 32 3b 0a 20 20 20 20 20 20 69 66 28 20  cv*2;.      if( 
7990: 6d 78 50 68 61 6e 74 6f 6d 52 65 71 3c 32 30 30  mxPhantomReq<200
79a0: 20 29 20 6d 78 50 68 61 6e 74 6f 6d 52 65 71 20   ) mxPhantomReq 
79b0: 3d 20 32 30 30 3b 0a 20 20 20 20 7d 0a 20 20 20  = 200;.    }.   
79c0: 20 6e 43 61 72 64 20 3d 20 30 3b 0a 20 20 20 20   nCard = 0;.    
79d0: 78 66 65 72 2e 6e 46 69 6c 65 52 63 76 64 20 3d  xfer.nFileRcvd =
79e0: 20 30 3b 0a 20 20 20 20 78 66 65 72 2e 6e 44 65   0;.    xfer.nDe
79f0: 6c 74 61 52 63 76 64 20 3d 20 30 3b 0a 20 20 20  ltaRcvd = 0;.   
7a00: 20 78 66 65 72 2e 6e 44 61 6e 67 6c 69 6e 67 46   xfer.nDanglingF
7a10: 69 6c 65 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a  ile = 0;..    /*
7a20: 20 49 66 20 77 65 20 68 61 76 65 20 6f 6e 65 20   If we have one 
7a30: 6f 72 20 6d 6f 72 65 20 66 69 6c 65 73 20 71 75  or more files qu
7a40: 65 75 65 64 20 74 6f 20 73 65 6e 64 2c 20 74 68  eued to send, th
7a50: 65 6e 20 67 6f 0a 20 20 20 20 2a 2a 20 61 6e 6f  en go.    ** ano
7a60: 74 68 65 72 20 72 6f 75 6e 64 20 0a 20 20 20 20  ther round .    
7a70: 2a 2f 0a 20 20 20 20 69 66 28 20 78 66 65 72 2e  */.    if( xfer.
7a80: 6e 46 69 6c 65 53 65 6e 74 2b 78 66 65 72 2e 6e  nFileSent+xfer.n
7a90: 44 65 6c 74 61 53 65 6e 74 3e 30 20 29 7b 0a 20  DeltaSent>0 ){. 
7aa0: 20 20 20 20 20 67 6f 20 3d 20 31 3b 0a 20 20 20       go = 1;.   
7ab0: 20 7d 0a 20 20 7d 3b 0a 20 20 68 74 74 70 5f 63   }.  };.  http_c
7ac0: 6c 6f 73 65 28 29 3b 0a 20 20 64 62 5f 6d 75 6c  lose();.  db_mul
7ad0: 74 69 5f 65 78 65 63 28 22 44 52 4f 50 20 54 41  ti_exec("DROP TA
7ae0: 42 4c 45 20 6f 6e 72 65 6d 6f 74 65 22 29 3b 0a  BLE onremote");.
7af0: 20 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63    db_end_transac
7b00: 74 69 6f 6e 28 30 29 3b 0a 7d 0a                 tion(0);.}.