Skip to content
Merged
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
20 changes: 16 additions & 4 deletions apps/files_trashbin/lib/Trashbin.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
namespace OCA\Files_Trashbin;

use OC\Files\Filesystem;
use OC\Files\ObjectStore\ObjectStoreStorage;
use OC\Files\View;
use OCA\Files_Trashbin\AppInfo\Application;
use OCA\Files_Trashbin\Command\Expire;
Expand Down Expand Up @@ -278,12 +279,22 @@ public static function move2trash($file_path, $ownerOnly = false) {
/** @var \OC\Files\Storage\Storage $sourceStorage */
[$sourceStorage, $sourceInternalPath] = $ownerView->resolvePath('/files/' . $ownerPath);


if ($trashStorage->file_exists($trashInternalPath)) {
$trashStorage->unlink($trashInternalPath);
}

$connection = \OC::$server->getDatabaseConnection();
$connection->beginTransaction();
$trashStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $trashInternalPath);

try {
$moveSuccessful = true;
if ($trashStorage->file_exists($trashInternalPath)) {
$trashStorage->unlink($trashInternalPath);

// when moving within the same object store, the cache update done above is enough to move the file
if (!($trashStorage->instanceOfStorage(ObjectStoreStorage::class) && $trashStorage->getId() === $sourceStorage->getId())) {
$trashStorage->moveFromStorage($sourceStorage, $sourceInternalPath, $trashInternalPath);
}
$trashStorage->moveFromStorage($sourceStorage, $sourceInternalPath, $trashInternalPath);
} catch (\OCA\Files_Trashbin\Exceptions\CopyRecursiveException $e) {
$moveSuccessful = false;
if ($trashStorage->file_exists($trashInternalPath)) {
Expand All @@ -298,10 +309,11 @@ public static function move2trash($file_path, $ownerOnly = false) {
} else {
$sourceStorage->unlink($sourceInternalPath);
}
$connection->rollBack();
return false;
}

$trashStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $trashInternalPath);
$connection->commit();

if ($moveSuccessful) {
$query = \OC_DB::prepare("INSERT INTO `*PREFIX*files_trash` (`id`,`timestamp`,`location`,`user`) VALUES (?,?,?,?)");
Expand Down
21 changes: 14 additions & 7 deletions lib/private/Files/Cache/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

namespace OC\Files\Cache;

use OC\Files\FileInfo;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\Cache\IUpdater;
use OCP\Files\Storage\IStorage;
Expand Down Expand Up @@ -187,7 +188,9 @@ public function renameFromStorage(IStorage $sourceStorage, $source, $target) {
$sourceUpdater = $sourceStorage->getUpdater();
$sourcePropagator = $sourceStorage->getPropagator();

if ($sourceCache->inCache($source)) {
$sourceInfo = $sourceCache->get($source);

if ($sourceInfo !== false) {
if ($this->cache->inCache($target)) {
$this->cache->remove($target);
}
Expand All @@ -197,13 +200,17 @@ public function renameFromStorage(IStorage $sourceStorage, $source, $target) {
} else {
$this->cache->moveFromCache($sourceCache, $source, $target);
}
}

if (pathinfo($source, PATHINFO_EXTENSION) !== pathinfo($target, PATHINFO_EXTENSION)) {
// handle mime type change
$mimeType = $this->storage->getMimeType($target);
$fileId = $this->cache->getId($target);
$this->cache->update($fileId, ['mimetype' => $mimeType]);
$sourceExtension = pathinfo($source, PATHINFO_EXTENSION);
$targetExtension = pathinfo($target, PATHINFO_EXTENSION);
$targetIsTrash = preg_match("/d\d+/", $targetExtension);

if ($sourceExtension !== $targetExtension && $sourceInfo->getMimeType() !== FileInfo::MIMETYPE_FOLDER && !$targetIsTrash) {
// handle mime type change
$mimeType = $this->storage->getMimeType($target);
$fileId = $this->cache->getId($target);
$this->cache->update($fileId, ['mimetype' => $mimeType]);
}
}

if ($sourceCache instanceof Cache) {
Expand Down
25 changes: 15 additions & 10 deletions lib/private/Files/Storage/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
use OC\Files\Storage\Wrapper\Wrapper;
use OCP\Files\EmptyFileNameException;
use OCP\Files\FileNameTooLongException;
use OCP\Files\GenericFileException;
use OCP\Files\InvalidCharacterInPathException;
use OCP\Files\InvalidDirectoryException;
use OCP\Files\InvalidPathException;
Expand Down Expand Up @@ -618,18 +619,19 @@ public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $t
}
} else {
$source = $sourceStorage->fopen($sourceInternalPath, 'r');
// TODO: call fopen in a way that we execute again all storage wrappers
// to avoid that we bypass storage wrappers which perform important actions
// for this operation. Same is true for all other operations which
// are not the same as the original one.Once this is fixed we also
// need to adjust the encryption wrapper.
$target = $this->fopen($targetInternalPath, 'w');
list(, $result) = \OC_Helper::streamCopy($source, $target);
$result = false;
if ($source) {
try {
$this->writeStream($targetInternalPath, $source);
$result = true;
} catch (\Exception $e) {
\OC::$server->getLogger()->logException($e, ['level' => ILogger::WARN, 'message' => 'Failed to copy stream to storage']);
}
}

if ($result and $preserveMtime) {
$this->touch($targetInternalPath, $sourceStorage->filemtime($sourceInternalPath));
}
fclose($source);
fclose($target);

if (!$result) {
// delete partially written target file
Expand Down Expand Up @@ -855,10 +857,13 @@ public function needsPartFile() {
public function writeStream(string $path, $stream, int $size = null): int {
$target = $this->fopen($path, 'w');
if (!$target) {
return 0;
throw new GenericFileException("Failed to open $path for writing");
}
try {
[$count, $result] = \OC_Helper::streamCopy($stream, $target);
if (!$result) {
throw new GenericFileException("Failed to copy stream");
}
} finally {
fclose($target);
fclose($stream);
Expand Down
8 changes: 7 additions & 1 deletion lib/private/Files/Storage/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use OC\Files\Filesystem;
use OC\Files\Storage\Wrapper\Jail;
use OCP\Files\ForbiddenException;
use OCP\Files\GenericFileException;
use OCP\Files\Storage\IStorage;
use OCP\ILogger;

Expand Down Expand Up @@ -500,6 +501,11 @@ public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $t
}

public function writeStream(string $path, $stream, int $size = null): int {
return (int)file_put_contents($this->getSourcePath($path), $stream);
$result = file_put_contents($this->getSourcePath($path), $stream);
if ($result === false) {
throw new GenericFileException("Failed write steam to $path");
} else {
return $result;
}
}
}
14 changes: 4 additions & 10 deletions tests/lib/Files/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use OC\Files\Mount\MountPoint;
use OC\Files\Storage\Common;
use OC\Files\Storage\Temporary;
use OC\Files\Stream\Quota;
use OC\Files\View;
use OCP\Files\Config\IMountProvider;
use OCP\Files\FileInfo;
Expand Down Expand Up @@ -242,7 +241,7 @@ public function testGetPath() {
$this->assertEquals('/foo.txt', $folderView->getPath($id2));
}


public function testGetPathNotExisting() {
$this->expectException(\OCP\Files\NotFoundException::class);

Expand Down Expand Up @@ -1167,13 +1166,8 @@ private function doTestCopyRenameFail($operation) {
->setMethods(['fopen'])
->getMock();

$storage2->expects($this->any())
->method('fopen')
->will($this->returnCallback(function ($path, $mode) use ($storage2) {
/** @var \PHPUnit_Framework_MockObject_MockObject | \OC\Files\Storage\Temporary $storage2 */
$source = fopen($storage2->getSourcePath($path), $mode);
return Quota::wrap($source, 9);
}));
$storage2->method('writeStream')
->willReturn(0);

$storage1->mkdir('sub');
$storage1->file_put_contents('foo.txt', '0123456789ABCDEFGH');
Expand Down Expand Up @@ -1291,7 +1285,7 @@ public function testGetRelativePathOnNull() {
$this->assertNull($view->getRelativePath(null));
}


public function testNullAsRoot() {
$this->expectException(\InvalidArgumentException::class);

Expand Down