2
1

dl-wrapper 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #!/usr/bin/env bash
  2. # This script is a wrapper to the other download backends.
  3. # Its role is to ensure atomicity when saving downloaded files
  4. # back to BR2_DL_DIR, and not clutter BR2_DL_DIR with partial,
  5. # failed downloads.
  6. # To avoid cluttering BR2_DL_DIR, we download to a trashable
  7. # location, namely in $(BUILD_DIR).
  8. # Then, we move the downloaded file to a temporary file in the
  9. # same directory as the final output file.
  10. # This allows us to finally atomically rename it to its final
  11. # name.
  12. # If anything goes wrong, we just remove all the temporaries
  13. # created so far.
  14. # We want to catch any unexpected failure, and exit immediately.
  15. set -e
  16. export BR_BACKEND_DL_GETOPTS=":hc:d:o:n:N:H:lru:qf:e"
  17. main() {
  18. local OPT OPTARG
  19. local backend output hfile large_file recurse quiet rc
  20. local -a uris
  21. # Parse our options; anything after '--' is for the backend
  22. while getopts ":c:d:D:o:n:N:H:lrf:u:q" OPT; do
  23. case "${OPT}" in
  24. c) cset="${OPTARG}";;
  25. d) dl_dir="${OPTARG}";;
  26. D) old_dl_dir="${OPTARG}";;
  27. o) output="${OPTARG}";;
  28. n) raw_base_name="${OPTARG}";;
  29. N) base_name="${OPTARG}";;
  30. H) hfile="${OPTARG}";;
  31. l) large_file="-l";;
  32. r) recurse="-r";;
  33. f) filename="${OPTARG}";;
  34. u) uris+=( "${OPTARG}" );;
  35. q) quiet="-q";;
  36. :) error "option '%s' expects a mandatory argument\n" "${OPTARG}";;
  37. \?) error "unknown option '%s'\n" "${OPTARG}";;
  38. esac
  39. done
  40. # Forget our options, and keep only those for the backend
  41. shift $((OPTIND-1))
  42. if [ -z "${output}" ]; then
  43. error "no output specified, use -o\n"
  44. fi
  45. # Legacy handling: check if the file already exists in the global
  46. # download directory. If it does, hard-link it. If it turns out it
  47. # was an incorrect download, we'd still check it below anyway.
  48. # If we can neither link nor copy, fallback to doing a download.
  49. # NOTE! This is not atomic, is subject to TOCTTOU, but the whole
  50. # dl-wrapper runs under an flock, so we're safe.
  51. if [ ! -e "${output}" -a -e "${old_dl_dir}/${filename}" ]; then
  52. ln "${old_dl_dir}/${filename}" "${output}" || \
  53. cp "${old_dl_dir}/${filename}" "${output}" || \
  54. true
  55. fi
  56. # If the output file already exists and:
  57. # - there's no .hash file: do not download it again and exit promptly
  58. # - matches all its hashes: do not download it again and exit promptly
  59. # - fails at least one of its hashes: force a re-download
  60. # - there's no hash (but a .hash file): consider it a hard error
  61. if [ -e "${output}" ]; then
  62. if support/download/check-hash ${quiet} "${hfile}" "${output}" "${output##*/}"; then
  63. exit 0
  64. elif [ ${?} -ne 2 ]; then
  65. # Do not remove the file, otherwise it might get re-downloaded
  66. # from a later location (i.e. primary -> upstream -> mirror).
  67. # Do not print a message, check-hash already did.
  68. exit 1
  69. fi
  70. rm -f "${output}"
  71. warn "Re-downloading '%s'...\n" "${output##*/}"
  72. fi
  73. # Look through all the uris that we were given to download the package
  74. # source
  75. download_and_check=0
  76. rc=1
  77. for uri in "${uris[@]}"; do
  78. backend_urlencode="${uri%%+*}"
  79. backend="${backend_urlencode%|*}"
  80. case "${backend}" in
  81. git|svn|cvs|bzr|file|scp|hg|sftp) ;;
  82. *) backend="wget" ;;
  83. esac
  84. uri=${uri#*+}
  85. urlencode=${backend_urlencode#*|}
  86. # urlencode must be "urlencode"
  87. [ "${urlencode}" != "urlencode" ] && urlencode=""
  88. # tmpd is a temporary directory in which backends may store
  89. # intermediate by-products of the download.
  90. # tmpf is the file in which the backends should put the downloaded
  91. # content.
  92. # tmpd is located in $(BUILD_DIR), so as not to clutter the (precious)
  93. # $(BR2_DL_DIR)
  94. # We let the backends create tmpf, so they are able to set whatever
  95. # permission bits they want (although we're only really interested in
  96. # the executable bit.)
  97. tmpd="$(mktemp -d "${BUILD_DIR}/.${output##*/}.XXXXXX")"
  98. tmpf="${tmpd}/output"
  99. # Helpers expect to run in a directory that is *really* trashable, so
  100. # they are free to create whatever files and/or sub-dirs they might need.
  101. # Doing the 'cd' here rather than in all backends is easier.
  102. cd "${tmpd}"
  103. # If the backend fails, we can just remove the content of the temporary
  104. # directory to remove all the cruft it may have left behind, and try
  105. # the next URI until it succeeds. Once out of URI to try, we need to
  106. # cleanup and exit.
  107. if ! "${OLDPWD}/support/download/${backend}" \
  108. $([ -n "${urlencode}" ] && printf %s '-e') \
  109. -c "${cset}" \
  110. -d "${dl_dir}" \
  111. -n "${raw_base_name}" \
  112. -N "${base_name}" \
  113. -f "${filename}" \
  114. -u "${uri}" \
  115. -o "${tmpf}" \
  116. ${quiet} ${large_file} ${recurse} -- "${@}"
  117. then
  118. # cd back to keep path coherence
  119. cd "${OLDPWD}"
  120. rm -rf "${tmpd}"
  121. continue
  122. fi
  123. # cd back to free the temp-dir, so we can remove it later
  124. cd "${OLDPWD}"
  125. # Check if the downloaded file is sane, and matches the stored hashes
  126. # for that file
  127. if support/download/check-hash ${quiet} "${hfile}" "${tmpf}" "${output##*/}"; then
  128. rc=0
  129. else
  130. if [ ${?} -ne 3 ]; then
  131. rm -rf "${tmpd}"
  132. continue
  133. fi
  134. # the hash file exists and there was no hash to check the file
  135. # against
  136. rc=1
  137. fi
  138. download_and_check=1
  139. break
  140. done
  141. # We tried every URI possible, none seems to work or to check against the
  142. # available hash. *ABORT MISSION*
  143. if [ "${download_and_check}" -eq 0 ]; then
  144. rm -rf "${tmpd}"
  145. exit 1
  146. fi
  147. # tmp_output is in the same directory as the final output, so we can
  148. # later move it atomically.
  149. tmp_output="$(mktemp "${output}.XXXXXX")"
  150. # 'mktemp' creates files with 'go=-rwx', so the files are not accessible
  151. # to users other than the one doing the download (and root, of course).
  152. # This can be problematic when a shared BR2_DL_DIR is used by different
  153. # users (e.g. on a build server), where all users may write to the shared
  154. # location, since other users would not be allowed to read the files
  155. # another user downloaded.
  156. # So, we restore the 'go' access rights to a more sensible value, while
  157. # still abiding by the current user's umask. We must do that before the
  158. # final 'mv', so just do it now.
  159. # Some backends (cp and scp) may create executable files, so we need to
  160. # carry the executable bit if needed.
  161. [ -x "${tmpf}" ] && new_mode=755 || new_mode=644
  162. new_mode=$(printf "%04o" $((0${new_mode} & ~0$(umask))))
  163. chmod ${new_mode} "${tmp_output}"
  164. # We must *not* unlink tmp_output, otherwise there is a small window
  165. # during which another download process may create the same tmp_output
  166. # name (very, very unlikely; but not impossible.)
  167. # Using 'cp' is not reliable, since 'cp' may unlink the destination file
  168. # if it is unable to open it with O_WRONLY|O_TRUNC; see:
  169. # http://pubs.opengroup.org/onlinepubs/9699919799/utilities/cp.html
  170. # Since the destination filesystem can be anything, it might not support
  171. # O_TRUNC, so 'cp' would unlink it first.
  172. # Use 'cat' and append-redirection '>>' to save to the final location,
  173. # since that is the only way we can be 100% sure of the behaviour.
  174. if ! cat "${tmpf}" >>"${tmp_output}"; then
  175. rm -rf "${tmpd}" "${tmp_output}"
  176. exit 1
  177. fi
  178. rm -rf "${tmpd}"
  179. # tmp_output and output are on the same filesystem, so POSIX guarantees
  180. # that 'mv' is atomic, because it then uses rename() that POSIX mandates
  181. # to be atomic, see:
  182. # http://pubs.opengroup.org/onlinepubs/9699919799/functions/rename.html
  183. if ! mv -f "${tmp_output}" "${output}"; then
  184. rm -f "${tmp_output}"
  185. exit 1
  186. fi
  187. return ${rc}
  188. }
  189. trace() { local msg="${1}"; shift; printf "%s: ${msg}" "${my_name}" "${@}"; }
  190. warn() { trace "${@}" >&2; }
  191. errorN() { local ret="${1}"; shift; warn "${@}"; exit ${ret}; }
  192. error() { errorN 1 "${@}"; }
  193. my_name="${0##*/}"
  194. main "${@}"