-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Implementation of delta-sync support on server-side. #29404
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| <?php | ||
| /* | ||
| * Copyright (C) by Ahmed Ammar <ahmed.a.ammar@gmail.com> | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | ||
| * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the | ||
| * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to | ||
| * permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the | ||
| * Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE | ||
| * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
| * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
| * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| * | ||
| */ | ||
| namespace OCA\DAV\Files; | ||
|
|
||
| use OC\AppFramework\Http; | ||
| use OCP\Files\NotFoundException; | ||
| use OCP\Files\NotPermittedException; | ||
| use Sabre\DAV\Server; | ||
| use Sabre\DAV\ServerPlugin; | ||
| use Sabre\DAV\PropFind; | ||
| use Sabre\HTTP\RequestInterface; | ||
| use Sabre\HTTP\ResponseInterface; | ||
| use OC\Files\View; | ||
| use \Exception; | ||
|
|
||
| class ZsyncPlugin extends ServerPlugin { | ||
|
|
||
| // namespace | ||
| const ZSYNC_PROPERTYNAME = '{http://owncloud.org/ns}zsync'; | ||
|
|
||
| /** @var OC\Files\View */ | ||
| private $view; | ||
|
|
||
| public function __construct(View $view) { | ||
| $this->view = $view; | ||
| $this->view->mkdir('files_zsync'); | ||
| } | ||
|
|
||
| /** | ||
| * Initializes the plugin and registers event handlers | ||
| * | ||
| * @param Server $server | ||
| * @return void | ||
| */ | ||
| function initialize(Server $server) { | ||
| $server->on('method:GET', [$this, 'httpGet'], 90); | ||
| $server->on('method:DELETE', [$this, 'httpDelete'], 90); | ||
| $server->on('propFind', [$this, 'handleGetProperties']); | ||
| } | ||
|
|
||
| /** | ||
| * Intercepts GET requests on file urls ending with ?zsync. | ||
| * | ||
| * @param RequestInterface $request | ||
| * @param ResponseInterface $response | ||
| */ | ||
| function httpGet(RequestInterface $request, ResponseInterface $response) { | ||
|
|
||
| $queryParams = $request->getQueryParameters(); | ||
| if (!array_key_exists('zsync', $queryParams)) { | ||
| return true; | ||
| } | ||
|
|
||
| $path = ltrim($request->getPath(), '/'); | ||
| /* remove files/$user */ | ||
| $path = implode('/', array_slice(explode('/', $path), 2)); | ||
| /* If basefile not found this is an error */ | ||
| if (!$this->view->file_exists('files/'.$path)) { | ||
| $response->setStatus(Http::STATUS_NOT_FOUND); | ||
| return false; | ||
| } | ||
|
|
||
| $info = $this->view->getFileInfo('files/'.$path); | ||
| $zsyncMetadataFile = 'files_zsync/'.$info->getId(); | ||
| if ($this->view->file_exists($zsyncMetadataFile)) { | ||
| $content = $this->view->file_get_contents($zsyncMetadataFile); | ||
| $response->setHeader('OC-ETag', $info->getEtag()); | ||
| $response->setStatus(Http::STATUS_OK); | ||
| $response->setBody($content); | ||
| } else { | ||
| $response->setStatus(Http::STATUS_NOT_FOUND); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Intercepts DELETE requests on file urls ending with ?zsync. | ||
| * | ||
| * @param RequestInterface $request | ||
| * @param ResponseInterface $response | ||
| */ | ||
| function httpDelete(RequestInterface $request, ResponseInterface $response) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would love to see at some point some relations here.. without integrity contraints it can be a mess and corruption on corruption. I need to test what happens when I terminate script in very bad moment. |
||
|
|
||
| $queryParams = $request->getQueryParameters(); | ||
| if (!array_key_exists('zsync', $queryParams)) { | ||
| return true; | ||
| } | ||
|
|
||
| $path = ltrim($request->getPath(), '/'); | ||
| /* remove files/$user */ | ||
| $path = implode('/', array_slice(explode('/', $path), 2)); | ||
| /* If basefile not found this is an error */ | ||
| if (!$this->view->file_exists('files/'.$path)) { | ||
| $response->setStatus(Http::STATUS_NOT_FOUND); | ||
| return false; | ||
| } | ||
|
|
||
| $info = $this->view->getFileInfo('files/'.$path); | ||
| $zsyncMetadataFile = 'files_zsync/'.$info->getId(); | ||
| if ($this->view->file_exists($zsyncMetadataFile)) { | ||
| $this->view->unlink($zsyncMetadataFile); | ||
| $response->setStatus(Http::STATUS_OK); | ||
| } else { | ||
| $response->setStatus(Http::STATUS_NOT_FOUND); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Adds zsync property if metadata exists | ||
| * | ||
| * @param PropFind $propFind | ||
| * @param \Sabre\DAV\INode $node | ||
| * @return void | ||
| */ | ||
| public function handleGetProperties(PropFind $propFind, \Sabre\DAV\INode $node) { | ||
| if ($node instanceof \OCA\DAV\Connector\Sabre\File) { | ||
| if (!$this->view->is_file('files/'.$node->getPath())) | ||
| return; | ||
| $info = $this->view->getFileInfo('files/'.$node->getPath()); | ||
| $zsyncMetadataFile = 'files_zsync/'.$info->getId(); | ||
| if ($this->view->file_exists($zsyncMetadataFile)) { | ||
| $propFind->handle(self::ZSYNC_PROPERTYNAME, function() use ($node) { | ||
| return 'true'; | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,8 +27,10 @@ | |
| use OCA\DAV\CardDAV\SyncService; | ||
| use OCP\IL10N; | ||
| use OCP\IUser; | ||
| use OCP\User; | ||
| use OCP\IUserManager; | ||
| use OCP\Util; | ||
| use OC\Files\View; | ||
|
|
||
| class HookManager { | ||
|
|
||
|
|
@@ -85,6 +87,23 @@ public function setup() { | |
| 'changeUser', | ||
| $this, | ||
| 'changeUser'); | ||
|
|
||
| Util::connectHook('OC_Filesystem', | ||
| 'post_copy', | ||
| $this, | ||
| 'copyZsyncMetadata'); | ||
| Util::connectHook('OC_Filesystem', | ||
| 'write', | ||
| $this, | ||
| 'deleteZsyncMetadata'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wait wait.. ok this PR needs hard performance review... |
||
| Util::connectHook('OC_Filesystem', | ||
| 'delete', | ||
| $this, | ||
| 'deleteZsyncMetadata'); | ||
| Util::connectHook('\OCP\Versions', | ||
| 'rollback', | ||
| $this, | ||
| 'deleteZsyncMetadata'); | ||
| } | ||
|
|
||
| public function postCreateUser($params) { | ||
|
|
@@ -144,4 +163,66 @@ public function firstLogin(IUser $user = null) { | |
| } | ||
| } | ||
| } | ||
|
|
||
| public function deleteZsyncMetadata($params) { | ||
| $view = new View('/'.User::getUser()); | ||
| $path = $params[\OC\Files\Filesystem::signal_param_path]; | ||
| $path = 'files/' . ltrim($path, '/'); | ||
|
|
||
| /* if a file then just delete zsync metadata for file */ | ||
| if ($view->is_file($path)) { | ||
| $info = $view->getFileInfo($path); | ||
| if ($view->file_exists('files_zsync/'.$info->getId())) | ||
| $view->unlink('files_zsync/'.$info->getId()); | ||
| } else if ($view->is_dir($path)) { | ||
| /* if a folder then iteratively delete all zsync metadata for all files in folder, including subdirs */ | ||
| $array[] = $path; | ||
| while (count($array)) { | ||
| $current = array_pop($array); | ||
| $handle = $view->opendir($current); | ||
| while (($entry = readdir($handle)) !== false) { | ||
| if($entry[0]!='.' and $view->is_dir($current.'/'.$entry)) { | ||
| $array[] = $current.'/'.$entry; | ||
| } else if ($view->is_file($current.'/'.$entry)) { | ||
| $info = $view->getFileInfo($current.'/'.$entry); | ||
| if ($view->file_exists('files_zsync/'.$info->getId())) | ||
| $view->unlink('files_zsync/'.$info->getId()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public function copyZsyncMetadata($params) { | ||
| $view = new View('/'.User::getUser()); | ||
| $from = $params[\OC\Files\Filesystem::signal_param_oldpath]; | ||
| $from = 'files/' . ltrim($from, '/'); | ||
| $to = $params[\OC\Files\Filesystem::signal_param_newpath]; | ||
| $to = 'files/' . ltrim($to, '/'); | ||
|
|
||
| /* if a file then just copy zsync metadata for file */ | ||
| if ($view->is_file($from)) { | ||
| $info_from = $view->getFileInfo($from); | ||
| $info_to = $view->getFileInfo($to); | ||
| if ($view->file_exists('files_zsync/'.$info_from->getId())) | ||
| $view->copy('files_zsync/'.$info_from->getId(), 'files_zsync/'.$info_to->getId()); | ||
| } else if ($view->is_dir($from)) { | ||
| /* if a folder then iteratively copy all zsync metadata for all files in folder, including subdirs */ | ||
| $array[] = [$from, $to]; | ||
| while (count($array)) { | ||
| list($from_current, $to_current) = array_pop($array); | ||
| $handle = $view->opendir($from_current); | ||
| while (($entry = readdir($handle)) !== false) { | ||
| if($entry[0]!='.' and $view->is_dir($from_current.'/'.$entry)) { | ||
| $array[] = [$from_current.'/'.$entry, $to_current.'/'.$entry]; | ||
| } else if ($view->is_file($from_current.'/'.$entry)) { | ||
| $info_from = $view->getFileInfo($from_current.'/'.$entry); | ||
| $info_to = $view->getFileInfo($to_current.'/'.$entry); | ||
| if ($view->file_exists('files_zsync/'.$info_from->getId())) | ||
| $view->copy('files_zsync/'.$info_from->getId(), 'files_zsync/'.$info_to->getId()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, it is just style thing probably, but I think in the rest of the codebase we use ||. @DeepDiver1975 .
About logic - makes sense as it is a similar scenario as in chunking.