Skip to content

Commit 716c47c

Browse files
refactor(View): getFileInfo cleanup (typing, logic clarity, docs)
Signed-off-by: Josh <josh.t.richards@gmail.com>
1 parent 66000c1 commit 716c47c

File tree

1 file changed

+66
-58
lines changed

1 file changed

+66
-58
lines changed

lib/private/Files/View.php

Lines changed: 66 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,31 +1357,35 @@ private function getUserObjectForOwner(string $ownerId): IUser {
13571357
}
13581358

13591359
/**
1360-
* Get file info from cache
1360+
* Returns cache metadata from the given storage for specified path.
1361+
* Scans and updates cache if needed (i.e. not in cache or file has changed). If file is locked,
1362+
* returns prior (outdated) cache entry.
13611363
*
1362-
* If the file is not in cached it will be scanned
1363-
* If the file has changed on storage the cache will be updated
1364-
*
1365-
* @param Storage $storage
1366-
* @param string $internalPath
1367-
* @param string $relativePath
1368-
* @return ICacheEntry|bool
1364+
* @param Storage $storage Storage backend for the file
1365+
* @param string $internalPath Path to the file within the storage
1366+
* @param string $relativePath Relative path used for locking and reference
1367+
* @return ICacheEntry|false Metadata object or false if file not found
13691368
*/
1370-
private function getCacheEntry($storage, $internalPath, $relativePath) {
1369+
private function getCacheEntry(Storage $storage, string $internalPath, string $relativePath): ICacheEntry|false {
13711370
$cache = $storage->getCache($internalPath);
1371+
13721372
$data = $cache->get($internalPath);
13731373
$watcher = $storage->getWatcher($internalPath);
13741374

13751375
try {
1376-
// if the file is not in the cache or needs to be updated, trigger the scanner and reload the data
1376+
// If the file is not in the cache or has a placeholder "size == -1", check underlying storage and rescan if it exists.
13771377
if (!$data || (isset($data['size']) && $data['size'] === -1)) {
13781378
if (!$storage->file_exists($internalPath)) {
13791379
return false;
13801380
}
1381-
// don't need to get a lock here since the scanner does it's own locking
1381+
// scanner does it's own locking
13821382
$scanner = $storage->getScanner($internalPath);
13831383
$scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW);
13841384
$data = $cache->get($internalPath);
1385+
if (!data) {
1386+
return false;
1387+
}
1388+
// If watcher says cache is out of date (and not a partial file), lock/update/re-check cache
13851389
} elseif (!Cache\Scanner::isPartialFile($internalPath) && $watcher->needsUpdate($internalPath, $data)) {
13861390
$this->lockFile($relativePath, ILockingProvider::LOCK_SHARED);
13871391
$watcher->update($internalPath, $data);
@@ -1390,81 +1394,85 @@ private function getCacheEntry($storage, $internalPath, $relativePath) {
13901394
$this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED);
13911395
}
13921396
} catch (LockedException $e) {
1393-
// if the file is locked we just use the old cache info
1397+
// Use the old cache info if locked
13941398
}
1395-
1396-
return $data;
1399+
return $data ?: false;
13971400
}
13981401

13991402
/**
1400-
* get the filesystem info
1403+
* Looks up and returns the file or folder metadata as a FileInfo object for the given path.
14011404
*
1402-
* @param string $path
1403-
* @param bool|string $includeMountPoints true to add mountpoint sizes,
1404-
* 'ext' to add only ext storage mount point sizes. Defaults to true.
1405-
* @return \OC\Files\FileInfo|false False if file does not exist
1405+
* @param string $path Relative path to file or directory
1406+
* @param bool|string $includeMountPoints true to add mount sizes,
1407+
* 'ext' for external mounts only.
1408+
* @return \OC\Files\FileInfo|false FileInfo object or false not found
14061409
*/
1407-
public function getFileInfo($path, $includeMountPoints = true) {
1410+
public function getFileInfo(string $path, bool|string $includeMountPoints = true): \OC\Files\FileInfo|false {
14081411
$this->assertPathLength($path);
14091412
if (!Filesystem::isValidPath($path)) {
14101413
return false;
14111414
}
1415+
14121416
$relativePath = $path;
14131417
$path = Filesystem::normalizePath($this->fakeRoot . '/' . $path);
14141418

14151419
$mount = Filesystem::getMountManager()->find($path);
14161420
$storage = $mount->getStorage();
1421+
if (!$storage) {
1422+
$this->logger->warning('Storage not valid for mountpoint: ' . $mount->getMountPoint(), ['app' => 'core']);
1423+
return false;
1424+
}
1425+
14171426
$internalPath = $mount->getInternalPath($path);
1418-
if ($storage) {
1419-
$data = $this->getCacheEntry($storage, $internalPath, $relativePath);
1420-
1421-
if (!$data instanceof ICacheEntry) {
1422-
if (Cache\Scanner::isPartialFile($relativePath)) {
1423-
return $this->getPartFileInfo($relativePath);
1424-
}
1427+
$data = $this->getCacheEntry($storage, $internalPath, $relativePath);
14251428

1426-
return false;
1429+
if (!$data instanceof ICacheEntry) {
1430+
if (Cache\Scanner::isPartialFile($relativePath)) {
1431+
return $this->getPartFileInfo($relativePath);
14271432
}
1433+
return false;
1434+
}
14281435

1429-
if ($mount instanceof MoveableMount && $internalPath === '') {
1430-
$data['permissions'] |= \OCP\Constants::PERMISSION_DELETE;
1431-
}
1432-
if ($internalPath === '' && $data['name']) {
1433-
$data['name'] = basename($path);
1434-
}
1436+
if ($mount instanceof MoveableMount && $internalPath === '') {
1437+
$data['permissions'] |= \OCP\Constants::PERMISSION_DELETE;
1438+
}
1439+
if ($internalPath === '' && isset($data['name'])) {
1440+
$data['name'] = basename($path);
1441+
}
14351442

1436-
$ownerId = $storage->getOwner($internalPath);
1437-
$owner = null;
1438-
if ($ownerId !== false) {
1439-
// ownerId might be null if files are accessed with an access token without file system access
1440-
$owner = $this->getUserObjectForOwner($ownerId);
1441-
}
1442-
$info = new FileInfo($path, $storage, $internalPath, $data, $mount, $owner);
1443+
$ownerId = $storage->getOwner($internalPath);
1444+
// ownerId might be null if files are accessed with an access token without file system access
1445+
$owner = ($ownerId !== false && $ownerId !== null) ? $this->getUserObjectForOwner($ownerId) : null;
14431446

1444-
if (isset($data['fileid'])) {
1445-
if ($includeMountPoints && $data['mimetype'] === 'httpd/unix-directory') {
1446-
//add the sizes of other mount points to the folder
1447-
$extOnly = ($includeMountPoints === 'ext');
1448-
$this->addSubMounts($info, $extOnly);
1449-
}
1450-
}
1447+
$info = new FileInfo($path, $storage, $internalPath, $data, $mount, $owner);
14511448

1452-
return $info;
1453-
} else {
1454-
$this->logger->warning('Storage not valid for mountpoint: ' . $mount->getMountPoint(), ['app' => 'core']);
1449+
if (isset($data['fileid']) && $includeMountPoints && $data['mimetype'] === 'httpd/unix-directory') {
1450+
// add the sizes of other mount points to the folder
1451+
$extOnly = ($includeMountPoints === 'ext');
1452+
$this->addSubMounts($info, $extOnly);
14551453
}
14561454

1457-
return false;
1455+
return $info;
14581456
}
14591457

14601458
/**
1461-
* Extend a FileInfo that was previously requested with `$includeMountPoints = false` to include the sub mounts
1459+
* Assigns sub-mount points found within the given FileInfo's path.
1460+
*
1461+
* e.g., Extend a FileInfo that was previously requested with `$includeMountPoints = false`
1462+
* to include the sub mounts.
1463+
*
1464+
* @param FileInfo $info FileInfo for which to find and assign sub-mounts
1465+
* @param bool $extOnly Include only non-shared (external) mounts (defaults to false)
14621466
*/
1463-
public function addSubMounts(FileInfo $info, $extOnly = false): void {
1464-
$mounts = Filesystem::getMountManager()->findIn($info->getPath());
1465-
$info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) {
1466-
return !($extOnly && $mount instanceof SharedMount);
1467-
}));
1467+
public function addSubMounts(FileInfo $info, bool $extOnly = false): void {
1468+
$path = $info->getPath();
1469+
$mounts = Filesystem::getMountManager()->findIn($path);
1470+
$info->setSubMounts(array_filter(
1471+
$mounts,
1472+
function (IMountPoint $mount) use ($extOnly) {
1473+
return !($extOnly && $mount instanceof SharedMount);
1474+
}
1475+
));
14681476
}
14691477

14701478
/**

0 commit comments

Comments
 (0)