2012-11-09 00:12:40 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Files\Cache;
|
2013-06-10 20:35:47 +04:00
|
|
|
use OCP\Util;
|
2012-11-09 00:12:40 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* listen to filesystem hooks and change the cache accordingly
|
|
|
|
*/
|
|
|
|
class Updater {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* resolve a path to a storage and internal path
|
|
|
|
*
|
2013-03-24 05:06:50 +04:00
|
|
|
* @param string $path the relative path
|
2012-11-09 00:12:40 +04:00
|
|
|
* @return array consisting of the storage and the internal path
|
|
|
|
*/
|
|
|
|
static public function resolvePath($path) {
|
|
|
|
$view = \OC\Files\Filesystem::getView();
|
|
|
|
return $view->resolvePath($path);
|
|
|
|
}
|
|
|
|
|
2013-03-24 05:06:50 +04:00
|
|
|
/**
|
|
|
|
* preform a write update
|
|
|
|
*
|
|
|
|
* @param string $path the relative path of the file
|
|
|
|
*/
|
2012-11-09 00:12:40 +04:00
|
|
|
static public function writeUpdate($path) {
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\Storage\Storage $storage
|
|
|
|
* @var string $internalPath
|
|
|
|
*/
|
|
|
|
list($storage, $internalPath) = self::resolvePath($path);
|
2012-12-31 05:27:38 +04:00
|
|
|
if ($storage) {
|
2013-01-01 23:19:53 +04:00
|
|
|
$cache = $storage->getCache($internalPath);
|
|
|
|
$scanner = $storage->getScanner($internalPath);
|
2012-12-31 05:27:38 +04:00
|
|
|
$scanner->scan($internalPath, Scanner::SCAN_SHALLOW);
|
|
|
|
$cache->correctFolderSize($internalPath);
|
2013-06-13 02:09:52 +04:00
|
|
|
self::correctFolder($path, $storage->filemtime($internalPath));
|
2012-12-31 05:27:38 +04:00
|
|
|
}
|
2012-11-09 00:12:40 +04:00
|
|
|
}
|
|
|
|
|
2013-03-24 05:06:50 +04:00
|
|
|
/**
|
|
|
|
* preform a delete update
|
|
|
|
*
|
|
|
|
* @param string $path the relative path of the file
|
|
|
|
*/
|
2012-11-09 00:12:40 +04:00
|
|
|
static public function deleteUpdate($path) {
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\Storage\Storage $storage
|
|
|
|
* @var string $internalPath
|
|
|
|
*/
|
|
|
|
list($storage, $internalPath) = self::resolvePath($path);
|
2012-12-31 05:27:38 +04:00
|
|
|
if ($storage) {
|
2013-01-01 23:19:53 +04:00
|
|
|
$cache = $storage->getCache($internalPath);
|
2012-12-31 05:27:38 +04:00
|
|
|
$cache->remove($internalPath);
|
|
|
|
$cache->correctFolderSize($internalPath);
|
2013-06-13 02:09:52 +04:00
|
|
|
self::correctFolder($path, time());
|
2012-12-31 06:26:48 +04:00
|
|
|
}
|
2012-12-31 04:54:51 +04:00
|
|
|
}
|
|
|
|
|
2013-03-24 05:06:50 +04:00
|
|
|
/**
|
|
|
|
* preform a rename update
|
|
|
|
*
|
|
|
|
* @param string $from the relative path of the source file
|
|
|
|
* @param string $to the relative path of the target file
|
|
|
|
*/
|
2013-03-08 22:08:07 +04:00
|
|
|
static public function renameUpdate($from, $to) {
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\Storage\Storage $storageFrom
|
|
|
|
* @var \OC\Files\Storage\Storage $storageTo
|
|
|
|
* @var string $internalFrom
|
|
|
|
* @var string $internalTo
|
|
|
|
*/
|
|
|
|
list($storageFrom, $internalFrom) = self::resolvePath($from);
|
|
|
|
list($storageTo, $internalTo) = self::resolvePath($to);
|
|
|
|
if ($storageFrom && $storageTo) {
|
|
|
|
if ($storageFrom === $storageTo) {
|
|
|
|
$cache = $storageFrom->getCache($internalFrom);
|
|
|
|
$cache->move($internalFrom, $internalTo);
|
|
|
|
$cache->correctFolderSize($internalFrom);
|
|
|
|
$cache->correctFolderSize($internalTo);
|
2013-06-13 02:09:52 +04:00
|
|
|
self::correctFolder($from, time());
|
|
|
|
self::correctFolder($to, time());
|
2013-03-08 22:08:07 +04:00
|
|
|
} else {
|
|
|
|
self::deleteUpdate($from);
|
|
|
|
self::writeUpdate($to);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-20 06:04:25 +04:00
|
|
|
/**
|
2013-03-08 22:08:07 +04:00
|
|
|
* Update the mtime and ETag of all parent folders
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param string $time
|
|
|
|
*/
|
2013-01-20 06:04:25 +04:00
|
|
|
static public function correctFolder($path, $time) {
|
2013-01-01 03:16:44 +04:00
|
|
|
if ($path !== '' && $path !== '/') {
|
2012-12-31 04:54:51 +04:00
|
|
|
$parent = dirname($path);
|
|
|
|
if ($parent === '.') {
|
|
|
|
$parent = '';
|
|
|
|
}
|
|
|
|
/**
|
2013-03-08 22:08:07 +04:00
|
|
|
* @var \OC\Files\Storage\Storage $storage
|
|
|
|
* @var string $internalPath
|
|
|
|
*/
|
2012-12-31 04:54:51 +04:00
|
|
|
list($storage, $internalPath) = self::resolvePath($parent);
|
|
|
|
if ($storage) {
|
|
|
|
$cache = $storage->getCache();
|
|
|
|
$id = $cache->getId($internalPath);
|
|
|
|
if ($id !== -1) {
|
2013-01-20 06:04:25 +04:00
|
|
|
$cache->update($id, array('mtime' => $time, 'etag' => $storage->getETag($internalPath)));
|
|
|
|
self::correctFolder($parent, $time);
|
2013-06-10 20:35:47 +04:00
|
|
|
} else {
|
|
|
|
Util::writeLog('core', 'Path not in cache: '.$internalPath, Util::ERROR);
|
2012-12-31 04:54:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-09 00:12:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
static public function writeHook($params) {
|
|
|
|
self::writeUpdate($params['path']);
|
|
|
|
}
|
|
|
|
|
2013-03-25 19:26:17 +04:00
|
|
|
/**
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
static public function touchHook($params) {
|
2013-05-08 17:49:45 +04:00
|
|
|
$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);
|
2013-03-25 19:26:17 +04:00
|
|
|
}
|
|
|
|
|
2012-11-09 00:12:40 +04:00
|
|
|
/**
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
static public function renameHook($params) {
|
2013-03-08 22:08:07 +04:00
|
|
|
self::renameUpdate($params['oldpath'], $params['newpath']);
|
2012-11-09 00:12:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
static public function deleteHook($params) {
|
|
|
|
self::deleteUpdate($params['path']);
|
|
|
|
}
|
|
|
|
}
|