Hex Artifact Content
Not logged in

Artifact f732b233d1ab879b603b26716e10e08f81c80769:

File src/xfer.c part of check-in [8c828207a2] - Give an error if an attempt is made to merge, update, or checkout against an incomplete baseline - one that contains phantoms. Update the xfer protocol to converge on a stable synchronization faster and (hopeful) not quit until the sync is complete. by drh on 2007-08-27 00:04:32. Also file src/xfer.c part of check-in [15652ff081] - Merged drh's fixes new features (xfer, timeline handling, javascript based timeline highlighting) into my branch. by aku on 2007-08-29 02:55:33.

0000: 2f 2a 0a 2a 2a 20 43 6f 70 79 72 69 67 68 74 20  /*.** Copyright 
0010: 28 63 29 20 32 30 30 37 20 44 2e 20 52 69 63 68  (c) 2007 D. Rich
0020: 61 72 64 20 48 69 70 70 0a 2a 2a 0a 2a 2a 20 54  ard Hipp.**.** T
0030: 68 69 73 20 70 72 6f 67 72 61 6d 20 69 73 20 66  his program is f
0040: 72 65 65 20 73 6f 66 74 77 61 72 65 3b 20 79 6f  ree software; yo
0050: 75 20 63 61 6e 20 72 65 64 69 73 74 72 69 62 75  u can redistribu
0060: 74 65 20 69 74 20 61 6e 64 2f 6f 72 0a 2a 2a 20  te it and/or.** 
0070: 6d 6f 64 69 66 79 20 69 74 20 75 6e 64 65 72 20  modify it under 
0080: 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65  the terms of the
0090: 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 50 75 62   GNU General Pub
00a0: 6c 69 63 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 76  lic.** License v
00b0: 65 72 73 69 6f 6e 20 32 20 61 73 20 70 75 62 6c  ersion 2 as publ
00c0: 69 73 68 65 64 20 62 79 20 74 68 65 20 46 72 65  ished by the Fre
00d0: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64  e Software Found
00e0: 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ation..**.** Thi
00f0: 73 20 70 72 6f 67 72 61 6d 20 69 73 20 64 69 73  s program is dis
0100: 74 72 69 62 75 74 65 64 20 69 6e 20 74 68 65 20  tributed in the 
0110: 68 6f 70 65 20 74 68 61 74 20 69 74 20 77 69 6c  hope that it wil
0120: 6c 20 62 65 20 75 73 65 66 75 6c 2c 0a 2a 2a 20  l be useful,.** 
0130: 62 75 74 20 57 49 54 48 4f 55 54 20 41 4e 59 20  but WITHOUT ANY 
0140: 57 41 52 52 41 4e 54 59 3b 20 77 69 74 68 6f 75  WARRANTY; withou
0150: 74 20 65 76 65 6e 20 74 68 65 20 69 6d 70 6c 69  t even the impli
0160: 65 64 20 77 61 72 72 61 6e 74 79 20 6f 66 0a 2a  ed warranty of.*
0170: 2a 20 4d 45 52 43 48 41 4e 54 41 42 49 4c 49 54  * MERCHANTABILIT
0180: 59 20 6f 72 20 46 49 54 4e 45 53 53 20 46 4f 52  Y or FITNESS FOR
0190: 20 41 20 50 41 52 54 49 43 55 4c 41 52 20 50 55   A PARTICULAR PU
01a0: 52 50 4f 53 45 2e 20 20 53 65 65 20 74 68 65 20  RPOSE.  See the 
01b0: 47 4e 55 0a 2a 2a 20 47 65 6e 65 72 61 6c 20 50  GNU.** General P
01c0: 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 20 66 6f  ublic License fo
01d0: 72 20 6d 6f 72 65 20 64 65 74 61 69 6c 73 2e 0a  r more details..
01e0: 2a 2a 20 0a 2a 2a 20 59 6f 75 20 73 68 6f 75 6c  ** .** You shoul
01f0: 64 20 68 61 76 65 20 72 65 63 65 69 76 65 64 20  d have received 
0200: 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 47 4e  a copy of the GN
0210: 55 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69 63  U General Public
0220: 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 61 6c 6f 6e  .** License alon
0230: 67 20 77 69 74 68 20 74 68 69 73 20 6c 69 62 72  g with this libr
0240: 61 72 79 3b 20 69 66 20 6e 6f 74 2c 20 77 72 69  ary; if not, wri
0250: 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 46 72 65  te to the.** Fre
0260: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64  e Software Found
0270: 61 74 69 6f 6e 2c 20 49 6e 63 2e 2c 20 35 39 20  ation, Inc., 59 
0280: 54 65 6d 70 6c 65 20 50 6c 61 63 65 20 2d 20 53  Temple Place - S
0290: 75 69 74 65 20 33 33 30 2c 0a 2a 2a 20 42 6f 73  uite 330,.** Bos
02a0: 74 6f 6e 2c 20 4d 41 20 20 30 32 31 31 31 2d 31  ton, MA  02111-1
02b0: 33 30 37 2c 20 55 53 41 2e 0a 2a 2a 0a 2a 2a 20  307, USA..**.** 
02c0: 41 75 74 68 6f 72 20 63 6f 6e 74 61 63 74 20 69  Author contact i
02d0: 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a 20 20  nformation:.**  
02e0: 20 64 72 68 40 68 77 61 63 69 2e 63 6f 6d 0a 2a   drh@hwaci.com.*
02f0: 2a 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 68  *   http://www.h
0300: 77 61 63 69 2e 63 6f 6d 2f 64 72 68 2f 0a 2a 2a  waci.com/drh/.**
0310: 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
0320: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0330: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0340: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0350: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0360: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
0370: 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74   contains code t
0380: 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  o implement the 
0390: 66 69 6c 65 20 74 72 61 6e 73 66 65 72 20 70 72  file transfer pr
03a0: 6f 74 6f 63 6f 6c 2e 0a 2a 2f 0a 23 69 6e 63 6c  otocol..*/.#incl
03b0: 75 64 65 20 22 63 6f 6e 66 69 67 2e 68 22 0a 23  ude "config.h".#
03c0: 69 6e 63 6c 75 64 65 20 22 78 66 65 72 2e 68 22  include "xfer.h"
03d0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72  ../*.** This str
03e0: 75 63 74 75 72 65 20 68 6f 6c 64 73 20 69 6e 66  ucture holds inf
03f0: 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74  ormation about t
0400: 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 65  he current state
0410: 20 6f 66 20 65 69 74 68 65 72 0a 2a 2a 20 61 20   of either.** a 
0420: 63 6c 69 65 6e 74 20 6f 72 20 61 20 73 65 72 76  client or a serv
0430: 65 72 20 74 68 61 74 20 69 73 20 70 61 72 74 69  er that is parti
0440: 63 69 70 61 74 69 6e 67 20 69 6e 20 78 66 65 72  cipating in xfer
0450: 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
0460: 75 63 74 20 58 66 65 72 20 58 66 65 72 3b 0a 73  uct Xfer Xfer;.s
0470: 74 72 75 63 74 20 58 66 65 72 20 7b 0a 20 20 42  truct Xfer {.  B
0480: 6c 6f 62 20 2a 70 49 6e 3b 20 20 20 20 20 20 20  lob *pIn;       
0490: 20 20 20 2f 2a 20 49 6e 70 75 74 20 74 65 78 74     /* Input text
04a0: 20 66 72 6f 6d 20 74 68 65 20 6f 74 68 65 72 20   from the other 
04b0: 73 69 64 65 20 2a 2f 0a 20 20 42 6c 6f 62 20 2a  side */.  Blob *
04c0: 70 4f 75 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  pOut;         /*
04d0: 20 43 6f 6d 70 6f 73 65 20 6f 75 72 20 72 65 70   Compose our rep
04e0: 6c 79 20 68 65 72 65 20 2a 2f 0a 20 20 42 6c 6f  ly here */.  Blo
04f0: 62 20 6c 69 6e 65 3b 20 20 20 20 20 20 20 20 20  b line;         
0500: 20 2f 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20   /* The current 
0510: 6c 69 6e 65 20 6f 66 20 69 6e 70 75 74 20 2a 2f  line of input */
0520: 0a 20 20 42 6c 6f 62 20 61 54 6f 6b 65 6e 5b 35  .  Blob aToken[5
0530: 5d 3b 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69  ];     /* Tokeni
0540: 7a 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 6c  zed version of l
0550: 69 6e 65 20 2a 2f 0a 20 20 42 6c 6f 62 20 65 72  ine */.  Blob er
0560: 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  r;           /* 
0570: 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65  Error message te
0580: 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 6b  xt */.  int nTok
0590: 65 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  en;         /* N
05a0: 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20  umber of tokens 
05b0: 69 6e 20 6c 69 6e 65 20 2a 2f 0a 20 20 69 6e 74  in line */.  int
05c0: 20 6e 49 47 6f 74 53 65 6e 74 3b 20 20 20 20 20   nIGotSent;     
05d0: 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 22 69   /* Number of "i
05e0: 67 6f 74 22 20 6d 65 73 73 61 67 65 73 20 73 65  got" messages se
05f0: 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 47 69 6d  nt */.  int nGim
0600: 6d 65 53 65 6e 74 3b 20 20 20 20 20 2f 2a 20 4e  meSent;     /* N
0610: 75 6d 62 65 72 20 6f 66 20 67 69 6d 6d 65 20 6d  umber of gimme m
0620: 65 73 73 61 67 65 73 20 73 65 6e 74 20 2a 2f 0a  essages sent */.
0630: 20 20 69 6e 74 20 6e 46 69 6c 65 53 65 6e 74 3b    int nFileSent;
0640: 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
0650: 6f 66 20 66 69 6c 65 73 20 73 65 6e 74 20 2a 2f  of files sent */
0660: 0a 20 20 69 6e 74 20 6e 44 65 6c 74 61 53 65 6e  .  int nDeltaSen
0670: 74 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  t;     /* Number
0680: 20 6f 66 20 64 65 6c 74 61 73 20 73 65 6e 74 20   of deltas sent 
0690: 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 6c 65 52 63  */.  int nFileRc
06a0: 76 64 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  vd;      /* Numb
06b0: 65 72 20 6f 66 20 66 69 6c 65 73 20 72 65 63 65  er of files rece
06c0: 69 76 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 44  ived */.  int nD
06d0: 65 6c 74 61 52 63 76 64 3b 20 20 20 20 20 2f 2a  eltaRcvd;     /*
06e0: 20 4e 75 6d 62 65 72 20 6f 66 20 64 65 6c 74 61   Number of delta
06f0: 73 20 72 65 63 65 69 76 65 64 20 2a 2f 0a 20 20  s received */.  
0700: 69 6e 74 20 6e 44 61 6e 67 6c 69 6e 67 46 69 6c  int nDanglingFil
0710: 65 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66  e;  /* Number of
0720: 20 64 61 6e 67 6c 69 6e 67 20 64 65 6c 74 61 73   dangling deltas
0730: 20 72 65 63 65 69 76 65 64 20 2a 2f 0a 20 20 69   received */.  i
0740: 6e 74 20 6d 78 53 65 6e 64 3b 20 20 20 20 20 20  nt mxSend;      
0750: 20 20 20 2f 2a 20 53 74 6f 70 20 73 65 6e 64 69     /* Stop sendi
0760: 6e 67 20 22 66 69 6c 65 22 20 77 69 74 68 20 70  ng "file" with p
0770: 4f 75 74 20 72 65 61 63 68 65 73 20 74 68 69 73  Out reaches this
0780: 20 73 69 7a 65 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a   size */.};.../*
0790: 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 62 6c  .** The input bl
07a0: 6f 62 20 63 6f 6e 74 61 69 6e 73 20 61 20 55 55  ob contains a UU
07b0: 49 44 2e 20 20 43 6f 6e 76 65 72 74 20 69 74 20  ID.  Convert it 
07c0: 69 6e 74 6f 20 61 20 72 65 63 6f 72 64 20 49 44  into a record ID
07d0: 2e 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 70 68  ..** Create a ph
07e0: 61 6e 74 6f 6d 20 72 65 63 6f 72 64 20 69 66 20  antom record if 
07f0: 6e 6f 20 70 72 69 6f 72 20 72 65 63 6f 72 64 20  no prior record 
0800: 65 78 69 73 74 73 20 61 6e 64 0a 2a 2a 20 70 68  exists and.** ph
0810: 61 6e 74 6f 6d 69 7a 65 20 69 73 20 74 72 75 65  antomize is true
0820: 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20  ..**.** Compare 
0830: 74 6f 20 75 75 69 64 5f 74 6f 5f 72 69 64 28 29  to uuid_to_rid()
0840: 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
0850: 74 61 6b 65 73 20 61 20 62 6c 6f 62 20 61 72 67  takes a blob arg
0860: 75 6d 65 6e 74 0a 2a 2a 20 61 6e 64 20 64 6f 65  ument.** and doe
0870: 73 20 6c 65 73 73 20 65 72 72 6f 72 20 63 68 65  s less error che
0880: 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63  cking..*/.static
0890: 20 69 6e 74 20 72 69 64 5f 66 72 6f 6d 5f 75 75   int rid_from_uu
08a0: 69 64 28 42 6c 6f 62 20 2a 70 55 75 69 64 2c 20  id(Blob *pUuid, 
08b0: 69 6e 74 20 70 68 61 6e 74 6f 6d 69 7a 65 29 7b  int phantomize){
08c0: 0a 20 20 69 6e 74 20 72 69 64 20 3d 20 64 62 5f  .  int rid = db_
08d0: 69 6e 74 28 30 2c 20 22 53 45 4c 45 43 54 20 72  int(0, "SELECT r
08e0: 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45  id FROM blob WHE
08f0: 52 45 20 75 75 69 64 3d 27 25 62 27 22 2c 20 70  RE uuid='%b'", p
0900: 55 75 69 64 29 3b 0a 20 20 69 66 28 20 72 69 64  Uuid);.  if( rid
0910: 3d 3d 30 20 26 26 20 70 68 61 6e 74 6f 6d 69 7a  ==0 && phantomiz
0920: 65 20 29 7b 0a 20 20 20 20 72 69 64 20 3d 20 63  e ){.    rid = c
0930: 6f 6e 74 65 6e 74 5f 70 75 74 28 30 2c 20 62 6c  ontent_put(0, bl
0940: 6f 62 5f 73 74 72 28 70 55 75 69 64 29 2c 20 30  ob_str(pUuid), 0
0950: 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
0960: 72 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  rid;.}../*.** Re
0970: 6d 65 6d 62 65 72 20 74 68 61 74 20 74 68 65 20  member that the 
0980: 6f 74 68 65 72 20 73 69 64 65 20 6f 66 20 74 68  other side of th
0990: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 6c 72  e connection alr
09a0: 65 61 64 79 20 68 61 73 20 61 20 63 6f 70 79 0a  eady has a copy.
09b0: 2a 2a 20 6f 66 20 74 68 65 20 66 69 6c 65 20 72  ** of the file r
09c0: 69 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  id..*/.static vo
09d0: 69 64 20 72 65 6d 6f 74 65 5f 68 61 73 28 69 6e  id remote_has(in
09e0: 74 20 72 69 64 29 7b 0a 20 20 64 62 5f 6d 75 6c  t rid){.  db_mul
09f0: 74 69 5f 65 78 65 63 28 22 49 4e 53 45 52 54 20  ti_exec("INSERT 
0a00: 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 6f  OR IGNORE INTO o
0a10: 6e 72 65 6d 6f 74 65 20 56 41 4c 55 45 53 28 25  nremote VALUES(%
0a20: 64 29 22 2c 20 72 69 64 29 3b 0a 7d 0a 0a 2f 2a  d)", rid);.}../*
0a30: 0a 2a 2a 20 54 68 65 20 61 54 6f 6b 65 6e 5b 30  .** The aToken[0
0a40: 2e 2e 6e 54 6f 6b 65 6e 2d 31 5d 20 62 6c 6f 62  ..nToken-1] blob
0a50: 20 61 72 72 61 79 20 69 73 20 61 20 70 61 72 73   array is a pars
0a60: 65 20 6f 66 20 61 20 22 66 69 6c 65 22 20 6c 69  e of a "file" li
0a70: 6e 65 20 0a 2a 2a 20 6d 65 73 73 61 67 65 2e 20  ne .** message. 
0a80: 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 66 69   This routine fi
0a90: 6e 69 73 68 65 73 20 70 61 72 73 69 6e 67 20 74  nishes parsing t
0aa0: 68 61 74 20 6d 65 73 73 61 67 65 20 61 6e 64 20  hat message and 
0ab0: 64 6f 65 73 0a 2a 2a 20 61 20 72 65 63 6f 72 64  does.** a record
0ac0: 20 69 6e 73 65 72 74 20 6f 66 20 74 68 65 20 66   insert of the f
0ad0: 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  ile..**.** The f
0ae0: 69 6c 65 20 6c 69 6e 65 20 69 73 20 69 6e 20 6f  ile line is in o
0af0: 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ne of the follow
0b00: 69 6e 67 20 74 77 6f 20 66 6f 72 6d 73 3a 0a 2a  ing two forms:.*
0b10: 2a 0a 2a 2a 20 20 20 20 20 20 66 69 6c 65 20 55  *.**      file U
0b20: 55 49 44 20 53 49 5a 45 20 5c 6e 20 43 4f 4e 54  UID SIZE \n CONT
0b30: 45 4e 54 0a 2a 2a 20 20 20 20 20 20 66 69 6c 65  ENT.**      file
0b40: 20 55 55 49 44 20 44 45 4c 54 41 53 52 43 20 53   UUID DELTASRC S
0b50: 49 5a 45 20 5c 6e 20 43 4f 4e 54 45 4e 54 0a 2a  IZE \n CONTENT.*
0b60: 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74  *.** The content
0b70: 20 69 73 20 53 49 5a 45 20 62 79 74 65 73 20 69   is SIZE bytes i
0b80: 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f  mmediately follo
0b90: 77 69 6e 67 20 74 68 65 20 6e 65 77 6c 69 6e 65  wing the newline
0ba0: 2e 0a 2a 2a 20 49 66 20 44 45 4c 54 41 53 52 43  ..** If DELTASRC
0bb0: 20 65 78 69 73 74 73 2c 20 74 68 65 6e 20 74 68   exists, then th
0bc0: 65 20 43 4f 4e 54 45 4e 54 20 69 73 20 61 20 64  e CONTENT is a d
0bd0: 65 6c 74 61 20 61 67 61 69 6e 73 74 20 74 68 65  elta against the
0be0: 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 44  .** content of D
0bf0: 45 4c 54 41 53 52 43 2e 0a 2a 2a 0a 2a 2a 20 49  ELTASRC..**.** I
0c00: 66 20 61 6e 79 20 65 72 72 6f 72 20 6f 63 63 75  f any error occu
0c10: 72 73 2c 20 77 72 69 74 65 20 61 20 6d 65 73 73  rs, write a mess
0c20: 61 67 65 20 69 6e 74 6f 20 70 45 72 72 20 77 68  age into pErr wh
0c30: 69 63 68 20 68 61 73 20 61 6c 72 65 61 64 79 0a  ich has already.
0c40: 2a 2a 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65  ** be initialize
0c50: 64 20 74 6f 20 61 6e 20 65 6d 70 74 79 20 73 74  d to an empty st
0c60: 72 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ring..*/.static 
0c70: 76 6f 69 64 20 78 66 65 72 5f 61 63 63 65 70 74  void xfer_accept
0c80: 5f 66 69 6c 65 28 58 66 65 72 20 2a 70 58 66 65  _file(Xfer *pXfe
0c90: 72 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 69  r){.  int n;.  i
0ca0: 6e 74 20 72 69 64 3b 0a 20 20 42 6c 6f 62 20 63  nt rid;.  Blob c
0cb0: 6f 6e 74 65 6e 74 2c 20 68 61 73 68 3b 0a 20 20  ontent, hash;.  
0cc0: 0a 20 20 69 66 28 20 70 58 66 65 72 2d 3e 6e 54  .  if( pXfer->nT
0cd0: 6f 6b 65 6e 3c 33 20 0a 20 20 20 7c 7c 20 70 58  oken<3 .   || pX
0ce0: 66 65 72 2d 3e 6e 54 6f 6b 65 6e 3e 34 0a 20 20  fer->nToken>4.  
0cf0: 20 7c 7c 20 21 62 6c 6f 62 5f 69 73 5f 75 75 69   || !blob_is_uui
0d00: 64 28 26 70 58 66 65 72 2d 3e 61 54 6f 6b 65 6e  d(&pXfer->aToken
0d10: 5b 31 5d 29 0a 20 20 20 7c 7c 20 21 62 6c 6f 62  [1]).   || !blob
0d20: 5f 69 73 5f 69 6e 74 28 26 70 58 66 65 72 2d 3e  _is_int(&pXfer->
0d30: 61 54 6f 6b 65 6e 5b 70 58 66 65 72 2d 3e 6e 54  aToken[pXfer->nT
0d40: 6f 6b 65 6e 2d 31 5d 2c 20 26 6e 29 0a 20 20 20  oken-1], &n).   
0d50: 7c 7c 20 6e 3c 30 0a 20 20 20 7c 7c 20 28 70 58  || n<0.   || (pX
0d60: 66 65 72 2d 3e 6e 54 6f 6b 65 6e 3d 3d 34 20 26  fer->nToken==4 &
0d70: 26 20 21 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28  & !blob_is_uuid(
0d80: 26 70 58 66 65 72 2d 3e 61 54 6f 6b 65 6e 5b 32  &pXfer->aToken[2
0d90: 5d 29 29 0a 20 20 29 7b 0a 20 20 20 20 62 6c 6f  ])).  ){.    blo
0da0: 62 5f 61 70 70 65 6e 64 66 28 26 70 58 66 65 72  b_appendf(&pXfer
0db0: 2d 3e 65 72 72 2c 20 22 6d 61 6c 66 6f 72 6d 65  ->err, "malforme
0dc0: 64 20 66 69 6c 65 20 6c 69 6e 65 22 29 3b 0a 20  d file line");. 
0dd0: 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
0de0: 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 63 6f 6e 74   blob_zero(&cont
0df0: 65 6e 74 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72  ent);.  blob_zer
0e00: 6f 28 26 68 61 73 68 29 3b 0a 20 20 62 6c 6f 62  o(&hash);.  blob
0e10: 5f 65 78 74 72 61 63 74 28 70 58 66 65 72 2d 3e  _extract(pXfer->
0e20: 70 49 6e 2c 20 6e 2c 20 26 63 6f 6e 74 65 6e 74  pIn, n, &content
0e30: 29 3b 0a 20 20 69 66 28 20 70 58 66 65 72 2d 3e  );.  if( pXfer->
0e40: 6e 54 6f 6b 65 6e 3d 3d 34 20 29 7b 0a 20 20 20  nToken==4 ){.   
0e50: 20 42 6c 6f 62 20 73 72 63 3b 0a 20 20 20 20 69   Blob src;.    i
0e60: 6e 74 20 73 72 63 69 64 20 3d 20 72 69 64 5f 66  nt srcid = rid_f
0e70: 72 6f 6d 5f 75 75 69 64 28 26 70 58 66 65 72 2d  rom_uuid(&pXfer-
0e80: 3e 61 54 6f 6b 65 6e 5b 32 5d 2c 20 31 29 3b 0a  >aToken[2], 1);.
0e90: 20 20 20 20 69 66 28 20 63 6f 6e 74 65 6e 74 5f      if( content_
0ea0: 67 65 74 28 73 72 63 69 64 2c 20 26 73 72 63 29  get(srcid, &src)
0eb0: 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e  ==0 ){.      con
0ec0: 74 65 6e 74 5f 70 75 74 28 26 63 6f 6e 74 65 6e  tent_put(&conten
0ed0: 74 2c 20 62 6c 6f 62 5f 73 74 72 28 26 70 58 66  t, blob_str(&pXf
0ee0: 65 72 2d 3e 61 54 6f 6b 65 6e 5b 31 5d 29 2c 20  er->aToken[1]), 
0ef0: 73 72 63 69 64 29 3b 0a 20 20 20 20 20 20 62 6c  srcid);.      bl
0f00: 6f 62 5f 61 70 70 65 6e 64 66 28 70 58 66 65 72  ob_appendf(pXfer
0f10: 2d 3e 70 4f 75 74 2c 20 22 67 69 6d 6d 65 20 25  ->pOut, "gimme %
0f20: 62 5c 6e 22 2c 20 26 70 58 66 65 72 2d 3e 61 54  b\n", &pXfer->aT
0f30: 6f 6b 65 6e 5b 32 5d 29 3b 0a 20 20 20 20 20 20  oken[2]);.      
0f40: 70 58 66 65 72 2d 3e 6e 47 69 6d 6d 65 53 65 6e  pXfer->nGimmeSen
0f50: 74 2b 2b 3b 0a 20 20 20 20 20 20 70 58 66 65 72  t++;.      pXfer
0f60: 2d 3e 6e 44 61 6e 67 6c 69 6e 67 46 69 6c 65 2b  ->nDanglingFile+
0f70: 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b  +;.      return;
0f80: 0a 20 20 20 20 7d 0a 20 20 20 20 70 58 66 65 72  .    }.    pXfer
0f90: 2d 3e 6e 44 65 6c 74 61 52 63 76 64 2b 2b 3b 0a  ->nDeltaRcvd++;.
0fa0: 20 20 20 20 62 6c 6f 62 5f 64 65 6c 74 61 5f 61      blob_delta_a
0fb0: 70 70 6c 79 28 26 73 72 63 2c 20 26 63 6f 6e 74  pply(&src, &cont
0fc0: 65 6e 74 2c 20 26 63 6f 6e 74 65 6e 74 29 3b 0a  ent, &content);.
0fd0: 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26      blob_reset(&
0fe0: 73 72 63 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  src);.  }else{. 
0ff0: 20 20 20 70 58 66 65 72 2d 3e 6e 46 69 6c 65 52     pXfer->nFileR
1000: 63 76 64 2b 2b 3b 0a 20 20 7d 0a 20 20 73 68 61  cvd++;.  }.  sha
1010: 31 73 75 6d 5f 62 6c 6f 62 28 26 63 6f 6e 74 65  1sum_blob(&conte
1020: 6e 74 2c 20 26 68 61 73 68 29 3b 0a 20 20 69 66  nt, &hash);.  if
1030: 28 20 21 62 6c 6f 62 5f 65 71 5f 73 74 72 28 26  ( !blob_eq_str(&
1040: 70 58 66 65 72 2d 3e 61 54 6f 6b 65 6e 5b 31 5d  pXfer->aToken[1]
1050: 2c 20 62 6c 6f 62 5f 73 74 72 28 26 68 61 73 68  , blob_str(&hash
1060: 29 2c 20 2d 31 29 20 29 7b 0a 20 20 20 20 62 6c  ), -1) ){.    bl
1070: 6f 62 5f 61 70 70 65 6e 64 66 28 26 70 58 66 65  ob_appendf(&pXfe
1080: 72 2d 3e 65 72 72 2c 20 22 63 6f 6e 74 65 6e 74  r->err, "content
1090: 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20   does not match 
10a0: 73 68 61 31 20 68 61 73 68 22 29 3b 0a 20 20 7d  sha1 hash");.  }
10b0: 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 68  .  blob_reset(&h
10c0: 61 73 68 29 3b 0a 20 20 72 69 64 20 3d 20 63 6f  ash);.  rid = co
10d0: 6e 74 65 6e 74 5f 70 75 74 28 26 63 6f 6e 74 65  ntent_put(&conte
10e0: 6e 74 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66 28  nt, 0, 0);.  if(
10f0: 20 72 69 64 3d 3d 30 20 29 7b 0a 20 20 20 20 62   rid==0 ){.    b
1100: 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 70 58 66  lob_appendf(&pXf
1110: 65 72 2d 3e 65 72 72 2c 20 22 25 73 22 2c 20 67  er->err, "%s", g
1120: 2e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 7d 65 6c  .zErrMsg);.  }el
1130: 73 65 7b 0a 20 20 20 20 6d 61 6e 69 66 65 73 74  se{.    manifest
1140: 5f 63 72 6f 73 73 6c 69 6e 6b 28 72 69 64 2c 20  _crosslink(rid, 
1150: 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 7d 0a 20  &content);.  }. 
1160: 20 72 65 6d 6f 74 65 5f 68 61 73 28 72 69 64 29   remote_has(rid)
1170: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74  ;.}../*.** Try t
1180: 6f 20 73 65 6e 64 20 61 20 66 69 6c 65 20 61 73  o send a file as
1190: 20 61 20 64 65 6c 74 61 2e 20 20 49 66 20 73 75   a delta.  If su
11a0: 63 63 65 73 73 66 75 6c 2c 20 72 65 74 75 72 6e  ccessful, return
11b0: 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f   the number.** o
11c0: 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 64  f bytes in the d
11d0: 65 6c 74 61 2e 20 20 49 66 20 6e 6f 74 2c 20 72  elta.  If not, r
11e0: 65 74 75 72 6e 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a  eturn zero..**.*
11f0: 2a 20 49 66 20 73 72 63 49 64 20 69 73 20 73 70  * If srcId is sp
1200: 65 63 69 66 69 65 64 2c 20 75 73 65 20 69 74 2e  ecified, use it.
1210: 20 20 49 66 20 6e 6f 74 2c 20 74 72 79 20 74 6f    If not, try to
1220: 20 66 69 67 75 72 65 20 6f 75 74 20 61 0a 2a 2a   figure out a.**
1230: 20 72 65 61 73 6f 6e 61 62 6c 65 20 73 72 63 49   reasonable srcI
1240: 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
1250: 20 73 65 6e 64 5f 61 73 5f 64 65 6c 74 61 28 0a   send_as_delta(.
1260: 20 20 58 66 65 72 20 2a 70 58 66 65 72 2c 20 20    Xfer *pXfer,  
1270: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1280: 20 74 72 61 6e 73 66 65 72 20 63 6f 6e 74 65 78   transfer contex
1290: 74 20 2a 2f 0a 20 20 69 6e 74 20 72 69 64 2c 20  t */.  int rid, 
12a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
12b0: 2a 20 72 65 63 6f 72 64 20 69 64 20 6f 66 20 74  * record id of t
12c0: 68 65 20 66 69 6c 65 20 74 6f 20 73 65 6e 64 20  he file to send 
12d0: 2a 2f 0a 20 20 42 6c 6f 62 20 2a 70 43 6f 6e 74  */.  Blob *pCont
12e0: 65 6e 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  ent,         /* 
12f0: 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74  The content of t
1300: 68 65 20 66 69 6c 65 20 74 6f 20 73 65 6e 64 20  he file to send 
1310: 2a 2f 0a 20 20 42 6c 6f 62 20 2a 70 55 75 69 64  */.  Blob *pUuid
1320: 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
1330: 54 68 65 20 55 55 49 44 20 6f 66 20 74 68 65 20  The UUID of the 
1340: 66 69 6c 65 20 74 6f 20 73 65 6e 64 20 2a 2f 0a  file to send */.
1350: 20 20 69 6e 74 20 73 72 63 49 64 20 20 20 20 20    int srcId     
1360: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 6e            /* Sen
1370: 64 20 61 73 20 61 20 64 65 6c 74 61 20 61 67 61  d as a delta aga
1380: 69 6e 73 74 20 74 68 69 73 20 72 65 63 6f 72 64  inst this record
1390: 20 2a 2f 0a 29 7b 0a 20 20 73 74 61 74 69 63 20   */.){.  static 
13a0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 7a 51 75  const char *azQu
13b0: 65 72 79 5b 5d 20 3d 20 7b 0a 20 20 20 20 22 53  ery[] = {.    "S
13c0: 45 4c 45 43 54 20 70 69 64 20 46 52 4f 4d 20 70  ELECT pid FROM p
13d0: 6c 69 6e 6b 22 0a 20 20 20 20 22 20 57 48 45 52  link".    " WHER
13e0: 45 20 63 69 64 3d 25 64 22 0a 20 20 20 20 22 20  E cid=%d".    " 
13f0: 20 20 41 4e 44 20 4e 4f 54 20 45 58 49 53 54 53    AND NOT EXISTS
1400: 28 53 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 70  (SELECT 1 FROM p
1410: 68 61 6e 74 6f 6d 20 57 48 45 52 45 20 72 69 64  hantom WHERE rid
1420: 3d 70 69 64 29 22 2c 0a 0a 20 20 20 20 22 53 45  =pid)",..    "SE
1430: 4c 45 43 54 20 70 69 64 20 46 52 4f 4d 20 6d 6c  LECT pid FROM ml
1440: 69 6e 6b 22 0a 20 20 20 20 22 20 57 48 45 52 45  ink".    " WHERE
1450: 20 66 69 64 3d 25 64 22 0a 20 20 20 20 22 20 20   fid=%d".    "  
1460: 20 41 4e 44 20 4e 4f 54 20 45 58 49 53 54 53 28   AND NOT EXISTS(
1470: 53 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 70 68  SELECT 1 FROM ph
1480: 61 6e 74 6f 6d 20 57 48 45 52 45 20 72 69 64 3d  antom WHERE rid=
1490: 70 69 64 29 22 2c 0a 20 20 7d 3b 0a 20 20 69 6e  pid)",.  };.  in
14a0: 74 20 69 3b 0a 20 20 42 6c 6f 62 20 73 72 63 2c  t i;.  Blob src,
14b0: 20 64 65 6c 74 61 3b 0a 20 20 69 6e 74 20 73 69   delta;.  int si
14c0: 7a 65 20 3d 20 30 3b 0a 0a 20 20 66 6f 72 28 69  ze = 0;..  for(i
14d0: 3d 30 3b 20 73 72 63 49 64 3d 3d 30 20 26 26 20  =0; srcId==0 && 
14e0: 69 3c 63 6f 75 6e 74 28 61 7a 51 75 65 72 79 29  i<count(azQuery)
14f0: 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 72 63 49  ; i++){.    srcI
1500: 64 20 3d 20 64 62 5f 69 6e 74 28 30 2c 20 61 7a  d = db_int(0, az
1510: 51 75 65 72 79 5b 69 5d 2c 20 72 69 64 29 3b 0a  Query[i], rid);.
1520: 20 20 7d 0a 20 20 69 66 28 20 73 72 63 49 64 3e    }.  if( srcId>
1530: 30 20 26 26 20 63 6f 6e 74 65 6e 74 5f 67 65 74  0 && content_get
1540: 28 73 72 63 49 64 2c 20 26 73 72 63 29 20 29 7b  (srcId, &src) ){
1550: 0a 20 20 20 20 63 68 61 72 20 2a 7a 55 75 69 64  .    char *zUuid
1560: 20 3d 20 64 62 5f 74 65 78 74 28 30 2c 20 22 53   = db_text(0, "S
1570: 45 4c 45 43 54 20 75 75 69 64 20 46 52 4f 4d 20  ELECT uuid FROM 
1580: 62 6c 6f 62 20 57 48 45 52 45 20 72 69 64 3d 25  blob WHERE rid=%
1590: 64 22 2c 20 73 72 63 49 64 29 3b 0a 20 20 20 20  d", srcId);.    
15a0: 62 6c 6f 62 5f 64 65 6c 74 61 5f 63 72 65 61 74  blob_delta_creat
15b0: 65 28 26 73 72 63 2c 20 70 43 6f 6e 74 65 6e 74  e(&src, pContent
15c0: 2c 20 26 64 65 6c 74 61 29 3b 0a 20 20 20 20 73  , &delta);.    s
15d0: 69 7a 65 20 3d 20 62 6c 6f 62 5f 73 69 7a 65 28  ize = blob_size(
15e0: 26 64 65 6c 74 61 29 3b 0a 20 20 20 20 69 66 28  &delta);.    if(
15f0: 20 73 69 7a 65 3e 3d 62 6c 6f 62 5f 73 69 7a 65   size>=blob_size
1600: 28 70 43 6f 6e 74 65 6e 74 29 2d 35 30 20 29 7b  (pContent)-50 ){
1610: 0a 20 20 20 20 20 20 73 69 7a 65 20 3d 20 30 3b  .      size = 0;
1620: 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1630: 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70    blob_appendf(p
1640: 58 66 65 72 2d 3e 70 4f 75 74 2c 20 22 66 69 6c  Xfer->pOut, "fil
1650: 65 20 25 62 20 25 73 20 25 64 5c 6e 22 2c 20 70  e %b %s %d\n", p
1660: 55 75 69 64 2c 20 7a 55 75 69 64 2c 20 73 69 7a  Uuid, zUuid, siz
1670: 65 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 61  e);.      blob_a
1680: 70 70 65 6e 64 28 70 58 66 65 72 2d 3e 70 4f 75  ppend(pXfer->pOu
1690: 74 2c 20 62 6c 6f 62 5f 62 75 66 66 65 72 28 26  t, blob_buffer(&
16a0: 64 65 6c 74 61 29 2c 20 73 69 7a 65 29 3b 0a 20  delta), size);. 
16b0: 20 20 20 20 20 2f 2a 20 62 6c 6f 62 5f 61 70 70       /* blob_app
16c0: 65 6e 64 66 28 70 58 66 65 72 2d 3e 70 4f 75 74  endf(pXfer->pOut
16d0: 2c 20 22 5c 6e 22 2c 20 31 29 3b 20 2a 2f 0a 20  , "\n", 1); */. 
16e0: 20 20 20 7d 0a 20 20 20 20 62 6c 6f 62 5f 72 65     }.    blob_re
16f0: 73 65 74 28 26 64 65 6c 74 61 29 3b 0a 20 20 20  set(&delta);.   
1700: 20 66 72 65 65 28 7a 55 75 69 64 29 3b 0a 20 20   free(zUuid);.  
1710: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 73 72    blob_reset(&sr
1720: 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  c);.  }.  return
1730: 20 73 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   size;.}../*.** 
1740: 53 65 6e 64 20 74 68 65 20 66 69 6c 65 20 69 64  Send the file id
1750: 65 6e 74 69 66 69 65 64 20 62 79 20 72 69 64 2e  entified by rid.
1760: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 55 75 69 64  .**.** The pUuid
1770: 20 63 61 6e 20 62 65 20 4e 55 4c 4c 20 69 6e 20   can be NULL in 
1780: 77 68 69 63 68 20 63 61 73 65 20 74 68 65 20 63  which case the c
1790: 6f 72 72 65 63 74 20 55 55 49 44 20 69 73 20 63  orrect UUID is c
17a0: 6f 6d 70 75 74 65 64 0a 2a 2a 20 66 72 6f 6d 20  omputed.** from 
17b0: 74 68 65 20 72 69 64 2e 0a 2a 2a 0a 2a 2a 20 49  the rid..**.** I
17c0: 66 20 73 72 63 49 64 20 69 73 20 70 6f 73 69 74  f srcId is posit
17d0: 69 76 65 2c 20 74 68 65 6e 20 61 20 64 65 6c 74  ive, then a delt
17e0: 61 20 69 73 20 73 65 6e 74 20 61 67 61 69 6e 73  a is sent agains
17f0: 74 20 74 68 61 74 20 73 72 63 49 64 2e 0a 2a 2a  t that srcId..**
1800: 20 49 66 20 73 72 63 49 64 20 69 73 20 7a 65 72   If srcId is zer
1810: 6f 2c 20 74 68 65 6e 20 61 6e 20 61 74 74 65 6d  o, then an attem
1820: 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 66 69  pt is made to fi
1830: 6e 64 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74  nd an appropriat
1840: 65 0a 2a 2a 20 66 69 6c 65 20 74 6f 20 64 65 6c  e.** file to del
1850: 74 61 20 61 67 61 69 6e 73 74 2e 20 20 20 49 66  ta against.   If
1860: 20 73 72 63 49 64 20 69 73 20 6e 65 67 61 74 69   srcId is negati
1870: 76 65 2c 20 74 68 65 20 66 69 6c 65 20 69 73 20  ve, the file is 
1880: 73 65 6e 74 0a 2a 2a 20 77 69 74 68 6f 75 74 20  sent.** without 
1890: 64 65 6c 74 61 69 6e 67 2e 0a 2a 2f 0a 73 74 61  deltaing..*/.sta
18a0: 74 69 63 20 76 6f 69 64 20 73 65 6e 64 5f 66 69  tic void send_fi
18b0: 6c 65 28 58 66 65 72 20 2a 70 58 66 65 72 2c 20  le(Xfer *pXfer, 
18c0: 69 6e 74 20 72 69 64 2c 20 42 6c 6f 62 20 2a 70  int rid, Blob *p
18d0: 55 75 69 64 2c 20 69 6e 74 20 73 72 63 49 64 29  Uuid, int srcId)
18e0: 7b 0a 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e 74  {.  Blob content
18f0: 2c 20 75 75 69 64 3b 0a 20 20 69 6e 74 20 73 69  , uuid;.  int si
1900: 7a 65 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 64  ze = 0;..  if( d
1910: 62 5f 65 78 69 73 74 73 28 22 53 45 4c 45 43 54  b_exists("SELECT
1920: 20 31 20 46 52 4f 4d 20 6f 6e 72 65 6d 6f 74 65   1 FROM onremote
1930: 20 57 48 45 52 45 20 72 69 64 3d 25 64 22 2c 20   WHERE rid=%d", 
1940: 72 69 64 29 20 29 7b 0a 20 20 20 20 20 72 65 74  rid) ){.     ret
1950: 75 72 6e 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f  urn;.  }.  blob_
1960: 7a 65 72 6f 28 26 75 75 69 64 29 3b 0a 20 20 69  zero(&uuid);.  i
1970: 66 28 20 70 55 75 69 64 3d 3d 30 20 29 7b 0a 20  f( pUuid==0 ){. 
1980: 20 20 20 64 62 5f 62 6c 6f 62 28 26 75 75 69 64     db_blob(&uuid
1990: 2c 20 22 53 45 4c 45 43 54 20 75 75 69 64 20 46  , "SELECT uuid F
19a0: 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 72  ROM blob WHERE r
19b0: 69 64 3d 25 64 20 41 4e 44 20 73 69 7a 65 3e 3d  id=%d AND size>=
19c0: 30 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 69 66  0", rid);.    if
19d0: 28 20 62 6c 6f 62 5f 73 69 7a 65 28 26 75 75 69  ( blob_size(&uui
19e0: 64 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  d)==0 ){.      r
19f0: 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20  eturn;.    }.   
1a00: 20 70 55 75 69 64 20 3d 20 26 75 75 69 64 3b 0a   pUuid = &uuid;.
1a10: 20 20 7d 0a 20 20 69 66 28 20 70 58 66 65 72 2d    }.  if( pXfer-
1a20: 3e 6d 78 53 65 6e 64 3c 3d 62 6c 6f 62 5f 73 69  >mxSend<=blob_si
1a30: 7a 65 28 70 58 66 65 72 2d 3e 70 4f 75 74 29 20  ze(pXfer->pOut) 
1a40: 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65  ){.    blob_appe
1a50: 6e 64 66 28 70 58 66 65 72 2d 3e 70 4f 75 74 2c  ndf(pXfer->pOut,
1a60: 20 22 69 67 6f 74 20 25 62 5c 6e 22 2c 20 70 55   "igot %b\n", pU
1a70: 75 69 64 29 3b 0a 20 20 20 20 70 58 66 65 72 2d  uid);.    pXfer-
1a80: 3e 6e 49 47 6f 74 53 65 6e 74 2b 2b 3b 0a 20 20  >nIGotSent++;.  
1a90: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 75 75    blob_reset(&uu
1aa0: 69 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  id);.    return;
1ab0: 0a 20 20 7d 0a 20 20 63 6f 6e 74 65 6e 74 5f 67  .  }.  content_g
1ac0: 65 74 28 72 69 64 2c 20 26 63 6f 6e 74 65 6e 74  et(rid, &content
1ad0: 29 3b 0a 0a 20 20 69 66 28 20 62 6c 6f 62 5f 73  );..  if( blob_s
1ae0: 69 7a 65 28 26 63 6f 6e 74 65 6e 74 29 3e 31 30  ize(&content)>10
1af0: 30 20 29 7b 0a 20 20 20 20 73 69 7a 65 20 3d 20  0 ){.    size = 
1b00: 73 65 6e 64 5f 61 73 5f 64 65 6c 74 61 28 70 58  send_as_delta(pX
1b10: 66 65 72 2c 20 72 69 64 2c 20 26 63 6f 6e 74 65  fer, rid, &conte
1b20: 6e 74 2c 20 70 55 75 69 64 2c 20 73 72 63 49 64  nt, pUuid, srcId
1b30: 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 69 7a  );.  }.  if( siz
1b40: 65 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  e==0 ){.    int 
1b50: 73 69 7a 65 20 3d 20 62 6c 6f 62 5f 73 69 7a 65  size = blob_size
1b60: 28 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 20 20  (&content);.    
1b70: 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70 58 66  blob_appendf(pXf
1b80: 65 72 2d 3e 70 4f 75 74 2c 20 22 66 69 6c 65 20  er->pOut, "file 
1b90: 25 62 20 25 64 5c 6e 22 2c 20 70 55 75 69 64 2c  %b %d\n", pUuid,
1ba0: 20 73 69 7a 65 29 3b 0a 20 20 20 20 62 6c 6f 62   size);.    blob
1bb0: 5f 61 70 70 65 6e 64 28 70 58 66 65 72 2d 3e 70  _append(pXfer->p
1bc0: 4f 75 74 2c 20 62 6c 6f 62 5f 62 75 66 66 65 72  Out, blob_buffer
1bd0: 28 26 63 6f 6e 74 65 6e 74 29 2c 20 73 69 7a 65  (&content), size
1be0: 29 3b 0a 20 20 20 20 70 58 66 65 72 2d 3e 6e 46  );.    pXfer->nF
1bf0: 69 6c 65 53 65 6e 74 2b 2b 3b 0a 20 20 7d 65 6c  ileSent++;.  }el
1c00: 73 65 7b 0a 20 20 20 20 70 58 66 65 72 2d 3e 6e  se{.    pXfer->n
1c10: 44 65 6c 74 61 53 65 6e 74 2b 2b 3b 0a 20 20 7d  DeltaSent++;.  }
1c20: 0a 20 20 72 65 6d 6f 74 65 5f 68 61 73 28 72 69  .  remote_has(ri
1c30: 64 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74  d);.  blob_reset
1c40: 28 26 75 75 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  (&uuid);.}../*.*
1c50: 2a 20 53 65 6e 64 20 74 68 65 20 66 69 6c 65 20  * Send the file 
1c60: 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 6d 69  identified by mi
1c70: 64 20 61 6e 64 20 70 55 75 69 64 2e 20 20 49 66  d and pUuid.  If
1c80: 20 74 68 61 74 20 66 69 6c 65 20 68 61 70 70 65   that file happe
1c90: 6e 73 0a 2a 2a 20 74 6f 20 62 65 20 61 20 6d 61  ns.** to be a ma
1ca0: 6e 69 66 65 73 74 2c 20 74 68 65 6e 20 61 6c 73  nifest, then als
1cb0: 6f 20 73 65 6e 64 20 61 6c 6c 20 6f 66 20 74 68  o send all of th
1cc0: 65 20 61 73 73 6f 63 69 61 74 65 64 20 63 6f 6e  e associated con
1cd0: 74 65 6e 74 0a 2a 2a 20 66 69 6c 65 73 20 66 6f  tent.** files fo
1ce0: 72 20 74 68 61 74 20 6d 61 6e 69 66 65 73 74 2e  r that manifest.
1cf0: 20 20 49 66 20 74 68 65 20 66 69 6c 65 20 69 73    If the file is
1d00: 20 6e 6f 74 20 61 20 6d 61 6e 69 66 65 73 74 2c   not a manifest,
1d10: 20 74 68 65 6e 20 74 68 69 73 0a 2a 2a 20 72 6f   then this.** ro
1d20: 75 74 69 6e 65 20 69 73 20 74 68 65 20 65 71 75  utine is the equ
1d30: 69 76 61 6c 65 6e 74 20 6f 66 20 73 65 6e 64 5f  ivalent of send_
1d40: 66 69 6c 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  file()..*/.stati
1d50: 63 20 76 6f 69 64 20 73 65 6e 64 5f 6d 61 6e 69  c void send_mani
1d60: 66 65 73 74 28 58 66 65 72 20 2a 70 58 66 65 72  fest(Xfer *pXfer
1d70: 2c 20 69 6e 74 20 6d 69 64 2c 20 42 6c 6f 62 20  , int mid, Blob 
1d80: 2a 70 55 75 69 64 2c 20 69 6e 74 20 73 72 63 49  *pUuid, int srcI
1d90: 64 29 7b 0a 20 20 53 74 6d 74 20 71 32 3b 0a 20  d){.  Stmt q2;. 
1da0: 20 73 65 6e 64 5f 66 69 6c 65 28 70 58 66 65 72   send_file(pXfer
1db0: 2c 20 6d 69 64 2c 20 70 55 75 69 64 2c 20 73 72  , mid, pUuid, sr
1dc0: 63 49 64 29 3b 0a 20 20 64 62 5f 70 72 65 70 61  cId);.  db_prepa
1dd0: 72 65 28 26 71 32 2c 0a 20 20 20 20 20 22 53 45  re(&q2,.     "SE
1de0: 4c 45 43 54 20 70 69 64 2c 20 75 75 69 64 2c 20  LECT pid, uuid, 
1df0: 66 69 64 20 46 52 4f 4d 20 6d 6c 69 6e 6b 2c 20  fid FROM mlink, 
1e00: 62 6c 6f 62 22 0a 20 20 20 20 20 22 20 57 48 45  blob".     " WHE
1e10: 52 45 20 72 69 64 3d 66 69 64 20 41 4e 44 20 6d  RE rid=fid AND m
1e20: 69 64 3d 25 64 22 2c 0a 20 20 20 20 20 6d 69 64  id=%d",.     mid
1e30: 0a 20 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 64  .  );.  while( d
1e40: 62 5f 73 74 65 70 28 26 71 32 29 3d 3d 53 51 4c  b_step(&q2)==SQL
1e50: 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 69  ITE_ROW ){.    i
1e60: 6e 74 20 70 69 64 2c 20 66 69 64 3b 0a 20 20 20  nt pid, fid;.   
1e70: 20 42 6c 6f 62 20 75 75 69 64 3b 0a 20 20 20 20   Blob uuid;.    
1e80: 70 69 64 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f  pid = db_column_
1e90: 69 6e 74 28 26 71 32 2c 20 30 29 3b 0a 20 20 20  int(&q2, 0);.   
1ea0: 20 64 62 5f 65 70 68 65 6d 65 72 61 6c 5f 62 6c   db_ephemeral_bl
1eb0: 6f 62 28 26 71 32 2c 20 31 2c 20 26 75 75 69 64  ob(&q2, 1, &uuid
1ec0: 29 3b 0a 20 20 20 20 66 69 64 20 3d 20 64 62 5f  );.    fid = db_
1ed0: 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 32 2c 20  column_int(&q2, 
1ee0: 32 29 3b 0a 20 20 20 20 73 65 6e 64 5f 66 69 6c  2);.    send_fil
1ef0: 65 28 70 58 66 65 72 2c 20 66 69 64 2c 20 26 75  e(pXfer, fid, &u
1f00: 75 69 64 2c 20 70 69 64 29 3b 0a 20 20 7d 0a 20  uid, pid);.  }. 
1f10: 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 32   db_finalize(&q2
1f20: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  );.}../*.** This
1f30: 20 72 6f 75 74 69 6e 65 20 72 75 6e 73 20 77 68   routine runs wh
1f40: 65 6e 20 65 69 74 68 65 72 20 63 6c 69 65 6e 74  en either client
1f50: 20 6f 72 20 73 65 72 76 65 72 20 69 73 20 6e 6f   or server is no
1f60: 74 69 66 69 65 64 20 74 68 61 74 0a 2a 2a 20 74  tified that.** t
1f70: 68 65 20 6f 74 68 65 72 20 73 69 64 65 20 74 68  he other side th
1f80: 69 6e 6b 73 20 72 69 64 20 69 73 20 61 20 6c 65  inks rid is a le
1f90: 61 66 20 6d 61 6e 69 66 65 73 74 2e 20 20 49 66  af manifest.  If
1fa0: 20 77 65 20 68 6f 6c 64 0a 2a 2a 20 63 68 69 6c   we hold.** chil
1fb0: 64 72 65 6e 20 6f 66 20 72 69 64 2c 20 74 68 65  dren of rid, the
1fc0: 6e 20 73 65 6e 64 20 74 68 65 6d 20 6f 76 65 72  n send them over
1fd0: 20 74 6f 20 74 68 65 20 6f 74 68 65 72 20 73 69   to the other si
1fe0: 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  de..*/.static vo
1ff0: 69 64 20 6c 65 61 66 5f 72 65 73 70 6f 6e 73 65  id leaf_response
2000: 28 58 66 65 72 20 2a 70 58 66 65 72 2c 20 69 6e  (Xfer *pXfer, in
2010: 74 20 72 69 64 29 7b 0a 20 20 53 74 6d 74 20 71  t rid){.  Stmt q
2020: 31 3b 0a 20 20 64 62 5f 70 72 65 70 61 72 65 28  1;.  db_prepare(
2030: 26 71 31 2c 0a 20 20 20 20 20 20 22 53 45 4c 45  &q1,.      "SELE
2040: 43 54 20 63 69 64 2c 20 75 75 69 64 20 46 52 4f  CT cid, uuid FRO
2050: 4d 20 70 6c 69 6e 6b 2c 20 62 6c 6f 62 22 0a 20  M plink, blob". 
2060: 20 20 20 20 20 22 20 57 48 45 52 45 20 62 6c 6f       " WHERE blo
2070: 62 2e 72 69 64 3d 70 6c 69 6e 6b 2e 63 69 64 22  b.rid=plink.cid"
2080: 0a 20 20 20 20 20 20 22 20 20 20 41 4e 44 20 70  .      "   AND p
2090: 6c 69 6e 6b 2e 70 69 64 3d 25 64 22 2c 0a 20 20  link.pid=%d",.  
20a0: 20 20 20 20 72 69 64 0a 20 20 29 3b 0a 20 20 77      rid.  );.  w
20b0: 68 69 6c 65 28 20 64 62 5f 73 74 65 70 28 26 71  hile( db_step(&q
20c0: 31 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  1)==SQLITE_ROW )
20d0: 7b 0a 20 20 20 20 42 6c 6f 62 20 75 75 69 64 3b  {.    Blob uuid;
20e0: 0a 20 20 20 20 69 6e 74 20 63 69 64 3b 0a 0a 20  .    int cid;.. 
20f0: 20 20 20 63 69 64 20 3d 20 64 62 5f 63 6f 6c 75     cid = db_colu
2100: 6d 6e 5f 69 6e 74 28 26 71 31 2c 20 30 29 3b 0a  mn_int(&q1, 0);.
2110: 20 20 20 20 64 62 5f 65 70 68 65 6d 65 72 61 6c      db_ephemeral
2120: 5f 62 6c 6f 62 28 26 71 31 2c 20 31 2c 20 26 75  _blob(&q1, 1, &u
2130: 75 69 64 29 3b 0a 20 20 20 20 73 65 6e 64 5f 6d  uid);.    send_m
2140: 61 6e 69 66 65 73 74 28 70 58 66 65 72 2c 20 63  anifest(pXfer, c
2150: 69 64 2c 20 26 75 75 69 64 2c 20 72 69 64 29 3b  id, &uuid, rid);
2160: 0a 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 73 69  .    if( blob_si
2170: 7a 65 28 70 58 66 65 72 2d 3e 70 4f 75 74 29 3c  ze(pXfer->pOut)<
2180: 70 58 66 65 72 2d 3e 6d 78 53 65 6e 64 20 29 7b  pXfer->mxSend ){
2190: 0a 20 20 20 20 20 20 6c 65 61 66 5f 72 65 73 70  .      leaf_resp
21a0: 6f 6e 73 65 28 70 58 66 65 72 2c 20 63 69 64 29  onse(pXfer, cid)
21b0: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  ;.    }.  }.}../
21c0: 2a 0a 2a 2a 20 53 65 6e 74 20 61 20 6c 65 61 66  *.** Sent a leaf
21d0: 20 6d 65 73 73 61 67 65 20 66 6f 72 20 65 76 65   message for eve
21e0: 72 79 20 6c 65 61 66 2e 0a 2a 2f 0a 73 74 61 74  ry leaf..*/.stat
21f0: 69 63 20 76 6f 69 64 20 73 65 6e 64 5f 6c 65 61  ic void send_lea
2200: 76 65 73 28 58 66 65 72 20 2a 70 58 66 65 72 29  ves(Xfer *pXfer)
2210: 7b 0a 20 20 53 74 6d 74 20 71 3b 0a 20 20 64 62  {.  Stmt q;.  db
2220: 5f 70 72 65 70 61 72 65 28 26 71 2c 20 0a 20 20  _prepare(&q, .  
2230: 20 20 22 53 45 4c 45 43 54 20 75 75 69 64 20 46    "SELECT uuid F
2240: 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 72  ROM blob WHERE r
2250: 69 64 20 49 4e 22 0a 20 20 20 20 22 20 20 28 53  id IN".    "  (S
2260: 45 4c 45 43 54 20 63 69 64 20 46 52 4f 4d 20 70  ELECT cid FROM p
2270: 6c 69 6e 6b 20 45 58 43 45 50 54 20 53 45 4c 45  link EXCEPT SELE
2280: 43 54 20 70 69 64 20 46 52 4f 4d 20 70 6c 69 6e  CT pid FROM plin
2290: 6b 29 22 0a 20 20 29 3b 0a 20 20 77 68 69 6c 65  k)".  );.  while
22a0: 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53  ( db_step(&q)==S
22b0: 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20  QLITE_ROW ){.   
22c0: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 55 75   const char *zUu
22d0: 69 64 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74  id = db_column_t
22e0: 65 78 74 28 26 71 2c 20 30 29 3b 0a 20 20 20 20  ext(&q, 0);.    
22f0: 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70 58 66  blob_appendf(pXf
2300: 65 72 2d 3e 70 4f 75 74 2c 20 22 6c 65 61 66 20  er->pOut, "leaf 
2310: 25 73 5c 6e 22 2c 20 7a 55 75 69 64 29 3b 0a 20  %s\n", zUuid);. 
2320: 20 7d 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65   }.  db_finalize
2330: 28 26 71 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  (&q);.}../*.** S
2340: 65 6e 74 20 6c 65 61 66 20 63 6f 6e 74 65 6e 74  ent leaf content
2350: 20 66 6f 72 20 65 76 65 72 79 20 6c 65 61 66 20   for every leaf 
2360: 74 68 61 74 20 69 73 20 6e 6f 74 20 66 6f 75 6e  that is not foun
2370: 64 20 69 6e 20 74 68 65 0a 2a 2a 20 6f 6e 72 65  d in the.** onre
2380: 6d 6f 74 65 20 74 61 62 6c 65 2e 20 20 54 68 69  mote table.  Thi
2390: 73 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f  s is intended to
23a0: 20 73 65 6e 64 20 6c 65 61 66 20 63 6f 6e 74 65   send leaf conte
23b0: 6e 74 20 66 6f 72 0a 2a 2a 20 65 76 65 72 79 20  nt for.** every 
23c0: 6c 65 61 66 20 74 68 61 74 20 69 73 20 75 6e 6b  leaf that is unk
23d0: 6e 6f 77 6e 20 6f 6e 20 74 68 65 20 72 65 6d 6f  nown on the remo
23e0: 74 65 20 65 6e 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e  te end..**.** In
23f0: 20 61 64 64 69 74 69 6f 6e 2c 20 77 65 20 6d 69   addition, we mi
2400: 67 68 74 20 73 65 6e 64 20 22 69 67 6f 74 22 20  ght send "igot" 
2410: 6d 65 73 73 61 67 65 73 20 66 6f 72 20 61 20 66  messages for a f
2420: 65 77 20 67 65 6e 65 72 61 74 69 6f 6e 73 20 6f  ew generations o
2430: 66 0a 2a 2a 20 70 61 72 65 6e 74 73 20 6f 66 20  f.** parents of 
2440: 74 68 65 20 75 6e 6b 6e 6f 77 6e 20 6c 65 61 76  the unknown leav
2450: 65 73 2e 20 20 54 68 69 73 20 77 69 6c 6c 20 73  es.  This will s
2460: 70 65 65 64 20 74 68 65 20 74 72 61 6e 73 6d 69  peed the transmi
2470: 73 73 69 6f 6e 0a 2a 2a 20 6f 66 20 6e 65 77 20  ssion.** of new 
2480: 62 72 61 6e 63 68 65 73 2e 20 20 0a 2a 2f 0a 73  branches.  .*/.s
2490: 74 61 74 69 63 20 76 6f 69 64 20 73 65 6e 64 5f  tatic void send_
24a0: 75 6e 6b 6e 6f 77 6e 5f 6c 65 61 66 5f 63 6f 6e  unknown_leaf_con
24b0: 74 65 6e 74 28 58 66 65 72 20 2a 70 58 66 65 72  tent(Xfer *pXfer
24c0: 29 7b 0a 20 20 53 74 6d 74 20 71 31 3b 0a 20 20  ){.  Stmt q1;.  
24d0: 64 62 5f 70 72 65 70 61 72 65 28 26 71 31 2c 0a  db_prepare(&q1,.
24e0: 20 20 20 20 22 53 45 4c 45 43 54 20 72 69 64 2c      "SELECT rid,
24f0: 20 75 75 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20   uuid FROM blob 
2500: 57 48 45 52 45 20 72 69 64 20 49 4e 22 0a 20 20  WHERE rid IN".  
2510: 20 20 22 20 20 28 53 45 4c 45 43 54 20 63 69 64    "  (SELECT cid
2520: 20 46 52 4f 4d 20 70 6c 69 6e 6b 20 45 58 43 45   FROM plink EXCE
2530: 50 54 20 53 45 4c 45 43 54 20 70 69 64 20 46 52  PT SELECT pid FR
2540: 4f 4d 20 70 6c 69 6e 6b 29 22 0a 20 20 20 20 22  OM plink)".    "
2550: 20 20 41 4e 44 20 4e 4f 54 20 45 58 49 53 54 53    AND NOT EXISTS
2560: 28 53 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 6f  (SELECT 1 FROM o
2570: 6e 72 65 6d 6f 74 65 20 57 48 45 52 45 20 72 69  nremote WHERE ri
2580: 64 3d 62 6c 6f 62 2e 72 69 64 29 22 0a 20 20 29  d=blob.rid)".  )
2590: 3b 0a 20 20 77 68 69 6c 65 28 20 64 62 5f 73 74  ;.  while( db_st
25a0: 65 70 28 26 71 31 29 3d 3d 53 51 4c 49 54 45 5f  ep(&q1)==SQLITE_
25b0: 52 4f 57 20 29 7b 0a 20 20 20 20 42 6c 6f 62 20  ROW ){.    Blob 
25c0: 75 75 69 64 3b 0a 20 20 20 20 69 6e 74 20 63 69  uuid;.    int ci
25d0: 64 3b 0a 0a 20 20 20 20 63 69 64 20 3d 20 64 62  d;..    cid = db
25e0: 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 31 2c  _column_int(&q1,
25f0: 20 30 29 3b 0a 20 20 20 20 64 62 5f 65 70 68 65   0);.    db_ephe
2600: 6d 65 72 61 6c 5f 62 6c 6f 62 28 26 71 31 2c 20  meral_blob(&q1, 
2610: 31 2c 20 26 75 75 69 64 29 3b 0a 20 20 20 20 73  1, &uuid);.    s
2620: 65 6e 64 5f 6d 61 6e 69 66 65 73 74 28 70 58 66  end_manifest(pXf
2630: 65 72 2c 20 63 69 64 2c 20 26 75 75 69 64 2c 20  er, cid, &uuid, 
2640: 30 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 66 69 6e  0);.  }.  db_fin
2650: 61 6c 69 7a 65 28 26 71 31 29 3b 0a 7d 0a 0a 2f  alize(&q1);.}../
2660: 2a 0a 2a 2a 20 53 65 6e 20 61 20 67 69 6d 6d 65  *.** Sen a gimme
2670: 20 6d 65 73 73 61 67 65 20 66 6f 72 20 65 76 65   message for eve
2680: 72 79 20 70 68 61 6e 74 6f 6d 2e 0a 2a 2f 0a 73  ry phantom..*/.s
2690: 74 61 74 69 63 20 76 6f 69 64 20 72 65 71 75 65  tatic void reque
26a0: 73 74 5f 70 68 61 6e 74 6f 6d 73 28 58 66 65 72  st_phantoms(Xfer
26b0: 20 2a 70 58 66 65 72 29 7b 0a 20 20 53 74 6d 74   *pXfer){.  Stmt
26c0: 20 71 3b 0a 20 20 64 62 5f 70 72 65 70 61 72 65   q;.  db_prepare
26d0: 28 26 71 2c 20 22 53 45 4c 45 43 54 20 75 75 69  (&q, "SELECT uui
26e0: 64 20 46 52 4f 4d 20 70 68 61 6e 74 6f 6d 20 4a  d FROM phantom J
26f0: 4f 49 4e 20 62 6c 6f 62 20 55 53 49 4e 47 28 72  OIN blob USING(r
2700: 69 64 29 22 29 3b 0a 20 20 77 68 69 6c 65 28 20  id)");.  while( 
2710: 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53 51 4c  db_step(&q)==SQL
2720: 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 63  ITE_ROW ){.    c
2730: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 55 75 69 64  onst char *zUuid
2740: 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78   = db_column_tex
2750: 74 28 26 71 2c 20 30 29 3b 0a 20 20 20 20 62 6c  t(&q, 0);.    bl
2760: 6f 62 5f 61 70 70 65 6e 64 66 28 70 58 66 65 72  ob_appendf(pXfer
2770: 2d 3e 70 4f 75 74 2c 20 22 67 69 6d 6d 65 20 25  ->pOut, "gimme %
2780: 73 5c 6e 22 2c 20 7a 55 75 69 64 29 3b 0a 20 20  s\n", zUuid);.  
2790: 20 20 70 58 66 65 72 2d 3e 6e 47 69 6d 6d 65 53    pXfer->nGimmeS
27a0: 65 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 20 64 62 5f  ent++;.  }.  db_
27b0: 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 7d 0a  finalize(&q);.}.
27c0: 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68  ../*.** Check th
27d0: 65 20 73 69 67 6e 61 74 75 72 65 20 6f 6e 20 61  e signature on a
27e0: 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2f 78 2d  n application/x-
27f0: 66 6f 73 73 69 6c 20 70 61 79 6c 6f 61 64 20 72  fossil payload r
2800: 65 63 65 69 76 65 64 20 62 79 0a 2a 2a 20 74 68  eceived by.** th
2810: 65 20 48 54 54 50 20 73 65 72 76 65 72 2e 20 20  e HTTP server.  
2820: 54 68 65 20 73 69 67 6e 61 74 75 72 65 20 69 73  The signature is
2830: 20 61 20 6c 69 6e 65 20 6f 66 20 74 68 65 20 66   a line of the f
2840: 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 3a 0a 2a  ollowing form:.*
2850: 2a 0a 2a 2a 20 20 20 20 20 20 20 20 6c 6f 67 69  *.**        logi
2860: 6e 20 4c 4f 47 49 4e 20 4e 4f 4e 43 45 20 53 49  n LOGIN NONCE SI
2870: 47 4e 41 54 55 52 45 0a 2a 2a 0a 2a 2a 20 54 68  GNATURE.**.** Th
2880: 65 20 4e 4f 4e 43 45 20 69 73 20 61 20 72 61 6e  e NONCE is a ran
2890: 64 6f 6d 20 73 74 72 69 6e 67 2e 20 20 54 68 65  dom string.  The
28a0: 20 73 65 72 76 65 72 20 77 69 6c 6c 20 6e 65 76   server will nev
28b0: 65 72 20 61 63 63 65 70 74 20 61 0a 2a 2a 20 72  er accept a.** r
28c0: 65 70 65 61 74 20 4e 4f 4e 43 45 2e 20 20 53 49  epeat NONCE.  SI
28d0: 47 4e 41 54 55 52 45 20 69 73 20 74 68 65 20 53  GNATURE is the S
28e0: 48 41 31 20 63 68 65 63 6b 73 75 6d 20 6f 66 20  HA1 checksum of 
28f0: 74 68 65 20 4e 4f 4e 43 45 20 0a 2a 2a 20 63 6f  the NONCE .** co
2900: 6e 63 61 74 65 6e 61 74 65 64 20 77 69 74 68 20  ncatenated with 
2910: 74 68 65 20 75 73 65 72 73 20 70 61 73 73 77 6f  the users passwo
2920: 72 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61  rd..**.** The pa
2930: 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73  rameters to this
2940: 20 72 6f 75 74 69 6e 65 20 61 72 65 20 65 70 68   routine are eph
2950: 65 72 6d 65 72 61 6c 20 62 6c 6f 62 73 20 68 6f  ermeral blobs ho
2960: 6c 64 69 6e 67 20 74 68 65 0a 2a 2a 20 4c 4f 47  lding the.** LOG
2970: 49 4e 2c 20 4e 4f 4e 43 45 20 61 6e 64 20 53 49  IN, NONCE and SI
2980: 47 4e 41 54 55 52 45 2e 0a 2a 2a 0a 2a 2a 20 54  GNATURE..**.** T
2990: 68 69 73 20 72 6f 75 74 69 6e 65 20 61 74 74 65  his routine atte
29a0: 6d 70 74 73 20 74 6f 20 6c 6f 63 61 74 65 20 74  mpts to locate t
29b0: 68 65 20 75 73 65 72 20 61 6e 64 20 76 65 72 69  he user and veri
29c0: 66 79 20 74 68 65 20 73 69 67 6e 61 74 75 72 65  fy the signature
29d0: 2e 0a 2a 2a 20 49 66 20 65 76 65 72 79 74 68 69  ..** If everythi
29e0: 6e 67 20 63 68 65 63 6b 73 20 6f 75 74 2c 20 74  ng checks out, t
29f0: 68 65 20 55 53 45 52 2e 43 41 50 20 63 6f 6c 75  he USER.CAP colu
2a00: 6d 6e 20 66 6f 72 20 74 68 65 20 55 53 45 52 20  mn for the USER 
2a10: 74 61 62 6c 65 0a 2a 2a 20 69 73 20 63 6f 6e 73  table.** is cons
2a20: 75 6c 74 65 64 20 74 6f 20 73 65 74 20 70 72 69  ulted to set pri
2a30: 76 69 6c 65 67 65 73 20 69 6e 20 74 68 65 20 67  vileges in the g
2a40: 6c 6f 62 61 6c 20 67 20 76 61 72 69 61 62 6c 65  lobal g variable
2a50: 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 74 68  ..**.** If anyth
2a60: 69 6e 67 20 66 61 69 6c 73 20 74 6f 20 63 68 65  ing fails to che
2a70: 63 6b 20 6f 75 74 2c 20 6e 6f 20 63 68 61 6e 67  ck out, no chang
2a80: 65 73 20 61 72 65 20 6d 61 64 65 20 74 6f 20 70  es are made to p
2a90: 72 69 76 69 6c 65 67 65 73 2e 0a 2a 2a 0a 2a 2a  rivileges..**.**
2aa0: 20 53 69 67 6e 61 74 75 72 65 20 67 65 6e 65 72   Signature gener
2ab0: 61 74 69 6f 6e 20 6f 6e 20 74 68 65 20 63 6c 69  ation on the cli
2ac0: 65 6e 74 20 73 69 64 65 20 69 73 20 68 61 6e 64  ent side is hand
2ad0: 6c 65 64 20 62 79 20 74 68 65 20 0a 2a 2a 20 68  led by the .** h
2ae0: 74 74 70 5f 65 78 63 68 61 6e 67 65 28 29 20 72  ttp_exchange() r
2af0: 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 76 6f 69 64 20  outine..*/.void 
2b00: 63 68 65 63 6b 5f 6c 6f 67 69 6e 28 42 6c 6f 62  check_login(Blob
2b10: 20 2a 70 4c 6f 67 69 6e 2c 20 42 6c 6f 62 20 2a   *pLogin, Blob *
2b20: 70 4e 6f 6e 63 65 2c 20 42 6c 6f 62 20 2a 70 53  pNonce, Blob *pS
2b30: 69 67 29 7b 0a 20 20 53 74 6d 74 20 71 3b 0a 20  ig){.  Stmt q;. 
2b40: 20 69 6e 74 20 72 63 3b 0a 0a 20 20 69 66 28 20   int rc;..  if( 
2b50: 64 62 5f 65 78 69 73 74 73 28 22 53 45 4c 45 43  db_exists("SELEC
2b60: 54 20 31 20 46 52 4f 4d 20 72 63 76 66 72 6f 6d  T 1 FROM rcvfrom
2b70: 20 57 48 45 52 45 20 6e 6f 6e 63 65 3d 25 42 22   WHERE nonce=%B"
2b80: 2c 20 70 4e 6f 6e 63 65 29 20 29 7b 0a 20 20 20  , pNonce) ){.   
2b90: 20 72 65 74 75 72 6e 3b 20 20 2f 2a 20 4e 65 76   return;  /* Nev
2ba0: 65 72 20 61 63 63 65 70 74 20 61 20 72 65 70 65  er accept a repe
2bb0: 61 74 65 64 20 6e 6f 6e 63 65 20 2a 2f 0a 20 20  ated nonce */.  
2bc0: 7d 0a 20 20 64 62 5f 70 72 65 70 61 72 65 28 26  }.  db_prepare(&
2bd0: 71 2c 20 22 53 45 4c 45 43 54 20 70 77 2c 20 63  q, "SELECT pw, c
2be0: 61 70 2c 20 75 69 64 20 46 52 4f 4d 20 75 73 65  ap, uid FROM use
2bf0: 72 20 57 48 45 52 45 20 6c 6f 67 69 6e 3d 25 42  r WHERE login=%B
2c00: 22 2c 20 70 4c 6f 67 69 6e 29 3b 0a 20 20 69 66  ", pLogin);.  if
2c10: 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53  ( db_step(&q)==S
2c20: 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20  QLITE_ROW ){.   
2c30: 20 42 6c 6f 62 20 70 77 2c 20 63 6f 6d 62 69 6e   Blob pw, combin
2c40: 65 64 2c 20 68 61 73 68 3b 0a 20 20 20 20 62 6c  ed, hash;.    bl
2c50: 6f 62 5f 7a 65 72 6f 28 26 70 77 29 3b 0a 20 20  ob_zero(&pw);.  
2c60: 20 20 64 62 5f 65 70 68 65 6d 65 72 61 6c 5f 62    db_ephemeral_b
2c70: 6c 6f 62 28 26 71 2c 20 30 2c 20 26 70 77 29 3b  lob(&q, 0, &pw);
2c80: 0a 20 20 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26  .    blob_zero(&
2c90: 63 6f 6d 62 69 6e 65 64 29 3b 0a 20 20 20 20 62  combined);.    b
2ca0: 6c 6f 62 5f 63 6f 70 79 28 26 63 6f 6d 62 69 6e  lob_copy(&combin
2cb0: 65 64 2c 20 70 4e 6f 6e 63 65 29 3b 0a 20 20 20  ed, pNonce);.   
2cc0: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 28 26 63 6f   blob_append(&co
2cd0: 6d 62 69 6e 65 64 2c 20 62 6c 6f 62 5f 62 75 66  mbined, blob_buf
2ce0: 66 65 72 28 26 70 77 29 2c 20 62 6c 6f 62 5f 73  fer(&pw), blob_s
2cf0: 69 7a 65 28 26 70 77 29 29 3b 0a 20 20 20 20 2f  ize(&pw));.    /
2d00: 2a 20 43 47 49 44 45 42 55 47 28 28 22 70 72 65  * CGIDEBUG(("pre
2d10: 73 69 67 3d 5b 25 73 5d 5c 6e 22 2c 20 62 6c 6f  sig=[%s]\n", blo
2d20: 62 5f 73 74 72 28 26 63 6f 6d 62 69 6e 65 64 29  b_str(&combined)
2d30: 29 29 3b 20 2a 2f 0a 20 20 20 20 73 68 61 31 73  )); */.    sha1s
2d40: 75 6d 5f 62 6c 6f 62 28 26 63 6f 6d 62 69 6e 65  um_blob(&combine
2d50: 64 2c 20 26 68 61 73 68 29 3b 0a 20 20 20 20 72  d, &hash);.    r
2d60: 63 20 3d 20 62 6c 6f 62 5f 63 6f 6d 70 61 72 65  c = blob_compare
2d70: 28 26 68 61 73 68 2c 20 70 53 69 67 29 3b 0a 20  (&hash, pSig);. 
2d80: 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 68     blob_reset(&h
2d90: 61 73 68 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 72  ash);.    blob_r
2da0: 65 73 65 74 28 26 63 6f 6d 62 69 6e 65 64 29 3b  eset(&combined);
2db0: 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 30 20 29  .    if( rc==0 )
2dc0: 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  {.      const ch
2dd0: 61 72 20 2a 7a 43 61 70 3b 0a 20 20 20 20 20 20  ar *zCap;.      
2de0: 7a 43 61 70 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e  zCap = db_column
2df0: 5f 74 65 78 74 28 26 71 2c 20 31 29 3b 0a 20 20  _text(&q, 1);.  
2e00: 20 20 20 20 6c 6f 67 69 6e 5f 73 65 74 5f 63 61      login_set_ca
2e10: 70 61 62 69 6c 69 74 69 65 73 28 7a 43 61 70 29  pabilities(zCap)
2e20: 3b 0a 20 20 20 20 20 20 67 2e 75 73 65 72 55 69  ;.      g.userUi
2e30: 64 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e  d = db_column_in
2e40: 74 28 26 71 2c 20 32 29 3b 0a 20 20 20 20 20 20  t(&q, 2);.      
2e50: 67 2e 7a 4c 6f 67 69 6e 20 3d 20 6d 70 72 69 6e  g.zLogin = mprin
2e60: 74 66 28 22 25 62 22 2c 20 70 4c 6f 67 69 6e 29  tf("%b", pLogin)
2e70: 3b 0a 20 20 20 20 20 20 67 2e 7a 4e 6f 6e 63 65  ;.      g.zNonce
2e80: 20 3d 20 6d 70 72 69 6e 74 66 28 22 25 62 22 2c   = mprintf("%b",
2e90: 20 70 4e 6f 6e 63 65 29 3b 0a 20 20 20 20 7d 0a   pNonce);.    }.
2ea0: 20 20 7d 0a 20 20 64 62 5f 72 65 73 65 74 28 26    }.  db_reset(&
2eb0: 71 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66  q);.}.../*.** If
2ec0: 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 69   this variable i
2ed0: 73 20 73 65 74 2c 20 64 69 73 61 62 6c 65 20 6c  s set, disable l
2ee0: 6f 67 69 6e 20 63 68 65 63 6b 73 2e 20 20 55 73  ogin checks.  Us
2ef0: 65 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67  ed for debugging
2f00: 0a 2a 2a 20 6f 6e 6c 79 2e 0a 2a 2f 0a 73 74 61  .** only..*/.sta
2f10: 74 69 63 20 69 6e 74 20 64 69 73 61 62 6c 65 4c  tic int disableL
2f20: 6f 67 69 6e 20 3d 20 30 3b 0a 0a 2f 2a 0a 2a 2a  ogin = 0;../*.**
2f30: 20 57 45 42 50 41 47 45 3a 20 78 66 65 72 0a 2a   WEBPAGE: xfer.*
2f40: 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65  *.** This is the
2f50: 20 74 72 61 6e 73 66 65 72 20 68 61 6e 64 6c 65   transfer handle
2f60: 72 20 6f 6e 20 74 68 65 20 73 65 72 76 65 72 20  r on the server 
2f70: 73 69 64 65 2e 20 20 54 68 65 20 74 72 61 6e 73  side.  The trans
2f80: 66 65 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 68  fer.** message h
2f90: 61 73 20 62 65 65 6e 20 75 6e 63 6f 6d 70 72 65  as been uncompre
2fa0: 73 73 65 64 20 61 6e 64 20 70 6c 61 63 65 64 20  ssed and placed 
2fb0: 69 6e 20 74 68 65 20 67 2e 63 67 69 49 6e 20 62  in the g.cgiIn b
2fc0: 6c 6f 62 2e 0a 2a 2a 20 50 72 6f 63 65 73 73 20  lob..** Process 
2fd0: 74 68 69 73 20 6d 65 73 73 61 67 65 20 61 6e 64  this message and
2fe0: 20 66 6f 72 6d 20 61 6e 20 61 70 70 72 6f 70 72   form an appropr
2ff0: 69 61 74 65 20 72 65 70 6c 79 2e 0a 2a 2f 0a 76  iate reply..*/.v
3000: 6f 69 64 20 70 61 67 65 5f 78 66 65 72 28 76 6f  oid page_xfer(vo
3010: 69 64 29 7b 0a 20 20 69 6e 74 20 69 73 50 75 6c  id){.  int isPul
3020: 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 73 50  l = 0;.  int isP
3030: 75 73 68 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e  ush = 0;.  int n
3040: 45 72 72 20 3d 20 30 3b 0a 20 20 58 66 65 72 20  Err = 0;.  Xfer 
3050: 78 66 65 72 3b 0a 0a 20 20 6d 65 6d 73 65 74 28  xfer;..  memset(
3060: 26 78 66 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66  &xfer, 0, sizeof
3070: 28 78 66 65 72 29 29 3b 0a 20 20 62 6c 6f 62 61  (xfer));.  bloba
3080: 72 72 61 79 5f 7a 65 72 6f 28 78 66 65 72 2e 61  rray_zero(xfer.a
3090: 54 6f 6b 65 6e 2c 20 63 6f 75 6e 74 28 78 66 65  Token, count(xfe
30a0: 72 2e 61 54 6f 6b 65 6e 29 29 3b 0a 20 20 63 67  r.aToken));.  cg
30b0: 69 5f 73 65 74 5f 63 6f 6e 74 65 6e 74 5f 74 79  i_set_content_ty
30c0: 70 65 28 67 2e 7a 43 6f 6e 74 65 6e 74 54 79 70  pe(g.zContentTyp
30d0: 65 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28  e);.  blob_zero(
30e0: 26 78 66 65 72 2e 65 72 72 29 3b 0a 20 20 78 66  &xfer.err);.  xf
30f0: 65 72 2e 70 49 6e 20 3d 20 26 67 2e 63 67 69 49  er.pIn = &g.cgiI
3100: 6e 3b 0a 20 20 78 66 65 72 2e 70 4f 75 74 20 3d  n;.  xfer.pOut =
3110: 20 63 67 69 5f 6f 75 74 70 75 74 5f 62 6c 6f 62   cgi_output_blob
3120: 28 29 3b 0a 20 20 78 66 65 72 2e 6d 78 53 65 6e  ();.  xfer.mxSen
3130: 64 20 3d 20 64 62 5f 67 65 74 5f 69 6e 74 28 22  d = db_get_int("
3140: 6d 61 78 2d 64 6f 77 6e 6c 6f 61 64 22 2c 20 31  max-download", 1
3150: 30 30 30 30 30 30 29 3b 0a 0a 20 20 64 62 5f 62  000000);..  db_b
3160: 65 67 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f 6e  egin_transaction
3170: 28 29 3b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65  ();.  db_multi_e
3180: 78 65 63 28 0a 20 20 20 20 20 22 43 52 45 41 54  xec(.     "CREAT
3190: 45 20 54 45 4d 50 20 54 41 42 4c 45 20 6f 6e 72  E TEMP TABLE onr
31a0: 65 6d 6f 74 65 28 72 69 64 20 49 4e 54 45 47 45  emote(rid INTEGE
31b0: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 29 3b 22  R PRIMARY KEY);"
31c0: 0a 20 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 62  .  );.  while( b
31d0: 6c 6f 62 5f 6c 69 6e 65 28 78 66 65 72 2e 70 49  lob_line(xfer.pI
31e0: 6e 2c 20 26 78 66 65 72 2e 6c 69 6e 65 29 20 29  n, &xfer.line) )
31f0: 7b 0a 20 20 20 20 78 66 65 72 2e 6e 54 6f 6b 65  {.    xfer.nToke
3200: 6e 20 3d 20 62 6c 6f 62 5f 74 6f 6b 65 6e 69 7a  n = blob_tokeniz
3210: 65 28 26 78 66 65 72 2e 6c 69 6e 65 2c 20 78 66  e(&xfer.line, xf
3220: 65 72 2e 61 54 6f 6b 65 6e 2c 20 63 6f 75 6e 74  er.aToken, count
3230: 28 78 66 65 72 2e 61 54 6f 6b 65 6e 29 29 3b 0a  (xfer.aToken));.
3240: 0a 20 20 20 20 2f 2a 20 20 20 66 69 6c 65 20 55  .    /*   file U
3250: 55 49 44 20 53 49 5a 45 20 5c 6e 20 43 4f 4e 54  UID SIZE \n CONT
3260: 45 4e 54 0a 20 20 20 20 2a 2a 20 20 20 66 69 6c  ENT.    **   fil
3270: 65 20 55 55 49 44 20 44 45 4c 54 41 53 52 43 20  e UUID DELTASRC 
3280: 53 49 5a 45 20 5c 6e 20 43 4f 4e 54 45 4e 54 0a  SIZE \n CONTENT.
3290: 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 63      **.    ** Ac
32a0: 63 65 70 74 20 61 20 66 69 6c 65 20 66 72 6f 6d  cept a file from
32b0: 20 74 68 65 20 63 6c 69 65 6e 74 2e 0a 20 20 20   the client..   
32c0: 20 2a 2f 0a 20 20 20 20 69 66 28 20 62 6c 6f 62   */.    if( blob
32d0: 5f 65 71 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e  _eq(&xfer.aToken
32e0: 5b 30 5d 2c 20 22 66 69 6c 65 22 29 20 29 7b 0a  [0], "file") ){.
32f0: 20 20 20 20 20 20 69 66 28 20 21 69 73 50 75 73        if( !isPus
3300: 68 20 29 7b 0a 20 20 20 20 20 20 20 20 63 67 69  h ){.        cgi
3310: 5f 72 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29  _reset_content()
3320: 3b 0a 20 20 20 20 20 20 20 20 40 20 65 72 72 6f  ;.        @ erro
3330: 72 20 6e 6f 74 5c 73 61 75 74 68 6f 72 69 7a 65  r not\sauthorize
3340: 64 5c 73 74 6f 5c 73 77 72 69 74 65 0a 20 20 20  d\sto\swrite.   
3350: 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20       nErr++;.   
3360: 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3370: 20 20 7d 0a 20 20 20 20 20 20 78 66 65 72 5f 61    }.      xfer_a
3380: 63 63 65 70 74 5f 66 69 6c 65 28 26 78 66 65 72  ccept_file(&xfer
3390: 29 3b 0a 20 20 20 20 20 20 69 66 28 20 62 6c 6f  );.      if( blo
33a0: 62 5f 73 69 7a 65 28 26 78 66 65 72 2e 65 72 72  b_size(&xfer.err
33b0: 29 20 29 7b 0a 20 20 20 20 20 20 20 20 63 67 69  ) ){.        cgi
33c0: 5f 72 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29  _reset_content()
33d0: 3b 0a 20 20 20 20 20 20 20 20 40 20 65 72 72 6f  ;.        @ erro
33e0: 72 20 25 54 28 62 6c 6f 62 5f 73 74 72 28 26 78  r %T(blob_str(&x
33f0: 66 65 72 2e 65 72 72 29 29 0a 20 20 20 20 20 20  fer.err)).      
3400: 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20    nErr++;.      
3410: 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
3420: 0a 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20 20  .    }else..    
3430: 2f 2a 20 20 20 67 69 6d 6d 65 20 55 55 49 44 0a  /*   gimme UUID.
3440: 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 6c      **.    ** Cl
3450: 69 65 6e 74 20 69 73 20 72 65 71 75 65 73 74 69  ient is requesti
3460: 6e 67 20 61 20 66 69 6c 65 2e 20 20 49 66 20 74  ng a file.  If t
3470: 68 65 20 66 69 6c 65 20 69 73 20 61 20 6d 61 6e  he file is a man
3480: 69 66 65 73 74 2c 0a 20 20 20 20 2a 2a 20 74 68  ifest,.    ** th
3490: 65 20 73 65 72 76 65 72 20 63 61 6e 20 61 73 73  e server can ass
34a0: 75 6d 65 20 74 68 61 74 20 74 68 65 20 63 6c 69  ume that the cli
34b0: 65 6e 74 20 61 6c 73 6f 20 6e 65 65 64 73 20 61  ent also needs a
34c0: 6c 6c 20 63 6f 6e 74 65 6e 74 0a 20 20 20 20 2a  ll content.    *
34d0: 2a 20 66 69 6c 65 73 20 61 73 73 6f 63 69 61 74  * files associat
34e0: 65 64 20 77 69 74 68 20 74 68 61 74 20 6d 61 6e  ed with that man
34f0: 69 66 65 73 74 2e 0a 20 20 20 20 2a 2f 0a 20 20  ifest..    */.  
3500: 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 78    if( blob_eq(&x
3510: 66 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22  fer.aToken[0], "
3520: 67 69 6d 6d 65 22 29 0a 20 20 20 20 20 26 26 20  gimme").     && 
3530: 78 66 65 72 2e 6e 54 6f 6b 65 6e 3d 3d 32 0a 20  xfer.nToken==2. 
3540: 20 20 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75      && blob_is_u
3550: 75 69 64 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e  uid(&xfer.aToken
3560: 5b 31 5d 29 0a 20 20 20 20 29 7b 0a 20 20 20 20  [1]).    ){.    
3570: 20 20 69 66 28 20 69 73 50 75 6c 6c 20 29 7b 0a    if( isPull ){.
3580: 20 20 20 20 20 20 20 20 69 6e 74 20 72 69 64 20          int rid 
3590: 3d 20 72 69 64 5f 66 72 6f 6d 5f 75 75 69 64 28  = rid_from_uuid(
35a0: 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d 2c  &xfer.aToken[1],
35b0: 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28   0);.        if(
35c0: 20 72 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20   rid ){.        
35d0: 20 20 73 65 6e 64 5f 6d 61 6e 69 66 65 73 74 28    send_manifest(
35e0: 26 78 66 65 72 2c 20 72 69 64 2c 20 26 78 66 65  &xfer, rid, &xfe
35f0: 72 2e 61 54 6f 6b 65 6e 5b 31 5d 2c 20 30 29 3b  r.aToken[1], 0);
3600: 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
3610: 20 7d 0a 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20   }.    }else..  
3620: 20 20 2f 2a 20 20 20 69 67 6f 74 20 55 55 49 44    /*   igot UUID
3630: 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43  .    **.    ** C
3640: 6c 69 65 6e 74 20 61 6e 6e 6f 75 6e 63 65 73 20  lient announces 
3650: 74 68 61 74 20 69 74 20 68 61 73 20 61 20 70 61  that it has a pa
3660: 72 74 69 63 75 6c 61 72 20 66 69 6c 65 2e 0a 20  rticular file.. 
3670: 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 78 66     */.    if( xf
3680: 65 72 2e 6e 54 6f 6b 65 6e 3d 3d 32 0a 20 20 20  er.nToken==2.   
3690: 20 20 26 26 20 62 6c 6f 62 5f 65 71 28 26 78 66    && blob_eq(&xf
36a0: 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 69  er.aToken[0], "i
36b0: 67 6f 74 22 29 0a 20 20 20 20 20 26 26 20 62 6c  got").     && bl
36c0: 6f 62 5f 69 73 5f 75 75 69 64 28 26 78 66 65 72  ob_is_uuid(&xfer
36d0: 2e 61 54 6f 6b 65 6e 5b 31 5d 29 0a 20 20 20 20  .aToken[1]).    
36e0: 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73 50  ){.      if( isP
36f0: 75 73 68 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ush ){.        r
3700: 69 64 5f 66 72 6f 6d 5f 75 75 69 64 28 26 78 66  id_from_uuid(&xf
3710: 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d 2c 20 31 29  er.aToken[1], 1)
3720: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
3730: 6c 73 65 0a 20 20 0a 20 20 20 20 0a 20 20 20 20  lse.  .    .    
3740: 2f 2a 20 20 20 6c 65 61 66 20 55 55 49 44 0a 20  /*   leaf UUID. 
3750: 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 6c 69     **.    ** Cli
3760: 65 6e 74 20 61 6e 6e 6f 75 6e 63 65 73 20 74 68  ent announces th
3770: 61 74 20 69 74 20 68 61 73 20 61 20 70 61 72 74  at it has a part
3780: 69 63 75 6c 61 72 20 6d 61 6e 69 66 65 73 74 2e  icular manifest.
3790: 20 20 49 66 0a 20 20 20 20 2a 2a 20 74 68 65 20    If.    ** the 
37a0: 73 65 72 76 65 72 20 68 61 73 20 63 68 69 6c 64  server has child
37b0: 72 65 6e 20 6f 66 20 74 68 69 73 20 6c 65 61 66  ren of this leaf
37c0: 2c 20 74 68 65 6e 20 73 65 6e 64 20 74 68 6f 73  , then send thos
37d0: 65 0a 20 20 20 20 2a 2a 20 63 68 69 6c 64 72 65  e.    ** childre
37e0: 6e 20 62 61 63 6b 20 74 6f 20 74 68 65 20 63 6c  n back to the cl
37f0: 69 65 6e 74 2e 20 20 49 66 20 74 68 65 20 73 65  ient.  If the se
3800: 72 76 65 72 20 6c 61 63 6b 73 20 74 68 69 73 0a  rver lacks this.
3810: 20 20 20 20 2a 2a 20 6c 65 61 66 2c 20 72 65 71      ** leaf, req
3820: 75 65 73 74 20 69 74 2e 0a 20 20 20 20 2a 2f 0a  uest it..    */.
3830: 20 20 20 20 69 66 28 20 78 66 65 72 2e 6e 54 6f      if( xfer.nTo
3840: 6b 65 6e 3d 3d 32 0a 20 20 20 20 20 26 26 20 62  ken==2.     && b
3850: 6c 6f 62 5f 65 71 28 26 78 66 65 72 2e 61 54 6f  lob_eq(&xfer.aTo
3860: 6b 65 6e 5b 30 5d 2c 20 22 6c 65 61 66 22 29 0a  ken[0], "leaf").
3870: 20 20 20 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f       && blob_is_
3880: 75 75 69 64 28 26 78 66 65 72 2e 61 54 6f 6b 65  uuid(&xfer.aToke
3890: 6e 5b 31 5d 29 0a 20 20 20 20 29 7b 0a 20 20 20  n[1]).    ){.   
38a0: 20 20 20 69 6e 74 20 72 69 64 20 3d 20 72 69 64     int rid = rid
38b0: 5f 66 72 6f 6d 5f 75 75 69 64 28 26 78 66 65 72  _from_uuid(&xfer
38c0: 2e 61 54 6f 6b 65 6e 5b 31 5d 2c 20 30 29 3b 0a  .aToken[1], 0);.
38d0: 20 20 20 20 20 20 69 66 28 20 72 69 64 20 29 7b        if( rid ){
38e0: 0a 20 20 20 20 20 20 20 20 72 65 6d 6f 74 65 5f  .        remote_
38f0: 68 61 73 28 72 69 64 29 3b 0a 20 20 20 20 20 20  has(rid);.      
3900: 20 20 69 66 28 20 69 73 50 75 6c 6c 20 29 7b 0a    if( isPull ){.
3910: 20 20 20 20 20 20 20 20 20 20 6c 65 61 66 5f 72            leaf_r
3920: 65 73 70 6f 6e 73 65 28 26 78 66 65 72 2c 20 72  esponse(&xfer, r
3930: 69 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  id);.        }. 
3940: 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 69       }else if( i
3950: 73 50 75 73 68 20 29 7b 0a 20 20 20 20 20 20 20  sPush ){.       
3960: 20 63 6f 6e 74 65 6e 74 5f 70 75 74 28 30 2c 20   content_put(0, 
3970: 62 6c 6f 62 5f 73 74 72 28 26 78 66 65 72 2e 61  blob_str(&xfer.a
3980: 54 6f 6b 65 6e 5b 31 5d 29 2c 20 30 29 3b 0a 20  Token[1]), 0);. 
3990: 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
39a0: 0a 0a 20 20 20 20 2f 2a 20 20 20 20 70 75 6c 6c  ..    /*    pull
39b0: 20 20 53 45 52 56 45 52 43 4f 44 45 20 20 50 52    SERVERCODE  PR
39c0: 4f 4a 45 43 54 43 4f 44 45 0a 20 20 20 20 2a 2a  OJECTCODE.    **
39d0: 20 20 20 20 70 75 73 68 20 20 53 45 52 56 45 52      push  SERVER
39e0: 43 4f 44 45 20 20 50 52 4f 4a 45 43 54 43 4f 44  CODE  PROJECTCOD
39f0: 45 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  E.    **.    ** 
3a00: 54 68 65 20 63 6c 69 65 6e 74 20 77 61 6e 74 73  The client wants
3a10: 20 65 69 74 68 65 72 20 73 65 6e 64 20 6f 72 20   either send or 
3a20: 72 65 63 65 69 76 65 2e 20 20 54 68 65 20 73 65  receive.  The se
3a30: 72 76 65 72 20 73 68 6f 75 6c 64 0a 20 20 20 20  rver should.    
3a40: 2a 2a 20 76 65 72 69 66 79 20 74 68 61 74 20 74  ** verify that t
3a50: 68 65 20 70 72 6f 6a 65 63 74 20 63 6f 64 65 20  he project code 
3a60: 6d 61 74 63 68 65 73 20 61 6e 64 20 74 68 61 74  matches and that
3a70: 20 74 68 65 20 73 65 72 76 65 72 20 63 6f 64 65   the server code
3a80: 0a 20 20 20 20 2a 2a 20 64 6f 65 73 20 6e 6f 74  .    ** does not
3a90: 20 6d 61 74 63 68 2e 0a 20 20 20 20 2a 2f 0a 20   match..    */. 
3aa0: 20 20 20 69 66 28 20 78 66 65 72 2e 6e 54 6f 6b     if( xfer.nTok
3ab0: 65 6e 3d 3d 33 0a 20 20 20 20 20 26 26 20 28 62  en==3.     && (b
3ac0: 6c 6f 62 5f 65 71 28 26 78 66 65 72 2e 61 54 6f  lob_eq(&xfer.aTo
3ad0: 6b 65 6e 5b 30 5d 2c 20 22 70 75 6c 6c 22 29 20  ken[0], "pull") 
3ae0: 7c 7c 20 62 6c 6f 62 5f 65 71 28 26 78 66 65 72  || blob_eq(&xfer
3af0: 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 70 75 73  .aToken[0], "pus
3b00: 68 22 29 29 0a 20 20 20 20 20 26 26 20 62 6c 6f  h")).     && blo
3b10: 62 5f 69 73 5f 75 75 69 64 28 26 78 66 65 72 2e  b_is_uuid(&xfer.
3b20: 61 54 6f 6b 65 6e 5b 31 5d 29 0a 20 20 20 20 20  aToken[1]).     
3b30: 26 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28  && blob_is_uuid(
3b40: 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 32 5d 29  &xfer.aToken[2])
3b50: 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 63 6f  .    ){.      co
3b60: 6e 73 74 20 63 68 61 72 20 2a 7a 53 43 6f 64 65  nst char *zSCode
3b70: 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  ;.      const ch
3b80: 61 72 20 2a 7a 50 43 6f 64 65 3b 0a 0a 20 20 20  ar *zPCode;..   
3b90: 20 20 20 7a 53 43 6f 64 65 20 3d 20 64 62 5f 67     zSCode = db_g
3ba0: 65 74 28 22 73 65 72 76 65 72 2d 63 6f 64 65 22  et("server-code"
3bb0: 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , 0);.      if( 
3bc0: 7a 53 43 6f 64 65 3d 3d 30 20 29 7b 0a 20 20 20  zSCode==0 ){.   
3bd0: 20 20 20 20 20 66 6f 73 73 69 6c 5f 70 61 6e 69       fossil_pani
3be0: 63 28 22 6d 69 73 73 69 6e 67 20 73 65 72 76 65  c("missing serve
3bf0: 72 20 63 6f 64 65 22 29 3b 0a 20 20 20 20 20 20  r code");.      
3c00: 7d 0a 20 20 20 20 20 20 69 66 28 20 62 6c 6f 62  }.      if( blob
3c10: 5f 65 71 5f 73 74 72 28 26 78 66 65 72 2e 61 54  _eq_str(&xfer.aT
3c20: 6f 6b 65 6e 5b 31 5d 2c 20 7a 53 43 6f 64 65 2c  oken[1], zSCode,
3c30: 20 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20   -1) ){.        
3c40: 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e 74 65 6e  cgi_reset_conten
3c50: 74 28 29 3b 0a 20 20 20 20 20 20 20 20 40 20 65  t();.        @ e
3c60: 72 72 6f 72 20 73 65 72 76 65 72 5c 73 6c 6f 6f  rror server\sloo
3c70: 70 0a 20 20 20 20 20 20 20 20 6e 45 72 72 2b 2b  p.        nErr++
3c80: 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
3c90: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a  .      }.      z
3ca0: 50 43 6f 64 65 20 3d 20 64 62 5f 67 65 74 28 22  PCode = db_get("
3cb0: 70 72 6f 6a 65 63 74 2d 63 6f 64 65 22 2c 20 30  project-code", 0
3cc0: 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 50 43  );.      if( zPC
3cd0: 6f 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ode==0 ){.      
3ce0: 20 20 66 6f 73 73 69 6c 5f 70 61 6e 69 63 28 22    fossil_panic("
3cf0: 6d 69 73 73 69 6e 67 20 70 72 6f 6a 65 63 74 20  missing project 
3d00: 63 6f 64 65 22 29 3b 0a 20 20 20 20 20 20 7d 0a  code");.      }.
3d10: 20 20 20 20 20 20 69 66 28 20 21 62 6c 6f 62 5f        if( !blob_
3d20: 65 71 5f 73 74 72 28 26 78 66 65 72 2e 61 54 6f  eq_str(&xfer.aTo
3d30: 6b 65 6e 5b 32 5d 2c 20 7a 50 43 6f 64 65 2c 20  ken[2], zPCode, 
3d40: 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 63  -1) ){.        c
3d50: 67 69 5f 72 65 73 65 74 5f 63 6f 6e 74 65 6e 74  gi_reset_content
3d60: 28 29 3b 0a 20 20 20 20 20 20 20 20 40 20 65 72  ();.        @ er
3d70: 72 6f 72 20 77 72 6f 6e 67 5c 73 70 72 6f 6a 65  ror wrong\sproje
3d80: 63 74 0a 20 20 20 20 20 20 20 20 6e 45 72 72 2b  ct.        nErr+
3d90: 2b 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  +;.        break
3da0: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
3db0: 6c 6f 67 69 6e 5f 63 68 65 63 6b 5f 63 72 65 64  login_check_cred
3dc0: 65 6e 74 69 61 6c 73 28 29 3b 0a 20 20 20 20 20  entials();.     
3dd0: 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 78 66   if( blob_eq(&xf
3de0: 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 70  er.aToken[0], "p
3df0: 75 6c 6c 22 29 20 29 7b 0a 20 20 20 20 20 20 20  ull") ){.       
3e00: 20 69 66 28 20 21 67 2e 6f 6b 52 65 61 64 20 29   if( !g.okRead )
3e10: 7b 0a 20 20 20 20 20 20 20 20 20 20 63 67 69 5f  {.          cgi_
3e20: 72 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b  reset_content();
3e30: 0a 20 20 20 20 20 20 20 20 20 20 40 20 65 72 72  .          @ err
3e40: 6f 72 20 6e 6f 74 5c 73 61 75 74 68 6f 72 69 7a  or not\sauthoriz
3e50: 65 64 5c 73 74 6f 5c 73 72 65 61 64 0a 20 20 20  ed\sto\sread.   
3e60: 20 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20         nErr++;. 
3e70: 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
3e80: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
3e90: 20 20 69 73 50 75 6c 6c 20 3d 20 31 3b 0a 20 20    isPull = 1;.  
3ea0: 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
3eb0: 20 20 20 69 66 28 20 21 67 2e 6f 6b 57 72 69 74     if( !g.okWrit
3ec0: 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63  e ){.          c
3ed0: 67 69 5f 72 65 73 65 74 5f 63 6f 6e 74 65 6e 74  gi_reset_content
3ee0: 28 29 3b 0a 20 20 20 20 20 20 20 20 20 20 40 20  ();.          @ 
3ef0: 65 72 72 6f 72 20 6e 6f 74 5c 73 61 75 74 68 6f  error not\sautho
3f00: 72 69 7a 65 64 5c 73 74 6f 5c 73 77 72 69 74 65  rized\sto\swrite
3f10: 0a 20 20 20 20 20 20 20 20 20 20 6e 45 72 72 2b  .          nErr+
3f20: 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  +;.          bre
3f30: 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
3f40: 20 20 20 20 20 20 73 65 6e 64 5f 6c 65 61 76 65        send_leave
3f50: 73 28 26 78 66 65 72 29 3b 0a 20 20 20 20 20 20  s(&xfer);.      
3f60: 20 20 69 73 50 75 73 68 20 3d 20 31 3b 0a 20 20    isPush = 1;.  
3f70: 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 0a      }.    }else.
3f80: 0a 20 20 20 20 2f 2a 20 20 20 20 63 6c 6f 6e 65  .    /*    clone
3f90: 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54  .    **.    ** T
3fa0: 68 65 20 63 6c 69 65 6e 74 20 6b 6e 6f 77 73 20  he client knows 
3fb0: 6e 6f 74 68 69 6e 67 2e 20 20 54 65 6c 6c 20 61  nothing.  Tell a
3fc0: 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ll..    */.    i
3fd0: 66 28 20 62 6c 6f 62 5f 65 71 28 26 78 66 65 72  f( blob_eq(&xfer
3fe0: 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 63 6c 6f  .aToken[0], "clo
3ff0: 6e 65 22 29 20 29 7b 0a 20 20 20 20 20 20 69 6e  ne") ){.      in
4000: 74 20 72 6f 6f 74 69 64 3b 0a 20 20 20 20 20 20  t rootid;.      
4010: 6c 6f 67 69 6e 5f 63 68 65 63 6b 5f 63 72 65 64  login_check_cred
4020: 65 6e 74 69 61 6c 73 28 29 3b 0a 20 20 20 20 20  entials();.     
4030: 20 69 66 28 20 21 67 2e 6f 6b 43 6c 6f 6e 65 20   if( !g.okClone 
4040: 29 7b 0a 20 20 20 20 20 20 20 20 63 67 69 5f 72  ){.        cgi_r
4050: 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a  eset_content();.
4060: 20 20 20 20 20 20 20 20 40 20 65 72 72 6f 72 20          @ error 
4070: 6e 6f 74 5c 73 61 75 74 68 6f 72 69 7a 65 64 5c  not\sauthorized\
4080: 73 74 6f 5c 73 63 6c 6f 6e 65 0a 20 20 20 20 20  sto\sclone.     
4090: 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20     nErr++;.     
40a0: 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
40b0: 7d 0a 20 20 20 20 20 20 69 73 50 75 6c 6c 20 3d  }.      isPull =
40c0: 20 31 3b 0a 20 20 20 20 20 20 40 20 70 75 73 68   1;.      @ push
40d0: 20 25 73 28 64 62 5f 67 65 74 28 22 73 65 72 76   %s(db_get("serv
40e0: 65 72 2d 63 6f 64 65 22 2c 20 22 78 22 29 29 20  er-code", "x")) 
40f0: 25 73 28 64 62 5f 67 65 74 28 22 70 72 6f 6a 65  %s(db_get("proje
4100: 63 74 2d 63 6f 64 65 22 2c 20 22 78 22 29 29 0a  ct-code", "x")).
4110: 20 20 20 20 20 20 72 6f 6f 74 69 64 20 3d 20 64        rootid = d
4120: 62 5f 69 6e 74 28 30 2c 20 0a 20 20 20 20 20 20  b_int(0, .      
4130: 20 20 20 20 22 53 45 4c 45 43 54 20 70 69 64 20      "SELECT pid 
4140: 46 52 4f 4d 20 70 6c 69 6e 6b 20 41 53 20 61 22  FROM plink AS a"
4150: 0a 20 20 20 20 20 20 20 20 20 20 22 20 57 48 45  .          " WHE
4160: 52 45 20 4e 4f 54 20 45 58 49 53 54 53 28 53 45  RE NOT EXISTS(SE
4170: 4c 45 43 54 20 31 20 46 52 4f 4d 20 70 6c 69 6e  LECT 1 FROM plin
4180: 6b 20 57 48 45 52 45 20 63 69 64 3d 61 2e 70 69  k WHERE cid=a.pi
4190: 64 29 22 0a 20 20 20 20 20 20 29 3b 0a 20 20 20  d)".      );.   
41a0: 20 20 20 69 66 28 20 72 6f 6f 74 69 64 20 29 7b     if( rootid ){
41b0: 0a 20 20 20 20 20 20 20 20 73 65 6e 64 5f 66 69  .        send_fi
41c0: 6c 65 28 26 78 66 65 72 2c 20 72 6f 6f 74 69 64  le(&xfer, rootid
41d0: 2c 20 30 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20  , 0, -1);.      
41e0: 20 20 6c 65 61 66 5f 72 65 73 70 6f 6e 73 65 28    leaf_response(
41f0: 26 78 66 65 72 2c 20 72 6f 6f 74 69 64 29 3b 0a  &xfer, rootid);.
4200: 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
4210: 65 0a 0a 20 20 20 20 2f 2a 20 20 20 20 6c 6f 67  e..    /*    log
4220: 69 6e 20 20 55 53 45 52 20 20 4e 4f 4e 43 45 20  in  USER  NONCE 
4230: 20 53 49 47 4e 41 54 55 52 45 0a 20 20 20 20 2a   SIGNATURE.    *
4240: 2a 0a 20 20 20 20 2a 2a 20 43 68 65 63 6b 20 66  *.    ** Check f
4250: 6f 72 20 61 20 76 61 6c 69 64 20 6c 6f 67 69 6e  or a valid login
4260: 2e 20 20 54 68 69 73 20 68 61 73 20 74 6f 20 68  .  This has to h
4270: 61 70 70 65 6e 20 62 65 66 6f 72 65 20 61 6e 79  appen before any
4280: 74 68 69 6e 67 20 65 6c 73 65 2e 0a 20 20 20 20  thing else..    
4290: 2a 2a 20 54 68 65 20 63 6c 69 65 6e 74 20 63 61  ** The client ca
42a0: 6e 20 73 65 6e 64 20 6d 75 6c 74 69 70 6c 65 20  n send multiple 
42b0: 6c 6f 67 69 6e 73 2e 20 20 50 65 72 6d 69 73 73  logins.  Permiss
42c0: 69 6f 6e 73 20 61 72 65 20 63 75 6d 75 6c 61 74  ions are cumulat
42d0: 69 76 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ive..    */.    
42e0: 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 78 66 65  if( blob_eq(&xfe
42f0: 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 6c 6f  r.aToken[0], "lo
4300: 67 69 6e 22 29 0a 20 20 20 20 20 26 26 20 78 66  gin").     && xf
4310: 65 72 2e 6e 54 6f 6b 65 6e 3d 3d 34 0a 20 20 20  er.nToken==4.   
4320: 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 64 69   ){.      if( di
4330: 73 61 62 6c 65 4c 6f 67 69 6e 20 29 7b 0a 20 20  sableLogin ){.  
4340: 20 20 20 20 20 20 67 2e 6f 6b 52 65 61 64 20 3d        g.okRead =
4350: 20 67 2e 6f 6b 57 72 69 74 65 20 3d 20 31 3b 0a   g.okWrite = 1;.
4360: 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
4370: 20 20 20 20 20 63 68 65 63 6b 5f 6c 6f 67 69 6e       check_login
4380: 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d  (&xfer.aToken[1]
4390: 2c 20 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 32  , &xfer.aToken[2
43a0: 5d 2c 20 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b  ], &xfer.aToken[
43b0: 33 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  3]);.      }.   
43c0: 20 7d 65 6c 73 65 0a 0a 20 20 20 20 2f 2a 20 55   }else..    /* U
43d0: 6e 6b 6e 6f 77 6e 20 6d 65 73 73 61 67 65 0a 20  nknown message. 
43e0: 20 20 20 2a 2f 0a 20 20 20 20 7b 0a 20 20 20 20     */.    {.    
43f0: 20 20 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e 74    cgi_reset_cont
4400: 65 6e 74 28 29 3b 0a 20 20 20 20 20 20 40 20 65  ent();.      @ e
4410: 72 72 6f 72 20 62 61 64 5c 73 63 6f 6d 6d 61 6e  rror bad\scomman
4420: 64 3a 5c 73 25 46 28 62 6c 6f 62 5f 73 74 72 28  d:\s%F(blob_str(
4430: 26 78 66 65 72 2e 6c 69 6e 65 29 29 0a 20 20 20  &xfer.line)).   
4440: 20 7d 0a 20 20 20 20 62 6c 6f 62 61 72 72 61 79   }.    blobarray
4450: 5f 72 65 73 65 74 28 78 66 65 72 2e 61 54 6f 6b  _reset(xfer.aTok
4460: 65 6e 2c 20 78 66 65 72 2e 6e 54 6f 6b 65 6e 29  en, xfer.nToken)
4470: 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 50 75  ;.  }.  if( isPu
4480: 73 68 20 29 7b 0a 20 20 20 20 72 65 71 75 65 73  sh ){.    reques
4490: 74 5f 70 68 61 6e 74 6f 6d 73 28 26 78 66 65 72  t_phantoms(&xfer
44a0: 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 50  );.  }.  if( isP
44b0: 75 6c 6c 20 29 7b 0a 20 20 20 20 73 65 6e 64 5f  ull ){.    send_
44c0: 75 6e 6b 6e 6f 77 6e 5f 6c 65 61 66 5f 63 6f 6e  unknown_leaf_con
44d0: 74 65 6e 74 28 26 78 66 65 72 29 3b 0a 20 20 7d  tent(&xfer);.  }
44e0: 0a 20 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73 61  .  db_end_transa
44f0: 63 74 69 6f 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a 0a  ction(0);.}../*.
4500: 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 74 65 73 74  ** COMMAND: test
4510: 2d 78 66 65 72 0a 2a 2a 0a 2a 2a 20 54 68 69 73  -xfer.**.** This
4520: 20 63 6f 6d 6d 61 6e 64 20 69 73 20 75 73 65 64   command is used
4530: 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67 20 74   for debugging t
4540: 68 65 20 73 65 72 76 65 72 2e 20 20 54 68 65 72  he server.  Ther
4550: 65 20 69 73 20 61 20 73 69 6e 67 6c 65 0a 2a 2a  e is a single.**
4560: 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20   argument which 
4570: 69 73 20 74 68 65 20 75 6e 63 6f 6d 70 72 65 73  is the uncompres
4580: 73 65 64 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61  sed content of a
4590: 6e 20 22 78 66 65 72 22 20 6d 65 73 73 61 67 65  n "xfer" message
45a0: 0a 2a 2a 20 66 72 6f 6d 20 63 6c 69 65 6e 74 20  .** from client 
45b0: 74 6f 20 73 65 72 76 65 72 2e 20 20 54 68 69 73  to server.  This
45c0: 20 63 6f 6d 6d 61 6e 64 20 69 6e 74 65 72 70 72   command interpr
45d0: 65 74 73 20 74 68 61 74 20 6d 65 73 73 61 67 65  ets that message
45e0: 20 61 73 0a 2a 2a 20 69 66 20 68 61 64 20 62 65   as.** if had be
45f0: 65 6e 20 72 65 63 65 69 76 65 64 20 62 79 20 74  en received by t
4600: 68 65 20 73 65 72 76 65 72 2e 0a 2a 2a 0a 2a 2a  he server..**.**
4610: 20 4f 6e 20 74 68 65 20 63 6c 69 65 6e 74 20 73   On the client s
4620: 69 64 65 2c 20 72 75 6e 3a 0a 2a 2a 0a 2a 2a 20  ide, run:.**.** 
4630: 20 20 20 20 20 66 6f 73 73 69 6c 20 70 75 73 68       fossil push
4640: 20 68 74 74 70 3a 2f 2f 62 6f 67 75 73 2f 20 2d   http://bogus/ -
4650: 2d 68 74 74 70 74 72 61 63 65 0a 2a 2a 0a 2a 2a  -httptrace.**.**
4660: 20 4f 72 20 61 20 73 69 6d 69 6c 61 72 20 63 6f   Or a similar co
4670: 6d 6d 61 6e 64 20 74 6f 20 70 72 6f 76 69 64 65  mmand to provide
4680: 20 74 68 65 20 6f 75 74 70 75 74 2e 20 20 54 68   the output.  Th
4690: 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65  e content of the
46a0: 0a 2a 2a 20 6d 65 73 73 61 67 65 20 77 69 6c 6c  .** message will
46b0: 20 61 70 70 65 61 72 20 6f 6e 20 73 74 61 6e 64   appear on stand
46c0: 61 72 64 20 6f 75 74 70 75 74 2e 20 20 43 61 70  ard output.  Cap
46d0: 74 75 72 65 20 74 68 69 73 20 6d 65 73 73 61 67  ture this messag
46e0: 65 0a 2a 2a 20 69 6e 74 6f 20 61 20 66 69 6c 65  e.** into a file
46f0: 20 6e 61 6d 65 64 20 28 66 6f 72 20 65 78 61 6d   named (for exam
4700: 70 6c 65 29 20 6f 75 74 2e 74 78 74 2e 20 20 54  ple) out.txt.  T
4710: 68 65 6e 20 72 75 6e 20 74 68 65 0a 2a 2a 20 73  hen run the.** s
4720: 65 72 76 65 72 20 69 6e 20 67 64 62 3a 0a 2a 2a  erver in gdb:.**
4730: 0a 2a 2a 20 20 20 20 20 67 64 62 20 66 6f 73 73  .**     gdb foss
4740: 69 6c 0a 2a 2a 20 20 20 20 20 72 20 74 65 73 74  il.**     r test
4750: 2d 78 66 65 72 20 6f 75 74 2e 74 78 74 0a 2a 2f  -xfer out.txt.*/
4760: 0a 76 6f 69 64 20 63 6d 64 5f 74 65 73 74 5f 78  .void cmd_test_x
4770: 66 65 72 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74  fer(void){.  int
4780: 20 6e 6f 74 55 73 65 64 3b 0a 20 20 69 66 28 20   notUsed;.  if( 
4790: 67 2e 61 72 67 63 21 3d 32 20 26 26 20 67 2e 61  g.argc!=2 && g.a
47a0: 72 67 63 21 3d 33 20 29 7b 0a 20 20 20 20 75 73  rgc!=3 ){.    us
47b0: 61 67 65 28 22 3f 4d 45 53 53 41 47 45 46 49 4c  age("?MESSAGEFIL
47c0: 45 3f 22 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 6d  E?");.  }.  db_m
47d0: 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74 72  ust_be_within_tr
47e0: 65 65 28 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72  ee();.  blob_zer
47f0: 6f 28 26 67 2e 63 67 69 49 6e 29 3b 0a 20 20 62  o(&g.cgiIn);.  b
4800: 6c 6f 62 5f 72 65 61 64 5f 66 72 6f 6d 5f 66 69  lob_read_from_fi
4810: 6c 65 28 26 67 2e 63 67 69 49 6e 2c 20 67 2e 61  le(&g.cgiIn, g.a
4820: 72 67 63 3d 3d 32 20 3f 20 22 2d 22 20 3a 20 67  rgc==2 ? "-" : g
4830: 2e 61 72 67 76 5b 32 5d 29 3b 0a 20 20 64 69 73  .argv[2]);.  dis
4840: 61 62 6c 65 4c 6f 67 69 6e 20 3d 20 31 3b 0a 20  ableLogin = 1;. 
4850: 20 70 61 67 65 5f 78 66 65 72 28 29 3b 0a 20 20   page_xfer();.  
4860: 70 72 69 6e 74 66 28 22 25 73 5c 6e 22 2c 20 63  printf("%s\n", c
4870: 67 69 5f 65 78 74 72 61 63 74 5f 63 6f 6e 74 65  gi_extract_conte
4880: 6e 74 28 26 6e 6f 74 55 73 65 64 29 29 3b 0a 7d  nt(&notUsed));.}
4890: 0a 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 6f  .../*.** Sync to
48a0: 20 74 68 65 20 68 6f 73 74 20 69 64 65 6e 74 69   the host identi
48b0: 66 69 65 64 20 69 6e 20 67 2e 75 72 6c 4e 61 6d  fied in g.urlNam
48c0: 65 20 61 6e 64 20 67 2e 75 72 6c 50 61 74 68 2e  e and g.urlPath.
48d0: 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e    This.** routin
48e0: 65 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74  e is called by t
48f0: 68 65 20 63 6c 69 65 6e 74 2e 0a 2a 2a 0a 2a 2a  he client..**.**
4900: 20 52 65 63 6f 72 64 73 20 61 72 65 20 70 75 73   Records are pus
4910: 68 65 64 20 74 6f 20 74 68 65 20 73 65 72 76 65  hed to the serve
4920: 72 20 69 66 20 70 75 73 68 46 6c 61 67 20 69 73  r if pushFlag is
4930: 20 74 72 75 65 2e 20 20 52 65 63 6f 72 64 73 0a   true.  Records.
4940: 2a 2a 20 61 72 65 20 70 75 6c 6c 65 64 20 69 66  ** are pulled if
4950: 20 70 75 6c 6c 46 6c 61 67 20 69 73 20 74 72 75   pullFlag is tru
4960: 65 2e 20 20 41 20 66 75 6c 6c 20 73 79 6e 63 20  e.  A full sync 
4970: 6f 63 63 75 72 73 20 69 66 20 62 6f 74 68 20 61  occurs if both a
4980: 72 65 0a 2a 2a 20 74 72 75 65 2e 0a 2a 2f 0a 76  re.** true..*/.v
4990: 6f 69 64 20 63 6c 69 65 6e 74 5f 73 79 6e 63 28  oid client_sync(
49a0: 69 6e 74 20 70 75 73 68 46 6c 61 67 2c 20 69 6e  int pushFlag, in
49b0: 74 20 70 75 6c 6c 46 6c 61 67 2c 20 69 6e 74 20  t pullFlag, int 
49c0: 63 6c 6f 6e 65 46 6c 61 67 29 7b 0a 20 20 69 6e  cloneFlag){.  in
49d0: 74 20 67 6f 20 3d 20 31 3b 20 20 20 20 20 20 20  t go = 1;       
49e0: 20 2f 2a 20 4c 6f 6f 70 20 75 6e 74 69 6c 20 7a   /* Loop until z
49f0: 65 72 6f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ero */.  const c
4a00: 68 61 72 20 2a 7a 53 43 6f 64 65 20 3d 20 64 62  har *zSCode = db
4a10: 5f 67 65 74 28 22 73 65 72 76 65 72 2d 63 6f 64  _get("server-cod
4a20: 65 22 2c 20 22 78 22 29 3b 0a 20 20 63 6f 6e 73  e", "x");.  cons
4a30: 74 20 63 68 61 72 20 2a 7a 50 43 6f 64 65 20 3d  t char *zPCode =
4a40: 20 64 62 5f 67 65 74 28 22 70 72 6f 6a 65 63 74   db_get("project
4a50: 2d 63 6f 64 65 22 2c 20 30 29 3b 0a 20 20 69 6e  -code", 0);.  in
4a60: 74 20 6e 4d 73 67 20 3d 20 30 3b 20 20 20 20 20  t nMsg = 0;     
4a70: 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
4a80: 66 20 6d 65 73 73 61 67 65 73 20 73 65 6e 74 20  f messages sent 
4a90: 6f 72 20 72 65 63 65 69 76 65 64 20 2a 2f 0a 20  or received */. 
4aa0: 20 69 6e 74 20 6e 43 79 63 6c 65 20 3d 20 30 3b   int nCycle = 0;
4ab0: 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
4ac0: 72 20 6f 66 20 72 6f 75 6e 64 20 74 72 69 70 73  r of round trips
4ad0: 20 74 6f 20 74 68 65 20 73 65 72 76 65 72 20 2a   to the server *
4ae0: 2f 0a 20 20 69 6e 74 20 6e 46 69 6c 65 53 65 6e  /.  int nFileSen
4af0: 64 20 3d 20 30 3b 0a 20 20 42 6c 6f 62 20 73 65  d = 0;.  Blob se
4b00: 6e 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 65  nd;        /* Te
4b10: 78 74 20 77 65 20 61 72 65 20 73 65 6e 64 69 6e  xt we are sendin
4b20: 67 20 74 6f 20 74 68 65 20 73 65 72 76 65 72 20  g to the server 
4b30: 2a 2f 0a 20 20 42 6c 6f 62 20 72 65 63 76 3b 20  */.  Blob recv; 
4b40: 20 20 20 20 20 20 20 2f 2a 20 52 65 70 6c 79 20         /* Reply 
4b50: 77 65 20 67 6f 74 20 62 61 63 6b 20 66 72 6f 6d  we got back from
4b60: 20 74 68 65 20 73 65 72 76 65 72 20 2a 2f 0a 20   the server */. 
4b70: 20 58 66 65 72 20 78 66 65 72 3b 20 20 20 20 20   Xfer xfer;     
4b80: 20 20 20 2f 2a 20 54 72 61 6e 73 66 65 72 20 64     /* Transfer d
4b90: 61 74 61 20 2a 2f 0a 0a 20 20 6d 65 6d 73 65 74  ata */..  memset
4ba0: 28 26 78 66 65 72 2c 20 30 2c 20 73 69 7a 65 6f  (&xfer, 0, sizeo
4bb0: 66 28 78 66 65 72 29 29 3b 0a 20 20 78 66 65 72  f(xfer));.  xfer
4bc0: 2e 70 49 6e 20 3d 20 26 72 65 63 76 3b 0a 20 20  .pIn = &recv;.  
4bd0: 78 66 65 72 2e 70 4f 75 74 20 3d 20 26 73 65 6e  xfer.pOut = &sen
4be0: 64 3b 0a 20 20 78 66 65 72 2e 6d 78 53 65 6e 64  d;.  xfer.mxSend
4bf0: 20 3d 20 64 62 5f 67 65 74 5f 69 6e 74 28 22 6d   = db_get_int("m
4c00: 61 78 2d 75 70 6c 6f 61 64 22 2c 20 32 35 30 30  ax-upload", 2500
4c10: 30 30 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  00);..  assert( 
4c20: 70 75 73 68 46 6c 61 67 20 7c 7c 20 70 75 6c 6c  pushFlag || pull
4c30: 46 6c 61 67 20 7c 7c 20 63 6c 6f 6e 65 46 6c 61  Flag || cloneFla
4c40: 67 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  g );.  assert( !
4c50: 67 2e 75 72 6c 49 73 46 69 6c 65 20 29 3b 20 20  g.urlIsFile );  
4c60: 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20          /* This 
4c70: 6f 6e 6c 79 20 77 6f 72 6b 73 20 66 6f 72 20 6e  only works for n
4c80: 65 74 77 6f 72 6b 69 6e 67 20 2a 2f 0a 0a 20 20  etworking */..  
4c90: 64 62 5f 62 65 67 69 6e 5f 74 72 61 6e 73 61 63  db_begin_transac
4ca0: 74 69 6f 6e 28 29 3b 0a 20 20 64 62 5f 6d 75 6c  tion();.  db_mul
4cb0: 74 69 5f 65 78 65 63 28 0a 20 20 20 20 22 43 52  ti_exec(.    "CR
4cc0: 45 41 54 45 20 54 45 4d 50 20 54 41 42 4c 45 20  EATE TEMP TABLE 
4cd0: 6f 6e 72 65 6d 6f 74 65 28 72 69 64 20 49 4e 54  onremote(rid INT
4ce0: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
4cf0: 29 3b 22 0a 20 20 29 3b 0a 20 20 62 6c 6f 62 61  );".  );.  bloba
4d00: 72 72 61 79 5f 7a 65 72 6f 28 78 66 65 72 2e 61  rray_zero(xfer.a
4d10: 54 6f 6b 65 6e 2c 20 63 6f 75 6e 74 28 78 66 65  Token, count(xfe
4d20: 72 2e 61 54 6f 6b 65 6e 29 29 3b 0a 20 20 62 6c  r.aToken));.  bl
4d30: 6f 62 5f 7a 65 72 6f 28 26 73 65 6e 64 29 3b 0a  ob_zero(&send);.
4d40: 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 72 65 63    blob_zero(&rec
4d50: 76 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28  v);.  blob_zero(
4d60: 26 78 66 65 72 2e 65 72 72 29 3b 0a 20 20 62 6c  &xfer.err);.  bl
4d70: 6f 62 5f 7a 65 72 6f 28 26 78 66 65 72 2e 6c 69  ob_zero(&xfer.li
4d80: 6e 65 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  ne);..  /*.  ** 
4d90: 41 6c 77 61 79 73 20 62 65 67 69 6e 20 77 69 74  Always begin wit
4da0: 68 20 61 20 63 6c 6f 6e 65 2c 20 70 75 6c 6c 2c  h a clone, pull,
4db0: 20 6f 72 20 70 75 73 68 20 6d 65 73 73 61 67 65   or push message
4dc0: 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6c 6f 6e  .  */.  if( clon
4dd0: 65 46 6c 61 67 20 29 7b 0a 20 20 20 20 62 6c 6f  eFlag ){.    blo
4de0: 62 5f 61 70 70 65 6e 64 66 28 26 73 65 6e 64 2c  b_appendf(&send,
4df0: 20 22 63 6c 6f 6e 65 5c 6e 22 29 3b 0a 20 20 20   "clone\n");.   
4e00: 20 70 75 73 68 46 6c 61 67 20 3d 20 30 3b 0a 20   pushFlag = 0;. 
4e10: 20 20 20 70 75 6c 6c 46 6c 61 67 20 3d 20 30 3b     pullFlag = 0;
4e20: 0a 20 20 20 20 6e 4d 73 67 2b 2b 3b 0a 20 20 7d  .    nMsg++;.  }
4e30: 65 6c 73 65 20 69 66 28 20 70 75 6c 6c 46 6c 61  else if( pullFla
4e40: 67 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 61 70  g ){.    blob_ap
4e50: 70 65 6e 64 66 28 26 73 65 6e 64 2c 20 22 70 75  pendf(&send, "pu
4e60: 6c 6c 20 25 73 20 25 73 5c 6e 22 2c 20 7a 53 43  ll %s %s\n", zSC
4e70: 6f 64 65 2c 20 7a 50 43 6f 64 65 29 3b 0a 20 20  ode, zPCode);.  
4e80: 20 20 6e 4d 73 67 2b 2b 3b 0a 20 20 7d 0a 20 20    nMsg++;.  }.  
4e90: 69 66 28 20 70 75 73 68 46 6c 61 67 20 29 7b 0a  if( pushFlag ){.
4ea0: 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66      blob_appendf
4eb0: 28 26 73 65 6e 64 2c 20 22 70 75 73 68 20 25 73  (&send, "push %s
4ec0: 20 25 73 5c 6e 22 2c 20 7a 53 43 6f 64 65 2c 20   %s\n", zSCode, 
4ed0: 7a 50 43 6f 64 65 29 3b 0a 20 20 20 20 6e 4d 73  zPCode);.    nMs
4ee0: 67 2b 2b 3b 0a 20 20 7d 0a 0a 0a 20 20 77 68 69  g++;.  }...  whi
4ef0: 6c 65 28 20 67 6f 20 29 7b 0a 20 20 20 20 69 6e  le( go ){.    in
4f00: 74 20 6e 65 77 50 68 61 6e 74 6f 6d 20 3d 20 30  t newPhantom = 0
4f10: 3b 0a 0a 20 20 20 20 2f 2a 20 47 65 6e 65 72 61  ;..    /* Genera
4f20: 74 65 20 67 69 6d 6d 65 20 6d 65 73 73 61 67 65  te gimme message
4f30: 73 20 66 6f 72 20 70 68 61 6e 74 6f 6d 73 20 61  s for phantoms a
4f40: 6e 64 20 6c 65 61 66 20 6d 65 73 73 61 67 65 73  nd leaf messages
4f50: 0a 20 20 20 20 2a 2a 20 66 6f 72 20 61 6c 6c 20  .    ** for all 
4f60: 6c 65 61 76 65 73 2e 0a 20 20 20 20 2a 2f 0a 20  leaves..    */. 
4f70: 20 20 20 69 66 28 20 70 75 6c 6c 46 6c 61 67 20     if( pullFlag 
4f80: 29 7b 0a 20 20 20 20 20 20 72 65 71 75 65 73 74  ){.      request
4f90: 5f 70 68 61 6e 74 6f 6d 73 28 26 78 66 65 72 29  _phantoms(&xfer)
4fa0: 3b 0a 20 20 20 20 20 20 73 65 6e 64 5f 6c 65 61  ;.      send_lea
4fb0: 76 65 73 28 26 78 66 65 72 29 3b 0a 20 20 20 20  ves(&xfer);.    
4fc0: 7d 0a 0a 20 20 20 20 2f 2a 20 45 78 63 68 61 6e  }..    /* Exchan
4fd0: 67 65 20 6d 65 73 73 61 67 65 73 20 77 69 74 68  ge messages with
4fe0: 20 74 68 65 20 73 65 72 76 65 72 20 2a 2f 0a 20   the server */. 
4ff0: 20 20 20 6e 46 69 6c 65 53 65 6e 64 20 3d 20 78     nFileSend = x
5000: 66 65 72 2e 6e 46 69 6c 65 53 65 6e 74 20 2b 20  fer.nFileSent + 
5010: 78 66 65 72 2e 6e 44 65 6c 74 61 53 65 6e 74 3b  xfer.nDeltaSent;
5020: 0a 20 20 20 20 70 72 69 6e 74 66 28 22 53 65 6e  .    printf("Sen
5030: 64 3a 20 20 20 20 20 20 25 31 30 64 20 62 79 74  d:      %10d byt
5040: 65 73 2c 20 25 33 64 20 6d 65 73 73 61 67 65 73  es, %3d messages
5050: 2c 20 25 33 64 20 66 69 6c 65 73 20 28 25 64 2b  , %3d files (%d+
5060: 25 64 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20  %d)\n",.        
5070: 20 20 20 20 62 6c 6f 62 5f 73 69 7a 65 28 26 73      blob_size(&s
5080: 65 6e 64 29 2c 20 6e 4d 73 67 2b 78 66 65 72 2e  end), nMsg+xfer.
5090: 6e 47 69 6d 6d 65 53 65 6e 74 2b 78 66 65 72 2e  nGimmeSent+xfer.
50a0: 6e 49 47 6f 74 53 65 6e 74 2c 0a 20 20 20 20 20  nIGotSent,.     
50b0: 20 20 20 20 20 20 20 6e 46 69 6c 65 53 65 6e 64         nFileSend
50c0: 2c 20 78 66 65 72 2e 6e 46 69 6c 65 53 65 6e 74  , xfer.nFileSent
50d0: 2c 20 78 66 65 72 2e 6e 44 65 6c 74 61 53 65 6e  , xfer.nDeltaSen
50e0: 74 29 3b 0a 20 20 20 20 6e 4d 73 67 20 3d 20 30  t);.    nMsg = 0
50f0: 3b 0a 20 20 20 20 78 66 65 72 2e 6e 46 69 6c 65  ;.    xfer.nFile
5100: 53 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20 78 66  Sent = 0;.    xf
5110: 65 72 2e 6e 44 65 6c 74 61 53 65 6e 74 20 3d 20  er.nDeltaSent = 
5120: 30 3b 0a 20 20 20 20 78 66 65 72 2e 6e 47 69 6d  0;.    xfer.nGim
5130: 6d 65 53 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20  meSent = 0;.    
5140: 68 74 74 70 5f 65 78 63 68 61 6e 67 65 28 26 73  http_exchange(&s
5150: 65 6e 64 2c 20 26 72 65 63 76 29 3b 0a 20 20 20  end, &recv);.   
5160: 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 73 65 6e   blob_reset(&sen
5170: 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 42 65 67 69  d);..    /* Begi
5180: 6e 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 20 74  n constructing t
5190: 68 65 20 6e 65 78 74 20 6d 65 73 73 61 67 65 20  he next message 
51a0: 28 77 68 69 63 68 20 6d 69 67 68 74 20 6e 65 76  (which might nev
51b0: 65 72 20 62 65 0a 20 20 20 20 2a 2a 20 73 65 6e  er be.    ** sen
51c0: 74 29 20 62 79 20 62 65 67 69 6e 6e 69 6e 67 20  t) by beginning 
51d0: 77 69 74 68 20 74 68 65 20 70 75 6c 6c 20 6f 72  with the pull or
51e0: 20 70 75 73 68 20 6d 65 73 73 61 67 65 73 0a 20   push messages. 
51f0: 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 75     */.    if( pu
5200: 6c 6c 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20  llFlag ){.      
5210: 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 73 65  blob_appendf(&se
5220: 6e 64 2c 20 22 70 75 6c 6c 20 25 73 20 25 73 5c  nd, "pull %s %s\
5230: 6e 22 2c 20 7a 53 43 6f 64 65 2c 20 7a 50 43 6f  n", zSCode, zPCo
5240: 64 65 29 3b 0a 20 20 20 20 20 20 6e 4d 73 67 2b  de);.      nMsg+
5250: 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  +;.    }.    if(
5260: 20 70 75 73 68 46 6c 61 67 20 29 7b 0a 20 20 20   pushFlag ){.   
5270: 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28     blob_appendf(
5280: 26 73 65 6e 64 2c 20 22 70 75 73 68 20 25 73 20  &send, "push %s 
5290: 25 73 5c 6e 22 2c 20 7a 53 43 6f 64 65 2c 20 7a  %s\n", zSCode, z
52a0: 50 43 6f 64 65 29 3b 0a 20 20 20 20 20 20 6e 4d  PCode);.      nM
52b0: 73 67 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 0a 20 20  sg++;.    }...  
52c0: 20 20 2f 2a 20 50 72 6f 63 65 73 73 20 74 68 65    /* Process the
52d0: 20 72 65 70 6c 79 20 74 68 61 74 20 63 61 6d 65   reply that came
52e0: 20 62 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 73   back from the s
52f0: 65 72 76 65 72 20 2a 2f 0a 20 20 20 20 77 68 69  erver */.    whi
5300: 6c 65 28 20 62 6c 6f 62 5f 6c 69 6e 65 28 26 72  le( blob_line(&r
5310: 65 63 76 2c 20 26 78 66 65 72 2e 6c 69 6e 65 29  ecv, &xfer.line)
5320: 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 62 6c   ){.      if( bl
5330: 6f 62 5f 62 75 66 66 65 72 28 26 78 66 65 72 2e  ob_buffer(&xfer.
5340: 6c 69 6e 65 29 5b 30 5d 3d 3d 27 23 27 20 29 7b  line)[0]=='#' ){
5350: 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75  .        continu
5360: 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  e;.      }.     
5370: 20 78 66 65 72 2e 6e 54 6f 6b 65 6e 20 3d 20 62   xfer.nToken = b
5380: 6c 6f 62 5f 74 6f 6b 65 6e 69 7a 65 28 26 78 66  lob_tokenize(&xf
5390: 65 72 2e 6c 69 6e 65 2c 20 78 66 65 72 2e 61 54  er.line, xfer.aT
53a0: 6f 6b 65 6e 2c 20 63 6f 75 6e 74 28 78 66 65 72  oken, count(xfer
53b0: 2e 61 54 6f 6b 65 6e 29 29 3b 0a 0a 20 20 20 20  .aToken));..    
53c0: 20 20 2f 2a 20 20 20 66 69 6c 65 20 55 55 49 44    /*   file UUID
53d0: 20 53 49 5a 45 20 5c 6e 20 43 4f 4e 54 45 4e 54   SIZE \n CONTENT
53e0: 0a 20 20 20 20 20 20 2a 2a 20 20 20 66 69 6c 65  .      **   file
53f0: 20 55 55 49 44 20 44 45 4c 54 41 53 52 43 20 53   UUID DELTASRC S
5400: 49 5a 45 20 5c 6e 20 43 4f 4e 54 45 4e 54 0a 20  IZE \n CONTENT. 
5410: 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
5420: 20 52 65 63 65 69 76 65 20 61 20 66 69 6c 65 20   Receive a file 
5430: 74 72 61 6e 73 6d 69 74 74 65 64 20 66 72 6f 6d  transmitted from
5440: 20 74 68 65 20 73 65 72 76 65 72 2e 0a 20 20 20   the server..   
5450: 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
5460: 62 6c 6f 62 5f 65 71 28 26 78 66 65 72 2e 61 54  blob_eq(&xfer.aT
5470: 6f 6b 65 6e 5b 30 5d 2c 22 66 69 6c 65 22 29 20  oken[0],"file") 
5480: 29 7b 0a 20 20 20 20 20 20 20 20 78 66 65 72 5f  ){.        xfer_
5490: 61 63 63 65 70 74 5f 66 69 6c 65 28 26 78 66 65  accept_file(&xfe
54a0: 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a  r);.      }else.
54b0: 0a 20 20 20 20 20 20 2f 2a 20 20 20 67 69 6d 6d  .      /*   gimm
54c0: 65 20 55 55 49 44 0a 20 20 20 20 20 20 2a 2a 0a  e UUID.      **.
54d0: 20 20 20 20 20 20 2a 2a 20 53 65 72 76 65 72 20        ** Server 
54e0: 69 73 20 72 65 71 75 65 73 74 69 6e 67 20 61 20  is requesting a 
54f0: 66 69 6c 65 2e 20 20 49 66 20 74 68 65 20 66 69  file.  If the fi
5500: 6c 65 20 69 73 20 61 20 6d 61 6e 69 66 65 73 74  le is a manifest
5510: 2c 20 61 73 73 75 6d 65 0a 20 20 20 20 20 20 2a  , assume.      *
5520: 2a 20 74 68 61 74 20 74 68 65 20 73 65 72 76 65  * that the serve
5530: 72 20 77 69 6c 6c 20 61 6c 73 6f 20 77 61 6e 74  r will also want
5540: 20 74 6f 20 6b 6e 6f 77 20 61 6c 6c 20 6f 66 20   to know all of 
5550: 74 68 65 20 63 6f 6e 74 65 6e 74 20 66 69 6c 65  the content file
5560: 73 0a 20 20 20 20 20 20 2a 2a 20 61 73 73 6f 63  s.      ** assoc
5570: 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 6d  iated with the m
5580: 61 6e 69 66 65 73 74 20 61 6e 64 20 73 65 6e 64  anifest and send
5590: 20 74 68 6f 73 65 20 74 6f 6f 2e 0a 20 20 20 20   those too..    
55a0: 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 62    */.      if( b
55b0: 6c 6f 62 5f 65 71 28 26 78 66 65 72 2e 61 54 6f  lob_eq(&xfer.aTo
55c0: 6b 65 6e 5b 30 5d 2c 20 22 67 69 6d 6d 65 22 29  ken[0], "gimme")
55d0: 0a 20 20 20 20 20 20 20 26 26 20 78 66 65 72 2e  .       && xfer.
55e0: 6e 54 6f 6b 65 6e 3d 3d 32 0a 20 20 20 20 20 20  nToken==2.      
55f0: 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69 64   && blob_is_uuid
5600: 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d  (&xfer.aToken[1]
5610: 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20  ).      ){.     
5620: 20 20 20 6e 4d 73 67 2b 2b 3b 0a 20 20 20 20 20     nMsg++;.     
5630: 20 20 20 69 66 28 20 70 75 73 68 46 6c 61 67 20     if( pushFlag 
5640: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  ){.          int
5650: 20 72 69 64 20 3d 20 72 69 64 5f 66 72 6f 6d 5f   rid = rid_from_
5660: 75 75 69 64 28 26 78 66 65 72 2e 61 54 6f 6b 65  uuid(&xfer.aToke
5670: 6e 5b 31 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20  n[1], 0);.      
5680: 20 20 20 20 73 65 6e 64 5f 6d 61 6e 69 66 65 73      send_manifes
5690: 74 28 26 78 66 65 72 2c 20 72 69 64 2c 20 26 78  t(&xfer, rid, &x
56a0: 66 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d 2c 20 30  fer.aToken[1], 0
56b0: 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
56c0: 20 20 20 7d 65 6c 73 65 0a 20 20 0a 20 20 20 20     }else.  .    
56d0: 20 20 2f 2a 20 20 20 69 67 6f 74 20 55 55 49 44    /*   igot UUID
56e0: 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
56f0: 2a 2a 20 53 65 72 76 65 72 20 61 6e 6e 6f 75 6e  ** Server announ
5700: 63 65 73 20 74 68 61 74 20 69 74 20 68 61 73 20  ces that it has 
5710: 61 20 70 61 72 74 69 63 75 6c 61 72 20 66 69 6c  a particular fil
5720: 65 2e 20 20 49 66 20 74 68 69 73 20 69 73 0a 20  e.  If this is. 
5730: 20 20 20 20 20 2a 2a 20 6e 6f 74 20 61 20 66 69       ** not a fi
5740: 6c 65 20 74 68 61 74 20 77 65 20 68 61 76 65 20  le that we have 
5750: 61 6e 64 20 77 65 20 61 72 65 20 70 75 6c 6c 69  and we are pulli
5760: 6e 67 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20  ng, then create 
5770: 61 0a 20 20 20 20 20 20 2a 2a 20 70 68 61 6e 74  a.      ** phant
5780: 6f 6d 20 74 6f 20 63 61 75 73 65 20 74 68 69 73  om to cause this
5790: 20 66 69 6c 65 20 74 6f 20 62 65 20 72 65 71 75   file to be requ
57a0: 65 73 74 65 64 20 6f 6e 20 74 68 65 20 6e 65 78  ested on the nex
57b0: 74 20 63 79 63 6c 65 2e 0a 20 20 20 20 20 20 2a  t cycle..      *
57c0: 2a 20 41 6c 77 61 79 73 20 72 65 6d 65 6d 62 65  * Always remembe
57d0: 72 20 74 68 61 74 20 74 68 65 20 73 65 72 76 65  r that the serve
57e0: 72 20 68 61 73 20 74 68 69 73 20 66 69 6c 65 20  r has this file 
57f0: 73 6f 20 74 68 61 74 20 77 65 20 64 6f 0a 20 20  so that we do.  
5800: 20 20 20 20 2a 2a 20 6e 6f 74 20 74 72 61 6e 73      ** not trans
5810: 6d 69 74 20 69 74 20 62 79 20 61 63 63 69 64 65  mit it by accide
5820: 6e 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  nt..      */.   
5830: 20 20 20 69 66 28 20 78 66 65 72 2e 6e 54 6f 6b     if( xfer.nTok
5840: 65 6e 3d 3d 32 0a 20 20 20 20 20 20 20 26 26 20  en==2.       && 
5850: 62 6c 6f 62 5f 65 71 28 26 78 66 65 72 2e 61 54  blob_eq(&xfer.aT
5860: 6f 6b 65 6e 5b 30 5d 2c 20 22 69 67 6f 74 22 29  oken[0], "igot")
5870: 0a 20 20 20 20 20 20 20 26 26 20 62 6c 6f 62 5f  .       && blob_
5880: 69 73 5f 75 75 69 64 28 26 78 66 65 72 2e 61 54  is_uuid(&xfer.aT
5890: 6f 6b 65 6e 5b 31 5d 29 0a 20 20 20 20 20 20 29  oken[1]).      )
58a0: 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 69  {.        int ri
58b0: 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 6e  d = 0;.        n
58c0: 4d 73 67 2b 2b 3b 0a 20 20 20 20 20 20 20 20 69  Msg++;.        i
58d0: 66 28 20 70 75 6c 6c 46 6c 61 67 20 29 7b 0a 20  f( pullFlag ){. 
58e0: 20 20 20 20 20 20 20 20 20 69 66 28 20 21 64 62           if( !db
58f0: 5f 65 78 69 73 74 73 28 22 53 45 4c 45 43 54 20  _exists("SELECT 
5900: 31 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52  1 FROM blob WHER
5910: 45 20 75 75 69 64 3d 27 25 62 27 20 41 4e 44 20  E uuid='%b' AND 
5920: 73 69 7a 65 3e 3d 30 22 2c 0a 20 20 20 20 20 20  size>=0",.      
5930: 20 20 20 20 20 20 20 20 20 20 26 78 66 65 72 2e            &xfer.
5940: 61 54 6f 6b 65 6e 5b 31 5d 29 20 29 7b 0a 20 20  aToken[1]) ){.  
5950: 20 20 20 20 20 20 20 20 20 20 72 69 64 20 3d 20            rid = 
5960: 63 6f 6e 74 65 6e 74 5f 70 75 74 28 30 2c 20 62  content_put(0, b
5970: 6c 6f 62 5f 73 74 72 28 26 78 66 65 72 2e 61 54  lob_str(&xfer.aT
5980: 6f 6b 65 6e 5b 31 5d 29 2c 20 30 29 3b 0a 20 20  oken[1]), 0);.  
5990: 20 20 20 20 20 20 20 20 20 20 6e 65 77 50 68 61            newPha
59a0: 6e 74 6f 6d 20 3d 20 31 3b 0a 20 20 20 20 20 20  ntom = 1;.      
59b0: 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
59c0: 20 20 20 20 20 20 20 20 69 66 28 20 72 69 64 3d          if( rid=
59d0: 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
59e0: 72 69 64 20 3d 20 72 69 64 5f 66 72 6f 6d 5f 75  rid = rid_from_u
59f0: 75 69 64 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e  uid(&xfer.aToken
5a00: 5b 31 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  [1], 0);.       
5a10: 20 7d 0a 20 20 20 20 20 20 20 20 72 65 6d 6f 74   }.        remot
5a20: 65 5f 68 61 73 28 72 69 64 29 3b 0a 20 20 20 20  e_has(rid);.    
5a30: 20 20 7d 65 6c 73 65 0a 20 20 20 20 0a 20 20 20    }else.    .   
5a40: 20 20 20 0a 20 20 20 20 20 20 2f 2a 20 20 20 6c     .      /*   l
5a50: 65 61 66 20 55 55 49 44 0a 20 20 20 20 20 20 2a  eaf UUID.      *
5a60: 2a 0a 20 20 20 20 20 20 2a 2a 20 53 65 72 76 65  *.      ** Serve
5a70: 72 20 61 6e 6e 6f 75 6e 63 65 73 20 74 68 61 74  r announces that
5a80: 20 69 74 20 68 61 73 20 61 20 70 61 72 74 69 63   it has a partic
5a90: 75 6c 61 72 20 6d 61 6e 69 66 65 73 74 2e 20 20  ular manifest.  
5aa0: 53 65 6e 64 0a 20 20 20 20 20 20 2a 2a 20 61 6e  Send.      ** an
5ab0: 79 20 63 68 69 6c 64 72 65 6e 20 6f 66 20 74 68  y children of th
5ac0: 69 73 20 6c 65 61 66 20 74 68 61 74 20 77 65 20  is leaf that we 
5ad0: 68 61 76 65 20 69 66 20 77 65 20 61 72 65 20 70  have if we are p
5ae0: 75 73 68 69 6e 67 2e 0a 20 20 20 20 20 20 2a 2a  ushing..      **
5af0: 20 4d 61 6b 65 20 74 68 65 20 6c 65 61 66 20 61   Make the leaf a
5b00: 20 70 68 61 6e 74 6f 6d 20 69 66 20 77 65 20 61   phantom if we a
5b10: 72 65 20 70 75 6c 6c 69 6e 67 2e 20 20 52 65 6d  re pulling.  Rem
5b20: 65 6d 62 65 72 20 74 68 61 74 20 74 68 65 0a 20  ember that the. 
5b30: 20 20 20 20 20 2a 2a 20 72 65 6d 6f 74 65 20 65       ** remote e
5b40: 6e 64 20 68 61 73 20 74 68 65 20 73 70 65 63 69  nd has the speci
5b50: 66 69 65 64 20 55 55 49 44 2e 0a 20 20 20 20 20  fied UUID..     
5b60: 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 78 66   */.      if( xf
5b70: 65 72 2e 6e 54 6f 6b 65 6e 3d 3d 32 0a 20 20 20  er.nToken==2.   
5b80: 20 20 20 20 26 26 20 62 6c 6f 62 5f 65 71 28 26      && blob_eq(&
5b90: 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 20  xfer.aToken[0], 
5ba0: 22 6c 65 61 66 22 29 0a 20 20 20 20 20 20 20 26  "leaf").       &
5bb0: 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26  & blob_is_uuid(&
5bc0: 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 31 5d 29 0a  xfer.aToken[1]).
5bd0: 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
5be0: 20 69 6e 74 20 72 69 64 20 3d 20 72 69 64 5f 66   int rid = rid_f
5bf0: 72 6f 6d 5f 75 75 69 64 28 26 78 66 65 72 2e 61  rom_uuid(&xfer.a
5c00: 54 6f 6b 65 6e 5b 31 5d 2c 20 30 29 3b 0a 20 20  Token[1], 0);.  
5c10: 20 20 20 20 20 20 6e 4d 73 67 2b 2b 3b 0a 20 20        nMsg++;.  
5c20: 20 20 20 20 20 20 69 66 28 20 70 75 73 68 46 6c        if( pushFl
5c30: 61 67 20 26 26 20 72 69 64 20 29 7b 0a 20 20 20  ag && rid ){.   
5c40: 20 20 20 20 20 20 20 6c 65 61 66 5f 72 65 73 70         leaf_resp
5c50: 6f 6e 73 65 28 26 78 66 65 72 2c 20 72 69 64 29  onse(&xfer, rid)
5c60: 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
5c70: 20 20 20 20 69 66 28 20 70 75 6c 6c 46 6c 61 67      if( pullFlag
5c80: 20 26 26 20 72 69 64 3d 3d 30 20 29 7b 0a 20 20   && rid==0 ){.  
5c90: 20 20 20 20 20 20 20 20 72 69 64 20 3d 20 63 6f          rid = co
5ca0: 6e 74 65 6e 74 5f 70 75 74 28 30 2c 20 62 6c 6f  ntent_put(0, blo
5cb0: 62 5f 73 74 72 28 26 78 66 65 72 2e 61 54 6f 6b  b_str(&xfer.aTok
5cc0: 65 6e 5b 31 5d 29 2c 20 30 29 3b 0a 20 20 20 20  en[1]), 0);.    
5cd0: 20 20 20 20 20 20 6e 65 77 50 68 61 6e 74 6f 6d        newPhantom
5ce0: 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 1;.        }.
5cf0: 20 20 20 20 20 20 20 20 72 65 6d 6f 74 65 5f 68          remote_h
5d00: 61 73 28 72 69 64 29 3b 0a 20 20 20 20 20 20 7d  as(rid);.      }
5d10: 65 6c 73 65 0a 20 20 0a 20 20 0a 20 20 20 20 20  else.  .  .     
5d20: 20 2f 2a 20 20 20 70 75 73 68 20 20 53 45 52 56   /*   push  SERV
5d30: 45 52 43 4f 44 45 20 20 50 52 4f 44 55 43 54 43  ERCODE  PRODUCTC
5d40: 4f 44 45 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  ODE.      **.   
5d50: 20 20 20 2a 2a 20 53 68 6f 75 6c 64 20 6f 6e 6c     ** Should onl
5d60: 79 20 68 61 70 70 65 6e 20 69 6e 20 72 65 73 70  y happen in resp
5d70: 6f 6e 73 65 20 74 6f 20 61 20 63 6c 6f 6e 65 2e  onse to a clone.
5d80: 20 20 54 68 69 73 20 6d 65 73 73 61 67 65 20 74    This message t
5d90: 65 6c 6c 73 0a 20 20 20 20 20 20 2a 2a 20 74 68  ells.      ** th
5da0: 65 20 63 6c 69 65 6e 74 20 77 68 61 74 20 70 72  e client what pr
5db0: 6f 64 75 63 74 20 74 6f 20 75 73 65 20 66 6f 72  oduct to use for
5dc0: 20 74 68 65 20 6e 65 77 20 64 61 74 61 62 61 73   the new databas
5dd0: 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  e..      */.    
5de0: 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 78    if( blob_eq(&x
5df0: 66 65 72 2e 61 54 6f 6b 65 6e 5b 30 5d 2c 22 70  fer.aToken[0],"p
5e00: 75 73 68 22 29 0a 20 20 20 20 20 20 20 26 26 20  ush").       && 
5e10: 78 66 65 72 2e 6e 54 6f 6b 65 6e 3d 3d 33 0a 20  xfer.nToken==3. 
5e20: 20 20 20 20 20 20 26 26 20 63 6c 6f 6e 65 46 6c        && cloneFl
5e30: 61 67 0a 20 20 20 20 20 20 20 26 26 20 62 6c 6f  ag.       && blo
5e40: 62 5f 69 73 5f 75 75 69 64 28 26 78 66 65 72 2e  b_is_uuid(&xfer.
5e50: 61 54 6f 6b 65 6e 5b 31 5d 29 0a 20 20 20 20 20  aToken[1]).     
5e60: 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69    && blob_is_uui
5e70: 64 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 32  d(&xfer.aToken[2
5e80: 5d 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20  ]).      ){.    
5e90: 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 5f      if( blob_eq_
5ea0: 73 74 72 28 26 78 66 65 72 2e 61 54 6f 6b 65 6e  str(&xfer.aToken
5eb0: 5b 31 5d 2c 20 7a 53 43 6f 64 65 2c 20 2d 31 29  [1], zSCode, -1)
5ec0: 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f   ){.          fo
5ed0: 73 73 69 6c 5f 66 61 74 61 6c 28 22 73 65 72 76  ssil_fatal("serv
5ee0: 65 72 20 6c 6f 6f 70 22 29 3b 0a 20 20 20 20 20  er loop");.     
5ef0: 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 4d 73     }.        nMs
5f00: 67 2b 2b 3b 0a 20 20 20 20 20 20 20 20 69 66 28  g++;.        if(
5f10: 20 7a 50 43 6f 64 65 3d 3d 30 20 29 7b 0a 20 20   zPCode==0 ){.  
5f20: 20 20 20 20 20 20 20 20 7a 50 43 6f 64 65 20 3d          zPCode =
5f30: 20 6d 70 72 69 6e 74 66 28 22 25 62 22 2c 20 26   mprintf("%b", &
5f40: 78 66 65 72 2e 61 54 6f 6b 65 6e 5b 32 5d 29 3b  xfer.aToken[2]);
5f50: 0a 20 20 20 20 20 20 20 20 20 20 64 62 5f 73 65  .          db_se
5f60: 74 28 22 70 72 6f 6a 65 63 74 2d 63 6f 64 65 22  t("project-code"
5f70: 2c 20 7a 50 43 6f 64 65 29 3b 0a 20 20 20 20 20  , zPCode);.     
5f80: 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 6c 6f     }.        clo
5f90: 6e 65 46 6c 61 67 20 3d 20 30 3b 0a 20 20 20 20  neFlag = 0;.    
5fa0: 20 20 20 20 70 75 6c 6c 46 6c 61 67 20 3d 20 31      pullFlag = 1
5fb0: 3b 0a 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 61  ;.        blob_a
5fc0: 70 70 65 6e 64 66 28 26 73 65 6e 64 2c 20 22 70  ppendf(&send, "p
5fd0: 75 6c 6c 20 25 73 20 25 73 5c 6e 22 2c 20 7a 53  ull %s %s\n", zS
5fe0: 43 6f 64 65 2c 20 7a 50 43 6f 64 65 29 3b 0a 20  Code, zPCode);. 
5ff0: 20 20 20 20 20 20 20 6e 4d 73 67 2b 2b 3b 0a 20         nMsg++;. 
6000: 20 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20 20       }else..    
6010: 20 20 2f 2a 20 20 20 65 72 72 6f 72 20 4d 45 53    /*   error MES
6020: 53 41 47 45 0a 20 20 20 20 20 20 2a 2a 0a 20 20  SAGE.      **.  
6030: 20 20 20 20 2a 2a 20 52 65 70 6f 72 74 20 61 6e      ** Report an
6040: 20 65 72 72 6f 72 0a 20 20 20 20 20 20 2a 2f 20   error.      */ 
6050: 20 20 20 20 20 20 20 0a 20 20 20 20 20 20 69 66         .      if
6060: 28 20 62 6c 6f 62 5f 65 71 28 26 78 66 65 72 2e  ( blob_eq(&xfer.
6070: 61 54 6f 6b 65 6e 5b 30 5d 2c 22 65 72 72 6f 72  aToken[0],"error
6080: 22 29 20 26 26 20 78 66 65 72 2e 6e 54 6f 6b 65  ") && xfer.nToke
6090: 6e 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20 20 20  n==2 ){.        
60a0: 63 68 61 72 20 2a 7a 4d 73 67 20 3d 20 62 6c 6f  char *zMsg = blo
60b0: 62 5f 74 65 72 6d 69 6e 61 74 65 28 26 78 66 65  b_terminate(&xfe
60c0: 72 2e 61 54 6f 6b 65 6e 5b 31 5d 29 3b 0a 20 20  r.aToken[1]);.  
60d0: 20 20 20 20 20 20 64 65 66 6f 73 73 69 6c 69 7a        defossiliz
60e0: 65 28 7a 4d 73 67 29 3b 0a 20 20 20 20 20 20 20  e(zMsg);.       
60f0: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 78   blob_appendf(&x
6100: 66 65 72 2e 65 72 72 2c 20 22 73 65 72 76 65 72  fer.err, "server
6110: 20 73 61 79 73 3a 20 25 73 22 2c 20 7a 4d 73 67   says: %s", zMsg
6120: 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a 0a  );.      }else..
6130: 20 20 20 20 20 20 2f 2a 20 55 6e 6b 6e 6f 77 6e        /* Unknown
6140: 20 6d 65 73 73 61 67 65 20 2a 2f 0a 20 20 20 20   message */.    
6150: 20 20 7b 0a 20 20 20 20 20 20 20 20 62 6c 6f 62    {.        blob
6160: 5f 61 70 70 65 6e 64 66 28 26 78 66 65 72 2e 65  _appendf(&xfer.e
6170: 72 72 2c 20 22 75 6e 6b 6e 6f 77 6e 20 63 6f 6d  rr, "unknown com
6180: 6d 61 6e 64 3a 20 25 62 22 2c 20 26 78 66 65 72  mand: %b", &xfer
6190: 2e 61 54 6f 6b 65 6e 5b 30 5d 29 3b 0a 20 20 20  .aToken[0]);.   
61a0: 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20     }..      if( 
61b0: 62 6c 6f 62 5f 73 69 7a 65 28 26 78 66 65 72 2e  blob_size(&xfer.
61c0: 65 72 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20  err) ){.        
61d0: 66 6f 73 73 69 6c 5f 66 61 74 61 6c 28 22 25 62  fossil_fatal("%b
61e0: 22 2c 20 26 78 66 65 72 2e 65 72 72 29 3b 0a 20  ", &xfer.err);. 
61f0: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 6c 6f       }.      blo
6200: 62 61 72 72 61 79 5f 72 65 73 65 74 28 78 66 65  barray_reset(xfe
6210: 72 2e 61 54 6f 6b 65 6e 2c 20 78 66 65 72 2e 6e  r.aToken, xfer.n
6220: 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 62 6c  Token);.      bl
6230: 6f 62 5f 72 65 73 65 74 28 26 78 66 65 72 2e 6c  ob_reset(&xfer.l
6240: 69 6e 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ine);.    }.    
6250: 70 72 69 6e 74 66 28 22 52 65 63 65 69 76 65 64  printf("Received
6260: 3a 20 20 25 31 30 64 20 62 79 74 65 73 2c 20 25  :  %10d bytes, %
6270: 33 64 20 6d 65 73 73 61 67 65 73 2c 20 25 33 64  3d messages, %3d
6280: 20 66 69 6c 65 73 20 28 25 64 2b 25 64 2b 25 64   files (%d+%d+%d
6290: 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  )\n",.          
62a0: 20 20 62 6c 6f 62 5f 73 69 7a 65 28 26 72 65 63    blob_size(&rec
62b0: 76 29 2c 20 6e 4d 73 67 2c 0a 20 20 20 20 20 20  v), nMsg,.      
62c0: 20 20 20 20 20 20 78 66 65 72 2e 6e 46 69 6c 65        xfer.nFile
62d0: 52 63 76 64 20 2b 20 78 66 65 72 2e 6e 44 65 6c  Rcvd + xfer.nDel
62e0: 74 61 52 63 76 64 20 2b 20 78 66 65 72 2e 6e 44  taRcvd + xfer.nD
62f0: 61 6e 67 6c 69 6e 67 46 69 6c 65 2c 0a 20 20 20  anglingFile,.   
6300: 20 20 20 20 20 20 20 20 20 78 66 65 72 2e 6e 46           xfer.nF
6310: 69 6c 65 52 63 76 64 2c 20 78 66 65 72 2e 6e 44  ileRcvd, xfer.nD
6320: 65 6c 74 61 52 63 76 64 2c 20 78 66 65 72 2e 6e  eltaRcvd, xfer.n
6330: 44 61 6e 67 6c 69 6e 67 46 69 6c 65 29 3b 0a 0a  DanglingFile);..
6340: 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26      blob_reset(&
6350: 72 65 63 76 29 3b 0a 20 20 20 20 6e 4d 73 67 20  recv);.    nMsg 
6360: 3d 20 30 3b 0a 20 20 20 20 78 66 65 72 2e 6e 46  = 0;.    xfer.nF
6370: 69 6c 65 52 63 76 64 20 3d 20 30 3b 0a 20 20 20  ileRcvd = 0;.   
6380: 20 78 66 65 72 2e 6e 44 65 6c 74 61 52 63 76 64   xfer.nDeltaRcvd
6390: 20 3d 20 30 3b 0a 20 20 20 20 78 66 65 72 2e 6e   = 0;.    xfer.n
63a0: 44 61 6e 67 6c 69 6e 67 46 69 6c 65 20 3d 20 30  DanglingFile = 0
63b0: 3b 0a 20 20 20 20 6e 43 79 63 6c 65 2b 2b 3b 0a  ;.    nCycle++;.
63c0: 20 20 20 20 67 6f 20 3d 20 30 3b 0a 0a 20 20 20      go = 0;..   
63d0: 20 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 72   /* If we have r
63e0: 65 63 65 69 76 65 64 20 6f 6e 65 20 6f 72 20 6d  eceived one or m
63f0: 6f 72 65 20 66 69 6c 65 73 20 6f 6e 20 74 68 69  ore files on thi
6400: 73 20 63 79 63 6c 65 20 6f 72 20 69 66 0a 20 20  s cycle or if.  
6410: 20 20 2a 2a 20 77 65 20 68 61 76 65 20 72 65 63    ** we have rec
6420: 65 69 76 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f  eived informatio
6430: 6e 20 74 68 61 74 20 68 61 73 20 63 61 75 73 65  n that has cause
6440: 64 20 75 73 20 74 6f 20 63 72 65 61 74 65 0a 20  d us to create. 
6450: 20 20 20 2a 2a 20 6e 65 77 20 70 68 61 6e 74 6f     ** new phanto
6460: 6d 73 20 61 6e 64 20 77 65 20 68 61 76 65 20 6f  ms and we have o
6470: 6e 65 20 6f 72 20 6d 6f 72 65 20 70 68 61 6e 74  ne or more phant
6480: 6f 6d 73 2c 20 74 68 65 6e 20 67 6f 20 66 6f 72  oms, then go for
6490: 20 0a 20 20 20 20 2a 2a 20 61 6e 6f 74 68 65 72   .    ** another
64a0: 20 72 6f 75 6e 64 0a 20 20 20 20 2a 2f 0a 20 20   round.    */.  
64b0: 20 20 69 66 28 20 28 78 66 65 72 2e 6e 46 69 6c    if( (xfer.nFil
64c0: 65 52 63 76 64 2b 78 66 65 72 2e 6e 44 65 6c 74  eRcvd+xfer.nDelt
64d0: 61 52 63 76 64 2b 78 66 65 72 2e 6e 44 61 6e 67  aRcvd+xfer.nDang
64e0: 6c 69 6e 67 46 69 6c 65 3e 30 20 7c 7c 20 6e 65  lingFile>0 || ne
64f0: 77 50 68 61 6e 74 6f 6d 29 0a 20 20 20 20 20 26  wPhantom).     &
6500: 26 20 64 62 5f 65 78 69 73 74 73 28 22 53 45 4c  & db_exists("SEL
6510: 45 43 54 20 31 20 46 52 4f 4d 20 70 68 61 6e 74  ECT 1 FROM phant
6520: 6f 6d 22 29 0a 20 20 20 20 29 7b 0a 20 20 20 20  om").    ){.    
6530: 20 20 67 6f 20 3d 20 31 3b 0a 20 20 20 20 7d 0a    go = 1;.    }.
6540: 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 68 61  .    /* If we ha
6550: 76 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 66  ve one or more f
6560: 69 6c 65 73 20 71 75 65 75 65 64 20 74 6f 20 73  iles queued to s
6570: 65 6e 64 2c 20 74 68 65 6e 20 67 6f 0a 20 20 20  end, then go.   
6580: 20 2a 2a 20 61 6e 6f 74 68 65 72 20 72 6f 75 6e   ** another roun
6590: 64 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  d .    */.    if
65a0: 28 20 78 66 65 72 2e 6e 46 69 6c 65 53 65 6e 74  ( xfer.nFileSent
65b0: 2b 78 66 65 72 2e 6e 44 65 6c 74 61 53 65 6e 74  +xfer.nDeltaSent
65c0: 3e 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 20 3d  >0 ){.      go =
65d0: 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 3b 0a 20   1;.    }.  };. 
65e0: 20 68 74 74 70 5f 63 6c 6f 73 65 28 29 3b 0a 20   http_close();. 
65f0: 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74   db_end_transact
6600: 69 6f 6e 28 30 29 3b 0a 7d 0a                    ion(0);.}.