dl-wrapper 7.0 KB

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