Skip to content

Commit

Permalink
Merge pull request #407 from zecrazytux/fixracecondition
Browse files Browse the repository at this point in the history
[-] Fixes a race condition
  • Loading branch information
rGaillard committed Apr 26, 2013
2 parents ea6b4ea + 2ac6e52 commit 60ea65d
Showing 1 changed file with 7 additions and 21 deletions.
28 changes: 7 additions & 21 deletions classes/Autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,27 +136,13 @@ public function generateIndex()
}
else
{
// Let's write index content in cache file
// In order to be sure that this file is correctly written, a check is done on the file content
$loop_protection = 0;
do
{
$integrity_is_ok = false;
file_put_contents($filename, $content, LOCK_EX);
if ($loop_protection++ > 10)
break;

// If the file content end with PHP tag, integrity of the file is ok
if (preg_match('#\?>\s*$#', file_get_contents($filename)))
$integrity_is_ok = true;
}
while (!$integrity_is_ok);

if (!$integrity_is_ok)
{
file_put_contents($filename, '<?php return array(); ?>', LOCK_EX);
// Cannot use PrestaShopException in this context
die('Your file '.$filename.' is corrupted. Please remove this file, a new one will be regenerated automatically');
$filename_tmp = tempnam(dirname($filename), basename($filename.'.'));
if($filename_tmp !== FALSE and file_put_contents($filename_tmp, $content, LOCK_EX) !== FALSE) {
rename($filename_tmp, $filename);
} else {
// $filename_tmp couldn't be written. $filename should be there anyway (even if outdated),
// no need to die.
error_log('Cannot write temporary file '.$filename_tmp);
}
}

Expand Down

0 comments on commit 60ea65d

Please sign in to comment.