2011-11-09 21:41:57 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Robin Appelman
|
|
|
|
* @copyright 2011 Robin Appelman icewind1991@gmail.com
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or any later version.
|
|
|
|
*
|
|
|
|
* This library 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 along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* provide caching for filesystem info in the database
|
|
|
|
*
|
|
|
|
* not used by OC_Filesystem for reading filesystem info,
|
|
|
|
* instread apps should use OC_FileCache::get where possible
|
|
|
|
*
|
|
|
|
* It will try to keep the data up to date but changes from outside ownCloud can invalidate the cache
|
|
|
|
*/
|
|
|
|
class OC_FileCache{
|
2012-11-28 20:57:44 +04:00
|
|
|
|
2011-11-09 21:41:57 +04:00
|
|
|
/**
|
|
|
|
* get the filesystem info from the cache
|
|
|
|
* @param string path
|
2012-02-02 22:04:13 +04:00
|
|
|
* @param string root (optional)
|
2011-11-09 21:41:57 +04:00
|
|
|
* @return array
|
|
|
|
*
|
2012-06-15 19:05:27 +04:00
|
|
|
* returns an associative array with the following keys:
|
2011-11-09 21:41:57 +04:00
|
|
|
* - size
|
|
|
|
* - mtime
|
|
|
|
* - ctime
|
|
|
|
* - mimetype
|
2011-11-12 04:00:04 +04:00
|
|
|
* - encrypted
|
|
|
|
* - versioned
|
2011-11-09 21:41:57 +04:00
|
|
|
*/
|
2012-11-02 22:53:02 +04:00
|
|
|
public static function get($path, $root=false) {
|
2012-10-23 02:28:12 +04:00
|
|
|
if(OC_FileCache_Update::hasUpdated($path, $root)) {
|
2012-09-07 17:22:01 +04:00
|
|
|
if($root===false) {//filesystem hooks are only valid for the default root
|
2012-10-23 02:28:12 +04:00
|
|
|
OC_Hook::emit('OC_Filesystem', 'post_write', array('path'=>$path));
|
2012-02-05 04:25:36 +04:00
|
|
|
}else{
|
2012-10-23 02:28:12 +04:00
|
|
|
OC_FileCache_Update::update($path, $root);
|
2012-02-05 04:25:36 +04:00
|
|
|
}
|
|
|
|
}
|
2012-10-23 02:28:12 +04:00
|
|
|
return OC_FileCache_Cached::get($path, $root);
|
2011-11-09 21:41:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* put filesystem info in the cache
|
|
|
|
* @param string $path
|
|
|
|
* @param array data
|
2012-02-02 22:04:13 +04:00
|
|
|
* @param string root (optional)
|
2012-11-28 20:57:44 +04:00
|
|
|
* @note $data is an associative array in the same format as returned
|
|
|
|
* by get
|
2011-11-09 21:41:57 +04:00
|
|
|
*/
|
2012-11-04 14:10:46 +04:00
|
|
|
public static function put($path, $data, $root=false) {
|
2012-09-07 17:22:01 +04:00
|
|
|
if($root===false) {
|
2012-02-02 22:04:13 +04:00
|
|
|
$root=OC_Filesystem::getRoot();
|
|
|
|
}
|
2012-10-24 19:23:36 +04:00
|
|
|
$fullpath=OC_Filesystem::normalizePath($root.'/'.$path);
|
2012-08-08 13:47:23 +04:00
|
|
|
$parent=self::getParentId($fullpath);
|
2012-10-23 02:28:12 +04:00
|
|
|
$id=self::getId($fullpath, '');
|
2012-09-07 17:22:01 +04:00
|
|
|
if(isset(OC_FileCache_Cached::$savedData[$fullpath])) {
|
2012-10-23 02:28:12 +04:00
|
|
|
$data=array_merge(OC_FileCache_Cached::$savedData[$fullpath], $data);
|
2012-08-08 12:51:19 +04:00
|
|
|
unset(OC_FileCache_Cached::$savedData[$fullpath]);
|
2012-08-08 13:47:23 +04:00
|
|
|
}
|
2012-09-07 17:22:01 +04:00
|
|
|
if($id!=-1) {
|
2012-10-23 02:28:12 +04:00
|
|
|
self::update($id, $data);
|
2012-06-21 19:37:53 +04:00
|
|
|
return;
|
|
|
|
}
|
2012-08-29 10:38:33 +04:00
|
|
|
|
2012-08-08 13:29:44 +04:00
|
|
|
// add parent directory to the file cache if it does not exist yet.
|
2012-08-08 12:51:19 +04:00
|
|
|
if ($parent == -1 && $fullpath != $root) {
|
2012-10-09 18:21:46 +04:00
|
|
|
$parentDir = dirname($path);
|
2012-08-08 13:29:44 +04:00
|
|
|
self::scanFile($parentDir);
|
|
|
|
$parent = self::getParentId($fullpath);
|
2012-08-08 12:51:19 +04:00
|
|
|
}
|
2012-08-29 10:38:33 +04:00
|
|
|
|
2012-09-07 17:22:01 +04:00
|
|
|
if(!isset($data['size']) or !isset($data['mtime'])) {//save incomplete data for the next time we write it
|
2012-08-08 12:51:19 +04:00
|
|
|
OC_FileCache_Cached::$savedData[$fullpath]=$data;
|
2012-06-21 20:07:56 +04:00
|
|
|
return;
|
|
|
|
}
|
2012-09-07 17:22:01 +04:00
|
|
|
if(!isset($data['encrypted'])) {
|
2011-11-12 04:00:04 +04:00
|
|
|
$data['encrypted']=false;
|
|
|
|
}
|
2012-09-07 17:22:01 +04:00
|
|
|
if(!isset($data['versioned'])) {
|
2011-11-12 04:00:04 +04:00
|
|
|
$data['versioned']=false;
|
|
|
|
}
|
2011-11-09 21:41:57 +04:00
|
|
|
$mimePart=dirname($data['mimetype']);
|
2012-05-04 01:16:18 +04:00
|
|
|
$data['size']=(int)$data['size'];
|
|
|
|
$data['ctime']=(int)$data['mtime'];
|
|
|
|
$data['writable']=(int)$data['writable'];
|
|
|
|
$data['encrypted']=(int)$data['encrypted'];
|
|
|
|
$data['versioned']=(int)$data['versioned'];
|
2012-01-31 19:12:49 +04:00
|
|
|
$user=OC_User::getUser();
|
2012-07-30 22:46:14 +04:00
|
|
|
$query=OC_DB::prepare('INSERT INTO `*PREFIX*fscache`(`parent`, `name`, `path`, `path_hash`, `size`, `mtime`, `ctime`, `mimetype`, `mimepart`,`user`,`writable`,`encrypted`,`versioned`) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)');
|
2012-10-24 01:01:10 +04:00
|
|
|
$result=$query->execute(array($parent, basename($fullpath), $fullpath, md5($fullpath), $data['size'], $data['mtime'], $data['ctime'], $data['mimetype'], $mimePart, $user, $data['writable'], $data['encrypted'], $data['versioned']));
|
2012-09-07 17:22:01 +04:00
|
|
|
if(OC_DB::isError($result)) {
|
2012-10-23 02:28:12 +04:00
|
|
|
OC_Log::write('files', 'error while writing file('.$fullpath.') to cache', OC_Log::ERROR);
|
2012-02-25 23:27:16 +04:00
|
|
|
}
|
2012-07-22 05:24:34 +04:00
|
|
|
|
2012-09-07 17:22:01 +04:00
|
|
|
if($cache=OC_Cache::getUserCache(true)) {
|
2012-08-08 12:51:19 +04:00
|
|
|
$cache->remove('fileid/'.$fullpath);//ensure we don't have -1 cached
|
2012-07-22 05:24:34 +04:00
|
|
|
}
|
2011-11-09 21:41:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* update filesystem info of a file
|
|
|
|
* @param int $id
|
|
|
|
* @param array $data
|
|
|
|
*/
|
2012-11-02 22:53:02 +04:00
|
|
|
private static function update($id, $data) {
|
2011-11-12 04:00:04 +04:00
|
|
|
$arguments=array();
|
|
|
|
$queryParts=array();
|
2012-11-04 14:10:46 +04:00
|
|
|
foreach(array('size','mtime','ctime','mimetype','encrypted','versioned', 'writable') as $attribute) {
|
2012-09-07 17:22:01 +04:00
|
|
|
if(isset($data[$attribute])) {
|
2012-06-22 12:46:49 +04:00
|
|
|
//Convert to int it args are false
|
2012-09-07 17:22:01 +04:00
|
|
|
if($data[$attribute] === false) {
|
2012-06-24 01:32:44 +04:00
|
|
|
$arguments[] = 0;
|
|
|
|
}else{
|
|
|
|
$arguments[] = $data[$attribute];
|
|
|
|
}
|
2012-07-30 22:46:14 +04:00
|
|
|
$queryParts[]='`'.$attribute.'`=?';
|
2011-11-12 04:00:04 +04:00
|
|
|
}
|
|
|
|
}
|
2012-09-07 17:22:01 +04:00
|
|
|
if(isset($data['mimetype'])) {
|
2011-11-12 04:00:04 +04:00
|
|
|
$arguments[]=dirname($data['mimetype']);
|
2012-08-01 17:00:07 +04:00
|
|
|
$queryParts[]='`mimepart`=?';
|
2011-11-12 04:00:04 +04:00
|
|
|
}
|
|
|
|
$arguments[]=$id;
|
2012-08-29 10:38:33 +04:00
|
|
|
|
2012-11-21 02:34:25 +04:00
|
|
|
if(!empty($queryParts)) {
|
|
|
|
$sql = 'UPDATE `*PREFIX*fscache` SET '.implode(' , ', $queryParts).' WHERE `id`=?';
|
|
|
|
$query=OC_DB::prepare($sql);
|
|
|
|
$result=$query->execute($arguments);
|
|
|
|
if(OC_DB::isError($result)) {
|
|
|
|
OC_Log::write('files', 'error while updating file('.$id.') in cache', OC_Log::ERROR);
|
|
|
|
}
|
2012-02-25 23:27:16 +04:00
|
|
|
}
|
2011-11-09 21:41:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* register a file move in the cache
|
|
|
|
* @param string oldPath
|
|
|
|
* @param string newPath
|
2012-02-02 22:04:13 +04:00
|
|
|
* @param string root (optional)
|
2011-11-09 21:41:57 +04:00
|
|
|
*/
|
2012-11-02 22:53:02 +04:00
|
|
|
public static function move($oldPath, $newPath, $root=false) {
|
2012-09-07 17:22:01 +04:00
|
|
|
if($root===false) {
|
2012-02-02 22:04:13 +04:00
|
|
|
$root=OC_Filesystem::getRoot();
|
|
|
|
}
|
2012-09-06 06:13:50 +04:00
|
|
|
// If replacing an existing file, delete the file
|
|
|
|
if (self::inCache($newPath, $root)) {
|
|
|
|
self::delete($newPath, $root);
|
|
|
|
}
|
2012-02-02 22:04:13 +04:00
|
|
|
$oldPath=$root.$oldPath;
|
|
|
|
$newPath=$root.$newPath;
|
2011-11-09 21:41:57 +04:00
|
|
|
$newParent=self::getParentId($newPath);
|
2012-07-30 22:46:14 +04:00
|
|
|
$query=OC_DB::prepare('UPDATE `*PREFIX*fscache` SET `parent`=? ,`name`=?, `path`=?, `path_hash`=? WHERE `path_hash`=?');
|
2012-10-24 01:01:10 +04:00
|
|
|
$query->execute(array($newParent, basename($newPath), $newPath, md5($newPath), md5($oldPath)));
|
2012-06-04 19:59:21 +04:00
|
|
|
|
2012-09-07 17:22:01 +04:00
|
|
|
if(($cache=OC_Cache::getUserCache(true)) && $cache->hasKey('fileid/'.$oldPath)) {
|
2012-10-23 02:28:12 +04:00
|
|
|
$cache->set('fileid/'.$newPath, $cache->get('fileid/'.$oldPath));
|
2012-07-22 05:24:34 +04:00
|
|
|
$cache->remove('fileid/'.$oldPath);
|
|
|
|
}
|
|
|
|
|
2012-07-30 22:46:14 +04:00
|
|
|
$query=OC_DB::prepare('SELECT `path` FROM `*PREFIX*fscache` WHERE `path` LIKE ?');
|
2012-06-04 19:59:21 +04:00
|
|
|
$oldLength=strlen($oldPath);
|
2012-07-30 22:46:14 +04:00
|
|
|
$updateQuery=OC_DB::prepare('UPDATE `*PREFIX*fscache` SET `path`=?, `path_hash`=? WHERE `path_hash`=?');
|
2012-09-07 17:22:01 +04:00
|
|
|
while($row= $query->execute(array($oldPath.'/%'))->fetchRow()) {
|
2012-06-04 19:59:21 +04:00
|
|
|
$old=$row['path'];
|
2012-10-23 02:28:12 +04:00
|
|
|
$new=$newPath.substr($old, $oldLength);
|
2012-10-24 01:01:10 +04:00
|
|
|
$updateQuery->execute(array($new, md5($new), md5($old)));
|
2012-07-22 05:24:34 +04:00
|
|
|
|
2012-09-07 17:22:01 +04:00
|
|
|
if(($cache=OC_Cache::getUserCache(true)) && $cache->hasKey('fileid/'.$old)) {
|
2012-10-23 02:28:12 +04:00
|
|
|
$cache->set('fileid/'.$new, $cache->get('fileid/'.$old));
|
2012-07-22 05:24:34 +04:00
|
|
|
$cache->remove('fileid/'.$old);
|
|
|
|
}
|
2012-06-04 19:59:21 +04:00
|
|
|
}
|
2011-11-09 21:41:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* delete info from the cache
|
2012-06-15 21:56:15 +04:00
|
|
|
* @param string path
|
2012-02-02 22:04:13 +04:00
|
|
|
* @param string root (optional)
|
2011-11-09 21:41:57 +04:00
|
|
|
*/
|
2012-11-02 22:53:02 +04:00
|
|
|
public static function delete($path, $root=false) {
|
2012-09-07 17:22:01 +04:00
|
|
|
if($root===false) {
|
2012-06-15 21:56:15 +04:00
|
|
|
$root=OC_Filesystem::getRoot();
|
2012-02-05 04:25:36 +04:00
|
|
|
}
|
2012-08-25 02:05:07 +04:00
|
|
|
$query=OC_DB::prepare('DELETE FROM `*PREFIX*fscache` WHERE `path_hash`=?');
|
2012-06-15 21:56:15 +04:00
|
|
|
$query->execute(array(md5($root.$path)));
|
2012-08-29 10:38:33 +04:00
|
|
|
|
2012-06-15 21:56:15 +04:00
|
|
|
//delete everything inside the folder
|
2012-08-25 02:05:07 +04:00
|
|
|
$query=OC_DB::prepare('DELETE FROM `*PREFIX*fscache` WHERE `path` LIKE ?');
|
2012-06-15 21:56:15 +04:00
|
|
|
$query->execute(array($root.$path.'/%'));
|
2012-07-22 05:24:34 +04:00
|
|
|
|
|
|
|
OC_Cache::remove('fileid/'.$root.$path);
|
2011-11-09 21:41:57 +04:00
|
|
|
}
|
2012-06-14 18:09:32 +04:00
|
|
|
|
2011-11-09 21:41:57 +04:00
|
|
|
/**
|
|
|
|
* return array of filenames matching the querty
|
|
|
|
* @param string $query
|
2012-01-31 19:12:49 +04:00
|
|
|
* @param boolean $returnData
|
2012-02-02 22:04:13 +04:00
|
|
|
* @param string root (optional)
|
2011-11-09 21:41:57 +04:00
|
|
|
* @return array of filepaths
|
|
|
|
*/
|
2012-11-02 22:53:02 +04:00
|
|
|
public static function search($search, $returnData=false, $root=false) {
|
2012-09-07 17:22:01 +04:00
|
|
|
if($root===false) {
|
2012-02-02 22:04:13 +04:00
|
|
|
$root=OC_Filesystem::getRoot();
|
|
|
|
}
|
2012-01-31 19:12:49 +04:00
|
|
|
$rootLen=strlen($root);
|
2012-09-07 17:22:01 +04:00
|
|
|
if(!$returnData) {
|
2012-10-17 20:17:20 +04:00
|
|
|
$select = '`path`';
|
2012-01-31 19:12:49 +04:00
|
|
|
}else{
|
2012-10-17 20:17:20 +04:00
|
|
|
$select = '*';
|
2012-01-31 19:12:49 +04:00
|
|
|
}
|
2012-10-17 20:17:20 +04:00
|
|
|
if (OC_Config::getValue('dbtype') === 'oci8') {
|
|
|
|
$where = 'LOWER(`name`) LIKE LOWER(?) AND `user`=?';
|
|
|
|
} else {
|
|
|
|
$where = '`name` LIKE ? AND `user`=?';
|
|
|
|
}
|
|
|
|
$query=OC_DB::prepare('SELECT '.$select.' FROM `*PREFIX*fscache` WHERE '.$where);
|
2012-11-04 14:10:46 +04:00
|
|
|
$result=$query->execute(array("%$search%", OC_User::getUser()));
|
2011-11-09 21:41:57 +04:00
|
|
|
$names=array();
|
2012-09-07 17:22:01 +04:00
|
|
|
while($row=$result->fetchRow()) {
|
|
|
|
if(!$returnData) {
|
2012-10-23 02:28:12 +04:00
|
|
|
$names[]=substr($row['path'], $rootLen);
|
2012-01-31 19:12:49 +04:00
|
|
|
}else{
|
2012-10-23 02:28:12 +04:00
|
|
|
$row['path']=substr($row['path'], $rootLen);
|
2012-01-31 19:12:49 +04:00
|
|
|
$names[]=$row;
|
|
|
|
}
|
2011-11-09 21:41:57 +04:00
|
|
|
}
|
|
|
|
return $names;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get all files and folders in a folder
|
|
|
|
* @param string path
|
2012-02-02 22:04:13 +04:00
|
|
|
* @param string root (optional)
|
2011-11-09 21:41:57 +04:00
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
* returns an array of assiciative arrays with the following keys:
|
|
|
|
* - name
|
|
|
|
* - size
|
|
|
|
* - mtime
|
|
|
|
* - ctime
|
|
|
|
* - mimetype
|
2011-11-12 04:00:04 +04:00
|
|
|
* - encrypted
|
|
|
|
* - versioned
|
2011-11-09 21:41:57 +04:00
|
|
|
*/
|
2012-11-02 22:53:02 +04:00
|
|
|
public static function getFolderContent($path, $root=false, $mimetype_filter='') {
|
2012-10-23 02:28:12 +04:00
|
|
|
if(OC_FileCache_Update::hasUpdated($path, $root, true)) {
|
|
|
|
OC_FileCache_Update::updateFolder($path, $root);
|
2011-11-09 21:41:57 +04:00
|
|
|
}
|
2012-10-23 02:28:12 +04:00
|
|
|
return OC_FileCache_Cached::getFolderContent($path, $root, $mimetype_filter);
|
2011-11-09 21:41:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if a file or folder is in the cache
|
|
|
|
* @param string $path
|
2012-02-02 22:04:13 +04:00
|
|
|
* @param string root (optional)
|
2011-11-09 21:41:57 +04:00
|
|
|
* @return bool
|
|
|
|
*/
|
2012-11-02 22:53:02 +04:00
|
|
|
public static function inCache($path, $root=false) {
|
2012-10-23 02:28:12 +04:00
|
|
|
return self::getId($path, $root)!=-1;
|
2011-11-09 21:41:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the file id as used in the cache
|
2012-06-15 21:56:15 +04:00
|
|
|
* @param string path
|
|
|
|
* @param string root (optional)
|
2011-11-09 21:41:57 +04:00
|
|
|
* @return int
|
|
|
|
*/
|
2012-11-02 22:53:02 +04:00
|
|
|
public static function getId($path, $root=false) {
|
2012-09-07 17:22:01 +04:00
|
|
|
if($root===false) {
|
2012-06-15 21:56:15 +04:00
|
|
|
$root=OC_Filesystem::getRoot();
|
2012-02-25 23:27:16 +04:00
|
|
|
}
|
2012-07-22 05:24:34 +04:00
|
|
|
|
|
|
|
$fullPath=$root.$path;
|
2012-09-07 17:22:01 +04:00
|
|
|
if(($cache=OC_Cache::getUserCache(true)) && $cache->hasKey('fileid/'.$fullPath)) {
|
2012-07-22 05:24:34 +04:00
|
|
|
return $cache->get('fileid/'.$fullPath);
|
2012-02-25 23:27:16 +04:00
|
|
|
}
|
2012-08-29 10:38:33 +04:00
|
|
|
|
2012-08-25 02:05:07 +04:00
|
|
|
$query=OC_DB::prepare('SELECT `id` FROM `*PREFIX*fscache` WHERE `path_hash`=?');
|
2012-07-22 05:24:34 +04:00
|
|
|
$result=$query->execute(array(md5($fullPath)));
|
2012-09-07 17:22:01 +04:00
|
|
|
if(OC_DB::isError($result)) {
|
2012-10-23 02:28:12 +04:00
|
|
|
OC_Log::write('files', 'error while getting file id of '.$path, OC_Log::ERROR);
|
2012-02-25 23:27:16 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2012-08-29 10:38:33 +04:00
|
|
|
|
2012-02-25 23:27:16 +04:00
|
|
|
$result=$result->fetchRow();
|
2012-09-07 17:22:01 +04:00
|
|
|
if(is_array($result)) {
|
2012-07-22 05:24:34 +04:00
|
|
|
$id=$result['id'];
|
2011-11-09 21:41:57 +04:00
|
|
|
}else{
|
2012-07-22 05:24:34 +04:00
|
|
|
$id=-1;
|
2011-11-09 21:41:57 +04:00
|
|
|
}
|
2012-09-07 17:22:01 +04:00
|
|
|
if($cache=OC_Cache::getUserCache(true)) {
|
2012-10-23 02:28:12 +04:00
|
|
|
$cache->set('fileid/'.$fullPath, $id);
|
2012-04-15 13:44:04 +04:00
|
|
|
}
|
2012-08-29 10:38:33 +04:00
|
|
|
|
2012-07-22 05:24:34 +04:00
|
|
|
return $id;
|
2012-04-15 13:44:04 +04:00
|
|
|
}
|
2012-08-29 10:38:33 +04:00
|
|
|
|
2012-04-15 13:44:04 +04:00
|
|
|
/**
|
|
|
|
* get the file path from the id, relative to the home folder of the user
|
|
|
|
* @param int id
|
|
|
|
* @param string user (optional)
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-11-02 22:53:02 +04:00
|
|
|
public static function getPath($id, $user='') {
|
2012-09-07 17:22:01 +04:00
|
|
|
if(!$user) {
|
2012-04-15 13:44:04 +04:00
|
|
|
$user=OC_User::getUser();
|
|
|
|
}
|
2012-07-30 22:46:14 +04:00
|
|
|
$query=OC_DB::prepare('SELECT `path` FROM `*PREFIX*fscache` WHERE `id`=? AND `user`=?');
|
2012-10-23 02:28:12 +04:00
|
|
|
$result=$query->execute(array($id, $user));
|
2012-04-15 13:44:04 +04:00
|
|
|
$row=$result->fetchRow();
|
2012-04-16 12:06:52 +04:00
|
|
|
$path=$row['path'];
|
|
|
|
$root='/'.$user.'/files';
|
2012-10-23 02:28:12 +04:00
|
|
|
if(substr($path, 0, strlen($root))!=$root) {
|
2012-04-16 12:06:52 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-23 02:28:12 +04:00
|
|
|
return substr($path, strlen($root));
|
2012-04-15 13:44:04 +04:00
|
|
|
}
|
2011-11-09 21:41:57 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* get the file id of the parent folder, taking into account '/' has no parent
|
|
|
|
* @param string $path
|
|
|
|
* @return int
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
private static function getParentId($path) {
|
|
|
|
if($path=='/') {
|
2011-11-09 21:41:57 +04:00
|
|
|
return -1;
|
|
|
|
}else{
|
2012-10-23 02:28:12 +04:00
|
|
|
return self::getId(dirname($path), '');
|
2012-02-05 04:25:36 +04:00
|
|
|
}
|
|
|
|
}
|
2012-08-29 10:38:33 +04:00
|
|
|
|
2011-11-09 21:41:57 +04:00
|
|
|
/**
|
|
|
|
* adjust the size of the parent folders
|
|
|
|
* @param string $path
|
|
|
|
* @param int $sizeDiff
|
2012-06-15 21:56:15 +04:00
|
|
|
* @param string root (optinal)
|
2011-11-09 21:41:57 +04:00
|
|
|
*/
|
2012-11-02 22:53:02 +04:00
|
|
|
public static function increaseSize($path, $sizeDiff, $root=false) {
|
2012-02-05 04:25:36 +04:00
|
|
|
if($sizeDiff==0) return;
|
2012-11-16 20:54:58 +04:00
|
|
|
$item = OC_FileCache_Cached::get($path);
|
|
|
|
//stop walking up the filetree if we hit a non-folder
|
|
|
|
if($item['mimetype'] !== 'httpd/unix-directory'){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$id = $item['id'];
|
2012-09-07 17:22:01 +04:00
|
|
|
while($id!=-1) {//walk up the filetree increasing the size of all parent folders
|
2012-07-30 22:46:14 +04:00
|
|
|
$query=OC_DB::prepare('UPDATE `*PREFIX*fscache` SET `size`=`size`+? WHERE `id`=?');
|
2012-11-02 22:53:02 +04:00
|
|
|
$query->execute(array($sizeDiff, $id));
|
2012-11-16 20:54:58 +04:00
|
|
|
if($path == '' or $path =='/'){
|
|
|
|
return;
|
|
|
|
}
|
2011-11-09 21:41:57 +04:00
|
|
|
$path=dirname($path);
|
2012-11-16 20:54:58 +04:00
|
|
|
$parent = OC_FileCache_Cached::get($path);
|
|
|
|
$id = $parent['id'];
|
|
|
|
//stop walking up the filetree if we hit a non-folder
|
|
|
|
if($parent['mimetype'] !== 'httpd/unix-directory'){
|
|
|
|
return;
|
|
|
|
}
|
2011-11-09 21:41:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* recursively scan the filesystem and fill the cache
|
|
|
|
* @param string $path
|
2012-11-16 20:41:38 +04:00
|
|
|
* @param OC_EventSource $eventSource (optional)
|
|
|
|
* @param int $count (optional)
|
|
|
|
* @param string $root (optional)
|
2011-11-09 21:41:57 +04:00
|
|
|
*/
|
2012-11-04 14:10:46 +04:00
|
|
|
public static function scan($path, $eventSource=false,&$count=0, $root=false) {
|
2012-09-07 17:22:01 +04:00
|
|
|
if($eventSource) {
|
2012-10-23 02:28:12 +04:00
|
|
|
$eventSource->send('scanning', array('file'=>$path, 'count'=>$count));
|
2012-03-31 18:24:53 +04:00
|
|
|
}
|
|
|
|
$lastSend=$count;
|
2012-07-11 02:56:22 +04:00
|
|
|
// NOTE: Ugly hack to prevent shared files from going into the cache (the source already exists somewhere in the cache)
|
|
|
|
if (substr($path, 0, 7) == '/Shared') {
|
|
|
|
return;
|
|
|
|
}
|
2012-09-07 17:22:01 +04:00
|
|
|
if($root===false) {
|
2012-02-05 04:25:36 +04:00
|
|
|
$view=OC_Filesystem::getView();
|
|
|
|
}else{
|
2012-06-15 21:56:15 +04:00
|
|
|
$view=new OC_FilesystemView($root);
|
2011-11-09 21:41:57 +04:00
|
|
|
}
|
2012-10-23 02:28:12 +04:00
|
|
|
self::scanFile($path, $root);
|
2012-04-29 18:11:17 +04:00
|
|
|
$dh=$view->opendir($path.'/');
|
|
|
|
$totalSize=0;
|
2012-09-07 17:22:01 +04:00
|
|
|
if($dh) {
|
2012-04-29 18:11:17 +04:00
|
|
|
while (($filename = readdir($dh)) !== false) {
|
2012-09-07 17:22:01 +04:00
|
|
|
if($filename != '.' and $filename != '..') {
|
2012-04-29 18:11:17 +04:00
|
|
|
$file=$path.'/'.$filename;
|
2012-09-07 17:22:01 +04:00
|
|
|
if($view->is_dir($file.'/')) {
|
2012-10-23 02:28:12 +04:00
|
|
|
self::scan($file, $eventSource, $count, $root);
|
2012-04-29 18:11:17 +04:00
|
|
|
}else{
|
2012-10-23 02:28:12 +04:00
|
|
|
$totalSize+=self::scanFile($file, $root);
|
2012-04-29 18:11:17 +04:00
|
|
|
$count++;
|
2012-09-07 17:22:01 +04:00
|
|
|
if($count>$lastSend+25 and $eventSource) {
|
2012-04-29 18:11:17 +04:00
|
|
|
$lastSend=$count;
|
2012-10-23 02:28:12 +04:00
|
|
|
$eventSource->send('scanning', array('file'=>$path, 'count'=>$count));
|
2012-03-31 18:24:53 +04:00
|
|
|
}
|
2011-11-09 21:41:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-29 10:38:33 +04:00
|
|
|
|
2012-11-02 22:53:02 +04:00
|
|
|
OC_FileCache_Update::cleanFolder($path, $root);
|
2012-11-04 14:10:46 +04:00
|
|
|
self::increaseSize($path, $totalSize, $root);
|
2012-02-05 04:25:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* scan a single file
|
|
|
|
* @param string path
|
|
|
|
* @param string root (optional)
|
|
|
|
* @return int size of the scanned file
|
|
|
|
*/
|
2012-11-02 22:53:02 +04:00
|
|
|
public static function scanFile($path, $root=false) {
|
2012-07-11 02:56:22 +04:00
|
|
|
// NOTE: Ugly hack to prevent shared files from going into the cache (the source already exists somewhere in the cache)
|
|
|
|
if (substr($path, 0, 7) == '/Shared') {
|
|
|
|
return;
|
|
|
|
}
|
2012-09-07 17:22:01 +04:00
|
|
|
if($root===false) {
|
2012-02-05 04:25:36 +04:00
|
|
|
$view=OC_Filesystem::getView();
|
|
|
|
}else{
|
2012-06-15 21:56:15 +04:00
|
|
|
$view=new OC_FilesystemView($root);
|
2012-02-05 04:25:36 +04:00
|
|
|
}
|
|
|
|
if(!$view->is_readable($path)) return; //cant read, nothing we can do
|
2012-04-25 02:10:02 +04:00
|
|
|
clearstatcache();
|
2012-02-05 04:25:36 +04:00
|
|
|
$mimetype=$view->getMimeType($path);
|
2012-05-17 03:47:58 +04:00
|
|
|
$stat=$view->stat($path);
|
2012-09-07 17:22:01 +04:00
|
|
|
if($mimetype=='httpd/unix-directory') {
|
2012-10-06 00:35:51 +04:00
|
|
|
$stat['size'] = 0;
|
2012-05-17 03:47:58 +04:00
|
|
|
$writable=$view->is_writable($path.'/');
|
|
|
|
}else{
|
|
|
|
$writable=$view->is_writable($path);
|
|
|
|
}
|
2012-02-05 04:25:36 +04:00
|
|
|
$stat['mimetype']=$mimetype;
|
2012-02-05 17:04:10 +04:00
|
|
|
$stat['writable']=$writable;
|
2012-09-07 17:22:01 +04:00
|
|
|
if($path=='/') {
|
2012-02-05 04:25:36 +04:00
|
|
|
$path='';
|
|
|
|
}
|
2012-10-23 02:28:12 +04:00
|
|
|
self::put($path, $stat, $root);
|
2012-02-05 04:25:36 +04:00
|
|
|
return $stat['size'];
|
2011-11-09 21:41:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-03-15 03:09:06 +04:00
|
|
|
* find files by mimetype
|
2011-11-09 21:41:57 +04:00
|
|
|
* @param string $part1
|
|
|
|
* @param string $part2 (optional)
|
2012-02-08 20:29:54 +04:00
|
|
|
* @param string root (optional)
|
2011-11-09 21:41:57 +04:00
|
|
|
* @return array of file paths
|
|
|
|
*
|
|
|
|
* $part1 and $part2 together form the complete mimetype.
|
2012-11-04 14:10:46 +04:00
|
|
|
* e.g. searchByMime('text', 'plain')
|
2011-11-09 21:41:57 +04:00
|
|
|
*
|
|
|
|
* seccond mimetype part can be ommited
|
|
|
|
* e.g. searchByMime('audio')
|
|
|
|
*/
|
2012-11-02 22:53:02 +04:00
|
|
|
public static function searchByMime($part1, $part2=null, $root=false) {
|
2012-09-07 17:22:01 +04:00
|
|
|
if($root===false) {
|
2012-02-08 20:29:54 +04:00
|
|
|
$root=OC_Filesystem::getRoot();
|
|
|
|
}
|
2012-03-09 19:47:53 +04:00
|
|
|
$rootLen=strlen($root);
|
|
|
|
$root .= '%';
|
2012-02-08 20:29:54 +04:00
|
|
|
$user=OC_User::getUser();
|
2012-09-07 17:22:01 +04:00
|
|
|
if(!$part2) {
|
2012-07-30 22:46:14 +04:00
|
|
|
$query=OC_DB::prepare('SELECT `path` FROM `*PREFIX*fscache` WHERE `mimepart`=? AND `user`=? AND `path` LIKE ?');
|
2012-10-23 02:28:12 +04:00
|
|
|
$result=$query->execute(array($part1, $user, $root));
|
2011-11-09 21:41:57 +04:00
|
|
|
}else{
|
2012-07-30 22:46:14 +04:00
|
|
|
$query=OC_DB::prepare('SELECT `path` FROM `*PREFIX*fscache` WHERE `mimetype`=? AND `user`=? AND `path` LIKE ? ');
|
2012-10-23 02:28:12 +04:00
|
|
|
$result=$query->execute(array($part1.'/'.$part2, $user, $root));
|
2011-11-09 21:41:57 +04:00
|
|
|
}
|
|
|
|
$names=array();
|
2012-09-07 17:22:01 +04:00
|
|
|
while($row=$result->fetchRow()) {
|
2012-10-23 02:28:12 +04:00
|
|
|
$names[]=substr($row['path'], $rootLen);
|
2011-11-09 21:41:57 +04:00
|
|
|
}
|
|
|
|
return $names;
|
|
|
|
}
|
2012-02-05 04:25:36 +04:00
|
|
|
|
2012-03-30 21:44:38 +04:00
|
|
|
/**
|
|
|
|
* clean old pre-path_hash entries
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public static function clean() {
|
2012-07-30 22:46:14 +04:00
|
|
|
$query=OC_DB::prepare('DELETE FROM `*PREFIX*fscache` WHERE LENGTH(`path_hash`)<30');
|
2012-03-30 21:44:38 +04:00
|
|
|
$query->execute();
|
|
|
|
}
|
2012-09-07 01:10:24 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* clear filecache entries
|
|
|
|
* @param string user (optonal)
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public static function clear($user='') {
|
|
|
|
if($user) {
|
2012-09-20 23:06:46 +04:00
|
|
|
$query=OC_DB::prepare('DELETE FROM `*PREFIX*fscache` WHERE `user`=?');
|
2012-09-07 01:10:24 +04:00
|
|
|
$query->execute(array($user));
|
|
|
|
}else{
|
|
|
|
$query=OC_DB::prepare('DELETE FROM `*PREFIX*fscache`');
|
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
}
|
2012-09-30 18:12:16 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* trigger an update for the cache by setting the mtimes to 0
|
|
|
|
* @param string $user (optional)
|
|
|
|
*/
|
2012-11-04 13:46:32 +04:00
|
|
|
public static function triggerUpdate($user='') {
|
2012-09-30 18:12:16 +04:00
|
|
|
if($user) {
|
2012-11-06 22:40:45 +04:00
|
|
|
$query=OC_DB::prepare('UPDATE `*PREFIX*fscache` SET `mtime`=0 WHERE `user`=? AND `mimetype`= ? ');
|
|
|
|
$query->execute(array($user,'httpd/unix-directory'));
|
2012-09-30 18:12:16 +04:00
|
|
|
}else{
|
2012-11-06 22:40:45 +04:00
|
|
|
$query=OC_DB::prepare('UPDATE `*PREFIX*fscache` SET `mtime`=0 AND `mimetype`= ? ');
|
|
|
|
$query->execute(array('httpd/unix-directory'));
|
2012-09-30 18:12:16 +04:00
|
|
|
}
|
|
|
|
}
|
2011-11-09 21:41:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//watch for changes and try to keep the cache up to date
|
2012-10-23 02:28:12 +04:00
|
|
|
OC_Hook::connect('OC_Filesystem', 'post_write', 'OC_FileCache_Update', 'fileSystemWatcherWrite');
|
|
|
|
OC_Hook::connect('OC_Filesystem', 'post_delete', 'OC_FileCache_Update', 'fileSystemWatcherDelete');
|
|
|
|
OC_Hook::connect('OC_Filesystem', 'post_rename', 'OC_FileCache_Update', 'fileSystemWatcherRename');
|
|
|
|
OC_Hook::connect('OC_User', 'post_deleteUser', 'OC_FileCache_Update', 'deleteFromUser');
|