2012-09-16 18:52:32 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Files\Cache;
|
|
|
|
|
2012-10-26 14:30:25 +04:00
|
|
|
/**
|
|
|
|
* Metadata cache for the filesystem
|
|
|
|
*
|
|
|
|
* don't use this class directly if you need to get metadata, use \OC\Files\Filesystem::getFileInfo instead
|
|
|
|
*/
|
2012-09-16 18:52:32 +04:00
|
|
|
class Cache {
|
2012-10-08 16:58:21 +04:00
|
|
|
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;
|
|
|
|
|
2012-09-16 18:52:32 +04:00
|
|
|
/**
|
|
|
|
* @var array partial data for the cache
|
|
|
|
*/
|
2012-09-26 19:52:02 +04:00
|
|
|
private $partial = array();
|
|
|
|
|
2012-11-08 20:59:08 +04:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2012-09-26 19:52:02 +04:00
|
|
|
private $storageId;
|
|
|
|
|
|
|
|
/**
|
2012-11-08 20:59:08 +04:00
|
|
|
* @param \OC\Files\Storage\Storage|string $storage
|
2012-09-26 19:52:02 +04:00
|
|
|
*/
|
2012-11-08 20:59:08 +04:00
|
|
|
public function __construct($storage) {
|
|
|
|
if($storage instanceof \OC\Files\Storage\Storage){
|
|
|
|
$this->storageId = $storage->getId();
|
|
|
|
}else{
|
|
|
|
$this->storageId = $storage;
|
|
|
|
}
|
2012-09-26 19:52:02 +04:00
|
|
|
}
|
2012-09-16 18:52:32 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* get the stored metadata of a file or folder
|
|
|
|
*
|
2012-09-26 19:52:02 +04:00
|
|
|
* @param string/int $file
|
2012-09-16 18:52:32 +04:00
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-26 19:52:02 +04:00
|
|
|
public function get($file) {
|
|
|
|
if (is_string($file)) {
|
2012-09-16 18:52:32 +04:00
|
|
|
$where = 'WHERE `storage` = ? AND `path_hash` = ?';
|
2012-09-26 19:52:02 +04:00
|
|
|
$params = array($this->storageId, md5($file));
|
2012-09-16 18:52:32 +04:00
|
|
|
} else { //file id
|
|
|
|
$where = 'WHERE `fileid` = ?';
|
|
|
|
$params = array($file);
|
|
|
|
}
|
|
|
|
$query = \OC_DB::prepare(
|
2012-10-03 01:34:45 +04:00
|
|
|
'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, `encrypted`
|
2012-09-16 18:52:32 +04:00
|
|
|
FROM `*PREFIX*filecache` ' . $where);
|
2012-09-22 16:40:04 +04:00
|
|
|
$result = $query->execute($params);
|
2012-09-22 17:43:48 +04:00
|
|
|
$data = $result->fetchRow();
|
2012-09-22 16:40:04 +04:00
|
|
|
|
|
|
|
//merge partial data
|
2012-09-26 19:52:02 +04:00
|
|
|
if (!$data and is_string($file)) {
|
|
|
|
if (isset($this->partial[$file])) {
|
|
|
|
$data = $this->partial[$file];
|
2012-09-22 17:43:48 +04:00
|
|
|
}
|
2012-09-26 19:52:02 +04:00
|
|
|
} else {
|
|
|
|
//fix types
|
|
|
|
$data['fileid'] = (int)$data['fileid'];
|
|
|
|
$data['size'] = (int)$data['size'];
|
|
|
|
$data['mtime'] = (int)$data['mtime'];
|
2012-10-03 01:34:45 +04:00
|
|
|
$data['encrypted'] = (bool)$data['encrypted'];
|
2012-09-22 16:40:04 +04:00
|
|
|
}
|
2012-09-26 19:52:02 +04:00
|
|
|
|
2012-09-22 17:43:48 +04:00
|
|
|
return $data;
|
2012-09-16 18:52:32 +04:00
|
|
|
}
|
|
|
|
|
2012-09-23 17:25:03 +04:00
|
|
|
/**
|
|
|
|
* get the metadata of all files stored in $folder
|
|
|
|
*
|
2012-09-26 19:52:02 +04:00
|
|
|
* @param string $folder
|
2012-09-23 17:25:03 +04:00
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-26 19:52:02 +04:00
|
|
|
public function getFolderContents($folder) {
|
|
|
|
$fileId = $this->getId($folder);
|
2012-09-23 17:25:03 +04:00
|
|
|
if ($fileId > -1) {
|
|
|
|
$query = \OC_DB::prepare(
|
2012-10-03 01:34:45 +04:00
|
|
|
'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, `encrypted`
|
2012-09-23 17:25:03 +04:00
|
|
|
FROM `*PREFIX*filecache` WHERE parent = ?');
|
|
|
|
$result = $query->execute(array($fileId));
|
|
|
|
return $result->fetchAll();
|
|
|
|
} else {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-16 18:52:32 +04:00
|
|
|
/**
|
|
|
|
* store meta data for a file or folder
|
|
|
|
*
|
2012-09-26 19:52:02 +04:00
|
|
|
* @param string $file
|
2012-09-16 18:52:32 +04:00
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return int file id
|
|
|
|
*/
|
2012-09-26 19:52:02 +04:00
|
|
|
public function put($file, array $data) {
|
|
|
|
if (($id = $this->getId($file)) > -1) {
|
|
|
|
$this->update($id, $data);
|
2012-09-16 18:52:32 +04:00
|
|
|
return $id;
|
|
|
|
} else {
|
2012-09-26 19:52:02 +04:00
|
|
|
if (isset($this->partial[$file])) { //add any saved partial data
|
|
|
|
$data = array_merge($this->partial[$file], $data);
|
|
|
|
unset($this->partial[$file]);
|
2012-09-16 18:52:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$requiredFields = array('size', 'mtime', 'mimetype');
|
|
|
|
foreach ($requiredFields as $field) {
|
|
|
|
if (!isset($data[$field])) { //data not complete save as partial and return
|
2012-09-26 19:52:02 +04:00
|
|
|
$this->partial[$file] = $data;
|
2012-09-16 18:52:32 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-26 19:52:02 +04:00
|
|
|
$data['path'] = $file;
|
|
|
|
$data['parent'] = $this->getParentId($file);
|
|
|
|
$data['name'] = basename($file);
|
2012-10-03 01:34:45 +04:00
|
|
|
$data['encrypted'] = isset($data['encrypted']) ? ((int)$data['encrypted']) : 0;
|
2012-09-16 18:52:32 +04:00
|
|
|
|
2012-09-26 19:52:02 +04:00
|
|
|
list($queryParts, $params) = $this->buildParts($data);
|
2012-09-16 18:52:32 +04:00
|
|
|
$queryParts[] = '`storage`';
|
2012-09-26 19:52:02 +04:00
|
|
|
$params[] = $this->storageId;
|
2012-09-16 18:52:32 +04:00
|
|
|
$valuesPlaceholder = array_fill(0, count($queryParts), '?');
|
|
|
|
|
2012-09-22 17:43:48 +04:00
|
|
|
$query = \OC_DB::prepare('INSERT INTO `*PREFIX*filecache`(' . implode(', ', $queryParts) . ') VALUES(' . implode(', ', $valuesPlaceholder) . ')');
|
2012-09-16 18:52:32 +04:00
|
|
|
$query->execute($params);
|
|
|
|
|
2012-10-03 01:34:45 +04:00
|
|
|
return (int)\OC_DB::insertid('*PREFIX*filecache');
|
2012-09-16 18:52:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* update the metadata in the cache
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @param array $data
|
|
|
|
*/
|
2012-09-26 19:52:02 +04:00
|
|
|
public function update($id, array $data) {
|
|
|
|
list($queryParts, $params) = $this->buildParts($data);
|
2012-09-16 18:52:32 +04:00
|
|
|
$params[] = $id;
|
|
|
|
|
|
|
|
$query = \OC_DB::prepare('UPDATE `*PREFIX*filecache` SET ' . implode(' = ?, ', $queryParts) . '=? WHERE fileid = ?');
|
|
|
|
$query->execute($params);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* extract query parts and params array from data array
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-26 19:52:02 +04:00
|
|
|
static function buildParts(array $data) {
|
2012-10-03 01:34:45 +04:00
|
|
|
$fields = array('path', 'parent', 'name', 'mimetype', 'size', 'mtime', 'encrypted');
|
2012-09-16 18:52:32 +04:00
|
|
|
|
|
|
|
$params = array();
|
|
|
|
$queryParts = array();
|
|
|
|
foreach ($data as $name => $value) {
|
|
|
|
if (array_search($name, $fields) !== false) {
|
|
|
|
$params[] = $value;
|
|
|
|
$queryParts[] = '`' . $name . '`';
|
|
|
|
if ($name === 'path') {
|
|
|
|
$params[] = md5($value);
|
|
|
|
$queryParts[] = '`path_hash`';
|
|
|
|
} elseif ($name === 'mimetype') {
|
|
|
|
$params[] = substr($value, 0, strpos($value, '/'));
|
|
|
|
$queryParts[] = '`mimepart`';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array($queryParts, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the file id for a file
|
|
|
|
*
|
2012-09-26 19:52:02 +04:00
|
|
|
* @param string $file
|
2012-09-16 18:52:32 +04:00
|
|
|
* @return int
|
|
|
|
*/
|
2012-09-26 19:52:02 +04:00
|
|
|
public function getId($file) {
|
|
|
|
$pathHash = md5($file);
|
2012-09-16 18:52:32 +04:00
|
|
|
|
2012-09-22 17:43:48 +04:00
|
|
|
$query = \OC_DB::prepare('SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?');
|
2012-09-26 19:52:02 +04:00
|
|
|
$result = $query->execute(array($this->storageId, $pathHash));
|
2012-09-16 18:52:32 +04:00
|
|
|
|
|
|
|
if ($row = $result->fetchRow()) {
|
2012-09-22 17:43:48 +04:00
|
|
|
return $row['fileid'];
|
2012-09-16 18:52:32 +04:00
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the id of the parent folder of a file
|
|
|
|
*
|
2012-09-26 19:52:02 +04:00
|
|
|
* @param string $file
|
2012-09-22 17:48:39 +04:00
|
|
|
* @return int
|
2012-09-16 18:52:32 +04:00
|
|
|
*/
|
2012-09-26 19:52:02 +04:00
|
|
|
public function getParentId($file) {
|
2012-10-03 13:23:33 +04:00
|
|
|
if ($file === '') {
|
2012-09-16 18:52:32 +04:00
|
|
|
return -1;
|
|
|
|
} else {
|
2012-10-03 13:23:33 +04:00
|
|
|
$parent = dirname($file);
|
|
|
|
if ($parent === '.') {
|
|
|
|
$parent = '';
|
|
|
|
}
|
|
|
|
return $this->getId($parent);
|
2012-09-16 18:52:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if a file is available in the cache
|
|
|
|
*
|
2012-09-26 19:52:02 +04:00
|
|
|
* @param string $file
|
2012-09-16 18:52:32 +04:00
|
|
|
* @return bool
|
|
|
|
*/
|
2012-09-26 19:52:02 +04:00
|
|
|
public function inCache($file) {
|
|
|
|
return $this->getId($file) != -1;
|
2012-09-16 18:52:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* remove a file or folder from the cache
|
|
|
|
*
|
2012-09-26 19:52:02 +04:00
|
|
|
* @param string $file
|
2012-09-16 18:52:32 +04:00
|
|
|
*/
|
2012-09-26 19:52:02 +04:00
|
|
|
public function remove($file) {
|
2012-10-27 20:05:40 +04:00
|
|
|
$entry = $this->get($file);
|
|
|
|
if ($entry['mimetype'] === 'httpd/unix-directory') {
|
|
|
|
$children = $this->getFolderContents($file);
|
2012-10-28 14:26:31 +04:00
|
|
|
foreach ($children as $child) {
|
2012-10-27 20:05:40 +04:00
|
|
|
$this->remove($child['path']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$query = \OC_DB::prepare('DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?');
|
|
|
|
$query->execute(array($entry['fileid']));
|
2012-09-16 18:52:32 +04:00
|
|
|
}
|
|
|
|
|
2012-11-03 01:25:33 +04:00
|
|
|
/**
|
|
|
|
* Move a file or folder in the cache
|
|
|
|
*
|
|
|
|
* @param string $source
|
|
|
|
* @param string $target
|
|
|
|
*/
|
|
|
|
public function move($source, $target) {
|
|
|
|
$sourceId = $this->getId($source);
|
|
|
|
$newParentId = $this->getParentId($target);
|
|
|
|
|
|
|
|
//find all child entries
|
|
|
|
$query = \OC_DB::prepare('SELECT `path`, `fileid` FROM `*PREFIX*filecache` WHERE `path` LIKE ?');
|
|
|
|
$result = $query->execute(array($source . '/%'));
|
|
|
|
$childEntries = $result->fetchAll();
|
|
|
|
$sourceLength = strlen($source);
|
|
|
|
$query = \OC_DB::prepare('UPDATE `*PREFIX*filecache` SET `path` = ?, `path_hash` = ? WHERE `fileid` = ?');
|
|
|
|
|
|
|
|
foreach ($childEntries as $child) {
|
|
|
|
$targetPath = $target . substr($child['path'], $sourceLength);
|
|
|
|
$query->execute(array($targetPath, md5($targetPath), $child['fileid']));
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = \OC_DB::prepare('UPDATE `*PREFIX*filecache` SET `path` = ?, `path_hash` = ?, `parent` =? WHERE `fileid` = ?');
|
|
|
|
$query->execute(array($target, md5($target), $newParentId, $sourceId));
|
|
|
|
}
|
|
|
|
|
2012-09-16 18:52:32 +04:00
|
|
|
/**
|
2012-09-26 19:52:02 +04:00
|
|
|
* remove all entries for files that are stored on the storage from the cache
|
2012-09-16 18:52:32 +04:00
|
|
|
*/
|
2012-09-26 19:52:02 +04:00
|
|
|
public function clear() {
|
2012-09-16 18:52:32 +04:00
|
|
|
$query = \OC_DB::prepare('DELETE FROM `*PREFIX*filecache` WHERE storage=?');
|
2012-09-26 19:52:02 +04:00
|
|
|
$query->execute(array($this->storageId));
|
2012-09-16 18:52:32 +04:00
|
|
|
}
|
2012-10-08 16:58:21 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $file
|
|
|
|
*
|
|
|
|
* @return int, Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
|
|
|
|
*/
|
|
|
|
public function getStatus($file) {
|
|
|
|
$pathHash = md5($file);
|
2012-10-26 15:23:15 +04:00
|
|
|
$query = \OC_DB::prepare('SELECT `size` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?');
|
2012-10-08 16:58:21 +04:00
|
|
|
$result = $query->execute(array($this->storageId, $pathHash));
|
|
|
|
if ($row = $result->fetchRow()) {
|
|
|
|
if ((int)$row['size'] === -1) {
|
|
|
|
return self::SHALLOW;
|
|
|
|
} else {
|
|
|
|
return self::COMPLETE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (isset($this->partial[$file])) {
|
|
|
|
return self::PARTIAL;
|
|
|
|
} else {
|
|
|
|
return self::NOT_FOUND;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-26 15:23:15 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* search for files matching $pattern
|
|
|
|
*
|
|
|
|
* @param string $pattern
|
|
|
|
* @return array of file data
|
|
|
|
*/
|
|
|
|
public function search($pattern) {
|
|
|
|
$query = \OC_DB::prepare('
|
|
|
|
SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, `encrypted`
|
|
|
|
FROM `*PREFIX*filecache` WHERE `name` LIKE ? AND `storage` = ?'
|
|
|
|
);
|
|
|
|
$result = $query->execute(array($pattern, $this->storageId));
|
|
|
|
$files = array();
|
|
|
|
while ($row = $result->fetchRow()) {
|
|
|
|
$files[] = $row;
|
|
|
|
}
|
|
|
|
return $files;
|
|
|
|
}
|
2012-10-27 12:01:20 +04:00
|
|
|
|
2012-10-27 12:34:25 +04:00
|
|
|
/**
|
|
|
|
* search for files by mimetype
|
|
|
|
*
|
2012-11-03 01:25:33 +04:00
|
|
|
* @param string $mimetype
|
2012-10-27 12:34:25 +04:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function searchByMime($mimetype) {
|
|
|
|
if (strpos($mimetype, '/')) {
|
|
|
|
$where = '`mimetype` = ?';
|
|
|
|
} else {
|
|
|
|
$where = '`mimepart` = ?';
|
|
|
|
}
|
|
|
|
$query = \OC_DB::prepare('
|
|
|
|
SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, `encrypted`
|
|
|
|
FROM `*PREFIX*filecache` WHERE ' . $where . ' AND `storage` = ?'
|
|
|
|
);
|
|
|
|
$result = $query->execute(array($mimetype, $this->storageId));
|
|
|
|
return $result->fetchAll();
|
|
|
|
}
|
|
|
|
|
2012-10-27 19:02:05 +04:00
|
|
|
/**
|
|
|
|
* get the size of a folder and set it in the cache
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function calculateFolderSize($path) {
|
|
|
|
$id = $this->getId($path);
|
|
|
|
$query = \OC_DB::prepare('SELECT `size` FROM `*PREFIX*filecache` WHERE `parent` = ? AND `storage` = ?');
|
|
|
|
$result = $query->execute(array($id, $this->storageId));
|
|
|
|
$totalSize = 0;
|
2012-10-28 14:26:31 +04:00
|
|
|
$hasChilds = 0;
|
2012-10-27 19:02:05 +04:00
|
|
|
while ($row = $result->fetchRow()) {
|
2012-10-28 14:26:31 +04:00
|
|
|
$hasChilds = true;
|
2012-10-27 19:02:05 +04:00
|
|
|
$size = (int)$row['size'];
|
|
|
|
if ($size === -1) {
|
|
|
|
$totalSize = -1;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
$totalSize += $size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-28 14:26:31 +04:00
|
|
|
if ($hasChilds) {
|
|
|
|
$this->update($id, array('size' => $totalSize));
|
|
|
|
}
|
2012-10-27 19:02:05 +04:00
|
|
|
return $totalSize;
|
|
|
|
}
|
|
|
|
|
2012-10-27 12:01:20 +04:00
|
|
|
/**
|
|
|
|
* get all file ids on the files on the storage
|
|
|
|
*
|
|
|
|
* @return int[]
|
|
|
|
*/
|
|
|
|
public function getAll() {
|
|
|
|
$query = \OC_DB::prepare('SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ?');
|
|
|
|
$result = $query->execute(array($this->storageId));
|
|
|
|
$ids = array();
|
|
|
|
while ($row = $result->fetchRow()) {
|
|
|
|
$ids[] = $row['fileid'];
|
|
|
|
}
|
|
|
|
return $ids;
|
|
|
|
}
|
2012-09-16 18:52:32 +04:00
|
|
|
}
|