diff options
author | Vincent Petry <pvince81@owncloud.com> | 2015-04-08 17:56:20 +0200 |
---|---|---|
committer | Vincent Petry <pvince81@owncloud.com> | 2015-04-30 16:15:18 +0200 |
commit | 2228faaa39b03e7162451ac6ca38c645a5b141a1 (patch) | |
tree | 1b9f8bb7846a1c008f3fe96903b2f8de7db0f31a /apps/files_versions/lib/storage.php | |
parent | 41d7870d7b8aa1b635fd1d2d589322160bb46d31 (diff) | |
download | nextcloud-server-2228faaa39b03e7162451ac6ca38c645a5b141a1.tar.gz nextcloud-server-2228faaa39b03e7162451ac6ca38c645a5b141a1.zip |
Fix version rollback to keep fileid
Diffstat (limited to 'apps/files_versions/lib/storage.php')
-rw-r--r-- | apps/files_versions/lib/storage.php | 42 |
1 files changed, 38 insertions, 4 deletions
diff --git a/apps/files_versions/lib/storage.php b/apps/files_versions/lib/storage.php index 125fb5d983c..6da22d6459c 100644 --- a/apps/files_versions/lib/storage.php +++ b/apps/files_versions/lib/storage.php @@ -262,11 +262,16 @@ class Storage { } /** - * rollback to an old version of a file. + * Rollback to an old version of a file. + * + * @param string $file file name + * @param int $revision revision timestamp */ public static function rollback($file, $revision) { if(\OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true') { + // add expected leading slash + $file = '/' . ltrim($file, '/'); list($uid, $filename) = self::getUidAndFilename($file); $users_view = new \OC\Files\View('/'.$uid); $files_view = new \OC\Files\View('/'.\OCP\User::getUser().'/files'); @@ -282,12 +287,11 @@ class Storage { } // rollback - if( @$users_view->rename('files_versions'.$filename.'.v'.$revision, 'files'.$filename) ) { + if (self::copyFileContents($users_view, 'files_versions' . $filename . '.v' . $revision, 'files' . $filename)) { $files_view->touch($file, $revision); Storage::scheduleExpire($file); return true; - - }else if ( $versionCreated ) { + } else if ($versionCreated) { self::deleteVersion($users_view, $version); } } @@ -295,6 +299,36 @@ class Storage { } + /** + * Stream copy file contents from $path1 to $path2 + * + * @param \OC\Files\View $view view to use for copying + * @param string $path1 source file to copy + * @param string $path2 target file + * + * @return bool true for success, false otherwise + */ + private static function copyFileContents($view, $path1, $path2) { + list($storage1, $internalPath1) = $view->resolvePath($path1); + list($storage2, $internalPath2) = $view->resolvePath($path2); + + if ($storage1 === $storage2) { + return $storage1->rename($internalPath1, $internalPath2); + } + $source = $storage1->fopen($internalPath1, 'r'); + $target = $storage2->fopen($internalPath2, 'w'); + // FIXME: might need to use part file to avoid concurrent writes + // (this would be an issue anyway when renaming/restoring cross-storage) + list(, $result) = \OC_Helper::streamCopy($source, $target); + fclose($source); + fclose($target); + + if ($result !== false) { + $storage1->unlink($internalPath1); + } + + return ($result !== false); + } /** * get a list of all available versions of a file in descending chronological order |