dl-wrapper 8.8 KB

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