Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 56 additions & 50 deletions apps/files_trashbin/lib/Trashbin.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,66 +342,72 @@ public static function move2trash($file_path, $ownerOnly = false) {
return false;
}

$moveSuccessful = true;
$propagator = $trashStorage->getPropagator();
$propagator->beginBatch();
try {
$inCache = $sourceStorage->getCache()->inCache($sourceInternalPath);
$trashStorage->moveFromStorage($sourceStorage, $sourceInternalPath, $trashInternalPath);
if ($inCache) {
$trashStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $trashInternalPath);
} else {
$sizeDifference = $sourceInfo->getSize();
if ($sizeDifference < 0) {
$sizeDifference = null;
$moveSuccessful = true;
try {
$inCache = $sourceStorage->getCache()->inCache($sourceInternalPath);
$trashStorage->moveFromStorage($sourceStorage, $sourceInternalPath, $trashInternalPath);
if ($inCache) {
$trashStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $trashInternalPath);
} else {
$sizeDifference = (int)$sizeDifference;
$sizeDifference = $sourceInfo->getSize();
if ($sizeDifference < 0) {
$sizeDifference = null;
} else {
$sizeDifference = (int)$sizeDifference;
}
$trashStorage->getUpdater()->update($trashInternalPath, null, $sizeDifference);
}
$trashStorage->getUpdater()->update($trashInternalPath, null, $sizeDifference);
}
} catch (\Exception $e) {
$moveSuccessful = false;
if ($trashStorage->file_exists($trashInternalPath)) {
$trashStorage->unlink($trashInternalPath);
} catch (\Exception $e) {
$moveSuccessful = false;
if ($trashStorage->file_exists($trashInternalPath)) {
$trashStorage->unlink($trashInternalPath);
}
Server::get(LoggerInterface::class)->error('Couldn\'t move ' . $file_path . ' to the trash bin', ['app' => 'files_trashbin']);
}
Server::get(LoggerInterface::class)->error('Couldn\'t move ' . $file_path . ' to the trash bin', ['app' => 'files_trashbin']);
}

if ($sourceStorage->file_exists($sourceInternalPath)) { // failed to delete the original file, abort
if ($sourceStorage->is_dir($sourceInternalPath)) {
$sourceStorage->rmdir($sourceInternalPath);
} else {
$sourceStorage->unlink($sourceInternalPath);
}
if ($sourceStorage->file_exists($sourceInternalPath)) { // failed to delete the original file, abort
if ($sourceStorage->is_dir($sourceInternalPath)) {
$sourceStorage->rmdir($sourceInternalPath);
} else {
$sourceStorage->unlink($sourceInternalPath);
}

if ($sourceStorage->file_exists($sourceInternalPath)) {
// undo the cache move
$sourceStorage->getUpdater()->renameFromStorage($trashStorage, $trashInternalPath, $sourceInternalPath);
} else {
$trashStorage->getUpdater()->remove($trashInternalPath);
if ($sourceStorage->file_exists($sourceInternalPath)) {
// undo the cache move
$sourceStorage->getUpdater()->renameFromStorage($trashStorage, $trashInternalPath, $sourceInternalPath);
} else {
$trashStorage->getUpdater()->remove($trashInternalPath);
}
$moveSuccessful = false;
}
$moveSuccessful = false;
}

if (!$moveSuccessful) {
Server::get(LoggerInterface::class)->error(
'trash move failed, removing trash metadata and payload',
[
'app' => 'files_trashbin',
'user' => $owner,
'filename' => $filename,
'timestamp' => $timestamp,
]
);
// The metadata row belongs to the owner, even when another user initiated
// the deletion.
self::deleteTrashRow($owner, $filename, $timestamp);
if ($trashStorage->file_exists($trashInternalPath)) {
if ($trashStorage->is_dir($trashInternalPath)) {
$trashStorage->rmdir($trashInternalPath);
} else {
$trashStorage->unlink($trashInternalPath);
if (!$moveSuccessful) {
Server::get(LoggerInterface::class)->error(
'trash move failed, removing trash metadata and payload',
[
'app' => 'files_trashbin',
'user' => $owner,
'filename' => $filename,
'timestamp' => $timestamp,
]
);
// The metadata row belongs to the owner, even when another user initiated
// the deletion.
self::deleteTrashRow($owner, $filename, $timestamp);
if ($trashStorage->file_exists($trashInternalPath)) {
if ($trashStorage->is_dir($trashInternalPath)) {
$trashStorage->rmdir($trashInternalPath);
} else {
$trashStorage->unlink($trashInternalPath);
}
}
$trashStorage->getUpdater()->remove($trashInternalPath);
}
$trashStorage->getUpdater()->remove($trashInternalPath);
} finally {
$propagator->commitBatch();
}

if ($moveSuccessful) {
Expand Down
86 changes: 86 additions & 0 deletions apps/files_trashbin/tests/StorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@

namespace OCA\Files_Trashbin\Tests;

use OC\Files\Cache\Propagator;
use OC\Files\Cache\Updater;
use OC\Files\Filesystem;
use OC\Files\ObjectStore\ObjectStoreStorage;
use OC\Files\Storage\Common;
use OC\Files\Storage\Local;
use OC\Files\Storage\Temporary;
use OC\Files\Storage\Wrapper\Wrapper;
use OC\Files\View;
use OCA\Files_Trashbin\AppInfo\Application;
use OCA\Files_Trashbin\Events\MoveToTrashEvent;
use OCA\Files_Trashbin\Exceptions\CopyRecursiveException;
use OCA\Files_Trashbin\Storage;
use OCA\Files_Trashbin\Trash\ITrashManager;
use OCA\Files_Trashbin\Trashbin;
Expand Down Expand Up @@ -48,6 +51,23 @@ public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalP
}
}


class TemporaryFailingRead extends Temporary {
private bool $failReads = false;

public function failReads(): void {
$this->failReads = true;
}

public function fopen(string $path, string $mode) {
if ($this->failReads && str_starts_with($mode, 'r')) {
throw new CopyRecursiveException();
}

return parent::fopen($path, $mode);
}
}

/**
* Class Storage
*
Expand Down Expand Up @@ -746,6 +766,72 @@ public function testTrashbinCollision(): void {
$this->assertEquals('bar', $this->rootView->file_get_contents($this->user . '/files_trashbin/files/test.txt.d1001'));
}

public function testDeleteMultipleFoldersWithSameTimestamp(): void {
$timeFactory = $this->createMock(ITimeFactory::class);
$timeFactory->method('getTime')
->willReturn(1000);
$this->overwriteService(ITimeFactory::class, $timeFactory);

$folders = [
'alpha' => 'source-a',
'bravo' => 'source-b',
'charlie' => 'source-c',
];
foreach ($folders as $folder => $location) {
$this->userView->mkdir($location . '/' . $folder);
for ($i = 0; $i < 10; $i++) {
$this->userView->file_put_contents($location . '/' . $folder . '/file-' . $i . '.txt', $folder . '-' . $i);
}
}

foreach ($folders as $folder => $location) {
$this->assertTrue($this->userView->rmdir($location . '/' . $folder));
}

$trashEntries = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files');
$trashNames = array_map(static fn ($entry) => $entry->getName(), $trashEntries);
sort($trashNames);
$this->assertSame(['alpha.d1000', 'bravo.d1000', 'charlie.d1000'], $trashNames);

foreach ($folders as $folder => $location) {
$trashPath = $this->user . '/files_trashbin/files/' . $folder . '.d1000';
$children = $this->rootView->getDirectoryContent($trashPath);
$this->assertCount(10, $children);
for ($i = 0; $i < 10; $i++) {
$this->assertSame($folder . '-' . $i, $this->rootView->file_get_contents($trashPath . '/file-' . $i . '.txt'));
}
$this->assertSame($location, Trashbin::getLocation($this->user, $folder, '1000'));
}
}

public function testFailedMoveCommitsPropagationBatch(): void {
$timeFactory = $this->createMock(ITimeFactory::class);
$timeFactory->method('getTime')
->willReturn(1000);
$this->overwriteService(ITimeFactory::class, $timeFactory);

[$userStorage,] = $this->rootView->resolvePath('/' . $this->user . '/files/test.txt');
while ($userStorage instanceof Wrapper) {
$userStorage = $userStorage->getWrapperStorage();
}
$propagator = $this->createMock(Propagator::class);
$propagator->expects($this->once())->method('beginBatch');
$propagator->expects($this->once())->method('commitBatch');
$propagatorProperty = new \ReflectionProperty(Common::class, 'propagator');
$propagatorProperty->setValue($userStorage, $propagator);

$failingStorage = new TemporaryFailingRead([]);
Filesystem::mount($failingStorage, [], $this->user . '/files/failing');
$this->userView->file_put_contents('failing/broken.txt', 'broken');
$failingStorage->getScanner()->scan('');
$failingStorage->failReads();

[$storage, $internalPath] = $this->userView->resolvePath('failing/broken.txt');
$this->assertFalse(Server::get(ITrashManager::class)->moveToTrash($storage, $internalPath));
$this->assertFalse($this->rootView->file_exists($this->user . '/files_trashbin/files/broken.txt.d1000'));
$this->assertFalse(Trashbin::getLocation($this->user, 'broken.txt', '1000'));
}

public function testMoveFromStoragePreserveFileId(): void {
$this->userView->file_put_contents('test.txt', 'foo');
$fileId = $this->userView->getFileInfo('test.txt')->getId();
Expand Down