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.
 
 
 
 

196 lines
5.1 KiB

  1. #!/bin/sh -
  2. #
  3. # Copyright 2018, 2022-2023 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. set -e
  28. #
  29. # Note: It appears that sometimes files can "disappear" from the server, so,
  30. # uncompress snapshot.complete.idx.xz to both snapshot.complete.idx and
  31. # snapshot.idx, and then run "sh addinfo.sh -c YYYYMMDD" with the oldest
  32. # known snapshot.
  33. #
  34. # $ xzcat snapshot.idx.xz | awk '{ print $5 }' | sort -u
  35. # $ unxz < snapshot.complete.idx.xz > snapshot.complete.idx
  36. # $ unxz < snapshot.complete.idx.xz > snapshot.idx
  37. SNAPDIR=$(dirname $0)
  38. MAILDIR="$SNAPDIR"/mail
  39. usage() {
  40. echo "Usage: $0 [ -m ] <file>"
  41. echo "Usage: $0 -c <date>"
  42. echo ''
  43. echo 'Options:'
  44. echo ' -m Do not check what files are available. This is useful for'
  45. echo ' bulk import.'
  46. echo ' -c Complete a bulk import (previously using -m), and assume that'
  47. echo ' any snapshots before specified data are unfetchable.'
  48. echo ''
  49. echo 'date is specified as YYYYMMDD'
  50. if [ x"$1" != x"" ]; then
  51. exit $1
  52. fi
  53. }
  54. args=`getopt cm $*`
  55. if [ $? -ne 0 ]; then
  56. usage 2
  57. fi
  58. set -- $args
  59. while :; do
  60. case "$1" in
  61. -c)
  62. complete=1
  63. shift
  64. ;;
  65. -m)
  66. more=1
  67. shift
  68. ;;
  69. --)
  70. shift; break
  71. ;;
  72. esac
  73. done
  74. if [ ! -d "$MAILDIR" ]; then
  75. echo 'ERROR: '"$MAILDIR"' does not exist.'
  76. exit 3
  77. fi
  78. if [ x"$complete" = x"1" -a x"$more" = x"1" ]; then
  79. echo '-m and -c cannot be specified at the same time.'
  80. usage 2
  81. elif [ x"$complete" = x"1" -a $# -ne 1 ]; then
  82. echo 'must only specify a date with -c'
  83. usage 2
  84. elif [ x"$complete" != x"1" -a $# -ne 1 ]; then
  85. echo 'must specify exactly one file'
  86. usage 2
  87. fi
  88. while ! mkdir "$0.running"; do
  89. sleep 1;
  90. done
  91. trap 'rmdir "$0.running"' 0
  92. export SNAPAID_SH=source
  93. . "$SNAPDIR"/snapaid.sh
  94. if [ x"$complete" = x"1" ]; then
  95. if [ ! -f snapshot.complete.idx ]; then
  96. echo 'snapshot.complete.idx does not exist, aborting...'
  97. rmdir "$0.running"
  98. exit 5
  99. fi
  100. if [ ! -f snapshot.idx ]; then
  101. echo 'snapshot.idx does not exist, aborting...'
  102. rmdir "$0.running"
  103. exit 5
  104. fi
  105. sort -u snapshot.complete.idx | xz > snapshot.complete.idx.new.xz
  106. awk '$5 == "xxx" || $5 >= "'"$1"'" {
  107. if (!system("wget --method=HEAD " $9))
  108. print
  109. }
  110. ' snapshot.idx | sort -u | xz > snapshot.idx.new.xz
  111. chmod 644 snapshot.idx.new.xz snapshot.complete.idx.new.xz
  112. mv snapshot.idx.new.xz snapshot.idx.xz
  113. mv snapshot.complete.idx.new.xz snapshot.complete.idx.xz
  114. rm snapshot.idx snapshot.complete.idx
  115. exit 0
  116. fi
  117. # minimize file
  118. tmpfname="tmp.snapinf.asc"
  119. minimizeemail < "$1" > "$tmpfname"
  120. if ! verifygpgfile "$tmpfname"; then
  121. echo 'failed verify'
  122. rm "$tmpfname"
  123. rmdir "$0.running"
  124. exit 1
  125. fi
  126. mid="$(awk 'tolower($1) == "message-id:" {
  127. MID=$2
  128. sub(".*<", "", MID)
  129. sub(">.*", "", MID)
  130. printf "%s", MID
  131. exit 0
  132. }' "$tmpfname")"
  133. midfile="$MAILDIR/$mid"
  134. if [ -e "$midfile" ]; then
  135. echo "ERROR: $midfile already exists, failed processing $tmpfname"
  136. exit 3
  137. fi
  138. # process file
  139. awk -f ./mksnapidx.awk "$tmpfname" > additional
  140. cp "$tmpfname" "$midfile"
  141. chmod og+rX "$midfile"
  142. rm "$tmpfname"
  143. # only check if there isn't more to come
  144. if [ x"$more" = x"1" ]; then
  145. echo queuing $(wc -l additional) entries
  146. (cat snapshot.idx 2>/dev/null || xzcat snapshot.idx.xz; cat additional) > snapshot.idx.new
  147. (cat snapshot.complete.idx 2>/dev/null || xzcat snapshot.complete.idx.xz; cat additional) > snapshot.complete.idx.new
  148. else
  149. (xzcat snapshot.idx.xz; cat additional) | sort -u | awk '
  150. {
  151. print "testing " $9 > "/dev/stderr"
  152. if (!system("wget --method=HEAD " $9))
  153. print
  154. }
  155. ' > snapshot.idx.new
  156. xz snapshot.idx.new
  157. (xzcat snapshot.complete.idx.xz || :; cat additional) | sort -u > snapshot.complete.idx.new
  158. xz snapshot.complete.idx.new
  159. fi
  160. rm additional
  161. # install new indexes
  162. if [ x"$more" = x"1" ]; then
  163. mv snapshot.idx.new snapshot.idx
  164. mv snapshot.complete.idx.new snapshot.complete.idx
  165. else
  166. chmod 644 snapshot.idx.new.xz snapshot.complete.idx.new.xz
  167. mv snapshot.idx.new.xz snapshot.idx.xz
  168. mv snapshot.complete.idx.new.xz snapshot.complete.idx.xz
  169. fi