summaryrefslogtreecommitdiff
path: root/inc/io.php
diff options
context:
space:
mode:
authorandi <andi@splitbrain.org>2005-06-26 18:12:53 +0200
committerandi <andi@splitbrain.org>2005-06-26 18:12:53 +0200
commit90eb8392cdad2d4e8e8b8b6731f5400c37849ed3 (patch)
treed4b20b276b11d2f1582192e1c809cbd373b1874f /inc/io.php
parent896a5c22ad2bfe6b07b70324ed639fbaf9a20869 (diff)
downloadrpg-90eb8392cdad2d4e8e8b8b6731f5400c37849ed3.tar.gz
rpg-90eb8392cdad2d4e8e8b8b6731f5400c37849ed3.tar.bz2
added file locking support
darcs-hash:20050626161253-9977f-5600ca7134aa7244b08407d5a44c065a34091f20.gz
Diffstat (limited to 'inc/io.php')
-rw-r--r--inc/io.php45
1 files changed, 45 insertions, 0 deletions
diff --git a/inc/io.php b/inc/io.php
index a3e5f85c4..4e6faefa8 100644
--- a/inc/io.php
+++ b/inc/io.php
@@ -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,10 +75,54 @@ 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
*
* @author Andreas Gohr <andi@splitbrain.org>