Hex Artifact Content
Not logged in

Artifact e782ae3d33c6f11a2e2b1946ad359a0ba3aced37:

File src/xfer.c part of check-in [edbb332d54] - The xfer mechanism has been completely reworked to better support delta compression and to require fewer round-trips. The wire protocol is roughly the same but is different enough that you will need to recompile before sync will work. by drh on 2007-08-10 02:59:52.

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: 3d 30 0a 20 20 20 7c 7c 20 28 70 58 66 65 72 2d  =0.   || (pXfer-
0ca0: 3e 6e 54 6f 6b 65 6e 3d 3d 34 20 26 26 20 21 62  >nToken==4 && !b
0cb0: 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26 70 58 66  lob_is_uuid(&pXf
0cc0: 65 72 2d 3e 61 54 6f 6b 65 6e 5b 32 5d 29 29 0a  er->aToken[2])).
0cd0: 20 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 61 70    ){.    blob_ap
0ce0: 70 65 6e 64 66 28 26 70 58 66 65 72 2d 3e 65 72  pendf(&pXfer->er
0cf0: 72 2c 20 22 6d 61 6c 66 6f 72 6d 65 64 20 66 69  r, "malformed fi
0d00: 6c 65 20 6c 69 6e 65 22 29 3b 0a 20 20 20 20 72  le line");.    r
0d10: 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 62 6c 6f  eturn;.  }.  blo
0d20: 62 5f 7a 65 72 6f 28 26 63 6f 6e 74 65 6e 74 29  b_zero(&content)
0d30: 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 68  ;.  blob_zero(&h
0d40: 61 73 68 29 3b 0a 20 20 62 6c 6f 62 5f 65 78 74  ash);.  blob_ext
0d50: 72 61 63 74 28 70 58 66 65 72 2d 3e 70 49 6e 2c  ract(pXfer->pIn,
0d60: 20 6e 2c 20 26 63 6f 6e 74 65 6e 74 29 3b 0a 20   n, &content);. 
0d70: 20 69 66 28 20 70 58 66 65 72 2d 3e 6e 54 6f 6b   if( pXfer->nTok
0d80: 65 6e 3d 3d 34 20 29 7b 0a 20 20 20 20 42 6c 6f  en==4 ){.    Blo
0d90: 62 20 73 72 63 3b 0a 20 20 20 20 69 6e 74 20 73  b src;.    int s
0da0: 72 63 69 64 20 3d 20 72 69 64 5f 66 72 6f 6d 5f  rcid = rid_from_
0db0: 75 75 69 64 28 26 70 58 66 65 72 2d 3e 61 54 6f  uuid(&pXfer->aTo
0dc0: 6b 65 6e 5b 32 5d 2c 20 31 29 3b 0a 20 20 20 20  ken[2], 1);.    
0dd0: 69 66 28 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28  if( content_get(
0de0: 73 72 63 69 64 2c 20 26 73 72 63 29 3d 3d 30 20  srcid, &src)==0 
0df0: 29 7b 0a 20 20 20 20 20 20 63 6f 6e 74 65 6e 74  ){.      content
0e00: 5f 70 75 74 28 26 63 6f 6e 74 65 6e 74 2c 20 62  _put(&content, b
0e10: 6c 6f 62 5f 73 74 72 28 26 70 58 66 65 72 2d 3e  lob_str(&pXfer->
0e20: 61 54 6f 6b 65 6e 5b 31 5d 29 2c 20 73 72 63 69  aToken[1]), srci
0e30: 64 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 61  d);.      blob_a
0e40: 70 70 65 6e 64 66 28 70 58 66 65 72 2d 3e 70 4f  ppendf(pXfer->pO
0e50: 75 74 2c 20 22 67 69 6d 6d 65 20 25 62 5c 6e 22  ut, "gimme %b\n"
0e60: 2c 20 26 70 58 66 65 72 2d 3e 61 54 6f 6b 65 6e  , &pXfer->aToken
0e70: 5b 32 5d 29 3b 0a 20 20 20 20 20 20 70 58 66 65  [2]);.      pXfe
0e80: 72 2d 3e 6e 47 69 6d 6d 65 53 65 6e 74 2b 2b 3b  r->nGimmeSent++;
0e90: 0a 20 20 20 20 20 20 70 58 66 65 72 2d 3e 6e 44  .      pXfer->nD
0ea0: 61 6e 67 6c 69 6e 67 46 69 6c 65 2b 2b 3b 0a 20  anglingFile++;. 
0eb0: 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
0ec0: 20 7d 0a 20 20 20 20 70 58 66 65 72 2d 3e 6e 44   }.    pXfer->nD
0ed0: 65 6c 74 61 52 63 76 64 2b 2b 3b 0a 20 20 20 20  eltaRcvd++;.    
0ee0: 62 6c 6f 62 5f 64 65 6c 74 61 5f 61 70 70 6c 79  blob_delta_apply
0ef0: 28 26 73 72 63 2c 20 26 63 6f 6e 74 65 6e 74 2c  (&src, &content,
0f00: 20 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 20 20   &content);.    
0f10: 62 6c 6f 62 5f 72 65 73 65 74 28 26 73 72 63 29  blob_reset(&src)
0f20: 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
0f30: 58 66 65 72 2d 3e 6e 46 69 6c 65 52 63 76 64 2b  Xfer->nFileRcvd+
0f40: 2b 3b 0a 20 20 7d 0a 20 20 73 68 61 31 73 75 6d  +;.  }.  sha1sum
0f50: 5f 62 6c 6f 62 28 26 63 6f 6e 74 65 6e 74 2c 20  _blob(&content, 
0f60: 26 68 61 73 68 29 3b 0a 20 20 69 66 28 20 21 62  &hash);.  if( !b
0f70: 6c 6f 62 5f 65 71 5f 73 74 72 28 26 70 58 66 65  lob_eq_str(&pXfe
0f80: 72 2d 3e 61 54 6f 6b 65 6e 5b 31 5d 2c 20 62 6c  r->aToken[1], bl
0f90: 6f 62 5f 73 74 72 28 26 68 61 73 68 29 2c 20 2d  ob_str(&hash), -
0fa0: 31 29 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 61  1) ){.    blob_a
0fb0: 70 70 65 6e 64 66 28 26 70 58 66 65 72 2d 3e 65  ppendf(&pXfer->e
0fc0: 72 72 2c 20 22 63 6f 6e 74 65 6e 74 20 64 6f 65  rr, "content doe
0fd0: 73 20 6e 6f 74 20 6d 61 74 63 68 20 73 68 61 31  s not match sha1
0fe0: 20 68 61 73 68 22 29 3b 0a 20 20 7d 0a 20 20 62   hash");.  }.  b
0ff0: 6c 6f 62 5f 72 65 73 65 74 28 26 68 61 73 68 29  lob_reset(&hash)
1000: 3b 0a 20 20 72 69 64 20 3d 20 63 6f 6e 74 65 6e  ;.  rid = conten
1010: 74 5f 70 75 74 28 26 63 6f 6e 74 65 6e 74 2c 20  t_put(&content, 
1020: 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 69 64  0, 0);.  if( rid
1030: 3d 3d 30 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f  ==0 ){.    blob_
1040: 61 70 70 65 6e 64 66 28 26 70 58 66 65 72 2d 3e  appendf(&pXfer->
1050: 65 72 72 2c 20 22 25 73 22 2c 20 67 2e 7a 45 72  err, "%s", g.zEr
1060: 72 4d 73 67 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  rMsg);.  }else{.
1070: 20 20 20 20 6d 61 6e 69 66 65 73 74 5f 63 72 6f      manifest_cro
1080: 73 73 6c 69 6e 6b 28 72 69 64 2c 20 26 63 6f 6e  sslink(rid, &con
1090: 74 65 6e 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  tent);.  }.}../*
10a0: 0a 2a 2a 20 54 72 79 20 74 6f 20 73 65 6e 64 20  .** Try to send 
10b0: 61 20 66 69 6c 65 20 61 73 20 61 20 64 65 6c 74  a file as a delt
10c0: 61 2e 20 20 49 66 20 73 75 63 63 65 73 73 66 75  a.  If successfu
10d0: 6c 2c 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75  l, return the nu
10e0: 6d 62 65 72 0a 2a 2a 20 6f 66 20 62 79 74 65 73  mber.** of bytes
10f0: 20 69 6e 20 74 68 65 20 64 65 6c 74 61 2e 20 20   in the delta.  
1100: 49 66 20 6e 6f 74 2c 20 72 65 74 75 72 6e 20 7a  If not, return z
1110: 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 72  ero..**.** If sr
1120: 63 49 64 20 69 73 20 73 70 65 63 69 66 69 65 64  cId is specified
1130: 2c 20 75 73 65 20 69 74 2e 20 20 49 66 20 6e 6f  , use it.  If no
1140: 74 2c 20 74 72 79 20 74 6f 20 66 69 67 75 72 65  t, try to figure
1150: 20 6f 75 74 20 61 0a 2a 2a 20 72 65 61 73 6f 6e   out a.** reason
1160: 61 62 6c 65 20 73 72 63 49 64 2e 0a 2a 2f 0a 73  able srcId..*/.s
1170: 74 61 74 69 63 20 69 6e 74 20 73 65 6e 64 5f 61  tatic int send_a
1180: 73 5f 64 65 6c 74 61 28 0a 20 20 58 66 65 72 20  s_delta(.  Xfer 
1190: 2a 70 58 66 65 72 2c 20 20 20 20 20 20 20 20 20  *pXfer,         
11a0: 20 20 20 2f 2a 20 54 68 65 20 74 72 61 6e 73 66     /* The transf
11b0: 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
11c0: 69 6e 74 20 72 69 64 2c 20 20 20 20 20 20 20 20  int rid,        
11d0: 20 20 20 20 20 20 20 20 2f 2a 20 72 65 63 6f 72          /* recor
11e0: 64 20 69 64 20 6f 66 20 74 68 65 20 66 69 6c 65  d id of the file
11f0: 20 74 6f 20 73 65 6e 64 20 2a 2f 0a 20 20 42 6c   to send */.  Bl
1200: 6f 62 20 2a 70 43 6f 6e 74 65 6e 74 2c 20 20 20  ob *pContent,   
1210: 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e        /* The con
1220: 74 65 6e 74 20 6f 66 20 74 68 65 20 66 69 6c 65  tent of the file
1230: 20 74 6f 20 73 65 6e 64 20 2a 2f 0a 20 20 42 6c   to send */.  Bl
1240: 6f 62 20 2a 70 55 75 69 64 2c 20 20 20 20 20 20  ob *pUuid,      
1250: 20 20 20 20 20 20 2f 2a 20 54 68 65 20 55 55 49        /* The UUI
1260: 44 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 6f  D of the file to
1270: 20 73 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 73   send */.  int s
1280: 72 63 49 64 20 20 20 20 20 20 20 20 20 20 20 20  rcId            
1290: 20 20 20 2f 2a 20 53 65 6e 64 20 61 73 20 61 20     /* Send as a 
12a0: 64 65 6c 74 61 20 61 67 61 69 6e 73 74 20 74 68  delta against th
12b0: 69 73 20 72 65 63 6f 72 64 20 2a 2f 0a 29 7b 0a  is record */.){.
12c0: 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63    static const c
12d0: 68 61 72 20 2a 61 7a 51 75 65 72 79 5b 5d 20 3d  har *azQuery[] =
12e0: 20 7b 0a 20 20 20 20 22 53 45 4c 45 43 54 20 70   {.    "SELECT p
12f0: 69 64 20 46 52 4f 4d 20 70 6c 69 6e 6b 22 0a 20  id FROM plink". 
1300: 20 20 20 22 20 57 48 45 52 45 20 63 69 64 3d 25     " WHERE cid=%
1310: 64 22 0a 20 20 20 20 22 20 20 20 41 4e 44 20 4e  d".    "   AND N
1320: 4f 54 20 45 58 49 53 54 53 28 53 45 4c 45 43 54  OT EXISTS(SELECT
1330: 20 31 20 46 52 4f 4d 20 70 68 61 6e 74 6f 6d 20   1 FROM phantom 
1340: 57 48 45 52 45 20 72 69 64 3d 70 69 64 29 22 2c  WHERE rid=pid)",
1350: 0a 0a 20 20 20 20 22 53 45 4c 45 43 54 20 70 69  ..    "SELECT pi
1360: 64 20 46 52 4f 4d 20 6d 6c 69 6e 6b 22 0a 20 20  d FROM mlink".  
1370: 20 20 22 20 57 48 45 52 45 20 66 69 64 3d 25 64    " WHERE fid=%d
1380: 22 0a 20 20 20 20 22 20 20 20 41 4e 44 20 4e 4f  ".    "   AND NO
1390: 54 20 45 58 49 53 54 53 28 53 45 4c 45 43 54 20  T EXISTS(SELECT 
13a0: 31 20 46 52 4f 4d 20 70 68 61 6e 74 6f 6d 20 57  1 FROM phantom W
13b0: 48 45 52 45 20 72 69 64 3d 70 69 64 29 22 2c 0a  HERE rid=pid)",.
13c0: 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20    };.  int i;.  
13d0: 42 6c 6f 62 20 73 72 63 2c 20 64 65 6c 74 61 3b  Blob src, delta;
13e0: 0a 20 20 69 6e 74 20 73 69 7a 65 20 3d 20 30 3b  .  int size = 0;
13f0: 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 73 72 63  ..  for(i=0; src
1400: 49 64 3d 3d 30 20 26 26 20 69 3c 63 6f 75 6e 74  Id==0 && i<count
1410: 28 61 7a 51 75 65 72 79 29 3b 20 69 2b 2b 29 7b  (azQuery); i++){
1420: 0a 20 20 20 20 73 72 63 49 64 20 3d 20 64 62 5f  .    srcId = db_
1430: 69 6e 74 28 30 2c 20 61 7a 51 75 65 72 79 5b 69  int(0, azQuery[i
1440: 5d 2c 20 72 69 64 29 3b 0a 20 20 7d 0a 20 20 69  ], rid);.  }.  i
1450: 66 28 20 73 72 63 49 64 3e 30 20 26 26 20 63 6f  f( srcId>0 && co
1460: 6e 74 65 6e 74 5f 67 65 74 28 73 72 63 49 64 2c  ntent_get(srcId,
1470: 20 26 73 72 63 29 20 29 7b 0a 20 20 20 20 63 68   &src) ){.    ch
1480: 61 72 20 2a 7a 55 75 69 64 20 3d 20 64 62 5f 74  ar *zUuid = db_t
1490: 65 78 74 28 30 2c 20 22 53 45 4c 45 43 54 20 75  ext(0, "SELECT u
14a0: 75 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48  uid FROM blob WH
14b0: 45 52 45 20 72 69 64 3d 25 64 22 2c 20 73 72 63  ERE rid=%d", src
14c0: 49 64 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 64 65  Id);.    blob_de
14d0: 6c 74 61 5f 63 72 65 61 74 65 28 26 73 72 63 2c  lta_create(&src,
14e0: 20 70 43 6f 6e 74 65 6e 74 2c 20 26 64 65 6c 74   pContent, &delt
14f0: 61 29 3b 0a 20 20 20 20 73 69 7a 65 20 3d 20 62  a);.    size = b
1500: 6c 6f 62 5f 73 69 7a 65 28 26 64 65 6c 74 61 29  lob_size(&delta)
1510: 3b 0a 20 20 20 20 69 66 28 20 73 69 7a 65 3e 3d  ;.    if( size>=
1520: 62 6c 6f 62 5f 73 69 7a 65 28 70 43 6f 6e 74 65  blob_size(pConte
1530: 6e 74 29 2d 35 30 20 29 7b 0a 20 20 20 20 20 20  nt)-50 ){.      
1540: 73 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65  size = 0;.    }e
1550: 6c 73 65 7b 0a 20 20 20 20 20 20 62 6c 6f 62 5f  lse{.      blob_
1560: 61 70 70 65 6e 64 66 28 70 58 66 65 72 2d 3e 70  appendf(pXfer->p
1570: 4f 75 74 2c 20 22 66 69 6c 65 20 25 62 20 25 73  Out, "file %b %s
1580: 20 25 64 5c 6e 22 2c 20 70 55 75 69 64 2c 20 7a   %d\n", pUuid, z
1590: 55 75 69 64 2c 20 73 69 7a 65 29 3b 0a 20 20 20  Uuid, size);.   
15a0: 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 28 70     blob_append(p
15b0: 58 66 65 72 2d 3e 70 4f 75 74 2c 20 62 6c 6f 62  Xfer->pOut, blob
15c0: 5f 62 75 66 66 65 72 28 26 64 65 6c 74 61 29 2c  _buffer(&delta),
15d0: 20 73 69 7a 65 29 3b 0a 20 20 20 20 20 20 2f 2a   size);.      /*
15e0: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70 58   blob_appendf(pX
15f0: 66 65 72 2d 3e 70 4f 75 74 2c 20 22 5c 6e 22 2c  fer->pOut, "\n",
1600: 20 31 29 3b 20 2a 2f 0a 20 20 20 20 7d 0a 20 20   1); */.    }.  
1610: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 64 65    blob_reset(&de
1620: 6c 74 61 29 3b 0a 20 20 20 20 66 72 65 65 28 7a  lta);.    free(z
1630: 55 75 69 64 29 3b 0a 20 20 20 20 62 6c 6f 62 5f  Uuid);.    blob_
1640: 72 65 73 65 74 28 26 73 72 63 29 3b 0a 20 20 7d  reset(&src);.  }
1650: 0a 20 20 72 65 74 75 72 6e 20 73 69 7a 65 3b 0a  .  return size;.
1660: 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 6e 64 20 74 68  }../*.** Send th
1670: 65 20 66 69 6c 65 20 69 64 65 6e 74 69 66 69 65  e file identifie
1680: 64 20 62 79 20 72 69 64 2e 0a 2a 2a 0a 2a 2a 20  d by rid..**.** 
1690: 54 68 65 20 70 55 75 69 64 20 63 61 6e 20 62 65  The pUuid can be
16a0: 20 4e 55 4c 4c 20 69 6e 20 77 68 69 63 68 20 63   NULL in which c
16b0: 61 73 65 20 74 68 65 20 63 6f 72 72 65 63 74 20  ase the correct 
16c0: 55 55 49 44 20 69 73 20 63 6f 6d 70 75 74 65 64  UUID is computed
16d0: 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 72 69 64  .** from the rid
16e0: 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 72 63 49 64  ..**.** If srcId
16f0: 20 69 73 20 70 6f 73 69 74 69 76 65 2c 20 74 68   is positive, th
1700: 65 6e 20 61 20 64 65 6c 74 61 20 69 73 20 73 65  en a delta is se
1710: 6e 74 20 61 67 61 69 6e 73 74 20 74 68 61 74 20  nt against that 
1720: 73 72 63 49 64 2e 0a 2a 2a 20 49 66 20 73 72 63  srcId..** If src
1730: 49 64 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e  Id is zero, then
1740: 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d   an attempt is m
1750: 61 64 65 20 74 6f 20 66 69 6e 64 20 61 6e 20 61  ade to find an a
1760: 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20 66 69  ppropriate.** fi
1770: 6c 65 20 74 6f 20 64 65 6c 74 61 20 61 67 61 69  le to delta agai
1780: 6e 73 74 2e 20 20 20 49 66 20 73 72 63 49 64 20  nst.   If srcId 
1790: 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65  is negative, the
17a0: 20 66 69 6c 65 20 69 73 20 73 65 6e 74 0a 2a 2a   file is sent.**
17b0: 20 77 69 74 68 6f 75 74 20 64 65 6c 74 61 69 6e   without deltain
17c0: 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  g..*/.static voi
17d0: 64 20 73 65 6e 64 5f 66 69 6c 65 28 58 66 65 72  d send_file(Xfer
17e0: 20 2a 70 58 66 65 72 2c 20 69 6e 74 20 72 69 64   *pXfer, int rid
17f0: 2c 20 42 6c 6f 62 20 2a 70 55 75 69 64 2c 20 69  , Blob *pUuid, i
1800: 6e 74 20 73 72 63 49 64 29 7b 0a 20 20 42 6c 6f  nt srcId){.  Blo
1810: 62 20 63 6f 6e 74 65 6e 74 2c 20 75 75 69 64 3b  b content, uuid;
1820: 0a 20 20 69 6e 74 20 73 69 7a 65 20 3d 20 30 3b  .  int size = 0;
1830: 0a 0a 20 20 69 66 28 20 64 62 5f 65 78 69 73 74  ..  if( db_exist
1840: 73 28 22 53 45 4c 45 43 54 20 31 20 46 52 4f 4d  s("SELECT 1 FROM
1850: 20 73 65 6e 74 20 57 48 45 52 45 20 72 69 64 3d   sent WHERE rid=
1860: 25 64 22 2c 20 72 69 64 29 20 29 7b 0a 20 20 20  %d", rid) ){.   
1870: 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
1880: 62 6c 6f 62 5f 7a 65 72 6f 28 26 75 75 69 64 29  blob_zero(&uuid)
1890: 3b 0a 20 20 69 66 28 20 70 55 75 69 64 3d 3d 30  ;.  if( pUuid==0
18a0: 20 29 7b 0a 20 20 20 20 64 62 5f 62 6c 6f 62 28   ){.    db_blob(
18b0: 26 75 75 69 64 2c 20 22 53 45 4c 45 43 54 20 75  &uuid, "SELECT u
18c0: 75 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48  uid FROM blob WH
18d0: 45 52 45 20 72 69 64 3d 25 64 20 41 4e 44 20 73  ERE rid=%d AND s
18e0: 69 7a 65 3e 3d 30 22 2c 20 72 69 64 29 3b 0a 20  ize>=0", rid);. 
18f0: 20 20 20 69 66 28 20 62 6c 6f 62 5f 73 69 7a 65     if( blob_size
1900: 28 26 75 75 69 64 29 3d 3d 30 20 29 7b 0a 20 20  (&uuid)==0 ){.  
1910: 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
1920: 7d 0a 20 20 20 20 70 55 75 69 64 20 3d 20 26 75  }.    pUuid = &u
1930: 75 69 64 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  uid;.  }.  if( p
1940: 58 66 65 72 2d 3e 6d 78 53 65 6e 64 3c 3d 62 6c  Xfer->mxSend<=bl
1950: 6f 62 5f 73 69 7a 65 28 70 58 66 65 72 2d 3e 70  ob_size(pXfer->p
1960: 4f 75 74 29 20 29 7b 0a 20 20 20 20 62 6c 6f 62  Out) ){.    blob
1970: 5f 61 70 70 65 6e 64 66 28 70 58 66 65 72 2d 3e  _appendf(pXfer->
1980: 70 4f 75 74 2c 20 22 69 67 6f 74 20 25 62 5c 6e  pOut, "igot %b\n
1990: 22 2c 20 70 55 75 69 64 29 3b 0a 20 20 20 20 70  ", pUuid);.    p
19a0: 58 66 65 72 2d 3e 6e 49 47 6f 74 53 65 6e 74 2b  Xfer->nIGotSent+
19b0: 2b 3b 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 65  +;.    blob_rese
19c0: 74 28 26 75 75 69 64 29 3b 0a 20 20 20 20 72 65  t(&uuid);.    re
19d0: 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 63 6f 6e 74  turn;.  }.  cont
19e0: 65 6e 74 5f 67 65 74 28 72 69 64 2c 20 26 63 6f  ent_get(rid, &co
19f0: 6e 74 65 6e 74 29 3b 0a 0a 20 20 69 66 28 20 62  ntent);..  if( b
1a00: 6c 6f 62 5f 73 69 7a 65 28 26 63 6f 6e 74 65 6e  lob_size(&conten
1a10: 74 29 3e 31 30 30 20 29 7b 0a 20 20 20 20 73 69  t)>100 ){.    si
1a20: 7a 65 20 3d 20 73 65 6e 64 5f 61 73 5f 64 65 6c  ze = send_as_del
1a30: 74 61 28 70 58 66 65 72 2c 20 72 69 64 2c 20 26  ta(pXfer, rid, &
1a40: 63 6f 6e 74 65 6e 74 2c 20 70 55 75 69 64 2c 20  content, pUuid, 
1a50: 73 72 63 49 64 29 3b 0a 20 20 7d 0a 20 20 69 66  srcId);.  }.  if
1a60: 28 20 73 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20  ( size==0 ){.   
1a70: 20 69 6e 74 20 73 69 7a 65 20 3d 20 62 6c 6f 62   int size = blob
1a80: 5f 73 69 7a 65 28 26 63 6f 6e 74 65 6e 74 29 3b  _size(&content);
1a90: 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64  .    blob_append
1aa0: 66 28 70 58 66 65 72 2d 3e 70 4f 75 74 2c 20 22  f(pXfer->pOut, "
1ab0: 66 69 6c 65 20 25 62 20 25 64 5c 6e 22 2c 20 70  file %b %d\n", p
1ac0: 55 75 69 64 2c 20 73 69 7a 65 29 3b 0a 20 20 20  Uuid, size);.   
1ad0: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 28 70 58 66   blob_append(pXf
1ae0: 65 72 2d 3e 70 4f 75 74 2c 20 62 6c 6f 62 5f 62  er->pOut, blob_b
1af0: 75 66 66 65 72 28 26 63 6f 6e 74 65 6e 74 29 2c  uffer(&content),
1b00: 20 73 69 7a 65 29 3b 0a 20 20 20 20 70 58 66 65   size);.    pXfe
1b10: 72 2d 3e 6e 46 69 6c 65 53 65 6e 74 2b 2b 3b 0a  r->nFileSent++;.
1b20: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 58 66    }else{.    pXf
1b30: 65 72 2d 3e 6e 44 65 6c 74 61 53 65 6e 74 2b 2b  er->nDeltaSent++
1b40: 3b 0a 20 20 7d 0a 20 20 64 62 5f 6d 75 6c 74 69  ;.  }.  db_multi
1b50: 5f 65 78 65 63 28 22 49 4e 53 45 52 54 20 49 4e  _exec("INSERT IN
1b60: 54 4f 20 73 65 6e 74 20 56 41 4c 55 45 53 28 25  TO sent VALUES(%
1b70: 64 29 22 2c 20 72 69 64 29 3b 0a 20 20 62 6c 6f  d)", rid);.  blo
1b80: 62 5f 72 65 73 65 74 28 26 75 75 69 64 29 3b 0a  b_reset(&uuid);.
1b90: 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
1ba0: 75 74 69 6e 65 20 72 75 6e 73 20 77 68 65 6e 20  utine runs when 
1bb0: 65 69 74 68 65 72 20 63 6c 69 65 6e 74 20 6f 72  either client or
1bc0: 20 73 65 72 76 65 72 20 69 73 20 6e 6f 74 69 66   server is notif
1bd0: 69 65 64 20 74 68 61 74 0a 2a 2a 20 74 68 65 20  ied that.** the 
1be0: 6f 74 68 65 72 20 73 69 64 65 20 74 68 69 6e 67  other side thing
1bf0: 73 20 72 69 64 20 69 73 20 61 20 6c 65 61 66 20  s rid is a leaf 
1c00: 6d 61 6e 69 66 65 73 74 2e 20 20 49 66 20 77 65  manifest.  If we
1c10: 20 68 6f 6c 64 0a 2a 2a 20 63 68 69 6c 64 72 65   hold.** childre
1c20: 6e 20 6f 66 20 72 69 64 2c 20 74 68 65 6e 20 73  n of rid, then s
1c30: 65 6e 64 20 74 68 65 6d 20 6f 76 65 72 20 74 6f  end them over to
1c40: 20 74 68 65 20 6f 74 68 65 72 20 73 69 64 65 2e   the other side.
1c50: 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1c60: 6c 65 61 66 5f 72 65 73 70 6f 6e 73 65 28 58 66  leaf_response(Xf
1c70: 65 72 20 2a 70 58 66 65 72 2c 20 69 6e 74 20 72  er *pXfer, int r
1c80: 69 64 29 7b 0a 20 20 53 74 6d 74 20 71 31 2c 20  id){.  Stmt q1, 
1c90: 71 32 3b 0a 20 20 64 62 5f 70 72 65 70 61 72 65  q2;.  db_prepare
1ca0: 28 26 71 31 2c 0a 20 20 20 20 20 20 22 53 45 4c  (&q1,.      "SEL
1cb0: 45 43 54 20 63 69 64 2c 20 75 75 69 64 20 46 52  ECT cid, uuid FR
1cc0: 4f 4d 20 70 6c 69 6e 6b 2c 20 62 6c 6f 62 22 0a  OM plink, blob".
1cd0: 20 20 20 20 20 20 22 20 57 48 45 52 45 20 62 6c        " WHERE bl
1ce0: 6f 62 2e 72 69 64 3d 70 6c 69 6e 6b 2e 63 69 64  ob.rid=plink.cid
1cf0: 22 0a 20 20 20 20 20 20 22 20 20 20 41 4e 44 20  ".      "   AND 
1d00: 70 6c 69 6e 6b 2e 70 69 64 3d 25 64 22 2c 0a 20  plink.pid=%d",. 
1d10: 20 20 20 20 20 72 69 64 0a 20 20 29 3b 0a 20 20       rid.  );.  
1d20: 77 68 69 6c 65 28 20 64 62 5f 73 74 65 70 28 26  while( db_step(&
1d30: 71 31 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20  q1)==SQLITE_ROW 
1d40: 29 7b 0a 20 20 20 20 42 6c 6f 62 20 75 75 69 64  ){.    Blob uuid
1d50: 3b 0a 20 20 20 20 69 6e 74 20 63 69 64 3b 0a 0a  ;.    int cid;..
1d60: 20 20 20 20 63 69 64 20 3d 20 64 62 5f 63 6f 6c      cid = db_col
1d70: 75 6d 6e 5f 69 6e 74 28 26 71 31 2c 20 30 29 3b  umn_int(&q1, 0);
1d80: 0a 20 20 20 20 64 62 5f 65 70 68 65 6d 65 72 61  .    db_ephemera
1d90: 6c 5f 62 6c 6f 62 28 26 71 31 2c 20 31 2c 20 26  l_blob(&q1, 1, &
1da0: 75 75 69 64 29 3b 0a 20 20 20 20 73 65 6e 64 5f  uuid);.    send_
1db0: 66 69 6c 65 28 70 58 66 65 72 2c 20 63 69 64 2c  file(pXfer, cid,
1dc0: 20 26 75 75 69 64 2c 20 72 69 64 29 3b 0a 20 20   &uuid, rid);.  
1dd0: 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 71 32    db_prepare(&q2
1de0: 2c 0a 20 20 20 20 20 20 20 22 53 45 4c 45 43 54  ,.       "SELECT
1df0: 20 70 69 64 2c 20 75 75 69 64 2c 20 66 69 64 20   pid, uuid, fid 
1e00: 46 52 4f 4d 20 6d 6c 69 6e 6b 2c 20 62 6c 6f 62  FROM mlink, blob
1e10: 22 0a 20 20 20 20 20 20 20 22 20 57 48 45 52 45  ".       " WHERE
1e20: 20 72 69 64 3d 66 69 64 20 41 4e 44 20 6d 69 64   rid=fid AND mid
1e30: 3d 25 64 22 2c 0a 20 20 20 20 20 20 20 63 69 64  =%d",.       cid
1e40: 0a 20 20 20 20 29 3b 0a 20 20 20 20 77 68 69 6c  .    );.    whil
1e50: 65 28 20 64 62 5f 73 74 65 70 28 26 71 32 29 3d  e( db_step(&q2)=
1e60: 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20  =SQLITE_ROW ){. 
1e70: 20 20 20 20 20 69 6e 74 20 70 69 64 2c 20 66 69       int pid, fi
1e80: 64 3b 0a 20 20 20 20 20 20 70 69 64 20 3d 20 64  d;.      pid = d
1e90: 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 32  b_column_int(&q2
1ea0: 2c 20 30 29 3b 0a 20 20 20 20 20 20 64 62 5f 65  , 0);.      db_e
1eb0: 70 68 65 6d 65 72 61 6c 5f 62 6c 6f 62 28 26 71  phemeral_blob(&q
1ec0: 32 2c 20 31 2c 20 26 75 75 69 64 29 3b 0a 20 20  2, 1, &uuid);.  
1ed0: 20 20 20 20 66 69 64 20 3d 20 64 62 5f 63 6f 6c      fid = db_col
1ee0: 75 6d 6e 5f 69 6e 74 28 26 71 32 2c 20 32 29 3b  umn_int(&q2, 2);
1ef0: 0a 20 20 20 20 20 20 73 65 6e 64 5f 66 69 6c 65  .      send_file
1f00: 28 70 58 66 65 72 2c 20 66 69 64 2c 20 26 75 75  (pXfer, fid, &uu
1f10: 69 64 2c 20 70 69 64 29 3b 0a 20 20 20 20 7d 0a  id, pid);.    }.
1f20: 20 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28      db_finalize(
1f30: 26 71 32 29 3b 0a 20 20 20 20 69 66 28 20 62 6c  &q2);.    if( bl
1f40: 6f 62 5f 73 69 7a 65 28 70 58 66 65 72 2d 3e 70  ob_size(pXfer->p
1f50: 4f 75 74 29 3c 70 58 66 65 72 2d 3e 6d 78 53 65  Out)<pXfer->mxSe
1f60: 6e 64 20 29 7b 0a 20 20 20 20 20 20 6c 65 61 66  nd ){.      leaf
1f70: 5f 72 65 73 70 6f 6e 73 65 28 70 58 66 65 72 2c  _response(pXfer,
1f80: 20 63 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d   cid);.    }.  }
1f90: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 6e 74 20 61  .}../*.** Sent a
1fa0: 20 6c 65 61 66 20 6d 65 73 73 61 67 65 20 66 6f   leaf message fo
1fb0: 72 20 65 76 65 72 79 20 6c 65 61 66 2e 0a 2a 2f  r every leaf..*/
1fc0: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 6e  .static void sen
1fd0: 64 5f 6c 65 61 76 65 73 28 58 66 65 72 20 2a 70  d_leaves(Xfer *p
1fe0: 58 66 65 72 29 7b 0a 20 20 53 74 6d 74 20 71 3b  Xfer){.  Stmt q;
1ff0: 0a 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 71  .  db_prepare(&q
2000: 2c 20 0a 20 20 20 20 22 53 45 4c 45 43 54 20 75  , .    "SELECT u
2010: 75 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48  uid FROM blob WH
2020: 45 52 45 20 72 69 64 20 49 4e 22 0a 20 20 20 20  ERE rid IN".    
2030: 22 20 20 28 53 45 4c 45 43 54 20 63 69 64 20 46  "  (SELECT cid F
2040: 52 4f 4d 20 70 6c 69 6e 6b 20 45 58 43 45 50 54  ROM plink EXCEPT
2050: 20 53 45 4c 45 43 54 20 70 69 64 20 46 52 4f 4d   SELECT pid FROM
2060: 20 70 6c 69 6e 6b 29 22 0a 20 20 29 3b 0a 20 20   plink)".  );.  
2070: 77 68 69 6c 65 28 20 64 62 5f 73 74 65 70 28 26  while( db_step(&
2080: 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  q)==SQLITE_ROW )
2090: 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
20a0: 20 2a 7a 55 75 69 64 20 3d 20 64 62 5f 63 6f 6c   *zUuid = db_col
20b0: 75 6d 6e 5f 74 65 78 74 28 26 71 2c 20 30 29 3b  umn_text(&q, 0);
20c0: 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64  .    blob_append
20d0: 66 28 70 58 66 65 72 2d 3e 70 4f 75 74 2c 20 22  f(pXfer->pOut, "
20e0: 6c 65 61 66 20 25 73 5c 6e 22 2c 20 7a 55 75 69  leaf %s\n", zUui
20f0: 64 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 66 69 6e  d);.  }.  db_fin
2100: 61 6c 69 7a 65 28 26 71 29 3b 0a 7d 0a 0a 2f 2a  alize(&q);.}../*
2110: 0a 2a 2a 20 53 65 6e 20 61 20 67 69 6d 6d 65 20  .** Sen a gimme 
2120: 6d 65 73 73 61 67 65 20 66 6f 72 20 65 76 65 72  message for ever
2130: 79 20 70 68 61 6e 74 6f 6d 2e 0a 2a 2f 0a 73 74  y phantom..*/.st
2140: 61 74 69 63 20 76 6f 69 64 20 72 65 71 75 65 73  atic void reques
2150: 74 5f 70 68 61 6e 74 6f 6d 73 28 58 66 65 72 20  t_phantoms(Xfer 
2160: 2a 70 58 66 65 72 29 7b 0a 20 20 53 74 6d 74 20  *pXfer){.  Stmt 
2170: 71 3b 0a 20 20 64 62 5f 70 72 65 70 61 72 65 28  q;.  db_prepare(
2180: 26 71 2c 20 22 53 45 4c 45 43 54 20 75 75 69 64  &q, "SELECT uuid
2190: 20 46 52 4f 4d 20 70 68 61 6e 74 6f 6d 20 4a 4f   FROM phantom JO
21a0: 49 4e 20 62 6c 6f 62 20 55 53 49 4e 47 28 72 69  IN blob USING(ri
21b0: 64 29 22 29 3b 0a 20 20 77 68 69 6c 65 28 20 64  d)");.  while( d
21c0: 62 5f 73 74 65 70 28 26 71 29 3d 3d 53 51 4c 49  b_step(&q)==SQLI
21d0: 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 63 6f  TE_ROW ){.    co
21e0: 6e 73 74 20 63 68 61 72 20 2a 7a 55 75 69 64 20  nst char *zUuid 
21f0: 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  = db_column_text
2200: 28 26 71 2c 20 30 29 3b 0a 20 20 20 20 62 6c 6f  (&q, 0);.    blo
2210: 62 5f 61 70 70 65 6e 64 66 28 70 58 66 65 72 2d  b_appendf(pXfer-
2220: 3e 70 4f 75 74 2c 20 22 67 69 6d 6d 65 20 25 73  >pOut, "gimme %s
2230: 5c 6e 22 2c 20 7a 55 75 69 64 29 3b 0a 20 20 20  \n", zUuid);.   
2240: 20 70 58 66 65 72 2d 3e 6e 47 69 6d 6d 65 53 65   pXfer->nGimmeSe
2250: 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 20 64 62 5f 66  nt++;.  }.  db_f
2260: 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 7d 0a 0a  inalize(&q);.}..
2270: 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65  ./*.** Check the
2280: 20 73 69 67 6e 61 74 75 72 65 20 6f 6e 20 61 6e   signature on an
2290: 20 61 70 70 6c 69 63 61 74 69 6f 6e 2f 78 2d 66   application/x-f
22a0: 6f 73 73 69 6c 20 70 61 79 6c 6f 61 64 20 72 65  ossil payload re
22b0: 63 65 69 76 65 64 20 62 79 0a 2a 2a 20 74 68 65  ceived by.** the
22c0: 20 48 54 54 50 20 73 65 72 76 65 72 2e 20 20 54   HTTP server.  T
22d0: 68 65 20 73 69 67 6e 61 74 75 72 65 20 69 73 20  he signature is 
22e0: 61 20 6c 69 6e 65 20 6f 66 20 74 68 65 20 66 6f  a line of the fo
22f0: 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 3a 0a 2a 2a  llowing form:.**
2300: 0a 2a 2a 20 20 20 20 20 20 20 20 6c 6f 67 69 6e  .**        login
2310: 20 4c 4f 47 49 4e 20 4e 4f 4e 43 45 20 53 49 47   LOGIN NONCE SIG
2320: 4e 41 54 55 52 45 0a 2a 2a 0a 2a 2a 20 54 68 65  NATURE.**.** The
2330: 20 4e 4f 4e 43 45 20 69 73 20 61 20 72 61 6e 64   NONCE is a rand
2340: 6f 6d 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20  om string.  The 
2350: 73 65 72 76 65 72 20 77 69 6c 6c 20 6e 65 76 65  server will neve
2360: 72 20 61 63 63 65 70 74 20 61 0a 2a 2a 20 72 65  r accept a.** re
2370: 70 65 61 74 20 4e 4f 4e 43 45 2e 20 20 53 49 47  peat NONCE.  SIG
2380: 4e 41 54 55 52 45 20 69 73 20 74 68 65 20 53 48  NATURE is the SH
2390: 41 31 20 63 68 65 63 6b 73 75 6d 20 6f 66 20 74  A1 checksum of t
23a0: 68 65 20 4e 4f 4e 43 45 20 0a 2a 2a 20 63 6f 6e  he NONCE .** con
23b0: 63 61 74 65 6e 61 74 65 64 20 77 69 74 68 20 74  catenated with t
23c0: 68 65 20 75 73 65 72 73 20 70 61 73 73 77 6f 72  he users passwor
23d0: 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 72  d..**.** The par
23e0: 61 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 20  ameters to this 
23f0: 72 6f 75 74 69 6e 65 20 61 72 65 20 65 70 68 65  routine are ephe
2400: 72 6d 65 72 61 6c 20 62 6c 6f 62 73 20 68 6f 6c  rmeral blobs hol
2410: 64 69 6e 67 20 74 68 65 0a 2a 2a 20 4c 4f 47 49  ding the.** LOGI
2420: 4e 2c 20 4e 4f 4e 43 45 20 61 6e 64 20 53 49 47  N, NONCE and SIG
2430: 4e 41 54 55 52 45 2e 0a 2a 2a 0a 2a 2a 20 54 68  NATURE..**.** Th
2440: 69 73 20 72 6f 75 74 69 6e 65 20 61 74 74 65 6d  is routine attem
2450: 70 74 73 20 74 6f 20 6c 6f 63 61 74 65 20 74 68  pts to locate th
2460: 65 20 75 73 65 72 20 61 6e 64 20 76 65 72 69 66  e user and verif
2470: 79 20 74 68 65 20 73 69 67 6e 61 74 75 72 65 2e  y the signature.
2480: 0a 2a 2a 20 49 66 20 65 76 65 72 79 74 68 69 6e  .** If everythin
2490: 67 20 63 68 65 63 6b 73 20 6f 75 74 2c 20 74 68  g checks out, th
24a0: 65 20 55 53 45 52 2e 43 41 50 20 63 6f 6c 75 6d  e USER.CAP colum
24b0: 6e 20 66 6f 72 20 74 68 65 20 55 53 45 52 20 74  n for the USER t
24c0: 61 62 6c 65 0a 2a 2a 20 69 73 20 63 6f 6e 73 75  able.** is consu
24d0: 6c 74 65 64 20 74 6f 20 73 65 74 20 70 72 69 76  lted to set priv
24e0: 69 6c 65 67 65 73 20 69 6e 20 74 68 65 20 67 6c  ileges in the gl
24f0: 6f 62 61 6c 20 67 20 76 61 72 69 61 62 6c 65 2e  obal g variable.
2500: 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 74 68 69  .**.** If anythi
2510: 6e 67 20 66 61 69 6c 73 20 74 6f 20 63 68 65 63  ng fails to chec
2520: 6b 20 6f 75 74 2c 20 6e 6f 20 63 68 61 6e 67 65  k out, no change
2530: 73 20 61 72 65 20 6d 61 64 65 20 74 6f 20 70 72  s are made to pr
2540: 69 76 69 6c 65 67 65 73 2e 0a 2a 2a 0a 2a 2a 20  ivileges..**.** 
2550: 53 69 67 6e 61 74 75 72 65 20 67 65 6e 65 72 61  Signature genera
2560: 74 69 6f 6e 20 6f 6e 20 74 68 65 20 63 6c 69 65  tion on the clie
2570: 6e 74 20 73 69 64 65 20 69 73 20 68 61 6e 64 6c  nt side is handl
2580: 65 64 20 62 79 20 74 68 65 20 0a 2a 2a 20 68 74  ed by the .** ht
2590: 74 70 5f 65 78 63 68 61 6e 67 65 28 29 20 72 6f  tp_exchange() ro
25a0: 75 74 69 6e 65 2e 0a 2a 2f 0a 76 6f 69 64 20 63  utine..*/.void c
25b0: 68 65 63 6b 5f 6c 6f 67 69 6e 28 42 6c 6f 62 20  heck_login(Blob 
25c0: 2a 70 4c 6f 67 69 6e 2c 20 42 6c 6f 62 20 2a 70  *pLogin, Blob *p
25d0: 4e 6f 6e 63 65 2c 20 42 6c 6f 62 20 2a 70 53 69  Nonce, Blob *pSi
25e0: 67 29 7b 0a 20 20 53 74 6d 74 20 71 3b 0a 20 20  g){.  Stmt q;.  
25f0: 69 6e 74 20 72 63 3b 0a 0a 20 20 69 66 28 20 64  int rc;..  if( d
2600: 62 5f 65 78 69 73 74 73 28 22 53 45 4c 45 43 54  b_exists("SELECT
2610: 20 31 20 46 52 4f 4d 20 72 63 76 66 72 6f 6d 20   1 FROM rcvfrom 
2620: 57 48 45 52 45 20 6e 6f 6e 63 65 3d 25 42 22 2c  WHERE nonce=%B",
2630: 20 70 4e 6f 6e 63 65 29 20 29 7b 0a 20 20 20 20   pNonce) ){.    
2640: 72 65 74 75 72 6e 3b 20 20 2f 2a 20 4e 65 76 65  return;  /* Neve
2650: 72 20 61 63 63 65 70 74 20 61 20 72 65 70 65 61  r accept a repea
2660: 74 65 64 20 6e 6f 6e 63 65 20 2a 2f 0a 20 20 7d  ted nonce */.  }
2670: 0a 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 71  .  db_prepare(&q
2680: 2c 20 22 53 45 4c 45 43 54 20 70 77 2c 20 63 61  , "SELECT pw, ca
2690: 70 2c 20 75 69 64 20 46 52 4f 4d 20 75 73 65 72  p, uid FROM user
26a0: 20 57 48 45 52 45 20 6c 6f 67 69 6e 3d 25 42 22   WHERE login=%B"
26b0: 2c 20 70 4c 6f 67 69 6e 29 3b 0a 20 20 69 66 28  , pLogin);.  if(
26c0: 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53 51   db_step(&q)==SQ
26d0: 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20  LITE_ROW ){.    
26e0: 42 6c 6f 62 20 70 77 2c 20 63 6f 6d 62 69 6e 65  Blob pw, combine
26f0: 64 2c 20 68 61 73 68 3b 0a 20 20 20 20 62 6c 6f  d, hash;.    blo
2700: 62 5f 7a 65 72 6f 28 26 70 77 29 3b 0a 20 20 20  b_zero(&pw);.   
2710: 20 64 62 5f 65 70 68 65 6d 65 72 61 6c 5f 62 6c   db_ephemeral_bl
2720: 6f 62 28 26 71 2c 20 30 2c 20 26 70 77 29 3b 0a  ob(&q, 0, &pw);.
2730: 20 20 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 63      blob_zero(&c
2740: 6f 6d 62 69 6e 65 64 29 3b 0a 20 20 20 20 62 6c  ombined);.    bl
2750: 6f 62 5f 63 6f 70 79 28 26 63 6f 6d 62 69 6e 65  ob_copy(&combine
2760: 64 2c 20 70 4e 6f 6e 63 65 29 3b 0a 20 20 20 20  d, pNonce);.    
2770: 62 6c 6f 62 5f 61 70 70 65 6e 64 28 26 63 6f 6d  blob_append(&com
2780: 62 69 6e 65 64 2c 20 62 6c 6f 62 5f 62 75 66 66  bined, blob_buff
2790: 65 72 28 26 70 77 29 2c 20 62 6c 6f 62 5f 73 69  er(&pw), blob_si
27a0: 7a 65 28 26 70 77 29 29 3b 0a 20 20 20 20 2f 2a  ze(&pw));.    /*
27b0: 20 43 47 49 44 45 42 55 47 28 28 22 70 72 65 73   CGIDEBUG(("pres
27c0: 69 67 3d 5b 25 73 5d 5c 6e 22 2c 20 62 6c 6f 62  ig=[%s]\n", blob
27d0: 5f 73 74 72 28 26 63 6f 6d 62 69 6e 65 64 29 29  _str(&combined))
27e0: 29 3b 20 2a 2f 0a 20 20 20 20 73 68 61 31 73 75  ); */.    sha1su
27f0: 6d 5f 62 6c 6f 62 28 26 63 6f 6d 62 69 6e 65 64  m_blob(&combined
2800: 2c 20 26 68 61 73 68 29 3b 0a 20 20 20 20 72 63  , &hash);.    rc
2810: 20 3d 20 62 6c 6f 62 5f 63 6f 6d 70 61 72 65 28   = blob_compare(
2820: 26 68 61 73 68 2c 20 70 53 69 67 29 3b 0a 20 20  &hash, pSig);.  
2830: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 68 61    blob_reset(&ha
2840: 73 68 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 72 65  sh);.    blob_re
2850: 73 65 74 28 26 63 6f 6d 62 69 6e 65 64 29 3b 0a  set(&combined);.
2860: 20 20 20 20 69 66 28 20 72 63 3d 3d 30 20 29 7b      if( rc==0 ){
2870: 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
2880: 72 20 2a 7a 43 61 70 3b 0a 20 20 20 20 20 20 7a  r *zCap;.      z
2890: 43 61 70 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f  Cap = db_column_
28a0: 74 65 78 74 28 26 71 2c 20 31 29 3b 0a 20 20 20  text(&q, 1);.   
28b0: 20 20 20 6c 6f 67 69 6e 5f 73 65 74 5f 63 61 70     login_set_cap
28c0: 61 62 69 6c 69 74 69 65 73 28 7a 43 61 70 29 3b  abilities(zCap);
28d0: 0a 20 20 20 20 20 20 67 2e 75 73 65 72 55 69 64  .      g.userUid
28e0: 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74   = db_column_int
28f0: 28 26 71 2c 20 32 29 3b 0a 20 20 20 20 20 20 67  (&q, 2);.      g
2900: 2e 7a 4c 6f 67 69 6e 20 3d 20 6d 70 72 69 6e 74  .zLogin = mprint
2910: 66 28 22 25 62 22 2c 20 70 4c 6f 67 69 6e 29 3b  f("%b", pLogin);
2920: 0a 20 20 20 20 20 20 67 2e 7a 4e 6f 6e 63 65 20  .      g.zNonce 
2930: 3d 20 6d 70 72 69 6e 74 66 28 22 25 62 22 2c 20  = mprintf("%b", 
2940: 70 4e 6f 6e 63 65 29 3b 0a 20 20 20 20 7d 0a 20  pNonce);.    }. 
2950: 20 7d 0a 20 20 64 62 5f 72 65 73 65 74 28 26 71   }.  db_reset(&q
2960: 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  );.}.../*.** If 
2970: 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 73  this variable is
2980: 20 73 65 74 2c 20 64 69 73 61 62 6c 65 20 6c 6f   set, disable lo
2990: 67 69 6e 20 63 68 65 63 6b 73 2e 20 20 55 73 65  gin checks.  Use
29a0: 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67 0a  d for debugging.
29b0: 2a 2a 20 6f 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 74  ** only..*/.stat
29c0: 69 63 20 69 6e 74 20 64 69 73 61 62 6c 65 4c 6f  ic int disableLo
29d0: 67 69 6e 20 3d 20 30 3b 0a 0a 2f 2a 0a 2a 2a 20  gin = 0;../*.** 
29e0: 57 45 42 50 41 47 45 3a 20 78 66 65 72 0a 2a 2a  WEBPAGE: xfer.**
29f0: 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20  .** This is the 
2a00: 74 72 61 6e 73 66 65 72 20 68 61 6e 64 6c 65 72  transfer handler
2a10: 20 6f 6e 20 74 68 65 20 73 65 72 76 65 72 20 73   on the server s
2a20: 69 64 65 2e 20 20 54 68 65 20 74 72 61 6e 73 66  ide.  The transf
2a30: 65 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 68 61  er.** message ha
2a40: 73 20 62 65 65 6e 20 75 6e 63 6f 6d 70 72 65 73  s been uncompres
2a50: 73 65 64 20 61 6e 64 20 70 6c 61 63 65 64 20 69  sed and placed i
2a60: 6e 20 74 68 65 20 67 2e 63 67 69 49 6e 20 62 6c  n the g.cgiIn bl
2a70: 6f 62 2e 0a 2a 2a 20 50 72 6f 63 65 73 73 20 74  ob..** Process t
2a80: 68 69 73 20 6d 65 73 73 61 67 65 20 61 6e 64 20  his message and 
2a90: 66 6f 72 6d 20 61 6e 20 61 70 70 72 6f 70 72 69  form an appropri
2aa0: 61 74 65 20 72 65 70 6c 79 2e 0a 2a 2f 0a 76 6f  ate reply..*/.vo
2ab0: 69 64 20 70 61 67 65 5f 78 66 65 72 28 76 6f 69  id page_xfer(voi
2ac0: 64 29 7b 0a 20 20 69 6e 74 20 69 73 50 75 6c 6c  d){.  int isPull
2ad0: 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 73 50 75   = 0;.  int isPu
2ae0: 73 68 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 45  sh = 0;.  int nE
2af0: 72 72 20 3d 20 30 3b 0a 20 20 58 66 65 72 20 78  rr = 0;.  Xfer x
2b00: 66 65 72 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 26  fer;..  memset(&
2b10: 78 66 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28  xfer, 0, sizeof(
2b20: 78 66 65 72 29 29 3b 0a 20 20 62 6c 6f 62 61 72  xfer));.  blobar
2b30: 72 61 79 5f 7a 65 72 6f 28 78 66 65 72 2e 61 54  ray_zero(xfer.aT
2b40: 6f 6b 65 6e 2c 20 63 6f 75 6e 74 28 78 66 65 72  oken, count(xfer
2b50: 2e 61 54 6f 6b 65 6e 29 29 3b 0a 20 20 63 67 69  .aToken));.  cgi
2b60: 5f 73 65 74 5f 63 6f 6e 74 65 6e 74 5f 74 79 70  _set_content_typ
2b70: 65 28 67 2e 7a 43 6f 6e 74 65 6e 74 54 79 70 65  e(g.zContentType
2b80: 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26  );.  blob_zero(&
2b90: 78 66 65 72 2e 65 72 72 29 3b 0a 20 20 78 66 65  xfer.err);.  xfe
2ba0: 72 2e 70 49 6e 20 3d 20 26 67 2e 63 67 69 49 6e  r.pIn = &g.cgiIn
2bb0: 3b 0a 20 20 78 66 65 72 2e 70 4f 75 74 20 3d 20  ;.  xfer.pOut = 
2bc0: 63 67 69 5f 6f 75 74 70 75 74 5f 62 6c 6f 62 28  cgi_output_blob(
2bd0: 29 3b 0a 20 20 78 66 65 72 2e 6d 78 53 65 6e 64  );.  xfer.mxSend
2be0: 20 3d 20 64 62 5f 67 65 74 5f 69 6e 74 28 22 6d   = db_get_int("m
2bf0: 61 78 2d 64 6f 77 6e 6c 6f 61 64 22 2c 20 31 30  ax-download", 10
2c00: 30 30 30 30 30 29 3b 0a 0a 20 20 64 62 5f 62 65  00000);..  db_be
2c10: 67 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28  gin_transaction(
2c20: 29 3b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78  );.  db_multi_ex
2c30: 65 63 28 0a 20 20 20 20 20 22 43 52 45 41 54 45  ec(.     "CREATE
2c40: 20 54 45 4d 50 20 54 41 42 4c 45 20 73 65 6e 74   TEMP TABLE sent
2c50: 28 72 69 64 20 49 4e 54 45 47 45 52 20 50 52 49  (rid INTEGER PRI
2c60: 4d 41 52 59 20 4b 45 59 29 3b 22 0a 20 20 29 3b  MARY KEY);".  );
2c70: 0a 20 20 77 68 69 6c 65 28 20 62 6c 6f 62 5f 6c  .  while( blob_l
2c80: 69 6e 65 28 78 66 65 72 2e 70 49 6e 2c 20 26 78  ine(xfer.pIn, &x
2c90: 66 65 72 2e 6c 69 6e 65 29 20 29 7b 0a 20 20 20  fer.line) ){.   
2ca0: 20 78 66 65 72 2e 6e 54 6f 6b 65 6e 20 3d 20 62   xfer.nToken = b
2cb0: 6c 6f 62 5f 74 6f 6b 65 6e 69 7a 65 28 26 78 66  lob_tokenize(&xf
2cc0: 65 72 2e 6c 69 6e 65 2c 20 78 66 65 72 2e 61 54  er.line, xfer.aT
2cd0: 6f 6b 65 6e 2c 20 63 6f 75 6e 74 28 78 66 65 72  oken, count(xfer
2ce0: 2e 61 54 6f 6b 65 6e 29 29 3b 0a 0a 20 20 20 20  .aToken));..    
2cf0: 2f 2a 20 20 20 66 69 6c 65 20 55 55 49 44 20 53  /*   file UUID S
2d00: 49 5a 45 20 5c 6e 20 43 4f 4e 54 45 4e 54 0a 20  IZE \n CONTENT. 
2d10: 20 20 20 2a 2a 20 20 20 66 69 6c 65 20 55 55 49     **   file UUI
2d20: 44 20 44 45 4c 54 41 53 52 43 20 53 49 5a 45 20  D DELTASRC SIZE 
2d30: 5c 6e 20 43 4f 4e 54 45 4e 54 0a 20 20 20 20 2a  \n CONTENT.    *
2d40: 2a 0a 20 20 20 20 2a 2a 20 41 63 63 65 70 74 20  *.    ** Accept 
2d50: 61 20 66 69 6c 65 20 66 72 6f 6d 20 74 68 65 20  a file from the 
2d60: 63 6c 69 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20  client..    */. 
2d70: 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26     if( blob_eq(&
2d80: 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20  xfer.aToken[0], 
2d90: 22 66 69 6c 65 22 29 20 29 7b 0a 20 20 20 20 20  "file") ){.     
2da0: 20 69 66 28 20 21 67 2e 6f 6b 57 72 69 74 65 20   if( !g.okWrite 
2db0: 29 7b 0a 20 20 20 20 20 20 20 20 63 67 69 5f 72  ){.        cgi_r
2dc0: 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a  eset_content();.
2dd0: 20 20 20 20 20 20 20 20 40 20 65 72 72 6f 72 20          @ error 
2de0: 6e 6f 74 5c 73 61 75 74 68 6f 72 69 7a 65 64 5c  not\sauthorized\
2df0: 73 74 6f 5c 73 77 72 69 74 65 0a 20 20 20 20 20  sto\swrite.     
2e00: 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20     nErr++;.     
2e10: 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2e20: 7d 0a 20 20 20 20 20 20 78 66 65 72 5f 61 63 63  }.      xfer_acc
2e30: 65 70 74 5f 66 69 6c 65 28 26 78 66 65 72 29 3b  ept_file(&xfer);
2e40: 0a 20 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f  .      if( blob_
2e50: 73 69 7a 65 28 26 78 66 65 72 2e 65 72 72 29 20  size(&xfer.err) 
2e60: 29 7b 0a 20 20 20 20 20 20 20 20 63 67 69 5f 72  ){.        cgi_r
2e70: 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a  eset_content();.
2e80: 20 20 20 20 20 20 20 20 40 20 65 72 72 6f 72 20          @ error 
2e90: 25 54 28 62 6c 6f 62 5f 73 74 72 28 26 78 66 65  %T(blob_str(&xfe
2ea0: 72 2e 65 72 72 29 29 0a 20 20 20 20 20 20 20 20  r.err)).        
2eb0: 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20  nErr++;.        
2ec0: 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
2ed0: 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20 20 2f 2a     }else..    /*
2ee0: 20 20 20 67 69 6d 6d 65 20 55 55 49 44 0a 20 20     gimme UUID.  
2ef0: 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 6c 69 65    **.    ** Clie
2f00: 6e 74 20 69 73 20 72 65 71 75 65 73 74 69 6e 67  nt is requesting
2f10: 20 61 20 66 69 6c 65 0a 20 20 20 20 2a 2f 0a 20   a file.    */. 
2f20: 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26     if( blob_eq(&
2f30: 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20  xfer.aToken[0], 
2f40: 22 67 69 6d 6d 65 22 29 0a 20 20 20 20 20 26 26  "gimme").     &&
2f50: 20 78 66 65 72 2e 6e 54 6f 6b 65 6e 3d 3d 32 0a   xfer.nToken==2.
2f60: 20 20 20 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f       && blob_is_
2f70: 75 75 69 64 28 26 78 66 65 72 2e 61 54 6f 6b 65  uuid(&xfer.aToke
2f80: 6e 5b 31 5d 29 0a 20 20 20 20 29 7b 0a 20 20 20  n[1]).    ){.   
2f90: 20 20 20 69 66 28 20 67 2e 6f 6b 52 65 61 64 20     if( g.okRead 
2fa0: 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72  ){.        int r
2fb0: 69 64 20 3d 20 72 69 64 5f 66 72 6f 6d 5f 75 75  id = rid_from_uu
2fc0: 69 64 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b  id(&xfer.aToken[
2fd0: 31 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  1], 0);.        
2fe0: 69 66 28 20 72 69 64 20 29 7b 0a 20 20 20 20 20  if( rid ){.     
2ff0: 20 20 20 20 20 73 65 6e 64 5f 66 69 6c 65 28 26       send_file(&
3000: 78 66 65 72 2c 20 72 69 64 2c 20 26 78 66 65 72  xfer, rid, &xfer
3010: 2e 61 54 6f 6b 65 6e 5b 31 5d 2c 20 30 29 3b 0a  .aToken[1], 0);.
3020: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
3030: 7d 0a 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20  }.    }else..   
3040: 20 2f 2a 20 20 20 69 67 6f 74 20 55 55 49 44 0a   /*   igot UUID.
3050: 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 6c      **.    ** Cl
3060: 69 65 6e 74 20 61 6e 6e 6f 75 6e 63 65 73 20 74  ient announces t
3070: 68 61 74 20 69 74 20 68 61 73 20 61 20 70 61 72  hat it has a par
3080: 74 69 63 75 6c 61 72 20 66 69 6c 65 0a 20 20 20  ticular file.   
3090: 20 2a 2f 0a 20 20 20 20 69 66 28 20 78 66 65 72   */.    if( xfer
30a0: 2e 6e 54 6f 6b 65 6e 3d 3d 32 0a 20 20 20 20 20  .nToken==2.     
30b0: 26 26 20 62 6c 6f 62 5f 65 71 28 26 78 66 65 72  && blob_eq(&xfer
30c0: 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 69 67 6f  .aToken[0], "igo
30d0: 74 22 29 0a 20 20 20 20 20 26 26 20 62 6c 6f 62  t").     && blob
30e0: 5f 69 73 5f 75 75 69 64 28 26 78 66 65 72 2e 61  _is_uuid(&xfer.a
30f0: 54 6f 6b 65 6e 5b 31 5d 29 0a 20 20 20 20 29 7b  Token[1]).    ){
3100: 0a 20 20 20 20 20 20 69 66 28 20 67 2e 6f 6b 57  .      if( g.okW
3110: 72 69 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20  rite ){.        
3120: 72 69 64 5f 66 72 6f 6d 5f 75 75 69 64 28 26 78  rid_from_uuid(&x
3130: 66 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d 2c 20 31  fer.aToken[1], 1
3140: 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
3150: 65 6c 73 65 0a 20 20 0a 20 20 20 20 0a 20 20 20  else.  .    .   
3160: 20 2f 2a 20 20 20 6c 65 61 66 20 55 55 49 44 0a   /*   leaf UUID.
3170: 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 6c      **.    ** Cl
3180: 69 65 6e 74 20 61 6e 6e 6f 75 6e 63 65 73 20 74  ient announces t
3190: 68 61 74 20 69 74 20 68 61 73 20 61 20 70 61 72  hat it has a par
31a0: 74 69 63 75 6c 61 72 20 6d 61 6e 69 66 65 73 74  ticular manifest
31b0: 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
31c0: 78 66 65 72 2e 6e 54 6f 6b 65 6e 3d 3d 32 0a 20  xfer.nToken==2. 
31d0: 20 20 20 20 26 26 20 62 6c 6f 62 5f 65 71 28 26      && blob_eq(&
31e0: 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20  xfer.aToken[0], 
31f0: 22 6c 65 61 66 22 29 0a 20 20 20 20 20 26 26 20  "leaf").     && 
3200: 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26 78 66  blob_is_uuid(&xf
3210: 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d 29 0a 20 20  er.aToken[1]).  
3220: 20 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 67    ){.      if( g
3230: 2e 6f 6b 52 65 61 64 20 29 7b 0a 20 20 20 20 20  .okRead ){.     
3240: 20 20 20 69 6e 74 20 72 69 64 20 3d 20 72 69 64     int rid = rid
3250: 5f 66 72 6f 6d 5f 75 75 69 64 28 26 78 66 65 72  _from_uuid(&xfer
3260: 2e 61 54 6f 6b 65 6e 5b 31 5d 2c 20 30 29 3b 0a  .aToken[1], 0);.
3270: 20 20 20 20 20 20 20 20 6c 65 61 66 5f 72 65 73          leaf_res
3280: 70 6f 6e 73 65 28 26 78 66 65 72 2c 20 72 69 64  ponse(&xfer, rid
3290: 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
32a0: 65 6c 73 65 0a 0a 20 20 20 20 2f 2a 20 20 20 20  else..    /*    
32b0: 70 75 6c 6c 20 20 53 45 52 56 45 52 43 4f 44 45  pull  SERVERCODE
32c0: 20 20 50 52 4f 4a 45 43 54 43 4f 44 45 0a 20 20    PROJECTCODE.  
32d0: 20 20 2a 2a 20 20 20 20 70 75 73 68 20 20 53 45    **    push  SE
32e0: 52 56 45 52 43 4f 44 45 20 20 50 52 4f 4a 45 43  RVERCODE  PROJEC
32f0: 54 43 4f 44 45 0a 20 20 20 20 2a 2a 0a 20 20 20  TCODE.    **.   
3300: 20 2a 2a 20 54 68 65 20 63 6c 69 65 6e 74 20 77   ** The client w
3310: 61 6e 74 73 20 65 69 74 68 65 72 20 73 65 6e 64  ants either send
3320: 20 6f 72 20 72 65 63 65 69 76 65 0a 20 20 20 20   or receive.    
3330: 2a 2f 0a 20 20 20 20 69 66 28 20 78 66 65 72 2e  */.    if( xfer.
3340: 6e 54 6f 6b 65 6e 3d 3d 33 0a 20 20 20 20 20 26  nToken==3.     &
3350: 26 20 28 62 6c 6f 62 5f 65 71 28 26 78 66 65 72  & (blob_eq(&xfer
3360: 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 70 75 6c  .aToken[0], "pul
3370: 6c 22 29 20 7c 7c 20 62 6c 6f 62 5f 65 71 28 26  l") || blob_eq(&
3380: 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20  xfer.aToken[0], 
3390: 22 70 75 73 68 22 29 29 0a 20 20 20 20 20 26 26  "push")).     &&
33a0: 20 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26 78   blob_is_uuid(&x
33b0: 66 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d 29 0a 20  fer.aToken[1]). 
33c0: 20 20 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75      && blob_is_u
33d0: 75 69 64 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e  uid(&xfer.aToken
33e0: 5b 32 5d 29 0a 20 20 20 20 29 7b 0a 20 20 20 20  [2]).    ){.    
33f0: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
3400: 43 6f 64 65 3b 0a 20 20 20 20 20 20 63 6f 6e 73  Code;.      cons
3410: 74 20 63 68 61 72 20 2a 7a 50 43 6f 64 65 3b 0a  t char *zPCode;.
3420: 0a 20 20 20 20 20 20 7a 53 43 6f 64 65 20 3d 20  .      zSCode = 
3430: 64 62 5f 67 65 74 28 22 73 65 72 76 65 72 2d 63  db_get("server-c
3440: 6f 64 65 22 2c 20 30 29 3b 0a 20 20 20 20 20 20  ode", 0);.      
3450: 69 66 28 20 7a 53 43 6f 64 65 3d 3d 30 20 29 7b  if( zSCode==0 ){
3460: 0a 20 20 20 20 20 20 20 20 66 6f 73 73 69 6c 5f  .        fossil_
3470: 70 61 6e 69 63 28 22 6d 69 73 73 69 6e 67 20 73  panic("missing s
3480: 65 72 76 65 72 20 63 6f 64 65 22 29 3b 0a 20 20  erver code");.  
3490: 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
34a0: 62 6c 6f 62 5f 65 71 5f 73 74 72 28 26 78 66 65  blob_eq_str(&xfe
34b0: 72 2e 61 54 6f 6b 65 6e 5b 31 5d 2c 20 7a 53 43  r.aToken[1], zSC
34c0: 6f 64 65 2c 20 2d 31 29 20 29 7b 0a 20 20 20 20  ode, -1) ){.    
34d0: 20 20 20 20 63 67 69 5f 72 65 73 65 74 5f 63 6f      cgi_reset_co
34e0: 6e 74 65 6e 74 28 29 3b 0a 20 20 20 20 20 20 20  ntent();.       
34f0: 20 40 20 65 72 72 6f 72 20 73 65 72 76 65 72 5c   @ error server\
3500: 73 6c 6f 6f 70 0a 20 20 20 20 20 20 20 20 6e 45  sloop.        nE
3510: 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 62 72  rr++;.        br
3520: 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
3530: 20 20 20 7a 50 43 6f 64 65 20 3d 20 64 62 5f 67     zPCode = db_g
3540: 65 74 28 22 70 72 6f 6a 65 63 74 2d 63 6f 64 65  et("project-code
3550: 22 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28  ", 0);.      if(
3560: 20 7a 50 43 6f 64 65 3d 3d 30 20 29 7b 0a 20 20   zPCode==0 ){.  
3570: 20 20 20 20 20 20 66 6f 73 73 69 6c 5f 70 61 6e        fossil_pan
3580: 69 63 28 22 6d 69 73 73 69 6e 67 20 70 72 6f 6a  ic("missing proj
3590: 65 63 74 20 63 6f 64 65 22 29 3b 0a 20 20 20 20  ect code");.    
35a0: 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 62    }.      if( !b
35b0: 6c 6f 62 5f 65 71 5f 73 74 72 28 26 78 66 65 72  lob_eq_str(&xfer
35c0: 2e 61 54 6f 6b 65 6e 5b 32 5d 2c 20 7a 50 43 6f  .aToken[2], zPCo
35d0: 64 65 2c 20 2d 31 29 20 29 7b 0a 20 20 20 20 20  de, -1) ){.     
35e0: 20 20 20 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e     cgi_reset_con
35f0: 74 65 6e 74 28 29 3b 0a 20 20 20 20 20 20 20 20  tent();.        
3600: 40 20 65 72 72 6f 72 20 77 72 6f 6e 67 5c 73 70  @ error wrong\sp
3610: 72 6f 6a 65 63 74 0a 20 20 20 20 20 20 20 20 6e  roject.        n
3620: 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 62  Err++;.        b
3630: 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
3640: 20 20 20 20 6c 6f 67 69 6e 5f 63 68 65 63 6b 5f      login_check_
3650: 63 72 65 64 65 6e 74 69 61 6c 73 28 29 3b 0a 20  credentials();. 
3660: 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71       if( blob_eq
3670: 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d  (&xfer.aToken[0]
3680: 2c 20 22 70 75 6c 6c 22 29 20 29 7b 0a 20 20 20  , "pull") ){.   
3690: 20 20 20 20 20 69 66 28 20 21 67 2e 6f 6b 52 65       if( !g.okRe
36a0: 61 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ad ){.          
36b0: 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e 74 65 6e  cgi_reset_conten
36c0: 74 28 29 3b 0a 20 20 20 20 20 20 20 20 20 20 40  t();.          @
36d0: 20 65 72 72 6f 72 20 6e 6f 74 5c 73 61 75 74 68   error not\sauth
36e0: 6f 72 69 7a 65 64 5c 73 74 6f 5c 73 72 65 61 64  orized\sto\sread
36f0: 0a 20 20 20 20 20 20 20 20 20 20 6e 45 72 72 2b  .          nErr+
3700: 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  +;.          bre
3710: 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
3720: 20 20 20 20 20 20 69 73 50 75 6c 6c 20 3d 20 31        isPull = 1
3730: 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
3740: 20 20 20 20 20 20 20 69 66 28 20 21 67 2e 6f 6b         if( !g.ok
3750: 57 72 69 74 65 20 29 7b 0a 20 20 20 20 20 20 20  Write ){.       
3760: 20 20 20 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e     cgi_reset_con
3770: 74 65 6e 74 28 29 3b 0a 20 20 20 20 20 20 20 20  tent();.        
3780: 20 20 40 20 65 72 72 6f 72 20 6e 6f 74 5c 73 61    @ error not\sa
3790: 75 74 68 6f 72 69 7a 65 64 5c 73 74 6f 5c 73 77  uthorized\sto\sw
37a0: 72 69 74 65 0a 20 20 20 20 20 20 20 20 20 20 6e  rite.          n
37b0: 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20  Err++;.         
37c0: 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
37d0: 7d 0a 20 20 20 20 20 20 20 20 73 65 6e 64 5f 6c  }.        send_l
37e0: 65 61 76 65 73 28 26 78 66 65 72 29 3b 0a 20 20  eaves(&xfer);.  
37f0: 20 20 20 20 20 20 69 73 50 75 73 68 20 3d 20 31        isPush = 1
3800: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
3810: 6c 73 65 0a 0a 20 20 20 20 2f 2a 20 20 20 20 63  lse..    /*    c
3820: 6c 6f 6e 65 0a 20 20 20 20 2a 2a 0a 20 20 20 20  lone.    **.    
3830: 2a 2a 20 54 68 65 20 63 6c 69 65 6e 74 20 6b 6e  ** The client kn
3840: 6f 77 73 20 6e 6f 74 68 69 6e 67 2e 20 20 54 65  ows nothing.  Te
3850: 6c 6c 20 61 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20  ll all..    */. 
3860: 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26     if( blob_eq(&
3870: 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20  xfer.aToken[0], 
3880: 22 63 6c 6f 6e 65 22 29 20 29 7b 0a 20 20 20 20  "clone") ){.    
3890: 20 20 69 6e 74 20 72 6f 6f 74 69 64 3b 0a 20 20    int rootid;.  
38a0: 20 20 20 20 6c 6f 67 69 6e 5f 63 68 65 63 6b 5f      login_check_
38b0: 63 72 65 64 65 6e 74 69 61 6c 73 28 29 3b 0a 20  credentials();. 
38c0: 20 20 20 20 20 69 66 28 20 21 67 2e 6f 6b 52 65       if( !g.okRe
38d0: 61 64 20 7c 7c 20 21 67 2e 6f 6b 48 69 73 74 6f  ad || !g.okHisto
38e0: 72 79 20 29 7b 0a 20 20 20 20 20 20 20 20 63 67  ry ){.        cg
38f0: 69 5f 72 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28  i_reset_content(
3900: 29 3b 0a 20 20 20 20 20 20 20 20 40 20 65 72 72  );.        @ err
3910: 6f 72 20 6e 6f 74 5c 73 61 75 74 68 6f 72 69 7a  or not\sauthoriz
3920: 65 64 5c 73 74 6f 5c 73 63 6c 6f 6e 65 0a 20 20  ed\sto\sclone.  
3930: 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20        nErr++;.  
3940: 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3950: 20 20 20 7d 0a 20 20 20 20 20 20 69 73 50 75 6c     }.      isPul
3960: 6c 20 3d 20 31 3b 0a 20 20 20 20 20 20 40 20 70  l = 1;.      @ p
3970: 75 73 68 20 25 73 28 64 62 5f 67 65 74 28 22 73  ush %s(db_get("s
3980: 65 72 76 65 72 2d 63 6f 64 65 22 2c 20 22 78 22  erver-code", "x"
3990: 29 29 20 25 73 28 64 62 5f 67 65 74 28 22 70 72  )) %s(db_get("pr
39a0: 6f 6a 65 63 74 2d 63 6f 64 65 22 2c 20 22 78 22  oject-code", "x"
39b0: 29 29 0a 20 20 20 20 20 20 72 6f 6f 74 69 64 20  )).      rootid 
39c0: 3d 20 64 62 5f 69 6e 74 28 30 2c 20 0a 20 20 20  = db_int(0, .   
39d0: 20 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 70         "SELECT p
39e0: 69 64 20 46 52 4f 4d 20 70 6c 69 6e 6b 20 41 53  id FROM plink AS
39f0: 20 61 22 0a 20 20 20 20 20 20 20 20 20 20 22 20   a".          " 
3a00: 57 48 45 52 45 20 4e 4f 54 20 45 58 49 53 54 53  WHERE NOT EXISTS
3a10: 28 53 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 70  (SELECT 1 FROM p
3a20: 6c 69 6e 6b 20 57 48 45 52 45 20 63 69 64 3d 61  link WHERE cid=a
3a30: 2e 70 69 64 29 22 0a 20 20 20 20 20 20 29 3b 0a  .pid)".      );.
3a40: 20 20 20 20 20 20 69 66 28 20 72 6f 6f 74 69 64        if( rootid
3a50: 20 29 7b 0a 20 20 20 20 20 20 20 20 73 65 6e 64   ){.        send
3a60: 5f 66 69 6c 65 28 26 78 66 65 72 2c 20 72 6f 6f  _file(&xfer, roo
3a70: 74 69 64 2c 20 30 2c 20 2d 31 29 3b 0a 20 20 20  tid, 0, -1);.   
3a80: 20 20 20 20 20 6c 65 61 66 5f 72 65 73 70 6f 6e       leaf_respon
3a90: 73 65 28 26 78 66 65 72 2c 20 72 6f 6f 74 69 64  se(&xfer, rootid
3aa0: 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
3ab0: 65 6c 73 65 0a 0a 20 20 20 20 2f 2a 20 20 20 20  else..    /*    
3ac0: 6c 6f 67 69 6e 20 20 55 53 45 52 20 20 4e 4f 4e  login  USER  NON
3ad0: 43 45 20 20 53 49 47 4e 41 54 55 52 45 0a 20 20  CE  SIGNATURE.  
3ae0: 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 68 65 63    **.    ** Chec
3af0: 6b 20 66 6f 72 20 61 20 76 61 6c 69 64 20 6c 6f  k for a valid lo
3b00: 67 69 6e 2e 20 20 54 68 69 73 20 68 61 73 20 74  gin.  This has t
3b10: 6f 20 68 61 70 70 65 6e 20 62 65 66 6f 72 65 20  o happen before 
3b20: 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 2e 0a 20  anything else.. 
3b30: 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 62 6c     */.    if( bl
3b40: 6f 62 5f 65 71 28 26 78 66 65 72 2e 61 54 6f 6b  ob_eq(&xfer.aTok
3b50: 65 6e 5b 30 5d 2c 20 22 6c 6f 67 69 6e 22 29 0a  en[0], "login").
3b60: 20 20 20 20 20 26 26 20 78 66 65 72 2e 6e 54 6f       && xfer.nTo
3b70: 6b 65 6e 3d 3d 34 0a 20 20 20 20 29 7b 0a 20 20  ken==4.    ){.  
3b80: 20 20 20 20 69 66 28 20 64 69 73 61 62 6c 65 4c      if( disableL
3b90: 6f 67 69 6e 20 29 7b 0a 20 20 20 20 20 20 20 20  ogin ){.        
3ba0: 67 2e 6f 6b 52 65 61 64 20 3d 20 67 2e 6f 6b 57  g.okRead = g.okW
3bb0: 72 69 74 65 20 3d 20 31 3b 0a 20 20 20 20 20 20  rite = 1;.      
3bc0: 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63  }else{.        c
3bd0: 68 65 63 6b 5f 6c 6f 67 69 6e 28 26 78 66 65 72  heck_login(&xfer
3be0: 2e 61 54 6f 6b 65 6e 5b 31 5d 2c 20 26 78 66 65  .aToken[1], &xfe
3bf0: 72 2e 61 54 6f 6b 65 6e 5b 32 5d 2c 20 26 78 66  r.aToken[2], &xf
3c00: 65 72 2e 61 54 6f 6b 65 6e 5b 33 5d 29 3b 0a 20  er.aToken[3]);. 
3c10: 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
3c20: 0a 0a 20 20 20 20 2f 2a 20 55 6e 6b 6e 6f 77 6e  ..    /* Unknown
3c30: 20 6d 65 73 73 61 67 65 0a 20 20 20 20 2a 2f 0a   message.    */.
3c40: 20 20 20 20 7b 0a 20 20 20 20 20 20 63 67 69 5f      {.      cgi_
3c50: 72 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b  reset_content();
3c60: 0a 20 20 20 20 20 20 40 20 65 72 72 6f 72 20 62  .      @ error b
3c70: 61 64 5c 73 63 6f 6d 6d 61 6e 64 3a 5c 73 25 46  ad\scommand:\s%F
3c80: 28 62 6c 6f 62 5f 73 74 72 28 26 78 66 65 72 2e  (blob_str(&xfer.
3c90: 6c 69 6e 65 29 29 0a 20 20 20 20 7d 0a 20 20 20  line)).    }.   
3ca0: 20 62 6c 6f 62 61 72 72 61 79 5f 72 65 73 65 74   blobarray_reset
3cb0: 28 78 66 65 72 2e 61 54 6f 6b 65 6e 2c 20 78 66  (xfer.aToken, xf
3cc0: 65 72 2e 6e 54 6f 6b 65 6e 29 3b 0a 20 20 7d 0a  er.nToken);.  }.
3cd0: 20 20 69 66 28 20 69 73 50 75 73 68 20 29 7b 0a    if( isPush ){.
3ce0: 20 20 20 20 72 65 71 75 65 73 74 5f 70 68 61 6e      request_phan
3cf0: 74 6f 6d 73 28 26 78 66 65 72 29 3b 0a 20 20 7d  toms(&xfer);.  }
3d00: 0a 20 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73 61  .  db_end_transa
3d10: 63 74 69 6f 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a 0a  ction(0);.}../*.
3d20: 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 74 65 73 74  ** COMMAND: test
3d30: 2d 78 66 65 72 0a 2a 2a 0a 2a 2a 20 54 68 69 73  -xfer.**.** This
3d40: 20 63 6f 6d 6d 61 6e 64 20 69 73 20 75 73 65 64   command is used
3d50: 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67 20 74   for debugging t
3d60: 68 65 20 73 65 72 76 65 72 2e 20 20 54 68 65 72  he server.  Ther
3d70: 65 20 69 73 20 61 20 73 69 6e 67 6c 65 0a 2a 2a  e is a single.**
3d80: 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20   argument which 
3d90: 69 73 20 74 68 65 20 75 6e 63 6f 6d 70 72 65 73  is the uncompres
3da0: 73 65 64 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61  sed content of a
3db0: 6e 20 22 78 66 65 72 22 20 6d 65 73 73 61 67 65  n "xfer" message
3dc0: 0a 2a 2a 20 66 72 6f 6d 20 63 6c 69 65 6e 74 20  .** from client 
3dd0: 74 6f 20 73 65 72 76 65 72 2e 20 20 54 68 69 73  to server.  This
3de0: 20 63 6f 6d 6d 61 6e 64 20 69 6e 74 65 72 70 72   command interpr
3df0: 65 74 73 20 74 68 61 74 20 6d 65 73 73 61 67 65  ets that message
3e00: 20 61 73 0a 2a 2a 20 69 66 20 68 61 64 20 62 65   as.** if had be
3e10: 65 6e 20 72 65 63 65 69 76 65 64 20 62 79 20 74  en received by t
3e20: 68 65 20 73 65 72 76 65 72 2e 0a 2a 2a 0a 2a 2a  he server..**.**
3e30: 20 4f 6e 20 74 68 65 20 63 6c 69 65 6e 74 20 73   On the client s
3e40: 69 64 65 2c 20 72 75 6e 3a 0a 2a 2a 0a 2a 2a 20  ide, run:.**.** 
3e50: 20 20 20 20 20 66 6f 73 73 69 6c 20 70 75 73 68       fossil push
3e60: 20 68 74 74 70 3a 2f 2f 62 6f 67 75 73 2f 20 2d   http://bogus/ -
3e70: 2d 68 74 74 70 74 72 61 63 65 0a 2a 2a 0a 2a 2a  -httptrace.**.**
3e80: 20 4f 72 20 61 20 73 69 6d 69 6c 61 72 20 63 6f   Or a similar co
3e90: 6d 6d 61 6e 64 20 74 6f 20 70 72 6f 76 69 64 65  mmand to provide
3ea0: 20 74 68 65 20 6f 75 74 70 75 74 2e 20 20 54 68   the output.  Th
3eb0: 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65  e content of the
3ec0: 0a 2a 2a 20 6d 65 73 73 61 67 65 20 77 69 6c 6c  .** message will
3ed0: 20 61 70 70 65 61 72 20 6f 6e 20 73 74 61 6e 64   appear on stand
3ee0: 61 72 64 20 6f 75 74 70 75 74 2e 20 20 43 61 70  ard output.  Cap
3ef0: 74 75 72 65 20 74 68 69 73 20 6d 65 73 73 61 67  ture this messag
3f00: 65 0a 2a 2a 20 69 6e 74 6f 20 61 20 66 69 6c 65  e.** into a file
3f10: 20 6e 61 6d 65 64 20 28 66 6f 72 20 65 78 61 6d   named (for exam
3f20: 70 6c 65 29 20 6f 75 74 2e 74 78 74 2e 20 20 54  ple) out.txt.  T
3f30: 68 65 6e 20 72 75 6e 20 74 68 65 0a 2a 2a 20 73  hen run the.** s
3f40: 65 72 76 65 72 20 69 6e 20 67 64 62 3a 0a 2a 2a  erver in gdb:.**
3f50: 0a 2a 2a 20 20 20 20 20 67 64 62 20 66 6f 73 73  .**     gdb foss
3f60: 69 6c 0a 2a 2a 20 20 20 20 20 72 20 74 65 73 74  il.**     r test
3f70: 2d 78 66 65 72 20 6f 75 74 2e 74 78 74 0a 2a 2f  -xfer out.txt.*/
3f80: 0a 76 6f 69 64 20 63 6d 64 5f 74 65 73 74 5f 78  .void cmd_test_x
3f90: 66 65 72 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74  fer(void){.  int
3fa0: 20 6e 6f 74 55 73 65 64 3b 0a 20 20 69 66 28 20   notUsed;.  if( 
3fb0: 67 2e 61 72 67 63 21 3d 32 20 26 26 20 67 2e 61  g.argc!=2 && g.a
3fc0: 72 67 63 21 3d 33 20 29 7b 0a 20 20 20 20 75 73  rgc!=3 ){.    us
3fd0: 61 67 65 28 22 3f 4d 45 53 53 41 47 45 46 49 4c  age("?MESSAGEFIL
3fe0: 45 3f 22 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 6d  E?");.  }.  db_m
3ff0: 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74 72  ust_be_within_tr
4000: 65 65 28 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72  ee();.  blob_zer
4010: 6f 28 26 67 2e 63 67 69 49 6e 29 3b 0a 20 20 62  o(&g.cgiIn);.  b
4020: 6c 6f 62 5f 72 65 61 64 5f 66 72 6f 6d 5f 66 69  lob_read_from_fi
4030: 6c 65 28 26 67 2e 63 67 69 49 6e 2c 20 67 2e 61  le(&g.cgiIn, g.a
4040: 72 67 63 3d 3d 32 20 3f 20 22 2d 22 20 3a 20 67  rgc==2 ? "-" : g
4050: 2e 61 72 67 76 5b 32 5d 29 3b 0a 20 20 64 69 73  .argv[2]);.  dis
4060: 61 62 6c 65 4c 6f 67 69 6e 20 3d 20 31 3b 0a 20  ableLogin = 1;. 
4070: 20 70 61 67 65 5f 78 66 65 72 28 29 3b 0a 20 20   page_xfer();.  
4080: 70 72 69 6e 74 66 28 22 25 73 5c 6e 22 2c 20 63  printf("%s\n", c
4090: 67 69 5f 65 78 74 72 61 63 74 5f 63 6f 6e 74 65  gi_extract_conte
40a0: 6e 74 28 26 6e 6f 74 55 73 65 64 29 29 3b 0a 7d  nt(&notUsed));.}
40b0: 0a 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 6f  .../*.** Sync to
40c0: 20 74 68 65 20 68 6f 73 74 20 69 64 65 6e 74 69   the host identi
40d0: 66 69 65 64 20 69 6e 20 67 2e 75 72 6c 4e 61 6d  fied in g.urlNam
40e0: 65 20 61 6e 64 20 67 2e 75 72 6c 50 61 74 68 2e  e and g.urlPath.
40f0: 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e    This.** routin
4100: 65 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74  e is called by t
4110: 68 65 20 63 6c 69 65 6e 74 2e 0a 2a 2a 0a 2a 2a  he client..**.**
4120: 20 52 65 63 6f 72 64 73 20 61 72 65 20 70 75 73   Records are pus
4130: 68 65 64 20 74 6f 20 74 68 65 20 73 65 72 76 65  hed to the serve
4140: 72 20 69 66 20 70 75 73 68 46 6c 61 67 20 69 73  r if pushFlag is
4150: 20 74 72 75 65 2e 20 20 52 65 63 6f 72 64 73 0a   true.  Records.
4160: 2a 2a 20 61 72 65 20 70 75 6c 6c 65 64 20 69 66  ** are pulled if
4170: 20 70 75 6c 6c 46 6c 61 67 20 69 73 20 74 72 75   pullFlag is tru
4180: 65 2e 20 20 41 20 66 75 6c 6c 20 73 79 6e 63 20  e.  A full sync 
4190: 6f 63 63 75 72 73 20 69 66 20 62 6f 74 68 20 61  occurs if both a
41a0: 72 65 0a 2a 2a 20 74 72 75 65 2e 0a 2a 2f 0a 76  re.** true..*/.v
41b0: 6f 69 64 20 63 6c 69 65 6e 74 5f 73 79 6e 63 28  oid client_sync(
41c0: 69 6e 74 20 70 75 73 68 46 6c 61 67 2c 20 69 6e  int pushFlag, in
41d0: 74 20 70 75 6c 6c 46 6c 61 67 2c 20 69 6e 74 20  t pullFlag, int 
41e0: 63 6c 6f 6e 65 46 6c 61 67 29 7b 0a 20 20 69 6e  cloneFlag){.  in
41f0: 74 20 67 6f 20 3d 20 31 3b 20 20 20 20 20 20 20  t go = 1;       
4200: 20 2f 2a 20 4c 6f 6f 70 20 75 6e 74 69 6c 20 7a   /* Loop until z
4210: 65 72 6f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ero */.  const c
4220: 68 61 72 20 2a 7a 53 43 6f 64 65 20 3d 20 64 62  har *zSCode = db
4230: 5f 67 65 74 28 22 73 65 72 76 65 72 2d 63 6f 64  _get("server-cod
4240: 65 22 2c 20 22 78 22 29 3b 0a 20 20 63 6f 6e 73  e", "x");.  cons
4250: 74 20 63 68 61 72 20 2a 7a 50 43 6f 64 65 20 3d  t char *zPCode =
4260: 20 64 62 5f 67 65 74 28 22 70 72 6f 6a 65 63 74   db_get("project
4270: 2d 63 6f 64 65 22 2c 20 30 29 3b 0a 20 20 69 6e  -code", 0);.  in
4280: 74 20 6e 4d 73 67 20 3d 20 30 3b 20 20 20 20 20  t nMsg = 0;     
4290: 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
42a0: 66 20 6d 65 73 73 61 67 65 73 20 73 65 6e 74 20  f messages sent 
42b0: 6f 72 20 72 65 63 65 69 76 65 64 20 2a 2f 0a 20  or received */. 
42c0: 20 69 6e 74 20 6e 43 79 63 6c 65 20 3d 20 30 3b   int nCycle = 0;
42d0: 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
42e0: 72 20 6f 66 20 72 6f 75 6e 64 20 74 72 69 70 73  r of round trips
42f0: 20 74 6f 20 74 68 65 20 73 65 72 76 65 72 20 2a   to the server *
4300: 2f 0a 20 20 69 6e 74 20 6e 46 69 6c 65 53 65 6e  /.  int nFileSen
4310: 64 20 3d 20 30 3b 0a 20 20 42 6c 6f 62 20 73 65  d = 0;.  Blob se
4320: 6e 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 65  nd;        /* Te
4330: 78 74 20 77 65 20 61 72 65 20 73 65 6e 64 69 6e  xt we are sendin
4340: 67 20 74 6f 20 74 68 65 20 73 65 72 76 65 72 20  g to the server 
4350: 2a 2f 0a 20 20 42 6c 6f 62 20 72 65 63 76 3b 20  */.  Blob recv; 
4360: 20 20 20 20 20 20 20 2f 2a 20 52 65 70 6c 79 20         /* Reply 
4370: 77 65 20 67 6f 74 20 62 61 63 6b 20 66 72 6f 6d  we got back from
4380: 20 74 68 65 20 73 65 72 76 65 72 20 2a 2f 0a 20   the server */. 
4390: 20 58 66 65 72 20 78 66 65 72 3b 20 20 20 20 20   Xfer xfer;     
43a0: 20 20 20 2f 2a 20 54 72 61 6e 73 66 65 72 20 64     /* Transfer d
43b0: 61 74 61 20 2a 2f 0a 0a 20 20 6d 65 6d 73 65 74  ata */..  memset
43c0: 28 26 78 66 65 72 2c 20 30 2c 20 73 69 7a 65 6f  (&xfer, 0, sizeo
43d0: 66 28 78 66 65 72 29 29 3b 0a 20 20 78 66 65 72  f(xfer));.  xfer
43e0: 2e 70 49 6e 20 3d 20 26 72 65 63 76 3b 0a 20 20  .pIn = &recv;.  
43f0: 78 66 65 72 2e 70 4f 75 74 20 3d 20 26 73 65 6e  xfer.pOut = &sen
4400: 64 3b 0a 20 20 78 66 65 72 2e 6d 78 53 65 6e 64  d;.  xfer.mxSend
4410: 20 3d 20 64 62 5f 67 65 74 5f 69 6e 74 28 22 6d   = db_get_int("m
4420: 61 78 2d 75 70 6c 6f 61 64 22 2c 20 32 35 30 30  ax-upload", 2500
4430: 30 30 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  00);..  assert( 
4440: 70 75 73 68 46 6c 61 67 20 7c 7c 20 70 75 6c 6c  pushFlag || pull
4450: 46 6c 61 67 20 7c 7c 20 63 6c 6f 6e 65 46 6c 61  Flag || cloneFla
4460: 67 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  g );.  assert( !
4470: 67 2e 75 72 6c 49 73 46 69 6c 65 20 29 3b 20 20  g.urlIsFile );  
4480: 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20          /* This 
4490: 6f 6e 6c 79 20 77 6f 72 6b 73 20 66 6f 72 20 6e  only works for n
44a0: 65 74 77 6f 72 6b 69 6e 67 20 2a 2f 0a 0a 20 20  etworking */..  
44b0: 64 62 5f 62 65 67 69 6e 5f 74 72 61 6e 73 61 63  db_begin_transac
44c0: 74 69 6f 6e 28 29 3b 0a 20 20 64 62 5f 6d 75 6c  tion();.  db_mul
44d0: 74 69 5f 65 78 65 63 28 0a 20 20 20 20 22 43 52  ti_exec(.    "CR
44e0: 45 41 54 45 20 54 45 4d 50 20 54 41 42 4c 45 20  EATE TEMP TABLE 
44f0: 73 65 6e 74 28 72 69 64 20 49 4e 54 45 47 45 52  sent(rid INTEGER
4500: 20 50 52 49 4d 41 52 59 20 4b 45 59 29 3b 22 0a   PRIMARY KEY);".
4510: 20 20 29 3b 0a 20 20 62 6c 6f 62 61 72 72 61 79    );.  blobarray
4520: 5f 7a 65 72 6f 28 78 66 65 72 2e 61 54 6f 6b 65  _zero(xfer.aToke
4530: 6e 2c 20 63 6f 75 6e 74 28 78 66 65 72 2e 61 54  n, count(xfer.aT
4540: 6f 6b 65 6e 29 29 3b 0a 20 20 62 6c 6f 62 5f 7a  oken));.  blob_z
4550: 65 72 6f 28 26 73 65 6e 64 29 3b 0a 20 20 62 6c  ero(&send);.  bl
4560: 6f 62 5f 7a 65 72 6f 28 26 72 65 63 76 29 3b 0a  ob_zero(&recv);.
4570: 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 78 66 65    blob_zero(&xfe
4580: 72 2e 65 72 72 29 3b 0a 20 20 62 6c 6f 62 5f 7a  r.err);.  blob_z
4590: 65 72 6f 28 26 78 66 65 72 2e 6c 69 6e 65 29 3b  ero(&xfer.line);
45a0: 0a 0a 0a 20 20 77 68 69 6c 65 28 20 67 6f 20 29  ...  while( go )
45b0: 7b 0a 0a 20 20 20 20 2f 2a 20 47 65 6e 65 72 61  {..    /* Genera
45c0: 74 65 20 61 20 72 65 71 75 65 73 74 20 74 6f 20  te a request to 
45d0: 62 65 20 73 65 6e 74 20 74 6f 20 74 68 65 20 73  be sent to the s
45e0: 65 72 76 65 72 2e 0a 20 20 20 20 2a 2a 20 41 6c  erver..    ** Al
45f0: 77 61 79 73 20 62 65 67 69 6e 20 77 69 74 68 20  ways begin with 
4600: 61 20 63 6c 6f 6e 65 2c 20 70 75 6c 6c 2c 20 6f  a clone, pull, o
4610: 72 20 70 75 73 68 20 6d 65 73 73 61 67 65 0a 20  r push message. 
4620: 20 20 20 2a 2f 0a 20 20 20 20 0a 20 20 20 20 69     */.    .    i
4630: 66 28 20 63 6c 6f 6e 65 46 6c 61 67 20 29 7b 0a  f( cloneFlag ){.
4640: 20 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e        blob_appen
4650: 64 66 28 26 73 65 6e 64 2c 20 22 63 6c 6f 6e 65  df(&send, "clone
4660: 5c 6e 22 29 3b 0a 20 20 20 20 20 20 70 75 73 68  \n");.      push
4670: 46 6c 61 67 20 3d 20 30 3b 0a 20 20 20 20 20 20  Flag = 0;.      
4680: 70 75 6c 6c 46 6c 61 67 20 3d 20 30 3b 0a 20 20  pullFlag = 0;.  
4690: 20 20 20 20 6e 4d 73 67 2b 2b 3b 0a 20 20 20 20      nMsg++;.    
46a0: 7d 65 6c 73 65 20 69 66 28 20 70 75 6c 6c 46 6c  }else if( pullFl
46b0: 61 67 20 29 7b 0a 20 20 20 20 20 20 62 6c 6f 62  ag ){.      blob
46c0: 5f 61 70 70 65 6e 64 66 28 26 73 65 6e 64 2c 20  _appendf(&send, 
46d0: 22 70 75 6c 6c 20 25 73 20 25 73 5c 6e 22 2c 20  "pull %s %s\n", 
46e0: 7a 53 43 6f 64 65 2c 20 7a 50 43 6f 64 65 29 3b  zSCode, zPCode);
46f0: 0a 20 20 20 20 20 20 6e 4d 73 67 2b 2b 3b 0a 20  .      nMsg++;. 
4700: 20 20 20 20 20 72 65 71 75 65 73 74 5f 70 68 61       request_pha
4710: 6e 74 6f 6d 73 28 26 78 66 65 72 29 3b 0a 20 20  ntoms(&xfer);.  
4720: 20 20 20 20 73 65 6e 64 5f 6c 65 61 76 65 73 28      send_leaves(
4730: 26 78 66 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20  &xfer);.    }.  
4740: 20 20 69 66 28 20 70 75 73 68 46 6c 61 67 20 29    if( pushFlag )
4750: 7b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 61 70 70  {.      blob_app
4760: 65 6e 64 66 28 26 73 65 6e 64 2c 20 22 70 75 73  endf(&send, "pus
4770: 68 20 25 73 20 25 73 5c 6e 22 2c 20 7a 53 43 6f  h %s %s\n", zSCo
4780: 64 65 2c 20 7a 50 43 6f 64 65 29 3b 0a 20 20 20  de, zPCode);.   
4790: 20 20 20 6e 4d 73 67 2b 2b 3b 0a 20 20 20 20 7d     nMsg++;.    }
47a0: 0a 0a 20 20 20 20 2f 2a 20 45 78 63 68 61 6e 67  ..    /* Exchang
47b0: 65 20 6d 65 73 73 61 67 65 73 20 77 69 74 68 20  e messages with 
47c0: 74 68 65 20 73 65 72 76 65 72 20 2a 2f 0a 20 20  the server */.  
47d0: 20 20 6e 46 69 6c 65 53 65 6e 64 20 3d 20 78 66    nFileSend = xf
47e0: 65 72 2e 6e 46 69 6c 65 53 65 6e 74 20 2b 20 78  er.nFileSent + x
47f0: 66 65 72 2e 6e 44 65 6c 74 61 53 65 6e 74 3b 0a  fer.nDeltaSent;.
4800: 20 20 20 20 70 72 69 6e 74 66 28 22 53 65 6e 64      printf("Send
4810: 3a 20 20 20 20 20 20 25 31 30 64 20 62 79 74 65  :      %10d byte
4820: 73 2c 20 25 33 64 20 6d 65 73 73 61 67 65 73 2c  s, %3d messages,
4830: 20 25 33 64 20 66 69 6c 65 73 20 28 25 64 2b 25   %3d files (%d+%
4840: 64 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20  d)\n",.         
4850: 20 20 20 62 6c 6f 62 5f 73 69 7a 65 28 26 73 65     blob_size(&se
4860: 6e 64 29 2c 20 6e 4d 73 67 2b 78 66 65 72 2e 6e  nd), nMsg+xfer.n
4870: 47 69 6d 6d 65 53 65 6e 74 2b 78 66 65 72 2e 6e  GimmeSent+xfer.n
4880: 49 47 6f 74 53 65 6e 74 2c 0a 20 20 20 20 20 20  IGotSent,.      
4890: 20 20 20 20 20 20 6e 46 69 6c 65 53 65 6e 64 2c        nFileSend,
48a0: 20 78 66 65 72 2e 6e 46 69 6c 65 53 65 6e 74 2c   xfer.nFileSent,
48b0: 20 78 66 65 72 2e 6e 44 65 6c 74 61 53 65 6e 74   xfer.nDeltaSent
48c0: 29 3b 0a 20 20 20 20 6e 4d 73 67 20 3d 20 30 3b  );.    nMsg = 0;
48d0: 0a 20 20 20 20 78 66 65 72 2e 6e 46 69 6c 65 53  .    xfer.nFileS
48e0: 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20 78 66 65  ent = 0;.    xfe
48f0: 72 2e 6e 44 65 6c 74 61 53 65 6e 74 20 3d 20 30  r.nDeltaSent = 0
4900: 3b 0a 20 20 20 20 78 66 65 72 2e 6e 47 69 6d 6d  ;.    xfer.nGimm
4910: 65 53 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20 68  eSent = 0;.    h
4920: 74 74 70 5f 65 78 63 68 61 6e 67 65 28 26 73 65  ttp_exchange(&se
4930: 6e 64 2c 20 26 72 65 63 76 29 3b 0a 20 20 20 20  nd, &recv);.    
4940: 62 6c 6f 62 5f 72 65 73 65 74 28 26 73 65 6e 64  blob_reset(&send
4950: 29 3b 0a 0a 20 20 20 20 2f 2a 20 50 72 6f 63 65  );..    /* Proce
4960: 73 73 20 74 68 65 20 72 65 70 6c 79 20 74 68 61  ss the reply tha
4970: 74 20 63 61 6d 65 20 62 61 63 6b 20 66 72 6f 6d  t came back from
4980: 20 74 68 65 20 73 65 72 76 65 72 20 2a 2f 0a 20   the server */. 
4990: 20 20 20 77 68 69 6c 65 28 20 62 6c 6f 62 5f 6c     while( blob_l
49a0: 69 6e 65 28 26 72 65 63 76 2c 20 26 78 66 65 72  ine(&recv, &xfer
49b0: 2e 6c 69 6e 65 29 20 29 7b 0a 20 20 20 20 20 20  .line) ){.      
49c0: 69 66 28 20 62 6c 6f 62 5f 62 75 66 66 65 72 28  if( blob_buffer(
49d0: 26 78 66 65 72 2e 6c 69 6e 65 29 5b 30 5d 3d 3d  &xfer.line)[0]==
49e0: 27 23 27 20 29 7b 0a 20 20 20 20 20 20 20 20 63  '#' ){.        c
49f0: 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d  ontinue;.      }
4a00: 0a 20 20 20 20 20 20 78 66 65 72 2e 6e 54 6f 6b  .      xfer.nTok
4a10: 65 6e 20 3d 20 62 6c 6f 62 5f 74 6f 6b 65 6e 69  en = blob_tokeni
4a20: 7a 65 28 26 78 66 65 72 2e 6c 69 6e 65 2c 20 78  ze(&xfer.line, x
4a30: 66 65 72 2e 61 54 6f 6b 65 6e 2c 20 63 6f 75 6e  fer.aToken, coun
4a40: 74 28 78 66 65 72 2e 61 54 6f 6b 65 6e 29 29 3b  t(xfer.aToken));
4a50: 0a 0a 20 20 20 20 20 20 2f 2a 20 20 20 66 69 6c  ..      /*   fil
4a60: 65 20 55 55 49 44 20 53 49 5a 45 20 5c 6e 20 43  e UUID SIZE \n C
4a70: 4f 4e 54 45 4e 54 0a 20 20 20 20 20 20 2a 2a 20  ONTENT.      ** 
4a80: 20 20 66 69 6c 65 20 55 55 49 44 20 44 45 4c 54    file UUID DELT
4a90: 41 53 52 43 20 53 49 5a 45 20 5c 6e 20 43 4f 4e  ASRC SIZE \n CON
4aa0: 54 45 4e 54 0a 20 20 20 20 20 20 2a 2a 0a 20 20  TENT.      **.  
4ab0: 20 20 20 20 2a 2a 20 52 65 63 65 69 76 65 20 61      ** Receive a
4ac0: 20 66 69 6c 65 20 74 72 61 6e 73 6d 69 74 74 65   file transmitte
4ad0: 64 20 66 72 6f 6d 20 74 68 65 20 6f 74 68 65 72  d from the other
4ae0: 20 73 69 64 65 0a 20 20 20 20 20 20 2a 2f 0a 20   side.      */. 
4af0: 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71       if( blob_eq
4b00: 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d  (&xfer.aToken[0]
4b10: 2c 22 66 69 6c 65 22 29 20 29 7b 0a 20 20 20 20  ,"file") ){.    
4b20: 20 20 20 20 78 66 65 72 5f 61 63 63 65 70 74 5f      xfer_accept_
4b30: 66 69 6c 65 28 26 78 66 65 72 29 3b 0a 20 20 20  file(&xfer);.   
4b40: 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20 20 20 20     }else..      
4b50: 2f 2a 20 20 20 67 69 6d 6d 65 20 55 55 49 44 0a  /*   gimme UUID.
4b60: 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
4b70: 2a 20 53 65 72 76 65 72 20 69 73 20 72 65 71 75  * Server is requ
4b80: 65 73 74 69 6e 67 20 61 20 66 69 6c 65 0a 20 20  esting a file.  
4b90: 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
4ba0: 20 62 6c 6f 62 5f 65 71 28 26 78 66 65 72 2e 61   blob_eq(&xfer.a
4bb0: 54 6f 6b 65 6e 5b 30 5d 2c 20 22 67 69 6d 6d 65  Token[0], "gimme
4bc0: 22 29 0a 20 20 20 20 20 20 20 26 26 20 78 66 65  ").       && xfe
4bd0: 72 2e 6e 54 6f 6b 65 6e 3d 3d 32 0a 20 20 20 20  r.nToken==2.    
4be0: 20 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75 75     && blob_is_uu
4bf0: 69 64 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b  id(&xfer.aToken[
4c00: 31 5d 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20  1]).      ){.   
4c10: 20 20 20 20 20 6e 4d 73 67 2b 2b 3b 0a 20 20 20       nMsg++;.   
4c20: 20 20 20 20 20 69 66 28 20 70 75 73 68 46 6c 61       if( pushFla
4c30: 67 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  g ){.          i
4c40: 6e 74 20 72 69 64 20 3d 20 72 69 64 5f 66 72 6f  nt rid = rid_fro
4c50: 6d 5f 75 75 69 64 28 26 78 66 65 72 2e 61 54 6f  m_uuid(&xfer.aTo
4c60: 6b 65 6e 5b 31 5d 2c 20 30 29 3b 0a 20 20 20 20  ken[1], 0);.    
4c70: 20 20 20 20 20 20 73 65 6e 64 5f 66 69 6c 65 28        send_file(
4c80: 26 78 66 65 72 2c 20 72 69 64 2c 20 26 78 66 65  &xfer, rid, &xfe
4c90: 72 2e 61 54 6f 6b 65 6e 5b 31 5d 2c 20 30 29 3b  r.aToken[1], 0);
4ca0: 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
4cb0: 20 7d 65 6c 73 65 0a 20 20 0a 20 20 20 20 20 20   }else.  .      
4cc0: 2f 2a 20 20 20 69 67 6f 74 20 55 55 49 44 0a 20  /*   igot UUID. 
4cd0: 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
4ce0: 20 53 65 72 76 65 72 20 61 6e 6e 6f 75 6e 63 65   Server announce
4cf0: 73 20 74 68 61 74 20 69 74 20 68 61 73 20 61 20  s that it has a 
4d00: 70 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65 0a  particular file.
4d10: 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
4d20: 66 28 20 78 66 65 72 2e 6e 54 6f 6b 65 6e 3d 3d  f( xfer.nToken==
4d30: 32 0a 20 20 20 20 20 20 20 26 26 20 62 6c 6f 62  2.       && blob
4d40: 5f 65 71 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e  _eq(&xfer.aToken
4d50: 5b 30 5d 2c 20 22 69 67 6f 74 22 29 0a 20 20 20  [0], "igot").   
4d60: 20 20 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75      && blob_is_u
4d70: 75 69 64 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e  uid(&xfer.aToken
4d80: 5b 31 5d 29 0a 20 20 20 20 20 20 29 7b 0a 20 20  [1]).      ){.  
4d90: 20 20 20 20 20 20 6e 4d 73 67 2b 2b 3b 0a 20 20        nMsg++;.  
4da0: 20 20 20 20 20 20 69 66 28 20 70 75 6c 6c 46 6c        if( pullFl
4db0: 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ag ){.          
4dc0: 69 66 28 20 21 64 62 5f 65 78 69 73 74 73 28 22  if( !db_exists("
4dd0: 53 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 62 6c  SELECT 1 FROM bl
4de0: 6f 62 20 57 48 45 52 45 20 75 75 69 64 3d 27 25  ob WHERE uuid='%
4df0: 62 27 20 41 4e 44 20 73 69 7a 65 3e 3d 30 22 2c  b' AND size>=0",
4e00: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
4e10: 20 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d   &xfer.aToken[1]
4e20: 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ) ){.           
4e30: 20 63 6f 6e 74 65 6e 74 5f 70 75 74 28 30 2c 20   content_put(0, 
4e40: 62 6c 6f 62 5f 73 74 72 28 26 78 66 65 72 2e 61  blob_str(&xfer.a
4e50: 54 6f 6b 65 6e 5b 31 5d 29 2c 20 30 29 3b 0a 20  Token[1]), 0);. 
4e60: 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
4e70: 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
4e80: 0a 20 20 20 20 0a 20 20 20 20 20 20 0a 20 20 20  .    .      .   
4e90: 20 20 20 2f 2a 20 20 20 6c 65 61 66 20 55 55 49     /*   leaf UUI
4ea0: 44 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  D.      **.     
4eb0: 20 2a 2a 20 53 65 72 76 65 72 20 61 6e 6e 6f 75   ** Server annou
4ec0: 6e 63 65 73 20 74 68 61 74 20 69 74 20 68 61 73  nces that it has
4ed0: 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 6d 61   a particular ma
4ee0: 6e 69 66 65 73 74 0a 20 20 20 20 20 20 2a 2f 0a  nifest.      */.
4ef0: 20 20 20 20 20 20 69 66 28 20 78 66 65 72 2e 6e        if( xfer.n
4f00: 54 6f 6b 65 6e 3d 3d 32 0a 20 20 20 20 20 20 20  Token==2.       
4f10: 26 26 20 62 6c 6f 62 5f 65 71 28 26 78 66 65 72  && blob_eq(&xfer
4f20: 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 6c 65 61  .aToken[0], "lea
4f30: 66 22 29 0a 20 20 20 20 20 20 20 26 26 20 62 6c  f").       && bl
4f40: 6f 62 5f 69 73 5f 75 75 69 64 28 26 78 66 65 72  ob_is_uuid(&xfer
4f50: 2e 61 54 6f 6b 65 6e 5b 31 5d 29 0a 20 20 20 20  .aToken[1]).    
4f60: 20 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 4d 73    ){.        nMs
4f70: 67 2b 2b 3b 0a 20 20 20 20 20 20 20 20 69 66 28  g++;.        if(
4f80: 20 70 75 73 68 46 6c 61 67 20 29 7b 0a 20 20 20   pushFlag ){.   
4f90: 20 20 20 20 20 20 20 69 6e 74 20 72 69 64 20 3d         int rid =
4fa0: 20 72 69 64 5f 66 72 6f 6d 5f 75 75 69 64 28 26   rid_from_uuid(&
4fb0: 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d 2c 20  xfer.aToken[1], 
4fc0: 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6c 65  0);.          le
4fd0: 61 66 5f 72 65 73 70 6f 6e 73 65 28 26 78 66 65  af_response(&xfe
4fe0: 72 2c 20 72 69 64 29 3b 0a 20 20 20 20 20 20 20  r, rid);.       
4ff0: 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a 20   }.      }else. 
5000: 20 0a 20 20 0a 20 20 20 20 20 20 2f 2a 20 20 20   .  .      /*   
5010: 70 75 73 68 20 20 53 45 52 56 45 52 43 4f 44 45  push  SERVERCODE
5020: 20 20 50 52 4f 44 55 43 54 43 4f 44 45 0a 20 20    PRODUCTCODE.  
5030: 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
5040: 53 68 6f 75 6c 64 20 6f 6e 6c 79 20 68 61 70 70  Should only happ
5050: 65 6e 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74  en in response t
5060: 6f 20 61 20 63 6c 6f 6e 65 2e 0a 20 20 20 20 20  o a clone..     
5070: 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 62 6c   */.      if( bl
5080: 6f 62 5f 65 71 28 26 78 66 65 72 2e 61 54 6f 6b  ob_eq(&xfer.aTok
5090: 65 6e 5b 30 5d 2c 22 70 75 73 68 22 29 0a 20 20  en[0],"push").  
50a0: 20 20 20 20 20 26 26 20 78 66 65 72 2e 6e 54 6f       && xfer.nTo
50b0: 6b 65 6e 3d 3d 33 0a 20 20 20 20 20 20 20 26 26  ken==3.       &&
50c0: 20 63 6c 6f 6e 65 46 6c 61 67 0a 20 20 20 20 20   cloneFlag.     
50d0: 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69    && blob_is_uui
50e0: 64 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31  d(&xfer.aToken[1
50f0: 5d 29 0a 20 20 20 20 20 20 20 26 26 20 62 6c 6f  ]).       && blo
5100: 62 5f 69 73 5f 75 75 69 64 28 26 78 66 65 72 2e  b_is_uuid(&xfer.
5110: 61 54 6f 6b 65 6e 5b 32 5d 29 0a 20 20 20 20 20  aToken[2]).     
5120: 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
5130: 62 6c 6f 62 5f 65 71 5f 73 74 72 28 26 78 66 65  blob_eq_str(&xfe
5140: 72 2e 61 54 6f 6b 65 6e 5b 31 5d 2c 20 7a 53 43  r.aToken[1], zSC
5150: 6f 64 65 2c 20 2d 31 29 20 29 7b 0a 20 20 20 20  ode, -1) ){.    
5160: 20 20 20 20 20 20 66 6f 73 73 69 6c 5f 66 61 74        fossil_fat
5170: 61 6c 28 22 73 65 72 76 65 72 20 6c 6f 6f 70 22  al("server loop"
5180: 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
5190: 20 20 20 20 20 6e 4d 73 67 2b 2b 3b 0a 20 20 20       nMsg++;.   
51a0: 20 20 20 20 20 69 66 28 20 7a 50 43 6f 64 65 3d       if( zPCode=
51b0: 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
51c0: 7a 50 43 6f 64 65 20 3d 20 6d 70 72 69 6e 74 66  zPCode = mprintf
51d0: 28 22 25 62 22 2c 20 26 78 66 65 72 2e 61 54 6f  ("%b", &xfer.aTo
51e0: 6b 65 6e 5b 32 5d 29 3b 0a 20 20 20 20 20 20 20  ken[2]);.       
51f0: 20 20 20 64 62 5f 73 65 74 28 22 70 72 6f 6a 65     db_set("proje
5200: 63 74 2d 63 6f 64 65 22 2c 20 7a 50 43 6f 64 65  ct-code", zPCode
5210: 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
5220: 20 20 20 20 20 63 6c 6f 6e 65 46 6c 61 67 20 3d       cloneFlag =
5230: 20 30 3b 0a 20 20 20 20 20 20 20 20 70 75 6c 6c   0;.        pull
5240: 46 6c 61 67 20 3d 20 31 3b 0a 20 20 20 20 20 20  Flag = 1;.      
5250: 7d 65 6c 73 65 0a 0a 20 20 20 20 20 20 2f 2a 20  }else..      /* 
5260: 20 20 65 72 72 6f 72 20 4d 45 53 53 41 47 45 0a    error MESSAGE.
5270: 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
5280: 2a 20 52 65 70 6f 72 74 20 61 6e 20 65 72 72 6f  * Report an erro
5290: 72 0a 20 20 20 20 20 20 2a 2f 20 20 20 20 20 20  r.      */      
52a0: 20 20 0a 20 20 20 20 20 20 69 66 28 20 62 6c 6f    .      if( blo
52b0: 62 5f 65 71 28 26 78 66 65 72 2e 61 54 6f 6b 65  b_eq(&xfer.aToke
52c0: 6e 5b 30 5d 2c 22 65 72 72 6f 72 22 29 20 26 26  n[0],"error") &&
52d0: 20 78 66 65 72 2e 6e 54 6f 6b 65 6e 3d 3d 32 20   xfer.nToken==2 
52e0: 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20  ){.        char 
52f0: 2a 7a 4d 73 67 20 3d 20 62 6c 6f 62 5f 74 65 72  *zMsg = blob_ter
5300: 6d 69 6e 61 74 65 28 26 78 66 65 72 2e 61 54 6f  minate(&xfer.aTo
5310: 6b 65 6e 5b 31 5d 29 3b 0a 20 20 20 20 20 20 20  ken[1]);.       
5320: 20 64 65 66 6f 73 73 69 6c 69 7a 65 28 7a 4d 73   defossilize(zMs
5330: 67 29 3b 0a 20 20 20 20 20 20 20 20 62 6c 6f 62  g);.        blob
5340: 5f 61 70 70 65 6e 64 66 28 26 78 66 65 72 2e 65  _appendf(&xfer.e
5350: 72 72 2c 20 22 73 65 72 76 65 72 20 73 61 79 73  rr, "server says
5360: 3a 20 25 73 22 2c 20 7a 4d 73 67 29 3b 0a 20 20  : %s", zMsg);.  
5370: 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20 20 20      }else..     
5380: 20 2f 2a 20 55 6e 6b 6e 6f 77 6e 20 6d 65 73 73   /* Unknown mess
5390: 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 7b 0a 20  age */.      {. 
53a0: 20 20 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 65         blob_appe
53b0: 6e 64 66 28 26 78 66 65 72 2e 65 72 72 2c 20 22  ndf(&xfer.err, "
53c0: 75 6e 6b 6e 6f 77 6e 20 63 6f 6d 6d 61 6e 64 3a  unknown command:
53d0: 20 25 62 22 2c 20 26 78 66 65 72 2e 61 54 6f 6b   %b", &xfer.aTok
53e0: 65 6e 5b 30 5d 29 3b 0a 20 20 20 20 20 20 7d 0a  en[0]);.      }.
53f0: 0a 20 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f  .      if( blob_
5400: 73 69 7a 65 28 26 78 66 65 72 2e 65 72 72 29 20  size(&xfer.err) 
5410: 29 7b 0a 20 20 20 20 20 20 20 20 66 6f 73 73 69  ){.        fossi
5420: 6c 5f 66 61 74 61 6c 28 22 25 62 22 2c 20 26 78  l_fatal("%b", &x
5430: 66 65 72 2e 65 72 72 29 3b 0a 20 20 20 20 20 20  fer.err);.      
5440: 7d 0a 20 20 20 20 20 20 62 6c 6f 62 61 72 72 61  }.      blobarra
5450: 79 5f 72 65 73 65 74 28 78 66 65 72 2e 61 54 6f  y_reset(xfer.aTo
5460: 6b 65 6e 2c 20 78 66 65 72 2e 6e 54 6f 6b 65 6e  ken, xfer.nToken
5470: 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 72 65  );.      blob_re
5480: 73 65 74 28 26 78 66 65 72 2e 6c 69 6e 65 29 3b  set(&xfer.line);
5490: 0a 20 20 20 20 7d 0a 20 20 20 20 70 72 69 6e 74  .    }.    print
54a0: 66 28 22 52 65 63 65 69 76 65 64 3a 20 20 25 31  f("Received:  %1
54b0: 30 64 20 62 79 74 65 73 2c 20 25 33 64 20 6d 65  0d bytes, %3d me
54c0: 73 73 61 67 65 73 2c 20 25 33 64 20 66 69 6c 65  ssages, %3d file
54d0: 73 20 28 25 64 2b 25 64 2b 25 64 29 5c 6e 22 2c  s (%d+%d+%d)\n",
54e0: 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 6c 6f  .            blo
54f0: 62 5f 73 69 7a 65 28 26 72 65 63 76 29 2c 20 6e  b_size(&recv), n
5500: 4d 73 67 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Msg,.           
5510: 20 78 66 65 72 2e 6e 46 69 6c 65 52 63 76 64 20   xfer.nFileRcvd 
5520: 2b 20 78 66 65 72 2e 6e 44 65 6c 74 61 52 63 76  + xfer.nDeltaRcv
5530: 64 20 2b 20 78 66 65 72 2e 6e 44 61 6e 67 6c 69  d + xfer.nDangli
5540: 6e 67 46 69 6c 65 2c 0a 20 20 20 20 20 20 20 20  ngFile,.        
5550: 20 20 20 20 78 66 65 72 2e 6e 46 69 6c 65 52 63      xfer.nFileRc
5560: 76 64 2c 20 78 66 65 72 2e 6e 44 65 6c 74 61 52  vd, xfer.nDeltaR
5570: 63 76 64 2c 20 78 66 65 72 2e 6e 44 61 6e 67 6c  cvd, xfer.nDangl
5580: 69 6e 67 46 69 6c 65 29 3b 0a 0a 20 20 20 20 62  ingFile);..    b
5590: 6c 6f 62 5f 72 65 73 65 74 28 26 72 65 63 76 29  lob_reset(&recv)
55a0: 3b 0a 20 20 20 20 6e 4d 73 67 20 3d 20 30 3b 0a  ;.    nMsg = 0;.
55b0: 20 20 20 20 78 66 65 72 2e 6e 46 69 6c 65 52 63      xfer.nFileRc
55c0: 76 64 20 3d 20 30 3b 0a 20 20 20 20 78 66 65 72  vd = 0;.    xfer
55d0: 2e 6e 44 65 6c 74 61 52 63 76 64 20 3d 20 30 3b  .nDeltaRcvd = 0;
55e0: 0a 20 20 20 20 78 66 65 72 2e 6e 44 61 6e 67 6c  .    xfer.nDangl
55f0: 69 6e 67 46 69 6c 65 20 3d 20 30 3b 0a 20 20 20  ingFile = 0;.   
5600: 20 6e 43 79 63 6c 65 2b 2b 3b 0a 20 20 20 20 67   nCycle++;.    g
5610: 6f 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 49  o = 0;..    /* I
5620: 66 20 77 65 20 68 61 76 65 20 72 65 63 65 69 76  f we have receiv
5630: 65 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 66  ed one or more f
5640: 69 6c 65 73 20 6f 6e 20 74 68 69 73 20 63 79 63  iles on this cyc
5650: 6c 65 20 61 6e 64 0a 20 20 20 20 2a 2a 20 77 65  le and.    ** we
5660: 20 68 61 76 65 20 6f 6e 65 20 6f 72 20 6d 6f 72   have one or mor
5670: 65 20 70 68 61 6e 74 6f 6d 73 2c 20 74 68 65 6e  e phantoms, then
5680: 20 67 6f 20 66 6f 72 20 61 6e 6f 74 68 65 72 20   go for another 
5690: 72 6f 75 6e 64 0a 20 20 20 20 2a 2f 0a 20 20 20  round.    */.   
56a0: 20 69 66 28 78 66 65 72 2e 6e 46 69 6c 65 52 63   if(xfer.nFileRc
56b0: 76 64 2b 78 66 65 72 2e 6e 44 65 6c 74 61 52 63  vd+xfer.nDeltaRc
56c0: 76 64 2b 78 66 65 72 2e 6e 44 61 6e 67 6c 69 6e  vd+xfer.nDanglin
56d0: 67 46 69 6c 65 3e 30 0a 20 20 20 20 20 26 26 20  gFile>0.     && 
56e0: 64 62 5f 65 78 69 73 74 73 28 22 53 45 4c 45 43  db_exists("SELEC
56f0: 54 20 31 20 46 52 4f 4d 20 70 68 61 6e 74 6f 6d  T 1 FROM phantom
5700: 22 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  ").    ){.      
5710: 67 6f 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a 20  go = 1;.    }.. 
5720: 20 20 20 2f 2a 20 49 66 20 77 65 20 68 61 76 65     /* If we have
5730: 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 66 69 6c   one or more fil
5740: 65 73 20 71 75 65 75 65 64 20 74 6f 20 73 65 6e  es queued to sen
5750: 64 2c 20 74 68 65 6e 20 67 6f 0a 20 20 20 20 2a  d, then go.    *
5760: 2a 20 61 6e 6f 74 68 65 72 20 72 6f 75 6e 64 20  * another round 
5770: 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
5780: 78 66 65 72 2e 6e 46 69 6c 65 53 65 6e 74 2b 78  xfer.nFileSent+x
5790: 66 65 72 2e 6e 44 65 6c 74 61 53 65 6e 74 3e 30  fer.nDeltaSent>0
57a0: 20 29 7b 0a 20 20 20 20 20 20 67 6f 20 3d 20 31   ){.      go = 1
57b0: 3b 0a 20 20 20 20 7d 0a 20 20 7d 3b 0a 20 20 68  ;.    }.  };.  h
57c0: 74 74 70 5f 63 6c 6f 73 65 28 29 3b 0a 20 20 64  ttp_close();.  d
57d0: 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f  b_end_transactio
57e0: 6e 28 30 29 3b 0a 7d 0a                          n(0);.}.