Skip to content

Commit

Permalink
improved control over and handling of file and dir permissions
Browse files Browse the repository at this point in the history
- rename FileHelper::mkdir for API consistency
- changed default permission for directories to 775 instead of 777
- added some properties to classes that deal with files to allow control
  over directory permissions.
  • Loading branch information
cebe committed Aug 30, 2013
1 parent fa07d31 commit 4b7f5a7
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 21 deletions.
24 changes: 21 additions & 3 deletions extensions/mutex/yii/mutex/FileMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\FileHelper;

/**
* @author resurtm <[email protected]>
Expand All @@ -21,6 +22,19 @@ class FileMutex extends Mutex
* Defaults to the "mutex" subdirectory under the application runtime path.
*/
public $mutexPath = '@runtime/mutex';
/**
* @var integer the permission to be set for newly created mutex files.
* This value will be used by PHP chmod() function. No umask will be applied.
* If not set, the permission will be determined by the current environment.
*/
public $fileMode;
/**
* @var integer the permission to be set for newly created directories.
* This value will be used by PHP chmod() function. No umask will be applied.
* Defaults to 0775, meaning the directory is read-writable by owner and group,
* but read-only for other users.
*/
public $dirMode = 0775;
/**
* @var resource[] stores all opened lock files. Keys are lock names and values are file handles.
*/
Expand All @@ -39,22 +53,26 @@ public function init()
}
$this->mutexPath = Yii::getAlias($this->mutexPath);
if (!is_dir($this->mutexPath)) {
mkdir($this->mutexPath, 0777, true);
FileHelper::createDirectory($this->mutexPath, $this->dirMode, true);
}
}

/**
* This method should be extended by concrete mutex implementations. Acquires lock by given name.
* Acquires lock by given name.
* @param string $name of the lock to be acquired.
* @param integer $timeout to wait for lock to become released.
* @return boolean acquiring result.
*/
protected function acquireLock($name, $timeout = 0)
{
$file = fopen($this->mutexPath . '/' . md5($name) . '.lock', 'w+');
$fileName = $this->mutexPath . '/' . md5($name) . '.lock';
$file = fopen($fileName, 'w+');
if ($file === false) {
return false;
}
if ($this->fileMode !== null) {
@chmod($fileName, $this->fileMode);
}
$waitTime = 0;
while (!flock($file, LOCK_EX | LOCK_NB)) {
$waitTime++;
Expand Down
23 changes: 20 additions & 3 deletions framework/yii/caching/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace yii\caching;

use Yii;
use yii\helpers\FileHelper;

/**
* FileCache implements a cache component using files.
Expand Down Expand Up @@ -45,6 +46,20 @@ class FileCache extends Cache
* This number should be between 0 and 1000000. A value 0 means no GC will be performed at all.
**/
public $gcProbability = 10;
/**
* @var integer the permission to be set for newly created cache files.
* This value will be used by PHP chmod() function. No umask will be applied.
* If not set, the permission will be determined by the current environment.
*/
public $fileMode;
/**
* @var integer the permission to be set for newly created directories.
* This value will be used by PHP chmod() function. No umask will be applied.
* Defaults to 0775, meaning the directory is read-writable by owner and group,
* but read-only for other users.
*/
public $dirMode = 0775;


/**
* Initializes this component by ensuring the existence of the cache path.
Expand All @@ -54,7 +69,7 @@ public function init()
parent::init();
$this->cachePath = Yii::getAlias($this->cachePath);
if (!is_dir($this->cachePath)) {
mkdir($this->cachePath, 0777, true);
FileHelper::createDirectory($this->cachePath, $this->dirMode, true);
}
}

Expand Down Expand Up @@ -108,10 +123,12 @@ protected function setValue($key, $value, $expire)

$cacheFile = $this->getCacheFile($key);
if ($this->directoryLevel > 0) {
@mkdir(dirname($cacheFile), 0777, true);
@FileHelper::createDirectory(dirname($cacheFile), $this->dirMode, true);
}
if (@file_put_contents($cacheFile, $value, LOCK_EX) !== false) {
@chmod($cacheFile, 0777);
if ($this->fileMode !== null) {
@chmod($cacheFile, $this->fileMode);
}
return @touch($cacheFile, $expire);
} else {
return false;
Expand Down
14 changes: 7 additions & 7 deletions framework/yii/helpers/FileHelperBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public static function getMimeTypeByExtension($file, $magicFile = null)
* @param string $dst the destination directory
* @param array $options options for directory copy. Valid options are:
*
* - dirMode: integer, the permission to be set for newly copied directories. Defaults to 0777.
* - dirMode: integer, the permission to be set for newly copied directories. Defaults to 0775.
* - fileMode: integer, the permission to be set for newly copied files. Defaults to the current environment setting.
* - filter: callback, a PHP callback that is called for each directory or file.
* The signature of the callback should be: `function ($path)`, where `$path` refers the full path to be filtered.
Expand Down Expand Up @@ -162,7 +162,7 @@ public static function getMimeTypeByExtension($file, $magicFile = null)
public static function copyDirectory($src, $dst, $options = array())
{
if (!is_dir($dst)) {
static::mkdir($dst, isset($options['dirMode']) ? $options['dirMode'] : 0777, true);
static::createDirectory($dst, isset($options['dirMode']) ? $options['dirMode'] : 0775, true);
}

$handle = opendir($src);
Expand Down Expand Up @@ -302,25 +302,25 @@ public static function filterPath($path, $options)
}

/**
* Makes directory.
* Creates a new directory.
*
* This method is similar to the PHP `mkdir()` function except that
* it uses `chmod()` to set the permission of the created directory
* in order to avoid the impact of the `umask` setting.
*
* @param string $path path to be created.
* @param integer $mode the permission to be set for created directory.
* @param string $path path of the directory to be created.
* @param integer $mode the permission to be set for the created directory.
* @param boolean $recursive whether to create parent directories if they do not exist.
* @return boolean whether the directory is created successfully
*/
public static function mkdir($path, $mode = 0777, $recursive = true)
public static function createDirectory($path, $mode = 0775, $recursive = true)
{
if (is_dir($path)) {
return true;
}
$parentDir = dirname($path);
if ($recursive && !is_dir($parentDir)) {
static::mkdir($parentDir, $mode, true);
static::createDirectory($parentDir, $mode, true);
}
$result = mkdir($path, $mode);
chmod($path, $mode);
Expand Down
19 changes: 18 additions & 1 deletion framework/yii/log/FileTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\FileHelper;

/**
* FileTarget records log messages in a file.
Expand Down Expand Up @@ -37,6 +38,19 @@ class FileTarget extends Target
* @var integer number of log files used for rotation. Defaults to 5.
*/
public $maxLogFiles = 5;
/**
* @var integer the permission to be set for newly created log files.
* This value will be used by PHP chmod() function. No umask will be applied.
* If not set, the permission will be determined by the current environment.
*/
public $fileMode;
/**
* @var integer the permission to be set for newly created directories.
* This value will be used by PHP chmod() function. No umask will be applied.
* Defaults to 0775, meaning the directory is read-writable by owner and group,
* but read-only for other users.
*/
public $dirMode = 0775;


/**
Expand All @@ -53,7 +67,7 @@ public function init()
}
$logPath = dirname($this->logFile);
if (!is_dir($logPath)) {
@mkdir($logPath, 0777, true);
FileHelper::createDirectory($logPath, $this->dirMode, true);
}
if ($this->maxLogFiles < 1) {
$this->maxLogFiles = 1;
Expand Down Expand Up @@ -87,6 +101,9 @@ public function export()
@flock($fp, LOCK_UN);
@fclose($fp);
}
if ($this->fileMode !== null) {
@chmod($this->logFile, $this->fileMode);
}
}

/**
Expand Down
11 changes: 6 additions & 5 deletions framework/yii/web/AssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,17 @@ class AssetManager extends Component
public $linkAssets = false;
/**
* @var integer the permission to be set for newly published asset files.
* This value will be used by PHP chmod() function.
* This value will be used by PHP chmod() function. No umask will be applied.
* If not set, the permission will be determined by the current environment.
*/
public $fileMode;
/**
* @var integer the permission to be set for newly generated asset directories.
* This value will be used by PHP chmod() function.
* Defaults to 0777, meaning the directory can be read, written and executed by all users.
* This value will be used by PHP chmod() function. No umask will be applied.
* Defaults to 0775, meaning the directory is read-writable by owner and group,
* but read-only for other users.
*/
public $dirMode = 0777;
public $dirMode = 0775;

/**
* Initializes the component.
Expand Down Expand Up @@ -205,7 +206,7 @@ public function publish($path, $options = array())
$dstFile = $dstDir . DIRECTORY_SEPARATOR . $fileName;

if (!is_dir($dstDir)) {
mkdir($dstDir, $this->dirMode, true);
FileHelper::createDirectory($dstDir, $this->dirMode, true);
}

if ($this->linkAssets) {
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/framework/helpers/FileHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,9 @@ public function testMkdir()
{
$basePath = $this->testFilePath;
$dirName = $basePath . DIRECTORY_SEPARATOR . 'test_dir_level_1' . DIRECTORY_SEPARATOR . 'test_dir_level_2';
$this->assertTrue(FileHelper::mkdir($dirName), 'FileHelper::mkdir should return true if directory was created!');
$this->assertTrue(FileHelper::createDirectory($dirName), 'FileHelper::mkdir should return true if directory was created!');
$this->assertTrue(file_exists($dirName), 'Unable to create directory recursively!');
$this->assertTrue(FileHelper::mkdir($dirName), 'FileHelper::mkdir should return true for already existing directories!');
$this->assertTrue(FileHelper::createDirectory($dirName), 'FileHelper::mkdir should return true for already existing directories!');
}

public function testGetMimeTypeByExtension()
Expand Down

0 comments on commit 4b7f5a7

Please sign in to comment.