Skip to content

Commit bc9cc63

Browse files
authored
Merge pull request #58553 from nextcloud/backport/58543/stable33
[stable33] fix: correctly return false for filesize on non-existing file
2 parents 360de52 + e88748e commit bc9cc63

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

lib/private/Files/Storage/Common.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,15 @@ public function is_file(string $path): bool {
9292
}
9393

9494
public function filesize(string $path): int|float|false {
95-
if ($this->is_dir($path)) {
96-
return 0; //by definition
95+
$type = $this->filetype($path);
96+
if ($type === false) {
97+
return false;
98+
}
99+
if ($type !== 'file') {
100+
return 0;
97101
} else {
98102
$stat = $this->stat($path);
99-
return isset($stat['size']) ? $stat['size'] : 0;
103+
return $stat['size'] ?? 0;
100104
}
101105
}
102106

lib/private/Files/Storage/Local.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,19 @@ public function getMetaData(string $path): ?array {
215215
}
216216

217217
public function filetype(string $path): string|false {
218-
$filetype = filetype($this->getSourcePath($path));
218+
$filetype = @filetype($this->getSourcePath($path));
219219
if ($filetype == 'link') {
220220
$filetype = filetype(realpath($this->getSourcePath($path)));
221221
}
222222
return $filetype;
223223
}
224224

225225
public function filesize(string $path): int|float|false {
226-
if (!$this->is_file($path)) {
226+
$type = $this->filetype($path);
227+
if ($type === false) {
228+
return false;
229+
}
230+
if ($type !== 'file') {
227231
return 0;
228232
}
229233
$fullPath = $this->getSourcePath($path);

tests/lib/Files/Storage/Storage.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@ public function testStat(): void {
311311

312312
$this->instance->unlink('/lorem.txt');
313313
$this->assertTrue($this->instance->hasUpdated('/', $mtimeStart - 5));
314+
315+
$this->assertFalse($this->instance->filesize('/non-existing-file.txt'));
314316
}
315317

316318
/**

0 commit comments

Comments
 (0)