dl-wrapper 6.6 KB

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