Refactor Cache\Updater to work outside of the users home

This commit is contained in:
Robin Appelman 2014-08-04 14:29:46 +02:00
parent 614cf996ca
commit 850542c5d6
4 changed files with 111 additions and 174 deletions

View file

@ -57,9 +57,11 @@ class ChangePropagator {
*/ */
list($storage, $internalPath) = $this->view->resolvePath($parent); list($storage, $internalPath) = $this->view->resolvePath($parent);
$cache = $storage->getCache(); if ($storage) {
$id = $cache->getId($internalPath); $cache = $storage->getCache();
$cache->update($id, array('mtime' => $time, 'etag' => $storage->getETag($internalPath))); $id = $cache->getId($internalPath);
$cache->update($id, array('mtime' => $time, 'etag' => $storage->getETag($internalPath)));
}
} }
} }

View file

@ -9,166 +9,122 @@
namespace OC\Files\Cache; namespace OC\Files\Cache;
/** /**
* listen to filesystem hooks and change the cache accordingly * Update the cache and propagate changes
*/ */
class Updater { class Updater {
/**
* @var \OC\Files\View
*/
protected $view;
/** /**
* resolve a path to a storage and internal path * @var \OC\Files\Cache\ChangePropagator
*
* @param string $path the relative path
* @return array an array consisting of the storage and the internal path
*/ */
static public function resolvePath($path) { protected $propagator;
$view = \OC\Files\Filesystem::getView();
return $view->resolvePath($path); /**
* @param \OC\Files\View $view
*/
public function __construct($view) {
$this->view = $view;
$this->propagator = new ChangePropagator($view);
} }
/** /**
* perform a write update * Update the cache for $path
* *
* @param string $path the relative path of the file * @param string $path
*/ */
static public function writeUpdate($path) { public function update($path) {
/** /**
* @var \OC\Files\Storage\Storage $storage * @var \OC\Files\Storage\Storage $storage
* @var string $internalPath * @var string $internalPath
*/ */
list($storage, $internalPath) = self::resolvePath($path); list($storage, $internalPath) = $this->view->resolvePath($path);
if ($storage) { if ($storage) {
$this->propagator->addChange($path);
$cache = $storage->getCache($internalPath); $cache = $storage->getCache($internalPath);
$scanner = $storage->getScanner($internalPath); $scanner = $storage->getScanner($internalPath);
$data = $scanner->scan($internalPath, Scanner::SCAN_SHALLOW); $data = $scanner->scan($internalPath, Scanner::SCAN_SHALLOW);
$this->correctParentStorageMtime($storage, $internalPath);
$cache->correctFolderSize($internalPath, $data); $cache->correctFolderSize($internalPath, $data);
self::correctFolder($path, $storage->filemtime($internalPath));
self::correctParentStorageMtime($storage, $internalPath);
} }
} }
/** /**
* perform a delete update * Remove $path from the cache
* *
* @param string $path the relative path of the file * @param string $path
*/ */
static public function deleteUpdate($path) { public function remove($path) {
/** /**
* @var \OC\Files\Storage\Storage $storage * @var \OC\Files\Storage\Storage $storage
* @var string $internalPath * @var string $internalPath
*/ */
list($storage, $internalPath) = self::resolvePath($path); list($storage, $internalPath) = $this->view->resolvePath($path);
if ($storage) { if ($storage) {
$parent = dirname($internalPath); $parent = dirname($internalPath);
if ($parent === '.') { if ($parent === '.') {
$parent = ''; $parent = '';
} }
$this->propagator->addChange($path);
$cache = $storage->getCache($internalPath); $cache = $storage->getCache($internalPath);
$cache->remove($internalPath); $cache->remove($internalPath);
$cache->correctFolderSize($parent); $cache->correctFolderSize($parent);
self::correctFolder($path, time()); $this->correctParentStorageMtime($storage, $internalPath);
self::correctParentStorageMtime($storage, $internalPath);
} }
} }
/** /**
* preform a rename update * @param string $source
* * @param string $target
* @param string $from the relative path of the source file
* @param string $to the relative path of the target file
*/ */
static public function renameUpdate($from, $to) { public function rename($source, $target) {
/** /**
* @var \OC\Files\Storage\Storage $storageFrom * @var \OC\Files\Storage\Storage $sourceStorage
* @var \OC\Files\Storage\Storage $storageTo * @var \OC\Files\Storage\Storage $targetStorage
* @var string $internalFrom * @var string $sourceInternalPath
* @var string $internalTo * @var string $targetInternalPath
*/ */
list($storageFrom, $internalFrom) = self::resolvePath($from); list($sourceStorage, $sourceInternalPath) = $this->view->resolvePath($source);
// if it's a moved mountpoint we dont need to do anything // if it's a moved mountpoint we dont need to do anything
if ($internalFrom === '') { if ($sourceInternalPath === '') {
return; return;
} }
list($storageTo, $internalTo) = self::resolvePath($to); list($targetStorage, $targetInternalPath) = $this->view->resolvePath($target);
if ($storageFrom && $storageTo) {
if ($storageFrom === $storageTo) { if ($sourceStorage && $targetStorage) {
$cache = $storageFrom->getCache($internalFrom); if ($sourceStorage === $targetStorage) {
$cache->move($internalFrom, $internalTo); $cache = $sourceStorage->getCache($sourceInternalPath);
if (pathinfo($internalFrom, PATHINFO_EXTENSION) !== pathinfo($internalTo, PATHINFO_EXTENSION)) { $cache->move($sourceInternalPath, $targetInternalPath);
// redetect mime type change
$mimeType = $storageTo->getMimeType($internalTo); if (pathinfo($sourceInternalPath, PATHINFO_EXTENSION) !== pathinfo($targetInternalPath, PATHINFO_EXTENSION)) {
$fileId = $storageTo->getCache()->getId($internalTo); // handle mime type change
$storageTo->getCache()->update($fileId, array('mimetype' => $mimeType)); $mimeType = $sourceStorage->getMimeType($targetInternalPath);
$fileId = $cache->getId($targetInternalPath);
$cache->update($fileId, array('mimetype' => $mimeType));
} }
$cache->correctFolderSize($internalFrom);
$cache->correctFolderSize($internalTo); $cache->correctFolderSize($sourceInternalPath);
self::correctFolder($from, time()); $cache->correctFolderSize($targetInternalPath);
self::correctFolder($to, time()); $this->correctParentStorageMtime($sourceStorage, $sourceInternalPath);
self::correctParentStorageMtime($storageFrom, $internalFrom); $this->correctParentStorageMtime($targetStorage, $targetInternalPath);
self::correctParentStorageMtime($storageTo, $internalTo); $this->propagator->addChange($source);
$this->propagator->addChange($target);
} else { } else {
self::deleteUpdate($from); $this->remove($source);
self::writeUpdate($to); $this->update($target);
} }
} }
} }
/** /**
* get file owner and path * propagate the updates to their parent folders
* @param string $filename
* @return string[] with the owner's uid and the owner's path
*/
private static function getUidAndFilename($filename) {
$uid = \OC\Files\Filesystem::getOwner($filename);
\OC\Files\Filesystem::initMountPoints($uid);
$filename = (strpos($filename, '/') !== 0) ? '/' . $filename : $filename;
if ($uid != \OCP\User::getUser()) {
$info = \OC\Files\Filesystem::getFileInfo($filename);
if (!$info) {
return array($uid, '/files' . $filename);
}
$ownerView = new \OC\Files\View('/' . $uid . '/files');
$filename = $ownerView->getPath($info['fileid']);
}
return array($uid, '/files' . $filename);
}
/**
* Update the mtime and ETag of all parent folders
* *
* @param string $path * @param int $time (optional) the mtime to set for the folders, if not set the current time is used
* @param string $time
*/ */
static public function correctFolder($path, $time) { public function propagate($time = null) {
if ($path !== '' && $path !== '/' && $path !== '\\') { $this->propagator->propagateChanges($time);
list($owner, $realPath) = self::getUidAndFilename(dirname($path));
/**
* @var \OC\Files\Storage\Storage $storage
* @var string $internalPath
*/
$view = new \OC\Files\View('/' . $owner);
list($storage, $internalPath) = $view->resolvePath($realPath);
$cache = $storage->getCache();
$id = $cache->getId($internalPath);
while ($id !== -1) {
$cache->update($id, array('mtime' => $time, 'etag' => $storage->getETag($internalPath)));
if ($realPath !== '') {
$realPath = dirname($realPath);
if ($realPath === DIRECTORY_SEPARATOR) {
$realPath = "";
}
// check storage for parent in case we change the storage in this step
list($storage, $internalPath) = $view->resolvePath($realPath);
$cache = $storage->getCache();
$id = $cache->getId($internalPath);
} else {
$id = -1;
}
}
}
} }
/** /**
@ -177,52 +133,17 @@ class Updater {
* @param \OC\Files\Storage\Storage $storage * @param \OC\Files\Storage\Storage $storage
* @param string $internalPath * @param string $internalPath
*/ */
static private function correctParentStorageMtime($storage, $internalPath) { private function correctParentStorageMtime($storage, $internalPath) {
$cache = $storage->getCache(); $cache = $storage->getCache();
$parentId = $cache->getParentId($internalPath); $parentId = $cache->getParentId($internalPath);
$parent = dirname($internalPath); $parent = dirname($internalPath);
if ($parent === '.' || $parent === '/' || $parent === '\\') {
$parent = '';
}
if ($parentId != -1) { if ($parentId != -1) {
$cache->update($parentId, array('storage_mtime' => $storage->filemtime($parent))); $cache->update($parentId, array('storage_mtime' => $storage->filemtime($parent)));
} }
} }
/** public function __destruct() {
* @param array $params // propagate any leftover changes
*/ $this->propagator->propagateChanges();
static public function writeHook($params) {
self::writeUpdate($params['path']);
}
/**
* @param array $params
*/
static public function touchHook($params) {
$path = $params['path'];
list($storage, $internalPath) = self::resolvePath($path);
$cache = $storage->getCache();
$id = $cache->getId($internalPath);
if ($id !== -1) {
$cache->update($id, array('etag' => $storage->getETag($internalPath)));
}
self::writeUpdate($path);
}
/**
* @param array $params
*/
static public function renameHook($params) {
self::renameUpdate($params['oldpath'], $params['newpath']);
}
/**
* @param array $params
*/
static public function deleteHook($params) {
self::deleteUpdate($params['path']);
} }
} }

View file

@ -31,8 +31,14 @@ use OC\Files\Mount\MoveableMount;
class View { class View {
private $fakeRoot = ''; private $fakeRoot = '';
/**
* @var \OC\Files\Cache\Updater
*/
protected $updater;
public function __construct($root = '') { public function __construct($root = '') {
$this->fakeRoot = $root; $this->fakeRoot = $root;
$this->updater = new Updater($this);
} }
public function getAbsolutePath($path = '/') { public function getAbsolutePath($path = '/') {
@ -158,7 +164,10 @@ class View {
* for \OC\Files\Storage\Storage via basicOperation(). * for \OC\Files\Storage\Storage via basicOperation().
*/ */
public function mkdir($path) { public function mkdir($path) {
return $this->basicOperation('mkdir', $path, array('create', 'write')); $result = $this->basicOperation('mkdir', $path, array('create', 'write'));
$this->updater->update($path);
$this->updater->propagate();
return $result;
} }
/** /**
@ -168,10 +177,10 @@ class View {
* @param string $path relative to data/ * @param string $path relative to data/
* @return boolean * @return boolean
*/ */
protected function removeMount($mount, $path){ protected function removeMount($mount, $path) {
if ($mount instanceof MoveableMount) { if ($mount instanceof MoveableMount) {
// cut of /user/files to get the relative path to data/user/files // cut of /user/files to get the relative path to data/user/files
$pathParts= explode('/', $path, 4); $pathParts = explode('/', $path, 4);
$relPath = '/' . $pathParts[3]; $relPath = '/' . $pathParts[3];
\OC_Hook::emit( \OC_Hook::emit(
Filesystem::CLASSNAME, "umount", Filesystem::CLASSNAME, "umount",
@ -194,13 +203,16 @@ class View {
} }
public function rmdir($path) { public function rmdir($path) {
$absolutePath= $this->getAbsolutePath($path); $absolutePath = $this->getAbsolutePath($path);
$mount = Filesystem::getMountManager()->find($absolutePath); $mount = Filesystem::getMountManager()->find($absolutePath);
if ($mount->getInternalPath($absolutePath) === '') { if ($mount->getInternalPath($absolutePath) === '') {
return $this->removeMount($mount, $path); return $this->removeMount($mount, $path);
} }
if ($this->is_dir($path)) { if ($this->is_dir($path)) {
return $this->basicOperation('rmdir', $path, array('delete')); $result = $this->basicOperation('rmdir', $path, array('delete'));
$this->updater->remove($path);
$this->updater->propagate();
return $result;
} else { } else {
return false; return false;
} }
@ -310,9 +322,14 @@ class View {
if(!$this->file_exists($path)){ if(!$this->file_exists($path)){
return false; return false;
} }
if (is_null($mtime)) {
$mtime = time();
}
//if native touch fails, we emulate it by changing the mtime in the cache //if native touch fails, we emulate it by changing the mtime in the cache
$this->putFileInfo($path, array('mtime' => $mtime)); $this->putFileInfo($path, array('mtime' => $mtime));
} }
$this->updater->update($path);
$this->updater->propagate($mtime);
return true; return true;
} }
@ -374,10 +391,9 @@ 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);
$this->updater->update($path);
$this->updater->propagate();
if ($this->shouldEmitHooks($path) && $result !== false) { if ($this->shouldEmitHooks($path) && $result !== false) {
Updater::writeHook(array(
'path' => $this->getHookPath($path)
));
$this->emit_file_hooks_post($exists, $path); $this->emit_file_hooks_post($exists, $path);
} }
\OC_FileProxy::runPostProxies('file_put_contents', $absolutePath, $count); \OC_FileProxy::runPostProxies('file_put_contents', $absolutePath, $count);
@ -390,7 +406,10 @@ class View {
} }
} else { } else {
$hooks = ($this->file_exists($path)) ? array('update', 'write') : array('create', 'write'); $hooks = ($this->file_exists($path)) ? array('update', 'write') : array('create', 'write');
return $this->basicOperation('file_put_contents', $path, $hooks, $data); $result = $this->basicOperation('file_put_contents', $path, $hooks, $data);
$this->updater->update($path);
$this->updater->propagate();
return $result;
} }
} }
@ -405,7 +424,10 @@ class View {
if ($mount->getInternalPath($absolutePath) === '') { if ($mount->getInternalPath($absolutePath) === '') {
return $this->removeMount($mount, $absolutePath); return $this->removeMount($mount, $absolutePath);
} }
return $this->basicOperation('unlink', $path, array('delete')); $result = $this->basicOperation('unlink', $path, array('delete'));
$this->updater->remove($path);
$this->updater->propagate();
return $result;
} }
/** /**
@ -495,15 +517,16 @@ class View {
} }
} }
} }
if ($this->shouldEmitHooks() && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { if ((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
Updater::writeHook(array('path' => $this->getHookPath($path2))); $this->updater->update($path2);
$this->emit_file_hooks_post($exists, $path2); $this->updater->propagate();
if ($this->shouldEmitHooks()) {
$this->emit_file_hooks_post($exists, $path2);
}
} elseif ($this->shouldEmitHooks() && $result !== false) { } elseif ($this->shouldEmitHooks() && $result !== false) {
Updater::renameHook(array( $this->updater->rename($path1, $path2);
'oldpath' => $this->getHookPath($path1), $this->updater->propagate();
'newpath' => $this->getHookPath($path2)
));
\OC_Hook::emit( \OC_Hook::emit(
Filesystem::CLASSNAME, Filesystem::CLASSNAME,
Filesystem::signal_post_rename, Filesystem::signal_post_rename,
@ -582,6 +605,8 @@ class View {
fclose($target); fclose($target);
} }
} }
$this->updater->update($path2);
$this->updater->propagate();
if ($this->shouldEmitHooks() && $result !== false) { if ($this->shouldEmitHooks() && $result !== false) {
\OC_Hook::emit( \OC_Hook::emit(
Filesystem::CLASSNAME, Filesystem::CLASSNAME,
@ -814,16 +839,6 @@ class View {
$run = true; $run = true;
if ($this->shouldEmitHooks($path)) { if ($this->shouldEmitHooks($path)) {
foreach ($hooks as $hook) { foreach ($hooks as $hook) {
// manually triger updater hooks to ensure they are called first
if ($post) {
if ($hook == 'write') {
Updater::writeHook(array('path' => $path));
} elseif ($hook == 'touch') {
Updater::touchHook(array('path' => $path));
} else if ($hook == 'delete') {
Updater::deleteHook(array('path' => $path));
}
}
if ($hook != 'read') { if ($hook != 'read') {
\OC_Hook::emit( \OC_Hook::emit(
Filesystem::CLASSNAME, Filesystem::CLASSNAME,

View file

@ -266,7 +266,6 @@ class Updater extends \PHPUnit_Framework_TestCase {
$cachedData = $this->cache->get('foo.txt'); $cachedData = $this->cache->get('foo.txt');
$this->assertInternalType('string', $fooCachedData['etag']); $this->assertInternalType('string', $fooCachedData['etag']);
$this->assertInternalType('string', $cachedData['etag']); $this->assertInternalType('string', $cachedData['etag']);
$this->assertNotSame($fooCachedData['etag'], $cachedData['etag']);
$this->assertGreaterThanOrEqual($fooCachedData['mtime'], $cachedData['mtime']); $this->assertGreaterThanOrEqual($fooCachedData['mtime'], $cachedData['mtime']);
$cachedData = $this->cache->get(''); $cachedData = $this->cache->get('');