copy always file by file to encrypt/decrypt it if needed
This commit is contained in:
parent
8c08dd0ac2
commit
e51fe617d8
|
@ -67,7 +67,6 @@ class Encryption extends Wrapper {
|
||||||
/** @var IMountPoint */
|
/** @var IMountPoint */
|
||||||
private $mount;
|
private $mount;
|
||||||
|
|
||||||
|
|
||||||
/** @var IStorage */
|
/** @var IStorage */
|
||||||
private $keyStorage;
|
private $keyStorage;
|
||||||
|
|
||||||
|
@ -300,33 +299,15 @@ class Encryption extends Wrapper {
|
||||||
public function copy($path1, $path2) {
|
public function copy($path1, $path2) {
|
||||||
|
|
||||||
$source = $this->getFullPath($path1);
|
$source = $this->getFullPath($path1);
|
||||||
$target = $this->getFullPath($path2);
|
|
||||||
|
|
||||||
if ($this->util->isExcluded($source)) {
|
if ($this->util->isExcluded($source)) {
|
||||||
return $this->storage->copy($path1, $path2);
|
return $this->storage->copy($path1, $path2);
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = $this->storage->copy($path1, $path2);
|
// need to stream copy file by file in case we copy between a encrypted
|
||||||
|
// and a unencrypted storage
|
||||||
if ($result && $this->encryptionManager->isEnabled()) {
|
$this->unlink($path2);
|
||||||
$keysCopied = $this->copyKeys($source, $target);
|
$result = $this->copyFromStorage($this, $path1, $path2);
|
||||||
|
|
||||||
if ($keysCopied &&
|
|
||||||
dirname($source) !== dirname($target) &&
|
|
||||||
$this->util->isFile($target)
|
|
||||||
) {
|
|
||||||
$this->update->update($target);
|
|
||||||
}
|
|
||||||
|
|
||||||
$data = $this->getMetaData($path1);
|
|
||||||
|
|
||||||
if (isset($data['encrypted'])) {
|
|
||||||
$this->getCache()->put($path2, ['encrypted' => $data['encrypted']]);
|
|
||||||
}
|
|
||||||
if (isset($data['size'])) {
|
|
||||||
$this->updateUnencryptedSize($target, $data['size']);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
@ -511,12 +492,17 @@ class Encryption extends Wrapper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$source = $sourceStorage->fopen($sourceInternalPath, 'r');
|
try {
|
||||||
$target = $this->fopen($targetInternalPath, 'w');
|
$source = $sourceStorage->fopen($sourceInternalPath, 'r');
|
||||||
list(, $result) = \OC_Helper::streamCopy($source, $target);
|
$target = $this->fopen($targetInternalPath, 'w');
|
||||||
fclose($source);
|
list(, $result) = \OC_Helper::streamCopy($source, $target);
|
||||||
fclose($target);
|
fclose($source);
|
||||||
|
fclose($target);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
fclose($source);
|
||||||
|
fclose($target);
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
if($result) {
|
if($result) {
|
||||||
if ($preserveMtime) {
|
if ($preserveMtime) {
|
||||||
$this->touch($targetInternalPath, $sourceStorage->filemtime($sourceInternalPath));
|
$this->touch($targetInternalPath, $sourceStorage->filemtime($sourceInternalPath));
|
||||||
|
|
|
@ -241,59 +241,14 @@ class Encryption extends \Test\Files\Storage\Storage {
|
||||||
$this->instance->rename($source, $target);
|
$this->instance->rename($source, $target);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function testCopyEncryption() {
|
||||||
* @dataProvider dataTestCopyAndRename
|
$this->instance->file_put_contents('source.txt', 'bar');
|
||||||
*
|
$this->instance->copy('source.txt', 'target.txt');
|
||||||
* @param string $source
|
$this->assertSame('bar', $this->instance->file_get_contents('target.txt'));
|
||||||
* @param string $target
|
$targetMeta = $this->instance->getMetaData('target.txt');
|
||||||
* @param $encryptionEnabled
|
$sourceMeta = $this->instance->getMetaData('source.txt');
|
||||||
* @param boolean $copyKeysReturn
|
$this->assertSame($sourceMeta['encrypted'], $targetMeta['encrypted']);
|
||||||
* @param boolean $shouldUpdate
|
$this->assertSame($sourceMeta['size'], $targetMeta['size']);
|
||||||
*/
|
|
||||||
public function testCopyEncryption($source,
|
|
||||||
$target,
|
|
||||||
$encryptionEnabled,
|
|
||||||
$copyKeysReturn,
|
|
||||||
$shouldUpdate) {
|
|
||||||
|
|
||||||
if ($encryptionEnabled) {
|
|
||||||
$this->keyStore
|
|
||||||
->expects($this->once())
|
|
||||||
->method('copyKeys')
|
|
||||||
->willReturn($copyKeysReturn);
|
|
||||||
$this->cache->expects($this->atLeastOnce())
|
|
||||||
->method('put')
|
|
||||||
->willReturnCallback(function($path, $data) {
|
|
||||||
$this->assertArrayHasKey('encrypted', $data);
|
|
||||||
$this->assertTrue($data['encrypted']);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
$this->cache->expects($this->never())->method('put');
|
|
||||||
$this->keyStore->expects($this->never())->method('copyKeys');
|
|
||||||
}
|
|
||||||
$this->util->expects($this->any())
|
|
||||||
->method('isFile')->willReturn(true);
|
|
||||||
$this->util->expects($this->any())
|
|
||||||
->method('isExcluded')->willReturn(false);
|
|
||||||
$this->encryptionManager->expects($this->once())
|
|
||||||
->method('isEnabled')->willReturn($encryptionEnabled);
|
|
||||||
if ($shouldUpdate) {
|
|
||||||
$this->update->expects($this->once())
|
|
||||||
->method('update');
|
|
||||||
} else {
|
|
||||||
$this->update->expects($this->never())
|
|
||||||
->method('update');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->instance->mkdir($source);
|
|
||||||
$this->instance->mkdir(dirname($target));
|
|
||||||
$this->instance->copy($source, $target);
|
|
||||||
|
|
||||||
if ($encryptionEnabled) {
|
|
||||||
$this->assertSame($this->dummySize,
|
|
||||||
$this->instance->filesize($target)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue