@ -83,7 +83,7 @@ class Scanner extends BasicEmitter implements IScanner {
*
* @param bool $useTransactions
*/
public function setUseTransactions($useTransactions) {
public function setUseTransactions($useTransactions): void {
$this->useTransactions = $useTransactions;
}
@ -108,9 +108,9 @@ class Scanner extends BasicEmitter implements IScanner {
* @param string $file
* @param int $reuseExisting
* @param int $parentId
* @param array|null|false $cacheData existing data in the cache for the file to be scanned
* @param array|CacheEntry| null|false $cacheData existing data in the cache for the file to be scanned
* @param bool $lock set to false to disable getting an additional read lock during scanning
* @param null $data the metadata for the file, as returned by the storage
* @param array| null $data the metadata for the file, as returned by the storage
* @return array|null an array of metadata of the scanned file
* @throws \OCP\Lock\LockedException
*/
@ -122,8 +122,12 @@ class Scanner extends BasicEmitter implements IScanner {
return null;
}
}
// only proceed if $file is not a partial file, blacklist is handled by the storage
if (!self::isPartialFile($file)) {
if (self::isPartialFile($file)) {
return null;
}
// acquire a lock
if ($lock) {
if ($this->storage->instanceOfStorage(ILockingStorage::class)) {
@ -144,7 +148,9 @@ class Scanner extends BasicEmitter implements IScanner {
}
try {
if ($data) {
if ($data === null) {
$this->removeFromCache($file);
} else {
// pre-emit only if it was a file. By that we avoid counting/treating folders as files
if ($data['mimetype'] !== 'httpd/unix-directory') {
$this->emit('\OC\Files\Cache\Scanner', 'scanFile', [$file, $this->storageId]);
@ -162,19 +168,18 @@ class Scanner extends BasicEmitter implements IScanner {
// scan the parent if it's not in the cache (id -1) and the current file is not the root folder
if ($file & & $parentId === -1) {
$parentData = $this->scanFile($parent);
if (! $parentData) {
if ($parentData === null ) {
return null;
}
$parentId = $parentData['fileid'];
}
if ($parent) {
$data['parent'] = $parentId;
}
if (is_null($cacheData)) {
/** @var CacheEntry $cacheData */
$cacheData = $this->cache->get($file);
}
if ($cacheData & & $reuseExisting & & isset($cacheData['fileid'])) {
$cacheData = $cacheData ?? $this->cache->get($file);
if ($reuseExisting & & $cacheData !== false & & isset($cacheData['fileid'])) {
// prevent empty etag
$etag = empty($cacheData['etag']) ? $data['etag'] : $cacheData['etag'];
$fileId = $cacheData['fileid'];
@ -193,12 +198,17 @@ class Scanner extends BasicEmitter implements IScanner {
}
// we only updated unencrypted_size if it's already set
if ($cacheData['unencrypted_size'] === 0) {
if (isset($cacheData['unencrypted_size']) & & $cacheData['unencrypted_size'] === 0) {
unset($data['unencrypted_size']);
}
// Only update metadata that has changed
// i.e. get all the values in $data that are not present in the cache already
/**
* Only update metadata that has changed.
* i.e. get all the values in $data that are not present in the cache already
*
* We need the OC implementation for usage of "getData" method below.
* @var \OC\Files\Cache\CacheEntry $cacheData
*/
$newData = $this->array_diff_assoc_multi($data, $cacheData->getData());
// make it known to the caller that etag has been changed and needs propagation
@ -206,7 +216,6 @@ class Scanner extends BasicEmitter implements IScanner {
$data['etag_changed'] = true;
}
} else {
// we only updated unencrypted_size if it's already set
unset($data['unencrypted_size']);
$newData = $data;
$fileId = -1;
@ -218,10 +227,9 @@ class Scanner extends BasicEmitter implements IScanner {
$data['fileid'] = $this->addToCache($file, $newData, $fileId);
}
$data['oldSize'] = ($cacheData & & isset($cacheData['size'])) ? $cacheData['size'] : 0;
if ($cacheData & & isset($cacheData['encrypted'])) {
$data['encrypted'] = $cacheData['encrypted'];
if ($cacheData !== false) {
$data['oldSize'] = $cacheData['size'] ?? 0;
$data['encrypted'] = $cacheData['encrypted'] ?? false;
}
// post-emit only if it was a file. By that we avoid counting/treating folders as files
@ -229,34 +237,17 @@ class Scanner extends BasicEmitter implements IScanner {
$this->emit('\OC\Files\Cache\Scanner', 'postScanFile', [$file, $this->storageId]);
\OC_Hook::emit('\OC\Files\Cache\Scanner', 'post_scan_file', ['path' => $file, 'storage' => $this->storageId]);
}
} else {
$this->removeFromCache($file);
}
} catch (\Exception $e) {
if ($lock) {
if ($this->storage->instanceOfStorage(ILockingStorage::class)) {
$this->storage->releaseLock($file, ILockingProvider::LOCK_SHARED, $this->lockingProvider);
}
}
throw $e;
}
} finally {
// release the acquired lock
if ($lock) {
if ($this->storage->instanceOfStorage(ILockingStorage::class)) {
if ($lock & & $this->storage->instanceOfStorage(ILockingStorage::class)) {
$this->storage->releaseLock($file, ILockingProvider::LOCK_SHARED, $this->lockingProvider);
}
}
if ($data & & !isset($data['encrypted'])) {
$data['encrypted'] = false;
}
return $data;
}
return null;
}
protected function removeFromCache($path) {
\OC_Hook::emit('Scanner', 'removeFromCache', ['file' => $path]);
$this->emit('\OC\Files\Cache\Scanner', 'removeFromCache', [$path]);
@ -319,31 +310,28 @@ class Scanner extends BasicEmitter implements IScanner {
if ($reuse === -1) {
$reuse = ($recursive === self::SCAN_SHALLOW) ? self::REUSE_ETAG | self::REUSE_SIZE : self::REUSE_ETAG;
}
if ($lock) {
if ($this->storage->instanceOfStorage(ILockingStorage::class)) {
if ($lock & & $this->storage->instanceOfStorage(ILockingStorage::class)) {
$this->storage->acquireLock('scanner::' . $path, ILockingProvider::LOCK_EXCLUSIVE, $this->lockingProvider);
$this->storage->acquireLock($path, ILockingProvider::LOCK_SHARED, $this->lockingProvider);
}
}
try {
try {
$data = $this->scanFile($path, $reuse, -1, null, $lock);
if ($data & & $data['mimetype'] === 'httpd/unix-directory') {
$data = $this->scanFile($path, $reuse, -1, lock: $lock);
if ($data !== null & & $data['mimetype'] === 'httpd/unix-directory') {
$size = $this->scanChildren($path, $recursive, $reuse, $data['fileid'], $lock, $data['size']);
$data['size'] = $size;
}
} catch (NotFoundException $e) {
$this->removeFromCache($path);
return null;
}
} finally {
if ($lock) {
if ($this->storage->instanceOfStorage(ILockingStorage::class)) {
if ($lock & & $this->storage->instanceOfStorage(ILockingStorage::class)) {
$this->storage->releaseLock($path, ILockingProvider::LOCK_SHARED, $this->lockingProvider);
$this->storage->releaseLock('scanner::' . $path, ILockingProvider::LOCK_EXCLUSIVE, $this->lockingProvider);
}
}
}
return $data;
}
@ -395,9 +383,9 @@ class Scanner extends BasicEmitter implements IScanner {
* Get the children currently in the cache
*
* @param int $folderId
* @return array[]
* @return array< string , \ OCP \ Files \ Cache \ ICacheEntry >
*/
protected function getExistingChildren($folderId) {
protected function getExistingChildren($folderId): array {
$existingChildren = [];
$children = $this->cache->getFolderContentsById($folderId);
foreach ($children as $child) {