Hex Artifact Content
Not logged in

Artifact caacb8871c3fbf38eafe4377a184a9a2c305c19a:

File src/xfer.c part of check-in [507ee45f25] - Fix an off-by-one bug in the network protocol handler so that it can accept a zero-length file. by drh on 2007-08-25 12:31:55. Also file src/xfer.c part of check-in [b0ad3f90bc] - Merging aku's changes into the head. by drh on 2007-08-25 19:00:33.

0000: 2f 2a 0a 2a 2a 20 43 6f 70 79 72 69 67 68 74 20  /*.** Copyright 
0010: 28 63 29 20 32 30 30 37 20 44 2e 20 52 69 63 68  (c) 2007 D. Rich
0020: 61 72 64 20 48 69 70 70 0a 2a 2a 0a 2a 2a 20 54  ard Hipp.**.** T
0030: 68 69 73 20 70 72 6f 67 72 61 6d 20 69 73 20 66  his program is f
0040: 72 65 65 20 73 6f 66 74 77 61 72 65 3b 20 79 6f  ree software; yo
0050: 75 20 63 61 6e 20 72 65 64 69 73 74 72 69 62 75  u can redistribu
0060: 74 65 20 69 74 20 61 6e 64 2f 6f 72 0a 2a 2a 20  te it and/or.** 
0070: 6d 6f 64 69 66 79 20 69 74 20 75 6e 64 65 72 20  modify it under 
0080: 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65  the terms of the
0090: 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 50 75 62   GNU General Pub
00a0: 6c 69 63 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 76  lic.** License v
00b0: 65 72 73 69 6f 6e 20 32 20 61 73 20 70 75 62 6c  ersion 2 as publ
00c0: 69 73 68 65 64 20 62 79 20 74 68 65 20 46 72 65  ished by the Fre
00d0: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64  e Software Found
00e0: 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ation..**.** Thi
00f0: 73 20 70 72 6f 67 72 61 6d 20 69 73 20 64 69 73  s program is dis
0100: 74 72 69 62 75 74 65 64 20 69 6e 20 74 68 65 20  tributed in the 
0110: 68 6f 70 65 20 74 68 61 74 20 69 74 20 77 69 6c  hope that it wil
0120: 6c 20 62 65 20 75 73 65 66 75 6c 2c 0a 2a 2a 20  l be useful,.** 
0130: 62 75 74 20 57 49 54 48 4f 55 54 20 41 4e 59 20  but WITHOUT ANY 
0140: 57 41 52 52 41 4e 54 59 3b 20 77 69 74 68 6f 75  WARRANTY; withou
0150: 74 20 65 76 65 6e 20 74 68 65 20 69 6d 70 6c 69  t even the impli
0160: 65 64 20 77 61 72 72 61 6e 74 79 20 6f 66 0a 2a  ed warranty of.*
0170: 2a 20 4d 45 52 43 48 41 4e 54 41 42 49 4c 49 54  * MERCHANTABILIT
0180: 59 20 6f 72 20 46 49 54 4e 45 53 53 20 46 4f 52  Y or FITNESS FOR
0190: 20 41 20 50 41 52 54 49 43 55 4c 41 52 20 50 55   A PARTICULAR PU
01a0: 52 50 4f 53 45 2e 20 20 53 65 65 20 74 68 65 20  RPOSE.  See the 
01b0: 47 4e 55 0a 2a 2a 20 47 65 6e 65 72 61 6c 20 50  GNU.** General P
01c0: 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 20 66 6f  ublic License fo
01d0: 72 20 6d 6f 72 65 20 64 65 74 61 69 6c 73 2e 0a  r more details..
01e0: 2a 2a 20 0a 2a 2a 20 59 6f 75 20 73 68 6f 75 6c  ** .** You shoul
01f0: 64 20 68 61 76 65 20 72 65 63 65 69 76 65 64 20  d have received 
0200: 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 47 4e  a copy of the GN
0210: 55 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69 63  U General Public
0220: 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 61 6c 6f 6e  .** License alon
0230: 67 20 77 69 74 68 20 74 68 69 73 20 6c 69 62 72  g with this libr
0240: 61 72 79 3b 20 69 66 20 6e 6f 74 2c 20 77 72 69  ary; if not, wri
0250: 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 46 72 65  te to the.** Fre
0260: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64  e Software Found
0270: 61 74 69 6f 6e 2c 20 49 6e 63 2e 2c 20 35 39 20  ation, Inc., 59 
0280: 54 65 6d 70 6c 65 20 50 6c 61 63 65 20 2d 20 53  Temple Place - S
0290: 75 69 74 65 20 33 33 30 2c 0a 2a 2a 20 42 6f 73  uite 330,.** Bos
02a0: 74 6f 6e 2c 20 4d 41 20 20 30 32 31 31 31 2d 31  ton, MA  02111-1
02b0: 33 30 37 2c 20 55 53 41 2e 0a 2a 2a 0a 2a 2a 20  307, USA..**.** 
02c0: 41 75 74 68 6f 72 20 63 6f 6e 74 61 63 74 20 69  Author contact i
02d0: 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a 20 20  nformation:.**  
02e0: 20 64 72 68 40 68 77 61 63 69 2e 63 6f 6d 0a 2a   drh@hwaci.com.*
02f0: 2a 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 68  *   http://www.h
0300: 77 61 63 69 2e 63 6f 6d 2f 64 72 68 2f 0a 2a 2a  waci.com/drh/.**
0310: 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
0320: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0330: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0340: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0350: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0360: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
0370: 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74   contains code t
0380: 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  o implement the 
0390: 66 69 6c 65 20 74 72 61 6e 73 66 65 72 20 70 72  file transfer pr
03a0: 6f 74 6f 63 6f 6c 2e 0a 2a 2f 0a 23 69 6e 63 6c  otocol..*/.#incl
03b0: 75 64 65 20 22 63 6f 6e 66 69 67 2e 68 22 0a 23  ude "config.h".#
03c0: 69 6e 63 6c 75 64 65 20 22 78 66 65 72 2e 68 22  include "xfer.h"
03d0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72  ../*.** This str
03e0: 75 63 74 75 72 65 20 68 6f 6c 64 73 20 69 6e 66  ucture holds inf
03f0: 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74  ormation about t
0400: 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 65  he current state
0410: 20 6f 66 20 65 69 74 68 65 72 0a 2a 2a 20 61 20   of either.** a 
0420: 63 6c 69 65 6e 74 20 6f 72 20 61 20 73 65 72 76  client or a serv
0430: 65 72 20 74 68 61 74 20 69 73 20 70 61 72 74 69  er that is parti
0440: 63 69 70 61 74 69 6e 67 20 69 6e 20 78 66 65 72  cipating in xfer
0450: 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
0460: 75 63 74 20 58 66 65 72 20 58 66 65 72 3b 0a 73  uct Xfer Xfer;.s
0470: 74 72 75 63 74 20 58 66 65 72 20 7b 0a 20 20 42  truct Xfer {.  B
0480: 6c 6f 62 20 2a 70 49 6e 3b 20 20 20 20 20 20 20  lob *pIn;       
0490: 20 20 20 2f 2a 20 49 6e 70 75 74 20 74 65 78 74     /* Input text
04a0: 20 66 72 6f 6d 20 74 68 65 20 6f 74 68 65 72 20   from the other 
04b0: 73 69 64 65 20 2a 2f 0a 20 20 42 6c 6f 62 20 2a  side */.  Blob *
04c0: 70 4f 75 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  pOut;         /*
04d0: 20 43 6f 6d 70 6f 73 65 20 6f 75 72 20 72 65 70   Compose our rep
04e0: 6c 79 20 68 65 72 65 20 2a 2f 0a 20 20 42 6c 6f  ly here */.  Blo
04f0: 62 20 6c 69 6e 65 3b 20 20 20 20 20 20 20 20 20  b line;         
0500: 20 2f 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20   /* The current 
0510: 6c 69 6e 65 20 6f 66 20 69 6e 70 75 74 20 2a 2f  line of input */
0520: 0a 20 20 42 6c 6f 62 20 61 54 6f 6b 65 6e 5b 35  .  Blob aToken[5
0530: 5d 3b 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69  ];     /* Tokeni
0540: 7a 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 6c  zed version of l
0550: 69 6e 65 20 2a 2f 0a 20 20 42 6c 6f 62 20 65 72  ine */.  Blob er
0560: 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  r;           /* 
0570: 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65  Error message te
0580: 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 6b  xt */.  int nTok
0590: 65 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  en;         /* N
05a0: 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20  umber of tokens 
05b0: 69 6e 20 6c 69 6e 65 20 2a 2f 0a 20 20 69 6e 74  in line */.  int
05c0: 20 6e 49 47 6f 74 53 65 6e 74 3b 20 20 20 20 20   nIGotSent;     
05d0: 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 22 69   /* Number of "i
05e0: 67 6f 74 22 20 6d 65 73 73 61 67 65 73 20 73 65  got" messages se
05f0: 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 47 69 6d  nt */.  int nGim
0600: 6d 65 53 65 6e 74 3b 20 20 20 20 20 2f 2a 20 4e  meSent;     /* N
0610: 75 6d 62 65 72 20 6f 66 20 67 69 6d 6d 65 20 6d  umber of gimme m
0620: 65 73 73 61 67 65 73 20 73 65 6e 74 20 2a 2f 0a  essages sent */.
0630: 20 20 69 6e 74 20 6e 46 69 6c 65 53 65 6e 74 3b    int nFileSent;
0640: 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
0650: 6f 66 20 66 69 6c 65 73 20 73 65 6e 74 20 2a 2f  of files sent */
0660: 0a 20 20 69 6e 74 20 6e 44 65 6c 74 61 53 65 6e  .  int nDeltaSen
0670: 74 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  t;     /* Number
0680: 20 6f 66 20 64 65 6c 74 61 73 20 73 65 6e 74 20   of deltas sent 
0690: 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 6c 65 52 63  */.  int nFileRc
06a0: 76 64 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  vd;      /* Numb
06b0: 65 72 20 6f 66 20 66 69 6c 65 73 20 72 65 63 65  er of files rece
06c0: 69 76 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 44  ived */.  int nD
06d0: 65 6c 74 61 52 63 76 64 3b 20 20 20 20 20 2f 2a  eltaRcvd;     /*
06e0: 20 4e 75 6d 62 65 72 20 6f 66 20 64 65 6c 74 61   Number of delta
06f0: 73 20 72 65 63 65 69 76 65 64 20 2a 2f 0a 20 20  s received */.  
0700: 69 6e 74 20 6e 44 61 6e 67 6c 69 6e 67 46 69 6c  int nDanglingFil
0710: 65 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66  e;  /* Number of
0720: 20 64 61 6e 67 6c 69 6e 67 20 64 65 6c 74 61 73   dangling deltas
0730: 20 72 65 63 65 69 76 65 64 20 2a 2f 0a 20 20 69   received */.  i
0740: 6e 74 20 6d 78 53 65 6e 64 3b 20 20 20 20 20 20  nt mxSend;      
0750: 20 20 20 2f 2a 20 53 74 6f 70 20 73 65 6e 64 69     /* Stop sendi
0760: 6e 67 20 22 66 69 6c 65 22 20 77 69 74 68 20 70  ng "file" with p
0770: 4f 75 74 20 72 65 61 63 68 65 73 20 74 68 69 73  Out reaches this
0780: 20 73 69 7a 65 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a   size */.};.../*
0790: 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 62 6c  .** The input bl
07a0: 6f 62 20 63 6f 6e 74 61 69 6e 73 20 61 20 55 55  ob contains a UU
07b0: 49 44 2e 20 20 43 6f 6e 76 65 72 74 20 69 74 20  ID.  Convert it 
07c0: 69 6e 74 6f 20 61 20 72 65 63 6f 72 64 20 49 44  into a record ID
07d0: 2e 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 70 68  ..** Create a ph
07e0: 61 6e 74 6f 6d 20 72 65 63 6f 72 64 20 69 66 20  antom record if 
07f0: 6e 6f 20 70 72 69 6f 72 20 72 65 63 6f 72 64 20  no prior record 
0800: 65 78 69 73 74 73 20 61 6e 64 0a 2a 2a 20 70 68  exists and.** ph
0810: 61 6e 74 6f 6d 69 7a 65 20 69 73 20 74 72 75 65  antomize is true
0820: 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20  ..**.** Compare 
0830: 74 6f 20 75 75 69 64 5f 74 6f 5f 72 69 64 28 29  to uuid_to_rid()
0840: 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
0850: 74 61 6b 65 73 20 61 20 62 6c 6f 62 20 61 72 67  takes a blob arg
0860: 75 6d 65 6e 74 0a 2a 2a 20 61 6e 64 20 64 6f 65  ument.** and doe
0870: 73 20 6c 65 73 73 20 65 72 72 6f 72 20 63 68 65  s less error che
0880: 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63  cking..*/.static
0890: 20 69 6e 74 20 72 69 64 5f 66 72 6f 6d 5f 75 75   int rid_from_uu
08a0: 69 64 28 42 6c 6f 62 20 2a 70 55 75 69 64 2c 20  id(Blob *pUuid, 
08b0: 69 6e 74 20 70 68 61 6e 74 6f 6d 69 7a 65 29 7b  int phantomize){
08c0: 0a 20 20 69 6e 74 20 72 69 64 20 3d 20 64 62 5f  .  int rid = db_
08d0: 69 6e 74 28 30 2c 20 22 53 45 4c 45 43 54 20 72  int(0, "SELECT r
08e0: 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45  id FROM blob WHE
08f0: 52 45 20 75 75 69 64 3d 27 25 62 27 22 2c 20 70  RE uuid='%b'", p
0900: 55 75 69 64 29 3b 0a 20 20 69 66 28 20 72 69 64  Uuid);.  if( rid
0910: 3d 3d 30 20 26 26 20 70 68 61 6e 74 6f 6d 69 7a  ==0 && phantomiz
0920: 65 20 29 7b 0a 20 20 20 20 72 69 64 20 3d 20 63  e ){.    rid = c
0930: 6f 6e 74 65 6e 74 5f 70 75 74 28 30 2c 20 62 6c  ontent_put(0, bl
0940: 6f 62 5f 73 74 72 28 70 55 75 69 64 29 2c 20 30  ob_str(pUuid), 0
0950: 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
0960: 72 69 64 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54  rid;.}.../*.** T
0970: 68 65 20 61 54 6f 6b 65 6e 5b 30 2e 2e 6e 54 6f  he aToken[0..nTo
0980: 6b 65 6e 2d 31 5d 20 62 6c 6f 62 20 61 72 72 61  ken-1] blob arra
0990: 79 20 69 73 20 61 20 70 61 72 73 65 20 6f 66 20  y is a parse of 
09a0: 61 20 22 66 69 6c 65 22 20 6c 69 6e 65 20 0a 2a  a "file" line .*
09b0: 2a 20 6d 65 73 73 61 67 65 2e 20 20 54 68 69 73  * message.  This
09c0: 20 72 6f 75 74 69 6e 65 20 66 69 6e 69 73 68 65   routine finishe
09d0: 73 20 70 61 72 73 69 6e 67 20 74 68 61 74 20 6d  s parsing that m
09e0: 65 73 73 61 67 65 20 61 6e 64 20 64 6f 65 73 0a  essage and does.
09f0: 2a 2a 20 61 20 72 65 63 6f 72 64 20 69 6e 73 65  ** a record inse
0a00: 72 74 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a  rt of the file..
0a10: 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 6c  **.** The file l
0a20: 69 6e 65 20 69 73 20 69 6e 20 6f 6e 65 20 6f 66  ine is in one of
0a30: 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   the following t
0a40: 77 6f 20 66 6f 72 6d 73 3a 0a 2a 2a 0a 2a 2a 20  wo forms:.**.** 
0a50: 20 20 20 20 20 66 69 6c 65 20 55 55 49 44 20 53       file UUID S
0a60: 49 5a 45 20 5c 6e 20 43 4f 4e 54 45 4e 54 0a 2a  IZE \n CONTENT.*
0a70: 2a 20 20 20 20 20 20 66 69 6c 65 20 55 55 49 44  *      file UUID
0a80: 20 44 45 4c 54 41 53 52 43 20 53 49 5a 45 20 5c   DELTASRC SIZE \
0a90: 6e 20 43 4f 4e 54 45 4e 54 0a 2a 2a 0a 2a 2a 20  n CONTENT.**.** 
0aa0: 54 68 65 20 63 6f 6e 74 65 6e 74 20 69 73 20 53  The content is S
0ab0: 49 5a 45 20 62 79 74 65 73 20 69 6d 6d 65 64 69  IZE bytes immedi
0ac0: 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20  ately following 
0ad0: 74 68 65 20 6e 65 77 6c 69 6e 65 2e 0a 2a 2a 20  the newline..** 
0ae0: 49 66 20 44 45 4c 54 41 53 52 43 20 65 78 69 73  If DELTASRC exis
0af0: 74 73 2c 20 74 68 65 6e 20 74 68 65 20 43 4f 4e  ts, then the CON
0b00: 54 45 4e 54 20 69 73 20 61 20 64 65 6c 74 61 20  TENT is a delta 
0b10: 61 67 61 69 6e 73 74 20 74 68 65 0a 2a 2a 20 63  against the.** c
0b20: 6f 6e 74 65 6e 74 20 6f 66 20 44 45 4c 54 41 53  ontent of DELTAS
0b30: 52 43 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79  RC..**.** If any
0b40: 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 77   error occurs, w
0b50: 72 69 74 65 20 61 20 6d 65 73 73 61 67 65 20 69  rite a message i
0b60: 6e 74 6f 20 70 45 72 72 20 77 68 69 63 68 20 68  nto pErr which h
0b70: 61 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 62 65  as already.** be
0b80: 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20   initialized to 
0b90: 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2e  an empty string.
0ba0: 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
0bb0: 78 66 65 72 5f 61 63 63 65 70 74 5f 66 69 6c 65  xfer_accept_file
0bc0: 28 58 66 65 72 20 2a 70 58 66 65 72 29 7b 0a 20  (Xfer *pXfer){. 
0bd0: 20 69 6e 74 20 6e 3b 0a 20 20 69 6e 74 20 72 69   int n;.  int ri
0be0: 64 3b 0a 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e  d;.  Blob conten
0bf0: 74 2c 20 68 61 73 68 3b 0a 20 20 0a 20 20 69 66  t, hash;.  .  if
0c00: 28 20 70 58 66 65 72 2d 3e 6e 54 6f 6b 65 6e 3c  ( pXfer->nToken<
0c10: 33 20 0a 20 20 20 7c 7c 20 70 58 66 65 72 2d 3e  3 .   || pXfer->
0c20: 6e 54 6f 6b 65 6e 3e 34 0a 20 20 20 7c 7c 20 21  nToken>4.   || !
0c30: 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26 70 58  blob_is_uuid(&pX
0c40: 66 65 72 2d 3e 61 54 6f 6b 65 6e 5b 31 5d 29 0a  fer->aToken[1]).
0c50: 20 20 20 7c 7c 20 21 62 6c 6f 62 5f 69 73 5f 69     || !blob_is_i
0c60: 6e 74 28 26 70 58 66 65 72 2d 3e 61 54 6f 6b 65  nt(&pXfer->aToke
0c70: 6e 5b 70 58 66 65 72 2d 3e 6e 54 6f 6b 65 6e 2d  n[pXfer->nToken-
0c80: 31 5d 2c 20 26 6e 29 0a 20 20 20 7c 7c 20 6e 3c  1], &n).   || n<
0c90: 30 0a 20 20 20 7c 7c 20 28 70 58 66 65 72 2d 3e  0.   || (pXfer->
0ca0: 6e 54 6f 6b 65 6e 3d 3d 34 20 26 26 20 21 62 6c  nToken==4 && !bl
0cb0: 6f 62 5f 69 73 5f 75 75 69 64 28 26 70 58 66 65  ob_is_uuid(&pXfe
0cc0: 72 2d 3e 61 54 6f 6b 65 6e 5b 32 5d 29 29 0a 20  r->aToken[2])). 
0cd0: 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70   ){.    blob_app
0ce0: 65 6e 64 66 28 26 70 58 66 65 72 2d 3e 65 72 72  endf(&pXfer->err
0cf0: 2c 20 22 6d 61 6c 66 6f 72 6d 65 64 20 66 69 6c  , "malformed fil
0d00: 65 20 6c 69 6e 65 22 29 3b 0a 20 20 20 20 72 65  e line");.    re
0d10: 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62  turn;.  }.  blob
0d20: 5f 7a 65 72 6f 28 26 63 6f 6e 74 65 6e 74 29 3b  _zero(&content);
0d30: 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 68 61  .  blob_zero(&ha
0d40: 73 68 29 3b 0a 20 20 62 6c 6f 62 5f 65 78 74 72  sh);.  blob_extr
0d50: 61 63 74 28 70 58 66 65 72 2d 3e 70 49 6e 2c 20  act(pXfer->pIn, 
0d60: 6e 2c 20 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20  n, &content);.  
0d70: 69 66 28 20 70 58 66 65 72 2d 3e 6e 54 6f 6b 65  if( pXfer->nToke
0d80: 6e 3d 3d 34 20 29 7b 0a 20 20 20 20 42 6c 6f 62  n==4 ){.    Blob
0d90: 20 73 72 63 3b 0a 20 20 20 20 69 6e 74 20 73 72   src;.    int sr
0da0: 63 69 64 20 3d 20 72 69 64 5f 66 72 6f 6d 5f 75  cid = rid_from_u
0db0: 75 69 64 28 26 70 58 66 65 72 2d 3e 61 54 6f 6b  uid(&pXfer->aTok
0dc0: 65 6e 5b 32 5d 2c 20 31 29 3b 0a 20 20 20 20 69  en[2], 1);.    i
0dd0: 66 28 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28 73  f( content_get(s
0de0: 72 63 69 64 2c 20 26 73 72 63 29 3d 3d 30 20 29  rcid, &src)==0 )
0df0: 7b 0a 20 20 20 20 20 20 63 6f 6e 74 65 6e 74 5f  {.      content_
0e00: 70 75 74 28 26 63 6f 6e 74 65 6e 74 2c 20 62 6c  put(&content, bl
0e10: 6f 62 5f 73 74 72 28 26 70 58 66 65 72 2d 3e 61  ob_str(&pXfer->a
0e20: 54 6f 6b 65 6e 5b 31 5d 29 2c 20 73 72 63 69 64  Token[1]), srcid
0e30: 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 61 70  );.      blob_ap
0e40: 70 65 6e 64 66 28 70 58 66 65 72 2d 3e 70 4f 75  pendf(pXfer->pOu
0e50: 74 2c 20 22 67 69 6d 6d 65 20 25 62 5c 6e 22 2c  t, "gimme %b\n",
0e60: 20 26 70 58 66 65 72 2d 3e 61 54 6f 6b 65 6e 5b   &pXfer->aToken[
0e70: 32 5d 29 3b 0a 20 20 20 20 20 20 70 58 66 65 72  2]);.      pXfer
0e80: 2d 3e 6e 47 69 6d 6d 65 53 65 6e 74 2b 2b 3b 0a  ->nGimmeSent++;.
0e90: 20 20 20 20 20 20 70 58 66 65 72 2d 3e 6e 44 61        pXfer->nDa
0ea0: 6e 67 6c 69 6e 67 46 69 6c 65 2b 2b 3b 0a 20 20  nglingFile++;.  
0eb0: 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
0ec0: 7d 0a 20 20 20 20 70 58 66 65 72 2d 3e 6e 44 65  }.    pXfer->nDe
0ed0: 6c 74 61 52 63 76 64 2b 2b 3b 0a 20 20 20 20 62  ltaRcvd++;.    b
0ee0: 6c 6f 62 5f 64 65 6c 74 61 5f 61 70 70 6c 79 28  lob_delta_apply(
0ef0: 26 73 72 63 2c 20 26 63 6f 6e 74 65 6e 74 2c 20  &src, &content, 
0f00: 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 20 20 62  &content);.    b
0f10: 6c 6f 62 5f 72 65 73 65 74 28 26 73 72 63 29 3b  lob_reset(&src);
0f20: 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 58  .  }else{.    pX
0f30: 66 65 72 2d 3e 6e 46 69 6c 65 52 63 76 64 2b 2b  fer->nFileRcvd++
0f40: 3b 0a 20 20 7d 0a 20 20 73 68 61 31 73 75 6d 5f  ;.  }.  sha1sum_
0f50: 62 6c 6f 62 28 26 63 6f 6e 74 65 6e 74 2c 20 26  blob(&content, &
0f60: 68 61 73 68 29 3b 0a 20 20 69 66 28 20 21 62 6c  hash);.  if( !bl
0f70: 6f 62 5f 65 71 5f 73 74 72 28 26 70 58 66 65 72  ob_eq_str(&pXfer
0f80: 2d 3e 61 54 6f 6b 65 6e 5b 31 5d 2c 20 62 6c 6f  ->aToken[1], blo
0f90: 62 5f 73 74 72 28 26 68 61 73 68 29 2c 20 2d 31  b_str(&hash), -1
0fa0: 29 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 61 70  ) ){.    blob_ap
0fb0: 70 65 6e 64 66 28 26 70 58 66 65 72 2d 3e 65 72  pendf(&pXfer->er
0fc0: 72 2c 20 22 63 6f 6e 74 65 6e 74 20 64 6f 65 73  r, "content does
0fd0: 20 6e 6f 74 20 6d 61 74 63 68 20 73 68 61 31 20   not match sha1 
0fe0: 68 61 73 68 22 29 3b 0a 20 20 7d 0a 20 20 62 6c  hash");.  }.  bl
0ff0: 6f 62 5f 72 65 73 65 74 28 26 68 61 73 68 29 3b  ob_reset(&hash);
1000: 0a 20 20 72 69 64 20 3d 20 63 6f 6e 74 65 6e 74  .  rid = content
1010: 5f 70 75 74 28 26 63 6f 6e 74 65 6e 74 2c 20 30  _put(&content, 0
1020: 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 69 64 3d  , 0);.  if( rid=
1030: 3d 30 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 61  =0 ){.    blob_a
1040: 70 70 65 6e 64 66 28 26 70 58 66 65 72 2d 3e 65  ppendf(&pXfer->e
1050: 72 72 2c 20 22 25 73 22 2c 20 67 2e 7a 45 72 72  rr, "%s", g.zErr
1060: 4d 73 67 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Msg);.  }else{. 
1070: 20 20 20 6d 61 6e 69 66 65 73 74 5f 63 72 6f 73     manifest_cros
1080: 73 6c 69 6e 6b 28 72 69 64 2c 20 26 63 6f 6e 74  slink(rid, &cont
1090: 65 6e 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ent);.  }.}../*.
10a0: 2a 2a 20 54 72 79 20 74 6f 20 73 65 6e 64 20 61  ** Try to send a
10b0: 20 66 69 6c 65 20 61 73 20 61 20 64 65 6c 74 61   file as a delta
10c0: 2e 20 20 49 66 20 73 75 63 63 65 73 73 66 75 6c  .  If successful
10d0: 2c 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  , return the num
10e0: 62 65 72 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20  ber.** of bytes 
10f0: 69 6e 20 74 68 65 20 64 65 6c 74 61 2e 20 20 49  in the delta.  I
1100: 66 20 6e 6f 74 2c 20 72 65 74 75 72 6e 20 7a 65  f not, return ze
1110: 72 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 72 63  ro..**.** If src
1120: 49 64 20 69 73 20 73 70 65 63 69 66 69 65 64 2c  Id is specified,
1130: 20 75 73 65 20 69 74 2e 20 20 49 66 20 6e 6f 74   use it.  If not
1140: 2c 20 74 72 79 20 74 6f 20 66 69 67 75 72 65 20  , try to figure 
1150: 6f 75 74 20 61 0a 2a 2a 20 72 65 61 73 6f 6e 61  out a.** reasona
1160: 62 6c 65 20 73 72 63 49 64 2e 0a 2a 2f 0a 73 74  ble srcId..*/.st
1170: 61 74 69 63 20 69 6e 74 20 73 65 6e 64 5f 61 73  atic int send_as
1180: 5f 64 65 6c 74 61 28 0a 20 20 58 66 65 72 20 2a  _delta(.  Xfer *
1190: 70 58 66 65 72 2c 20 20 20 20 20 20 20 20 20 20  pXfer,          
11a0: 20 20 2f 2a 20 54 68 65 20 74 72 61 6e 73 66 65    /* The transfe
11b0: 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69  r context */.  i
11c0: 6e 74 20 72 69 64 2c 20 20 20 20 20 20 20 20 20  nt rid,         
11d0: 20 20 20 20 20 20 20 2f 2a 20 72 65 63 6f 72 64         /* record
11e0: 20 69 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20   id of the file 
11f0: 74 6f 20 73 65 6e 64 20 2a 2f 0a 20 20 42 6c 6f  to send */.  Blo
1200: 62 20 2a 70 43 6f 6e 74 65 6e 74 2c 20 20 20 20  b *pContent,    
1210: 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 74       /* The cont
1220: 65 6e 74 20 6f 66 20 74 68 65 20 66 69 6c 65 20  ent of the file 
1230: 74 6f 20 73 65 6e 64 20 2a 2f 0a 20 20 42 6c 6f  to send */.  Blo
1240: 62 20 2a 70 55 75 69 64 2c 20 20 20 20 20 20 20  b *pUuid,       
1250: 20 20 20 20 20 2f 2a 20 54 68 65 20 55 55 49 44       /* The UUID
1260: 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 6f 20   of the file to 
1270: 73 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 73 72  send */.  int sr
1280: 63 49 64 20 20 20 20 20 20 20 20 20 20 20 20 20  cId             
1290: 20 20 2f 2a 20 53 65 6e 64 20 61 73 20 61 20 64    /* Send as a d
12a0: 65 6c 74 61 20 61 67 61 69 6e 73 74 20 74 68 69  elta against thi
12b0: 73 20 72 65 63 6f 72 64 20 2a 2f 0a 29 7b 0a 20  s record */.){. 
12c0: 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68   static const ch
12d0: 61 72 20 2a 61 7a 51 75 65 72 79 5b 5d 20 3d 20  ar *azQuery[] = 
12e0: 7b 0a 20 20 20 20 22 53 45 4c 45 43 54 20 70 69  {.    "SELECT pi
12f0: 64 20 46 52 4f 4d 20 70 6c 69 6e 6b 22 0a 20 20  d FROM plink".  
1300: 20 20 22 20 57 48 45 52 45 20 63 69 64 3d 25 64    " WHERE cid=%d
1310: 22 0a 20 20 20 20 22 20 20 20 41 4e 44 20 4e 4f  ".    "   AND NO
1320: 54 20 45 58 49 53 54 53 28 53 45 4c 45 43 54 20  T EXISTS(SELECT 
1330: 31 20 46 52 4f 4d 20 70 68 61 6e 74 6f 6d 20 57  1 FROM phantom W
1340: 48 45 52 45 20 72 69 64 3d 70 69 64 29 22 2c 0a  HERE rid=pid)",.
1350: 0a 20 20 20 20 22 53 45 4c 45 43 54 20 70 69 64  .    "SELECT pid
1360: 20 46 52 4f 4d 20 6d 6c 69 6e 6b 22 0a 20 20 20   FROM mlink".   
1370: 20 22 20 57 48 45 52 45 20 66 69 64 3d 25 64 22   " WHERE fid=%d"
1380: 0a 20 20 20 20 22 20 20 20 41 4e 44 20 4e 4f 54  .    "   AND NOT
1390: 20 45 58 49 53 54 53 28 53 45 4c 45 43 54 20 31   EXISTS(SELECT 1
13a0: 20 46 52 4f 4d 20 70 68 61 6e 74 6f 6d 20 57 48   FROM phantom WH
13b0: 45 52 45 20 72 69 64 3d 70 69 64 29 22 2c 0a 20  ERE rid=pid)",. 
13c0: 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 42   };.  int i;.  B
13d0: 6c 6f 62 20 73 72 63 2c 20 64 65 6c 74 61 3b 0a  lob src, delta;.
13e0: 20 20 69 6e 74 20 73 69 7a 65 20 3d 20 30 3b 0a    int size = 0;.
13f0: 0a 20 20 66 6f 72 28 69 3d 30 3b 20 73 72 63 49  .  for(i=0; srcI
1400: 64 3d 3d 30 20 26 26 20 69 3c 63 6f 75 6e 74 28  d==0 && i<count(
1410: 61 7a 51 75 65 72 79 29 3b 20 69 2b 2b 29 7b 0a  azQuery); i++){.
1420: 20 20 20 20 73 72 63 49 64 20 3d 20 64 62 5f 69      srcId = db_i
1430: 6e 74 28 30 2c 20 61 7a 51 75 65 72 79 5b 69 5d  nt(0, azQuery[i]
1440: 2c 20 72 69 64 29 3b 0a 20 20 7d 0a 20 20 69 66  , rid);.  }.  if
1450: 28 20 73 72 63 49 64 3e 30 20 26 26 20 63 6f 6e  ( srcId>0 && con
1460: 74 65 6e 74 5f 67 65 74 28 73 72 63 49 64 2c 20  tent_get(srcId, 
1470: 26 73 72 63 29 20 29 7b 0a 20 20 20 20 63 68 61  &src) ){.    cha
1480: 72 20 2a 7a 55 75 69 64 20 3d 20 64 62 5f 74 65  r *zUuid = db_te
1490: 78 74 28 30 2c 20 22 53 45 4c 45 43 54 20 75 75  xt(0, "SELECT uu
14a0: 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45  id FROM blob WHE
14b0: 52 45 20 72 69 64 3d 25 64 22 2c 20 73 72 63 49  RE rid=%d", srcI
14c0: 64 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 64 65 6c  d);.    blob_del
14d0: 74 61 5f 63 72 65 61 74 65 28 26 73 72 63 2c 20  ta_create(&src, 
14e0: 70 43 6f 6e 74 65 6e 74 2c 20 26 64 65 6c 74 61  pContent, &delta
14f0: 29 3b 0a 20 20 20 20 73 69 7a 65 20 3d 20 62 6c  );.    size = bl
1500: 6f 62 5f 73 69 7a 65 28 26 64 65 6c 74 61 29 3b  ob_size(&delta);
1510: 0a 20 20 20 20 69 66 28 20 73 69 7a 65 3e 3d 62  .    if( size>=b
1520: 6c 6f 62 5f 73 69 7a 65 28 70 43 6f 6e 74 65 6e  lob_size(pConten
1530: 74 29 2d 35 30 20 29 7b 0a 20 20 20 20 20 20 73  t)-50 ){.      s
1540: 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c  ize = 0;.    }el
1550: 73 65 7b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 61  se{.      blob_a
1560: 70 70 65 6e 64 66 28 70 58 66 65 72 2d 3e 70 4f  ppendf(pXfer->pO
1570: 75 74 2c 20 22 66 69 6c 65 20 25 62 20 25 73 20  ut, "file %b %s 
1580: 25 64 5c 6e 22 2c 20 70 55 75 69 64 2c 20 7a 55  %d\n", pUuid, zU
1590: 75 69 64 2c 20 73 69 7a 65 29 3b 0a 20 20 20 20  uid, size);.    
15a0: 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 28 70 58    blob_append(pX
15b0: 66 65 72 2d 3e 70 4f 75 74 2c 20 62 6c 6f 62 5f  fer->pOut, blob_
15c0: 62 75 66 66 65 72 28 26 64 65 6c 74 61 29 2c 20  buffer(&delta), 
15d0: 73 69 7a 65 29 3b 0a 20 20 20 20 20 20 2f 2a 20  size);.      /* 
15e0: 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70 58 66  blob_appendf(pXf
15f0: 65 72 2d 3e 70 4f 75 74 2c 20 22 5c 6e 22 2c 20  er->pOut, "\n", 
1600: 31 29 3b 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20  1); */.    }.   
1610: 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 64 65 6c   blob_reset(&del
1620: 74 61 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 55  ta);.    free(zU
1630: 75 69 64 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 72  uid);.    blob_r
1640: 65 73 65 74 28 26 73 72 63 29 3b 0a 20 20 7d 0a  eset(&src);.  }.
1650: 20 20 72 65 74 75 72 6e 20 73 69 7a 65 3b 0a 7d    return size;.}
1660: 0a 0a 2f 2a 0a 2a 2a 20 53 65 6e 64 20 74 68 65  ../*.** Send the
1670: 20 66 69 6c 65 20 69 64 65 6e 74 69 66 69 65 64   file identified
1680: 20 62 79 20 72 69 64 2e 0a 2a 2a 0a 2a 2a 20 54   by rid..**.** T
1690: 68 65 20 70 55 75 69 64 20 63 61 6e 20 62 65 20  he pUuid can be 
16a0: 4e 55 4c 4c 20 69 6e 20 77 68 69 63 68 20 63 61  NULL in which ca
16b0: 73 65 20 74 68 65 20 63 6f 72 72 65 63 74 20 55  se the correct U
16c0: 55 49 44 20 69 73 20 63 6f 6d 70 75 74 65 64 0a  UID is computed.
16d0: 2a 2a 20 66 72 6f 6d 20 74 68 65 20 72 69 64 2e  ** from the rid.
16e0: 0a 2a 2a 0a 2a 2a 20 49 66 20 73 72 63 49 64 20  .**.** If srcId 
16f0: 69 73 20 70 6f 73 69 74 69 76 65 2c 20 74 68 65  is positive, the
1700: 6e 20 61 20 64 65 6c 74 61 20 69 73 20 73 65 6e  n a delta is sen
1710: 74 20 61 67 61 69 6e 73 74 20 74 68 61 74 20 73  t against that s
1720: 72 63 49 64 2e 0a 2a 2a 20 49 66 20 73 72 63 49  rcId..** If srcI
1730: 64 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20  d is zero, then 
1740: 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61  an attempt is ma
1750: 64 65 20 74 6f 20 66 69 6e 64 20 61 6e 20 61 70  de to find an ap
1760: 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20 66 69 6c  propriate.** fil
1770: 65 20 74 6f 20 64 65 6c 74 61 20 61 67 61 69 6e  e to delta again
1780: 73 74 2e 20 20 20 49 66 20 73 72 63 49 64 20 69  st.   If srcId i
1790: 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 20  s negative, the 
17a0: 66 69 6c 65 20 69 73 20 73 65 6e 74 0a 2a 2a 20  file is sent.** 
17b0: 77 69 74 68 6f 75 74 20 64 65 6c 74 61 69 6e 67  without deltaing
17c0: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
17d0: 20 73 65 6e 64 5f 66 69 6c 65 28 58 66 65 72 20   send_file(Xfer 
17e0: 2a 70 58 66 65 72 2c 20 69 6e 74 20 72 69 64 2c  *pXfer, int rid,
17f0: 20 42 6c 6f 62 20 2a 70 55 75 69 64 2c 20 69 6e   Blob *pUuid, in
1800: 74 20 73 72 63 49 64 29 7b 0a 20 20 42 6c 6f 62  t srcId){.  Blob
1810: 20 63 6f 6e 74 65 6e 74 2c 20 75 75 69 64 3b 0a   content, uuid;.
1820: 20 20 69 6e 74 20 73 69 7a 65 20 3d 20 30 3b 0a    int size = 0;.
1830: 0a 20 20 69 66 28 20 64 62 5f 65 78 69 73 74 73  .  if( db_exists
1840: 28 22 53 45 4c 45 43 54 20 31 20 46 52 4f 4d 20  ("SELECT 1 FROM 
1850: 6f 6e 72 65 6d 6f 74 65 20 57 48 45 52 45 20 72  onremote WHERE r
1860: 69 64 3d 25 64 22 2c 20 72 69 64 29 20 29 7b 0a  id=%d", rid) ){.
1870: 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d       return;.  }
1880: 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 75 75  .  blob_zero(&uu
1890: 69 64 29 3b 0a 20 20 69 66 28 20 70 55 75 69 64  id);.  if( pUuid
18a0: 3d 3d 30 20 29 7b 0a 20 20 20 20 64 62 5f 62 6c  ==0 ){.    db_bl
18b0: 6f 62 28 26 75 75 69 64 2c 20 22 53 45 4c 45 43  ob(&uuid, "SELEC
18c0: 54 20 75 75 69 64 20 46 52 4f 4d 20 62 6c 6f 62  T uuid FROM blob
18d0: 20 57 48 45 52 45 20 72 69 64 3d 25 64 20 41 4e   WHERE rid=%d AN
18e0: 44 20 73 69 7a 65 3e 3d 30 22 2c 20 72 69 64 29  D size>=0", rid)
18f0: 3b 0a 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 73  ;.    if( blob_s
1900: 69 7a 65 28 26 75 75 69 64 29 3d 3d 30 20 29 7b  ize(&uuid)==0 ){
1910: 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
1920: 20 20 20 7d 0a 20 20 20 20 70 55 75 69 64 20 3d     }.    pUuid =
1930: 20 26 75 75 69 64 3b 0a 20 20 7d 0a 20 20 69 66   &uuid;.  }.  if
1940: 28 20 70 58 66 65 72 2d 3e 6d 78 53 65 6e 64 3c  ( pXfer->mxSend<
1950: 3d 62 6c 6f 62 5f 73 69 7a 65 28 70 58 66 65 72  =blob_size(pXfer
1960: 2d 3e 70 4f 75 74 29 20 29 7b 0a 20 20 20 20 62  ->pOut) ){.    b
1970: 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70 58 66 65  lob_appendf(pXfe
1980: 72 2d 3e 70 4f 75 74 2c 20 22 69 67 6f 74 20 25  r->pOut, "igot %
1990: 62 5c 6e 22 2c 20 70 55 75 69 64 29 3b 0a 20 20  b\n", pUuid);.  
19a0: 20 20 70 58 66 65 72 2d 3e 6e 49 47 6f 74 53 65    pXfer->nIGotSe
19b0: 6e 74 2b 2b 3b 0a 20 20 20 20 62 6c 6f 62 5f 72  nt++;.    blob_r
19c0: 65 73 65 74 28 26 75 75 69 64 29 3b 0a 20 20 20  eset(&uuid);.   
19d0: 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 63   return;.  }.  c
19e0: 6f 6e 74 65 6e 74 5f 67 65 74 28 72 69 64 2c 20  ontent_get(rid, 
19f0: 26 63 6f 6e 74 65 6e 74 29 3b 0a 0a 20 20 69 66  &content);..  if
1a00: 28 20 62 6c 6f 62 5f 73 69 7a 65 28 26 63 6f 6e  ( blob_size(&con
1a10: 74 65 6e 74 29 3e 31 30 30 20 29 7b 0a 20 20 20  tent)>100 ){.   
1a20: 20 73 69 7a 65 20 3d 20 73 65 6e 64 5f 61 73 5f   size = send_as_
1a30: 64 65 6c 74 61 28 70 58 66 65 72 2c 20 72 69 64  delta(pXfer, rid
1a40: 2c 20 26 63 6f 6e 74 65 6e 74 2c 20 70 55 75 69  , &content, pUui
1a50: 64 2c 20 73 72 63 49 64 29 3b 0a 20 20 7d 0a 20  d, srcId);.  }. 
1a60: 20 69 66 28 20 73 69 7a 65 3d 3d 30 20 29 7b 0a   if( size==0 ){.
1a70: 20 20 20 20 69 6e 74 20 73 69 7a 65 20 3d 20 62      int size = b
1a80: 6c 6f 62 5f 73 69 7a 65 28 26 63 6f 6e 74 65 6e  lob_size(&conten
1a90: 74 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70  t);.    blob_app
1aa0: 65 6e 64 66 28 70 58 66 65 72 2d 3e 70 4f 75 74  endf(pXfer->pOut
1ab0: 2c 20 22 66 69 6c 65 20 25 62 20 25 64 5c 6e 22  , "file %b %d\n"
1ac0: 2c 20 70 55 75 69 64 2c 20 73 69 7a 65 29 3b 0a  , pUuid, size);.
1ad0: 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 28      blob_append(
1ae0: 70 58 66 65 72 2d 3e 70 4f 75 74 2c 20 62 6c 6f  pXfer->pOut, blo
1af0: 62 5f 62 75 66 66 65 72 28 26 63 6f 6e 74 65 6e  b_buffer(&conten
1b00: 74 29 2c 20 73 69 7a 65 29 3b 0a 20 20 20 20 70  t), size);.    p
1b10: 58 66 65 72 2d 3e 6e 46 69 6c 65 53 65 6e 74 2b  Xfer->nFileSent+
1b20: 2b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  +;.  }else{.    
1b30: 70 58 66 65 72 2d 3e 6e 44 65 6c 74 61 53 65 6e  pXfer->nDeltaSen
1b40: 74 2b 2b 3b 0a 20 20 7d 0a 20 20 64 62 5f 6d 75  t++;.  }.  db_mu
1b50: 6c 74 69 5f 65 78 65 63 28 22 49 4e 53 45 52 54  lti_exec("INSERT
1b60: 20 49 4e 54 4f 20 6f 6e 72 65 6d 6f 74 65 20 56   INTO onremote V
1b70: 41 4c 55 45 53 28 25 64 29 22 2c 20 72 69 64 29  ALUES(%d)", rid)
1b80: 3b 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26  ;.  blob_reset(&
1b90: 75 75 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  uuid);.}../*.** 
1ba0: 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75 6e  This routine run
1bb0: 73 20 77 68 65 6e 20 65 69 74 68 65 72 20 63 6c  s when either cl
1bc0: 69 65 6e 74 20 6f 72 20 73 65 72 76 65 72 20 69  ient or server i
1bd0: 73 20 6e 6f 74 69 66 69 65 64 20 74 68 61 74 0a  s notified that.
1be0: 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 73 69 64  ** the other sid
1bf0: 65 20 74 68 69 6e 67 73 20 72 69 64 20 69 73 20  e things rid is 
1c00: 61 20 6c 65 61 66 20 6d 61 6e 69 66 65 73 74 2e  a leaf manifest.
1c10: 20 20 49 66 20 77 65 20 68 6f 6c 64 0a 2a 2a 20    If we hold.** 
1c20: 63 68 69 6c 64 72 65 6e 20 6f 66 20 72 69 64 2c  children of rid,
1c30: 20 74 68 65 6e 20 73 65 6e 64 20 74 68 65 6d 20   then send them 
1c40: 6f 76 65 72 20 74 6f 20 74 68 65 20 6f 74 68 65  over to the othe
1c50: 72 20 73 69 64 65 2e 0a 2a 2f 0a 73 74 61 74 69  r side..*/.stati
1c60: 63 20 76 6f 69 64 20 6c 65 61 66 5f 72 65 73 70  c void leaf_resp
1c70: 6f 6e 73 65 28 58 66 65 72 20 2a 70 58 66 65 72  onse(Xfer *pXfer
1c80: 2c 20 69 6e 74 20 72 69 64 29 7b 0a 20 20 53 74  , int rid){.  St
1c90: 6d 74 20 71 31 2c 20 71 32 3b 0a 20 20 64 62 5f  mt q1, q2;.  db_
1ca0: 70 72 65 70 61 72 65 28 26 71 31 2c 0a 20 20 20  prepare(&q1,.   
1cb0: 20 20 20 22 53 45 4c 45 43 54 20 63 69 64 2c 20     "SELECT cid, 
1cc0: 75 75 69 64 20 46 52 4f 4d 20 70 6c 69 6e 6b 2c  uuid FROM plink,
1cd0: 20 62 6c 6f 62 22 0a 20 20 20 20 20 20 22 20 57   blob".      " W
1ce0: 48 45 52 45 20 62 6c 6f 62 2e 72 69 64 3d 70 6c  HERE blob.rid=pl
1cf0: 69 6e 6b 2e 63 69 64 22 0a 20 20 20 20 20 20 22  ink.cid".      "
1d00: 20 20 20 41 4e 44 20 70 6c 69 6e 6b 2e 70 69 64     AND plink.pid
1d10: 3d 25 64 22 2c 0a 20 20 20 20 20 20 72 69 64 0a  =%d",.      rid.
1d20: 20 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 64 62    );.  while( db
1d30: 5f 73 74 65 70 28 26 71 31 29 3d 3d 53 51 4c 49  _step(&q1)==SQLI
1d40: 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 42 6c  TE_ROW ){.    Bl
1d50: 6f 62 20 75 75 69 64 3b 0a 20 20 20 20 69 6e 74  ob uuid;.    int
1d60: 20 63 69 64 3b 0a 0a 20 20 20 20 63 69 64 20 3d   cid;..    cid =
1d70: 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26   db_column_int(&
1d80: 71 31 2c 20 30 29 3b 0a 20 20 20 20 64 62 5f 65  q1, 0);.    db_e
1d90: 70 68 65 6d 65 72 61 6c 5f 62 6c 6f 62 28 26 71  phemeral_blob(&q
1da0: 31 2c 20 31 2c 20 26 75 75 69 64 29 3b 0a 20 20  1, 1, &uuid);.  
1db0: 20 20 73 65 6e 64 5f 66 69 6c 65 28 70 58 66 65    send_file(pXfe
1dc0: 72 2c 20 63 69 64 2c 20 26 75 75 69 64 2c 20 72  r, cid, &uuid, r
1dd0: 69 64 29 3b 0a 20 20 20 20 64 62 5f 70 72 65 70  id);.    db_prep
1de0: 61 72 65 28 26 71 32 2c 0a 20 20 20 20 20 20 20  are(&q2,.       
1df0: 22 53 45 4c 45 43 54 20 70 69 64 2c 20 75 75 69  "SELECT pid, uui
1e00: 64 2c 20 66 69 64 20 46 52 4f 4d 20 6d 6c 69 6e  d, fid FROM mlin
1e10: 6b 2c 20 62 6c 6f 62 22 0a 20 20 20 20 20 20 20  k, blob".       
1e20: 22 20 57 48 45 52 45 20 72 69 64 3d 66 69 64 20  " WHERE rid=fid 
1e30: 41 4e 44 20 6d 69 64 3d 25 64 22 2c 0a 20 20 20  AND mid=%d",.   
1e40: 20 20 20 20 63 69 64 0a 20 20 20 20 29 3b 0a 20      cid.    );. 
1e50: 20 20 20 77 68 69 6c 65 28 20 64 62 5f 73 74 65     while( db_ste
1e60: 70 28 26 71 32 29 3d 3d 53 51 4c 49 54 45 5f 52  p(&q2)==SQLITE_R
1e70: 4f 57 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  OW ){.      int 
1e80: 70 69 64 2c 20 66 69 64 3b 0a 20 20 20 20 20 20  pid, fid;.      
1e90: 70 69 64 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f  pid = db_column_
1ea0: 69 6e 74 28 26 71 32 2c 20 30 29 3b 0a 20 20 20  int(&q2, 0);.   
1eb0: 20 20 20 64 62 5f 65 70 68 65 6d 65 72 61 6c 5f     db_ephemeral_
1ec0: 62 6c 6f 62 28 26 71 32 2c 20 31 2c 20 26 75 75  blob(&q2, 1, &uu
1ed0: 69 64 29 3b 0a 20 20 20 20 20 20 66 69 64 20 3d  id);.      fid =
1ee0: 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26   db_column_int(&
1ef0: 71 32 2c 20 32 29 3b 0a 20 20 20 20 20 20 73 65  q2, 2);.      se
1f00: 6e 64 5f 66 69 6c 65 28 70 58 66 65 72 2c 20 66  nd_file(pXfer, f
1f10: 69 64 2c 20 26 75 75 69 64 2c 20 70 69 64 29 3b  id, &uuid, pid);
1f20: 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 5f 66 69  .    }.    db_fi
1f30: 6e 61 6c 69 7a 65 28 26 71 32 29 3b 0a 20 20 20  nalize(&q2);.   
1f40: 20 69 66 28 20 62 6c 6f 62 5f 73 69 7a 65 28 70   if( blob_size(p
1f50: 58 66 65 72 2d 3e 70 4f 75 74 29 3c 70 58 66 65  Xfer->pOut)<pXfe
1f60: 72 2d 3e 6d 78 53 65 6e 64 20 29 7b 0a 20 20 20  r->mxSend ){.   
1f70: 20 20 20 6c 65 61 66 5f 72 65 73 70 6f 6e 73 65     leaf_response
1f80: 28 70 58 66 65 72 2c 20 63 69 64 29 3b 0a 20 20  (pXfer, cid);.  
1f90: 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
1fa0: 20 53 65 6e 74 20 61 20 6c 65 61 66 20 6d 65 73   Sent a leaf mes
1fb0: 73 61 67 65 20 66 6f 72 20 65 76 65 72 79 20 6c  sage for every l
1fc0: 65 61 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  eaf..*/.static v
1fd0: 6f 69 64 20 73 65 6e 64 5f 6c 65 61 76 65 73 28  oid send_leaves(
1fe0: 58 66 65 72 20 2a 70 58 66 65 72 29 7b 0a 20 20  Xfer *pXfer){.  
1ff0: 53 74 6d 74 20 71 3b 0a 20 20 64 62 5f 70 72 65  Stmt q;.  db_pre
2000: 70 61 72 65 28 26 71 2c 20 0a 20 20 20 20 22 53  pare(&q, .    "S
2010: 45 4c 45 43 54 20 75 75 69 64 20 46 52 4f 4d 20  ELECT uuid FROM 
2020: 62 6c 6f 62 20 57 48 45 52 45 20 72 69 64 20 49  blob WHERE rid I
2030: 4e 22 0a 20 20 20 20 22 20 20 28 53 45 4c 45 43  N".    "  (SELEC
2040: 54 20 63 69 64 20 46 52 4f 4d 20 70 6c 69 6e 6b  T cid FROM plink
2050: 20 45 58 43 45 50 54 20 53 45 4c 45 43 54 20 70   EXCEPT SELECT p
2060: 69 64 20 46 52 4f 4d 20 70 6c 69 6e 6b 29 22 0a  id FROM plink)".
2070: 20 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 64 62    );.  while( db
2080: 5f 73 74 65 70 28 26 71 29 3d 3d 53 51 4c 49 54  _step(&q)==SQLIT
2090: 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 63 6f 6e  E_ROW ){.    con
20a0: 73 74 20 63 68 61 72 20 2a 7a 55 75 69 64 20 3d  st char *zUuid =
20b0: 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28   db_column_text(
20c0: 26 71 2c 20 30 29 3b 0a 20 20 20 20 62 6c 6f 62  &q, 0);.    blob
20d0: 5f 61 70 70 65 6e 64 66 28 70 58 66 65 72 2d 3e  _appendf(pXfer->
20e0: 70 4f 75 74 2c 20 22 6c 65 61 66 20 25 73 5c 6e  pOut, "leaf %s\n
20f0: 22 2c 20 7a 55 75 69 64 29 3b 0a 20 20 7d 0a 20  ", zUuid);.  }. 
2100: 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29   db_finalize(&q)
2110: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 6e 20 61  ;.}../*.** Sen a
2120: 20 67 69 6d 6d 65 20 6d 65 73 73 61 67 65 20 66   gimme message f
2130: 6f 72 20 65 76 65 72 79 20 70 68 61 6e 74 6f 6d  or every phantom
2140: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2150: 20 72 65 71 75 65 73 74 5f 70 68 61 6e 74 6f 6d   request_phantom
2160: 73 28 58 66 65 72 20 2a 70 58 66 65 72 29 7b 0a  s(Xfer *pXfer){.
2170: 20 20 53 74 6d 74 20 71 3b 0a 20 20 64 62 5f 70    Stmt q;.  db_p
2180: 72 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c 45  repare(&q, "SELE
2190: 43 54 20 75 75 69 64 20 46 52 4f 4d 20 70 68 61  CT uuid FROM pha
21a0: 6e 74 6f 6d 20 4a 4f 49 4e 20 62 6c 6f 62 20 55  ntom JOIN blob U
21b0: 53 49 4e 47 28 72 69 64 29 22 29 3b 0a 20 20 77  SING(rid)");.  w
21c0: 68 69 6c 65 28 20 64 62 5f 73 74 65 70 28 26 71  hile( db_step(&q
21d0: 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  )==SQLITE_ROW ){
21e0: 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
21f0: 2a 7a 55 75 69 64 20 3d 20 64 62 5f 63 6f 6c 75  *zUuid = db_colu
2200: 6d 6e 5f 74 65 78 74 28 26 71 2c 20 30 29 3b 0a  mn_text(&q, 0);.
2210: 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66      blob_appendf
2220: 28 70 58 66 65 72 2d 3e 70 4f 75 74 2c 20 22 67  (pXfer->pOut, "g
2230: 69 6d 6d 65 20 25 73 5c 6e 22 2c 20 7a 55 75 69  imme %s\n", zUui
2240: 64 29 3b 0a 20 20 20 20 70 58 66 65 72 2d 3e 6e  d);.    pXfer->n
2250: 47 69 6d 6d 65 53 65 6e 74 2b 2b 3b 0a 20 20 7d  GimmeSent++;.  }
2260: 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26  .  db_finalize(&
2270: 71 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68  q);.}.../*.** Ch
2280: 65 63 6b 20 74 68 65 20 73 69 67 6e 61 74 75 72  eck the signatur
2290: 65 20 6f 6e 20 61 6e 20 61 70 70 6c 69 63 61 74  e on an applicat
22a0: 69 6f 6e 2f 78 2d 66 6f 73 73 69 6c 20 70 61 79  ion/x-fossil pay
22b0: 6c 6f 61 64 20 72 65 63 65 69 76 65 64 20 62 79  load received by
22c0: 0a 2a 2a 20 74 68 65 20 48 54 54 50 20 73 65 72  .** the HTTP ser
22d0: 76 65 72 2e 20 20 54 68 65 20 73 69 67 6e 61 74  ver.  The signat
22e0: 75 72 65 20 69 73 20 61 20 6c 69 6e 65 20 6f 66  ure is a line of
22f0: 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   the following f
2300: 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  orm:.**.**      
2310: 20 20 6c 6f 67 69 6e 20 4c 4f 47 49 4e 20 4e 4f    login LOGIN NO
2320: 4e 43 45 20 53 49 47 4e 41 54 55 52 45 0a 2a 2a  NCE SIGNATURE.**
2330: 0a 2a 2a 20 54 68 65 20 4e 4f 4e 43 45 20 69 73  .** The NONCE is
2340: 20 61 20 72 61 6e 64 6f 6d 20 73 74 72 69 6e 67   a random string
2350: 2e 20 20 54 68 65 20 73 65 72 76 65 72 20 77 69  .  The server wi
2360: 6c 6c 20 6e 65 76 65 72 20 61 63 63 65 70 74 20  ll never accept 
2370: 61 0a 2a 2a 20 72 65 70 65 61 74 20 4e 4f 4e 43  a.** repeat NONC
2380: 45 2e 20 20 53 49 47 4e 41 54 55 52 45 20 69 73  E.  SIGNATURE is
2390: 20 74 68 65 20 53 48 41 31 20 63 68 65 63 6b 73   the SHA1 checks
23a0: 75 6d 20 6f 66 20 74 68 65 20 4e 4f 4e 43 45 20  um of the NONCE 
23b0: 0a 2a 2a 20 63 6f 6e 63 61 74 65 6e 61 74 65 64  .** concatenated
23c0: 20 77 69 74 68 20 74 68 65 20 75 73 65 72 73 20   with the users 
23d0: 70 61 73 73 77 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20  password..**.** 
23e0: 54 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 74  The parameters t
23f0: 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61  o this routine a
2400: 72 65 20 65 70 68 65 72 6d 65 72 61 6c 20 62 6c  re ephermeral bl
2410: 6f 62 73 20 68 6f 6c 64 69 6e 67 20 74 68 65 0a  obs holding the.
2420: 2a 2a 20 4c 4f 47 49 4e 2c 20 4e 4f 4e 43 45 20  ** LOGIN, NONCE 
2430: 61 6e 64 20 53 49 47 4e 41 54 55 52 45 2e 0a 2a  and SIGNATURE..*
2440: 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
2450: 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6c 6f  e attempts to lo
2460: 63 61 74 65 20 74 68 65 20 75 73 65 72 20 61 6e  cate the user an
2470: 64 20 76 65 72 69 66 79 20 74 68 65 20 73 69 67  d verify the sig
2480: 6e 61 74 75 72 65 2e 0a 2a 2a 20 49 66 20 65 76  nature..** If ev
2490: 65 72 79 74 68 69 6e 67 20 63 68 65 63 6b 73 20  erything checks 
24a0: 6f 75 74 2c 20 74 68 65 20 55 53 45 52 2e 43 41  out, the USER.CA
24b0: 50 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 74 68 65  P column for the
24c0: 20 55 53 45 52 20 74 61 62 6c 65 0a 2a 2a 20 69   USER table.** i
24d0: 73 20 63 6f 6e 73 75 6c 74 65 64 20 74 6f 20 73  s consulted to s
24e0: 65 74 20 70 72 69 76 69 6c 65 67 65 73 20 69 6e  et privileges in
24f0: 20 74 68 65 20 67 6c 6f 62 61 6c 20 67 20 76 61   the global g va
2500: 72 69 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  riable..**.** If
2510: 20 61 6e 79 74 68 69 6e 67 20 66 61 69 6c 73 20   anything fails 
2520: 74 6f 20 63 68 65 63 6b 20 6f 75 74 2c 20 6e 6f  to check out, no
2530: 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64   changes are mad
2540: 65 20 74 6f 20 70 72 69 76 69 6c 65 67 65 73 2e  e to privileges.
2550: 0a 2a 2a 0a 2a 2a 20 53 69 67 6e 61 74 75 72 65  .**.** Signature
2560: 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 6e 20 74   generation on t
2570: 68 65 20 63 6c 69 65 6e 74 20 73 69 64 65 20 69  he client side i
2580: 73 20 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65  s handled by the
2590: 20 0a 2a 2a 20 68 74 74 70 5f 65 78 63 68 61 6e   .** http_exchan
25a0: 67 65 28 29 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f  ge() routine..*/
25b0: 0a 76 6f 69 64 20 63 68 65 63 6b 5f 6c 6f 67 69  .void check_logi
25c0: 6e 28 42 6c 6f 62 20 2a 70 4c 6f 67 69 6e 2c 20  n(Blob *pLogin, 
25d0: 42 6c 6f 62 20 2a 70 4e 6f 6e 63 65 2c 20 42 6c  Blob *pNonce, Bl
25e0: 6f 62 20 2a 70 53 69 67 29 7b 0a 20 20 53 74 6d  ob *pSig){.  Stm
25f0: 74 20 71 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  t q;.  int rc;..
2600: 20 20 69 66 28 20 64 62 5f 65 78 69 73 74 73 28    if( db_exists(
2610: 22 53 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 72  "SELECT 1 FROM r
2620: 63 76 66 72 6f 6d 20 57 48 45 52 45 20 6e 6f 6e  cvfrom WHERE non
2630: 63 65 3d 25 42 22 2c 20 70 4e 6f 6e 63 65 29 20  ce=%B", pNonce) 
2640: 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 20 20  ){.    return;  
2650: 2f 2a 20 4e 65 76 65 72 20 61 63 63 65 70 74 20  /* Never accept 
2660: 61 20 72 65 70 65 61 74 65 64 20 6e 6f 6e 63 65  a repeated nonce
2670: 20 2a 2f 0a 20 20 7d 0a 20 20 64 62 5f 70 72 65   */.  }.  db_pre
2680: 70 61 72 65 28 26 71 2c 20 22 53 45 4c 45 43 54  pare(&q, "SELECT
2690: 20 70 77 2c 20 63 61 70 2c 20 75 69 64 20 46 52   pw, cap, uid FR
26a0: 4f 4d 20 75 73 65 72 20 57 48 45 52 45 20 6c 6f  OM user WHERE lo
26b0: 67 69 6e 3d 25 42 22 2c 20 70 4c 6f 67 69 6e 29  gin=%B", pLogin)
26c0: 3b 0a 20 20 69 66 28 20 64 62 5f 73 74 65 70 28  ;.  if( db_step(
26d0: 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20  &q)==SQLITE_ROW 
26e0: 29 7b 0a 20 20 20 20 42 6c 6f 62 20 70 77 2c 20  ){.    Blob pw, 
26f0: 63 6f 6d 62 69 6e 65 64 2c 20 68 61 73 68 3b 0a  combined, hash;.
2700: 20 20 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 70      blob_zero(&p
2710: 77 29 3b 0a 20 20 20 20 64 62 5f 65 70 68 65 6d  w);.    db_ephem
2720: 65 72 61 6c 5f 62 6c 6f 62 28 26 71 2c 20 30 2c  eral_blob(&q, 0,
2730: 20 26 70 77 29 3b 0a 20 20 20 20 62 6c 6f 62 5f   &pw);.    blob_
2740: 7a 65 72 6f 28 26 63 6f 6d 62 69 6e 65 64 29 3b  zero(&combined);
2750: 0a 20 20 20 20 62 6c 6f 62 5f 63 6f 70 79 28 26  .    blob_copy(&
2760: 63 6f 6d 62 69 6e 65 64 2c 20 70 4e 6f 6e 63 65  combined, pNonce
2770: 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65  );.    blob_appe
2780: 6e 64 28 26 63 6f 6d 62 69 6e 65 64 2c 20 62 6c  nd(&combined, bl
2790: 6f 62 5f 62 75 66 66 65 72 28 26 70 77 29 2c 20  ob_buffer(&pw), 
27a0: 62 6c 6f 62 5f 73 69 7a 65 28 26 70 77 29 29 3b  blob_size(&pw));
27b0: 0a 20 20 20 20 2f 2a 20 43 47 49 44 45 42 55 47  .    /* CGIDEBUG
27c0: 28 28 22 70 72 65 73 69 67 3d 5b 25 73 5d 5c 6e  (("presig=[%s]\n
27d0: 22 2c 20 62 6c 6f 62 5f 73 74 72 28 26 63 6f 6d  ", blob_str(&com
27e0: 62 69 6e 65 64 29 29 29 3b 20 2a 2f 0a 20 20 20  bined))); */.   
27f0: 20 73 68 61 31 73 75 6d 5f 62 6c 6f 62 28 26 63   sha1sum_blob(&c
2800: 6f 6d 62 69 6e 65 64 2c 20 26 68 61 73 68 29 3b  ombined, &hash);
2810: 0a 20 20 20 20 72 63 20 3d 20 62 6c 6f 62 5f 63  .    rc = blob_c
2820: 6f 6d 70 61 72 65 28 26 68 61 73 68 2c 20 70 53  ompare(&hash, pS
2830: 69 67 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 72 65  ig);.    blob_re
2840: 73 65 74 28 26 68 61 73 68 29 3b 0a 20 20 20 20  set(&hash);.    
2850: 62 6c 6f 62 5f 72 65 73 65 74 28 26 63 6f 6d 62  blob_reset(&comb
2860: 69 6e 65 64 29 3b 0a 20 20 20 20 69 66 28 20 72  ined);.    if( r
2870: 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 6f  c==0 ){.      co
2880: 6e 73 74 20 63 68 61 72 20 2a 7a 43 61 70 3b 0a  nst char *zCap;.
2890: 20 20 20 20 20 20 7a 43 61 70 20 3d 20 64 62 5f        zCap = db_
28a0: 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 26 71 2c 20  column_text(&q, 
28b0: 31 29 3b 0a 20 20 20 20 20 20 6c 6f 67 69 6e 5f  1);.      login_
28c0: 73 65 74 5f 63 61 70 61 62 69 6c 69 74 69 65 73  set_capabilities
28d0: 28 7a 43 61 70 29 3b 0a 20 20 20 20 20 20 67 2e  (zCap);.      g.
28e0: 75 73 65 72 55 69 64 20 3d 20 64 62 5f 63 6f 6c  userUid = db_col
28f0: 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 32 29 3b 0a  umn_int(&q, 2);.
2900: 20 20 20 20 20 20 67 2e 7a 4c 6f 67 69 6e 20 3d        g.zLogin =
2910: 20 6d 70 72 69 6e 74 66 28 22 25 62 22 2c 20 70   mprintf("%b", p
2920: 4c 6f 67 69 6e 29 3b 0a 20 20 20 20 20 20 67 2e  Login);.      g.
2930: 7a 4e 6f 6e 63 65 20 3d 20 6d 70 72 69 6e 74 66  zNonce = mprintf
2940: 28 22 25 62 22 2c 20 70 4e 6f 6e 63 65 29 3b 0a  ("%b", pNonce);.
2950: 20 20 20 20 7d 0a 20 20 7d 0a 20 20 64 62 5f 72      }.  }.  db_r
2960: 65 73 65 74 28 26 71 29 3b 0a 7d 0a 0a 0a 2f 2a  eset(&q);.}.../*
2970: 0a 2a 2a 20 49 66 20 74 68 69 73 20 76 61 72 69  .** If this vari
2980: 61 62 6c 65 20 69 73 20 73 65 74 2c 20 64 69 73  able is set, dis
2990: 61 62 6c 65 20 6c 6f 67 69 6e 20 63 68 65 63 6b  able login check
29a0: 73 2e 20 20 55 73 65 64 20 66 6f 72 20 64 65 62  s.  Used for deb
29b0: 75 67 67 69 6e 67 0a 2a 2a 20 6f 6e 6c 79 2e 0a  ugging.** only..
29c0: 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 69  */.static int di
29d0: 73 61 62 6c 65 4c 6f 67 69 6e 20 3d 20 30 3b 0a  sableLogin = 0;.
29e0: 0a 2f 2a 0a 2a 2a 20 57 45 42 50 41 47 45 3a 20  ./*.** WEBPAGE: 
29f0: 78 66 65 72 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  xfer.**.** This 
2a00: 69 73 20 74 68 65 20 74 72 61 6e 73 66 65 72 20  is the transfer 
2a10: 68 61 6e 64 6c 65 72 20 6f 6e 20 74 68 65 20 73  handler on the s
2a20: 65 72 76 65 72 20 73 69 64 65 2e 20 20 54 68 65  erver side.  The
2a30: 20 74 72 61 6e 73 66 65 72 0a 2a 2a 20 6d 65 73   transfer.** mes
2a40: 73 61 67 65 20 68 61 73 20 62 65 65 6e 20 75 6e  sage has been un
2a50: 63 6f 6d 70 72 65 73 73 65 64 20 61 6e 64 20 70  compressed and p
2a60: 6c 61 63 65 64 20 69 6e 20 74 68 65 20 67 2e 63  laced in the g.c
2a70: 67 69 49 6e 20 62 6c 6f 62 2e 0a 2a 2a 20 50 72  giIn blob..** Pr
2a80: 6f 63 65 73 73 20 74 68 69 73 20 6d 65 73 73 61  ocess this messa
2a90: 67 65 20 61 6e 64 20 66 6f 72 6d 20 61 6e 20 61  ge and form an a
2aa0: 70 70 72 6f 70 72 69 61 74 65 20 72 65 70 6c 79  ppropriate reply
2ab0: 2e 0a 2a 2f 0a 76 6f 69 64 20 70 61 67 65 5f 78  ..*/.void page_x
2ac0: 66 65 72 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74  fer(void){.  int
2ad0: 20 69 73 50 75 6c 6c 20 3d 20 30 3b 0a 20 20 69   isPull = 0;.  i
2ae0: 6e 74 20 69 73 50 75 73 68 20 3d 20 30 3b 0a 20  nt isPush = 0;. 
2af0: 20 69 6e 74 20 6e 45 72 72 20 3d 20 30 3b 0a 20   int nErr = 0;. 
2b00: 20 58 66 65 72 20 78 66 65 72 3b 0a 0a 20 20 6d   Xfer xfer;..  m
2b10: 65 6d 73 65 74 28 26 78 66 65 72 2c 20 30 2c 20  emset(&xfer, 0, 
2b20: 73 69 7a 65 6f 66 28 78 66 65 72 29 29 3b 0a 20  sizeof(xfer));. 
2b30: 20 62 6c 6f 62 61 72 72 61 79 5f 7a 65 72 6f 28   blobarray_zero(
2b40: 78 66 65 72 2e 61 54 6f 6b 65 6e 2c 20 63 6f 75  xfer.aToken, cou
2b50: 6e 74 28 78 66 65 72 2e 61 54 6f 6b 65 6e 29 29  nt(xfer.aToken))
2b60: 3b 0a 20 20 63 67 69 5f 73 65 74 5f 63 6f 6e 74  ;.  cgi_set_cont
2b70: 65 6e 74 5f 74 79 70 65 28 67 2e 7a 43 6f 6e 74  ent_type(g.zCont
2b80: 65 6e 74 54 79 70 65 29 3b 0a 20 20 62 6c 6f 62  entType);.  blob
2b90: 5f 7a 65 72 6f 28 26 78 66 65 72 2e 65 72 72 29  _zero(&xfer.err)
2ba0: 3b 0a 20 20 78 66 65 72 2e 70 49 6e 20 3d 20 26  ;.  xfer.pIn = &
2bb0: 67 2e 63 67 69 49 6e 3b 0a 20 20 78 66 65 72 2e  g.cgiIn;.  xfer.
2bc0: 70 4f 75 74 20 3d 20 63 67 69 5f 6f 75 74 70 75  pOut = cgi_outpu
2bd0: 74 5f 62 6c 6f 62 28 29 3b 0a 20 20 78 66 65 72  t_blob();.  xfer
2be0: 2e 6d 78 53 65 6e 64 20 3d 20 64 62 5f 67 65 74  .mxSend = db_get
2bf0: 5f 69 6e 74 28 22 6d 61 78 2d 64 6f 77 6e 6c 6f  _int("max-downlo
2c00: 61 64 22 2c 20 31 30 30 30 30 30 30 29 3b 0a 0a  ad", 1000000);..
2c10: 20 20 64 62 5f 62 65 67 69 6e 5f 74 72 61 6e 73    db_begin_trans
2c20: 61 63 74 69 6f 6e 28 29 3b 0a 20 20 64 62 5f 6d  action();.  db_m
2c30: 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 20 20  ulti_exec(.     
2c40: 22 43 52 45 41 54 45 20 54 45 4d 50 20 54 41 42  "CREATE TEMP TAB
2c50: 4c 45 20 6f 6e 72 65 6d 6f 74 65 28 72 69 64 20  LE onremote(rid 
2c60: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
2c70: 4b 45 59 29 3b 22 0a 20 20 29 3b 0a 20 20 77 68  KEY);".  );.  wh
2c80: 69 6c 65 28 20 62 6c 6f 62 5f 6c 69 6e 65 28 78  ile( blob_line(x
2c90: 66 65 72 2e 70 49 6e 2c 20 26 78 66 65 72 2e 6c  fer.pIn, &xfer.l
2ca0: 69 6e 65 29 20 29 7b 0a 20 20 20 20 78 66 65 72  ine) ){.    xfer
2cb0: 2e 6e 54 6f 6b 65 6e 20 3d 20 62 6c 6f 62 5f 74  .nToken = blob_t
2cc0: 6f 6b 65 6e 69 7a 65 28 26 78 66 65 72 2e 6c 69  okenize(&xfer.li
2cd0: 6e 65 2c 20 78 66 65 72 2e 61 54 6f 6b 65 6e 2c  ne, xfer.aToken,
2ce0: 20 63 6f 75 6e 74 28 78 66 65 72 2e 61 54 6f 6b   count(xfer.aTok
2cf0: 65 6e 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 20 20  en));..    /*   
2d00: 66 69 6c 65 20 55 55 49 44 20 53 49 5a 45 20 5c  file UUID SIZE \
2d10: 6e 20 43 4f 4e 54 45 4e 54 0a 20 20 20 20 2a 2a  n CONTENT.    **
2d20: 20 20 20 66 69 6c 65 20 55 55 49 44 20 44 45 4c     file UUID DEL
2d30: 54 41 53 52 43 20 53 49 5a 45 20 5c 6e 20 43 4f  TASRC SIZE \n CO
2d40: 4e 54 45 4e 54 0a 20 20 20 20 2a 2a 0a 20 20 20  NTENT.    **.   
2d50: 20 2a 2a 20 41 63 63 65 70 74 20 61 20 66 69 6c   ** Accept a fil
2d60: 65 20 66 72 6f 6d 20 74 68 65 20 63 6c 69 65 6e  e from the clien
2d70: 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  t..    */.    if
2d80: 28 20 62 6c 6f 62 5f 65 71 28 26 78 66 65 72 2e  ( blob_eq(&xfer.
2d90: 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 66 69 6c 65  aToken[0], "file
2da0: 22 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ") ){.      if( 
2db0: 21 69 73 50 75 73 68 20 29 7b 0a 20 20 20 20 20  !isPush ){.     
2dc0: 20 20 20 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e     cgi_reset_con
2dd0: 74 65 6e 74 28 29 3b 0a 20 20 20 20 20 20 20 20  tent();.        
2de0: 40 20 65 72 72 6f 72 20 6e 6f 74 5c 73 61 75 74  @ error not\saut
2df0: 68 6f 72 69 7a 65 64 5c 73 74 6f 5c 73 77 72 69  horized\sto\swri
2e00: 74 65 0a 20 20 20 20 20 20 20 20 6e 45 72 72 2b  te.        nErr+
2e10: 2b 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  +;.        break
2e20: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2e30: 78 66 65 72 5f 61 63 63 65 70 74 5f 66 69 6c 65  xfer_accept_file
2e40: 28 26 78 66 65 72 29 3b 0a 20 20 20 20 20 20 69  (&xfer);.      i
2e50: 66 28 20 62 6c 6f 62 5f 73 69 7a 65 28 26 78 66  f( blob_size(&xf
2e60: 65 72 2e 65 72 72 29 20 29 7b 0a 20 20 20 20 20  er.err) ){.     
2e70: 20 20 20 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e     cgi_reset_con
2e80: 74 65 6e 74 28 29 3b 0a 20 20 20 20 20 20 20 20  tent();.        
2e90: 40 20 65 72 72 6f 72 20 25 54 28 62 6c 6f 62 5f  @ error %T(blob_
2ea0: 73 74 72 28 26 78 66 65 72 2e 65 72 72 29 29 0a  str(&xfer.err)).
2eb0: 20 20 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a          nErr++;.
2ec0: 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2ed0: 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
2ee0: 0a 0a 20 20 20 20 2f 2a 20 20 20 67 69 6d 6d 65  ..    /*   gimme
2ef0: 20 55 55 49 44 0a 20 20 20 20 2a 2a 0a 20 20 20   UUID.    **.   
2f00: 20 2a 2a 20 43 6c 69 65 6e 74 20 69 73 20 72 65   ** Client is re
2f10: 71 75 65 73 74 69 6e 67 20 61 20 66 69 6c 65 0a  questing a file.
2f20: 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 62      */.    if( b
2f30: 6c 6f 62 5f 65 71 28 26 78 66 65 72 2e 61 54 6f  lob_eq(&xfer.aTo
2f40: 6b 65 6e 5b 30 5d 2c 20 22 67 69 6d 6d 65 22 29  ken[0], "gimme")
2f50: 0a 20 20 20 20 20 26 26 20 78 66 65 72 2e 6e 54  .     && xfer.nT
2f60: 6f 6b 65 6e 3d 3d 32 0a 20 20 20 20 20 26 26 20  oken==2.     && 
2f70: 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26 78 66  blob_is_uuid(&xf
2f80: 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d 29 0a 20 20  er.aToken[1]).  
2f90: 20 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69    ){.      if( i
2fa0: 73 50 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20  sPull ){.       
2fb0: 20 69 6e 74 20 72 69 64 20 3d 20 72 69 64 5f 66   int rid = rid_f
2fc0: 72 6f 6d 5f 75 75 69 64 28 26 78 66 65 72 2e 61  rom_uuid(&xfer.a
2fd0: 54 6f 6b 65 6e 5b 31 5d 2c 20 30 29 3b 0a 20 20  Token[1], 0);.  
2fe0: 20 20 20 20 20 20 69 66 28 20 72 69 64 20 29 7b        if( rid ){
2ff0: 0a 20 20 20 20 20 20 20 20 20 20 73 65 6e 64 5f  .          send_
3000: 66 69 6c 65 28 26 78 66 65 72 2c 20 72 69 64 2c  file(&xfer, rid,
3010: 20 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d   &xfer.aToken[1]
3020: 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  , 0);.        }.
3030: 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
3040: 65 0a 0a 20 20 20 20 2f 2a 20 20 20 69 67 6f 74  e..    /*   igot
3050: 20 55 55 49 44 0a 20 20 20 20 2a 2a 0a 20 20 20   UUID.    **.   
3060: 20 2a 2a 20 43 6c 69 65 6e 74 20 61 6e 6e 6f 75   ** Client annou
3070: 6e 63 65 73 20 74 68 61 74 20 69 74 20 68 61 73  nces that it has
3080: 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 66 69   a particular fi
3090: 6c 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  le.    */.    if
30a0: 28 20 78 66 65 72 2e 6e 54 6f 6b 65 6e 3d 3d 32  ( xfer.nToken==2
30b0: 0a 20 20 20 20 20 26 26 20 62 6c 6f 62 5f 65 71  .     && blob_eq
30c0: 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d  (&xfer.aToken[0]
30d0: 2c 20 22 69 67 6f 74 22 29 0a 20 20 20 20 20 26  , "igot").     &
30e0: 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26  & blob_is_uuid(&
30f0: 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d 29 0a  xfer.aToken[1]).
3100: 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 66 28      ){.      if(
3110: 20 69 73 50 75 73 68 20 29 7b 0a 20 20 20 20 20   isPush ){.     
3120: 20 20 20 72 69 64 5f 66 72 6f 6d 5f 75 75 69 64     rid_from_uuid
3130: 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d  (&xfer.aToken[1]
3140: 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , 1);.      }.  
3150: 20 20 7d 65 6c 73 65 0a 20 20 0a 20 20 20 20 0a    }else.  .    .
3160: 20 20 20 20 2f 2a 20 20 20 6c 65 61 66 20 55 55      /*   leaf UU
3170: 49 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  ID.    **.    **
3180: 20 43 6c 69 65 6e 74 20 61 6e 6e 6f 75 6e 63 65   Client announce
3190: 73 20 74 68 61 74 20 69 74 20 68 61 73 20 61 20  s that it has a 
31a0: 70 61 72 74 69 63 75 6c 61 72 20 6d 61 6e 69 66  particular manif
31b0: 65 73 74 2e 20 20 49 66 0a 20 20 20 20 2a 2a 20  est.  If.    ** 
31c0: 74 68 65 20 73 65 72 76 65 72 20 68 61 73 20 63  the server has c
31d0: 68 69 6c 64 72 65 6e 20 6f 66 20 74 68 69 73 20  hildren of this 
31e0: 6c 65 61 66 2c 20 74 68 65 6e 20 73 65 6e 64 20  leaf, then send 
31f0: 74 68 6f 73 65 0a 20 20 20 20 2a 2a 20 63 68 69  those.    ** chi
3200: 6c 64 72 65 6e 20 62 61 63 6b 20 74 6f 20 74 68  ldren back to th
3210: 65 20 63 6c 69 65 6e 74 2e 20 20 49 66 20 74 68  e client.  If th
3220: 65 20 73 65 72 76 65 72 20 6c 61 63 6b 73 20 74  e server lacks t
3230: 68 69 73 0a 20 20 20 20 2a 2a 20 6c 65 61 66 2c  his.    ** leaf,
3240: 20 72 65 71 75 65 73 74 20 69 74 2e 0a 20 20 20   request it..   
3250: 20 2a 2f 0a 20 20 20 20 69 66 28 20 78 66 65 72   */.    if( xfer
3260: 2e 6e 54 6f 6b 65 6e 3d 3d 32 0a 20 20 20 20 20  .nToken==2.     
3270: 26 26 20 62 6c 6f 62 5f 65 71 28 26 78 66 65 72  && blob_eq(&xfer
3280: 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 6c 65 61  .aToken[0], "lea
3290: 66 22 29 0a 20 20 20 20 20 26 26 20 62 6c 6f 62  f").     && blob
32a0: 5f 69 73 5f 75 75 69 64 28 26 78 66 65 72 2e 61  _is_uuid(&xfer.a
32b0: 54 6f 6b 65 6e 5b 31 5d 29 0a 20 20 20 20 29 7b  Token[1]).    ){
32c0: 0a 20 20 20 20 20 20 69 6e 74 20 72 69 64 20 3d  .      int rid =
32d0: 20 72 69 64 5f 66 72 6f 6d 5f 75 75 69 64 28 26   rid_from_uuid(&
32e0: 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d 2c 20  xfer.aToken[1], 
32f0: 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 73  0);.      if( is
3300: 50 75 6c 6c 20 26 26 20 72 69 64 20 29 7b 0a 20  Pull && rid ){. 
3310: 20 20 20 20 20 20 20 6c 65 61 66 5f 72 65 73 70         leaf_resp
3320: 6f 6e 73 65 28 26 78 66 65 72 2c 20 72 69 64 29  onse(&xfer, rid)
3330: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
3340: 69 66 28 20 69 73 50 75 73 68 20 26 26 20 21 72  if( isPush && !r
3350: 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f  id ){.        co
3360: 6e 74 65 6e 74 5f 70 75 74 28 30 2c 20 62 6c 6f  ntent_put(0, blo
3370: 62 5f 73 74 72 28 26 78 66 65 72 2e 61 54 6f 6b  b_str(&xfer.aTok
3380: 65 6e 5b 31 5d 29 2c 20 30 29 3b 0a 20 20 20 20  en[1]), 0);.    
3390: 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 0a 0a 20    }.    }else.. 
33a0: 20 20 20 2f 2a 20 20 20 20 70 75 6c 6c 20 20 53     /*    pull  S
33b0: 45 52 56 45 52 43 4f 44 45 20 20 50 52 4f 4a 45  ERVERCODE  PROJE
33c0: 43 54 43 4f 44 45 0a 20 20 20 20 2a 2a 20 20 20  CTCODE.    **   
33d0: 20 70 75 73 68 20 20 53 45 52 56 45 52 43 4f 44   push  SERVERCOD
33e0: 45 20 20 50 52 4f 4a 45 43 54 43 4f 44 45 0a 20  E  PROJECTCODE. 
33f0: 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65     **.    ** The
3400: 20 63 6c 69 65 6e 74 20 77 61 6e 74 73 20 65 69   client wants ei
3410: 74 68 65 72 20 73 65 6e 64 20 6f 72 20 72 65 63  ther send or rec
3420: 65 69 76 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20  eive.    */.    
3430: 69 66 28 20 78 66 65 72 2e 6e 54 6f 6b 65 6e 3d  if( xfer.nToken=
3440: 3d 33 0a 20 20 20 20 20 26 26 20 28 62 6c 6f 62  =3.     && (blob
3450: 5f 65 71 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e  _eq(&xfer.aToken
3460: 5b 30 5d 2c 20 22 70 75 6c 6c 22 29 20 7c 7c 20  [0], "pull") || 
3470: 62 6c 6f 62 5f 65 71 28 26 78 66 65 72 2e 61 54  blob_eq(&xfer.aT
3480: 6f 6b 65 6e 5b 30 5d 2c 20 22 70 75 73 68 22 29  oken[0], "push")
3490: 29 0a 20 20 20 20 20 26 26 20 62 6c 6f 62 5f 69  ).     && blob_i
34a0: 73 5f 75 75 69 64 28 26 78 66 65 72 2e 61 54 6f  s_uuid(&xfer.aTo
34b0: 6b 65 6e 5b 31 5d 29 0a 20 20 20 20 20 26 26 20  ken[1]).     && 
34c0: 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26 78 66  blob_is_uuid(&xf
34d0: 65 72 2e 61 54 6f 6b 65 6e 5b 32 5d 29 0a 20 20  er.aToken[2]).  
34e0: 20 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74    ){.      const
34f0: 20 63 68 61 72 20 2a 7a 53 43 6f 64 65 3b 0a 20   char *zSCode;. 
3500: 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
3510: 2a 7a 50 43 6f 64 65 3b 0a 0a 20 20 20 20 20 20  *zPCode;..      
3520: 7a 53 43 6f 64 65 20 3d 20 64 62 5f 67 65 74 28  zSCode = db_get(
3530: 22 73 65 72 76 65 72 2d 63 6f 64 65 22 2c 20 30  "server-code", 0
3540: 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 53 43  );.      if( zSC
3550: 6f 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ode==0 ){.      
3560: 20 20 66 6f 73 73 69 6c 5f 70 61 6e 69 63 28 22    fossil_panic("
3570: 6d 69 73 73 69 6e 67 20 73 65 72 76 65 72 20 63  missing server c
3580: 6f 64 65 22 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ode");.      }. 
3590: 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71       if( blob_eq
35a0: 5f 73 74 72 28 26 78 66 65 72 2e 61 54 6f 6b 65  _str(&xfer.aToke
35b0: 6e 5b 31 5d 2c 20 7a 53 43 6f 64 65 2c 20 2d 31  n[1], zSCode, -1
35c0: 29 20 29 7b 0a 20 20 20 20 20 20 20 20 63 67 69  ) ){.        cgi
35d0: 5f 72 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29  _reset_content()
35e0: 3b 0a 20 20 20 20 20 20 20 20 40 20 65 72 72 6f  ;.        @ erro
35f0: 72 20 73 65 72 76 65 72 5c 73 6c 6f 6f 70 0a 20  r server\sloop. 
3600: 20 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20         nErr++;. 
3610: 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3620: 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 50 43 6f      }.      zPCo
3630: 64 65 20 3d 20 64 62 5f 67 65 74 28 22 70 72 6f  de = db_get("pro
3640: 6a 65 63 74 2d 63 6f 64 65 22 2c 20 30 29 3b 0a  ject-code", 0);.
3650: 20 20 20 20 20 20 69 66 28 20 7a 50 43 6f 64 65        if( zPCode
3660: 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 66  ==0 ){.        f
3670: 6f 73 73 69 6c 5f 70 61 6e 69 63 28 22 6d 69 73  ossil_panic("mis
3680: 73 69 6e 67 20 70 72 6f 6a 65 63 74 20 63 6f 64  sing project cod
3690: 65 22 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  e");.      }.   
36a0: 20 20 20 69 66 28 20 21 62 6c 6f 62 5f 65 71 5f     if( !blob_eq_
36b0: 73 74 72 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e  str(&xfer.aToken
36c0: 5b 32 5d 2c 20 7a 50 43 6f 64 65 2c 20 2d 31 29  [2], zPCode, -1)
36d0: 20 29 7b 0a 20 20 20 20 20 20 20 20 63 67 69 5f   ){.        cgi_
36e0: 72 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b  reset_content();
36f0: 0a 20 20 20 20 20 20 20 20 40 20 65 72 72 6f 72  .        @ error
3700: 20 77 72 6f 6e 67 5c 73 70 72 6f 6a 65 63 74 0a   wrong\sproject.
3710: 20 20 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a          nErr++;.
3720: 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3730: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6c 6f 67       }.      log
3740: 69 6e 5f 63 68 65 63 6b 5f 63 72 65 64 65 6e 74  in_check_credent
3750: 69 61 6c 73 28 29 3b 0a 20 20 20 20 20 20 69 66  ials();.      if
3760: 28 20 62 6c 6f 62 5f 65 71 28 26 78 66 65 72 2e  ( blob_eq(&xfer.
3770: 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 70 75 6c 6c  aToken[0], "pull
3780: 22 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  ") ){.        if
3790: 28 20 21 67 2e 6f 6b 52 65 61 64 20 29 7b 0a 20  ( !g.okRead ){. 
37a0: 20 20 20 20 20 20 20 20 20 63 67 69 5f 72 65 73           cgi_res
37b0: 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20  et_content();.  
37c0: 20 20 20 20 20 20 20 20 40 20 65 72 72 6f 72 20          @ error 
37d0: 6e 6f 74 5c 73 61 75 74 68 6f 72 69 7a 65 64 5c  not\sauthorized\
37e0: 73 74 6f 5c 73 72 65 61 64 0a 20 20 20 20 20 20  sto\sread.      
37f0: 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20      nErr++;.    
3800: 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3810: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
3820: 73 50 75 6c 6c 20 3d 20 31 3b 0a 20 20 20 20 20  sPull = 1;.     
3830: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
3840: 69 66 28 20 21 67 2e 6f 6b 57 72 69 74 65 20 29  if( !g.okWrite )
3850: 7b 0a 20 20 20 20 20 20 20 20 20 20 63 67 69 5f  {.          cgi_
3860: 72 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b  reset_content();
3870: 0a 20 20 20 20 20 20 20 20 20 20 40 20 65 72 72  .          @ err
3880: 6f 72 20 6e 6f 74 5c 73 61 75 74 68 6f 72 69 7a  or not\sauthoriz
3890: 65 64 5c 73 74 6f 5c 73 77 72 69 74 65 0a 20 20  ed\sto\swrite.  
38a0: 20 20 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a          nErr++;.
38b0: 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
38c0: 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
38d0: 20 20 20 73 65 6e 64 5f 6c 65 61 76 65 73 28 26     send_leaves(&
38e0: 78 66 65 72 29 3b 0a 20 20 20 20 20 20 20 20 69  xfer);.        i
38f0: 73 50 75 73 68 20 3d 20 31 3b 0a 20 20 20 20 20  sPush = 1;.     
3900: 20 7d 0a 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20   }.    }else..  
3910: 20 20 2f 2a 20 20 20 20 63 6c 6f 6e 65 0a 20 20    /*    clone.  
3920: 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20    **.    ** The 
3930: 63 6c 69 65 6e 74 20 6b 6e 6f 77 73 20 6e 6f 74  client knows not
3940: 68 69 6e 67 2e 20 20 54 65 6c 6c 20 61 6c 6c 2e  hing.  Tell all.
3950: 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
3960: 62 6c 6f 62 5f 65 71 28 26 78 66 65 72 2e 61 54  blob_eq(&xfer.aT
3970: 6f 6b 65 6e 5b 30 5d 2c 20 22 63 6c 6f 6e 65 22  oken[0], "clone"
3980: 29 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72  ) ){.      int r
3990: 6f 6f 74 69 64 3b 0a 20 20 20 20 20 20 6c 6f 67  ootid;.      log
39a0: 69 6e 5f 63 68 65 63 6b 5f 63 72 65 64 65 6e 74  in_check_credent
39b0: 69 61 6c 73 28 29 3b 0a 20 20 20 20 20 20 69 66  ials();.      if
39c0: 28 20 21 67 2e 6f 6b 43 6c 6f 6e 65 20 29 7b 0a  ( !g.okClone ){.
39d0: 20 20 20 20 20 20 20 20 63 67 69 5f 72 65 73 65          cgi_rese
39e0: 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 20  t_content();.   
39f0: 20 20 20 20 20 40 20 65 72 72 6f 72 20 6e 6f 74       @ error not
3a00: 5c 73 61 75 74 68 6f 72 69 7a 65 64 5c 73 74 6f  \sauthorized\sto
3a10: 5c 73 63 6c 6f 6e 65 0a 20 20 20 20 20 20 20 20  \sclone.        
3a20: 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20  nErr++;.        
3a30: 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
3a40: 20 20 20 20 20 69 73 50 75 6c 6c 20 3d 20 31 3b       isPull = 1;
3a50: 0a 20 20 20 20 20 20 40 20 70 75 73 68 20 25 73  .      @ push %s
3a60: 28 64 62 5f 67 65 74 28 22 73 65 72 76 65 72 2d  (db_get("server-
3a70: 63 6f 64 65 22 2c 20 22 78 22 29 29 20 25 73 28  code", "x")) %s(
3a80: 64 62 5f 67 65 74 28 22 70 72 6f 6a 65 63 74 2d  db_get("project-
3a90: 63 6f 64 65 22 2c 20 22 78 22 29 29 0a 20 20 20  code", "x")).   
3aa0: 20 20 20 72 6f 6f 74 69 64 20 3d 20 64 62 5f 69     rootid = db_i
3ab0: 6e 74 28 30 2c 20 0a 20 20 20 20 20 20 20 20 20  nt(0, .         
3ac0: 20 22 53 45 4c 45 43 54 20 70 69 64 20 46 52 4f   "SELECT pid FRO
3ad0: 4d 20 70 6c 69 6e 6b 20 41 53 20 61 22 0a 20 20  M plink AS a".  
3ae0: 20 20 20 20 20 20 20 20 22 20 57 48 45 52 45 20          " WHERE 
3af0: 4e 4f 54 20 45 58 49 53 54 53 28 53 45 4c 45 43  NOT EXISTS(SELEC
3b00: 54 20 31 20 46 52 4f 4d 20 70 6c 69 6e 6b 20 57  T 1 FROM plink W
3b10: 48 45 52 45 20 63 69 64 3d 61 2e 70 69 64 29 22  HERE cid=a.pid)"
3b20: 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20  .      );.      
3b30: 69 66 28 20 72 6f 6f 74 69 64 20 29 7b 0a 20 20  if( rootid ){.  
3b40: 20 20 20 20 20 20 73 65 6e 64 5f 66 69 6c 65 28        send_file(
3b50: 26 78 66 65 72 2c 20 72 6f 6f 74 69 64 2c 20 30  &xfer, rootid, 0
3b60: 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 6c  , -1);.        l
3b70: 65 61 66 5f 72 65 73 70 6f 6e 73 65 28 26 78 66  eaf_response(&xf
3b80: 65 72 2c 20 72 6f 6f 74 69 64 29 3b 0a 20 20 20  er, rootid);.   
3b90: 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 0a 0a     }.    }else..
3ba0: 20 20 20 20 2f 2a 20 20 20 20 6c 6f 67 69 6e 20      /*    login 
3bb0: 20 55 53 45 52 20 20 4e 4f 4e 43 45 20 20 53 49   USER  NONCE  SI
3bc0: 47 4e 41 54 55 52 45 0a 20 20 20 20 2a 2a 0a 20  GNATURE.    **. 
3bd0: 20 20 20 2a 2a 20 43 68 65 63 6b 20 66 6f 72 20     ** Check for 
3be0: 61 20 76 61 6c 69 64 20 6c 6f 67 69 6e 2e 20 20  a valid login.  
3bf0: 54 68 69 73 20 68 61 73 20 74 6f 20 68 61 70 70  This has to happ
3c00: 65 6e 20 62 65 66 6f 72 65 20 61 6e 79 74 68 69  en before anythi
3c10: 6e 67 20 65 6c 73 65 2e 0a 20 20 20 20 2a 2f 0a  ng else..    */.
3c20: 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28      if( blob_eq(
3c30: 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c  &xfer.aToken[0],
3c40: 20 22 6c 6f 67 69 6e 22 29 0a 20 20 20 20 20 26   "login").     &
3c50: 26 20 78 66 65 72 2e 6e 54 6f 6b 65 6e 3d 3d 34  & xfer.nToken==4
3c60: 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 66  .    ){.      if
3c70: 28 20 64 69 73 61 62 6c 65 4c 6f 67 69 6e 20 29  ( disableLogin )
3c80: 7b 0a 20 20 20 20 20 20 20 20 67 2e 6f 6b 52 65  {.        g.okRe
3c90: 61 64 20 3d 20 67 2e 6f 6b 57 72 69 74 65 20 3d  ad = g.okWrite =
3ca0: 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b   1;.      }else{
3cb0: 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 5f 6c  .        check_l
3cc0: 6f 67 69 6e 28 26 78 66 65 72 2e 61 54 6f 6b 65  ogin(&xfer.aToke
3cd0: 6e 5b 31 5d 2c 20 26 78 66 65 72 2e 61 54 6f 6b  n[1], &xfer.aTok
3ce0: 65 6e 5b 32 5d 2c 20 26 78 66 65 72 2e 61 54 6f  en[2], &xfer.aTo
3cf0: 6b 65 6e 5b 33 5d 29 3b 0a 20 20 20 20 20 20 7d  ken[3]);.      }
3d00: 0a 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20 20  .    }else..    
3d10: 2f 2a 20 55 6e 6b 6e 6f 77 6e 20 6d 65 73 73 61  /* Unknown messa
3d20: 67 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7b 0a  ge.    */.    {.
3d30: 20 20 20 20 20 20 63 67 69 5f 72 65 73 65 74 5f        cgi_reset_
3d40: 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 20 20 20  content();.     
3d50: 20 40 20 65 72 72 6f 72 20 62 61 64 5c 73 63 6f   @ error bad\sco
3d60: 6d 6d 61 6e 64 3a 5c 73 25 46 28 62 6c 6f 62 5f  mmand:\s%F(blob_
3d70: 73 74 72 28 26 78 66 65 72 2e 6c 69 6e 65 29 29  str(&xfer.line))
3d80: 0a 20 20 20 20 7d 0a 20 20 20 20 62 6c 6f 62 61  .    }.    bloba
3d90: 72 72 61 79 5f 72 65 73 65 74 28 78 66 65 72 2e  rray_reset(xfer.
3da0: 61 54 6f 6b 65 6e 2c 20 78 66 65 72 2e 6e 54 6f  aToken, xfer.nTo
3db0: 6b 65 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  ken);.  }.  if( 
3dc0: 69 73 50 75 73 68 20 29 7b 0a 20 20 20 20 72 65  isPush ){.    re
3dd0: 71 75 65 73 74 5f 70 68 61 6e 74 6f 6d 73 28 26  quest_phantoms(&
3de0: 78 66 65 72 29 3b 0a 20 20 7d 0a 20 20 64 62 5f  xfer);.  }.  db_
3df0: 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28  end_transaction(
3e00: 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d  0);.}../*.** COM
3e10: 4d 41 4e 44 3a 20 74 65 73 74 2d 78 66 65 72 0a  MAND: test-xfer.
3e20: 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 6f 6d 6d 61  **.** This comma
3e30: 6e 64 20 69 73 20 75 73 65 64 20 66 6f 72 20 64  nd is used for d
3e40: 65 62 75 67 67 69 6e 67 20 74 68 65 20 73 65 72  ebugging the ser
3e50: 76 65 72 2e 20 20 54 68 65 72 65 20 69 73 20 61  ver.  There is a
3e60: 20 73 69 6e 67 6c 65 0a 2a 2a 20 61 72 67 75 6d   single.** argum
3e70: 65 6e 74 20 77 68 69 63 68 20 69 73 20 74 68 65  ent which is the
3e80: 20 75 6e 63 6f 6d 70 72 65 73 73 65 64 20 63 6f   uncompressed co
3e90: 6e 74 65 6e 74 20 6f 66 20 61 6e 20 22 78 66 65  ntent of an "xfe
3ea0: 72 22 20 6d 65 73 73 61 67 65 0a 2a 2a 20 66 72  r" message.** fr
3eb0: 6f 6d 20 63 6c 69 65 6e 74 20 74 6f 20 73 65 72  om client to ser
3ec0: 76 65 72 2e 20 20 54 68 69 73 20 63 6f 6d 6d 61  ver.  This comma
3ed0: 6e 64 20 69 6e 74 65 72 70 72 65 74 73 20 74 68  nd interprets th
3ee0: 61 74 20 6d 65 73 73 61 67 65 20 61 73 0a 2a 2a  at message as.**
3ef0: 20 69 66 20 68 61 64 20 62 65 65 6e 20 72 65 63   if had been rec
3f00: 65 69 76 65 64 20 62 79 20 74 68 65 20 73 65 72  eived by the ser
3f10: 76 65 72 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 74 68  ver..**.** On th
3f20: 65 20 63 6c 69 65 6e 74 20 73 69 64 65 2c 20 72  e client side, r
3f30: 75 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 66  un:.**.**      f
3f40: 6f 73 73 69 6c 20 70 75 73 68 20 68 74 74 70 3a  ossil push http:
3f50: 2f 2f 62 6f 67 75 73 2f 20 2d 2d 68 74 74 70 74  //bogus/ --httpt
3f60: 72 61 63 65 0a 2a 2a 0a 2a 2a 20 4f 72 20 61 20  race.**.** Or a 
3f70: 73 69 6d 69 6c 61 72 20 63 6f 6d 6d 61 6e 64 20  similar command 
3f80: 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65 20 6f  to provide the o
3f90: 75 74 70 75 74 2e 20 20 54 68 65 20 63 6f 6e 74  utput.  The cont
3fa0: 65 6e 74 20 6f 66 20 74 68 65 0a 2a 2a 20 6d 65  ent of the.** me
3fb0: 73 73 61 67 65 20 77 69 6c 6c 20 61 70 70 65 61  ssage will appea
3fc0: 72 20 6f 6e 20 73 74 61 6e 64 61 72 64 20 6f 75  r on standard ou
3fd0: 74 70 75 74 2e 20 20 43 61 70 74 75 72 65 20 74  tput.  Capture t
3fe0: 68 69 73 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69  his message.** i
3ff0: 6e 74 6f 20 61 20 66 69 6c 65 20 6e 61 6d 65 64  nto a file named
4000: 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 29 20 6f   (for example) o
4010: 75 74 2e 74 78 74 2e 20 20 54 68 65 6e 20 72 75  ut.txt.  Then ru
4020: 6e 20 74 68 65 0a 2a 2a 20 73 65 72 76 65 72 20  n the.** server 
4030: 69 6e 20 67 64 62 3a 0a 2a 2a 0a 2a 2a 20 20 20  in gdb:.**.**   
4040: 20 20 67 64 62 20 66 6f 73 73 69 6c 0a 2a 2a 20    gdb fossil.** 
4050: 20 20 20 20 72 20 74 65 73 74 2d 78 66 65 72 20      r test-xfer 
4060: 6f 75 74 2e 74 78 74 0a 2a 2f 0a 76 6f 69 64 20  out.txt.*/.void 
4070: 63 6d 64 5f 74 65 73 74 5f 78 66 65 72 28 76 6f  cmd_test_xfer(vo
4080: 69 64 29 7b 0a 20 20 69 6e 74 20 6e 6f 74 55 73  id){.  int notUs
4090: 65 64 3b 0a 20 20 69 66 28 20 67 2e 61 72 67 63  ed;.  if( g.argc
40a0: 21 3d 32 20 26 26 20 67 2e 61 72 67 63 21 3d 33  !=2 && g.argc!=3
40b0: 20 29 7b 0a 20 20 20 20 75 73 61 67 65 28 22 3f   ){.    usage("?
40c0: 4d 45 53 53 41 47 45 46 49 4c 45 3f 22 29 3b 0a  MESSAGEFILE?");.
40d0: 20 20 7d 0a 20 20 64 62 5f 6d 75 73 74 5f 62 65    }.  db_must_be
40e0: 5f 77 69 74 68 69 6e 5f 74 72 65 65 28 29 3b 0a  _within_tree();.
40f0: 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 67 2e 63    blob_zero(&g.c
4100: 67 69 49 6e 29 3b 0a 20 20 62 6c 6f 62 5f 72 65  giIn);.  blob_re
4110: 61 64 5f 66 72 6f 6d 5f 66 69 6c 65 28 26 67 2e  ad_from_file(&g.
4120: 63 67 69 49 6e 2c 20 67 2e 61 72 67 63 3d 3d 32  cgiIn, g.argc==2
4130: 20 3f 20 22 2d 22 20 3a 20 67 2e 61 72 67 76 5b   ? "-" : g.argv[
4140: 32 5d 29 3b 0a 20 20 64 69 73 61 62 6c 65 4c 6f  2]);.  disableLo
4150: 67 69 6e 20 3d 20 31 3b 0a 20 20 70 61 67 65 5f  gin = 1;.  page_
4160: 78 66 65 72 28 29 3b 0a 20 20 70 72 69 6e 74 66  xfer();.  printf
4170: 28 22 25 73 5c 6e 22 2c 20 63 67 69 5f 65 78 74  ("%s\n", cgi_ext
4180: 72 61 63 74 5f 63 6f 6e 74 65 6e 74 28 26 6e 6f  ract_content(&no
4190: 74 55 73 65 64 29 29 3b 0a 7d 0a 0a 0a 2f 2a 0a  tUsed));.}.../*.
41a0: 2a 2a 20 53 79 6e 63 20 74 6f 20 74 68 65 20 68  ** Sync to the h
41b0: 6f 73 74 20 69 64 65 6e 74 69 66 69 65 64 20 69  ost identified i
41c0: 6e 20 67 2e 75 72 6c 4e 61 6d 65 20 61 6e 64 20  n g.urlName and 
41d0: 67 2e 75 72 6c 50 61 74 68 2e 20 20 54 68 69 73  g.urlPath.  This
41e0: 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 63  .** routine is c
41f0: 61 6c 6c 65 64 20 62 79 20 74 68 65 20 63 6c 69  alled by the cli
4200: 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 63 6f 72  ent..**.** Recor
4210: 64 73 20 61 72 65 20 70 75 73 68 65 64 20 74 6f  ds are pushed to
4220: 20 74 68 65 20 73 65 72 76 65 72 20 69 66 20 70   the server if p
4230: 75 73 68 46 6c 61 67 20 69 73 20 74 72 75 65 2e  ushFlag is true.
4240: 20 20 52 65 63 6f 72 64 73 0a 2a 2a 20 61 72 65    Records.** are
4250: 20 70 75 6c 6c 65 64 20 69 66 20 70 75 6c 6c 46   pulled if pullF
4260: 6c 61 67 20 69 73 20 74 72 75 65 2e 20 20 41 20  lag is true.  A 
4270: 66 75 6c 6c 20 73 79 6e 63 20 6f 63 63 75 72 73  full sync occurs
4280: 20 69 66 20 62 6f 74 68 20 61 72 65 0a 2a 2a 20   if both are.** 
4290: 74 72 75 65 2e 0a 2a 2f 0a 76 6f 69 64 20 63 6c  true..*/.void cl
42a0: 69 65 6e 74 5f 73 79 6e 63 28 69 6e 74 20 70 75  ient_sync(int pu
42b0: 73 68 46 6c 61 67 2c 20 69 6e 74 20 70 75 6c 6c  shFlag, int pull
42c0: 46 6c 61 67 2c 20 69 6e 74 20 63 6c 6f 6e 65 46  Flag, int cloneF
42d0: 6c 61 67 29 7b 0a 20 20 69 6e 74 20 67 6f 20 3d  lag){.  int go =
42e0: 20 31 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f   1;        /* Lo
42f0: 6f 70 20 75 6e 74 69 6c 20 7a 65 72 6f 20 2a 2f  op until zero */
4300: 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
4310: 53 43 6f 64 65 20 3d 20 64 62 5f 67 65 74 28 22  SCode = db_get("
4320: 73 65 72 76 65 72 2d 63 6f 64 65 22 2c 20 22 78  server-code", "x
4330: 22 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  ");.  const char
4340: 20 2a 7a 50 43 6f 64 65 20 3d 20 64 62 5f 67 65   *zPCode = db_ge
4350: 74 28 22 70 72 6f 6a 65 63 74 2d 63 6f 64 65 22  t("project-code"
4360: 2c 20 30 29 3b 0a 20 20 69 6e 74 20 6e 4d 73 67  , 0);.  int nMsg
4370: 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f   = 0;          /
4380: 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 73 73  * Number of mess
4390: 61 67 65 73 20 73 65 6e 74 20 6f 72 20 72 65 63  ages sent or rec
43a0: 65 69 76 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  eived */.  int n
43b0: 43 79 63 6c 65 20 3d 20 30 3b 20 20 20 20 20 20  Cycle = 0;      
43c0: 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72    /* Number of r
43d0: 6f 75 6e 64 20 74 72 69 70 73 20 74 6f 20 74 68  ound trips to th
43e0: 65 20 73 65 72 76 65 72 20 2a 2f 0a 20 20 69 6e  e server */.  in
43f0: 74 20 6e 46 69 6c 65 53 65 6e 64 20 3d 20 30 3b  t nFileSend = 0;
4400: 0a 20 20 42 6c 6f 62 20 73 65 6e 64 3b 20 20 20  .  Blob send;   
4410: 20 20 20 20 20 2f 2a 20 54 65 78 74 20 77 65 20       /* Text we 
4420: 61 72 65 20 73 65 6e 64 69 6e 67 20 74 6f 20 74  are sending to t
4430: 68 65 20 73 65 72 76 65 72 20 2a 2f 0a 20 20 42  he server */.  B
4440: 6c 6f 62 20 72 65 63 76 3b 20 20 20 20 20 20 20  lob recv;       
4450: 20 2f 2a 20 52 65 70 6c 79 20 77 65 20 67 6f 74   /* Reply we got
4460: 20 62 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 73   back from the s
4470: 65 72 76 65 72 20 2a 2f 0a 20 20 58 66 65 72 20  erver */.  Xfer 
4480: 78 66 65 72 3b 20 20 20 20 20 20 20 20 2f 2a 20  xfer;        /* 
4490: 54 72 61 6e 73 66 65 72 20 64 61 74 61 20 2a 2f  Transfer data */
44a0: 0a 0a 20 20 6d 65 6d 73 65 74 28 26 78 66 65 72  ..  memset(&xfer
44b0: 2c 20 30 2c 20 73 69 7a 65 6f 66 28 78 66 65 72  , 0, sizeof(xfer
44c0: 29 29 3b 0a 20 20 78 66 65 72 2e 70 49 6e 20 3d  ));.  xfer.pIn =
44d0: 20 26 72 65 63 76 3b 0a 20 20 78 66 65 72 2e 70   &recv;.  xfer.p
44e0: 4f 75 74 20 3d 20 26 73 65 6e 64 3b 0a 20 20 78  Out = &send;.  x
44f0: 66 65 72 2e 6d 78 53 65 6e 64 20 3d 20 64 62 5f  fer.mxSend = db_
4500: 67 65 74 5f 69 6e 74 28 22 6d 61 78 2d 75 70 6c  get_int("max-upl
4510: 6f 61 64 22 2c 20 32 35 30 30 30 30 29 3b 0a 0a  oad", 250000);..
4520: 20 20 61 73 73 65 72 74 28 20 70 75 73 68 46 6c    assert( pushFl
4530: 61 67 20 7c 7c 20 70 75 6c 6c 46 6c 61 67 20 7c  ag || pullFlag |
4540: 7c 20 63 6c 6f 6e 65 46 6c 61 67 20 29 3b 0a 20  | cloneFlag );. 
4550: 20 61 73 73 65 72 74 28 20 21 67 2e 75 72 6c 49   assert( !g.urlI
4560: 73 46 69 6c 65 20 29 3b 20 20 20 20 20 20 20 20  sFile );        
4570: 20 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20 77    /* This only w
4580: 6f 72 6b 73 20 66 6f 72 20 6e 65 74 77 6f 72 6b  orks for network
4590: 69 6e 67 20 2a 2f 0a 0a 20 20 64 62 5f 62 65 67  ing */..  db_beg
45a0: 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 29  in_transaction()
45b0: 3b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65  ;.  db_multi_exe
45c0: 63 28 0a 20 20 20 20 22 43 52 45 41 54 45 20 54  c(.    "CREATE T
45d0: 45 4d 50 20 54 41 42 4c 45 20 6f 6e 72 65 6d 6f  EMP TABLE onremo
45e0: 74 65 28 72 69 64 20 49 4e 54 45 47 45 52 20 50  te(rid INTEGER P
45f0: 52 49 4d 41 52 59 20 4b 45 59 29 3b 22 0a 20 20  RIMARY KEY);".  
4600: 29 3b 0a 20 20 62 6c 6f 62 61 72 72 61 79 5f 7a  );.  blobarray_z
4610: 65 72 6f 28 78 66 65 72 2e 61 54 6f 6b 65 6e 2c  ero(xfer.aToken,
4620: 20 63 6f 75 6e 74 28 78 66 65 72 2e 61 54 6f 6b   count(xfer.aTok
4630: 65 6e 29 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72  en));.  blob_zer
4640: 6f 28 26 73 65 6e 64 29 3b 0a 20 20 62 6c 6f 62  o(&send);.  blob
4650: 5f 7a 65 72 6f 28 26 72 65 63 76 29 3b 0a 20 20  _zero(&recv);.  
4660: 62 6c 6f 62 5f 7a 65 72 6f 28 26 78 66 65 72 2e  blob_zero(&xfer.
4670: 65 72 72 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72  err);.  blob_zer
4680: 6f 28 26 78 66 65 72 2e 6c 69 6e 65 29 3b 0a 0a  o(&xfer.line);..
4690: 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 77 61 79 73    /*.  ** Always
46a0: 20 62 65 67 69 6e 20 77 69 74 68 20 61 20 63 6c   begin with a cl
46b0: 6f 6e 65 2c 20 70 75 6c 6c 2c 20 6f 72 20 70 75  one, pull, or pu
46c0: 73 68 20 6d 65 73 73 61 67 65 0a 20 20 2a 2f 0a  sh message.  */.
46d0: 20 20 69 66 28 20 63 6c 6f 6e 65 46 6c 61 67 20    if( cloneFlag 
46e0: 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65  ){.    blob_appe
46f0: 6e 64 66 28 26 73 65 6e 64 2c 20 22 63 6c 6f 6e  ndf(&send, "clon
4700: 65 5c 6e 22 29 3b 0a 20 20 20 20 70 75 73 68 46  e\n");.    pushF
4710: 6c 61 67 20 3d 20 30 3b 0a 20 20 20 20 70 75 6c  lag = 0;.    pul
4720: 6c 46 6c 61 67 20 3d 20 30 3b 0a 20 20 20 20 6e  lFlag = 0;.    n
4730: 4d 73 67 2b 2b 3b 0a 20 20 7d 65 6c 73 65 20 69  Msg++;.  }else i
4740: 66 28 20 70 75 6c 6c 46 6c 61 67 20 29 7b 0a 20  f( pullFlag ){. 
4750: 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28     blob_appendf(
4760: 26 73 65 6e 64 2c 20 22 70 75 6c 6c 20 25 73 20  &send, "pull %s 
4770: 25 73 5c 6e 22 2c 20 7a 53 43 6f 64 65 2c 20 7a  %s\n", zSCode, z
4780: 50 43 6f 64 65 29 3b 0a 20 20 20 20 6e 4d 73 67  PCode);.    nMsg
4790: 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 75  ++;.  }.  if( pu
47a0: 73 68 46 6c 61 67 20 29 7b 0a 20 20 20 20 62 6c  shFlag ){.    bl
47b0: 6f 62 5f 61 70 70 65 6e 64 66 28 26 73 65 6e 64  ob_appendf(&send
47c0: 2c 20 22 70 75 73 68 20 25 73 20 25 73 5c 6e 22  , "push %s %s\n"
47d0: 2c 20 7a 53 43 6f 64 65 2c 20 7a 50 43 6f 64 65  , zSCode, zPCode
47e0: 29 3b 0a 20 20 20 20 6e 4d 73 67 2b 2b 3b 0a 20  );.    nMsg++;. 
47f0: 20 7d 0a 0a 0a 20 20 77 68 69 6c 65 28 20 67 6f   }...  while( go
4800: 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 65 77 50   ){.    int newP
4810: 68 61 6e 74 6f 6d 20 3d 20 30 3b 0a 0a 20 20 20  hantom = 0;..   
4820: 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 67 69 6d   /* Generate gim
4830: 6d 65 20 6d 65 73 73 61 67 65 73 20 66 6f 72 20  me messages for 
4840: 70 68 61 6e 74 6f 6d 73 20 61 6e 64 20 6c 65 61  phantoms and lea
4850: 66 20 6d 65 73 73 61 67 65 73 0a 20 20 20 20 2a  f messages.    *
4860: 2a 20 66 6f 72 20 61 6c 6c 20 6c 65 61 76 65 73  * for all leaves
4870: 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
4880: 20 70 75 6c 6c 46 6c 61 67 20 29 7b 0a 20 20 20   pullFlag ){.   
4890: 20 20 20 72 65 71 75 65 73 74 5f 70 68 61 6e 74     request_phant
48a0: 6f 6d 73 28 26 78 66 65 72 29 3b 0a 20 20 20 20  oms(&xfer);.    
48b0: 20 20 73 65 6e 64 5f 6c 65 61 76 65 73 28 26 78    send_leaves(&x
48c0: 66 65 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  fer);.    }..   
48d0: 20 2f 2a 20 45 78 63 68 61 6e 67 65 20 6d 65 73   /* Exchange mes
48e0: 73 61 67 65 73 20 77 69 74 68 20 74 68 65 20 73  sages with the s
48f0: 65 72 76 65 72 20 2a 2f 0a 20 20 20 20 6e 46 69  erver */.    nFi
4900: 6c 65 53 65 6e 64 20 3d 20 78 66 65 72 2e 6e 46  leSend = xfer.nF
4910: 69 6c 65 53 65 6e 74 20 2b 20 78 66 65 72 2e 6e  ileSent + xfer.n
4920: 44 65 6c 74 61 53 65 6e 74 3b 0a 20 20 20 20 70  DeltaSent;.    p
4930: 72 69 6e 74 66 28 22 53 65 6e 64 3a 20 20 20 20  rintf("Send:    
4940: 20 20 25 31 30 64 20 62 79 74 65 73 2c 20 25 33    %10d bytes, %3
4950: 64 20 6d 65 73 73 61 67 65 73 2c 20 25 33 64 20  d messages, %3d 
4960: 66 69 6c 65 73 20 28 25 64 2b 25 64 29 5c 6e 22  files (%d+%d)\n"
4970: 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 6c  ,.            bl
4980: 6f 62 5f 73 69 7a 65 28 26 73 65 6e 64 29 2c 20  ob_size(&send), 
4990: 6e 4d 73 67 2b 78 66 65 72 2e 6e 47 69 6d 6d 65  nMsg+xfer.nGimme
49a0: 53 65 6e 74 2b 78 66 65 72 2e 6e 49 47 6f 74 53  Sent+xfer.nIGotS
49b0: 65 6e 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ent,.           
49c0: 20 6e 46 69 6c 65 53 65 6e 64 2c 20 78 66 65 72   nFileSend, xfer
49d0: 2e 6e 46 69 6c 65 53 65 6e 74 2c 20 78 66 65 72  .nFileSent, xfer
49e0: 2e 6e 44 65 6c 74 61 53 65 6e 74 29 3b 0a 20 20  .nDeltaSent);.  
49f0: 20 20 6e 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20    nMsg = 0;.    
4a00: 78 66 65 72 2e 6e 46 69 6c 65 53 65 6e 74 20 3d  xfer.nFileSent =
4a10: 20 30 3b 0a 20 20 20 20 78 66 65 72 2e 6e 44 65   0;.    xfer.nDe
4a20: 6c 74 61 53 65 6e 74 20 3d 20 30 3b 0a 20 20 20  ltaSent = 0;.   
4a30: 20 78 66 65 72 2e 6e 47 69 6d 6d 65 53 65 6e 74   xfer.nGimmeSent
4a40: 20 3d 20 30 3b 0a 20 20 20 20 68 74 74 70 5f 65   = 0;.    http_e
4a50: 78 63 68 61 6e 67 65 28 26 73 65 6e 64 2c 20 26  xchange(&send, &
4a60: 72 65 63 76 29 3b 0a 20 20 20 20 62 6c 6f 62 5f  recv);.    blob_
4a70: 72 65 73 65 74 28 26 73 65 6e 64 29 3b 0a 0a 20  reset(&send);.. 
4a80: 20 20 20 2f 2a 20 42 65 67 69 6e 20 63 6f 6e 73     /* Begin cons
4a90: 74 72 75 63 74 69 6e 67 20 74 68 65 20 6e 65 78  tructing the nex
4aa0: 74 20 6d 65 73 73 61 67 65 20 28 77 68 69 63 68  t message (which
4ab0: 20 6d 69 67 68 74 20 6e 65 76 65 72 20 62 65 0a   might never be.
4ac0: 20 20 20 20 2a 2a 20 73 65 6e 74 29 20 62 79 20      ** sent) by 
4ad0: 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 74  beginning with t
4ae0: 68 65 20 70 75 6c 6c 20 6f 72 20 70 75 73 68 20  he pull or push 
4af0: 6d 65 73 73 61 67 65 73 0a 20 20 20 20 2a 2f 0a  messages.    */.
4b00: 20 20 20 20 69 66 28 20 70 75 6c 6c 46 6c 61 67      if( pullFlag
4b10: 20 29 7b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 61   ){.      blob_a
4b20: 70 70 65 6e 64 66 28 26 73 65 6e 64 2c 20 22 70  ppendf(&send, "p
4b30: 75 6c 6c 20 25 73 20 25 73 5c 6e 22 2c 20 7a 53  ull %s %s\n", zS
4b40: 43 6f 64 65 2c 20 7a 50 43 6f 64 65 29 3b 0a 20  Code, zPCode);. 
4b50: 20 20 20 20 20 6e 4d 73 67 2b 2b 3b 0a 20 20 20       nMsg++;.   
4b60: 20 7d 0a 20 20 20 20 69 66 28 20 70 75 73 68 46   }.    if( pushF
4b70: 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 62 6c 6f  lag ){.      blo
4b80: 62 5f 61 70 70 65 6e 64 66 28 26 73 65 6e 64 2c  b_appendf(&send,
4b90: 20 22 70 75 73 68 20 25 73 20 25 73 5c 6e 22 2c   "push %s %s\n",
4ba0: 20 7a 53 43 6f 64 65 2c 20 7a 50 43 6f 64 65 29   zSCode, zPCode)
4bb0: 3b 0a 20 20 20 20 20 20 6e 4d 73 67 2b 2b 3b 0a  ;.      nMsg++;.
4bc0: 20 20 20 20 7d 0a 0a 0a 20 20 20 20 2f 2a 20 50      }...    /* P
4bd0: 72 6f 63 65 73 73 20 74 68 65 20 72 65 70 6c 79  rocess the reply
4be0: 20 74 68 61 74 20 63 61 6d 65 20 62 61 63 6b 20   that came back 
4bf0: 66 72 6f 6d 20 74 68 65 20 73 65 72 76 65 72 20  from the server 
4c00: 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 62 6c  */.    while( bl
4c10: 6f 62 5f 6c 69 6e 65 28 26 72 65 63 76 2c 20 26  ob_line(&recv, &
4c20: 78 66 65 72 2e 6c 69 6e 65 29 20 29 7b 0a 20 20  xfer.line) ){.  
4c30: 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 62 75 66      if( blob_buf
4c40: 66 65 72 28 26 78 66 65 72 2e 6c 69 6e 65 29 5b  fer(&xfer.line)[
4c50: 30 5d 3d 3d 27 23 27 20 29 7b 0a 20 20 20 20 20  0]=='#' ){.     
4c60: 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
4c70: 20 20 20 7d 0a 20 20 20 20 20 20 78 66 65 72 2e     }.      xfer.
4c80: 6e 54 6f 6b 65 6e 20 3d 20 62 6c 6f 62 5f 74 6f  nToken = blob_to
4c90: 6b 65 6e 69 7a 65 28 26 78 66 65 72 2e 6c 69 6e  kenize(&xfer.lin
4ca0: 65 2c 20 78 66 65 72 2e 61 54 6f 6b 65 6e 2c 20  e, xfer.aToken, 
4cb0: 63 6f 75 6e 74 28 78 66 65 72 2e 61 54 6f 6b 65  count(xfer.aToke
4cc0: 6e 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 20  n));..      /*  
4cd0: 20 66 69 6c 65 20 55 55 49 44 20 53 49 5a 45 20   file UUID SIZE 
4ce0: 5c 6e 20 43 4f 4e 54 45 4e 54 0a 20 20 20 20 20  \n CONTENT.     
4cf0: 20 2a 2a 20 20 20 66 69 6c 65 20 55 55 49 44 20   **   file UUID 
4d00: 44 45 4c 54 41 53 52 43 20 53 49 5a 45 20 5c 6e  DELTASRC SIZE \n
4d10: 20 43 4f 4e 54 45 4e 54 0a 20 20 20 20 20 20 2a   CONTENT.      *
4d20: 2a 0a 20 20 20 20 20 20 2a 2a 20 52 65 63 65 69  *.      ** Recei
4d30: 76 65 20 61 20 66 69 6c 65 20 74 72 61 6e 73 6d  ve a file transm
4d40: 69 74 74 65 64 20 66 72 6f 6d 20 74 68 65 20 6f  itted from the o
4d50: 74 68 65 72 20 73 69 64 65 0a 20 20 20 20 20 20  ther side.      
4d60: 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 62 6c 6f  */.      if( blo
4d70: 62 5f 65 71 28 26 78 66 65 72 2e 61 54 6f 6b 65  b_eq(&xfer.aToke
4d80: 6e 5b 30 5d 2c 22 66 69 6c 65 22 29 20 29 7b 0a  n[0],"file") ){.
4d90: 20 20 20 20 20 20 20 20 78 66 65 72 5f 61 63 63          xfer_acc
4da0: 65 70 74 5f 66 69 6c 65 28 26 78 66 65 72 29 3b  ept_file(&xfer);
4db0: 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20  .      }else..  
4dc0: 20 20 20 20 2f 2a 20 20 20 67 69 6d 6d 65 20 55      /*   gimme U
4dd0: 55 49 44 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  UID.      **.   
4de0: 20 20 20 2a 2a 20 53 65 72 76 65 72 20 69 73 20     ** Server is 
4df0: 72 65 71 75 65 73 74 69 6e 67 20 61 20 66 69 6c  requesting a fil
4e00: 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  e.      */.     
4e10: 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 78 66   if( blob_eq(&xf
4e20: 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 67  er.aToken[0], "g
4e30: 69 6d 6d 65 22 29 0a 20 20 20 20 20 20 20 26 26  imme").       &&
4e40: 20 78 66 65 72 2e 6e 54 6f 6b 65 6e 3d 3d 32 0a   xfer.nToken==2.
4e50: 20 20 20 20 20 20 20 26 26 20 62 6c 6f 62 5f 69         && blob_i
4e60: 73 5f 75 75 69 64 28 26 78 66 65 72 2e 61 54 6f  s_uuid(&xfer.aTo
4e70: 6b 65 6e 5b 31 5d 29 0a 20 20 20 20 20 20 29 7b  ken[1]).      ){
4e80: 0a 20 20 20 20 20 20 20 20 6e 4d 73 67 2b 2b 3b  .        nMsg++;
4e90: 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 75 73  .        if( pus
4ea0: 68 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20  hFlag ){.       
4eb0: 20 20 20 69 6e 74 20 72 69 64 20 3d 20 72 69 64     int rid = rid
4ec0: 5f 66 72 6f 6d 5f 75 75 69 64 28 26 78 66 65 72  _from_uuid(&xfer
4ed0: 2e 61 54 6f 6b 65 6e 5b 31 5d 2c 20 30 29 3b 0a  .aToken[1], 0);.
4ee0: 20 20 20 20 20 20 20 20 20 20 73 65 6e 64 5f 66            send_f
4ef0: 69 6c 65 28 26 78 66 65 72 2c 20 72 69 64 2c 20  ile(&xfer, rid, 
4f00: 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d 2c  &xfer.aToken[1],
4f10: 20 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   0);.        }. 
4f20: 20 20 20 20 20 7d 65 6c 73 65 0a 20 20 0a 20 20       }else.  .  
4f30: 20 20 20 20 2f 2a 20 20 20 69 67 6f 74 20 55 55      /*   igot UU
4f40: 49 44 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  ID.      **.    
4f50: 20 20 2a 2a 20 53 65 72 76 65 72 20 61 6e 6e 6f    ** Server anno
4f60: 75 6e 63 65 73 20 74 68 61 74 20 69 74 20 68 61  unces that it ha
4f70: 73 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 66  s a particular f
4f80: 69 6c 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ile.      */.   
4f90: 20 20 20 69 66 28 20 78 66 65 72 2e 6e 54 6f 6b     if( xfer.nTok
4fa0: 65 6e 3d 3d 32 0a 20 20 20 20 20 20 20 26 26 20  en==2.       && 
4fb0: 62 6c 6f 62 5f 65 71 28 26 78 66 65 72 2e 61 54  blob_eq(&xfer.aT
4fc0: 6f 6b 65 6e 5b 30 5d 2c 20 22 69 67 6f 74 22 29  oken[0], "igot")
4fd0: 0a 20 20 20 20 20 20 20 26 26 20 62 6c 6f 62 5f  .       && blob_
4fe0: 69 73 5f 75 75 69 64 28 26 78 66 65 72 2e 61 54  is_uuid(&xfer.aT
4ff0: 6f 6b 65 6e 5b 31 5d 29 0a 20 20 20 20 20 20 29  oken[1]).      )
5000: 7b 0a 20 20 20 20 20 20 20 20 6e 4d 73 67 2b 2b  {.        nMsg++
5010: 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 75  ;.        if( pu
5020: 6c 6c 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20  llFlag ){.      
5030: 20 20 20 20 69 66 28 20 21 64 62 5f 65 78 69 73      if( !db_exis
5040: 74 73 28 22 53 45 4c 45 43 54 20 31 20 46 52 4f  ts("SELECT 1 FRO
5050: 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 75 75 69  M blob WHERE uui
5060: 64 3d 27 25 62 27 20 41 4e 44 20 73 69 7a 65 3e  d='%b' AND size>
5070: 3d 30 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  =0",.           
5080: 20 20 20 20 20 26 78 66 65 72 2e 61 54 6f 6b 65       &xfer.aToke
5090: 6e 5b 31 5d 29 20 29 7b 0a 20 20 20 20 20 20 20  n[1]) ){.       
50a0: 20 20 20 20 20 63 6f 6e 74 65 6e 74 5f 70 75 74       content_put
50b0: 28 30 2c 20 62 6c 6f 62 5f 73 74 72 28 26 78 66  (0, blob_str(&xf
50c0: 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d 29 2c 20 30  er.aToken[1]), 0
50d0: 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e  );.            n
50e0: 65 77 50 68 61 6e 74 6f 6d 20 3d 20 31 3b 0a 20  ewPhantom = 1;. 
50f0: 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
5100: 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
5110: 0a 20 20 20 20 0a 20 20 20 20 20 20 0a 20 20 20  .    .      .   
5120: 20 20 20 2f 2a 20 20 20 6c 65 61 66 20 55 55 49     /*   leaf UUI
5130: 44 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  D.      **.     
5140: 20 2a 2a 20 53 65 72 76 65 72 20 61 6e 6e 6f 75   ** Server annou
5150: 6e 63 65 73 20 74 68 61 74 20 69 74 20 68 61 73  nces that it has
5160: 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 6d 61   a particular ma
5170: 6e 69 66 65 73 74 0a 20 20 20 20 20 20 2a 2f 0a  nifest.      */.
5180: 20 20 20 20 20 20 69 66 28 20 78 66 65 72 2e 6e        if( xfer.n
5190: 54 6f 6b 65 6e 3d 3d 32 0a 20 20 20 20 20 20 20  Token==2.       
51a0: 26 26 20 62 6c 6f 62 5f 65 71 28 26 78 66 65 72  && blob_eq(&xfer
51b0: 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 6c 65 61  .aToken[0], "lea
51c0: 66 22 29 0a 20 20 20 20 20 20 20 26 26 20 62 6c  f").       && bl
51d0: 6f 62 5f 69 73 5f 75 75 69 64 28 26 78 66 65 72  ob_is_uuid(&xfer
51e0: 2e 61 54 6f 6b 65 6e 5b 31 5d 29 0a 20 20 20 20  .aToken[1]).    
51f0: 20 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74    ){.        int
5200: 20 72 69 64 20 3d 20 72 69 64 5f 66 72 6f 6d 5f   rid = rid_from_
5210: 75 75 69 64 28 26 78 66 65 72 2e 61 54 6f 6b 65  uuid(&xfer.aToke
5220: 6e 5b 31 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20  n[1], 0);.      
5230: 20 20 6e 4d 73 67 2b 2b 3b 0a 20 20 20 20 20 20    nMsg++;.      
5240: 20 20 69 66 28 20 70 75 73 68 46 6c 61 67 20 26    if( pushFlag &
5250: 26 20 72 69 64 20 29 7b 0a 20 20 20 20 20 20 20  & rid ){.       
5260: 20 20 20 6c 65 61 66 5f 72 65 73 70 6f 6e 73 65     leaf_response
5270: 28 26 78 66 65 72 2c 20 72 69 64 29 3b 0a 20 20  (&xfer, rid);.  
5280: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
5290: 69 66 28 20 70 75 6c 6c 46 6c 61 67 20 26 26 20  if( pullFlag && 
52a0: 72 69 64 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  rid==0 ){.      
52b0: 20 20 20 20 63 6f 6e 74 65 6e 74 5f 70 75 74 28      content_put(
52c0: 30 2c 20 62 6c 6f 62 5f 73 74 72 28 26 78 66 65  0, blob_str(&xfe
52d0: 72 2e 61 54 6f 6b 65 6e 5b 31 5d 29 2c 20 30 29  r.aToken[1]), 0)
52e0: 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 65 77 50  ;.          newP
52f0: 68 61 6e 74 6f 6d 20 3d 20 31 3b 0a 20 20 20 20  hantom = 1;.    
5300: 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
5310: 65 0a 20 20 0a 20 20 0a 20 20 20 20 20 20 2f 2a  e.  .  .      /*
5320: 20 20 20 70 75 73 68 20 20 53 45 52 56 45 52 43     push  SERVERC
5330: 4f 44 45 20 20 50 52 4f 44 55 43 54 43 4f 44 45  ODE  PRODUCTCODE
5340: 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
5350: 2a 2a 20 53 68 6f 75 6c 64 20 6f 6e 6c 79 20 68  ** Should only h
5360: 61 70 70 65 6e 20 69 6e 20 72 65 73 70 6f 6e 73  appen in respons
5370: 65 20 74 6f 20 61 20 63 6c 6f 6e 65 2e 0a 20 20  e to a clone..  
5380: 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
5390: 20 62 6c 6f 62 5f 65 71 28 26 78 66 65 72 2e 61   blob_eq(&xfer.a
53a0: 54 6f 6b 65 6e 5b 30 5d 2c 22 70 75 73 68 22 29  Token[0],"push")
53b0: 0a 20 20 20 20 20 20 20 26 26 20 78 66 65 72 2e  .       && xfer.
53c0: 6e 54 6f 6b 65 6e 3d 3d 33 0a 20 20 20 20 20 20  nToken==3.      
53d0: 20 26 26 20 63 6c 6f 6e 65 46 6c 61 67 0a 20 20   && cloneFlag.  
53e0: 20 20 20 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f       && blob_is_
53f0: 75 75 69 64 28 26 78 66 65 72 2e 61 54 6f 6b 65  uuid(&xfer.aToke
5400: 6e 5b 31 5d 29 0a 20 20 20 20 20 20 20 26 26 20  n[1]).       && 
5410: 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26 78 66  blob_is_uuid(&xf
5420: 65 72 2e 61 54 6f 6b 65 6e 5b 32 5d 29 0a 20 20  er.aToken[2]).  
5430: 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 69      ){.        i
5440: 66 28 20 62 6c 6f 62 5f 65 71 5f 73 74 72 28 26  f( blob_eq_str(&
5450: 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d 2c 20  xfer.aToken[1], 
5460: 7a 53 43 6f 64 65 2c 20 2d 31 29 20 29 7b 0a 20  zSCode, -1) ){. 
5470: 20 20 20 20 20 20 20 20 20 66 6f 73 73 69 6c 5f           fossil_
5480: 66 61 74 61 6c 28 22 73 65 72 76 65 72 20 6c 6f  fatal("server lo
5490: 6f 70 22 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  op");.        }.
54a0: 20 20 20 20 20 20 20 20 6e 4d 73 67 2b 2b 3b 0a          nMsg++;.
54b0: 20 20 20 20 20 20 20 20 69 66 28 20 7a 50 43 6f          if( zPCo
54c0: 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  de==0 ){.       
54d0: 20 20 20 7a 50 43 6f 64 65 20 3d 20 6d 70 72 69     zPCode = mpri
54e0: 6e 74 66 28 22 25 62 22 2c 20 26 78 66 65 72 2e  ntf("%b", &xfer.
54f0: 61 54 6f 6b 65 6e 5b 32 5d 29 3b 0a 20 20 20 20  aToken[2]);.    
5500: 20 20 20 20 20 20 64 62 5f 73 65 74 28 22 70 72        db_set("pr
5510: 6f 6a 65 63 74 2d 63 6f 64 65 22 2c 20 7a 50 43  oject-code", zPC
5520: 6f 64 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ode);.        }.
5530: 20 20 20 20 20 20 20 20 63 6c 6f 6e 65 46 6c 61          cloneFla
5540: 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70  g = 0;.        p
5550: 75 6c 6c 46 6c 61 67 20 3d 20 31 3b 0a 20 20 20  ullFlag = 1;.   
5560: 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64       blob_append
5570: 66 28 26 73 65 6e 64 2c 20 22 70 75 6c 6c 20 25  f(&send, "pull %
5580: 73 20 25 73 5c 6e 22 2c 20 7a 53 43 6f 64 65 2c  s %s\n", zSCode,
5590: 20 7a 50 43 6f 64 65 29 3b 0a 20 20 20 20 20 20   zPCode);.      
55a0: 20 20 6e 4d 73 67 2b 2b 3b 0a 20 20 20 20 20 20    nMsg++;.      
55b0: 7d 65 6c 73 65 0a 0a 20 20 20 20 20 20 2f 2a 20  }else..      /* 
55c0: 20 20 65 72 72 6f 72 20 4d 45 53 53 41 47 45 0a    error MESSAGE.
55d0: 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
55e0: 2a 20 52 65 70 6f 72 74 20 61 6e 20 65 72 72 6f  * Report an erro
55f0: 72 0a 20 20 20 20 20 20 2a 2f 20 20 20 20 20 20  r.      */      
5600: 20 20 0a 20 20 20 20 20 20 69 66 28 20 62 6c 6f    .      if( blo
5610: 62 5f 65 71 28 26 78 66 65 72 2e 61 54 6f 6b 65  b_eq(&xfer.aToke
5620: 6e 5b 30 5d 2c 22 65 72 72 6f 72 22 29 20 26 26  n[0],"error") &&
5630: 20 78 66 65 72 2e 6e 54 6f 6b 65 6e 3d 3d 32 20   xfer.nToken==2 
5640: 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20  ){.        char 
5650: 2a 7a 4d 73 67 20 3d 20 62 6c 6f 62 5f 74 65 72  *zMsg = blob_ter
5660: 6d 69 6e 61 74 65 28 26 78 66 65 72 2e 61 54 6f  minate(&xfer.aTo
5670: 6b 65 6e 5b 31 5d 29 3b 0a 20 20 20 20 20 20 20  ken[1]);.       
5680: 20 64 65 66 6f 73 73 69 6c 69 7a 65 28 7a 4d 73   defossilize(zMs
5690: 67 29 3b 0a 20 20 20 20 20 20 20 20 62 6c 6f 62  g);.        blob
56a0: 5f 61 70 70 65 6e 64 66 28 26 78 66 65 72 2e 65  _appendf(&xfer.e
56b0: 72 72 2c 20 22 73 65 72 76 65 72 20 73 61 79 73  rr, "server says
56c0: 3a 20 25 73 22 2c 20 7a 4d 73 67 29 3b 0a 20 20  : %s", zMsg);.  
56d0: 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20 20 20      }else..     
56e0: 20 2f 2a 20 55 6e 6b 6e 6f 77 6e 20 6d 65 73 73   /* Unknown mess
56f0: 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 7b 0a 20  age */.      {. 
5700: 20 20 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 65         blob_appe
5710: 6e 64 66 28 26 78 66 65 72 2e 65 72 72 2c 20 22  ndf(&xfer.err, "
5720: 75 6e 6b 6e 6f 77 6e 20 63 6f 6d 6d 61 6e 64 3a  unknown command:
5730: 20 25 62 22 2c 20 26 78 66 65 72 2e 61 54 6f 6b   %b", &xfer.aTok
5740: 65 6e 5b 30 5d 29 3b 0a 20 20 20 20 20 20 7d 0a  en[0]);.      }.
5750: 0a 20 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f  .      if( blob_
5760: 73 69 7a 65 28 26 78 66 65 72 2e 65 72 72 29 20  size(&xfer.err) 
5770: 29 7b 0a 20 20 20 20 20 20 20 20 66 6f 73 73 69  ){.        fossi
5780: 6c 5f 66 61 74 61 6c 28 22 25 62 22 2c 20 26 78  l_fatal("%b", &x
5790: 66 65 72 2e 65 72 72 29 3b 0a 20 20 20 20 20 20  fer.err);.      
57a0: 7d 0a 20 20 20 20 20 20 62 6c 6f 62 61 72 72 61  }.      blobarra
57b0: 79 5f 72 65 73 65 74 28 78 66 65 72 2e 61 54 6f  y_reset(xfer.aTo
57c0: 6b 65 6e 2c 20 78 66 65 72 2e 6e 54 6f 6b 65 6e  ken, xfer.nToken
57d0: 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 72 65  );.      blob_re
57e0: 73 65 74 28 26 78 66 65 72 2e 6c 69 6e 65 29 3b  set(&xfer.line);
57f0: 0a 20 20 20 20 7d 0a 20 20 20 20 70 72 69 6e 74  .    }.    print
5800: 66 28 22 52 65 63 65 69 76 65 64 3a 20 20 25 31  f("Received:  %1
5810: 30 64 20 62 79 74 65 73 2c 20 25 33 64 20 6d 65  0d bytes, %3d me
5820: 73 73 61 67 65 73 2c 20 25 33 64 20 66 69 6c 65  ssages, %3d file
5830: 73 20 28 25 64 2b 25 64 2b 25 64 29 5c 6e 22 2c  s (%d+%d+%d)\n",
5840: 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 6c 6f  .            blo
5850: 62 5f 73 69 7a 65 28 26 72 65 63 76 29 2c 20 6e  b_size(&recv), n
5860: 4d 73 67 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Msg,.           
5870: 20 78 66 65 72 2e 6e 46 69 6c 65 52 63 76 64 20   xfer.nFileRcvd 
5880: 2b 20 78 66 65 72 2e 6e 44 65 6c 74 61 52 63 76  + xfer.nDeltaRcv
5890: 64 20 2b 20 78 66 65 72 2e 6e 44 61 6e 67 6c 69  d + xfer.nDangli
58a0: 6e 67 46 69 6c 65 2c 0a 20 20 20 20 20 20 20 20  ngFile,.        
58b0: 20 20 20 20 78 66 65 72 2e 6e 46 69 6c 65 52 63      xfer.nFileRc
58c0: 76 64 2c 20 78 66 65 72 2e 6e 44 65 6c 74 61 52  vd, xfer.nDeltaR
58d0: 63 76 64 2c 20 78 66 65 72 2e 6e 44 61 6e 67 6c  cvd, xfer.nDangl
58e0: 69 6e 67 46 69 6c 65 29 3b 0a 0a 20 20 20 20 62  ingFile);..    b
58f0: 6c 6f 62 5f 72 65 73 65 74 28 26 72 65 63 76 29  lob_reset(&recv)
5900: 3b 0a 20 20 20 20 6e 4d 73 67 20 3d 20 30 3b 0a  ;.    nMsg = 0;.
5910: 20 20 20 20 78 66 65 72 2e 6e 46 69 6c 65 52 63      xfer.nFileRc
5920: 76 64 20 3d 20 30 3b 0a 20 20 20 20 78 66 65 72  vd = 0;.    xfer
5930: 2e 6e 44 65 6c 74 61 52 63 76 64 20 3d 20 30 3b  .nDeltaRcvd = 0;
5940: 0a 20 20 20 20 78 66 65 72 2e 6e 44 61 6e 67 6c  .    xfer.nDangl
5950: 69 6e 67 46 69 6c 65 20 3d 20 30 3b 0a 20 20 20  ingFile = 0;.   
5960: 20 6e 43 79 63 6c 65 2b 2b 3b 0a 20 20 20 20 67   nCycle++;.    g
5970: 6f 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 49  o = 0;..    /* I
5980: 66 20 77 65 20 68 61 76 65 20 72 65 63 65 69 76  f we have receiv
5990: 65 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 66  ed one or more f
59a0: 69 6c 65 73 20 6f 6e 20 74 68 69 73 20 63 79 63  iles on this cyc
59b0: 6c 65 20 6f 72 20 69 66 0a 20 20 20 20 2a 2a 20  le or if.    ** 
59c0: 77 65 20 68 61 76 65 20 72 65 63 65 69 76 65 64  we have received
59d0: 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61   information tha
59e0: 74 20 68 61 73 20 63 61 75 73 65 64 20 75 73 20  t has caused us 
59f0: 74 6f 20 63 72 65 61 74 65 0a 20 20 20 20 2a 2a  to create.    **
5a00: 20 6e 65 77 20 70 68 61 6e 74 6f 6d 73 20 61 6e   new phantoms an
5a10: 64 20 77 65 20 68 61 76 65 20 6f 6e 65 20 6f 72  d we have one or
5a20: 20 6d 6f 72 65 20 70 68 61 6e 74 6f 6d 73 2c 20   more phantoms, 
5a30: 74 68 65 6e 20 67 6f 20 66 6f 72 20 0a 20 20 20  then go for .   
5a40: 20 2a 2a 20 61 6e 6f 74 68 65 72 20 72 6f 75 6e   ** another roun
5a50: 64 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  d.    */.    if(
5a60: 20 28 78 66 65 72 2e 6e 46 69 6c 65 52 63 76 64   (xfer.nFileRcvd
5a70: 2b 78 66 65 72 2e 6e 44 65 6c 74 61 52 63 76 64  +xfer.nDeltaRcvd
5a80: 2b 78 66 65 72 2e 6e 44 61 6e 67 6c 69 6e 67 46  +xfer.nDanglingF
5a90: 69 6c 65 3e 30 20 7c 7c 20 6e 65 77 50 68 61 6e  ile>0 || newPhan
5aa0: 74 6f 6d 29 0a 20 20 20 20 20 26 26 20 64 62 5f  tom).     && db_
5ab0: 65 78 69 73 74 73 28 22 53 45 4c 45 43 54 20 31  exists("SELECT 1
5ac0: 20 46 52 4f 4d 20 70 68 61 6e 74 6f 6d 22 29 0a   FROM phantom").
5ad0: 20 20 20 20 29 7b 0a 20 20 20 20 20 20 67 6f 20      ){.      go 
5ae0: 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  = 1;.    }..    
5af0: 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 6f 6e  /* If we have on
5b00: 65 20 6f 72 20 6d 6f 72 65 20 66 69 6c 65 73 20  e or more files 
5b10: 71 75 65 75 65 64 20 74 6f 20 73 65 6e 64 2c 20  queued to send, 
5b20: 74 68 65 6e 20 67 6f 0a 20 20 20 20 2a 2a 20 61  then go.    ** a
5b30: 6e 6f 74 68 65 72 20 72 6f 75 6e 64 20 0a 20 20  nother round .  
5b40: 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 78 66 65    */.    if( xfe
5b50: 72 2e 6e 46 69 6c 65 53 65 6e 74 2b 78 66 65 72  r.nFileSent+xfer
5b60: 2e 6e 44 65 6c 74 61 53 65 6e 74 3e 30 20 29 7b  .nDeltaSent>0 ){
5b70: 0a 20 20 20 20 20 20 67 6f 20 3d 20 31 3b 0a 20  .      go = 1;. 
5b80: 20 20 20 7d 0a 20 20 7d 3b 0a 20 20 68 74 74 70     }.  };.  http
5b90: 5f 63 6c 6f 73 65 28 29 3b 0a 20 20 64 62 5f 65  _close();.  db_e
5ba0: 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 30  nd_transaction(0
5bb0: 29 3b 0a 7d 0a                                   );.}.