Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Revert "store unencrypted size in the unencrypted_size column"
This reverts commit 8238582.

Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 committed Aug 16, 2022
commit 5e375d9092efd1e40a0ed37dfd2c208598b27bb9
35 changes: 5 additions & 30 deletions lib/private/Files/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OC\Files\Cache;

use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
Expand Down Expand Up @@ -189,7 +188,6 @@ public static function cacheEntryFromData($data, IMimeTypeLoader $mimetypeLoader
$data['fileid'] = (int)$data['fileid'];
$data['parent'] = (int)$data['parent'];
$data['size'] = 0 + $data['size'];
$data['unencrypted_size'] = 0 + ($data['unencrypted_size'] ?? 0);
$data['mtime'] = (int)$data['mtime'];
$data['storage_mtime'] = (int)$data['storage_mtime'];
$data['encryptedVersion'] = (int)$data['encrypted'];
Expand Down Expand Up @@ -430,7 +428,7 @@ public function update($id, array $data) {
protected function normalizeData(array $data): array {
$fields = [
'path', 'parent', 'name', 'mimetype', 'size', 'mtime', 'storage_mtime', 'encrypted',
'etag', 'permissions', 'checksum', 'storage', 'unencrypted_size'];
'etag', 'permissions', 'checksum', 'storage'];
$extensionFields = ['metadata_etag', 'creation_time', 'upload_time'];

$doNotCopyStorageMTime = false;
Expand Down Expand Up @@ -875,16 +873,8 @@ public function calculateFolderSize($path, $entry = null) {
$id = $entry['fileid'];

$query = $this->getQueryBuilder();
$query->selectAlias($query->func()->sum('size'), 'size_sum')
->selectAlias($query->func()->min('size'), 'size_min')
// in case of encryption being enabled after some files are already uploaded, some entries will have an unencrypted_size of 0 and a non-zero size
->selectAlias($query->func()->sum(
$query->func()->case([
['when' => $query->expr()->eq('unencrypted_size', $query->expr()->literal(0, IQueryBuilder::PARAM_INT)), 'then' => 'size'],
], 'unencrypted_size')
), 'unencrypted_sum')
->selectAlias($query->func()->min('unencrypted_size'), 'unencrypted_min')
->selectAlias($query->func()->max('unencrypted_size'), 'unencrypted_max')
$query->selectAlias($query->func()->sum('size'), 'f1')
->selectAlias($query->func()->min('size'), 'f2')
->from('filecache')
->whereStorageId($this->getNumericStorageId())
->whereParent($id);
Expand All @@ -894,31 +884,16 @@ public function calculateFolderSize($path, $entry = null) {
$result->closeCursor();

if ($row) {
['size_sum' => $sum, 'size_min' => $min, 'unencrypted_sum' => $unencryptedSum, 'unencrypted_min' => $unencryptedMin, 'unencrypted_max' => $unencryptedMax] = $row;
[$sum, $min] = array_values($row);
$sum = 0 + $sum;
$min = 0 + $min;
if ($min === -1) {
$totalSize = $min;
} else {
$totalSize = $sum;
}
if ($unencryptedMin === -1 || $min === -1) {
$unencryptedTotal = $unencryptedMin;
} else {
$unencryptedTotal = $unencryptedSum;
}
if ($entry['size'] !== $totalSize) {
// only set unencrypted size for a folder if any child entries have it set
if ($unencryptedMax > 0) {
$this->update($id, [
'size' => $totalSize,
'unencrypted_size' => $unencryptedTotal,
]);
} else {
$this->update($id, [
'size' => $totalSize,
]);
}
$this->update($id, ['size' => $totalSize]);
}
}
}
Expand Down
8 changes: 0 additions & 8 deletions lib/private/Files/Cache/CacheEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,4 @@ public function getData() {
public function __clone() {
$this->data = array_merge([], $this->data);
}

public function getUnencryptedSize(): int {
if (isset($this->data['unencrypted_size']) && $this->data['unencrypted_size'] > 0) {
return $this->data['unencrypted_size'];
} else {
return $this->data['size'];
}
}
}
15 changes: 0 additions & 15 deletions lib/private/Files/Cache/Propagator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

namespace OC\Files\Cache;

use OC\Files\Storage\Wrapper\Encryption;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\Cache\IPropagator;
use OCP\Files\Storage\IReliableEtagStorage;
Expand Down Expand Up @@ -114,20 +113,6 @@ public function propagateChange($internalPath, $time, $sizeDifference = 0) {
->andWhere($builder->expr()->in('path_hash', $hashParams))
->andWhere($builder->expr()->gt('size', $builder->expr()->literal(-1, IQueryBuilder::PARAM_INT)));

if ($this->storage->instanceOfStorage(Encryption::class)) {
// in case of encryption being enabled after some files are already uploaded, some entries will have an unencrypted_size of 0 and a non-zero size
$builder->set('unencrypted_size', $builder->func()->greatest(
$builder->func()->add(
$builder->func()->case([
['when' => $builder->expr()->eq('unencrypted_size', $builder->expr()->literal(0, IQueryBuilder::PARAM_INT)), 'then' => 'size']
], 'unencrypted_size'),
$builder->createNamedParameter($sizeDifference)
),
$builder->createNamedParameter(-1, IQueryBuilder::PARAM_INT)
));
}

$a = $builder->getSQL();
$builder->execute();
}
}
Expand Down
21 changes: 2 additions & 19 deletions lib/private/Files/FileInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,7 @@ public function getEtag() {
public function getSize($includeMounts = true) {
if ($includeMounts) {
$this->updateEntryfromSubMounts();

if (isset($this->data['unencrypted_size']) && $this->data['unencrypted_size'] > 0) {
return $this->data['unencrypted_size'];
} else {
return isset($this->data['size']) ? 0 + $this->data['size'] : 0;
}
return isset($this->data['size']) ? 0 + $this->data['size'] : 0;
} else {
return $this->rawSize;
}
Expand Down Expand Up @@ -395,19 +390,7 @@ private function updateEntryfromSubMounts() {
* @param string $entryPath full path of the child entry
*/
public function addSubEntry($data, $entryPath) {
if (!$data) {
return;
}
$hasUnencryptedSize = isset($data['unencrypted_size']) && $data['unencrypted_size'] > 0;
if ($hasUnencryptedSize) {
$subSize = $data['unencrypted_size'];
} else {
$subSize = $data['size'] ?: 0;
}
$this->data['size'] += $subSize;
if ($hasUnencryptedSize) {
$this->data['unencrypted_size'] += $subSize;
}
$this->data['size'] += isset($data['size']) ? $data['size'] : 0;
if (isset($data['mtime'])) {
$this->data['mtime'] = max($this->data['mtime'], $data['mtime']);
}
Expand Down
98 changes: 40 additions & 58 deletions lib/private/Files/Storage/Wrapper/Encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OC\Files\Storage\Wrapper;

use OC\Encryption\Exceptions\ModuleDoesNotExistsException;
Expand All @@ -42,7 +41,6 @@
use OC\Files\Cache\CacheEntry;
use OC\Files\Filesystem;
use OC\Files\Mount\Manager;
use OC\Files\ObjectStore\ObjectStoreStorage;
use OC\Files\Storage\LocalTempFileTrait;
use OC\Memcache\ArrayCache;
use OCP\Encryption\Exceptions\GenericEncryptionException;
Expand Down Expand Up @@ -141,36 +139,28 @@ public function filesize($path) {
$size = $this->unencryptedSize[$fullPath];
// update file cache
if ($info instanceof ICacheEntry) {
$info = $info->getData();
$info['encrypted'] = $info['encryptedVersion'];
} else {
if (!is_array($info)) {
$info = [];
}
$info['encrypted'] = true;
$info = new CacheEntry($info);
}

if ($size !== $info->getUnencryptedSize()) {
$this->getCache()->update($info->getId(), [
'unencrypted_size' => $size
]);
}
$info['size'] = $size;
$this->getCache()->put($path, $info);

return $size;
}

if (isset($info['fileid']) && $info['encrypted']) {
return $this->verifyUnencryptedSize($path, $info->getUnencryptedSize());
return $this->verifyUnencryptedSize($path, $info['size']);
}

return $this->storage->filesize($path);
}

/**
* @param string $path
* @param array $data
* @return array
*/
private function modifyMetaData(string $path, array $data): array {
$fullPath = $this->getFullPath($path);
$info = $this->getCache()->get($path);
Expand All @@ -180,7 +170,7 @@ private function modifyMetaData(string $path, array $data): array {
$data['size'] = $this->unencryptedSize[$fullPath];
} else {
if (isset($info['fileid']) && $info['encrypted']) {
$data['size'] = $this->verifyUnencryptedSize($path, $info->getUnencryptedSize());
$data['size'] = $this->verifyUnencryptedSize($path, $info['size']);
$data['encrypted'] = true;
}
}
Expand Down Expand Up @@ -488,7 +478,7 @@ public function fopen($path, $mode) {
*
* @return int unencrypted size
*/
protected function verifyUnencryptedSize(string $path, int $unencryptedSize): int {
protected function verifyUnencryptedSize($path, $unencryptedSize) {
$size = $this->storage->filesize($path);
$result = $unencryptedSize;

Expand Down Expand Up @@ -520,7 +510,7 @@ protected function verifyUnencryptedSize(string $path, int $unencryptedSize): in
*
* @return int calculated unencrypted size
*/
protected function fixUnencryptedSize(string $path, int $size, int $unencryptedSize): int {
protected function fixUnencryptedSize($path, $size, $unencryptedSize) {
$headerSize = $this->getHeaderSize($path);
$header = $this->getHeader($path);
$encryptionModule = $this->getEncryptionModule($path);
Expand Down Expand Up @@ -591,9 +581,7 @@ protected function fixUnencryptedSize(string $path, int $size, int $unencryptedS
$cache = $this->storage->getCache();
if ($cache) {
$entry = $cache->get($path);
$cache->update($entry['fileid'], [
'unencrypted_size' => $newUnencryptedSize
]);
$cache->update($entry['fileid'], ['size' => $newUnencryptedSize]);
}

return $newUnencryptedSize;
Expand Down Expand Up @@ -633,12 +621,7 @@ private function fread_block($handle, int $blockSize): string {
* @param bool $preserveMtime
* @return bool
*/
public function moveFromStorage(
Storage\IStorage $sourceStorage,
$sourceInternalPath,
$targetInternalPath,
$preserveMtime = true
) {
public function moveFromStorage(Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = true) {
if ($sourceStorage === $this) {
return $this->rename($sourceInternalPath, $targetInternalPath);
}
Expand Down Expand Up @@ -673,13 +656,7 @@ public function moveFromStorage(
* @param bool $isRename
* @return bool
*/
public function copyFromStorage(
Storage\IStorage $sourceStorage,
$sourceInternalPath,
$targetInternalPath,
$preserveMtime = false,
$isRename = false
) {
public function copyFromStorage(Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false, $isRename = false) {

// TODO clean this up once the underlying moveFromStorage in OC\Files\Storage\Wrapper\Common is fixed:
// - call $this->storage->copyFromStorage() instead of $this->copyBetweenStorage
Expand All @@ -699,13 +676,7 @@ public function copyFromStorage(
* @param bool $isRename
* @param bool $keepEncryptionVersion
*/
private function updateEncryptedVersion(
Storage\IStorage $sourceStorage,
$sourceInternalPath,
$targetInternalPath,
$isRename,
$keepEncryptionVersion
) {
private function updateEncryptedVersion(Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename, $keepEncryptionVersion) {
$isEncrypted = $this->encryptionManager->isEnabled() && $this->shouldEncrypt($targetInternalPath);
$cacheInformation = [
'encrypted' => $isEncrypted,
Expand Down Expand Up @@ -754,13 +725,7 @@ private function updateEncryptedVersion(
* @return bool
* @throws \Exception
*/
private function copyBetweenStorage(
Storage\IStorage $sourceStorage,
$sourceInternalPath,
$targetInternalPath,
$preserveMtime,
$isRename
) {
private function copyBetweenStorage(Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime, $isRename) {

// for versions we have nothing to do, because versions should always use the
// key from the original file. Just create a 1:1 copy and done
Expand All @@ -778,7 +743,7 @@ private function copyBetweenStorage(
if (isset($info['encrypted']) && $info['encrypted'] === true) {
$this->updateUnencryptedSize(
$this->getFullPath($targetInternalPath),
$info->getUnencryptedSize()
$info['size']
);
}
$this->updateEncryptedVersion($sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename, true);
Expand Down Expand Up @@ -843,6 +808,13 @@ private function copyBetweenStorage(
return (bool)$result;
}

/**
* get the path to a local version of the file.
* The local version of the file can be temporary and doesn't have to be persistent across requests
*
* @param string $path
* @return string
*/
public function getLocalFile($path) {
if ($this->encryptionManager->isEnabled()) {
$cachedFile = $this->getCachedFile($path);
Expand All @@ -853,25 +825,42 @@ public function getLocalFile($path) {
return $this->storage->getLocalFile($path);
}

/**
* Returns the wrapped storage's value for isLocal()
*
* @return bool wrapped storage's isLocal() value
*/
public function isLocal() {
if ($this->encryptionManager->isEnabled()) {
return false;
}
return $this->storage->isLocal();
}

/**
* see https://www.php.net/manual/en/function.stat.php
* only the following keys are required in the result: size and mtime
*
* @param string $path
* @return array
*/
public function stat($path) {
$stat = $this->storage->stat($path);
if (!$stat) {
return false;
}
$fileSize = $this->filesize($path);
$stat['size'] = $fileSize;
$stat[7] = $fileSize;
$stat['hasHeader'] = $this->getHeaderSize($path) > 0;
return $stat;
}

/**
* see https://www.php.net/manual/en/function.hash.php
*
* @param string $type
* @param string $path
* @param bool $raw
* @return string
*/
public function hash($type, $path, $raw = false) {
$fh = $this->fopen($path, 'rb');
$ctx = hash_init($type);
Expand Down Expand Up @@ -1079,13 +1068,6 @@ public function writeStream(string $path, $stream, int $size = null): int {
[$count, $result] = \OC_Helper::streamCopy($stream, $target);
fclose($stream);
fclose($target);

// object store, stores the size after write and doesn't update this during scan
// manually store the unencrypted size
if ($result && $this->getWrapperStorage()->instanceOfStorage(ObjectStoreStorage::class)) {
$this->getCache()->put($path, ['unencrypted_size' => $count]);
}

return $count;
}
}
Loading