]> source.dussan.org Git - nextcloud-server.git/commitdiff
add notify support to smb storage
authorRobin Appelman <icewind@owncloud.com>
Thu, 14 Jul 2016 12:46:01 +0000 (14:46 +0200)
committerRobin Appelman <icewind@owncloud.com>
Thu, 14 Jul 2016 12:46:01 +0000 (14:46 +0200)
apps/files_external/lib/Lib/Storage/SMB.php
lib/public/Files/Storage/INotifyStorage.php [new file with mode: 0644]

index e677f8c9eba916027ff84364972cd9002066b25d..6a24c6b17d94f8bf5b35fd0924f7c2b2cc3f9c2a 100644 (file)
@@ -34,15 +34,18 @@ use Icewind\SMB\Exception\ConnectException;
 use Icewind\SMB\Exception\Exception;
 use Icewind\SMB\Exception\ForbiddenException;
 use Icewind\SMB\Exception\NotFoundException;
+use Icewind\SMB\IShare;
 use Icewind\SMB\NativeServer;
 use Icewind\SMB\Server;
 use Icewind\Streams\CallbackWrapper;
 use Icewind\Streams\IteratorDirectory;
 use OC\Cache\CappedMemoryCache;
 use OC\Files\Filesystem;
+use OC\Files\Storage\Common;
+use OCP\Files\Storage\INotifyStorage;
 use OCP\Files\StorageNotAvailableException;
 
-class SMB extends \OC\Files\Storage\Common {
+class SMB extends Common implements INotifyStorage {
        /**
         * @var \Icewind\SMB\Server
         */
@@ -103,6 +106,16 @@ class SMB extends \OC\Files\Storage\Common {
                return Filesystem::normalizePath($this->root . '/' . $path, true, false, true);
        }
 
+       protected function relativePath($fullPath) {
+               if ($fullPath === $this->root) {
+                       return '';
+               } else if (substr($fullPath, 0, strlen($this->root)) === $this->root) {
+                       return substr($fullPath, strlen($this->root));
+               } else {
+                       return null;
+               }
+       }
+
        /**
         * @param string $path
         * @return \Icewind\SMB\IFileInfo
@@ -413,4 +426,48 @@ class SMB extends \OC\Files\Storage\Common {
                        return false;
                }
        }
+
+       public function listen($path, callable $callback) {
+               $fullPath = $this->buildPath($path);
+               $oldRenamePath = null;
+               $this->share->notify($fullPath, function ($smbType, $fullPath) use (&$oldRenamePath, $callback) {
+                       $path = $this->relativePath($fullPath);
+                       if (is_null($path)) {
+                               return true;
+                       }
+                       if ($smbType === IShare::NOTIFY_RENAMED_OLD) {
+                               $oldRenamePath = $path;
+                               return true;
+                       }
+                       $type = $this->mapNotifyType($smbType);
+                       if (is_null($type)) {
+                               return true;
+                       }
+                       if ($type === INotifyStorage::NOTIFY_RENAMED && !is_null($oldRenamePath)) {
+                               $result = $callback($type, $path, $oldRenamePath);
+                               $oldRenamePath = null;
+                       } else {
+                               $result = $callback($type, $path);
+                       }
+                       return $result;
+               });
+       }
+
+       private function mapNotifyType($smbType) {
+               switch ($smbType) {
+                       case IShare::NOTIFY_ADDED:
+                               return INotifyStorage::NOTIFY_ADDED;
+                       case IShare::NOTIFY_REMOVED:
+                               return INotifyStorage::NOTIFY_REMOVED;
+                       case IShare::NOTIFY_MODIFIED:
+                       case IShare::NOTIFY_ADDED_STREAM:
+                       case IShare::NOTIFY_MODIFIED_STREAM:
+                       case IShare::NOTIFY_REMOVED_STREAM:
+                               return INotifyStorage::NOTIFY_MODIFIED;
+                       case IShare::NOTIFY_RENAMED_NEW:
+                               return INotifyStorage::NOTIFY_RENAMED;
+                       default:
+                               return null;
+               }
+       }
 }
diff --git a/lib/public/Files/Storage/INotifyStorage.php b/lib/public/Files/Storage/INotifyStorage.php
new file mode 100644 (file)
index 0000000..0bf9b2a
--- /dev/null
@@ -0,0 +1,47 @@
+<?php
+/**
+ * @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCP\Files\Storage;
+
+/**
+ * Storage backend that support active notifications
+ */
+interface INotifyStorage {
+       const NOTIFY_ADDED = 1;
+       const NOTIFY_REMOVED = 2;
+       const NOTIFY_MODIFIED = 3;
+       const NOTIFY_RENAMED = 4;
+
+       /**
+        * Start listening for update notifications
+        *
+        * The provided callback will be called for every incoming notification with the following parameters
+        *  - int $type the type of update, one of the INotifyStorage::NOTIFY_* constants
+        *  - string $path the path of the update
+        *  - string $renameTarget the target of the rename operation, only provided for rename updates
+        *
+        * Note that this call is blocking and will not exit on it's own, to stop listening for notifications return `false` from the callback
+        *
+        * @param string $path
+        * @param callable $callback
+        */
+       public function listen($path, callable $callback);
+}