namespace OC\Files\Cache;
use OC\User\NoUserException;
+use OCP\Files\Cache\ICacheEntry;
use OCP\Share_Backend_Collection;
/**
/**
* get the stored metadata of a file or folder
*
- * @param string $file
- * @return array|false
+ * @param string|int $file
+ * @return ICacheEntry|false
*/
public function get($file) {
$mimetypeLoader = \OC::$server->getMimeTypeLoader();
* get the metadata of all files stored in $folder
*
* @param string $folderId
- * @return array|false
+ * @return ICacheEntry[]|false
*/
public function getFolderContentsById($folderId) {
$cache = $this->getSourceCache('');
* search for files matching $pattern
*
* @param string $pattern
- * @return array of file data
+ * @return ICacheEntry[] of file data
*/
public function search($pattern) {
* search for files by mimetype
*
* @param string $mimetype
- * @return array
+ * @return ICacheEntry[]
*/
public function searchByMime($mimetype) {
$mimepart = null;
*
* @param string|int $tag tag to search for
* @param string $userId owner of the tags
- * @return array file data
+ * @return ICacheEntry[] file data
*/
public function searchByTag($tag, $userId) {
// TODO: inject this
namespace OC\Files\Cache;
use OCP\Files\Cache\ICache;
+use OCP\Files\Cache\ICacheEntry;
use \OCP\Files\IMimeTypeLoader;
use OCP\IDBConnection;
* - ChangePropagator: updates the mtime and etags of parent folders whenever a change to the cache is made to the cache by the updater
*/
class Cache implements ICache {
- const NOT_FOUND = 0;
- const PARTIAL = 1; //only partial data available, file not cached in the database
- const SHALLOW = 2; //folder in cache, but not all child files are completely scanned
- const COMPLETE = 3;
-
/**
* @var array partial data for the cache
*/
/**
* get the stored metadata of a file or folder
*
- * the returned cache entry contains at least the following values:
- * [
- * 'fileid' => int, the numeric id of a file (see getId)
- * 'storage' => int, the numeric id of the storage the file is stored on
- * 'path' => string, the path of the file within the storage ('foo/bar.txt')
- * 'name' => string, the basename of a file ('bar.txt)
- * 'mimetype' => string, the full mimetype of the file ('text/plain')
- * 'mimepart' => string, the first half of the mimetype ('text')
- * 'size' => int, the size of the file or folder in bytes
- * 'mtime' => int, the last modified date of the file as unix timestamp as shown in the ui
- * 'storage_mtime' => int, the last modified date of the file as unix timestamp as stored on the storage
- * Note that when a file is updated we also update the mtime of all parent folders to make it visible to the user which folder has had updates most recently
- * This can differ from the mtime on the underlying storage which usually only changes when a direct child is added, removed or renamed
- * 'etag' => string, the etag for the file
- * An etag is used for change detection of files and folders, an etag of a file changes whenever the content of the file changes
- * Etag for folders change whenever a file in the folder has changed
- * 'permissions' int, the permissions for the file stored as bitwise combination of \OCP\PERMISSION_READ, \OCP\PERMISSION_CREATE
- * \OCP\PERMISSION_UPDATE, \OCP\PERMISSION_DELETE and \OCP\PERMISSION_SHARE
- * ]
- *
* @param string | int $file either the path of a file or folder or the file id for a file or folder
- * @return array|false the cache entry as array of false if the file is not found in the cache
+ * @return ICacheEntry|false the cache entry as array of false if the file is not found in the cache
*/
public function get($file) {
if (is_string($file) or $file == '') {
if (isset($this->partial[$file])) {
$data = $this->partial[$file];
}
+ return $data;
} else {
//fix types
$data['fileid'] = (int)$data['fileid'];
$data['storage_mtime'] = $data['mtime'];
}
$data['permissions'] = (int)$data['permissions'];
+ return new CacheEntry($data);
}
-
- return $data;
}
/**
* get the metadata of all files stored in $folder
*
* @param string $folder
- * @return array
+ * @return ICacheEntry[]
*/
public function getFolderContents($folder) {
$fileId = $this->getId($folder);
* get the metadata of all files stored in $folder
*
* @param int $fileId the file id of the folder
- * @return array
+ * @return ICacheEntry[]
*/
public function getFolderContentsById($fileId) {
if ($fileId > -1) {
$file['storage_mtime'] = (int)$file['storage_mtime'];
$file['size'] = 0 + $file['size'];
}
- return $files;
+ return array_map(function (array $data) {
+ return new CacheEntry($data);
+ }, $files);
} else {
return array();
}
return -1;
} else {
$parent = $this->getParentPath($file);
- return (int) $this->getId($parent);
+ return (int)$this->getId($parent);
}
}
/**
* Move a file or folder in the cache
*
- * @param \OC\Files\Cache\Cache $sourceCache
+ * @param \OCP\Files\Cache\ICache $sourceCache
* @param string $sourcePath
* @param string $targetPath
* @throws \OC\DatabaseException
*/
- public function moveFromCache(Cache $sourceCache, $sourcePath, $targetPath) {
+ public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
// normalize source and target
$sourcePath = $this->normalize($sourcePath);
$targetPath = $this->normalize($targetPath);
* search for files matching $pattern
*
* @param string $pattern the search pattern using SQL search syntax (e.g. '%searchstring%')
- * @return array an array of cache entries where the name matches the search pattern
+ * @return ICacheEntry[] an array of cache entries where the name matches the search pattern
*/
public function search($pattern) {
// normalize pattern
$row['mimepart'] = $this->mimetypeLoader->getMimetypeById($row['mimepart']);
$files[] = $row;
}
- return $files;
+ return array_map(function(array $data) {
+ return new CacheEntry($data);
+ }, $files);
}
/**
*
* @param string $mimetype either a full mimetype to search ('text/plain') or only the first part of a mimetype ('image')
* where it will search for all mimetypes in the group ('image/*')
- * @return array an array of cache entries where the mimetype matches the search
+ * @return ICacheEntry[] an array of cache entries where the mimetype matches the search
*/
public function searchByMime($mimetype) {
if (strpos($mimetype, '/')) {
$row['mimepart'] = $this->mimetypeLoader->getMimetypeById($row['mimepart']);
$files[] = $row;
}
- return $files;
+ return array_map(function (array $data) {
+ return new CacheEntry($data);
+ }, $files);
}
/**
*
* @param string|int $tag name or tag id
* @param string $userId owner of the tags
- * @return array file data
+ * @return ICacheEntry[] file data
*/
public function searchByTag($tag, $userId) {
$sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, ' .
while ($row = $result->fetch()) {
$files[] = $row;
}
- return $files;
+ return array_map(function (array $data) {
+ return new CacheEntry($data);
+ }, $files);
}
/**
public function isEncrypted() {
return isset($this->data['encrypted']) && $this->data['encrypted'];
}
+
+ public function getData() {
+ return $this->data;
+ }
}
namespace OC\Files\Cache;
+use OCP\Files\Cache\ICacheEntry;
+
class HomeCache extends Cache {
/**
* get the size of a folder and set it in the cache
/**
* @param string $path
- * @return array
+ * @return ICacheEntry
*/
public function get($path) {
$data = parent::get($path);
$data['parent'] = $parentId;
}
if (is_null($cacheData)) {
+ /** @var CacheEntry $cacheData */
$cacheData = $this->cache->get($file);
}
if ($cacheData and $reuseExisting and isset($cacheData['fileid'])) {
}
}
// Only update metadata that has changed
- $newData = array_diff_assoc($data, $cacheData);
+ $newData = array_diff_assoc($data, $cacheData->getData());
} else {
$newData = $data;
$fileId = -1;
namespace OC\Files\Cache\Wrapper;
use OC\Files\Cache\Cache;
+use OCP\Files\Cache\ICacheEntry;
class CacheWrapper extends Cache {
/**
/**
* Make it easy for wrappers to modify every returned cache entry
*
- * @param array $entry
- * @return array
+ * @param ICacheEntry $entry
+ * @return ICacheEntry
*/
protected function formatCacheEntry($entry) {
return $entry;
* get the stored metadata of a file or folder
*
* @param string /int $file
- * @return array|false
+ * @return ICacheEntry|false
*/
public function get($file) {
$result = $this->cache->get($file);
* get the metadata of all files stored in $folder
*
* @param string $folder
- * @return array
+ * @return ICacheEntry[]
*/
public function getFolderContents($folder) {
// cant do a simple $this->cache->.... call here since getFolderContentsById needs to be called on this
* search for files matching $pattern
*
* @param string $pattern
- * @return array an array of file data
+ * @return ICacheEntry[] an array of file data
*/
public function search($pattern) {
$results = $this->cache->search($pattern);
* search for files by mimetype
*
* @param string $mimetype
- * @return array
+ * @return ICacheEntry[]
*/
public function searchByMime($mimetype) {
$results = $this->cache->searchByMime($mimetype);
*
* @param string|int $tag name or tag id
* @param string $userId owner of the tags
- * @return array file data
+ * @return ICacheEntry[] file data
*/
public function searchByTag($tag, $userId) {
$results = $this->cache->searchByTag($tag, $userId);
namespace OC\Files;
+use OCP\Files\Cache\ICacheEntry;
use OCP\IUser;
class FileInfo implements \OCP\Files\FileInfo, \ArrayAccess {
* @param string|boolean $path
* @param Storage\Storage $storage
* @param string $internalPath
- * @param array $data
+ * @param array|ICacheEntry $data
* @param \OCP\Files\Mount\IMountPoint $mount
* @param \OCP\IUser|null $owner
*/
use OC\Encryption\Exceptions\ModuleDoesNotExistsException;
use OC\Encryption\Update;
use OC\Encryption\Util;
+use OC\Files\Cache\CacheEntry;
use OC\Files\Filesystem;
use OC\Files\Mount\Manager;
use OC\Files\Storage\LocalTempFileTrait;
public function filesize($path) {
$fullPath = $this->getFullPath($path);
+ /** @var CacheEntry $info */
$info = $this->getCache()->get($path);
if (isset($this->unencryptedSize[$fullPath])) {
$size = $this->unencryptedSize[$fullPath];
// update file cache
$info['encrypted'] = true;
$info['size'] = $size;
- $this->getCache()->put($path, $info);
+ $this->getCache()->put($path, $info->getData());
return $size;
}
use OC\Files\Mount\MoveableMount;
use OC\Files\Storage\Storage;
use OC\User\User;
+use OCP\Files\Cache\ICacheEntry;
use OCP\Files\FileNameTooLongException;
use OCP\Files\InvalidCharacterInPathException;
use OCP\Files\InvalidPathException;
if ($storage) {
$data = $this->getCacheEntry($storage, $internalPath, $relativePath);
- if (!is_array($data)) {
+ if (!$data instanceof ICacheEntry) {
return false;
}
$data = $this->getCacheEntry($storage, $internalPath, $directory);
- if (!is_array($data) || !isset($data['fileid'])) {
+ if (!$data instanceof ICacheEntry || !isset($data['fileid'])) {
return [];
}
/**
* @var \OC\Files\FileInfo[] $files
*/
- $files = array_map(function (array $content) use ($path, $storage, $mount, $sharingDisabled) {
+ $files = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) {
if ($sharingDisabled) {
$content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE;
}
/**
* get the stored metadata of a file or folder
*
- * the returned cache entry contains at least the following values:
- * [
- * 'fileid' => int, the numeric id of a file (see getId)
- * 'storage' => int, the numeric id of the storage the file is stored on
- * 'path' => string, the path of the file within the storage ('foo/bar.txt')
- * 'name' => string, the basename of a file ('bar.txt)
- * 'mimetype' => string, the full mimetype of the file ('text/plain')
- * 'mimepart' => string, the first half of the mimetype ('text')
- * 'size' => int, the size of the file or folder in bytes
- * 'mtime' => int, the last modified date of the file as unix timestamp as shown in the ui
- * 'storage_mtime' => int, the last modified date of the file as unix timestamp as stored on the storage
- * Note that when a file is updated we also update the mtime of all parent folders to make it visible to the user which folder has had updates most recently
- * This can differ from the mtime on the underlying storage which usually only changes when a direct child is added, removed or renamed
- * 'etag' => string, the etag for the file
- * An etag is used for change detection of files and folders, an etag of a file changes whenever the content of the file changes
- * Etag for folders change whenever a file in the folder has changed
- * 'permissions' int, the permissions for the file stored as bitwise combination of \OCP\PERMISSION_READ, \OCP\PERMISSION_CREATE
- * \OCP\PERMISSION_UPDATE, \OCP\PERMISSION_DELETE and \OCP\PERMISSION_SHARE
- * ]
- *
* @param string | int $file either the path of a file or folder or the file id for a file or folder
- * @return array|false the cache entry as array of false if the file is not found in the cache
+ * @return ICacheEntry[]|false the cache entry or false if the file is not found in the cache
*/
public function get($file);
* get the metadata of all files stored in $folder
*
* @param string $folder
- * @return array
+ * @return ICacheEntry[]
*/
public function getFolderContents($folder);
* get the metadata of all files stored in $folder
*
* @param int $fileId the file id of the folder
- * @return array
+ * @return ICacheEntry[]
*/
public function getFolderContentsById($fileId);
* search for files matching $pattern
*
* @param string $pattern the search pattern using SQL search syntax (e.g. '%searchstring%')
- * @return array an array of cache entries where the name matches the search pattern
+ * @return ICacheEntry[] an array of cache entries where the name matches the search pattern
*/
public function search($pattern);
*
* @param string $mimetype either a full mimetype to search ('text/plain') or only the first part of a mimetype ('image')
* where it will search for all mimetypes in the group ('image/*')
- * @return array an array of cache entries where the mimetype matches the search
+ * @return ICacheEntry[] an array of cache entries where the mimetype matches the search
*/
public function searchByMime($mimetype);
*
* @param string|int $tag name or tag id
* @param string $userId owner of the tags
- * @return array file data
+ * @return ICacheEntry[] file data
*/
public function searchByTag($tag, $userId);
--- /dev/null
+<?php
+/**
+ * @author Robin Appelman <icewind@owncloud.com>>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCP\Files\Cache;
+
+/**
+ * meta data for a file or folder
+ */
+interface ICacheEntry {
+ /**
+ * Get the numeric id of a file
+ *
+ * @return int
+ */
+ public function getId();
+
+ /**
+ * Get the numeric id for the storage
+ *
+ * @return int
+ */
+ public function getStorageId();
+
+ /**
+ * Get the path of the file relative to the storage root
+ *
+ * @return string
+ */
+ public function getPath();
+
+ /**
+ * Get the file name
+ *
+ * @return string
+ */
+ public function getName();
+
+ /**
+ * Get the full mimetype
+ *
+ * @return string
+ */
+ public function getMimeType();
+
+ /**
+ * Get the first part of the mimetype
+ *
+ * @return string
+ */
+ public function getMimePart();
+
+ /**
+ * Get the file size in bytes
+ *
+ * @return int
+ */
+ public function getSize();
+
+ /**
+ * Get the last modified date as unix timestamp
+ *
+ * @return int
+ */
+ public function getMTime();
+
+ /**
+ * Get the last modified date on the storage as unix timestamp
+ *
+ * Note that when a file is updated we also update the mtime of all parent folders to make it visible to the user which folder has had updates most recently
+ * This can differ from the mtime on the underlying storage which usually only changes when a direct child is added, removed or renamed
+ *
+ * @return int
+ */
+ public function getStorageMTime();
+
+ /**
+ * Get the etag for the file
+ *
+ * An etag is used for change detection of files and folders, an etag of a file changes whenever the content of the file changes
+ * Etag for folders change whenever a file in the folder has changed
+ *
+ * @return string
+ */
+ public function getEtag();
+
+ /**
+ * Get the permissions for the file stored as bitwise combination of \OCP\PERMISSION_READ, \OCP\PERMISSION_CREATE
+ * \OCP\PERMISSION_UPDATE, \OCP\PERMISSION_DELETE and \OCP\PERMISSION_SHARE
+ *
+ * @return int
+ */
+ public function getPermissions();
+
+ /**
+ * Check if the file is encrypted
+ *
+ * @return bool
+ */
+ public function isEncrypted();
+}
+++ /dev/null
-<?php
-/**
- * @author Robin Appelman <icewind@owncloud.com>>
- *
- * @copyright Copyright (c) 2015, ownCloud, Inc.
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License, version 3,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
- */
-
-namespace OCP\Files\Cache;
-
-/**
- * meta data for a file or folder
- */
-interface ICacheEntry {
- /**
- * Get the numeric id of a file
- *
- * @return int
- */
- public function getId();
-
- /**
- * Get the numeric id for the storage
- *
- * @return int
- */
- public function getStorageId();
-
- /**
- * Get the path of the file relative to the storage root
- *
- * @return string
- */
- public function getPath();
-
- /**
- * Get the file name
- *
- * @return string
- */
- public function getName();
-
- /**
- * Get the full mimetype
- *
- * @return string
- */
- public function getMimeType();
-
- /**
- * Get the first part of the mimetype
- *
- * @return string
- */
- public function getMimePart();
-
- /**
- * Get the file size in bytes
- *
- * @return int
- */
- public function getSize();
-
- /**
- * Get the last modified date as unix timestamp
- *
- * @return int
- */
- public function getMTime();
-
- /**
- * Get the last modified date on the storage as unix timestamp
- *
- * Note that when a file is updated we also update the mtime of all parent folders to make it visible to the user which folder has had updates most recently
- * This can differ from the mtime on the underlying storage which usually only changes when a direct child is added, removed or renamed
- *
- * @return int
- */
- public function getStorageMTime();
-
- /**
- * Get the etag for the file
- *
- * An etag is used for change detection of files and folders, an etag of a file changes whenever the content of the file changes
- * Etag for folders change whenever a file in the folder has changed
- *
- * @return string
- */
- public function getEtag();
-
- /**
- * Get the permissions for the file stored as bitwise combination of \OCP\PERMISSION_READ, \OCP\PERMISSION_CREATE
- * \OCP\PERMISSION_UPDATE, \OCP\PERMISSION_DELETE and \OCP\PERMISSION_SHARE
- *
- * @return int
- */
- public function getPermissions();
-
- /**
- * Check if the file is encrypted
- *
- * @return bool
- */
- public function isEncrypted();
-}