From 77ed6c4e2492c8a9227925d6227d28f3d741d948 Mon Sep 17 00:00:00 2001
From: Christian Schneider
Date: Tue, 1 Mar 2011 13:07:24 +0000
Subject: Handle keepfailed when blocked process discovers that file could not
 be fetched

---
 it_url.class | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/it_url.class b/it_url.class
index 76d9203..c5252aa 100644
--- a/it_url.class
+++ b/it_url.class
@@ -405,17 +405,19 @@ function get_cache($p = array())
 
 	if ($filemtime = it_url::_expired($path, $p['maxage']))	# Outdated(non-zero int) or non-existant(true)?
 	{
+		$fileexists = $filemtime !== true;
+
 		if ($lock = it_url::_lock($path))
 		{
 			# Touch existing file to prevent locking other getters while refreshing
-			if ($filemtime !== true)
+			if ($fileexists)
 				touch($path);
 
 			EDC('getcache', "new", $filemtime, $p['url'], $path);
 			if ($result = it_url::get($p + array('filemtime' => EDC('nocache') ? null : $filemtime)))	# => true means not modified (no new data fetched)
 				$newfile = it_url::_atomicwrite($path, $result);
 			else if ($p['keepfailed'])
-				$result = file_exists($path);
+				$result = $fileexists;
 			else
 				@unlink($path);	# Expired and failed to get
 
@@ -426,6 +428,11 @@ function get_cache($p = array())
 			# Wait for file currently being transferred
 			EDC('getcache', "wait", $p['url'], $path);
 			$result = it_url::_waitforlockedfile($path, $p);
+
+			# If file could no be fetched by other thread but exists and we are in keepfailed mode then return old file
+			if (!$result && $p['keepfailed'])
+				$result = $fileexists;
+
 		}
 	}
 	else
-- 
cgit v1.2.3