A utility for downloading and verifying FreeBSD releases and snapshots
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

176 lines
4.3 KiB

  1. #!/bin/sh -
  2. #
  3. # Copyright 2018 John-Mark Gurney.
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. # 1. Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. # notice, this list of conditions and the following disclaimer in the
  13. # documentation and/or other materials provided with the distribution.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. # SUCH DAMAGE.
  26. #
  27. # $Id$
  28. #
  29. set -e
  30. #
  31. # Note: It appears that sometimes files can "disappear" from the server, so,
  32. # uncompress snapshot.complete.idx.xz to both snapshot.complete.idx and
  33. # snapshot.idx, and then run "sh addinfo.sh -c YYYYMMDD" with the oldest
  34. # known snapshot.
  35. usage() {
  36. echo "Usage: $0 [ -m ] <file>"
  37. echo "Usage: $0 -c <date>"
  38. echo ''
  39. echo 'Options:'
  40. echo ' -m Do not check what files are available. This is useful for'
  41. echo ' bulk import.'
  42. echo ' -c Complete a bulk import (previously using -m), and assume that'
  43. echo ' any snapshots before specified data are unfetchable.'
  44. echo ''
  45. echo 'date is specified as YYYYMMDD'
  46. if [ x"$1" != x"" ]; then
  47. exit $1
  48. fi
  49. }
  50. args=`getopt cm $*`
  51. if [ $? -ne 0 ]; then
  52. usage 2
  53. fi
  54. set -- $args
  55. while :; do
  56. case "$1" in
  57. -c)
  58. complete=1
  59. shift
  60. ;;
  61. -m)
  62. more=1
  63. shift
  64. ;;
  65. --)
  66. shift; break
  67. ;;
  68. esac
  69. done
  70. if [ x"$complete" = x"1" -a x"$more" = x"1" ]; then
  71. echo '-m and -c cannot be specified at the same time.'
  72. usage 2
  73. elif [ x"$complete" = x"1" -a $# -ne 1 ]; then
  74. echo 'must only specify a date with -c'
  75. usage 2
  76. elif [ x"$complete" != x"1" -a $# -ne 1 ]; then
  77. echo 'must specify exactly one file'
  78. usage 2
  79. fi
  80. while ! mkdir "$0.running"; do
  81. sleep 1;
  82. done
  83. if [ x"$complete" = x"1" ]; then
  84. if [ ! -f snapshot.complete.idx ]; then
  85. echo 'snapshot.complete.idx does not exist, aborting...'
  86. rmdir "$0.running"
  87. exit 5
  88. fi
  89. if [ ! -f snapshot.idx ]; then
  90. echo 'snapshot.idx does not exist, aborting...'
  91. rmdir "$0.running"
  92. exit 5
  93. fi
  94. sort -u snapshot.complete.idx | xz > snapshot.complete.idx.xz
  95. awk '$5 == "xxx" || $5 >= "'"$1"'" {
  96. if (!system("wget --method=HEAD " $9))
  97. print
  98. }
  99. ' snapshot.idx | sort -u | xz > snapshot.idx.xz
  100. rm snapshot.idx snapshot.complete.idx
  101. rmdir "$0.running"
  102. exit 0
  103. fi
  104. # minimize file
  105. tmpfname="tmp.snapinf.asc"
  106. awk '
  107. output != 1 && tolower($1) == "message-id:" {
  108. print
  109. next
  110. }
  111. $0 == "-----BEGIN PGP SIGNED MESSAGE-----" {
  112. output = 1
  113. }
  114. output == 1 {
  115. print
  116. }
  117. $0 == "-----END PGP SIGNATURE-----" {
  118. output = 0
  119. }' "$1" > "$tmpfname"
  120. if ! gpg --verify "$tmpfname"; then
  121. echo 'failed verify'
  122. rm "$tmpfname"
  123. rmdir "$0.running"
  124. exit 1
  125. fi
  126. # process file
  127. awk -f ./mksnapidx.awk "$tmpfname" > additional
  128. rm "$tmpfname"
  129. # only check if there isn't more to come
  130. if [ x"$more" = x"1" ]; then
  131. (cat snapshot.idx || :; cat additional) > snapshot.idx.new
  132. (cat snapshot.complete.idx || :; cat additional) > snapshot.complete.idx.new
  133. else
  134. (xzcat snapshot.idx.xz; cat additional) | sort -u | awk '
  135. {
  136. if (!system("wget --method=HEAD " $9))
  137. print
  138. }
  139. ' > snapshot.idx.new
  140. xz snapshot.idx.new
  141. (xzcat snapshot.complete.idx.xz || :; cat additional) | sort -u > snapshot.complete.idx.new
  142. xz snapshot.complete.idx.new
  143. fi
  144. rm additional
  145. # install new indexes
  146. if [ x"$more" = x"1" ]; then
  147. mv snapshot.idx.new snapshot.idx
  148. mv snapshot.complete.idx.new snapshot.complete.idx
  149. else
  150. chmod 644 snapshot.idx.new.xz snapshot.complete.idx.new.xz
  151. mv snapshot.idx.new.xz snapshot.idx.xz
  152. mv snapshot.complete.idx.new.xz snapshot.complete.idx.xz
  153. fi
  154. rmdir "$0.running"