Skip to content

Commit

Permalink
clear encrypted flag when moving away from encrypted storage
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 committed Feb 15, 2023
1 parent 54954cc commit b4c810e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/private/Files/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OC\Files\Search\SearchComparison;
use OC\Files\Search\SearchQuery;
use OC\Files\Storage\Wrapper\Encryption;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Cache\CacheEntryInsertedEvent;
Expand Down Expand Up @@ -641,6 +642,10 @@ protected function getMoveInfo($path) {
return [$this->getNumericStorageId(), $path];
}

protected function hasEncryptionWrapper(): bool {
return $this->storage->instanceOfStorage(Encryption::class);
}

/**
* Move a file or folder in the cache
*
Expand Down Expand Up @@ -692,6 +697,11 @@ public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
->where($query->expr()->eq('storage', $query->createNamedParameter($sourceStorageId, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->like('path', $query->createNamedParameter($this->connection->escapeLikeParameter($sourcePath) . '/%')));

// when moving from an encrypted storage to a non-encrypted storage remove the `encrypted` mark
if ($sourceCache->hasEncryptionWrapper() && !$this->hasEncryptionWrapper()) {
$query->set('encrypted', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT));
}

try {
$query->execute();
} catch (\OC\DatabaseException $e) {
Expand All @@ -708,6 +718,12 @@ public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
->set('name', $query->createNamedParameter(basename($targetPath)))
->set('parent', $query->createNamedParameter($newParentId, IQueryBuilder::PARAM_INT))
->whereFileId($sourceId);

// when moving from an encrypted storage to a non-encrypted storage remove the `encrypted` mark
if ($sourceCache->hasEncryptionWrapper() && !$this->hasEncryptionWrapper()) {
$query->set('encrypted', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT));
}

$query->execute();

$this->connection->commit();
Expand Down Expand Up @@ -1067,6 +1083,12 @@ public function copyFromCache(ICache $sourceCache, ICacheEntry $sourceEntry, str
throw new \RuntimeException("Invalid source cache entry on copyFromCache");
}
$data = $this->cacheEntryToArray($sourceEntry);

// when moving from an encrypted storage to a non-encrypted storage remove the `encrypted` mark
if ($sourceCache instanceof Cache && $sourceCache->hasEncryptionWrapper() && !$this->hasEncryptionWrapper()) {
$data['encrypted'] = 0;
}

$fileId = $this->put($targetPath, $data);
if ($fileId <= 0) {
throw new \RuntimeException("Failed to copy to " . $targetPath . " from cache with source data " . json_encode($data) . " ");
Expand Down
9 changes: 9 additions & 0 deletions lib/private/Files/Cache/Wrapper/CacheWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ protected function getCache() {
return $this->cache;
}

protected function hasEncryptionWrapper(): bool {
$cache = $this->getCache();
if ($cache instanceof Cache) {
return $cache->hasEncryptionWrapper();
} else {
return false;
}
}

/**
* Make it easy for wrappers to modify every returned cache entry
*
Expand Down

0 comments on commit b4c810e

Please sign in to comment.