#24677 [Asn->Fbk]: [PEAR] File::readAll() does not close file handle after reading

From: Date: Mon, 16 May 2005 10:04:55 +0000
Subject: #24677 [Asn->Fbk]: [PEAR] File::readAll() does not close file handle after reading
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-37671@lists.php.net to get a copy of this message
ID: 24677 Updated by: tony2001@php.net Reported By: pjones at ciaweb dot net -Status: Assigned +Status: Feedback Bug Type: PEAR related Operating System: OpenBSD 3.1 PHP Version: Irrelevant Assigned To: coredumb New Comment: Please try using this CVS snapshot: http://snaps.php.net/php4-STABLE-latest.tar.gz For Windows: http://snaps.php.net/win32/php4-win32-STABLE-latest.zip Previous Comments: ------------------------------------------------------------------------ [2004-08-02 23:01:59] c0254328 at hud dot ac dot uk Anybody managed to fix this problem. I have just come accross this myself. I tried the patch too with no success. ------------------------------------------------------------------------ [2003-09-30 13:40:26] pmjones at ciaweb dot net Sorry to take so long to get back to you. Applied the patch, does not appear to work. Using File::readAll() on a file the first time gives a notice "Undefined variable 'lock' at line 360 in File.php'" but reads the file OK. Using it subsequently on the same file, it goes into an infinite loop with "WARNING [feof(): 7 is not a valid File-Handle resource] in file [/usr/local/ share/pear/File.php] at line [209]". ------------------------------------------------------------------------ [2003-07-31 04:24:57] nicos@php.net That should fix it? Can anyone take a look at it? Index: File.php =================================================================== RCS file: /repository/pear/File/File.php,v retrieving revision 1.24 diff -u -u -r1.24 File.php --- File.php 28 Jan 2003 11:19:27 -0000 1.24 +++ File.php 31 Jul 2003 09:23:38 -0000 @@ -173,7 +173,7 @@ } $file .= $tmp; } - + File::close($filename); return $file; } @@ -352,15 +352,21 @@ * @param string $mode Mode the file was opened in * @return mixed PEAR Error on error, true otherwise */ - function close($filename, $mode) + function close($filename, $mode = FILE_MODE_READ) { - if (!PEAR::isError($fp = &File::_getFilePointer($filename, $mode))) { - $filePointers = &PEAR::getStaticProperty('File', 'filePointers'); + $filePointers = &PEAR::getStaticProperty('File', 'filePointers'); + + if (!isset($filePointers[$filename]) OR !is_resource($filePointers[$filename])) { + if (PEAR::isError($fp = &File::_getFilePointer($filename, $mode, $lock))) { + return $fp; + } + } else { + $fp = &$filePointers[$filename]; + } + if(isset($filePointers[$filename][$mode])) { unset($filePointers[$filename][$mode]); - return fclose($fp) ? true : PEAR::raiseError('Failed to close file: ' . $filename); } - - return $fp; + return fclose($fp) ? true : PEAR::raiseError('Failed to close file: ' . $filename); } /** ------------------------------------------------------------------------ [2003-07-16 09:08:18] pjones at ciaweb dot net Description: ------------ When using File::readAll() to read the entire contents of the same file more than once in the same script, the first readAll() returns the contents properly, but subsequent readAll() calls do not. I think this is because readAll() does not close the file handle when the method is complete (it leaves the file handle open, thus locking the file off from subsequent access). ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=24677&edit=1

« previous php.pear.dev (#37671) next »