summaryrefslogtreecommitdiffstats
path: root/lib/files/cache
diff options
context:
space:
mode:
authorRobin Appelman <icewind@owncloud.com>2012-10-28 11:26:31 +0100
committerRobin Appelman <icewind@owncloud.com>2012-10-28 11:39:37 +0100
commitfba7be1194fd31da2cb71c56d0cd8e6ab1c1ff49 (patch)
tree01d081c750098d7efaba9c5349ba8c246736b25d /lib/files/cache
parentc4a793913cd148d14385ed1b0e017f9a3772bf39 (diff)
downloadnextcloud-server-fba7be1194fd31da2cb71c56d0cd8e6ab1c1ff49.tar.gz
nextcloud-server-fba7be1194fd31da2cb71c56d0cd8e6ab1c1ff49.zip
add filesystem watcher to detect updates
Diffstat (limited to 'lib/files/cache')
-rw-r--r--lib/files/cache/cache.php8
-rw-r--r--lib/files/cache/watcher.php70
2 files changed, 76 insertions, 2 deletions
diff --git a/lib/files/cache/cache.php b/lib/files/cache/cache.php
index cba48e4dbea..138a5e6e63e 100644
--- a/lib/files/cache/cache.php
+++ b/lib/files/cache/cache.php
@@ -234,7 +234,7 @@ class Cache {
$entry = $this->get($file);
if ($entry['mimetype'] === 'httpd/unix-directory') {
$children = $this->getFolderContents($file);
- foreach($children as $child){
+ foreach ($children as $child) {
$this->remove($child['path']);
}
}
@@ -325,7 +325,9 @@ class Cache {
$query = \OC_DB::prepare('SELECT `size` FROM `*PREFIX*filecache` WHERE `parent` = ? AND `storage` = ?');
$result = $query->execute(array($id, $this->storageId));
$totalSize = 0;
+ $hasChilds = 0;
while ($row = $result->fetchRow()) {
+ $hasChilds = true;
$size = (int)$row['size'];
if ($size === -1) {
$totalSize = -1;
@@ -335,7 +337,9 @@ class Cache {
}
}
- $this->update($id, array('size' => $totalSize));
+ if ($hasChilds) {
+ $this->update($id, array('size' => $totalSize));
+ }
return $totalSize;
}
diff --git a/lib/files/cache/watcher.php b/lib/files/cache/watcher.php
new file mode 100644
index 00000000000..f04ca9b4656
--- /dev/null
+++ b/lib/files/cache/watcher.php
@@ -0,0 +1,70 @@
+<?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 ($cachedEntry['mimetype'] === 'httpd/unix-directory') {
+ $this->scanner->scan($path, Scanner::SCAN_SHALLOW);
+ $this->cleanFolder($path);
+ } else {
+ $this->scanner->scanFile($path);
+ }
+ $this->scanner->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']);
+ }
+ }
+ }
+}