Skip to contentSkip to conversation

Cleanups in download helpers (branch yem/check-downloads)

PATCH New Yann E. MORIN 3 participants 12 messages Aug 3, 2014 – Aug 4, 2014

lore
Share & tools
lore
#1 lore raw

PATCH v4 1/9 support/download: add download wrapper

In reply to Yann E. MORIN

The download wrapper is responsible for ensuring the atomicity of saving into $(BR2_DL_DIR).

It calls the appropriate download helper, telling it to save the downloaded content to a temporary file in $(BUILD_DIR) (so it does not clutter $(BR2_DL_DIR) with partial, failed downloads.

Then, only if the download helper was successful, does the wrapper save the downloaded content to the final location, yet still in a temporary file, and finally atomically renames it to the final output file.

---
 support/download/wrapper | 99 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 99 insertions(+)
 create mode 100755 support/download/wrapper
support/download/wrapper new file +99 -0
new file mode 100755
index 0000000..8ae2797
@@ -0,0 +1,99 @@
+#!/bin/bash
+
+# This script is a wrapper to the other download helpers.
+# Its role is to ensure atomicity when saving downloaded files
+# back to BR2_DL_DIR, and not clutter BR2_DL_DIR with partial,
+# failed downloads.
+#
+# Call it with:
+#   $1: name of the helper (eg. cvs, git, cp...)
+#   $2: full path to the file in which to save the download
+#   $*: additional arguments to the helper in $1
+# Environment:
+#   BUILD_DIR: the path to Buildroot's build dir
+
+# To avoid cluttering BR2_DL_DIR, we download to a trashable
+# location, namely in $(BUILD_DIR).
+# Then, we move the downloaded file to a temporary file in the
+# same directory as the final output file.
+# This allows us to finally atomically rename it to its final
+# name.
+# If anything goes wrong, we just remove all the temporaries
+# created so far.
+
+# We want to catch any unexpected failure, and exit immediately.
+set -e
+
+helper="${1}"
+output="${2}"
+shift 2
+
+# tmpd is a temporary directory in which helpers may store intermediate
+# by-products of the download.
+# tmpf is the file in which the helpers should put the downloaded content.
+# tmpd is located in $(BUILD_DIR), so as not to clutter the (precious)
+# $(BR2_DL_DIR)
+# We let the helpers create tmpf, so they are able to set whatever
+# permission bits they want (although we're only really interested in
+# the executable bit.)
+tmpd="$( mktemp -d "${BUILD_DIR}/.${output##*/}.XXXXXX" )"
+tmpf="${tmpd}/output"
+
+# Helpers expect to run in a directory that is *really* trashable, so
+# they are free to create whatever files and/or sub-dirs they might need.
+# Doing the 'cd' here rather than in all helpers is easier.
+cd "${tmpd}"
+
+# If the helper fails, we can just remove the temporary directory to
+# remove all the cruft it may have left behind. Then we just exit in
+# error too.
+if ! "${OLDPWD}/support/download/${helper}" "${tmpf}" "${@}"; then
+    rm -rf "${tmpd}"
+    exit 1
+fi
+
+# cd back to free the temp-dir, so we can remove it later
+cd "${OLDPWD}"
+
+# tmp_output is in the same directory as the final output, so we can
+# later move it atomically.
+tmp_output="$( mktemp "${output}.XXXXXX" )"
+
+# 'mktemp' creates files with 'go=-rwx', so the files are not accessible
+# to users other than the one doing the download (and root, of course).
+# This can be problematic when a shared BR2_DL_DIR is used by different
+# users (e.g. on a build server), where all users may write to the shared
+# location, since other users would not be allowed to read the files
+# another user downloaded.
+# So, we restore the 'go' access rights to a more sensible value, while
+# still abiding by the current user's umask. We must do that before the
+# final 'mv', so just do it now.
+# Some helpers (cp and scp) may create executable files, so we need to
+# carry the executable bit if needed.
+[ -x "${tmpf}" ] && new_mode=755 || new_mode=644
+new_mode=$( printf "%04o" $((0${new_mode} & ~0$(umask))) )
+chmod ${new_mode} "${tmp_output}"
+
+# We must *not* unlink tmp_output, otherwise there is a small window
+# during which another download process may create the same tmp_output
+# name (very, very unlikely; but not impossible.)
+# Using 'cp' is not reliable, since 'cp' may unlink the destination file
+# if it is unable to open it with O_WRONLY|O_TRUNC; see:
+#   http://pubs.opengroup.org/onlinepubs/9699919799/utilities/cp.html
+# Since the destination filesystem can be anything, it might not support
+# O_TRUNC, so 'cp' would unlink it first.
+# Use 'cat' and append-redirection '>>' to save to the final location,
+# since that is the only way we can be 100% sure of the behaviour.
+if ! cat "${tmpf}" >>"${tmp_output}"; then
+    rm -rf "${tmpd}" "${tmp_output}"
+    exit 1
+fi
+rm -rf "${tmpd}"
+# tmp_output and output are on the same filesystem, so POSIX guarantees
+# that 'mv' is atomic, because it then uses rename() that POSIX mandates
+# to be atomic, see:
+#   http://pubs.opengroup.org/onlinepubs/9699919799/functions/rename.html
+if ! mv "${tmp_output}" "${output}"; then
+    rm -f "${tmp_output}"
+    exit 1
+fi
Signature · 1 line
1.9.1
#3 lore raw

PATCH v4 2/9 support/download: convert bzr to use the wrapper

In reply to Yann E. MORIN

This drastically simplifies the bzr helper, as it no longer has to deal with atomically saving the downloaded archive.

---
Changes v3 -> v4:
- fix root dir of the archive (Thomas DS)
- tweak the usage comment (Thomas DS)

---
 package/pkg-download.mk |  6 +++++-
 support/download/bzr    | 42 ++++++++++++------------------------------
 2 files changed, 17 insertions(+), 31 deletions(-)
package/pkg-download.mk +5 -1
index 118591c..0c9397a 100644
@@ -112,7 +112,11 @@ endef
 
 define DOWNLOAD_BZR
 	test -e $(DL_DIR)/$($(PKG)_SOURCE) || \
-	$(EXTRA_ENV) support/download/bzr $($(PKG)_SITE) $($(PKG)_DL_VERSION) $(DL_DIR)/$($(PKG)_SOURCE)
+	$(EXTRA_ENV) support/download/wrapper bzr \
+		$(DL_DIR)/$($(PKG)_SOURCE) \
+		$($(PKG)_SITE) \
+		$($(PKG)_DL_VERSION) \
+		$($(PKG)_BASE_NAME)
 endef
 
 define SOURCE_CHECK_BZR
support/download/bzr +12 -30

50 lines not on this page · raw message

Signature · 1 line
1.9.1
#4 lore raw

PATCH v4 3/9 support/download: convert localfiles to use the wrapper

In reply to Yann E. MORIN

This drastically simplifies the localfiles helper, as it no longer has to deal with atomically saving the downloaded archive.

(Tested by setting BUSYBOX_SITE = file:///tmp and running 'make busybox-source')

--
Changes v3 -> v4:
- tweak the usage comment (Thomas DS)

---
 package/pkg-download.mk |  5 +++--
 support/download/cp     | 26 ++++++++------------------
 2 files changed, 11 insertions(+), 20 deletions(-)
package/pkg-download.mk +3 -2
index 0c9397a..f10aad8 100644
@@ -209,8 +209,9 @@ endef
 
 define DOWNLOAD_LOCALFILES
 	test -e $(DL_DIR)/$(2) || \
-	$(EXTRA_ENV) support/download/cp $(call stripurischeme,$(call qstrip,$(1))) \
-					 $(DL_DIR)/$(2) && \
+	$(EXTRA_ENV) support/download/wrapper cp \
+		$(DL_DIR)/$(2) \
+		$(call stripurischeme,$(call qstrip,$(1))) && \
 	$(call VERIFY_HASH,$(PKGDIR)/$($(PKG)_NAME).hash,$(DL_DIR)/$(2))
 endef
 
support/download/cp +8 -18
index 8f6bc06..264f5dc 100755
@@ -1,26 +1,16 @@
 #!/bin/bash
 
-# We want to catch any command failure, and exit immediately
+# We want to catch any unexpected failure, and exit immediately
 set -e
 
-# Download helper for cp
-# Call it with:
-#   $1: source file
-#   $2: output file
+# Download helper for cp, to be called from the download wrapper script
+# Expected arguments:
+#   $1: output file
+#   $2: source file
 # And this environment:
 #   LOCALFILES: the cp command to call
 
-source="${1}"
-output="${2}"
+output="${1}"
+source="${2}"
 
-tmp_output="$( mktemp "${output}.XXXXXX" )"
-
-ret=1
-if ${LOCALFILES} "${source}" "${tmp_output}"; then
-    mv "${tmp_output}" "${output}"
-    ret=0
-fi
-
-# Cleanup
-rm -f "${tmp_output}"
-exit ${ret}
+${LOCALFILES} "${source}" "${output}"
Signature · 1 line
1.9.1
#5 lore raw

PATCH v4 4/9 support/download: convert cvs to use the wrapper

In reply to Yann E. MORIN

This drastically simplifies the cvs helper, as it no longer has to deal with atomically saving the downloaded archive.

---
Changes v3 -> v4:
- tweak the usage comment (Thomas DS)

---
 package/pkg-download.mk |  9 ++++++---
 support/download/cvs    | 54 +++++++++++++++----------------------------------
 2 files changed, 22 insertions(+), 41 deletions(-)
package/pkg-download.mk +6 -3
index f10aad8..66ce02c 100644
@@ -129,9 +129,12 @@ endef
 
 define DOWNLOAD_CVS
 	test -e $(DL_DIR)/$($(PKG)_SOURCE) || \
-	$(EXTRA_ENV) support/download/cvs $(call stripurischeme,$(call qstrip,$($(PKG)_SITE))) \
-					  $($(PKG)_DL_VERSION) $($(PKG)_RAWNAME) \
-					  $($(PKG)_BASE_NAME) $(DL_DIR)/$($(PKG)_SOURCE)
+	$(EXTRA_ENV) support/download/wrapper cvs \
+		$(DL_DIR)/$($(PKG)_SOURCE) \
+		$(call stripurischeme,$(call qstrip,$($(PKG)_SITE))) \
+		$($(PKG)_DL_VERSION) \
+		$($(PKG)_RAWNAME) \
+		$($(PKG)_BASE_NAME)
 endef
 
 # Not all CVS servers support ls/rls, use login to see if we can connect
support/download/cvs +16 -38

63 lines not on this page · raw message

Signature · 1 line
1.9.1
#0 lore raw

v4 cover Cleanups in download helpers (branch yem/check-downloads)

Hello All!

This series is a follow-up to the previous download series. Following the advice from Arnout, I introduced a wrapper script that is responsible for the saving atomically in BR2_DL_DIR. If also manages all the temporary files, and properly cleans up.

Consequently, the helpers are now nuch more simple, and no longer need to duplicate the complex handling of atomicity in BR2_DL_DIR, nor do they need to manage temporary files.

Changes v3 -> v4:
- fix root-dir in bzr-generated archives (Thomas DS)
- enhance the usage comment in every hepers (Thomas DS)
- typoes (Thomas DS)

Changes v2 -> v3:
- complete rewrite of the series

Regards,
Yann E. MORIN.

The following changes since commit 01370132c10cb2d194042e08fefbc9cec8392c66:

gst-fsl-plugins, libfslvpuwrap: add missing select (2014-08-03 14:47:03 +0200)

are available in the git repository at:

git://gitorious.org/buildroot/buildroot.git yem/check-downloads

for you to fetch changes up to d43ab6ef106a7a55948b533b22ad7215fc580c51:

support/download: convert wget to use the wrapper (2014-08-03 19:49:55 +0200)

----------------------------------------------------------------
Yann E. MORIN (9):
support/download: add download wrapper support/download: convert bzr to use the wrapper support/download: convert localfiles to use the wrapper support/download: convert cvs to use the wrapper support/download: convert git to use the wrapper support/download: convert Hg to use the wrapper support/download: convert scp to use the wrapper support/download: convert svn to use the wrapper support/download: convert wget to use the wrapper

 package/pkg-download.mk  | 50 ++++++++++++++++--------
 support/download/bzr     | 42 ++++++--------------
 support/download/cp      | 26 ++++---------
 support/download/cvs     | 54 ++++++++------------------
 support/download/git     | 62 ++++++++++--------------------
 support/download/hg      | 52 ++++++++-----------------
 support/download/scp     | 28 ++++----------
 support/download/svn     | 48 +++++++----------------
 support/download/wget    | 35 ++++-------------
 support/download/wrapper | 99 ++++++++++++++++++++++++++++++++++++++++++++++++
 10 files changed, 234 insertions(+), 262 deletions(-)
 create mode 100755 support/download/wrapper
Signature · 6 lines
.-----------------.--------------------.------------------.--------------------.
|  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
| +33 662 376 056 | Software  Designer | \ / CAMPAIGN     |  ___               |
| +33 223 225 172 `------------.-------:  X  AGAINST      |  \e/  There is no  |
| http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL    |   v   conspiracy.  |
'------------------------------^-------^------------------^--------------------'
#6 lore raw

PATCH v4 5/9 support/download: convert git to use the wrapper

This drastically simplifies the git helper, as it no longer has to deal with atomically saving the downloaded archive.

(Tested by running 'make fmc-fsl-sdk-source')

---
Changes v3 -> v4:
- tweak the usage comment (Thomas DS)

---
 package/pkg-download.mk |  7 ++++--
 support/download/git    | 62 ++++++++++++++++---------------------------------
 2 files changed, 25 insertions(+), 44 deletions(-)
package/pkg-download.mk +5 -2
index 66ce02c..1649c83 100644
@@ -95,8 +95,11 @@ endef
 # problems
 define DOWNLOAD_GIT
 	test -e $(DL_DIR)/$($(PKG)_SOURCE) || \
-	$(EXTRA_ENV) support/download/git $($(PKG)_SITE) $($(PKG)_DL_VERSION) \
-					  $($(PKG)_BASE_NAME) $(DL_DIR)/$($(PKG)_SOURCE)
+	$(EXTRA_ENV) support/download/wrapper git \
+		$(DL_DIR)/$($(PKG)_SOURCE) \
+		$($(PKG)_SITE) \
+		$($(PKG)_DL_VERSION) \
+		$($(PKG)_BASE_NAME)
 endef
 
 # TODO: improve to check that the given PKG_DL_VERSION exists on the remote
support/download/git +20 -42

80 lines not on this page · raw message

Signature · 1 line
1.9.1
#7 lore raw

PATCH v4 6/9 support/download: convert Hg to use the wrapper

In reply to Yann E. MORIN

This drastically simplifies the hg helper, as it no longer has to deal with atomically saving the downloaded archive.

(Tested by running 'make vim-source')

---
Changes v3 -> v4:
- tweak the usage comment (Thomas DS)

---
 package/pkg-download.mk |  7 +++++--
 support/download/hg     | 52 ++++++++++++++-----------------------------------
 2 files changed, 20 insertions(+), 39 deletions(-)
package/pkg-download.mk +5 -2
index 1649c83..7878c66 100644
@@ -184,8 +184,11 @@ endef
 
 define DOWNLOAD_HG
 	test -e $(DL_DIR)/$($(PKG)_SOURCE) || \
-	$(EXTRA_ENV) support/download/hg $($(PKG)_SITE) $($(PKG)_DL_VERSION) \
-					  $($(PKG)_BASE_NAME) $(DL_DIR)/$($(PKG)_SOURCE)
+	$(EXTRA_ENV) support/download/wrapper hg \
+		$(DL_DIR)/$($(PKG)_SOURCE) \
+		$($(PKG)_SITE) \
+		$($(PKG)_DL_VERSION) \
+		$($(PKG)_BASE_NAME)
 endef
 
 # TODO: improve to check that the given PKG_DL_VERSION exists on the remote
support/download/hg +15 -37

61 lines not on this page · raw message

Signature · 1 line
1.9.1
#8 lore raw

PATCH v4 7/9 support/download: convert scp to use the wrapper

In reply to Yann E. MORIN

This drastically simplifies the scp helper, as it no longer has to deal with atomically saving the downloaded archive.

(Tested by setting a primary site to 'scp://localhost:/tmp' and running 'make vim-source')

---
Changes v3 -> v4:
- tweak the usage comment (Thomas DS)

---
 package/pkg-download.mk |  5 +++--
 support/download/scp    | 28 ++++++++--------------------
 2 files changed, 11 insertions(+), 22 deletions(-)
package/pkg-download.mk +3 -2
index 7878c66..d749b54 100644
@@ -168,8 +168,9 @@ endef
 # to prepend the path with a slash: scp://[user@]host:/absolutepath
 define DOWNLOAD_SCP
 	test -e $(DL_DIR)/$(2) || \
-	$(EXTRA_ENV) support/download/scp '$(call stripurischeme,$(call qstrip,$(1)))' \
-					  $(DL_DIR)/$(2) && \
+	$(EXTRA_ENV) support/download/wrapper scp \
+		$(DL_DIR)/$(2) \
+		'$(call stripurischeme,$(call qstrip,$(1)))' && \
 	$(call VERIFY_HASH,$(PKGDIR)/$($(PKG)_NAME).hash,$(DL_DIR)/$(2))
 endef
 
support/download/scp +8 -20
index d3aad43..1676929 100755
@@ -1,28 +1,16 @@
 #!/bin/bash
 
-# We want to catch any command failure, and exit immediately
+# We want to catch any unexpected failure, and exit immediately
 set -e
 
-# Download helper for scp
-# Call it with:
-#   $1: URL
-#   $2: output file
+# Download helper for scp, to be called from the download wrapper script
+# Expected arguments:
+#   $1: output file
+#   $2: URL
 # And this environment:
 #   SCP       : the scp command to call
 
-url="${1}"
-output="${2}"
-tmp_dl="$( mktemp "${BUILD_DIR}/.XXXXXX" )"
-tmp_output="$( mktemp "${output}.XXXXXX" )"
+output="${1}"
+url="${2}"
 
-ret=1
-if ${SCP} "${url}" "${tmp_dl}"; then
-    if mv "${tmp_dl}" "${tmp_output}"; then
-        mv "${tmp_output}" "${output}"
-        ret=0
-    fi
-fi
-
-# Cleanup
-rm -f "${tmp_dl}" "${tmp_output}"
-exit ${ret}
+${SCP} "${url}" "${output}"
Signature · 1 line
1.9.1
#9 lore raw

PATCH v4 8/9 support/download: convert svn to use the wrapper

In reply to Yann E. MORIN

This drastically simplifies the svn helper, as it no longer has to deal with atomically saving the downloaded archive.

(Tested by running 'make open2300-source')

---
Changes v3 -> v4:
- tweak the usage comment (Thomas DS)

---
 package/pkg-download.mk |  7 +++++--
 support/download/svn    | 48 +++++++++++++-----------------------------------
 2 files changed, 18 insertions(+), 37 deletions(-)
package/pkg-download.mk +5 -2
index d749b54..c23dcfa 100644
@@ -151,8 +151,11 @@ endef
 
 define DOWNLOAD_SVN
 	test -e $(DL_DIR)/$($(PKG)_SOURCE) || \
-	$(EXTRA_ENV) support/download/svn $($(PKG)_SITE) $($(PKG)_DL_VERSION) \
-					  $($(PKG)_BASE_NAME) $(DL_DIR)/$($(PKG)_SOURCE)
+	$(EXTRA_ENV) support/download/wrapper svn \
+		$(DL_DIR)/$($(PKG)_SOURCE) \
+		$($(PKG)_SITE) \
+		$($(PKG)_DL_VERSION) \
+		$($(PKG)_BASE_NAME)
 endef
 
 define SOURCE_CHECK_SVN
support/download/svn +13 -35

57 lines not on this page · raw message

Signature · 1 line
1.9.1
#10 lore raw

PATCH v4 9/9 support/download: convert wget to use the wrapper

In reply to Yann E. MORIN

This drastically simplifies the wget helper, as it no longer has to deal with atomically saving the downloaded archive.

(Tested by running 'make busybox-source')

---
Changes v3 -> v4:
- tweak the usage comment (Thomas DS)

---
 package/pkg-download.mk |  4 +++-
 support/download/wget   | 35 ++++++++---------------------------
 2 files changed, 11 insertions(+), 28 deletions(-)
package/pkg-download.mk +3 -1
index c23dcfa..0761736 100644
@@ -208,7 +208,9 @@ endef
 
 define DOWNLOAD_WGET
 	test -e $(DL_DIR)/$(2) || \
-	$(EXTRA_ENV) support/download/wget '$(call qstrip,$(1))' $(DL_DIR)/$(2) && \
+	$(EXTRA_ENV) support/download/wrapper wget \
+		$(DL_DIR)/$(2) \
+		'$(call qstrip,$(1))' && \
 	$(call VERIFY_HASH,$(PKGDIR)/$($(PKG)_NAME).hash,$(DL_DIR)/$(2))
 endef
 
support/download/wget +8 -27
index cbeca3b..2cea100 100755
@@ -1,35 +1,16 @@
 #!/bin/bash
 
-# We want to catch any command failure, and exit immediately
+# We want to catch any unexpected failure, and exit immediately
 set -e
 
-# Download helper for wget
-# Call it with:
-#   $1: URL
-#   $2: output file
+# Download helper for wget, to be called from the download wrapper script
+# Expected arguments:
+#   $1: output file
+#   $2: URL
 # And this environment:
 #   WGET     : the wget command to call
-#   BUILD_DIR: path to Buildroot's build dir
 
-url="${1}"
-output="${2}"
+output="${1}"
+url="${2}"
 
-tmp_dl="$( mktemp "${BUILD_DIR}/.XXXXXX" )"
-tmp_output="$( mktemp "${output}.XXXXXX" )"
-
-# Play tic-tac-toe with temp files
-# - first, we download to a trashable location (the build-dir)
-# - then we copy to a temporary tarball in the final location, so it is
-#   on the same filesystem as the final file
-# - finally, we atomically rename to the final file
-
-ret=1
-if ${WGET} -O "${tmp_dl}" "${url}"; then
-    if mv "${tmp_dl}" "${tmp_output}"; then
-        mv "${tmp_output}" "${output}"
-        ret=0
-    fi
-fi
-
-rm -f "${tmp_dl}" "${tmp_output}"
-exit ${ret}
+${WGET} -O "${output}" "${url}"
Signature · 1 line
1.9.1
#2 lore raw

PATCH v4 1/9 support/download: add download wrapper

In reply to Yann E. MORIN

"Yann E. MORIN" <[email protected]> schreef:

Quoted 113 lines

The download wrapper is responsible for ensuring the atomicity of saving into $(BR2_DL_DIR).

It calls the appropriate download helper, telling it to save the downloaded content to a temporary file in $(BUILD_DIR) (so it does not clutter $(BR2_DL_DIR) with partial, failed downloads.

Then, only if the download helper was successful, does the wrapper save the downloaded content to the final location, yet still in a temporary file, and finally atomically renames it to the final output file.

---
support/download/wrapper | 99 ++++++++++++++++++++++++++++++++++++++++++++++++

1 file changed, 99 insertions(+)

create mode 100755 support/download/wrapper

support/download/wrapper new file +89 -0
new file mode 100755
index 0000000..8ae2797
@@ -0,0 +1,99 @@
+#!/bin/bash
+
+# This script is a wrapper to the other download helpers.
+# Its role is to ensure atomicity when saving downloaded files
+# back to BR2_DL_DIR, and not clutter BR2_DL_DIR with partial,
+# failed downloads.
+#
+# Call it with:
+#   $1: name of the helper (eg. cvs, git, cp...)
+#   $2: full path to the file in which to save the download
+#   $*: additional arguments to the helper in $1
+# Environment:
+#   BUILD_DIR: the path to Buildroot's build dir
+
+# To avoid cluttering BR2_DL_DIR, we download to a trashable
+# location, namely in $(BUILD_DIR).
+# Then, we move the downloaded file to a temporary file in the
+# same directory as the final output file.
+# This allows us to finally atomically rename it to its final
+# name.
+# If anything goes wrong, we just remove all the temporaries
+# created so far.
+
+# We want to catch any unexpected failure, and exit immediately.
+set -e
+
+helper="${1}"
+output="${2}"
+shift 2
+
+# tmpd is a temporary directory in which helpers may store intermediate
+# by-products of the download.
+# tmpf is the file in which the helpers should put the downloaded content.
+# tmpd is located in $(BUILD_DIR), so as not to clutter the (precious)
+# $(BR2_DL_DIR)
+# We let the helpers create tmpf, so they are able to set whatever
+# permission bits they want (although we're only really interested in
+# the executable bit.)
+tmpd="$( mktemp -d "${BUILD_DIR}/.${output##*/}.XXXXXX" )"
+tmpf="${tmpd}/output"
+
+# Helpers expect to run in a directory that is *really* trashable, so
+# they are free to create whatever files and/or sub-dirs they might need.
+# Doing the 'cd' here rather than in all helpers is easier.
+cd "${tmpd}"
+
+# If the helper fails, we can just remove the temporary directory to
+# remove all the cruft it may have left behind. Then we just exit in
+# error too.
+if ! "${OLDPWD}/support/download/${helper}" "${tmpf}" "${@}"; then
+    rm -rf "${tmpd}"
+    exit 1
+fi
+
+# cd back to free the temp-dir, so we can remove it later
+cd "${OLDPWD}"
+
+# tmp_output is in the same directory as the final output, so we can
+# later move it atomically.
+tmp_output="$( mktemp "${output}.XXXXXX" )"
+
+# 'mktemp' creates files with 'go=-rwx', so the files are not accessible
+# to users other than the one doing the download (and root, of course).
+# This can be problematic when a shared BR2_DL_DIR is used by different
+# users (e.g. on a build server), where all users may write to the shared
+# location, since other users would not be allowed to read the files
+# another user downloaded.
+# So, we restore the 'go' access rights to a more sensible value, while
+# still abiding by the current user's umask. We must do that before the
+# final 'mv', so just do it now.
+# Some helpers (cp and scp) may create executable files, so we need to
+# carry the executable bit if needed.
+[ -x "${tmpf}" ] && new_mode=755 || new_mode=644
+new_mode=$( printf "%04o" $((0${new_mode} & ~0$(umask))) )
+chmod ${new_mode} "${tmp_output}"
+
+# We must *not* unlink tmp_output, otherwise there is a small window
+# during which another download process may create the same tmp_output
+# name (very, very unlikely; but not impossible.)
+# Using 'cp' is not reliable, since 'cp' may unlink the destination file
+# if it is unable to open it with O_WRONLY|O_TRUNC; see:
+#   http://pubs.opengroup.org/onlinepubs/9699919799/utilities/cp.html
+# Since the destination filesystem can be anything, it might not support
+# O_TRUNC, so 'cp' would unlink it first.
+# Use 'cat' and append-redirection '>>' to save to the final location,
+# since that is the only way we can be 100% sure of the behaviour.
+if ! cat "${tmpf}" >>"${tmp_output}"; then
+    rm -rf "${tmpd}" "${tmp_output}"
+    exit 1

support/download/wrapper line 99

Quoted 10 lines
support/download/wrapper new file +10 -0
+fi
+rm -rf "${tmpd}"
+# tmp_output and output are on the same filesystem, so POSIX guarantees
+# that 'mv' is atomic, because it then uses rename() that POSIX mandates
+# to be atomic, see:
+#   http://pubs.opengroup.org/onlinepubs/9699919799/functions/rename.html
+if ! mv "${tmp_output}" "${output}"; then
+    rm -f "${tmp_output}"
+    exit 1
+fi
#11 lore raw

v4 cover Cleanups in download helpers (branch yem/check-downloads)

In reply to Yann E. MORIN

Hello,

Quoted 10 lines · Yann E. MORIN

Yann E. MORIN (9):
support/download: add download wrapper support/download: convert bzr to use the wrapper support/download: convert localfiles to use the wrapper support/download: convert cvs to use the wrapper support/download: convert git to use the wrapper support/download: convert Hg to use the wrapper support/download: convert scp to use the wrapper support/download: convert svn to use the wrapper support/download: convert wget to use the wrapper

Series applied. Thanks for your work, and thanks a lot to Thomas DS for the testing.

I've also tested it here locally with a few packages (using wget, git, bzr and svn) while pointing to an empty BR2_DL_DIR, and it worked fine. Let's see if it survives the autobuilder testing :)

Thanks,

Thomas

Signature · 3 lines
Thomas Petazzoni, CTO, Free Electrons
Embedded Linux, Kernel and Android engineering
http://free-electrons.com