dl-wrapper 6.5 KB

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