Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mkdir() doesn't take care about the umask #189

Merged
merged 1 commit into from
May 29, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions Imagine/Cache/Resolver/AbstractFilesystemResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ abstract class AbstractFilesystemResolver implements ResolverInterface, CacheMan
*/
protected $cacheManager;

/**
* @var int
*/
protected $folderPermissions = 0777;

/**
* Constructs a filesystem based cache resolver.
*
Expand Down Expand Up @@ -55,6 +60,14 @@ public function setBasePath($basePath)
$this->basePath = $basePath;
}

/**
* @param int $mkdirMode
*/
public function setFolderPermissions ($folderPermissions)
{
$this->folderPermissions = $folderPermissions;
}

/**
* Stores the content into a static file.
*
Expand All @@ -70,13 +83,7 @@ public function store(Response $response, $targetPath, $filter)
{
$dir = pathinfo($targetPath, PATHINFO_DIRNAME);

try {
if (!is_dir($dir)) {
$this->filesystem->mkdir($dir);
}
} catch (IOException $e) {
throw new \RuntimeException(sprintf('Could not create directory %s', $dir), 0, $e);
}
$this->makeFolder($dir);

file_put_contents($targetPath, $response->getContent());

Expand All @@ -101,6 +108,24 @@ public function remove($targetPath, $filter)
return !file_exists($filename);
}

/**
* @param string $dir
* @throws \RuntimeException
*/
protected function makeFolder ($dir)
{
if (!is_dir($dir)) {
$parent = dirname($dir);
try {
$this->makeFolder($parent);
$this->filesystem->mkdir($dir);
$this->filesystem->chmod($dir, $this->folderPermissions);
} catch (IOException $e) {
throw new \RuntimeException(sprintf('Could not create directory %s', $dir), 0, $e);
}
}
}

/**
* Return the local filepath.
*
Expand Down
3 changes: 3 additions & 0 deletions Resources/config/imagine.xml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@
<call method="setBasePath">
<argument type="string">%liip_imagine.cache.resolver.base_path%</argument>
</call>
<call method="setFolderPermissions">
<argument type="string">%liip_imagine.cache_mkdir_mode%</argument>
</call>
</service>

<service id="liip_imagine.cache.resolver.no_cache" class="%liip_imagine.cache.resolver.no_cache.class%">
Expand Down
23 changes: 23 additions & 0 deletions Tests/Imagine/Cache/Resolver/AbstractFilesystemResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,29 @@ public function testStoreCyrillicFilename()
$this->assertEquals($data, file_get_contents($targetPath));
}

public function testMkdirVerifyPermissionOnLastLevel () {
if (false !== strpos(strtolower(PHP_OS), 'win')) {
$this->markTestSkipped('mkdir mode is ignored on windows');
}

$resolver = $this->getMockAbstractFilesystemResolver(new Filesystem());


$resolver->store(new Response(''), $this->tempDir . '/first-level/second-level/cats.jpeg', 'thumbnail');
$this->assertEquals(040777, fileperms($this->tempDir . '/first-level/second-level'));
}

public function testMkdirVerifyPermissionOnFirstLevel () {
if (false !== strpos(strtolower(PHP_OS), 'win')) {
$this->markTestSkipped('mkdir mode is ignored on windows');
}

$resolver = $this->getMockAbstractFilesystemResolver(new Filesystem());

$resolver->store(new Response(''), $this->tempDir . '/first-level/second-level/cats.jpeg', 'thumbnail');
$this->assertEquals(040777, fileperms($this->tempDir . '/first-level'));
}

public function testStoreInvalidDirectory()
{
if (false !== strpos(strtolower(PHP_OS), 'win')) {
Expand Down