2
0

apt-download 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/bin/bash
  2. set -e
  3. LOADED_APT_DOWNLOAD="yes"
  4. function check-and-download() {
  5. remote_file=$1
  6. local_file=$2
  7. wget -q --spider ${remote_file}
  8. if [ $? -eq 0 ]; then
  9. echo "downloading ${remote_file}"
  10. wget -q -N -O ${local_file} ${remote_file}
  11. return
  12. fi
  13. return 0
  14. }
  15. function apt-download-binary() {
  16. base_url=$1
  17. dist=$2
  18. repo=$3
  19. arch=$4
  20. dest_base_dir=$5
  21. dest_dir="${dest_base_dir}/dists/${dist}"
  22. [ ! -d "$dest_dir" ] && mkdir -p "$dest_dir"
  23. check-and-download "${base_url}/dists/${dist}/Contents-${arch}.gz" "${dest_dir}/Contents-${arch}.gz" || true
  24. check-and-download "${base_url}/dists/${dist}/InRelease" "${dest_dir}/InRelease" || true
  25. check-and-download "${base_url}/dists/${dist}/Release" "${dest_dir}/Release"
  26. check-and-download "${base_url}/dists/${dist}/Release.gpg" "${dest_dir}/Release.gpg" || true
  27. # Load Package Index URLs from Release file
  28. release_file="${dest_dir}/Release"
  29. dest_dir="${dest_base_dir}/dists/${dist}/${repo}/binary-${arch}"
  30. [ ! -d "$dest_dir" ] && mkdir -p "$dest_dir"
  31. declare pkgidx_content=""
  32. declare cnt_start=false
  33. while read line; do
  34. if [[ ${cnt_start} = true ]]; then
  35. read -a tokens <<< $line
  36. md5=${tokens[0]}
  37. if [[ ${#md5} != 32 ]]; then
  38. break
  39. fi
  40. filesize=${tokens[1]}
  41. filename=${tokens[2]}
  42. if [[ "$filename" =~ ${repo}/binary-${arch} ]]; then
  43. # Load package list from Packages file
  44. pkgidx_file="${dest_base_dir}/dists/${dist}/${filename}"
  45. pkglist_url="${base_url}/dists/${dist}/${filename}"
  46. check-and-download "${pkglist_url}" ${pkgidx_file} || true
  47. echo "${md5} ${pkgidx_file}" | md5sum -c -
  48. if [ -z "${pkgidx_content}" -a -f ${pkgidx_file} ]; then
  49. echo "getting packages index content"
  50. case $filename in
  51. "*.bz2")
  52. pkgidx_content=`bunzip2 -c ${pkgidx_file}`
  53. ;;
  54. "*.gz")
  55. pkgidx_content=`gunzip -c ${pkgidx_file}`
  56. ;;
  57. *)
  58. pkgidx_content=`cat ${pkgidx_file}`
  59. ;;
  60. esac
  61. fi
  62. fi
  63. else
  64. if [[ "$line" =~ ^MD5Sum:$ ]]; then
  65. cnt_start=true
  66. fi
  67. fi
  68. done < ${release_file}
  69. if [ -z "${pkgidx_content}" ]; then
  70. echo "index is empty, failed"
  71. return 1
  72. fi
  73. # Download packages
  74. (echo -e "${pkgidx_content}" | grep -e '^Filename' -e '^Size' -e '^MD5sum' | cut -d' ' -f 2) | \
  75. while read pkg_filename; read pkg_size; read pkg_md5; do
  76. dest_filename="${dest_base_dir}/${pkg_filename}"
  77. dest_dir=`dirname ${dest_filename}`
  78. [ ! -d "$dest_dir" ] && mkdir -p "$dest_dir"
  79. pkg_url="${base_url}/${pkg_filename}"
  80. declare downloaded="false"
  81. if [ -f ${dest_filename} ]; then
  82. rsize=`stat -c "%s" ${dest_filename}`
  83. if [ ${rsize} -eq ${pkg_size} ]; then
  84. downloaded="true"
  85. echo "Skipping ${pkg_filename}, size ${pkg_size}"
  86. fi
  87. fi
  88. while [ $downloaded != "true" ]; do
  89. echo "downloading ${pkg_url}"
  90. wget -q -O ${dest_filename} ${pkg_url} && {
  91. echo "${pkg_md5} ${dest_filename}" | md5sum -c - && downloaded=true # two space for md5sum check format
  92. }
  93. done
  94. done
  95. }
  96. # vim: ts=4 sts=4 sw=4