wrapper 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env bash
  2. # This script is a wrapper to the other download helpers.
  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:
  8. # $1: name of the helper (eg. cvs, git, cp...)
  9. # $2: full path to the file in which to save the download
  10. # $*: additional arguments to the helper in $1
  11. # Environment:
  12. # BUILD_DIR: the path to Buildroot's build dir
  13. # To avoid cluttering BR2_DL_DIR, we download to a trashable
  14. # location, namely in $(BUILD_DIR).
  15. # Then, we move the downloaded file to a temporary file in the
  16. # same directory as the final output file.
  17. # This allows us to finally atomically rename it to its final
  18. # name.
  19. # If anything goes wrong, we just remove all the temporaries
  20. # created so far.
  21. # We want to catch any unexpected failure, and exit immediately.
  22. set -e
  23. helper="${1}"
  24. output="${2}"
  25. shift 2
  26. # tmpd is a temporary directory in which helpers may store intermediate
  27. # by-products of the download.
  28. # tmpf is the file in which the helpers should put the downloaded content.
  29. # tmpd is located in $(BUILD_DIR), so as not to clutter the (precious)
  30. # $(BR2_DL_DIR)
  31. # We let the helpers create tmpf, so they are able to set whatever
  32. # permission bits they want (although we're only really interested in
  33. # the executable bit.)
  34. tmpd="$( mktemp -d "${BUILD_DIR}/.${output##*/}.XXXXXX" )"
  35. tmpf="${tmpd}/output"
  36. # Helpers expect to run in a directory that is *really* trashable, so
  37. # they are free to create whatever files and/or sub-dirs they might need.
  38. # Doing the 'cd' here rather than in all helpers is easier.
  39. cd "${tmpd}"
  40. # If the helper fails, we can just remove the temporary directory to
  41. # remove all the cruft it may have left behind. Then we just exit in
  42. # error too.
  43. if ! "${OLDPWD}/support/download/${helper}" "${tmpf}" "${@}"; then
  44. rm -rf "${tmpd}"
  45. exit 1
  46. fi
  47. # cd back to free the temp-dir, so we can remove it later
  48. cd "${OLDPWD}"
  49. # tmp_output is in the same directory as the final output, so we can
  50. # later move it atomically.
  51. tmp_output="$( mktemp "${output}.XXXXXX" )"
  52. # 'mktemp' creates files with 'go=-rwx', so the files are not accessible
  53. # to users other than the one doing the download (and root, of course).
  54. # This can be problematic when a shared BR2_DL_DIR is used by different
  55. # users (e.g. on a build server), where all users may write to the shared
  56. # location, since other users would not be allowed to read the files
  57. # another user downloaded.
  58. # So, we restore the 'go' access rights to a more sensible value, while
  59. # still abiding by the current user's umask. We must do that before the
  60. # final 'mv', so just do it now.
  61. # Some helpers (cp and scp) may create executable files, so we need to
  62. # carry the executable bit if needed.
  63. [ -x "${tmpf}" ] && new_mode=755 || new_mode=644
  64. new_mode=$( printf "%04o" $((0${new_mode} & ~0$(umask))) )
  65. chmod ${new_mode} "${tmp_output}"
  66. # We must *not* unlink tmp_output, otherwise there is a small window
  67. # during which another download process may create the same tmp_output
  68. # name (very, very unlikely; but not impossible.)
  69. # Using 'cp' is not reliable, since 'cp' may unlink the destination file
  70. # if it is unable to open it with O_WRONLY|O_TRUNC; see:
  71. # http://pubs.opengroup.org/onlinepubs/9699919799/utilities/cp.html
  72. # Since the destination filesystem can be anything, it might not support
  73. # O_TRUNC, so 'cp' would unlink it first.
  74. # Use 'cat' and append-redirection '>>' to save to the final location,
  75. # since that is the only way we can be 100% sure of the behaviour.
  76. if ! cat "${tmpf}" >>"${tmp_output}"; then
  77. rm -rf "${tmpd}" "${tmp_output}"
  78. exit 1
  79. fi
  80. rm -rf "${tmpd}"
  81. # tmp_output and output are on the same filesystem, so POSIX guarantees
  82. # that 'mv' is atomic, because it then uses rename() that POSIX mandates
  83. # to be atomic, see:
  84. # http://pubs.opengroup.org/onlinepubs/9699919799/functions/rename.html
  85. if ! mv "${tmp_output}" "${output}"; then
  86. rm -f "${tmp_output}"
  87. exit 1
  88. fi