nextcloud/lib/private/files/cache/updater.php

219 lines
6.0 KiB
PHP
Raw Normal View History

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;
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
/**
2013-07-16 07:56:52 +04:00
* perform a write update
2013-03-24 05:06:50 +04:00
*
* @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);
if ($storage) {
$cache = $storage->getCache($internalPath);
$scanner = $storage->getScanner($internalPath);
$scanner->scan($internalPath, Scanner::SCAN_SHALLOW);
$cache->correctFolderSize($internalPath);
self::correctFolder($path, $storage->filemtime($internalPath));
self::correctParentStorageMtime($storage, $internalPath);
}
2012-11-09 00:12:40 +04:00
}
2013-03-24 05:06:50 +04:00
/**
2013-07-16 07:56:52 +04:00
* perform a delete update
2013-03-24 05:06:50 +04:00
*
* @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);
if ($storage) {
$parent = dirname($internalPath);
if ($parent === '.') {
$parent = '';
}
$cache = $storage->getCache($internalPath);
$cache->remove($internalPath);
$cache->correctFolderSize($parent);
self::correctFolder($path, time());
self::correctParentStorageMtime($storage, $internalPath);
}
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
*/
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);
if (pathinfo($internalFrom, PATHINFO_EXTENSION) !== pathinfo($internalTo, PATHINFO_EXTENSION)) {
// redetect mime type change
$mimeType = $storageTo->getMimeType($internalTo);
$fileId = $storageTo->getCache()->getId($internalTo);
$storageTo->getCache()->update($fileId, array('mimetype' => $mimeType));
}
$cache->correctFolderSize($internalFrom);
$cache->correctFolderSize($internalTo);
self::correctFolder($from, time());
self::correctFolder($to, time());
self::correctParentStorageMtime($storageFrom, $internalFrom);
self::correctParentStorageMtime($storageTo, $internalTo);
} else {
self::deleteUpdate($from);
self::writeUpdate($to);
}
}
}
/**
* @brief get file owner and path
* @param string $filename
* @return array with the oweners uid and the owners path
*/
2013-10-29 18:10:02 +04:00
private static function getUidAndFilename($filename) {
$uid = \OC\Files\Filesystem::getOwner($filename);
\OC\Files\Filesystem::initMountPoints($uid);
if ($uid != \OCP\User::getUser()) {
$info = \OC\Files\Filesystem::getFileInfo($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 string $time
*/
static public function correctFolder($path, $time) {
2013-01-01 03:16:44 +04:00
if ($path !== '' && $path !== '/') {
list($owner, $realPath) = self::getUidAndFilename(dirname($path));
2012-12-31 04:54:51 +04:00
/**
* @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;
2012-12-31 04:54:51 +04:00
}
}
}
2012-11-09 00:12:40 +04:00
}
/**
* update the storage_mtime of the parent
*
* @param \OC\Files\Storage\Storage $storage
* @param string $internalPath
*/
static private function correctParentStorageMtime($storage, $internalPath) {
$cache = $storage->getCache();
$parentId = $cache->getParentId($internalPath);
$parent = dirname($internalPath);
if ($parentId != -1) {
$cache->update($parentId, array('storage_mtime' => $storage->filemtime($parent)));
}
}
2012-11-09 00:12:40 +04:00
/**
* @param array $params
*/
static public function writeHook($params) {
self::writeUpdate($params['path']);
}
/**
* @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);
}
2012-11-09 00:12:40 +04:00
/**
* @param array $params
*/
static public function renameHook($params) {
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']);
}
}