@fopen in Nette/Caching/Storages/FileStorage.php

6 years ago

esorimer
Member | 108
+
0
-

@fopen($file,‘r+b’) in Nette/Caching/Storages/FileStorage.php causes an error in methods lock($key) and readMetaAndLock($file, $lock); (if you have set your own strict error handlers changing notices to exception)

Nette 2.1.1

Solution:

<?php
  protected function readMetaAndLock($file, $lock)
        {
                if(!file_exists($file)) return NULL;
                $handle = @fopen($file, 'r+b'); // @ - file may not exist
                if (!$handle) {
                        return NULL;
                }
....
?>
<?php
    public function lock($key)
        {
                $cacheFile = $this->getCacheFile($key);
                if ($this->useDirs && !is_dir($dir = dirname($cacheFile))) {
                        @mkdir($dir); // @ - directory may already exist
                }
                if(file_exists($cacheFile)) {
                        $handle = @fopen($cacheFile, 'r+b'); // @ - file may not exist
                } else {
                        $handle = NULL;
                }
....
?>

6 years ago

Majkl578
Moderator | 1379
+
0
-

What error? Using file_exists is not atomic.

6 years ago

David Grudl
Nette Core | 6877
+
0
-

Your error handler is wrong, check for error_reporting() https://github.com/…ironment.php#L56

6 years ago

esorimer
Member | 108
+
0
-

Yes, I do not use “error_reporting()” condition in my error handler. I am a little bit paranoid and I do not thing its a good idea use ‘@’ operator.

Btw, this peace of code of lock() method is also not atomic:

<?php
...
$handle = @fopen($cacheFile, 'r+b'); // @ - file may not exist
if (!$handle) {
    $handle = fopen($cacheFile, 'wb');
....
?>

But I do not know the reason of this code, so I am not saying it is wrong :)