Hex Artifact Content

Artifact 1d195c7296924f9091c25bba03c19fdeaf7e8b70:


0000: 23 21 20 2f 75 73 72 2f 62 69 6e 2f 65 6e 76 20  #! /usr/bin/env 
0010: 62 61 73 68 0a 0a 66 75 6e 63 74 69 6f 6e 20 69  bash..function i
0020: 6e 76 61 6c 69 64 61 74 65 28 29 20 7b 0a 09 61  nvalidate() {..a
0030: 70 70 66 73 64 20 2d 73 71 6c 69 74 65 33 20 27  ppfsd -sqlite3 '
0040: 55 50 44 41 54 45 20 73 69 74 65 73 20 53 45 54  UPDATE sites SET
0050: 20 74 74 6c 20 3d 20 22 30 22 3b 27 0a 7d 0a 0a   ttl = "0";'.}..
0060: 66 75 6e 63 74 69 6f 6e 20 72 65 6d 6f 76 65 5f  function remove_
0070: 73 69 74 65 28 29 20 7b 0a 09 6c 6f 63 61 6c 20  site() {..local 
0080: 73 69 74 65 0a 0a 09 73 69 74 65 3d 22 24 31 22  site...site="$1"
0090: 0a 0a 09 61 70 70 66 73 64 20 2d 73 71 6c 69 74  ...appfsd -sqlit
00a0: 65 33 20 27 44 45 4c 45 54 45 20 46 52 4f 4d 20  e3 'DELETE FROM 
00b0: 73 69 74 65 73 20 57 48 45 52 45 20 68 6f 73 74  sites WHERE host
00c0: 6e 61 6d 65 20 3d 20 27 22 27 24 73 69 74 65 27  name = '"'$site'
00d0: 22 27 3b 20 44 45 4c 45 54 45 20 46 52 4f 4d 20  "'; DELETE FROM 
00e0: 70 61 63 6b 61 67 65 73 20 57 48 45 52 45 20 68  packages WHERE h
00f0: 6f 73 74 6e 61 6d 65 20 3d 20 27 22 27 24 73 69  ostname = '"'$si
0100: 74 65 27 22 27 3b 27 20 7c 7c 20 72 65 74 75 72  te'"';' || retur
0110: 6e 20 31 0a 0a 09 63 6c 65 61 6e 0a 7d 0a 0a 66  n 1...clean.}..f
0120: 75 6e 63 74 69 6f 6e 20 63 6c 65 61 6e 28 29 20  unction clean() 
0130: 7b 0a 09 61 70 70 66 73 64 20 2d 74 63 6c 20 22  {..appfsd -tcl "
0140: 24 28 63 61 74 20 3c 3c 5c 5f 45 4f 46 5f 0a 09  $(cat <<\_EOF_..
0150: 09 75 6e 73 65 74 20 2d 6e 6f 63 6f 6d 70 6c 61  .unset -nocompla
0160: 69 6e 20 72 6f 77 0a 09 09 3a 3a 61 70 70 66 73  in row...::appfs
0170: 3a 3a 64 62 20 65 76 61 6c 20 7b 53 45 4c 45 43  ::db eval {SELEC
0180: 54 20 73 68 61 31 2c 20 68 6f 73 74 6e 61 6d 65  T sha1, hostname
0190: 20 46 52 4f 4d 20 70 61 63 6b 61 67 65 73 3b 7d   FROM packages;}
01a0: 20 72 6f 77 20 7b 0a 09 09 09 73 65 74 20 68 6f   row {....set ho
01b0: 73 74 6e 61 6d 65 20 5b 3a 3a 61 70 70 66 73 3a  stname [::appfs:
01c0: 3a 64 62 20 6f 6e 65 63 6f 6c 75 6d 6e 20 7b 53  :db onecolumn {S
01d0: 45 4c 45 43 54 20 68 6f 73 74 6e 61 6d 65 20 46  ELECT hostname F
01e0: 52 4f 4d 20 73 69 74 65 73 20 57 48 45 52 45 20  ROM sites WHERE 
01f0: 68 6f 73 74 6e 61 6d 65 20 3d 20 24 72 6f 77 28  hostname = $row(
0200: 68 6f 73 74 6e 61 6d 65 29 20 4c 49 4d 49 54 20  hostname) LIMIT 
0210: 31 3b 7d 5d 0a 09 09 09 69 66 20 7b 24 68 6f 73  1;}]....if {$hos
0220: 74 6e 61 6d 65 20 3d 3d 20 22 22 7d 20 7b 0a 09  tname == ""} {..
0230: 09 09 09 63 6f 6e 74 69 6e 75 65 0a 09 09 09 7d  ...continue....}
0240: 0a 0a 09 09 09 73 65 74 20 76 61 6c 69 64 5f 73  .....set valid_s
0250: 68 61 31 28 24 72 6f 77 28 73 68 61 31 29 29 20  ha1($row(sha1)) 
0260: 31 0a 09 09 09 3a 3a 61 70 70 66 73 3a 3a 64 62  1....::appfs::db
0270: 20 65 76 61 6c 20 7b 53 45 4c 45 43 54 20 66 69   eval {SELECT fi
0280: 6c 65 5f 73 68 61 31 20 46 52 4f 4d 20 66 69 6c  le_sha1 FROM fil
0290: 65 73 20 57 48 45 52 45 20 66 69 6c 65 5f 73 68  es WHERE file_sh
02a0: 61 31 20 49 53 20 4e 4f 54 20 4e 55 4c 4c 20 41  a1 IS NOT NULL A
02b0: 4e 44 20 66 69 6c 65 5f 73 68 61 31 20 21 3d 20  ND file_sha1 != 
02c0: 27 27 20 41 4e 44 20 70 61 63 6b 61 67 65 5f 73  '' AND package_s
02d0: 68 61 31 20 3d 20 24 72 6f 77 28 73 68 61 31 29  ha1 = $row(sha1)
02e0: 3b 7d 20 73 75 62 72 6f 77 20 7b 0a 09 09 09 09  ;} subrow {.....
02f0: 73 65 74 20 76 61 6c 69 64 5f 73 68 61 31 28 24  set valid_sha1($
0300: 73 75 62 72 6f 77 28 66 69 6c 65 5f 73 68 61 31  subrow(file_sha1
0310: 29 29 20 31 0a 09 09 09 7d 0a 09 09 7d 0a 0a 09  )) 1....}...}...
0320: 09 66 6f 72 65 61 63 68 20 66 69 6c 65 20 5b 67  .foreach file [g
0330: 6c 6f 62 20 2d 6e 6f 63 6f 6d 70 6c 61 69 6e 20  lob -nocomplain 
0340: 2d 74 61 69 6c 73 20 2d 64 69 72 65 63 74 6f 72  -tails -director
0350: 79 20 24 3a 3a 61 70 70 66 73 3a 3a 63 61 63 68  y $::appfs::cach
0360: 65 64 69 72 20 7b 5b 30 2d 39 61 2d 66 5d 5b 30  edir {[0-9a-f][0
0370: 2d 39 61 2d 66 5d 2f 2a 2f 2a 2f 2a 2f 2a 7d 5d  -9a-f]/*/*/*/*}]
0380: 20 7b 0a 09 09 09 73 65 74 20 73 68 61 31 20 5b   {....set sha1 [
0390: 73 74 72 69 6e 67 20 6d 61 70 20 5b 6c 69 73 74  string map [list
03a0: 20 22 2f 22 20 22 22 20 22 5c 5c 22 20 22 22 5d   "/" "" "\\" ""]
03b0: 20 24 66 69 6c 65 5d 0a 09 09 09 73 65 74 20 66   $file]....set f
03c0: 69 6c 65 20 5b 66 69 6c 65 20 6a 6f 69 6e 20 24  ile [file join $
03d0: 3a 3a 61 70 70 66 73 3a 3a 63 61 63 68 65 64 69  ::appfs::cachedi
03e0: 72 20 24 66 69 6c 65 5d 0a 0a 09 09 09 69 66 20  r $file].....if 
03f0: 7b 5b 69 6e 66 6f 20 65 78 69 73 74 73 20 76 61  {[info exists va
0400: 6c 69 64 5f 73 68 61 31 28 24 73 68 61 31 29 5d  lid_sha1($sha1)]
0410: 7d 20 7b 0a 09 09 09 09 63 6f 6e 74 69 6e 75 65  } {.....continue
0420: 0a 09 09 09 7d 0a 0a 09 09 09 70 75 74 73 20 22  ....}.....puts "
0430: 43 6c 65 61 6e 69 6e 67 20 24 66 69 6c 65 22 0a  Cleaning $file".
0440: 09 09 09 66 69 6c 65 20 64 65 6c 65 74 65 20 2d  ...file delete -
0450: 66 6f 72 63 65 20 2d 2d 20 24 66 69 6c 65 0a 09  force -- $file..
0460: 09 7d 0a 5f 45 4f 46 5f 0a 09 29 22 0a 7d 0a 0a  .}._EOF_..)".}..
0470: 66 75 6e 63 74 69 6f 6e 20 63 6c 65 61 72 28 29  function clear()
0480: 20 7b 0a 09 6c 6f 63 61 6c 20 70 61 63 6b 61 67   {..local packag
0490: 65 0a 0a 09 70 61 63 6b 61 67 65 3d 22 24 31 22  e...package="$1"
04a0: 0a 0a 09 69 66 20 5b 20 2d 6e 20 22 24 7b 70 61  ...if [ -n "${pa
04b0: 63 6b 61 67 65 7d 22 20 5d 3b 20 74 68 65 6e 0a  ckage}" ]; then.
04c0: 09 09 65 63 68 6f 20 22 6e 6f 74 20 69 6d 70 6c  ..echo "not impl
04d0: 65 6d 65 6e 74 65 64 22 20 3e 26 32 0a 0a 09 09  emented" >&2....
04e0: 72 65 74 75 72 6e 20 31 0a 09 66 69 0a 0a 09 61  return 1..fi...a
04f0: 70 70 66 73 64 20 2d 74 63 6c 20 27 66 69 6c 65  ppfsd -tcl 'file
0500: 20 64 65 6c 65 74 65 20 2d 66 6f 72 63 65 20 2d   delete -force -
0510: 2d 20 7b 2a 7d 5b 67 6c 6f 62 20 2d 64 69 72 65  - {*}[glob -dire
0520: 63 74 6f 72 79 20 24 3a 3a 61 70 70 66 73 3a 3a  ctory $::appfs::
0530: 63 61 63 68 65 64 69 72 20 7b 5b 30 2d 39 61 2d  cachedir {[0-9a-
0540: 66 5d 5b 30 2d 39 61 2d 66 5d 7d 5d 27 20 7c 7c  f][0-9a-f]}]' ||
0550: 20 72 65 74 75 72 6e 20 31 0a 09 61 70 70 66 73   return 1..appfs
0560: 64 20 2d 73 71 6c 69 74 65 33 20 27 44 45 4c 45  d -sqlite3 'DELE
0570: 54 45 20 46 52 4f 4d 20 73 69 74 65 73 3b 20 44  TE FROM sites; D
0580: 45 4c 45 54 45 20 46 52 4f 4d 20 70 61 63 6b 61  ELETE FROM packa
0590: 67 65 73 3b 20 44 45 4c 45 54 45 20 46 52 4f 4d  ges; DELETE FROM
05a0: 20 66 69 6c 65 73 3b 20 56 41 43 55 55 4d 3b 27   files; VACUUM;'
05b0: 20 7c 7c 20 72 65 74 75 72 6e 20 31 0a 7d 0a 0a   || return 1.}..
05c0: 63 61 73 65 20 22 24 31 22 20 69 6e 0a 09 69 6e  case "$1" in..in
05d0: 76 61 6c 69 64 61 74 65 29 0a 09 09 69 6e 76 61  validate)...inva
05e0: 6c 69 64 61 74 65 20 7c 7c 20 65 78 69 74 20 31  lidate || exit 1
05f0: 0a 09 09 3b 3b 0a 09 72 65 6d 6f 76 65 2d 73 69  ...;;..remove-si
0600: 74 65 29 0a 09 09 72 65 6d 6f 76 65 5f 73 69 74  te)...remove_sit
0610: 65 20 22 24 32 22 20 7c 7c 20 65 78 69 74 20 31  e "$2" || exit 1
0620: 0a 09 09 3b 3b 0a 09 63 6c 65 61 6e 29 0a 09 09  ...;;..clean)...
0630: 63 6c 65 61 6e 20 7c 7c 20 65 78 69 74 20 31 0a  clean || exit 1.
0640: 09 09 3b 3b 0a 09 63 6c 65 61 72 29 0a 09 09 63  ..;;..clear)...c
0650: 6c 65 61 72 20 22 24 32 22 20 7c 7c 20 65 78 69  lear "$2" || exi
0660: 74 20 31 0a 09 09 3b 3b 0a 09 2a 29 0a 09 09 65  t 1...;;..*)...e
0670: 63 68 6f 20 22 55 73 61 67 65 3a 20 61 70 70 66  cho "Usage: appf
0680: 73 2d 63 61 63 68 65 20 7b 69 6e 76 61 6c 69 64  s-cache {invalid
0690: 61 74 65 7c 63 6c 65 61 6e 7c 63 6c 65 61 72 7c  ate|clean|clear|
06a0: 63 6c 65 61 72 20 3c 70 61 63 6b 61 67 65 3e 7c  clear <package>|
06b0: 72 65 6d 6f 76 65 2d 73 69 74 65 20 3c 73 69 74  remove-site <sit
06c0: 65 3e 7d 22 20 3e 26 32 0a 0a 09 09 65 78 69 74  e>}" >&2....exit
06d0: 20 31 0a 09 09 3b 3b 0a 65 73 61 63 0a 0a 65 78   1...;;.esac..ex
06e0: 69 74 20 30 0a                                   it 0.