Re: [PHP] Debugging custom streams
Jared Williams wrote: Hi, Short version, is there any way of listing all open resources from within a PHP script? Think I may have a problem relating to the object/resource shutdown order within PHP, but cant see which custom stream handler still has an open resource. Long version... I've been attempting to write a Zip archive class, which uses streams for providing the individual files within the archive. Gotten to the point where this code works as expected, producing a valid zip file, with 1 deflated file named digits.txt with 1234567890 as contents. $zip = new ZipArchive('test.zip', 'w+'); $stream = $zip-create('digits.txt'); fputs($stream, '1234567890'); fclose($stream); $zip-close(); unset($zip); The problem is once go beyond 1 file per zip, as in $zip = new ZipArchive('test.zip', 'w+'); $stream = $zip-create('digits.txt'); fputs($stream, '1234567890'); fclose($stream); $stream = $zip-create('alpha.txt'); fputs($stream, 'abcdefghijklmnopqrstuvwxyz'); fclose($stream); $zip-close(); I don't suppose that your closing a stream twice? [ once with fclose() and one inside $zip-close() ] unset($zip); I get an application exception on PHP shutdown, though the zip file is valid with 2 files. what exactly is the exception? Jared -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Debugging custom streams
-Original Message- From: Jochem Maas [mailto:[EMAIL PROTECTED] Sent: 10 February 2006 14:17 To: [EMAIL PROTECTED] Cc: php-general@lists.php.net Subject: Re: [PHP] Debugging custom streams Jared Williams wrote: Hi, Short version, is there any way of listing all open resources from within a PHP script? Think I may have a problem relating to the object/resource shutdown order within PHP, but cant see which custom stream handler still has an open resource. Long version... I've been attempting to write a Zip archive class, which uses streams for providing the individual files within the archive. Gotten to the point where this code works as expected, producing a valid zip file, with 1 deflated file named digits.txt with 1234567890 as contents. $zip = new ZipArchive('test.zip', 'w+'); $stream = $zip-create('digits.txt'); fputs($stream, '1234567890'); fclose($stream); $zip-close(); unset($zip); The problem is once go beyond 1 file per zip, as in $zip = new ZipArchive('test.zip', 'w+'); $stream = $zip-create('digits.txt'); fputs($stream, '1234567890'); fclose($stream); $stream = $zip-create('alpha.txt'); fputs($stream, 'abcdefghijklmnopqrstuvwxyz'); fclose($stream); $zip-close(); I don't suppose that your closing a stream twice? [ once with fclose() and one inside $zip-close() ] $zip-close() doesn't (yet) ensure that internal streams are closed. So don't think it can be that. unset($zip); I get an application exception on PHP shutdown, though the zip file is valid with 2 files. what exactly is the exception? --- php.exe - Application Error --- The instruction at 0x100ac22f referenced memory at 0x004c. The memory could not be read. Click on OK to terminate the program Click on CANCEL to debug the program --- OK Cancel --- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Debugging custom streams
Jared Williams wrote: -Original Message- From: Jochem Maas [mailto:[EMAIL PROTECTED] Sent: 10 February 2006 14:17 To: [EMAIL PROTECTED] Cc: php-general@lists.php.net Subject: Re: [PHP] Debugging custom streams Jared Williams wrote: Hi, Short version, is there any way of listing all open resources from within a PHP script? Think I may have a problem relating to the object/resource shutdown order within PHP, but cant see which custom stream handler still has an open resource. why not let your zip class keep a list of opened streams (I can't imagine that it's not doing that already) and write a function which will output a list of 'registered' stream resources that are still open [mis]using ftell() or feof() to check which streams are still 'active'. ... I don't suppose that your closing a stream twice? [ once with fclose() and one inside $zip-close() ] $zip-close() doesn't (yet) ensure that internal streams are closed. So don't think it can be that. was worth a guess :-) unset($zip); I get an application exception on PHP shutdown, though the zip file is valid with 2 files. what exactly is the exception? --- php.exe - Application Error --- The instruction at 0x100ac22f referenced memory at 0x004c. The memory could not be read. Click on OK to terminate the program Click on CANCEL to debug the program --- OK Cancel --- I have the same thing occur on windows machines with some setups (running certain code) - and the same situation where the code seems to work regardless of the error. I would suspect that it's an internal 'bug' and that there is nothing you can do at the php level to fix it (at least there is nothing you should need to do - made there _is_ actually something you can do at the php level). have you tried running the code on a linux machine? (maybe using strace or gdb) - personally I only use windows for preliminary development, all production machines are linux (and the odd errors/exceptions that popup in windows never seem to have a corresponding problem in linux) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Debugging custom streams
-Original Message- From: Jochem Maas [mailto:[EMAIL PROTECTED] Sent: 10 February 2006 14:17 To: [EMAIL PROTECTED] Cc: php-general@lists.php.net Subject: Re: [PHP] Debugging custom streams Jared Williams wrote: Hi, Short version, is there any way of listing all open resources from within a PHP script? Think I may have a problem relating to the object/resource shutdown order within PHP, but cant see which custom stream handler still has an open resource. why not let your zip class keep a list of opened streams (I can't imagine that it's not doing that already) and write a function which will output a list of 'registered' stream resources that are still open [mis]using ftell() or feof() to check which streams are still 'active'. There can only be one open stream at a time, when writing, as a stream writes to what will be the final zip file, and if they were not closed in the correct manner the resulting zip file wouldn't be valid. Perhaps I should explain in abit more detail how this is working. The ZipArchive creates two streams, one for the actual final zip, and the another for the central directories headers. When fputs($stream, '0123456789') occurs the data goes through 2 custom streams, and an optional filter. Zip custom stream - optional compression filter - Append custom stream - final zip file. The Zip custom stream, handles writing the local header to the final zip file, calculating the compressed and uncompressed size, and crc (using hash extension). Like so function stream_write($data) { $r = fwrite($this-compressedStream, $data); $this-uncompressedSize += strlen($data); hash_update($this-hashContext, $data); return $r; } When the Zip custom stream is closed the zip local header is rewritten with the correct values, and a central directory header is written to the central directory stream. So if any resource was left open, then an invalid zip file would result. All $zip-close() has left todo, is append the central directory stream contents to the final zip, add a central end directory header and close both streams for a valid zip. Jared -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Debugging custom streams
Ahha, after some rethinking, I've dropped the append custom stream, as its not really required. And instead of using a custom in memory stream for handling the central directory stream, just using tmpfile(), the application error has disappeared. Jared Jared Williams wrote: Hi, Short version, is there any way of listing all open resources from within a PHP script? Think I may have a problem relating to the object/resource shutdown order within PHP, but cant see which custom stream handler still has an open resource. why not let your zip class keep a list of opened streams (I can't imagine that it's not doing that already) and write a function which will output a list of 'registered' stream resources that are still open [mis]using ftell() or feof() to check which streams are still 'active'. There can only be one open stream at a time, when writing, as a stream writes to what will be the final zip file, and if they were not closed in the correct manner the resulting zip file wouldn't be valid. Perhaps I should explain in abit more detail how this is working. The ZipArchive creates two streams, one for the actual final zip, and the another for the central directories headers. When fputs($stream, '0123456789') occurs the data goes through 2 custom streams, and an optional filter. Zip custom stream - optional compression filter - Append custom stream - final zip file. The Zip custom stream, handles writing the local header to the final zip file, calculating the compressed and uncompressed size, and crc (using hash extension). Like so function stream_write($data) { $r = fwrite($this-compressedStream, $data); $this-uncompressedSize += strlen($data); hash_update($this-hashContext, $data); return $r; } When the Zip custom stream is closed the zip local header is rewritten with the correct values, and a central directory header is written to the central directory stream. So if any resource was left open, then an invalid zip file would result. All $zip-close() has left todo, is append the central directory stream contents to the final zip, add a central end directory header and close both streams for a valid zip. Jared -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Debugging custom streams
WILD GUESS!!! Since you're NOT closing off the streams, PHP is trying to close them for you, but you've unset various variables and whatnot, and confused PHP into fclose()ing the WRONG thing. Try closing your own files. If that doesn't do it, just for fun, tack on an exit; at the very end of the script. This might get PHP to close the streams earlier than it normally would. This is a total hack, but there it is. File a bug report. http://bugs.php.net You seem to have a pretty clear-cut simple code-base to demonstrate the problem, so if you can provide that, it's odds-on that somebody on PHP Dev Team can fix it. On Fri, February 10, 2006 9:40 am, Jared Williams wrote: -Original Message- From: Jochem Maas [mailto:[EMAIL PROTECTED] Sent: 10 February 2006 14:17 To: [EMAIL PROTECTED] Cc: php-general@lists.php.net Subject: Re: [PHP] Debugging custom streams Jared Williams wrote: Hi, Short version, is there any way of listing all open resources from within a PHP script? Think I may have a problem relating to the object/resource shutdown order within PHP, but cant see which custom stream handler still has an open resource. why not let your zip class keep a list of opened streams (I can't imagine that it's not doing that already) and write a function which will output a list of 'registered' stream resources that are still open [mis]using ftell() or feof() to check which streams are still 'active'. There can only be one open stream at a time, when writing, as a stream writes to what will be the final zip file, and if they were not closed in the correct manner the resulting zip file wouldn't be valid. Perhaps I should explain in abit more detail how this is working. The ZipArchive creates two streams, one for the actual final zip, and the another for the central directories headers. When fputs($stream, '0123456789') occurs the data goes through 2 custom streams, and an optional filter. Zip custom stream - optional compression filter - Append custom stream - final zip file. The Zip custom stream, handles writing the local header to the final zip file, calculating the compressed and uncompressed size, and crc (using hash extension). Like so function stream_write($data) { $r = fwrite($this-compressedStream, $data); $this-uncompressedSize += strlen($data); hash_update($this-hashContext, $data); return $r; } When the Zip custom stream is closed the zip local header is rewritten with the correct values, and a central directory header is written to the central directory stream. So if any resource was left open, then an invalid zip file would result. All $zip-close() has left todo, is append the central directory stream contents to the final zip, add a central end directory header and close both streams for a valid zip. Jared -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Debugging custom streams
WILD GUESS!!! Since you're NOT closing off the streams, PHP is trying to close them for you, but you've unset various variables and whatnot, and confused PHP into fclose()ing the WRONG thing. Hmm, I said all streams are getting closed. If they were not then a malformed zip would be created. As only when a stream is closed that you can determine the size and crc, to write to zip headers. Jared -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php