diff --git a/changelog/unreleased/39016 b/changelog/unreleased/39016 new file mode 100644 index 000000000000..f0a4b78fbc55 --- /dev/null +++ b/changelog/unreleased/39016 @@ -0,0 +1,3 @@ +Bugfix: Update file size in cache after writing empty content to non-empty file + +https://github.com/owncloud/core/pull/39016 diff --git a/lib/private/Files/View.php b/lib/private/Files/View.php index 01ffa2e32f95..3cbca2d4d5f2 100644 --- a/lib/private/Files/View.php +++ b/lib/private/Files/View.php @@ -1234,7 +1234,7 @@ private function basicOperation($operation, $path, $hooks = [], $extraParam = nu throw $e; } - if ($result) { + if ($result !== false) { if (\in_array('delete', $hooks)) { $this->removeUpdate($storage, $internalPath); } diff --git a/tests/lib/Files/ViewTest.php b/tests/lib/Files/ViewTest.php index 83baa4a13829..86458f3a1213 100644 --- a/tests/lib/Files/ViewTest.php +++ b/tests/lib/Files/ViewTest.php @@ -117,7 +117,7 @@ protected function tearDown(): void { \OC_User::setUserId($this->user); foreach ($this->storages as $storage) { $cache = $storage->getCache(); - $ids = $cache->getAll(); + $cache->getAll(); $cache->clear(); } @@ -1884,7 +1884,7 @@ function () use ($view, $lockedPath, &$lockTypeDuring, $operation) { $lockTypeDuring = $this->getFileLockType($view, $lockedPath); if ($operation === 'fopen') { - return \fopen('data://text/plain,test', 'r'); + return \fopen('data://text/plain,test', 'rb'); } return true; } @@ -2750,4 +2750,27 @@ public function testDeleteNonShareFolder($shareFolder, $deleteFolder) { $view->mkdir($deleteFolder); $this->assertTrue($view->rmdir($deleteFolder)); } + + public function testCacheSizeUpdatedWhenEmptyingFile (): void { + $storage1 = $this->getTestStorage(); + Filesystem::mount($storage1, [], '/'); + + $rootView = new View(''); + + # test with string as $data + $rootView->file_put_contents('welcome.txt', '1234567890'); + $this->assertEquals(10, $rootView->filesize('welcome.txt')); + + $rootView->file_put_contents('welcome.txt', ''); + $this->assertEquals(0, $rootView->filesize('welcome.txt')); + + # test with resource as $data + $stream = fopen('data://text/plain,1234567890', 'rb'); + $rootView->file_put_contents('welcome.txt', $stream); + $this->assertEquals(10, $rootView->filesize('welcome.txt')); + + $stream = fopen('data://text/plain,', 'rb'); + $rootView->file_put_contents('welcome.txt', ''); + $this->assertEquals(0, $rootView->filesize('welcome.txt')); + } }