2011-11-09 17:41:57 +00: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{
|
|
|
|
/**
|
|
|
|
* get the filesystem info from the cache
|
|
|
|
* @param string path
|
2012-02-02 18:04:13 +00:00
|
|
|
* @param string root (optional)
|
2011-11-09 17:41:57 +00:00
|
|
|
* @return array
|
|
|
|
*
|
2012-06-15 15:05:27 +00:00
|
|
|
* returns an associative array with the following keys:
|
2011-11-09 17:41:57 +00:00
|
|
|
* - size
|
|
|
|
* - mtime
|
|
|
|
* - ctime
|
|
|
|
* - mimetype
|
2011-11-12 00:00:04 +00:00
|
|
|
* - encrypted
|
|
|
|
* - versioned
|
2011-11-09 17:41:57 +00:00
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function get($path,$root=false) {
|
2012-10-22 22:28:12 +00:00
|
|
|
if(OC_FileCache_Update::hasUpdated($path, $root)) {
|
2012-09-07 13:22:01 +00:00
|
|
|
if($root===false) {//filesystem hooks are only valid for the default root
|
2012-10-22 22:28:12 +00:00
|
|
|
OC_Hook::emit('OC_Filesystem', 'post_write', array('path'=>$path));
|
2012-02-05 00:25:36 +00:00
|
|
|
}else{
|
2012-10-22 22:28:12 +00:00
|
|
|
OC_FileCache_Update::update($path, $root);
|
2012-02-05 00:25:36 +00:00
|
|
|
}
|
|
|
|
}
|
2012-10-22 22:28:12 +00:00
|
|
|
return OC_FileCache_Cached::get($path, $root);
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* put filesystem info in the cache
|
|
|
|
* @param string $path
|
|
|
|
* @param array data
|
2012-02-02 18:04:13 +00:00
|
|
|
* @param string root (optional)
|
2011-11-09 17:41:57 +00:00
|
|
|
*
|
|
|
|
* $data is an assiciative array in the same format as returned by get
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function put($path,$data,$root=false) {
|
|
|
|
if($root===false) {
|
2012-02-02 18:04:13 +00:00
|
|
|
$root=OC_Filesystem::getRoot();
|
|
|
|
}
|
2012-10-24 15:23:36 +00:00
|
|
|
$fullpath=OC_Filesystem::normalizePath($root.'/'.$path);
|
2012-08-08 09:47:23 +00:00
|
|
|
$parent=self::getParentId($fullpath);
|
2012-10-22 22:28:12 +00:00
|
|
|
$id=self::getId($fullpath, '');
|
2012-09-07 13:22:01 +00:00
|
|
|
if(isset(OC_FileCache_Cached::$savedData[$fullpath])) {
|
2012-10-22 22:28:12 +00:00
|
|
|
$data=array_merge(OC_FileCache_Cached::$savedData[$fullpath], $data);
|
2012-08-08 08:51:19 +00:00
|
|
|
unset(OC_FileCache_Cached::$savedData[$fullpath]);
|
2012-08-08 09:47:23 +00:00
|
|
|
}
|
2012-09-07 13:22:01 +00:00
|
|
|
if($id!=-1) {
|
2012-10-22 22:28:12 +00:00
|
|
|
self::update($id, $data);
|
2012-06-21 15:37:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-08-29 06:38:33 +00:00
|
|
|
|
2012-08-08 09:29:44 +00:00
|
|
|
// add parent directory to the file cache if it does not exist yet.
|
2012-08-08 08:51:19 +00:00
|
|
|
if ($parent == -1 && $fullpath != $root) {
|
2012-10-09 14:21:46 +00:00
|
|
|
$parentDir = dirname($path);
|
2012-08-08 09:29:44 +00:00
|
|
|
self::scanFile($parentDir);
|
|
|
|
$parent = self::getParentId($fullpath);
|
2012-08-08 08:51:19 +00:00
|
|
|
}
|
2012-08-29 06:38:33 +00:00
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
if(!isset($data['size']) or !isset($data['mtime'])) {//save incomplete data for the next time we write it
|
2012-08-08 08:51:19 +00:00
|
|
|
OC_FileCache_Cached::$savedData[$fullpath]=$data;
|
2012-06-21 16:07:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-09-07 13:22:01 +00:00
|
|
|
if(!isset($data['encrypted'])) {
|
2011-11-12 00:00:04 +00:00
|
|
|
$data['encrypted']=false;
|
|
|
|
}
|
2012-09-07 13:22:01 +00:00
|
|
|
if(!isset($data['versioned'])) {
|
2011-11-12 00:00:04 +00:00
|
|
|
$data['versioned']=false;
|
|
|
|
}
|
2011-11-09 17:41:57 +00:00
|
|
|
$mimePart=dirname($data['mimetype']);
|
2012-05-03 21:16:18 +00: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 15:12:49 +00:00
|
|
|
$user=OC_User::getUser();
|
2012-07-30 18:46:14 +00: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-23 21:01:10 +00: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 13:22:01 +00:00
|
|
|
if(OC_DB::isError($result)) {
|
2012-10-22 22:28:12 +00:00
|
|
|
OC_Log::write('files', 'error while writing file('.$fullpath.') to cache', OC_Log::ERROR);
|
2012-02-25 19:27:16 +00:00
|
|
|
}
|
2012-07-22 01:24:34 +00:00
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
if($cache=OC_Cache::getUserCache(true)) {
|
2012-08-08 08:51:19 +00:00
|
|
|
$cache->remove('fileid/'.$fullpath);//ensure we don't have -1 cached
|
2012-07-22 01:24:34 +00:00
|
|
|
}
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* update filesystem info of a file
|
|
|
|
* @param int $id
|
|
|
|
* @param array $data
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
private static function update($id,$data) {
|
2011-11-12 00:00:04 +00:00
|
|
|
$arguments=array();
|
|
|
|
$queryParts=array();
|
2012-09-07 13:22:01 +00:00
|
|
|
foreach(array('size','mtime','ctime','mimetype','encrypted','versioned','writable') as $attribute) {
|
|
|
|
if(isset($data[$attribute])) {
|
2012-06-22 08:46:49 +00:00
|
|
|
//Convert to int it args are false
|
2012-09-07 13:22:01 +00:00
|
|
|
if($data[$attribute] === false) {
|
2012-06-23 21:32:44 +00:00
|
|
|
$arguments[] = 0;
|
|
|
|
}else{
|
|
|
|
$arguments[] = $data[$attribute];
|
|
|
|
}
|
2012-07-30 18:46:14 +00:00
|
|
|
$queryParts[]='`'.$attribute.'`=?';
|
2011-11-12 00:00:04 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-07 13:22:01 +00:00
|
|
|
if(isset($data['mimetype'])) {
|
2011-11-12 00:00:04 +00:00
|
|
|
$arguments[]=dirname($data['mimetype']);
|
2012-08-01 13:00:07 +00:00
|
|
|
$queryParts[]='`mimepart`=?';
|
2011-11-12 00:00:04 +00:00
|
|
|
}
|
|
|
|
$arguments[]=$id;
|
2012-08-29 06:38:33 +00:00
|
|
|
|
2012-10-22 22:28:12 +00:00
|
|
|
$sql = 'UPDATE `*PREFIX*fscache` SET '.implode(' , ', $queryParts).' WHERE `id`=?';
|
2012-02-10 10:30:38 +00:00
|
|
|
$query=OC_DB::prepare($sql);
|
2012-02-25 19:27:16 +00:00
|
|
|
$result=$query->execute($arguments);
|
2012-09-07 13:22:01 +00:00
|
|
|
if(OC_DB::isError($result)) {
|
2012-10-22 22:28:12 +00:00
|
|
|
OC_Log::write('files', 'error while updating file('.$id.') in cache', OC_Log::ERROR);
|
2012-02-25 19:27:16 +00:00
|
|
|
}
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* register a file move in the cache
|
|
|
|
* @param string oldPath
|
|
|
|
* @param string newPath
|
2012-02-02 18:04:13 +00:00
|
|
|
* @param string root (optional)
|
2011-11-09 17:41:57 +00:00
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function move($oldPath,$newPath,$root=false) {
|
|
|
|
if($root===false) {
|
2012-02-02 18:04:13 +00:00
|
|
|
$root=OC_Filesystem::getRoot();
|
|
|
|
}
|
2012-09-06 02:13:50 +00:00
|
|
|
// If replacing an existing file, delete the file
|
|
|
|
if (self::inCache($newPath, $root)) {
|
|
|
|
self::delete($newPath, $root);
|
|
|
|
}
|
2012-02-02 18:04:13 +00:00
|
|
|
$oldPath=$root.$oldPath;
|
|
|
|
$newPath=$root.$newPath;
|
2011-11-09 17:41:57 +00:00
|
|
|
$newParent=self::getParentId($newPath);
|
2012-07-30 18:46:14 +00:00
|
|
|
$query=OC_DB::prepare('UPDATE `*PREFIX*fscache` SET `parent`=? ,`name`=?, `path`=?, `path_hash`=? WHERE `path_hash`=?');
|
2012-10-23 21:01:10 +00:00
|
|
|
$query->execute(array($newParent, basename($newPath), $newPath, md5($newPath), md5($oldPath)));
|
2012-06-04 15:59:21 +00:00
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
if(($cache=OC_Cache::getUserCache(true)) && $cache->hasKey('fileid/'.$oldPath)) {
|
2012-10-22 22:28:12 +00:00
|
|
|
$cache->set('fileid/'.$newPath, $cache->get('fileid/'.$oldPath));
|
2012-07-22 01:24:34 +00:00
|
|
|
$cache->remove('fileid/'.$oldPath);
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:46:14 +00:00
|
|
|
$query=OC_DB::prepare('SELECT `path` FROM `*PREFIX*fscache` WHERE `path` LIKE ?');
|
2012-06-04 15:59:21 +00:00
|
|
|
$oldLength=strlen($oldPath);
|
2012-07-30 18:46:14 +00:00
|
|
|
$updateQuery=OC_DB::prepare('UPDATE `*PREFIX*fscache` SET `path`=?, `path_hash`=? WHERE `path_hash`=?');
|
2012-09-07 13:22:01 +00:00
|
|
|
while($row= $query->execute(array($oldPath.'/%'))->fetchRow()) {
|
2012-06-04 15:59:21 +00:00
|
|
|
$old=$row['path'];
|
2012-10-22 22:28:12 +00:00
|
|
|
$new=$newPath.substr($old, $oldLength);
|
2012-10-23 21:01:10 +00:00
|
|
|
$updateQuery->execute(array($new, md5($new), md5($old)));
|
2012-07-22 01:24:34 +00:00
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
if(($cache=OC_Cache::getUserCache(true)) && $cache->hasKey('fileid/'.$old)) {
|
2012-10-22 22:28:12 +00:00
|
|
|
$cache->set('fileid/'.$new, $cache->get('fileid/'.$old));
|
2012-07-22 01:24:34 +00:00
|
|
|
$cache->remove('fileid/'.$old);
|
|
|
|
}
|
2012-06-04 15:59:21 +00:00
|
|
|
}
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* delete info from the cache
|
2012-06-15 17:56:15 +00:00
|
|
|
* @param string path
|
2012-02-02 18:04:13 +00:00
|
|
|
* @param string root (optional)
|
2011-11-09 17:41:57 +00:00
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function delete($path,$root=false) {
|
|
|
|
if($root===false) {
|
2012-06-15 17:56:15 +00:00
|
|
|
$root=OC_Filesystem::getRoot();
|
2012-02-05 00:25:36 +00:00
|
|
|
}
|
2012-08-24 22:05:07 +00:00
|
|
|
$query=OC_DB::prepare('DELETE FROM `*PREFIX*fscache` WHERE `path_hash`=?');
|
2012-06-15 17:56:15 +00:00
|
|
|
$query->execute(array(md5($root.$path)));
|
2012-08-29 06:38:33 +00:00
|
|
|
|
2012-06-15 17:56:15 +00:00
|
|
|
//delete everything inside the folder
|
2012-08-24 22:05:07 +00:00
|
|
|
$query=OC_DB::prepare('DELETE FROM `*PREFIX*fscache` WHERE `path` LIKE ?');
|
2012-06-15 17:56:15 +00:00
|
|
|
$query->execute(array($root.$path.'/%'));
|
2012-07-22 01:24:34 +00:00
|
|
|
|
|
|
|
OC_Cache::remove('fileid/'.$root.$path);
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
2012-06-14 14:09:32 +00:00
|
|
|
|
2011-11-09 17:41:57 +00:00
|
|
|
/**
|
|
|
|
* return array of filenames matching the querty
|
|
|
|
* @param string $query
|
2012-01-31 15:12:49 +00:00
|
|
|
* @param boolean $returnData
|
2012-02-02 18:04:13 +00:00
|
|
|
* @param string root (optional)
|
2011-11-09 17:41:57 +00:00
|
|
|
* @return array of filepaths
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function search($search,$returnData=false,$root=false) {
|
|
|
|
if($root===false) {
|
2012-02-02 18:04:13 +00:00
|
|
|
$root=OC_Filesystem::getRoot();
|
|
|
|
}
|
2012-01-31 15:12:49 +00:00
|
|
|
$rootLen=strlen($root);
|
2012-09-07 13:22:01 +00:00
|
|
|
if(!$returnData) {
|
2012-10-17 16:17:20 +00:00
|
|
|
$select = '`path`';
|
2012-01-31 15:12:49 +00:00
|
|
|
}else{
|
2012-10-17 16:17:20 +00:00
|
|
|
$select = '*';
|
2012-01-31 15:12:49 +00:00
|
|
|
}
|
2012-10-17 16:17:20 +00: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-01-31 15:12:49 +00:00
|
|
|
$result=$query->execute(array("%$search%",OC_User::getUser()));
|
2011-11-09 17:41:57 +00:00
|
|
|
$names=array();
|
2012-09-07 13:22:01 +00:00
|
|
|
while($row=$result->fetchRow()) {
|
|
|
|
if(!$returnData) {
|
2012-10-22 22:28:12 +00:00
|
|
|
$names[]=substr($row['path'], $rootLen);
|
2012-01-31 15:12:49 +00:00
|
|
|
}else{
|
2012-10-22 22:28:12 +00:00
|
|
|
$row['path']=substr($row['path'], $rootLen);
|
2012-01-31 15:12:49 +00:00
|
|
|
$names[]=$row;
|
|
|
|
}
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
|
|
|
return $names;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get all files and folders in a folder
|
|
|
|
* @param string path
|
2012-02-02 18:04:13 +00:00
|
|
|
* @param string root (optional)
|
2011-11-09 17:41:57 +00:00
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
* returns an array of assiciative arrays with the following keys:
|
|
|
|
* - name
|
|
|
|
* - size
|
|
|
|
* - mtime
|
|
|
|
* - ctime
|
|
|
|
* - mimetype
|
2011-11-12 00:00:04 +00:00
|
|
|
* - encrypted
|
|
|
|
* - versioned
|
2011-11-09 17:41:57 +00:00
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function getFolderContent($path,$root=false,$mimetype_filter='') {
|
2012-10-22 22:28:12 +00:00
|
|
|
if(OC_FileCache_Update::hasUpdated($path, $root, true)) {
|
|
|
|
OC_FileCache_Update::updateFolder($path, $root);
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
2012-10-22 22:28:12 +00:00
|
|
|
return OC_FileCache_Cached::getFolderContent($path, $root, $mimetype_filter);
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if a file or folder is in the cache
|
|
|
|
* @param string $path
|
2012-02-02 18:04:13 +00:00
|
|
|
* @param string root (optional)
|
2011-11-09 17:41:57 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function inCache($path,$root=false) {
|
2012-10-22 22:28:12 +00:00
|
|
|
return self::getId($path, $root)!=-1;
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the file id as used in the cache
|
2012-06-15 17:56:15 +00:00
|
|
|
* @param string path
|
|
|
|
* @param string root (optional)
|
2011-11-09 17:41:57 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function getId($path,$root=false) {
|
|
|
|
if($root===false) {
|
2012-06-15 17:56:15 +00:00
|
|
|
$root=OC_Filesystem::getRoot();
|
2012-02-25 19:27:16 +00:00
|
|
|
}
|
2012-07-22 01:24:34 +00:00
|
|
|
|
|
|
|
$fullPath=$root.$path;
|
2012-09-07 13:22:01 +00:00
|
|
|
if(($cache=OC_Cache::getUserCache(true)) && $cache->hasKey('fileid/'.$fullPath)) {
|
2012-07-22 01:24:34 +00:00
|
|
|
return $cache->get('fileid/'.$fullPath);
|
2012-02-25 19:27:16 +00:00
|
|
|
}
|
2012-08-29 06:38:33 +00:00
|
|
|
|
2012-08-24 22:05:07 +00:00
|
|
|
$query=OC_DB::prepare('SELECT `id` FROM `*PREFIX*fscache` WHERE `path_hash`=?');
|
2012-07-22 01:24:34 +00:00
|
|
|
$result=$query->execute(array(md5($fullPath)));
|
2012-09-07 13:22:01 +00:00
|
|
|
if(OC_DB::isError($result)) {
|
2012-10-22 22:28:12 +00:00
|
|
|
OC_Log::write('files', 'error while getting file id of '.$path, OC_Log::ERROR);
|
2012-02-25 19:27:16 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2012-08-29 06:38:33 +00:00
|
|
|
|
2012-02-25 19:27:16 +00:00
|
|
|
$result=$result->fetchRow();
|
2012-09-07 13:22:01 +00:00
|
|
|
if(is_array($result)) {
|
2012-07-22 01:24:34 +00:00
|
|
|
$id=$result['id'];
|
2011-11-09 17:41:57 +00:00
|
|
|
}else{
|
2012-07-22 01:24:34 +00:00
|
|
|
$id=-1;
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
2012-09-07 13:22:01 +00:00
|
|
|
if($cache=OC_Cache::getUserCache(true)) {
|
2012-10-22 22:28:12 +00:00
|
|
|
$cache->set('fileid/'.$fullPath, $id);
|
2012-04-15 09:44:04 +00:00
|
|
|
}
|
2012-08-29 06:38:33 +00:00
|
|
|
|
2012-07-22 01:24:34 +00:00
|
|
|
return $id;
|
2012-04-15 09:44:04 +00:00
|
|
|
}
|
2012-08-29 06:38:33 +00:00
|
|
|
|
2012-04-15 09:44:04 +00: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-09-07 13:22:01 +00:00
|
|
|
public static function getPath($id,$user='') {
|
|
|
|
if(!$user) {
|
2012-04-15 09:44:04 +00:00
|
|
|
$user=OC_User::getUser();
|
|
|
|
}
|
2012-07-30 18:46:14 +00:00
|
|
|
$query=OC_DB::prepare('SELECT `path` FROM `*PREFIX*fscache` WHERE `id`=? AND `user`=?');
|
2012-10-22 22:28:12 +00:00
|
|
|
$result=$query->execute(array($id, $user));
|
2012-04-15 09:44:04 +00:00
|
|
|
$row=$result->fetchRow();
|
2012-04-16 08:06:52 +00:00
|
|
|
$path=$row['path'];
|
|
|
|
$root='/'.$user.'/files';
|
2012-10-22 22:28:12 +00:00
|
|
|
if(substr($path, 0, strlen($root))!=$root) {
|
2012-04-16 08:06:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-22 22:28:12 +00:00
|
|
|
return substr($path, strlen($root));
|
2012-04-15 09:44:04 +00:00
|
|
|
}
|
2011-11-09 17:41:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* get the file id of the parent folder, taking into account '/' has no parent
|
|
|
|
* @param string $path
|
|
|
|
* @return int
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
private static function getParentId($path) {
|
|
|
|
if($path=='/') {
|
2011-11-09 17:41:57 +00:00
|
|
|
return -1;
|
|
|
|
}else{
|
2012-10-22 22:28:12 +00:00
|
|
|
return self::getId(dirname($path), '');
|
2012-02-05 00:25:36 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-29 06:38:33 +00:00
|
|
|
|
2011-11-09 17:41:57 +00:00
|
|
|
/**
|
|
|
|
* adjust the size of the parent folders
|
|
|
|
* @param string $path
|
|
|
|
* @param int $sizeDiff
|
2012-06-15 17:56:15 +00:00
|
|
|
* @param string root (optinal)
|
2011-11-09 17:41:57 +00:00
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function increaseSize($path,$sizeDiff, $root=false) {
|
2012-02-05 00:25:36 +00:00
|
|
|
if($sizeDiff==0) return;
|
2012-10-22 22:28:12 +00:00
|
|
|
$id=self::getId($path, $root);
|
2012-09-07 13:22:01 +00:00
|
|
|
while($id!=-1) {//walk up the filetree increasing the size of all parent folders
|
2012-07-30 18:46:14 +00:00
|
|
|
$query=OC_DB::prepare('UPDATE `*PREFIX*fscache` SET `size`=`size`+? WHERE `id`=?');
|
2011-11-09 17:41:57 +00:00
|
|
|
$query->execute(array($sizeDiff,$id));
|
2012-06-15 17:56:15 +00:00
|
|
|
$id=self::getParentId($path);
|
2011-11-09 17:41:57 +00:00
|
|
|
$path=dirname($path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* recursively scan the filesystem and fill the cache
|
|
|
|
* @param string $path
|
2012-02-02 18:04:13 +00:00
|
|
|
* @param OC_EventSource $enventSource (optional)
|
|
|
|
* @param int count (optional)
|
2012-08-08 08:51:19 +00:00
|
|
|
* @param string root (optional)
|
2011-11-09 17:41:57 +00:00
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function scan($path,$eventSource=false,&$count=0,$root=false) {
|
|
|
|
if($eventSource) {
|
2012-10-22 22:28:12 +00:00
|
|
|
$eventSource->send('scanning', array('file'=>$path, 'count'=>$count));
|
2012-03-31 14:24:53 +00:00
|
|
|
}
|
|
|
|
$lastSend=$count;
|
2012-07-10 22:56:22 +00: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 13:22:01 +00:00
|
|
|
if($root===false) {
|
2012-02-05 00:25:36 +00:00
|
|
|
$view=OC_Filesystem::getView();
|
|
|
|
}else{
|
2012-06-15 17:56:15 +00:00
|
|
|
$view=new OC_FilesystemView($root);
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
2012-10-22 22:28:12 +00:00
|
|
|
self::scanFile($path, $root);
|
2012-04-29 14:11:17 +00:00
|
|
|
$dh=$view->opendir($path.'/');
|
|
|
|
$totalSize=0;
|
2012-09-07 13:22:01 +00:00
|
|
|
if($dh) {
|
2012-04-29 14:11:17 +00:00
|
|
|
while (($filename = readdir($dh)) !== false) {
|
2012-09-07 13:22:01 +00:00
|
|
|
if($filename != '.' and $filename != '..') {
|
2012-04-29 14:11:17 +00:00
|
|
|
$file=$path.'/'.$filename;
|
2012-09-07 13:22:01 +00:00
|
|
|
if($view->is_dir($file.'/')) {
|
2012-10-22 22:28:12 +00:00
|
|
|
self::scan($file, $eventSource, $count, $root);
|
2012-04-29 14:11:17 +00:00
|
|
|
}else{
|
2012-10-22 22:28:12 +00:00
|
|
|
$totalSize+=self::scanFile($file, $root);
|
2012-04-29 14:11:17 +00:00
|
|
|
$count++;
|
2012-09-07 13:22:01 +00:00
|
|
|
if($count>$lastSend+25 and $eventSource) {
|
2012-04-29 14:11:17 +00:00
|
|
|
$lastSend=$count;
|
2012-10-22 22:28:12 +00:00
|
|
|
$eventSource->send('scanning', array('file'=>$path, 'count'=>$count));
|
2012-03-31 14:24:53 +00:00
|
|
|
}
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-29 06:38:33 +00:00
|
|
|
|
2012-06-15 17:56:15 +00:00
|
|
|
OC_FileCache_Update::cleanFolder($path,$root);
|
|
|
|
self::increaseSize($path,$totalSize,$root);
|
2012-02-05 00:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* scan a single file
|
|
|
|
* @param string path
|
|
|
|
* @param string root (optional)
|
|
|
|
* @return int size of the scanned file
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function scanFile($path,$root=false) {
|
2012-07-10 22:56:22 +00: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 13:22:01 +00:00
|
|
|
if($root===false) {
|
2012-02-05 00:25:36 +00:00
|
|
|
$view=OC_Filesystem::getView();
|
|
|
|
}else{
|
2012-06-15 17:56:15 +00:00
|
|
|
$view=new OC_FilesystemView($root);
|
2012-02-05 00:25:36 +00:00
|
|
|
}
|
|
|
|
if(!$view->is_readable($path)) return; //cant read, nothing we can do
|
2012-04-24 22:10:02 +00:00
|
|
|
clearstatcache();
|
2012-02-05 00:25:36 +00:00
|
|
|
$mimetype=$view->getMimeType($path);
|
2012-05-16 23:47:58 +00:00
|
|
|
$stat=$view->stat($path);
|
2012-09-07 13:22:01 +00:00
|
|
|
if($mimetype=='httpd/unix-directory') {
|
2012-10-05 20:35:51 +00:00
|
|
|
$stat['size'] = 0;
|
2012-05-16 23:47:58 +00:00
|
|
|
$writable=$view->is_writable($path.'/');
|
|
|
|
}else{
|
|
|
|
$writable=$view->is_writable($path);
|
|
|
|
}
|
2012-02-05 00:25:36 +00:00
|
|
|
$stat['mimetype']=$mimetype;
|
2012-02-05 13:04:10 +00:00
|
|
|
$stat['writable']=$writable;
|
2012-09-07 13:22:01 +00:00
|
|
|
if($path=='/') {
|
2012-02-05 00:25:36 +00:00
|
|
|
$path='';
|
|
|
|
}
|
2012-10-22 22:28:12 +00:00
|
|
|
self::put($path, $stat, $root);
|
2012-02-05 00:25:36 +00:00
|
|
|
return $stat['size'];
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-03-14 23:09:06 +00:00
|
|
|
* find files by mimetype
|
2011-11-09 17:41:57 +00:00
|
|
|
* @param string $part1
|
|
|
|
* @param string $part2 (optional)
|
2012-02-08 16:29:54 +00:00
|
|
|
* @param string root (optional)
|
2011-11-09 17:41:57 +00:00
|
|
|
* @return array of file paths
|
|
|
|
*
|
|
|
|
* $part1 and $part2 together form the complete mimetype.
|
|
|
|
* e.g. searchByMime('text','plain')
|
|
|
|
*
|
|
|
|
* seccond mimetype part can be ommited
|
|
|
|
* e.g. searchByMime('audio')
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function searchByMime($part1,$part2=null,$root=false) {
|
|
|
|
if($root===false) {
|
2012-02-08 16:29:54 +00:00
|
|
|
$root=OC_Filesystem::getRoot();
|
|
|
|
}
|
2012-03-09 15:47:53 +00:00
|
|
|
$rootLen=strlen($root);
|
|
|
|
$root .= '%';
|
2012-02-08 16:29:54 +00:00
|
|
|
$user=OC_User::getUser();
|
2012-09-07 13:22:01 +00:00
|
|
|
if(!$part2) {
|
2012-07-30 18:46:14 +00:00
|
|
|
$query=OC_DB::prepare('SELECT `path` FROM `*PREFIX*fscache` WHERE `mimepart`=? AND `user`=? AND `path` LIKE ?');
|
2012-10-22 22:28:12 +00:00
|
|
|
$result=$query->execute(array($part1, $user, $root));
|
2011-11-09 17:41:57 +00:00
|
|
|
}else{
|
2012-07-30 18:46:14 +00:00
|
|
|
$query=OC_DB::prepare('SELECT `path` FROM `*PREFIX*fscache` WHERE `mimetype`=? AND `user`=? AND `path` LIKE ? ');
|
2012-10-22 22:28:12 +00:00
|
|
|
$result=$query->execute(array($part1.'/'.$part2, $user, $root));
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
|
|
|
$names=array();
|
2012-09-07 13:22:01 +00:00
|
|
|
while($row=$result->fetchRow()) {
|
2012-10-22 22:28:12 +00:00
|
|
|
$names[]=substr($row['path'], $rootLen);
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
|
|
|
return $names;
|
|
|
|
}
|
2012-02-05 00:25:36 +00:00
|
|
|
|
2012-03-30 17:44:38 +00:00
|
|
|
/**
|
|
|
|
* clean old pre-path_hash entries
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function clean() {
|
2012-07-30 18:46:14 +00:00
|
|
|
$query=OC_DB::prepare('DELETE FROM `*PREFIX*fscache` WHERE LENGTH(`path_hash`)<30');
|
2012-03-30 17:44:38 +00:00
|
|
|
$query->execute();
|
|
|
|
}
|
2012-09-06 21:10:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* clear filecache entries
|
|
|
|
* @param string user (optonal)
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function clear($user='') {
|
|
|
|
if($user) {
|
2012-09-20 19:06:46 +00:00
|
|
|
$query=OC_DB::prepare('DELETE FROM `*PREFIX*fscache` WHERE `user`=?');
|
2012-09-06 21:10:24 +00:00
|
|
|
$query->execute(array($user));
|
|
|
|
}else{
|
|
|
|
$query=OC_DB::prepare('DELETE FROM `*PREFIX*fscache`');
|
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
}
|
2012-09-30 14:12:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* trigger an update for the cache by setting the mtimes to 0
|
|
|
|
* @param string $user (optional)
|
|
|
|
*/
|
|
|
|
public static function triggerUpdate($user=''){
|
|
|
|
if($user) {
|
|
|
|
$query=OC_DB::prepare('UPDATE `*PREFIX*fscache` SET `mtime`=0 WHERE `user`=? AND `mimetype`="httpd/unix-directory"');
|
|
|
|
$query->execute(array($user));
|
|
|
|
}else{
|
|
|
|
$query=OC_DB::prepare('UPDATE `*PREFIX*fscache` SET `mtime`=0 AND `mimetype`="httpd/unix-directory"');
|
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
}
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//watch for changes and try to keep the cache up to date
|
2012-10-22 22:28:12 +00: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');
|