Skip to content

Commit 7d24904

Browse files
Merge pull request #58543 from nextcloud/filesize-non-existing
2 parents 52a1d63 + 3f19e13 commit 7d24904

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
@@ -97,11 +97,15 @@ public function is_file(string $path): bool {
9797
}
9898

9999
public function filesize(string $path): int|float|false {
100-
if ($this->is_dir($path)) {
101-
return 0; //by definition
100+
$type = $this->filetype($path);
101+
if ($type === false) {
102+
return false;
103+
}
104+
if ($type !== 'file') {
105+
return 0;
102106
} else {
103107
$stat = $this->stat($path);
104-
return isset($stat['size']) ? $stat['size'] : 0;
108+
return $stat['size'] ?? 0;
105109
}
106110
}
107111

lib/private/Files/Storage/Local.php

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

220220
public function filetype(string $path): string|false {
221-
$filetype = filetype($this->getSourcePath($path));
221+
$filetype = @filetype($this->getSourcePath($path));
222222
if ($filetype === 'link') {
223223
$filetype = filetype(realpath($this->getSourcePath($path)));
224224
}
225225
return $filetype;
226226
}
227227

228228
public function filesize(string $path): int|float|false {
229-
if (!$this->is_file($path)) {
229+
$type = $this->filetype($path);
230+
if ($type === false) {
231+
return false;
232+
}
233+
if ($type !== 'file') {
230234
return 0;
231235
}
232236
$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)