Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Already pass size difference to the cache updater to avoid calculation of the folder size to update #28310

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions apps/dav/lib/Connector/Sabre/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,16 +236,17 @@ public function put($data) {
}
}

$previousFileSize = $exists ? $partStorage->filesize($internalPath) : 0;
if ($partStorage->instanceOfStorage(Storage\IWriteStreamStorage::class)) {
$isEOF = false;
$wrappedData = CallbackWrapper::wrap($data, null, null, null, null, function ($stream) use (&$isEOF) {
$isEOF = feof($stream);
});

$result = true;
$count = -1;
$writtenByteCount = -1;
try {
$count = $partStorage->writeStream($internalPartPath, $wrappedData);
$writtenByteCount = $partStorage->writeStream($internalPartPath, $wrappedData);

Check notice

Code scanning / Psalm

PossiblyInvalidArgument

Argument 2 of OCP\Files\Storage\IWriteStreamStorage::writeStream expects resource, possibly different type bool|resource provided
} catch (GenericFileException $e) {
$result = false;
} catch (BadGateway $e) {
Expand All @@ -266,7 +267,7 @@ public function put($data) {
// because we have no clue about the cause we can only throw back a 500/Internal Server Error
throw new Exception($this->l10n->t('Could not write file contents'));
}
[$count, $result] = \OC_Helper::streamCopy($data, $target);
[$writtenByteCount, $result] = \OC_Helper::streamCopy($data, $target);

Check notice

Code scanning / Psalm

PossiblyInvalidArgument

Argument 2 of OC_Helper::streamCopy expects resource, possibly different type resource|true provided
fclose($target);
}

Expand All @@ -280,7 +281,7 @@ public function put($data) {
$this->l10n->t(
'Error while copying file to target location (copied: %1$s, expected filesize: %2$s)',
[
$this->l10n->n('%n byte', '%n bytes', $count),
$this->l10n->n('%n byte', '%n bytes', $writtenByteCount),
$this->l10n->n('%n byte', '%n bytes', $expected),
],
)
Expand All @@ -293,13 +294,13 @@ public function put($data) {
// compare expected and actual size
if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['REQUEST_METHOD'] === 'PUT') {
$expected = (int)$_SERVER['CONTENT_LENGTH'];
if ($count !== $expected) {
if ($writtenByteCount !== $expected) {
throw new BadRequest(
$this->l10n->t(
'Expected filesize of %1$s but read (from Nextcloud client) and wrote (to Nextcloud storage) %2$s. Could either be a network problem on the sending side or a problem writing to the storage on the server side.',
[
$this->l10n->n('%n byte', '%n bytes', $expected),
$this->l10n->n('%n byte', '%n bytes', $count),
$this->l10n->n('%n byte', '%n bytes', $writtenByteCount),
],
)
);
Expand Down Expand Up @@ -365,7 +366,8 @@ public function put($data) {
}

// since we skipped the view we need to scan and emit the hooks ourselves
$storage->getUpdater()->update($internalPath);
$sizeDifference = isset($previousFileSize, $writtenByteCount) ? ($writtenByteCount - $previousFileSize) : null;
$storage->getUpdater()->update($internalPath, null, $sizeDifference);

try {
$this->changeLock(ILockingProvider::LOCK_SHARED);
Expand Down
12 changes: 7 additions & 5 deletions lib/private/Files/Cache/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ public function propagate($path, $time = null) {
*
* @param string $path
* @param int $time
* @param int $sizeDifference
*/
public function update($path, $time = null) {
public function update($path, $time = null, $sizeDifference = null) {
if (!$this->enabled or Scanner::isPartialFile($path)) {
return;
}
Expand All @@ -129,14 +130,15 @@ public function update($path, $time = null) {
) {
$sizeDifference = $data['size'] - $data['oldSize'];
} else {
// scanner didn't provide size info, fallback to full size calculation
$sizeDifference = 0;
if ($this->cache instanceof Cache) {
// scanner didn't provide size info, fallback to full size calculation if the difference was not already passed
// otherwise we can update through the propagator
if ($this->cache instanceof Cache && $sizeDifference === null) {
$this->cache->correctFolderSize($path, $data);
$sizeDifference = 0;
}
}
$this->correctParentStorageMtime($path);
$this->propagator->propagateChange($path, $time, $sizeDifference);
$this->propagator->propagateChange($path, $time, $sizeDifference ?? 0);
}

/**
Expand Down
14 changes: 10 additions & 4 deletions lib/private/Files/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
use OC\Files\Storage\Storage;
use OC\User\LazyUser;
use OC\Share\Share;
use OC\User\User;
use OCA\Files_Sharing\SharedMount;
use OCP\Constants;
use OCP\Files\Cache\ICacheEntry;
Expand Down Expand Up @@ -316,12 +315,12 @@ public function enableCacheUpdate() {
$this->updaterEnabled = true;
}

protected function writeUpdate(Storage $storage, $internalPath, $time = null) {
protected function writeUpdate(Storage $storage, $internalPath, $time = null, $size = null) {
if ($this->updaterEnabled) {
if (is_null($time)) {
$time = time();
}
$storage->getUpdater()->update($internalPath, $time);
$storage->getUpdater()->update($internalPath, $time, $size);
}
}

Expand Down Expand Up @@ -1175,6 +1174,7 @@ private function basicOperation($operation, $path, $hooks = [], $extraParam = nu
$this->unlockFile($path, ILockingProvider::LOCK_SHARED);
throw $e;
}
$previousSize = $storage->filesize($internalPath);
}
try {
if (!is_null($extraParam)) {
Expand All @@ -1195,7 +1195,13 @@ private function basicOperation($operation, $path, $hooks = [], $extraParam = nu
$this->removeUpdate($storage, $internalPath);
}
if ($result !== false && in_array('write', $hooks, true) && $operation !== 'fopen' && $operation !== 'touch') {
$this->writeUpdate($storage, $internalPath);
$size = null;
if ($operation === 'mkdir') {
$size = 0;
} elseif ($operation === 'file_put_contents' && isset($previousSize)) {
$size = $result - $previousSize;
}
$this->writeUpdate($storage, $internalPath, null, $size);
juliusknorr marked this conversation as resolved.
Show resolved Hide resolved
}
if ($result !== false && in_array('touch', $hooks)) {
$this->writeUpdate($storage, $internalPath, $extraParam);
Expand Down