mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-05-12 13:32:16 +09:00
Merge pull request #545 from kijin/pr/safe-overwrite-improvements
쓰기 퍼미션이 없는 폴더 내에서 쓰기 퍼미션이 있는 파일을 덮어쓸 때 오류 해결
This commit is contained in:
commit
7b973d105b
2 changed files with 32 additions and 4 deletions
|
|
@ -240,11 +240,16 @@ class Storage
|
|||
}
|
||||
}
|
||||
|
||||
if (self::$safe_overwrite && strncasecmp($mode, 'a', 1))
|
||||
if (self::$safe_overwrite && strncasecmp($mode, 'a', 1) && @is_writable($destination_dir))
|
||||
{
|
||||
$use_atomic_rename = true;
|
||||
$original_filename = $filename;
|
||||
$filename = $filename . '.tmp.' . microtime(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
$use_atomic_rename = false;
|
||||
}
|
||||
|
||||
if ($fp = @fopen($filename, $mode))
|
||||
{
|
||||
|
|
@ -275,7 +280,7 @@ class Storage
|
|||
|
||||
@chmod($filename, ($perms === null ? (0666 & ~self::getUmask()) : $perms));
|
||||
|
||||
if (self::$safe_overwrite && strncasecmp($mode, 'a', 1))
|
||||
if ($use_atomic_rename)
|
||||
{
|
||||
$rename_success = @rename($filename, $original_filename);
|
||||
if (!$rename_success)
|
||||
|
|
@ -350,14 +355,20 @@ class Storage
|
|||
}
|
||||
elseif (self::isDirectory($destination))
|
||||
{
|
||||
$destination_dir = $destination;
|
||||
$destination = $destination . '/' . basename($source);
|
||||
}
|
||||
|
||||
if (self::$safe_overwrite)
|
||||
if (self::$safe_overwrite && @is_writable($destination_dir))
|
||||
{
|
||||
$use_atomic_rename = true;
|
||||
$original_destination = $destination;
|
||||
$destination = $destination . '.tmp.' . microtime(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
$use_atomic_rename = false;
|
||||
}
|
||||
|
||||
$copy_success = @copy($source, $destination);
|
||||
if (!$copy_success)
|
||||
|
|
@ -382,7 +393,7 @@ class Storage
|
|||
@chmod($destination, $destination_perms);
|
||||
}
|
||||
|
||||
if (self::$safe_overwrite)
|
||||
if ($use_atomic_rename)
|
||||
{
|
||||
$rename_success = @rename($destination, $original_destination);
|
||||
if (!$rename_success)
|
||||
|
|
|
|||
|
|
@ -9,11 +9,13 @@ class StorageTest extends \Codeception\TestCase\Test
|
|||
|
||||
public function _after()
|
||||
{
|
||||
@chmod(\RX_BASEDIR . 'tests/_output', 0755);
|
||||
Rhymix\Framework\Storage::deleteDirectory(\RX_BASEDIR . 'tests/_output', false);
|
||||
}
|
||||
|
||||
public function _failed()
|
||||
{
|
||||
@chmod(\RX_BASEDIR . 'tests/_output', 0755);
|
||||
Rhymix\Framework\Storage::deleteDirectory(\RX_BASEDIR . 'tests/_output', false);
|
||||
}
|
||||
|
||||
|
|
@ -191,13 +193,28 @@ class StorageTest extends \Codeception\TestCase\Test
|
|||
{
|
||||
$source = \RX_BASEDIR . 'tests/_output/copy.source.txt';
|
||||
$target = \RX_BASEDIR . 'tests/_output/copy.target.txt';
|
||||
$target_dir = \RX_BASEDIR . 'tests/_output';
|
||||
file_put_contents($source, 'foobarbaz');
|
||||
chmod($source, 0646);
|
||||
|
||||
// Copy with exact destination filename
|
||||
$this->assertTrue(Rhymix\Framework\Storage::copy($source, $target));
|
||||
$this->assertTrue(file_exists($target));
|
||||
$this->assertTrue(file_get_contents($target) === 'foobarbaz');
|
||||
|
||||
// Copy into directory with source filename
|
||||
$this->assertTrue(Rhymix\Framework\Storage::copy($source, $target_dir));
|
||||
$this->assertTrue(file_exists($target_dir . '/copy.source.txt'));
|
||||
$this->assertTrue(file_get_contents($target_dir . '/copy.source.txt') === 'foobarbaz');
|
||||
|
||||
// Copy into directory with no write permissions
|
||||
chmod($target_dir, 0555);
|
||||
file_put_contents($source, 'foobarbaz has changed');
|
||||
$this->assertTrue(Rhymix\Framework\Storage::copy($source, $target));
|
||||
$this->assertTrue(file_exists($target));
|
||||
$this->assertTrue(file_get_contents($target) === 'foobarbaz has changed');
|
||||
chmod($target_dir, 0755);
|
||||
|
||||
if (strncasecmp(\PHP_OS, 'Win', 3) !== 0)
|
||||
{
|
||||
$this->assertEquals(0646, fileperms($target) & 0777);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue