[Phpfreechat-svn] SF.net SVN: phpfreechat: [1064] trunk/src
Status: Beta
Brought to you by:
kerphi
From: <gpi...@us...> - 2007-07-31 20:02:52
|
Revision: 1064 http://phpfreechat.svn.sourceforge.net/phpfreechat/?rev=1064&view=rev Author: gpinzone Date: 2007-07-31 13:02:55 -0700 (Tue, 31 Jul 2007) Log Message: ----------- Eliminated flock_get_contents and flock_put_contents. Created patched version of file_get_contents that include file locking: file_get_contents_flock. Modified setMeta and getMeta to use file_get_contents_flock and file_put_contents. Rewrote setMeta's null $leafvalue handling to use file_put_contents. Modified Paths: -------------- trunk/src/containers/file.class.php trunk/src/pfctools.php Modified: trunk/src/containers/file.class.php =================================================================== --- trunk/src/containers/file.class.php 2007-07-30 14:43:40 UTC (rev 1063) +++ trunk/src/containers/file.class.php 2007-07-31 20:02:55 UTC (rev 1064) @@ -85,13 +85,11 @@ $leafexists = file_exists($leaffilename); if ($leafvalue == NULL) { - if (file_exists($leaffilename) && - filesize($leaffilename)>0) unlink($leaffilename); - touch($leaffilename); + file_put_contents($leaffilename, '', LOCK_EX); } else { - flock_put_contents($leaffilename, $leafvalue); + file_put_contents($leaffilename, $leafvalue, LOCK_EX); } // store the value in the memory cache @@ -157,7 +155,7 @@ if (!file_exists($leaffilename)) return $ret; if ($withleafvalue) - $ret["value"][] = flock_get_contents($leaffilename); + $ret["value"][] = file_get_contents_flock($leaffilename); else $ret["value"][] = NULL; $ret["timestamp"][] = filemtime($leaffilename); Modified: trunk/src/pfctools.php =================================================================== --- trunk/src/pfctools.php 2007-07-30 14:43:40 UTC (rev 1063) +++ trunk/src/pfctools.php 2007-07-31 20:02:55 UTC (rev 1064) @@ -255,8 +255,42 @@ return $errors; } +/** + * file_get_contents_flock + * define an alternative file_get_contents when this function doesn't exists on the used php version (<4.3.0) + */ +if (!defined('LOCK_SH')) { + define('LOCK_SH', 1); +} +function file_get_contents_flock($filename, $incpath = false, $resource_context = null) +{ + if (false === $fh = fopen($filename, 'rb', $incpath)) { + user_error('file_get_contents() failed to open stream: No such file or directory ['.$filename.']', + E_USER_WARNING); + return false; + } + + // Attempt to get a shared (read) lock + if (!flock($fh, LOCK_SH)) { + return false; + } + + clearstatcache(); + if ($fsize = @filesize($filename)) { + $data = fread($fh, $fsize); + } else { + $data = ''; + while (!feof($fh)) { + $data .= fread($fh, 8192); + } + } + + fclose($fh); + return $data; +} + /** * file_get_contents * define an alternative file_get_contents when this function doesn't exists on the used php version (<4.3.0) @@ -267,7 +301,7 @@ { if (false === $fh = fopen($filename, 'rb', $incpath)) { - trigger_error('file_get_contents() failed to open stream: No such file or directory', E_USER_WARNING); + trigger_error('file_get_contents() failed to open stream: No such file or directory ['.$filename.']', E_USER_WARNING); return false; } clearstatcache(); @@ -403,40 +437,4 @@ } } -/** - * Almost the Same as file_get_contents except that it uses flock() to lock the file - */ -function flock_get_contents($filename, $mode = "rb") -{ - $data = ''; - $fp = fopen( $filename, $mode ); - if( $fp && flock( $fp, LOCK_SH ) ) - { - clearstatcache(); - $size = filesize($filename); - if ($size > 0) - $data = fread( $fp, $size ); - // flock($fp, LOCK_UN); // will be done by fclose - } - fclose( $fp ); - return $data; -} - -/** - * Almost the Same as file_put_contents except that it uses flock() to lock the file - */ -function flock_put_contents($filename, $data, $mode = "wb") -{ - $fp = fopen( $filename, $mode ); - if( $fp ) - { - if ( flock( $fp, LOCK_EX ) ) - { - fwrite( $fp, $data ); - // flock($fp, LOCK_UN); // will be done by fclose - } - fclose( $fp ); - } -} - -?> +?> \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |