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 36 20 44 2e 20 52 69 63 68 (c) 2006 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 50 72 6f 63 65 64 75 72 65 .**.** Procedure
0370: 73 20 73 74 6f 72 65 20 61 6e 64 20 72 65 74 72 s store and retr
0380: 69 65 76 65 20 72 65 63 6f 72 64 73 20 66 72 6f ieve records fro
0390: 6d 20 74 68 65 20 72 65 70 6f 73 69 74 6f 72 79 m the repository
03a0: 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 22 63 6f .*/.#include "co
03b0: 6e 66 69 67 2e 68 22 0a 23 69 6e 63 6c 75 64 65 nfig.h".#include
03c0: 20 22 63 6f 6e 74 65 6e 74 2e 68 22 0a 23 69 6e "content.h".#in
03d0: 63 6c 75 64 65 20 3c 61 73 73 65 72 74 2e 68 3e clude <assert.h>
03e0: 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
03f0: 68 65 20 73 72 63 69 64 20 61 73 73 6f 63 69 61 he srcid associa
0400: 74 65 64 20 77 69 74 68 20 72 69 64 2e 20 20 4f ted with rid. O
0410: 72 20 72 65 74 75 72 6e 20 30 20 69 66 20 72 69 r return 0 if ri
0420: 64 20 69 73 20 0a 2a 2a 20 6f 72 69 67 69 6e 61 d is .** origina
0430: 6c 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 6e 6f l content and no
0440: 74 20 61 20 64 65 6c 74 61 2e 0a 2a 2f 0a 73 74 t a delta..*/.st
0450: 61 74 69 63 20 69 6e 74 20 66 69 6e 64 53 72 63 atic int findSrc
0460: 69 64 28 69 6e 74 20 72 69 64 2c 20 63 6f 6e 73 id(int rid, cons
0470: 74 20 63 68 61 72 20 2a 7a 44 62 29 7b 0a 20 20 t char *zDb){.
0480: 53 74 6d 74 20 71 73 72 63 3b 0a 20 20 69 6e 74 Stmt qsrc;. int
0490: 20 73 72 63 69 64 3b 0a 20 20 69 66 28 20 7a 44 srcid;. if( zD
04a0: 62 20 29 7b 0a 20 20 20 20 64 62 5f 70 72 65 70 b ){. db_prep
04b0: 61 72 65 28 26 71 73 72 63 2c 20 22 53 45 4c 45 are(&qsrc, "SELE
04c0: 43 54 20 73 72 63 69 64 20 46 52 4f 4d 20 25 73 CT srcid FROM %s
04d0: 2e 64 65 6c 74 61 20 57 48 45 52 45 20 72 69 64 .delta WHERE rid
04e0: 3d 25 64 22 2c 20 7a 44 62 2c 20 72 69 64 29 3b =%d", zDb, rid);
04f0: 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62 . }else{. db
0500: 5f 70 72 65 70 61 72 65 28 26 71 73 72 63 2c 20 _prepare(&qsrc,
0510: 22 53 45 4c 45 43 54 20 73 72 63 69 64 20 46 52 "SELECT srcid FR
0520: 4f 4d 20 64 65 6c 74 61 20 57 48 45 52 45 20 72 OM delta WHERE r
0530: 69 64 3d 25 64 22 2c 20 72 69 64 29 3b 0a 20 20 id=%d", rid);.
0540: 7d 0a 20 20 69 66 28 20 64 62 5f 73 74 65 70 28 }. if( db_step(
0550: 26 71 73 72 63 29 3d 3d 53 51 4c 49 54 45 5f 52 &qsrc)==SQLITE_R
0560: 4f 57 20 29 7b 0a 20 20 20 20 73 72 63 69 64 20 OW ){. srcid
0570: 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 = db_column_int(
0580: 26 71 73 72 63 2c 20 30 29 3b 0a 20 20 7d 65 6c &qsrc, 0);. }el
0590: 73 65 7b 0a 20 20 20 20 73 72 63 69 64 20 3d 20 se{. srcid =
05a0: 30 3b 0a 20 20 7d 0a 20 20 64 62 5f 66 69 6e 61 0;. }. db_fina
05b0: 6c 69 7a 65 28 26 71 73 72 63 29 3b 0a 20 20 72 lize(&qsrc);. r
05c0: 65 74 75 72 6e 20 73 72 63 69 64 3b 0a 7d 0a 0a eturn srcid;.}..
05d0: 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 /*.** Extract th
05e0: 65 20 63 6f 6e 74 65 6e 74 20 66 6f 72 20 49 44 e content for ID
05f0: 20 72 69 64 20 61 6e 64 20 70 75 74 20 69 74 20 rid and put it
0600: 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 75 6e 69 6e into the.** unin
0610: 69 74 69 61 6c 69 7a 65 64 20 62 6c 6f 62 2e 0a itialized blob..
0620: 2a 2f 0a 76 6f 69 64 20 63 6f 6e 74 65 6e 74 5f */.void content_
0630: 67 65 74 5f 66 72 6f 6d 5f 64 62 28 69 6e 74 20 get_from_db(int
0640: 72 69 64 2c 20 42 6c 6f 62 20 2a 70 42 6c 6f 62 rid, Blob *pBlob
0650: 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 , const char *zD
0660: 62 29 7b 0a 20 20 53 74 6d 74 20 71 3b 0a 20 20 b){. Stmt q;.
0670: 69 6e 74 20 73 72 63 69 64 3b 0a 20 20 61 73 73 int srcid;. ass
0680: 65 72 74 28 20 67 2e 72 65 70 6f 73 69 74 6f 72 ert( g.repositor
0690: 79 4f 70 65 6e 20 29 3b 0a 20 20 73 72 63 69 64 yOpen );. srcid
06a0: 20 3d 20 66 69 6e 64 53 72 63 69 64 28 72 69 64 = findSrcid(rid
06b0: 2c 20 7a 44 62 29 3b 0a 20 20 69 66 28 20 7a 44 , zDb);. if( zD
06c0: 62 20 29 7b 0a 20 20 20 20 64 62 5f 70 72 65 70 b ){. db_prep
06d0: 61 72 65 28 26 71 2c 20 0a 20 20 20 20 20 20 20 are(&q, .
06e0: 22 53 45 4c 45 43 54 20 63 6f 6e 74 65 6e 74 20 "SELECT content
06f0: 46 52 4f 4d 20 25 73 2e 62 6c 6f 62 20 57 48 45 FROM %s.blob WHE
0700: 52 45 20 72 69 64 3d 25 64 20 41 4e 44 20 73 69 RE rid=%d AND si
0710: 7a 65 3e 3d 30 22 2c 0a 20 20 20 20 20 20 20 7a ze>=0",. z
0720: 44 62 2c 20 72 69 64 0a 20 20 20 20 29 3b 0a 20 Db, rid. );.
0730: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62 5f 70 }else{. db_p
0740: 72 65 70 61 72 65 28 26 71 2c 20 0a 20 20 20 20 repare(&q, .
0750: 20 20 20 22 53 45 4c 45 43 54 20 63 6f 6e 74 65 "SELECT conte
0760: 6e 74 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 nt FROM blob WHE
0770: 52 45 20 72 69 64 3d 25 64 20 41 4e 44 20 73 69 RE rid=%d AND si
0780: 7a 65 3e 3d 30 22 2c 0a 20 20 20 20 20 20 20 72 ze>=0",. r
0790: 69 64 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 20 20 id. );. }.
07a0: 69 66 28 20 73 72 63 69 64 20 29 7b 0a 20 20 20 if( srcid ){.
07b0: 20 42 6c 6f 62 20 73 72 63 3b 0a 20 20 20 20 63 Blob src;. c
07c0: 6f 6e 74 65 6e 74 5f 67 65 74 5f 66 72 6f 6d 5f ontent_get_from_
07d0: 64 62 28 73 72 63 69 64 2c 20 26 73 72 63 2c 20 db(srcid, &src,
07e0: 7a 44 62 29 3b 0a 20 20 20 20 69 66 28 20 64 62 zDb);. if( db
07f0: 5f 73 74 65 70 28 26 71 29 3d 3d 53 51 4c 49 54 _step(&q)==SQLIT
0800: 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 42 E_ROW ){. B
0810: 6c 6f 62 20 64 65 6c 74 61 3b 0a 20 20 20 20 20 lob delta;.
0820: 20 64 62 5f 65 70 68 65 6d 65 72 61 6c 5f 62 6c db_ephemeral_bl
0830: 6f 62 28 26 71 2c 20 30 2c 20 26 64 65 6c 74 61 ob(&q, 0, &delta
0840: 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 75 6e );. blob_un
0850: 63 6f 6d 70 72 65 73 73 28 26 64 65 6c 74 61 2c compress(&delta,
0860: 20 26 64 65 6c 74 61 29 3b 0a 20 20 20 20 20 20 &delta);.
0870: 62 6c 6f 62 5f 69 6e 69 74 28 70 42 6c 6f 62 2c blob_init(pBlob,
0880: 30 2c 30 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 0,0);. blob
0890: 5f 64 65 6c 74 61 5f 61 70 70 6c 79 28 26 73 72 _delta_apply(&sr
08a0: 63 2c 20 26 64 65 6c 74 61 2c 20 70 42 6c 6f 62 c, &delta, pBlob
08b0: 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 72 65 );. blob_re
08c0: 73 65 74 28 26 64 65 6c 74 61 29 3b 0a 20 20 20 set(&delta);.
08d0: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 62 6c }else{. bl
08e0: 6f 62 5f 69 6e 69 74 28 70 42 6c 6f 62 2c 20 30 ob_init(pBlob, 0
08f0: 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 , 0);. }.
0900: 62 6c 6f 62 5f 72 65 73 65 74 28 26 73 72 63 29 blob_reset(&src)
0910: 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 ;. }else{. i
0920: 66 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d f( db_step(&q)==
0930: 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 SQLITE_ROW ){.
0940: 20 20 20 20 64 62 5f 65 70 68 65 6d 65 72 61 6c db_ephemeral
0950: 5f 62 6c 6f 62 28 26 71 2c 20 30 2c 20 70 42 6c _blob(&q, 0, pBl
0960: 6f 62 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f ob);. blob_
0970: 75 6e 63 6f 6d 70 72 65 73 73 28 70 42 6c 6f 62 uncompress(pBlob
0980: 2c 20 70 42 6c 6f 62 29 3b 0a 20 20 20 20 7d 65 , pBlob);. }e
0990: 6c 73 65 7b 0a 20 20 20 20 20 20 62 6c 6f 62 5f lse{. blob_
09a0: 69 6e 69 74 28 70 42 6c 6f 62 2c 30 2c 20 30 29 init(pBlob,0, 0)
09b0: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 64 62 ;. }. }. db
09c0: 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 7d _finalize(&q);.}
09d0: 0a 76 6f 69 64 20 63 6f 6e 74 65 6e 74 5f 67 65 .void content_ge
09e0: 74 28 69 6e 74 20 72 69 64 2c 20 42 6c 6f 62 20 t(int rid, Blob
09f0: 2a 70 42 6c 6f 62 29 7b 0a 20 20 63 6f 6e 74 65 *pBlob){. conte
0a00: 6e 74 5f 67 65 74 5f 66 72 6f 6d 5f 64 62 28 72 nt_get_from_db(r
0a10: 69 64 2c 20 70 42 6c 6f 62 2c 20 30 29 3b 0a 7d id, pBlob, 0);.}
0a20: 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a ../*.** COMMAND:
0a30: 20 20 74 65 73 74 2d 63 6f 6e 74 65 6e 74 2d 67 test-content-g
0a40: 65 74 0a 2a 2a 0a 2a 2a 20 45 78 74 72 61 63 74 et.**.** Extract
0a50: 20 61 20 62 6c 6f 62 20 66 72 6f 6d 20 74 68 65 a blob from the
0a60: 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 77 72 database and wr
0a70: 69 74 65 20 69 74 20 69 6e 74 6f 20 61 20 66 69 ite it into a fi
0a80: 6c 65 2e 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 74 le..*/.void test
0a90: 5f 63 6f 6e 74 65 6e 74 5f 67 65 74 5f 63 6d 64 _content_get_cmd
0aa0: 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72 69 (void){. int ri
0ab0: 64 3b 0a 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e d;. Blob conten
0ac0: 74 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 t;. const char
0ad0: 2a 7a 46 69 6c 65 3b 0a 20 20 69 66 28 20 67 2e *zFile;. if( g.
0ae0: 61 72 67 63 21 3d 34 20 26 26 20 67 2e 61 72 67 argc!=4 && g.arg
0af0: 63 21 3d 33 20 29 20 75 73 61 67 65 28 22 52 45 c!=3 ) usage("RE
0b00: 43 4f 52 44 49 44 20 3f 46 49 4c 45 4e 41 4d 45 CORDID ?FILENAME
0b10: 3f 22 29 3b 0a 20 20 7a 46 69 6c 65 20 3d 20 67 ?");. zFile = g
0b20: 2e 61 72 67 63 3d 3d 34 20 3f 20 67 2e 61 72 67 .argc==4 ? g.arg
0b30: 76 5b 33 5d 20 3a 20 22 2d 22 3b 0a 20 20 64 62 v[3] : "-";. db
0b40: 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f _must_be_within_
0b50: 74 72 65 65 28 29 3b 0a 20 20 72 69 64 20 3d 20 tree();. rid =
0b60: 6e 61 6d 65 5f 74 6f 5f 72 69 64 28 67 2e 61 72 name_to_rid(g.ar
0b70: 67 76 5b 32 5d 29 3b 0a 20 20 63 6f 6e 74 65 6e gv[2]);. conten
0b80: 74 5f 67 65 74 28 72 69 64 2c 20 26 63 6f 6e 74 t_get(rid, &cont
0b90: 65 6e 74 29 3b 0a 20 20 62 6c 6f 62 5f 77 72 69 ent);. blob_wri
0ba0: 74 65 5f 74 6f 5f 66 69 6c 65 28 26 63 6f 6e 74 te_to_file(&cont
0bb0: 65 6e 74 2c 20 7a 46 69 6c 65 29 3b 0a 7d 0a 0a ent, zFile);.}..
0bc0: 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 20 /*.** COMMAND:
0bd0: 74 65 73 74 2d 63 6f 6e 74 65 6e 74 2d 72 61 77 test-content-raw
0be0: 67 65 74 0a 2a 2a 0a 2a 2a 20 45 78 74 72 61 63 get.**.** Extrac
0bf0: 74 20 61 20 62 6c 6f 62 20 66 72 6f 6d 20 74 68 t a blob from th
0c00: 65 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 77 e database and w
0c10: 72 69 74 65 20 69 74 20 69 6e 74 6f 20 61 20 66 rite it into a f
0c20: 69 6c 65 2e 20 20 54 68 69 73 0a 2a 2a 20 76 65 ile. This.** ve
0c30: 72 73 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 65 rsion does not e
0c40: 78 70 61 6e 64 20 74 68 65 20 64 65 6c 74 61 2e xpand the delta.
0c50: 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 74 5f 63 6f .*/.void test_co
0c60: 6e 74 65 6e 74 5f 72 61 77 67 65 74 5f 63 6d 64 ntent_rawget_cmd
0c70: 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72 69 (void){. int ri
0c80: 64 3b 0a 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e d;. Blob conten
0c90: 74 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 t;. const char
0ca0: 2a 7a 46 69 6c 65 3b 0a 20 20 69 66 28 20 67 2e *zFile;. if( g.
0cb0: 61 72 67 63 21 3d 34 20 26 26 20 67 2e 61 72 67 argc!=4 && g.arg
0cc0: 63 21 3d 33 20 29 20 75 73 61 67 65 28 22 52 45 c!=3 ) usage("RE
0cd0: 43 4f 52 44 49 44 20 3f 46 49 4c 45 4e 41 4d 45 CORDID ?FILENAME
0ce0: 3f 22 29 3b 0a 20 20 7a 46 69 6c 65 20 3d 20 67 ?");. zFile = g
0cf0: 2e 61 72 67 63 3d 3d 34 20 3f 20 67 2e 61 72 67 .argc==4 ? g.arg
0d00: 76 5b 33 5d 20 3a 20 22 2d 22 3b 0a 20 20 64 62 v[3] : "-";. db
0d10: 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f _must_be_within_
0d20: 74 72 65 65 28 29 3b 0a 20 20 72 69 64 20 3d 20 tree();. rid =
0d30: 6e 61 6d 65 5f 74 6f 5f 72 69 64 28 67 2e 61 72 name_to_rid(g.ar
0d40: 67 76 5b 32 5d 29 3b 0a 20 20 62 6c 6f 62 5f 7a gv[2]);. blob_z
0d50: 65 72 6f 28 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 ero(&content);.
0d60: 20 64 62 5f 62 6c 6f 62 28 26 63 6f 6e 74 65 6e db_blob(&conten
0d70: 74 2c 20 22 53 45 4c 45 43 54 20 63 6f 6e 74 65 t, "SELECT conte
0d80: 6e 74 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 nt FROM blob WHE
0d90: 52 45 20 72 69 64 3d 25 64 22 2c 20 72 69 64 29 RE rid=%d", rid)
0da0: 3b 0a 20 20 62 6c 6f 62 5f 75 6e 63 6f 6d 70 72 ;. blob_uncompr
0db0: 65 73 73 28 26 63 6f 6e 74 65 6e 74 2c 20 26 63 ess(&content, &c
0dc0: 6f 6e 74 65 6e 74 29 3b 0a 20 20 62 6c 6f 62 5f ontent);. blob_
0dd0: 77 72 69 74 65 5f 74 6f 5f 66 69 6c 65 28 26 63 write_to_file(&c
0de0: 6f 6e 74 65 6e 74 2c 20 7a 46 69 6c 65 29 3b 0a ontent, zFile);.
0df0: 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 63 }../*.** Write c
0e00: 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 ontent into the
0e10: 64 61 74 61 62 61 73 65 2e 20 20 52 65 74 75 72 database. Retur
0e20: 6e 20 74 68 65 20 72 65 63 6f 72 64 20 49 44 2e n the record ID.
0e30: 20 20 49 66 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 If the.** cont
0e40: 65 6e 74 20 69 73 20 61 6c 72 65 61 64 79 20 69 ent is already i
0e50: 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 n the database,
0e60: 6a 75 73 74 20 72 65 74 75 72 6e 20 74 68 65 20 just return the
0e70: 72 65 63 6f 72 64 20 49 44 2e 0a 2a 2a 0a 2a 2a record ID..**.**
0e80: 20 41 20 70 68 61 6e 74 6f 6d 20 69 73 20 77 72 A phantom is wr
0e90: 69 74 74 65 6e 20 69 66 20 70 42 6c 6f 62 3d 3d itten if pBlob==
0ea0: 30 2e 20 20 49 66 20 70 42 6c 6f 62 3d 3d 30 20 0. If pBlob==0
0eb0: 74 68 65 6e 20 74 68 65 20 55 55 49 44 20 69 73 then the UUID is
0ec0: 20 73 65 74 0a 2a 2a 20 74 6f 20 7a 55 75 69 64 set.** to zUuid
0ed0: 2e 20 20 4f 74 68 65 72 77 69 73 65 20 7a 55 75 . Otherwise zUu
0ee0: 69 64 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a id is ignored..*
0ef0: 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 63 6f *.** If the reco
0f00: 72 64 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 rd already exist
0f10: 73 20 62 75 74 20 69 73 20 61 20 70 68 61 6e 74 s but is a phant
0f20: 6f 6d 2c 20 74 68 65 20 70 42 6c 6f 62 20 63 6f om, the pBlob co
0f30: 6e 74 65 6e 74 0a 2a 2a 20 69 73 20 69 6e 73 65 ntent.** is inse
0f40: 72 74 65 64 20 61 6e 64 20 74 68 65 20 70 68 61 rted and the pha
0f50: 74 6f 6d 20 62 65 63 6f 6d 65 73 20 61 20 72 65 tom becomes a re
0f60: 61 6c 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a 69 6e al record..*/.in
0f70: 74 20 63 6f 6e 74 65 6e 74 5f 70 75 74 28 42 6c t content_put(Bl
0f80: 6f 62 20 2a 70 42 6c 6f 62 2c 20 63 6f 6e 73 74 ob *pBlob, const
0f90: 20 63 68 61 72 20 2a 7a 55 75 69 64 29 7b 0a 20 char *zUuid){.
0fa0: 20 69 6e 74 20 73 69 7a 65 3b 0a 20 20 69 6e 74 int size;. int
0fb0: 20 72 69 64 3b 0a 20 20 53 74 6d 74 20 73 31 3b rid;. Stmt s1;
0fc0: 0a 20 20 42 6c 6f 62 20 63 6d 70 72 3b 0a 20 20 . Blob cmpr;.
0fd0: 42 6c 6f 62 20 68 61 73 68 3b 0a 20 20 61 73 73 Blob hash;. ass
0fe0: 65 72 74 28 20 67 2e 72 65 70 6f 73 69 74 6f 72 ert( g.repositor
0ff0: 79 4f 70 65 6e 20 29 3b 0a 20 20 69 66 28 20 70 yOpen );. if( p
1000: 42 6c 6f 62 3d 3d 30 20 29 7b 0a 20 20 20 20 62 Blob==0 ){. b
1010: 6c 6f 62 5f 69 6e 69 74 28 26 68 61 73 68 2c 20 lob_init(&hash,
1020: 7a 55 75 69 64 2c 20 2d 31 29 3b 0a 20 20 20 20 zUuid, -1);.
1030: 73 69 7a 65 20 3d 20 2d 31 3b 0a 20 20 7d 65 6c size = -1;. }el
1040: 73 65 7b 0a 20 20 20 20 73 68 61 31 73 75 6d 5f se{. sha1sum_
1050: 62 6c 6f 62 28 70 42 6c 6f 62 2c 20 26 68 61 73 blob(pBlob, &has
1060: 68 29 3b 0a 20 20 20 20 73 69 7a 65 20 3d 20 62 h);. size = b
1070: 6c 6f 62 5f 73 69 7a 65 28 70 42 6c 6f 62 29 3b lob_size(pBlob);
1080: 0a 20 20 7d 0a 20 20 64 62 5f 62 65 67 69 6e 5f . }. db_begin_
1090: 74 72 61 6e 73 61 63 74 69 6f 6e 28 29 3b 0a 0a transaction();..
10a0: 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 /* Check to se
10b0: 65 20 69 66 20 74 68 65 20 65 6e 74 72 79 20 61 e if the entry a
10c0: 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 61 6e lready exists an
10d0: 64 20 69 66 20 69 74 20 64 6f 65 73 20 77 68 65 d if it does whe
10e0: 74 68 65 72 0a 20 20 2a 2a 20 6f 72 20 6e 6f 74 ther. ** or not
10f0: 20 74 68 65 20 65 6e 74 72 79 20 69 73 20 61 20 the entry is a
1100: 70 68 61 6e 74 6f 6d 0a 20 20 2a 2f 0a 20 20 64 phantom. */. d
1110: 62 5f 70 72 65 70 61 72 65 28 26 73 31 2c 20 22 b_prepare(&s1, "
1120: 53 45 4c 45 43 54 20 72 69 64 2c 20 73 69 7a 65 SELECT rid, size
1130: 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 FROM blob WHERE
1140: 20 75 75 69 64 3d 25 42 22 2c 20 26 68 61 73 68 uuid=%B", &hash
1150: 29 3b 0a 20 20 69 66 28 20 64 62 5f 73 74 65 70 );. if( db_step
1160: 28 26 73 31 29 3d 3d 53 51 4c 49 54 45 5f 52 4f (&s1)==SQLITE_RO
1170: 57 20 29 7b 0a 20 20 20 20 72 69 64 20 3d 20 64 W ){. rid = d
1180: 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 73 31 b_column_int(&s1
1190: 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 64 62 , 0);. if( db
11a0: 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 73 31 2c _column_int(&s1,
11b0: 20 31 29 3e 3d 30 20 7c 7c 20 70 42 6c 6f 62 3d 1)>=0 || pBlob=
11c0: 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 45 =0 ){. /* E
11d0: 69 74 68 65 72 20 74 68 65 20 65 6e 74 72 79 20 ither the entry
11e0: 69 73 20 6e 6f 74 20 61 20 70 68 61 6e 74 6f 6d is not a phantom
11f0: 20 6f 72 20 69 74 20 69 73 20 61 20 70 68 61 6e or it is a phan
1200: 74 6f 6d 20 62 75 74 20 77 65 0a 20 20 20 20 20 tom but we.
1210: 20 2a 2a 20 68 61 76 65 20 6e 6f 20 64 61 74 61 ** have no data
1220: 20 77 69 74 68 20 77 68 69 63 68 20 74 6f 20 64 with which to d
1230: 65 70 68 61 74 68 6f 6d 69 7a 65 20 69 74 2e 20 ephathomize it.
1240: 20 49 6e 20 65 69 74 68 65 72 20 63 61 73 65 2c In either case,
1250: 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 72 65 20 . ** there
1260: 69 73 20 6e 6f 74 68 69 6e 67 20 66 6f 72 20 75 is nothing for u
1270: 73 65 20 74 6f 20 64 6f 20 6f 74 68 65 72 20 74 se to do other t
1280: 68 61 6e 20 72 65 74 75 72 6e 20 74 68 65 20 52 han return the R
1290: 49 44 2e 20 2a 2f 0a 20 20 20 20 20 20 64 62 5f ID. */. db_
12a0: 66 69 6e 61 6c 69 7a 65 28 26 73 31 29 3b 0a 20 finalize(&s1);.
12b0: 20 20 20 20 20 64 62 5f 65 6e 64 5f 74 72 61 6e db_end_tran
12c0: 73 61 63 74 69 6f 6e 28 30 29 3b 0a 20 20 20 20 saction(0);.
12d0: 20 20 72 65 74 75 72 6e 20 72 69 64 3b 0a 20 20 return rid;.
12e0: 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
12f0: 20 72 69 64 20 3d 20 30 3b 20 20 2f 2a 20 4e 6f rid = 0; /* No
1300: 20 65 6e 74 72 79 20 77 69 74 68 20 74 68 65 20 entry with the
1310: 73 61 6d 65 20 55 55 49 44 20 63 75 72 72 65 6e same UUID curren
1320: 74 6c 79 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 tly exists */.
1330: 7d 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 }. db_finalize(
1340: 26 73 31 29 3b 0a 0a 20 20 2f 2a 20 43 6f 6e 73 &s1);.. /* Cons
1350: 74 72 75 63 74 20 61 20 72 65 63 65 69 76 65 64 truct a received
1360: 2d 66 72 6f 6d 20 49 44 20 69 66 20 77 65 20 64 -from ID if we d
1370: 6f 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 61 o not already ha
1380: 76 65 20 6f 6e 65 20 2a 2f 0a 20 20 69 66 28 20 ve one */. if(
1390: 67 2e 72 63 76 69 64 3d 3d 30 20 26 26 20 70 42 g.rcvid==0 && pB
13a0: 6c 6f 62 21 3d 30 20 29 7b 0a 20 20 20 20 64 62 lob!=0 ){. db
13b0: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 _multi_exec(.
13c0: 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f "INSERT INTO
13d0: 20 72 63 76 66 72 6f 6d 28 75 69 64 2c 20 6d 74 rcvfrom(uid, mt
13e0: 69 6d 65 2c 20 6e 6f 6e 63 65 2c 20 69 70 61 64 ime, nonce, ipad
13f0: 64 72 29 22 0a 20 20 20 20 20 20 20 22 56 41 4c dr)". "VAL
1400: 55 45 53 28 25 64 2c 20 6a 75 6c 69 61 6e 64 61 UES(%d, julianda
1410: 79 28 27 6e 6f 77 27 29 2c 20 25 51 2c 20 25 51 y('now'), %Q, %Q
1420: 29 22 2c 0a 20 20 20 20 20 20 20 67 2e 75 73 65 )",. g.use
1430: 72 55 69 64 2c 20 67 2e 7a 4e 6f 6e 63 65 2c 20 rUid, g.zNonce,
1440: 67 2e 7a 49 70 41 64 64 72 0a 20 20 20 20 29 3b g.zIpAddr. );
1450: 0a 20 20 20 20 67 2e 72 63 76 69 64 20 3d 20 64 . g.rcvid = d
1460: 62 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f b_last_insert_ro
1470: 77 69 64 28 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 wid();. }.. if
1480: 28 20 72 69 64 3e 30 20 29 7b 0a 20 20 20 20 2f ( rid>0 ){. /
1490: 2a 20 57 65 20 61 72 65 20 6a 75 73 74 20 61 64 * We are just ad
14a0: 64 69 6e 67 20 64 61 74 61 20 74 6f 20 61 20 70 ding data to a p
14b0: 68 61 6e 74 6f 6d 20 2a 2f 0a 20 20 20 20 61 73 hantom */. as
14c0: 73 65 72 74 28 20 70 42 6c 6f 62 21 3d 30 20 29 sert( pBlob!=0 )
14d0: 3b 0a 20 20 20 20 64 62 5f 70 72 65 70 61 72 65 ;. db_prepare
14e0: 28 26 73 31 2c 0a 20 20 20 20 20 20 22 55 50 44 (&s1,. "UPD
14f0: 41 54 45 20 62 6c 6f 62 20 53 45 54 20 72 63 76 ATE blob SET rcv
1500: 69 64 3d 25 64 2c 20 73 69 7a 65 3d 25 64 2c 20 id=%d, size=%d,
1510: 63 6f 6e 74 65 6e 74 3d 3a 64 61 74 61 20 57 48 content=:data WH
1520: 45 52 45 20 72 69 64 3d 25 64 22 2c 0a 20 20 20 ERE rid=%d",.
1530: 20 20 20 20 67 2e 72 63 76 69 64 2c 20 73 69 7a g.rcvid, siz
1540: 65 2c 20 72 69 64 0a 20 20 20 20 29 3b 0a 20 20 e, rid. );.
1550: 20 20 62 6c 6f 62 5f 63 6f 6d 70 72 65 73 73 28 blob_compress(
1560: 70 42 6c 6f 62 2c 20 26 63 6d 70 72 29 3b 0a 20 pBlob, &cmpr);.
1570: 20 20 20 64 62 5f 62 69 6e 64 5f 62 6c 6f 62 28 db_bind_blob(
1580: 26 73 31 2c 20 22 3a 64 61 74 61 22 2c 20 26 63 &s1, ":data", &c
1590: 6d 70 72 29 3b 0a 20 20 20 20 64 62 5f 65 78 65 mpr);. db_exe
15a0: 63 28 26 73 31 29 3b 0a 20 20 7d 65 6c 73 65 7b c(&s1);. }else{
15b0: 0a 20 20 20 20 2f 2a 20 57 65 20 61 72 65 20 63 . /* We are c
15c0: 72 65 61 74 69 6e 67 20 61 20 6e 65 77 20 65 6e reating a new en
15d0: 74 72 79 20 2a 2f 0a 20 20 20 20 64 62 5f 70 72 try */. db_pr
15e0: 65 70 61 72 65 28 26 73 31 2c 0a 20 20 20 20 20 epare(&s1,.
15f0: 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 62 6c "INSERT INTO bl
1600: 6f 62 28 72 63 76 69 64 2c 73 69 7a 65 2c 75 75 ob(rcvid,size,uu
1610: 69 64 2c 63 6f 6e 74 65 6e 74 29 22 0a 20 20 20 id,content)".
1620: 20 20 20 22 56 41 4c 55 45 53 28 25 64 2c 25 64 "VALUES(%d,%d
1630: 2c 27 25 73 27 2c 3a 64 61 74 61 29 22 2c 0a 20 ,'%s',:data)",.
1640: 20 20 20 20 20 20 67 2e 72 63 76 69 64 2c 20 73 g.rcvid, s
1650: 69 7a 65 2c 20 62 6c 6f 62 5f 73 74 72 28 26 68 ize, blob_str(&h
1660: 61 73 68 29 0a 20 20 20 20 29 3b 0a 20 20 20 20 ash). );.
1670: 69 66 28 20 70 42 6c 6f 62 20 29 7b 0a 20 20 20 if( pBlob ){.
1680: 20 20 20 62 6c 6f 62 5f 63 6f 6d 70 72 65 73 73 blob_compress
1690: 28 70 42 6c 6f 62 2c 20 26 63 6d 70 72 29 3b 0a (pBlob, &cmpr);.
16a0: 20 20 20 20 20 20 64 62 5f 62 69 6e 64 5f 62 6c db_bind_bl
16b0: 6f 62 28 26 73 31 2c 20 22 3a 64 61 74 61 22 2c ob(&s1, ":data",
16c0: 20 26 63 6d 70 72 29 3b 0a 20 20 20 20 7d 0a 20 &cmpr);. }.
16d0: 20 20 20 64 62 5f 65 78 65 63 28 26 73 31 29 3b db_exec(&s1);
16e0: 0a 20 20 20 20 72 69 64 20 3d 20 64 62 5f 6c 61 . rid = db_la
16f0: 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 st_insert_rowid(
1700: 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 46 69 );. }... /* Fi
1710: 6e 69 73 68 20 74 68 65 20 74 72 61 6e 73 61 63 nish the transac
1720: 74 69 6f 6e 20 61 6e 64 20 63 6c 65 61 6e 75 70 tion and cleanup
1730: 20 2a 2f 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a */. db_finaliz
1740: 65 28 26 73 31 29 3b 0a 20 20 64 62 5f 65 6e 64 e(&s1);. db_end
1750: 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 30 29 3b _transaction(0);
1760: 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 68 . blob_reset(&h
1770: 61 73 68 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 ash);.. /* Make
1780: 20 61 72 72 61 6e 67 65 6d 65 6e 74 73 20 74 6f arrangements to
1790: 20 76 65 72 69 66 79 20 74 68 61 74 20 74 68 65 verify that the
17a0: 20 64 61 74 61 20 63 61 6e 20 62 65 20 72 65 63 data can be rec
17b0: 6f 76 65 72 65 64 0a 20 20 2a 2a 20 62 65 66 6f overed. ** befo
17c0: 72 65 20 77 65 20 63 6f 6d 6d 69 74 20 2a 2f 0a re we commit */.
17d0: 20 20 69 66 28 20 70 42 6c 6f 62 20 29 7b 0a 20 if( pBlob ){.
17e0: 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 63 blob_reset(&c
17f0: 6d 70 72 29 3b 0a 20 20 20 20 76 65 72 69 66 79 mpr);. verify
1800: 5f 62 65 66 6f 72 65 5f 63 6f 6d 6d 69 74 28 72 _before_commit(r
1810: 69 64 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 id);. }. retur
1820: 6e 20 72 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 n rid;.}../*.**
1830: 43 4f 4d 4d 41 4e 44 3a 20 20 74 65 73 74 2d 63 COMMAND: test-c
1840: 6f 6e 74 65 6e 74 2d 70 75 74 0a 2a 2a 0a 2a 2a ontent-put.**.**
1850: 20 45 78 74 72 61 63 74 20 61 20 62 6c 6f 62 20 Extract a blob
1860: 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 from the databas
1870: 65 20 61 6e 64 20 77 72 69 74 65 20 69 74 20 69 e and write it i
1880: 6e 74 6f 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 76 nto a file..*/.v
1890: 6f 69 64 20 74 65 73 74 5f 63 6f 6e 74 65 6e 74 oid test_content
18a0: 5f 70 75 74 5f 63 6d 64 28 76 6f 69 64 29 7b 0a _put_cmd(void){.
18b0: 20 20 69 6e 74 20 72 69 64 3b 0a 20 20 42 6c 6f int rid;. Blo
18c0: 62 20 63 6f 6e 74 65 6e 74 3b 0a 20 20 69 66 28 b content;. if(
18d0: 20 67 2e 61 72 67 63 21 3d 33 20 29 20 75 73 61 g.argc!=3 ) usa
18e0: 67 65 28 22 46 49 4c 45 4e 41 4d 45 22 29 3b 0a ge("FILENAME");.
18f0: 20 20 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74 db_must_be_wit
1900: 68 69 6e 5f 74 72 65 65 28 29 3b 0a 20 20 75 73 hin_tree();. us
1910: 65 72 5f 73 65 6c 65 63 74 28 29 3b 0a 20 20 62 er_select();. b
1920: 6c 6f 62 5f 72 65 61 64 5f 66 72 6f 6d 5f 66 69 lob_read_from_fi
1930: 6c 65 28 26 63 6f 6e 74 65 6e 74 2c 20 67 2e 61 le(&content, g.a
1940: 72 67 76 5b 32 5d 29 3b 0a 20 20 72 69 64 20 3d rgv[2]);. rid =
1950: 20 63 6f 6e 74 65 6e 74 5f 70 75 74 28 26 63 6f content_put(&co
1960: 6e 74 65 6e 74 2c 20 30 29 3b 0a 20 20 70 72 69 ntent, 0);. pri
1970: 6e 74 66 28 22 69 6e 73 65 72 74 65 64 20 61 73 ntf("inserted as
1980: 20 72 65 63 6f 72 64 20 25 64 5c 6e 22 2c 20 72 record %d\n", r
1990: 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 id);.}../*.** Ma
19a0: 6b 65 20 73 75 72 65 20 74 68 65 20 63 6f 6e 74 ke sure the cont
19b0: 65 6e 74 20 61 74 20 72 69 64 20 69 73 20 74 68 ent at rid is th
19c0: 65 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 74 65 e original conte
19d0: 6e 74 20 61 6e 64 20 69 73 20 6e 6f 74 20 61 0a nt and is not a.
19e0: 2a 2a 20 64 65 6c 74 61 2e 0a 2a 2f 0a 76 6f 69 ** delta..*/.voi
19f0: 64 20 63 6f 6e 74 65 6e 74 5f 75 6e 64 65 6c 74 d content_undelt
1a00: 61 28 69 6e 74 20 72 69 64 29 7b 0a 20 20 69 66 a(int rid){. if
1a10: 28 20 66 69 6e 64 53 72 63 69 64 28 72 69 64 2c ( findSrcid(rid,
1a20: 20 30 29 3e 30 20 29 7b 0a 20 20 20 20 42 6c 6f 0)>0 ){. Blo
1a30: 62 20 78 3b 0a 20 20 20 20 53 74 6d 74 20 73 3b b x;. Stmt s;
1a40: 0a 20 20 20 20 63 6f 6e 74 65 6e 74 5f 67 65 74 . content_get
1a50: 28 72 69 64 2c 20 26 78 29 3b 0a 20 20 20 20 64 (rid, &x);. d
1a60: 62 5f 70 72 65 70 61 72 65 28 26 73 2c 20 22 55 b_prepare(&s, "U
1a70: 50 44 41 54 45 20 62 6c 6f 62 20 53 45 54 20 63 PDATE blob SET c
1a80: 6f 6e 74 65 6e 74 3d 3a 63 20 57 48 45 52 45 20 ontent=:c WHERE
1a90: 72 69 64 3d 25 64 22 2c 20 72 69 64 29 3b 0a 20 rid=%d", rid);.
1aa0: 20 20 20 64 62 5f 62 69 6e 64 5f 62 6c 6f 62 28 db_bind_blob(
1ab0: 26 73 2c 20 22 3a 63 22 2c 20 26 78 29 3b 0a 20 &s, ":c", &x);.
1ac0: 20 20 20 64 62 5f 65 78 65 63 28 26 73 29 3b 0a db_exec(&s);.
1ad0: 20 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 db_finalize(
1ae0: 26 73 29 3b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 &s);. db_mult
1af0: 69 5f 65 78 65 63 28 22 44 45 4c 45 54 45 20 46 i_exec("DELETE F
1b00: 52 4f 4d 20 64 65 6c 74 61 20 57 48 45 52 45 20 ROM delta WHERE
1b10: 72 69 64 3d 25 64 22 2c 20 72 69 64 29 3b 0a 20 rid=%d", rid);.
1b20: 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d }.}../*.** COMM
1b30: 41 4e 44 3a 20 20 74 65 73 74 2d 63 6f 6e 74 65 AND: test-conte
1b40: 6e 74 2d 75 6e 64 65 6c 74 61 0a 2a 2a 0a 2a 2a nt-undelta.**.**
1b50: 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 63 Make sure the c
1b60: 6f 6e 74 65 6e 74 20 61 74 20 52 45 43 4f 52 44 ontent at RECORD
1b70: 49 44 20 69 73 20 6e 6f 74 20 61 20 64 65 6c 74 ID is not a delt
1b80: 61 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 74 5f 63 a.*/.void test_c
1b90: 6f 6e 74 65 6e 74 5f 75 6e 64 65 6c 74 61 5f 63 ontent_undelta_c
1ba0: 6d 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 md(void){. int
1bb0: 72 69 64 3b 0a 20 20 69 66 28 20 67 2e 61 72 67 rid;. if( g.arg
1bc0: 63 21 3d 32 20 29 20 75 73 61 67 65 28 22 52 45 c!=2 ) usage("RE
1bd0: 43 4f 52 44 49 44 22 29 3b 0a 20 20 64 62 5f 6d CORDID");. db_m
1be0: 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74 72 ust_be_within_tr
1bf0: 65 65 28 29 3b 0a 20 20 72 69 64 20 3d 20 61 74 ee();. rid = at
1c00: 6f 69 28 67 2e 61 72 67 76 5b 32 5d 29 3b 0a 20 oi(g.argv[2]);.
1c10: 20 63 6f 6e 74 65 6e 74 5f 75 6e 64 65 6c 74 61 content_undelta
1c20: 28 72 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 (rid);.}../*.**
1c30: 43 68 61 6e 67 65 20 74 68 65 20 73 74 6f 72 61 Change the stora
1c40: 67 65 20 6f 66 20 72 69 64 20 73 6f 20 74 68 61 ge of rid so tha
1c50: 74 20 69 74 20 69 73 20 61 20 64 65 6c 74 61 20 t it is a delta
1c60: 6f 66 20 73 72 63 69 64 2e 0a 2a 2a 0a 2a 2a 20 of srcid..**.**
1c70: 49 66 20 72 69 64 20 69 73 20 61 6c 72 65 61 64 If rid is alread
1c80: 79 20 61 20 64 65 6c 74 61 20 66 72 6f 6d 20 73 y a delta from s
1c90: 6f 6d 65 20 6f 74 68 65 72 20 70 6c 61 63 65 20 ome other place
1ca0: 74 68 65 6e 20 6e 6f 0a 2a 2a 20 63 6f 6e 76 65 then no.** conve
1cb0: 72 73 69 6f 6e 20 6f 63 63 75 72 73 20 61 6e 64 rsion occurs and
1cc0: 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 this is a no-op
1cd0: 20 75 6e 6c 65 73 73 20 66 6f 72 63 65 3d 3d 31 unless force==1
1ce0: 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 72 63 69 64 ..**.** If srcid
1cf0: 20 69 73 20 61 20 64 65 6c 74 61 20 74 68 61 74 is a delta that
1d00: 20 64 65 70 65 6e 64 73 20 6f 6e 20 72 69 64 2c depends on rid,
1d10: 20 74 68 65 6e 20 73 72 63 69 64 20 69 73 0a 2a then srcid is.*
1d20: 2a 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 75 * converted to u
1d30: 6e 64 65 6c 74 61 65 64 20 74 65 78 74 2e 0a 2a ndeltaed text..*
1d40: 2f 0a 76 6f 69 64 20 63 6f 6e 74 65 6e 74 5f 64 /.void content_d
1d50: 65 6c 74 69 66 79 28 69 6e 74 20 72 69 64 2c 20 eltify(int rid,
1d60: 69 6e 74 20 73 72 63 69 64 2c 20 69 6e 74 20 66 int srcid, int f
1d70: 6f 72 63 65 29 7b 0a 20 20 69 6e 74 20 73 3b 0a orce){. int s;.
1d80: 20 20 42 6c 6f 62 20 64 61 74 61 2c 20 73 72 63 Blob data, src
1d90: 2c 20 64 65 6c 74 61 3b 0a 20 20 53 74 6d 74 20 , delta;. Stmt
1da0: 73 31 2c 20 73 32 3b 0a 20 20 69 66 28 20 73 72 s1, s2;. if( sr
1db0: 63 69 64 3d 3d 72 69 64 20 29 20 72 65 74 75 72 cid==rid ) retur
1dc0: 6e 3b 0a 20 20 69 66 28 20 21 66 6f 72 63 65 20 n;. if( !force
1dd0: 26 26 20 66 69 6e 64 53 72 63 69 64 28 72 69 64 && findSrcid(rid
1de0: 2c 20 30 29 3e 30 20 29 20 72 65 74 75 72 6e 3b , 0)>0 ) return;
1df0: 0a 20 20 73 20 3d 20 73 72 63 69 64 3b 0a 20 20 . s = srcid;.
1e00: 77 68 69 6c 65 28 20 28 73 20 3d 20 66 69 6e 64 while( (s = find
1e10: 53 72 63 69 64 28 73 2c 20 30 29 29 3e 30 20 29 Srcid(s, 0))>0 )
1e20: 7b 0a 20 20 20 20 69 66 28 20 73 3d 3d 72 69 64 {. if( s==rid
1e30: 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 74 65 6e ){. conten
1e40: 74 5f 75 6e 64 65 6c 74 61 28 73 72 63 69 64 29 t_undelta(srcid)
1e50: 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
1e60: 20 20 20 7d 0a 20 20 7d 0a 20 20 63 6f 6e 74 65 }. }. conte
1e70: 6e 74 5f 67 65 74 28 73 72 63 69 64 2c 20 26 73 nt_get(srcid, &s
1e80: 72 63 29 3b 0a 20 20 63 6f 6e 74 65 6e 74 5f 67 rc);. content_g
1e90: 65 74 28 72 69 64 2c 20 26 64 61 74 61 29 3b 0a et(rid, &data);.
1ea0: 20 20 62 6c 6f 62 5f 64 65 6c 74 61 5f 63 72 65 blob_delta_cre
1eb0: 61 74 65 28 26 73 72 63 2c 20 26 64 61 74 61 2c ate(&src, &data,
1ec0: 20 26 64 65 6c 74 61 29 3b 0a 20 20 69 66 28 20 &delta);. if(
1ed0: 62 6c 6f 62 5f 73 69 7a 65 28 26 73 72 63 29 3e blob_size(&src)>
1ee0: 3d 35 30 20 26 26 20 62 6c 6f 62 5f 73 69 7a 65 =50 && blob_size
1ef0: 28 26 64 61 74 61 29 3e 3d 35 30 20 26 26 0a 20 (&data)>=50 &&.
1f00: 20 20 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 73 blob_s
1f10: 69 7a 65 28 26 64 65 6c 74 61 29 20 3c 20 62 6c ize(&delta) < bl
1f20: 6f 62 5f 73 69 7a 65 28 26 64 61 74 61 29 2a 30 ob_size(&data)*0
1f30: 2e 37 35 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f .75 ){. blob_
1f40: 63 6f 6d 70 72 65 73 73 28 26 64 65 6c 74 61 2c compress(&delta,
1f50: 20 26 64 65 6c 74 61 29 3b 0a 20 20 20 20 64 62 &delta);. db
1f60: 5f 70 72 65 70 61 72 65 28 26 73 31 2c 20 22 55 _prepare(&s1, "U
1f70: 50 44 41 54 45 20 62 6c 6f 62 20 53 45 54 20 63 PDATE blob SET c
1f80: 6f 6e 74 65 6e 74 3d 3a 64 61 74 61 20 57 48 45 ontent=:data WHE
1f90: 52 45 20 72 69 64 3d 25 64 22 2c 20 72 69 64 29 RE rid=%d", rid)
1fa0: 3b 0a 20 20 20 20 64 62 5f 70 72 65 70 61 72 65 ;. db_prepare
1fb0: 28 26 73 32 2c 20 22 52 45 50 4c 41 43 45 20 49 (&s2, "REPLACE I
1fc0: 4e 54 4f 20 64 65 6c 74 61 28 72 69 64 2c 73 72 NTO delta(rid,sr
1fd0: 63 69 64 29 56 41 4c 55 45 53 28 25 64 2c 3a 73 cid)VALUES(%d,:s
1fe0: 69 64 29 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 id)", rid);.
1ff0: 64 62 5f 62 69 6e 64 5f 62 6c 6f 62 28 26 73 31 db_bind_blob(&s1
2000: 2c 20 22 3a 64 61 74 61 22 2c 20 26 64 65 6c 74 , ":data", &delt
2010: 61 29 3b 0a 20 20 20 20 64 62 5f 62 69 6e 64 5f a);. db_bind_
2020: 69 6e 74 28 26 73 32 2c 20 22 3a 73 69 64 22 2c int(&s2, ":sid",
2030: 20 73 72 63 69 64 29 3b 0a 20 20 20 20 64 62 5f srcid);. db_
2040: 62 65 67 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f begin_transactio
2050: 6e 28 29 3b 0a 20 20 20 20 64 62 5f 65 78 65 63 n();. db_exec
2060: 28 26 73 31 29 3b 0a 20 20 20 20 64 62 5f 65 78 (&s1);. db_ex
2070: 65 63 28 26 73 32 29 3b 0a 20 20 20 20 64 62 5f ec(&s2);. db_
2080: 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 end_transaction(
2090: 30 29 3b 0a 20 20 20 20 64 62 5f 66 69 6e 61 6c 0);. db_final
20a0: 69 7a 65 28 26 73 31 29 3b 0a 20 20 20 20 64 62 ize(&s1);. db
20b0: 5f 66 69 6e 61 6c 69 7a 65 28 26 73 32 29 3b 0a _finalize(&s2);.
20c0: 20 20 7d 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 }. blob_reset
20d0: 28 26 73 72 63 29 3b 0a 20 20 62 6c 6f 62 5f 72 (&src);. blob_r
20e0: 65 73 65 74 28 26 64 61 74 61 29 3b 0a 20 20 62 eset(&data);. b
20f0: 6c 6f 62 5f 72 65 73 65 74 28 26 64 65 6c 74 61 lob_reset(&delta
2100: 29 3b 0a 20 20 76 65 72 69 66 79 5f 62 65 66 6f );. verify_befo
2110: 72 65 5f 63 6f 6d 6d 69 74 28 72 69 64 29 3b 0a re_commit(rid);.
2120: 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 }../*.** COMMAND
2130: 3a 20 20 74 65 73 74 2d 63 6f 6e 74 65 6e 74 2d : test-content-
2140: 64 65 6c 74 69 66 79 0a 2a 2a 0a 2a 2a 20 43 6f deltify.**.** Co
2150: 6e 76 65 72 74 20 74 68 65 20 63 6f 6e 74 65 6e nvert the conten
2160: 74 20 61 74 20 52 49 44 20 69 6e 74 6f 20 61 20 t at RID into a
2170: 64 65 6c 74 61 20 66 72 6f 6d 20 53 52 43 49 44 delta from SRCID
2180: 2e 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 74 5f 63 ..*/.void test_c
2190: 6f 6e 74 65 6e 74 5f 64 65 6c 74 69 66 79 5f 63 ontent_deltify_c
21a0: 6d 64 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 md(void){. if(
21b0: 67 2e 61 72 67 63 21 3d 35 20 29 20 75 73 61 67 g.argc!=5 ) usag
21c0: 65 28 22 52 49 44 20 53 52 43 49 44 20 46 4f 52 e("RID SRCID FOR
21d0: 43 45 22 29 3b 0a 20 20 64 62 5f 6d 75 73 74 5f CE");. db_must_
21e0: 62 65 5f 77 69 74 68 69 6e 5f 74 72 65 65 28 29 be_within_tree()
21f0: 3b 0a 20 20 63 6f 6e 74 65 6e 74 5f 64 65 6c 74 ;. content_delt
2200: 69 66 79 28 61 74 6f 69 28 67 2e 61 72 67 76 5b ify(atoi(g.argv[
2210: 32 5d 29 2c 20 61 74 6f 69 28 67 2e 61 72 67 76 2]), atoi(g.argv
2220: 5b 33 5d 29 2c 20 61 74 6f 69 28 67 2e 61 72 67 [3]), atoi(g.arg
2230: 76 5b 34 5d 29 29 3b 0a 7d 0a v[4]));.}.