Skip to content

Commit

Permalink
optimize getMetaData for local storage
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <robin@icewind.nl>
  • Loading branch information
icewind1991 committed Apr 1, 2020
1 parent 32577f2 commit 5439469
Showing 1 changed file with 57 additions and 6 deletions.
63 changes: 57 additions & 6 deletions lib/private/Files/Storage/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

use OC\Files\Filesystem;
use OC\Files\Storage\Wrapper\Jail;
use OCP\Constants;
use OCP\Files\ForbiddenException;
use OCP\Files\Storage\IStorage;
use OCP\ILogger;
Expand Down Expand Up @@ -111,9 +112,9 @@ public function rmdir($path) {
if (in_array($file->getBasename(), ['.', '..'])) {
$it->next();
continue;
} elseif ($file->isDir()) {
} else if ($file->isDir()) {
rmdir($file->getPathname());
} elseif ($file->isFile() || $file->isLink()) {
} else if ($file->isFile() || $file->isLink()) {
unlink($file->getPathname());
}
$it->next();
Expand Down Expand Up @@ -151,6 +152,54 @@ public function stat($path) {
return $statResult;
}

/**
* @inheritdoc
*/
public function getMetaData($path) {
$fullPath = $this->getSourcePath($path);
$stat = @stat($fullPath);
if (!$stat) {
return null;
}

$permissions = Constants::PERMISSION_SHARE;
$statPermissions = $stat['mode'];
$isDir = ($statPermissions & 0x4000) === 0x4000;
if ($statPermissions & 0x0100) {
$permissions += Constants::PERMISSION_READ;
}
if ($statPermissions & 0x0080) {
$permissions += Constants::PERMISSION_UPDATE;
if ($isDir) {
$permissions += Constants::PERMISSION_CREATE;
}
}

if (!($path === '' || $path === '/')) { // deletable depends on the parents unix permissions
$parent = dirname($fullPath);
if (is_writable($parent)) {
$permissions += Constants::PERMISSION_DELETE;
}
}

$data = [];
$data['mimetype'] = $isDir ? 'httpd/unix-directory' : \OC::$server->getMimeTypeDetector()->detectPath($path);
$data['mtime'] = $stat['mtime'];
if ($data['mtime'] === false) {
$data['mtime'] = time();
}
if ($isDir) {
$data['size'] = -1; //unknown
} else {
$data['size'] = $stat['size'];
}
$data['etag'] = $this->calculateEtag($path, $stat);
$data['storage_mtime'] = $data['mtime'];
$data['permissions'] = $permissions;

return $data;
}

public function filetype($path) {
$filetype = filetype($this->getSourcePath($path));
if ($filetype == 'link') {
Expand Down Expand Up @@ -424,9 +473,13 @@ public function isLocal() {
* @return string
*/
public function getETag($path) {
if ($this->is_file($path)) {
$stat = $this->stat($path);
return $this->calculateEtag($path, $this->stat($path));
}

private function calculateEtag(string $path, array $stat): string {
if ($stat['mode'] & 0x4000) { // is_dir
return parent::getETag($path);
} else {
if ($stat === false) {
return md5('');
}
Expand All @@ -446,8 +499,6 @@ public function getETag($path) {
}

return md5($toHash);
} else {
return parent::getETag($path);
}
}

Expand Down

0 comments on commit 5439469

Please sign in to comment.