Hex Artifact Content
Not logged in

Artifact d3786bb3e7439ec673294c954e1394b5d7620f64:

File src/content.c part of check-in [dbda8d6ce9] - Initial check-in of m1 sources. by drh on 2007-07-21 14:10:57.

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 62 6c 6f 62 5f 73 74 72 28 26 68 61 73  e, blob_str(&has
1550: 68 29 0a 20 20 20 20 29 3b 0a 20 20 20 20 62 6c  h).    );.    bl
1560: 6f 62 5f 63 6f 6d 70 72 65 73 73 28 70 42 6c 6f  ob_compress(pBlo
1570: 62 2c 20 26 63 6d 70 72 29 3b 0a 20 20 20 20 64  b, &cmpr);.    d
1580: 62 5f 62 69 6e 64 5f 62 6c 6f 62 28 26 73 31 2c  b_bind_blob(&s1,
1590: 20 22 3a 64 61 74 61 22 2c 20 26 63 6d 70 72 29   ":data", &cmpr)
15a0: 3b 0a 20 20 20 20 64 62 5f 65 78 65 63 28 26 73  ;.    db_exec(&s
15b0: 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  1);.  }else{.   
15c0: 20 2f 2a 20 57 65 20 61 72 65 20 63 72 65 61 74   /* We are creat
15d0: 69 6e 67 20 61 20 6e 65 77 20 65 6e 74 72 79 20  ing a new entry 
15e0: 2a 2f 0a 20 20 20 20 64 62 5f 70 72 65 70 61 72  */.    db_prepar
15f0: 65 28 26 73 31 2c 0a 20 20 20 20 20 20 22 49 4e  e(&s1,.      "IN
1600: 53 45 52 54 20 49 4e 54 4f 20 62 6c 6f 62 28 72  SERT INTO blob(r
1610: 63 76 69 64 2c 73 69 7a 65 2c 75 75 69 64 2c 63  cvid,size,uuid,c
1620: 6f 6e 74 65 6e 74 29 22 0a 20 20 20 20 20 20 22  ontent)".      "
1630: 56 41 4c 55 45 53 28 25 64 2c 25 64 2c 27 25 73  VALUES(%d,%d,'%s
1640: 27 2c 3a 64 61 74 61 29 22 2c 0a 20 20 20 20 20  ',:data)",.     
1650: 20 20 67 2e 72 63 76 69 64 2c 20 73 69 7a 65 2c    g.rcvid, size,
1660: 20 62 6c 6f 62 5f 73 74 72 28 26 68 61 73 68 29   blob_str(&hash)
1670: 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20  .    );.    if( 
1680: 70 42 6c 6f 62 20 29 7b 0a 20 20 20 20 20 20 62  pBlob ){.      b
1690: 6c 6f 62 5f 63 6f 6d 70 72 65 73 73 28 70 42 6c  lob_compress(pBl
16a0: 6f 62 2c 20 26 63 6d 70 72 29 3b 0a 20 20 20 20  ob, &cmpr);.    
16b0: 20 20 64 62 5f 62 69 6e 64 5f 62 6c 6f 62 28 26    db_bind_blob(&
16c0: 73 31 2c 20 22 3a 64 61 74 61 22 2c 20 26 63 6d  s1, ":data", &cm
16d0: 70 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  pr);.    }.    d
16e0: 62 5f 65 78 65 63 28 26 73 31 29 3b 0a 20 20 20  b_exec(&s1);.   
16f0: 20 72 69 64 20 3d 20 64 62 5f 6c 61 73 74 5f 69   rid = db_last_i
1700: 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 3b 0a 20  nsert_rowid();. 
1710: 20 7d 0a 0a 0a 20 20 2f 2a 20 46 69 6e 69 73 68   }...  /* Finish
1720: 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
1730: 20 61 6e 64 20 63 6c 65 61 6e 75 70 20 2a 2f 0a   and cleanup */.
1740: 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73    db_finalize(&s
1750: 31 29 3b 0a 20 20 64 62 5f 65 6e 64 5f 74 72 61  1);.  db_end_tra
1760: 6e 73 61 63 74 69 6f 6e 28 30 29 3b 0a 20 20 62  nsaction(0);.  b
1770: 6c 6f 62 5f 72 65 73 65 74 28 26 68 61 73 68 29  lob_reset(&hash)
1780: 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 61 72 72  ;..  /* Make arr
1790: 61 6e 67 65 6d 65 6e 74 73 20 74 6f 20 76 65 72  angements to ver
17a0: 69 66 79 20 74 68 61 74 20 74 68 65 20 64 61 74  ify that the dat
17b0: 61 20 63 61 6e 20 62 65 20 72 65 63 6f 76 65 72  a can be recover
17c0: 65 64 0a 20 20 2a 2a 20 62 65 66 6f 72 65 20 77  ed.  ** before w
17d0: 65 20 63 6f 6d 6d 69 74 20 2a 2f 0a 20 20 69 66  e commit */.  if
17e0: 28 20 70 42 6c 6f 62 20 29 7b 0a 20 20 20 20 62  ( pBlob ){.    b
17f0: 6c 6f 62 5f 72 65 73 65 74 28 26 63 6d 70 72 29  lob_reset(&cmpr)
1800: 3b 0a 20 20 20 20 76 65 72 69 66 79 5f 62 65 66  ;.    verify_bef
1810: 6f 72 65 5f 63 6f 6d 6d 69 74 28 72 69 64 29 3b  ore_commit(rid);
1820: 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 69  .  }.  return ri
1830: 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d  d;.}../*.** COMM
1840: 41 4e 44 3a 20 20 74 65 73 74 2d 63 6f 6e 74 65  AND:  test-conte
1850: 6e 74 2d 70 75 74 0a 2a 2a 0a 2a 2a 20 45 78 74  nt-put.**.** Ext
1860: 72 61 63 74 20 61 20 62 6c 6f 62 20 66 72 6f 6d  ract a blob from
1870: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e   the database an
1880: 64 20 77 72 69 74 65 20 69 74 20 69 6e 74 6f 20  d write it into 
1890: 61 20 66 69 6c 65 2e 0a 2a 2f 0a 76 6f 69 64 20  a file..*/.void 
18a0: 74 65 73 74 5f 63 6f 6e 74 65 6e 74 5f 70 75 74  test_content_put
18b0: 5f 63 6d 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e  _cmd(void){.  in
18c0: 74 20 72 69 64 3b 0a 20 20 42 6c 6f 62 20 63 6f  t rid;.  Blob co
18d0: 6e 74 65 6e 74 3b 0a 20 20 69 66 28 20 67 2e 61  ntent;.  if( g.a
18e0: 72 67 63 21 3d 33 20 29 20 75 73 61 67 65 28 22  rgc!=3 ) usage("
18f0: 46 49 4c 45 4e 41 4d 45 22 29 3b 0a 20 20 64 62  FILENAME");.  db
1900: 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f  _must_be_within_
1910: 74 72 65 65 28 29 3b 0a 20 20 75 73 65 72 5f 73  tree();.  user_s
1920: 65 6c 65 63 74 28 29 3b 0a 20 20 62 6c 6f 62 5f  elect();.  blob_
1930: 72 65 61 64 5f 66 72 6f 6d 5f 66 69 6c 65 28 26  read_from_file(&
1940: 63 6f 6e 74 65 6e 74 2c 20 67 2e 61 72 67 76 5b  content, g.argv[
1950: 32 5d 29 3b 0a 20 20 72 69 64 20 3d 20 63 6f 6e  2]);.  rid = con
1960: 74 65 6e 74 5f 70 75 74 28 26 63 6f 6e 74 65 6e  tent_put(&conten
1970: 74 2c 20 30 29 3b 0a 20 20 70 72 69 6e 74 66 28  t, 0);.  printf(
1980: 22 69 6e 73 65 72 74 65 64 20 61 73 20 72 65 63  "inserted as rec
1990: 6f 72 64 20 25 64 5c 6e 22 2c 20 72 69 64 29 3b  ord %d\n", rid);
19a0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73  .}../*.** Make s
19b0: 75 72 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  ure the content 
19c0: 61 74 20 72 69 64 20 69 73 20 74 68 65 20 6f 72  at rid is the or
19d0: 69 67 69 6e 61 6c 20 63 6f 6e 74 65 6e 74 20 61  iginal content a
19e0: 6e 64 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 64  nd is not a.** d
19f0: 65 6c 74 61 2e 0a 2a 2f 0a 76 6f 69 64 20 63 6f  elta..*/.void co
1a00: 6e 74 65 6e 74 5f 75 6e 64 65 6c 74 61 28 69 6e  ntent_undelta(in
1a10: 74 20 72 69 64 29 7b 0a 20 20 69 66 28 20 66 69  t rid){.  if( fi
1a20: 6e 64 53 72 63 69 64 28 72 69 64 2c 20 30 29 3e  ndSrcid(rid, 0)>
1a30: 30 20 29 7b 0a 20 20 20 20 42 6c 6f 62 20 78 3b  0 ){.    Blob x;
1a40: 0a 20 20 20 20 53 74 6d 74 20 73 3b 0a 20 20 20  .    Stmt s;.   
1a50: 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28 72 69 64   content_get(rid
1a60: 2c 20 26 78 29 3b 0a 20 20 20 20 64 62 5f 70 72  , &x);.    db_pr
1a70: 65 70 61 72 65 28 26 73 2c 20 22 55 50 44 41 54  epare(&s, "UPDAT
1a80: 45 20 62 6c 6f 62 20 53 45 54 20 63 6f 6e 74 65  E blob SET conte
1a90: 6e 74 3d 3a 63 20 57 48 45 52 45 20 72 69 64 3d  nt=:c WHERE rid=
1aa0: 25 64 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 64  %d", rid);.    d
1ab0: 62 5f 62 69 6e 64 5f 62 6c 6f 62 28 26 73 2c 20  b_bind_blob(&s, 
1ac0: 22 3a 63 22 2c 20 26 78 29 3b 0a 20 20 20 20 64  ":c", &x);.    d
1ad0: 62 5f 65 78 65 63 28 26 73 29 3b 0a 20 20 20 20  b_exec(&s);.    
1ae0: 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 73 29 3b  db_finalize(&s);
1af0: 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78  .    db_multi_ex
1b00: 65 63 28 22 44 45 4c 45 54 45 20 46 52 4f 4d 20  ec("DELETE FROM 
1b10: 64 65 6c 74 61 20 57 48 45 52 45 20 72 69 64 3d  delta WHERE rid=
1b20: 25 64 22 2c 20 72 69 64 29 3b 0a 20 20 7d 0a 7d  %d", rid);.  }.}
1b30: 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a  ../*.** COMMAND:
1b40: 20 20 74 65 73 74 2d 63 6f 6e 74 65 6e 74 2d 75    test-content-u
1b50: 6e 64 65 6c 74 61 0a 2a 2a 0a 2a 2a 20 4d 61 6b  ndelta.**.** Mak
1b60: 65 20 73 75 72 65 20 74 68 65 20 63 6f 6e 74 65  e sure the conte
1b70: 6e 74 20 61 74 20 52 45 43 4f 52 44 49 44 20 69  nt at RECORDID i
1b80: 73 20 6e 6f 74 20 61 20 64 65 6c 74 61 0a 2a 2f  s not a delta.*/
1b90: 0a 76 6f 69 64 20 74 65 73 74 5f 63 6f 6e 74 65  .void test_conte
1ba0: 6e 74 5f 75 6e 64 65 6c 74 61 5f 63 6d 64 28 76  nt_undelta_cmd(v
1bb0: 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72 69 64 3b  oid){.  int rid;
1bc0: 0a 20 20 69 66 28 20 67 2e 61 72 67 63 21 3d 32  .  if( g.argc!=2
1bd0: 20 29 20 75 73 61 67 65 28 22 52 45 43 4f 52 44   ) usage("RECORD
1be0: 49 44 22 29 3b 0a 20 20 64 62 5f 6d 75 73 74 5f  ID");.  db_must_
1bf0: 62 65 5f 77 69 74 68 69 6e 5f 74 72 65 65 28 29  be_within_tree()
1c00: 3b 0a 20 20 72 69 64 20 3d 20 61 74 6f 69 28 67  ;.  rid = atoi(g
1c10: 2e 61 72 67 76 5b 32 5d 29 3b 0a 20 20 63 6f 6e  .argv[2]);.  con
1c20: 74 65 6e 74 5f 75 6e 64 65 6c 74 61 28 72 69 64  tent_undelta(rid
1c30: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e  );.}../*.** Chan
1c40: 67 65 20 74 68 65 20 73 74 6f 72 61 67 65 20 6f  ge the storage o
1c50: 66 20 72 69 64 20 73 6f 20 74 68 61 74 20 69 74  f rid so that it
1c60: 20 69 73 20 61 20 64 65 6c 74 61 20 6f 66 20 73   is a delta of s
1c70: 72 63 69 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72  rcid..**.** If r
1c80: 69 64 20 69 73 20 61 6c 72 65 61 64 79 20 61 20  id is already a 
1c90: 64 65 6c 74 61 20 66 72 6f 6d 20 73 6f 6d 65 20  delta from some 
1ca0: 6f 74 68 65 72 20 70 6c 61 63 65 20 74 68 65 6e  other place then
1cb0: 20 6e 6f 0a 2a 2a 20 63 6f 6e 76 65 72 73 69 6f   no.** conversio
1cc0: 6e 20 6f 63 63 75 72 73 20 61 6e 64 20 74 68 69  n occurs and thi
1cd0: 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 75 6e 6c  s is a no-op unl
1ce0: 65 73 73 20 66 6f 72 63 65 3d 3d 31 2e 0a 2a 2a  ess force==1..**
1cf0: 0a 2a 2a 20 49 66 20 73 72 63 69 64 20 69 73 20  .** If srcid is 
1d00: 61 20 64 65 6c 74 61 20 74 68 61 74 20 64 65 70  a delta that dep
1d10: 65 6e 64 73 20 6f 6e 20 72 69 64 2c 20 74 68 65  ends on rid, the
1d20: 6e 20 73 72 63 69 64 20 69 73 0a 2a 2a 20 63 6f  n srcid is.** co
1d30: 6e 76 65 72 74 65 64 20 74 6f 20 75 6e 64 65 6c  nverted to undel
1d40: 74 61 65 64 20 74 65 78 74 2e 0a 2a 2f 0a 76 6f  taed text..*/.vo
1d50: 69 64 20 63 6f 6e 74 65 6e 74 5f 64 65 6c 74 69  id content_delti
1d60: 66 79 28 69 6e 74 20 72 69 64 2c 20 69 6e 74 20  fy(int rid, int 
1d70: 73 72 63 69 64 2c 20 69 6e 74 20 66 6f 72 63 65  srcid, int force
1d80: 29 7b 0a 20 20 69 6e 74 20 73 3b 0a 20 20 42 6c  ){.  int s;.  Bl
1d90: 6f 62 20 64 61 74 61 2c 20 73 72 63 2c 20 64 65  ob data, src, de
1da0: 6c 74 61 3b 0a 20 20 73 74 61 74 69 63 20 53 74  lta;.  static St
1db0: 6d 74 20 73 31 2c 20 73 32 3b 0a 20 20 69 66 28  mt s1, s2;.  if(
1dc0: 20 73 72 63 69 64 3d 3d 72 69 64 20 29 20 72 65   srcid==rid ) re
1dd0: 74 75 72 6e 3b 0a 20 20 69 66 28 20 21 66 6f 72  turn;.  if( !for
1de0: 63 65 20 26 26 20 66 69 6e 64 53 72 63 69 64 28  ce && findSrcid(
1df0: 72 69 64 2c 20 30 29 3e 30 20 29 20 72 65 74 75  rid, 0)>0 ) retu
1e00: 72 6e 3b 0a 20 20 73 20 3d 20 73 72 63 69 64 3b  rn;.  s = srcid;
1e10: 0a 20 20 77 68 69 6c 65 28 20 28 73 20 3d 20 66  .  while( (s = f
1e20: 69 6e 64 53 72 63 69 64 28 73 2c 20 30 29 29 3e  indSrcid(s, 0))>
1e30: 30 20 29 7b 0a 20 20 20 20 69 66 28 20 73 3d 3d  0 ){.    if( s==
1e40: 72 69 64 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e  rid ){.      con
1e50: 74 65 6e 74 5f 75 6e 64 65 6c 74 61 28 73 72 63  tent_undelta(src
1e60: 69 64 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  id);.      break
1e70: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 63 6f  ;.    }.  }.  co
1e80: 6e 74 65 6e 74 5f 67 65 74 28 73 72 63 69 64 2c  ntent_get(srcid,
1e90: 20 26 73 72 63 29 3b 0a 20 20 63 6f 6e 74 65 6e   &src);.  conten
1ea0: 74 5f 67 65 74 28 72 69 64 2c 20 26 64 61 74 61  t_get(rid, &data
1eb0: 29 3b 0a 20 20 62 6c 6f 62 5f 64 65 6c 74 61 5f  );.  blob_delta_
1ec0: 63 72 65 61 74 65 28 26 73 72 63 2c 20 26 64 61  create(&src, &da
1ed0: 74 61 2c 20 26 64 65 6c 74 61 29 3b 0a 20 20 69  ta, &delta);.  i
1ee0: 66 28 20 62 6c 6f 62 5f 73 69 7a 65 28 26 73 72  f( blob_size(&sr
1ef0: 63 29 3e 3d 35 30 20 26 26 20 62 6c 6f 62 5f 73  c)>=50 && blob_s
1f00: 69 7a 65 28 26 64 61 74 61 29 3e 3d 35 30 20 26  ize(&data)>=50 &
1f10: 26 0a 20 20 20 20 20 20 20 20 20 20 20 62 6c 6f  &.           blo
1f20: 62 5f 73 69 7a 65 28 26 64 65 6c 74 61 29 20 3c  b_size(&delta) <
1f30: 20 62 6c 6f 62 5f 73 69 7a 65 28 26 64 61 74 61   blob_size(&data
1f40: 29 2a 30 2e 37 35 20 29 7b 0a 20 20 20 20 62 6c  )*0.75 ){.    bl
1f50: 6f 62 5f 63 6f 6d 70 72 65 73 73 28 26 64 65 6c  ob_compress(&del
1f60: 74 61 2c 20 26 64 65 6c 74 61 29 3b 0a 20 20 20  ta, &delta);.   
1f70: 20 64 62 5f 73 74 61 74 69 63 5f 70 72 65 70 61   db_static_prepa
1f80: 72 65 28 26 73 31 2c 20 22 55 50 44 41 54 45 20  re(&s1, "UPDATE 
1f90: 62 6c 6f 62 20 53 45 54 20 63 6f 6e 74 65 6e 74  blob SET content
1fa0: 3d 3a 64 61 74 61 20 57 48 45 52 45 20 72 69 64  =:data WHERE rid
1fb0: 3d 3a 72 69 64 22 29 3b 0a 20 20 20 20 64 62 5f  =:rid");.    db_
1fc0: 73 74 61 74 69 63 5f 70 72 65 70 61 72 65 28 26  static_prepare(&
1fd0: 73 32 2c 20 22 52 45 50 4c 41 43 45 20 49 4e 54  s2, "REPLACE INT
1fe0: 4f 20 64 65 6c 74 61 28 72 69 64 2c 73 72 63 69  O delta(rid,srci
1ff0: 64 29 56 41 4c 55 45 53 28 3a 72 69 64 2c 3a 73  d)VALUES(:rid,:s
2000: 69 64 29 22 29 3b 0a 20 20 20 20 64 62 5f 62 69  id)");.    db_bi
2010: 6e 64 5f 69 6e 74 28 26 73 31 2c 20 22 3a 72 69  nd_int(&s1, ":ri
2020: 64 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 64 62  d", rid);.    db
2030: 5f 62 69 6e 64 5f 62 6c 6f 62 28 26 73 31 2c 20  _bind_blob(&s1, 
2040: 22 3a 64 61 74 61 22 2c 20 26 64 65 6c 74 61 29  ":data", &delta)
2050: 3b 0a 20 20 20 20 64 62 5f 62 69 6e 64 5f 69 6e  ;.    db_bind_in
2060: 74 28 26 73 32 2c 20 22 3a 72 69 64 22 2c 20 72  t(&s2, ":rid", r
2070: 69 64 29 3b 0a 20 20 20 20 64 62 5f 62 69 6e 64  id);.    db_bind
2080: 5f 69 6e 74 28 26 73 32 2c 20 22 3a 73 69 64 22  _int(&s2, ":sid"
2090: 2c 20 73 72 63 69 64 29 3b 0a 20 20 20 20 64 62  , srcid);.    db
20a0: 5f 62 65 67 69 6e 5f 74 72 61 6e 73 61 63 74 69  _begin_transacti
20b0: 6f 6e 28 29 3b 0a 20 20 20 20 64 62 5f 65 78 65  on();.    db_exe
20c0: 63 28 26 73 31 29 3b 0a 20 20 20 20 64 62 5f 65  c(&s1);.    db_e
20d0: 78 65 63 28 26 73 32 29 3b 0a 20 20 20 20 64 62  xec(&s2);.    db
20e0: 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e  _end_transaction
20f0: 28 30 29 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f  (0);.  }.  blob_
2100: 72 65 73 65 74 28 26 73 72 63 29 3b 0a 20 20 62  reset(&src);.  b
2110: 6c 6f 62 5f 72 65 73 65 74 28 26 64 61 74 61 29  lob_reset(&data)
2120: 3b 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26  ;.  blob_reset(&
2130: 64 65 6c 74 61 29 3b 0a 20 20 76 65 72 69 66 79  delta);.  verify
2140: 5f 62 65 66 6f 72 65 5f 63 6f 6d 6d 69 74 28 72  _before_commit(r
2150: 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f  id);.}../*.** CO
2160: 4d 4d 41 4e 44 3a 20 20 74 65 73 74 2d 63 6f 6e  MMAND:  test-con
2170: 74 65 6e 74 2d 64 65 6c 74 69 66 79 0a 2a 2a 0a  tent-deltify.**.
2180: 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 63  ** Convert the c
2190: 6f 6e 74 65 6e 74 20 61 74 20 52 49 44 20 69 6e  ontent at RID in
21a0: 74 6f 20 61 20 64 65 6c 74 61 20 66 72 6f 6d 20  to a delta from 
21b0: 53 52 43 49 44 2e 0a 2a 2f 0a 76 6f 69 64 20 74  SRCID..*/.void t
21c0: 65 73 74 5f 63 6f 6e 74 65 6e 74 5f 64 65 6c 74  est_content_delt
21d0: 69 66 79 5f 63 6d 64 28 76 6f 69 64 29 7b 0a 20  ify_cmd(void){. 
21e0: 20 69 66 28 20 67 2e 61 72 67 63 21 3d 35 20 29   if( g.argc!=5 )
21f0: 20 75 73 61 67 65 28 22 52 49 44 20 53 52 43 49   usage("RID SRCI
2200: 44 20 46 4f 52 43 45 22 29 3b 0a 20 20 64 62 5f  D FORCE");.  db_
2210: 6d 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74  must_be_within_t
2220: 72 65 65 28 29 3b 0a 20 20 63 6f 6e 74 65 6e 74  ree();.  content
2230: 5f 64 65 6c 74 69 66 79 28 61 74 6f 69 28 67 2e  _deltify(atoi(g.
2240: 61 72 67 76 5b 32 5d 29 2c 20 61 74 6f 69 28 67  argv[2]), atoi(g
2250: 2e 61 72 67 76 5b 33 5d 29 2c 20 61 74 6f 69 28  .argv[3]), atoi(
2260: 67 2e 61 72 67 76 5b 34 5d 29 29 3b 0a 7d 0a     g.argv[4]));.}.