Ocasional error warning when CakePHP is writing to the cache
Asked Answered
P

4

6

I'm developing a CakePHP 2.2 site locally with MAMP. Every so often, I get one or more warnings similar to this, about not being able to write to one or more cache files:

Warning: SplFileInfo::openFile(/Applications/MAMP/htdocs/mywebsite/www/app/tmp/cache/persistent/myapp_cake_core_cake_console_en-au): failed to open stream: Permission denied in /Applications/MAMP/htdocs/mywebsite/www/lib/Cake/Cache/Engine/FileEngine.php on line 313

The weird thing is, /tmp is 777, tmp/cache is 777, and tmp/cache/persistent is 777 (don't worry... it won't be 777 on the server!). The file itself inside tmp/cache/persistent is 644 - but I assume Cake is creating and managing that file, and does so with the permissions it needs.

If I just refresh the page, the error goes away (and then re-appears sometime later). I'm not doing any explicit caching, so this stuff is just Cake doing whatever it automatically does.

So my question is:

a) How does this automatic caching of Cake's work? Is it trying to write to that file on every page refresh, and failing only once in a while? Or is it only trying to write to that file once in a while, but failing every time it tries?

b) If it's only failing only once in a while, can I safely just ignore it? And if it's failing every time it tries, how can I fix it?

Thanks in advance for any help!

Parthenos answered 3/10, 2012 at 23:31 Comment(2)
Not much help, but I’ve had this a few times myself. Unfortunately haven’t really find a way to fix other than run sudo chmod -R 777 app/tmp when I encounter it.Fibrillation
I had this issue when I did chown -R 0777 accidently hehContactor
M
14

This happens probably when a process different from Apache create files in the cache. This can be the case for instance when you run shell commands as you probably do it as a different user than apache.

By default the File cache creates files with permissions allowing only the user that has created the files to modify them, but this can be fixed by setting a mask in the Cache config in core.php:

Cache::config('_cake_core_', array(
    'engine' => $engine,
    'prefix' => 'cake_core_',
    'path' => CACHE . 'persistent' . DS,
    'serialize' => ($engine === 'File'),
    'duration' => $duration,
    'mask' => 0666
));

Cache::config('_cake_model_', array(
    'engine' => $engine,
    'prefix' => 'cake_model_',
    'path' => CACHE . 'models' . DS,
    'serialize' => ($engine === 'File'),
    'duration' => $duration,
    'mask' => 0666
));
Mordy answered 4/10, 2012 at 9:48 Comment(5)
OK thanks. I can't imagine what process other than apache would be changing those files, but anyway. I've added that mask and I'll monitor it over the next few days to see if it works.Parthenos
OK, that seems to be working. A note to others: you'll also need to delete the existing cache files, so that cake re-creates them with 666 permissions. Otherwise they'll just remain with 664 permission.Parthenos
Hmm, the interesting thing is that this in NOT in the API. What I've previously used to resolve this issue is a simple bash script that calls the Cake Consoles I need to run and then changes the permissions on all files in App/tmp. I will file this with the Cake team.Hedonics
Seems to be the case in cake 3 as wellEugene
An additional hint what the difference of setting the 'mask' => 0666 is: If you set it, the cache files have read and write permissions for the user, ,group AND all other users (-rw-rw-rw-). If you do not set the 'mask' option, only the user and the group have the read and write permission, the other users can ONLY read (-rw-rw-r--). Without that mask setting, you are getting problems when you use cake Shell and it is running as another user as the apache user is, for example bitnami LAMP stack: Apache runs as daemon user, cake Shell is running usually as bitnami userEarlearla
B
1

If you want to avoid giving read/write access to the "other" group, check out my other solution here:

https://mcmap.net/q/412459/-splfileinfo-openfile-app-tmp-cache-persistent-cake_core_cake_console_-failed-to-open-stream-permission-denied-in-lib-fileengine-php-line-293

Baskerville answered 9/9, 2013 at 17:46 Comment(0)
B
0

Simple really, assuming and you are a sudoer and your user name is martinlutherking

sudo adduser martinlutherking www-data

This way cake console commands can read cache files created by apache2, however you may need to do the inverse group add to ensure that www-data can read cache files created by martinlutherking

Becerra answered 16/8, 2017 at 16:43 Comment(0)
E
0

Just in case anybody is seeing this and wonders how it works in cakePHP 3.x:

modify /config/app.php and add 'mask' => 0666 to

/**
 * Configure the cache adapters.
 */
'Cache' => [
    'default' => [
        'className' => 'File',
        'path' => CACHE,
        'url' => env('CACHE_DEFAULT_URL', null),
        'mask' => 0666
    ],

and probably you also want to add it to the log files:

/**
 * Configures logging options
 */
'Log' => [
    'debug' => [
        'className' => 'Cake\Log\Engine\FileLog',
        'path' => LOGS,
        'file' => 'debug',
        'levels' => ['notice', 'info', 'debug'],
        'url' => env('LOG_DEBUG_URL', null),
        'mask' => 0666
    ],
    'error' => [
        'className' => 'Cake\Log\Engine\FileLog',
        'path' => LOGS,
        'file' => 'error',
        'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
        'url' => env('LOG_ERROR_URL', null),
        'mask' => 0666
    ],
],
Earlearla answered 16/2, 2018 at 18:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.