Hex Artifact Content
Not logged in

Artifact f52a353f944e8b874275ae44bf7f4ae8da318038:

File src/verify.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 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 75   contains code u
0380: 73 65 64 20 74 6f 20 68 65 6c 70 20 76 65 72 69  sed to help veri
0390: 66 79 20 74 68 65 20 69 6e 74 65 67 72 69 74 79  fy the integrity
03a0: 20 6f 66 20 74 68 65 0a 2a 2a 20 74 68 65 20 72   of the.** the r
03b0: 65 70 6f 73 69 74 6f 72 79 0a 2a 2f 0a 23 69 6e  epository.*/.#in
03c0: 63 6c 75 64 65 20 22 63 6f 6e 66 69 67 2e 68 22  clude "config.h"
03d0: 0a 23 69 6e 63 6c 75 64 65 20 22 76 65 72 69 66  .#include "verif
03e0: 79 2e 68 22 0a 23 69 6e 63 6c 75 64 65 20 3c 61  y.h".#include <a
03f0: 73 73 65 72 74 2e 68 3e 0a 0a 2f 2a 0a 2a 2a 20  ssert.h>../*.** 
0400: 4c 6f 61 64 20 74 68 65 20 72 65 63 6f 72 64 20  Load the record 
0410: 69 64 65 6e 74 69 66 79 20 62 79 20 72 69 64 2e  identify by rid.
0420: 20 20 4d 61 6b 65 20 73 75 72 65 20 77 65 20 63    Make sure we c
0430: 61 6e 20 72 65 70 72 6f 64 75 63 65 20 69 74 0a  an reproduce it.
0440: 2a 2a 20 77 69 74 68 6f 75 74 20 65 72 72 6f 72  ** without error
0450: 2e 0a 2a 2a 0a 2a 2a 20 50 61 6e 69 63 20 69 66  ..**.** Panic if
0460: 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 77   anything goes w
0470: 72 6f 6e 67 2e 20 20 49 66 20 74 68 69 73 20 70  rong.  If this p
0480: 72 6f 63 65 64 75 72 65 20 72 65 74 75 72 6e 73  rocedure returns
0490: 20 69 74 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 61   it means.** tha
04a0: 74 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 20  t everything is 
04b0: 4f 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  OK..*/.static vo
04c0: 69 64 20 76 65 72 69 66 79 5f 72 69 64 28 69 6e  id verify_rid(in
04d0: 74 20 72 69 64 29 7b 0a 20 20 42 6c 6f 62 20 75  t rid){.  Blob u
04e0: 75 69 64 2c 20 68 61 73 68 2c 20 63 6f 6e 74 65  uid, hash, conte
04f0: 6e 74 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28  nt;.  blob_zero(
0500: 26 75 75 69 64 29 3b 0a 20 20 64 62 5f 62 6c 6f  &uuid);.  db_blo
0510: 62 28 26 75 75 69 64 2c 20 22 53 45 4c 45 43 54  b(&uuid, "SELECT
0520: 20 75 75 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20   uuid FROM blob 
0530: 57 48 45 52 45 20 72 69 64 3d 25 64 22 2c 20 72  WHERE rid=%d", r
0540: 69 64 29 3b 0a 20 20 69 66 28 20 62 6c 6f 62 5f  id);.  if( blob_
0550: 73 69 7a 65 28 26 75 75 69 64 29 21 3d 55 55 49  size(&uuid)!=UUI
0560: 44 5f 53 49 5a 45 20 29 7b 0a 20 20 20 20 66 6f  D_SIZE ){.    fo
0570: 73 73 69 6c 5f 70 61 6e 69 63 28 22 6e 6f 74 20  ssil_panic("not 
0580: 61 20 76 61 6c 69 64 20 72 69 64 3a 20 25 64 22  a valid rid: %d"
0590: 2c 20 72 69 64 29 3b 0a 20 20 7d 0a 20 20 63 6f  , rid);.  }.  co
05a0: 6e 74 65 6e 74 5f 67 65 74 28 72 69 64 2c 20 26  ntent_get(rid, &
05b0: 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 73 68 61 31  content);.  sha1
05c0: 73 75 6d 5f 62 6c 6f 62 28 26 63 6f 6e 74 65 6e  sum_blob(&conten
05d0: 74 2c 20 26 68 61 73 68 29 3b 0a 20 20 62 6c 6f  t, &hash);.  blo
05e0: 62 5f 72 65 73 65 74 28 26 63 6f 6e 74 65 6e 74  b_reset(&content
05f0: 29 3b 0a 20 20 69 66 28 20 62 6c 6f 62 5f 63 6f  );.  if( blob_co
0600: 6d 70 61 72 65 28 26 75 75 69 64 2c 20 26 68 61  mpare(&uuid, &ha
0610: 73 68 29 20 29 7b 0a 20 20 20 20 66 6f 73 73 69  sh) ){.    fossi
0620: 6c 5f 70 61 6e 69 63 28 22 68 61 73 68 20 6f 66  l_panic("hash of
0630: 20 72 69 64 20 25 64 20 64 6f 65 73 20 6e 6f 74   rid %d does not
0640: 20 6d 61 74 63 68 20 69 74 73 20 75 75 69 64 22   match its uuid"
0650: 2c 20 72 69 64 29 3b 0a 20 20 7d 0a 20 20 62 6c  , rid);.  }.  bl
0660: 6f 62 5f 72 65 73 65 74 28 26 75 75 69 64 29 3b  ob_reset(&uuid);
0670: 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 68  .  blob_reset(&h
0680: 61 73 68 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20  ash);.}../*.**  
0690: 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76  .*/.static int v
06a0: 65 72 69 66 79 5f 61 74 5f 63 6f 6d 6d 69 74 28  erify_at_commit(
06b0: 76 6f 69 64 20 2a 6e 6f 74 55 73 65 64 29 7b 0a  void *notUsed){.
06c0: 20 20 53 74 6d 74 20 71 3b 0a 20 20 64 62 5f 70    Stmt q;.  db_p
06d0: 72 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c 45  repare(&q, "SELE
06e0: 43 54 20 72 69 64 20 46 52 4f 4d 20 74 6f 76 65  CT rid FROM tove
06f0: 72 69 66 79 22 29 3b 0a 20 20 77 68 69 6c 65 28  rify");.  while(
0700: 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53 51   db_step(&q)==SQ
0710: 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20  LITE_ROW ){.    
0720: 69 6e 74 20 72 69 64 20 3d 20 64 62 5f 63 6f 6c  int rid = db_col
0730: 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 30 29 3b 0a  umn_int(&q, 0);.
0740: 20 20 20 20 76 65 72 69 66 79 5f 72 69 64 28 72      verify_rid(r
0750: 69 64 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 66 69  id);.  }.  db_fi
0760: 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 20 20 64 62  nalize(&q);.  db
0770: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22 44 45 4c  _multi_exec("DEL
0780: 45 54 45 20 46 52 4f 4d 20 74 6f 76 65 72 69 66  ETE FROM toverif
0790: 79 22 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b  y");.  return 0;
07a0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 72 72 61 6e 67  .}../*.** Arrang
07b0: 65 20 74 6f 20 76 65 72 69 66 79 20 61 20 70 61  e to verify a pa
07c0: 72 74 69 63 75 6c 61 72 20 72 65 63 6f 72 64 20  rticular record 
07d0: 70 72 69 6f 72 20 74 6f 20 63 6f 6d 6d 69 74 74  prior to committ
07e0: 69 6e 67 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74  ing..** .** If t
07f0: 68 65 20 72 65 63 6f 72 64 20 72 69 64 20 69 73  he record rid is
0800: 20 6c 65 73 73 20 74 68 61 6e 20 31 2c 20 74 68   less than 1, th
0810: 65 6e 20 6a 75 73 74 20 69 6e 69 74 69 61 6c 69  en just initiali
0820: 7a 65 20 74 68 65 0a 2a 2a 20 76 65 72 69 66 69  ze the.** verifi
0830: 63 61 74 69 6f 6e 20 73 79 73 74 65 6d 20 62 75  cation system bu
0840: 74 20 64 6f 20 6e 6f 74 20 72 65 63 6f 72 64 20  t do not record 
0850: 61 6e 79 74 68 69 6e 67 20 61 73 20 6e 65 65 64  anything as need
0860: 69 6e 67 0a 2a 2a 20 76 65 72 69 66 69 63 61 74  ing.** verificat
0870: 69 6f 6e 2e 0a 2a 2f 0a 76 6f 69 64 20 76 65 72  ion..*/.void ver
0880: 69 66 79 5f 62 65 66 6f 72 65 5f 63 6f 6d 6d 69  ify_before_commi
0890: 74 28 69 6e 74 20 72 69 64 29 7b 0a 20 20 73 74  t(int rid){.  st
08a0: 61 74 69 63 20 69 6e 74 20 69 73 49 6e 69 74 20  atic int isInit 
08b0: 3d 20 30 3b 0a 20 20 69 66 28 20 21 69 73 49 6e  = 0;.  if( !isIn
08c0: 69 74 20 29 7b 0a 20 20 20 20 64 62 5f 6d 75 6c  it ){.    db_mul
08d0: 74 69 5f 65 78 65 63 28 0a 20 20 20 20 20 20 20  ti_exec(.       
08e0: 22 43 52 45 41 54 45 20 54 45 4d 50 20 54 41 42  "CREATE TEMP TAB
08f0: 4c 45 20 74 6f 76 65 72 69 66 79 28 72 69 64 20  LE toverify(rid 
0900: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
0910: 4b 45 59 29 3b 22 0a 20 20 20 20 29 3b 0a 20 20  KEY);".    );.  
0920: 20 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74    sqlite3_commit
0930: 5f 68 6f 6f 6b 28 67 2e 64 62 2c 20 76 65 72 69  _hook(g.db, veri
0940: 66 79 5f 61 74 5f 63 6f 6d 6d 69 74 2c 20 30 29  fy_at_commit, 0)
0950: 3b 0a 20 20 20 20 69 73 49 6e 69 74 20 3d 20 31  ;.    isInit = 1
0960: 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 69 64 3e  ;.  }.  if( rid>
0970: 30 20 29 7b 0a 20 20 20 20 64 62 5f 6d 75 6c 74  0 ){.    db_mult
0980: 69 5f 65 78 65 63 28 0a 20 20 20 20 20 20 22 49  i_exec(.      "I
0990: 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20  NSERT OR IGNORE 
09a0: 49 4e 54 4f 20 74 6f 76 65 72 69 66 79 20 56 41  INTO toverify VA
09b0: 4c 55 45 53 28 25 64 29 22 2c 20 72 69 64 0a 20  LUES(%d)", rid. 
09c0: 20 20 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a     );.  }.}../*.
09d0: 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 74 65 73 74  ** COMMAND: test
09e0: 2d 76 65 72 69 66 79 2d 61 6c 6c 0a 2a 2a 0a 2a  -verify-all.**.*
09f0: 2a 20 56 65 72 69 66 79 20 61 6c 6c 20 72 65 63  * Verify all rec
0a00: 6f 72 64 73 20 69 6e 20 74 68 65 20 72 65 70 6f  ords in the repo
0a10: 73 69 74 6f 72 79 2e 0a 2a 2f 0a 76 6f 69 64 20  sitory..*/.void 
0a20: 76 65 72 69 66 79 5f 61 6c 6c 5f 63 6d 64 28 76  verify_all_cmd(v
0a30: 6f 69 64 29 7b 0a 20 20 53 74 6d 74 20 71 3b 0a  oid){.  Stmt q;.
0a40: 20 20 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74    db_must_be_wit
0a50: 68 69 6e 5f 74 72 65 65 28 29 3b 0a 20 20 64 62  hin_tree();.  db
0a60: 5f 70 72 65 70 61 72 65 28 26 71 2c 20 22 53 45  _prepare(&q, "SE
0a70: 4c 45 43 54 20 72 69 64 20 46 52 4f 4d 20 62 6c  LECT rid FROM bl
0a80: 6f 62 22 29 3b 0a 20 20 77 68 69 6c 65 28 20 64  ob");.  while( d
0a90: 62 5f 73 74 65 70 28 26 71 29 3d 3d 53 51 4c 49  b_step(&q)==SQLI
0aa0: 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 69 6e  TE_ROW ){.    in
0ab0: 74 20 72 69 64 20 3d 20 64 62 5f 63 6f 6c 75 6d  t rid = db_colum
0ac0: 6e 5f 69 6e 74 28 26 71 2c 20 30 29 3b 0a 20 20  n_int(&q, 0);.  
0ad0: 20 20 76 65 72 69 66 79 5f 72 69 64 28 72 69 64    verify_rid(rid
0ae0: 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 66 69 6e 61  );.  }.  db_fina
0af0: 6c 69 7a 65 28 26 71 29 3b 0a 7d 0a              lize(&q);.}.