Skip to content

Commit d60d1e6

Browse files
Merge pull request #58554 from nextcloud/backport/58543/stable32
[stable32] fix: correctly return false for filesize on non-existing file
2 parents 8668020 + 32299a8 commit d60d1e6

3 files changed

Lines changed: 15 additions & 5 deletions

File tree

lib/private/Files/Storage/Common.php

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

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

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)