0000: 2f 2a 0a 2a 2a 20 43 6f 70 79 72 69 67 68 74 20 /*.** Copyright
0010: 28 63 29 20 32 30 30 37 20 44 2e 20 52 69 63 68 (c) 2007 D. Rich
0020: 61 72 64 20 48 69 70 70 0a 2a 2a 0a 2a 2a 20 54 ard Hipp.**.** T
0030: 68 69 73 20 70 72 6f 67 72 61 6d 20 69 73 20 66 his program is f
0040: 72 65 65 20 73 6f 66 74 77 61 72 65 3b 20 79 6f ree software; yo
0050: 75 20 63 61 6e 20 72 65 64 69 73 74 72 69 62 75 u can redistribu
0060: 74 65 20 69 74 20 61 6e 64 2f 6f 72 0a 2a 2a 20 te it and/or.**
0070: 6d 6f 64 69 66 79 20 69 74 20 75 6e 64 65 72 20 modify it under
0080: 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65 the terms of the
0090: 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 50 75 62 GNU General Pub
00a0: 6c 69 63 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 76 lic.** License v
00b0: 65 72 73 69 6f 6e 20 32 20 61 73 20 70 75 62 6c ersion 2 as publ
00c0: 69 73 68 65 64 20 62 79 20 74 68 65 20 46 72 65 ished by the Fre
00d0: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64 e Software Found
00e0: 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 ation..**.** Thi
00f0: 73 20 70 72 6f 67 72 61 6d 20 69 73 20 64 69 73 s program is dis
0100: 74 72 69 62 75 74 65 64 20 69 6e 20 74 68 65 20 tributed in the
0110: 68 6f 70 65 20 74 68 61 74 20 69 74 20 77 69 6c hope that it wil
0120: 6c 20 62 65 20 75 73 65 66 75 6c 2c 0a 2a 2a 20 l be useful,.**
0130: 62 75 74 20 57 49 54 48 4f 55 54 20 41 4e 59 20 but WITHOUT ANY
0140: 57 41 52 52 41 4e 54 59 3b 20 77 69 74 68 6f 75 WARRANTY; withou
0150: 74 20 65 76 65 6e 20 74 68 65 20 69 6d 70 6c 69 t even the impli
0160: 65 64 20 77 61 72 72 61 6e 74 79 20 6f 66 0a 2a ed warranty of.*
0170: 2a 20 4d 45 52 43 48 41 4e 54 41 42 49 4c 49 54 * MERCHANTABILIT
0180: 59 20 6f 72 20 46 49 54 4e 45 53 53 20 46 4f 52 Y or FITNESS FOR
0190: 20 41 20 50 41 52 54 49 43 55 4c 41 52 20 50 55 A PARTICULAR PU
01a0: 52 50 4f 53 45 2e 20 20 53 65 65 20 74 68 65 20 RPOSE. See the
01b0: 47 4e 55 0a 2a 2a 20 47 65 6e 65 72 61 6c 20 50 GNU.** General P
01c0: 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 20 66 6f ublic License fo
01d0: 72 20 6d 6f 72 65 20 64 65 74 61 69 6c 73 2e 0a r more details..
01e0: 2a 2a 20 0a 2a 2a 20 59 6f 75 20 73 68 6f 75 6c ** .** You shoul
01f0: 64 20 68 61 76 65 20 72 65 63 65 69 76 65 64 20 d have received
0200: 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 47 4e a copy of the GN
0210: 55 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69 63 U General Public
0220: 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 61 6c 6f 6e .** License alon
0230: 67 20 77 69 74 68 20 74 68 69 73 20 6c 69 62 72 g with this libr
0240: 61 72 79 3b 20 69 66 20 6e 6f 74 2c 20 77 72 69 ary; if not, wri
0250: 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 46 72 65 te to the.** Fre
0260: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64 e Software Found
0270: 61 74 69 6f 6e 2c 20 49 6e 63 2e 2c 20 35 39 20 ation, Inc., 59
0280: 54 65 6d 70 6c 65 20 50 6c 61 63 65 20 2d 20 53 Temple Place - S
0290: 75 69 74 65 20 33 33 30 2c 0a 2a 2a 20 42 6f 73 uite 330,.** Bos
02a0: 74 6f 6e 2c 20 4d 41 20 20 30 32 31 31 31 2d 31 ton, MA 02111-1
02b0: 33 30 37 2c 20 55 53 41 2e 0a 2a 2a 0a 2a 2a 20 307, USA..**.**
02c0: 41 75 74 68 6f 72 20 63 6f 6e 74 61 63 74 20 69 Author contact i
02d0: 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a 20 20 nformation:.**
02e0: 20 64 72 68 40 68 77 61 63 69 2e 63 6f 6d 0a 2a drh@hwaci.com.*
02f0: 2a 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 68 * http://www.h
0300: 77 61 63 69 2e 63 6f 6d 2f 64 72 68 2f 0a 2a 2a waci.com/drh/.**
0310: 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
0320: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0330: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0340: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0350: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0360: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
0370: 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 contains code t
0380: 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 o implement the
0390: 66 69 6c 65 20 74 72 61 6e 73 66 65 72 20 70 72 file transfer pr
03a0: 6f 74 6f 63 6f 6c 2e 0a 2a 2f 0a 23 69 6e 63 6c otocol..*/.#incl
03b0: 75 64 65 20 22 63 6f 6e 66 69 67 2e 68 22 0a 23 ude "config.h".#
03c0: 69 6e 63 6c 75 64 65 20 22 78 66 65 72 2e 68 22 include "xfer.h"
03d0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 54 6f 6b ../*.** The aTok
03e0: 65 6e 5b 30 2e 2e 6e 54 6f 6b 65 6e 2d 31 5d 20 en[0..nToken-1]
03f0: 62 6c 6f 62 20 61 72 72 61 79 20 69 73 20 61 20 blob array is a
0400: 70 61 72 73 65 20 6f 66 20 61 20 22 66 69 6c 65 parse of a "file
0410: 22 20 6c 69 6e 65 20 0a 2a 2a 20 6d 65 73 73 61 " line .** messa
0420: 67 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e ge. This routin
0430: 65 20 66 69 6e 69 73 68 65 73 20 70 61 72 73 69 e finishes parsi
0440: 6e 67 20 74 68 61 74 20 6d 65 73 73 61 67 65 20 ng that message
0450: 61 6e 64 20 64 6f 65 73 0a 2a 2a 20 61 20 72 65 and does.** a re
0460: 63 6f 72 64 20 69 6e 73 65 72 74 20 6f 66 20 74 cord insert of t
0470: 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 he file..**.** T
0480: 68 65 20 66 69 6c 65 20 6c 69 6e 65 20 69 73 20 he file line is
0490: 69 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f in one of the fo
04a0: 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 66 6f 72 6d llowing two form
04b0: 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 66 69 s:.**.** fi
04c0: 6c 65 20 55 55 49 44 20 53 49 5a 45 20 5c 6e 20 le UUID SIZE \n
04d0: 43 4f 4e 54 45 4e 54 0a 2a 2a 20 20 20 20 20 20 CONTENT.**
04e0: 66 69 6c 65 20 55 55 49 44 20 44 45 4c 54 41 53 file UUID DELTAS
04f0: 52 43 20 53 49 5a 45 20 5c 6e 20 43 4f 4e 54 45 RC SIZE \n CONTE
0500: 4e 54 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e NT.**.** The con
0510: 74 65 6e 74 20 69 73 20 53 49 5a 45 20 62 79 74 tent is SIZE byt
0520: 65 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 es immediately f
0530: 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 6e 65 77 ollowing the new
0540: 6c 69 6e 65 2e 0a 2a 2a 20 49 66 20 44 45 4c 54 line..** If DELT
0550: 41 53 52 43 20 65 78 69 73 74 73 2c 20 74 68 65 ASRC exists, the
0560: 6e 20 74 68 65 20 43 4f 4e 54 45 4e 54 20 69 73 n the CONTENT is
0570: 20 61 20 64 65 6c 74 61 20 61 67 61 69 6e 73 74 a delta against
0580: 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 the.** content
0590: 6f 66 20 44 45 4c 54 41 53 52 43 2e 0a 2a 2a 0a of DELTASRC..**.
05a0: 2a 2a 20 49 66 20 61 6e 79 20 65 72 72 6f 72 20 ** If any error
05b0: 6f 63 63 75 72 73 2c 20 77 72 69 74 65 20 61 20 occurs, write a
05c0: 6d 65 73 73 61 67 65 20 69 6e 74 6f 20 70 45 72 message into pEr
05d0: 72 20 77 68 69 63 68 20 68 61 73 20 61 6c 72 65 r which has alre
05e0: 61 64 79 0a 2a 2a 20 62 65 20 69 6e 69 74 69 61 ady.** be initia
05f0: 6c 69 7a 65 64 20 74 6f 20 61 6e 20 65 6d 70 74 lized to an empt
0600: 79 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 73 74 61 y string..*/.sta
0610: 74 69 63 20 76 6f 69 64 20 78 66 65 72 5f 61 63 tic void xfer_ac
0620: 63 65 70 74 5f 66 69 6c 65 28 42 6c 6f 62 20 2a cept_file(Blob *
0630: 70 49 6e 2c 20 42 6c 6f 62 20 2a 61 54 6f 6b 65 pIn, Blob *aToke
0640: 6e 2c 20 69 6e 74 20 6e 54 6f 6b 65 6e 2c 20 42 n, int nToken, B
0650: 6c 6f 62 20 2a 70 45 72 72 29 7b 0a 20 20 69 6e lob *pErr){. in
0660: 74 20 6e 3b 0a 20 20 69 6e 74 20 72 69 64 3b 0a t n;. int rid;.
0670: 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 2c 20 Blob content,
0680: 68 61 73 68 3b 0a 20 20 0a 20 20 69 66 28 20 6e hash;. . if( n
0690: 54 6f 6b 65 6e 3c 33 20 7c 7c 20 6e 54 6f 6b 65 Token<3 || nToke
06a0: 6e 3e 34 20 7c 7c 20 21 62 6c 6f 62 5f 69 73 5f n>4 || !blob_is_
06b0: 75 75 69 64 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 uuid(&aToken[1])
06c0: 0a 20 20 20 20 20 20 20 7c 7c 20 21 62 6c 6f 62 . || !blob
06d0: 5f 69 73 5f 69 6e 74 28 26 61 54 6f 6b 65 6e 5b _is_int(&aToken[
06e0: 6e 54 6f 6b 65 6e 2d 31 5d 2c 20 26 6e 29 20 7c nToken-1], &n) |
06f0: 7c 20 6e 3c 3d 30 0a 20 20 20 20 20 20 20 7c 7c | n<=0. ||
0700: 20 28 6e 54 6f 6b 65 6e 3d 3d 34 20 26 26 20 21 (nToken==4 && !
0710: 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26 61 54 blob_is_uuid(&aT
0720: 6f 6b 65 6e 5b 32 5d 29 29 20 29 7b 0a 20 20 20 oken[2])) ){.
0730: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70 45 blob_appendf(pE
0740: 72 72 2c 20 22 6d 61 6c 66 6f 72 6d 65 64 20 66 rr, "malformed f
0750: 69 6c 65 20 6c 69 6e 65 22 29 3b 0a 20 20 20 20 ile line");.
0760: 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 62 6c return;. }. bl
0770: 6f 62 5f 7a 65 72 6f 28 26 63 6f 6e 74 65 6e 74 ob_zero(&content
0780: 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 );. blob_zero(&
0790: 68 61 73 68 29 3b 0a 20 20 62 6c 6f 62 5f 65 78 hash);. blob_ex
07a0: 74 72 61 63 74 28 70 49 6e 2c 20 6e 2c 20 26 63 tract(pIn, n, &c
07b0: 6f 6e 74 65 6e 74 29 3b 0a 20 20 69 66 28 20 6e ontent);. if( n
07c0: 54 6f 6b 65 6e 3d 3d 34 20 29 7b 0a 20 20 20 20 Token==4 ){.
07d0: 42 6c 6f 62 20 73 72 63 3b 0a 20 20 20 20 69 6e Blob src;. in
07e0: 74 20 73 72 63 69 64 20 3d 20 64 62 5f 69 6e 74 t srcid = db_int
07f0: 28 30 2c 20 22 53 45 4c 45 43 54 20 72 69 64 20 (0, "SELECT rid
0800: 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 FROM blob WHERE
0810: 75 75 69 64 3d 25 42 22 2c 20 26 61 54 6f 6b 65 uuid=%B", &aToke
0820: 6e 5b 32 5d 29 3b 0a 20 20 20 20 69 66 28 20 73 n[2]);. if( s
0830: 72 63 69 64 3d 3d 30 20 29 7b 0a 20 20 20 20 20 rcid==0 ){.
0840: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70 45 blob_appendf(pE
0850: 72 72 2c 20 22 75 6e 6b 6e 6f 77 6e 20 64 65 6c rr, "unknown del
0860: 74 61 20 73 6f 75 72 63 65 3a 20 25 62 22 2c 20 ta source: %b",
0870: 26 61 54 6f 6b 65 6e 5b 32 5d 29 3b 0a 20 20 20 &aToken[2]);.
0880: 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d return;. }
0890: 0a 20 20 20 20 63 6f 6e 74 65 6e 74 5f 67 65 74 . content_get
08a0: 28 73 72 63 69 64 2c 20 26 73 72 63 29 3b 0a 20 (srcid, &src);.
08b0: 20 20 20 62 6c 6f 62 5f 64 65 6c 74 61 5f 61 70 blob_delta_ap
08c0: 70 6c 79 28 26 73 72 63 2c 20 26 63 6f 6e 74 65 ply(&src, &conte
08d0: 6e 74 2c 20 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 nt, &content);.
08e0: 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 73 blob_reset(&s
08f0: 72 63 29 3b 0a 20 20 7d 0a 20 20 73 68 61 31 73 rc);. }. sha1s
0900: 75 6d 5f 62 6c 6f 62 28 26 63 6f 6e 74 65 6e 74 um_blob(&content
0910: 2c 20 26 68 61 73 68 29 3b 0a 20 20 69 66 28 20 , &hash);. if(
0920: 21 62 6c 6f 62 5f 65 71 5f 73 74 72 28 26 61 54 !blob_eq_str(&aT
0930: 6f 6b 65 6e 5b 31 5d 2c 20 62 6c 6f 62 5f 73 74 oken[1], blob_st
0940: 72 28 26 68 61 73 68 29 2c 20 2d 31 29 20 29 7b r(&hash), -1) ){
0950: 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 . blob_append
0960: 66 28 70 45 72 72 2c 20 22 63 6f 6e 74 65 6e 74 f(pErr, "content
0970: 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 does not match
0980: 73 68 61 31 20 68 61 73 68 22 29 3b 0a 20 20 7d sha1 hash");. }
0990: 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 68 . blob_reset(&h
09a0: 61 73 68 29 3b 0a 20 20 72 69 64 20 3d 20 63 6f ash);. rid = co
09b0: 6e 74 65 6e 74 5f 70 75 74 28 26 63 6f 6e 74 65 ntent_put(&conte
09c0: 6e 74 2c 20 30 29 3b 0a 20 20 6d 61 6e 69 66 65 nt, 0);. manife
09d0: 73 74 5f 63 72 6f 73 73 6c 69 6e 6b 28 72 69 64 st_crosslink(rid
09e0: 2c 20 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 69 , &content);. i
09f0: 66 28 20 72 69 64 3d 3d 30 20 29 7b 0a 20 20 20 f( rid==0 ){.
0a00: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70 45 blob_appendf(pE
0a10: 72 72 2c 20 22 25 73 22 2c 20 67 2e 7a 45 72 72 rr, "%s", g.zErr
0a20: 4d 73 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a Msg);. }.}../*.
0a30: 2a 2a 20 53 65 6e 64 20 74 68 65 20 66 69 6c 65 ** Send the file
0a40: 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 72 identified by r
0a50: 69 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4f 75 id..**.** If pOu
0a60: 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 t is not NULL, t
0a70: 68 65 6e 20 61 70 70 65 6e 64 20 74 68 65 20 74 hen append the t
0a80: 65 78 74 20 6f 66 20 74 68 65 20 73 65 6e 64 20 ext of the send
0a90: 6d 65 73 73 61 67 65 0a 2a 2a 20 74 6f 20 70 4f message.** to pO
0aa0: 75 74 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 ut. Otherwise,
0ab0: 61 70 70 65 6e 64 20 74 68 65 20 74 65 78 74 20 append the text
0ac0: 74 6f 20 74 68 65 20 43 47 49 20 6f 75 74 70 75 to the CGI outpu
0ad0: 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 t..*/.static int
0ae0: 20 73 65 6e 64 5f 66 69 6c 65 28 69 6e 74 20 72 send_file(int r
0af0: 69 64 2c 20 42 6c 6f 62 20 2a 70 4f 75 74 29 7b id, Blob *pOut){
0b00: 0a 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 2c . Blob content,
0b10: 20 75 75 69 64 3b 0a 20 20 69 6e 74 20 73 69 7a uuid;. int siz
0b20: 65 3b 0a 0a 23 69 66 20 30 0a 53 45 4c 45 43 54 e;..#if 0.SELECT
0b30: 20 73 72 63 69 64 20 46 52 4f 4d 20 64 65 6c 74 srcid FROM delt
0b40: 61 0a 20 57 48 45 52 45 20 72 69 64 3d 25 64 0a a. WHERE rid=%d.
0b50: 20 20 20 41 4e 44 20 45 58 49 53 54 53 28 53 45 AND EXISTS(SE
0b60: 4c 45 43 54 20 31 20 46 52 4f 4d 20 6f 6e 72 65 LECT 1 FROM onre
0b70: 6d 6f 74 65 20 57 48 45 52 45 20 72 69 64 3d 73 mote WHERE rid=s
0b80: 72 63 69 64 29 0a 55 4e 49 4f 4e 20 41 4c 4c 0a rcid).UNION ALL.
0b90: 53 45 4c 45 43 54 20 69 64 20 46 52 4f 4d 20 64 SELECT id FROM d
0ba0: 65 6c 74 61 0a 20 57 48 45 52 45 20 73 72 63 69 elta. WHERE srci
0bb0: 64 3d 25 64 0a 20 20 20 41 4e 44 20 45 58 49 53 d=%d. AND EXIS
0bc0: 54 53 28 53 45 4c 45 43 54 20 31 20 46 52 4f 4d TS(SELECT 1 FROM
0bd0: 20 6f 6e 72 65 6d 6f 74 65 20 57 48 45 52 45 20 onremote WHERE
0be0: 72 69 64 3d 64 65 6c 74 61 2e 72 69 64 29 0a 4c rid=delta.rid).L
0bf0: 49 4d 49 54 20 31 0a 23 65 6e 64 69 66 0a 0a 20 IMIT 1.#endif..
0c00: 20 2f 2a 20 54 4f 44 4f 3a 0a 20 20 2a 2a 20 43 /* TODO:. ** C
0c10: 68 65 63 6b 20 66 6f 72 20 72 65 6c 61 74 65 64 heck for related
0c20: 20 66 69 6c 65 73 20 69 6e 20 74 68 65 20 6f 6e files in the on
0c30: 72 65 6d 6f 74 65 20 54 45 4d 50 20 74 61 62 6c remote TEMP tabl
0c40: 65 2e 20 20 49 66 20 72 65 6c 61 74 65 64 0a 20 e. If related.
0c50: 20 2a 2a 20 66 69 6c 65 73 20 61 72 65 20 66 6f ** files are fo
0c60: 75 6e 64 2c 20 74 68 65 6e 20 73 65 6e 64 20 61 und, then send a
0c70: 20 64 65 6c 74 61 20 72 61 74 68 65 72 20 74 68 delta rather th
0c80: 61 6e 20 74 68 65 20 77 68 6f 6c 65 20 66 69 6c an the whole fil
0c90: 65 2e 0a 20 20 2a 2f 0a 0a 20 20 62 6c 6f 62 5f e.. */.. blob_
0ca0: 7a 65 72 6f 28 26 75 75 69 64 29 3b 0a 20 20 64 zero(&uuid);. d
0cb0: 62 5f 62 6c 6f 62 28 26 75 75 69 64 2c 20 22 53 b_blob(&uuid, "S
0cc0: 45 4c 45 43 54 20 75 75 69 64 20 46 52 4f 4d 20 ELECT uuid FROM
0cd0: 62 6c 6f 62 20 57 48 45 52 45 20 72 69 64 3d 25 blob WHERE rid=%
0ce0: 64 20 41 4e 44 20 73 69 7a 65 3e 3d 30 22 2c 20 d AND size>=0",
0cf0: 72 69 64 29 3b 0a 20 20 69 66 28 20 62 6c 6f 62 rid);. if( blob
0d00: 5f 73 69 7a 65 28 26 75 75 69 64 29 3d 3d 30 20 _size(&uuid)==0
0d10: 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
0d20: 0a 20 20 7d 0a 20 20 63 6f 6e 74 65 6e 74 5f 67 . }. content_g
0d30: 65 74 28 72 69 64 2c 20 26 63 6f 6e 74 65 6e 74 et(rid, &content
0d40: 29 3b 0a 20 20 73 69 7a 65 20 3d 20 62 6c 6f 62 );. size = blob
0d50: 5f 73 69 7a 65 28 26 63 6f 6e 74 65 6e 74 29 3b _size(&content);
0d60: 0a 20 20 69 66 28 20 70 4f 75 74 20 29 7b 0a 20 . if( pOut ){.
0d70: 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 blob_appendf(
0d80: 70 4f 75 74 2c 20 22 66 69 6c 65 20 25 62 20 25 pOut, "file %b %
0d90: 64 5c 6e 22 2c 20 26 75 75 69 64 2c 20 73 69 7a d\n", &uuid, siz
0da0: 65 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 e);. blob_app
0db0: 65 6e 64 28 70 4f 75 74 2c 20 62 6c 6f 62 5f 62 end(pOut, blob_b
0dc0: 75 66 66 65 72 28 26 63 6f 6e 74 65 6e 74 29 2c uffer(&content),
0dd0: 20 73 69 7a 65 29 3b 0a 20 20 7d 65 6c 73 65 7b size);. }else{
0de0: 0a 20 20 20 20 63 67 69 5f 70 72 69 6e 74 66 28 . cgi_printf(
0df0: 22 66 69 6c 65 20 25 62 20 25 64 5c 6e 22 2c 20 "file %b %d\n",
0e00: 26 75 75 69 64 2c 20 73 69 7a 65 29 3b 0a 20 20 &uuid, size);.
0e10: 20 20 63 67 69 5f 61 70 70 65 6e 64 5f 63 6f 6e cgi_append_con
0e20: 74 65 6e 74 28 62 6c 6f 62 5f 62 75 66 66 65 72 tent(blob_buffer
0e30: 28 26 63 6f 6e 74 65 6e 74 29 2c 20 73 69 7a 65 (&content), size
0e40: 29 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f 72 65 );. }. blob_re
0e50: 73 65 74 28 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 set(&content);.
0e60: 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 75 75 69 blob_reset(&uui
0e70: 64 29 3b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 d);. db_multi_e
0e80: 78 65 63 28 22 49 4e 53 45 52 54 20 4f 52 20 49 xec("INSERT OR I
0e90: 47 4e 4f 52 45 20 49 4e 54 4f 20 6f 6e 72 65 6d GNORE INTO onrem
0ea0: 6f 74 65 20 56 41 4c 55 45 53 28 25 64 29 22 2c ote VALUES(%d)",
0eb0: 20 72 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 rid);. return
0ec0: 73 69 7a 65 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 size;.}.../*.**
0ed0: 53 65 6e 64 20 61 6c 6c 20 70 65 6e 64 69 6e 67 Send all pending
0ee0: 20 66 69 6c 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 files..*/.stati
0ef0: 63 20 69 6e 74 20 73 65 6e 64 5f 61 6c 6c 5f 70 c int send_all_p
0f00: 65 6e 64 69 6e 67 28 42 6c 6f 62 20 2a 70 4f 75 ending(Blob *pOu
0f10: 74 29 7b 0a 20 20 69 6e 74 20 69 52 69 64 53 65 t){. int iRidSe
0f20: 6e 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 73 65 nt = 0;. int se
0f30: 6e 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 53 nt = 0;. int nS
0f40: 65 6e 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6d ent = 0;. int m
0f50: 61 78 53 69 7a 65 20 3d 20 64 62 5f 67 65 74 5f axSize = db_get_
0f60: 69 6e 74 28 22 68 74 74 70 2d 6d 73 67 2d 73 69 int("http-msg-si
0f70: 7a 65 22 2c 20 31 30 30 30 30 30 30 29 3b 0a 20 ze", 1000000);.
0f80: 20 53 74 6d 74 20 71 3b 0a 23 69 66 20 30 0a 20 Stmt q;.#if 0.
0f90: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a db_multi_exec(.
0fa0: 20 20 20 20 22 43 52 45 41 54 45 20 54 45 4d 50 "CREATE TEMP
0fb0: 20 54 41 42 4c 45 20 70 72 69 6f 72 69 74 79 28 TABLE priority(
0fc0: 72 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d rid INTEGER PRIM
0fd0: 41 52 59 20 4b 45 59 29 3b 22 0a 20 20 20 20 22 ARY KEY);". "
0fe0: 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 72 69 6f INSERT INTO prio
0ff0: 72 69 74 79 22 0a 20 20 20 20 22 20 53 45 4c 45 rity". " SELE
1000: 43 54 20 73 72 63 69 64 20 46 52 4f 4d 20 64 65 CT srcid FROM de
1010: 6c 74 61 22 0a 20 20 20 20 22 20 20 57 48 45 52 lta". " WHER
1020: 45 20 45 58 49 53 54 53 28 53 45 4c 45 43 54 20 E EXISTS(SELECT
1030: 31 20 46 52 4f 4d 20 6f 6e 72 65 6d 6f 74 65 20 1 FROM onremote
1040: 57 48 45 52 45 20 6f 6e 72 65 6d 6f 74 65 2e 72 WHERE onremote.r
1050: 69 64 3d 64 65 6c 74 61 2e 72 69 64 29 22 0a 20 id=delta.rid)".
1060: 20 20 20 22 20 20 20 20 41 4e 44 20 45 58 49 53 " AND EXIS
1070: 54 53 28 53 45 4c 45 43 54 20 31 20 46 52 4f 4d TS(SELECT 1 FROM
1080: 20 70 65 6e 64 69 6e 67 20 57 48 45 52 45 20 64 pending WHERE d
1090: 65 6c 74 61 2e 73 72 63 69 64 3d 70 65 6e 64 69 elta.srcid=pendi
10a0: 6e 67 2e 72 69 64 29 3b 22 0a 20 20 20 20 22 49 ng.rid);". "I
10b0: 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 NSERT OR IGNORE
10c0: 49 4e 54 4f 20 70 72 69 6f 72 69 74 79 22 0a 20 INTO priority".
10d0: 20 20 20 22 20 53 45 4c 45 43 54 20 72 69 64 20 " SELECT rid
10e0: 46 52 4f 4d 20 64 65 6c 74 61 22 0a 20 20 20 20 FROM delta".
10f0: 22 20 20 57 48 45 52 45 20 45 58 49 53 54 53 28 " WHERE EXISTS(
1100: 53 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 6f 6e SELECT 1 FROM on
1110: 72 65 6d 6f 74 65 20 57 48 45 52 45 20 6f 6e 72 remote WHERE onr
1120: 65 6d 6f 74 65 2e 72 69 64 3d 64 65 6c 74 61 2e emote.rid=delta.
1130: 73 72 63 69 64 29 22 0a 20 20 20 20 22 20 20 20 srcid)". "
1140: 20 41 4e 44 20 45 58 49 53 54 53 28 53 45 4c 45 AND EXISTS(SELE
1150: 43 54 20 31 20 46 52 4f 4d 20 70 65 6e 64 69 6e CT 1 FROM pendin
1160: 67 20 57 48 45 52 45 20 64 65 6c 74 61 2e 72 69 g WHERE delta.ri
1170: 64 3d 70 65 6e 64 69 6e 67 2e 72 69 64 29 3b 22 d=pending.rid);"
1180: 0a 20 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 73 . );. while( s
1190: 65 6e 74 3c 6d 61 78 53 69 7a 65 20 26 26 20 28 ent<maxSize && (
11a0: 72 69 64 20 3d 20 64 62 5f 69 6e 74 28 30 2c 20 rid = db_int(0,
11b0: 22 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f 4d "SELECT rid FROM
11c0: 20 70 72 69 6f 72 69 74 79 22 29 29 21 3d 30 20 priority"))!=0
11d0: 29 7b 0a 20 20 20 20 73 65 6e 74 20 2b 3d 20 73 ){. sent += s
11e0: 65 6e 64 5f 66 69 6c 65 28 72 69 64 2c 20 70 4f end_file(rid, pO
11f0: 75 74 29 3b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 ut);. db_mult
1200: 69 5f 65 78 65 63 28 0a 20 20 20 20 20 20 22 49 i_exec(. "I
1210: 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 NSERT OR IGNORE
1220: 49 4e 54 4f 20 70 72 69 6f 72 69 74 79 22 0a 20 INTO priority".
1230: 20 20 20 20 20 22 20 53 45 4c 45 43 54 20 73 72 " SELECT sr
1240: 63 69 64 20 46 52 4f 4d 20 64 65 6c 74 61 20 57 cid FROM delta W
1250: 48 45 52 45 20 72 69 64 3d 25 64 22 0a 20 20 20 HERE rid=%d".
1260: 20 20 20 22 20 55 4e 49 4f 4e 20 41 4c 4c 22 0a " UNION ALL".
1270: 20 20 20 20 20 20 22 20 53 45 4c 45 43 54 20 72 " SELECT r
1280: 69 64 20 46 52 4f 4d 20 64 65 6c 74 61 20 57 48 id FROM delta WH
1290: 45 52 45 20 73 72 63 69 64 3d 25 64 22 2c 0a 20 ERE srcid=%d",.
12a0: 20 20 20 20 20 72 69 64 2c 20 72 69 64 0a 20 20 rid, rid.
12b0: 20 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a );. }.#endif.
12c0: 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 71 2c db_prepare(&q,
12d0: 20 22 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f "SELECT rid FRO
12e0: 4d 20 70 65 6e 64 69 6e 67 20 4f 52 44 45 52 20 M pending ORDER
12f0: 42 59 20 72 69 64 22 29 3b 0a 20 20 77 68 69 6c BY rid");. whil
1300: 65 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d e( db_step(&q)==
1310: 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 SQLITE_ROW ){.
1320: 20 20 69 6e 74 20 72 69 64 20 3d 20 64 62 5f 63 int rid = db_c
1330: 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 30 29 olumn_int(&q, 0)
1340: 3b 0a 20 20 20 20 69 66 28 20 73 65 6e 74 3c 6d ;. if( sent<m
1350: 61 78 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 axSize ){.
1360: 73 65 6e 74 20 2b 3d 20 73 65 6e 64 5f 66 69 6c sent += send_fil
1370: 65 28 72 69 64 2c 20 70 4f 75 74 29 3b 0a 20 20 e(rid, pOut);.
1380: 20 20 20 20 6e 53 65 6e 74 2b 2b 3b 0a 20 20 20 nSent++;.
1390: 20 20 20 69 52 69 64 53 65 6e 74 20 3d 20 72 69 iRidSent = ri
13a0: 64 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 d;. }else{.
13b0: 20 20 20 20 63 68 61 72 20 2a 7a 55 75 69 64 20 char *zUuid
13c0: 3d 20 64 62 5f 74 65 78 74 28 30 2c 0a 20 20 20 = db_text(0,.
13d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13e0: 20 20 20 22 53 45 4c 45 43 54 20 75 75 69 64 20 "SELECT uuid
13f0: 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 FROM blob WHERE
1400: 72 69 64 3d 25 64 20 41 4e 44 20 73 69 7a 65 3e rid=%d AND size>
1410: 3d 30 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 20 =0", rid);.
1420: 20 69 66 28 20 7a 55 75 69 64 20 29 7b 0a 20 20 if( zUuid ){.
1430: 20 20 20 20 20 20 69 66 28 20 70 4f 75 74 20 29 if( pOut )
1440: 7b 0a 20 20 20 20 20 20 20 20 20 20 62 6c 6f 62 {. blob
1450: 5f 61 70 70 65 6e 64 66 28 70 4f 75 74 2c 20 22 _appendf(pOut, "
1460: 69 67 6f 74 20 25 73 5c 6e 22 2c 20 7a 55 75 69 igot %s\n", zUui
1470: 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 d);. }els
1480: 65 7b 0a 20 20 20 20 20 20 20 20 20 20 63 67 69 e{. cgi
1490: 5f 70 72 69 6e 74 66 28 22 69 67 6f 74 20 25 73 _printf("igot %s
14a0: 5c 6e 22 2c 20 7a 55 75 69 64 29 3b 0a 20 20 20 \n", zUuid);.
14b0: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 66 }. f
14c0: 72 65 65 28 7a 55 75 69 64 29 3b 0a 20 20 20 20 ree(zUuid);.
14d0: 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 }. }. }.
14e0: 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b db_finalize(&q);
14f0: 0a 20 20 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 . . /* Delete
1500: 74 68 65 20 27 70 65 6e 64 69 6e 67 27 20 72 65 the 'pending' re
1510: 63 6f 72 64 73 20 66 6f 72 20 61 6c 6c 20 66 69 cords for all fi
1520: 6c 65 73 20 6a 75 73 74 20 73 65 6e 74 2e 20 4f les just sent. O
1530: 74 68 65 72 77 69 73 65 2c 0a 20 20 2a 2a 20 77 therwise,. ** w
1540: 65 20 63 61 6e 20 77 69 6e 64 20 75 70 20 73 65 e can wind up se
1550: 6e 64 69 6e 67 20 73 6f 6d 65 20 66 69 6c 65 73 nding some files
1560: 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2e more than once.
1570: 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 53 65 6e . */. if( nSen
1580: 74 3e 30 20 29 7b 0a 20 20 20 20 64 62 5f 6d 75 t>0 ){. db_mu
1590: 6c 74 69 5f 65 78 65 63 28 22 44 45 4c 45 54 45 lti_exec("DELETE
15a0: 20 46 52 4f 4d 20 70 65 6e 64 69 6e 67 20 57 48 FROM pending WH
15b0: 45 52 45 20 72 69 64 20 3c 3d 20 25 64 22 2c 20 ERE rid <= %d",
15c0: 69 52 69 64 53 65 6e 74 29 3b 0a 20 20 7d 0a 0a iRidSent);. }..
15d0: 23 69 66 20 30 0a 20 20 64 62 5f 6d 75 6c 74 69 #if 0. db_multi
15e0: 5f 65 78 65 63 28 22 44 52 4f 50 20 54 41 42 4c _exec("DROP TABL
15f0: 45 20 70 72 69 6f 72 69 74 79 22 29 3b 0a 23 65 E priority");.#e
1600: 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 6e 53 ndif. return nS
1610: 65 6e 74 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 ent;.}.../*.** C
1620: 68 65 63 6b 20 74 68 65 20 73 69 67 6e 61 74 75 heck the signatu
1630: 72 65 20 6f 6e 20 61 6e 20 61 70 70 6c 69 63 61 re on an applica
1640: 74 69 6f 6e 2f 78 2d 66 6f 73 73 69 6c 20 70 61 tion/x-fossil pa
1650: 79 6c 6f 61 64 20 72 65 63 65 69 76 65 64 20 62 yload received b
1660: 79 0a 2a 2a 20 74 68 65 20 48 54 54 50 20 73 65 y.** the HTTP se
1670: 72 76 65 72 2e 20 20 54 68 65 20 73 69 67 6e 61 rver. The signa
1680: 74 75 72 65 20 69 73 20 61 20 6c 69 6e 65 20 6f ture is a line o
1690: 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
16a0: 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 form:.**.**
16b0: 20 20 20 6c 6f 67 69 6e 20 4c 4f 47 49 4e 20 4e login LOGIN N
16c0: 4f 4e 43 45 20 53 49 47 4e 41 54 55 52 45 0a 2a ONCE SIGNATURE.*
16d0: 2a 0a 2a 2a 20 54 68 65 20 4e 4f 4e 43 45 20 69 *.** The NONCE i
16e0: 73 20 61 20 72 61 6e 64 6f 6d 20 73 74 72 69 6e s a random strin
16f0: 67 2e 20 20 54 68 65 20 73 65 72 76 65 72 20 77 g. The server w
1700: 69 6c 6c 20 6e 65 76 65 72 20 61 63 63 65 70 74 ill never accept
1710: 20 61 0a 2a 2a 20 72 65 70 65 61 74 20 4e 4f 4e a.** repeat NON
1720: 43 45 2e 20 20 53 49 47 4e 41 54 55 52 45 20 69 CE. SIGNATURE i
1730: 73 20 74 68 65 20 53 48 41 31 20 63 68 65 63 6b s the SHA1 check
1740: 73 75 6d 20 6f 66 20 74 68 65 20 4e 4f 4e 43 45 sum of the NONCE
1750: 20 0a 2a 2a 20 63 6f 6e 63 61 74 65 6e 61 74 65 .** concatenate
1760: 64 20 77 69 74 68 20 74 68 65 20 75 73 65 72 73 d with the users
1770: 20 70 61 73 73 77 6f 72 64 2e 0a 2a 2a 0a 2a 2a password..**.**
1780: 20 54 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 The parameters
1790: 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 to this routine
17a0: 61 72 65 20 65 70 68 65 72 6d 65 72 61 6c 20 62 are ephermeral b
17b0: 6c 6f 62 73 20 68 6f 6c 64 69 6e 67 20 74 68 65 lobs holding the
17c0: 0a 2a 2a 20 4c 4f 47 49 4e 2c 20 4e 4f 4e 43 45 .** LOGIN, NONCE
17d0: 20 61 6e 64 20 53 49 47 4e 41 54 55 52 45 2e 0a and SIGNATURE..
17e0: 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
17f0: 6e 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6c ne attempts to l
1800: 6f 63 61 74 65 20 74 68 65 20 75 73 65 72 20 61 ocate the user a
1810: 6e 64 20 76 65 72 69 66 79 20 74 68 65 20 73 69 nd verify the si
1820: 67 6e 61 74 75 72 65 2e 0a 2a 2a 20 49 66 20 65 gnature..** If e
1830: 76 65 72 79 74 68 69 6e 67 20 63 68 65 63 6b 73 verything checks
1840: 20 6f 75 74 2c 20 74 68 65 20 55 53 45 52 2e 43 out, the USER.C
1850: 41 50 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 74 68 AP column for th
1860: 65 20 55 53 45 52 20 74 61 62 6c 65 0a 2a 2a 20 e USER table.**
1870: 69 73 20 63 6f 6e 73 75 6c 74 65 64 20 74 6f 20 is consulted to
1880: 73 65 74 20 70 72 69 76 69 6c 65 67 65 73 20 69 set privileges i
1890: 6e 20 74 68 65 20 67 6c 6f 62 61 6c 20 67 20 76 n the global g v
18a0: 61 72 69 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 ariable..**.** I
18b0: 66 20 61 6e 79 74 68 69 6e 67 20 66 61 69 6c 73 f anything fails
18c0: 20 74 6f 20 63 68 65 63 6b 20 6f 75 74 2c 20 6e to check out, n
18d0: 6f 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d 61 o changes are ma
18e0: 64 65 20 74 6f 20 70 72 69 76 69 6c 65 67 65 73 de to privileges
18f0: 2e 0a 2a 2a 0a 2a 2a 20 53 69 67 6e 61 74 75 72 ..**.** Signatur
1900: 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 6e 20 e generation on
1910: 74 68 65 20 63 6c 69 65 6e 74 20 73 69 64 65 20 the client side
1920: 69 73 20 68 61 6e 64 6c 65 64 20 62 79 20 74 68 is handled by th
1930: 65 20 0a 2a 2a 20 68 74 74 70 5f 65 78 63 68 61 e .** http_excha
1940: 6e 67 65 28 29 20 72 6f 75 74 69 6e 65 2e 0a 2a nge() routine..*
1950: 2f 0a 76 6f 69 64 20 63 68 65 63 6b 5f 6c 6f 67 /.void check_log
1960: 69 6e 28 42 6c 6f 62 20 2a 70 4c 6f 67 69 6e 2c in(Blob *pLogin,
1970: 20 42 6c 6f 62 20 2a 70 4e 6f 6e 63 65 2c 20 42 Blob *pNonce, B
1980: 6c 6f 62 20 2a 70 53 69 67 29 7b 0a 20 20 53 74 lob *pSig){. St
1990: 6d 74 20 71 3b 0a 20 20 69 6e 74 20 72 63 3b 0a mt q;. int rc;.
19a0: 0a 20 20 69 66 28 20 64 62 5f 65 78 69 73 74 73 . if( db_exists
19b0: 28 22 53 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 ("SELECT 1 FROM
19c0: 72 63 76 66 72 6f 6d 20 57 48 45 52 45 20 6e 6f rcvfrom WHERE no
19d0: 6e 63 65 3d 25 42 22 2c 20 70 4e 6f 6e 63 65 29 nce=%B", pNonce)
19e0: 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 20 ){. return;
19f0: 20 2f 2a 20 4e 65 76 65 72 20 61 63 63 65 70 74 /* Never accept
1a00: 20 61 20 72 65 70 65 61 74 65 64 20 6e 6f 6e 63 a repeated nonc
1a10: 65 20 2a 2f 0a 20 20 7d 0a 20 20 64 62 5f 70 72 e */. }. db_pr
1a20: 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c 45 43 epare(&q, "SELEC
1a30: 54 20 70 77 2c 20 63 61 70 2c 20 75 69 64 20 46 T pw, cap, uid F
1a40: 52 4f 4d 20 75 73 65 72 20 57 48 45 52 45 20 6c ROM user WHERE l
1a50: 6f 67 69 6e 3d 25 42 22 2c 20 70 4c 6f 67 69 6e ogin=%B", pLogin
1a60: 29 3b 0a 20 20 69 66 28 20 64 62 5f 73 74 65 70 );. if( db_step
1a70: 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 (&q)==SQLITE_ROW
1a80: 20 29 7b 0a 20 20 20 20 42 6c 6f 62 20 70 77 2c ){. Blob pw,
1a90: 20 63 6f 6d 62 69 6e 65 64 2c 20 68 61 73 68 3b combined, hash;
1aa0: 0a 20 20 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 . blob_zero(&
1ab0: 70 77 29 3b 0a 20 20 20 20 64 62 5f 65 70 68 65 pw);. db_ephe
1ac0: 6d 65 72 61 6c 5f 62 6c 6f 62 28 26 71 2c 20 30 meral_blob(&q, 0
1ad0: 2c 20 26 70 77 29 3b 0a 20 20 20 20 62 6c 6f 62 , &pw);. blob
1ae0: 5f 7a 65 72 6f 28 26 63 6f 6d 62 69 6e 65 64 29 _zero(&combined)
1af0: 3b 0a 20 20 20 20 62 6c 6f 62 5f 63 6f 70 79 28 ;. blob_copy(
1b00: 26 63 6f 6d 62 69 6e 65 64 2c 20 70 4e 6f 6e 63 &combined, pNonc
1b10: 65 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 e);. blob_app
1b20: 65 6e 64 28 26 63 6f 6d 62 69 6e 65 64 2c 20 62 end(&combined, b
1b30: 6c 6f 62 5f 62 75 66 66 65 72 28 26 70 77 29 2c lob_buffer(&pw),
1b40: 20 62 6c 6f 62 5f 73 69 7a 65 28 26 70 77 29 29 blob_size(&pw))
1b50: 3b 0a 20 20 20 20 73 68 61 31 73 75 6d 5f 62 6c ;. sha1sum_bl
1b60: 6f 62 28 26 63 6f 6d 62 69 6e 65 64 2c 20 26 68 ob(&combined, &h
1b70: 61 73 68 29 3b 0a 20 20 20 20 72 63 20 3d 20 62 ash);. rc = b
1b80: 6c 6f 62 5f 63 6f 6d 70 61 72 65 28 26 68 61 73 lob_compare(&has
1b90: 68 2c 20 70 53 69 67 29 3b 0a 20 20 20 20 62 6c h, pSig);. bl
1ba0: 6f 62 5f 72 65 73 65 74 28 26 68 61 73 68 29 3b ob_reset(&hash);
1bb0: 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 . blob_reset(
1bc0: 26 63 6f 6d 62 69 6e 65 64 29 3b 0a 20 20 20 20 &combined);.
1bd0: 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20 if( rc==0 ){.
1be0: 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
1bf0: 43 61 70 3b 0a 20 20 20 20 20 20 7a 43 61 70 20 Cap;. zCap
1c00: 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 = db_column_text
1c10: 28 26 71 2c 20 31 29 3b 0a 20 20 20 20 20 20 6c (&q, 1);. l
1c20: 6f 67 69 6e 5f 73 65 74 5f 63 61 70 61 62 69 6c ogin_set_capabil
1c30: 69 74 69 65 73 28 7a 43 61 70 29 3b 0a 20 20 20 ities(zCap);.
1c40: 20 20 20 67 2e 75 73 65 72 55 69 64 20 3d 20 64 g.userUid = d
1c50: 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c b_column_int(&q,
1c60: 20 32 29 3b 0a 20 20 20 20 20 20 67 2e 7a 4c 6f 2);. g.zLo
1c70: 67 69 6e 20 3d 20 6d 70 72 69 6e 74 66 28 22 25 gin = mprintf("%
1c80: 62 22 2c 20 70 4c 6f 67 69 6e 29 3b 0a 20 20 20 b", pLogin);.
1c90: 20 20 20 67 2e 7a 4e 6f 6e 63 65 20 3d 20 6d 70 g.zNonce = mp
1ca0: 72 69 6e 74 66 28 22 25 62 22 2c 20 70 4e 6f 6e rintf("%b", pNon
1cb0: 63 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 ce);. }. }.
1cc0: 20 64 62 5f 72 65 73 65 74 28 26 71 29 3b 0a 7d db_reset(&q);.}
1cd0: 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 69 73 .../*.** If this
1ce0: 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 variable is set
1cf0: 2c 20 64 69 73 61 62 6c 65 20 6c 6f 67 69 6e 20 , disable login
1d00: 63 68 65 63 6b 73 2e 20 20 55 73 65 64 20 66 6f checks. Used fo
1d10: 72 20 64 65 62 75 67 67 69 6e 67 0a 2a 2a 20 6f r debugging.** o
1d20: 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 nly..*/.static i
1d30: 6e 74 20 64 69 73 61 62 6c 65 4c 6f 67 69 6e 20 nt disableLogin
1d40: 3d 20 30 3b 0a 0a 2f 2a 0a 2a 2a 20 57 45 42 50 = 0;../*.** WEBP
1d50: 41 47 45 3a 20 78 66 65 72 0a 2a 2a 0a 2a 2a 20 AGE: xfer.**.**
1d60: 54 68 69 73 20 69 73 20 74 68 65 20 74 72 61 6e This is the tran
1d70: 73 66 65 72 20 68 61 6e 64 6c 65 72 20 6f 6e 20 sfer handler on
1d80: 74 68 65 20 73 65 72 76 65 72 20 73 69 64 65 2e the server side.
1d90: 20 20 54 68 65 20 74 72 61 6e 73 66 65 72 0a 2a The transfer.*
1da0: 2a 20 6d 65 73 73 61 67 65 20 68 61 73 20 62 65 * message has be
1db0: 65 6e 20 75 6e 63 6f 6d 70 72 65 73 73 65 64 20 en uncompressed
1dc0: 61 6e 64 20 70 6c 61 63 65 64 20 69 6e 20 74 68 and placed in th
1dd0: 65 20 67 2e 63 67 69 49 6e 20 62 6c 6f 62 2e 0a e g.cgiIn blob..
1de0: 2a 2a 20 50 72 6f 63 65 73 73 20 74 68 69 73 20 ** Process this
1df0: 6d 65 73 73 61 67 65 20 61 6e 64 20 66 6f 72 6d message and form
1e00: 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 an appropriate
1e10: 72 65 70 6c 79 2e 0a 2a 2f 0a 76 6f 69 64 20 70 reply..*/.void p
1e20: 61 67 65 5f 78 66 65 72 28 76 6f 69 64 29 7b 0a age_xfer(void){.
1e30: 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 0a 20 20 int nToken;.
1e40: 69 6e 74 20 69 73 50 75 6c 6c 20 3d 20 30 3b 0a int isPull = 0;.
1e50: 20 20 69 6e 74 20 69 73 50 75 73 68 20 3d 20 30 int isPush = 0
1e60: 3b 0a 20 20 69 6e 74 20 6e 45 72 72 20 3d 20 30 ;. int nErr = 0
1e70: 3b 0a 20 20 42 6c 6f 62 20 6c 69 6e 65 2c 20 65 ;. Blob line, e
1e80: 72 72 6d 73 67 2c 20 61 54 6f 6b 65 6e 5b 35 5d rrmsg, aToken[5]
1e90: 3b 0a 0a 20 20 64 62 5f 62 65 67 69 6e 5f 74 72 ;.. db_begin_tr
1ea0: 61 6e 73 61 63 74 69 6f 6e 28 29 3b 0a 20 20 62 ansaction();. b
1eb0: 6c 6f 62 61 72 72 61 79 5f 7a 65 72 6f 28 61 54 lobarray_zero(aT
1ec0: 6f 6b 65 6e 2c 20 63 6f 75 6e 74 28 61 54 6f 6b oken, count(aTok
1ed0: 65 6e 29 29 3b 0a 20 20 63 67 69 5f 73 65 74 5f en));. cgi_set_
1ee0: 63 6f 6e 74 65 6e 74 5f 74 79 70 65 28 67 2e 7a content_type(g.z
1ef0: 43 6f 6e 74 65 6e 74 54 79 70 65 29 3b 0a 20 20 ContentType);.
1f00: 62 6c 6f 62 5f 7a 65 72 6f 28 26 65 72 72 6d 73 blob_zero(&errms
1f10: 67 29 3b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 g);. db_multi_e
1f20: 78 65 63 28 0a 20 20 20 20 20 22 43 52 45 41 54 xec(. "CREAT
1f30: 45 20 54 45 4d 50 20 54 41 42 4c 45 20 6f 6e 72 E TEMP TABLE onr
1f40: 65 6d 6f 74 65 28 72 69 64 20 49 4e 54 45 47 45 emote(rid INTEGE
1f50: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 29 3b 22 R PRIMARY KEY);"
1f60: 20 2f 2a 20 43 6c 69 65 6e 74 20 68 61 73 20 2a /* Client has *
1f70: 2f 0a 20 20 20 20 20 22 43 52 45 41 54 45 20 54 /. "CREATE T
1f80: 45 4d 50 20 54 41 42 4c 45 20 70 65 6e 64 69 6e EMP TABLE pendin
1f90: 67 28 72 69 64 20 49 4e 54 45 47 45 52 20 50 52 g(rid INTEGER PR
1fa0: 49 4d 41 52 59 20 4b 45 59 29 3b 22 20 20 2f 2a IMARY KEY);" /*
1fb0: 20 43 6c 69 65 6e 74 20 6e 65 65 64 73 20 2a 2f Client needs */
1fc0: 0a 20 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 62 . );. while( b
1fd0: 6c 6f 62 5f 6c 69 6e 65 28 26 67 2e 63 67 69 49 lob_line(&g.cgiI
1fe0: 6e 2c 20 26 6c 69 6e 65 29 20 29 7b 0a 20 20 20 n, &line) ){.
1ff0: 20 6e 54 6f 6b 65 6e 20 3d 20 62 6c 6f 62 5f 74 nToken = blob_t
2000: 6f 6b 65 6e 69 7a 65 28 26 6c 69 6e 65 2c 20 61 okenize(&line, a
2010: 54 6f 6b 65 6e 2c 20 63 6f 75 6e 74 28 61 54 6f Token, count(aTo
2020: 6b 65 6e 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 20 ken));.. /*
2030: 20 66 69 6c 65 20 55 55 49 44 20 53 49 5a 45 20 file UUID SIZE
2040: 5c 6e 20 43 4f 4e 54 45 4e 54 0a 20 20 20 20 2a \n CONTENT. *
2050: 2a 20 20 20 66 69 6c 65 20 55 55 49 44 20 44 45 * file UUID DE
2060: 4c 54 41 53 52 43 20 53 49 5a 45 20 5c 6e 20 43 LTASRC SIZE \n C
2070: 4f 4e 54 45 4e 54 0a 20 20 20 20 2a 2a 0a 20 20 ONTENT. **.
2080: 20 20 2a 2a 20 41 63 63 65 70 74 20 61 20 66 69 ** Accept a fi
2090: 6c 65 20 66 72 6f 6d 20 74 68 65 20 63 6c 69 65 le from the clie
20a0: 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 nt.. */. i
20b0: 66 28 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b f( blob_eq(&aTok
20c0: 65 6e 5b 30 5d 2c 20 22 66 69 6c 65 22 29 20 26 en[0], "file") &
20d0: 26 20 6e 54 6f 6b 65 6e 3e 3d 32 20 26 26 20 6e & nToken>=2 && n
20e0: 54 6f 6b 65 6e 3c 3d 33 20 29 7b 0a 20 20 20 20 Token<=3 ){.
20f0: 20 20 69 66 28 20 21 69 73 50 75 73 68 20 29 7b if( !isPush ){
2100: 0a 20 20 20 20 20 20 20 20 63 67 69 5f 72 65 73 . cgi_res
2110: 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 et_content();.
2120: 20 20 20 20 20 20 40 20 65 72 72 6f 72 20 6e 6f @ error no
2130: 74 5c 73 61 75 74 68 6f 72 69 7a 65 64 5c 73 74 t\sauthorized\st
2140: 6f 5c 73 77 72 69 74 65 0a 20 20 20 20 20 20 20 o\swrite.
2150: 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 nErr++;.
2160: 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
2170: 20 20 20 20 20 20 78 66 65 72 5f 61 63 63 65 70 xfer_accep
2180: 74 5f 66 69 6c 65 28 26 67 2e 63 67 69 49 6e 2c t_file(&g.cgiIn,
2190: 20 61 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 2c aToken, nToken,
21a0: 20 26 65 72 72 6d 73 67 29 3b 0a 20 20 20 20 20 &errmsg);.
21b0: 20 69 66 28 20 62 6c 6f 62 5f 73 69 7a 65 28 26 if( blob_size(&
21c0: 65 72 72 6d 73 67 29 20 29 7b 0a 20 20 20 20 20 errmsg) ){.
21d0: 20 20 20 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e cgi_reset_con
21e0: 74 65 6e 74 28 29 3b 0a 20 20 20 20 20 20 20 20 tent();.
21f0: 40 20 65 72 72 6f 72 20 25 54 28 62 6c 6f 62 5f @ error %T(blob_
2200: 73 74 72 28 26 65 72 72 6d 73 67 29 29 0a 20 20 str(&errmsg)).
2210: 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 nErr++;.
2220: 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
2230: 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 0a 0a }. }else..
2240: 20 20 20 20 2f 2a 20 20 20 67 69 6d 6d 65 20 55 /* gimme U
2250: 55 49 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a UID. **. *
2260: 2a 20 43 6c 69 65 6e 74 20 69 73 20 72 65 71 75 * Client is requ
2270: 65 73 74 69 6e 67 20 61 20 66 69 6c 65 0a 20 20 esting a file.
2280: 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 62 6c 6f */. if( blo
2290: 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c b_eq(&aToken[0],
22a0: 20 22 67 69 6d 6d 65 22 29 20 26 26 20 6e 54 6f "gimme") && nTo
22b0: 6b 65 6e 3d 3d 32 20 26 26 20 62 6c 6f 62 5f 69 ken==2 && blob_i
22c0: 73 5f 75 75 69 64 28 26 61 54 6f 6b 65 6e 5b 31 s_uuid(&aToken[1
22d0: 5d 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ]) ){. if(
22e0: 69 73 50 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 isPull ){.
22f0: 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 db_multi_exec(
2300: 0a 20 20 20 20 20 20 20 20 20 20 22 49 4e 53 45 . "INSE
2310: 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 RT OR IGNORE INT
2320: 4f 20 70 65 6e 64 69 6e 67 28 72 69 64 29 20 22 O pending(rid) "
2330: 0a 20 20 20 20 20 20 20 20 20 20 22 53 45 4c 45 . "SELE
2340: 43 54 20 72 69 64 20 46 52 4f 4d 20 62 6c 6f 62 CT rid FROM blob
2350: 20 57 48 45 52 45 20 75 75 69 64 3d 25 42 20 41 WHERE uuid=%B A
2360: 4e 44 20 73 69 7a 65 3e 3d 30 22 2c 20 26 61 54 ND size>=0", &aT
2370: 6f 6b 65 6e 5b 31 5d 0a 20 20 20 20 20 20 20 20 oken[1].
2380: 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
2390: 65 6c 73 65 0a 0a 20 20 20 20 2f 2a 20 20 20 69 else.. /* i
23a0: 67 6f 74 20 55 55 49 44 0a 20 20 20 20 2a 2a 20 got UUID. **
23b0: 20 20 6c 65 61 66 20 55 55 49 44 0a 20 20 20 20 leaf UUID.
23c0: 2a 2a 0a 20 20 20 20 2a 2a 20 43 6c 69 65 6e 74 **. ** Client
23d0: 20 61 6e 6e 6f 75 6e 63 65 73 20 74 68 61 74 20 announces that
23e0: 69 74 20 68 61 73 20 61 20 70 61 72 74 69 63 75 it has a particu
23f0: 6c 61 72 20 66 69 6c 65 0a 20 20 20 20 2a 2f 0a lar file. */.
2400: 20 20 20 20 69 66 28 20 6e 54 6f 6b 65 6e 3d 3d if( nToken==
2410: 32 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 28 2. && (
2420: 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b blob_eq(&aToken[
2430: 30 5d 2c 20 22 69 67 6f 74 22 29 20 7c 7c 20 62 0], "igot") || b
2440: 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 lob_eq(&aToken[0
2450: 5d 2c 22 6c 65 61 66 22 29 29 0a 20 20 20 20 20 ],"leaf")).
2460: 20 20 20 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f && blob_is_
2470: 75 75 69 64 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 uuid(&aToken[1])
2480: 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73 ){. if( is
2490: 50 75 6c 6c 20 7c 7c 20 69 73 50 75 73 68 20 29 Pull || isPush )
24a0: 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 69 {. int ri
24b0: 64 20 3d 20 64 62 5f 69 6e 74 28 30 2c 20 22 53 d = db_int(0, "S
24c0: 45 4c 45 43 54 20 72 69 64 20 46 52 4f 4d 20 62 ELECT rid FROM b
24d0: 6c 6f 62 20 57 48 45 52 45 20 75 75 69 64 3d 25 lob WHERE uuid=%
24e0: 42 22 2c 20 26 61 54 6f 6b 65 6e 5b 31 5d 29 3b B", &aToken[1]);
24f0: 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 69 64 . if( rid
2500: 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 >0 ){.
2510: 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 db_multi_exec(.
2520: 20 20 20 20 20 20 20 20 20 20 20 22 49 4e 53 45 "INSE
2530: 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 RT OR IGNORE INT
2540: 4f 20 6f 6e 72 65 6d 6f 74 65 28 72 69 64 29 20 O onremote(rid)
2550: 56 41 4c 55 45 53 28 25 64 29 22 2c 20 72 69 64 VALUES(%d)", rid
2560: 0a 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20 20 . );.
2570: 20 20 20 20 20 20 20 20 69 66 28 20 69 73 50 75 if( isPu
2580: 6c 6c 20 26 26 20 62 6c 6f 62 5f 65 71 28 26 61 ll && blob_eq(&a
2590: 54 6f 6b 65 6e 5b 30 5d 2c 20 22 6c 65 61 66 22 Token[0], "leaf"
25a0: 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 ) ){.
25b0: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a db_multi_exec(.
25c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 49 "I
25d0: 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 NSERT OR IGNORE
25e0: 49 4e 54 4f 20 70 65 6e 64 69 6e 67 28 72 69 64 INTO pending(rid
25f0: 29 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 ) ".
2600: 20 20 22 53 45 4c 45 43 54 20 63 69 64 20 46 52 "SELECT cid FR
2610: 4f 4d 20 70 6c 69 6e 6b 20 57 48 45 52 45 20 70 OM plink WHERE p
2620: 69 64 3d 25 64 22 2c 20 72 69 64 0a 20 20 20 20 id=%d", rid.
2630: 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 );.
2640: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
2650: 65 6c 73 65 20 69 66 28 20 69 73 50 75 73 68 20 else if( isPush
2660: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e ){. con
2670: 74 65 6e 74 5f 70 75 74 28 30 2c 20 62 6c 6f 62 tent_put(0, blob
2680: 5f 73 74 72 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 _str(&aToken[1])
2690: 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
26a0: 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 0a 0a }. }else..
26b0: 20 20 20 20 2f 2a 20 20 20 20 70 75 6c 6c 20 20 /* pull
26c0: 53 45 52 56 45 52 43 4f 44 45 20 20 50 52 4f 4a SERVERCODE PROJ
26d0: 45 43 54 43 4f 44 45 0a 20 20 20 20 2a 2a 20 20 ECTCODE. **
26e0: 20 20 70 75 73 68 20 20 53 45 52 56 45 52 43 4f push SERVERCO
26f0: 44 45 20 20 50 52 4f 4a 45 43 54 43 4f 44 45 0a DE PROJECTCODE.
2700: 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 **. ** Th
2710: 65 20 63 6c 69 65 6e 74 20 77 61 6e 74 73 20 65 e client wants e
2720: 69 74 68 65 72 20 73 65 6e 64 20 6f 72 20 72 65 ither send or re
2730: 63 65 69 76 65 0a 20 20 20 20 2a 2f 0a 20 20 20 ceive. */.
2740: 20 69 66 28 20 6e 54 6f 6b 65 6e 3d 3d 33 0a 20 if( nToken==3.
2750: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 &&
2760: 20 28 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 (blob_eq(&aToke
2770: 6e 5b 30 5d 2c 20 22 70 75 6c 6c 22 29 20 7c 7c n[0], "pull") ||
2780: 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e blob_eq(&aToken
2790: 5b 30 5d 2c 20 22 70 75 73 68 22 29 29 0a 20 20 [0], "push")).
27a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 &&
27b0: 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26 61 54 blob_is_uuid(&aT
27c0: 6f 6b 65 6e 5b 31 5d 29 20 26 26 20 62 6c 6f 62 oken[1]) && blob
27d0: 5f 69 73 5f 75 75 69 64 28 26 61 54 6f 6b 65 6e _is_uuid(&aToken
27e0: 5b 32 5d 29 20 29 7b 0a 20 20 20 20 20 20 63 6f [2]) ){. co
27f0: 6e 73 74 20 63 68 61 72 20 2a 7a 53 43 6f 64 65 nst char *zSCode
2800: 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 ;. const ch
2810: 61 72 20 2a 7a 50 43 6f 64 65 3b 0a 0a 20 20 20 ar *zPCode;..
2820: 20 20 20 7a 53 43 6f 64 65 20 3d 20 64 62 5f 67 zSCode = db_g
2830: 65 74 28 22 73 65 72 76 65 72 2d 63 6f 64 65 22 et("server-code"
2840: 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 , 0);. if(
2850: 7a 53 43 6f 64 65 3d 3d 30 20 29 7b 0a 20 20 20 zSCode==0 ){.
2860: 20 20 20 20 20 66 6f 73 73 69 6c 5f 70 61 6e 69 fossil_pani
2870: 63 28 22 6d 69 73 73 69 6e 67 20 73 65 72 76 65 c("missing serve
2880: 72 20 63 6f 64 65 22 29 3b 0a 20 20 20 20 20 20 r code");.
2890: 7d 0a 20 20 20 20 20 20 69 66 28 20 62 6c 6f 62 }. if( blob
28a0: 5f 65 71 5f 73 74 72 28 26 61 54 6f 6b 65 6e 5b _eq_str(&aToken[
28b0: 31 5d 2c 20 7a 53 43 6f 64 65 2c 20 2d 31 29 20 1], zSCode, -1)
28c0: 29 7b 0a 20 20 20 20 20 20 20 20 63 67 69 5f 72 ){. cgi_r
28d0: 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a eset_content();.
28e0: 20 20 20 20 20 20 20 20 40 20 65 72 72 6f 72 20 @ error
28f0: 73 65 72 76 65 72 5c 73 6c 6f 6f 70 0a 20 20 20 server\sloop.
2900: 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 nErr++;.
2910: 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
2920: 20 20 7d 0a 20 20 20 20 20 20 7a 50 43 6f 64 65 }. zPCode
2930: 20 3d 20 64 62 5f 67 65 74 28 22 70 72 6f 6a 65 = db_get("proje
2940: 63 74 2d 63 6f 64 65 22 2c 20 30 29 3b 0a 20 20 ct-code", 0);.
2950: 20 20 20 20 69 66 28 20 7a 50 43 6f 64 65 3d 3d if( zPCode==
2960: 30 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6f 73 0 ){. fos
2970: 73 69 6c 5f 70 61 6e 69 63 28 22 6d 69 73 73 69 sil_panic("missi
2980: 6e 67 20 70 72 6f 6a 65 63 74 20 63 6f 64 65 22 ng project code"
2990: 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
29a0: 20 69 66 28 20 21 62 6c 6f 62 5f 65 71 5f 73 74 if( !blob_eq_st
29b0: 72 28 26 61 54 6f 6b 65 6e 5b 32 5d 2c 20 7a 50 r(&aToken[2], zP
29c0: 43 6f 64 65 2c 20 2d 31 29 20 29 7b 0a 20 20 20 Code, -1) ){.
29d0: 20 20 20 20 20 63 67 69 5f 72 65 73 65 74 5f 63 cgi_reset_c
29e0: 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 20 20 20 20 ontent();.
29f0: 20 20 40 20 65 72 72 6f 72 20 77 72 6f 6e 67 5c @ error wrong\
2a00: 73 70 72 6f 6a 65 63 74 0a 20 20 20 20 20 20 20 sproject.
2a10: 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 nErr++;.
2a20: 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
2a30: 20 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 if( blob_e
2a40: 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 70 q(&aToken[0], "p
2a50: 75 6c 6c 22 29 20 29 7b 0a 20 20 20 20 20 20 20 ull") ){.
2a60: 20 69 66 28 20 21 67 2e 6f 6b 52 65 61 64 20 29 if( !g.okRead )
2a70: 7b 0a 20 20 20 20 20 20 20 20 20 20 63 67 69 5f {. cgi_
2a80: 72 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b reset_content();
2a90: 0a 20 20 20 20 20 20 20 20 20 20 40 20 65 72 72 . @ err
2aa0: 6f 72 20 6e 6f 74 5c 73 61 75 74 68 6f 72 69 7a or not\sauthoriz
2ab0: 65 64 5c 73 74 6f 5c 73 72 65 61 64 0a 20 20 20 ed\sto\sread.
2ac0: 20 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 nErr++;.
2ad0: 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a break;.
2ae0: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
2af0: 20 20 69 73 50 75 6c 6c 20 3d 20 31 3b 0a 20 20 isPull = 1;.
2b00: 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
2b10: 20 20 20 69 66 28 20 21 67 2e 6f 6b 57 72 69 74 if( !g.okWrit
2b20: 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 e ){. c
2b30: 67 69 5f 72 65 73 65 74 5f 63 6f 6e 74 65 6e 74 gi_reset_content
2b40: 28 29 3b 0a 20 20 20 20 20 20 20 20 20 20 40 20 ();. @
2b50: 65 72 72 6f 72 20 6e 6f 74 5c 73 61 75 74 68 6f error not\sautho
2b60: 72 69 7a 65 64 5c 73 74 6f 5c 73 77 72 69 74 65 rized\sto\swrite
2b70: 0a 20 20 20 20 20 20 20 20 20 20 6e 45 72 72 2b . nErr+
2b80: 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 +;. bre
2b90: 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ak;. }.
2ba0: 20 20 20 20 20 20 69 73 50 75 73 68 20 3d 20 31 isPush = 1
2bb0: 3b 0a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d ;.. }. }
2bc0: 65 6c 73 65 0a 0a 20 20 20 20 2f 2a 20 20 20 20 else.. /*
2bd0: 63 6c 6f 6e 65 0a 20 20 20 20 2a 2a 0a 20 20 20 clone. **.
2be0: 20 2a 2a 20 54 68 65 20 63 6c 69 65 6e 74 20 6b ** The client k
2bf0: 6e 6f 77 73 20 6e 6f 74 68 69 6e 67 2e 20 20 54 nows nothing. T
2c00: 65 6c 6c 20 61 6c 6c 2e 0a 20 20 20 20 2a 2f 0a ell all.. */.
2c10: 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 if( blob_eq(
2c20: 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 63 6c 6f &aToken[0], "clo
2c30: 6e 65 22 29 20 29 7b 0a 20 20 20 20 20 20 69 66 ne") ){. if
2c40: 28 20 21 67 2e 6f 6b 52 65 61 64 20 29 7b 0a 20 ( !g.okRead ){.
2c50: 20 20 20 20 20 20 20 63 67 69 5f 72 65 73 65 74 cgi_reset
2c60: 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 20 20 _content();.
2c70: 20 20 20 20 40 20 65 72 72 6f 72 20 6e 6f 74 5c @ error not\
2c80: 73 61 75 74 68 6f 72 69 7a 65 64 5c 73 74 6f 5c sauthorized\sto\
2c90: 73 72 65 61 64 0a 20 20 20 20 20 20 20 20 6e 45 sread. nE
2ca0: 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 62 72 rr++;. br
2cb0: 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
2cc0: 20 20 20 69 73 50 75 6c 6c 20 3d 20 31 3b 0a 20 isPull = 1;.
2cd0: 20 20 20 20 20 40 20 70 75 73 68 20 25 73 28 64 @ push %s(d
2ce0: 62 5f 67 65 74 28 22 73 65 72 76 65 72 2d 63 6f b_get("server-co
2cf0: 64 65 22 2c 20 22 78 22 29 29 20 25 73 28 64 62 de", "x")) %s(db
2d00: 5f 67 65 74 28 22 70 72 6f 6a 65 63 74 2d 63 6f _get("project-co
2d10: 64 65 22 2c 20 22 78 22 29 29 0a 20 20 20 20 20 de", "x")).
2d20: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a db_multi_exec(.
2d30: 20 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 20 "INSERT
2d40: 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 70 OR IGNORE INTO p
2d50: 65 6e 64 69 6e 67 28 72 69 64 29 20 22 0a 20 20 ending(rid) ".
2d60: 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 72 69 "SELECT ri
2d70: 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 d FROM blob WHER
2d80: 45 20 73 69 7a 65 3e 3d 30 22 0a 20 20 20 20 20 E size>=0".
2d90: 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 0a 20 );. }else..
2da0: 20 20 20 2f 2a 20 20 20 20 6c 6f 67 69 6e 20 20 /* login
2db0: 55 53 45 52 20 20 4e 4f 4e 43 45 20 20 53 49 47 USER NONCE SIG
2dc0: 4e 41 54 55 52 45 0a 20 20 20 20 2a 2a 0a 20 20 NATURE. **.
2dd0: 20 20 2a 2a 20 43 68 65 63 6b 20 66 6f 72 20 61 ** Check for a
2de0: 20 76 61 6c 69 64 20 6c 6f 67 69 6e 2e 20 20 54 valid login. T
2df0: 68 69 73 20 68 61 73 20 74 6f 20 68 61 70 70 65 his has to happe
2e00: 6e 20 62 65 66 6f 72 65 20 61 6e 79 74 68 69 6e n before anythin
2e10: 67 20 65 6c 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 g else.. */.
2e20: 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 if( blob_eq(&
2e30: 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 6c 6f 67 69 aToken[0], "logi
2e40: 6e 22 29 20 26 26 20 6e 54 6f 6b 65 6e 3d 3d 34 n") && nToken==4
2e50: 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 64 69 ){. if( di
2e60: 73 61 62 6c 65 4c 6f 67 69 6e 20 29 7b 0a 20 20 sableLogin ){.
2e70: 20 20 20 20 20 20 67 2e 6f 6b 52 65 61 64 20 3d g.okRead =
2e80: 20 67 2e 6f 6b 57 72 69 74 65 20 3d 20 31 3b 0a g.okWrite = 1;.
2e90: 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
2ea0: 20 20 20 20 20 63 68 65 63 6b 5f 6c 6f 67 69 6e check_login
2eb0: 28 26 61 54 6f 6b 65 6e 5b 31 5d 2c 20 26 61 54 (&aToken[1], &aT
2ec0: 6f 6b 65 6e 5b 32 5d 2c 20 26 61 54 6f 6b 65 6e oken[2], &aToken
2ed0: 5b 33 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 [3]);. }.
2ee0: 20 20 7d 65 6c 73 65 0a 0a 20 20 20 20 2f 2a 20 }else.. /*
2ef0: 55 6e 6b 6e 6f 77 6e 20 6d 65 73 73 61 67 65 0a Unknown message.
2f00: 20 20 20 20 2a 2f 0a 20 20 20 20 7b 0a 20 20 20 */. {.
2f10: 20 20 20 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e cgi_reset_con
2f20: 74 65 6e 74 28 29 3b 0a 20 20 20 20 20 20 40 20 tent();. @
2f30: 65 72 72 6f 72 20 62 61 64 5c 73 63 6f 6d 6d 61 error bad\scomma
2f40: 6e 64 3a 5c 73 25 46 28 62 6c 6f 62 5f 73 74 72 nd:\s%F(blob_str
2f50: 28 26 6c 69 6e 65 29 29 0a 20 20 20 20 7d 0a 20 (&line)). }.
2f60: 20 20 20 62 6c 6f 62 61 72 72 61 79 5f 72 65 73 blobarray_res
2f70: 65 74 28 61 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 et(aToken, nToke
2f80: 6e 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 n);. }.. /* Th
2f90: 65 20 69 6e 70 75 74 20 6d 65 73 73 61 67 65 20 e input message
2fa0: 68 61 73 20 6e 6f 77 20 62 65 65 6e 20 70 72 6f has now been pro
2fb0: 63 65 73 73 65 64 2e 20 20 47 65 6e 65 72 61 74 cessed. Generat
2fc0: 65 20 61 20 72 65 70 6c 79 2e 20 2a 2f 0a 20 20 e a reply. */.
2fd0: 69 66 28 20 69 73 50 75 73 68 20 29 7b 0a 20 20 if( isPush ){.
2fe0: 20 20 53 74 6d 74 20 71 3b 0a 20 20 20 20 64 62 Stmt q;. db
2ff0: 5f 70 72 65 70 61 72 65 28 26 71 2c 20 22 53 45 _prepare(&q, "SE
3000: 4c 45 43 54 20 75 75 69 64 20 46 52 4f 4d 20 62 LECT uuid FROM b
3010: 6c 6f 62 20 57 48 45 52 45 20 73 69 7a 65 3c 30 lob WHERE size<0
3020: 22 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 64 ");. while( d
3030: 62 5f 73 74 65 70 28 26 71 29 3d 3d 53 51 4c 49 b_step(&q)==SQLI
3040: 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 TE_ROW ){.
3050: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 55 75 69 const char *zUui
3060: 64 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65 d = db_column_te
3070: 78 74 28 26 71 2c 20 30 29 3b 0a 20 20 20 20 20 xt(&q, 0);.
3080: 20 40 20 67 69 6d 6d 65 20 25 73 28 7a 55 75 69 @ gimme %s(zUui
3090: 64 29 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 5f d). }. db_
30a0: 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 20 20 finalize(&q);.
30b0: 7d 0a 20 20 69 66 28 20 69 73 50 75 6c 6c 20 29 }. if( isPull )
30c0: 7b 0a 20 20 20 20 73 65 6e 64 5f 61 6c 6c 5f 70 {. send_all_p
30d0: 65 6e 64 69 6e 67 28 30 29 3b 0a 20 20 7d 0a 20 ending(0);. }.
30e0: 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 db_end_transact
30f0: 69 6f 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ion(0);.}../*.**
3100: 20 43 4f 4d 4d 41 4e 44 3a 20 74 65 73 74 2d 78 COMMAND: test-x
3110: 66 65 72 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 fer.**.** This c
3120: 6f 6d 6d 61 6e 64 20 69 73 20 75 73 65 64 20 66 ommand is used f
3130: 6f 72 20 64 65 62 75 67 67 69 6e 67 20 74 68 65 or debugging the
3140: 20 73 65 72 76 65 72 2e 20 20 54 68 65 72 65 20 server. There
3150: 69 73 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 61 is a single.** a
3160: 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 rgument which is
3170: 20 74 68 65 20 75 6e 63 6f 6d 70 72 65 73 73 65 the uncompresse
3180: 64 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 6e 20 d content of an
3190: 22 78 66 65 72 22 20 6d 65 73 73 61 67 65 0a 2a "xfer" message.*
31a0: 2a 20 66 72 6f 6d 20 63 6c 69 65 6e 74 20 74 6f * from client to
31b0: 20 73 65 72 76 65 72 2e 20 20 54 68 69 73 20 63 server. This c
31c0: 6f 6d 6d 61 6e 64 20 69 6e 74 65 72 70 72 65 74 ommand interpret
31d0: 73 20 74 68 61 74 20 6d 65 73 73 61 67 65 20 61 s that message a
31e0: 73 0a 2a 2a 20 69 66 20 68 61 64 20 62 65 65 6e s.** if had been
31f0: 20 72 65 63 65 69 76 65 64 20 62 79 20 74 68 65 received by the
3200: 20 73 65 72 76 65 72 2e 0a 2a 2a 0a 2a 2a 20 4f server..**.** O
3210: 6e 20 74 68 65 20 63 6c 69 65 6e 74 20 73 69 64 n the client sid
3220: 65 2c 20 72 75 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 e, run:.**.**
3230: 20 20 20 66 6f 73 73 69 6c 20 70 75 73 68 20 68 fossil push h
3240: 74 74 70 3a 2f 2f 62 6f 67 75 73 2f 20 2d 2d 68 ttp://bogus/ --h
3250: 74 74 70 74 72 61 63 65 0a 2a 2a 0a 2a 2a 20 4f ttptrace.**.** O
3260: 72 20 61 20 73 69 6d 69 6c 61 72 20 63 6f 6d 6d r a similar comm
3270: 61 6e 64 20 74 6f 20 70 72 6f 76 69 64 65 20 74 and to provide t
3280: 68 65 20 6f 75 74 70 75 74 2e 20 20 54 68 65 20 he output. The
3290: 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 0a 2a content of the.*
32a0: 2a 20 6d 65 73 73 61 67 65 20 77 69 6c 6c 20 61 * message will a
32b0: 70 70 65 61 72 20 6f 6e 20 73 74 61 6e 64 61 72 ppear on standar
32c0: 64 20 6f 75 74 70 75 74 2e 20 20 43 61 70 74 75 d output. Captu
32d0: 72 65 20 74 68 69 73 20 6d 65 73 73 61 67 65 0a re this message.
32e0: 2a 2a 20 69 6e 74 6f 20 61 20 66 69 6c 65 20 6e ** into a file n
32f0: 61 6d 65 64 20 28 66 6f 72 20 65 78 61 6d 70 6c amed (for exampl
3300: 65 29 20 6f 75 74 2e 74 78 74 2e 20 20 54 68 65 e) out.txt. The
3310: 6e 20 72 75 6e 20 74 68 65 0a 2a 2a 20 73 65 72 n run the.** ser
3320: 76 65 72 20 69 6e 20 67 64 62 3a 0a 2a 2a 0a 2a ver in gdb:.**.*
3330: 2a 20 20 20 20 20 67 64 62 20 66 6f 73 73 69 6c * gdb fossil
3340: 0a 2a 2a 20 20 20 20 20 72 20 74 65 73 74 2d 78 .** r test-x
3350: 66 65 72 20 6f 75 74 2e 74 78 74 0a 2a 2f 0a 76 fer out.txt.*/.v
3360: 6f 69 64 20 63 6d 64 5f 74 65 73 74 5f 78 66 65 oid cmd_test_xfe
3370: 72 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 6e r(void){. int n
3380: 6f 74 55 73 65 64 3b 0a 20 20 69 66 28 20 67 2e otUsed;. if( g.
3390: 61 72 67 63 21 3d 32 20 26 26 20 67 2e 61 72 67 argc!=2 && g.arg
33a0: 63 21 3d 33 20 29 7b 0a 20 20 20 20 75 73 61 67 c!=3 ){. usag
33b0: 65 28 22 3f 4d 45 53 53 41 47 45 46 49 4c 45 3f e("?MESSAGEFILE?
33c0: 22 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 6d 75 73 ");. }. db_mus
33d0: 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74 72 65 65 t_be_within_tree
33e0: 28 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 ();. blob_zero(
33f0: 26 67 2e 63 67 69 49 6e 29 3b 0a 20 20 62 6c 6f &g.cgiIn);. blo
3400: 62 5f 72 65 61 64 5f 66 72 6f 6d 5f 66 69 6c 65 b_read_from_file
3410: 28 26 67 2e 63 67 69 49 6e 2c 20 67 2e 61 72 67 (&g.cgiIn, g.arg
3420: 63 3d 3d 32 20 3f 20 22 2d 22 20 3a 20 67 2e 61 c==2 ? "-" : g.a
3430: 72 67 76 5b 32 5d 29 3b 0a 20 20 64 69 73 61 62 rgv[2]);. disab
3440: 6c 65 4c 6f 67 69 6e 20 3d 20 31 3b 0a 20 20 70 leLogin = 1;. p
3450: 61 67 65 5f 78 66 65 72 28 29 3b 0a 20 20 70 72 age_xfer();. pr
3460: 69 6e 74 66 28 22 25 73 5c 6e 22 2c 20 63 67 69 intf("%s\n", cgi
3470: 5f 65 78 74 72 61 63 74 5f 63 6f 6e 74 65 6e 74 _extract_content
3480: 28 26 6e 6f 74 55 73 65 64 29 29 3b 0a 7d 0a 0a (¬Used));.}..
3490: 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 6f 20 74 ./*.** Sync to t
34a0: 68 65 20 68 6f 73 74 20 69 64 65 6e 74 69 66 69 he host identifi
34b0: 65 64 20 69 6e 20 67 2e 75 72 6c 4e 61 6d 65 20 ed in g.urlName
34c0: 61 6e 64 20 67 2e 75 72 6c 50 61 74 68 2e 20 20 and g.urlPath.
34d0: 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 This.** routine
34e0: 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 is called by the
34f0: 20 63 6c 69 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 52 client..**.** R
3500: 65 63 6f 72 64 73 20 61 72 65 20 70 75 73 68 65 ecords are pushe
3510: 64 20 74 6f 20 74 68 65 20 73 65 72 76 65 72 20 d to the server
3520: 69 66 20 70 75 73 68 46 6c 61 67 20 69 73 20 74 if pushFlag is t
3530: 72 75 65 2e 20 20 52 65 63 6f 72 64 73 0a 2a 2a rue. Records.**
3540: 20 61 72 65 20 70 75 6c 6c 65 64 20 69 66 20 70 are pulled if p
3550: 75 6c 6c 46 6c 61 67 20 69 73 20 74 72 75 65 2e ullFlag is true.
3560: 20 20 41 20 66 75 6c 6c 20 73 79 6e 63 20 6f 63 A full sync oc
3570: 63 75 72 73 20 69 66 20 62 6f 74 68 20 61 72 65 curs if both are
3580: 0a 2a 2a 20 74 72 75 65 2e 0a 2a 2f 0a 76 6f 69 .** true..*/.voi
3590: 64 20 63 6c 69 65 6e 74 5f 73 79 6e 63 28 69 6e d client_sync(in
35a0: 74 20 70 75 73 68 46 6c 61 67 2c 20 69 6e 74 20 t pushFlag, int
35b0: 70 75 6c 6c 46 6c 61 67 2c 20 69 6e 74 20 63 6c pullFlag, int cl
35c0: 6f 6e 65 46 6c 61 67 29 7b 0a 20 20 69 6e 74 20 oneFlag){. int
35d0: 67 6f 20 3d 20 31 3b 20 20 20 20 20 20 20 20 2f go = 1; /
35e0: 2a 20 4c 6f 6f 70 20 75 6e 74 69 6c 20 7a 65 72 * Loop until zer
35f0: 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 o */. int nToke
3600: 6e 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 n;. const char
3610: 2a 7a 53 43 6f 64 65 20 3d 20 64 62 5f 67 65 74 *zSCode = db_get
3620: 28 22 73 65 72 76 65 72 2d 63 6f 64 65 22 2c 20 ("server-code",
3630: 22 78 22 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 "x");. const ch
3640: 61 72 20 2a 7a 50 43 6f 64 65 20 3d 20 64 62 5f ar *zPCode = db_
3650: 67 65 74 28 22 70 72 6f 6a 65 63 74 2d 63 6f 64 get("project-cod
3660: 65 22 2c 20 30 29 3b 0a 20 20 69 6e 74 20 6e 46 e", 0);. int nF
3670: 69 6c 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e ile = 0;. int n
3680: 4d 73 67 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e Msg = 0;. int n
3690: 52 65 71 20 3d 20 30 3b 0a 20 20 42 6c 6f 62 20 Req = 0;. Blob
36a0: 73 65 6e 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 send; /*
36b0: 54 65 78 74 20 77 65 20 61 72 65 20 73 65 6e 64 Text we are send
36c0: 69 6e 67 20 74 6f 20 74 68 65 20 73 65 72 76 65 ing to the serve
36d0: 72 20 2a 2f 0a 20 20 42 6c 6f 62 20 72 65 63 76 r */. Blob recv
36e0: 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 70 6c ; /* Repl
36f0: 79 20 77 65 20 67 6f 74 20 62 61 63 6b 20 66 72 y we got back fr
3700: 6f 6d 20 74 68 65 20 73 65 72 76 65 72 20 2a 2f om the server */
3710: 0a 20 20 42 6c 6f 62 20 6c 69 6e 65 3b 20 20 20 . Blob line;
3720: 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 /* A single
3730: 20 6c 69 6e 65 20 6f 66 20 74 68 65 20 72 65 70 line of the rep
3740: 6c 79 20 2a 2f 0a 20 20 42 6c 6f 62 20 61 54 6f ly */. Blob aTo
3750: 6b 65 6e 5b 35 5d 3b 20 20 20 2f 2a 20 41 20 74 ken[5]; /* A t
3760: 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 6f 66 20 6c okenization of l
3770: 69 6e 65 20 2a 2f 0a 20 20 42 6c 6f 62 20 65 72 ine */. Blob er
3780: 72 6d 73 67 3b 20 20 20 20 20 20 2f 2a 20 45 72 rmsg; /* Er
3790: 72 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f 0a 0a ror message */..
37a0: 20 20 61 73 73 65 72 74 28 20 70 75 73 68 46 6c assert( pushFl
37b0: 61 67 20 7c 7c 20 70 75 6c 6c 46 6c 61 67 20 7c ag || pullFlag |
37c0: 7c 20 63 6c 6f 6e 65 46 6c 61 67 20 29 3b 0a 20 | cloneFlag );.
37d0: 20 61 73 73 65 72 74 28 20 21 67 2e 75 72 6c 49 assert( !g.urlI
37e0: 73 46 69 6c 65 20 29 3b 20 20 20 20 20 20 20 20 sFile );
37f0: 20 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20 77 /* This only w
3800: 6f 72 6b 73 20 66 6f 72 20 6e 65 74 77 6f 72 6b orks for network
3810: 69 6e 67 20 2a 2f 0a 0a 20 20 64 62 5f 62 65 67 ing */.. db_beg
3820: 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 29 in_transaction()
3830: 3b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 ;. db_multi_exe
3840: 63 28 0a 20 20 20 20 2f 2a 20 52 65 63 6f 72 64 c(. /* Record
3850: 73 20 77 68 69 63 68 20 77 65 20 6b 6e 6f 77 20 s which we know
3860: 74 68 65 20 6f 74 68 65 72 20 73 69 64 65 20 61 the other side a
3870: 6c 73 6f 20 68 61 73 20 2a 2f 0a 20 20 20 20 22 lso has */. "
3880: 43 52 45 41 54 45 20 54 45 4d 50 20 54 41 42 4c CREATE TEMP TABL
3890: 45 20 6f 6e 72 65 6d 6f 74 65 28 72 69 64 20 49 E onremote(rid I
38a0: 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b NTEGER PRIMARY K
38b0: 45 59 29 3b 22 0a 20 20 20 20 2f 2a 20 52 65 63 EY);". /* Rec
38c0: 6f 72 64 73 20 77 65 20 6b 6e 6f 77 20 74 68 65 ords we know the
38d0: 20 6f 74 68 65 72 20 73 69 64 65 20 6e 65 65 64 other side need
38e0: 73 20 2a 2f 0a 20 20 20 20 22 43 52 45 41 54 45 s */. "CREATE
38f0: 20 54 45 4d 50 20 54 41 42 4c 45 20 70 65 6e 64 TEMP TABLE pend
3900: 69 6e 67 28 72 69 64 20 49 4e 54 45 47 45 52 20 ing(rid INTEGER
3910: 50 52 49 4d 41 52 59 20 4b 45 59 29 3b 22 0a 20 PRIMARY KEY);".
3920: 20 29 3b 0a 20 20 62 6c 6f 62 61 72 72 61 79 5f );. blobarray_
3930: 7a 65 72 6f 28 61 54 6f 6b 65 6e 2c 20 63 6f 75 zero(aToken, cou
3940: 6e 74 28 61 54 6f 6b 65 6e 29 29 3b 0a 20 20 62 nt(aToken));. b
3950: 6c 6f 62 5f 7a 65 72 6f 28 26 73 65 6e 64 29 3b lob_zero(&send);
3960: 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 72 65 . blob_zero(&re
3970: 63 76 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f cv);. blob_zero
3980: 28 26 65 72 72 6d 73 67 29 3b 0a 0a 0a 20 20 77 (&errmsg);... w
3990: 68 69 6c 65 28 20 67 6f 20 29 7b 0a 20 20 20 20 hile( go ){.
39a0: 67 6f 20 3d 20 30 3b 0a 20 20 20 20 6e 46 69 6c go = 0;. nFil
39b0: 65 20 3d 20 6e 52 65 71 20 3d 20 6e 4d 73 67 20 e = nReq = nMsg
39c0: 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 47 65 6e = 0;.. /* Gen
39d0: 65 72 61 74 65 20 61 20 72 65 71 75 65 73 74 20 erate a request
39e0: 74 6f 20 62 65 20 73 65 6e 74 20 74 6f 20 74 68 to be sent to th
39f0: 65 20 73 65 72 76 65 72 2e 0a 20 20 20 20 2a 2a e server.. **
3a00: 20 41 6c 77 61 79 73 20 62 65 67 69 6e 20 77 69 Always begin wi
3a10: 74 68 20 61 20 63 6c 6f 6e 65 2c 20 70 75 6c 6c th a clone, pull
3a20: 2c 20 6f 72 20 70 75 73 68 20 6d 65 73 73 61 67 , or push messag
3a30: 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 0a 20 20 e. */. .
3a40: 20 20 69 66 28 20 63 6c 6f 6e 65 46 6c 61 67 20 if( cloneFlag
3a50: 29 7b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 61 70 ){. blob_ap
3a60: 70 65 6e 64 66 28 26 73 65 6e 64 2c 20 22 63 6c pendf(&send, "cl
3a70: 6f 6e 65 5c 6e 22 29 3b 0a 20 20 20 20 20 20 70 one\n");. p
3a80: 75 73 68 46 6c 61 67 20 3d 20 30 3b 0a 20 20 20 ushFlag = 0;.
3a90: 20 20 20 70 75 6c 6c 46 6c 61 67 20 3d 20 30 3b pullFlag = 0;
3aa0: 0a 20 20 20 20 20 20 6e 4d 73 67 2b 2b 3b 0a 20 . nMsg++;.
3ab0: 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 75 6c }else if( pul
3ac0: 6c 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 62 lFlag ){. b
3ad0: 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 73 65 6e lob_appendf(&sen
3ae0: 64 2c 20 22 70 75 6c 6c 20 25 73 20 25 73 5c 6e d, "pull %s %s\n
3af0: 22 2c 20 7a 53 43 6f 64 65 2c 20 7a 50 43 6f 64 ", zSCode, zPCod
3b00: 65 29 3b 0a 20 20 20 20 20 20 6e 4d 73 67 2b 2b e);. nMsg++
3b10: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
3b20: 70 75 73 68 46 6c 61 67 20 29 7b 0a 20 20 20 20 pushFlag ){.
3b30: 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 blob_appendf(&
3b40: 73 65 6e 64 2c 20 22 70 75 73 68 20 25 73 20 25 send, "push %s %
3b50: 73 5c 6e 22 2c 20 7a 53 43 6f 64 65 2c 20 7a 50 s\n", zSCode, zP
3b60: 43 6f 64 65 29 3b 0a 20 20 20 20 20 20 6e 4d 73 Code);. nMs
3b70: 67 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 g++;. }..
3b80: 69 66 28 20 70 75 6c 6c 46 6c 61 67 20 29 7b 0a if( pullFlag ){.
3b90: 20 20 20 20 20 20 2f 2a 20 53 65 6e 64 20 67 69 /* Send gi
3ba0: 6d 6d 65 20 6d 65 73 73 61 67 65 20 66 6f 72 20 mme message for
3bb0: 65 76 65 72 79 20 70 68 61 6e 74 6f 6d 20 74 68 every phantom th
3bc0: 61 74 20 77 65 20 68 6f 6c 64 2e 0a 20 20 20 20 at we hold..
3bd0: 20 20 2a 2f 0a 20 20 20 20 20 20 53 74 6d 74 20 */. Stmt
3be0: 71 3b 0a 20 20 20 20 20 20 64 62 5f 70 72 65 70 q;. db_prep
3bf0: 61 72 65 28 26 71 2c 20 22 53 45 4c 45 43 54 20 are(&q, "SELECT
3c00: 75 75 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 uuid FROM blob W
3c10: 48 45 52 45 20 73 69 7a 65 3c 30 22 29 3b 0a 20 HERE size<0");.
3c20: 20 20 20 20 20 77 68 69 6c 65 28 20 64 62 5f 73 while( db_s
3c30: 74 65 70 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f tep(&q)==SQLITE_
3c40: 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 20 20 63 ROW ){. c
3c50: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 55 75 69 64 onst char *zUuid
3c60: 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78 = db_column_tex
3c70: 74 28 26 71 2c 20 30 29 3b 0a 20 20 20 20 20 20 t(&q, 0);.
3c80: 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 blob_appendf(&
3c90: 73 65 6e 64 2c 22 67 69 6d 6d 65 20 25 73 5c 6e send,"gimme %s\n
3ca0: 22 2c 20 7a 55 75 69 64 29 3b 0a 20 20 20 20 20 ", zUuid);.
3cb0: 20 20 20 6e 52 65 71 2b 2b 3b 0a 20 20 20 20 20 nReq++;.
3cc0: 20 7d 0a 20 20 20 20 20 20 64 62 5f 66 69 6e 61 }. db_fina
3cd0: 6c 69 7a 65 28 26 71 29 3b 0a 20 20 20 20 7d 0a lize(&q);. }.
3ce0: 0a 20 20 20 20 69 66 28 20 70 75 73 68 46 6c 61 . if( pushFla
3cf0: 67 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53 65 g ){. /* Se
3d00: 6e 64 20 74 68 65 20 73 65 72 76 65 72 20 61 6e nd the server an
3d10: 79 20 66 69 6c 65 73 20 74 68 61 74 20 74 68 65 y files that the
3d20: 20 73 65 72 76 65 72 20 68 61 73 20 72 65 71 75 server has requ
3d30: 65 73 74 65 64 20 2a 2f 0a 20 20 20 20 20 20 6e ested */. n
3d40: 46 69 6c 65 20 2b 3d 20 73 65 6e 64 5f 61 6c 6c File += send_all
3d50: 5f 70 65 6e 64 69 6e 67 28 26 73 65 6e 64 29 3b _pending(&send);
3d60: 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 . }.. if(
3d70: 70 75 6c 6c 46 6c 61 67 20 7c 7c 20 70 75 73 68 pullFlag || push
3d80: 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 2f 2a Flag ){. /*
3d90: 20 41 6c 77 61 79 73 20 73 65 6e 64 20 6f 75 72 Always send our
3da0: 20 6c 65 61 76 65 73 20 2a 2f 0a 20 20 20 20 20 leaves */.
3db0: 20 53 74 6d 74 20 71 3b 0a 20 20 20 20 20 20 64 Stmt q;. d
3dc0: 62 5f 70 72 65 70 61 72 65 28 26 71 2c 20 0a 20 b_prepare(&q, .
3dd0: 20 20 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 "SELECT
3de0: 75 75 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 uuid FROM blob W
3df0: 48 45 52 45 20 72 69 64 20 49 4e 22 0a 20 20 20 HERE rid IN".
3e00: 20 20 20 20 20 20 22 20 20 28 53 45 4c 45 43 54 " (SELECT
3e10: 20 63 69 64 20 46 52 4f 4d 20 70 6c 69 6e 6b 20 cid FROM plink
3e20: 45 58 43 45 50 54 20 53 45 4c 45 43 54 20 70 69 EXCEPT SELECT pi
3e30: 64 20 46 52 4f 4d 20 70 6c 69 6e 6b 29 22 0a 20 d FROM plink)".
3e40: 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 77 68 );. wh
3e50: 69 6c 65 28 20 64 62 5f 73 74 65 70 28 26 71 29 ile( db_step(&q)
3e60: 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a ==SQLITE_ROW ){.
3e70: 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 const ch
3e80: 61 72 20 2a 7a 55 75 69 64 20 3d 20 64 62 5f 63 ar *zUuid = db_c
3e90: 6f 6c 75 6d 6e 5f 74 65 78 74 28 26 71 2c 20 30 olumn_text(&q, 0
3ea0: 29 3b 0a 20 20 20 20 20 20 20 20 62 6c 6f 62 5f );. blob_
3eb0: 61 70 70 65 6e 64 66 28 26 73 65 6e 64 2c 20 22 appendf(&send, "
3ec0: 6c 65 61 66 20 25 73 5c 6e 22 2c 20 7a 55 75 69 leaf %s\n", zUui
3ed0: 64 29 3b 0a 20 20 20 20 20 20 20 20 6e 4d 73 67 d);. nMsg
3ee0: 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ++;. }.
3ef0: 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 db_finalize(&q
3f00: 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a );. }.. /*
3f10: 20 45 78 63 68 61 6e 67 65 20 6d 65 73 73 61 67 Exchange messag
3f20: 65 73 20 77 69 74 68 20 74 68 65 20 73 65 72 76 es with the serv
3f30: 65 72 20 2a 2f 0a 20 20 20 20 70 72 69 6e 74 66 er */. printf
3f40: 28 22 53 65 6e 64 3a 20 20 20 20 20 20 25 64 20 ("Send: %d
3f50: 66 69 6c 65 73 2c 20 25 64 20 72 65 71 75 65 73 files, %d reques
3f60: 74 73 2c 20 25 64 20 6f 74 68 65 72 20 6d 65 73 ts, %d other mes
3f70: 73 61 67 65 73 5c 6e 22 2c 0a 20 20 20 20 20 20 sages\n",.
3f80: 20 20 20 20 20 20 6e 46 69 6c 65 2c 20 6e 52 65 nFile, nRe
3f90: 71 2c 20 6e 4d 73 67 29 3b 0a 20 20 20 20 6e 46 q, nMsg);. nF
3fa0: 69 6c 65 20 3d 20 6e 52 65 71 20 3d 20 6e 4d 73 ile = nReq = nMs
3fb0: 67 20 3d 20 30 3b 0a 20 20 20 20 68 74 74 70 5f g = 0;. http_
3fc0: 65 78 63 68 61 6e 67 65 28 26 73 65 6e 64 2c 20 exchange(&send,
3fd0: 26 72 65 63 76 29 3b 0a 20 20 20 20 62 6c 6f 62 &recv);. blob
3fe0: 5f 72 65 73 65 74 28 26 73 65 6e 64 29 3b 0a 0a _reset(&send);..
3ff0: 20 20 20 20 2f 2a 20 50 72 6f 63 65 73 73 20 74 /* Process t
4000: 68 65 20 72 65 70 6c 79 20 74 68 61 74 20 63 61 he reply that ca
4010: 6d 65 20 62 61 63 6b 20 66 72 6f 6d 20 74 68 65 me back from the
4020: 20 73 65 72 76 65 72 20 2a 2f 0a 20 20 20 20 77 server */. w
4030: 68 69 6c 65 28 20 62 6c 6f 62 5f 6c 69 6e 65 28 hile( blob_line(
4040: 26 72 65 63 76 2c 20 26 6c 69 6e 65 29 20 29 7b &recv, &line) ){
4050: 0a 20 20 20 20 20 20 6e 54 6f 6b 65 6e 20 3d 20 . nToken =
4060: 62 6c 6f 62 5f 74 6f 6b 65 6e 69 7a 65 28 26 6c blob_tokenize(&l
4070: 69 6e 65 2c 20 61 54 6f 6b 65 6e 2c 20 63 6f 75 ine, aToken, cou
4080: 6e 74 28 61 54 6f 6b 65 6e 29 29 3b 0a 0a 20 20 nt(aToken));..
4090: 20 20 20 20 2f 2a 20 20 20 66 69 6c 65 20 55 55 /* file UU
40a0: 49 44 20 53 49 5a 45 20 5c 6e 20 43 4f 4e 54 45 ID SIZE \n CONTE
40b0: 4e 54 0a 20 20 20 20 20 20 2a 2a 20 20 20 66 69 NT. ** fi
40c0: 6c 65 20 55 55 49 44 20 44 45 4c 54 41 53 52 43 le UUID DELTASRC
40d0: 20 53 49 5a 45 20 5c 6e 20 43 4f 4e 54 45 4e 54 SIZE \n CONTENT
40e0: 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 . **.
40f0: 2a 2a 20 52 65 63 65 69 76 65 20 61 20 66 69 6c ** Receive a fil
4100: 65 20 74 72 61 6e 73 6d 69 74 74 65 64 20 66 72 e transmitted fr
4110: 6f 6d 20 74 68 65 20 6f 74 68 65 72 20 73 69 64 om the other sid
4120: 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 e. */.
4130: 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 61 54 if( blob_eq(&aT
4140: 6f 6b 65 6e 5b 30 5d 2c 22 66 69 6c 65 22 29 20 oken[0],"file")
4150: 29 7b 0a 20 20 20 20 20 20 20 20 78 66 65 72 5f ){. xfer_
4160: 61 63 63 65 70 74 5f 66 69 6c 65 28 26 72 65 63 accept_file(&rec
4170: 76 2c 20 61 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 v, aToken, nToke
4180: 6e 2c 20 26 65 72 72 6d 73 67 29 3b 0a 20 20 20 n, &errmsg);.
4190: 20 20 20 20 20 6e 46 69 6c 65 2b 2b 3b 0a 20 20 nFile++;.
41a0: 20 20 20 20 20 20 67 6f 20 3d 20 31 3b 0a 20 20 go = 1;.
41b0: 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20 20 20 }else..
41c0: 20 2f 2a 20 20 20 67 69 6d 6d 65 20 55 55 49 44 /* gimme UUID
41d0: 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 . **.
41e0: 2a 2a 20 53 65 72 76 65 72 20 69 73 20 72 65 71 ** Server is req
41f0: 75 65 73 74 69 6e 67 20 61 20 66 69 6c 65 0a 20 uesting a file.
4200: 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 */. if
4210: 28 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 ( blob_eq(&aToke
4220: 6e 5b 30 5d 2c 20 22 67 69 6d 6d 65 22 29 20 26 n[0], "gimme") &
4230: 26 20 6e 54 6f 6b 65 6e 3d 3d 32 0a 20 20 20 20 & nToken==2.
4240: 20 20 20 20 20 20 20 20 20 20 20 26 26 20 62 6c && bl
4250: 6f 62 5f 69 73 5f 75 75 69 64 28 26 61 54 6f 6b ob_is_uuid(&aTok
4260: 65 6e 5b 31 5d 29 20 29 7b 0a 20 20 20 20 20 20 en[1]) ){.
4270: 20 20 6e 52 65 71 2b 2b 3b 0a 20 20 20 20 20 20 nReq++;.
4280: 20 20 69 66 28 20 70 75 73 68 46 6c 61 67 20 29 if( pushFlag )
4290: 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 5f 6d {. db_m
42a0: 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 20 20 ulti_exec(.
42b0: 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 20 4f "INSERT O
42c0: 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 70 65 R IGNORE INTO pe
42d0: 6e 64 69 6e 67 28 72 69 64 29 20 22 0a 20 20 20 nding(rid) ".
42e0: 20 20 20 20 20 20 20 20 20 22 53 45 4c 45 43 54 "SELECT
42f0: 20 72 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 rid FROM blob W
4300: 48 45 52 45 20 75 75 69 64 3d 25 42 20 41 4e 44 HERE uuid=%B AND
4310: 20 73 69 7a 65 3e 3d 30 22 2c 20 26 61 54 6f 6b size>=0", &aTok
4320: 65 6e 5b 31 5d 0a 20 20 20 20 20 20 20 20 20 20 en[1].
4330: 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 20 );. go
4340: 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 = 1;. }.
4350: 20 20 20 20 20 7d 65 6c 73 65 0a 20 20 0a 20 20 }else. .
4360: 20 20 20 20 2f 2a 20 20 20 69 67 6f 74 20 55 55 /* igot UU
4370: 49 44 0a 20 20 20 20 20 20 2a 2a 20 20 20 6c 65 ID. ** le
4380: 61 66 20 55 55 49 44 0a 20 20 20 20 20 20 2a 2a af UUID. **
4390: 0a 20 20 20 20 20 20 2a 2a 20 53 65 72 76 65 72 . ** Server
43a0: 20 70 72 6f 63 6c 61 69 6d 73 20 74 68 61 74 20 proclaims that
43b0: 69 74 20 68 61 73 20 61 20 70 61 72 74 69 63 75 it has a particu
43c0: 6c 61 72 20 66 69 6c 65 2e 20 20 41 20 6c 65 61 lar file. A lea
43d0: 66 20 6d 65 73 73 61 67 65 0a 20 20 20 20 20 20 f message.
43e0: 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 ** means that th
43f0: 65 20 66 69 6c 65 20 69 73 20 61 20 6c 65 61 66 e file is a leaf
4400: 20 6d 61 6e 69 66 65 73 74 20 6f 6e 20 74 68 65 manifest on the
4410: 20 73 65 72 76 65 72 2e 0a 20 20 20 20 20 20 2a server.. *
4420: 2f 0a 20 20 20 20 20 20 69 66 28 20 6e 54 6f 6b /. if( nTok
4430: 65 6e 3d 3d 32 0a 20 20 20 20 20 20 20 20 20 20 en==2.
4440: 20 20 20 20 26 26 20 28 62 6c 6f 62 5f 65 71 28 && (blob_eq(
4450: 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 69 67 6f &aToken[0], "igo
4460: 74 22 29 20 7c 7c 20 62 6c 6f 62 5f 65 71 28 26 t") || blob_eq(&
4470: 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 6c 65 61 66 aToken[0], "leaf
4480: 22 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 ")).
4490: 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69 && blob_is_uui
44a0: 64 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 20 29 7b d(&aToken[1]) ){
44b0: 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 69 64 . int rid
44c0: 20 3d 20 64 62 5f 69 6e 74 28 30 2c 20 22 53 45 = db_int(0, "SE
44d0: 4c 45 43 54 20 72 69 64 20 46 52 4f 4d 20 62 6c LECT rid FROM bl
44e0: 6f 62 20 57 48 45 52 45 20 75 75 69 64 3d 25 42 ob WHERE uuid=%B
44f0: 22 2c 20 26 61 54 6f 6b 65 6e 5b 31 5d 29 3b 0a ", &aToken[1]);.
4500: 20 20 20 20 20 20 20 20 6e 4d 73 67 2b 2b 3b 0a nMsg++;.
4510: 20 20 20 20 20 20 20 20 69 66 28 20 72 69 64 3e if( rid>
4520: 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 0 ){. d
4530: 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 b_multi_exec(.
4540: 20 20 20 20 20 20 20 20 20 20 22 49 4e 53 45 52 "INSER
4550: 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f T OR IGNORE INTO
4560: 20 6f 6e 72 65 6d 6f 74 65 28 72 69 64 29 20 56 onremote(rid) V
4570: 41 4c 55 45 53 28 25 64 29 22 2c 20 72 69 64 0a ALUES(%d)", rid.
4580: 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 );.
4590: 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20 74 6f /* Add to
45a0: 20 74 68 65 20 70 65 6e 64 69 6e 67 20 73 65 74 the pending set
45b0: 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20 6f 66 all children of
45c0: 20 74 68 65 20 73 65 72 76 65 72 27 73 20 6c 65 the server's le
45d0: 61 76 65 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 aves */.
45e0: 20 20 69 66 28 20 70 75 73 68 46 6c 61 67 20 26 if( pushFlag &
45f0: 26 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 & blob_eq(&aToke
4600: 6e 5b 30 5d 2c 20 22 6c 65 61 66 22 29 20 29 7b n[0], "leaf") ){
4610: 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 62 5f . db_
4620: 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 20 multi_exec(.
4630: 20 20 20 20 20 20 20 20 20 20 22 49 4e 53 45 52 "INSER
4640: 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f T OR IGNORE INTO
4650: 20 70 65 6e 64 69 6e 67 28 72 69 64 29 20 22 0a pending(rid) ".
4660: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 53 "S
4670: 45 4c 45 43 54 20 63 69 64 20 46 52 4f 4d 20 70 ELECT cid FROM p
4680: 6c 69 6e 6b 20 57 48 45 52 45 20 70 69 64 3d 25 link WHERE pid=%
4690: 64 22 2c 20 72 69 64 0a 20 20 20 20 20 20 20 20 d", rid.
46a0: 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 20 );.
46b0: 20 20 20 67 6f 20 3d 20 31 3b 0a 20 20 20 20 20 go = 1;.
46c0: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
46d0: 20 69 66 28 20 70 75 6c 6c 46 6c 61 67 20 26 26 if( pullFlag &&
46e0: 20 21 67 6f 20 26 26 20 0a 20 20 20 20 20 20 20 !go && .
46f0: 20 20 20 20 20 20 20 64 62 5f 65 78 69 73 74 73 db_exists
4700: 28 22 53 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 ("SELECT 1 FROM
4710: 62 6c 6f 62 20 57 48 45 52 45 20 72 69 64 3d 25 blob WHERE rid=%
4720: 64 20 41 4e 44 20 73 69 7a 65 3e 3d 30 22 2c 20 d AND size>=0",
4730: 72 69 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20 rid) ){.
4740: 20 20 20 20 67 6f 20 3d 20 31 3b 0a 20 20 20 20 go = 1;.
4750: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
4760: 7d 65 6c 73 65 20 69 66 28 20 70 75 6c 6c 46 6c }else if( pullFl
4770: 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ag ){.
4780: 67 6f 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 go = 1;.
4790: 20 20 63 6f 6e 74 65 6e 74 5f 70 75 74 28 30 2c content_put(0,
47a0: 20 62 6c 6f 62 5f 73 74 72 28 26 61 54 6f 6b 65 blob_str(&aToke
47b0: 6e 5b 31 5d 29 29 3b 0a 20 20 20 20 20 20 20 20 n[1]));.
47c0: 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a 20 20 }. }else.
47d0: 0a 20 20 20 20 20 20 2f 2a 20 20 20 70 75 73 68 . /* push
47e0: 20 20 53 45 52 56 45 52 43 4f 44 45 20 20 50 52 SERVERCODE PR
47f0: 4f 44 55 43 54 43 4f 44 45 0a 20 20 20 20 20 20 ODUCTCODE.
4800: 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 53 68 6f 75 **. ** Shou
4810: 6c 64 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69 ld only happen i
4820: 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 61 20 n response to a
4830: 63 6c 6f 6e 65 2e 0a 20 20 20 20 20 20 2a 2f 0a clone.. */.
4840: 20 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 if( blob_e
4850: 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 22 70 75 q(&aToken[0],"pu
4860: 73 68 22 29 20 26 26 20 6e 54 6f 6b 65 6e 3d 3d sh") && nToken==
4870: 33 20 26 26 20 63 6c 6f 6e 65 46 6c 61 67 0a 20 3 && cloneFlag.
4880: 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 &&
4890: 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26 61 54 blob_is_uuid(&aT
48a0: 6f 6b 65 6e 5b 31 5d 29 20 26 26 20 62 6c 6f 62 oken[1]) && blob
48b0: 5f 69 73 5f 75 75 69 64 28 26 61 54 6f 6b 65 6e _is_uuid(&aToken
48c0: 5b 32 5d 29 20 29 7b 0a 0a 20 20 20 20 20 20 20 [2]) ){..
48d0: 20 69 66 28 20 62 6c 6f 62 5f 65 71 5f 73 74 72 if( blob_eq_str
48e0: 28 26 61 54 6f 6b 65 6e 5b 31 5d 2c 20 7a 53 43 (&aToken[1], zSC
48f0: 6f 64 65 2c 20 2d 31 29 20 29 7b 0a 20 20 20 20 ode, -1) ){.
4900: 20 20 20 20 20 20 66 6f 73 73 69 6c 5f 66 61 74 fossil_fat
4910: 61 6c 28 22 73 65 72 76 65 72 20 6c 6f 6f 70 22 al("server loop"
4920: 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
4930: 20 20 20 20 20 6e 4d 73 67 2b 2b 3b 0a 20 20 20 nMsg++;.
4940: 20 20 20 20 20 69 66 28 20 7a 50 43 6f 64 65 3d if( zPCode=
4950: 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
4960: 7a 50 43 6f 64 65 20 3d 20 6d 70 72 69 6e 74 66 zPCode = mprintf
4970: 28 22 25 62 22 2c 20 26 61 54 6f 6b 65 6e 5b 32 ("%b", &aToken[2
4980: 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 64 62 ]);. db
4990: 5f 73 65 74 28 22 70 72 6f 6a 65 63 74 2d 63 6f _set("project-co
49a0: 64 65 22 2c 20 7a 50 43 6f 64 65 29 3b 0a 20 20 de", zPCode);.
49b0: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
49c0: 63 6c 6f 6e 65 46 6c 61 67 20 3d 20 30 3b 0a 20 cloneFlag = 0;.
49d0: 20 20 20 20 20 20 20 70 75 6c 6c 46 6c 61 67 20 pullFlag
49e0: 3d 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 = 1;. }else
49f0: 0a 0a 20 20 20 20 20 20 2f 2a 20 20 20 65 72 72 .. /* err
4a00: 6f 72 20 4d 45 53 53 41 47 45 0a 20 20 20 20 20 or MESSAGE.
4a10: 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 52 65 70 **. ** Rep
4a20: 6f 72 74 20 61 6e 20 65 72 72 6f 72 0a 20 20 20 ort an error.
4a30: 20 20 20 2a 2f 20 20 20 20 20 20 20 20 0a 20 20 */ .
4a40: 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 if( blob_eq(
4a50: 26 61 54 6f 6b 65 6e 5b 30 5d 2c 22 65 72 72 6f &aToken[0],"erro
4a60: 72 22 29 20 26 26 20 6e 54 6f 6b 65 6e 3d 3d 32 r") && nToken==2
4a70: 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 ){. char
4a80: 20 2a 7a 4d 73 67 20 3d 20 62 6c 6f 62 5f 74 65 *zMsg = blob_te
4a90: 72 6d 69 6e 61 74 65 28 26 61 54 6f 6b 65 6e 5b rminate(&aToken[
4aa0: 31 5d 29 3b 0a 20 20 20 20 20 20 20 20 64 65 66 1]);. def
4ab0: 6f 73 73 69 6c 69 7a 65 28 7a 4d 73 67 29 3b 0a ossilize(zMsg);.
4ac0: 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 blob_app
4ad0: 65 6e 64 66 28 26 65 72 72 6d 73 67 2c 20 22 73 endf(&errmsg, "s
4ae0: 65 72 76 65 72 20 73 61 79 73 3a 20 25 73 22 2c erver says: %s",
4af0: 20 7a 4d 73 67 29 3b 0a 20 20 20 20 20 20 7d 65 zMsg);. }e
4b00: 6c 73 65 0a 0a 20 20 20 20 20 20 2f 2a 20 55 6e lse.. /* Un
4b10: 6b 6e 6f 77 6e 20 6d 65 73 73 61 67 65 20 2a 2f known message */
4b20: 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 . {.
4b30: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 65 blob_appendf(&e
4b40: 72 72 6d 73 67 2c 20 22 75 6e 6b 6e 6f 77 6e 20 rrmsg, "unknown
4b50: 63 6f 6d 6d 61 6e 64 3a 20 25 62 22 2c 20 26 61 command: %b", &a
4b60: 54 6f 6b 65 6e 5b 30 5d 29 3b 0a 20 20 20 20 20 Token[0]);.
4b70: 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 62 6c }.. if( bl
4b80: 6f 62 5f 73 69 7a 65 28 26 65 72 72 6d 73 67 29 ob_size(&errmsg)
4b90: 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6f 73 73 ){. foss
4ba0: 69 6c 5f 66 61 74 61 6c 28 22 25 62 22 2c 20 26 il_fatal("%b", &
4bb0: 65 72 72 6d 73 67 29 3b 0a 20 20 20 20 20 20 7d errmsg);. }
4bc0: 0a 20 20 20 20 20 20 62 6c 6f 62 61 72 72 61 79 . blobarray
4bd0: 5f 72 65 73 65 74 28 61 54 6f 6b 65 6e 2c 20 6e _reset(aToken, n
4be0: 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 Token);. }.
4bf0: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 72 65 blob_reset(&re
4c00: 63 76 29 3b 0a 20 20 20 20 70 72 69 6e 74 66 28 cv);. printf(
4c10: 22 52 65 63 65 69 76 65 64 3a 20 20 25 64 20 66 "Received: %d f
4c20: 69 6c 65 73 2c 20 25 64 20 72 65 71 75 65 73 74 iles, %d request
4c30: 73 2c 20 25 64 20 6f 74 68 65 72 20 6d 65 73 73 s, %d other mess
4c40: 61 67 65 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 ages\n",.
4c50: 20 20 20 20 20 6e 46 69 6c 65 2c 20 6e 52 65 71 nFile, nReq
4c60: 2c 20 6e 4d 73 67 29 3b 0a 20 20 20 20 6e 46 69 , nMsg);. nFi
4c70: 6c 65 20 3d 20 6e 52 65 71 20 3d 20 6e 4d 73 67 le = nReq = nMsg
4c80: 20 3d 20 30 3b 0a 20 20 7d 3b 0a 20 20 68 74 74 = 0;. };. htt
4c90: 70 5f 63 6c 6f 73 65 28 29 3b 0a 20 20 64 62 5f p_close();. db_
4ca0: 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 end_transaction(
4cb0: 30 29 3b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 0);. db_multi_e
4cc0: 78 65 63 28 0a 20 20 20 20 22 44 52 4f 50 20 54 xec(. "DROP T
4cd0: 41 42 4c 45 20 6f 6e 72 65 6d 6f 74 65 3b 22 0a ABLE onremote;".
4ce0: 20 20 20 20 22 44 52 4f 50 20 54 41 42 4c 45 20 "DROP TABLE
4cf0: 70 65 6e 64 69 6e 67 3b 22 0a 20 20 29 3b 0a 7d pending;". );.}
4d00: 0a .