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/Cache/Cache.php b/lib/private/Files/Cache/Cache.php index 5dc8a1a9bd01..fde771184e64 100644 --- a/lib/private/Files/Cache/Cache.php +++ b/lib/private/Files/Cache/Cache.php @@ -359,6 +359,9 @@ public function update($id, array $data) { if ($value === '') { $data[$param] = null; } + if ($value === null && \in_array($param, ['mtime', 'storage_mtime'], true)) { + $data[$param] = 0; + } } } @@ -429,7 +432,7 @@ protected function buildParts(array $data) { $params = []; $queryParts = []; foreach ($data as $name => $value) { - if (\array_search($name, $fields) !== false) { + if (\in_array($name, $fields, true)) { if ($name === 'path') { $params[] = \md5($value); $queryParts[] = '`path_hash`'; diff --git a/lib/private/Files/View.php b/lib/private/Files/View.php index 9bd55a420c3b..09fe7049aa0e 100644 --- a/lib/private/Files/View.php +++ b/lib/private/Files/View.php @@ -1233,7 +1233,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 61b15a2dd5f6..edce5a0412ea 100644 --- a/tests/lib/Files/ViewTest.php +++ b/tests/lib/Files/ViewTest.php @@ -119,7 +119,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(); } @@ -1886,7 +1886,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; } @@ -2752,4 +2752,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')); + } }