dl-wrapper 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #
  7. # Call it with -h to see some help.
  8. # To avoid cluttering BR2_DL_DIR, we download to a trashable
  9. # location, namely in $(BUILD_DIR).
  10. # Then, we move the downloaded file to a temporary file in the
  11. # same directory as the final output file.
  12. # This allows us to finally atomically rename it to its final
  13. # name.
  14. # If anything goes wrong, we just remove all the temporaries
  15. # created so far.
  16. # We want to catch any unexpected failure, and exit immediately.
  17. set -e
  18. export BR_BACKEND_DL_GETOPTS=":hc:o:n:N:H:ru:q"
  19. main() {
  20. local OPT OPTARG
  21. local backend output hfile recurse quiet rc
  22. # Parse our options; anything after '--' is for the backend
  23. while getopts :hb:o:H:rq OPT; do
  24. case "${OPT}" in
  25. h) help; exit 0;;
  26. b) backend="${OPTARG}";;
  27. o) output="${OPTARG}";;
  28. H) hfile="${OPTARG}";;
  29. r) recurse="-r";;
  30. q) quiet="-q";;
  31. :) error "option '%s' expects a mandatory argument\n" "${OPTARG}";;
  32. \?) error "unknown option '%s'\n" "${OPTARG}";;
  33. esac
  34. done
  35. # Forget our options, and keep only those for the backend
  36. shift $((OPTIND-1))
  37. if [ -z "${backend}" ]; then
  38. error "no backend specified, use -b\n"
  39. fi
  40. if [ -z "${output}" ]; then
  41. error "no output specified, use -o\n"
  42. fi
  43. # If the output file already exists and:
  44. # - there's no .hash file: do not download it again and exit promptly
  45. # - matches all its hashes: do not download it again and exit promptly
  46. # - fails at least one of its hashes: force a re-download
  47. # - there's no hash (but a .hash file): consider it a hard error
  48. if [ -e "${output}" ]; then
  49. if support/download/check-hash ${quiet} "${hfile}" "${output}" "${output##*/}"; then
  50. exit 0
  51. elif [ ${?} -ne 2 ]; then
  52. # Do not remove the file, otherwise it might get re-downloaded
  53. # from a later location (i.e. primary -> upstream -> mirror).
  54. # Do not print a message, check-hash already did.
  55. exit 1
  56. fi
  57. rm -f "${output}"
  58. warn "Re-downloading '%s'...\n" "${output##*/}"
  59. fi
  60. # tmpd is a temporary directory in which backends may store intermediate
  61. # by-products of the download.
  62. # tmpf is the file in which the backends should put the downloaded content.
  63. # tmpd is located in $(BUILD_DIR), so as not to clutter the (precious)
  64. # $(BR2_DL_DIR)
  65. # We let the backends create tmpf, so they are able to set whatever
  66. # permission bits they want (although we're only really interested in
  67. # the executable bit.)
  68. tmpd="$(mktemp -d "${BUILD_DIR}/.${output##*/}.XXXXXX")"
  69. tmpf="${tmpd}/output"
  70. # Helpers expect to run in a directory that is *really* trashable, so
  71. # they are free to create whatever files and/or sub-dirs they might need.
  72. # Doing the 'cd' here rather than in all backends is easier.
  73. cd "${tmpd}"
  74. # If the backend fails, we can just remove the temporary directory to
  75. # remove all the cruft it may have left behind. Then we just exit in
  76. # error too.
  77. if ! "${OLDPWD}/support/download/${backend}" \
  78. ${quiet} ${recurse} \
  79. -o "${tmpf}" "${@}"
  80. then
  81. rm -rf "${tmpd}"
  82. exit 1
  83. fi
  84. # cd back to free the temp-dir, so we can remove it later
  85. cd "${OLDPWD}"
  86. # Check if the downloaded file is sane, and matches the stored hashes
  87. # for that file
  88. if support/download/check-hash ${quiet} "${hfile}" "${tmpf}" "${output##*/}"; then
  89. rc=0
  90. else
  91. if [ ${?} -ne 3 ]; then
  92. rm -rf "${tmpd}"
  93. exit 1
  94. fi
  95. # the hash file exists and there was no hash to check the file against
  96. rc=1
  97. fi
  98. # tmp_output is in the same directory as the final output, so we can
  99. # later move it atomically.
  100. tmp_output="$(mktemp "${output}.XXXXXX")"
  101. # 'mktemp' creates files with 'go=-rwx', so the files are not accessible
  102. # to users other than the one doing the download (and root, of course).
  103. # This can be problematic when a shared BR2_DL_DIR is used by different
  104. # users (e.g. on a build server), where all users may write to the shared
  105. # location, since other users would not be allowed to read the files
  106. # another user downloaded.
  107. # So, we restore the 'go' access rights to a more sensible value, while
  108. # still abiding by the current user's umask. We must do that before the
  109. # final 'mv', so just do it now.
  110. # Some backends (cp and scp) may create executable files, so we need to
  111. # carry the executable bit if needed.
  112. [ -x "${tmpf}" ] && new_mode=755 || new_mode=644
  113. new_mode=$(printf "%04o" $((0${new_mode} & ~0$(umask))))
  114. chmod ${new_mode} "${tmp_output}"
  115. # We must *not* unlink tmp_output, otherwise there is a small window
  116. # during which another download process may create the same tmp_output
  117. # name (very, very unlikely; but not impossible.)
  118. # Using 'cp' is not reliable, since 'cp' may unlink the destination file
  119. # if it is unable to open it with O_WRONLY|O_TRUNC; see:
  120. # http://pubs.opengroup.org/onlinepubs/9699919799/utilities/cp.html
  121. # Since the destination filesystem can be anything, it might not support
  122. # O_TRUNC, so 'cp' would unlink it first.
  123. # Use 'cat' and append-redirection '>>' to save to the final location,
  124. # since that is the only way we can be 100% sure of the behaviour.
  125. if ! cat "${tmpf}" >>"${tmp_output}"; then
  126. rm -rf "${tmpd}" "${tmp_output}"
  127. exit 1
  128. fi
  129. rm -rf "${tmpd}"
  130. # tmp_output and output are on the same filesystem, so POSIX guarantees
  131. # that 'mv' is atomic, because it then uses rename() that POSIX mandates
  132. # to be atomic, see:
  133. # http://pubs.opengroup.org/onlinepubs/9699919799/functions/rename.html
  134. if ! mv -f "${tmp_output}" "${output}"; then
  135. rm -f "${tmp_output}"
  136. exit 1
  137. fi
  138. return ${rc}
  139. }
  140. help() {
  141. cat <<_EOF_
  142. NAME
  143. ${my_name} - download wrapper for Buildroot
  144. SYNOPSIS
  145. ${my_name} [OPTION]... -- [BACKEND OPTION]...
  146. DESCRIPTION
  147. Wrapper script around different download mechanisms. Ensures that
  148. concurrent downloads do not conflict, that partial downloads are
  149. properly evicted without leaving temporary files, and that access
  150. rights are maintained.
  151. -h This help text.
  152. -b BACKEND
  153. Wrap the specified BACKEND. Known backends are:
  154. bzr Bazaar
  155. cp Local files
  156. cvs Concurrent Versions System
  157. git Git
  158. hg Mercurial
  159. scp Secure copy
  160. svn Subversion
  161. wget HTTP download
  162. -o FILE
  163. Store the downloaded archive in FILE.
  164. -H FILE
  165. Use FILE to read hashes from, and check them against the downloaded
  166. archive.
  167. Exit status:
  168. 0 if OK
  169. !0 in case of error
  170. ENVIRONMENT
  171. BUILD_DIR
  172. The path to Buildroot's build dir
  173. _EOF_
  174. }
  175. trace() { local msg="${1}"; shift; printf "%s: ${msg}" "${my_name}" "${@}"; }
  176. warn() { trace "${@}" >&2; }
  177. errorN() { local ret="${1}"; shift; warn "${@}"; exit ${ret}; }
  178. error() { errorN 1 "${@}"; }
  179. my_name="${0##*/}"
  180. main "${@}"