Skip to content

Commit ae7241f

Browse files
Merge pull request #48221 from nextcloud/backport/30/fix_move_on_same_bucket
2 parents 5f32c2f + 656e32c commit ae7241f

File tree

4 files changed

+187
-0
lines changed

4 files changed

+187
-0
lines changed

lib/private/Files/ObjectStore/ObjectStoreStorage.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,11 @@ public function copyFromStorage(
597597

598598
public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, ?ICacheEntry $sourceCacheEntry = null): bool {
599599
$sourceCache = $sourceStorage->getCache();
600+
if ($sourceStorage->instanceOfStorage(ObjectStoreStorage::class) && $sourceStorage->getObjectStore()->getStorageId() === $this->getObjectStore()->getStorageId()) {
601+
$this->getCache()->moveFromCache($sourceCache, $sourceInternalPath, $targetInternalPath);
602+
// Do not import any data when source and target bucket are identical.
603+
return true;
604+
}
600605
if (!$sourceCacheEntry) {
601606
$sourceCacheEntry = $sourceCache->get($sourceInternalPath);
602607
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
4+
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
5+
* SPDX-License-Identifier: AGPL-3.0-or-later
6+
*/
7+
8+
namespace Test\Files\ObjectStore;
9+
10+
use OC\Files\ObjectStore\StorageObjectStore;
11+
use OC\Files\Storage\Temporary;
12+
use Test\Files\Storage\StoragesTest;
13+
14+
/**
15+
* @group DB
16+
*/
17+
class ObjectStoreStoragesDifferentBucketTest extends StoragesTest {
18+
/**
19+
* @var \OCP\Files\ObjectStore\IObjectStore
20+
*/
21+
private $objectStore1;
22+
23+
/**
24+
* @var \OCP\Files\ObjectStore\IObjectStore
25+
*/
26+
private $objectStore2;
27+
28+
protected function setUp(): void {
29+
parent::setUp();
30+
31+
$baseStorage1 = new Temporary();
32+
$this->objectStore1 = new StorageObjectStore($baseStorage1);
33+
$config['objectstore'] = $this->objectStore1;
34+
$this->storage1 = new ObjectStoreStorageOverwrite($config);
35+
36+
$baseStorage2 = new Temporary();
37+
$this->objectStore2 = new StorageObjectStore($baseStorage2);
38+
$config['objectstore'] = $this->objectStore2;
39+
$this->storage2 = new ObjectStoreStorageOverwrite($config);
40+
}
41+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
4+
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
5+
* SPDX-License-Identifier: AGPL-3.0-or-later
6+
*/
7+
8+
namespace Test\Files\ObjectStore;
9+
10+
use OC\Files\ObjectStore\StorageObjectStore;
11+
use OC\Files\Storage\Temporary;
12+
use Test\Files\Storage\StoragesTest;
13+
14+
/**
15+
* @group DB
16+
*/
17+
class ObjectStoreStoragesSameBucketTest extends StoragesTest {
18+
/**
19+
* @var \OCP\Files\ObjectStore\IObjectStore
20+
*/
21+
private $objectStore;
22+
23+
protected function setUp(): void {
24+
parent::setUp();
25+
26+
$baseStorage = new Temporary();
27+
$this->objectStore = new StorageObjectStore($baseStorage);
28+
$config['objectstore'] = $this->objectStore;
29+
// storage1 and storage2 share the same object store.
30+
$this->storage1 = new ObjectStoreStorageOverwrite($config);
31+
$this->storage2 = new ObjectStoreStorageOverwrite($config);
32+
}
33+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
/**
3+
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
4+
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
5+
* SPDX-License-Identifier: AGPL-3.0-or-later
6+
*/
7+
8+
namespace Test\Files\Storage;
9+
10+
use Test\TestCase;
11+
12+
abstract class StoragesTest extends TestCase {
13+
/**
14+
* @var \OC\Files\Storage\Storage
15+
*/
16+
protected $storage1;
17+
18+
/**
19+
* @var \OC\Files\Storage\Storage
20+
*/
21+
protected $storage2;
22+
23+
protected function tearDown(): void {
24+
if (is_null($this->storage1) && is_null($this->storage2)) {
25+
return;
26+
}
27+
$this->storage1->getCache()->clear();
28+
$this->storage2->getCache()->clear();
29+
30+
parent::tearDown();
31+
}
32+
33+
public function testMoveFileFromStorage() {
34+
$source = 'source.txt';
35+
$target = 'target.txt';
36+
$this->storage2->file_put_contents($source, 'foo');
37+
38+
$this->storage1->moveFromStorage($this->storage2, $source, $target);
39+
40+
$this->assertTrue($this->storage1->file_exists($target), $target.' was not created');
41+
$this->assertFalse($this->storage2->file_exists($source), $source.' still exists');
42+
$this->assertEquals('foo', $this->storage1->file_get_contents($target));
43+
}
44+
45+
public function testMoveDirectoryFromStorage() {
46+
$this->storage2->mkdir('source');
47+
$this->storage2->file_put_contents('source/test1.txt', 'foo');
48+
$this->storage2->file_put_contents('source/test2.txt', 'qwerty');
49+
$this->storage2->mkdir('source/subfolder');
50+
$this->storage2->file_put_contents('source/subfolder/test.txt', 'bar');
51+
52+
$this->storage1->moveFromStorage($this->storage2, 'source', 'target');
53+
54+
$this->assertTrue($this->storage1->file_exists('target'));
55+
$this->assertTrue($this->storage1->file_exists('target/test1.txt'));
56+
$this->assertTrue($this->storage1->file_exists('target/test2.txt'));
57+
$this->assertTrue($this->storage1->file_exists('target/subfolder'));
58+
$this->assertTrue($this->storage1->file_exists('target/subfolder/test.txt'));
59+
60+
$this->assertFalse($this->storage2->file_exists('source'));
61+
$this->assertFalse($this->storage2->file_exists('source/test1.txt'));
62+
$this->assertFalse($this->storage2->file_exists('source/test2.txt'));
63+
$this->assertFalse($this->storage2->file_exists('source/subfolder'));
64+
$this->assertFalse($this->storage2->file_exists('source/subfolder/test.txt'));
65+
66+
$this->assertEquals('foo', $this->storage1->file_get_contents('target/test1.txt'));
67+
$this->assertEquals('qwerty', $this->storage1->file_get_contents('target/test2.txt'));
68+
$this->assertEquals('bar', $this->storage1->file_get_contents('target/subfolder/test.txt'));
69+
}
70+
71+
public function testCopyFileFromStorage() {
72+
$source = 'source.txt';
73+
$target = 'target.txt';
74+
$this->storage2->file_put_contents($source, 'foo');
75+
76+
$this->storage1->copyFromStorage($this->storage2, $source, $target);
77+
78+
$this->assertTrue($this->storage1->file_exists($target), $target.' was not created');
79+
$this->assertTrue($this->storage2->file_exists($source), $source.' was deleted');
80+
$this->assertEquals('foo', $this->storage1->file_get_contents($target));
81+
}
82+
83+
public function testCopyDirectoryFromStorage() {
84+
$this->storage2->mkdir('source');
85+
$this->storage2->file_put_contents('source/test1.txt', 'foo');
86+
$this->storage2->file_put_contents('source/test2.txt', 'qwerty');
87+
$this->storage2->mkdir('source/subfolder');
88+
$this->storage2->file_put_contents('source/subfolder/test.txt', 'bar');
89+
90+
$this->storage1->copyFromStorage($this->storage2, 'source', 'target');
91+
92+
$this->assertTrue($this->storage1->file_exists('target'));
93+
$this->assertTrue($this->storage1->file_exists('target/test1.txt'));
94+
$this->assertTrue($this->storage1->file_exists('target/test2.txt'));
95+
$this->assertTrue($this->storage1->file_exists('target/subfolder'));
96+
$this->assertTrue($this->storage1->file_exists('target/subfolder/test.txt'));
97+
98+
$this->assertTrue($this->storage2->file_exists('source'));
99+
$this->assertTrue($this->storage2->file_exists('source/test1.txt'));
100+
$this->assertTrue($this->storage2->file_exists('source/test2.txt'));
101+
$this->assertTrue($this->storage2->file_exists('source/subfolder'));
102+
$this->assertTrue($this->storage2->file_exists('source/subfolder/test.txt'));
103+
104+
$this->assertEquals('foo', $this->storage1->file_get_contents('target/test1.txt'));
105+
$this->assertEquals('qwerty', $this->storage1->file_get_contents('target/test2.txt'));
106+
$this->assertEquals('bar', $this->storage1->file_get_contents('target/subfolder/test.txt'));
107+
}
108+
}

0 commit comments

Comments
 (0)