Skip to content

Commit

Permalink
Move fallback logic to common
Browse files Browse the repository at this point in the history
  • Loading branch information
icewind1991 committed Jan 15, 2015
1 parent 5e780c0 commit 6c8a8ea
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 93 deletions.
41 changes: 41 additions & 0 deletions lib/private/files/storage/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,4 +451,45 @@ public function getDirectDownload($path) {
return [];
}

/**
* @param \OCP\Files\Storage $sourceStorage
* @param string $sourceInternalPath
* @param string $targetInternalPath
* @return bool true on success, false if the cross storage copy is not possible
*/
public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
if ($sourceStorage->is_dir($sourceInternalPath)) {
$dh = $sourceStorage->opendir($sourceInternalPath);
$result = $this->mkdir($targetInternalPath);
if (is_resource($dh)) {
while (($file = readdir($dh)) !== false) {
if (!Filesystem::isIgnoredDir($file)) {
$result = $this->copyFromStorage($sourceStorage, $sourceInternalPath . '/' . $file, $targetInternalPath . '/' . $file);
}
}
}
} else {
$source = $sourceStorage->fopen($sourceInternalPath, 'r');
$target = $this->fopen($targetInternalPath, 'w');
list(, $result) = \OC_Helper::streamCopy($source, $target);
fclose($source);
fclose($target);
}
return $result;
}

/**
* @param \OCP\Files\Storage $sourceStorage
* @param string $sourceInternalPath
* @param string $targetInternalPath
* @return bool true on success, false if the cross storage copy is not possible
*/
public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
$this->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
if ($sourceStorage->is_dir($sourceInternalPath)) {
$sourceStorage->rmdir($sourceInternalPath);
} else {
$sourceStorage->unlink($sourceInternalPath);
}
}
}
12 changes: 5 additions & 7 deletions lib/private/files/storage/local.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

namespace OC\Files\Storage;

use OCP\Files\Storage\ICrossCopyStorage;

if (\OC_Util::runningOnWindows()) {
class Local extends MappedLocal {

Expand All @@ -19,7 +17,7 @@ class Local extends MappedLocal {
/**
* for local filestore, we only have to map the paths
*/
class Local extends \OC\Files\Storage\Common implements ICrossCopyStorage {
class Local extends \OC\Files\Storage\Common {
protected $datadir;

public function __construct($arguments) {
Expand Down Expand Up @@ -322,15 +320,15 @@ public function getETag($path) {
* @param string $targetInternalPath
* @return bool
*/
public function crossCopy(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
if($sourceStorage->instanceOfStorage('\OC\Files\Storage\Local')){
/**
* @var \OC\Files\Storage\Local $sourceStorage
*/
$rootStorage = new Local(['datadir' => '/']);
return $rootStorage->copy($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath));
} else {
return false;
return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
}
}

Expand All @@ -340,15 +338,15 @@ public function crossCopy(\OCP\Files\Storage $sourceStorage, $sourceInternalPath
* @param string $targetInternalPath
* @return bool
*/
public function crossMove(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
if ($sourceStorage->instanceOfStorage('\OC\Files\Storage\Local')) {
/**
* @var \OC\Files\Storage\Local $sourceStorage
*/
$rootStorage = new Local(['datadir' => '/']);
return $rootStorage->rename($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath));
} else {
return false;
return parent::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
}
}
}
Expand Down
54 changes: 2 additions & 52 deletions lib/private/files/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -508,33 +508,7 @@ public function rename($path1, $path2) {
$result = false;
}
} else {
/**
* @var \OCP\Files\Storage\ICrossCopyStorage | \OCP\Files\Storage $storage2
*/
if ($storage2->instanceOfStorage('\OCP\Files\Storage\ICrossCopyStorage') and
$storage2->crossMove($storage1, $internalPath1, $internalPath2)) {
$result = true;
} else {
if ($this->is_dir($path1)) {
$result = $this->copy($path1, $path2);
if ($result === true) {
$result = $storage1->rmdir($internalPath1);
}
} else {
$source = $this->fopen($path1 . $postFix1, 'r');
$target = $this->fopen($path2 . $postFix2, 'w');
list($count, $result) = \OC_Helper::streamCopy($source, $target);

// close open handle - especially $source is necessary because unlink below will
// throw an exception on windows because the file is locked
fclose($source);
fclose($target);

if ($result !== false) {
$storage1->unlink($internalPath1);
}
}
}
return $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2);
}
\OC_FileProxy::runPostProxies('rename', $absolutePath1, $absolutePath2);
if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) {
Expand Down Expand Up @@ -608,31 +582,7 @@ public function copy($path1, $path2) {
$result = false;
}
} else {
/**
* @var \OCP\Files\Storage\ICrossCopyStorage | \OCP\Files\Storage $storage2
*/
if ($storage2->instanceOfStorage('\OCP\Files\Storage\ICrossCopyStorage') and
$storage2->crossCopy($storage1, $internalPath1, $internalPath2)
) {
$result = true;
} else {
if ($this->is_dir($path1) && ($dh = $this->opendir($path1))) {
$result = $this->mkdir($path2);
if (is_resource($dh)) {
while (($file = readdir($dh)) !== false) {
if (!Filesystem::isIgnoredDir($file)) {
$result = $this->copy($path1 . '/' . $file, $path2 . '/' . $file);
}
}
}
} else {
$source = $this->fopen($path1 . $postFix1, 'r');
$target = $this->fopen($path2 . $postFix2, 'w');
list($count, $result) = \OC_Helper::streamCopy($source, $target);
fclose($source);
fclose($target);
}
}
return $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2);
}
$this->updater->update($path2);
if ($this->shouldEmitHooks() && $result !== false) {
Expand Down
16 changes: 16 additions & 0 deletions lib/public/files/storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,4 +345,20 @@ public function instanceOfStorage($class);
* @return array
*/
public function getDirectDownload($path);

/**
* @param \OCP\Files\Storage $sourceStorage
* @param string $sourceInternalPath
* @param string $targetInternalPath
* @return bool true on success, false if the cross storage copy is not possible
*/
public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath);

/**
* @param \OCP\Files\Storage $sourceStorage
* @param string $sourceInternalPath
* @param string $targetInternalPath
* @return bool true on success, false if the cross storage copy is not possible
*/
public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath);
}
30 changes: 0 additions & 30 deletions lib/public/files/storage/icrosscopystorage.php

This file was deleted.

9 changes: 5 additions & 4 deletions tests/lib/files/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Test\Files;

use OC\Files\Cache\Watcher;
use OC\Files\Storage\Common;
use OC\Files\Storage\Temporary;

class TemporaryNoTouch extends \OC\Files\Storage\Temporary {
Expand All @@ -17,12 +18,12 @@ public function touch($path, $mtime = null) {
}

class TemporaryNoCross extends \OC\Files\Storage\Temporary {
public function crossCopy(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
return false;
public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
return Common::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
}

public function crossMove(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
return false;
public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
return Common::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
}
}

Expand Down

0 comments on commit 6c8a8ea

Please sign in to comment.