Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: file size in cache is set back to zero if empty content is writt… #39016

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions changelog/unreleased/39016
Original file line number Diff line number Diff line change
@@ -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
5 changes: 4 additions & 1 deletion lib/private/Files/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down Expand Up @@ -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`';
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Files/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
27 changes: 25 additions & 2 deletions tests/lib/Files/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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'));
}
}