Skip to content

Commit 5439469

Browse files
committed
optimize getMetaData for local storage
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent 32577f2 commit 5439469

File tree

1 file changed

+57
-6
lines changed

1 file changed

+57
-6
lines changed

lib/private/Files/Storage/Local.php

+57-6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
use OC\Files\Filesystem;
4545
use OC\Files\Storage\Wrapper\Jail;
46+
use OCP\Constants;
4647
use OCP\Files\ForbiddenException;
4748
use OCP\Files\Storage\IStorage;
4849
use OCP\ILogger;
@@ -111,9 +112,9 @@ public function rmdir($path) {
111112
if (in_array($file->getBasename(), ['.', '..'])) {
112113
$it->next();
113114
continue;
114-
} elseif ($file->isDir()) {
115+
} else if ($file->isDir()) {
115116
rmdir($file->getPathname());
116-
} elseif ($file->isFile() || $file->isLink()) {
117+
} else if ($file->isFile() || $file->isLink()) {
117118
unlink($file->getPathname());
118119
}
119120
$it->next();
@@ -151,6 +152,54 @@ public function stat($path) {
151152
return $statResult;
152153
}
153154

155+
/**
156+
* @inheritdoc
157+
*/
158+
public function getMetaData($path) {
159+
$fullPath = $this->getSourcePath($path);
160+
$stat = @stat($fullPath);
161+
if (!$stat) {
162+
return null;
163+
}
164+
165+
$permissions = Constants::PERMISSION_SHARE;
166+
$statPermissions = $stat['mode'];
167+
$isDir = ($statPermissions & 0x4000) === 0x4000;
168+
if ($statPermissions & 0x0100) {
169+
$permissions += Constants::PERMISSION_READ;
170+
}
171+
if ($statPermissions & 0x0080) {
172+
$permissions += Constants::PERMISSION_UPDATE;
173+
if ($isDir) {
174+
$permissions += Constants::PERMISSION_CREATE;
175+
}
176+
}
177+
178+
if (!($path === '' || $path === '/')) { // deletable depends on the parents unix permissions
179+
$parent = dirname($fullPath);
180+
if (is_writable($parent)) {
181+
$permissions += Constants::PERMISSION_DELETE;
182+
}
183+
}
184+
185+
$data = [];
186+
$data['mimetype'] = $isDir ? 'httpd/unix-directory' : \OC::$server->getMimeTypeDetector()->detectPath($path);
187+
$data['mtime'] = $stat['mtime'];
188+
if ($data['mtime'] === false) {
189+
$data['mtime'] = time();
190+
}
191+
if ($isDir) {
192+
$data['size'] = -1; //unknown
193+
} else {
194+
$data['size'] = $stat['size'];
195+
}
196+
$data['etag'] = $this->calculateEtag($path, $stat);
197+
$data['storage_mtime'] = $data['mtime'];
198+
$data['permissions'] = $permissions;
199+
200+
return $data;
201+
}
202+
154203
public function filetype($path) {
155204
$filetype = filetype($this->getSourcePath($path));
156205
if ($filetype == 'link') {
@@ -424,9 +473,13 @@ public function isLocal() {
424473
* @return string
425474
*/
426475
public function getETag($path) {
427-
if ($this->is_file($path)) {
428-
$stat = $this->stat($path);
476+
return $this->calculateEtag($path, $this->stat($path));
477+
}
429478

479+
private function calculateEtag(string $path, array $stat): string {
480+
if ($stat['mode'] & 0x4000) { // is_dir
481+
return parent::getETag($path);
482+
} else {
430483
if ($stat === false) {
431484
return md5('');
432485
}
@@ -446,8 +499,6 @@ public function getETag($path) {
446499
}
447500

448501
return md5($toHash);
449-
} else {
450-
return parent::getETag($path);
451502
}
452503
}
453504

0 commit comments

Comments
 (0)