changeset 15:72316564535a

wget.cache.wrapper: Fix racecondition and download a new copy if the URL changes. 2011-03-02 Xerxes Ranby <xerxes@zafena.se> * buildslave/tools/wget.cache.wrapper Fix racecondition when two or more builders are downloading the same file. Download a new copy if the URL changes.
author Xerxes R?nby <xerxes@zafena.se>
date Wed, 02 Mar 2011 11:45:22 +0100
parents 71feefad7baa
children 64749190d772
files buildslave/tools/wget.cache.wrapper
diffstat 1 files changed, 16 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/buildslave/tools/wget.cache.wrapper	Fri Feb 25 00:51:44 2011 +0100
+++ b/buildslave/tools/wget.cache.wrapper	Wed Mar 02 11:45:22 2011 +0100
@@ -11,7 +11,7 @@
 # wget foo -O files
 
 # This wget cache wrapper are currently being run on the buildslaves 
-# jaunty-ia32, natty-armv7l and squeeze-armv7l connected to the
+# jaunty-ia32, natty-armv7l and squeeze-armv5tel connected to the
 # http://builder.classpath.org/icedtea/buildbot/ buildmaster 
 # to prevent the buildslaves to re-download all openjdk drops for each build
 
@@ -23,34 +23,41 @@
 ##### Please do not change anything below this line
 
 TARGETDIR=$(pwd)
+PID=$$
 
 #remeber all arguments to wget
 ALLARG=$*
 
 #find out if the last argument are -O file
 FILEINDEX=$#
-shift $((${FILEINDEX}-2))
-ARG=$1
-FILE=$2
+shift $((${FILEINDEX}-3))
+URL=`echo $1|tr -d '/?;'`
+ARG=$2
+FILE=$3
 
 if [ ${ARG} == "-O" ] ; then
-  if [ ! -e ${CACHEDIR}${FILE} ]; then
+  if [ ! -e ${CACHEDIR}${URL}/${FILE} ]; then
     # file do not exist in cache
     # download the file to the cache using wget!
-    cd ${CACHEDIR}
 
     # make sure subdirs in the cache exist
     CACHEDIRSUBPATH=$(dirname ${FILE})
-    mkdir -p ${CACHEDIRSUBPATH}
+    mkdir -p ${CACHEDIR}${PID}/${URL}/${CACHEDIRSUBPATH}
+    mkdir -p ${CACHEDIR}${URL}/${CACHEDIRSUBPATH}
 
+    # download the file to the current pids cache directory.
+    cd ${CACHEDIR}${PID}/${URL}
     ${WGET} ${ALLARG}
+
+    # atomically move the downloaded file to the main cache.
+    mv ${CACHEDIR}${PID}/${URL}/${FILE} ${CACHEDIR}${URL}/
   fi
 
-  if [ -e ${CACHEDIR}${FILE} ] ; then
+  if [ -e ${CACHEDIR}${URL}/${FILE} ] ; then
     # ok good, file exist now in cache
     # copy the file from cache to destination
     cd ${TARGETDIR}
-    cp ${CACHEDIR}${FILE} ${FILE}
+    cp ${CACHEDIR}${URL}/${FILE} ${FILE}
     exit 0
   else
     #something got wrong