0000: 2f 2a 0a 2a 2a 20 43 6f 70 79 72 69 67 68 74 20 /*.** Copyright
0010: 28 63 29 20 32 30 30 37 20 44 2e 20 52 69 63 68 (c) 2007 D. Rich
0020: 61 72 64 20 48 69 70 70 0a 2a 2a 0a 2a 2a 20 54 ard Hipp.**.** T
0030: 68 69 73 20 70 72 6f 67 72 61 6d 20 69 73 20 66 his program is f
0040: 72 65 65 20 73 6f 66 74 77 61 72 65 3b 20 79 6f ree software; yo
0050: 75 20 63 61 6e 20 72 65 64 69 73 74 72 69 62 75 u can redistribu
0060: 74 65 20 69 74 20 61 6e 64 2f 6f 72 0a 2a 2a 20 te it and/or.**
0070: 6d 6f 64 69 66 79 20 69 74 20 75 6e 64 65 72 20 modify it under
0080: 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65 the terms of the
0090: 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 50 75 62 GNU General Pub
00a0: 6c 69 63 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 76 lic.** License v
00b0: 65 72 73 69 6f 6e 20 32 20 61 73 20 70 75 62 6c ersion 2 as publ
00c0: 69 73 68 65 64 20 62 79 20 74 68 65 20 46 72 65 ished by the Fre
00d0: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64 e Software Found
00e0: 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 ation..**.** Thi
00f0: 73 20 70 72 6f 67 72 61 6d 20 69 73 20 64 69 73 s program is dis
0100: 74 72 69 62 75 74 65 64 20 69 6e 20 74 68 65 20 tributed in the
0110: 68 6f 70 65 20 74 68 61 74 20 69 74 20 77 69 6c hope that it wil
0120: 6c 20 62 65 20 75 73 65 66 75 6c 2c 0a 2a 2a 20 l be useful,.**
0130: 62 75 74 20 57 49 54 48 4f 55 54 20 41 4e 59 20 but WITHOUT ANY
0140: 57 41 52 52 41 4e 54 59 3b 20 77 69 74 68 6f 75 WARRANTY; withou
0150: 74 20 65 76 65 6e 20 74 68 65 20 69 6d 70 6c 69 t even the impli
0160: 65 64 20 77 61 72 72 61 6e 74 79 20 6f 66 0a 2a ed warranty of.*
0170: 2a 20 4d 45 52 43 48 41 4e 54 41 42 49 4c 49 54 * MERCHANTABILIT
0180: 59 20 6f 72 20 46 49 54 4e 45 53 53 20 46 4f 52 Y or FITNESS FOR
0190: 20 41 20 50 41 52 54 49 43 55 4c 41 52 20 50 55 A PARTICULAR PU
01a0: 52 50 4f 53 45 2e 20 20 53 65 65 20 74 68 65 20 RPOSE. See the
01b0: 47 4e 55 0a 2a 2a 20 47 65 6e 65 72 61 6c 20 50 GNU.** General P
01c0: 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 20 66 6f ublic License fo
01d0: 72 20 6d 6f 72 65 20 64 65 74 61 69 6c 73 2e 0a r more details..
01e0: 2a 2a 20 0a 2a 2a 20 59 6f 75 20 73 68 6f 75 6c ** .** You shoul
01f0: 64 20 68 61 76 65 20 72 65 63 65 69 76 65 64 20 d have received
0200: 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 47 4e a copy of the GN
0210: 55 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69 63 U General Public
0220: 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 61 6c 6f 6e .** License alon
0230: 67 20 77 69 74 68 20 74 68 69 73 20 6c 69 62 72 g with this libr
0240: 61 72 79 3b 20 69 66 20 6e 6f 74 2c 20 77 72 69 ary; if not, wri
0250: 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 46 72 65 te to the.** Fre
0260: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64 e Software Found
0270: 61 74 69 6f 6e 2c 20 49 6e 63 2e 2c 20 35 39 20 ation, Inc., 59
0280: 54 65 6d 70 6c 65 20 50 6c 61 63 65 20 2d 20 53 Temple Place - S
0290: 75 69 74 65 20 33 33 30 2c 0a 2a 2a 20 42 6f 73 uite 330,.** Bos
02a0: 74 6f 6e 2c 20 4d 41 20 20 30 32 31 31 31 2d 31 ton, MA 02111-1
02b0: 33 30 37 2c 20 55 53 41 2e 0a 2a 2a 0a 2a 2a 20 307, USA..**.**
02c0: 41 75 74 68 6f 72 20 63 6f 6e 74 61 63 74 20 69 Author contact i
02d0: 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a 20 20 nformation:.**
02e0: 20 64 72 68 40 68 77 61 63 69 2e 63 6f 6d 0a 2a drh@hwaci.com.*
02f0: 2a 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 68 * http://www.h
0300: 77 61 63 69 2e 63 6f 6d 2f 64 72 68 2f 0a 2a 2a waci.com/drh/.**
0310: 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
0320: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0330: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0340: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0350: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0360: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
0370: 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 contains code t
0380: 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 o implement the
0390: 66 69 6c 65 20 74 72 61 6e 73 66 65 72 20 70 72 file transfer pr
03a0: 6f 74 6f 63 6f 6c 2e 0a 2a 2f 0a 23 69 6e 63 6c otocol..*/.#incl
03b0: 75 64 65 20 22 63 6f 6e 66 69 67 2e 68 22 0a 23 ude "config.h".#
03c0: 69 6e 63 6c 75 64 65 20 22 78 66 65 72 2e 68 22 include "xfer.h"
03d0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 54 6f 6b ../*.** The aTok
03e0: 65 6e 5b 30 2e 2e 6e 54 6f 6b 65 6e 2d 31 5d 20 en[0..nToken-1]
03f0: 62 6c 6f 62 20 61 72 72 61 79 20 69 73 20 61 20 blob array is a
0400: 70 61 72 73 65 20 6f 66 20 61 20 22 66 69 6c 65 parse of a "file
0410: 22 20 6c 69 6e 65 20 0a 2a 2a 20 6d 65 73 73 61 " line .** messa
0420: 67 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e ge. This routin
0430: 65 20 66 69 6e 69 73 68 65 73 20 70 61 72 73 69 e finishes parsi
0440: 6e 67 20 74 68 61 74 20 6d 65 73 73 61 67 65 20 ng that message
0450: 61 6e 64 20 64 6f 65 73 0a 2a 2a 20 61 20 72 65 and does.** a re
0460: 63 6f 72 64 20 69 6e 73 65 72 74 20 6f 66 20 74 cord insert of t
0470: 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 he file..**.** T
0480: 68 65 20 66 69 6c 65 20 6c 69 6e 65 20 69 73 20 he file line is
0490: 69 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f in one of the fo
04a0: 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 66 6f 72 6d llowing two form
04b0: 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 66 69 s:.**.** fi
04c0: 6c 65 20 55 55 49 44 20 53 49 5a 45 20 5c 6e 20 le UUID SIZE \n
04d0: 43 4f 4e 54 45 4e 54 0a 2a 2a 20 20 20 20 20 20 CONTENT.**
04e0: 66 69 6c 65 20 55 55 49 44 20 44 45 4c 54 41 53 file UUID DELTAS
04f0: 52 43 20 53 49 5a 45 20 5c 6e 20 43 4f 4e 54 45 RC SIZE \n CONTE
0500: 4e 54 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e NT.**.** The con
0510: 74 65 6e 74 20 69 73 20 53 49 5a 45 20 62 79 74 tent is SIZE byt
0520: 65 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 es immediately f
0530: 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 6e 65 77 ollowing the new
0540: 6c 69 6e 65 2e 0a 2a 2a 20 49 66 20 44 45 4c 54 line..** If DELT
0550: 41 53 52 43 20 65 78 69 73 74 73 2c 20 74 68 65 ASRC exists, the
0560: 6e 20 74 68 65 20 43 4f 4e 54 45 4e 54 20 69 73 n the CONTENT is
0570: 20 61 20 64 65 6c 74 61 20 61 67 61 69 6e 73 74 a delta against
0580: 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 the.** content
0590: 6f 66 20 44 45 4c 54 41 53 52 43 2e 0a 2a 2a 0a of DELTASRC..**.
05a0: 2a 2a 20 49 66 20 61 6e 79 20 65 72 72 6f 72 20 ** If any error
05b0: 6f 63 63 75 72 73 2c 20 77 72 69 74 65 20 61 20 occurs, write a
05c0: 6d 65 73 73 61 67 65 20 69 6e 74 6f 20 70 45 72 message into pEr
05d0: 72 20 77 68 69 63 68 20 68 61 73 20 61 6c 72 65 r which has alre
05e0: 61 64 79 0a 2a 2a 20 62 65 20 69 6e 69 74 69 61 ady.** be initia
05f0: 6c 69 7a 65 64 20 74 6f 20 61 6e 20 65 6d 70 74 lized to an empt
0600: 79 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 73 74 61 y string..*/.sta
0610: 74 69 63 20 76 6f 69 64 20 78 66 65 72 5f 61 63 tic void xfer_ac
0620: 63 65 70 74 5f 66 69 6c 65 28 42 6c 6f 62 20 2a cept_file(Blob *
0630: 70 49 6e 2c 20 42 6c 6f 62 20 2a 61 54 6f 6b 65 pIn, Blob *aToke
0640: 6e 2c 20 69 6e 74 20 6e 54 6f 6b 65 6e 2c 20 42 n, int nToken, B
0650: 6c 6f 62 20 2a 70 45 72 72 29 7b 0a 20 20 69 6e lob *pErr){. in
0660: 74 20 6e 3b 0a 20 20 69 6e 74 20 72 69 64 3b 0a t n;. int rid;.
0670: 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 2c 20 Blob content,
0680: 68 61 73 68 3b 0a 20 20 0a 20 20 69 66 28 20 6e hash;. . if( n
0690: 54 6f 6b 65 6e 3c 33 20 7c 7c 20 6e 54 6f 6b 65 Token<3 || nToke
06a0: 6e 3e 34 20 7c 7c 20 21 62 6c 6f 62 5f 69 73 5f n>4 || !blob_is_
06b0: 75 75 69 64 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 uuid(&aToken[1])
06c0: 0a 20 20 20 20 20 20 20 7c 7c 20 21 62 6c 6f 62 . || !blob
06d0: 5f 69 73 5f 69 6e 74 28 26 61 54 6f 6b 65 6e 5b _is_int(&aToken[
06e0: 6e 54 6f 6b 65 6e 2d 31 5d 2c 20 26 6e 29 20 7c nToken-1], &n) |
06f0: 7c 20 6e 3c 3d 30 0a 20 20 20 20 20 20 20 7c 7c | n<=0. ||
0700: 20 28 6e 54 6f 6b 65 6e 3d 3d 34 20 26 26 20 21 (nToken==4 && !
0710: 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26 61 54 blob_is_uuid(&aT
0720: 6f 6b 65 6e 5b 32 5d 29 29 20 29 7b 0a 20 20 20 oken[2])) ){.
0730: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70 45 blob_appendf(pE
0740: 72 72 2c 20 22 6d 61 6c 66 6f 72 6d 65 64 20 66 rr, "malformed f
0750: 69 6c 65 20 6c 69 6e 65 22 29 3b 0a 20 20 20 20 ile line");.
0760: 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 62 6c return;. }. bl
0770: 6f 62 5f 7a 65 72 6f 28 26 63 6f 6e 74 65 6e 74 ob_zero(&content
0780: 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 );. blob_zero(&
0790: 68 61 73 68 29 3b 0a 20 20 62 6c 6f 62 5f 65 78 hash);. blob_ex
07a0: 74 72 61 63 74 28 70 49 6e 2c 20 6e 2c 20 26 63 tract(pIn, n, &c
07b0: 6f 6e 74 65 6e 74 29 3b 0a 20 20 69 66 28 20 6e ontent);. if( n
07c0: 54 6f 6b 65 6e 3d 3d 34 20 29 7b 0a 20 20 20 20 Token==4 ){.
07d0: 42 6c 6f 62 20 73 72 63 3b 0a 20 20 20 20 69 6e Blob src;. in
07e0: 74 20 73 72 63 69 64 20 3d 20 64 62 5f 69 6e 74 t srcid = db_int
07f0: 28 30 2c 20 22 53 45 4c 45 43 54 20 72 69 64 20 (0, "SELECT rid
0800: 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 FROM blob WHERE
0810: 75 75 69 64 3d 25 42 22 2c 20 26 61 54 6f 6b 65 uuid=%B", &aToke
0820: 6e 5b 32 5d 29 3b 0a 20 20 20 20 69 66 28 20 73 n[2]);. if( s
0830: 72 63 69 64 3d 3d 30 20 29 7b 0a 20 20 20 20 20 rcid==0 ){.
0840: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70 45 blob_appendf(pE
0850: 72 72 2c 20 22 75 6e 6b 6e 6f 77 6e 20 64 65 6c rr, "unknown del
0860: 74 61 20 73 6f 75 72 63 65 3a 20 25 62 22 2c 20 ta source: %b",
0870: 26 61 54 6f 6b 65 6e 5b 32 5d 29 3b 0a 20 20 20 &aToken[2]);.
0880: 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d return;. }
0890: 0a 20 20 20 20 63 6f 6e 74 65 6e 74 5f 67 65 74 . content_get
08a0: 28 73 72 63 69 64 2c 20 26 73 72 63 29 3b 0a 20 (srcid, &src);.
08b0: 20 20 20 62 6c 6f 62 5f 64 65 6c 74 61 5f 61 70 blob_delta_ap
08c0: 70 6c 79 28 26 73 72 63 2c 20 26 63 6f 6e 74 65 ply(&src, &conte
08d0: 6e 74 2c 20 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 nt, &content);.
08e0: 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 73 blob_reset(&s
08f0: 72 63 29 3b 0a 20 20 7d 0a 20 20 73 68 61 31 73 rc);. }. sha1s
0900: 75 6d 5f 62 6c 6f 62 28 26 63 6f 6e 74 65 6e 74 um_blob(&content
0910: 2c 20 26 68 61 73 68 29 3b 0a 20 20 69 66 28 20 , &hash);. if(
0920: 21 62 6c 6f 62 5f 65 71 5f 73 74 72 28 26 61 54 !blob_eq_str(&aT
0930: 6f 6b 65 6e 5b 31 5d 2c 20 62 6c 6f 62 5f 73 74 oken[1], blob_st
0940: 72 28 26 63 6f 6e 74 65 6e 74 29 2c 20 2d 31 29 r(&content), -1)
0950: 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 ){. blob_app
0960: 65 6e 64 66 28 70 45 72 72 2c 20 22 63 6f 6e 74 endf(pErr, "cont
0970: 65 6e 74 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 ent does not mat
0980: 63 68 20 73 68 61 31 20 68 61 73 68 22 29 3b 0a ch sha1 hash");.
0990: 20 20 7d 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 }. blob_reset
09a0: 28 26 68 61 73 68 29 3b 0a 20 20 72 69 64 20 3d (&hash);. rid =
09b0: 20 63 6f 6e 74 65 6e 74 5f 70 75 74 28 26 63 6f content_put(&co
09c0: 6e 74 65 6e 74 2c 20 30 29 3b 0a 20 20 6d 61 6e ntent, 0);. man
09d0: 69 66 65 73 74 5f 63 72 6f 73 73 6c 69 6e 6b 28 ifest_crosslink(
09e0: 72 69 64 2c 20 26 63 6f 6e 74 65 6e 74 29 3b 0a rid, &content);.
09f0: 20 20 69 66 28 20 72 69 64 3d 3d 30 20 29 7b 0a if( rid==0 ){.
0a00: 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 blob_appendf
0a10: 28 70 45 72 72 2c 20 22 25 73 22 2c 20 67 2e 7a (pErr, "%s", g.z
0a20: 45 72 72 4d 73 67 29 3b 0a 20 20 7d 0a 7d 0a 0a ErrMsg);. }.}..
0a30: 2f 2a 0a 2a 2a 20 53 65 6e 64 20 74 68 65 20 66 /*.** Send the f
0a40: 69 6c 65 20 69 64 65 6e 74 69 66 69 65 64 20 62 ile identified b
0a50: 79 20 72 69 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 y rid..**.** If
0a60: 70 4f 75 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c pOut is not NULL
0a70: 2c 20 74 68 65 6e 20 61 70 70 65 6e 64 20 74 68 , then append th
0a80: 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 73 65 e text of the se
0a90: 6e 64 20 6d 65 73 73 61 67 65 0a 2a 2a 20 74 6f nd message.** to
0aa0: 20 70 4f 75 74 2e 20 20 4f 74 68 65 72 77 69 73 pOut. Otherwis
0ab0: 65 2c 20 61 70 70 65 6e 64 20 74 68 65 20 74 65 e, append the te
0ac0: 78 74 20 74 6f 20 74 68 65 20 43 47 49 20 6f 75 xt to the CGI ou
0ad0: 74 70 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tput..*/.static
0ae0: 69 6e 74 20 73 65 6e 64 5f 66 69 6c 65 28 69 6e int send_file(in
0af0: 74 20 72 69 64 2c 20 42 6c 6f 62 20 2a 70 4f 75 t rid, Blob *pOu
0b00: 74 29 7b 0a 20 20 42 6c 6f 62 20 63 6f 6e 74 65 t){. Blob conte
0b10: 6e 74 2c 20 75 75 69 64 3b 0a 20 20 69 6e 74 20 nt, uuid;. int
0b20: 73 69 7a 65 3b 0a 0a 23 69 66 20 30 0a 53 45 4c size;..#if 0.SEL
0b30: 45 43 54 20 73 72 63 69 64 20 46 52 4f 4d 20 64 ECT srcid FROM d
0b40: 65 6c 74 61 0a 20 57 48 45 52 45 20 72 69 64 3d elta. WHERE rid=
0b50: 25 64 0a 20 20 20 41 4e 44 20 45 58 49 53 54 53 %d. AND EXISTS
0b60: 28 53 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 6f (SELECT 1 FROM o
0b70: 6e 72 65 6d 6f 74 65 20 57 48 45 52 45 20 72 69 nremote WHERE ri
0b80: 64 3d 73 72 63 69 64 29 0a 55 4e 49 4f 4e 20 41 d=srcid).UNION A
0b90: 4c 4c 0a 53 45 4c 45 43 54 20 69 64 20 46 52 4f LL.SELECT id FRO
0ba0: 4d 20 64 65 6c 74 61 0a 20 57 48 45 52 45 20 73 M delta. WHERE s
0bb0: 72 63 69 64 3d 25 64 0a 20 20 20 41 4e 44 20 45 rcid=%d. AND E
0bc0: 58 49 53 54 53 28 53 45 4c 45 43 54 20 31 20 46 XISTS(SELECT 1 F
0bd0: 52 4f 4d 20 6f 6e 72 65 6d 6f 74 65 20 57 48 45 ROM onremote WHE
0be0: 52 45 20 72 69 64 3d 64 65 6c 74 61 2e 72 69 64 RE rid=delta.rid
0bf0: 29 0a 4c 49 4d 49 54 20 31 0a 23 65 6e 64 69 66 ).LIMIT 1.#endif
0c00: 0a 0a 20 20 2f 2a 20 54 4f 44 4f 3a 0a 20 20 2a .. /* TODO:. *
0c10: 2a 20 43 68 65 63 6b 20 66 6f 72 20 72 65 6c 61 * Check for rela
0c20: 74 65 64 20 66 69 6c 65 73 20 69 6e 20 74 68 65 ted files in the
0c30: 20 6f 6e 72 65 6d 6f 74 65 20 54 45 4d 50 20 74 onremote TEMP t
0c40: 61 62 6c 65 2e 20 20 49 66 20 72 65 6c 61 74 65 able. If relate
0c50: 64 0a 20 20 2a 2a 20 66 69 6c 65 73 20 61 72 65 d. ** files are
0c60: 20 66 6f 75 6e 64 2c 20 74 68 65 6e 20 73 65 6e found, then sen
0c70: 64 20 61 20 64 65 6c 74 61 20 72 61 74 68 65 72 d a delta rather
0c80: 20 74 68 61 6e 20 74 68 65 20 77 68 6f 6c 65 20 than the whole
0c90: 66 69 6c 65 2e 0a 20 20 2a 2f 0a 0a 20 20 62 6c file.. */.. bl
0ca0: 6f 62 5f 7a 65 72 6f 28 26 75 75 69 64 29 3b 0a ob_zero(&uuid);.
0cb0: 20 20 64 62 5f 62 6c 6f 62 28 26 75 75 69 64 2c db_blob(&uuid,
0cc0: 20 22 53 45 4c 45 43 54 20 75 75 69 64 20 46 52 "SELECT uuid FR
0cd0: 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 72 69 OM blob WHERE ri
0ce0: 64 3d 25 64 20 41 4e 44 20 73 69 7a 65 3e 3d 30 d=%d AND size>=0
0cf0: 22 2c 20 72 69 64 29 3b 0a 20 20 69 66 28 20 62 ", rid);. if( b
0d00: 6c 6f 62 5f 73 69 7a 65 28 26 75 75 69 64 29 3d lob_size(&uuid)=
0d10: 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
0d20: 20 30 3b 0a 20 20 7d 0a 20 20 63 6f 6e 74 65 6e 0;. }. conten
0d30: 74 5f 67 65 74 28 72 69 64 2c 20 26 63 6f 6e 74 t_get(rid, &cont
0d40: 65 6e 74 29 3b 0a 20 20 73 69 7a 65 20 3d 20 62 ent);. size = b
0d50: 6c 6f 62 5f 73 69 7a 65 28 26 63 6f 6e 74 65 6e lob_size(&conten
0d60: 74 29 3b 0a 20 20 69 66 28 20 70 4f 75 74 20 29 t);. if( pOut )
0d70: 7b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e {. blob_appen
0d80: 64 66 28 70 4f 75 74 2c 20 22 66 69 6c 65 20 25 df(pOut, "file %
0d90: 62 20 25 64 5c 6e 22 2c 20 26 75 75 69 64 2c 20 b %d\n", &uuid,
0da0: 73 69 7a 65 29 3b 0a 20 20 20 20 62 6c 6f 62 5f size);. blob_
0db0: 61 70 70 65 6e 64 28 70 4f 75 74 2c 20 62 6c 6f append(pOut, blo
0dc0: 62 5f 62 75 66 66 65 72 28 26 63 6f 6e 74 65 6e b_buffer(&conten
0dd0: 74 29 2c 20 73 69 7a 65 29 3b 0a 20 20 7d 65 6c t), size);. }el
0de0: 73 65 7b 0a 20 20 20 20 63 67 69 5f 70 72 69 6e se{. cgi_prin
0df0: 74 66 28 22 66 69 6c 65 20 25 62 20 25 64 5c 6e tf("file %b %d\n
0e00: 22 2c 20 26 75 75 69 64 2c 20 73 69 7a 65 29 3b ", &uuid, size);
0e10: 0a 20 20 20 20 63 67 69 5f 61 70 70 65 6e 64 5f . cgi_append_
0e20: 63 6f 6e 74 65 6e 74 28 62 6c 6f 62 5f 62 75 66 content(blob_buf
0e30: 66 65 72 28 26 63 6f 6e 74 65 6e 74 29 2c 20 73 fer(&content), s
0e40: 69 7a 65 29 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62 ize);. }. blob
0e50: 5f 72 65 73 65 74 28 26 63 6f 6e 74 65 6e 74 29 _reset(&content)
0e60: 3b 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 ;. blob_reset(&
0e70: 75 75 69 64 29 3b 0a 20 20 64 62 5f 6d 75 6c 74 uuid);. db_mult
0e80: 69 5f 65 78 65 63 28 22 49 4e 53 45 52 54 20 4f i_exec("INSERT O
0e90: 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 6f 6e R IGNORE INTO on
0ea0: 72 65 6d 6f 74 65 20 56 41 4c 55 45 53 28 25 64 remote VALUES(%d
0eb0: 29 22 2c 20 72 69 64 29 3b 0a 20 20 72 65 74 75 )", rid);. retu
0ec0: 72 6e 20 73 69 7a 65 3b 0a 7d 0a 0a 0a 2f 2a 0a rn size;.}.../*.
0ed0: 2a 2a 20 53 65 6e 64 20 61 6c 6c 20 70 65 6e 64 ** Send all pend
0ee0: 69 6e 67 20 66 69 6c 65 73 2e 0a 2a 2f 0a 73 74 ing files..*/.st
0ef0: 61 74 69 63 20 76 6f 69 64 20 73 65 6e 64 5f 61 atic void send_a
0f00: 6c 6c 5f 70 65 6e 64 69 6e 67 28 42 6c 6f 62 20 ll_pending(Blob
0f10: 2a 70 4f 75 74 29 7b 0a 20 20 69 6e 74 20 73 65 *pOut){. int se
0f20: 6e 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6d 61 nt = 0;. int ma
0f30: 78 53 69 7a 65 20 3d 20 64 62 5f 67 65 74 5f 69 xSize = db_get_i
0f40: 6e 74 28 22 68 74 74 70 2d 6d 73 67 2d 73 69 7a nt("http-msg-siz
0f50: 65 22 2c 20 31 30 30 30 30 30 30 29 3b 0a 20 20 e", 1000000);.
0f60: 53 74 6d 74 20 71 3b 0a 23 69 66 20 30 0a 20 20 Stmt q;.#if 0.
0f70: 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 db_multi_exec(.
0f80: 20 20 20 22 43 52 45 41 54 45 20 54 45 4d 50 20 "CREATE TEMP
0f90: 54 41 42 4c 45 20 70 72 69 6f 72 69 74 79 28 72 TABLE priority(r
0fa0: 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 id INTEGER PRIMA
0fb0: 52 59 20 4b 45 59 29 3b 22 0a 20 20 20 20 22 49 RY KEY);". "I
0fc0: 4e 53 45 52 54 20 49 4e 54 4f 20 70 72 69 6f 72 NSERT INTO prior
0fd0: 69 74 79 22 0a 20 20 20 20 22 20 53 45 4c 45 43 ity". " SELEC
0fe0: 54 20 73 72 63 69 64 20 46 52 4f 4d 20 64 65 6c T srcid FROM del
0ff0: 74 61 22 0a 20 20 20 20 22 20 20 57 48 45 52 45 ta". " WHERE
1000: 20 45 58 49 53 54 53 28 53 45 4c 45 43 54 20 31 EXISTS(SELECT 1
1010: 20 46 52 4f 4d 20 6f 6e 72 65 6d 6f 74 65 20 57 FROM onremote W
1020: 48 45 52 45 20 6f 6e 72 65 6d 6f 74 65 2e 72 69 HERE onremote.ri
1030: 64 3d 64 65 6c 74 61 2e 72 69 64 29 22 0a 20 20 d=delta.rid)".
1040: 20 20 22 20 20 20 20 41 4e 44 20 45 58 49 53 54 " AND EXIST
1050: 53 28 53 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 S(SELECT 1 FROM
1060: 70 65 6e 64 69 6e 67 20 57 48 45 52 45 20 64 65 pending WHERE de
1070: 6c 74 61 2e 73 72 63 69 64 3d 70 65 6e 64 69 6e lta.srcid=pendin
1080: 67 2e 72 69 64 29 3b 22 0a 20 20 20 20 22 49 4e g.rid);". "IN
1090: 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 SERT OR IGNORE I
10a0: 4e 54 4f 20 70 72 69 6f 72 69 74 79 22 0a 20 20 NTO priority".
10b0: 20 20 22 20 53 45 4c 45 43 54 20 72 69 64 20 46 " SELECT rid F
10c0: 52 4f 4d 20 64 65 6c 74 61 22 0a 20 20 20 20 22 ROM delta". "
10d0: 20 20 57 48 45 52 45 20 45 58 49 53 54 53 28 53 WHERE EXISTS(S
10e0: 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 6f 6e 72 ELECT 1 FROM onr
10f0: 65 6d 6f 74 65 20 57 48 45 52 45 20 6f 6e 72 65 emote WHERE onre
1100: 6d 6f 74 65 2e 72 69 64 3d 64 65 6c 74 61 2e 73 mote.rid=delta.s
1110: 72 63 69 64 29 22 0a 20 20 20 20 22 20 20 20 20 rcid)". "
1120: 41 4e 44 20 45 58 49 53 54 53 28 53 45 4c 45 43 AND EXISTS(SELEC
1130: 54 20 31 20 46 52 4f 4d 20 70 65 6e 64 69 6e 67 T 1 FROM pending
1140: 20 57 48 45 52 45 20 64 65 6c 74 61 2e 72 69 64 WHERE delta.rid
1150: 3d 70 65 6e 64 69 6e 67 2e 72 69 64 29 3b 22 0a =pending.rid);".
1160: 20 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 73 65 );. while( se
1170: 6e 74 3c 6d 61 78 53 69 7a 65 20 26 26 20 28 72 nt<maxSize && (r
1180: 69 64 20 3d 20 64 62 5f 69 6e 74 28 30 2c 20 22 id = db_int(0, "
1190: 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f 4d 20 SELECT rid FROM
11a0: 70 72 69 6f 72 69 74 79 22 29 29 21 3d 30 20 29 priority"))!=0 )
11b0: 7b 0a 20 20 20 20 73 65 6e 74 20 2b 3d 20 73 65 {. sent += se
11c0: 6e 64 5f 66 69 6c 65 28 72 69 64 2c 20 70 4f 75 nd_file(rid, pOu
11d0: 74 29 3b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 t);. db_multi
11e0: 5f 65 78 65 63 28 0a 20 20 20 20 20 20 22 49 4e _exec(. "IN
11f0: 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 SERT OR IGNORE I
1200: 4e 54 4f 20 70 72 69 6f 72 69 74 79 22 0a 20 20 NTO priority".
1210: 20 20 20 20 22 20 53 45 4c 45 43 54 20 73 72 63 " SELECT src
1220: 69 64 20 46 52 4f 4d 20 64 65 6c 74 61 20 57 48 id FROM delta WH
1230: 45 52 45 20 72 69 64 3d 25 64 22 0a 20 20 20 20 ERE rid=%d".
1240: 20 20 22 20 55 4e 49 4f 4e 20 41 4c 4c 22 0a 20 " UNION ALL".
1250: 20 20 20 20 20 22 20 53 45 4c 45 43 54 20 72 69 " SELECT ri
1260: 64 20 46 52 4f 4d 20 64 65 6c 74 61 20 57 48 45 d FROM delta WHE
1270: 52 45 20 73 72 63 69 64 3d 25 64 22 2c 0a 20 20 RE srcid=%d",.
1280: 20 20 20 20 72 69 64 2c 20 72 69 64 0a 20 20 20 rid, rid.
1290: 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 );. }.#endif.
12a0: 20 64 62 5f 70 72 65 70 61 72 65 28 26 71 2c 20 db_prepare(&q,
12b0: 22 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f 4d "SELECT rid FROM
12c0: 20 70 65 6e 64 69 6e 67 22 29 3b 0a 20 20 77 68 pending");. wh
12d0: 69 6c 65 28 20 64 62 5f 73 74 65 70 28 26 71 29 ile( db_step(&q)
12e0: 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a ==SQLITE_ROW ){.
12f0: 20 20 20 20 69 6e 74 20 72 69 64 20 3d 20 64 62 int rid = db
1300: 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 _column_int(&q,
1310: 30 29 3b 0a 20 20 20 20 69 66 28 20 73 65 6e 74 0);. if( sent
1320: 3c 6d 61 78 53 69 7a 65 20 29 7b 0a 20 20 20 20 <maxSize ){.
1330: 20 20 73 65 6e 74 20 2b 3d 20 73 65 6e 64 5f 66 sent += send_f
1340: 69 6c 65 28 72 69 64 2c 20 70 4f 75 74 29 3b 0a ile(rid, pOut);.
1350: 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1360: 20 63 68 61 72 20 2a 7a 55 75 69 64 20 3d 20 64 char *zUuid = d
1370: 62 5f 74 65 78 74 28 30 2c 0a 20 20 20 20 20 20 b_text(0,.
1380: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1390: 22 53 45 4c 45 43 54 20 75 75 69 64 20 46 52 4f "SELECT uuid FRO
13a0: 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 72 69 64 M blob WHERE rid
13b0: 3d 25 64 20 41 4e 44 20 73 69 7a 65 3e 3d 30 22 =%d AND size>=0"
13c0: 2c 20 72 69 64 29 3b 0a 20 20 20 20 20 20 69 66 , rid);. if
13d0: 28 20 7a 55 75 69 64 20 29 7b 0a 20 20 20 20 20 ( zUuid ){.
13e0: 20 20 20 69 66 28 20 70 4f 75 74 20 29 7b 0a 20 if( pOut ){.
13f0: 20 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 61 70 blob_ap
1400: 70 65 6e 64 66 28 70 4f 75 74 2c 20 22 69 67 6f pendf(pOut, "igo
1410: 74 20 25 73 5c 6e 22 2c 20 7a 55 75 69 64 29 3b t %s\n", zUuid);
1420: 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
1430: 20 20 20 20 20 20 20 20 20 20 63 67 69 5f 70 72 cgi_pr
1440: 69 6e 74 66 28 22 69 67 6f 74 20 25 73 5c 6e 22 intf("igot %s\n"
1450: 2c 20 7a 55 75 69 64 29 3b 0a 20 20 20 20 20 20 , zUuid);.
1460: 20 20 7d 0a 20 20 20 20 20 20 20 20 66 72 65 65 }. free
1470: 28 7a 55 75 69 64 29 3b 0a 20 20 20 20 20 20 7d (zUuid);. }
1480: 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 64 62 5f . }. }. db_
1490: 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 20 20 finalize(&q);.
14a0: 0a 23 69 66 20 30 0a 20 20 64 62 5f 6d 75 6c 74 .#if 0. db_mult
14b0: 69 5f 65 78 65 63 28 22 44 52 4f 50 20 54 41 42 i_exec("DROP TAB
14c0: 4c 45 20 70 72 69 6f 72 69 74 79 22 29 3b 0a 23 LE priority");.#
14d0: 65 6e 64 69 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 endif.}.../*.**
14e0: 43 68 65 63 6b 20 74 68 65 20 73 69 67 6e 61 74 Check the signat
14f0: 75 72 65 20 6f 6e 20 61 6e 20 61 70 70 6c 69 63 ure on an applic
1500: 61 74 69 6f 6e 2f 78 2d 66 6f 73 73 69 6c 20 70 ation/x-fossil p
1510: 61 79 6c 6f 61 64 20 72 65 63 65 69 76 65 64 20 ayload received
1520: 62 79 0a 2a 2a 20 74 68 65 20 48 54 54 50 20 73 by.** the HTTP s
1530: 65 72 76 65 72 2e 20 20 54 68 65 20 73 69 67 6e erver. The sign
1540: 61 74 75 72 65 20 69 73 20 61 20 6c 69 6e 65 20 ature is a line
1550: 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
1560: 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 form:.**.**
1570: 20 20 20 20 6c 6f 67 69 6e 20 4c 4f 47 49 4e 20 login LOGIN
1580: 4e 4f 4e 43 45 20 53 49 47 4e 41 54 55 52 45 0a NONCE SIGNATURE.
1590: 2a 2a 0a 2a 2a 20 54 68 65 20 4e 4f 4e 43 45 20 **.** The NONCE
15a0: 69 73 20 61 20 72 61 6e 64 6f 6d 20 73 74 72 69 is a random stri
15b0: 6e 67 2e 20 20 54 68 65 20 73 65 72 76 65 72 20 ng. The server
15c0: 77 69 6c 6c 20 6e 65 76 65 72 20 61 63 63 65 70 will never accep
15d0: 74 20 61 0a 2a 2a 20 72 65 70 65 61 74 20 4e 4f t a.** repeat NO
15e0: 4e 43 45 2e 20 20 53 49 47 4e 41 54 55 52 45 20 NCE. SIGNATURE
15f0: 69 73 20 74 68 65 20 53 48 41 31 20 63 68 65 63 is the SHA1 chec
1600: 6b 73 75 6d 20 6f 66 20 74 68 65 20 4e 4f 4e 43 ksum of the NONC
1610: 45 20 0a 2a 2a 20 63 6f 6e 63 61 74 65 6e 61 74 E .** concatenat
1620: 65 64 20 77 69 74 68 20 74 68 65 20 75 73 65 72 ed with the user
1630: 73 20 70 61 73 73 77 6f 72 64 2e 0a 2a 2a 0a 2a s password..**.*
1640: 2a 20 54 68 65 20 70 61 72 61 6d 65 74 65 72 73 * The parameters
1650: 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 to this routine
1660: 20 61 72 65 20 65 70 68 65 72 6d 65 72 61 6c 20 are ephermeral
1670: 62 6c 6f 62 73 20 68 6f 6c 64 69 6e 67 20 74 68 blobs holding th
1680: 65 0a 2a 2a 20 4c 4f 47 49 4e 2c 20 4e 4f 4e 43 e.** LOGIN, NONC
1690: 45 20 61 6e 64 20 53 49 47 4e 41 54 55 52 45 2e E and SIGNATURE.
16a0: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
16b0: 69 6e 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 ine attempts to
16c0: 6c 6f 63 61 74 65 20 74 68 65 20 75 73 65 72 20 locate the user
16d0: 61 6e 64 20 76 65 72 69 66 79 20 74 68 65 20 73 and verify the s
16e0: 69 67 6e 61 74 75 72 65 2e 0a 2a 2a 20 49 66 20 ignature..** If
16f0: 65 76 65 72 79 74 68 69 6e 67 20 63 68 65 63 6b everything check
1700: 73 20 6f 75 74 2c 20 74 68 65 20 55 53 45 52 2e s out, the USER.
1710: 43 41 50 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 74 CAP column for t
1720: 68 65 20 55 53 45 52 20 74 61 62 6c 65 0a 2a 2a he USER table.**
1730: 20 69 73 20 63 6f 6e 73 75 6c 74 65 64 20 74 6f is consulted to
1740: 20 73 65 74 20 70 72 69 76 69 6c 65 67 65 73 20 set privileges
1750: 69 6e 20 74 68 65 20 67 6c 6f 62 61 6c 20 67 20 in the global g
1760: 76 61 72 69 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 variable..**.**
1770: 49 66 20 61 6e 79 74 68 69 6e 67 20 66 61 69 6c If anything fail
1780: 73 20 74 6f 20 63 68 65 63 6b 20 6f 75 74 2c 20 s to check out,
1790: 6e 6f 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d no changes are m
17a0: 61 64 65 20 74 6f 20 70 72 69 76 69 6c 65 67 65 ade to privilege
17b0: 73 2e 0a 2a 2a 0a 2a 2a 20 53 69 67 6e 61 74 75 s..**.** Signatu
17c0: 72 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 6e re generation on
17d0: 20 74 68 65 20 63 6c 69 65 6e 74 20 73 69 64 65 the client side
17e0: 20 69 73 20 68 61 6e 64 6c 65 64 20 62 79 20 74 is handled by t
17f0: 68 65 20 0a 2a 2a 20 68 74 74 70 5f 65 78 63 68 he .** http_exch
1800: 61 6e 67 65 28 29 20 72 6f 75 74 69 6e 65 2e 0a ange() routine..
1810: 2a 2f 0a 76 6f 69 64 20 63 68 65 63 6b 5f 6c 6f */.void check_lo
1820: 67 69 6e 28 42 6c 6f 62 20 2a 70 4c 6f 67 69 6e gin(Blob *pLogin
1830: 2c 20 42 6c 6f 62 20 2a 70 4e 6f 6e 63 65 2c 20 , Blob *pNonce,
1840: 42 6c 6f 62 20 2a 70 53 69 67 29 7b 0a 20 20 53 Blob *pSig){. S
1850: 74 6d 74 20 71 3b 0a 20 20 69 6e 74 20 72 63 3b tmt q;. int rc;
1860: 0a 0a 20 20 69 66 28 20 64 62 5f 65 78 69 73 74 .. if( db_exist
1870: 73 28 22 53 45 4c 45 43 54 20 31 20 46 52 4f 4d s("SELECT 1 FROM
1880: 20 72 63 76 66 72 6f 6d 20 57 48 45 52 45 20 6e rcvfrom WHERE n
1890: 6f 6e 63 65 3d 25 42 22 2c 20 70 4e 6f 6e 63 65 once=%B", pNonce
18a0: 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b ) ){. return;
18b0: 20 20 2f 2a 20 4e 65 76 65 72 20 61 63 63 65 70 /* Never accep
18c0: 74 20 61 20 72 65 70 65 61 74 65 64 20 6e 6f 6e t a repeated non
18d0: 63 65 20 2a 2f 0a 20 20 7d 0a 20 20 64 62 5f 70 ce */. }. db_p
18e0: 72 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c 45 repare(&q, "SELE
18f0: 43 54 20 70 77 2c 20 63 61 70 2c 20 75 69 64 20 CT pw, cap, uid
1900: 46 52 4f 4d 20 75 73 65 72 20 57 48 45 52 45 20 FROM user WHERE
1910: 6c 6f 67 69 6e 3d 25 42 22 2c 20 70 4c 6f 67 69 login=%B", pLogi
1920: 6e 29 3b 0a 20 20 69 66 28 20 64 62 5f 73 74 65 n);. if( db_ste
1930: 70 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f p(&q)==SQLITE_RO
1940: 57 20 29 7b 0a 20 20 20 20 42 6c 6f 62 20 70 77 W ){. Blob pw
1950: 2c 20 63 6f 6d 62 69 6e 65 64 2c 20 68 61 73 68 , combined, hash
1960: 3b 0a 20 20 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 ;. blob_zero(
1970: 26 70 77 29 3b 0a 20 20 20 20 64 62 5f 65 70 68 &pw);. db_eph
1980: 65 6d 65 72 61 6c 5f 62 6c 6f 62 28 26 71 2c 20 emeral_blob(&q,
1990: 30 2c 20 26 70 77 29 3b 0a 20 20 20 20 62 6c 6f 0, &pw);. blo
19a0: 62 5f 7a 65 72 6f 28 26 63 6f 6d 62 69 6e 65 64 b_zero(&combined
19b0: 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 63 6f 70 79 );. blob_copy
19c0: 28 26 63 6f 6d 62 69 6e 65 64 2c 20 70 4e 6f 6e (&combined, pNon
19d0: 63 65 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 ce);. blob_ap
19e0: 70 65 6e 64 28 26 63 6f 6d 62 69 6e 65 64 2c 20 pend(&combined,
19f0: 62 6c 6f 62 5f 62 75 66 66 65 72 28 26 70 77 29 blob_buffer(&pw)
1a00: 2c 20 62 6c 6f 62 5f 73 69 7a 65 28 26 70 77 29 , blob_size(&pw)
1a10: 29 3b 0a 20 20 20 20 73 68 61 31 73 75 6d 5f 62 );. sha1sum_b
1a20: 6c 6f 62 28 26 63 6f 6d 62 69 6e 65 64 2c 20 26 lob(&combined, &
1a30: 68 61 73 68 29 3b 0a 20 20 20 20 72 63 20 3d 20 hash);. rc =
1a40: 62 6c 6f 62 5f 63 6f 6d 70 61 72 65 28 26 68 61 blob_compare(&ha
1a50: 73 68 2c 20 70 53 69 67 29 3b 0a 20 20 20 20 62 sh, pSig);. b
1a60: 6c 6f 62 5f 72 65 73 65 74 28 26 68 61 73 68 29 lob_reset(&hash)
1a70: 3b 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 ;. blob_reset
1a80: 28 26 63 6f 6d 62 69 6e 65 64 29 3b 0a 20 20 20 (&combined);.
1a90: 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 if( rc ){.
1aa0: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 61 const char *zCa
1ab0: 70 3b 0a 20 20 20 20 20 20 7a 43 61 70 20 3d 20 p;. zCap =
1ac0: 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 26 db_column_text(&
1ad0: 71 2c 20 31 29 3b 0a 20 20 20 20 20 20 6c 6f 67 q, 1);. log
1ae0: 69 6e 5f 73 65 74 5f 63 61 70 61 62 69 6c 69 74 in_set_capabilit
1af0: 69 65 73 28 7a 43 61 70 29 3b 0a 20 20 20 20 20 ies(zCap);.
1b00: 20 67 2e 75 73 65 72 55 69 64 20 3d 20 64 62 5f g.userUid = db_
1b10: 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 32 column_int(&q, 2
1b20: 29 3b 0a 20 20 20 20 20 20 67 2e 7a 4c 6f 67 69 );. g.zLogi
1b30: 6e 20 3d 20 6d 70 72 69 6e 74 66 28 22 25 62 22 n = mprintf("%b"
1b40: 2c 20 70 4c 6f 67 69 6e 29 3b 0a 20 20 20 20 20 , pLogin);.
1b50: 20 67 2e 7a 4e 6f 6e 63 65 20 3d 20 6d 70 72 69 g.zNonce = mpri
1b60: 6e 74 66 28 22 25 62 22 2c 20 70 4e 6f 6e 63 65 ntf("%b", pNonce
1b70: 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 64 );. }. }. d
1b80: 62 5f 72 65 73 65 74 28 26 71 29 3b 0a 7d 0a 0a b_reset(&q);.}..
1b90: 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 76 ./*.** If this v
1ba0: 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 2c 20 ariable is set,
1bb0: 64 69 73 61 62 6c 65 20 6c 6f 67 69 6e 20 63 68 disable login ch
1bc0: 65 63 6b 73 2e 20 20 55 73 65 64 20 66 6f 72 20 ecks. Used for
1bd0: 64 65 62 75 67 67 69 6e 67 0a 2a 2a 20 6f 6e 6c debugging.** onl
1be0: 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 y..*/.static int
1bf0: 20 64 69 73 61 62 6c 65 4c 6f 67 69 6e 20 3d 20 disableLogin =
1c00: 30 3b 0a 0a 2f 2a 0a 2a 2a 20 57 45 42 50 41 47 0;../*.** WEBPAG
1c10: 45 3a 20 78 66 65 72 0a 2a 2a 0a 2a 2a 20 54 68 E: xfer.**.** Th
1c20: 69 73 20 69 73 20 74 68 65 20 74 72 61 6e 73 66 is is the transf
1c30: 65 72 20 68 61 6e 64 6c 65 72 20 6f 6e 20 74 68 er handler on th
1c40: 65 20 73 65 72 76 65 72 20 73 69 64 65 2e 20 20 e server side.
1c50: 54 68 65 20 74 72 61 6e 73 66 65 72 0a 2a 2a 20 The transfer.**
1c60: 6d 65 73 73 61 67 65 20 68 61 73 20 62 65 65 6e message has been
1c70: 20 75 6e 63 6f 6d 70 72 65 73 73 65 64 20 61 6e uncompressed an
1c80: 64 20 70 6c 61 63 65 64 20 69 6e 20 74 68 65 20 d placed in the
1c90: 67 2e 63 67 69 49 6e 20 62 6c 6f 62 2e 0a 2a 2a g.cgiIn blob..**
1ca0: 20 50 72 6f 63 65 73 73 20 74 68 69 73 20 6d 65 Process this me
1cb0: 73 73 61 67 65 20 61 6e 64 20 66 6f 72 6d 20 61 ssage and form a
1cc0: 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 72 65 n appropriate re
1cd0: 70 6c 79 2e 0a 2a 2f 0a 76 6f 69 64 20 70 61 67 ply..*/.void pag
1ce0: 65 5f 78 66 65 72 28 76 6f 69 64 29 7b 0a 20 20 e_xfer(void){.
1cf0: 69 6e 74 20 6e 54 6f 6b 65 6e 3b 0a 20 20 69 6e int nToken;. in
1d00: 74 20 69 73 50 75 6c 6c 20 3d 20 30 3b 0a 20 20 t isPull = 0;.
1d10: 69 6e 74 20 69 73 50 75 73 68 20 3d 20 30 3b 0a int isPush = 0;.
1d20: 20 20 69 6e 74 20 6e 45 72 72 20 3d 20 30 3b 0a int nErr = 0;.
1d30: 20 20 42 6c 6f 62 20 6c 69 6e 65 2c 20 65 72 72 Blob line, err
1d40: 6d 73 67 2c 20 61 54 6f 6b 65 6e 5b 35 5d 3b 0a msg, aToken[5];.
1d50: 0a 20 20 64 62 5f 62 65 67 69 6e 5f 74 72 61 6e . db_begin_tran
1d60: 73 61 63 74 69 6f 6e 28 29 3b 0a 20 20 62 6c 6f saction();. blo
1d70: 62 61 72 72 61 79 5f 7a 65 72 6f 28 61 54 6f 6b barray_zero(aTok
1d80: 65 6e 2c 20 63 6f 75 6e 74 28 61 54 6f 6b 65 6e en, count(aToken
1d90: 29 29 3b 0a 20 20 63 67 69 5f 73 65 74 5f 63 6f ));. cgi_set_co
1da0: 6e 74 65 6e 74 5f 74 79 70 65 28 67 2e 7a 43 6f ntent_type(g.zCo
1db0: 6e 74 65 6e 74 54 79 70 65 29 3b 0a 20 20 62 6c ntentType);. bl
1dc0: 6f 62 5f 7a 65 72 6f 28 26 65 72 72 6d 73 67 29 ob_zero(&errmsg)
1dd0: 3b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 ;. db_multi_exe
1de0: 63 28 0a 20 20 20 20 20 22 43 52 45 41 54 45 20 c(. "CREATE
1df0: 54 45 4d 50 20 54 41 42 4c 45 20 6f 6e 72 65 6d TEMP TABLE onrem
1e00: 6f 74 65 28 72 69 64 20 49 4e 54 45 47 45 52 20 ote(rid INTEGER
1e10: 50 52 49 4d 41 52 59 20 4b 45 59 29 3b 22 20 2f PRIMARY KEY);" /
1e20: 2a 20 43 6c 69 65 6e 74 20 68 61 73 20 2a 2f 0a * Client has */.
1e30: 20 20 20 20 20 22 43 52 45 41 54 45 20 54 45 4d "CREATE TEM
1e40: 50 20 54 41 42 4c 45 20 70 65 6e 64 69 6e 67 28 P TABLE pending(
1e50: 72 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d rid INTEGER PRIM
1e60: 41 52 59 20 4b 45 59 29 3b 22 20 20 2f 2a 20 43 ARY KEY);" /* C
1e70: 6c 69 65 6e 74 20 6e 65 65 64 73 20 2a 2f 0a 20 lient needs */.
1e80: 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 62 6c 6f );. while( blo
1e90: 62 5f 6c 69 6e 65 28 26 67 2e 63 67 69 49 6e 2c b_line(&g.cgiIn,
1ea0: 20 26 6c 69 6e 65 29 20 29 7b 0a 20 20 20 20 6e &line) ){. n
1eb0: 54 6f 6b 65 6e 20 3d 20 62 6c 6f 62 5f 74 6f 6b Token = blob_tok
1ec0: 65 6e 69 7a 65 28 26 6c 69 6e 65 2c 20 61 54 6f enize(&line, aTo
1ed0: 6b 65 6e 2c 20 63 6f 75 6e 74 28 61 54 6f 6b 65 ken, count(aToke
1ee0: 6e 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 20 20 66 n));.. /* f
1ef0: 69 6c 65 20 55 55 49 44 20 53 49 5a 45 20 5c 6e ile UUID SIZE \n
1f00: 20 43 4f 4e 54 45 4e 54 0a 20 20 20 20 2a 2a 20 CONTENT. **
1f10: 20 20 66 69 6c 65 20 55 55 49 44 20 44 45 4c 54 file UUID DELT
1f20: 41 53 52 43 20 53 49 5a 45 20 5c 6e 20 43 4f 4e ASRC SIZE \n CON
1f30: 54 45 4e 54 0a 20 20 20 20 2a 2a 0a 20 20 20 20 TENT. **.
1f40: 2a 2a 20 41 63 63 65 70 74 20 61 20 66 69 6c 65 ** Accept a file
1f50: 20 66 72 6f 6d 20 74 68 65 20 63 6c 69 65 6e 74 from the client
1f60: 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
1f70: 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e blob_eq(&aToken
1f80: 5b 30 5d 2c 20 22 66 69 6c 65 22 29 20 26 26 20 [0], "file") &&
1f90: 6e 54 6f 6b 65 6e 3e 3d 32 20 26 26 20 6e 54 6f nToken>=2 && nTo
1fa0: 6b 65 6e 3c 3d 33 20 29 7b 0a 20 20 20 20 20 20 ken<=3 ){.
1fb0: 69 66 28 20 21 69 73 50 75 73 68 20 29 7b 0a 20 if( !isPush ){.
1fc0: 20 20 20 20 20 20 20 63 67 69 5f 72 65 73 65 74 cgi_reset
1fd0: 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 20 20 _content();.
1fe0: 20 20 20 20 40 20 65 72 72 6f 72 20 6e 6f 74 5c @ error not\
1ff0: 73 61 75 74 68 6f 72 69 7a 65 64 5c 73 74 6f 5c sauthorized\sto\
2000: 73 77 72 69 74 65 0a 20 20 20 20 20 20 20 20 6e swrite. n
2010: 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 62 Err++;. b
2020: 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
2030: 20 20 20 20 78 66 65 72 5f 61 63 63 65 70 74 5f xfer_accept_
2040: 66 69 6c 65 28 26 67 2e 63 67 69 49 6e 2c 20 61 file(&g.cgiIn, a
2050: 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 2c 20 26 Token, nToken, &
2060: 65 72 72 6d 73 67 29 3b 0a 20 20 20 20 20 20 69 errmsg);. i
2070: 66 28 20 62 6c 6f 62 5f 73 69 7a 65 28 26 65 72 f( blob_size(&er
2080: 72 6d 73 67 29 20 29 7b 0a 20 20 20 20 20 20 20 rmsg) ){.
2090: 20 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e 74 65 cgi_reset_conte
20a0: 6e 74 28 29 3b 0a 20 20 20 20 20 20 20 20 40 20 nt();. @
20b0: 65 72 72 6f 72 20 25 54 28 62 6c 6f 62 5f 73 74 error %T(blob_st
20c0: 72 28 26 65 72 72 6d 73 67 29 29 0a 20 20 20 20 r(&errmsg)).
20d0: 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 nErr++;.
20e0: 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
20f0: 20 7d 0a 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 }. }else..
2100: 20 20 2f 2a 20 20 20 67 69 6d 6d 65 20 55 55 49 /* gimme UUI
2110: 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 D. **. **
2120: 43 6c 69 65 6e 74 20 69 73 20 72 65 71 75 65 73 Client is reques
2130: 74 69 6e 67 20 61 20 66 69 6c 65 0a 20 20 20 20 ting a file.
2140: 2a 2f 0a 20 20 20 20 69 66 28 20 62 6c 6f 62 5f */. if( blob_
2150: 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 eq(&aToken[0], "
2160: 67 69 6d 6d 65 22 29 20 26 26 20 6e 54 6f 6b 65 gimme") && nToke
2170: 6e 3d 3d 32 20 26 26 20 62 6c 6f 62 5f 69 73 5f n==2 && blob_is_
2180: 75 75 69 64 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 uuid(&aToken[1])
2190: 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73 ){. if( is
21a0: 50 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 Pull ){.
21b0: 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 db_multi_exec(.
21c0: 20 20 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 "INSERT
21d0: 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 OR IGNORE INTO
21e0: 70 65 6e 64 69 6e 67 28 72 69 64 29 20 22 0a 20 pending(rid) ".
21f0: 20 20 20 20 20 20 20 20 20 22 53 45 4c 45 43 54 "SELECT
2200: 20 72 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 rid FROM blob W
2210: 48 45 52 45 20 75 75 69 64 3d 25 42 20 41 4e 44 HERE uuid=%B AND
2220: 20 73 69 7a 65 3e 3d 30 22 2c 20 26 61 54 6f 6b size>=0", &aTok
2230: 65 6e 5b 31 5d 0a 20 20 20 20 20 20 20 20 29 3b en[1]. );
2240: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
2250: 73 65 0a 0a 20 20 20 20 2f 2a 20 20 20 69 67 6f se.. /* igo
2260: 74 20 55 55 49 44 0a 20 20 20 20 2a 2a 20 20 20 t UUID. **
2270: 6c 65 61 66 20 55 55 49 44 0a 20 20 20 20 2a 2a leaf UUID. **
2280: 0a 20 20 20 20 2a 2a 20 43 6c 69 65 6e 74 20 61 . ** Client a
2290: 6e 6e 6f 75 6e 63 65 73 20 74 68 61 74 20 69 74 nnounces that it
22a0: 20 68 61 73 20 61 20 70 61 72 74 69 63 75 6c 61 has a particula
22b0: 72 20 66 69 6c 65 0a 20 20 20 20 2a 2f 0a 20 20 r file. */.
22c0: 20 20 69 66 28 20 6e 54 6f 6b 65 6e 3d 3d 32 0a if( nToken==2.
22d0: 20 20 20 20 20 20 20 20 20 20 26 26 20 28 62 6c && (bl
22e0: 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d ob_eq(&aToken[0]
22f0: 2c 20 22 69 67 6f 74 22 29 20 7c 7c 20 62 6c 6f , "igot") || blo
2300: 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c b_eq(&aToken[0],
2310: 22 6c 65 61 66 22 29 29 0a 20 20 20 20 20 20 20 "leaf")).
2320: 20 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75 75 && blob_is_uu
2330: 69 64 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 20 29 id(&aToken[1]) )
2340: 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73 50 75 {. if( isPu
2350: 6c 6c 20 7c 7c 20 69 73 50 75 73 68 20 29 7b 0a ll || isPush ){.
2360: 20 20 20 20 20 20 20 20 69 6e 74 20 72 69 64 20 int rid
2370: 3d 20 64 62 5f 69 6e 74 28 30 2c 20 22 53 45 4c = db_int(0, "SEL
2380: 45 43 54 20 72 69 64 20 46 52 4f 4d 20 62 6c 6f ECT rid FROM blo
2390: 62 20 57 48 45 52 45 20 75 75 69 64 3d 25 42 22 b WHERE uuid=%B"
23a0: 2c 20 26 61 54 6f 6b 65 6e 5b 31 5d 29 3b 0a 20 , &aToken[1]);.
23b0: 20 20 20 20 20 20 20 69 66 28 20 72 69 64 3e 30 if( rid>0
23c0: 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 ){. db
23d0: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 _multi_exec(.
23e0: 20 20 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 "INSERT
23f0: 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 OR IGNORE INTO
2400: 6f 6e 72 65 6d 6f 74 65 28 72 69 64 29 20 56 41 onremote(rid) VA
2410: 4c 55 45 53 28 25 64 29 22 2c 20 72 69 64 0a 20 LUES(%d)", rid.
2420: 20 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 );.
2430: 20 20 20 20 20 20 69 66 28 20 69 73 50 75 6c 6c if( isPull
2440: 20 26 26 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f && blob_eq(&aTo
2450: 6b 65 6e 5b 30 5d 2c 20 22 6c 65 61 66 22 29 20 ken[0], "leaf")
2460: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 ){. d
2470: 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 b_multi_exec(.
2480: 20 20 20 20 20 20 20 20 20 20 20 20 22 49 4e 53 "INS
2490: 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e ERT OR IGNORE IN
24a0: 54 4f 20 70 65 6e 64 69 6e 67 28 72 69 64 29 20 TO pending(rid)
24b0: 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ".
24c0: 22 53 45 4c 45 43 54 20 63 69 64 20 46 52 4f 4d "SELECT cid FROM
24d0: 20 70 6c 69 6e 6b 20 57 48 45 52 45 20 70 69 64 plink WHERE pid
24e0: 3d 25 64 22 2c 20 72 69 64 0a 20 20 20 20 20 20 =%d", rid.
24f0: 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 );.
2500: 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c }. }el
2510: 73 65 20 69 66 28 20 69 73 50 75 73 68 20 29 7b se if( isPush ){
2520: 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 65 . conte
2530: 6e 74 5f 70 75 74 28 30 2c 20 62 6c 6f 62 5f 73 nt_put(0, blob_s
2540: 74 72 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 29 3b tr(&aToken[1]));
2550: 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
2560: 20 7d 0a 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 }. }else..
2570: 20 20 2f 2a 20 20 20 20 70 75 6c 6c 20 20 53 45 /* pull SE
2580: 52 56 45 52 43 4f 44 45 20 20 50 52 4f 4a 45 43 RVERCODE PROJEC
2590: 54 43 4f 44 45 0a 20 20 20 20 2a 2a 20 20 20 20 TCODE. **
25a0: 70 75 73 68 20 20 53 45 52 56 45 52 43 4f 44 45 push SERVERCODE
25b0: 20 20 50 52 4f 4a 45 43 54 43 4f 44 45 0a 20 20 PROJECTCODE.
25c0: 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 **. ** The
25d0: 63 6c 69 65 6e 74 20 77 61 6e 74 73 20 65 69 74 client wants eit
25e0: 68 65 72 20 73 65 6e 64 20 6f 72 20 72 65 63 65 her send or rece
25f0: 69 76 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ive. */. i
2600: 66 28 20 6e 54 6f 6b 65 6e 3d 3d 33 0a 20 20 20 f( nToken==3.
2610: 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 && (
2620: 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b blob_eq(&aToken[
2630: 30 5d 2c 20 22 70 75 6c 6c 22 29 20 7c 7c 20 62 0], "pull") || b
2640: 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 lob_eq(&aToken[0
2650: 5d 2c 20 22 70 75 73 68 22 29 29 0a 20 20 20 20 ], "push")).
2660: 20 20 20 20 20 20 20 20 20 20 20 26 26 20 62 6c && bl
2670: 6f 62 5f 69 73 5f 75 75 69 64 28 26 61 54 6f 6b ob_is_uuid(&aTok
2680: 65 6e 5b 31 5d 29 20 26 26 20 62 6c 6f 62 5f 69 en[1]) && blob_i
2690: 73 5f 75 75 69 64 28 26 61 54 6f 6b 65 6e 5b 32 s_uuid(&aToken[2
26a0: 5d 29 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 ]) ){. cons
26b0: 74 20 63 68 61 72 20 2a 7a 53 43 6f 64 65 3b 0a t char *zSCode;.
26c0: 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 const char
26d0: 20 2a 7a 50 43 6f 64 65 3b 0a 0a 20 20 20 20 20 *zPCode;..
26e0: 20 7a 53 43 6f 64 65 20 3d 20 64 62 5f 67 65 74 zSCode = db_get
26f0: 28 22 73 65 72 76 65 72 2d 63 6f 64 65 22 2c 20 ("server-code",
2700: 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 53 0);. if( zS
2710: 43 6f 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 Code==0 ){.
2720: 20 20 20 66 6f 73 73 69 6c 5f 70 61 6e 69 63 28 fossil_panic(
2730: 22 6d 69 73 73 69 6e 67 20 73 65 72 76 65 72 20 "missing server
2740: 63 6f 64 65 22 29 3b 0a 20 20 20 20 20 20 7d 0a code");. }.
2750: 20 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 if( blob_e
2760: 71 5f 73 74 72 28 26 61 54 6f 6b 65 6e 5b 31 5d q_str(&aToken[1]
2770: 2c 20 7a 53 43 6f 64 65 2c 20 2d 31 29 20 29 7b , zSCode, -1) ){
2780: 0a 20 20 20 20 20 20 20 20 63 67 69 5f 72 65 73 . cgi_res
2790: 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 et_content();.
27a0: 20 20 20 20 20 20 40 20 65 72 72 6f 72 20 73 65 @ error se
27b0: 72 76 65 72 5c 73 6c 6f 6f 70 0a 20 20 20 20 20 rver\sloop.
27c0: 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 nErr++;.
27d0: 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
27e0: 7d 0a 20 20 20 20 20 20 7a 50 43 6f 64 65 20 3d }. zPCode =
27f0: 20 64 62 5f 67 65 74 28 22 70 72 6f 6a 65 63 74 db_get("project
2800: 2d 63 6f 64 65 22 2c 20 30 29 3b 0a 20 20 20 20 -code", 0);.
2810: 20 20 69 66 28 20 7a 50 43 6f 64 65 3d 3d 30 20 if( zPCode==0
2820: 29 7b 0a 20 20 20 20 20 20 20 20 66 6f 73 73 69 ){. fossi
2830: 6c 5f 70 61 6e 69 63 28 22 6d 69 73 73 69 6e 67 l_panic("missing
2840: 20 70 72 6f 6a 65 63 74 20 63 6f 64 65 22 29 3b project code");
2850: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
2860: 66 28 20 21 62 6c 6f 62 5f 65 71 5f 73 74 72 28 f( !blob_eq_str(
2870: 26 61 54 6f 6b 65 6e 5b 32 5d 2c 20 7a 50 43 6f &aToken[2], zPCo
2880: 64 65 2c 20 2d 31 29 20 29 7b 0a 20 20 20 20 20 de, -1) ){.
2890: 20 20 20 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e cgi_reset_con
28a0: 74 65 6e 74 28 29 3b 0a 20 20 20 20 20 20 20 20 tent();.
28b0: 40 20 65 72 72 6f 72 20 77 72 6f 6e 67 5c 73 70 @ error wrong\sp
28c0: 72 6f 6a 65 63 74 0a 20 20 20 20 20 20 20 20 6e roject. n
28d0: 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 62 Err++;. b
28e0: 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
28f0: 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 if( blob_eq(
2900: 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 70 75 6c &aToken[0], "pul
2910: 6c 22 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 l") ){. i
2920: 66 28 20 21 67 2e 6f 6b 52 65 61 64 20 29 7b 0a f( !g.okRead ){.
2930: 20 20 20 20 20 20 20 20 20 20 63 67 69 5f 72 65 cgi_re
2940: 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 set_content();.
2950: 20 20 20 20 20 20 20 20 20 40 20 65 72 72 6f 72 @ error
2960: 20 6e 6f 74 5c 73 61 75 74 68 6f 72 69 7a 65 64 not\sauthorized
2970: 5c 73 74 6f 5c 73 72 65 61 64 0a 20 20 20 20 20 \sto\sread.
2980: 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 nErr++;.
2990: 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
29a0: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
29b0: 69 73 50 75 6c 6c 20 3d 20 31 3b 0a 20 20 20 20 isPull = 1;.
29c0: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
29d0: 20 69 66 28 20 21 67 2e 6f 6b 57 72 69 74 65 20 if( !g.okWrite
29e0: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 67 69 ){. cgi
29f0: 5f 72 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29 _reset_content()
2a00: 3b 0a 20 20 20 20 20 20 20 20 20 20 40 20 65 72 ;. @ er
2a10: 72 6f 72 20 6e 6f 74 5c 73 61 75 74 68 6f 72 69 ror not\sauthori
2a20: 7a 65 64 5c 73 74 6f 5c 73 77 72 69 74 65 0a 20 zed\sto\swrite.
2a30: 20 20 20 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b nErr++;
2a40: 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b . break
2a50: 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
2a60: 20 20 20 20 69 73 50 75 73 68 20 3d 20 31 3b 0a isPush = 1;.
2a70: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
2a80: 73 65 0a 0a 20 20 20 20 2f 2a 20 20 20 20 63 6c se.. /* cl
2a90: 6f 6e 65 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a one. **. *
2aa0: 2a 20 54 68 65 20 63 6c 69 65 6e 74 20 6b 6e 6f * The client kno
2ab0: 77 73 20 6e 6f 74 68 69 6e 67 2e 20 20 54 65 6c ws nothing. Tel
2ac0: 6c 20 61 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 l all.. */.
2ad0: 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 61 if( blob_eq(&a
2ae0: 54 6f 6b 65 6e 5b 30 5d 2c 20 22 63 6c 6f 6e 65 Token[0], "clone
2af0: 22 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ") ){. if(
2b00: 21 67 2e 6f 6b 52 65 61 64 20 29 7b 0a 20 20 20 !g.okRead ){.
2b10: 20 20 20 20 20 63 67 69 5f 72 65 73 65 74 5f 63 cgi_reset_c
2b20: 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 20 20 20 20 ontent();.
2b30: 20 20 40 20 65 72 72 6f 72 20 6e 6f 74 5c 73 61 @ error not\sa
2b40: 75 74 68 6f 72 69 7a 65 64 5c 73 74 6f 5c 73 72 uthorized\sto\sr
2b50: 65 61 64 0a 20 20 20 20 20 20 20 20 6e 45 72 72 ead. nErr
2b60: 2b 2b 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 ++;. brea
2b70: 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 k;. }.
2b80: 20 69 73 50 75 6c 6c 20 3d 20 31 3b 0a 20 20 20 isPull = 1;.
2b90: 20 20 20 40 20 70 75 73 68 20 25 73 28 64 62 5f @ push %s(db_
2ba0: 67 65 74 28 22 73 65 72 76 65 72 2d 63 6f 64 65 get("server-code
2bb0: 22 2c 20 22 78 22 29 29 20 25 73 28 64 62 5f 67 ", "x")) %s(db_g
2bc0: 65 74 28 22 70 72 6f 6a 65 63 74 2d 63 6f 64 65 et("project-code
2bd0: 22 2c 20 22 78 22 29 29 0a 20 20 20 20 20 20 64 ", "x")). d
2be0: 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 b_multi_exec(.
2bf0: 20 20 20 20 20 20 22 49 4e 53 45 52 54 20 4f 52 "INSERT OR
2c00: 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 70 65 6e IGNORE INTO pen
2c10: 64 69 6e 67 28 72 69 64 29 20 22 0a 20 20 20 20 ding(rid) ".
2c20: 20 20 20 20 22 53 45 4c 45 43 54 20 72 69 64 20 "SELECT rid
2c30: 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 FROM blob WHERE
2c40: 73 69 7a 65 3e 3d 30 22 0a 20 20 20 20 20 20 29 size>=0". )
2c50: 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
2c60: 20 20 20 6c 6f 67 69 6e 20 20 55 53 45 52 20 20 login USER
2c70: 4e 4f 4e 43 45 20 20 53 49 47 4e 41 54 55 52 45 NONCE SIGNATURE
2c80: 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 . **. ** C
2c90: 68 65 63 6b 20 66 6f 72 20 61 20 76 61 6c 69 64 heck for a valid
2ca0: 20 6c 6f 67 69 6e 2e 20 20 54 68 69 73 20 68 61 login. This ha
2cb0: 73 20 74 6f 20 68 61 70 70 65 6e 20 62 65 66 6f s to happen befo
2cc0: 72 65 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 re anything else
2cd0: 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
2ce0: 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e blob_eq(&aToken
2cf0: 5b 30 5d 2c 20 22 6c 6f 67 69 6e 22 29 20 26 26 [0], "login") &&
2d00: 20 6e 54 6f 6b 65 6e 3d 3d 34 20 29 7b 0a 20 20 nToken==4 ){.
2d10: 20 20 20 20 69 66 28 20 64 69 73 61 62 6c 65 4c if( disableL
2d20: 6f 67 69 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 ogin ){.
2d30: 67 2e 6f 6b 52 65 61 64 20 3d 20 67 2e 6f 6b 57 g.okRead = g.okW
2d40: 72 69 74 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 rite = 1;.
2d50: 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 }else{. c
2d60: 68 65 63 6b 5f 6c 6f 67 69 6e 28 26 61 54 6f 6b heck_login(&aTok
2d70: 65 6e 5b 31 5d 2c 20 26 61 54 6f 6b 65 6e 5b 32 en[1], &aToken[2
2d80: 5d 2c 20 26 61 54 6f 6b 65 6e 5b 33 5d 29 3b 0a ], &aToken[3]);.
2d90: 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
2da0: 65 0a 0a 20 20 20 20 2f 2a 20 55 6e 6b 6e 6f 77 e.. /* Unknow
2db0: 6e 20 6d 65 73 73 61 67 65 0a 20 20 20 20 2a 2f n message. */
2dc0: 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 63 67 69 . {. cgi
2dd0: 5f 72 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29 _reset_content()
2de0: 3b 0a 20 20 20 20 20 20 40 20 65 72 72 6f 72 20 ;. @ error
2df0: 62 61 64 5c 73 63 6f 6d 6d 61 6e 64 3a 5c 73 25 bad\scommand:\s%
2e00: 46 28 62 6c 6f 62 5f 73 74 72 28 26 6c 69 6e 65 F(blob_str(&line
2e10: 29 29 0a 20 20 20 20 7d 0a 20 20 20 20 62 6c 6f )). }. blo
2e20: 62 61 72 72 61 79 5f 72 65 73 65 74 28 61 54 6f barray_reset(aTo
2e30: 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29 3b 0a 20 20 ken, nToken);.
2e40: 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 69 6e 70 75 }.. /* The inpu
2e50: 74 20 6d 65 73 73 61 67 65 20 68 61 73 20 6e 6f t message has no
2e60: 77 20 62 65 65 6e 20 70 72 6f 63 65 73 73 65 64 w been processed
2e70: 2e 20 20 47 65 6e 65 72 61 74 65 20 61 20 72 65 . Generate a re
2e80: 70 6c 79 2e 20 2a 2f 0a 20 20 69 66 28 20 69 73 ply. */. if( is
2e90: 50 75 73 68 20 29 7b 0a 20 20 20 20 53 74 6d 74 Push ){. Stmt
2ea0: 20 71 3b 0a 20 20 20 20 64 62 5f 70 72 65 70 61 q;. db_prepa
2eb0: 72 65 28 26 71 2c 20 22 53 45 4c 45 43 54 20 75 re(&q, "SELECT u
2ec0: 75 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 uid FROM blob WH
2ed0: 45 52 45 20 73 69 7a 65 3c 30 22 29 3b 0a 20 20 ERE size<0");.
2ee0: 20 20 77 68 69 6c 65 28 20 64 62 5f 73 74 65 70 while( db_step
2ef0: 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 (&q)==SQLITE_ROW
2f00: 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 ){. const
2f10: 63 68 61 72 20 2a 7a 55 75 69 64 20 3d 20 64 62 char *zUuid = db
2f20: 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 26 71 2c _column_text(&q,
2f30: 20 30 29 3b 0a 20 20 20 20 20 20 40 20 67 69 6d 0);. @ gim
2f40: 6d 65 20 25 73 28 7a 55 75 69 64 29 0a 20 20 20 me %s(zUuid).
2f50: 20 7d 0a 20 20 20 20 64 62 5f 66 69 6e 61 6c 69 }. db_finali
2f60: 7a 65 28 26 71 29 3b 0a 20 20 7d 0a 20 20 69 66 ze(&q);. }. if
2f70: 28 20 69 73 50 75 6c 6c 20 29 7b 0a 20 20 20 20 ( isPull ){.
2f80: 73 65 6e 64 5f 61 6c 6c 5f 70 65 6e 64 69 6e 67 send_all_pending
2f90: 28 30 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 65 6e (0);. }. db_en
2fa0: 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 30 29 d_transaction(0)
2fb0: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 ;.}../*.** COMMA
2fc0: 4e 44 3a 20 74 65 73 74 2d 78 66 65 72 0a 2a 2a ND: test-xfer.**
2fd0: 0a 2a 2a 20 54 68 69 73 20 63 6f 6d 6d 61 6e 64 .** This command
2fe0: 20 69 73 20 75 73 65 64 20 66 6f 72 20 64 65 62 is used for deb
2ff0: 75 67 67 69 6e 67 20 74 68 65 20 73 65 72 76 65 ugging the serve
3000: 72 2e 20 20 54 68 65 72 65 20 69 73 20 61 20 73 r. There is a s
3010: 69 6e 67 6c 65 0a 2a 2a 20 61 72 67 75 6d 65 6e ingle.** argumen
3020: 74 20 77 68 69 63 68 20 69 73 20 74 68 65 20 75 t which is the u
3030: 6e 63 6f 6d 70 72 65 73 73 65 64 20 63 6f 6e 74 ncompressed cont
3040: 65 6e 74 20 6f 66 20 61 6e 20 22 78 66 65 72 22 ent of an "xfer"
3050: 20 6d 65 73 73 61 67 65 0a 2a 2a 20 66 72 6f 6d message.** from
3060: 20 63 6c 69 65 6e 74 20 74 6f 20 73 65 72 76 65 client to serve
3070: 72 2e 20 20 54 68 69 73 20 63 6f 6d 6d 61 6e 64 r. This command
3080: 20 69 6e 74 65 72 70 72 65 74 73 20 74 68 61 74 interprets that
3090: 20 6d 65 73 73 61 67 65 20 61 73 0a 2a 2a 20 69 message as.** i
30a0: 66 20 68 61 64 20 62 65 65 6e 20 72 65 63 65 69 f had been recei
30b0: 76 65 64 20 62 79 20 74 68 65 20 73 65 72 76 65 ved by the serve
30c0: 72 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 74 68 65 20 r..**.** On the
30d0: 63 6c 69 65 6e 74 20 73 69 64 65 2c 20 72 75 6e client side, run
30e0: 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 66 6f 73 :.**.** fos
30f0: 73 69 6c 20 70 75 73 68 20 68 74 74 70 3a 2f 2f sil push http://
3100: 62 6f 67 75 73 2f 20 2d 2d 68 74 74 70 74 72 61 bogus/ --httptra
3110: 63 65 0a 2a 2a 0a 2a 2a 20 4f 72 20 61 20 73 69 ce.**.** Or a si
3120: 6d 69 6c 61 72 20 63 6f 6d 6d 61 6e 64 20 74 6f milar command to
3130: 20 70 72 6f 76 69 64 65 20 74 68 65 20 6f 75 74 provide the out
3140: 70 75 74 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e put. The conten
3150: 74 20 6f 66 20 74 68 65 0a 2a 2a 20 6d 65 73 73 t of the.** mess
3160: 61 67 65 20 77 69 6c 6c 20 61 70 70 65 61 72 20 age will appear
3170: 6f 6e 20 73 74 61 6e 64 61 72 64 20 6f 75 74 70 on standard outp
3180: 75 74 2e 20 20 43 61 70 74 75 72 65 20 74 68 69 ut. Capture thi
3190: 73 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 6e 74 s message.** int
31a0: 6f 20 61 20 66 69 6c 65 20 6e 61 6d 65 64 20 28 o a file named (
31b0: 66 6f 72 20 65 78 61 6d 70 6c 65 29 20 6f 75 74 for example) out
31c0: 2e 74 78 74 2e 20 20 54 68 65 6e 20 72 75 6e 20 .txt. Then run
31d0: 74 68 65 0a 2a 2a 20 73 65 72 76 65 72 20 69 6e the.** server in
31e0: 20 67 64 62 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 gdb:.**.**
31f0: 67 64 62 20 66 6f 73 73 69 6c 0a 2a 2a 20 20 20 gdb fossil.**
3200: 20 20 72 20 74 65 73 74 2d 78 66 65 72 20 6f 75 r test-xfer ou
3210: 74 2e 74 78 74 0a 2a 2f 0a 76 6f 69 64 20 63 6d t.txt.*/.void cm
3220: 64 5f 74 65 73 74 5f 78 66 65 72 28 76 6f 69 64 d_test_xfer(void
3230: 29 7b 0a 20 20 69 6e 74 20 6e 6f 74 55 73 65 64 ){. int notUsed
3240: 3b 0a 20 20 69 66 28 20 67 2e 61 72 67 63 21 3d ;. if( g.argc!=
3250: 32 20 26 26 20 67 2e 61 72 67 63 21 3d 33 20 29 2 && g.argc!=3 )
3260: 7b 0a 20 20 20 20 75 73 61 67 65 28 22 3f 4d 45 {. usage("?ME
3270: 53 53 41 47 45 46 49 4c 45 3f 22 29 3b 0a 20 20 SSAGEFILE?");.
3280: 7d 0a 20 20 64 62 5f 6d 75 73 74 5f 62 65 5f 77 }. db_must_be_w
3290: 69 74 68 69 6e 5f 74 72 65 65 28 29 3b 0a 20 20 ithin_tree();.
32a0: 62 6c 6f 62 5f 7a 65 72 6f 28 26 67 2e 63 67 69 blob_zero(&g.cgi
32b0: 49 6e 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 61 64 In);. blob_read
32c0: 5f 66 72 6f 6d 5f 66 69 6c 65 28 26 67 2e 63 67 _from_file(&g.cg
32d0: 69 49 6e 2c 20 67 2e 61 72 67 63 3d 3d 32 20 3f iIn, g.argc==2 ?
32e0: 20 22 2d 22 20 3a 20 67 2e 61 72 67 76 5b 32 5d "-" : g.argv[2]
32f0: 29 3b 0a 20 20 64 69 73 61 62 6c 65 4c 6f 67 69 );. disableLogi
3300: 6e 20 3d 20 31 3b 0a 20 20 70 61 67 65 5f 78 66 n = 1;. page_xf
3310: 65 72 28 29 3b 0a 20 20 70 72 69 6e 74 66 28 22 er();. printf("
3320: 25 73 5c 6e 22 2c 20 63 67 69 5f 65 78 74 72 61 %s\n", cgi_extra
3330: 63 74 5f 63 6f 6e 74 65 6e 74 28 26 6e 6f 74 55 ct_content(¬U
3340: 73 65 64 29 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a sed));.}.../*.**
3350: 20 53 79 6e 63 20 74 6f 20 74 68 65 20 68 6f 73 Sync to the hos
3360: 74 20 69 64 65 6e 74 69 66 69 65 64 20 69 6e 20 t identified in
3370: 67 2e 75 72 6c 4e 61 6d 65 20 61 6e 64 20 67 2e g.urlName and g.
3380: 75 72 6c 50 61 74 68 2e 20 20 54 68 69 73 0a 2a urlPath. This.*
3390: 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c * routine is cal
33a0: 6c 65 64 20 62 79 20 74 68 65 20 63 6c 69 65 6e led by the clien
33b0: 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 63 6f 72 64 73 t..**.** Records
33c0: 20 61 72 65 20 70 75 73 68 65 64 20 74 6f 20 74 are pushed to t
33d0: 68 65 20 73 65 72 76 65 72 20 69 66 20 70 75 73 he server if pus
33e0: 68 46 6c 61 67 20 69 73 20 74 72 75 65 2e 20 20 hFlag is true.
33f0: 52 65 63 6f 72 64 73 0a 2a 2a 20 61 72 65 20 70 Records.** are p
3400: 75 6c 6c 65 64 20 69 66 20 70 75 6c 6c 46 6c 61 ulled if pullFla
3410: 67 20 69 73 20 74 72 75 65 2e 20 20 41 20 66 75 g is true. A fu
3420: 6c 6c 20 73 79 6e 63 20 6f 63 63 75 72 73 20 69 ll sync occurs i
3430: 66 20 62 6f 74 68 20 61 72 65 0a 2a 2a 20 74 72 f both are.** tr
3440: 75 65 2e 0a 2a 2f 0a 76 6f 69 64 20 63 6c 69 65 ue..*/.void clie
3450: 6e 74 5f 73 79 6e 63 28 69 6e 74 20 70 75 73 68 nt_sync(int push
3460: 46 6c 61 67 2c 20 69 6e 74 20 70 75 6c 6c 46 6c Flag, int pullFl
3470: 61 67 2c 20 69 6e 74 20 63 6c 6f 6e 65 46 6c 61 ag, int cloneFla
3480: 67 29 7b 0a 20 20 69 6e 74 20 67 6f 20 3d 20 31 g){. int go = 1
3490: 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 ; /* Loop
34a0: 20 75 6e 74 69 6c 20 7a 65 72 6f 20 2a 2f 0a 20 until zero */.
34b0: 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 0a 20 20 63 int nToken;. c
34c0: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 43 6f 64 onst char *zSCod
34d0: 65 20 3d 20 64 62 5f 67 65 74 28 22 73 65 72 76 e = db_get("serv
34e0: 65 72 2d 63 6f 64 65 22 2c 20 22 78 22 29 3b 0a er-code", "x");.
34f0: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 const char *zP
3500: 43 6f 64 65 20 3d 20 64 62 5f 67 65 74 28 22 70 Code = db_get("p
3510: 72 6f 6a 65 63 74 2d 63 6f 64 65 22 2c 20 30 29 roject-code", 0)
3520: 3b 0a 20 20 42 6c 6f 62 20 73 65 6e 64 3b 20 20 ;. Blob send;
3530: 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 77 65 /* Text we
3540: 20 61 72 65 20 73 65 6e 64 69 6e 67 20 74 6f 20 are sending to
3550: 74 68 65 20 73 65 72 76 65 72 20 2a 2f 0a 20 20 the server */.
3560: 42 6c 6f 62 20 72 65 63 76 3b 20 20 20 20 20 20 Blob recv;
3570: 20 20 2f 2a 20 52 65 70 6c 79 20 77 65 20 67 6f /* Reply we go
3580: 74 20 62 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 t back from the
3590: 73 65 72 76 65 72 20 2a 2f 0a 20 20 42 6c 6f 62 server */. Blob
35a0: 20 6c 69 6e 65 3b 20 20 20 20 20 20 20 20 2f 2a line; /*
35b0: 20 41 20 73 69 6e 67 6c 65 20 6c 69 6e 65 20 6f A single line o
35c0: 66 20 74 68 65 20 72 65 70 6c 79 20 2a 2f 0a 20 f the reply */.
35d0: 20 42 6c 6f 62 20 61 54 6f 6b 65 6e 5b 35 5d 3b Blob aToken[5];
35e0: 20 20 20 2f 2a 20 41 20 74 6f 6b 65 6e 69 7a 61 /* A tokeniza
35f0: 74 69 6f 6e 20 6f 66 20 6c 69 6e 65 20 2a 2f 0a tion of line */.
3600: 20 20 42 6c 6f 62 20 65 72 72 6d 73 67 3b 20 20 Blob errmsg;
3610: 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 /* Error mes
3620: 73 61 67 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 sage */.. asser
3630: 74 28 20 70 75 73 68 46 6c 61 67 20 7c 7c 20 70 t( pushFlag || p
3640: 75 6c 6c 46 6c 61 67 20 7c 7c 20 63 6c 6f 6e 65 ullFlag || clone
3650: 46 6c 61 67 20 29 3b 0a 20 20 61 73 73 65 72 74 Flag );. assert
3660: 28 20 21 67 2e 75 72 6c 49 73 46 69 6c 65 20 29 ( !g.urlIsFile )
3670: 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 ; /* Th
3680: 69 73 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 66 6f is only works fo
3690: 72 20 6e 65 74 77 6f 72 6b 69 6e 67 20 2a 2f 0a r networking */.
36a0: 0a 20 20 64 62 5f 62 65 67 69 6e 5f 74 72 61 6e . db_begin_tran
36b0: 73 61 63 74 69 6f 6e 28 29 3b 0a 20 20 64 62 5f saction();. db_
36c0: 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 20 multi_exec(.
36d0: 2f 2a 20 52 65 63 6f 72 64 73 20 77 68 69 63 68 /* Records which
36e0: 20 77 65 20 6b 6e 6f 77 20 74 68 65 20 6f 74 68 we know the oth
36f0: 65 72 20 73 69 64 65 20 61 6c 73 6f 20 68 61 73 er side also has
3700: 20 2a 2f 0a 20 20 20 20 22 43 52 45 41 54 45 20 */. "CREATE
3710: 54 45 4d 50 20 54 41 42 4c 45 20 6f 6e 72 65 6d TEMP TABLE onrem
3720: 6f 74 65 28 72 69 64 20 49 4e 54 45 47 45 52 20 ote(rid INTEGER
3730: 50 52 49 4d 41 52 59 20 4b 45 59 29 3b 22 0a 20 PRIMARY KEY);".
3740: 20 20 20 2f 2a 20 52 65 63 6f 72 64 73 20 77 65 /* Records we
3750: 20 6b 6e 6f 77 20 74 68 65 20 6f 74 68 65 72 20 know the other
3760: 73 69 64 65 20 6e 65 65 64 73 20 2a 2f 0a 20 20 side needs */.
3770: 20 20 22 43 52 45 41 54 45 20 54 45 4d 50 20 54 "CREATE TEMP T
3780: 41 42 4c 45 20 70 65 6e 64 69 6e 67 28 72 69 64 ABLE pending(rid
3790: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY
37a0: 20 4b 45 59 29 3b 22 0a 20 20 29 3b 0a 20 20 62 KEY);". );. b
37b0: 6c 6f 62 61 72 72 61 79 5f 7a 65 72 6f 28 61 54 lobarray_zero(aT
37c0: 6f 6b 65 6e 2c 20 63 6f 75 6e 74 28 61 54 6f 6b oken, count(aTok
37d0: 65 6e 29 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 en));. blob_zer
37e0: 6f 28 26 73 65 6e 64 29 3b 0a 20 20 62 6c 6f 62 o(&send);. blob
37f0: 5f 7a 65 72 6f 28 26 72 65 63 76 29 3b 0a 20 20 _zero(&recv);.
3800: 62 6c 6f 62 5f 7a 65 72 6f 28 26 65 72 72 6d 73 blob_zero(&errms
3810: 67 29 3b 0a 0a 0a 20 20 77 68 69 6c 65 28 20 67 g);... while( g
3820: 6f 20 29 7b 0a 20 20 20 20 67 6f 20 3d 20 30 3b o ){. go = 0;
3830: 0a 0a 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 .. /* Generat
3840: 65 20 61 20 72 65 71 75 65 73 74 20 74 6f 20 62 e a request to b
3850: 65 20 73 65 6e 74 20 74 6f 20 74 68 65 20 73 65 e sent to the se
3860: 72 76 65 72 2e 0a 20 20 20 20 2a 2a 20 41 6c 77 rver.. ** Alw
3870: 61 79 73 20 62 65 67 69 6e 20 77 69 74 68 20 61 ays begin with a
3880: 20 63 6c 6f 6e 65 2c 20 70 75 6c 6c 2c 20 6f 72 clone, pull, or
3890: 20 70 75 73 68 20 6d 65 73 73 61 67 65 0a 20 20 push message.
38a0: 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 6c 6f */. if( clo
38b0: 6e 65 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 neFlag ){.
38c0: 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 73 65 blob_appendf(&se
38d0: 6e 64 2c 20 22 63 6c 6f 6e 65 5c 6e 22 29 3b 0a nd, "clone\n");.
38e0: 20 20 20 20 20 20 70 75 73 68 46 6c 61 67 20 3d pushFlag =
38f0: 20 30 3b 0a 20 20 20 20 20 20 70 75 6c 6c 46 6c 0;. pullFl
3900: 61 67 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 ag = 0;. }els
3910: 65 20 69 66 28 20 70 75 6c 6c 46 6c 61 67 20 29 e if( pullFlag )
3920: 7b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 {. blob_app
3930: 65 6e 64 66 28 26 73 65 6e 64 2c 20 22 70 75 6c endf(&send, "pul
3940: 6c 20 25 73 20 25 73 5c 6e 22 2c 20 7a 53 43 6f l %s %s\n", zSCo
3950: 64 65 2c 20 7a 50 43 6f 64 65 29 3b 0a 20 20 20 de, zPCode);.
3960: 20 7d 0a 20 20 20 20 69 66 28 20 70 75 73 68 46 }. if( pushF
3970: 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 62 6c 6f lag ){. blo
3980: 62 5f 61 70 70 65 6e 64 66 28 26 73 65 6e 64 2c b_appendf(&send,
3990: 20 22 70 75 73 68 20 25 73 20 25 73 5c 6e 22 2c "push %s %s\n",
39a0: 20 7a 53 43 6f 64 65 2c 20 7a 50 43 6f 64 65 29 zSCode, zPCode)
39b0: 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 ;. }.. if(
39c0: 20 70 75 6c 6c 46 6c 61 67 20 29 7b 0a 20 20 20 pullFlag ){.
39d0: 20 20 20 2f 2a 20 53 65 6e 64 20 67 69 6d 6d 65 /* Send gimme
39e0: 20 6d 65 73 73 61 67 65 20 66 6f 72 20 65 76 65 message for eve
39f0: 72 79 20 70 68 61 6e 74 6f 6d 20 74 68 61 74 20 ry phantom that
3a00: 77 65 20 68 6f 6c 64 2e 0a 20 20 20 20 20 20 2a we hold.. *
3a10: 2f 0a 20 20 20 20 20 20 53 74 6d 74 20 71 3b 0a /. Stmt q;.
3a20: 20 20 20 20 20 20 64 62 5f 70 72 65 70 61 72 65 db_prepare
3a30: 28 26 71 2c 20 22 53 45 4c 45 43 54 20 75 75 69 (&q, "SELECT uui
3a40: 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 d FROM blob WHER
3a50: 45 20 73 69 7a 65 3c 30 22 29 3b 0a 20 20 20 20 E size<0");.
3a60: 20 20 77 68 69 6c 65 28 20 64 62 5f 73 74 65 70 while( db_step
3a70: 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 (&q)==SQLITE_ROW
3a80: 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 ){. cons
3a90: 74 20 63 68 61 72 20 2a 7a 55 75 69 64 20 3d 20 t char *zUuid =
3aa0: 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 26 db_column_text(&
3ab0: 71 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 62 q, 0);. b
3ac0: 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 73 65 6e lob_appendf(&sen
3ad0: 64 2c 22 67 69 6d 6d 65 20 25 73 5c 6e 22 2c 20 d,"gimme %s\n",
3ae0: 7a 55 75 69 64 29 3b 0a 20 20 20 20 20 20 7d 0a zUuid);. }.
3af0: 20 20 20 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a db_finaliz
3b00: 65 28 26 71 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 e(&q);. }..
3b10: 20 20 69 66 28 20 70 75 73 68 46 6c 61 67 20 29 if( pushFlag )
3b20: 7b 0a 20 20 20 20 20 20 2f 2a 20 53 65 6e 64 20 {. /* Send
3b30: 74 68 65 20 73 65 72 76 65 72 20 61 6e 79 20 66 the server any f
3b40: 69 6c 65 73 20 74 68 61 74 20 74 68 65 20 73 65 iles that the se
3b50: 72 76 65 72 20 68 61 73 20 72 65 71 75 65 73 74 rver has request
3b60: 65 64 20 2a 2f 0a 20 20 20 20 20 20 73 65 6e 64 ed */. send
3b70: 5f 61 6c 6c 5f 70 65 6e 64 69 6e 67 28 26 73 65 _all_pending(&se
3b80: 6e 64 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 nd);. }..
3b90: 69 66 28 20 70 75 6c 6c 46 6c 61 67 20 7c 7c 20 if( pullFlag ||
3ba0: 70 75 73 68 46 6c 61 67 20 29 7b 0a 20 20 20 20 pushFlag ){.
3bb0: 20 20 2f 2a 20 41 6c 77 61 79 73 20 73 65 6e 64 /* Always send
3bc0: 20 6f 75 72 20 6c 65 61 76 65 73 20 2a 2f 0a 20 our leaves */.
3bd0: 20 20 20 20 20 53 74 6d 74 20 71 3b 0a 20 20 20 Stmt q;.
3be0: 20 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 71 db_prepare(&q
3bf0: 2c 20 0a 20 20 20 20 20 20 20 20 20 22 53 45 4c , . "SEL
3c00: 45 43 54 20 75 75 69 64 20 46 52 4f 4d 20 62 6c ECT uuid FROM bl
3c10: 6f 62 20 57 48 45 52 45 20 72 69 64 20 49 4e 22 ob WHERE rid IN"
3c20: 0a 20 20 20 20 20 20 20 20 20 22 20 20 28 53 45 . " (SE
3c30: 4c 45 43 54 20 63 69 64 20 46 52 4f 4d 20 70 6c LECT cid FROM pl
3c40: 69 6e 6b 20 45 58 43 45 50 54 20 53 45 4c 45 43 ink EXCEPT SELEC
3c50: 54 20 70 69 64 20 46 52 4f 4d 20 70 6c 69 6e 6b T pid FROM plink
3c60: 29 22 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 )". );.
3c70: 20 20 77 68 69 6c 65 28 20 64 62 5f 73 74 65 70 while( db_step
3c80: 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 (&q)==SQLITE_ROW
3c90: 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 ){. cons
3ca0: 74 20 63 68 61 72 20 2a 7a 55 75 69 64 20 3d 20 t char *zUuid =
3cb0: 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 26 db_column_text(&
3cc0: 71 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 62 q, 0);. b
3cd0: 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 73 65 6e lob_appendf(&sen
3ce0: 64 2c 20 22 6c 65 61 66 20 25 73 5c 6e 22 2c 20 d, "leaf %s\n",
3cf0: 7a 55 75 69 64 29 3b 0a 20 20 20 20 20 20 7d 0a zUuid);. }.
3d00: 20 20 20 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a db_finaliz
3d10: 65 28 26 71 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 e(&q);. }..
3d20: 20 20 2f 2a 20 45 78 63 68 61 6e 67 65 20 6d 65 /* Exchange me
3d30: 73 73 61 67 65 73 20 77 69 74 68 20 74 68 65 20 ssages with the
3d40: 73 65 72 76 65 72 20 2a 2f 0a 20 20 20 20 68 74 server */. ht
3d50: 74 70 5f 65 78 63 68 61 6e 67 65 28 26 73 65 6e tp_exchange(&sen
3d60: 64 2c 20 26 72 65 63 76 29 3b 0a 20 20 20 20 62 d, &recv);. b
3d70: 6c 6f 62 5f 72 65 73 65 74 28 26 73 65 6e 64 29 lob_reset(&send)
3d80: 3b 0a 0a 20 20 20 20 2f 2a 20 50 72 6f 63 65 73 ;.. /* Proces
3d90: 73 20 74 68 65 20 72 65 70 6c 79 20 74 68 61 74 s the reply that
3da0: 20 63 61 6d 65 20 62 61 63 6b 20 66 72 6f 6d 20 came back from
3db0: 74 68 65 20 73 65 72 76 65 72 20 2a 2f 0a 20 20 the server */.
3dc0: 20 20 77 68 69 6c 65 28 20 62 6c 6f 62 5f 6c 69 while( blob_li
3dd0: 6e 65 28 26 72 65 63 76 2c 20 26 6c 69 6e 65 29 ne(&recv, &line)
3de0: 20 29 7b 0a 20 20 20 20 20 20 6e 54 6f 6b 65 6e ){. nToken
3df0: 20 3d 20 62 6c 6f 62 5f 74 6f 6b 65 6e 69 7a 65 = blob_tokenize
3e00: 28 26 6c 69 6e 65 2c 20 61 54 6f 6b 65 6e 2c 20 (&line, aToken,
3e10: 63 6f 75 6e 74 28 61 54 6f 6b 65 6e 29 29 3b 0a count(aToken));.
3e20: 0a 20 20 20 20 20 20 2f 2a 20 20 20 66 69 6c 65 . /* file
3e30: 20 55 55 49 44 20 53 49 5a 45 20 5c 6e 20 43 4f UUID SIZE \n CO
3e40: 4e 54 45 4e 54 0a 20 20 20 20 20 20 2a 2a 20 20 NTENT. **
3e50: 20 66 69 6c 65 20 55 55 49 44 20 44 45 4c 54 41 file UUID DELTA
3e60: 53 52 43 20 53 49 5a 45 20 5c 6e 20 43 4f 4e 54 SRC SIZE \n CONT
3e70: 45 4e 54 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 ENT. **.
3e80: 20 20 20 2a 2a 20 52 65 63 65 69 76 65 20 61 20 ** Receive a
3e90: 66 69 6c 65 20 74 72 61 6e 73 6d 69 74 74 65 64 file transmitted
3ea0: 20 66 72 6f 6d 20 74 68 65 20 6f 74 68 65 72 20 from the other
3eb0: 73 69 64 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20 side. */.
3ec0: 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 if( blob_eq(
3ed0: 26 61 54 6f 6b 65 6e 5b 30 5d 2c 22 66 69 6c 65 &aToken[0],"file
3ee0: 22 29 20 29 7b 0a 20 20 20 20 20 20 20 20 78 66 ") ){. xf
3ef0: 65 72 5f 61 63 63 65 70 74 5f 66 69 6c 65 28 26 er_accept_file(&
3f00: 72 65 63 76 2c 20 61 54 6f 6b 65 6e 2c 20 6e 54 recv, aToken, nT
3f10: 6f 6b 65 6e 2c 20 26 65 72 72 6d 73 67 29 3b 0a oken, &errmsg);.
3f20: 20 20 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20 }else..
3f30: 20 20 20 2f 2a 20 20 20 67 69 6d 6d 65 20 55 55 /* gimme UU
3f40: 49 44 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 ID. **.
3f50: 20 20 2a 2a 20 53 65 72 76 65 72 20 69 73 20 72 ** Server is r
3f60: 65 71 75 65 73 74 69 6e 67 20 61 20 66 69 6c 65 equesting a file
3f70: 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
3f80: 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f if( blob_eq(&aTo
3f90: 6b 65 6e 5b 30 5d 2c 20 22 67 69 6d 6d 65 22 29 ken[0], "gimme")
3fa0: 20 26 26 20 6e 54 6f 6b 65 6e 3d 3d 32 0a 20 20 && nToken==2.
3fb0: 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 &&
3fc0: 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26 61 54 blob_is_uuid(&aT
3fd0: 6f 6b 65 6e 5b 31 5d 29 20 29 7b 0a 20 20 20 20 oken[1]) ){.
3fe0: 20 20 20 20 69 66 28 20 70 75 73 68 46 6c 61 67 if( pushFlag
3ff0: 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 ){. db
4000: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 _multi_exec(.
4010: 20 20 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 "INSERT
4020: 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 OR IGNORE INTO
4030: 70 65 6e 64 69 6e 67 28 72 69 64 29 20 22 0a 20 pending(rid) ".
4040: 20 20 20 20 20 20 20 20 20 20 20 22 53 45 4c 45 "SELE
4050: 43 54 20 72 69 64 20 46 52 4f 4d 20 62 6c 6f 62 CT rid FROM blob
4060: 20 57 48 45 52 45 20 75 75 69 64 3d 25 42 20 41 WHERE uuid=%B A
4070: 4e 44 20 73 69 7a 65 3e 3d 30 22 2c 20 26 61 54 ND size>=0", &aT
4080: 6f 6b 65 6e 5b 31 5d 0a 20 20 20 20 20 20 20 20 oken[1].
4090: 20 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67 );. g
40a0: 6f 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d o = 1;. }
40b0: 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a 20 20 0a . }else. .
40c0: 20 20 20 20 20 20 2f 2a 20 20 20 69 67 6f 74 20 /* igot
40d0: 55 55 49 44 0a 20 20 20 20 20 20 2a 2a 20 20 20 UUID. **
40e0: 6c 65 61 66 20 55 55 49 44 0a 20 20 20 20 20 20 leaf UUID.
40f0: 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 53 65 72 76 **. ** Serv
4100: 65 72 20 70 72 6f 63 6c 61 69 6d 73 20 74 68 61 er proclaims tha
4110: 74 20 69 74 20 68 61 73 20 61 20 70 61 72 74 69 t it has a parti
4120: 63 75 6c 61 72 20 66 69 6c 65 2e 20 20 41 20 6c cular file. A l
4130: 65 61 66 20 6d 65 73 73 61 67 65 0a 20 20 20 20 eaf message.
4140: 20 20 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 20 ** means that
4150: 74 68 65 20 66 69 6c 65 20 69 73 20 61 20 6c 65 the file is a le
4160: 61 66 20 6d 61 6e 69 66 65 73 74 20 6f 6e 20 74 af manifest on t
4170: 68 65 20 73 65 72 76 65 72 2e 0a 20 20 20 20 20 he server..
4180: 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 6e 54 */. if( nT
4190: 6f 6b 65 6e 3d 3d 32 0a 20 20 20 20 20 20 20 20 oken==2.
41a0: 20 20 20 20 20 20 26 26 20 28 62 6c 6f 62 5f 65 && (blob_e
41b0: 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 69 q(&aToken[0], "i
41c0: 67 6f 74 22 29 20 7c 7c 20 62 6c 6f 62 5f 65 71 got") || blob_eq
41d0: 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 6c 65 (&aToken[0], "le
41e0: 61 66 22 29 29 0a 20 20 20 20 20 20 20 20 20 20 af")).
41f0: 20 20 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75 && blob_is_u
4200: 75 69 64 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 20 uid(&aToken[1])
4210: 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 ){. int r
4220: 69 64 20 3d 20 64 62 5f 69 6e 74 28 30 2c 20 22 id = db_int(0, "
4230: 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f 4d 20 SELECT rid FROM
4240: 62 6c 6f 62 20 57 48 45 52 45 20 75 75 69 64 3d blob WHERE uuid=
4250: 25 42 22 2c 20 26 61 54 6f 6b 65 6e 5b 31 5d 29 %B", &aToken[1])
4260: 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 69 ;. if( ri
4270: 64 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 d>0 ){.
4280: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a db_multi_exec(.
4290: 20 20 20 20 20 20 20 20 20 20 20 20 22 49 4e 53 "INS
42a0: 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e ERT OR IGNORE IN
42b0: 54 4f 20 6f 6e 72 65 6d 6f 74 65 28 72 69 64 29 TO onremote(rid)
42c0: 20 56 41 4c 55 45 53 28 25 64 29 22 2c 20 72 69 VALUES(%d)", ri
42d0: 64 0a 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20 d. );.
42e0: 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20 /* Add
42f0: 74 6f 20 74 68 65 20 70 65 6e 64 69 6e 67 20 73 to the pending s
4300: 65 74 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20 et all children
4310: 6f 66 20 74 68 65 20 73 65 72 76 65 72 27 73 20 of the server's
4320: 6c 65 61 76 65 73 20 2a 2f 0a 20 20 20 20 20 20 leaves */.
4330: 20 20 20 20 69 66 28 20 70 75 73 68 46 6c 61 67 if( pushFlag
4340: 20 26 26 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f && blob_eq(&aTo
4350: 6b 65 6e 5b 30 5d 2c 20 22 6c 65 61 66 22 29 20 ken[0], "leaf")
4360: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 ){. d
4370: 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 b_multi_exec(.
4380: 20 20 20 20 20 20 20 20 20 20 20 20 22 49 4e 53 "INS
4390: 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e ERT OR IGNORE IN
43a0: 54 4f 20 70 65 6e 64 69 6e 67 28 72 69 64 29 20 TO pending(rid)
43b0: 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ".
43c0: 22 53 45 4c 45 43 54 20 63 69 64 20 46 52 4f 4d "SELECT cid FROM
43d0: 20 70 6c 69 6e 6b 20 57 48 45 52 45 20 70 69 64 plink WHERE pid
43e0: 3d 25 64 22 2c 20 72 69 64 0a 20 20 20 20 20 20 =%d", rid.
43f0: 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 );.
4400: 20 20 20 20 20 67 6f 20 3d 20 31 3b 0a 20 20 20 go = 1;.
4410: 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
4420: 20 20 20 69 66 28 20 70 75 6c 6c 46 6c 61 67 20 if( pullFlag
4430: 26 26 20 21 67 6f 20 26 26 20 0a 20 20 20 20 20 && !go && .
4440: 20 20 20 20 20 20 20 20 20 64 62 5f 65 78 69 73 db_exis
4450: 74 73 28 22 53 45 4c 45 43 54 20 31 20 46 52 4f ts("SELECT 1 FRO
4460: 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 72 69 64 M blob WHERE rid
4470: 3d 25 64 20 41 4e 44 20 73 69 7a 65 3e 3d 30 22 =%d AND size>=0"
4480: 2c 20 72 69 64 29 20 29 7b 0a 20 20 20 20 20 20 , rid) ){.
4490: 20 20 20 20 20 20 67 6f 20 3d 20 31 3b 0a 20 20 go = 1;.
44a0: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
44b0: 20 20 7d 65 6c 73 65 20 69 66 28 20 70 75 6c 6c }else if( pull
44c0: 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20 Flag ){.
44d0: 20 20 67 6f 20 3d 20 31 3b 0a 20 20 20 20 20 20 go = 1;.
44e0: 20 20 20 20 63 6f 6e 74 65 6e 74 5f 70 75 74 28 content_put(
44f0: 30 2c 20 62 6c 6f 62 5f 73 74 72 28 26 61 54 6f 0, blob_str(&aTo
4500: 6b 65 6e 5b 31 5d 29 29 3b 0a 20 20 20 20 20 20 ken[1]));.
4510: 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a }. }else.
4520: 20 20 0a 20 20 20 20 20 20 2f 2a 20 20 20 70 75 . /* pu
4530: 73 68 20 20 53 45 52 56 45 52 43 4f 44 45 20 20 sh SERVERCODE
4540: 50 52 4f 44 55 43 54 43 4f 44 45 0a 20 20 20 20 PRODUCTCODE.
4550: 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 53 68 **. ** Sh
4560: 6f 75 6c 64 20 6f 6e 6c 79 20 68 61 70 70 65 6e ould only happen
4570: 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 in response to
4580: 61 20 63 6c 6f 6e 65 2e 0a 20 20 20 20 20 20 2a a clone.. *
4590: 2f 0a 20 20 20 20 20 20 69 66 28 20 62 6c 6f 62 /. if( blob
45a0: 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 22 _eq(&aToken[0],"
45b0: 70 75 73 68 22 29 20 26 26 20 6e 54 6f 6b 65 6e push") && nToken
45c0: 3d 3d 32 20 26 26 20 63 6c 6f 6e 65 46 6c 61 67 ==2 && cloneFlag
45d0: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 . &
45e0: 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26 & blob_is_uuid(&
45f0: 61 54 6f 6b 65 6e 5b 31 5d 29 20 26 26 20 62 6c aToken[1]) && bl
4600: 6f 62 5f 69 73 5f 75 75 69 64 28 26 61 54 6f 6b ob_is_uuid(&aTok
4610: 65 6e 5b 32 5d 29 20 29 7b 0a 0a 20 20 20 20 20 en[2]) ){..
4620: 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 5f 73 if( blob_eq_s
4630: 74 72 28 26 61 54 6f 6b 65 6e 5b 31 5d 2c 20 7a tr(&aToken[1], z
4640: 53 43 6f 64 65 2c 20 2d 31 29 20 29 7b 0a 20 20 SCode, -1) ){.
4650: 20 20 20 20 20 20 20 20 66 6f 73 73 69 6c 5f 66 fossil_f
4660: 61 74 61 6c 28 22 73 65 72 76 65 72 20 6c 6f 6f atal("server loo
4670: 70 22 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 p");. }.
4680: 20 20 20 20 20 20 20 69 66 28 20 7a 50 43 6f 64 if( zPCod
4690: 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 e==0 ){.
46a0: 20 20 7a 50 43 6f 64 65 20 3d 20 6d 70 72 69 6e zPCode = mprin
46b0: 74 66 28 22 25 62 22 2c 20 26 61 54 6f 6b 65 6e tf("%b", &aToken
46c0: 5b 32 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 [2]);.
46d0: 64 62 5f 73 65 74 28 22 70 72 6f 64 75 63 74 2d db_set("product-
46e0: 63 6f 64 65 22 2c 20 7a 50 43 6f 64 65 29 3b 0a code", zPCode);.
46f0: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
4700: 20 20 63 6c 6f 6e 65 46 6c 61 67 20 3d 20 30 3b cloneFlag = 0;
4710: 0a 20 20 20 20 20 20 20 20 70 75 6c 6c 46 6c 61 . pullFla
4720: 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c g = 1;. }el
4730: 73 65 0a 0a 20 20 20 20 20 20 2f 2a 20 20 20 65 se.. /* e
4740: 72 72 6f 72 20 4d 45 53 53 41 47 45 0a 20 20 20 rror MESSAGE.
4750: 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 52 **. ** R
4760: 65 70 6f 72 74 20 61 6e 20 65 72 72 6f 72 0a 20 eport an error.
4770: 20 20 20 20 20 2a 2f 20 20 20 20 20 20 20 20 0a */ .
4780: 20 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 if( blob_e
4790: 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 22 65 72 q(&aToken[0],"er
47a0: 72 6f 72 22 29 20 26 26 20 6e 54 6f 6b 65 6e 3d ror") && nToken=
47b0: 3d 32 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 =2 ){. ch
47c0: 61 72 20 2a 7a 4d 73 67 20 3d 20 62 6c 6f 62 5f ar *zMsg = blob_
47d0: 74 65 72 6d 69 6e 61 74 65 28 26 61 54 6f 6b 65 terminate(&aToke
47e0: 6e 5b 31 5d 29 3b 0a 20 20 20 20 20 20 20 20 64 n[1]);. d
47f0: 65 66 6f 73 73 69 6c 69 7a 65 28 7a 4d 73 67 29 efossilize(zMsg)
4800: 3b 0a 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 61 ;. blob_a
4810: 70 70 65 6e 64 66 28 26 65 72 72 6d 73 67 2c 20 ppendf(&errmsg,
4820: 22 73 65 72 76 65 72 20 73 61 79 73 3a 20 25 73 "server says: %s
4830: 22 2c 20 7a 4d 73 67 29 3b 0a 20 20 20 20 20 20 ", zMsg);.
4840: 7d 65 6c 73 65 0a 0a 20 20 20 20 20 20 2f 2a 20 }else.. /*
4850: 55 6e 6b 6e 6f 77 6e 20 6d 65 73 73 61 67 65 20 Unknown message
4860: 2a 2f 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 */. {.
4870: 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 blob_appendf(
4880: 26 65 72 72 6d 73 67 2c 20 22 75 6e 6b 6e 6f 77 &errmsg, "unknow
4890: 6e 20 63 6f 6d 6d 61 6e 64 3a 20 25 62 22 2c 20 n command: %b",
48a0: 26 61 54 6f 6b 65 6e 5b 30 5d 29 3b 0a 20 20 20 &aToken[0]);.
48b0: 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 }.. if(
48c0: 62 6c 6f 62 5f 73 69 7a 65 28 26 65 72 72 6d 73 blob_size(&errms
48d0: 67 29 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6f g) ){. fo
48e0: 73 73 69 6c 5f 66 61 74 61 6c 28 22 25 62 22 2c ssil_fatal("%b",
48f0: 20 26 65 72 72 6d 73 67 29 3b 0a 20 20 20 20 20 &errmsg);.
4900: 20 7d 0a 20 20 20 20 20 20 62 6c 6f 62 61 72 72 }. blobarr
4910: 61 79 5f 72 65 73 65 74 28 61 54 6f 6b 65 6e 2c ay_reset(aToken,
4920: 20 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 7d 0a nToken);. }.
4930: 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 blob_reset(&
4940: 72 65 63 76 29 3b 0a 20 20 7d 3b 0a 20 20 68 74 recv);. };. ht
4950: 74 70 5f 63 6c 6f 73 65 28 29 3b 0a 20 20 64 62 tp_close();. db
4960: 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e _end_transaction
4970: 28 30 29 3b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f (0);. db_multi_
4980: 65 78 65 63 28 0a 20 20 20 20 22 44 52 4f 50 20 exec(. "DROP
4990: 54 41 42 4c 45 20 6f 6e 72 65 6d 6f 74 65 3b 22 TABLE onremote;"
49a0: 0a 20 20 20 20 22 44 52 4f 50 20 54 41 42 4c 45 . "DROP TABLE
49b0: 20 70 65 6e 64 69 6e 67 3b 22 0a 20 20 29 3b 0a pending;". );.
49c0: 7d 0a }.