From patchwork Sun Jul 20 22:42:19 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Yann E. MORIN" X-Patchwork-Id: 371945 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from silver.osuosl.org (silver.osuosl.org [140.211.166.136]) by ozlabs.org (Postfix) with ESMTP id 1BD221400D7 for ; Mon, 21 Jul 2014 08:42:44 +1000 (EST) Received: from localhost (localhost [127.0.0.1]) by silver.osuosl.org (Postfix) with ESMTP id 5E816316C1; Sun, 20 Jul 2014 22:42:43 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from silver.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id To+Eb-+snwUn; Sun, 20 Jul 2014 22:42:39 +0000 (UTC) Received: from ash.osuosl.org (ash.osuosl.org [140.211.166.34]) by silver.osuosl.org (Postfix) with ESMTP id 129FB313A6; Sun, 20 Jul 2014 22:42:38 +0000 (UTC) X-Original-To: buildroot@lists.busybox.net Delivered-To: buildroot@osuosl.org Received: from silver.osuosl.org (silver.osuosl.org [140.211.166.136]) by ash.osuosl.org (Postfix) with ESMTP id E067F1BFA7F for ; Sun, 20 Jul 2014 22:42:33 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by silver.osuosl.org (Postfix) with ESMTP id D6E2825A64 for ; Sun, 20 Jul 2014 22:42:33 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from silver.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id n6CO+Aaksulb for ; Sun, 20 Jul 2014 22:42:32 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-we0-f174.google.com (mail-we0-f174.google.com [74.125.82.174]) by silver.osuosl.org (Postfix) with ESMTPS id 4102130A73 for ; Sun, 20 Jul 2014 22:42:31 +0000 (UTC) Received: by mail-we0-f174.google.com with SMTP id x48so6781161wes.5 for ; Sun, 20 Jul 2014 15:42:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=2xegNRkLEyDcai5VoeULhxOjDDG5KIjkrMtvguAwweE=; b=uqCqNaxDwgRkqiA8ILetm307aA+VHrn49a5ROdVexbcsh5y88ti/hskXsrzcFCEfB1 srPA1ypnr48WH27ro5os9cfG6y4ObiC0l5e4PLzJa5K8hBUjhp0pLmeI0yT0RYIqW+xJ vEr7Z0cJ5wYQ+Klt90EJtHoJqygZBxDatNWLnOUG3zq1QpLCR+2KVDPMKjaQaf5h1/BZ JrRNGVHl61aqnf7HCmijZ0k0suioUHb7Xoy52i36+9aAaprT0QCTB8vUYCtcOhClQBdL +r8i5w+d+xh8DwMoTNDFydRuUUv7nTiZtmC0U5QzAowg8ojfn0DRzEVXYh663gSQjX1E EUmQ== X-Received: by 10.194.83.39 with SMTP id n7mr17343704wjy.58.1405896149675; Sun, 20 Jul 2014 15:42:29 -0700 (PDT) Received: from gourin.bzh.lan (ks3095497.kimsufi.com. [94.23.60.27]) by mx.google.com with ESMTPSA id ft17sm32902084wjc.14.2014.07.20.15.42.28 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 20 Jul 2014 15:42:28 -0700 (PDT) From: "Yann E. MORIN" To: buildroot@buildroot.org Date: Mon, 21 Jul 2014 00:42:19 +0200 Message-Id: X-Mailer: git-send-email 1.9.1 In-Reply-To: References: Cc: "Yann E. MORIN" Subject: [Buildroot] [PATCH 1/9 v3] support/download: add download wrapper X-BeenThere: buildroot@busybox.net X-Mailman-Version: 2.1.14 Precedence: list List-Id: Discussion and development of buildroot List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: buildroot-bounces@busybox.net Sender: buildroot-bounces@busybox.net 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 it the download helper was successful, does the wrapper saves the downloaded content to the final location, yet still in a teporary file, and finally atomically renames it to the final output file. Signed-off-by: "Yann E. MORIN" --- support/download/wrapper | 99 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100755 support/download/wrapper diff --git a/support/download/wrapper b/support/download/wrapper new file mode 100755 index 0000000..bc0d486 --- /dev/null +++ b/support/download/wrapper @@ -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 downloaed 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 located in $(BUILD_DIR), so as not to cluter 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