Hex Artifact Content
Not logged in

Artifact 72dbb4250397dea3b1de4c261eebf8d5a3965c08:

File src/xfer.c part of check-in [75c476ccd1] - Work on network synchronization by drh on 2007-07-23 20:33:04.

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 65 20 61 54 6f 6b  ../*.** The aTok
03e0: 65 6e 5b 30 2e 2e 6e 54 6f 6b 65 6e 2d 31 5d 20  en[0..nToken-1] 
03f0: 62 6c 6f 62 20 61 72 72 61 79 20 69 73 20 61 20  blob array is a 
0400: 70 61 72 73 65 20 6f 66 20 61 20 22 66 69 6c 65  parse of a "file
0410: 22 20 6c 69 6e 65 20 0a 2a 2a 20 6d 65 73 73 61  " line .** messa
0420: 67 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  ge.  This routin
0430: 65 20 66 69 6e 69 73 68 65 73 20 70 61 72 73 69  e finishes parsi
0440: 6e 67 20 74 68 61 74 20 6d 65 73 73 61 67 65 20  ng that message 
0450: 61 6e 64 20 64 6f 65 73 0a 2a 2a 20 61 20 72 65  and does.** a re
0460: 63 6f 72 64 20 69 6e 73 65 72 74 20 6f 66 20 74  cord insert of t
0470: 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54  he file..**.** T
0480: 68 65 20 66 69 6c 65 20 6c 69 6e 65 20 69 73 20  he file line is 
0490: 69 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f  in one of the fo
04a0: 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 66 6f 72 6d  llowing two form
04b0: 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 66 69  s:.**.**      fi
04c0: 6c 65 20 55 55 49 44 20 53 49 5a 45 20 5c 6e 20  le UUID SIZE \n 
04d0: 43 4f 4e 54 45 4e 54 0a 2a 2a 20 20 20 20 20 20  CONTENT.**      
04e0: 66 69 6c 65 20 55 55 49 44 20 44 45 4c 54 41 53  file UUID DELTAS
04f0: 52 43 20 53 49 5a 45 20 5c 6e 20 43 4f 4e 54 45  RC SIZE \n CONTE
0500: 4e 54 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e  NT.**.** The con
0510: 74 65 6e 74 20 69 73 20 53 49 5a 45 20 62 79 74  tent is SIZE byt
0520: 65 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66  es immediately f
0530: 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 6e 65 77  ollowing the new
0540: 6c 69 6e 65 2e 0a 2a 2a 20 49 66 20 44 45 4c 54  line..** If DELT
0550: 41 53 52 43 20 65 78 69 73 74 73 2c 20 74 68 65  ASRC exists, the
0560: 6e 20 74 68 65 20 43 4f 4e 54 45 4e 54 20 69 73  n the CONTENT is
0570: 20 61 20 64 65 6c 74 61 20 61 67 61 69 6e 73 74   a delta against
0580: 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20   the.** content 
0590: 6f 66 20 44 45 4c 54 41 53 52 43 2e 0a 2a 2a 0a  of DELTASRC..**.
05a0: 2a 2a 20 49 66 20 61 6e 79 20 65 72 72 6f 72 20  ** If any error 
05b0: 6f 63 63 75 72 73 2c 20 77 72 69 74 65 20 61 20  occurs, write a 
05c0: 6d 65 73 73 61 67 65 20 69 6e 74 6f 20 70 45 72  message into pEr
05d0: 72 20 77 68 69 63 68 20 68 61 73 20 61 6c 72 65  r which has alre
05e0: 61 64 79 0a 2a 2a 20 62 65 20 69 6e 69 74 69 61  ady.** be initia
05f0: 6c 69 7a 65 64 20 74 6f 20 61 6e 20 65 6d 70 74  lized to an empt
0600: 79 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 73 74 61  y string..*/.sta
0610: 74 69 63 20 76 6f 69 64 20 78 66 65 72 5f 61 63  tic void xfer_ac
0620: 63 65 70 74 5f 66 69 6c 65 28 42 6c 6f 62 20 2a  cept_file(Blob *
0630: 70 49 6e 2c 20 42 6c 6f 62 20 2a 61 54 6f 6b 65  pIn, Blob *aToke
0640: 6e 2c 20 69 6e 74 20 6e 54 6f 6b 65 6e 2c 20 42  n, int nToken, B
0650: 6c 6f 62 20 2a 70 45 72 72 29 7b 0a 20 20 69 6e  lob *pErr){.  in
0660: 74 20 6e 3b 0a 20 20 69 6e 74 20 72 69 64 3b 0a  t n;.  int rid;.
0670: 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 2c 20    Blob content, 
0680: 68 61 73 68 3b 0a 20 20 0a 20 20 69 66 28 20 6e  hash;.  .  if( n
0690: 54 6f 6b 65 6e 3c 33 20 7c 7c 20 6e 54 6f 6b 65  Token<3 || nToke
06a0: 6e 3e 34 20 7c 7c 20 21 62 6c 6f 62 5f 69 73 5f  n>4 || !blob_is_
06b0: 75 75 69 64 28 26 61 54 6f 6b 65 6e 5b 31 5d 29  uuid(&aToken[1])
06c0: 0a 20 20 20 20 20 20 20 7c 7c 20 21 62 6c 6f 62  .       || !blob
06d0: 5f 69 73 5f 69 6e 74 28 26 61 54 6f 6b 65 6e 5b  _is_int(&aToken[
06e0: 6e 54 6f 6b 65 6e 2d 31 5d 2c 20 26 6e 29 20 7c  nToken-1], &n) |
06f0: 7c 20 6e 3c 3d 30 0a 20 20 20 20 20 20 20 7c 7c  | n<=0.       ||
0700: 20 28 6e 54 6f 6b 65 6e 3d 3d 34 20 26 26 20 21   (nToken==4 && !
0710: 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26 61 54  blob_is_uuid(&aT
0720: 6f 6b 65 6e 5b 32 5d 29 29 20 29 7b 0a 20 20 20  oken[2])) ){.   
0730: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70 45   blob_appendf(pE
0740: 72 72 2c 20 22 6d 61 6c 66 6f 72 6d 65 64 20 66  rr, "malformed f
0750: 69 6c 65 20 6c 69 6e 65 22 29 3b 0a 20 20 20 20  ile line");.    
0760: 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 62 6c  return;.  }.  bl
0770: 6f 62 5f 7a 65 72 6f 28 26 63 6f 6e 74 65 6e 74  ob_zero(&content
0780: 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26  );.  blob_zero(&
0790: 68 61 73 68 29 3b 0a 20 20 62 6c 6f 62 5f 65 78  hash);.  blob_ex
07a0: 74 72 61 63 74 28 70 49 6e 2c 20 6e 2c 20 26 63  tract(pIn, n, &c
07b0: 6f 6e 74 65 6e 74 29 3b 0a 20 20 69 66 28 20 6e  ontent);.  if( n
07c0: 54 6f 6b 65 6e 3d 3d 34 20 29 7b 0a 20 20 20 20  Token==4 ){.    
07d0: 42 6c 6f 62 20 73 72 63 3b 0a 20 20 20 20 69 6e  Blob src;.    in
07e0: 74 20 73 72 63 69 64 20 3d 20 64 62 5f 69 6e 74  t srcid = db_int
07f0: 28 30 2c 20 22 53 45 4c 45 43 54 20 72 69 64 20  (0, "SELECT rid 
0800: 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20  FROM blob WHERE 
0810: 75 75 69 64 3d 25 42 22 2c 20 26 61 54 6f 6b 65  uuid=%B", &aToke
0820: 6e 5b 32 5d 29 3b 0a 20 20 20 20 69 66 28 20 73  n[2]);.    if( s
0830: 72 63 69 64 3d 3d 30 20 29 7b 0a 20 20 20 20 20  rcid==0 ){.     
0840: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70 45   blob_appendf(pE
0850: 72 72 2c 20 22 75 6e 6b 6e 6f 77 6e 20 64 65 6c  rr, "unknown del
0860: 74 61 20 73 6f 75 72 63 65 3a 20 25 62 22 2c 20  ta source: %b", 
0870: 26 61 54 6f 6b 65 6e 5b 32 5d 29 3b 0a 20 20 20  &aToken[2]);.   
0880: 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
0890: 0a 20 20 20 20 63 6f 6e 74 65 6e 74 5f 67 65 74  .    content_get
08a0: 28 73 72 63 69 64 2c 20 26 73 72 63 29 3b 0a 20  (srcid, &src);. 
08b0: 20 20 20 62 6c 6f 62 5f 64 65 6c 74 61 5f 61 70     blob_delta_ap
08c0: 70 6c 79 28 26 73 72 63 2c 20 26 63 6f 6e 74 65  ply(&src, &conte
08d0: 6e 74 2c 20 26 63 6f 6e 74 65 6e 74 29 3b 0a 20  nt, &content);. 
08e0: 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 73     blob_reset(&s
08f0: 72 63 29 3b 0a 20 20 7d 0a 20 20 73 68 61 31 73  rc);.  }.  sha1s
0900: 75 6d 5f 62 6c 6f 62 28 26 63 6f 6e 74 65 6e 74  um_blob(&content
0910: 2c 20 26 68 61 73 68 29 3b 0a 20 20 69 66 28 20  , &hash);.  if( 
0920: 21 62 6c 6f 62 5f 65 71 5f 73 74 72 28 26 61 54  !blob_eq_str(&aT
0930: 6f 6b 65 6e 5b 31 5d 2c 20 62 6c 6f 62 5f 73 74  oken[1], blob_st
0940: 72 28 26 68 61 73 68 29 2c 20 2d 31 29 20 29 7b  r(&hash), -1) ){
0950: 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64  .    blob_append
0960: 66 28 70 45 72 72 2c 20 22 63 6f 6e 74 65 6e 74  f(pErr, "content
0970: 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20   does not match 
0980: 73 68 61 31 20 68 61 73 68 22 29 3b 0a 20 20 7d  sha1 hash");.  }
0990: 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 68  .  blob_reset(&h
09a0: 61 73 68 29 3b 0a 20 20 72 69 64 20 3d 20 63 6f  ash);.  rid = co
09b0: 6e 74 65 6e 74 5f 70 75 74 28 26 63 6f 6e 74 65  ntent_put(&conte
09c0: 6e 74 2c 20 30 29 3b 0a 20 20 6d 61 6e 69 66 65  nt, 0);.  manife
09d0: 73 74 5f 63 72 6f 73 73 6c 69 6e 6b 28 72 69 64  st_crosslink(rid
09e0: 2c 20 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 69  , &content);.  i
09f0: 66 28 20 72 69 64 3d 3d 30 20 29 7b 0a 20 20 20  f( rid==0 ){.   
0a00: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70 45   blob_appendf(pE
0a10: 72 72 2c 20 22 25 73 22 2c 20 67 2e 7a 45 72 72  rr, "%s", g.zErr
0a20: 4d 73 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  Msg);.  }.}../*.
0a30: 2a 2a 20 53 65 6e 64 20 74 68 65 20 66 69 6c 65  ** Send the file
0a40: 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 72   identified by r
0a50: 69 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4f 75  id..**.** If pOu
0a60: 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74  t is not NULL, t
0a70: 68 65 6e 20 61 70 70 65 6e 64 20 74 68 65 20 74  hen append the t
0a80: 65 78 74 20 6f 66 20 74 68 65 20 73 65 6e 64 20  ext of the send 
0a90: 6d 65 73 73 61 67 65 0a 2a 2a 20 74 6f 20 70 4f  message.** to pO
0aa0: 75 74 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20  ut.  Otherwise, 
0ab0: 61 70 70 65 6e 64 20 74 68 65 20 74 65 78 74 20  append the text 
0ac0: 74 6f 20 74 68 65 20 43 47 49 20 6f 75 74 70 75  to the CGI outpu
0ad0: 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
0ae0: 20 73 65 6e 64 5f 66 69 6c 65 28 69 6e 74 20 72   send_file(int r
0af0: 69 64 2c 20 42 6c 6f 62 20 2a 70 4f 75 74 29 7b  id, Blob *pOut){
0b00: 0a 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 2c  .  Blob content,
0b10: 20 75 75 69 64 3b 0a 20 20 69 6e 74 20 73 69 7a   uuid;.  int siz
0b20: 65 3b 0a 0a 23 69 66 20 30 0a 53 45 4c 45 43 54  e;..#if 0.SELECT
0b30: 20 73 72 63 69 64 20 46 52 4f 4d 20 64 65 6c 74   srcid FROM delt
0b40: 61 0a 20 57 48 45 52 45 20 72 69 64 3d 25 64 0a  a. WHERE rid=%d.
0b50: 20 20 20 41 4e 44 20 45 58 49 53 54 53 28 53 45     AND EXISTS(SE
0b60: 4c 45 43 54 20 31 20 46 52 4f 4d 20 6f 6e 72 65  LECT 1 FROM onre
0b70: 6d 6f 74 65 20 57 48 45 52 45 20 72 69 64 3d 73  mote WHERE rid=s
0b80: 72 63 69 64 29 0a 55 4e 49 4f 4e 20 41 4c 4c 0a  rcid).UNION ALL.
0b90: 53 45 4c 45 43 54 20 69 64 20 46 52 4f 4d 20 64  SELECT id FROM d
0ba0: 65 6c 74 61 0a 20 57 48 45 52 45 20 73 72 63 69  elta. WHERE srci
0bb0: 64 3d 25 64 0a 20 20 20 41 4e 44 20 45 58 49 53  d=%d.   AND EXIS
0bc0: 54 53 28 53 45 4c 45 43 54 20 31 20 46 52 4f 4d  TS(SELECT 1 FROM
0bd0: 20 6f 6e 72 65 6d 6f 74 65 20 57 48 45 52 45 20   onremote WHERE 
0be0: 72 69 64 3d 64 65 6c 74 61 2e 72 69 64 29 0a 4c  rid=delta.rid).L
0bf0: 49 4d 49 54 20 31 0a 23 65 6e 64 69 66 0a 0a 20  IMIT 1.#endif.. 
0c00: 20 2f 2a 20 54 4f 44 4f 3a 0a 20 20 2a 2a 20 43   /* TODO:.  ** C
0c10: 68 65 63 6b 20 66 6f 72 20 72 65 6c 61 74 65 64  heck for related
0c20: 20 66 69 6c 65 73 20 69 6e 20 74 68 65 20 6f 6e   files in the on
0c30: 72 65 6d 6f 74 65 20 54 45 4d 50 20 74 61 62 6c  remote TEMP tabl
0c40: 65 2e 20 20 49 66 20 72 65 6c 61 74 65 64 0a 20  e.  If related. 
0c50: 20 2a 2a 20 66 69 6c 65 73 20 61 72 65 20 66 6f   ** files are fo
0c60: 75 6e 64 2c 20 74 68 65 6e 20 73 65 6e 64 20 61  und, then send a
0c70: 20 64 65 6c 74 61 20 72 61 74 68 65 72 20 74 68   delta rather th
0c80: 61 6e 20 74 68 65 20 77 68 6f 6c 65 20 66 69 6c  an the whole fil
0c90: 65 2e 0a 20 20 2a 2f 0a 0a 20 20 62 6c 6f 62 5f  e..  */..  blob_
0ca0: 7a 65 72 6f 28 26 75 75 69 64 29 3b 0a 20 20 64  zero(&uuid);.  d
0cb0: 62 5f 62 6c 6f 62 28 26 75 75 69 64 2c 20 22 53  b_blob(&uuid, "S
0cc0: 45 4c 45 43 54 20 75 75 69 64 20 46 52 4f 4d 20  ELECT uuid FROM 
0cd0: 62 6c 6f 62 20 57 48 45 52 45 20 72 69 64 3d 25  blob WHERE rid=%
0ce0: 64 20 41 4e 44 20 73 69 7a 65 3e 3d 30 22 2c 20  d AND size>=0", 
0cf0: 72 69 64 29 3b 0a 20 20 69 66 28 20 62 6c 6f 62  rid);.  if( blob
0d00: 5f 73 69 7a 65 28 26 75 75 69 64 29 3d 3d 30 20  _size(&uuid)==0 
0d10: 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
0d20: 0a 20 20 7d 0a 20 20 63 6f 6e 74 65 6e 74 5f 67  .  }.  content_g
0d30: 65 74 28 72 69 64 2c 20 26 63 6f 6e 74 65 6e 74  et(rid, &content
0d40: 29 3b 0a 20 20 73 69 7a 65 20 3d 20 62 6c 6f 62  );.  size = blob
0d50: 5f 73 69 7a 65 28 26 63 6f 6e 74 65 6e 74 29 3b  _size(&content);
0d60: 0a 20 20 69 66 28 20 70 4f 75 74 20 29 7b 0a 20  .  if( pOut ){. 
0d70: 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28     blob_appendf(
0d80: 70 4f 75 74 2c 20 22 66 69 6c 65 20 25 62 20 25  pOut, "file %b %
0d90: 64 5c 6e 22 2c 20 26 75 75 69 64 2c 20 73 69 7a  d\n", &uuid, siz
0da0: 65 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70  e);.    blob_app
0db0: 65 6e 64 28 70 4f 75 74 2c 20 62 6c 6f 62 5f 62  end(pOut, blob_b
0dc0: 75 66 66 65 72 28 26 63 6f 6e 74 65 6e 74 29 2c  uffer(&content),
0dd0: 20 73 69 7a 65 29 3b 0a 20 20 7d 65 6c 73 65 7b   size);.  }else{
0de0: 0a 20 20 20 20 63 67 69 5f 70 72 69 6e 74 66 28  .    cgi_printf(
0df0: 22 66 69 6c 65 20 25 62 20 25 64 5c 6e 22 2c 20  "file %b %d\n", 
0e00: 26 75 75 69 64 2c 20 73 69 7a 65 29 3b 0a 20 20  &uuid, size);.  
0e10: 20 20 63 67 69 5f 61 70 70 65 6e 64 5f 63 6f 6e    cgi_append_con
0e20: 74 65 6e 74 28 62 6c 6f 62 5f 62 75 66 66 65 72  tent(blob_buffer
0e30: 28 26 63 6f 6e 74 65 6e 74 29 2c 20 73 69 7a 65  (&content), size
0e40: 29 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f 72 65  );.  }.  blob_re
0e50: 73 65 74 28 26 63 6f 6e 74 65 6e 74 29 3b 0a 20  set(&content);. 
0e60: 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 75 75 69   blob_reset(&uui
0e70: 64 29 3b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65  d);.  db_multi_e
0e80: 78 65 63 28 22 49 4e 53 45 52 54 20 4f 52 20 49  xec("INSERT OR I
0e90: 47 4e 4f 52 45 20 49 4e 54 4f 20 6f 6e 72 65 6d  GNORE INTO onrem
0ea0: 6f 74 65 20 56 41 4c 55 45 53 28 25 64 29 22 2c  ote VALUES(%d)",
0eb0: 20 72 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20   rid);.  return 
0ec0: 73 69 7a 65 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  size;.}.../*.** 
0ed0: 53 65 6e 64 20 61 6c 6c 20 70 65 6e 64 69 6e 67  Send all pending
0ee0: 20 66 69 6c 65 73 2e 0a 2a 2f 0a 73 74 61 74 69   files..*/.stati
0ef0: 63 20 69 6e 74 20 73 65 6e 64 5f 61 6c 6c 5f 70  c int send_all_p
0f00: 65 6e 64 69 6e 67 28 42 6c 6f 62 20 2a 70 4f 75  ending(Blob *pOu
0f10: 74 29 7b 0a 20 20 69 6e 74 20 73 65 6e 74 20 3d  t){.  int sent =
0f20: 20 30 3b 0a 20 20 69 6e 74 20 6e 53 65 6e 74 20   0;.  int nSent 
0f30: 3d 20 30 3b 0a 20 20 69 6e 74 20 6d 61 78 53 69  = 0;.  int maxSi
0f40: 7a 65 20 3d 20 64 62 5f 67 65 74 5f 69 6e 74 28  ze = db_get_int(
0f50: 22 68 74 74 70 2d 6d 73 67 2d 73 69 7a 65 22 2c  "http-msg-size",
0f60: 20 31 30 30 30 30 30 30 29 3b 0a 20 20 53 74 6d   1000000);.  Stm
0f70: 74 20 71 3b 0a 23 69 66 20 30 0a 20 20 64 62 5f  t q;.#if 0.  db_
0f80: 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 20  multi_exec(.    
0f90: 22 43 52 45 41 54 45 20 54 45 4d 50 20 54 41 42  "CREATE TEMP TAB
0fa0: 4c 45 20 70 72 69 6f 72 69 74 79 28 72 69 64 20  LE priority(rid 
0fb0: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
0fc0: 4b 45 59 29 3b 22 0a 20 20 20 20 22 49 4e 53 45  KEY);".    "INSE
0fd0: 52 54 20 49 4e 54 4f 20 70 72 69 6f 72 69 74 79  RT INTO priority
0fe0: 22 0a 20 20 20 20 22 20 53 45 4c 45 43 54 20 73  ".    " SELECT s
0ff0: 72 63 69 64 20 46 52 4f 4d 20 64 65 6c 74 61 22  rcid FROM delta"
1000: 0a 20 20 20 20 22 20 20 57 48 45 52 45 20 45 58  .    "  WHERE EX
1010: 49 53 54 53 28 53 45 4c 45 43 54 20 31 20 46 52  ISTS(SELECT 1 FR
1020: 4f 4d 20 6f 6e 72 65 6d 6f 74 65 20 57 48 45 52  OM onremote WHER
1030: 45 20 6f 6e 72 65 6d 6f 74 65 2e 72 69 64 3d 64  E onremote.rid=d
1040: 65 6c 74 61 2e 72 69 64 29 22 0a 20 20 20 20 22  elta.rid)".    "
1050: 20 20 20 20 41 4e 44 20 45 58 49 53 54 53 28 53      AND EXISTS(S
1060: 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 70 65 6e  ELECT 1 FROM pen
1070: 64 69 6e 67 20 57 48 45 52 45 20 64 65 6c 74 61  ding WHERE delta
1080: 2e 73 72 63 69 64 3d 70 65 6e 64 69 6e 67 2e 72  .srcid=pending.r
1090: 69 64 29 3b 22 0a 20 20 20 20 22 49 4e 53 45 52  id);".    "INSER
10a0: 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f  T OR IGNORE INTO
10b0: 20 70 72 69 6f 72 69 74 79 22 0a 20 20 20 20 22   priority".    "
10c0: 20 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f 4d   SELECT rid FROM
10d0: 20 64 65 6c 74 61 22 0a 20 20 20 20 22 20 20 57   delta".    "  W
10e0: 48 45 52 45 20 45 58 49 53 54 53 28 53 45 4c 45  HERE EXISTS(SELE
10f0: 43 54 20 31 20 46 52 4f 4d 20 6f 6e 72 65 6d 6f  CT 1 FROM onremo
1100: 74 65 20 57 48 45 52 45 20 6f 6e 72 65 6d 6f 74  te WHERE onremot
1110: 65 2e 72 69 64 3d 64 65 6c 74 61 2e 73 72 63 69  e.rid=delta.srci
1120: 64 29 22 0a 20 20 20 20 22 20 20 20 20 41 4e 44  d)".    "    AND
1130: 20 45 58 49 53 54 53 28 53 45 4c 45 43 54 20 31   EXISTS(SELECT 1
1140: 20 46 52 4f 4d 20 70 65 6e 64 69 6e 67 20 57 48   FROM pending WH
1150: 45 52 45 20 64 65 6c 74 61 2e 72 69 64 3d 70 65  ERE delta.rid=pe
1160: 6e 64 69 6e 67 2e 72 69 64 29 3b 22 0a 20 20 29  nding.rid);".  )
1170: 3b 0a 20 20 77 68 69 6c 65 28 20 73 65 6e 74 3c  ;.  while( sent<
1180: 6d 61 78 53 69 7a 65 20 26 26 20 28 72 69 64 20  maxSize && (rid 
1190: 3d 20 64 62 5f 69 6e 74 28 30 2c 20 22 53 45 4c  = db_int(0, "SEL
11a0: 45 43 54 20 72 69 64 20 46 52 4f 4d 20 70 72 69  ECT rid FROM pri
11b0: 6f 72 69 74 79 22 29 29 21 3d 30 20 29 7b 0a 20  ority"))!=0 ){. 
11c0: 20 20 20 73 65 6e 74 20 2b 3d 20 73 65 6e 64 5f     sent += send_
11d0: 66 69 6c 65 28 72 69 64 2c 20 70 4f 75 74 29 3b  file(rid, pOut);
11e0: 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78  .    db_multi_ex
11f0: 65 63 28 0a 20 20 20 20 20 20 22 49 4e 53 45 52  ec(.      "INSER
1200: 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f  T OR IGNORE INTO
1210: 20 70 72 69 6f 72 69 74 79 22 0a 20 20 20 20 20   priority".     
1220: 20 22 20 53 45 4c 45 43 54 20 73 72 63 69 64 20   " SELECT srcid 
1230: 46 52 4f 4d 20 64 65 6c 74 61 20 57 48 45 52 45  FROM delta WHERE
1240: 20 72 69 64 3d 25 64 22 0a 20 20 20 20 20 20 22   rid=%d".      "
1250: 20 55 4e 49 4f 4e 20 41 4c 4c 22 0a 20 20 20 20   UNION ALL".    
1260: 20 20 22 20 53 45 4c 45 43 54 20 72 69 64 20 46    " SELECT rid F
1270: 52 4f 4d 20 64 65 6c 74 61 20 57 48 45 52 45 20  ROM delta WHERE 
1280: 73 72 63 69 64 3d 25 64 22 2c 0a 20 20 20 20 20  srcid=%d",.     
1290: 20 72 69 64 2c 20 72 69 64 0a 20 20 20 20 29 3b   rid, rid.    );
12a0: 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 64 62  .  }.#endif.  db
12b0: 5f 70 72 65 70 61 72 65 28 26 71 2c 20 22 53 45  _prepare(&q, "SE
12c0: 4c 45 43 54 20 72 69 64 20 46 52 4f 4d 20 70 65  LECT rid FROM pe
12d0: 6e 64 69 6e 67 22 29 3b 0a 20 20 77 68 69 6c 65  nding");.  while
12e0: 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53  ( db_step(&q)==S
12f0: 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20  QLITE_ROW ){.   
1300: 20 69 6e 74 20 72 69 64 20 3d 20 64 62 5f 63 6f   int rid = db_co
1310: 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 30 29 3b  lumn_int(&q, 0);
1320: 0a 20 20 20 20 69 66 28 20 73 65 6e 74 3c 6d 61  .    if( sent<ma
1330: 78 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 73  xSize ){.      s
1340: 65 6e 74 20 2b 3d 20 73 65 6e 64 5f 66 69 6c 65  ent += send_file
1350: 28 72 69 64 2c 20 70 4f 75 74 29 3b 0a 20 20 20  (rid, pOut);.   
1360: 20 20 20 6e 53 65 6e 74 2b 2b 3b 0a 20 20 20 20     nSent++;.    
1370: 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 68 61  }else{.      cha
1380: 72 20 2a 7a 55 75 69 64 20 3d 20 64 62 5f 74 65  r *zUuid = db_te
1390: 78 74 28 30 2c 0a 20 20 20 20 20 20 20 20 20 20  xt(0,.          
13a0: 20 20 20 20 20 20 20 20 20 20 20 20 22 53 45 4c              "SEL
13b0: 45 43 54 20 75 75 69 64 20 46 52 4f 4d 20 62 6c  ECT uuid FROM bl
13c0: 6f 62 20 57 48 45 52 45 20 72 69 64 3d 25 64 20  ob WHERE rid=%d 
13d0: 41 4e 44 20 73 69 7a 65 3e 3d 30 22 2c 20 72 69  AND size>=0", ri
13e0: 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 55  d);.      if( zU
13f0: 75 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 69  uid ){.        i
1400: 66 28 20 70 4f 75 74 20 29 7b 0a 20 20 20 20 20  f( pOut ){.     
1410: 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64       blob_append
1420: 66 28 70 4f 75 74 2c 20 22 69 67 6f 74 20 25 73  f(pOut, "igot %s
1430: 5c 6e 22 2c 20 7a 55 75 69 64 29 3b 0a 20 20 20  \n", zUuid);.   
1440: 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1450: 20 20 20 20 20 20 63 67 69 5f 70 72 69 6e 74 66        cgi_printf
1460: 28 22 69 67 6f 74 20 25 73 5c 6e 22 2c 20 7a 55  ("igot %s\n", zU
1470: 75 69 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  uid);.        }.
1480: 20 20 20 20 20 20 20 20 66 72 65 65 28 7a 55 75          free(zUu
1490: 69 64 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  id);.      }.   
14a0: 20 7d 0a 20 20 7d 0a 20 20 64 62 5f 66 69 6e 61   }.  }.  db_fina
14b0: 6c 69 7a 65 28 26 71 29 3b 0a 20 20 0a 23 69 66  lize(&q);.  .#if
14c0: 20 30 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78   0.  db_multi_ex
14d0: 65 63 28 22 44 52 4f 50 20 54 41 42 4c 45 20 70  ec("DROP TABLE p
14e0: 72 69 6f 72 69 74 79 22 29 3b 0a 23 65 6e 64 69  riority");.#endi
14f0: 66 0a 20 20 72 65 74 75 72 6e 20 6e 53 65 6e 74  f.  return nSent
1500: 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63  ;.}.../*.** Chec
1510: 6b 20 74 68 65 20 73 69 67 6e 61 74 75 72 65 20  k the signature 
1520: 6f 6e 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f  on an applicatio
1530: 6e 2f 78 2d 66 6f 73 73 69 6c 20 70 61 79 6c 6f  n/x-fossil paylo
1540: 61 64 20 72 65 63 65 69 76 65 64 20 62 79 0a 2a  ad received by.*
1550: 2a 20 74 68 65 20 48 54 54 50 20 73 65 72 76 65  * the HTTP serve
1560: 72 2e 20 20 54 68 65 20 73 69 67 6e 61 74 75 72  r.  The signatur
1570: 65 20 69 73 20 61 20 6c 69 6e 65 20 6f 66 20 74  e is a line of t
1580: 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72  he following for
1590: 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  m:.**.**        
15a0: 6c 6f 67 69 6e 20 4c 4f 47 49 4e 20 4e 4f 4e 43  login LOGIN NONC
15b0: 45 20 53 49 47 4e 41 54 55 52 45 0a 2a 2a 0a 2a  E SIGNATURE.**.*
15c0: 2a 20 54 68 65 20 4e 4f 4e 43 45 20 69 73 20 61  * The NONCE is a
15d0: 20 72 61 6e 64 6f 6d 20 73 74 72 69 6e 67 2e 20   random string. 
15e0: 20 54 68 65 20 73 65 72 76 65 72 20 77 69 6c 6c   The server will
15f0: 20 6e 65 76 65 72 20 61 63 63 65 70 74 20 61 0a   never accept a.
1600: 2a 2a 20 72 65 70 65 61 74 20 4e 4f 4e 43 45 2e  ** repeat NONCE.
1610: 20 20 53 49 47 4e 41 54 55 52 45 20 69 73 20 74    SIGNATURE is t
1620: 68 65 20 53 48 41 31 20 63 68 65 63 6b 73 75 6d  he SHA1 checksum
1630: 20 6f 66 20 74 68 65 20 4e 4f 4e 43 45 20 0a 2a   of the NONCE .*
1640: 2a 20 63 6f 6e 63 61 74 65 6e 61 74 65 64 20 77  * concatenated w
1650: 69 74 68 20 74 68 65 20 75 73 65 72 73 20 70 61  ith the users pa
1660: 73 73 77 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  ssword..**.** Th
1670: 65 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20  e parameters to 
1680: 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 72 65  this routine are
1690: 20 65 70 68 65 72 6d 65 72 61 6c 20 62 6c 6f 62   ephermeral blob
16a0: 73 20 68 6f 6c 64 69 6e 67 20 74 68 65 0a 2a 2a  s holding the.**
16b0: 20 4c 4f 47 49 4e 2c 20 4e 4f 4e 43 45 20 61 6e   LOGIN, NONCE an
16c0: 64 20 53 49 47 4e 41 54 55 52 45 2e 0a 2a 2a 0a  d SIGNATURE..**.
16d0: 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
16e0: 61 74 74 65 6d 70 74 73 20 74 6f 20 6c 6f 63 61  attempts to loca
16f0: 74 65 20 74 68 65 20 75 73 65 72 20 61 6e 64 20  te the user and 
1700: 76 65 72 69 66 79 20 74 68 65 20 73 69 67 6e 61  verify the signa
1710: 74 75 72 65 2e 0a 2a 2a 20 49 66 20 65 76 65 72  ture..** If ever
1720: 79 74 68 69 6e 67 20 63 68 65 63 6b 73 20 6f 75  ything checks ou
1730: 74 2c 20 74 68 65 20 55 53 45 52 2e 43 41 50 20  t, the USER.CAP 
1740: 63 6f 6c 75 6d 6e 20 66 6f 72 20 74 68 65 20 55  column for the U
1750: 53 45 52 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20  SER table.** is 
1760: 63 6f 6e 73 75 6c 74 65 64 20 74 6f 20 73 65 74  consulted to set
1770: 20 70 72 69 76 69 6c 65 67 65 73 20 69 6e 20 74   privileges in t
1780: 68 65 20 67 6c 6f 62 61 6c 20 67 20 76 61 72 69  he global g vari
1790: 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  able..**.** If a
17a0: 6e 79 74 68 69 6e 67 20 66 61 69 6c 73 20 74 6f  nything fails to
17b0: 20 63 68 65 63 6b 20 6f 75 74 2c 20 6e 6f 20 63   check out, no c
17c0: 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64 65 20  hanges are made 
17d0: 74 6f 20 70 72 69 76 69 6c 65 67 65 73 2e 0a 2a  to privileges..*
17e0: 2a 0a 2a 2a 20 53 69 67 6e 61 74 75 72 65 20 67  *.** Signature g
17f0: 65 6e 65 72 61 74 69 6f 6e 20 6f 6e 20 74 68 65  eneration on the
1800: 20 63 6c 69 65 6e 74 20 73 69 64 65 20 69 73 20   client side is 
1810: 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65 20 0a  handled by the .
1820: 2a 2a 20 68 74 74 70 5f 65 78 63 68 61 6e 67 65  ** http_exchange
1830: 28 29 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 76  () routine..*/.v
1840: 6f 69 64 20 63 68 65 63 6b 5f 6c 6f 67 69 6e 28  oid check_login(
1850: 42 6c 6f 62 20 2a 70 4c 6f 67 69 6e 2c 20 42 6c  Blob *pLogin, Bl
1860: 6f 62 20 2a 70 4e 6f 6e 63 65 2c 20 42 6c 6f 62  ob *pNonce, Blob
1870: 20 2a 70 53 69 67 29 7b 0a 20 20 53 74 6d 74 20   *pSig){.  Stmt 
1880: 71 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  q;.  int rc;..  
1890: 69 66 28 20 64 62 5f 65 78 69 73 74 73 28 22 53  if( db_exists("S
18a0: 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 72 63 76  ELECT 1 FROM rcv
18b0: 66 72 6f 6d 20 57 48 45 52 45 20 6e 6f 6e 63 65  from WHERE nonce
18c0: 3d 25 42 22 2c 20 70 4e 6f 6e 63 65 29 20 29 7b  =%B", pNonce) ){
18d0: 0a 20 20 20 20 72 65 74 75 72 6e 3b 20 20 2f 2a  .    return;  /*
18e0: 20 4e 65 76 65 72 20 61 63 63 65 70 74 20 61 20   Never accept a 
18f0: 72 65 70 65 61 74 65 64 20 6e 6f 6e 63 65 20 2a  repeated nonce *
1900: 2f 0a 20 20 7d 0a 20 20 64 62 5f 70 72 65 70 61  /.  }.  db_prepa
1910: 72 65 28 26 71 2c 20 22 53 45 4c 45 43 54 20 70  re(&q, "SELECT p
1920: 77 2c 20 63 61 70 2c 20 75 69 64 20 46 52 4f 4d  w, cap, uid FROM
1930: 20 75 73 65 72 20 57 48 45 52 45 20 6c 6f 67 69   user WHERE logi
1940: 6e 3d 25 42 22 2c 20 70 4c 6f 67 69 6e 29 3b 0a  n=%B", pLogin);.
1950: 20 20 69 66 28 20 64 62 5f 73 74 65 70 28 26 71    if( db_step(&q
1960: 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  )==SQLITE_ROW ){
1970: 0a 20 20 20 20 42 6c 6f 62 20 70 77 2c 20 63 6f  .    Blob pw, co
1980: 6d 62 69 6e 65 64 2c 20 68 61 73 68 3b 0a 20 20  mbined, hash;.  
1990: 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 70 77 29    blob_zero(&pw)
19a0: 3b 0a 20 20 20 20 64 62 5f 65 70 68 65 6d 65 72  ;.    db_ephemer
19b0: 61 6c 5f 62 6c 6f 62 28 26 71 2c 20 30 2c 20 26  al_blob(&q, 0, &
19c0: 70 77 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 7a 65  pw);.    blob_ze
19d0: 72 6f 28 26 63 6f 6d 62 69 6e 65 64 29 3b 0a 20  ro(&combined);. 
19e0: 20 20 20 62 6c 6f 62 5f 63 6f 70 79 28 26 63 6f     blob_copy(&co
19f0: 6d 62 69 6e 65 64 2c 20 70 4e 6f 6e 63 65 29 3b  mbined, pNonce);
1a00: 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64  .    blob_append
1a10: 28 26 63 6f 6d 62 69 6e 65 64 2c 20 62 6c 6f 62  (&combined, blob
1a20: 5f 62 75 66 66 65 72 28 26 70 77 29 2c 20 62 6c  _buffer(&pw), bl
1a30: 6f 62 5f 73 69 7a 65 28 26 70 77 29 29 3b 0a 20  ob_size(&pw));. 
1a40: 20 20 20 73 68 61 31 73 75 6d 5f 62 6c 6f 62 28     sha1sum_blob(
1a50: 26 63 6f 6d 62 69 6e 65 64 2c 20 26 68 61 73 68  &combined, &hash
1a60: 29 3b 0a 20 20 20 20 72 63 20 3d 20 62 6c 6f 62  );.    rc = blob
1a70: 5f 63 6f 6d 70 61 72 65 28 26 68 61 73 68 2c 20  _compare(&hash, 
1a80: 70 53 69 67 29 3b 0a 20 20 20 20 62 6c 6f 62 5f  pSig);.    blob_
1a90: 72 65 73 65 74 28 26 68 61 73 68 29 3b 0a 20 20  reset(&hash);.  
1aa0: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 63 6f    blob_reset(&co
1ab0: 6d 62 69 6e 65 64 29 3b 0a 20 20 20 20 69 66 28  mbined);.    if(
1ac0: 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20   rc==0 ){.      
1ad0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 61 70  const char *zCap
1ae0: 3b 0a 20 20 20 20 20 20 7a 43 61 70 20 3d 20 64  ;.      zCap = d
1af0: 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 26 71  b_column_text(&q
1b00: 2c 20 31 29 3b 0a 20 20 20 20 20 20 6c 6f 67 69  , 1);.      logi
1b10: 6e 5f 73 65 74 5f 63 61 70 61 62 69 6c 69 74 69  n_set_capabiliti
1b20: 65 73 28 7a 43 61 70 29 3b 0a 20 20 20 20 20 20  es(zCap);.      
1b30: 67 2e 75 73 65 72 55 69 64 20 3d 20 64 62 5f 63  g.userUid = db_c
1b40: 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 32 29  olumn_int(&q, 2)
1b50: 3b 0a 20 20 20 20 20 20 67 2e 7a 4c 6f 67 69 6e  ;.      g.zLogin
1b60: 20 3d 20 6d 70 72 69 6e 74 66 28 22 25 62 22 2c   = mprintf("%b",
1b70: 20 70 4c 6f 67 69 6e 29 3b 0a 20 20 20 20 20 20   pLogin);.      
1b80: 67 2e 7a 4e 6f 6e 63 65 20 3d 20 6d 70 72 69 6e  g.zNonce = mprin
1b90: 74 66 28 22 25 62 22 2c 20 70 4e 6f 6e 63 65 29  tf("%b", pNonce)
1ba0: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 64 62  ;.    }.  }.  db
1bb0: 5f 72 65 73 65 74 28 26 71 29 3b 0a 7d 0a 0a 0a  _reset(&q);.}...
1bc0: 2f 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 76 61  /*.** If this va
1bd0: 72 69 61 62 6c 65 20 69 73 20 73 65 74 2c 20 64  riable is set, d
1be0: 69 73 61 62 6c 65 20 6c 6f 67 69 6e 20 63 68 65  isable login che
1bf0: 63 6b 73 2e 20 20 55 73 65 64 20 66 6f 72 20 64  cks.  Used for d
1c00: 65 62 75 67 67 69 6e 67 0a 2a 2a 20 6f 6e 6c 79  ebugging.** only
1c10: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1c20: 64 69 73 61 62 6c 65 4c 6f 67 69 6e 20 3d 20 30  disableLogin = 0
1c30: 3b 0a 0a 2f 2a 0a 2a 2a 20 57 45 42 50 41 47 45  ;../*.** WEBPAGE
1c40: 3a 20 78 66 65 72 0a 2a 2a 0a 2a 2a 20 54 68 69  : xfer.**.** Thi
1c50: 73 20 69 73 20 74 68 65 20 74 72 61 6e 73 66 65  s is the transfe
1c60: 72 20 68 61 6e 64 6c 65 72 20 6f 6e 20 74 68 65  r handler on the
1c70: 20 73 65 72 76 65 72 20 73 69 64 65 2e 20 20 54   server side.  T
1c80: 68 65 20 74 72 61 6e 73 66 65 72 0a 2a 2a 20 6d  he transfer.** m
1c90: 65 73 73 61 67 65 20 68 61 73 20 62 65 65 6e 20  essage has been 
1ca0: 75 6e 63 6f 6d 70 72 65 73 73 65 64 20 61 6e 64  uncompressed and
1cb0: 20 70 6c 61 63 65 64 20 69 6e 20 74 68 65 20 67   placed in the g
1cc0: 2e 63 67 69 49 6e 20 62 6c 6f 62 2e 0a 2a 2a 20  .cgiIn blob..** 
1cd0: 50 72 6f 63 65 73 73 20 74 68 69 73 20 6d 65 73  Process this mes
1ce0: 73 61 67 65 20 61 6e 64 20 66 6f 72 6d 20 61 6e  sage and form an
1cf0: 20 61 70 70 72 6f 70 72 69 61 74 65 20 72 65 70   appropriate rep
1d00: 6c 79 2e 0a 2a 2f 0a 76 6f 69 64 20 70 61 67 65  ly..*/.void page
1d10: 5f 78 66 65 72 28 76 6f 69 64 29 7b 0a 20 20 69  _xfer(void){.  i
1d20: 6e 74 20 6e 54 6f 6b 65 6e 3b 0a 20 20 69 6e 74  nt nToken;.  int
1d30: 20 69 73 50 75 6c 6c 20 3d 20 30 3b 0a 20 20 69   isPull = 0;.  i
1d40: 6e 74 20 69 73 50 75 73 68 20 3d 20 30 3b 0a 20  nt isPush = 0;. 
1d50: 20 69 6e 74 20 6e 45 72 72 20 3d 20 30 3b 0a 20   int nErr = 0;. 
1d60: 20 42 6c 6f 62 20 6c 69 6e 65 2c 20 65 72 72 6d   Blob line, errm
1d70: 73 67 2c 20 61 54 6f 6b 65 6e 5b 35 5d 3b 0a 0a  sg, aToken[5];..
1d80: 20 20 64 62 5f 62 65 67 69 6e 5f 74 72 61 6e 73    db_begin_trans
1d90: 61 63 74 69 6f 6e 28 29 3b 0a 20 20 62 6c 6f 62  action();.  blob
1da0: 61 72 72 61 79 5f 7a 65 72 6f 28 61 54 6f 6b 65  array_zero(aToke
1db0: 6e 2c 20 63 6f 75 6e 74 28 61 54 6f 6b 65 6e 29  n, count(aToken)
1dc0: 29 3b 0a 20 20 63 67 69 5f 73 65 74 5f 63 6f 6e  );.  cgi_set_con
1dd0: 74 65 6e 74 5f 74 79 70 65 28 67 2e 7a 43 6f 6e  tent_type(g.zCon
1de0: 74 65 6e 74 54 79 70 65 29 3b 0a 20 20 62 6c 6f  tentType);.  blo
1df0: 62 5f 7a 65 72 6f 28 26 65 72 72 6d 73 67 29 3b  b_zero(&errmsg);
1e00: 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63  .  db_multi_exec
1e10: 28 0a 20 20 20 20 20 22 43 52 45 41 54 45 20 54  (.     "CREATE T
1e20: 45 4d 50 20 54 41 42 4c 45 20 6f 6e 72 65 6d 6f  EMP TABLE onremo
1e30: 74 65 28 72 69 64 20 49 4e 54 45 47 45 52 20 50  te(rid INTEGER P
1e40: 52 49 4d 41 52 59 20 4b 45 59 29 3b 22 20 2f 2a  RIMARY KEY);" /*
1e50: 20 43 6c 69 65 6e 74 20 68 61 73 20 2a 2f 0a 20   Client has */. 
1e60: 20 20 20 20 22 43 52 45 41 54 45 20 54 45 4d 50      "CREATE TEMP
1e70: 20 54 41 42 4c 45 20 70 65 6e 64 69 6e 67 28 72   TABLE pending(r
1e80: 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  id INTEGER PRIMA
1e90: 52 59 20 4b 45 59 29 3b 22 20 20 2f 2a 20 43 6c  RY KEY);"  /* Cl
1ea0: 69 65 6e 74 20 6e 65 65 64 73 20 2a 2f 0a 20 20  ient needs */.  
1eb0: 29 3b 0a 20 20 77 68 69 6c 65 28 20 62 6c 6f 62  );.  while( blob
1ec0: 5f 6c 69 6e 65 28 26 67 2e 63 67 69 49 6e 2c 20  _line(&g.cgiIn, 
1ed0: 26 6c 69 6e 65 29 20 29 7b 0a 20 20 20 20 6e 54  &line) ){.    nT
1ee0: 6f 6b 65 6e 20 3d 20 62 6c 6f 62 5f 74 6f 6b 65  oken = blob_toke
1ef0: 6e 69 7a 65 28 26 6c 69 6e 65 2c 20 61 54 6f 6b  nize(&line, aTok
1f00: 65 6e 2c 20 63 6f 75 6e 74 28 61 54 6f 6b 65 6e  en, count(aToken
1f10: 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 20 20 66 69  ));..    /*   fi
1f20: 6c 65 20 55 55 49 44 20 53 49 5a 45 20 5c 6e 20  le UUID SIZE \n 
1f30: 43 4f 4e 54 45 4e 54 0a 20 20 20 20 2a 2a 20 20  CONTENT.    **  
1f40: 20 66 69 6c 65 20 55 55 49 44 20 44 45 4c 54 41   file UUID DELTA
1f50: 53 52 43 20 53 49 5a 45 20 5c 6e 20 43 4f 4e 54  SRC SIZE \n CONT
1f60: 45 4e 54 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ENT.    **.    *
1f70: 2a 20 41 63 63 65 70 74 20 61 20 66 69 6c 65 20  * Accept a file 
1f80: 66 72 6f 6d 20 74 68 65 20 63 6c 69 65 6e 74 2e  from the client.
1f90: 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
1fa0: 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b  blob_eq(&aToken[
1fb0: 30 5d 2c 20 22 66 69 6c 65 22 29 20 26 26 20 6e  0], "file") && n
1fc0: 54 6f 6b 65 6e 3e 3d 32 20 26 26 20 6e 54 6f 6b  Token>=2 && nTok
1fd0: 65 6e 3c 3d 33 20 29 7b 0a 20 20 20 20 20 20 69  en<=3 ){.      i
1fe0: 66 28 20 21 69 73 50 75 73 68 20 29 7b 0a 20 20  f( !isPush ){.  
1ff0: 20 20 20 20 20 20 63 67 69 5f 72 65 73 65 74 5f        cgi_reset_
2000: 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 20 20 20  content();.     
2010: 20 20 20 40 20 65 72 72 6f 72 20 6e 6f 74 5c 73     @ error not\s
2020: 61 75 74 68 6f 72 69 7a 65 64 5c 73 74 6f 5c 73  authorized\sto\s
2030: 77 72 69 74 65 0a 20 20 20 20 20 20 20 20 6e 45  write.        nE
2040: 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 62 72  rr++;.        br
2050: 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
2060: 20 20 20 78 66 65 72 5f 61 63 63 65 70 74 5f 66     xfer_accept_f
2070: 69 6c 65 28 26 67 2e 63 67 69 49 6e 2c 20 61 54  ile(&g.cgiIn, aT
2080: 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 2c 20 26 65  oken, nToken, &e
2090: 72 72 6d 73 67 29 3b 0a 20 20 20 20 20 20 69 66  rrmsg);.      if
20a0: 28 20 62 6c 6f 62 5f 73 69 7a 65 28 26 65 72 72  ( blob_size(&err
20b0: 6d 73 67 29 20 29 7b 0a 20 20 20 20 20 20 20 20  msg) ){.        
20c0: 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e 74 65 6e  cgi_reset_conten
20d0: 74 28 29 3b 0a 20 20 20 20 20 20 20 20 40 20 65  t();.        @ e
20e0: 72 72 6f 72 20 25 54 28 62 6c 6f 62 5f 73 74 72  rror %T(blob_str
20f0: 28 26 65 72 72 6d 73 67 29 29 0a 20 20 20 20 20  (&errmsg)).     
2100: 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20     nErr++;.     
2110: 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2120: 7d 0a 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20  }.    }else..   
2130: 20 2f 2a 20 20 20 67 69 6d 6d 65 20 55 55 49 44   /*   gimme UUID
2140: 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43  .    **.    ** C
2150: 6c 69 65 6e 74 20 69 73 20 72 65 71 75 65 73 74  lient is request
2160: 69 6e 67 20 61 20 66 69 6c 65 0a 20 20 20 20 2a  ing a file.    *
2170: 2f 0a 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65  /.    if( blob_e
2180: 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 67  q(&aToken[0], "g
2190: 69 6d 6d 65 22 29 20 26 26 20 6e 54 6f 6b 65 6e  imme") && nToken
21a0: 3d 3d 32 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75  ==2 && blob_is_u
21b0: 75 69 64 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 20  uid(&aToken[1]) 
21c0: 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73 50  ){.      if( isP
21d0: 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 64  ull ){.        d
21e0: 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20  b_multi_exec(.  
21f0: 20 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 20          "INSERT 
2200: 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 70  OR IGNORE INTO p
2210: 65 6e 64 69 6e 67 28 72 69 64 29 20 22 0a 20 20  ending(rid) ".  
2220: 20 20 20 20 20 20 20 20 22 53 45 4c 45 43 54 20          "SELECT 
2230: 72 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48  rid FROM blob WH
2240: 45 52 45 20 75 75 69 64 3d 25 42 20 41 4e 44 20  ERE uuid=%B AND 
2250: 73 69 7a 65 3e 3d 30 22 2c 20 26 61 54 6f 6b 65  size>=0", &aToke
2260: 6e 5b 31 5d 0a 20 20 20 20 20 20 20 20 29 3b 0a  n[1].        );.
2270: 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
2280: 65 0a 0a 20 20 20 20 2f 2a 20 20 20 69 67 6f 74  e..    /*   igot
2290: 20 55 55 49 44 0a 20 20 20 20 2a 2a 20 20 20 6c   UUID.    **   l
22a0: 65 61 66 20 55 55 49 44 0a 20 20 20 20 2a 2a 0a  eaf UUID.    **.
22b0: 20 20 20 20 2a 2a 20 43 6c 69 65 6e 74 20 61 6e      ** Client an
22c0: 6e 6f 75 6e 63 65 73 20 74 68 61 74 20 69 74 20  nounces that it 
22d0: 68 61 73 20 61 20 70 61 72 74 69 63 75 6c 61 72  has a particular
22e0: 20 66 69 6c 65 0a 20 20 20 20 2a 2f 0a 20 20 20   file.    */.   
22f0: 20 69 66 28 20 6e 54 6f 6b 65 6e 3d 3d 32 0a 20   if( nToken==2. 
2300: 20 20 20 20 20 20 20 20 20 26 26 20 28 62 6c 6f           && (blo
2310: 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c  b_eq(&aToken[0],
2320: 20 22 69 67 6f 74 22 29 20 7c 7c 20 62 6c 6f 62   "igot") || blob
2330: 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 22  _eq(&aToken[0],"
2340: 6c 65 61 66 22 29 29 0a 20 20 20 20 20 20 20 20  leaf")).        
2350: 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69    && blob_is_uui
2360: 64 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 20 29 7b  d(&aToken[1]) ){
2370: 0a 20 20 20 20 20 20 69 66 28 20 69 73 50 75 6c  .      if( isPul
2380: 6c 20 7c 7c 20 69 73 50 75 73 68 20 29 7b 0a 20  l || isPush ){. 
2390: 20 20 20 20 20 20 20 69 6e 74 20 72 69 64 20 3d         int rid =
23a0: 20 64 62 5f 69 6e 74 28 30 2c 20 22 53 45 4c 45   db_int(0, "SELE
23b0: 43 54 20 72 69 64 20 46 52 4f 4d 20 62 6c 6f 62  CT rid FROM blob
23c0: 20 57 48 45 52 45 20 75 75 69 64 3d 25 42 22 2c   WHERE uuid=%B",
23d0: 20 26 61 54 6f 6b 65 6e 5b 31 5d 29 3b 0a 20 20   &aToken[1]);.  
23e0: 20 20 20 20 20 20 69 66 28 20 72 69 64 3e 30 20        if( rid>0 
23f0: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 5f  ){.          db_
2400: 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 20  multi_exec(.    
2410: 20 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 20          "INSERT 
2420: 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 6f  OR IGNORE INTO o
2430: 6e 72 65 6d 6f 74 65 28 72 69 64 29 20 56 41 4c  nremote(rid) VAL
2440: 55 45 53 28 25 64 29 22 2c 20 72 69 64 0a 20 20  UES(%d)", rid.  
2450: 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20          );.     
2460: 20 20 20 20 20 69 66 28 20 69 73 50 75 6c 6c 20       if( isPull 
2470: 26 26 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b  && blob_eq(&aTok
2480: 65 6e 5b 30 5d 2c 20 22 6c 65 61 66 22 29 20 29  en[0], "leaf") )
2490: 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 62  {.            db
24a0: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20  _multi_exec(.   
24b0: 20 20 20 20 20 20 20 20 20 20 20 22 49 4e 53 45             "INSE
24c0: 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54  RT OR IGNORE INT
24d0: 4f 20 70 65 6e 64 69 6e 67 28 72 69 64 29 20 22  O pending(rid) "
24e0: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22  .              "
24f0: 53 45 4c 45 43 54 20 63 69 64 20 46 52 4f 4d 20  SELECT cid FROM 
2500: 70 6c 69 6e 6b 20 57 48 45 52 45 20 70 69 64 3d  plink WHERE pid=
2510: 25 64 22 2c 20 72 69 64 0a 20 20 20 20 20 20 20  %d", rid.       
2520: 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20       );.        
2530: 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73    }.        }els
2540: 65 20 69 66 28 20 69 73 50 75 73 68 20 29 7b 0a  e if( isPush ){.
2550: 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 65 6e            conten
2560: 74 5f 70 75 74 28 30 2c 20 62 6c 6f 62 5f 73 74  t_put(0, blob_st
2570: 72 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 29 3b 0a  r(&aToken[1]));.
2580: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2590: 7d 0a 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20  }.    }else..   
25a0: 20 2f 2a 20 20 20 20 70 75 6c 6c 20 20 53 45 52   /*    pull  SER
25b0: 56 45 52 43 4f 44 45 20 20 50 52 4f 4a 45 43 54  VERCODE  PROJECT
25c0: 43 4f 44 45 0a 20 20 20 20 2a 2a 20 20 20 20 70  CODE.    **    p
25d0: 75 73 68 20 20 53 45 52 56 45 52 43 4f 44 45 20  ush  SERVERCODE 
25e0: 20 50 52 4f 4a 45 43 54 43 4f 44 45 0a 20 20 20   PROJECTCODE.   
25f0: 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 63   **.    ** The c
2600: 6c 69 65 6e 74 20 77 61 6e 74 73 20 65 69 74 68  lient wants eith
2610: 65 72 20 73 65 6e 64 20 6f 72 20 72 65 63 65 69  er send or recei
2620: 76 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  ve.    */.    if
2630: 28 20 6e 54 6f 6b 65 6e 3d 3d 33 0a 20 20 20 20  ( nToken==3.    
2640: 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 62             && (b
2650: 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30  lob_eq(&aToken[0
2660: 5d 2c 20 22 70 75 6c 6c 22 29 20 7c 7c 20 62 6c  ], "pull") || bl
2670: 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d  ob_eq(&aToken[0]
2680: 2c 20 22 70 75 73 68 22 29 29 0a 20 20 20 20 20  , "push")).     
2690: 20 20 20 20 20 20 20 20 20 20 26 26 20 62 6c 6f            && blo
26a0: 62 5f 69 73 5f 75 75 69 64 28 26 61 54 6f 6b 65  b_is_uuid(&aToke
26b0: 6e 5b 31 5d 29 20 26 26 20 62 6c 6f 62 5f 69 73  n[1]) && blob_is
26c0: 5f 75 75 69 64 28 26 61 54 6f 6b 65 6e 5b 32 5d  _uuid(&aToken[2]
26d0: 29 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74  ) ){.      const
26e0: 20 63 68 61 72 20 2a 7a 53 43 6f 64 65 3b 0a 20   char *zSCode;. 
26f0: 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
2700: 2a 7a 50 43 6f 64 65 3b 0a 0a 20 20 20 20 20 20  *zPCode;..      
2710: 7a 53 43 6f 64 65 20 3d 20 64 62 5f 67 65 74 28  zSCode = db_get(
2720: 22 73 65 72 76 65 72 2d 63 6f 64 65 22 2c 20 30  "server-code", 0
2730: 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 53 43  );.      if( zSC
2740: 6f 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ode==0 ){.      
2750: 20 20 66 6f 73 73 69 6c 5f 70 61 6e 69 63 28 22    fossil_panic("
2760: 6d 69 73 73 69 6e 67 20 73 65 72 76 65 72 20 63  missing server c
2770: 6f 64 65 22 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ode");.      }. 
2780: 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71       if( blob_eq
2790: 5f 73 74 72 28 26 61 54 6f 6b 65 6e 5b 31 5d 2c  _str(&aToken[1],
27a0: 20 7a 53 43 6f 64 65 2c 20 2d 31 29 20 29 7b 0a   zSCode, -1) ){.
27b0: 20 20 20 20 20 20 20 20 63 67 69 5f 72 65 73 65          cgi_rese
27c0: 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 20  t_content();.   
27d0: 20 20 20 20 20 40 20 65 72 72 6f 72 20 73 65 72       @ error ser
27e0: 76 65 72 5c 73 6c 6f 6f 70 0a 20 20 20 20 20 20  ver\sloop.      
27f0: 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20    nErr++;.      
2800: 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
2810: 0a 20 20 20 20 20 20 7a 50 43 6f 64 65 20 3d 20  .      zPCode = 
2820: 64 62 5f 67 65 74 28 22 70 72 6f 6a 65 63 74 2d  db_get("project-
2830: 63 6f 64 65 22 2c 20 30 29 3b 0a 20 20 20 20 20  code", 0);.     
2840: 20 69 66 28 20 7a 50 43 6f 64 65 3d 3d 30 20 29   if( zPCode==0 )
2850: 7b 0a 20 20 20 20 20 20 20 20 66 6f 73 73 69 6c  {.        fossil
2860: 5f 70 61 6e 69 63 28 22 6d 69 73 73 69 6e 67 20  _panic("missing 
2870: 70 72 6f 6a 65 63 74 20 63 6f 64 65 22 29 3b 0a  project code");.
2880: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
2890: 28 20 21 62 6c 6f 62 5f 65 71 5f 73 74 72 28 26  ( !blob_eq_str(&
28a0: 61 54 6f 6b 65 6e 5b 32 5d 2c 20 7a 50 43 6f 64  aToken[2], zPCod
28b0: 65 2c 20 2d 31 29 20 29 7b 0a 20 20 20 20 20 20  e, -1) ){.      
28c0: 20 20 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e 74    cgi_reset_cont
28d0: 65 6e 74 28 29 3b 0a 20 20 20 20 20 20 20 20 40  ent();.        @
28e0: 20 65 72 72 6f 72 20 77 72 6f 6e 67 5c 73 70 72   error wrong\spr
28f0: 6f 6a 65 63 74 0a 20 20 20 20 20 20 20 20 6e 45  oject.        nE
2900: 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 62 72  rr++;.        br
2910: 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
2920: 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26     if( blob_eq(&
2930: 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 70 75 6c 6c  aToken[0], "pull
2940: 22 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  ") ){.        if
2950: 28 20 21 67 2e 6f 6b 52 65 61 64 20 29 7b 0a 20  ( !g.okRead ){. 
2960: 20 20 20 20 20 20 20 20 20 63 67 69 5f 72 65 73           cgi_res
2970: 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20  et_content();.  
2980: 20 20 20 20 20 20 20 20 40 20 65 72 72 6f 72 20          @ error 
2990: 6e 6f 74 5c 73 61 75 74 68 6f 72 69 7a 65 64 5c  not\sauthorized\
29a0: 73 74 6f 5c 73 72 65 61 64 0a 20 20 20 20 20 20  sto\sread.      
29b0: 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20      nErr++;.    
29c0: 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
29d0: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
29e0: 73 50 75 6c 6c 20 3d 20 31 3b 0a 20 20 20 20 20  sPull = 1;.     
29f0: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2a00: 69 66 28 20 21 67 2e 6f 6b 57 72 69 74 65 20 29  if( !g.okWrite )
2a10: 7b 0a 20 20 20 20 20 20 20 20 20 20 63 67 69 5f  {.          cgi_
2a20: 72 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b  reset_content();
2a30: 0a 20 20 20 20 20 20 20 20 20 20 40 20 65 72 72  .          @ err
2a40: 6f 72 20 6e 6f 74 5c 73 61 75 74 68 6f 72 69 7a  or not\sauthoriz
2a50: 65 64 5c 73 74 6f 5c 73 77 72 69 74 65 0a 20 20  ed\sto\swrite.  
2a60: 20 20 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a          nErr++;.
2a70: 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
2a80: 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2a90: 20 20 20 69 73 50 75 73 68 20 3d 20 31 3b 0a 0a     isPush = 1;..
2aa0: 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
2ab0: 65 0a 0a 20 20 20 20 2f 2a 20 20 20 20 63 6c 6f  e..    /*    clo
2ac0: 6e 65 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  ne.    **.    **
2ad0: 20 54 68 65 20 63 6c 69 65 6e 74 20 6b 6e 6f 77   The client know
2ae0: 73 20 6e 6f 74 68 69 6e 67 2e 20 20 54 65 6c 6c  s nothing.  Tell
2af0: 20 61 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20   all..    */.   
2b00: 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 61 54   if( blob_eq(&aT
2b10: 6f 6b 65 6e 5b 30 5d 2c 20 22 63 6c 6f 6e 65 22  oken[0], "clone"
2b20: 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21  ) ){.      if( !
2b30: 67 2e 6f 6b 52 65 61 64 20 29 7b 0a 20 20 20 20  g.okRead ){.    
2b40: 20 20 20 20 63 67 69 5f 72 65 73 65 74 5f 63 6f      cgi_reset_co
2b50: 6e 74 65 6e 74 28 29 3b 0a 20 20 20 20 20 20 20  ntent();.       
2b60: 20 40 20 65 72 72 6f 72 20 6e 6f 74 5c 73 61 75   @ error not\sau
2b70: 74 68 6f 72 69 7a 65 64 5c 73 74 6f 5c 73 72 65  thorized\sto\sre
2b80: 61 64 0a 20 20 20 20 20 20 20 20 6e 45 72 72 2b  ad.        nErr+
2b90: 2b 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  +;.        break
2ba0: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2bb0: 69 73 50 75 6c 6c 20 3d 20 31 3b 0a 20 20 20 20  isPull = 1;.    
2bc0: 20 20 40 20 70 75 73 68 20 25 73 28 64 62 5f 67    @ push %s(db_g
2bd0: 65 74 28 22 73 65 72 76 65 72 2d 63 6f 64 65 22  et("server-code"
2be0: 2c 20 22 78 22 29 29 20 25 73 28 64 62 5f 67 65  , "x")) %s(db_ge
2bf0: 74 28 22 70 72 6f 6a 65 63 74 2d 63 6f 64 65 22  t("project-code"
2c00: 2c 20 22 78 22 29 29 0a 20 20 20 20 20 20 64 62  , "x")).      db
2c10: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20  _multi_exec(.   
2c20: 20 20 20 20 20 22 49 4e 53 45 52 54 20 4f 52 20       "INSERT OR 
2c30: 49 47 4e 4f 52 45 20 49 4e 54 4f 20 70 65 6e 64  IGNORE INTO pend
2c40: 69 6e 67 28 72 69 64 29 20 22 0a 20 20 20 20 20  ing(rid) ".     
2c50: 20 20 20 22 53 45 4c 45 43 54 20 72 69 64 20 46     "SELECT rid F
2c60: 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 73  ROM blob WHERE s
2c70: 69 7a 65 3e 3d 30 22 0a 20 20 20 20 20 20 29 3b  ize>=0".      );
2c80: 0a 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20 20  .    }else..    
2c90: 2f 2a 20 20 20 20 6c 6f 67 69 6e 20 20 55 53 45  /*    login  USE
2ca0: 52 20 20 4e 4f 4e 43 45 20 20 53 49 47 4e 41 54  R  NONCE  SIGNAT
2cb0: 55 52 45 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  URE.    **.    *
2cc0: 2a 20 43 68 65 63 6b 20 66 6f 72 20 61 20 76 61  * Check for a va
2cd0: 6c 69 64 20 6c 6f 67 69 6e 2e 20 20 54 68 69 73  lid login.  This
2ce0: 20 68 61 73 20 74 6f 20 68 61 70 70 65 6e 20 62   has to happen b
2cf0: 65 66 6f 72 65 20 61 6e 79 74 68 69 6e 67 20 65  efore anything e
2d00: 6c 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  lse..    */.    
2d10: 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f  if( blob_eq(&aTo
2d20: 6b 65 6e 5b 30 5d 2c 20 22 6c 6f 67 69 6e 22 29  ken[0], "login")
2d30: 20 26 26 20 6e 54 6f 6b 65 6e 3d 3d 34 20 29 7b   && nToken==4 ){
2d40: 0a 20 20 20 20 20 20 69 66 28 20 64 69 73 61 62  .      if( disab
2d50: 6c 65 4c 6f 67 69 6e 20 29 7b 0a 20 20 20 20 20  leLogin ){.     
2d60: 20 20 20 67 2e 6f 6b 52 65 61 64 20 3d 20 67 2e     g.okRead = g.
2d70: 6f 6b 57 72 69 74 65 20 3d 20 31 3b 0a 20 20 20  okWrite = 1;.   
2d80: 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2d90: 20 20 63 68 65 63 6b 5f 6c 6f 67 69 6e 28 26 61    check_login(&a
2da0: 54 6f 6b 65 6e 5b 31 5d 2c 20 26 61 54 6f 6b 65  Token[1], &aToke
2db0: 6e 5b 32 5d 2c 20 26 61 54 6f 6b 65 6e 5b 33 5d  n[2], &aToken[3]
2dc0: 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
2dd0: 65 6c 73 65 0a 0a 20 20 20 20 2f 2a 20 55 6e 6b  else..    /* Unk
2de0: 6e 6f 77 6e 20 6d 65 73 73 61 67 65 0a 20 20 20  nown message.   
2df0: 20 2a 2f 0a 20 20 20 20 7b 0a 20 20 20 20 20 20   */.    {.      
2e00: 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e 74 65 6e  cgi_reset_conten
2e10: 74 28 29 3b 0a 20 20 20 20 20 20 40 20 65 72 72  t();.      @ err
2e20: 6f 72 20 62 61 64 5c 73 63 6f 6d 6d 61 6e 64 3a  or bad\scommand:
2e30: 5c 73 25 46 28 62 6c 6f 62 5f 73 74 72 28 26 6c  \s%F(blob_str(&l
2e40: 69 6e 65 29 29 0a 20 20 20 20 7d 0a 20 20 20 20  ine)).    }.    
2e50: 62 6c 6f 62 61 72 72 61 79 5f 72 65 73 65 74 28  blobarray_reset(
2e60: 61 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29 3b  aToken, nToken);
2e70: 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 69  .  }..  /* The i
2e80: 6e 70 75 74 20 6d 65 73 73 61 67 65 20 68 61 73  nput message has
2e90: 20 6e 6f 77 20 62 65 65 6e 20 70 72 6f 63 65 73   now been proces
2ea0: 73 65 64 2e 20 20 47 65 6e 65 72 61 74 65 20 61  sed.  Generate a
2eb0: 20 72 65 70 6c 79 2e 20 2a 2f 0a 20 20 69 66 28   reply. */.  if(
2ec0: 20 69 73 50 75 73 68 20 29 7b 0a 20 20 20 20 53   isPush ){.    S
2ed0: 74 6d 74 20 71 3b 0a 20 20 20 20 64 62 5f 70 72  tmt q;.    db_pr
2ee0: 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c 45 43  epare(&q, "SELEC
2ef0: 54 20 75 75 69 64 20 46 52 4f 4d 20 62 6c 6f 62  T uuid FROM blob
2f00: 20 57 48 45 52 45 20 73 69 7a 65 3c 30 22 29 3b   WHERE size<0");
2f10: 0a 20 20 20 20 77 68 69 6c 65 28 20 64 62 5f 73  .    while( db_s
2f20: 74 65 70 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f  tep(&q)==SQLITE_
2f30: 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e  ROW ){.      con
2f40: 73 74 20 63 68 61 72 20 2a 7a 55 75 69 64 20 3d  st char *zUuid =
2f50: 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28   db_column_text(
2f60: 26 71 2c 20 30 29 3b 0a 20 20 20 20 20 20 40 20  &q, 0);.      @ 
2f70: 67 69 6d 6d 65 20 25 73 28 7a 55 75 69 64 29 0a  gimme %s(zUuid).
2f80: 20 20 20 20 7d 0a 20 20 20 20 64 62 5f 66 69 6e      }.    db_fin
2f90: 61 6c 69 7a 65 28 26 71 29 3b 0a 20 20 7d 0a 20  alize(&q);.  }. 
2fa0: 20 69 66 28 20 69 73 50 75 6c 6c 20 29 7b 0a 20   if( isPull ){. 
2fb0: 20 20 20 73 65 6e 64 5f 61 6c 6c 5f 70 65 6e 64     send_all_pend
2fc0: 69 6e 67 28 30 29 3b 0a 20 20 7d 0a 20 20 64 62  ing(0);.  }.  db
2fd0: 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e  _end_transaction
2fe0: 28 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f  (0);.}../*.** CO
2ff0: 4d 4d 41 4e 44 3a 20 74 65 73 74 2d 78 66 65 72  MMAND: test-xfer
3000: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 6f 6d 6d  .**.** This comm
3010: 61 6e 64 20 69 73 20 75 73 65 64 20 66 6f 72 20  and is used for 
3020: 64 65 62 75 67 67 69 6e 67 20 74 68 65 20 73 65  debugging the se
3030: 72 76 65 72 2e 20 20 54 68 65 72 65 20 69 73 20  rver.  There is 
3040: 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 61 72 67 75  a single.** argu
3050: 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 74 68  ment which is th
3060: 65 20 75 6e 63 6f 6d 70 72 65 73 73 65 64 20 63  e uncompressed c
3070: 6f 6e 74 65 6e 74 20 6f 66 20 61 6e 20 22 78 66  ontent of an "xf
3080: 65 72 22 20 6d 65 73 73 61 67 65 0a 2a 2a 20 66  er" message.** f
3090: 72 6f 6d 20 63 6c 69 65 6e 74 20 74 6f 20 73 65  rom client to se
30a0: 72 76 65 72 2e 20 20 54 68 69 73 20 63 6f 6d 6d  rver.  This comm
30b0: 61 6e 64 20 69 6e 74 65 72 70 72 65 74 73 20 74  and interprets t
30c0: 68 61 74 20 6d 65 73 73 61 67 65 20 61 73 0a 2a  hat message as.*
30d0: 2a 20 69 66 20 68 61 64 20 62 65 65 6e 20 72 65  * if had been re
30e0: 63 65 69 76 65 64 20 62 79 20 74 68 65 20 73 65  ceived by the se
30f0: 72 76 65 72 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 74  rver..**.** On t
3100: 68 65 20 63 6c 69 65 6e 74 20 73 69 64 65 2c 20  he client side, 
3110: 72 75 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  run:.**.**      
3120: 66 6f 73 73 69 6c 20 70 75 73 68 20 68 74 74 70  fossil push http
3130: 3a 2f 2f 62 6f 67 75 73 2f 20 2d 2d 68 74 74 70  ://bogus/ --http
3140: 74 72 61 63 65 0a 2a 2a 0a 2a 2a 20 4f 72 20 61  trace.**.** Or a
3150: 20 73 69 6d 69 6c 61 72 20 63 6f 6d 6d 61 6e 64   similar command
3160: 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65 20   to provide the 
3170: 6f 75 74 70 75 74 2e 20 20 54 68 65 20 63 6f 6e  output.  The con
3180: 74 65 6e 74 20 6f 66 20 74 68 65 0a 2a 2a 20 6d  tent of the.** m
3190: 65 73 73 61 67 65 20 77 69 6c 6c 20 61 70 70 65  essage will appe
31a0: 61 72 20 6f 6e 20 73 74 61 6e 64 61 72 64 20 6f  ar on standard o
31b0: 75 74 70 75 74 2e 20 20 43 61 70 74 75 72 65 20  utput.  Capture 
31c0: 74 68 69 73 20 6d 65 73 73 61 67 65 0a 2a 2a 20  this message.** 
31d0: 69 6e 74 6f 20 61 20 66 69 6c 65 20 6e 61 6d 65  into a file name
31e0: 64 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 29 20  d (for example) 
31f0: 6f 75 74 2e 74 78 74 2e 20 20 54 68 65 6e 20 72  out.txt.  Then r
3200: 75 6e 20 74 68 65 0a 2a 2a 20 73 65 72 76 65 72  un the.** server
3210: 20 69 6e 20 67 64 62 3a 0a 2a 2a 0a 2a 2a 20 20   in gdb:.**.**  
3220: 20 20 20 67 64 62 20 66 6f 73 73 69 6c 0a 2a 2a     gdb fossil.**
3230: 20 20 20 20 20 72 20 74 65 73 74 2d 78 66 65 72       r test-xfer
3240: 20 6f 75 74 2e 74 78 74 0a 2a 2f 0a 76 6f 69 64   out.txt.*/.void
3250: 20 63 6d 64 5f 74 65 73 74 5f 78 66 65 72 28 76   cmd_test_xfer(v
3260: 6f 69 64 29 7b 0a 20 20 69 6e 74 20 6e 6f 74 55  oid){.  int notU
3270: 73 65 64 3b 0a 20 20 69 66 28 20 67 2e 61 72 67  sed;.  if( g.arg
3280: 63 21 3d 32 20 26 26 20 67 2e 61 72 67 63 21 3d  c!=2 && g.argc!=
3290: 33 20 29 7b 0a 20 20 20 20 75 73 61 67 65 28 22  3 ){.    usage("
32a0: 3f 4d 45 53 53 41 47 45 46 49 4c 45 3f 22 29 3b  ?MESSAGEFILE?");
32b0: 0a 20 20 7d 0a 20 20 64 62 5f 6d 75 73 74 5f 62  .  }.  db_must_b
32c0: 65 5f 77 69 74 68 69 6e 5f 74 72 65 65 28 29 3b  e_within_tree();
32d0: 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 67 2e  .  blob_zero(&g.
32e0: 63 67 69 49 6e 29 3b 0a 20 20 62 6c 6f 62 5f 72  cgiIn);.  blob_r
32f0: 65 61 64 5f 66 72 6f 6d 5f 66 69 6c 65 28 26 67  ead_from_file(&g
3300: 2e 63 67 69 49 6e 2c 20 67 2e 61 72 67 63 3d 3d  .cgiIn, g.argc==
3310: 32 20 3f 20 22 2d 22 20 3a 20 67 2e 61 72 67 76  2 ? "-" : g.argv
3320: 5b 32 5d 29 3b 0a 20 20 64 69 73 61 62 6c 65 4c  [2]);.  disableL
3330: 6f 67 69 6e 20 3d 20 31 3b 0a 20 20 70 61 67 65  ogin = 1;.  page
3340: 5f 78 66 65 72 28 29 3b 0a 20 20 70 72 69 6e 74  _xfer();.  print
3350: 66 28 22 25 73 5c 6e 22 2c 20 63 67 69 5f 65 78  f("%s\n", cgi_ex
3360: 74 72 61 63 74 5f 63 6f 6e 74 65 6e 74 28 26 6e  tract_content(&n
3370: 6f 74 55 73 65 64 29 29 3b 0a 7d 0a 0a 0a 2f 2a  otUsed));.}.../*
3380: 0a 2a 2a 20 53 79 6e 63 20 74 6f 20 74 68 65 20  .** Sync to the 
3390: 68 6f 73 74 20 69 64 65 6e 74 69 66 69 65 64 20  host identified 
33a0: 69 6e 20 67 2e 75 72 6c 4e 61 6d 65 20 61 6e 64  in g.urlName and
33b0: 20 67 2e 75 72 6c 50 61 74 68 2e 20 20 54 68 69   g.urlPath.  Thi
33c0: 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20  s.** routine is 
33d0: 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 63 6c  called by the cl
33e0: 69 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 63 6f  ient..**.** Reco
33f0: 72 64 73 20 61 72 65 20 70 75 73 68 65 64 20 74  rds are pushed t
3400: 6f 20 74 68 65 20 73 65 72 76 65 72 20 69 66 20  o the server if 
3410: 70 75 73 68 46 6c 61 67 20 69 73 20 74 72 75 65  pushFlag is true
3420: 2e 20 20 52 65 63 6f 72 64 73 0a 2a 2a 20 61 72  .  Records.** ar
3430: 65 20 70 75 6c 6c 65 64 20 69 66 20 70 75 6c 6c  e pulled if pull
3440: 46 6c 61 67 20 69 73 20 74 72 75 65 2e 20 20 41  Flag is true.  A
3450: 20 66 75 6c 6c 20 73 79 6e 63 20 6f 63 63 75 72   full sync occur
3460: 73 20 69 66 20 62 6f 74 68 20 61 72 65 0a 2a 2a  s if both are.**
3470: 20 74 72 75 65 2e 0a 2a 2f 0a 76 6f 69 64 20 63   true..*/.void c
3480: 6c 69 65 6e 74 5f 73 79 6e 63 28 69 6e 74 20 70  lient_sync(int p
3490: 75 73 68 46 6c 61 67 2c 20 69 6e 74 20 70 75 6c  ushFlag, int pul
34a0: 6c 46 6c 61 67 2c 20 69 6e 74 20 63 6c 6f 6e 65  lFlag, int clone
34b0: 46 6c 61 67 29 7b 0a 20 20 69 6e 74 20 67 6f 20  Flag){.  int go 
34c0: 3d 20 31 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c  = 1;        /* L
34d0: 6f 6f 70 20 75 6e 74 69 6c 20 7a 65 72 6f 20 2a  oop until zero *
34e0: 2f 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 0a  /.  int nToken;.
34f0: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
3500: 43 6f 64 65 20 3d 20 64 62 5f 67 65 74 28 22 73  Code = db_get("s
3510: 65 72 76 65 72 2d 63 6f 64 65 22 2c 20 22 78 22  erver-code", "x"
3520: 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  );.  const char 
3530: 2a 7a 50 43 6f 64 65 20 3d 20 64 62 5f 67 65 74  *zPCode = db_get
3540: 28 22 70 72 6f 6a 65 63 74 2d 63 6f 64 65 22 2c  ("project-code",
3550: 20 30 29 3b 0a 20 20 69 6e 74 20 6e 53 65 6e 74   0);.  int nSent
3560: 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 52 63 76   = 0;.  int nRcv
3570: 64 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 43 79  d = 0;.  int nCy
3580: 63 6c 65 20 3d 20 30 3b 0a 20 20 42 6c 6f 62 20  cle = 0;.  Blob 
3590: 73 65 6e 64 3b 20 20 20 20 20 20 20 20 2f 2a 20  send;        /* 
35a0: 54 65 78 74 20 77 65 20 61 72 65 20 73 65 6e 64  Text we are send
35b0: 69 6e 67 20 74 6f 20 74 68 65 20 73 65 72 76 65  ing to the serve
35c0: 72 20 2a 2f 0a 20 20 42 6c 6f 62 20 72 65 63 76  r */.  Blob recv
35d0: 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 70 6c  ;        /* Repl
35e0: 79 20 77 65 20 67 6f 74 20 62 61 63 6b 20 66 72  y we got back fr
35f0: 6f 6d 20 74 68 65 20 73 65 72 76 65 72 20 2a 2f  om the server */
3600: 0a 20 20 42 6c 6f 62 20 6c 69 6e 65 3b 20 20 20  .  Blob line;   
3610: 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65       /* A single
3620: 20 6c 69 6e 65 20 6f 66 20 74 68 65 20 72 65 70   line of the rep
3630: 6c 79 20 2a 2f 0a 20 20 42 6c 6f 62 20 61 54 6f  ly */.  Blob aTo
3640: 6b 65 6e 5b 35 5d 3b 20 20 20 2f 2a 20 41 20 74  ken[5];   /* A t
3650: 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 6f 66 20 6c  okenization of l
3660: 69 6e 65 20 2a 2f 0a 20 20 42 6c 6f 62 20 65 72  ine */.  Blob er
3670: 72 6d 73 67 3b 20 20 20 20 20 20 2f 2a 20 45 72  rmsg;      /* Er
3680: 72 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f 0a 0a  ror message */..
3690: 20 20 61 73 73 65 72 74 28 20 70 75 73 68 46 6c    assert( pushFl
36a0: 61 67 20 7c 7c 20 70 75 6c 6c 46 6c 61 67 20 7c  ag || pullFlag |
36b0: 7c 20 63 6c 6f 6e 65 46 6c 61 67 20 29 3b 0a 20  | cloneFlag );. 
36c0: 20 61 73 73 65 72 74 28 20 21 67 2e 75 72 6c 49   assert( !g.urlI
36d0: 73 46 69 6c 65 20 29 3b 20 20 20 20 20 20 20 20  sFile );        
36e0: 20 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20 77    /* This only w
36f0: 6f 72 6b 73 20 66 6f 72 20 6e 65 74 77 6f 72 6b  orks for network
3700: 69 6e 67 20 2a 2f 0a 0a 20 20 64 62 5f 62 65 67  ing */..  db_beg
3710: 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 29  in_transaction()
3720: 3b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65  ;.  db_multi_exe
3730: 63 28 0a 20 20 20 20 2f 2a 20 52 65 63 6f 72 64  c(.    /* Record
3740: 73 20 77 68 69 63 68 20 77 65 20 6b 6e 6f 77 20  s which we know 
3750: 74 68 65 20 6f 74 68 65 72 20 73 69 64 65 20 61  the other side a
3760: 6c 73 6f 20 68 61 73 20 2a 2f 0a 20 20 20 20 22  lso has */.    "
3770: 43 52 45 41 54 45 20 54 45 4d 50 20 54 41 42 4c  CREATE TEMP TABL
3780: 45 20 6f 6e 72 65 6d 6f 74 65 28 72 69 64 20 49  E onremote(rid I
3790: 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
37a0: 45 59 29 3b 22 0a 20 20 20 20 2f 2a 20 52 65 63  EY);".    /* Rec
37b0: 6f 72 64 73 20 77 65 20 6b 6e 6f 77 20 74 68 65  ords we know the
37c0: 20 6f 74 68 65 72 20 73 69 64 65 20 6e 65 65 64   other side need
37d0: 73 20 2a 2f 0a 20 20 20 20 22 43 52 45 41 54 45  s */.    "CREATE
37e0: 20 54 45 4d 50 20 54 41 42 4c 45 20 70 65 6e 64   TEMP TABLE pend
37f0: 69 6e 67 28 72 69 64 20 49 4e 54 45 47 45 52 20  ing(rid INTEGER 
3800: 50 52 49 4d 41 52 59 20 4b 45 59 29 3b 22 0a 20  PRIMARY KEY);". 
3810: 20 29 3b 0a 20 20 62 6c 6f 62 61 72 72 61 79 5f   );.  blobarray_
3820: 7a 65 72 6f 28 61 54 6f 6b 65 6e 2c 20 63 6f 75  zero(aToken, cou
3830: 6e 74 28 61 54 6f 6b 65 6e 29 29 3b 0a 20 20 62  nt(aToken));.  b
3840: 6c 6f 62 5f 7a 65 72 6f 28 26 73 65 6e 64 29 3b  lob_zero(&send);
3850: 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 72 65  .  blob_zero(&re
3860: 63 76 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f  cv);.  blob_zero
3870: 28 26 65 72 72 6d 73 67 29 3b 0a 0a 0a 20 20 77  (&errmsg);...  w
3880: 68 69 6c 65 28 20 67 6f 20 29 7b 0a 20 20 20 20  hile( go ){.    
3890: 67 6f 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20  go = 0;..    /* 
38a0: 47 65 6e 65 72 61 74 65 20 61 20 72 65 71 75 65  Generate a reque
38b0: 73 74 20 74 6f 20 62 65 20 73 65 6e 74 20 74 6f  st to be sent to
38c0: 20 74 68 65 20 73 65 72 76 65 72 2e 0a 20 20 20   the server..   
38d0: 20 2a 2a 20 41 6c 77 61 79 73 20 62 65 67 69 6e   ** Always begin
38e0: 20 77 69 74 68 20 61 20 63 6c 6f 6e 65 2c 20 70   with a clone, p
38f0: 75 6c 6c 2c 20 6f 72 20 70 75 73 68 20 6d 65 73  ull, or push mes
3900: 73 61 67 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20  sage.    */.    
3910: 69 66 28 20 63 6c 6f 6e 65 46 6c 61 67 20 29 7b  if( cloneFlag ){
3920: 0a 20 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 65  .      blob_appe
3930: 6e 64 66 28 26 73 65 6e 64 2c 20 22 63 6c 6f 6e  ndf(&send, "clon
3940: 65 5c 6e 22 29 3b 0a 20 20 20 20 20 20 70 75 73  e\n");.      pus
3950: 68 46 6c 61 67 20 3d 20 30 3b 0a 20 20 20 20 20  hFlag = 0;.     
3960: 20 70 75 6c 6c 46 6c 61 67 20 3d 20 30 3b 0a 20   pullFlag = 0;. 
3970: 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 75 6c     }else if( pul
3980: 6c 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 62  lFlag ){.      b
3990: 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 73 65 6e  lob_appendf(&sen
39a0: 64 2c 20 22 70 75 6c 6c 20 25 73 20 25 73 5c 6e  d, "pull %s %s\n
39b0: 22 2c 20 7a 53 43 6f 64 65 2c 20 7a 50 43 6f 64  ", zSCode, zPCod
39c0: 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  e);.    }.    if
39d0: 28 20 70 75 73 68 46 6c 61 67 20 29 7b 0a 20 20  ( pushFlag ){.  
39e0: 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66      blob_appendf
39f0: 28 26 73 65 6e 64 2c 20 22 70 75 73 68 20 25 73  (&send, "push %s
3a00: 20 25 73 5c 6e 22 2c 20 7a 53 43 6f 64 65 2c 20   %s\n", zSCode, 
3a10: 7a 50 43 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 0a  zPCode);.    }..
3a20: 20 20 20 20 69 66 28 20 70 75 6c 6c 46 6c 61 67      if( pullFlag
3a30: 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53 65 6e   ){.      /* Sen
3a40: 64 20 67 69 6d 6d 65 20 6d 65 73 73 61 67 65 20  d gimme message 
3a50: 66 6f 72 20 65 76 65 72 79 20 70 68 61 6e 74 6f  for every phanto
3a60: 6d 20 74 68 61 74 20 77 65 20 68 6f 6c 64 2e 0a  m that we hold..
3a70: 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 53        */.      S
3a80: 74 6d 74 20 71 3b 0a 20 20 20 20 20 20 64 62 5f  tmt q;.      db_
3a90: 70 72 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c  prepare(&q, "SEL
3aa0: 45 43 54 20 75 75 69 64 20 46 52 4f 4d 20 62 6c  ECT uuid FROM bl
3ab0: 6f 62 20 57 48 45 52 45 20 73 69 7a 65 3c 30 22  ob WHERE size<0"
3ac0: 29 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  );.      while( 
3ad0: 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53 51 4c  db_step(&q)==SQL
3ae0: 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20  ITE_ROW ){.     
3af0: 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
3b00: 55 75 69 64 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e  Uuid = db_column
3b10: 5f 74 65 78 74 28 26 71 2c 20 30 29 3b 0a 20 20  _text(&q, 0);.  
3b20: 20 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e        blob_appen
3b30: 64 66 28 26 73 65 6e 64 2c 22 67 69 6d 6d 65 20  df(&send,"gimme 
3b40: 25 73 5c 6e 22 2c 20 7a 55 75 69 64 29 3b 0a 20  %s\n", zUuid);. 
3b50: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 62 5f       }.      db_
3b60: 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 20 20  finalize(&q);.  
3b70: 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 70 75 73    }..    if( pus
3b80: 68 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 2f  hFlag ){.      /
3b90: 2a 20 53 65 6e 64 20 74 68 65 20 73 65 72 76 65  * Send the serve
3ba0: 72 20 61 6e 79 20 66 69 6c 65 73 20 74 68 61 74  r any files that
3bb0: 20 74 68 65 20 73 65 72 76 65 72 20 68 61 73 20   the server has 
3bc0: 72 65 71 75 65 73 74 65 64 20 2a 2f 0a 20 20 20  requested */.   
3bd0: 20 20 20 6e 53 65 6e 74 20 2b 3d 20 73 65 6e 64     nSent += send
3be0: 5f 61 6c 6c 5f 70 65 6e 64 69 6e 67 28 26 73 65  _all_pending(&se
3bf0: 6e 64 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  nd);.    }..    
3c00: 69 66 28 20 70 75 6c 6c 46 6c 61 67 20 7c 7c 20  if( pullFlag || 
3c10: 70 75 73 68 46 6c 61 67 20 29 7b 0a 20 20 20 20  pushFlag ){.    
3c20: 20 20 2f 2a 20 41 6c 77 61 79 73 20 73 65 6e 64    /* Always send
3c30: 20 6f 75 72 20 6c 65 61 76 65 73 20 2a 2f 0a 20   our leaves */. 
3c40: 20 20 20 20 20 53 74 6d 74 20 71 3b 0a 20 20 20       Stmt q;.   
3c50: 20 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 71     db_prepare(&q
3c60: 2c 20 0a 20 20 20 20 20 20 20 20 20 22 53 45 4c  , .         "SEL
3c70: 45 43 54 20 75 75 69 64 20 46 52 4f 4d 20 62 6c  ECT uuid FROM bl
3c80: 6f 62 20 57 48 45 52 45 20 72 69 64 20 49 4e 22  ob WHERE rid IN"
3c90: 0a 20 20 20 20 20 20 20 20 20 22 20 20 28 53 45  .         "  (SE
3ca0: 4c 45 43 54 20 63 69 64 20 46 52 4f 4d 20 70 6c  LECT cid FROM pl
3cb0: 69 6e 6b 20 45 58 43 45 50 54 20 53 45 4c 45 43  ink EXCEPT SELEC
3cc0: 54 20 70 69 64 20 46 52 4f 4d 20 70 6c 69 6e 6b  T pid FROM plink
3cd0: 29 22 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20  )".      );.    
3ce0: 20 20 77 68 69 6c 65 28 20 64 62 5f 73 74 65 70    while( db_step
3cf0: 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  (&q)==SQLITE_ROW
3d00: 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73   ){.        cons
3d10: 74 20 63 68 61 72 20 2a 7a 55 75 69 64 20 3d 20  t char *zUuid = 
3d20: 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 26  db_column_text(&
3d30: 71 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 62  q, 0);.        b
3d40: 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 73 65 6e  lob_appendf(&sen
3d50: 64 2c 20 22 6c 65 61 66 20 25 73 5c 6e 22 2c 20  d, "leaf %s\n", 
3d60: 7a 55 75 69 64 29 3b 0a 20 20 20 20 20 20 7d 0a  zUuid);.      }.
3d70: 20 20 20 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a        db_finaliz
3d80: 65 28 26 71 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  e(&q);.    }..  
3d90: 20 20 2f 2a 20 45 78 63 68 61 6e 67 65 20 6d 65    /* Exchange me
3da0: 73 73 61 67 65 73 20 77 69 74 68 20 74 68 65 20  ssages with the 
3db0: 73 65 72 76 65 72 20 2a 2f 0a 20 20 20 20 70 72  server */.    pr
3dc0: 69 6e 74 66 28 22 53 65 6e 64 69 6e 67 20 25 64  intf("Sending %d
3dd0: 20 66 69 6c 65 73 20 74 6f 20 73 65 72 76 65 72   files to server
3de0: 5c 6e 22 2c 20 6e 53 65 6e 74 29 3b 0a 20 20 20  \n", nSent);.   
3df0: 20 68 74 74 70 5f 65 78 63 68 61 6e 67 65 28 26   http_exchange(&
3e00: 73 65 6e 64 2c 20 26 72 65 63 76 29 3b 0a 20 20  send, &recv);.  
3e10: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 73 65    blob_reset(&se
3e20: 6e 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 50 72 6f  nd);..    /* Pro
3e30: 63 65 73 73 20 74 68 65 20 72 65 70 6c 79 20 74  cess the reply t
3e40: 68 61 74 20 63 61 6d 65 20 62 61 63 6b 20 66 72  hat came back fr
3e50: 6f 6d 20 74 68 65 20 73 65 72 76 65 72 20 2a 2f  om the server */
3e60: 0a 20 20 20 20 77 68 69 6c 65 28 20 62 6c 6f 62  .    while( blob
3e70: 5f 6c 69 6e 65 28 26 72 65 63 76 2c 20 26 6c 69  _line(&recv, &li
3e80: 6e 65 29 20 29 7b 0a 20 20 20 20 20 20 6e 54 6f  ne) ){.      nTo
3e90: 6b 65 6e 20 3d 20 62 6c 6f 62 5f 74 6f 6b 65 6e  ken = blob_token
3ea0: 69 7a 65 28 26 6c 69 6e 65 2c 20 61 54 6f 6b 65  ize(&line, aToke
3eb0: 6e 2c 20 63 6f 75 6e 74 28 61 54 6f 6b 65 6e 29  n, count(aToken)
3ec0: 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 20 20 66  );..      /*   f
3ed0: 69 6c 65 20 55 55 49 44 20 53 49 5a 45 20 5c 6e  ile UUID SIZE \n
3ee0: 20 43 4f 4e 54 45 4e 54 0a 20 20 20 20 20 20 2a   CONTENT.      *
3ef0: 2a 20 20 20 66 69 6c 65 20 55 55 49 44 20 44 45  *   file UUID DE
3f00: 4c 54 41 53 52 43 20 53 49 5a 45 20 5c 6e 20 43  LTASRC SIZE \n C
3f10: 4f 4e 54 45 4e 54 0a 20 20 20 20 20 20 2a 2a 0a  ONTENT.      **.
3f20: 20 20 20 20 20 20 2a 2a 20 52 65 63 65 69 76 65        ** Receive
3f30: 20 61 20 66 69 6c 65 20 74 72 61 6e 73 6d 69 74   a file transmit
3f40: 74 65 64 20 66 72 6f 6d 20 74 68 65 20 6f 74 68  ted from the oth
3f50: 65 72 20 73 69 64 65 0a 20 20 20 20 20 20 2a 2f  er side.      */
3f60: 0a 20 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f  .      if( blob_
3f70: 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 22 66  eq(&aToken[0],"f
3f80: 69 6c 65 22 29 20 29 7b 0a 20 20 20 20 20 20 20  ile") ){.       
3f90: 20 78 66 65 72 5f 61 63 63 65 70 74 5f 66 69 6c   xfer_accept_fil
3fa0: 65 28 26 72 65 63 76 2c 20 61 54 6f 6b 65 6e 2c  e(&recv, aToken,
3fb0: 20 6e 54 6f 6b 65 6e 2c 20 26 65 72 72 6d 73 67   nToken, &errmsg
3fc0: 29 3b 0a 20 20 20 20 20 20 20 20 6e 52 63 76 64  );.        nRcvd
3fd0: 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a  ++;.      }else.
3fe0: 0a 20 20 20 20 20 20 2f 2a 20 20 20 67 69 6d 6d  .      /*   gimm
3ff0: 65 20 55 55 49 44 0a 20 20 20 20 20 20 2a 2a 0a  e UUID.      **.
4000: 20 20 20 20 20 20 2a 2a 20 53 65 72 76 65 72 20        ** Server 
4010: 69 73 20 72 65 71 75 65 73 74 69 6e 67 20 61 20  is requesting a 
4020: 66 69 6c 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20  file.      */.  
4030: 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28      if( blob_eq(
4040: 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 67 69 6d  &aToken[0], "gim
4050: 6d 65 22 29 20 26 26 20 6e 54 6f 6b 65 6e 3d 3d  me") && nToken==
4060: 32 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  2.              
4070: 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69 64   && blob_is_uuid
4080: 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 20 29 7b 0a  (&aToken[1]) ){.
4090: 20 20 20 20 20 20 20 20 69 66 28 20 70 75 73 68          if( push
40a0: 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20  Flag ){.        
40b0: 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28    db_multi_exec(
40c0: 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 49 4e  .            "IN
40d0: 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49  SERT OR IGNORE I
40e0: 4e 54 4f 20 70 65 6e 64 69 6e 67 28 72 69 64 29  NTO pending(rid)
40f0: 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22   ".            "
4100: 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f 4d 20  SELECT rid FROM 
4110: 62 6c 6f 62 20 57 48 45 52 45 20 75 75 69 64 3d  blob WHERE uuid=
4120: 25 42 20 41 4e 44 20 73 69 7a 65 3e 3d 30 22 2c  %B AND size>=0",
4130: 20 26 61 54 6f 6b 65 6e 5b 31 5d 0a 20 20 20 20   &aToken[1].    
4140: 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20        );.       
4150: 20 20 20 67 6f 20 3d 20 31 3b 0a 20 20 20 20 20     go = 1;.     
4160: 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
4170: 0a 20 20 0a 20 20 20 20 20 20 2f 2a 20 20 20 69  .  .      /*   i
4180: 67 6f 74 20 55 55 49 44 0a 20 20 20 20 20 20 2a  got UUID.      *
4190: 2a 20 20 20 6c 65 61 66 20 55 55 49 44 0a 20 20  *   leaf UUID.  
41a0: 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
41b0: 53 65 72 76 65 72 20 70 72 6f 63 6c 61 69 6d 73  Server proclaims
41c0: 20 74 68 61 74 20 69 74 20 68 61 73 20 61 20 70   that it has a p
41d0: 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65 2e 20  articular file. 
41e0: 20 41 20 6c 65 61 66 20 6d 65 73 73 61 67 65 0a   A leaf message.
41f0: 20 20 20 20 20 20 2a 2a 20 6d 65 61 6e 73 20 74        ** means t
4200: 68 61 74 20 74 68 65 20 66 69 6c 65 20 69 73 20  hat the file is 
4210: 61 20 6c 65 61 66 20 6d 61 6e 69 66 65 73 74 20  a leaf manifest 
4220: 6f 6e 20 74 68 65 20 73 65 72 76 65 72 2e 0a 20  on the server.. 
4230: 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66       */.      if
4240: 28 20 6e 54 6f 6b 65 6e 3d 3d 32 0a 20 20 20 20  ( nToken==2.    
4250: 20 20 20 20 20 20 20 20 20 20 26 26 20 28 62 6c            && (bl
4260: 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d  ob_eq(&aToken[0]
4270: 2c 20 22 69 67 6f 74 22 29 20 7c 7c 20 62 6c 6f  , "igot") || blo
4280: 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c  b_eq(&aToken[0],
4290: 20 22 6c 65 61 66 22 29 29 0a 20 20 20 20 20 20   "leaf")).      
42a0: 20 20 20 20 20 20 20 20 26 26 20 62 6c 6f 62 5f          && blob_
42b0: 69 73 5f 75 75 69 64 28 26 61 54 6f 6b 65 6e 5b  is_uuid(&aToken[
42c0: 31 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69  1]) ){.        i
42d0: 6e 74 20 72 69 64 20 3d 20 64 62 5f 69 6e 74 28  nt rid = db_int(
42e0: 30 2c 20 22 53 45 4c 45 43 54 20 72 69 64 20 46  0, "SELECT rid F
42f0: 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 75  ROM blob WHERE u
4300: 75 69 64 3d 25 42 22 2c 20 26 61 54 6f 6b 65 6e  uid=%B", &aToken
4310: 5b 31 5d 29 3b 0a 20 20 20 20 20 20 20 20 69 66  [1]);.        if
4320: 28 20 72 69 64 3e 30 20 29 7b 0a 20 20 20 20 20  ( rid>0 ){.     
4330: 20 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78       db_multi_ex
4340: 65 63 28 0a 20 20 20 20 20 20 20 20 20 20 20 20  ec(.            
4350: 22 49 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f 52  "INSERT OR IGNOR
4360: 45 20 49 4e 54 4f 20 6f 6e 72 65 6d 6f 74 65 28  E INTO onremote(
4370: 72 69 64 29 20 56 41 4c 55 45 53 28 25 64 29 22  rid) VALUES(%d)"
4380: 2c 20 72 69 64 0a 20 20 20 20 20 20 20 20 20 20  , rid.          
4390: 29 3b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  );.          /* 
43a0: 41 64 64 20 74 6f 20 74 68 65 20 70 65 6e 64 69  Add to the pendi
43b0: 6e 67 20 73 65 74 20 61 6c 6c 20 63 68 69 6c 64  ng set all child
43c0: 72 65 6e 20 6f 66 20 74 68 65 20 73 65 72 76 65  ren of the serve
43d0: 72 27 73 20 6c 65 61 76 65 73 20 2a 2f 0a 20 20  r's leaves */.  
43e0: 20 20 20 20 20 20 20 20 69 66 28 20 70 75 73 68          if( push
43f0: 46 6c 61 67 20 26 26 20 62 6c 6f 62 5f 65 71 28  Flag && blob_eq(
4400: 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 6c 65 61  &aToken[0], "lea
4410: 66 22 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  f") ){.         
4420: 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63     db_multi_exec
4430: 28 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  (.              
4440: 22 49 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f 52  "INSERT OR IGNOR
4450: 45 20 49 4e 54 4f 20 70 65 6e 64 69 6e 67 28 72  E INTO pending(r
4460: 69 64 29 20 22 0a 20 20 20 20 20 20 20 20 20 20  id) ".          
4470: 20 20 20 20 22 53 45 4c 45 43 54 20 63 69 64 20      "SELECT cid 
4480: 46 52 4f 4d 20 70 6c 69 6e 6b 20 57 48 45 52 45  FROM plink WHERE
4490: 20 70 69 64 3d 25 64 22 2c 20 72 69 64 0a 20 20   pid=%d", rid.  
44a0: 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20            );.   
44b0: 20 20 20 20 20 20 20 20 20 67 6f 20 3d 20 31 3b           go = 1;
44c0: 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
44d0: 20 20 20 20 20 20 20 69 66 28 20 70 75 6c 6c 46         if( pullF
44e0: 6c 61 67 20 26 26 20 21 67 6f 20 26 26 20 0a 20  lag && !go && . 
44f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 64 62 5f               db_
4500: 65 78 69 73 74 73 28 22 53 45 4c 45 43 54 20 31  exists("SELECT 1
4510: 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45   FROM blob WHERE
4520: 20 72 69 64 3d 25 64 20 41 4e 44 20 73 69 7a 65   rid=%d AND size
4530: 3e 3d 30 22 2c 20 72 69 64 29 20 29 7b 0a 20 20  >=0", rid) ){.  
4540: 20 20 20 20 20 20 20 20 20 20 67 6f 20 3d 20 31            go = 1
4550: 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
4560: 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
4570: 70 75 6c 6c 46 6c 61 67 20 29 7b 0a 20 20 20 20  pullFlag ){.    
4580: 20 20 20 20 20 20 67 6f 20 3d 20 31 3b 0a 20 20        go = 1;.  
4590: 20 20 20 20 20 20 20 20 63 6f 6e 74 65 6e 74 5f          content_
45a0: 70 75 74 28 30 2c 20 62 6c 6f 62 5f 73 74 72 28  put(0, blob_str(
45b0: 26 61 54 6f 6b 65 6e 5b 31 5d 29 29 3b 0a 20 20  &aToken[1]));.  
45c0: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65        }.      }e
45d0: 6c 73 65 0a 20 20 0a 20 20 20 20 20 20 2f 2a 20  lse.  .      /* 
45e0: 20 20 70 75 73 68 20 20 53 45 52 56 45 52 43 4f    push  SERVERCO
45f0: 44 45 20 20 50 52 4f 44 55 43 54 43 4f 44 45 0a  DE  PRODUCTCODE.
4600: 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
4610: 2a 20 53 68 6f 75 6c 64 20 6f 6e 6c 79 20 68 61  * Should only ha
4620: 70 70 65 6e 20 69 6e 20 72 65 73 70 6f 6e 73 65  ppen in response
4630: 20 74 6f 20 61 20 63 6c 6f 6e 65 2e 0a 20 20 20   to a clone..   
4640: 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
4650: 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b  blob_eq(&aToken[
4660: 30 5d 2c 22 70 75 73 68 22 29 20 26 26 20 6e 54  0],"push") && nT
4670: 6f 6b 65 6e 3d 3d 33 20 26 26 20 63 6c 6f 6e 65  oken==3 && clone
4680: 46 6c 61 67 0a 20 20 20 20 20 20 20 20 20 20 20  Flag.           
4690: 20 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75 75     && blob_is_uu
46a0: 69 64 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 20 26  id(&aToken[1]) &
46b0: 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26  & blob_is_uuid(&
46c0: 61 54 6f 6b 65 6e 5b 32 5d 29 20 29 7b 0a 0a 20  aToken[2]) ){.. 
46d0: 20 20 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f         if( blob_
46e0: 65 71 5f 73 74 72 28 26 61 54 6f 6b 65 6e 5b 31  eq_str(&aToken[1
46f0: 5d 2c 20 7a 53 43 6f 64 65 2c 20 2d 31 29 20 29  ], zSCode, -1) )
4700: 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 73 73  {.          foss
4710: 69 6c 5f 66 61 74 61 6c 28 22 73 65 72 76 65 72  il_fatal("server
4720: 20 6c 6f 6f 70 22 29 3b 0a 20 20 20 20 20 20 20   loop");.       
4730: 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a   }.        if( z
4740: 50 43 6f 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20  PCode==0 ){.    
4750: 20 20 20 20 20 20 7a 50 43 6f 64 65 20 3d 20 6d        zPCode = m
4760: 70 72 69 6e 74 66 28 22 25 62 22 2c 20 26 61 54  printf("%b", &aT
4770: 6f 6b 65 6e 5b 32 5d 29 3b 0a 20 20 20 20 20 20  oken[2]);.      
4780: 20 20 20 20 64 62 5f 73 65 74 28 22 70 72 6f 6a      db_set("proj
4790: 65 63 74 2d 63 6f 64 65 22 2c 20 7a 50 43 6f 64  ect-code", zPCod
47a0: 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  e);.        }.  
47b0: 20 20 20 20 20 20 63 6c 6f 6e 65 46 6c 61 67 20        cloneFlag 
47c0: 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70 75 6c  = 0;.        pul
47d0: 6c 46 6c 61 67 20 3d 20 31 3b 0a 20 20 20 20 20  lFlag = 1;.     
47e0: 20 7d 65 6c 73 65 0a 0a 20 20 20 20 20 20 2f 2a   }else..      /*
47f0: 20 20 20 65 72 72 6f 72 20 4d 45 53 53 41 47 45     error MESSAGE
4800: 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
4810: 2a 2a 20 52 65 70 6f 72 74 20 61 6e 20 65 72 72  ** Report an err
4820: 6f 72 0a 20 20 20 20 20 20 2a 2f 20 20 20 20 20  or.      */     
4830: 20 20 20 0a 20 20 20 20 20 20 69 66 28 20 62 6c     .      if( bl
4840: 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d  ob_eq(&aToken[0]
4850: 2c 22 65 72 72 6f 72 22 29 20 26 26 20 6e 54 6f  ,"error") && nTo
4860: 6b 65 6e 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20  ken==2 ){.      
4870: 20 20 63 68 61 72 20 2a 7a 4d 73 67 20 3d 20 62    char *zMsg = b
4880: 6c 6f 62 5f 74 65 72 6d 69 6e 61 74 65 28 26 61  lob_terminate(&a
4890: 54 6f 6b 65 6e 5b 31 5d 29 3b 0a 20 20 20 20 20  Token[1]);.     
48a0: 20 20 20 64 65 66 6f 73 73 69 6c 69 7a 65 28 7a     defossilize(z
48b0: 4d 73 67 29 3b 0a 20 20 20 20 20 20 20 20 62 6c  Msg);.        bl
48c0: 6f 62 5f 61 70 70 65 6e 64 66 28 26 65 72 72 6d  ob_appendf(&errm
48d0: 73 67 2c 20 22 73 65 72 76 65 72 20 73 61 79 73  sg, "server says
48e0: 3a 20 25 73 22 2c 20 7a 4d 73 67 29 3b 0a 20 20  : %s", zMsg);.  
48f0: 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20 20 20      }else..     
4900: 20 2f 2a 20 55 6e 6b 6e 6f 77 6e 20 6d 65 73 73   /* Unknown mess
4910: 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 7b 0a 20  age */.      {. 
4920: 20 20 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 65         blob_appe
4930: 6e 64 66 28 26 65 72 72 6d 73 67 2c 20 22 75 6e  ndf(&errmsg, "un
4940: 6b 6e 6f 77 6e 20 63 6f 6d 6d 61 6e 64 3a 20 25  known command: %
4950: 62 22 2c 20 26 61 54 6f 6b 65 6e 5b 30 5d 29 3b  b", &aToken[0]);
4960: 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
4970: 69 66 28 20 62 6c 6f 62 5f 73 69 7a 65 28 26 65  if( blob_size(&e
4980: 72 72 6d 73 67 29 20 29 7b 0a 20 20 20 20 20 20  rrmsg) ){.      
4990: 20 20 66 6f 73 73 69 6c 5f 66 61 74 61 6c 28 22    fossil_fatal("
49a0: 25 62 22 2c 20 26 65 72 72 6d 73 67 29 3b 0a 20  %b", &errmsg);. 
49b0: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 6c 6f       }.      blo
49c0: 62 61 72 72 61 79 5f 72 65 73 65 74 28 61 54 6f  barray_reset(aTo
49d0: 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29 3b 0a 20 20  ken, nToken);.  
49e0: 20 20 7d 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73    }.    blob_res
49f0: 65 74 28 26 72 65 63 76 29 3b 0a 20 20 20 20 70  et(&recv);.    p
4a00: 72 69 6e 74 66 28 22 52 65 63 65 69 76 65 64 20  rintf("Received 
4a10: 25 64 20 66 69 6c 65 73 20 66 72 6f 6d 20 73 65  %d files from se
4a20: 72 76 65 72 5c 6e 22 2c 20 6e 52 63 76 64 29 3b  rver\n", nRcvd);
4a30: 0a 20 20 20 20 6e 53 65 6e 74 20 3d 20 6e 52 63  .    nSent = nRc
4a40: 76 64 20 3d 20 30 3b 0a 20 20 7d 3b 0a 20 20 68  vd = 0;.  };.  h
4a50: 74 74 70 5f 63 6c 6f 73 65 28 29 3b 0a 20 20 64  ttp_close();.  d
4a60: 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f  b_end_transactio
4a70: 6e 28 30 29 3b 0a 20 20 64 62 5f 6d 75 6c 74 69  n(0);.  db_multi
4a80: 5f 65 78 65 63 28 0a 20 20 20 20 22 44 52 4f 50  _exec(.    "DROP
4a90: 20 54 41 42 4c 45 20 6f 6e 72 65 6d 6f 74 65 3b   TABLE onremote;
4aa0: 22 0a 20 20 20 20 22 44 52 4f 50 20 54 41 42 4c  ".    "DROP TABL
4ab0: 45 20 70 65 6e 64 69 6e 67 3b 22 0a 20 20 29 3b  E pending;".  );
4ac0: 0a 7d 0a                                         .}.