mirror of
https://github.com/nextcloud/server.git
synced 2026-02-11 14:54:02 -05:00
Conflicts: apps/files/css/files.css apps/files/index.php apps/files/js/files.js apps/files/js/keyboardshortcuts.js apps/files/l10n/da.php apps/files/l10n/el.php apps/files/l10n/eu.php apps/files/l10n/hu_HU.php apps/files/l10n/mk.php apps/files/l10n/nb_NO.php apps/files/l10n/ro.php apps/files/l10n/ru.php apps/files/l10n/ru_RU.php apps/files/l10n/th_TH.php apps/files/l10n/tr.php apps/files/templates/admin.php apps/files/templates/index.php apps/files/templates/part.breadcrumb.php apps/files_encryption/appinfo/app.php apps/files_encryption/lib/crypt.php apps/files_encryption/lib/cryptstream.php apps/files_encryption/lib/proxy.php apps/files_encryption/settings.php apps/files_encryption/templates/settings.php apps/files_encryption/tests/proxy.php apps/files_encryption/tests/stream.php apps/files_external/l10n/gl.php apps/files_external/l10n/uk.php apps/files_external/personal.php apps/files_external/settings.php apps/files_external/templates/settings.php apps/files_sharing/public.php apps/user_ldap/l10n/gl.php apps/user_ldap/l10n/uk.php apps/user_ldap/lib/access.php apps/user_webdavauth/l10n/eo.php apps/user_webdavauth/l10n/eu.php apps/user_webdavauth/l10n/fr.php apps/user_webdavauth/l10n/gl.php apps/user_webdavauth/l10n/sl.php apps/user_webdavauth/l10n/tr.php apps/user_webdavauth/l10n/uk.php apps/user_webdavauth/l10n/zh_CN.php core/ajax/share.php core/css/styles.css core/l10n/ca.php core/l10n/de.php core/l10n/de_DE.php core/l10n/el.php core/l10n/eo.php core/l10n/es.php core/l10n/eu.php core/l10n/fr.php core/l10n/gl.php core/l10n/he.php core/l10n/is.php core/l10n/it.php core/l10n/nl.php core/l10n/pl.php core/l10n/pt_PT.php core/l10n/ru.php core/l10n/sl.php core/l10n/tr.php core/l10n/uk.php core/l10n/zh_CN.php core/templates/installation.php core/templates/login.php l10n/ar/files_external.po l10n/ar/settings.po l10n/bg_BG/files_external.po l10n/bg_BG/settings.po l10n/ca/core.po l10n/ca/files_external.po l10n/ca/settings.po l10n/cs_CZ/files_external.po l10n/cs_CZ/settings.po l10n/da/files.po l10n/da/files_external.po l10n/da/settings.po l10n/de/core.po l10n/de/files_external.po l10n/de/settings.po l10n/de_DE/core.po l10n/de_DE/files_external.po l10n/de_DE/settings.po l10n/el/core.po l10n/el/files.po l10n/el/files_external.po l10n/el/settings.po l10n/eo/core.po l10n/eo/files_external.po l10n/eo/settings.po l10n/eo/user_webdavauth.po l10n/es/core.po l10n/es/files_external.po l10n/es/settings.po l10n/es_AR/core.po l10n/es_AR/files_external.po l10n/es_AR/settings.po l10n/et_EE/files_external.po l10n/et_EE/settings.po l10n/et_EE/user_webdavauth.po l10n/eu/core.po l10n/eu/files.po l10n/eu/files_external.po l10n/eu/settings.po l10n/eu/user_webdavauth.po l10n/fa/files_external.po l10n/fa/settings.po l10n/fi_FI/files_external.po l10n/fi_FI/settings.po l10n/fr/core.po l10n/fr/files_external.po l10n/fr/settings.po l10n/fr/user_webdavauth.po l10n/gl/core.po l10n/gl/files_external.po l10n/gl/settings.po l10n/gl/user_ldap.po l10n/gl/user_webdavauth.po l10n/he/core.po l10n/he/files_external.po l10n/he/settings.po l10n/hi/files_external.po l10n/hi/settings.po l10n/hr/files_external.po l10n/hr/settings.po l10n/hu_HU/files.po l10n/hu_HU/files_external.po l10n/hu_HU/settings.po l10n/ia/files_external.po l10n/ia/settings.po l10n/id/files_external.po l10n/id/settings.po l10n/is/core.po l10n/is/files.po l10n/is/files_encryption.po l10n/is/files_external.po l10n/is/files_sharing.po l10n/is/files_versions.po l10n/is/lib.po l10n/is/settings.po l10n/is/user_ldap.po l10n/is/user_webdavauth.po l10n/it/core.po l10n/it/files_external.po l10n/it/settings.po l10n/ja_JP/core.po l10n/ja_JP/files_external.po l10n/ja_JP/settings.po l10n/ka_GE/files_external.po l10n/ka_GE/settings.po l10n/ko/core.po l10n/ko/files_external.po l10n/ko/settings.po l10n/ko/user_ldap.po l10n/ko/user_webdavauth.po l10n/ku_IQ/files_external.po l10n/ku_IQ/settings.po l10n/lb/files_external.po l10n/lb/settings.po l10n/lt_LT/files_external.po l10n/lt_LT/settings.po l10n/lv/files_external.po l10n/lv/settings.po l10n/mk/files.po l10n/mk/files_external.po l10n/mk/settings.po l10n/ms_MY/files_external.po l10n/ms_MY/settings.po l10n/nb_NO/files.po l10n/nb_NO/files_external.po l10n/nb_NO/settings.po l10n/nl/core.po l10n/nl/files_external.po l10n/nl/settings.po l10n/nn_NO/files_external.po l10n/nn_NO/settings.po l10n/oc/files_external.po l10n/oc/settings.po l10n/pl/core.po l10n/pl/files_external.po l10n/pl/settings.po l10n/pl_PL/files_external.po l10n/pl_PL/settings.po l10n/pt_BR/core.po l10n/pt_BR/files_external.po l10n/pt_BR/settings.po l10n/pt_PT/core.po l10n/pt_PT/files_external.po l10n/pt_PT/settings.po l10n/ro/files.po l10n/ro/files_external.po l10n/ro/settings.po l10n/ru/core.po l10n/ru/files.po l10n/ru/files_external.po l10n/ru/settings.po l10n/ru_RU/files.po l10n/ru_RU/files_external.po l10n/ru_RU/settings.po l10n/si_LK/files_external.po l10n/si_LK/settings.po l10n/sk_SK/core.po l10n/sk_SK/files_external.po l10n/sk_SK/settings.po l10n/sk_SK/user_webdavauth.po l10n/sl/core.po l10n/sl/files_external.po l10n/sl/settings.po l10n/sl/user_webdavauth.po l10n/sq/core.po l10n/sq/files_external.po l10n/sq/settings.po l10n/sq/user_ldap.po l10n/sq/user_webdavauth.po l10n/sr/core.po l10n/sr/files_external.po l10n/sr/settings.po l10n/sr@latin/files_external.po l10n/sr@latin/settings.po l10n/sv/files_external.po l10n/sv/settings.po l10n/ta_LK/files_external.po l10n/ta_LK/settings.po l10n/ta_LK/user_ldap.po l10n/ta_LK/user_webdavauth.po l10n/templates/core.pot l10n/templates/files.pot l10n/templates/files_encryption.pot l10n/templates/files_external.pot l10n/templates/files_sharing.pot l10n/templates/files_versions.pot l10n/templates/lib.pot l10n/templates/settings.pot l10n/templates/user_ldap.pot l10n/templates/user_webdavauth.pot l10n/th_TH/core.po l10n/th_TH/files.po l10n/th_TH/files_external.po l10n/th_TH/settings.po l10n/th_TH/user_webdavauth.po l10n/tr/core.po l10n/tr/files.po l10n/tr/files_external.po l10n/tr/settings.po l10n/tr/user_webdavauth.po l10n/uk/core.po l10n/uk/files_external.po l10n/uk/settings.po l10n/uk/user_ldap.po l10n/uk/user_webdavauth.po l10n/vi/core.po l10n/vi/files_external.po l10n/vi/settings.po l10n/zh_CN.GB2312/files_external.po l10n/zh_CN.GB2312/settings.po l10n/zh_CN/core.po l10n/zh_CN/files_external.po l10n/zh_CN/settings.po l10n/zh_CN/user_webdavauth.po l10n/zh_HK/core.po l10n/zh_HK/files_external.po l10n/zh_HK/settings.po l10n/zh_HK/user_ldap.po l10n/zh_HK/user_webdavauth.po l10n/zh_TW/core.po l10n/zh_TW/files_external.po l10n/zh_TW/settings.po l10n/zh_TW/user_ldap.po l10n/zh_TW/user_webdavauth.po l10n/zu_ZA/files_external.po l10n/zu_ZA/settings.po lib/base.php lib/filecache.php lib/files.php lib/helper.php lib/l10n.php lib/request.php lib/util.php settings/ajax/togglegroups.php settings/js/users.js settings/l10n/ar.php settings/l10n/de_DE.php settings/l10n/el.php settings/l10n/eo.php settings/l10n/es_AR.php settings/l10n/eu.php settings/l10n/fa.php settings/l10n/gl.php settings/l10n/he.php settings/l10n/hi.php settings/l10n/ko.php settings/l10n/si_LK.php settings/l10n/sk_SK.php settings/l10n/sl.php settings/l10n/sr.php settings/l10n/ta_LK.php settings/l10n/th_TH.php settings/l10n/tr.php settings/l10n/uk.php settings/l10n/vi.php settings/l10n/zh_CN.php settings/l10n/zh_TW.php settings/templates/help.php
540 lines
16 KiB
PHP
540 lines
16 KiB
PHP
<?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,
|
|
* instead 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
|
|
*
|
|
* Methods that take $path and $root params expect $path to be relative, like
|
|
* /admin/files/file.txt, if $root is false
|
|
*
|
|
*/
|
|
class OC_FileCache{
|
|
|
|
/**
|
|
* get the filesystem info from the cache
|
|
* @param string path
|
|
* @param string root (optional)
|
|
* @return array
|
|
*
|
|
* returns an associative array with the following keys:
|
|
* - size
|
|
* - mtime
|
|
* - ctime
|
|
* - mimetype
|
|
* - encrypted
|
|
* - versioned
|
|
*/
|
|
public static function get($path, $root=false) {
|
|
if(OC_FileCache_Update::hasUpdated($path, $root)) {
|
|
if($root===false) {//filesystem hooks are only valid for the default root
|
|
OC_Hook::emit('OC_Filesystem', 'post_write', array('path'=>$path));
|
|
}else{
|
|
OC_FileCache_Update::update($path, $root);
|
|
}
|
|
}
|
|
return OC_FileCache_Cached::get($path, $root);
|
|
}
|
|
|
|
/**
|
|
* put filesystem info in the cache
|
|
* @param string $path
|
|
* @param array data
|
|
* @param string root (optional)
|
|
* @note $data is an associative array in the same format as returned
|
|
* by get
|
|
*/
|
|
public static function put($path, $data, $root=false) {
|
|
if($root===false) {
|
|
$root=OC_Filesystem::getRoot();
|
|
}
|
|
$fullpath=OC_Filesystem::normalizePath($root.'/'.$path);
|
|
$parent=self::getParentId($fullpath);
|
|
$id=self::getId($fullpath, '');
|
|
if(isset(OC_FileCache_Cached::$savedData[$fullpath])) {
|
|
$data=array_merge(OC_FileCache_Cached::$savedData[$fullpath], $data);
|
|
unset(OC_FileCache_Cached::$savedData[$fullpath]);
|
|
}
|
|
if($id!=-1) {
|
|
self::update($id, $data);
|
|
return;
|
|
}
|
|
|
|
// add parent directory to the file cache if it does not exist yet.
|
|
if ($parent == -1 && $fullpath != $root) {
|
|
$parentDir = dirname($path);
|
|
self::scanFile($parentDir);
|
|
$parent = self::getParentId($fullpath);
|
|
}
|
|
|
|
if(!isset($data['size']) or !isset($data['mtime'])) {//save incomplete data for the next time we write it
|
|
OC_FileCache_Cached::$savedData[$fullpath]=$data;
|
|
return;
|
|
}
|
|
if(!isset($data['encrypted'])) {
|
|
$data['encrypted']=false;
|
|
}
|
|
if(!isset($data['versioned'])) {
|
|
$data['versioned']=false;
|
|
}
|
|
$mimePart=dirname($data['mimetype']);
|
|
$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'];
|
|
$user=OC_User::getUser();
|
|
$query=OC_DB::prepare('INSERT INTO `*PREFIX*fscache`(`parent`, `name`, `path`, `path_hash`, `size`, `mtime`, `ctime`, `mimetype`, `mimepart`,`user`,`writable`,`encrypted`,`versioned`) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)');
|
|
$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']));
|
|
if(OC_DB::isError($result)) {
|
|
OC_Log::write('files', 'error while writing file('.$fullpath.') to cache', OC_Log::ERROR);
|
|
}
|
|
|
|
if($cache=OC_Cache::getUserCache(true)) {
|
|
$cache->remove('fileid/'.$fullpath);//ensure we don't have -1 cached
|
|
}
|
|
}
|
|
|
|
/**
|
|
* update filesystem info of a file
|
|
* @param int $id
|
|
* @param array $data
|
|
*/
|
|
private static function update($id, $data) {
|
|
$arguments=array();
|
|
$queryParts=array();
|
|
foreach(array('size','mtime','ctime','mimetype','encrypted','versioned', 'writable') as $attribute) {
|
|
if(isset($data[$attribute])) {
|
|
//Convert to int it args are false
|
|
if($data[$attribute] === false) {
|
|
$arguments[] = 0;
|
|
}else{
|
|
$arguments[] = $data[$attribute];
|
|
}
|
|
$queryParts[]='`'.$attribute.'`=?';
|
|
}
|
|
}
|
|
if(isset($data['mimetype'])) {
|
|
$arguments[]=dirname($data['mimetype']);
|
|
$queryParts[]='`mimepart`=?';
|
|
}
|
|
$arguments[]=$id;
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* register a file move in the cache
|
|
* @param string oldPath
|
|
* @param string newPath
|
|
* @param string root (optional)
|
|
*/
|
|
public static function move($oldPath, $newPath, $root=false) {
|
|
if($root===false) {
|
|
$root=OC_Filesystem::getRoot();
|
|
}
|
|
// If replacing an existing file, delete the file
|
|
if (self::inCache($newPath, $root)) {
|
|
self::delete($newPath, $root);
|
|
}
|
|
$oldPath=$root.$oldPath;
|
|
$newPath=$root.$newPath;
|
|
$newParent=self::getParentId($newPath);
|
|
$query=OC_DB::prepare('UPDATE `*PREFIX*fscache` SET `parent`=? ,`name`=?, `path`=?, `path_hash`=? WHERE `path_hash`=?');
|
|
$query->execute(array($newParent, basename($newPath), $newPath, md5($newPath), md5($oldPath)));
|
|
|
|
if(($cache=OC_Cache::getUserCache(true)) && $cache->hasKey('fileid/'.$oldPath)) {
|
|
$cache->set('fileid/'.$newPath, $cache->get('fileid/'.$oldPath));
|
|
$cache->remove('fileid/'.$oldPath);
|
|
}
|
|
|
|
$query=OC_DB::prepare('SELECT `path` FROM `*PREFIX*fscache` WHERE `path` LIKE ?');
|
|
$oldLength=strlen($oldPath);
|
|
$updateQuery=OC_DB::prepare('UPDATE `*PREFIX*fscache` SET `path`=?, `path_hash`=? WHERE `path_hash`=?');
|
|
while($row= $query->execute(array($oldPath.'/%'))->fetchRow()) {
|
|
$old=$row['path'];
|
|
$new=$newPath.substr($old, $oldLength);
|
|
$updateQuery->execute(array($new, md5($new), md5($old)));
|
|
|
|
if(($cache=OC_Cache::getUserCache(true)) && $cache->hasKey('fileid/'.$old)) {
|
|
$cache->set('fileid/'.$new, $cache->get('fileid/'.$old));
|
|
$cache->remove('fileid/'.$old);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* delete info from the cache
|
|
* @param string path
|
|
* @param string root (optional)
|
|
*/
|
|
public static function delete($path, $root=false) {
|
|
if($root===false) {
|
|
$root=OC_Filesystem::getRoot();
|
|
}
|
|
$query=OC_DB::prepare('DELETE FROM `*PREFIX*fscache` WHERE `path_hash`=?');
|
|
$query->execute(array(md5($root.$path)));
|
|
|
|
//delete everything inside the folder
|
|
$query=OC_DB::prepare('DELETE FROM `*PREFIX*fscache` WHERE `path` LIKE ?');
|
|
$query->execute(array($root.$path.'/%'));
|
|
|
|
OC_Cache::remove('fileid/'.$root.$path);
|
|
}
|
|
|
|
/**
|
|
* return array of filenames matching the querty
|
|
* @param string $query
|
|
* @param boolean $returnData
|
|
* @param string root (optional)
|
|
* @return array of filepaths
|
|
*/
|
|
public static function search($search, $returnData=false, $root=false) {
|
|
if($root===false) {
|
|
$root=OC_Filesystem::getRoot();
|
|
}
|
|
$rootLen=strlen($root);
|
|
if(!$returnData) {
|
|
$select = '`path`';
|
|
}else{
|
|
$select = '*';
|
|
}
|
|
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);
|
|
$result=$query->execute(array("%$search%", OC_User::getUser()));
|
|
$names=array();
|
|
while($row=$result->fetchRow()) {
|
|
if(!$returnData) {
|
|
$names[]=substr($row['path'], $rootLen);
|
|
}else{
|
|
$row['path']=substr($row['path'], $rootLen);
|
|
$names[]=$row;
|
|
}
|
|
}
|
|
return $names;
|
|
}
|
|
|
|
/**
|
|
* get all files and folders in a folder
|
|
* @param string path
|
|
* @param string root (optional)
|
|
* @return array
|
|
*
|
|
* returns an array of assiciative arrays with the following keys:
|
|
* - name
|
|
* - size
|
|
* - mtime
|
|
* - ctime
|
|
* - mimetype
|
|
* - encrypted
|
|
* - versioned
|
|
*/
|
|
public static function getFolderContent($path, $root=false, $mimetype_filter='') {
|
|
if(OC_FileCache_Update::hasUpdated($path, $root, true)) {
|
|
OC_FileCache_Update::updateFolder($path, $root);
|
|
}
|
|
return OC_FileCache_Cached::getFolderContent($path, $root, $mimetype_filter);
|
|
}
|
|
|
|
/**
|
|
* check if a file or folder is in the cache
|
|
* @param string $path
|
|
* @param string root (optional)
|
|
* @return bool
|
|
*/
|
|
public static function inCache($path, $root=false) {
|
|
return self::getId($path, $root)!=-1;
|
|
}
|
|
|
|
/**
|
|
* get the file id as used in the cache
|
|
* @param string path
|
|
* @param string root (optional)
|
|
* @return int
|
|
*/
|
|
public static function getId($path, $root=false) {
|
|
if($root===false) {
|
|
$root=OC_Filesystem::getRoot();
|
|
}
|
|
|
|
$fullPath=$root.$path;
|
|
if(($cache=OC_Cache::getUserCache(true)) && $cache->hasKey('fileid/'.$fullPath)) {
|
|
return $cache->get('fileid/'.$fullPath);
|
|
}
|
|
|
|
$query=OC_DB::prepare('SELECT `id` FROM `*PREFIX*fscache` WHERE `path_hash`=?');
|
|
$result=$query->execute(array(md5($fullPath)));
|
|
if(OC_DB::isError($result)) {
|
|
OC_Log::write('files', 'error while getting file id of '.$path, OC_Log::ERROR);
|
|
return -1;
|
|
}
|
|
|
|
$result=$result->fetchRow();
|
|
if(is_array($result)) {
|
|
$id=$result['id'];
|
|
}else{
|
|
$id=-1;
|
|
}
|
|
if($cache=OC_Cache::getUserCache(true)) {
|
|
$cache->set('fileid/'.$fullPath, $id);
|
|
}
|
|
|
|
return $id;
|
|
}
|
|
|
|
/**
|
|
* get the file path from the id, relative to the home folder of the user
|
|
* @param int id
|
|
* @param string user (optional)
|
|
* @return string
|
|
*/
|
|
public static function getPath($id, $user='') {
|
|
if(!$user) {
|
|
$user=OC_User::getUser();
|
|
}
|
|
$query=OC_DB::prepare('SELECT `path` FROM `*PREFIX*fscache` WHERE `id`=? AND `user`=?');
|
|
$result=$query->execute(array($id, $user));
|
|
$row=$result->fetchRow();
|
|
$path=$row['path'];
|
|
$root='/'.$user.'/files';
|
|
if(substr($path, 0, strlen($root))!=$root) {
|
|
return false;
|
|
}
|
|
return substr($path, strlen($root));
|
|
}
|
|
|
|
/**
|
|
* get the file id of the parent folder, taking into account '/' has no parent
|
|
* @param string $path
|
|
* @return int
|
|
*/
|
|
private static function getParentId($path) {
|
|
if($path=='/') {
|
|
return -1;
|
|
}else{
|
|
return self::getId(dirname($path), '');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* adjust the size of the parent folders
|
|
* @param string $path
|
|
* @param int $sizeDiff
|
|
* @param string root (optinal)
|
|
*/
|
|
public static function increaseSize($path, $sizeDiff, $root=false) {
|
|
if($sizeDiff==0) return;
|
|
$item = OC_FileCache_Cached::get($path);
|
|
//stop walking up the filetree if we hit a non-folder or reached to root folder
|
|
if($path == '/' || $path=='' || $item['mimetype'] !== 'httpd/unix-directory') {
|
|
return;
|
|
}
|
|
$id = $item['id'];
|
|
while($id!=-1) {//walk up the filetree increasing the size of all parent folders
|
|
$query=OC_DB::prepare('UPDATE `*PREFIX*fscache` SET `size`=`size`+? WHERE `id`=?');
|
|
$query->execute(array($sizeDiff, $id));
|
|
$id=self::getParentId($path);
|
|
$path=dirname($path);
|
|
if($path == '' or $path =='/') {
|
|
return;
|
|
}
|
|
$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;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* recursively scan the filesystem and fill the cache
|
|
* @param string $path
|
|
* @param OC_EventSource $eventSource (optional)
|
|
* @param int $count (optional)
|
|
* @param string $root (optional)
|
|
*/
|
|
public static function scan($path, $eventSource=false,&$count=0, $root=false) {
|
|
if($eventSource) {
|
|
$eventSource->send('scanning', array('file'=>$path, 'count'=>$count));
|
|
}
|
|
$lastSend=$count;
|
|
// 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;
|
|
}
|
|
if($root===false) {
|
|
$view=OC_Filesystem::getView();
|
|
}else{
|
|
$view=new OC_FilesystemView($root);
|
|
}
|
|
self::scanFile($path, $root);
|
|
$dh=$view->opendir($path.'/');
|
|
$totalSize=0;
|
|
if($dh) {
|
|
while (($filename = readdir($dh)) !== false) {
|
|
if($filename != '.' and $filename != '..') {
|
|
$file=$path.'/'.$filename;
|
|
if($view->is_dir($file.'/')) {
|
|
self::scan($file, $eventSource, $count, $root);
|
|
}else{
|
|
$totalSize+=self::scanFile($file, $root);
|
|
$count++;
|
|
if($count>$lastSend+25 and $eventSource) {
|
|
$lastSend=$count;
|
|
$eventSource->send('scanning', array('file'=>$path, 'count'=>$count));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
OC_FileCache_Update::cleanFolder($path, $root);
|
|
self::increaseSize($path, $totalSize, $root);
|
|
}
|
|
|
|
/**
|
|
* scan a single file
|
|
* @param string path
|
|
* @param string root (optional)
|
|
* @return int size of the scanned file
|
|
*/
|
|
public static function scanFile($path, $root=false) {
|
|
// 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;
|
|
}
|
|
if($root===false) {
|
|
$view=OC_Filesystem::getView();
|
|
}else{
|
|
$view=new OC_FilesystemView($root);
|
|
}
|
|
if(!$view->is_readable($path)) return; //cant read, nothing we can do
|
|
clearstatcache();
|
|
$mimetype=$view->getMimeType($path);
|
|
$stat=$view->stat($path);
|
|
if($mimetype=='httpd/unix-directory') {
|
|
$stat['size'] = 0;
|
|
$writable=$view->is_writable($path.'/');
|
|
}else{
|
|
$writable=$view->is_writable($path);
|
|
}
|
|
$stat['mimetype']=$mimetype;
|
|
$stat['writable']=$writable;
|
|
if($path=='/') {
|
|
$path='';
|
|
}
|
|
self::put($path, $stat, $root);
|
|
return $stat['size'];
|
|
}
|
|
|
|
/**
|
|
* find files by mimetype
|
|
* @param string $part1
|
|
* @param string $part2 (optional)
|
|
* @param string root (optional)
|
|
* @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')
|
|
*/
|
|
public static function searchByMime($part1, $part2=null, $root=false) {
|
|
if($root===false) {
|
|
$root=OC_Filesystem::getRoot();
|
|
}
|
|
$rootLen=strlen($root);
|
|
$root .= '%';
|
|
$user=OC_User::getUser();
|
|
if(!$part2) {
|
|
$query=OC_DB::prepare('SELECT `path` FROM `*PREFIX*fscache` WHERE `mimepart`=? AND `user`=? AND `path` LIKE ?');
|
|
$result=$query->execute(array($part1, $user, $root));
|
|
}else{
|
|
$query=OC_DB::prepare('SELECT `path` FROM `*PREFIX*fscache` WHERE `mimetype`=? AND `user`=? AND `path` LIKE ? ');
|
|
$result=$query->execute(array($part1.'/'.$part2, $user, $root));
|
|
}
|
|
$names=array();
|
|
while($row=$result->fetchRow()) {
|
|
$names[]=substr($row['path'], $rootLen);
|
|
}
|
|
return $names;
|
|
}
|
|
|
|
/**
|
|
* clean old pre-path_hash entries
|
|
*/
|
|
public static function clean() {
|
|
$query=OC_DB::prepare('DELETE FROM `*PREFIX*fscache` WHERE LENGTH(`path_hash`)<30');
|
|
$query->execute();
|
|
}
|
|
|
|
/**
|
|
* clear filecache entries
|
|
* @param string user (optonal)
|
|
*/
|
|
public static function clear($user='') {
|
|
if($user) {
|
|
$query=OC_DB::prepare('DELETE FROM `*PREFIX*fscache` WHERE `user`=?');
|
|
$query->execute(array($user));
|
|
}else{
|
|
$query=OC_DB::prepare('DELETE FROM `*PREFIX*fscache`');
|
|
$query->execute();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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`= ? ');
|
|
$query->execute(array($user,'httpd/unix-directory'));
|
|
}else{
|
|
$query=OC_DB::prepare('UPDATE `*PREFIX*fscache` SET `mtime`=0 AND `mimetype`= ? ');
|
|
$query->execute(array('httpd/unix-directory'));
|
|
}
|
|
}
|
|
}
|
|
|
|
//watch for changes and try to keep the cache up to date
|
|
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');
|