dl-wrapper 7.0 KB

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