Hex Artifact Content
Not logged in

Artifact dc10588abf20b292c9cb3f7c9176fdf89bbcb18c:

File src/xfer.c part of check-in [869534e182] - Fix a bug in the xfer server that prevents it from receiving deltas. by drh on 2007-08-09 03:44:31.

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 72 79 20 74 6f 20 6c  ../*.** Try to l
03e0: 6f 63 61 74 65 20 61 20 72 65 63 6f 72 64 20 74  ocate a record t
03f0: 68 61 74 20 69 73 20 73 69 6d 69 6c 61 72 20 74  hat is similar t
0400: 6f 20 72 69 64 20 61 6e 64 20 69 73 20 61 20 6c  o rid and is a l
0410: 69 6b 65 6c 79 0a 2a 2a 20 63 61 6e 64 69 64 61  ikely.** candida
0420: 74 65 20 66 6f 72 20 64 65 6c 74 61 20 61 67 61  te for delta aga
0430: 69 6e 73 74 20 72 69 64 2e 20 20 54 68 65 20 73  inst rid.  The s
0440: 69 6d 69 6c 61 72 20 72 65 63 6f 72 64 20 6d 75  imilar record mu
0450: 73 74 20 62 65 0a 2a 2a 20 72 65 66 65 72 65 6e  st be.** referen
0460: 63 65 64 20 69 6e 20 74 68 65 20 6f 6e 72 65 6d  ced in the onrem
0470: 6f 74 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  ote table..**.**
0480: 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 74 65   Return the inte
0490: 67 65 72 20 72 65 63 6f 72 64 20 49 44 20 6f 66  ger record ID of
04a0: 20 74 68 65 20 73 69 6d 69 6c 61 72 20 72 65 63   the similar rec
04b0: 6f 72 64 2e 20 20 4f 72 20 72 65 74 75 72 6e 0a  ord.  Or return.
04c0: 2a 2a 20 30 20 69 66 20 6e 6f 6e 65 20 69 73 20  ** 0 if none is 
04d0: 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  found..*/.static
04e0: 20 69 6e 74 20 73 69 6d 69 6c 61 72 5f 72 65 63   int similar_rec
04f0: 6f 72 64 28 69 6e 74 20 72 69 64 2c 20 69 6e 74  ord(int rid, int
0500: 20 74 72 61 63 65 46 6c 61 67 29 7b 0a 20 20 69   traceFlag){.  i
0510: 6e 74 20 69 6e 43 6e 74 2c 20 6f 75 74 43 6e 74  nt inCnt, outCnt
0520: 3b 0a 20 20 53 74 6d 74 20 71 3b 0a 20 20 69 6e  ;.  Stmt q;.  in
0530: 74 20 71 75 65 75 65 5b 31 30 30 5d 3b 0a 0a 20  t queue[100];.. 
0540: 20 64 62 5f 70 72 65 70 61 72 65 28 26 71 2c 0a   db_prepare(&q,.
0550: 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 73 72        "SELECT sr
0560: 63 69 64 2c 20 45 58 49 53 54 53 28 53 45 4c 45  cid, EXISTS(SELE
0570: 43 54 20 31 20 46 52 4f 4d 20 6f 6e 72 65 6d 6f  CT 1 FROM onremo
0580: 74 65 20 57 48 45 52 45 20 72 69 64 3d 73 72 63  te WHERE rid=src
0590: 69 64 29 22 0a 20 20 20 20 20 20 22 20 20 46 52  id)".      "  FR
05a0: 4f 4d 20 64 65 6c 74 61 22 0a 20 20 20 20 20 20  OM delta".      
05b0: 22 20 57 48 45 52 45 20 72 69 64 3d 3a 78 22 0a  " WHERE rid=:x".
05c0: 20 20 20 20 20 20 22 20 55 4e 49 4f 4e 20 41 4c        " UNION AL
05d0: 4c 20 22 0a 20 20 20 20 20 20 22 53 45 4c 45 43  L ".      "SELEC
05e0: 54 20 72 69 64 2c 20 45 58 49 53 54 53 28 53 45  T rid, EXISTS(SE
05f0: 4c 45 43 54 20 31 20 46 52 4f 4d 20 6f 6e 72 65  LECT 1 FROM onre
0600: 6d 6f 74 65 20 57 48 45 52 45 20 72 69 64 3d 64  mote WHERE rid=d
0610: 65 6c 74 61 2e 72 69 64 29 22 0a 20 20 20 20 20  elta.rid)".     
0620: 20 22 20 20 46 52 4f 4d 20 64 65 6c 74 61 22 0a   "  FROM delta".
0630: 20 20 20 20 20 20 22 20 57 48 45 52 45 20 73 72        " WHERE sr
0640: 63 69 64 3d 3a 78 22 0a 20 20 29 3b 0a 20 20 71  cid=:x".  );.  q
0650: 75 65 75 65 5b 30 5d 20 3d 20 72 69 64 3b 0a 20  ueue[0] = rid;. 
0660: 20 69 6e 43 6e 74 20 3d 20 31 3b 0a 20 20 6f 75   inCnt = 1;.  ou
0670: 74 43 6e 74 20 3d 20 30 3b 0a 20 20 77 68 69 6c  tCnt = 0;.  whil
0680: 65 28 20 6f 75 74 43 6e 74 3c 69 6e 43 6e 74 20  e( outCnt<inCnt 
0690: 29 7b 0a 20 20 20 20 69 6e 74 20 78 69 64 20 3d  ){.    int xid =
06a0: 20 71 75 65 75 65 5b 6f 75 74 43 6e 74 25 36 34   queue[outCnt%64
06b0: 5d 3b 0a 20 20 20 20 6f 75 74 43 6e 74 2b 2b 3b  ];.    outCnt++;
06c0: 0a 20 20 20 20 64 62 5f 62 69 6e 64 5f 69 6e 74  .    db_bind_int
06d0: 28 26 71 2c 20 22 3a 78 22 2c 20 78 69 64 29 3b  (&q, ":x", xid);
06e0: 0a 20 20 20 20 69 66 28 20 74 72 61 63 65 46 6c  .    if( traceFl
06f0: 61 67 20 29 20 70 72 69 6e 74 66 28 22 78 69 64  ag ) printf("xid
0700: 3d 25 64 5c 6e 22 2c 20 78 69 64 29 3b 0a 20 20  =%d\n", xid);.  
0710: 20 20 77 68 69 6c 65 28 20 64 62 5f 73 74 65 70    while( db_step
0720: 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  (&q)==SQLITE_ROW
0730: 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 69   ){.      int ni
0740: 64 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e  d = db_column_in
0750: 74 28 26 71 2c 20 30 29 3b 0a 20 20 20 20 20 20  t(&q, 0);.      
0760: 69 6e 74 20 68 69 74 20 3d 20 64 62 5f 63 6f 6c  int hit = db_col
0770: 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 31 29 3b 0a  umn_int(&q, 1);.
0780: 20 20 20 20 20 20 69 66 28 20 74 72 61 63 65 46        if( traceF
0790: 6c 61 67 20 29 20 70 72 69 6e 74 66 28 22 6e 69  lag ) printf("ni
07a0: 64 3d 25 64 20 68 69 74 3d 25 64 5c 6e 22 2c 20  d=%d hit=%d\n", 
07b0: 6e 69 64 2c 20 68 69 74 29 3b 0a 20 20 20 20 20  nid, hit);.     
07c0: 20 69 66 28 20 68 69 74 20 20 29 7b 0a 20 20 20   if( hit  ){.   
07d0: 20 20 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65       db_finalize
07e0: 28 26 71 29 3b 0a 20 20 20 20 20 20 20 20 72 65  (&q);.        re
07f0: 74 75 72 6e 20 6e 69 64 3b 0a 20 20 20 20 20 20  turn nid;.      
0800: 7d 0a 20 20 20 20 20 20 69 66 28 20 69 6e 43 6e  }.      if( inCn
0810: 74 3c 73 69 7a 65 6f 66 28 71 75 65 75 65 29 2f  t<sizeof(queue)/
0820: 73 69 7a 65 6f 66 28 71 75 65 75 65 5b 30 5d 29  sizeof(queue[0])
0830: 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
0840: 69 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  i;.        for(i
0850: 3d 30 3b 20 69 3c 69 6e 43 6e 74 20 26 26 20 71  =0; i<inCnt && q
0860: 75 65 75 65 5b 69 5d 21 3d 6e 69 64 3b 20 69 2b  ueue[i]!=nid; i+
0870: 2b 29 7b 7d 0a 20 20 20 20 20 20 20 20 69 66 28  +){}.        if(
0880: 20 69 3e 3d 69 6e 43 6e 74 20 29 7b 0a 20 20 20   i>=inCnt ){.   
0890: 20 20 20 20 20 20 20 71 75 65 75 65 5b 69 6e 43         queue[inC
08a0: 6e 74 2b 2b 5d 20 3d 20 6e 69 64 3b 0a 20 20 20  nt++] = nid;.   
08b0: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
08c0: 20 20 20 7d 0a 20 20 20 20 64 62 5f 72 65 73 65     }.    db_rese
08d0: 74 28 26 71 29 3b 0a 20 20 7d 0a 20 20 64 62 5f  t(&q);.  }.  db_
08e0: 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 20 20  finalize(&q);.  
08f0: 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
0900: 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 74 65 73 74  ** COMMAND: test
0910: 2d 73 69 6d 69 6c 61 72 2d 72 65 63 6f 72 64 0a  -similar-record.
0920: 2a 2f 0a 76 6f 69 64 20 74 65 73 74 5f 73 69 6d  */.void test_sim
0930: 69 6c 61 72 5f 72 65 63 6f 72 64 28 76 6f 69 64  ilar_record(void
0940: 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66  ){.  int i;.  if
0950: 28 20 67 2e 61 72 67 63 3c 34 20 29 7b 0a 20 20  ( g.argc<4 ){.  
0960: 20 20 75 73 61 67 65 28 22 53 52 43 20 4f 4e 52    usage("SRC ONR
0970: 45 4d 4f 54 45 2e 2e 2e 22 29 3b 0a 20 20 7d 0a  EMOTE...");.  }.
0980: 20 20 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74    db_must_be_wit
0990: 68 69 6e 5f 74 72 65 65 28 29 3b 0a 20 20 64 62  hin_tree();.  db
09a0: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20  _multi_exec(.   
09b0: 20 22 43 52 45 41 54 45 20 54 45 4d 50 20 54 41   "CREATE TEMP TA
09c0: 42 4c 45 20 6f 6e 72 65 6d 6f 74 65 28 72 69 64  BLE onremote(rid
09d0: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
09e0: 20 4b 45 59 29 3b 22 0a 20 20 29 3b 0a 20 20 66   KEY);".  );.  f
09f0: 6f 72 28 69 3d 33 3b 20 69 3c 67 2e 61 72 67 63  or(i=3; i<g.argc
0a00: 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20  ; i++){.    int 
0a10: 72 69 64 20 3d 20 6e 61 6d 65 5f 74 6f 5f 72 69  rid = name_to_ri
0a20: 64 28 67 2e 61 72 67 76 5b 69 5d 29 3b 0a 20 20  d(g.argv[i]);.  
0a30: 20 20 70 72 69 6e 74 66 28 22 25 73 20 2d 3e 20    printf("%s -> 
0a40: 25 64 5c 6e 22 2c 20 67 2e 61 72 67 76 5b 69 5d  %d\n", g.argv[i]
0a50: 2c 20 72 69 64 29 3b 0a 20 20 20 20 64 62 5f 6d  , rid);.    db_m
0a60: 75 6c 74 69 5f 65 78 65 63 28 22 49 4e 53 45 52  ulti_exec("INSER
0a70: 54 20 49 4e 54 4f 20 6f 6e 72 65 6d 6f 74 65 20  T INTO onremote 
0a80: 56 41 4c 55 45 53 28 25 64 29 22 2c 20 72 69 64  VALUES(%d)", rid
0a90: 29 3b 0a 20 20 7d 0a 20 20 70 72 69 6e 74 66 28  );.  }.  printf(
0aa0: 22 73 69 6d 69 6c 61 72 3a 20 25 64 5c 6e 22 2c  "similar: %d\n",
0ab0: 20 73 69 6d 69 6c 61 72 5f 72 65 63 6f 72 64 28   similar_record(
0ac0: 6e 61 6d 65 5f 74 6f 5f 72 69 64 28 67 2e 61 72  name_to_rid(g.ar
0ad0: 67 76 5b 32 5d 29 2c 20 31 29 29 3b 0a 7d 0a 0a  gv[2]), 1));.}..
0ae0: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 54 6f 6b 65  ./*.** The aToke
0af0: 6e 5b 30 2e 2e 6e 54 6f 6b 65 6e 2d 31 5d 20 62  n[0..nToken-1] b
0b00: 6c 6f 62 20 61 72 72 61 79 20 69 73 20 61 20 70  lob array is a p
0b10: 61 72 73 65 20 6f 66 20 61 20 22 66 69 6c 65 22  arse of a "file"
0b20: 20 6c 69 6e 65 20 0a 2a 2a 20 6d 65 73 73 61 67   line .** messag
0b30: 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  e.  This routine
0b40: 20 66 69 6e 69 73 68 65 73 20 70 61 72 73 69 6e   finishes parsin
0b50: 67 20 74 68 61 74 20 6d 65 73 73 61 67 65 20 61  g that message a
0b60: 6e 64 20 64 6f 65 73 0a 2a 2a 20 61 20 72 65 63  nd does.** a rec
0b70: 6f 72 64 20 69 6e 73 65 72 74 20 6f 66 20 74 68  ord insert of th
0b80: 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  e file..**.** Th
0b90: 65 20 66 69 6c 65 20 6c 69 6e 65 20 69 73 20 69  e file line is i
0ba0: 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c  n one of the fol
0bb0: 6c 6f 77 69 6e 67 20 74 77 6f 20 66 6f 72 6d 73  lowing two forms
0bc0: 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 66 69 6c  :.**.**      fil
0bd0: 65 20 55 55 49 44 20 53 49 5a 45 20 5c 6e 20 43  e UUID SIZE \n C
0be0: 4f 4e 54 45 4e 54 0a 2a 2a 20 20 20 20 20 20 66  ONTENT.**      f
0bf0: 69 6c 65 20 55 55 49 44 20 44 45 4c 54 41 53 52  ile UUID DELTASR
0c00: 43 20 53 49 5a 45 20 5c 6e 20 43 4f 4e 54 45 4e  C SIZE \n CONTEN
0c10: 54 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74  T.**.** The cont
0c20: 65 6e 74 20 69 73 20 53 49 5a 45 20 62 79 74 65  ent is SIZE byte
0c30: 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f  s immediately fo
0c40: 6c 6c 6f 77 69 6e 67 20 74 68 65 20 6e 65 77 6c  llowing the newl
0c50: 69 6e 65 2e 0a 2a 2a 20 49 66 20 44 45 4c 54 41  ine..** If DELTA
0c60: 53 52 43 20 65 78 69 73 74 73 2c 20 74 68 65 6e  SRC exists, then
0c70: 20 74 68 65 20 43 4f 4e 54 45 4e 54 20 69 73 20   the CONTENT is 
0c80: 61 20 64 65 6c 74 61 20 61 67 61 69 6e 73 74 20  a delta against 
0c90: 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f  the.** content o
0ca0: 66 20 44 45 4c 54 41 53 52 43 2e 0a 2a 2a 0a 2a  f DELTASRC..**.*
0cb0: 2a 20 49 66 20 61 6e 79 20 65 72 72 6f 72 20 6f  * If any error o
0cc0: 63 63 75 72 73 2c 20 77 72 69 74 65 20 61 20 6d  ccurs, write a m
0cd0: 65 73 73 61 67 65 20 69 6e 74 6f 20 70 45 72 72  essage into pErr
0ce0: 20 77 68 69 63 68 20 68 61 73 20 61 6c 72 65 61   which has alrea
0cf0: 64 79 0a 2a 2a 20 62 65 20 69 6e 69 74 69 61 6c  dy.** be initial
0d00: 69 7a 65 64 20 74 6f 20 61 6e 20 65 6d 70 74 79  ized to an empty
0d10: 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74   string..*/.stat
0d20: 69 63 20 76 6f 69 64 20 78 66 65 72 5f 61 63 63  ic void xfer_acc
0d30: 65 70 74 5f 66 69 6c 65 28 42 6c 6f 62 20 2a 70  ept_file(Blob *p
0d40: 49 6e 2c 20 42 6c 6f 62 20 2a 61 54 6f 6b 65 6e  In, Blob *aToken
0d50: 2c 20 69 6e 74 20 6e 54 6f 6b 65 6e 2c 20 42 6c  , int nToken, Bl
0d60: 6f 62 20 2a 70 45 72 72 29 7b 0a 20 20 69 6e 74  ob *pErr){.  int
0d70: 20 6e 3b 0a 20 20 69 6e 74 20 72 69 64 3b 0a 20   n;.  int rid;. 
0d80: 20 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 2c 20 68   Blob content, h
0d90: 61 73 68 3b 0a 20 20 0a 20 20 69 66 28 20 6e 54  ash;.  .  if( nT
0da0: 6f 6b 65 6e 3c 33 20 7c 7c 20 6e 54 6f 6b 65 6e  oken<3 || nToken
0db0: 3e 34 20 7c 7c 20 21 62 6c 6f 62 5f 69 73 5f 75  >4 || !blob_is_u
0dc0: 75 69 64 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 0a  uid(&aToken[1]).
0dd0: 20 20 20 20 20 20 20 7c 7c 20 21 62 6c 6f 62 5f         || !blob_
0de0: 69 73 5f 69 6e 74 28 26 61 54 6f 6b 65 6e 5b 6e  is_int(&aToken[n
0df0: 54 6f 6b 65 6e 2d 31 5d 2c 20 26 6e 29 20 7c 7c  Token-1], &n) ||
0e00: 20 6e 3c 3d 30 0a 20 20 20 20 20 20 20 7c 7c 20   n<=0.       || 
0e10: 28 6e 54 6f 6b 65 6e 3d 3d 34 20 26 26 20 21 62  (nToken==4 && !b
0e20: 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26 61 54 6f  lob_is_uuid(&aTo
0e30: 6b 65 6e 5b 32 5d 29 29 20 29 7b 0a 20 20 20 20  ken[2])) ){.    
0e40: 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70 45 72  blob_appendf(pEr
0e50: 72 2c 20 22 6d 61 6c 66 6f 72 6d 65 64 20 66 69  r, "malformed fi
0e60: 6c 65 20 6c 69 6e 65 22 29 3b 0a 20 20 20 20 72  le line");.    r
0e70: 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 62 6c 6f  eturn;.  }.  blo
0e80: 62 5f 7a 65 72 6f 28 26 63 6f 6e 74 65 6e 74 29  b_zero(&content)
0e90: 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 68  ;.  blob_zero(&h
0ea0: 61 73 68 29 3b 0a 20 20 62 6c 6f 62 5f 65 78 74  ash);.  blob_ext
0eb0: 72 61 63 74 28 70 49 6e 2c 20 6e 2c 20 26 63 6f  ract(pIn, n, &co
0ec0: 6e 74 65 6e 74 29 3b 0a 20 20 69 66 28 20 6e 54  ntent);.  if( nT
0ed0: 6f 6b 65 6e 3d 3d 34 20 29 7b 0a 20 20 20 20 42  oken==4 ){.    B
0ee0: 6c 6f 62 20 73 72 63 3b 0a 20 20 20 20 69 6e 74  lob src;.    int
0ef0: 20 73 72 63 69 64 20 3d 20 64 62 5f 69 6e 74 28   srcid = db_int(
0f00: 30 2c 20 22 53 45 4c 45 43 54 20 72 69 64 20 46  0, "SELECT rid F
0f10: 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 75  ROM blob WHERE u
0f20: 75 69 64 3d 25 42 22 2c 20 26 61 54 6f 6b 65 6e  uid=%B", &aToken
0f30: 5b 32 5d 29 3b 0a 20 20 20 20 69 66 28 20 73 72  [2]);.    if( sr
0f40: 63 69 64 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  cid==0 ){.      
0f50: 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70 45 72  blob_appendf(pEr
0f60: 72 2c 20 22 75 6e 6b 6e 6f 77 6e 20 64 65 6c 74  r, "unknown delt
0f70: 61 20 73 6f 75 72 63 65 3a 20 25 62 22 2c 20 26  a source: %b", &
0f80: 61 54 6f 6b 65 6e 5b 32 5d 29 3b 0a 20 20 20 20  aToken[2]);.    
0f90: 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
0fa0: 20 20 20 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28      content_get(
0fb0: 73 72 63 69 64 2c 20 26 73 72 63 29 3b 0a 20 20  srcid, &src);.  
0fc0: 20 20 62 6c 6f 62 5f 64 65 6c 74 61 5f 61 70 70    blob_delta_app
0fd0: 6c 79 28 26 73 72 63 2c 20 26 63 6f 6e 74 65 6e  ly(&src, &conten
0fe0: 74 2c 20 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20  t, &content);.  
0ff0: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 73 72    blob_reset(&sr
1000: 63 29 3b 0a 20 20 7d 0a 20 20 73 68 61 31 73 75  c);.  }.  sha1su
1010: 6d 5f 62 6c 6f 62 28 26 63 6f 6e 74 65 6e 74 2c  m_blob(&content,
1020: 20 26 68 61 73 68 29 3b 0a 20 20 69 66 28 20 21   &hash);.  if( !
1030: 62 6c 6f 62 5f 65 71 5f 73 74 72 28 26 61 54 6f  blob_eq_str(&aTo
1040: 6b 65 6e 5b 31 5d 2c 20 62 6c 6f 62 5f 73 74 72  ken[1], blob_str
1050: 28 26 68 61 73 68 29 2c 20 2d 31 29 20 29 7b 0a  (&hash), -1) ){.
1060: 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66      blob_appendf
1070: 28 70 45 72 72 2c 20 22 63 6f 6e 74 65 6e 74 20  (pErr, "content 
1080: 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 73  does not match s
1090: 68 61 31 20 68 61 73 68 22 29 3b 0a 20 20 7d 0a  ha1 hash");.  }.
10a0: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 68 61    blob_reset(&ha
10b0: 73 68 29 3b 0a 20 20 72 69 64 20 3d 20 63 6f 6e  sh);.  rid = con
10c0: 74 65 6e 74 5f 70 75 74 28 26 63 6f 6e 74 65 6e  tent_put(&conten
10d0: 74 2c 20 30 29 3b 0a 20 20 6d 61 6e 69 66 65 73  t, 0);.  manifes
10e0: 74 5f 63 72 6f 73 73 6c 69 6e 6b 28 72 69 64 2c  t_crosslink(rid,
10f0: 20 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 69 66   &content);.  if
1100: 28 20 72 69 64 3d 3d 30 20 29 7b 0a 20 20 20 20  ( rid==0 ){.    
1110: 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70 45 72  blob_appendf(pEr
1120: 72 2c 20 22 25 73 22 2c 20 67 2e 7a 45 72 72 4d  r, "%s", g.zErrM
1130: 73 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  sg);.  }.}../*.*
1140: 2a 20 53 65 6e 64 20 74 68 65 20 66 69 6c 65 20  * Send the file 
1150: 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 72 69  identified by ri
1160: 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4f 75 74  d..**.** If pOut
1170: 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68   is not NULL, th
1180: 65 6e 20 61 70 70 65 6e 64 20 74 68 65 20 74 65  en append the te
1190: 78 74 20 6f 66 20 74 68 65 20 73 65 6e 64 20 6d  xt of the send m
11a0: 65 73 73 61 67 65 0a 2a 2a 20 74 6f 20 70 4f 75  essage.** to pOu
11b0: 74 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 61  t.  Otherwise, a
11c0: 70 70 65 6e 64 20 74 68 65 20 74 65 78 74 20 74  ppend the text t
11d0: 6f 20 74 68 65 20 43 47 49 20 6f 75 74 70 75 74  o the CGI output
11e0: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
11f0: 73 65 6e 64 5f 66 69 6c 65 28 69 6e 74 20 72 69  send_file(int ri
1200: 64 2c 20 42 6c 6f 62 20 2a 70 4f 75 74 29 7b 0a  d, Blob *pOut){.
1210: 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 2c 20    Blob content, 
1220: 75 75 69 64 3b 0a 20 20 69 6e 74 20 73 69 7a 65  uuid;.  int size
1230: 3b 0a 20 20 69 6e 74 20 73 72 63 69 64 3b 0a 0a  ;.  int srcid;..
1240: 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 75 75  .  blob_zero(&uu
1250: 69 64 29 3b 0a 20 20 64 62 5f 62 6c 6f 62 28 26  id);.  db_blob(&
1260: 75 75 69 64 2c 20 22 53 45 4c 45 43 54 20 75 75  uuid, "SELECT uu
1270: 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45  id FROM blob WHE
1280: 52 45 20 72 69 64 3d 25 64 20 41 4e 44 20 73 69  RE rid=%d AND si
1290: 7a 65 3e 3d 30 22 2c 20 72 69 64 29 3b 0a 20 20  ze>=0", rid);.  
12a0: 69 66 28 20 62 6c 6f 62 5f 73 69 7a 65 28 26 75  if( blob_size(&u
12b0: 75 69 64 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72  uid)==0 ){.    r
12c0: 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 63  eturn 0;.  }.  c
12d0: 6f 6e 74 65 6e 74 5f 67 65 74 28 72 69 64 2c 20  ontent_get(rid, 
12e0: 26 63 6f 6e 74 65 6e 74 29 3b 0a 0a 20 20 73 72  &content);..  sr
12f0: 63 69 64 20 3d 20 73 69 6d 69 6c 61 72 5f 72 65  cid = similar_re
1300: 63 6f 72 64 28 72 69 64 2c 20 30 29 3b 0a 20 20  cord(rid, 0);.  
1310: 69 66 28 20 73 72 63 69 64 20 29 7b 0a 20 20 20  if( srcid ){.   
1320: 20 42 6c 6f 62 20 73 72 63 2c 20 64 65 6c 74 61   Blob src, delta
1330: 3b 0a 20 20 20 20 42 6c 6f 62 20 73 72 63 75 75  ;.    Blob srcuu
1340: 69 64 3b 0a 20 20 20 20 63 6f 6e 74 65 6e 74 5f  id;.    content_
1350: 67 65 74 28 73 72 63 69 64 2c 20 26 73 72 63 29  get(srcid, &src)
1360: 3b 0a 20 20 20 20 62 6c 6f 62 5f 64 65 6c 74 61  ;.    blob_delta
1370: 5f 63 72 65 61 74 65 28 26 73 72 63 2c 20 26 63  _create(&src, &c
1380: 6f 6e 74 65 6e 74 2c 20 26 64 65 6c 74 61 29 3b  ontent, &delta);
1390: 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28  .    blob_reset(
13a0: 26 73 72 63 29 3b 0a 20 20 20 20 62 6c 6f 62 5f  &src);.    blob_
13b0: 72 65 73 65 74 28 26 63 6f 6e 74 65 6e 74 29 3b  reset(&content);
13c0: 0a 20 20 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26  .    blob_zero(&
13d0: 73 72 63 75 75 69 64 29 3b 0a 20 20 20 20 64 62  srcuuid);.    db
13e0: 5f 62 6c 6f 62 28 26 73 72 63 75 75 69 64 2c 20  _blob(&srcuuid, 
13f0: 22 53 45 4c 45 43 54 20 75 75 69 64 20 46 52 4f  "SELECT uuid FRO
1400: 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 72 69 64  M blob WHERE rid
1410: 3d 25 64 22 2c 20 73 72 63 69 64 29 3b 0a 20 20  =%d", srcid);.  
1420: 20 20 73 69 7a 65 20 3d 20 62 6c 6f 62 5f 73 69    size = blob_si
1430: 7a 65 28 26 64 65 6c 74 61 29 3b 0a 20 20 20 20  ze(&delta);.    
1440: 69 66 28 20 70 4f 75 74 20 29 7b 0a 20 20 20 20  if( pOut ){.    
1450: 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70    blob_appendf(p
1460: 4f 75 74 2c 20 22 66 69 6c 65 20 25 62 20 25 62  Out, "file %b %b
1470: 20 25 64 5c 6e 22 2c 20 26 75 75 69 64 2c 20 26   %d\n", &uuid, &
1480: 73 72 63 75 75 69 64 2c 20 73 69 7a 65 29 3b 0a  srcuuid, size);.
1490: 20 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e        blob_appen
14a0: 64 28 70 4f 75 74 2c 20 62 6c 6f 62 5f 62 75 66  d(pOut, blob_buf
14b0: 66 65 72 28 26 64 65 6c 74 61 29 2c 20 73 69 7a  fer(&delta), siz
14c0: 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  e);.    }else{. 
14d0: 20 20 20 20 20 63 67 69 5f 70 72 69 6e 74 66 28       cgi_printf(
14e0: 22 66 69 6c 65 20 25 62 20 25 62 20 25 64 5c 6e  "file %b %b %d\n
14f0: 22 2c 20 26 75 75 69 64 2c 20 26 73 72 63 75 75  ", &uuid, &srcuu
1500: 69 64 2c 20 73 69 7a 65 29 3b 0a 20 20 20 20 20  id, size);.     
1510: 20 63 67 69 5f 61 70 70 65 6e 64 5f 63 6f 6e 74   cgi_append_cont
1520: 65 6e 74 28 62 6c 6f 62 5f 62 75 66 66 65 72 28  ent(blob_buffer(
1530: 26 64 65 6c 74 61 29 2c 20 73 69 7a 65 29 3b 0a  &delta), size);.
1540: 20 20 20 20 7d 0a 20 20 20 20 62 6c 6f 62 5f 72      }.    blob_r
1550: 65 73 65 74 28 26 64 65 6c 74 61 29 3b 0a 20 20  eset(&delta);.  
1560: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 73 72    blob_reset(&sr
1570: 63 75 75 69 64 29 3b 0a 20 20 20 20 62 6c 6f 62  cuuid);.    blob
1580: 5f 72 65 73 65 74 28 26 75 75 69 64 29 3b 0a 20  _reset(&uuid);. 
1590: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 69 7a 65   }else{.    size
15a0: 20 3d 20 62 6c 6f 62 5f 73 69 7a 65 28 26 63 6f   = blob_size(&co
15b0: 6e 74 65 6e 74 29 3b 0a 20 20 20 20 69 66 28 20  ntent);.    if( 
15c0: 70 4f 75 74 20 29 7b 0a 20 20 20 20 20 20 62 6c  pOut ){.      bl
15d0: 6f 62 5f 61 70 70 65 6e 64 66 28 70 4f 75 74 2c  ob_appendf(pOut,
15e0: 20 22 66 69 6c 65 20 25 62 20 25 64 5c 6e 22 2c   "file %b %d\n",
15f0: 20 26 75 75 69 64 2c 20 73 69 7a 65 29 3b 0a 20   &uuid, size);. 
1600: 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64       blob_append
1610: 28 70 4f 75 74 2c 20 62 6c 6f 62 5f 62 75 66 66  (pOut, blob_buff
1620: 65 72 28 26 63 6f 6e 74 65 6e 74 29 2c 20 73 69  er(&content), si
1630: 7a 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ze);.    }else{.
1640: 20 20 20 20 20 20 63 67 69 5f 70 72 69 6e 74 66        cgi_printf
1650: 28 22 66 69 6c 65 20 25 62 20 25 64 5c 6e 22 2c  ("file %b %d\n",
1660: 20 26 75 75 69 64 2c 20 73 69 7a 65 29 3b 0a 20   &uuid, size);. 
1670: 20 20 20 20 20 63 67 69 5f 61 70 70 65 6e 64 5f       cgi_append_
1680: 63 6f 6e 74 65 6e 74 28 62 6c 6f 62 5f 62 75 66  content(blob_buf
1690: 66 65 72 28 26 63 6f 6e 74 65 6e 74 29 2c 20 73  fer(&content), s
16a0: 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ize);.    }.    
16b0: 62 6c 6f 62 5f 72 65 73 65 74 28 26 63 6f 6e 74  blob_reset(&cont
16c0: 65 6e 74 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 72  ent);.    blob_r
16d0: 65 73 65 74 28 26 75 75 69 64 29 3b 0a 20 20 7d  eset(&uuid);.  }
16e0: 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63  .  db_multi_exec
16f0: 28 22 49 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f  ("INSERT OR IGNO
1700: 52 45 20 49 4e 54 4f 20 6f 6e 72 65 6d 6f 74 65  RE INTO onremote
1710: 20 56 41 4c 55 45 53 28 25 64 29 22 2c 20 72 69   VALUES(%d)", ri
1720: 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 69 7a  d);.  return siz
1730: 65 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 65 6e  e;.}.../*.** Sen
1740: 64 20 61 6c 6c 20 70 65 6e 64 69 6e 67 20 66 69  d all pending fi
1750: 6c 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  les..*/.static i
1760: 6e 74 20 73 65 6e 64 5f 61 6c 6c 5f 70 65 6e 64  nt send_all_pend
1770: 69 6e 67 28 42 6c 6f 62 20 2a 70 4f 75 74 29 7b  ing(Blob *pOut){
1780: 0a 20 20 69 6e 74 20 69 52 69 64 53 65 6e 74 20  .  int iRidSent 
1790: 3d 20 30 3b 0a 20 20 69 6e 74 20 73 65 6e 74 20  = 0;.  int sent 
17a0: 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 53 65 6e 74  = 0;.  int nSent
17b0: 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6d 61 78 53   = 0;.  int maxS
17c0: 69 7a 65 20 3d 20 64 62 5f 67 65 74 5f 69 6e 74  ize = db_get_int
17d0: 28 22 68 74 74 70 2d 6d 73 67 2d 73 69 7a 65 22  ("http-msg-size"
17e0: 2c 20 35 30 30 30 30 30 29 3b 0a 20 20 53 74 6d  , 500000);.  Stm
17f0: 74 20 71 3b 0a 0a 20 20 64 62 5f 70 72 65 70 61  t q;..  db_prepa
1800: 72 65 28 26 71 2c 20 22 53 45 4c 45 43 54 20 72  re(&q, "SELECT r
1810: 69 64 20 46 52 4f 4d 20 70 65 6e 64 69 6e 67 20  id FROM pending 
1820: 4f 52 44 45 52 20 42 59 20 72 69 64 22 29 3b 0a  ORDER BY rid");.
1830: 20 20 77 68 69 6c 65 28 20 64 62 5f 73 74 65 70    while( db_step
1840: 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  (&q)==SQLITE_ROW
1850: 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 69 64 20   ){.    int rid 
1860: 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28  = db_column_int(
1870: 26 71 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  &q, 0);.    if( 
1880: 73 65 6e 74 3c 6d 61 78 53 69 7a 65 20 29 7b 0a  sent<maxSize ){.
1890: 20 20 20 20 20 20 73 65 6e 74 20 2b 3d 20 73 65        sent += se
18a0: 6e 64 5f 66 69 6c 65 28 72 69 64 2c 20 70 4f 75  nd_file(rid, pOu
18b0: 74 29 3b 0a 20 20 20 20 20 20 6e 53 65 6e 74 2b  t);.      nSent+
18c0: 2b 3b 0a 20 20 20 20 20 20 69 52 69 64 53 65 6e  +;.      iRidSen
18d0: 74 20 3d 20 72 69 64 3b 0a 20 20 20 20 7d 65 6c  t = rid;.    }el
18e0: 73 65 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a  se{.      char *
18f0: 7a 55 75 69 64 20 3d 20 64 62 5f 74 65 78 74 28  zUuid = db_text(
1900: 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  0,.             
1910: 20 20 20 20 20 20 20 20 20 22 53 45 4c 45 43 54           "SELECT
1920: 20 75 75 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20   uuid FROM blob 
1930: 57 48 45 52 45 20 72 69 64 3d 25 64 20 41 4e 44  WHERE rid=%d AND
1940: 20 73 69 7a 65 3e 3d 30 22 2c 20 72 69 64 29 3b   size>=0", rid);
1950: 0a 20 20 20 20 20 20 69 66 28 20 7a 55 75 69 64  .      if( zUuid
1960: 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
1970: 70 4f 75 74 20 29 7b 0a 20 20 20 20 20 20 20 20  pOut ){.        
1980: 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70    blob_appendf(p
1990: 4f 75 74 2c 20 22 69 67 6f 74 20 25 73 5c 6e 22  Out, "igot %s\n"
19a0: 2c 20 7a 55 75 69 64 29 3b 0a 20 20 20 20 20 20  , zUuid);.      
19b0: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
19c0: 20 20 20 63 67 69 5f 70 72 69 6e 74 66 28 22 69     cgi_printf("i
19d0: 67 6f 74 20 25 73 5c 6e 22 2c 20 7a 55 75 69 64  got %s\n", zUuid
19e0: 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
19f0: 20 20 20 20 20 66 72 65 65 28 7a 55 75 69 64 29       free(zUuid)
1a00: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1a10: 20 20 7d 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a    }.  db_finaliz
1a20: 65 28 26 71 29 3b 0a 20 20 0a 20 20 2f 2a 20 44  e(&q);.  .  /* D
1a30: 65 6c 65 74 65 20 74 68 65 20 27 70 65 6e 64 69  elete the 'pendi
1a40: 6e 67 27 20 72 65 63 6f 72 64 73 20 66 6f 72 20  ng' records for 
1a50: 61 6c 6c 20 66 69 6c 65 73 20 6a 75 73 74 20 73  all files just s
1a60: 65 6e 74 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a  ent. Otherwise,.
1a70: 20 20 2a 2a 20 77 65 20 63 61 6e 20 77 69 6e 64    ** we can wind
1a80: 20 75 70 20 73 65 6e 64 69 6e 67 20 73 6f 6d 65   up sending some
1a90: 20 66 69 6c 65 73 20 6d 6f 72 65 20 74 68 61 6e   files more than
1aa0: 20 6f 6e 63 65 2e 0a 20 20 2a 2f 0a 20 20 69 66   once..  */.  if
1ab0: 28 20 6e 53 65 6e 74 3e 30 20 29 7b 0a 20 20 20  ( nSent>0 ){.   
1ac0: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22   db_multi_exec("
1ad0: 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 65 6e 64  DELETE FROM pend
1ae0: 69 6e 67 20 57 48 45 52 45 20 72 69 64 20 3c 3d  ing WHERE rid <=
1af0: 20 25 64 22 2c 20 69 52 69 64 53 65 6e 74 29 3b   %d", iRidSent);
1b00: 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 6e  .  }..  return n
1b10: 53 65 6e 74 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  Sent;.}.../*.** 
1b20: 43 68 65 63 6b 20 74 68 65 20 73 69 67 6e 61 74  Check the signat
1b30: 75 72 65 20 6f 6e 20 61 6e 20 61 70 70 6c 69 63  ure on an applic
1b40: 61 74 69 6f 6e 2f 78 2d 66 6f 73 73 69 6c 20 70  ation/x-fossil p
1b50: 61 79 6c 6f 61 64 20 72 65 63 65 69 76 65 64 20  ayload received 
1b60: 62 79 0a 2a 2a 20 74 68 65 20 48 54 54 50 20 73  by.** the HTTP s
1b70: 65 72 76 65 72 2e 20 20 54 68 65 20 73 69 67 6e  erver.  The sign
1b80: 61 74 75 72 65 20 69 73 20 61 20 6c 69 6e 65 20  ature is a line 
1b90: 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
1ba0: 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   form:.**.**    
1bb0: 20 20 20 20 6c 6f 67 69 6e 20 4c 4f 47 49 4e 20      login LOGIN 
1bc0: 4e 4f 4e 43 45 20 53 49 47 4e 41 54 55 52 45 0a  NONCE SIGNATURE.
1bd0: 2a 2a 0a 2a 2a 20 54 68 65 20 4e 4f 4e 43 45 20  **.** The NONCE 
1be0: 69 73 20 61 20 72 61 6e 64 6f 6d 20 73 74 72 69  is a random stri
1bf0: 6e 67 2e 20 20 54 68 65 20 73 65 72 76 65 72 20  ng.  The server 
1c00: 77 69 6c 6c 20 6e 65 76 65 72 20 61 63 63 65 70  will never accep
1c10: 74 20 61 0a 2a 2a 20 72 65 70 65 61 74 20 4e 4f  t a.** repeat NO
1c20: 4e 43 45 2e 20 20 53 49 47 4e 41 54 55 52 45 20  NCE.  SIGNATURE 
1c30: 69 73 20 74 68 65 20 53 48 41 31 20 63 68 65 63  is the SHA1 chec
1c40: 6b 73 75 6d 20 6f 66 20 74 68 65 20 4e 4f 4e 43  ksum of the NONC
1c50: 45 20 0a 2a 2a 20 63 6f 6e 63 61 74 65 6e 61 74  E .** concatenat
1c60: 65 64 20 77 69 74 68 20 74 68 65 20 75 73 65 72  ed with the user
1c70: 73 20 70 61 73 73 77 6f 72 64 2e 0a 2a 2a 0a 2a  s password..**.*
1c80: 2a 20 54 68 65 20 70 61 72 61 6d 65 74 65 72 73  * The parameters
1c90: 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65   to this routine
1ca0: 20 61 72 65 20 65 70 68 65 72 6d 65 72 61 6c 20   are ephermeral 
1cb0: 62 6c 6f 62 73 20 68 6f 6c 64 69 6e 67 20 74 68  blobs holding th
1cc0: 65 0a 2a 2a 20 4c 4f 47 49 4e 2c 20 4e 4f 4e 43  e.** LOGIN, NONC
1cd0: 45 20 61 6e 64 20 53 49 47 4e 41 54 55 52 45 2e  E and SIGNATURE.
1ce0: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
1cf0: 69 6e 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20  ine attempts to 
1d00: 6c 6f 63 61 74 65 20 74 68 65 20 75 73 65 72 20  locate the user 
1d10: 61 6e 64 20 76 65 72 69 66 79 20 74 68 65 20 73  and verify the s
1d20: 69 67 6e 61 74 75 72 65 2e 0a 2a 2a 20 49 66 20  ignature..** If 
1d30: 65 76 65 72 79 74 68 69 6e 67 20 63 68 65 63 6b  everything check
1d40: 73 20 6f 75 74 2c 20 74 68 65 20 55 53 45 52 2e  s out, the USER.
1d50: 43 41 50 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 74  CAP column for t
1d60: 68 65 20 55 53 45 52 20 74 61 62 6c 65 0a 2a 2a  he USER table.**
1d70: 20 69 73 20 63 6f 6e 73 75 6c 74 65 64 20 74 6f   is consulted to
1d80: 20 73 65 74 20 70 72 69 76 69 6c 65 67 65 73 20   set privileges 
1d90: 69 6e 20 74 68 65 20 67 6c 6f 62 61 6c 20 67 20  in the global g 
1da0: 76 61 72 69 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  variable..**.** 
1db0: 49 66 20 61 6e 79 74 68 69 6e 67 20 66 61 69 6c  If anything fail
1dc0: 73 20 74 6f 20 63 68 65 63 6b 20 6f 75 74 2c 20  s to check out, 
1dd0: 6e 6f 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d  no changes are m
1de0: 61 64 65 20 74 6f 20 70 72 69 76 69 6c 65 67 65  ade to privilege
1df0: 73 2e 0a 2a 2a 0a 2a 2a 20 53 69 67 6e 61 74 75  s..**.** Signatu
1e00: 72 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 6e  re generation on
1e10: 20 74 68 65 20 63 6c 69 65 6e 74 20 73 69 64 65   the client side
1e20: 20 69 73 20 68 61 6e 64 6c 65 64 20 62 79 20 74   is handled by t
1e30: 68 65 20 0a 2a 2a 20 68 74 74 70 5f 65 78 63 68  he .** http_exch
1e40: 61 6e 67 65 28 29 20 72 6f 75 74 69 6e 65 2e 0a  ange() routine..
1e50: 2a 2f 0a 76 6f 69 64 20 63 68 65 63 6b 5f 6c 6f  */.void check_lo
1e60: 67 69 6e 28 42 6c 6f 62 20 2a 70 4c 6f 67 69 6e  gin(Blob *pLogin
1e70: 2c 20 42 6c 6f 62 20 2a 70 4e 6f 6e 63 65 2c 20  , Blob *pNonce, 
1e80: 42 6c 6f 62 20 2a 70 53 69 67 29 7b 0a 20 20 53  Blob *pSig){.  S
1e90: 74 6d 74 20 71 3b 0a 20 20 69 6e 74 20 72 63 3b  tmt q;.  int rc;
1ea0: 0a 0a 20 20 69 66 28 20 64 62 5f 65 78 69 73 74  ..  if( db_exist
1eb0: 73 28 22 53 45 4c 45 43 54 20 31 20 46 52 4f 4d  s("SELECT 1 FROM
1ec0: 20 72 63 76 66 72 6f 6d 20 57 48 45 52 45 20 6e   rcvfrom WHERE n
1ed0: 6f 6e 63 65 3d 25 42 22 2c 20 70 4e 6f 6e 63 65  once=%B", pNonce
1ee0: 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ) ){.    return;
1ef0: 20 20 2f 2a 20 4e 65 76 65 72 20 61 63 63 65 70    /* Never accep
1f00: 74 20 61 20 72 65 70 65 61 74 65 64 20 6e 6f 6e  t a repeated non
1f10: 63 65 20 2a 2f 0a 20 20 7d 0a 20 20 64 62 5f 70  ce */.  }.  db_p
1f20: 72 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c 45  repare(&q, "SELE
1f30: 43 54 20 70 77 2c 20 63 61 70 2c 20 75 69 64 20  CT pw, cap, uid 
1f40: 46 52 4f 4d 20 75 73 65 72 20 57 48 45 52 45 20  FROM user WHERE 
1f50: 6c 6f 67 69 6e 3d 25 42 22 2c 20 70 4c 6f 67 69  login=%B", pLogi
1f60: 6e 29 3b 0a 20 20 69 66 28 20 64 62 5f 73 74 65  n);.  if( db_ste
1f70: 70 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f  p(&q)==SQLITE_RO
1f80: 57 20 29 7b 0a 20 20 20 20 42 6c 6f 62 20 70 77  W ){.    Blob pw
1f90: 2c 20 63 6f 6d 62 69 6e 65 64 2c 20 68 61 73 68  , combined, hash
1fa0: 3b 0a 20 20 20 20 62 6c 6f 62 5f 7a 65 72 6f 28  ;.    blob_zero(
1fb0: 26 70 77 29 3b 0a 20 20 20 20 64 62 5f 65 70 68  &pw);.    db_eph
1fc0: 65 6d 65 72 61 6c 5f 62 6c 6f 62 28 26 71 2c 20  emeral_blob(&q, 
1fd0: 30 2c 20 26 70 77 29 3b 0a 20 20 20 20 62 6c 6f  0, &pw);.    blo
1fe0: 62 5f 7a 65 72 6f 28 26 63 6f 6d 62 69 6e 65 64  b_zero(&combined
1ff0: 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 63 6f 70 79  );.    blob_copy
2000: 28 26 63 6f 6d 62 69 6e 65 64 2c 20 70 4e 6f 6e  (&combined, pNon
2010: 63 65 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 61 70  ce);.    blob_ap
2020: 70 65 6e 64 28 26 63 6f 6d 62 69 6e 65 64 2c 20  pend(&combined, 
2030: 62 6c 6f 62 5f 62 75 66 66 65 72 28 26 70 77 29  blob_buffer(&pw)
2040: 2c 20 62 6c 6f 62 5f 73 69 7a 65 28 26 70 77 29  , blob_size(&pw)
2050: 29 3b 0a 20 20 20 20 2f 2a 20 43 47 49 44 45 42  );.    /* CGIDEB
2060: 55 47 28 28 22 70 72 65 73 69 67 3d 5b 25 73 5d  UG(("presig=[%s]
2070: 5c 6e 22 2c 20 62 6c 6f 62 5f 73 74 72 28 26 63  \n", blob_str(&c
2080: 6f 6d 62 69 6e 65 64 29 29 29 3b 20 2a 2f 0a 20  ombined))); */. 
2090: 20 20 20 73 68 61 31 73 75 6d 5f 62 6c 6f 62 28     sha1sum_blob(
20a0: 26 63 6f 6d 62 69 6e 65 64 2c 20 26 68 61 73 68  &combined, &hash
20b0: 29 3b 0a 20 20 20 20 72 63 20 3d 20 62 6c 6f 62  );.    rc = blob
20c0: 5f 63 6f 6d 70 61 72 65 28 26 68 61 73 68 2c 20  _compare(&hash, 
20d0: 70 53 69 67 29 3b 0a 20 20 20 20 62 6c 6f 62 5f  pSig);.    blob_
20e0: 72 65 73 65 74 28 26 68 61 73 68 29 3b 0a 20 20  reset(&hash);.  
20f0: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 63 6f    blob_reset(&co
2100: 6d 62 69 6e 65 64 29 3b 0a 20 20 20 20 69 66 28  mbined);.    if(
2110: 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20   rc==0 ){.      
2120: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 61 70  const char *zCap
2130: 3b 0a 20 20 20 20 20 20 7a 43 61 70 20 3d 20 64  ;.      zCap = d
2140: 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 26 71  b_column_text(&q
2150: 2c 20 31 29 3b 0a 20 20 20 20 20 20 6c 6f 67 69  , 1);.      logi
2160: 6e 5f 73 65 74 5f 63 61 70 61 62 69 6c 69 74 69  n_set_capabiliti
2170: 65 73 28 7a 43 61 70 29 3b 0a 20 20 20 20 20 20  es(zCap);.      
2180: 67 2e 75 73 65 72 55 69 64 20 3d 20 64 62 5f 63  g.userUid = db_c
2190: 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 32 29  olumn_int(&q, 2)
21a0: 3b 0a 20 20 20 20 20 20 67 2e 7a 4c 6f 67 69 6e  ;.      g.zLogin
21b0: 20 3d 20 6d 70 72 69 6e 74 66 28 22 25 62 22 2c   = mprintf("%b",
21c0: 20 70 4c 6f 67 69 6e 29 3b 0a 20 20 20 20 20 20   pLogin);.      
21d0: 67 2e 7a 4e 6f 6e 63 65 20 3d 20 6d 70 72 69 6e  g.zNonce = mprin
21e0: 74 66 28 22 25 62 22 2c 20 70 4e 6f 6e 63 65 29  tf("%b", pNonce)
21f0: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 64 62  ;.    }.  }.  db
2200: 5f 72 65 73 65 74 28 26 71 29 3b 0a 7d 0a 0a 0a  _reset(&q);.}...
2210: 2f 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 76 61  /*.** If this va
2220: 72 69 61 62 6c 65 20 69 73 20 73 65 74 2c 20 64  riable is set, d
2230: 69 73 61 62 6c 65 20 6c 6f 67 69 6e 20 63 68 65  isable login che
2240: 63 6b 73 2e 20 20 55 73 65 64 20 66 6f 72 20 64  cks.  Used for d
2250: 65 62 75 67 67 69 6e 67 0a 2a 2a 20 6f 6e 6c 79  ebugging.** only
2260: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2270: 64 69 73 61 62 6c 65 4c 6f 67 69 6e 20 3d 20 30  disableLogin = 0
2280: 3b 0a 0a 2f 2a 0a 2a 2a 20 57 45 42 50 41 47 45  ;../*.** WEBPAGE
2290: 3a 20 78 66 65 72 0a 2a 2a 0a 2a 2a 20 54 68 69  : xfer.**.** Thi
22a0: 73 20 69 73 20 74 68 65 20 74 72 61 6e 73 66 65  s is the transfe
22b0: 72 20 68 61 6e 64 6c 65 72 20 6f 6e 20 74 68 65  r handler on the
22c0: 20 73 65 72 76 65 72 20 73 69 64 65 2e 20 20 54   server side.  T
22d0: 68 65 20 74 72 61 6e 73 66 65 72 0a 2a 2a 20 6d  he transfer.** m
22e0: 65 73 73 61 67 65 20 68 61 73 20 62 65 65 6e 20  essage has been 
22f0: 75 6e 63 6f 6d 70 72 65 73 73 65 64 20 61 6e 64  uncompressed and
2300: 20 70 6c 61 63 65 64 20 69 6e 20 74 68 65 20 67   placed in the g
2310: 2e 63 67 69 49 6e 20 62 6c 6f 62 2e 0a 2a 2a 20  .cgiIn blob..** 
2320: 50 72 6f 63 65 73 73 20 74 68 69 73 20 6d 65 73  Process this mes
2330: 73 61 67 65 20 61 6e 64 20 66 6f 72 6d 20 61 6e  sage and form an
2340: 20 61 70 70 72 6f 70 72 69 61 74 65 20 72 65 70   appropriate rep
2350: 6c 79 2e 0a 2a 2f 0a 76 6f 69 64 20 70 61 67 65  ly..*/.void page
2360: 5f 78 66 65 72 28 76 6f 69 64 29 7b 0a 20 20 69  _xfer(void){.  i
2370: 6e 74 20 6e 54 6f 6b 65 6e 3b 0a 20 20 69 6e 74  nt nToken;.  int
2380: 20 69 73 50 75 6c 6c 20 3d 20 30 3b 0a 20 20 69   isPull = 0;.  i
2390: 6e 74 20 69 73 50 75 73 68 20 3d 20 30 3b 0a 20  nt isPush = 0;. 
23a0: 20 69 6e 74 20 6e 45 72 72 20 3d 20 30 3b 0a 20   int nErr = 0;. 
23b0: 20 42 6c 6f 62 20 6c 69 6e 65 2c 20 65 72 72 6d   Blob line, errm
23c0: 73 67 2c 20 61 54 6f 6b 65 6e 5b 35 5d 3b 0a 0a  sg, aToken[5];..
23d0: 20 20 64 62 5f 62 65 67 69 6e 5f 74 72 61 6e 73    db_begin_trans
23e0: 61 63 74 69 6f 6e 28 29 3b 0a 20 20 62 6c 6f 62  action();.  blob
23f0: 61 72 72 61 79 5f 7a 65 72 6f 28 61 54 6f 6b 65  array_zero(aToke
2400: 6e 2c 20 63 6f 75 6e 74 28 61 54 6f 6b 65 6e 29  n, count(aToken)
2410: 29 3b 0a 20 20 63 67 69 5f 73 65 74 5f 63 6f 6e  );.  cgi_set_con
2420: 74 65 6e 74 5f 74 79 70 65 28 67 2e 7a 43 6f 6e  tent_type(g.zCon
2430: 74 65 6e 74 54 79 70 65 29 3b 0a 20 20 62 6c 6f  tentType);.  blo
2440: 62 5f 7a 65 72 6f 28 26 65 72 72 6d 73 67 29 3b  b_zero(&errmsg);
2450: 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63  .  db_multi_exec
2460: 28 0a 20 20 20 20 20 22 43 52 45 41 54 45 20 54  (.     "CREATE T
2470: 45 4d 50 20 54 41 42 4c 45 20 6f 6e 72 65 6d 6f  EMP TABLE onremo
2480: 74 65 28 72 69 64 20 49 4e 54 45 47 45 52 20 50  te(rid INTEGER P
2490: 52 49 4d 41 52 59 20 4b 45 59 29 3b 22 20 2f 2a  RIMARY KEY);" /*
24a0: 20 43 6c 69 65 6e 74 20 68 61 73 20 2a 2f 0a 20   Client has */. 
24b0: 20 20 20 20 22 43 52 45 41 54 45 20 54 45 4d 50      "CREATE TEMP
24c0: 20 54 41 42 4c 45 20 70 65 6e 64 69 6e 67 28 72   TABLE pending(r
24d0: 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  id INTEGER PRIMA
24e0: 52 59 20 4b 45 59 29 3b 22 20 20 2f 2a 20 43 6c  RY KEY);"  /* Cl
24f0: 69 65 6e 74 20 6e 65 65 64 73 20 2a 2f 0a 20 20  ient needs */.  
2500: 29 3b 0a 20 20 77 68 69 6c 65 28 20 62 6c 6f 62  );.  while( blob
2510: 5f 6c 69 6e 65 28 26 67 2e 63 67 69 49 6e 2c 20  _line(&g.cgiIn, 
2520: 26 6c 69 6e 65 29 20 29 7b 0a 20 20 20 20 6e 54  &line) ){.    nT
2530: 6f 6b 65 6e 20 3d 20 62 6c 6f 62 5f 74 6f 6b 65  oken = blob_toke
2540: 6e 69 7a 65 28 26 6c 69 6e 65 2c 20 61 54 6f 6b  nize(&line, aTok
2550: 65 6e 2c 20 63 6f 75 6e 74 28 61 54 6f 6b 65 6e  en, count(aToken
2560: 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 20 20 66 69  ));..    /*   fi
2570: 6c 65 20 55 55 49 44 20 53 49 5a 45 20 5c 6e 20  le UUID SIZE \n 
2580: 43 4f 4e 54 45 4e 54 0a 20 20 20 20 2a 2a 20 20  CONTENT.    **  
2590: 20 66 69 6c 65 20 55 55 49 44 20 44 45 4c 54 41   file UUID DELTA
25a0: 53 52 43 20 53 49 5a 45 20 5c 6e 20 43 4f 4e 54  SRC SIZE \n CONT
25b0: 45 4e 54 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ENT.    **.    *
25c0: 2a 20 41 63 63 65 70 74 20 61 20 66 69 6c 65 20  * Accept a file 
25d0: 66 72 6f 6d 20 74 68 65 20 63 6c 69 65 6e 74 2e  from the client.
25e0: 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
25f0: 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b  blob_eq(&aToken[
2600: 30 5d 2c 20 22 66 69 6c 65 22 29 20 26 26 20 6e  0], "file") && n
2610: 54 6f 6b 65 6e 3e 3d 33 20 26 26 20 6e 54 6f 6b  Token>=3 && nTok
2620: 65 6e 3c 3d 34 20 29 7b 0a 20 20 20 20 20 20 69  en<=4 ){.      i
2630: 66 28 20 21 69 73 50 75 73 68 20 29 7b 0a 20 20  f( !isPush ){.  
2640: 20 20 20 20 20 20 63 67 69 5f 72 65 73 65 74 5f        cgi_reset_
2650: 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 20 20 20  content();.     
2660: 20 20 20 40 20 65 72 72 6f 72 20 6e 6f 74 5c 73     @ error not\s
2670: 61 75 74 68 6f 72 69 7a 65 64 5c 73 74 6f 5c 73  authorized\sto\s
2680: 77 72 69 74 65 0a 20 20 20 20 20 20 20 20 6e 45  write.        nE
2690: 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 62 72  rr++;.        br
26a0: 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
26b0: 20 20 20 78 66 65 72 5f 61 63 63 65 70 74 5f 66     xfer_accept_f
26c0: 69 6c 65 28 26 67 2e 63 67 69 49 6e 2c 20 61 54  ile(&g.cgiIn, aT
26d0: 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 2c 20 26 65  oken, nToken, &e
26e0: 72 72 6d 73 67 29 3b 0a 20 20 20 20 20 20 69 66  rrmsg);.      if
26f0: 28 20 62 6c 6f 62 5f 73 69 7a 65 28 26 65 72 72  ( blob_size(&err
2700: 6d 73 67 29 20 29 7b 0a 20 20 20 20 20 20 20 20  msg) ){.        
2710: 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e 74 65 6e  cgi_reset_conten
2720: 74 28 29 3b 0a 20 20 20 20 20 20 20 20 40 20 65  t();.        @ e
2730: 72 72 6f 72 20 25 54 28 62 6c 6f 62 5f 73 74 72  rror %T(blob_str
2740: 28 26 65 72 72 6d 73 67 29 29 0a 20 20 20 20 20  (&errmsg)).     
2750: 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20     nErr++;.     
2760: 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2770: 7d 0a 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20  }.    }else..   
2780: 20 2f 2a 20 20 20 67 69 6d 6d 65 20 55 55 49 44   /*   gimme UUID
2790: 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43  .    **.    ** C
27a0: 6c 69 65 6e 74 20 69 73 20 72 65 71 75 65 73 74  lient is request
27b0: 69 6e 67 20 61 20 66 69 6c 65 0a 20 20 20 20 2a  ing a file.    *
27c0: 2f 0a 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65  /.    if( blob_e
27d0: 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 67  q(&aToken[0], "g
27e0: 69 6d 6d 65 22 29 20 26 26 20 6e 54 6f 6b 65 6e  imme") && nToken
27f0: 3d 3d 32 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75  ==2 && blob_is_u
2800: 75 69 64 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 20  uid(&aToken[1]) 
2810: 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73 50  ){.      if( isP
2820: 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 64  ull ){.        d
2830: 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20  b_multi_exec(.  
2840: 20 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 20          "INSERT 
2850: 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 70  OR IGNORE INTO p
2860: 65 6e 64 69 6e 67 28 72 69 64 29 20 22 0a 20 20  ending(rid) ".  
2870: 20 20 20 20 20 20 20 20 22 53 45 4c 45 43 54 20          "SELECT 
2880: 72 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48  rid FROM blob WH
2890: 45 52 45 20 75 75 69 64 3d 25 42 20 41 4e 44 20  ERE uuid=%B AND 
28a0: 73 69 7a 65 3e 3d 30 22 2c 20 26 61 54 6f 6b 65  size>=0", &aToke
28b0: 6e 5b 31 5d 0a 20 20 20 20 20 20 20 20 29 3b 0a  n[1].        );.
28c0: 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
28d0: 65 0a 0a 20 20 20 20 2f 2a 20 20 20 69 67 6f 74  e..    /*   igot
28e0: 20 55 55 49 44 0a 20 20 20 20 2a 2a 20 20 20 6c   UUID.    **   l
28f0: 65 61 66 20 55 55 49 44 0a 20 20 20 20 2a 2a 0a  eaf UUID.    **.
2900: 20 20 20 20 2a 2a 20 43 6c 69 65 6e 74 20 61 6e      ** Client an
2910: 6e 6f 75 6e 63 65 73 20 74 68 61 74 20 69 74 20  nounces that it 
2920: 68 61 73 20 61 20 70 61 72 74 69 63 75 6c 61 72  has a particular
2930: 20 66 69 6c 65 0a 20 20 20 20 2a 2f 0a 20 20 20   file.    */.   
2940: 20 69 66 28 20 6e 54 6f 6b 65 6e 3d 3d 32 0a 20   if( nToken==2. 
2950: 20 20 20 20 20 20 20 20 20 26 26 20 28 62 6c 6f           && (blo
2960: 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c  b_eq(&aToken[0],
2970: 20 22 69 67 6f 74 22 29 20 7c 7c 20 62 6c 6f 62   "igot") || blob
2980: 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 22  _eq(&aToken[0],"
2990: 6c 65 61 66 22 29 29 0a 20 20 20 20 20 20 20 20  leaf")).        
29a0: 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69    && blob_is_uui
29b0: 64 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 20 29 7b  d(&aToken[1]) ){
29c0: 0a 20 20 20 20 20 20 69 66 28 20 69 73 50 75 6c  .      if( isPul
29d0: 6c 20 7c 7c 20 69 73 50 75 73 68 20 29 7b 0a 20  l || isPush ){. 
29e0: 20 20 20 20 20 20 20 69 6e 74 20 72 69 64 20 3d         int rid =
29f0: 20 64 62 5f 69 6e 74 28 30 2c 20 22 53 45 4c 45   db_int(0, "SELE
2a00: 43 54 20 72 69 64 20 46 52 4f 4d 20 62 6c 6f 62  CT rid FROM blob
2a10: 20 57 48 45 52 45 20 75 75 69 64 3d 25 42 22 2c   WHERE uuid=%B",
2a20: 20 26 61 54 6f 6b 65 6e 5b 31 5d 29 3b 0a 20 20   &aToken[1]);.  
2a30: 20 20 20 20 20 20 69 66 28 20 72 69 64 3e 30 20        if( rid>0 
2a40: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 5f  ){.          db_
2a50: 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 20  multi_exec(.    
2a60: 20 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 20          "INSERT 
2a70: 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 6f  OR IGNORE INTO o
2a80: 6e 72 65 6d 6f 74 65 28 72 69 64 29 20 56 41 4c  nremote(rid) VAL
2a90: 55 45 53 28 25 64 29 22 2c 20 72 69 64 0a 20 20  UES(%d)", rid.  
2aa0: 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20          );.     
2ab0: 20 20 20 20 20 69 66 28 20 69 73 50 75 6c 6c 20       if( isPull 
2ac0: 26 26 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b  && blob_eq(&aTok
2ad0: 65 6e 5b 30 5d 2c 20 22 6c 65 61 66 22 29 20 29  en[0], "leaf") )
2ae0: 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 62  {.            db
2af0: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20  _multi_exec(.   
2b00: 20 20 20 20 20 20 20 20 20 20 20 22 49 4e 53 45             "INSE
2b10: 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54  RT OR IGNORE INT
2b20: 4f 20 70 65 6e 64 69 6e 67 28 72 69 64 29 20 22  O pending(rid) "
2b30: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22  .              "
2b40: 53 45 4c 45 43 54 20 63 69 64 20 46 52 4f 4d 20  SELECT cid FROM 
2b50: 70 6c 69 6e 6b 20 57 48 45 52 45 20 70 69 64 3d  plink WHERE pid=
2b60: 25 64 22 2c 20 72 69 64 0a 20 20 20 20 20 20 20  %d", rid.       
2b70: 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20       );.        
2b80: 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73    }.        }els
2b90: 65 20 69 66 28 20 69 73 50 75 73 68 20 29 7b 0a  e if( isPush ){.
2ba0: 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 65 6e            conten
2bb0: 74 5f 70 75 74 28 30 2c 20 62 6c 6f 62 5f 73 74  t_put(0, blob_st
2bc0: 72 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 29 3b 0a  r(&aToken[1]));.
2bd0: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2be0: 7d 0a 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20  }.    }else..   
2bf0: 20 2f 2a 20 20 20 20 70 75 6c 6c 20 20 53 45 52   /*    pull  SER
2c00: 56 45 52 43 4f 44 45 20 20 50 52 4f 4a 45 43 54  VERCODE  PROJECT
2c10: 43 4f 44 45 0a 20 20 20 20 2a 2a 20 20 20 20 70  CODE.    **    p
2c20: 75 73 68 20 20 53 45 52 56 45 52 43 4f 44 45 20  ush  SERVERCODE 
2c30: 20 50 52 4f 4a 45 43 54 43 4f 44 45 0a 20 20 20   PROJECTCODE.   
2c40: 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 63   **.    ** The c
2c50: 6c 69 65 6e 74 20 77 61 6e 74 73 20 65 69 74 68  lient wants eith
2c60: 65 72 20 73 65 6e 64 20 6f 72 20 72 65 63 65 69  er send or recei
2c70: 76 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  ve.    */.    if
2c80: 28 20 6e 54 6f 6b 65 6e 3d 3d 33 0a 20 20 20 20  ( nToken==3.    
2c90: 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 62             && (b
2ca0: 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30  lob_eq(&aToken[0
2cb0: 5d 2c 20 22 70 75 6c 6c 22 29 20 7c 7c 20 62 6c  ], "pull") || bl
2cc0: 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d  ob_eq(&aToken[0]
2cd0: 2c 20 22 70 75 73 68 22 29 29 0a 20 20 20 20 20  , "push")).     
2ce0: 20 20 20 20 20 20 20 20 20 20 26 26 20 62 6c 6f            && blo
2cf0: 62 5f 69 73 5f 75 75 69 64 28 26 61 54 6f 6b 65  b_is_uuid(&aToke
2d00: 6e 5b 31 5d 29 20 26 26 20 62 6c 6f 62 5f 69 73  n[1]) && blob_is
2d10: 5f 75 75 69 64 28 26 61 54 6f 6b 65 6e 5b 32 5d  _uuid(&aToken[2]
2d20: 29 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74  ) ){.      const
2d30: 20 63 68 61 72 20 2a 7a 53 43 6f 64 65 3b 0a 20   char *zSCode;. 
2d40: 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
2d50: 2a 7a 50 43 6f 64 65 3b 0a 0a 20 20 20 20 20 20  *zPCode;..      
2d60: 7a 53 43 6f 64 65 20 3d 20 64 62 5f 67 65 74 28  zSCode = db_get(
2d70: 22 73 65 72 76 65 72 2d 63 6f 64 65 22 2c 20 30  "server-code", 0
2d80: 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 53 43  );.      if( zSC
2d90: 6f 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ode==0 ){.      
2da0: 20 20 66 6f 73 73 69 6c 5f 70 61 6e 69 63 28 22    fossil_panic("
2db0: 6d 69 73 73 69 6e 67 20 73 65 72 76 65 72 20 63  missing server c
2dc0: 6f 64 65 22 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ode");.      }. 
2dd0: 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71       if( blob_eq
2de0: 5f 73 74 72 28 26 61 54 6f 6b 65 6e 5b 31 5d 2c  _str(&aToken[1],
2df0: 20 7a 53 43 6f 64 65 2c 20 2d 31 29 20 29 7b 0a   zSCode, -1) ){.
2e00: 20 20 20 20 20 20 20 20 63 67 69 5f 72 65 73 65          cgi_rese
2e10: 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 20  t_content();.   
2e20: 20 20 20 20 20 40 20 65 72 72 6f 72 20 73 65 72       @ error ser
2e30: 76 65 72 5c 73 6c 6f 6f 70 0a 20 20 20 20 20 20  ver\sloop.      
2e40: 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20    nErr++;.      
2e50: 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
2e60: 0a 20 20 20 20 20 20 7a 50 43 6f 64 65 20 3d 20  .      zPCode = 
2e70: 64 62 5f 67 65 74 28 22 70 72 6f 6a 65 63 74 2d  db_get("project-
2e80: 63 6f 64 65 22 2c 20 30 29 3b 0a 20 20 20 20 20  code", 0);.     
2e90: 20 69 66 28 20 7a 50 43 6f 64 65 3d 3d 30 20 29   if( zPCode==0 )
2ea0: 7b 0a 20 20 20 20 20 20 20 20 66 6f 73 73 69 6c  {.        fossil
2eb0: 5f 70 61 6e 69 63 28 22 6d 69 73 73 69 6e 67 20  _panic("missing 
2ec0: 70 72 6f 6a 65 63 74 20 63 6f 64 65 22 29 3b 0a  project code");.
2ed0: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
2ee0: 28 20 21 62 6c 6f 62 5f 65 71 5f 73 74 72 28 26  ( !blob_eq_str(&
2ef0: 61 54 6f 6b 65 6e 5b 32 5d 2c 20 7a 50 43 6f 64  aToken[2], zPCod
2f00: 65 2c 20 2d 31 29 20 29 7b 0a 20 20 20 20 20 20  e, -1) ){.      
2f10: 20 20 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e 74    cgi_reset_cont
2f20: 65 6e 74 28 29 3b 0a 20 20 20 20 20 20 20 20 40  ent();.        @
2f30: 20 65 72 72 6f 72 20 77 72 6f 6e 67 5c 73 70 72   error wrong\spr
2f40: 6f 6a 65 63 74 0a 20 20 20 20 20 20 20 20 6e 45  oject.        nE
2f50: 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 62 72  rr++;.        br
2f60: 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
2f70: 20 20 20 6c 6f 67 69 6e 5f 63 68 65 63 6b 5f 63     login_check_c
2f80: 72 65 64 65 6e 74 69 61 6c 73 28 29 3b 0a 20 20  redentials();.  
2f90: 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28      if( blob_eq(
2fa0: 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 70 75 6c  &aToken[0], "pul
2fb0: 6c 22 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69  l") ){.        i
2fc0: 66 28 20 21 67 2e 6f 6b 52 65 61 64 20 29 7b 0a  f( !g.okRead ){.
2fd0: 20 20 20 20 20 20 20 20 20 20 63 67 69 5f 72 65            cgi_re
2fe0: 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a 20  set_content();. 
2ff0: 20 20 20 20 20 20 20 20 20 40 20 65 72 72 6f 72           @ error
3000: 20 6e 6f 74 5c 73 61 75 74 68 6f 72 69 7a 65 64   not\sauthorized
3010: 5c 73 74 6f 5c 73 72 65 61 64 0a 20 20 20 20 20  \sto\sread.     
3020: 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20       nErr++;.   
3030: 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3040: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
3050: 69 73 50 75 6c 6c 20 3d 20 31 3b 0a 20 20 20 20  isPull = 1;.    
3060: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
3070: 20 69 66 28 20 21 67 2e 6f 6b 57 72 69 74 65 20   if( !g.okWrite 
3080: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 67 69  ){.          cgi
3090: 5f 72 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29  _reset_content()
30a0: 3b 0a 20 20 20 20 20 20 20 20 20 20 40 20 65 72  ;.          @ er
30b0: 72 6f 72 20 6e 6f 74 5c 73 61 75 74 68 6f 72 69  ror not\sauthori
30c0: 7a 65 64 5c 73 74 6f 5c 73 77 72 69 74 65 0a 20  zed\sto\swrite. 
30d0: 20 20 20 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b           nErr++;
30e0: 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
30f0: 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
3100: 20 20 20 20 69 73 50 75 73 68 20 3d 20 31 3b 0a      isPush = 1;.
3110: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
3120: 73 65 0a 0a 20 20 20 20 2f 2a 20 20 20 20 63 6c  se..    /*    cl
3130: 6f 6e 65 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  one.    **.    *
3140: 2a 20 54 68 65 20 63 6c 69 65 6e 74 20 6b 6e 6f  * The client kno
3150: 77 73 20 6e 6f 74 68 69 6e 67 2e 20 20 54 65 6c  ws nothing.  Tel
3160: 6c 20 61 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20  l all..    */.  
3170: 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 61    if( blob_eq(&a
3180: 54 6f 6b 65 6e 5b 30 5d 2c 20 22 63 6c 6f 6e 65  Token[0], "clone
3190: 22 29 20 29 7b 0a 20 20 20 20 20 20 6c 6f 67 69  ") ){.      logi
31a0: 6e 5f 63 68 65 63 6b 5f 63 72 65 64 65 6e 74 69  n_check_credenti
31b0: 61 6c 73 28 29 3b 0a 20 20 20 20 20 20 69 66 28  als();.      if(
31c0: 20 21 67 2e 6f 6b 52 65 61 64 20 29 7b 0a 20 20   !g.okRead ){.  
31d0: 20 20 20 20 20 20 63 67 69 5f 72 65 73 65 74 5f        cgi_reset_
31e0: 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 20 20 20  content();.     
31f0: 20 20 20 40 20 65 72 72 6f 72 20 6e 6f 74 5c 73     @ error not\s
3200: 61 75 74 68 6f 72 69 7a 65 64 5c 73 74 6f 5c 73  authorized\sto\s
3210: 72 65 61 64 0a 20 20 20 20 20 20 20 20 6e 45 72  read.        nEr
3220: 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 62 72 65  r++;.        bre
3230: 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
3240: 20 20 69 73 50 75 6c 6c 20 3d 20 31 3b 0a 20 20    isPull = 1;.  
3250: 20 20 20 20 40 20 70 75 73 68 20 25 73 28 64 62      @ push %s(db
3260: 5f 67 65 74 28 22 73 65 72 76 65 72 2d 63 6f 64  _get("server-cod
3270: 65 22 2c 20 22 78 22 29 29 20 25 73 28 64 62 5f  e", "x")) %s(db_
3280: 67 65 74 28 22 70 72 6f 6a 65 63 74 2d 63 6f 64  get("project-cod
3290: 65 22 2c 20 22 78 22 29 29 0a 20 20 20 20 20 20  e", "x")).      
32a0: 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20  db_multi_exec(. 
32b0: 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 20 4f         "INSERT O
32c0: 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 70 65  R IGNORE INTO pe
32d0: 6e 64 69 6e 67 28 72 69 64 29 20 22 0a 20 20 20  nding(rid) ".   
32e0: 20 20 20 20 20 22 53 45 4c 45 43 54 20 72 69 64       "SELECT rid
32f0: 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45   FROM blob WHERE
3300: 20 73 69 7a 65 3e 3d 30 22 0a 20 20 20 20 20 20   size>=0".      
3310: 29 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20  );.    }else..  
3320: 20 20 2f 2a 20 20 20 20 6c 6f 67 69 6e 20 20 55    /*    login  U
3330: 53 45 52 20 20 4e 4f 4e 43 45 20 20 53 49 47 4e  SER  NONCE  SIGN
3340: 41 54 55 52 45 0a 20 20 20 20 2a 2a 0a 20 20 20  ATURE.    **.   
3350: 20 2a 2a 20 43 68 65 63 6b 20 66 6f 72 20 61 20   ** Check for a 
3360: 76 61 6c 69 64 20 6c 6f 67 69 6e 2e 20 20 54 68  valid login.  Th
3370: 69 73 20 68 61 73 20 74 6f 20 68 61 70 70 65 6e  is has to happen
3380: 20 62 65 66 6f 72 65 20 61 6e 79 74 68 69 6e 67   before anything
3390: 20 65 6c 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20   else..    */.  
33a0: 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 61    if( blob_eq(&a
33b0: 54 6f 6b 65 6e 5b 30 5d 2c 20 22 6c 6f 67 69 6e  Token[0], "login
33c0: 22 29 20 26 26 20 6e 54 6f 6b 65 6e 3d 3d 34 20  ") && nToken==4 
33d0: 29 7b 0a 20 20 20 20 20 20 69 66 28 20 64 69 73  ){.      if( dis
33e0: 61 62 6c 65 4c 6f 67 69 6e 20 29 7b 0a 20 20 20  ableLogin ){.   
33f0: 20 20 20 20 20 67 2e 6f 6b 52 65 61 64 20 3d 20       g.okRead = 
3400: 67 2e 6f 6b 57 72 69 74 65 20 3d 20 31 3b 0a 20  g.okWrite = 1;. 
3410: 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
3420: 20 20 20 20 63 68 65 63 6b 5f 6c 6f 67 69 6e 28      check_login(
3430: 26 61 54 6f 6b 65 6e 5b 31 5d 2c 20 26 61 54 6f  &aToken[1], &aTo
3440: 6b 65 6e 5b 32 5d 2c 20 26 61 54 6f 6b 65 6e 5b  ken[2], &aToken[
3450: 33 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  3]);.      }.   
3460: 20 7d 65 6c 73 65 0a 0a 20 20 20 20 2f 2a 20 55   }else..    /* U
3470: 6e 6b 6e 6f 77 6e 20 6d 65 73 73 61 67 65 0a 20  nknown message. 
3480: 20 20 20 2a 2f 0a 20 20 20 20 7b 0a 20 20 20 20     */.    {.    
3490: 20 20 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e 74    cgi_reset_cont
34a0: 65 6e 74 28 29 3b 0a 20 20 20 20 20 20 40 20 65  ent();.      @ e
34b0: 72 72 6f 72 20 62 61 64 5c 73 63 6f 6d 6d 61 6e  rror bad\scomman
34c0: 64 3a 5c 73 25 46 28 62 6c 6f 62 5f 73 74 72 28  d:\s%F(blob_str(
34d0: 26 6c 69 6e 65 29 29 0a 20 20 20 20 7d 0a 20 20  &line)).    }.  
34e0: 20 20 62 6c 6f 62 61 72 72 61 79 5f 72 65 73 65    blobarray_rese
34f0: 74 28 61 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e  t(aToken, nToken
3500: 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65  );.  }..  /* The
3510: 20 69 6e 70 75 74 20 6d 65 73 73 61 67 65 20 68   input message h
3520: 61 73 20 6e 6f 77 20 62 65 65 6e 20 70 72 6f 63  as now been proc
3530: 65 73 73 65 64 2e 20 20 47 65 6e 65 72 61 74 65  essed.  Generate
3540: 20 61 20 72 65 70 6c 79 2e 20 2a 2f 0a 20 20 69   a reply. */.  i
3550: 66 28 20 69 73 50 75 73 68 20 29 7b 0a 20 20 20  f( isPush ){.   
3560: 20 53 74 6d 74 20 71 3b 0a 20 20 20 20 64 62 5f   Stmt q;.    db_
3570: 70 72 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c  prepare(&q, "SEL
3580: 45 43 54 20 75 75 69 64 20 46 52 4f 4d 20 62 6c  ECT uuid FROM bl
3590: 6f 62 20 57 48 45 52 45 20 73 69 7a 65 3c 30 22  ob WHERE size<0"
35a0: 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 64 62  );.    while( db
35b0: 5f 73 74 65 70 28 26 71 29 3d 3d 53 51 4c 49 54  _step(&q)==SQLIT
35c0: 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 63  E_ROW ){.      c
35d0: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 55 75 69 64  onst char *zUuid
35e0: 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78   = db_column_tex
35f0: 74 28 26 71 2c 20 30 29 3b 0a 20 20 20 20 20 20  t(&q, 0);.      
3600: 40 20 67 69 6d 6d 65 20 25 73 28 7a 55 75 69 64  @ gimme %s(zUuid
3610: 29 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 5f 66  ).    }.    db_f
3620: 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 20 20 7d  inalize(&q);.  }
3630: 0a 20 20 69 66 28 20 69 73 50 75 6c 6c 20 29 7b  .  if( isPull ){
3640: 0a 20 20 20 20 73 65 6e 64 5f 61 6c 6c 5f 70 65  .    send_all_pe
3650: 6e 64 69 6e 67 28 30 29 3b 0a 20 20 7d 0a 20 20  nding(0);.  }.  
3660: 69 66 28 20 69 73 50 75 73 68 20 7c 7c 20 69 73  if( isPush || is
3670: 50 75 6c 6c 20 29 7b 0a 20 20 20 20 2f 2a 20 41  Pull ){.    /* A
3680: 6c 77 61 79 73 20 73 65 6e 64 20 6f 75 72 20 6c  lways send our l
3690: 65 61 76 65 73 20 2a 2f 0a 20 20 20 20 53 74 6d  eaves */.    Stm
36a0: 74 20 71 3b 0a 20 20 20 20 64 62 5f 70 72 65 70  t q;.    db_prep
36b0: 61 72 65 28 26 71 2c 20 0a 20 20 20 20 20 20 20  are(&q, .       
36c0: 22 53 45 4c 45 43 54 20 75 75 69 64 20 46 52 4f  "SELECT uuid FRO
36d0: 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 72 69 64  M blob WHERE rid
36e0: 20 49 4e 22 0a 20 20 20 20 20 20 20 22 20 20 28   IN".       "  (
36f0: 53 45 4c 45 43 54 20 63 69 64 20 46 52 4f 4d 20  SELECT cid FROM 
3700: 70 6c 69 6e 6b 20 45 58 43 45 50 54 20 53 45 4c  plink EXCEPT SEL
3710: 45 43 54 20 70 69 64 20 46 52 4f 4d 20 70 6c 69  ECT pid FROM pli
3720: 6e 6b 29 22 0a 20 20 20 20 29 3b 0a 20 20 20 20  nk)".    );.    
3730: 77 68 69 6c 65 28 20 64 62 5f 73 74 65 70 28 26  while( db_step(&
3740: 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  q)==SQLITE_ROW )
3750: 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  {.      const ch
3760: 61 72 20 2a 7a 55 75 69 64 20 3d 20 64 62 5f 63  ar *zUuid = db_c
3770: 6f 6c 75 6d 6e 5f 74 65 78 74 28 26 71 2c 20 30  olumn_text(&q, 0
3780: 29 3b 0a 20 20 20 20 20 20 40 20 6c 65 61 66 20  );.      @ leaf 
3790: 25 73 28 7a 55 75 69 64 29 0a 20 20 20 20 7d 0a  %s(zUuid).    }.
37a0: 20 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28      db_finalize(
37b0: 26 71 29 3b 0a 20 20 7d 0a 0a 20 20 64 62 5f 65  &q);.  }..  db_e
37c0: 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 30  nd_transaction(0
37d0: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d  );.}../*.** COMM
37e0: 41 4e 44 3a 20 74 65 73 74 2d 78 66 65 72 0a 2a  AND: test-xfer.*
37f0: 2a 0a 2a 2a 20 54 68 69 73 20 63 6f 6d 6d 61 6e  *.** This comman
3800: 64 20 69 73 20 75 73 65 64 20 66 6f 72 20 64 65  d is used for de
3810: 62 75 67 67 69 6e 67 20 74 68 65 20 73 65 72 76  bugging the serv
3820: 65 72 2e 20 20 54 68 65 72 65 20 69 73 20 61 20  er.  There is a 
3830: 73 69 6e 67 6c 65 0a 2a 2a 20 61 72 67 75 6d 65  single.** argume
3840: 6e 74 20 77 68 69 63 68 20 69 73 20 74 68 65 20  nt which is the 
3850: 75 6e 63 6f 6d 70 72 65 73 73 65 64 20 63 6f 6e  uncompressed con
3860: 74 65 6e 74 20 6f 66 20 61 6e 20 22 78 66 65 72  tent of an "xfer
3870: 22 20 6d 65 73 73 61 67 65 0a 2a 2a 20 66 72 6f  " message.** fro
3880: 6d 20 63 6c 69 65 6e 74 20 74 6f 20 73 65 72 76  m client to serv
3890: 65 72 2e 20 20 54 68 69 73 20 63 6f 6d 6d 61 6e  er.  This comman
38a0: 64 20 69 6e 74 65 72 70 72 65 74 73 20 74 68 61  d interprets tha
38b0: 74 20 6d 65 73 73 61 67 65 20 61 73 0a 2a 2a 20  t message as.** 
38c0: 69 66 20 68 61 64 20 62 65 65 6e 20 72 65 63 65  if had been rece
38d0: 69 76 65 64 20 62 79 20 74 68 65 20 73 65 72 76  ived by the serv
38e0: 65 72 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 74 68 65  er..**.** On the
38f0: 20 63 6c 69 65 6e 74 20 73 69 64 65 2c 20 72 75   client side, ru
3900: 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 66 6f  n:.**.**      fo
3910: 73 73 69 6c 20 70 75 73 68 20 68 74 74 70 3a 2f  ssil push http:/
3920: 2f 62 6f 67 75 73 2f 20 2d 2d 68 74 74 70 74 72  /bogus/ --httptr
3930: 61 63 65 0a 2a 2a 0a 2a 2a 20 4f 72 20 61 20 73  ace.**.** Or a s
3940: 69 6d 69 6c 61 72 20 63 6f 6d 6d 61 6e 64 20 74  imilar command t
3950: 6f 20 70 72 6f 76 69 64 65 20 74 68 65 20 6f 75  o provide the ou
3960: 74 70 75 74 2e 20 20 54 68 65 20 63 6f 6e 74 65  tput.  The conte
3970: 6e 74 20 6f 66 20 74 68 65 0a 2a 2a 20 6d 65 73  nt of the.** mes
3980: 73 61 67 65 20 77 69 6c 6c 20 61 70 70 65 61 72  sage will appear
3990: 20 6f 6e 20 73 74 61 6e 64 61 72 64 20 6f 75 74   on standard out
39a0: 70 75 74 2e 20 20 43 61 70 74 75 72 65 20 74 68  put.  Capture th
39b0: 69 73 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 6e  is message.** in
39c0: 74 6f 20 61 20 66 69 6c 65 20 6e 61 6d 65 64 20  to a file named 
39d0: 28 66 6f 72 20 65 78 61 6d 70 6c 65 29 20 6f 75  (for example) ou
39e0: 74 2e 74 78 74 2e 20 20 54 68 65 6e 20 72 75 6e  t.txt.  Then run
39f0: 20 74 68 65 0a 2a 2a 20 73 65 72 76 65 72 20 69   the.** server i
3a00: 6e 20 67 64 62 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  n gdb:.**.**    
3a10: 20 67 64 62 20 66 6f 73 73 69 6c 0a 2a 2a 20 20   gdb fossil.**  
3a20: 20 20 20 72 20 74 65 73 74 2d 78 66 65 72 20 6f     r test-xfer o
3a30: 75 74 2e 74 78 74 0a 2a 2f 0a 76 6f 69 64 20 63  ut.txt.*/.void c
3a40: 6d 64 5f 74 65 73 74 5f 78 66 65 72 28 76 6f 69  md_test_xfer(voi
3a50: 64 29 7b 0a 20 20 69 6e 74 20 6e 6f 74 55 73 65  d){.  int notUse
3a60: 64 3b 0a 20 20 69 66 28 20 67 2e 61 72 67 63 21  d;.  if( g.argc!
3a70: 3d 32 20 26 26 20 67 2e 61 72 67 63 21 3d 33 20  =2 && g.argc!=3 
3a80: 29 7b 0a 20 20 20 20 75 73 61 67 65 28 22 3f 4d  ){.    usage("?M
3a90: 45 53 53 41 47 45 46 49 4c 45 3f 22 29 3b 0a 20  ESSAGEFILE?");. 
3aa0: 20 7d 0a 20 20 64 62 5f 6d 75 73 74 5f 62 65 5f   }.  db_must_be_
3ab0: 77 69 74 68 69 6e 5f 74 72 65 65 28 29 3b 0a 20  within_tree();. 
3ac0: 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 67 2e 63 67   blob_zero(&g.cg
3ad0: 69 49 6e 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 61  iIn);.  blob_rea
3ae0: 64 5f 66 72 6f 6d 5f 66 69 6c 65 28 26 67 2e 63  d_from_file(&g.c
3af0: 67 69 49 6e 2c 20 67 2e 61 72 67 63 3d 3d 32 20  giIn, g.argc==2 
3b00: 3f 20 22 2d 22 20 3a 20 67 2e 61 72 67 76 5b 32  ? "-" : g.argv[2
3b10: 5d 29 3b 0a 20 20 64 69 73 61 62 6c 65 4c 6f 67  ]);.  disableLog
3b20: 69 6e 20 3d 20 31 3b 0a 20 20 70 61 67 65 5f 78  in = 1;.  page_x
3b30: 66 65 72 28 29 3b 0a 20 20 70 72 69 6e 74 66 28  fer();.  printf(
3b40: 22 25 73 5c 6e 22 2c 20 63 67 69 5f 65 78 74 72  "%s\n", cgi_extr
3b50: 61 63 74 5f 63 6f 6e 74 65 6e 74 28 26 6e 6f 74  act_content(&not
3b60: 55 73 65 64 29 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  Used));.}.../*.*
3b70: 2a 20 53 79 6e 63 20 74 6f 20 74 68 65 20 68 6f  * Sync to the ho
3b80: 73 74 20 69 64 65 6e 74 69 66 69 65 64 20 69 6e  st identified in
3b90: 20 67 2e 75 72 6c 4e 61 6d 65 20 61 6e 64 20 67   g.urlName and g
3ba0: 2e 75 72 6c 50 61 74 68 2e 20 20 54 68 69 73 0a  .urlPath.  This.
3bb0: 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  ** routine is ca
3bc0: 6c 6c 65 64 20 62 79 20 74 68 65 20 63 6c 69 65  lled by the clie
3bd0: 6e 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 63 6f 72 64  nt..**.** Record
3be0: 73 20 61 72 65 20 70 75 73 68 65 64 20 74 6f 20  s are pushed to 
3bf0: 74 68 65 20 73 65 72 76 65 72 20 69 66 20 70 75  the server if pu
3c00: 73 68 46 6c 61 67 20 69 73 20 74 72 75 65 2e 20  shFlag is true. 
3c10: 20 52 65 63 6f 72 64 73 0a 2a 2a 20 61 72 65 20   Records.** are 
3c20: 70 75 6c 6c 65 64 20 69 66 20 70 75 6c 6c 46 6c  pulled if pullFl
3c30: 61 67 20 69 73 20 74 72 75 65 2e 20 20 41 20 66  ag is true.  A f
3c40: 75 6c 6c 20 73 79 6e 63 20 6f 63 63 75 72 73 20  ull sync occurs 
3c50: 69 66 20 62 6f 74 68 20 61 72 65 0a 2a 2a 20 74  if both are.** t
3c60: 72 75 65 2e 0a 2a 2f 0a 76 6f 69 64 20 63 6c 69  rue..*/.void cli
3c70: 65 6e 74 5f 73 79 6e 63 28 69 6e 74 20 70 75 73  ent_sync(int pus
3c80: 68 46 6c 61 67 2c 20 69 6e 74 20 70 75 6c 6c 46  hFlag, int pullF
3c90: 6c 61 67 2c 20 69 6e 74 20 63 6c 6f 6e 65 46 6c  lag, int cloneFl
3ca0: 61 67 29 7b 0a 20 20 69 6e 74 20 67 6f 20 3d 20  ag){.  int go = 
3cb0: 31 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f  1;        /* Loo
3cc0: 70 20 75 6e 74 69 6c 20 7a 65 72 6f 20 2a 2f 0a  p until zero */.
3cd0: 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 0a 20 20    int nToken;.  
3ce0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 43 6f  const char *zSCo
3cf0: 64 65 20 3d 20 64 62 5f 67 65 74 28 22 73 65 72  de = db_get("ser
3d00: 76 65 72 2d 63 6f 64 65 22 2c 20 22 78 22 29 3b  ver-code", "x");
3d10: 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
3d20: 50 43 6f 64 65 20 3d 20 64 62 5f 67 65 74 28 22  PCode = db_get("
3d30: 70 72 6f 6a 65 63 74 2d 63 6f 64 65 22 2c 20 30  project-code", 0
3d40: 29 3b 0a 20 20 69 6e 74 20 6e 46 69 6c 65 20 3d  );.  int nFile =
3d50: 20 30 3b 0a 20 20 69 6e 74 20 6e 4d 73 67 20 3d   0;.  int nMsg =
3d60: 20 30 3b 0a 20 20 69 6e 74 20 6e 52 65 71 20 3d   0;.  int nReq =
3d70: 20 30 3b 0a 20 20 69 6e 74 20 6e 46 69 6c 65 53   0;.  int nFileS
3d80: 65 6e 64 3b 0a 20 20 69 6e 74 20 6e 4e 6f 46 69  end;.  int nNoFi
3d90: 6c 65 43 79 63 6c 65 20 3d 20 30 3b 0a 20 20 42  leCycle = 0;.  B
3da0: 6c 6f 62 20 73 65 6e 64 3b 20 20 20 20 20 20 20  lob send;       
3db0: 20 2f 2a 20 54 65 78 74 20 77 65 20 61 72 65 20   /* Text we are 
3dc0: 73 65 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 73  sending to the s
3dd0: 65 72 76 65 72 20 2a 2f 0a 20 20 42 6c 6f 62 20  erver */.  Blob 
3de0: 72 65 63 76 3b 20 20 20 20 20 20 20 20 2f 2a 20  recv;        /* 
3df0: 52 65 70 6c 79 20 77 65 20 67 6f 74 20 62 61 63  Reply we got bac
3e00: 6b 20 66 72 6f 6d 20 74 68 65 20 73 65 72 76 65  k from the serve
3e10: 72 20 2a 2f 0a 20 20 42 6c 6f 62 20 6c 69 6e 65  r */.  Blob line
3e20: 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69  ;        /* A si
3e30: 6e 67 6c 65 20 6c 69 6e 65 20 6f 66 20 74 68 65  ngle line of the
3e40: 20 72 65 70 6c 79 20 2a 2f 0a 20 20 42 6c 6f 62   reply */.  Blob
3e50: 20 61 54 6f 6b 65 6e 5b 35 5d 3b 20 20 20 2f 2a   aToken[5];   /*
3e60: 20 41 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20   A tokenization 
3e70: 6f 66 20 6c 69 6e 65 20 2a 2f 0a 20 20 42 6c 6f  of line */.  Blo
3e80: 62 20 65 72 72 6d 73 67 3b 20 20 20 20 20 20 2f  b errmsg;      /
3e90: 2a 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20  * Error message 
3ea0: 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 75  */..  assert( pu
3eb0: 73 68 46 6c 61 67 20 7c 7c 20 70 75 6c 6c 46 6c  shFlag || pullFl
3ec0: 61 67 20 7c 7c 20 63 6c 6f 6e 65 46 6c 61 67 20  ag || cloneFlag 
3ed0: 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 67 2e  );.  assert( !g.
3ee0: 75 72 6c 49 73 46 69 6c 65 20 29 3b 20 20 20 20  urlIsFile );    
3ef0: 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 6f 6e        /* This on
3f00: 6c 79 20 77 6f 72 6b 73 20 66 6f 72 20 6e 65 74  ly works for net
3f10: 77 6f 72 6b 69 6e 67 20 2a 2f 0a 0a 20 20 64 62  working */..  db
3f20: 5f 62 65 67 69 6e 5f 74 72 61 6e 73 61 63 74 69  _begin_transacti
3f30: 6f 6e 28 29 3b 0a 20 20 64 62 5f 6d 75 6c 74 69  on();.  db_multi
3f40: 5f 65 78 65 63 28 0a 20 20 20 20 2f 2a 20 52 65  _exec(.    /* Re
3f50: 63 6f 72 64 73 20 77 68 69 63 68 20 77 65 20 6b  cords which we k
3f60: 6e 6f 77 20 74 68 65 20 6f 74 68 65 72 20 73 69  now the other si
3f70: 64 65 20 61 6c 73 6f 20 68 61 73 20 2a 2f 0a 20  de also has */. 
3f80: 20 20 20 22 43 52 45 41 54 45 20 54 45 4d 50 20     "CREATE TEMP 
3f90: 54 41 42 4c 45 20 6f 6e 72 65 6d 6f 74 65 28 72  TABLE onremote(r
3fa0: 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  id INTEGER PRIMA
3fb0: 52 59 20 4b 45 59 29 3b 22 0a 20 20 20 20 2f 2a  RY KEY);".    /*
3fc0: 20 52 65 63 6f 72 64 73 20 77 65 20 6b 6e 6f 77   Records we know
3fd0: 20 74 68 65 20 6f 74 68 65 72 20 73 69 64 65 20   the other side 
3fe0: 6e 65 65 64 73 20 2a 2f 0a 20 20 20 20 22 43 52  needs */.    "CR
3ff0: 45 41 54 45 20 54 45 4d 50 20 54 41 42 4c 45 20  EATE TEMP TABLE 
4000: 70 65 6e 64 69 6e 67 28 72 69 64 20 49 4e 54 45  pending(rid INTE
4010: 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 29  GER PRIMARY KEY)
4020: 3b 22 0a 20 20 29 3b 0a 20 20 62 6c 6f 62 61 72  ;".  );.  blobar
4030: 72 61 79 5f 7a 65 72 6f 28 61 54 6f 6b 65 6e 2c  ray_zero(aToken,
4040: 20 63 6f 75 6e 74 28 61 54 6f 6b 65 6e 29 29 3b   count(aToken));
4050: 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 73 65  .  blob_zero(&se
4060: 6e 64 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f  nd);.  blob_zero
4070: 28 26 72 65 63 76 29 3b 0a 20 20 62 6c 6f 62 5f  (&recv);.  blob_
4080: 7a 65 72 6f 28 26 65 72 72 6d 73 67 29 3b 0a 0a  zero(&errmsg);..
4090: 0a 20 20 77 68 69 6c 65 28 20 67 6f 20 29 7b 0a  .  while( go ){.
40a0: 20 20 20 20 67 6f 20 3d 20 30 3b 0a 20 20 20 20      go = 0;.    
40b0: 6e 46 69 6c 65 20 3d 20 6e 52 65 71 20 3d 20 6e  nFile = nReq = n
40c0: 4d 73 67 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a  Msg = 0;..    /*
40d0: 20 47 65 6e 65 72 61 74 65 20 61 20 72 65 71 75   Generate a requ
40e0: 65 73 74 20 74 6f 20 62 65 20 73 65 6e 74 20 74  est to be sent t
40f0: 6f 20 74 68 65 20 73 65 72 76 65 72 2e 0a 20 20  o the server..  
4100: 20 20 2a 2a 20 41 6c 77 61 79 73 20 62 65 67 69    ** Always begi
4110: 6e 20 77 69 74 68 20 61 20 63 6c 6f 6e 65 2c 20  n with a clone, 
4120: 70 75 6c 6c 2c 20 6f 72 20 70 75 73 68 20 6d 65  pull, or push me
4130: 73 73 61 67 65 0a 20 20 20 20 2a 2f 0a 20 20 20  ssage.    */.   
4140: 20 0a 20 20 20 20 69 66 28 20 63 6c 6f 6e 65 46   .    if( cloneF
4150: 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 62 6c 6f  lag ){.      blo
4160: 62 5f 61 70 70 65 6e 64 66 28 26 73 65 6e 64 2c  b_appendf(&send,
4170: 20 22 63 6c 6f 6e 65 5c 6e 22 29 3b 0a 20 20 20   "clone\n");.   
4180: 20 20 20 70 75 73 68 46 6c 61 67 20 3d 20 30 3b     pushFlag = 0;
4190: 0a 20 20 20 20 20 20 70 75 6c 6c 46 6c 61 67 20  .      pullFlag 
41a0: 3d 20 30 3b 0a 20 20 20 20 20 20 6e 4d 73 67 2b  = 0;.      nMsg+
41b0: 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  +;.    }else if(
41c0: 20 70 75 6c 6c 46 6c 61 67 20 29 7b 0a 20 20 20   pullFlag ){.   
41d0: 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28     blob_appendf(
41e0: 26 73 65 6e 64 2c 20 22 70 75 6c 6c 20 25 73 20  &send, "pull %s 
41f0: 25 73 5c 6e 22 2c 20 7a 53 43 6f 64 65 2c 20 7a  %s\n", zSCode, z
4200: 50 43 6f 64 65 29 3b 0a 20 20 20 20 20 20 6e 4d  PCode);.      nM
4210: 73 67 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  sg++;.    }.    
4220: 69 66 28 20 70 75 73 68 46 6c 61 67 20 29 7b 0a  if( pushFlag ){.
4230: 20 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e        blob_appen
4240: 64 66 28 26 73 65 6e 64 2c 20 22 70 75 73 68 20  df(&send, "push 
4250: 25 73 20 25 73 5c 6e 22 2c 20 7a 53 43 6f 64 65  %s %s\n", zSCode
4260: 2c 20 7a 50 43 6f 64 65 29 3b 0a 20 20 20 20 20  , zPCode);.     
4270: 20 6e 4d 73 67 2b 2b 3b 0a 20 20 20 20 7d 0a 0a   nMsg++;.    }..
4280: 20 20 20 20 69 66 28 20 70 75 6c 6c 46 6c 61 67      if( pullFlag
4290: 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53 65 6e   ){.      /* Sen
42a0: 64 20 67 69 6d 6d 65 20 6d 65 73 73 61 67 65 20  d gimme message 
42b0: 66 6f 72 20 65 76 65 72 79 20 70 68 61 6e 74 6f  for every phanto
42c0: 6d 20 74 68 61 74 20 77 65 20 68 6f 6c 64 2e 0a  m that we hold..
42d0: 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 53        */.      S
42e0: 74 6d 74 20 71 3b 0a 20 20 20 20 20 20 64 62 5f  tmt q;.      db_
42f0: 70 72 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c  prepare(&q, "SEL
4300: 45 43 54 20 75 75 69 64 20 46 52 4f 4d 20 62 6c  ECT uuid FROM bl
4310: 6f 62 20 57 48 45 52 45 20 73 69 7a 65 3c 30 22  ob WHERE size<0"
4320: 29 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  );.      while( 
4330: 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53 51 4c  db_step(&q)==SQL
4340: 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20  ITE_ROW ){.     
4350: 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
4360: 55 75 69 64 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e  Uuid = db_column
4370: 5f 74 65 78 74 28 26 71 2c 20 30 29 3b 0a 20 20  _text(&q, 0);.  
4380: 20 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e        blob_appen
4390: 64 66 28 26 73 65 6e 64 2c 22 67 69 6d 6d 65 20  df(&send,"gimme 
43a0: 25 73 5c 6e 22 2c 20 7a 55 75 69 64 29 3b 0a 20  %s\n", zUuid);. 
43b0: 20 20 20 20 20 20 20 6e 52 65 71 2b 2b 3b 0a 20         nReq++;. 
43c0: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 62 5f       }.      db_
43d0: 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 20 20  finalize(&q);.  
43e0: 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 70 75 73    }..    if( pus
43f0: 68 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 2f  hFlag ){.      /
4400: 2a 20 53 65 6e 64 20 74 68 65 20 73 65 72 76 65  * Send the serve
4410: 72 20 61 6e 79 20 66 69 6c 65 73 20 74 68 61 74  r any files that
4420: 20 74 68 65 20 73 65 72 76 65 72 20 68 61 73 20   the server has 
4430: 72 65 71 75 65 73 74 65 64 20 2a 2f 0a 20 20 20  requested */.   
4440: 20 20 20 6e 46 69 6c 65 20 2b 3d 20 73 65 6e 64     nFile += send
4450: 5f 61 6c 6c 5f 70 65 6e 64 69 6e 67 28 26 73 65  _all_pending(&se
4460: 6e 64 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  nd);.    }..    
4470: 69 66 28 20 70 75 6c 6c 46 6c 61 67 20 7c 7c 20  if( pullFlag || 
4480: 70 75 73 68 46 6c 61 67 20 29 7b 0a 20 20 20 20  pushFlag ){.    
4490: 20 20 2f 2a 20 41 6c 77 61 79 73 20 73 65 6e 64    /* Always send
44a0: 20 6f 75 72 20 6c 65 61 76 65 73 20 2a 2f 0a 20   our leaves */. 
44b0: 20 20 20 20 20 53 74 6d 74 20 71 3b 0a 20 20 20       Stmt q;.   
44c0: 20 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 71     db_prepare(&q
44d0: 2c 20 0a 20 20 20 20 20 20 20 20 20 22 53 45 4c  , .         "SEL
44e0: 45 43 54 20 75 75 69 64 20 46 52 4f 4d 20 62 6c  ECT uuid FROM bl
44f0: 6f 62 20 57 48 45 52 45 20 72 69 64 20 49 4e 22  ob WHERE rid IN"
4500: 0a 20 20 20 20 20 20 20 20 20 22 20 20 28 53 45  .         "  (SE
4510: 4c 45 43 54 20 63 69 64 20 46 52 4f 4d 20 70 6c  LECT cid FROM pl
4520: 69 6e 6b 20 45 58 43 45 50 54 20 53 45 4c 45 43  ink EXCEPT SELEC
4530: 54 20 70 69 64 20 46 52 4f 4d 20 70 6c 69 6e 6b  T pid FROM plink
4540: 29 22 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20  )".      );.    
4550: 20 20 77 68 69 6c 65 28 20 64 62 5f 73 74 65 70    while( db_step
4560: 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  (&q)==SQLITE_ROW
4570: 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73   ){.        cons
4580: 74 20 63 68 61 72 20 2a 7a 55 75 69 64 20 3d 20  t char *zUuid = 
4590: 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 26  db_column_text(&
45a0: 71 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 62  q, 0);.        b
45b0: 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 73 65 6e  lob_appendf(&sen
45c0: 64 2c 20 22 6c 65 61 66 20 25 73 5c 6e 22 2c 20  d, "leaf %s\n", 
45d0: 7a 55 75 69 64 29 3b 0a 20 20 20 20 20 20 20 20  zUuid);.        
45e0: 6e 4d 73 67 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a  nMsg++;.      }.
45f0: 20 20 20 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a        db_finaliz
4600: 65 28 26 71 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  e(&q);.    }..  
4610: 20 20 2f 2a 20 45 78 63 68 61 6e 67 65 20 6d 65    /* Exchange me
4620: 73 73 61 67 65 73 20 77 69 74 68 20 74 68 65 20  ssages with the 
4630: 73 65 72 76 65 72 20 2a 2f 0a 20 20 20 20 70 72  server */.    pr
4640: 69 6e 74 66 28 22 53 65 6e 64 3a 20 20 20 20 20  intf("Send:     
4650: 20 25 33 64 20 66 69 6c 65 73 2c 20 25 33 64 20   %3d files, %3d 
4660: 72 65 71 75 65 73 74 73 2c 20 25 33 64 20 6f 74  requests, %3d ot
4670: 68 65 72 20 6d 73 67 73 2c 20 25 38 64 20 62 79  her msgs, %8d by
4680: 74 65 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20  tes\n",.        
4690: 20 20 20 20 6e 46 69 6c 65 2c 20 6e 52 65 71 2c      nFile, nReq,
46a0: 20 6e 4d 73 67 2c 20 62 6c 6f 62 5f 73 69 7a 65   nMsg, blob_size
46b0: 28 26 73 65 6e 64 29 29 3b 0a 20 20 20 20 6e 46  (&send));.    nF
46c0: 69 6c 65 53 65 6e 64 20 3d 20 6e 46 69 6c 65 3b  ileSend = nFile;
46d0: 0a 20 20 20 20 6e 46 69 6c 65 20 3d 20 6e 52 65  .    nFile = nRe
46e0: 71 20 3d 20 6e 4d 73 67 20 3d 20 30 3b 0a 20 20  q = nMsg = 0;.  
46f0: 20 20 68 74 74 70 5f 65 78 63 68 61 6e 67 65 28    http_exchange(
4700: 26 73 65 6e 64 2c 20 26 72 65 63 76 29 3b 0a 20  &send, &recv);. 
4710: 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 73     blob_reset(&s
4720: 65 6e 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 50 72  end);..    /* Pr
4730: 6f 63 65 73 73 20 74 68 65 20 72 65 70 6c 79 20  ocess the reply 
4740: 74 68 61 74 20 63 61 6d 65 20 62 61 63 6b 20 66  that came back f
4750: 72 6f 6d 20 74 68 65 20 73 65 72 76 65 72 20 2a  rom the server *
4760: 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 62 6c 6f  /.    while( blo
4770: 62 5f 6c 69 6e 65 28 26 72 65 63 76 2c 20 26 6c  b_line(&recv, &l
4780: 69 6e 65 29 20 29 7b 0a 20 20 20 20 20 20 6e 54  ine) ){.      nT
4790: 6f 6b 65 6e 20 3d 20 62 6c 6f 62 5f 74 6f 6b 65  oken = blob_toke
47a0: 6e 69 7a 65 28 26 6c 69 6e 65 2c 20 61 54 6f 6b  nize(&line, aTok
47b0: 65 6e 2c 20 63 6f 75 6e 74 28 61 54 6f 6b 65 6e  en, count(aToken
47c0: 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 20 20  ));..      /*   
47d0: 66 69 6c 65 20 55 55 49 44 20 53 49 5a 45 20 5c  file UUID SIZE \
47e0: 6e 20 43 4f 4e 54 45 4e 54 0a 20 20 20 20 20 20  n CONTENT.      
47f0: 2a 2a 20 20 20 66 69 6c 65 20 55 55 49 44 20 44  **   file UUID D
4800: 45 4c 54 41 53 52 43 20 53 49 5a 45 20 5c 6e 20  ELTASRC SIZE \n 
4810: 43 4f 4e 54 45 4e 54 0a 20 20 20 20 20 20 2a 2a  CONTENT.      **
4820: 0a 20 20 20 20 20 20 2a 2a 20 52 65 63 65 69 76  .      ** Receiv
4830: 65 20 61 20 66 69 6c 65 20 74 72 61 6e 73 6d 69  e a file transmi
4840: 74 74 65 64 20 66 72 6f 6d 20 74 68 65 20 6f 74  tted from the ot
4850: 68 65 72 20 73 69 64 65 0a 20 20 20 20 20 20 2a  her side.      *
4860: 2f 0a 20 20 20 20 20 20 69 66 28 20 62 6c 6f 62  /.      if( blob
4870: 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 22  _eq(&aToken[0],"
4880: 66 69 6c 65 22 29 20 29 7b 0a 20 20 20 20 20 20  file") ){.      
4890: 20 20 78 66 65 72 5f 61 63 63 65 70 74 5f 66 69    xfer_accept_fi
48a0: 6c 65 28 26 72 65 63 76 2c 20 61 54 6f 6b 65 6e  le(&recv, aToken
48b0: 2c 20 6e 54 6f 6b 65 6e 2c 20 26 65 72 72 6d 73  , nToken, &errms
48c0: 67 29 3b 0a 20 20 20 20 20 20 20 20 6e 46 69 6c  g);.        nFil
48d0: 65 2b 2b 3b 0a 20 20 20 20 20 20 20 20 67 6f 20  e++;.        go 
48e0: 3d 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  = 1;.      }else
48f0: 0a 0a 20 20 20 20 20 20 2f 2a 20 20 20 67 69 6d  ..      /*   gim
4900: 6d 65 20 55 55 49 44 0a 20 20 20 20 20 20 2a 2a  me UUID.      **
4910: 0a 20 20 20 20 20 20 2a 2a 20 53 65 72 76 65 72  .      ** Server
4920: 20 69 73 20 72 65 71 75 65 73 74 69 6e 67 20 61   is requesting a
4930: 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2f 0a 20   file.      */. 
4940: 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71       if( blob_eq
4950: 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 67 69  (&aToken[0], "gi
4960: 6d 6d 65 22 29 20 26 26 20 6e 54 6f 6b 65 6e 3d  mme") && nToken=
4970: 3d 32 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  =2.             
4980: 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69    && blob_is_uui
4990: 64 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 20 29 7b  d(&aToken[1]) ){
49a0: 0a 20 20 20 20 20 20 20 20 6e 52 65 71 2b 2b 3b  .        nReq++;
49b0: 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 75 73  .        if( pus
49c0: 68 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20  hFlag ){.       
49d0: 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63     db_multi_exec
49e0: 28 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 49  (.            "I
49f0: 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20  NSERT OR IGNORE 
4a00: 49 4e 54 4f 20 70 65 6e 64 69 6e 67 28 72 69 64  INTO pending(rid
4a10: 29 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  ) ".            
4a20: 22 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f 4d  "SELECT rid FROM
4a30: 20 62 6c 6f 62 20 57 48 45 52 45 20 75 75 69 64   blob WHERE uuid
4a40: 3d 25 42 20 41 4e 44 20 73 69 7a 65 3e 3d 30 22  =%B AND size>=0"
4a50: 2c 20 26 61 54 6f 6b 65 6e 5b 31 5d 0a 20 20 20  , &aToken[1].   
4a60: 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20         );.      
4a70: 20 20 20 20 67 6f 20 3d 20 31 3b 0a 20 20 20 20      go = 1;.    
4a80: 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
4a90: 65 0a 20 20 0a 20 20 20 20 20 20 2f 2a 20 20 20  e.  .      /*   
4aa0: 69 67 6f 74 20 55 55 49 44 0a 20 20 20 20 20 20  igot UUID.      
4ab0: 2a 2a 20 20 20 6c 65 61 66 20 55 55 49 44 0a 20  **   leaf UUID. 
4ac0: 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
4ad0: 20 53 65 72 76 65 72 20 70 72 6f 63 6c 61 69 6d   Server proclaim
4ae0: 73 20 74 68 61 74 20 69 74 20 68 61 73 20 61 20  s that it has a 
4af0: 70 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65 2e  particular file.
4b00: 20 20 41 20 6c 65 61 66 20 6d 65 73 73 61 67 65    A leaf message
4b10: 0a 20 20 20 20 20 20 2a 2a 20 6d 65 61 6e 73 20  .      ** means 
4b20: 74 68 61 74 20 74 68 65 20 66 69 6c 65 20 69 73  that the file is
4b30: 20 61 20 6c 65 61 66 20 6d 61 6e 69 66 65 73 74   a leaf manifest
4b40: 20 6f 6e 20 74 68 65 20 73 65 72 76 65 72 2e 0a   on the server..
4b50: 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
4b60: 66 28 20 6e 54 6f 6b 65 6e 3d 3d 32 0a 20 20 20  f( nToken==2.   
4b70: 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 62             && (b
4b80: 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30  lob_eq(&aToken[0
4b90: 5d 2c 20 22 69 67 6f 74 22 29 20 7c 7c 20 62 6c  ], "igot") || bl
4ba0: 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d  ob_eq(&aToken[0]
4bb0: 2c 20 22 6c 65 61 66 22 29 29 0a 20 20 20 20 20  , "leaf")).     
4bc0: 20 20 20 20 20 20 20 20 20 26 26 20 62 6c 6f 62           && blob
4bd0: 5f 69 73 5f 75 75 69 64 28 26 61 54 6f 6b 65 6e  _is_uuid(&aToken
4be0: 5b 31 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20  [1]) ){.        
4bf0: 69 6e 74 20 72 69 64 20 3d 20 64 62 5f 69 6e 74  int rid = db_int
4c00: 28 30 2c 20 22 53 45 4c 45 43 54 20 72 69 64 20  (0, "SELECT rid 
4c10: 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20  FROM blob WHERE 
4c20: 75 75 69 64 3d 25 42 22 2c 20 26 61 54 6f 6b 65  uuid=%B", &aToke
4c30: 6e 5b 31 5d 29 3b 0a 20 20 20 20 20 20 20 20 6e  n[1]);.        n
4c40: 4d 73 67 2b 2b 3b 0a 20 20 20 20 20 20 20 20 69  Msg++;.        i
4c50: 66 28 20 72 69 64 3e 30 20 29 7b 0a 20 20 20 20  f( rid>0 ){.    
4c60: 20 20 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65        db_multi_e
4c70: 78 65 63 28 0a 20 20 20 20 20 20 20 20 20 20 20  xec(.           
4c80: 20 22 49 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f   "INSERT OR IGNO
4c90: 52 45 20 49 4e 54 4f 20 6f 6e 72 65 6d 6f 74 65  RE INTO onremote
4ca0: 28 72 69 64 29 20 56 41 4c 55 45 53 28 25 64 29  (rid) VALUES(%d)
4cb0: 22 2c 20 72 69 64 0a 20 20 20 20 20 20 20 20 20  ", rid.         
4cc0: 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   );.          /*
4cd0: 20 41 64 64 20 74 6f 20 74 68 65 20 70 65 6e 64   Add to the pend
4ce0: 69 6e 67 20 73 65 74 20 61 6c 6c 20 63 68 69 6c  ing set all chil
4cf0: 64 72 65 6e 20 6f 66 20 74 68 65 20 73 65 72 76  dren of the serv
4d00: 65 72 27 73 20 6c 65 61 76 65 73 20 2a 2f 0a 20  er's leaves */. 
4d10: 20 20 20 20 20 20 20 20 20 69 66 28 20 70 75 73           if( pus
4d20: 68 46 6c 61 67 20 26 26 20 62 6c 6f 62 5f 65 71  hFlag && blob_eq
4d30: 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 6c 65  (&aToken[0], "le
4d40: 61 66 22 29 20 29 7b 0a 20 20 20 20 20 20 20 20  af") ){.        
4d50: 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65      db_multi_exe
4d60: 63 28 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  c(.             
4d70: 20 22 49 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f   "INSERT OR IGNO
4d80: 52 45 20 49 4e 54 4f 20 70 65 6e 64 69 6e 67 28  RE INTO pending(
4d90: 72 69 64 29 20 22 0a 20 20 20 20 20 20 20 20 20  rid) ".         
4da0: 20 20 20 20 20 22 53 45 4c 45 43 54 20 63 69 64       "SELECT cid
4db0: 20 46 52 4f 4d 20 70 6c 69 6e 6b 20 57 48 45 52   FROM plink WHER
4dc0: 45 20 70 69 64 3d 25 64 22 2c 20 72 69 64 0a 20  E pid=%d", rid. 
4dd0: 20 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20 20             );.  
4de0: 20 20 20 20 20 20 20 20 20 20 69 66 28 20 64 62            if( db
4df0: 5f 63 68 61 6e 67 65 73 28 29 3e 30 20 29 7b 0a  _changes()>0 ){.
4e00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 67 6f                go
4e10: 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20   = 1;.          
4e20: 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a    }.          }.
4e30: 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 75            if( pu
4e40: 6c 6c 46 6c 61 67 20 26 26 20 21 67 6f 20 26 26  llFlag && !go &&
4e50: 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
4e60: 64 62 5f 65 78 69 73 74 73 28 22 53 45 4c 45 43  db_exists("SELEC
4e70: 54 20 31 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48  T 1 FROM blob WH
4e80: 45 52 45 20 72 69 64 3d 25 64 20 41 4e 44 20 73  ERE rid=%d AND s
4e90: 69 7a 65 3c 30 22 2c 20 72 69 64 29 20 29 7b 0a  ize<0", rid) ){.
4ea0: 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 20 3d              go =
4eb0: 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a   1;.          }.
4ec0: 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
4ed0: 28 20 70 75 6c 6c 46 6c 61 67 20 29 7b 0a 20 20  ( pullFlag ){.  
4ee0: 20 20 20 20 20 20 20 20 67 6f 20 3d 20 31 3b 0a          go = 1;.
4ef0: 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 65 6e            conten
4f00: 74 5f 70 75 74 28 30 2c 20 62 6c 6f 62 5f 73 74  t_put(0, blob_st
4f10: 72 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 29 3b 0a  r(&aToken[1]));.
4f20: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
4f30: 7d 65 6c 73 65 0a 20 20 0a 20 20 20 20 20 20 2f  }else.  .      /
4f40: 2a 20 20 20 70 75 73 68 20 20 53 45 52 56 45 52  *   push  SERVER
4f50: 43 4f 44 45 20 20 50 52 4f 44 55 43 54 43 4f 44  CODE  PRODUCTCOD
4f60: 45 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  E.      **.     
4f70: 20 2a 2a 20 53 68 6f 75 6c 64 20 6f 6e 6c 79 20   ** Should only 
4f80: 68 61 70 70 65 6e 20 69 6e 20 72 65 73 70 6f 6e  happen in respon
4f90: 73 65 20 74 6f 20 61 20 63 6c 6f 6e 65 2e 0a 20  se to a clone.. 
4fa0: 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66       */.      if
4fb0: 28 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65  ( blob_eq(&aToke
4fc0: 6e 5b 30 5d 2c 22 70 75 73 68 22 29 20 26 26 20  n[0],"push") && 
4fd0: 6e 54 6f 6b 65 6e 3d 3d 33 20 26 26 20 63 6c 6f  nToken==3 && clo
4fe0: 6e 65 46 6c 61 67 0a 20 20 20 20 20 20 20 20 20  neFlag.         
4ff0: 20 20 20 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f       && blob_is_
5000: 75 75 69 64 28 26 61 54 6f 6b 65 6e 5b 31 5d 29  uuid(&aToken[1])
5010: 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69 64   && blob_is_uuid
5020: 28 26 61 54 6f 6b 65 6e 5b 32 5d 29 20 29 7b 0a  (&aToken[2]) ){.
5030: 0a 20 20 20 20 20 20 20 20 69 66 28 20 62 6c 6f  .        if( blo
5040: 62 5f 65 71 5f 73 74 72 28 26 61 54 6f 6b 65 6e  b_eq_str(&aToken
5050: 5b 31 5d 2c 20 7a 53 43 6f 64 65 2c 20 2d 31 29  [1], zSCode, -1)
5060: 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f   ){.          fo
5070: 73 73 69 6c 5f 66 61 74 61 6c 28 22 73 65 72 76  ssil_fatal("serv
5080: 65 72 20 6c 6f 6f 70 22 29 3b 0a 20 20 20 20 20  er loop");.     
5090: 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 4d 73     }.        nMs
50a0: 67 2b 2b 3b 0a 20 20 20 20 20 20 20 20 69 66 28  g++;.        if(
50b0: 20 7a 50 43 6f 64 65 3d 3d 30 20 29 7b 0a 20 20   zPCode==0 ){.  
50c0: 20 20 20 20 20 20 20 20 7a 50 43 6f 64 65 20 3d          zPCode =
50d0: 20 6d 70 72 69 6e 74 66 28 22 25 62 22 2c 20 26   mprintf("%b", &
50e0: 61 54 6f 6b 65 6e 5b 32 5d 29 3b 0a 20 20 20 20  aToken[2]);.    
50f0: 20 20 20 20 20 20 64 62 5f 73 65 74 28 22 70 72        db_set("pr
5100: 6f 6a 65 63 74 2d 63 6f 64 65 22 2c 20 7a 50 43  oject-code", zPC
5110: 6f 64 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ode);.        }.
5120: 20 20 20 20 20 20 20 20 63 6c 6f 6e 65 46 6c 61          cloneFla
5130: 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70  g = 0;.        p
5140: 75 6c 6c 46 6c 61 67 20 3d 20 31 3b 0a 20 20 20  ullFlag = 1;.   
5150: 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20 20 20 20     }else..      
5160: 2f 2a 20 20 20 65 72 72 6f 72 20 4d 45 53 53 41  /*   error MESSA
5170: 47 45 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  GE.      **.    
5180: 20 20 2a 2a 20 52 65 70 6f 72 74 20 61 6e 20 65    ** Report an e
5190: 72 72 6f 72 0a 20 20 20 20 20 20 2a 2f 20 20 20  rror.      */   
51a0: 20 20 20 20 20 0a 20 20 20 20 20 20 69 66 28 20       .      if( 
51b0: 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b  blob_eq(&aToken[
51c0: 30 5d 2c 22 65 72 72 6f 72 22 29 20 26 26 20 6e  0],"error") && n
51d0: 54 6f 6b 65 6e 3d 3d 32 20 29 7b 0a 20 20 20 20  Token==2 ){.    
51e0: 20 20 20 20 63 68 61 72 20 2a 7a 4d 73 67 20 3d      char *zMsg =
51f0: 20 62 6c 6f 62 5f 74 65 72 6d 69 6e 61 74 65 28   blob_terminate(
5200: 26 61 54 6f 6b 65 6e 5b 31 5d 29 3b 0a 20 20 20  &aToken[1]);.   
5210: 20 20 20 20 20 64 65 66 6f 73 73 69 6c 69 7a 65       defossilize
5220: 28 7a 4d 73 67 29 3b 0a 20 20 20 20 20 20 20 20  (zMsg);.        
5230: 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 65 72  blob_appendf(&er
5240: 72 6d 73 67 2c 20 22 73 65 72 76 65 72 20 73 61  rmsg, "server sa
5250: 79 73 3a 20 25 73 22 2c 20 7a 4d 73 67 29 3b 0a  ys: %s", zMsg);.
5260: 20 20 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20        }else..   
5270: 20 20 20 2f 2a 20 55 6e 6b 6e 6f 77 6e 20 6d 65     /* Unknown me
5280: 73 73 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 7b  ssage */.      {
5290: 0a 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 61 70  .        blob_ap
52a0: 70 65 6e 64 66 28 26 65 72 72 6d 73 67 2c 20 22  pendf(&errmsg, "
52b0: 75 6e 6b 6e 6f 77 6e 20 63 6f 6d 6d 61 6e 64 3a  unknown command:
52c0: 20 25 62 22 2c 20 26 61 54 6f 6b 65 6e 5b 30 5d   %b", &aToken[0]
52d0: 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  );.      }..    
52e0: 20 20 69 66 28 20 62 6c 6f 62 5f 73 69 7a 65 28    if( blob_size(
52f0: 26 65 72 72 6d 73 67 29 20 29 7b 0a 20 20 20 20  &errmsg) ){.    
5300: 20 20 20 20 66 6f 73 73 69 6c 5f 66 61 74 61 6c      fossil_fatal
5310: 28 22 25 62 22 2c 20 26 65 72 72 6d 73 67 29 3b  ("%b", &errmsg);
5320: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
5330: 6c 6f 62 61 72 72 61 79 5f 72 65 73 65 74 28 61  lobarray_reset(a
5340: 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29 3b 0a  Token, nToken);.
5350: 20 20 20 20 7d 0a 20 20 20 20 70 72 69 6e 74 66      }.    printf
5360: 28 22 52 65 63 65 69 76 65 64 3a 20 20 25 33 64  ("Received:  %3d
5370: 20 66 69 6c 65 73 2c 20 25 33 64 20 72 65 71 75   files, %3d requ
5380: 65 73 74 73 2c 20 25 33 64 20 6f 74 68 65 72 20  ests, %3d other 
5390: 6d 73 67 73 2c 20 25 38 64 20 62 79 74 65 73 5c  msgs, %8d bytes\
53a0: 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
53b0: 6e 46 69 6c 65 2c 20 6e 52 65 71 2c 20 6e 4d 73  nFile, nReq, nMs
53c0: 67 2c 20 62 6c 6f 62 5f 73 69 7a 65 28 26 72 65  g, blob_size(&re
53d0: 63 76 29 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 72  cv));.    blob_r
53e0: 65 73 65 74 28 26 72 65 63 76 29 3b 0a 20 20 20  eset(&recv);.   
53f0: 20 69 66 28 20 6e 46 69 6c 65 53 65 6e 64 20 2b   if( nFileSend +
5400: 20 6e 46 69 6c 65 3d 3d 30 20 29 7b 0a 20 20 20   nFile==0 ){.   
5410: 20 20 20 6e 4e 6f 46 69 6c 65 43 79 63 6c 65 2b     nNoFileCycle+
5420: 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 4e 6f  +;.      if( nNo
5430: 46 69 6c 65 43 79 63 6c 65 3e 31 20 29 7b 0a 20  FileCycle>1 ){. 
5440: 20 20 20 20 20 20 20 67 6f 20 3d 20 30 3b 0a 20         go = 0;. 
5450: 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
5460: 7b 0a 20 20 20 20 20 20 6e 4e 6f 46 69 6c 65 43  {.      nNoFileC
5470: 79 63 6c 65 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  ycle = 0;.    }.
5480: 20 20 20 20 6e 46 69 6c 65 20 3d 20 6e 52 65 71      nFile = nReq
5490: 20 3d 20 6e 4d 73 67 20 3d 20 30 3b 0a 20 20 7d   = nMsg = 0;.  }
54a0: 3b 0a 20 20 68 74 74 70 5f 63 6c 6f 73 65 28 29  ;.  http_close()
54b0: 3b 0a 20 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73  ;.  db_end_trans
54c0: 61 63 74 69 6f 6e 28 30 29 3b 0a 20 20 64 62 5f  action(0);.  db_
54d0: 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 20  multi_exec(.    
54e0: 22 44 52 4f 50 20 54 41 42 4c 45 20 6f 6e 72 65  "DROP TABLE onre
54f0: 6d 6f 74 65 3b 22 0a 20 20 20 20 22 44 52 4f 50  mote;".    "DROP
5500: 20 54 41 42 4c 45 20 70 65 6e 64 69 6e 67 3b 22   TABLE pending;"
5510: 0a 20 20 29 3b 0a 7d 0a                          .  );.}.