diff --git a/apps/files_external/lib/Lib/Storage/SMB.php b/apps/files_external/lib/Lib/Storage/SMB.php index e677f8c9eb..6a24c6b17d 100644 --- a/apps/files_external/lib/Lib/Storage/SMB.php +++ b/apps/files_external/lib/Lib/Storage/SMB.php @@ -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 index 0000000000..0bf9b2a87f --- /dev/null +++ b/lib/public/Files/Storage/INotifyStorage.php @@ -0,0 +1,47 @@ + + * + * @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 . + * + */ + +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); +}