コンソールで自作shellを実行するとパーミッションエラーになる

結構はまりました。原因がわかるまで意味がわからず時間をくってしまったので書いておきます。

症状

こんな感じのエラーがだーーーっとでまくり。

<?php
SplFileInfo::openFile(/app/tmp/cache/persistent/myapp_core_cake_console_):failed to open stream:Permission denied in /lib//FileEngine.php line 293
?>

で、/app/tmp/cache/modelの中身を削除して実行すると、今度はブラウザ側で同じエラーが表示される。

原因

/app/tmp/cache/model/app/tmp/cache/persistent内のキャッシュファイルのパーミッションが644になっている。
自分の環境だとブラウザからのアクションで生成されるキャッシュファイルは所有権がnobodyになっていて、コンソールからCakeシェルを実行するときは当然のようにhoge(ユーザー名)になってるわけです。

対策

/app/Congfig/core.phpmaskパラメーターを追加する。これでキャッシュファイルの生成時のパーミッションを設定することができます。

/app/Congfig/core.php
<?php
/**
 * Configure the cache used for general framework caching.  Path information,
 * object listings, and translation cache files are stored with this configuration.
 */
Cache::config('_cake_core_', array(
    'engine' => $engine,
    'prefix' => $prefix . 'cake_core_',
    'path' => CACHE . 'persistent' . DS,
    'serialize' => ($engine === 'File'),
    'duration' => $duration,
    'mask' => 0666
));

/**
 * Configure the cache for model and datasource caches.  This cache configuration
 * is used to store schema descriptions, and table listings in connections.
 */
Cache::config('_cake_model_', array(
    'engine' => $engine,
    'prefix' => $prefix . 'cake_model_',
    'path' => CACHE . 'models' . DS,
    'serialize' => ($engine === 'File'),
    'duration' => $duration,
    'mask' => 0666
));
?>

参考にしたサイト

  • stackoverflow

You can resolve this by adding a mask to your config in core.php

Cache::config('default', array(
    'engine' => 'File',
    'mask' => 0666,
));

cakephp - SplFileInfo::openFile(/app/tmp/cache/persistent/cake_core_cake_console_):failed to open stream:Permission denied in /lib/.../FileEngine.php line 293 - Stack Overflow

最近このサイトよく見ますね。数値です、文字列の"0666"だとおかしくなっちゃいます。実はこれでハマった。

  • Cake Book

When using the FileEngine you might need to use the mask option to ensure cache files are made with the correct permissions.
Caching — CakePHP Cookbook v2.x documentation

マニュアルにもちょっとだけ書いてある。