Skip to content
Snippets Groups Projects
Commit 90eb8392 authored by andi's avatar andi
Browse files

added file locking support

darcs-hash:20050626161253-9977f-5600ca7134aa7244b08407d5a44c065a34091f20.gz
parent 896a5c22
No related branches found
No related tags found
No related merge requests found
......@@ -57,6 +57,7 @@ function io_readFile($file){
*/
function io_saveFile($file,$content){
io_makeFileDir($file);
io_lock($file);
if(substr($file,-3) == '.gz'){
$fh = @gzopen($file,'wb9');
if(!$fh){
......@@ -74,9 +75,53 @@ function io_saveFile($file,$content){
fwrite($fh, $content);
fclose($fh);
}
io_unlock($file);
return true;
}
/**
* Tries to lock a file
*
* Locking is only done for io_savefile and uses directories
* inside $conf['lockdir']
*
* It waits maximal 3 seconds for the lock, after this time
* the lock is assumed to be stale and the function goes on
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
function io_lock($file){
global $conf;
// no locking if safemode hack
if($conf['safemodehack']) return;
$lockDir = $conf['lockdir'].'/'.md5($file);
@ignore_user_abort(1);
$timeStart = time();
do {
//waited longer than 3 seconds? -> stale lock
if ((time() - $timeStart) > 3) break;
$locked = @mkdir($lockDir);
} while ($locked === false);
}
/**
* Unlocks a file
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
function io_unlock($file){
global $conf;
// no locking if safemode hack
if($conf['safemodehack']) return;
$lockDir = $conf['lockdir'].'/'.md5($file);
@rmdir($lockDir);
@ignore_user_abort(0);
}
/**
* Create the directory needed for the given file
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment