[Phpfreechat-svn] SF.net SVN: phpfreechat: [1092] trunk/src/containers/file.class.php
Status: Beta
Brought to you by:
kerphi
From: <gpi...@us...> - 2007-08-06 19:59:51
|
Revision: 1092 http://phpfreechat.svn.sourceforge.net/phpfreechat/?rev=1092&view=rev Author: gpinzone Date: 2007-08-06 12:59:53 -0700 (Mon, 06 Aug 2007) Log Message: ----------- Added pushMeta and popMeta for future enhancement to AppendCmdToPlay and RunPendingCmdToPlay functions. Modified Paths: -------------- trunk/src/containers/file.class.php Modified: trunk/src/containers/file.class.php =================================================================== --- trunk/src/containers/file.class.php 2007-08-06 19:55:21 UTC (rev 1091) +++ trunk/src/containers/file.class.php 2007-08-06 19:59:53 UTC (rev 1092) @@ -217,6 +217,127 @@ return $ret; } + + function popMeta($group, $subgroup, $leaf) + { + $c =& pfcGlobalConfig::Instance(); + if ($c->debug) + file_put_contents("/tmp/debug", "\npopMeta(".$group.",".$subgroup.",".$leaf.")", FILE_APPEND | LOCK_EX); + + // create directories + $dir_base = $c->container_cfg_server_dir; + $dir = $dir_base.'/'.$group.'/'.$subgroup; + if (!is_dir($dir)) mkdir_r($dir); + + // create or replace metadata file + $leaffilename = $dir."/".$leaf; + + // create return array + $ret = array(); + $ret["timestamp"] = array(); + $ret["value"] = array(); + + // read and increment data from metadata file + clearstatcache(); + if (file_exists($leaffilename)) + { + $fh = fopen($leaffilename, 'r+'); + for($i = 0; $i < 10; $i++) // Try 10 times until an exclusive lock can be obtained + { + if (flock($fh, LOCK_EX)) + { + $ret["value"][] = chop(fread($fh, filesize($leaffilename))); + $ret["timestamp"][] = filemtime($leaffilename); + $leafvalue = $ret; + array_pop($leafvalue); + // check if array is now empty + if (count($leafvalue) == 0) + { + unlink($leaffilename); + break; + } + rewind($fh); + fwrite($fh, $leafvalue); + fflush($fh); + ftruncate($fh, ftell($fh)); + flock($fh, LOCK_UN); + break; + } + // If flock is working properly, this will never be reached + $delay = rand(0, pow(2, ($i+1)) - 1) * 5000; // Exponential backoff + usleep($delay); + } + fclose($fh); + } + else + { + return $ret; + } + + $ret["value"][] = $leafvalue; + $ret["timestamp"][] = filemtime($leaffilename); + + return $ret; + } + + + function pushMeta($group, $subgroup, $leaf, $leafvalue = NULL) + { + $c =& pfcGlobalConfig::Instance(); + if ($c->debug) + file_put_contents("/tmp/debug", "\npushMeta(".$group.",".$subgroup.",".$leaf.",".$leafvalue.")", FILE_APPEND | LOCK_EX); + + // create directories + $dir_base = $c->container_cfg_server_dir; + $dir = $dir_base.'/'.$group.'/'.$subgroup; + if (!is_dir($dir)) mkdir_r($dir); + + // create or replace metadata file + $leaffilename = $dir."/".$leaf; + + // create return array + $ret = array(); + $ret["timestamp"] = array(); + $ret["value"] = array(); + + // read and increment data from metadata file + clearstatcache(); + if ( $leafexists = file_exists($leaffilename) ) + { + $fh = fopen($leaffilename, 'r+'); + for($i = 0; $i < 10; $i++) // Try 10 times until an exclusive lock can be obtained + { + if (flock($fh, LOCK_EX)) + { + if ( $leafvalue == NULL ) $leafvalue = ''; + $leafvaltmp = chop(fread($fh, filesize($leaffilename))); + array_push($leafvaltmp, $leafvalue); + rewind($fh); + fwrite($fh, $leafvaltmp); + fflush($fh); + ftruncate($fh, ftell($fh)); + flock($fh, LOCK_UN); + break; + } + // If flock is working properly, this will never be reached + $delay = rand(0, pow(2, ($i+1)) - 1) * 5000; // Exponential backoff + usleep($delay); + } + fclose($fh); + } + else + { + file_put_contents($leaffilename, $leafvalue, LOCK_EX); + } + + if ($leafexists) + return 1; // value overwritten + else + return 0; // value created + } + + + function rmMeta($group, $subgroup = null, $leaf = null) { $c =& pfcGlobalConfig::Instance(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |