gen-sha1-db.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/bin/bash
  2. ## Iterate through complete package archive to generate a complete list of SHA1
  3. ## Uses some magic to directly generate sha1sums without extracting to disk
  4. ##
  5. ## Command parameter 'init' will build from scratch. Very slow.
  6. ##
  7. ## Bin packages may contain links. For simplicity we currently dereference
  8. ## these("tar -xhv"), so we have each file with its associated sha1 measurement.
  9. work=/srv/http/htdocs/apt-sec/
  10. dest=$work/mirror/Sha1Sums.db
  11. log=$work/mirror/sha1db.log
  12. apt=/srv/apt-mirror
  13. archive=$apt/mirror
  14. var=$apt/var
  15. NEW=$apt/var/NEW
  16. ALL=$apt/var/ALL
  17. ALL=$apt/var/DEL
  18. action="$1"
  19. #which sqlite3 > /dev/null || echo 'Need sqlite3. Exit.' 2>&1
  20. #which sqlite3 > /dev/null || exit
  21. # some overrides
  22. [ "AA${PKGS}" = "AA" ] || archive="$PKGS"
  23. [ "AA${DEST}" = "AA" ] || dest="$DEST"
  24. echo "Updating Sha1Sums.."
  25. echo > $log
  26. sha1pipe () {
  27. file="$1"
  28. pkg="$(basename $1)"
  29. tmp=$(mktemp -d)
  30. ar -t $file |grep -q data.tar.gz && ( ar p $file data.tar.gz | tar xzC $tmp || echo "Can't extract from $file" )
  31. ar -t $file |grep -q data.tar.bz2 && ( ar p $file data.tar.bz2 | tar xjC $tmp || echo "Can't extract from $file" )
  32. cd $tmp
  33. ## stream of <shasum>::<package>::<filepath>
  34. find . -type f -exec sha1sum {} \+ \
  35. | sed -e "s/'/\\\'/" -e "s/^/insert into sha2pkg (hash, pkg, file) VALUES('/" -e "s/ \./', '$pkg', '/" -e "s/$/')\;/"
  36. cd /
  37. rm -rf $tmp
  38. }
  39. case $action in
  40. "init")
  41. # delete and rebuild sha1 db
  42. echo "Reinit - deleting current SHA1 db.."
  43. rm $dest
  44. sqlite3 $dest 'create table sha2pkg (hash char(20), pkg text, file text)'
  45. sqlite3 $dest 'create index [IDX_hash] on sha2pkg (hash)'
  46. echo "Reinitializing SHA1 DB.."
  47. (echo "begin;";
  48. for pkg in $(find $archive -type f -iname \*.deb); do
  49. echo "Adding $pkg to sha1 db.." >> $log
  50. sha1pipe $pkg
  51. done; echo "commit"; ) > $dest.sql
  52. #cat $dest.sql |sqlite3 $dest >> $log 2>&1
  53. ;;
  54. "update")
  55. # record removed packets, remove sha1sums of packets recorded *last week*
  56. # delete packages that were removed last week
  57. # (how long are packages kept in online repo?)
  58. del=$DEL-$(date +%w)
  59. touch $del
  60. for pkg in $(cat $del); do
  61. echo "Deleting outdated package $pkg from db.." >> $log
  62. echo "delete * from sha2pkg where pkg='$pkg';"
  63. done > $dest.sql
  64. # | sqlite3 $dest >> $log 2>&1
  65. # identify newly removed packages, overwriting last week's DEL record
  66. for pkg in $(cat $ALL.old); do
  67. grep -q $pkg $ALL || echo $pkg >> $del
  68. done
  69. cp $ALL $ALL.old
  70. # add sha1 sums for new pkgs
  71. (echo "begin;";
  72. for pkg in $(cat $NEW); do
  73. pkg=$(echo $pkg|sed 's/.*:\/\///');
  74. echo "Adding $pkg to sha1 db.." >> $log
  75. sha1pipe $archive/$pkg
  76. done; echo "commit"; ) > $dest.sql
  77. #| sqlite3 $dest >> $log 2>&1
  78. # make sure we don't parse those packages again
  79. # (shouldn't wait for next apt-mirror call)
  80. echo -n > $NEW
  81. ;;
  82. *)
  83. echo
  84. echo "Usage: $0 (init|update)" 2>&1
  85. echo
  86. exit
  87. esac