2012-11-08 20:12:40 +00: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-10-29 13:18:57 +00:00
|
|
|
|
2012-11-08 20:12:40 +00:00
|
|
|
/**
|
|
|
|
* listen to filesystem hooks and change the cache accordingly
|
|
|
|
*/
|
|
|
|
class Updater {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* resolve a path to a storage and internal path
|
|
|
|
*
|
2013-03-24 01:06:50 +00:00
|
|
|
* @param string $path the relative path
|
2014-05-11 17:13:51 +00:00
|
|
|
* @return array an array consisting of the storage and the internal path
|
2012-11-08 20:12:40 +00:00
|
|
|
*/
|
|
|
|
static public function resolvePath($path) {
|
|
|
|
$view = \OC\Files\Filesystem::getView();
|
|
|
|
return $view->resolvePath($path);
|
|
|
|
}
|
|
|
|
|
2013-03-24 01:06:50 +00:00
|
|
|
/**
|
2013-07-16 03:56:52 +00:00
|
|
|
* perform a write update
|
2013-03-24 01:06:50 +00:00
|
|
|
*
|
|
|
|
* @param string $path the relative path of the file
|
|
|
|
*/
|
2012-11-08 20:12:40 +00:00
|
|
|
static public function writeUpdate($path) {
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\Storage\Storage $storage
|
|
|
|
* @var string $internalPath
|
|
|
|
*/
|
|
|
|
list($storage, $internalPath) = self::resolvePath($path);
|
2012-12-31 01:27:38 +00:00
|
|
|
if ($storage) {
|
2013-01-01 19:19:53 +00:00
|
|
|
$cache = $storage->getCache($internalPath);
|
|
|
|
$scanner = $storage->getScanner($internalPath);
|
2014-02-28 13:23:07 +00:00
|
|
|
$data = $scanner->scan($internalPath, Scanner::SCAN_SHALLOW);
|
|
|
|
$cache->correctFolderSize($internalPath, $data);
|
2013-06-12 22:09:52 +00:00
|
|
|
self::correctFolder($path, $storage->filemtime($internalPath));
|
2013-10-29 13:18:57 +00:00
|
|
|
self::correctParentStorageMtime($storage, $internalPath);
|
2012-12-31 01:27:38 +00:00
|
|
|
}
|
2012-11-08 20:12:40 +00:00
|
|
|
}
|
|
|
|
|
2013-03-24 01:06:50 +00:00
|
|
|
/**
|
2013-07-16 03:56:52 +00:00
|
|
|
* perform a delete update
|
2013-03-24 01:06:50 +00:00
|
|
|
*
|
|
|
|
* @param string $path the relative path of the file
|
|
|
|
*/
|
2012-11-08 20:12:40 +00:00
|
|
|
static public function deleteUpdate($path) {
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\Storage\Storage $storage
|
|
|
|
* @var string $internalPath
|
|
|
|
*/
|
|
|
|
list($storage, $internalPath) = self::resolvePath($path);
|
2012-12-31 01:27:38 +00:00
|
|
|
if ($storage) {
|
2013-11-28 12:17:19 +00:00
|
|
|
$parent = dirname($internalPath);
|
|
|
|
if ($parent === '.') {
|
|
|
|
$parent = '';
|
|
|
|
}
|
2013-01-01 19:19:53 +00:00
|
|
|
$cache = $storage->getCache($internalPath);
|
2012-12-31 01:27:38 +00:00
|
|
|
$cache->remove($internalPath);
|
2013-11-28 12:17:19 +00:00
|
|
|
$cache->correctFolderSize($parent);
|
2013-06-12 22:09:52 +00:00
|
|
|
self::correctFolder($path, time());
|
2013-10-29 13:18:57 +00:00
|
|
|
self::correctParentStorageMtime($storage, $internalPath);
|
2012-12-31 02:26:48 +00:00
|
|
|
}
|
2012-12-31 00:54:51 +00:00
|
|
|
}
|
|
|
|
|
2013-03-24 01:06:50 +00: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 18:08:07 +00: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);
|
2013-11-27 17:48:24 +00:00
|
|
|
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));
|
|
|
|
}
|
2013-03-08 18:08:07 +00:00
|
|
|
$cache->correctFolderSize($internalFrom);
|
|
|
|
$cache->correctFolderSize($internalTo);
|
2013-06-12 22:09:52 +00:00
|
|
|
self::correctFolder($from, time());
|
|
|
|
self::correctFolder($to, time());
|
2013-10-29 13:18:57 +00:00
|
|
|
self::correctParentStorageMtime($storageFrom, $internalFrom);
|
|
|
|
self::correctParentStorageMtime($storageTo, $internalTo);
|
2013-03-08 18:08:07 +00:00
|
|
|
} else {
|
|
|
|
self::deleteUpdate($from);
|
|
|
|
self::writeUpdate($to);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-29 14:08:12 +00:00
|
|
|
/**
|
|
|
|
* @brief get file owner and path
|
|
|
|
* @param string $filename
|
2014-02-06 15:30:58 +00:00
|
|
|
* @return string[] with the oweners uid and the owners path
|
2013-10-29 14:08:12 +00:00
|
|
|
*/
|
2013-10-29 14:10:02 +00:00
|
|
|
private static function getUidAndFilename($filename) {
|
2013-10-29 14:08:12 +00:00
|
|
|
|
|
|
|
$uid = \OC\Files\Filesystem::getOwner($filename);
|
|
|
|
\OC\Files\Filesystem::initMountPoints($uid);
|
|
|
|
|
|
|
|
if ($uid != \OCP\User::getUser()) {
|
|
|
|
$info = \OC\Files\Filesystem::getFileInfo($filename);
|
2014-03-03 11:56:08 +00:00
|
|
|
if (!$info) {
|
|
|
|
return array($uid, '/files/' . $filename);
|
|
|
|
}
|
2013-10-29 14:08:12 +00:00
|
|
|
$ownerView = new \OC\Files\View('/' . $uid . '/files');
|
|
|
|
$filename = $ownerView->getPath($info['fileid']);
|
|
|
|
}
|
|
|
|
return array($uid, '/files/' . $filename);
|
|
|
|
}
|
|
|
|
|
2013-01-20 02:04:25 +00:00
|
|
|
/**
|
2013-03-08 18:08:07 +00:00
|
|
|
* Update the mtime and ETag of all parent folders
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param string $time
|
|
|
|
*/
|
2013-01-20 02:04:25 +00:00
|
|
|
static public function correctFolder($path, $time) {
|
2012-12-31 23:16:44 +00:00
|
|
|
if ($path !== '' && $path !== '/') {
|
2013-10-29 14:08:12 +00:00
|
|
|
|
|
|
|
list($owner, $realPath) = self::getUidAndFilename(dirname($path));
|
|
|
|
|
2012-12-31 00:54:51 +00:00
|
|
|
/**
|
2013-03-08 18:08:07 +00:00
|
|
|
* @var \OC\Files\Storage\Storage $storage
|
|
|
|
* @var string $internalPath
|
|
|
|
*/
|
2013-10-29 14:08:12 +00:00
|
|
|
$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)));
|
2013-10-29 15:30:47 +00:00
|
|
|
if ($realPath !== '') {
|
|
|
|
$realPath = dirname($realPath);
|
2014-03-03 11:56:08 +00:00
|
|
|
if ($realPath === DIRECTORY_SEPARATOR) {
|
2013-10-29 15:30:47 +00:00
|
|
|
$realPath = "";
|
|
|
|
}
|
|
|
|
// check storage for parent in case we change the storage in this step
|
|
|
|
list($storage, $internalPath) = $view->resolvePath($realPath);
|
2013-10-29 14:08:12 +00:00
|
|
|
$cache = $storage->getCache();
|
|
|
|
$id = $cache->getId($internalPath);
|
2013-06-10 16:35:47 +00:00
|
|
|
} else {
|
2013-10-29 14:08:12 +00:00
|
|
|
$id = -1;
|
2012-12-31 00:54:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-08 20:12:40 +00:00
|
|
|
}
|
|
|
|
|
2013-10-29 13:18:57 +00: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-08 20:12:40 +00:00
|
|
|
/**
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
static public function writeHook($params) {
|
|
|
|
self::writeUpdate($params['path']);
|
|
|
|
}
|
|
|
|
|
2013-03-25 15:26:17 +00:00
|
|
|
/**
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
static public function touchHook($params) {
|
2013-05-08 13:49:45 +00: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 15:26:17 +00:00
|
|
|
}
|
|
|
|
|
2012-11-08 20:12:40 +00:00
|
|
|
/**
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
static public function renameHook($params) {
|
2013-03-08 18:08:07 +00:00
|
|
|
self::renameUpdate($params['oldpath'], $params['newpath']);
|
2012-11-08 20:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
static public function deleteHook($params) {
|
|
|
|
self::deleteUpdate($params['path']);
|
|
|
|
}
|
|
|
|
}
|