0
0
Fork 0
mirror of https://github.com/nextcloud/server.git synced 2025-05-08 15:50:43 +00:00
nextcloud_server/tests/lib/Files/Storage/StoragesTest.php
Louis Chemineau 8fdf2a7eae fix: Transfer ownership with S3 as primary
When using S3 as primary storage, transferring ownership with the `--move` option fail with the following error:

`SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '8-45b963397aa40d4a0063e0d85e4fe7a1' for key 'fs_storage_path_hash'`

The `--move` option moves the entire home folder from one account to another.
The error means that the move failed because the destination folder already exist in `oc_filecache`.

- With S3 as primary storage, folders only exists as entries in `oc_filecache`.
- With S3 as primary storage, `moveFromStorage(...)` only moves the cache entry, as nothing needs to be moved on disk. This cache move does not delete potentially pre-existing destination folder.
- With Local storage, `moveFromStorage(...)` calls `rename(...)` which delete pre-existing folder.

- `transfer(...)`: 687a4d9ac7/apps/files/lib/Service/OwnershipTransferService.php (L112)
- `oneTimeUserSetup(...)`: 687a4d9ac7/lib/private/Files/SetupManager.php (L261-L262)
- `mkdir(...)`: 687a4d9ac7/lib/private/Files/ObjectStore/ObjectStoreStorage.php (L91-L135)
- `moveFromStorage(...)`: 687a4d9ac7/lib/private/Files/ObjectStore/ObjectStoreStorage.php (L635-L636)

Delete pre-existing folder in `moveFromStorage(...)`

Signed-off-by: Louis Chemineau <louis@chmn.me>
2025-04-01 13:53:56 +02:00

126 lines
5.3 KiB
PHP

<?php
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\Files\Storage;
use Test\TestCase;
abstract class StoragesTest extends TestCase {
/**
* @var \OC\Files\Storage\Storage
*/
protected $storage1;
/**
* @var \OC\Files\Storage\Storage
*/
protected $storage2;
protected function tearDown(): void {
if (is_null($this->storage1) && is_null($this->storage2)) {
return;
}
$this->storage1->getCache()->clear();
$this->storage2->getCache()->clear();
parent::tearDown();
}
public function testMoveFileFromStorage() {
$source = 'source.txt';
$target = 'target.txt';
$this->storage2->file_put_contents($source, 'foo');
$this->storage1->moveFromStorage($this->storage2, $source, $target);
$this->assertTrue($this->storage1->file_exists($target), $target . ' was not created');
$this->assertFalse($this->storage2->file_exists($source), $source . ' still exists');
$this->assertEquals('foo', $this->storage1->file_get_contents($target));
}
public function testMoveFileFromStorageWithExistingTarget() {
$source = 'source.txt';
$target = 'target.txt';
$this->storage1->file_put_contents($target, 'bar');
$this->storage2->file_put_contents($source, 'foo');
$targetURN = $this->storage1->getURN($this->storage1->getCache()->get($target)->getID());
$sourceURN = $this->storage2->getURN($this->storage2->getCache()->get($source)->getID());
$this->storage1->moveFromStorage($this->storage2, $source, $target);
$this->assertTrue($this->storage1->file_exists($target), $target . ' was not created in DB');
$this->assertFalse($this->storage2->file_exists($source), $source . ' still exists in DB');
$this->assertTrue($this->storage1->getObjectStore()->objectExists($sourceURN), $sourceURN . ' was not created in bucket');
$this->assertFalse($this->storage1->getObjectStore()->objectExists($targetURN), $targetURN . ' still exists in bucket');
$this->assertEquals('foo', $this->storage1->file_get_contents($target));
}
public function testMoveDirectoryFromStorage() {
$this->storage2->mkdir('source');
$this->storage2->file_put_contents('source/test1.txt', 'foo');
$this->storage2->file_put_contents('source/test2.txt', 'qwerty');
$this->storage2->mkdir('source/subfolder');
$this->storage2->file_put_contents('source/subfolder/test.txt', 'bar');
$this->storage1->moveFromStorage($this->storage2, 'source', 'target');
$this->assertTrue($this->storage1->file_exists('target'));
$this->assertTrue($this->storage1->file_exists('target/test1.txt'));
$this->assertTrue($this->storage1->file_exists('target/test2.txt'));
$this->assertTrue($this->storage1->file_exists('target/subfolder'));
$this->assertTrue($this->storage1->file_exists('target/subfolder/test.txt'));
$this->assertFalse($this->storage2->file_exists('source'));
$this->assertFalse($this->storage2->file_exists('source/test1.txt'));
$this->assertFalse($this->storage2->file_exists('source/test2.txt'));
$this->assertFalse($this->storage2->file_exists('source/subfolder'));
$this->assertFalse($this->storage2->file_exists('source/subfolder/test.txt'));
$this->assertEquals('foo', $this->storage1->file_get_contents('target/test1.txt'));
$this->assertEquals('qwerty', $this->storage1->file_get_contents('target/test2.txt'));
$this->assertEquals('bar', $this->storage1->file_get_contents('target/subfolder/test.txt'));
}
public function testCopyFileFromStorage() {
$source = 'source.txt';
$target = 'target.txt';
$this->storage2->file_put_contents($source, 'foo');
$this->storage1->copyFromStorage($this->storage2, $source, $target);
$this->assertTrue($this->storage1->file_exists($target), $target . ' was not created');
$this->assertTrue($this->storage2->file_exists($source), $source . ' was deleted');
$this->assertEquals('foo', $this->storage1->file_get_contents($target));
}
public function testCopyDirectoryFromStorage() {
$this->storage2->mkdir('source');
$this->storage2->file_put_contents('source/test1.txt', 'foo');
$this->storage2->file_put_contents('source/test2.txt', 'qwerty');
$this->storage2->mkdir('source/subfolder');
$this->storage2->file_put_contents('source/subfolder/test.txt', 'bar');
$this->storage1->copyFromStorage($this->storage2, 'source', 'target');
$this->assertTrue($this->storage1->file_exists('target'));
$this->assertTrue($this->storage1->file_exists('target/test1.txt'));
$this->assertTrue($this->storage1->file_exists('target/test2.txt'));
$this->assertTrue($this->storage1->file_exists('target/subfolder'));
$this->assertTrue($this->storage1->file_exists('target/subfolder/test.txt'));
$this->assertTrue($this->storage2->file_exists('source'));
$this->assertTrue($this->storage2->file_exists('source/test1.txt'));
$this->assertTrue($this->storage2->file_exists('source/test2.txt'));
$this->assertTrue($this->storage2->file_exists('source/subfolder'));
$this->assertTrue($this->storage2->file_exists('source/subfolder/test.txt'));
$this->assertEquals('foo', $this->storage1->file_get_contents('target/test1.txt'));
$this->assertEquals('qwerty', $this->storage1->file_get_contents('target/test2.txt'));
$this->assertEquals('bar', $this->storage1->file_get_contents('target/subfolder/test.txt'));
}
}