forked from yiisoft/yii2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improved control over and handling of file and dir permissions
- 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
Showing
6 changed files
with
74 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
|
||
use Yii; | ||
use yii\base\InvalidConfigException; | ||
use yii\helpers\FileHelper; | ||
|
||
/** | ||
* @author resurtm <[email protected]> | ||
|
@@ -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. | ||
*/ | ||
|
@@ -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++; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters