Skip to content

Commit 99801e4

Browse files
committed
fix: file size in cache is set back to zero if empty content is written to a non-empty file
1 parent 0de7e40 commit 99801e4

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

changelog/unreleased/39016

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Bugfix: Update file size in cache after writing empty content to non-empty file
2+
3+
https://github.com/owncloud/core/pull/39016

lib/private/Files/View.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ private function basicOperation($operation, $path, $hooks = [], $extraParam = nu
12341234
throw $e;
12351235
}
12361236

1237-
if ($result) {
1237+
if ($result !== false) {
12381238
if (\in_array('delete', $hooks)) {
12391239
$this->removeUpdate($storage, $internalPath);
12401240
}

tests/lib/Files/ViewTest.php

+25-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ protected function tearDown(): void {
117117
\OC_User::setUserId($this->user);
118118
foreach ($this->storages as $storage) {
119119
$cache = $storage->getCache();
120-
$ids = $cache->getAll();
120+
$cache->getAll();
121121
$cache->clear();
122122
}
123123

@@ -1884,7 +1884,7 @@ function () use ($view, $lockedPath, &$lockTypeDuring, $operation) {
18841884
$lockTypeDuring = $this->getFileLockType($view, $lockedPath);
18851885

18861886
if ($operation === 'fopen') {
1887-
return \fopen('data://text/plain,test', 'r');
1887+
return \fopen('data://text/plain,test', 'rb');
18881888
}
18891889
return true;
18901890
}
@@ -2750,4 +2750,27 @@ public function testDeleteNonShareFolder($shareFolder, $deleteFolder) {
27502750
$view->mkdir($deleteFolder);
27512751
$this->assertTrue($view->rmdir($deleteFolder));
27522752
}
2753+
2754+
public function testCacheSizeUpdatedWhenEmptyingFile(): void {
2755+
$storage1 = $this->getTestStorage();
2756+
Filesystem::mount($storage1, [], '/');
2757+
2758+
$rootView = new View('');
2759+
2760+
# test with string as $data
2761+
$rootView->file_put_contents('welcome.txt', '1234567890');
2762+
$this->assertEquals(10, $rootView->filesize('welcome.txt'));
2763+
2764+
$rootView->file_put_contents('welcome.txt', '');
2765+
$this->assertEquals(0, $rootView->filesize('welcome.txt'));
2766+
2767+
# test with resource as $data
2768+
$stream = fopen('data://text/plain,1234567890', 'rb');
2769+
$rootView->file_put_contents('welcome.txt', $stream);
2770+
$this->assertEquals(10, $rootView->filesize('welcome.txt'));
2771+
2772+
$stream = fopen('data://text/plain,', 'rb');
2773+
$rootView->file_put_contents('welcome.txt', '');
2774+
$this->assertEquals(0, $rootView->filesize('welcome.txt'));
2775+
}
27532776
}

0 commit comments

Comments
 (0)