summaryrefslogtreecommitdiffstats
path: root/lib/files/cache
diff options
context:
space:
mode:
Diffstat (limited to 'lib/files/cache')
-rw-r--r--lib/files/cache/cache.php527
-rw-r--r--lib/files/cache/legacy.php81
-rw-r--r--lib/files/cache/permissions.php102
-rw-r--r--lib/files/cache/scanner.php162
-rw-r--r--lib/files/cache/updater.php105
-rw-r--r--lib/files/cache/upgrade.php159
-rw-r--r--lib/files/cache/watcher.php72
7 files changed, 1208 insertions, 0 deletions
diff --git a/lib/files/cache/cache.php b/lib/files/cache/cache.php
new file mode 100644
index 00000000000..dcb6e8fd39a
--- /dev/null
+++ b/lib/files/cache/cache.php
@@ -0,0 +1,527 @@
+<?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;
+
+/**
+ * Metadata cache for the filesystem
+ *
+ * don't use this class directly if you need to get metadata, use \OC\Files\Filesystem::getFileInfo instead
+ */
+class Cache {
+ const NOT_FOUND = 0;
+ const PARTIAL = 1; //only partial data available, file not cached in the database
+ const SHALLOW = 2; //folder in cache, but not all child files are completely scanned
+ const COMPLETE = 3;
+
+ /**
+ * @var array partial data for the cache
+ */
+ private $partial = array();
+
+ /**
+ * @var string
+ */
+ private $storageId;
+
+ /**
+ * numeric storage id
+ *
+ * @var int $numericId
+ */
+ private $numericId;
+
+ private $mimetypeIds = array();
+ private $mimetypes = array();
+
+ /**
+ * @param \OC\Files\Storage\Storage|string $storage
+ */
+ public function __construct($storage) {
+ if ($storage instanceof \OC\Files\Storage\Storage) {
+ $this->storageId = $storage->getId();
+ } else {
+ $this->storageId = $storage;
+ }
+
+ $query = \OC_DB::prepare('SELECT `numeric_id` FROM `*PREFIX*storages` WHERE `id` = ?');
+ $result = $query->execute(array($this->storageId));
+ if ($row = $result->fetchRow()) {
+ $this->numericId = $row['numeric_id'];
+ } else {
+ $query = \OC_DB::prepare('INSERT INTO `*PREFIX*storages`(`id`) VALUES(?)');
+ $query->execute(array($this->storageId));
+ $this->numericId = \OC_DB::insertid('*PREFIX*filecache');
+ }
+ }
+
+ public function getNumericStorageId() {
+ return $this->numericId;
+ }
+
+ /**
+ * normalize mimetypes
+ *
+ * @param string $mime
+ * @return int
+ */
+ public function getMimetypeId($mime) {
+ if (!isset($this->mimetypeIds[$mime])) {
+ $query = \OC_DB::prepare('SELECT `id` FROM `*PREFIX*mimetypes` WHERE `mimetype` = ?');
+ $result = $query->execute(array($mime));
+ if ($row = $result->fetchRow()) {
+ $this->mimetypeIds[$mime] = $row['id'];
+ } else {
+ $query = \OC_DB::prepare('INSERT INTO `*PREFIX*mimetypes`(`mimetype`) VALUES(?)');
+ $query->execute(array($mime));
+ $this->mimetypeIds[$mime] = \OC_DB::insertid('*PREFIX*mimetypes');
+ }
+ $this->mimetypes[$this->mimetypeIds[$mime]] = $mime;
+ }
+ return $this->mimetypeIds[$mime];
+ }
+
+ public function getMimetype($id) {
+ if (!isset($this->mimetypes[$id])) {
+ $query = \OC_DB::prepare('SELECT `mimetype` FROM `*PREFIX*mimetypes` WHERE `id` = ?');
+ $result = $query->execute(array($id));
+ if ($row = $result->fetchRow()) {
+ $this->mimetypes[$id] = $row['mimetype'];
+ } else {
+ return null;
+ }
+ }
+ return $this->mimetypes[$id];
+ }
+
+ /**
+ * get the stored metadata of a file or folder
+ *
+ * @param string/int $file
+ * @return array
+ */
+ public function get($file) {
+ if (is_string($file) or $file == '') {
+ $where = 'WHERE `storage` = ? AND `path_hash` = ?';
+ $params = array($this->numericId, md5($file));
+ } else { //file id
+ $where = 'WHERE `fileid` = ?';
+ $params = array($file);
+ }
+ $query = \OC_DB::prepare(
+ 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, `encrypted`, `etag`
+ FROM `*PREFIX*filecache` ' . $where);
+ $result = $query->execute($params);
+ $data = $result->fetchRow();
+
+ //merge partial data
+ if (!$data and is_string($file)) {
+ if (isset($this->partial[$file])) {
+ $data = $this->partial[$file];
+ }
+ } else {
+ //fix types
+ $data['fileid'] = (int)$data['fileid'];
+ $data['size'] = (int)$data['size'];
+ $data['mtime'] = (int)$data['mtime'];
+ $data['encrypted'] = (bool)$data['encrypted'];
+ $data['storage'] = $this->storageId;
+ $data['mimetype'] = $this->getMimetype($data['mimetype']);
+ $data['mimepart'] = $this->getMimetype($data['mimepart']);
+ }
+
+ return $data;
+ }
+
+ /**
+ * get the metadata of all files stored in $folder
+ *
+ * @param string $folder
+ * @return array
+ */
+ public function getFolderContents($folder) {
+ $fileId = $this->getId($folder);
+ if ($fileId > -1) {
+ $query = \OC_DB::prepare(
+ 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, `encrypted`, `etag`
+ FROM `*PREFIX*filecache` WHERE parent = ? ORDER BY `name` ASC');
+ $result = $query->execute(array($fileId));
+ $files = $result->fetchAll();
+ foreach ($files as &$file) {
+ $file['mimetype'] = $this->getMimetype($file['mimetype']);
+ $file['mimepart'] = $this->getMimetype($file['mimepart']);
+ }
+ return $files;
+ } else {
+ return array();
+ }
+ }
+
+ /**
+ * store meta data for a file or folder
+ *
+ * @param string $file
+ * @param array $data
+ *
+ * @return int file id
+ */
+ public function put($file, array $data) {
+ if (($id = $this->getId($file)) > -1) {
+ $this->update($id, $data);
+ return $id;
+ } else {
+ if (isset($this->partial[$file])) { //add any saved partial data
+ $data = array_merge($this->partial[$file], $data);
+ unset($this->partial[$file]);
+ }
+
+ $requiredFields = array('size', 'mtime', 'mimetype');
+ foreach ($requiredFields as $field) {
+ if (!isset($data[$field])) { //data not complete save as partial and return
+ $this->partial[$file] = $data;
+ return -1;
+ }
+ }
+
+ $data['path'] = $file;
+ $data['parent'] = $this->getParentId($file);
+ $data['name'] = basename($file);
+ $data['encrypted'] = isset($data['encrypted']) ? ((int)$data['encrypted']) : 0;
+
+ list($queryParts, $params) = $this->buildParts($data);
+ $queryParts[] = '`storage`';
+ $params[] = $this->numericId;
+ $valuesPlaceholder = array_fill(0, count($queryParts), '?');
+
+ $query = \OC_DB::prepare('INSERT INTO `*PREFIX*filecache`(' . implode(', ', $queryParts) . ') VALUES(' . implode(', ', $valuesPlaceholder) . ')');
+ $query->execute($params);
+
+ return (int)\OC_DB::insertid('*PREFIX*filecache');
+ }
+ }
+
+ /**
+ * update the metadata in the cache
+ *
+ * @param int $id
+ * @param array $data
+ */
+ public function update($id, array $data) {
+ list($queryParts, $params) = $this->buildParts($data);
+ $params[] = $id;
+
+ $query = \OC_DB::prepare('UPDATE `*PREFIX*filecache` SET ' . implode(' = ?, ', $queryParts) . '=? WHERE fileid = ?');
+ $query->execute($params);
+ }
+
+ /**
+ * extract query parts and params array from data array
+ *
+ * @param array $data
+ * @return array
+ */
+ function buildParts(array $data) {
+ $fields = array('path', 'parent', 'name', 'mimetype', 'size', 'mtime', 'encrypted', 'etag');
+ $params = array();
+ $queryParts = array();
+ foreach ($data as $name => $value) {
+ if (array_search($name, $fields) !== false) {
+ if ($name === 'path') {
+ $params[] = md5($value);
+ $queryParts[] = '`path_hash`';
+ } elseif ($name === 'mimetype') {
+ $params[] = $this->getMimetypeId(substr($value, 0, strpos($value, '/')));
+ $queryParts[] = '`mimepart`';
+ $value = $this->getMimetypeId($value);
+ }
+ $params[] = $value;
+ $queryParts[] = '`' . $name . '`';
+ }
+ }
+ return array($queryParts, $params);
+ }
+
+ /**
+ * get the file id for a file
+ *
+ * @param string $file
+ * @return int
+ */
+ public function getId($file) {
+ $pathHash = md5($file);
+
+ $query = \OC_DB::prepare('SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?');
+ $result = $query->execute(array($this->numericId, $pathHash));
+
+ if ($row = $result->fetchRow()) {
+ return $row['fileid'];
+ } else {
+ return -1;
+ }
+ }
+
+ /**
+ * get the id of the parent folder of a file
+ *
+ * @param string $file
+ * @return int
+ */
+ public function getParentId($file) {
+ if ($file === '') {
+ return -1;
+ } else {
+ $parent = dirname($file);
+ if ($parent === '.') {
+ $parent = '';
+ }
+ return $this->getId($parent);
+ }
+ }
+
+ /**
+ * check if a file is available in the cache
+ *
+ * @param string $file
+ * @return bool
+ */
+ public function inCache($file) {
+ return $this->getId($file) != -1;
+ }
+
+ /**
+ * remove a file or folder from the cache
+ *
+ * @param string $file
+ */
+ public function remove($file) {
+ $entry = $this->get($file);
+ if ($entry['mimetype'] === 'httpd/unix-directory') {
+ $children = $this->getFolderContents($file);
+ foreach ($children as $child) {
+ $this->remove($child['path']);
+ }
+ }
+ $query = \OC_DB::prepare('DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?');
+ $query->execute(array($entry['fileid']));
+ }
+
+ /**
+ * Move a file or folder in the cache
+ *
+ * @param string $source
+ * @param string $target
+ */
+ public function move($source, $target) {
+ $sourceId = $this->getId($source);
+ $newParentId = $this->getParentId($target);
+
+ //find all child entries
+ $query = \OC_DB::prepare('SELECT `path`, `fileid` FROM `*PREFIX*filecache` WHERE `path` LIKE ?');
+ $result = $query->execute(array($source . '/%'));
+ $childEntries = $result->fetchAll();
+ $sourceLength = strlen($source);
+ $query = \OC_DB::prepare('UPDATE `*PREFIX*filecache` SET `path` = ?, `path_hash` = ? WHERE `fileid` = ?');
+
+ foreach ($childEntries as $child) {
+ $targetPath = $target . substr($child['path'], $sourceLength);
+ $query->execute(array($targetPath, md5($targetPath), $child['fileid']));
+ }
+
+ $query = \OC_DB::prepare('UPDATE `*PREFIX*filecache` SET `path` = ?, `path_hash` = ?, `parent` =? WHERE `fileid` = ?');
+ $query->execute(array($target, md5($target), $newParentId, $sourceId));
+ }
+
+ /**
+ * remove all entries for files that are stored on the storage from the cache
+ */
+ public function clear() {
+ $query = \OC_DB::prepare('DELETE FROM `*PREFIX*filecache` WHERE storage = ?');
+ $query->execute(array($this->numericId));
+
+ $query = \OC_DB::prepare('DELETE FROM `*PREFIX*storages` WHERE id = ?');
+ $query->execute(array($this->storageId));
+ }
+
+ /**
+ * @param string $file
+ *
+ * @return int, Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
+ */
+ public function getStatus($file) {
+ $pathHash = md5($file);
+ $query = \OC_DB::prepare('SELECT `size` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?');
+ $result = $query->execute(array($this->numericId, $pathHash));
+ if ($row = $result->fetchRow()) {
+ if ((int)$row['size'] === -1) {
+ return self::SHALLOW;
+ } else {
+ return self::COMPLETE;
+ }
+ } else {
+ if (isset($this->partial[$file])) {
+ return self::PARTIAL;
+ } else {
+ return self::NOT_FOUND;
+ }
+ }
+ }
+
+ /**
+ * search for files matching $pattern
+ *
+ * @param string $pattern
+ * @return array of file data
+ */
+ public function search($pattern) {
+ $query = \OC_DB::prepare('
+ SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, `encrypted`, `etag`
+ FROM `*PREFIX*filecache` WHERE `name` LIKE ? AND `storage` = ?'
+ );
+ $result = $query->execute(array($pattern, $this->numericId));
+ $files = array();
+ while ($row = $result->fetchRow()) {
+ $row['mimetype'] = $this->getMimetype($row['mimetype']);
+ $row['mimepart'] = $this->getMimetype($row['mimepart']);
+ $files[] = $row;
+ }
+ return $files;
+ }
+
+ /**
+ * search for files by mimetype
+ *
+ * @param string $mimetype
+ * @return array
+ */
+ public function searchByMime($mimetype) {
+ if (strpos($mimetype, '/')) {
+ $where = '`mimetype` = ?';
+ } else {
+ $where = '`mimepart` = ?';
+ }
+ $query = \OC_DB::prepare('
+ SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, `encrypted`, `etag`
+ FROM `*PREFIX*filecache` WHERE ' . $where . ' AND `storage` = ?'
+ );
+ $mimetype = $this->getMimetypeId($mimetype);
+ $result = $query->execute(array($mimetype, $this->numericId));
+ $files = array();
+ while ($row = $result->fetchRow()) {
+ $row['mimetype'] = $this->getMimetype($row['mimetype']);
+ $row['mimepart'] = $this->getMimetype($row['mimepart']);
+ $files[] = $row;
+ }
+ return $files;
+ }
+
+ /**
+ * update the folder size and the size of all parent folders
+ *
+ * @param $path
+ */
+ public function correctFolderSize($path) {
+ $this->calculateFolderSize($path);
+ if ($path !== '') {
+ $parent = dirname($path);
+ if ($parent === '.') {
+ $parent = '';
+ }
+ $this->correctFolderSize($parent);
+ }
+ }
+
+ /**
+ * get the size of a folder and set it in the cache
+ *
+ * @param string $path
+ * @return int
+ */
+ public function calculateFolderSize($path) {
+ $id = $this->getId($path);
+ if ($id === -1) {
+ return 0;
+ }
+ $query = \OC_DB::prepare('SELECT `size` FROM `*PREFIX*filecache` WHERE `parent` = ? AND `storage` = ?');
+ $result = $query->execute(array($id, $this->numericId));
+ $totalSize = 0;
+ $hasChilds = 0;
+ while ($row = $result->fetchRow()) {
+ $hasChilds = true;
+ $size = (int)$row['size'];
+ if ($size === -1) {
+ $totalSize = -1;
+ break;
+ } else {
+ $totalSize += $size;
+ }
+ }
+
+ if ($hasChilds) {
+ $this->update($id, array('size' => $totalSize));
+ }
+ return $totalSize;
+ }
+
+ /**
+ * get all file ids on the files on the storage
+ *
+ * @return int[]
+ */
+ public function getAll() {
+ $query = \OC_DB::prepare('SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ?');
+ $result = $query->execute(array($this->numericId));
+ $ids = array();
+ while ($row = $result->fetchRow()) {
+ $ids[] = $row['fileid'];
+ }
+ return $ids;
+ }
+
+ /**
+ * find a folder in the cache which has not been fully scanned
+ *
+ * If multiply incomplete folders are in the cache, the one with the highest id will be returned,
+ * use the one with the highest id gives the best result with the background scanner, since that is most
+ * likely the folder where we stopped scanning previously
+ *
+ * @return string|bool the path of the folder or false when no folder matched
+ */
+ public function getIncomplete() {
+ $query = \OC_DB::prepare('SELECT `path` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `size` = -1 ORDER BY `fileid` DESC LIMIT 1');
+ $query->execute(array($this->numericId));
+ if ($row = $query->fetchRow()) {
+ return $row['path'];
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * get the storage id of the storage for a file and the internal path of the file
+ *
+ * @return array, first element holding the storage id, second the path
+ */
+ static public function getById($id) {
+ $query = \OC_DB::prepare('SELECT `storage`, `path` FROM `*PREFIX*filecache` WHERE `fileid` = ?');
+ $result = $query->execute(array($id));
+ if ($row = $result->fetchRow()) {
+ $numericId = $row['storage'];
+ $path = $row['path'];
+ } else {
+ return null;
+ }
+
+ $query = \OC_DB::prepare('SELECT `id` FROM `*PREFIX*storages` WHERE `numeric_id` = ?');
+ $result = $query->execute(array($numericId));
+ if ($row = $result->fetchRow()) {
+ return array($row['id'], $path);
+ } else {
+ return null;
+ }
+ }
+}
diff --git a/lib/files/cache/legacy.php b/lib/files/cache/legacy.php
new file mode 100644
index 00000000000..33d4b8e7c9f
--- /dev/null
+++ b/lib/files/cache/legacy.php
@@ -0,0 +1,81 @@
+<?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;
+
+/**
+ * Provide read only support for the old filecache
+ */
+class Legacy {
+ private $user;
+
+ private $cacheHasItems = null;
+
+ public function __construct($user) {
+ $this->user = $user;
+ }
+
+ function getCount() {
+ $query = \OC_DB::prepare('SELECT COUNT(`id`) AS `count` FROM `*PREFIX*fscache` WHERE `user` = ?');
+ $result = $query->execute(array($this->user));
+ if ($row = $result->fetchRow()) {
+ return $row['count'];
+ } else {
+ return 0;
+ }
+ }
+
+ /**
+ * check if a legacy cache is present and holds items
+ *
+ * @return bool
+ */
+ function hasItems() {
+ if (!is_null($this->cacheHasItems)) {
+ return $this->cacheHasItems;
+ }
+ try {
+ $query = \OC_DB::prepare('SELECT `id` FROM `*PREFIX*fscache` WHERE `user` = ? LIMIT 1');
+ } catch (\Exception $e) {
+ $this->cacheHasItems = false;
+ return false;
+ }
+ try {
+ $result = $query->execute(array($this->user));
+ } catch (\Exception $e) {
+ $this->cacheHasItems = false;
+ return false;
+ }
+ $this->cacheHasItems = (bool)$result->fetchRow();
+ return $this->cacheHasItems;
+ }
+
+ /**
+ * @param string|int $path
+ * @return array
+ */
+ function get($path) {
+ if (is_numeric($path)) {
+ $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*fscache` WHERE `id` = ?');
+ } else {
+ $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*fscache` WHERE `path` = ?');
+ }
+ $result = $query->execute(array($path));
+ return $result->fetchRow();
+ }
+
+ /**
+ * @param int $id
+ * @return array
+ */
+ function getChildren($id) {
+ $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*fscache` WHERE `parent` = ?');
+ $result = $query->execute(array($id));
+ return $result->fetchAll();
+ }
+}
diff --git a/lib/files/cache/permissions.php b/lib/files/cache/permissions.php
new file mode 100644
index 00000000000..d0968337f02
--- /dev/null
+++ b/lib/files/cache/permissions.php
@@ -0,0 +1,102 @@
+<?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;
+
+class Permissions {
+ /**
+ * @var string $storageId
+ */
+ private $storageId;
+
+ /**
+ * @param \OC\Files\Storage\Storage|string $storage
+ */
+ public function __construct($storage){
+ if($storage instanceof \OC\Files\Storage\Storage){
+ $this->storageId = $storage->getId();
+ }else{
+ $this->storageId = $storage;
+ }
+ }
+
+ /**
+ * get the permissions for a single file
+ *
+ * @param int $fileId
+ * @param string $user
+ * @return int (-1 if file no permissions set)
+ */
+ public function get($fileId, $user) {
+ $query = \OC_DB::prepare('SELECT `permissions` FROM `*PREFIX*permissions` WHERE `user` = ? AND `fileid` = ?');
+ $result = $query->execute(array($user, $fileId));
+ if ($row = $result->fetchRow()) {
+ return $row['permissions'];
+ } else {
+ return -1;
+ }
+ }
+
+ /**
+ * set the permissions of a file
+ *
+ * @param int $fileId
+ * @param string $user
+ * @param int $permissions
+ */
+ public function set($fileId, $user, $permissions) {
+ if (self::get($fileId, $user) !== -1) {
+ $query = \OC_DB::prepare('UPDATE `*PREFIX*permissions` SET `permissions` = ? WHERE `user` = ? AND `fileid` = ?');
+ } else {
+ $query = \OC_DB::prepare('INSERT INTO `*PREFIX*permissions`(`permissions`, `user`, `fileid`) VALUES(?, ?,? )');
+ }
+ $query->execute(array($permissions, $user, $fileId));
+ }
+
+ /**
+ * get the permissions of multiply files
+ *
+ * @param int[] $fileIds
+ * @param string $user
+ * @return int[]
+ */
+ public function getMultiple($fileIds, $user) {
+ if (count($fileIds) === 0) {
+ return array();
+ }
+ $params = $fileIds;
+ $params[] = $user;
+ $inPart = implode(', ', array_fill(0, count($fileIds), '?'));
+
+ $query = \OC_DB::prepare('SELECT `fileid`, `permissions` FROM `*PREFIX*permissions` WHERE `fileid` IN (' . $inPart . ') AND `user` = ?');
+ $result = $query->execute($params);
+ $filePermissions = array();
+ while ($row = $result->fetchRow()) {
+ $filePermissions[$row['fileid']] = $row['permissions'];
+ }
+ return $filePermissions;
+ }
+
+ /**
+ * remove the permissions for a file
+ *
+ * @param int $fileId
+ * @param string $user
+ */
+ public function remove($fileId, $user) {
+ $query = \OC_DB::prepare('DELETE FROM `*PREFIX*permissions` WHERE `fileid` = ? AND `user` = ?');
+ $query->execute(array($fileId, $user));
+ }
+
+ public function removeMultiple($fileIds, $user) {
+ $query = \OC_DB::prepare('DELETE FROM `*PREFIX*permissions` WHERE `fileid` = ? AND `user` = ?');
+ foreach($fileIds as $fileId){
+ $query->execute(array($fileId, $user));
+ }
+ }
+}
diff --git a/lib/files/cache/scanner.php b/lib/files/cache/scanner.php
new file mode 100644
index 00000000000..5a9a119458e
--- /dev/null
+++ b/lib/files/cache/scanner.php
@@ -0,0 +1,162 @@
+<?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;
+
+class Scanner {
+ /**
+ * @var \OC\Files\Storage\Storage $storage
+ */
+ private $storage;
+
+ /**
+ * @var string $storageId
+ */
+ private $storageId;
+
+ /**
+ * @var \OC\Files\Cache\Cache $cache
+ */
+ private $cache;
+
+ const SCAN_RECURSIVE = true;
+ const SCAN_SHALLOW = false;
+
+ public function __construct(\OC\Files\Storage\Storage $storage) {
+ $this->storage = $storage;
+ $this->storageId = $this->storage->getId();
+ $this->cache = $storage->getCache();
+ }
+
+ /**
+ * get all the metadata of a file or folder
+ * *
+ *
+ * @param string $path
+ * @return array with metadata of the file
+ */
+ public function getData($path) {
+ $data = array();
+ if (!$this->storage->isReadable($path)) return null; //cant read, nothing we can do
+ $data['mimetype'] = $this->storage->getMimeType($path);
+ $data['mtime'] = $this->storage->filemtime($path);
+ if ($data['mimetype'] == 'httpd/unix-directory') {
+ $data['size'] = -1; //unknown
+ } else {
+ $data['size'] = $this->storage->filesize($path);
+ }
+ $data['etag'] = $this->storage->getETag($path);
+ return $data;
+ }
+
+ /**
+ * scan a single file and store it in the cache
+ *
+ * @param string $file
+ * @return array with metadata of the scanned file
+ */
+ public function scanFile($file) {
+ \OC_Hook::emit('\OC\Files\Cache\Scanner', 'scan_file', array('path' => $file, 'storage' => $this->storageId));
+ $data = $this->getData($file);
+ if ($data) {
+ if ($file) {
+ $parent = dirname($file);
+ if ($parent === '.') {
+ $parent = '';
+ }
+ if (!$this->cache->inCache($parent)) {
+ $this->scanFile($parent);
+ }
+ }
+ $id = $this->cache->put($file, $data);
+ }
+ return $data;
+ }
+
+ /**
+ * scan all the files in a folder and store them in the cache
+ *
+ * @param string $path
+ * @param SCAN_RECURSIVE/SCAN_SHALLOW $recursive
+ * @param bool $onlyChilds
+ * @return int the size of the scanned folder or -1 if the size is unknown at this stage
+ */
+ public function scan($path, $recursive = self::SCAN_RECURSIVE, $onlyChilds = false) {
+ \OC_Hook::emit('\OC\Files\Cache\Scanner', 'scan_folder', array('path' => $path, 'storage' => $this->storageId));
+ $childQueue = array();
+ if (!$onlyChilds) {
+ $this->scanFile($path);
+ }
+
+ $size = 0;
+ if ($this->storage->is_dir($path) && ($dh = $this->storage->opendir($path))) {
+ \OC_DB::beginTransaction();
+ while ($file = readdir($dh)) {
+ if (!$this->isIgnoredFile($file)) {
+ $child = ($path) ? $path . '/' . $file : $file;
+ $data = $this->scanFile($child);
+ if ($data) {
+ if ($data['mimetype'] === 'httpd/unix-directory') {
+ if ($recursive === self::SCAN_RECURSIVE) {
+ $childQueue[] = $child;
+ $data['size'] = 0;
+ } else {
+ $data['size'] = -1;
+ }
+ } else {
+ }
+ if ($data['size'] === -1) {
+ $size = -1;
+ } elseif ($size !== -1) {
+ $size += $data['size'];
+ }
+ }
+ }
+ }
+ \OC_DB::commit();
+ foreach ($childQueue as $child) {
+ $childSize = $this->scan($child, self::SCAN_RECURSIVE, true);
+ if ($childSize === -1) {
+ $size = -1;
+ } else {
+ $size += $childSize;
+ }
+ }
+ if ($size !== -1) {
+ $this->cache->put($path, array('size' => $size));
+ }
+ }
+ return $size;
+ }
+
+ /**
+ * @brief check if the file should be ignored when scanning
+ * NOTE: files with a '.part' extension are ignored as well!
+ * prevents unfinished put requests to be scanned
+ * @param String $file
+ * @return boolean
+ */
+ private function isIgnoredFile($file) {
+ if ($file === '.' || $file === '..'
+ || pathinfo($file,PATHINFO_EXTENSION) === 'part')
+ {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * walk over any folders that are not fully scanned yet and scan them
+ */
+ public function backgroundScan() {
+ while (($path = $this->cache->getIncomplete()) !== false) {
+ $this->scan($path);
+ $this->cache->correctFolderSize($path);
+ }
+ }
+}
diff --git a/lib/files/cache/updater.php b/lib/files/cache/updater.php
new file mode 100644
index 00000000000..d04541c219f
--- /dev/null
+++ b/lib/files/cache/updater.php
@@ -0,0 +1,105 @@
+<?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;
+
+/**
+ * listen to filesystem hooks and change the cache accordingly
+ */
+class Updater {
+
+ /**
+ * resolve a path to a storage and internal path
+ *
+ * @param string $path
+ * @return array consisting of the storage and the internal path
+ */
+ static public function resolvePath($path) {
+ $view = \OC\Files\Filesystem::getView();
+ return $view->resolvePath($path);
+ }
+
+ 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));
+ }
+ }
+
+ static public function deleteUpdate($path) {
+ /**
+ * @var \OC\Files\Storage\Storage $storage
+ * @var string $internalPath
+ */
+ list($storage, $internalPath) = self::resolvePath($path);
+ if ($storage) {
+ $cache = $storage->getCache($internalPath);
+ $cache->remove($internalPath);
+ $cache->correctFolderSize($internalPath);
+ self::correctFolder($path, time());
+ }
+ }
+
+ /**
+ * Update the mtime and ETag of all parent folders
+ *
+ * @param string $path
+ * @param string $time
+ */
+ static public function correctFolder($path, $time) {
+ if ($path !== '' && $path !== '/') {
+ $parent = dirname($path);
+ if ($parent === '.') {
+ $parent = '';
+ }
+ /**
+ * @var \OC\Files\Storage\Storage $storage
+ * @var string $internalPath
+ */
+ list($storage, $internalPath) = self::resolvePath($parent);
+ if ($storage) {
+ $cache = $storage->getCache();
+ $id = $cache->getId($internalPath);
+ if ($id !== -1) {
+ $cache->update($id, array('mtime' => $time, 'etag' => $storage->getETag($internalPath)));
+ self::correctFolder($parent, $time);
+ }
+ }
+ }
+ }
+
+ /**
+ * @param array $params
+ */
+ static public function writeHook($params) {
+ self::writeUpdate($params['path']);
+ }
+
+ /**
+ * @param array $params
+ */
+ static public function renameHook($params) {
+ self::deleteUpdate($params['oldpath']);
+ self::writeUpdate($params['newpath']);
+ }
+
+ /**
+ * @param array $params
+ */
+ static public function deleteHook($params) {
+ self::deleteUpdate($params['path']);
+ }
+}
diff --git a/lib/files/cache/upgrade.php b/lib/files/cache/upgrade.php
new file mode 100644
index 00000000000..eb8c7297c3e
--- /dev/null
+++ b/lib/files/cache/upgrade.php
@@ -0,0 +1,159 @@
+<?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;
+
+class Upgrade {
+ /**
+ * @var Legacy $legacy
+ */
+ private $legacy;
+
+ private $numericIds = array();
+
+ private $mimeTypeIds = array();
+
+ /**
+ * @param Legacy $legacy
+ */
+ public function __construct($legacy) {
+ $this->legacy = $legacy;
+ }
+
+ /**
+ * Preform a shallow upgrade
+ *
+ * @param string $path
+ * @param int $mode
+ */
+ function upgradePath($path, $mode = Scanner::SCAN_RECURSIVE) {
+ if (!$this->legacy->hasItems()) {
+ return;
+ }
+ \OC_Hook::emit('\OC\Files\Cache\Upgrade', 'migrate_path', $path);
+
+ if ($row = $this->legacy->get($path)) {
+ $data = $this->getNewData($row);
+ $this->insert($data);
+
+ $this->upgradeChilds($data['id'], $mode);
+ }
+ }
+
+ /**
+ * @param int $id
+ */
+ function upgradeChilds($id, $mode = Scanner::SCAN_RECURSIVE) {
+ $children = $this->legacy->getChildren($id);
+ foreach ($children as $child) {
+ $childData = $this->getNewData($child);
+ \OC_Hook::emit('\OC\Files\Cache\Upgrade', 'migrate_path', $child['path']);
+ $this->insert($childData);
+ if ($mode == Scanner::SCAN_RECURSIVE) {
+ $this->upgradeChilds($child['id']);
+ }
+ }
+ }
+
+ /**
+ * @param array $data the data for the new cache
+ */
+ function insert($data) {
+ if (!$this->inCache($data['storage'], $data['path_hash'])) {
+ $insertQuery = \OC_DB::prepare('INSERT INTO `*PREFIX*filecache`
+ ( `fileid`, `storage`, `path`, `path_hash`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, `encrypted` )
+ VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
+
+ $insertQuery->execute(array($data['id'], $data['storage'], $data['path'], $data['path_hash'], $data['parent'], $data['name'],
+ $data['mimetype'], $data['mimepart'], $data['size'], $data['mtime'], $data['encrypted']));
+ }
+ }
+
+ /**
+ * @param string $storage
+ * @param string $pathHash
+ * @return bool
+ */
+ function inCache($storage, $pathHash) {
+ $query = \OC_DB::prepare('SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?');
+ $result = $query->execute(array($storage, $pathHash));
+ return (bool)$result->fetchRow();
+ }
+
+ /**
+ * get the new data array from the old one
+ *
+ * @param array $data the data from the old cache
+ * @return array
+ */
+ function getNewData($data) {
+ $newData = $data;
+ list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($data['path']);
+ /**
+ * @var \OC\Files\Storage\Storage $storage
+ * @var string $internalPath;
+ */
+ $newData['path_hash'] = md5($internalPath);
+ $newData['path'] = $internalPath;
+ $newData['storage'] = $this->getNumericId($storage);
+ $newData['parent'] = ($internalPath === '') ? -1 : $data['parent'];
+ $newData['permissions'] = ($data['writable']) ? \OCP\PERMISSION_ALL : \OCP\PERMISSION_READ;
+ $newData['storage_object'] = $storage;
+ $newData['mimetype'] = $this->getMimetypeId($newData['mimetype'], $storage);
+ $newData['mimepart'] = $this->getMimetypeId($newData['mimepart'], $storage);
+ return $newData;
+ }
+
+ /**
+ * get the numeric storage id
+ *
+ * @param \OC\Files\Storage\Storage $storage
+ * @return int
+ */
+ function getNumericId($storage) {
+ $storageId = $storage->getId();
+ if (!isset($this->numericIds[$storageId])) {
+ $cache = $storage->getCache();
+ $this->numericIds[$storageId] = $cache->getNumericStorageId();
+ }
+ return $this->numericIds[$storageId];
+ }
+
+ /**
+ * @param string $mimetype
+ * @param \OC\Files\Storage\Storage $storage
+ * @return int
+ */
+ function getMimetypeId($mimetype, $storage) {
+ if (!isset($this->mimeTypeIds[$mimetype])) {
+ $cache = new Cache($storage);
+ $this->mimeTypeIds[$mimetype] = $cache->getMimetypeId($mimetype);
+ }
+ return $this->mimeTypeIds[$mimetype];
+ }
+
+ /**
+ * check if a cache upgrade is required for $user
+ *
+ * @param string $user
+ * @return bool
+ */
+ static function needUpgrade($user) {
+ $cacheVersion = (int)\OCP\Config::getUserValue($user, 'files', 'cache_version', 4);
+ return $cacheVersion < 5;
+ }
+
+ /**
+ * mark the filecache as upgrade
+ *
+ * @param string $user
+ */
+ static function upgradeDone($user) {
+ \OCP\Config::setUserValue($user, 'files', 'cache_version', 5);
+ }
+}
diff --git a/lib/files/cache/watcher.php b/lib/files/cache/watcher.php
new file mode 100644
index 00000000000..31059ec7f56
--- /dev/null
+++ b/lib/files/cache/watcher.php
@@ -0,0 +1,72 @@
+<?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;
+
+/**
+ * check the storage backends for updates and change the cache accordingly
+ */
+class Watcher {
+ /**
+ * @var \OC\Files\Storage\Storage $storage
+ */
+ private $storage;
+
+ /**
+ * @var Cache $cache
+ */
+ private $cache;
+
+ /**
+ * @var Scanner $scanner;
+ */
+ private $scanner;
+
+ /**
+ * @param \OC\Files\Storage\Storage $storage
+ */
+ public function __construct(\OC\Files\Storage\Storage $storage) {
+ $this->storage = $storage;
+ $this->cache = $storage->getCache();
+ $this->scanner = $storage->getScanner();
+ }
+
+ /**
+ * check $path for updates
+ *
+ * @param string $path
+ */
+ public function checkUpdate($path) {
+ $cachedEntry = $this->cache->get($path);
+ if ($this->storage->hasUpdated($path, $cachedEntry['mtime'])) {
+ if ($this->storage->is_dir($path)) {
+ $this->scanner->scan($path, Scanner::SCAN_SHALLOW);
+ } else {
+ $this->scanner->scanFile($path);
+ }
+ if ($cachedEntry['mimetype'] === 'httpd/unix-directory') {
+ $this->cleanFolder($path);
+ }
+ $this->cache->correctFolderSize($path);
+ }
+ }
+
+ /**
+ * remove deleted files in $path from the cache
+ *
+ * @param string $path
+ */
+ public function cleanFolder($path) {
+ $cachedContent = $this->cache->getFolderContents($path);
+ foreach ($cachedContent as $entry) {
+ if (!$this->storage->file_exists($entry['path'])) {
+ $this->cache->remove($entry['path']);
+ }
+ }
+ }
+}