Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.8.3

Bug #19422 _read truncates cache files
Submitted: 2012-05-18 03:08 UTC
From: crackwilding Assigned: tacker
Status: Closed Package: Cache_Lite (version 1.7.14)
PHP Version: 5.3.12 OS: Irrelevant
Roadmaps: (Not assigned)    
Subscription  


 [2012-05-18 03:08 UTC] crackwilding (Fletcher Moore)
Description: ------------ I'm trying to read and write cache files to a directory specified by a stream wrapper generated by Drupal 7. The _read function is truncating the files to 16,352 characters, which renders them useless, not least of which because they fail checksum comparisons. This can be fixed by changing line 734 to: $data = @stream_get_contents($fp); This renders the $length variable sort of pointless, and of course it causes some issues with backward compatibility, but I wanted to know whether I'm missing something before diving into it any deeper.

Comments

 [2012-05-18 12:30 UTC] tacker (Markus Tacker)
-Status: Open +Status: Feedback
Hoi crackwilding, thank you for the report. Please provide the relevant code sections of your app which reproduces this error. Thanks!
 [2012-05-18 20:48 UTC] crackwilding (Fletcher Moore)
Sure thing. Here's the relevant part of the function: function hg_reader_cacheHandler($id, $type='feed', $cacheTime=3600) { $xml = array(); $cacheOpt = array(); $cacheOpt['cacheDir'] = variable_get('hg_cache_location', file_default_scheme() . '://hg/cache' . '/'); $cacheOpt['cache_time'] = $cacheTime; $cl = new Cache_Lite($cacheOpt); $cl->setToDebug(); if (($data = $cl->get($id . $type)) AND (!array_key_exists('clearcache', $_GET))) { $xml['err'] = ''; $xml['data'] = $data; } else { // Pull XML for feed if ($type == 'feed') { $xml_url = HG_URL . '/xml/' . $id; } else { // Pull XML for node $xml_url = HG_URL . '/node/' . $id . '/xml'; } } A couple notes: variable_get() and file_default_scheme() are Drupal API functions. The line those appear in set $cacheOpt['cacheDir'] to "public://hg/cache/" which is writeable and all that. The cache files appear there, they just don't ever get used. The code that registers the public stream wrapper is in Drupal's file.inc: http://api.drupal.org/api/drupal/includes%21stream_wrappers.inc/class/DrupalPubli cStreamWrapper/7 I think it would also be possible to loop the fread() function within _read so that it pulls the whole file. Someone mentioned that in a Cache_lite bug report long ago, but the thread (http://pear.php.net/bugs/bug.php?id=9836) was closed when the guy failed to produce a patch. I could probably write one — I haven't tried that approach yet, but it does seem to me better for backward compatibility purposes.
 [2012-05-19 15:24 UTC] tacker (Markus Tacker)
-Status: Feedback +Status: Assigned -Operating System: OS X 10.7.3 +Operating System: Irrelevant -Assigned To: +Assigned To: tacker
According to the php docs for fread [1] the problem might be a buffered stream: > if the stream is read buffered and it does not represent a plain file, at most one read of up to a number of bytes equal to the chunk size (usually 8192) is made; Using stream_get_contents is not an option, as Cache_Lite must run under PHP4. Using fread() in a loop until there is no more data is the right solution. I'll have a look into that in the next days. [1] http://php.net/manual/en/function.fread.php
 [2012-05-21 17:54 UTC] tacker (Markus Tacker)
-Status: Assigned +Status: Closed -Package Version: 1.7.12 +Package Version: 1.7.14
Hoi Fletcher, this has been fixed in the latest release. Please check the latest release. I've created a test for the bug: https://github.com/pear/Cache_Lite/blob/master/tests/pearbug19422.phpt Please feel free to submit a PR if the proplem persists: https://github.com/pear/Cache_Lite
 [2012-05-22 00:02 UTC] crackwilding (Fletcher Moore)
Works like a charm. Thanks!