Merge pull request #4477 from owncloud/hooks-view

also emit hooks for views that are a subfolder of the user folder
This commit is contained in:
Bernhard Posselt 2013-09-04 16:09:22 -07:00
commit fbedd643f7
2 changed files with 87 additions and 33 deletions

View File

@ -268,18 +268,18 @@ class View {
$absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
if (\OC_FileProxy::runPreProxies('file_put_contents', $absolutePath, $data) if (\OC_FileProxy::runPreProxies('file_put_contents', $absolutePath, $data)
and Filesystem::isValidPath($path) and Filesystem::isValidPath($path)
and !Filesystem::isFileBlacklisted($path) and !Filesystem::isFileBlacklisted($path)
) { ) {
$path = $this->getRelativePath($absolutePath); $path = $this->getRelativePath($absolutePath);
$exists = $this->file_exists($path); $exists = $this->file_exists($path);
$run = true; $run = true;
if ($this->fakeRoot == Filesystem::getRoot() && !Cache\Scanner::isPartialFile($path)) { if ($this->shouldEmitHooks($path)) {
if (!$exists) { if (!$exists) {
\OC_Hook::emit( \OC_Hook::emit(
Filesystem::CLASSNAME, Filesystem::CLASSNAME,
Filesystem::signal_create, Filesystem::signal_create,
array( array(
Filesystem::signal_param_path => $path, Filesystem::signal_param_path => $this->getHookPath($path),
Filesystem::signal_param_run => &$run Filesystem::signal_param_run => &$run
) )
); );
@ -288,7 +288,7 @@ class View {
Filesystem::CLASSNAME, Filesystem::CLASSNAME,
Filesystem::signal_write, Filesystem::signal_write,
array( array(
Filesystem::signal_param_path => $path, Filesystem::signal_param_path => $this->getHookPath($path),
Filesystem::signal_param_run => &$run Filesystem::signal_param_run => &$run
) )
); );
@ -301,18 +301,18 @@ class View {
list ($count, $result) = \OC_Helper::streamCopy($data, $target); list ($count, $result) = \OC_Helper::streamCopy($data, $target);
fclose($target); fclose($target);
fclose($data); fclose($data);
if ($this->fakeRoot == Filesystem::getRoot() && !Cache\Scanner::isPartialFile($path) && $result !== false) { if ($this->shouldEmitHooks($path) && $result !== false) {
if (!$exists) { if (!$exists) {
\OC_Hook::emit( \OC_Hook::emit(
Filesystem::CLASSNAME, Filesystem::CLASSNAME,
Filesystem::signal_post_create, Filesystem::signal_post_create,
array(Filesystem::signal_param_path => $path) array(Filesystem::signal_param_path => $this->getHookPath($path))
); );
} }
\OC_Hook::emit( \OC_Hook::emit(
Filesystem::CLASSNAME, Filesystem::CLASSNAME,
Filesystem::signal_post_write, Filesystem::signal_post_write,
array(Filesystem::signal_param_path => $path) array(Filesystem::signal_param_path => $this->getHookPath($path))
); );
} }
\OC_FileProxy::runPostProxies('file_put_contents', $absolutePath, $count); \OC_FileProxy::runPostProxies('file_put_contents', $absolutePath, $count);
@ -354,21 +354,21 @@ class View {
return false; return false;
} }
$run = true; $run = true;
if ($this->fakeRoot == Filesystem::getRoot() && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) { if ($this->shouldEmitHooks() && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) {
// if it was a rename from a part file to a regular file it was a write and not a rename operation // if it was a rename from a part file to a regular file it was a write and not a rename operation
\OC_Hook::emit( \OC_Hook::emit(
Filesystem::CLASSNAME, Filesystem::signal_write, Filesystem::CLASSNAME, Filesystem::signal_write,
array( array(
Filesystem::signal_param_path => $path2, Filesystem::signal_param_path => $this->getHookPath($path2),
Filesystem::signal_param_run => &$run Filesystem::signal_param_run => &$run
) )
); );
} elseif ($this->fakeRoot == Filesystem::getRoot()) { } elseif ($this->shouldEmitHooks()) {
\OC_Hook::emit( \OC_Hook::emit(
Filesystem::CLASSNAME, Filesystem::signal_rename, Filesystem::CLASSNAME, Filesystem::signal_rename,
array( array(
Filesystem::signal_param_oldpath => $path1, Filesystem::signal_param_oldpath => $this->getHookPath($path1),
Filesystem::signal_param_newpath => $path2, Filesystem::signal_param_newpath => $this->getHookPath($path2),
Filesystem::signal_param_run => &$run Filesystem::signal_param_run => &$run
) )
); );
@ -408,22 +408,22 @@ class View {
} }
} }
} }
if ($this->fakeRoot == Filesystem::getRoot() && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { if ($this->shouldEmitHooks() && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) {
// if it was a rename from a part file to a regular file it was a write and not a rename operation // if it was a rename from a part file to a regular file it was a write and not a rename operation
\OC_Hook::emit( \OC_Hook::emit(
Filesystem::CLASSNAME, Filesystem::CLASSNAME,
Filesystem::signal_post_write, Filesystem::signal_post_write,
array( array(
Filesystem::signal_param_path => $path2, Filesystem::signal_param_path => $this->getHookPath($path2),
) )
); );
} elseif ($this->fakeRoot == Filesystem::getRoot() && $result !== false) { } elseif ($this->shouldEmitHooks() && $result !== false) {
\OC_Hook::emit( \OC_Hook::emit(
Filesystem::CLASSNAME, Filesystem::CLASSNAME,
Filesystem::signal_post_rename, Filesystem::signal_post_rename,
array( array(
Filesystem::signal_param_oldpath => $path1, Filesystem::signal_param_oldpath => $this->getHookPath($path1),
Filesystem::signal_param_newpath => $path2 Filesystem::signal_param_newpath => $this->getHookPath($path2)
) )
); );
} }
@ -455,13 +455,13 @@ class View {
} }
$run = true; $run = true;
$exists = $this->file_exists($path2); $exists = $this->file_exists($path2);
if ($this->fakeRoot == Filesystem::getRoot()) { if ($this->shouldEmitHooks()) {
\OC_Hook::emit( \OC_Hook::emit(
Filesystem::CLASSNAME, Filesystem::CLASSNAME,
Filesystem::signal_copy, Filesystem::signal_copy,
array( array(
Filesystem::signal_param_oldpath => $path1, Filesystem::signal_param_oldpath => $this->getHookPath($path1),
Filesystem::signal_param_newpath => $path2, Filesystem::signal_param_newpath => $this->getHookPath($path2),
Filesystem::signal_param_run => &$run Filesystem::signal_param_run => &$run
) )
); );
@ -470,7 +470,7 @@ class View {
Filesystem::CLASSNAME, Filesystem::CLASSNAME,
Filesystem::signal_create, Filesystem::signal_create,
array( array(
Filesystem::signal_param_path => $path2, Filesystem::signal_param_path => $this->getHookPath($path2),
Filesystem::signal_param_run => &$run Filesystem::signal_param_run => &$run
) )
); );
@ -480,7 +480,7 @@ class View {
Filesystem::CLASSNAME, Filesystem::CLASSNAME,
Filesystem::signal_write, Filesystem::signal_write,
array( array(
Filesystem::signal_param_path => $path2, Filesystem::signal_param_path => $this->getHookPath($path2),
Filesystem::signal_param_run => &$run Filesystem::signal_param_run => &$run
) )
); );
@ -511,26 +511,26 @@ class View {
list($count, $result) = \OC_Helper::streamCopy($source, $target); list($count, $result) = \OC_Helper::streamCopy($source, $target);
} }
} }
if ($this->fakeRoot == Filesystem::getRoot() && $result !== false) { if ($this->shouldEmitHooks() && $result !== false) {
\OC_Hook::emit( \OC_Hook::emit(
Filesystem::CLASSNAME, Filesystem::CLASSNAME,
Filesystem::signal_post_copy, Filesystem::signal_post_copy,
array( array(
Filesystem::signal_param_oldpath => $path1, Filesystem::signal_param_oldpath => $this->getHookPath($path1),
Filesystem::signal_param_newpath => $path2 Filesystem::signal_param_newpath => $this->getHookPath($path2)
) )
); );
if (!$exists) { if (!$exists) {
\OC_Hook::emit( \OC_Hook::emit(
Filesystem::CLASSNAME, Filesystem::CLASSNAME,
Filesystem::signal_post_create, Filesystem::signal_post_create,
array(Filesystem::signal_param_path => $path2) array(Filesystem::signal_param_path => $this->getHookPath($path2))
); );
} }
\OC_Hook::emit( \OC_Hook::emit(
Filesystem::CLASSNAME, Filesystem::CLASSNAME,
Filesystem::signal_post_write, Filesystem::signal_post_write,
array(Filesystem::signal_param_path => $path2) array(Filesystem::signal_param_path => $this->getHookPath($path2))
); );
} }
return $result; return $result;
@ -621,11 +621,11 @@ class View {
if ($path == null) { if ($path == null) {
return false; return false;
} }
if (Filesystem::$loaded && $this->fakeRoot == Filesystem::getRoot()) { if ($this->shouldEmitHooks($path)) {
\OC_Hook::emit( \OC_Hook::emit(
Filesystem::CLASSNAME, Filesystem::CLASSNAME,
Filesystem::signal_read, Filesystem::signal_read,
array(Filesystem::signal_param_path => $path) array(Filesystem::signal_param_path => $this->getHookPath($path))
); );
} }
list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix);
@ -659,7 +659,7 @@ class View {
$absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
if (\OC_FileProxy::runPreProxies($operation, $absolutePath, $extraParam) if (\OC_FileProxy::runPreProxies($operation, $absolutePath, $extraParam)
and Filesystem::isValidPath($path) and Filesystem::isValidPath($path)
and !Filesystem::isFileBlacklisted($path) and !Filesystem::isFileBlacklisted($path)
) { ) {
$path = $this->getRelativePath($absolutePath); $path = $this->getRelativePath($absolutePath);
if ($path == null) { if ($path == null) {
@ -675,7 +675,7 @@ class View {
$result = $storage->$operation($internalPath); $result = $storage->$operation($internalPath);
} }
$result = \OC_FileProxy::runPostProxies($operation, $this->getAbsolutePath($path), $result); $result = \OC_FileProxy::runPostProxies($operation, $this->getAbsolutePath($path), $result);
if (Filesystem::$loaded and $this->fakeRoot == Filesystem::getRoot() && $result !== false) { if ($this->shouldEmitHooks($path) && $result !== false) {
if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open
$this->runHooks($hooks, $path, true); $this->runHooks($hooks, $path, true);
} }
@ -686,10 +686,35 @@ class View {
return null; return null;
} }
/**
* get the path relative to the default root for hook usage
*
* @param string $path
* @return string
*/
private function getHookPath($path) {
if (!Filesystem::getView()) {
return $path;
}
return Filesystem::getView()->getRelativePath($this->getAbsolutePath($path));
}
private function shouldEmitHooks($path = '') {
if ($path && Cache\Scanner::isPartialFile($path)) {
return false;
}
if (!Filesystem::$loaded) {
return false;
}
$defaultRoot = Filesystem::getRoot();
return (strlen($this->fakeRoot) >= strlen($defaultRoot)) && (substr($this->fakeRoot, 0, strlen($defaultRoot)) === $defaultRoot);
}
private function runHooks($hooks, $path, $post = false) { private function runHooks($hooks, $path, $post = false) {
$path = $this->getHookPath($path);
$prefix = ($post) ? 'post_' : ''; $prefix = ($post) ? 'post_' : '';
$run = true; $run = true;
if (Filesystem::$loaded and $this->fakeRoot == Filesystem::getRoot() && !Cache\Scanner::isPartialFile($path)) { if ($this->shouldEmitHooks($path)) {
foreach ($hooks as $hook) { foreach ($hooks as $hook) {
if ($hook != 'read') { if ($hook != 'read') {
\OC_Hook::emit( \OC_Hook::emit(

View File

@ -25,7 +25,7 @@ class View extends \PHPUnit_Framework_TestCase {
//login //login
\OC_User::createUser('test', 'test'); \OC_User::createUser('test', 'test');
$this->user=\OC_User::getUser(); $this->user = \OC_User::getUser();
\OC_User::setUserId('test'); \OC_User::setUserId('test');
\OC\Files\Filesystem::clearMounts(); \OC\Files\Filesystem::clearMounts();
@ -325,6 +325,35 @@ class View extends \PHPUnit_Framework_TestCase {
$this->assertEquals($cachedData['storage_mtime'], $cachedData['mtime']); $this->assertEquals($cachedData['storage_mtime'], $cachedData['mtime']);
} }
/**
* @medium
*/
function testViewHooks() {
$storage1 = $this->getTestStorage();
$storage2 = $this->getTestStorage();
$defaultRoot = \OC\Files\Filesystem::getRoot();
\OC\Files\Filesystem::mount($storage1, array(), '/');
\OC\Files\Filesystem::mount($storage2, array(), $defaultRoot . '/substorage');
\OC_Hook::connect('OC_Filesystem', 'post_write', $this, 'dummyHook');
$rootView = new \OC\Files\View('');
$subView = new \OC\Files\View($defaultRoot . '/substorage');
$this->hookPath = null;
$rootView->file_put_contents('/foo.txt', 'asd');
$this->assertNull($this->hookPath);
$subView->file_put_contents('/foo.txt', 'asd');
$this->assertNotNull($this->hookPath);
$this->assertEquals('/substorage/foo.txt', $this->hookPath);
}
private $hookPath;
function dummyHook($params) {
$this->hookPath = $params['path'];
}
/** /**
* @param bool $scan * @param bool $scan
* @return \OC\Files\Storage\Storage * @return \OC\Files\Storage\Storage