2012-06-15 17:56:15 +00: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* handles updating the filecache according to outside changes
|
|
|
|
*/
|
|
|
|
class OC_FileCache_Update{
|
|
|
|
/**
|
|
|
|
* check if a file or folder is updated outside owncloud
|
|
|
|
* @param string path
|
|
|
|
* @param string root (optional)
|
2012-06-16 23:26:30 +00:00
|
|
|
* @param boolean folder
|
2012-06-15 17:56:15 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2012-11-02 18:53:02 +00:00
|
|
|
public static function hasUpdated($path, $root=false, $folder=false) {
|
2012-09-07 13:22:01 +00:00
|
|
|
if($root===false) {
|
2012-06-15 17:56:15 +00:00
|
|
|
$view=OC_Filesystem::getView();
|
|
|
|
}else{
|
|
|
|
$view=new OC_FilesystemView($root);
|
|
|
|
}
|
2012-09-07 13:22:01 +00:00
|
|
|
if(!$view->file_exists($path)) {
|
2012-06-15 17:56:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-09-10 09:35:15 +00:00
|
|
|
$cachedData=OC_FileCache_Cached::get($path, $root);
|
2012-09-07 13:22:01 +00:00
|
|
|
if(isset($cachedData['mtime'])) {
|
2012-06-15 17:56:15 +00:00
|
|
|
$cachedMTime=$cachedData['mtime'];
|
2012-09-07 13:22:01 +00:00
|
|
|
if($folder) {
|
2012-09-10 09:35:15 +00:00
|
|
|
return $view->hasUpdated($path.'/', $cachedMTime);
|
2012-06-16 23:26:30 +00:00
|
|
|
}else{
|
2012-09-10 09:35:15 +00:00
|
|
|
return $view->hasUpdated($path, $cachedMTime);
|
2012-06-16 23:26:30 +00:00
|
|
|
}
|
2012-06-15 17:56:15 +00:00
|
|
|
}else{//file not in cache, so it has to be updated
|
2012-09-07 13:22:01 +00:00
|
|
|
if(($path=='/' or $path=='') and $root===false) {//dont auto update the home folder, it will be scanned
|
2012-06-15 17:56:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2012-08-29 06:38:33 +00:00
|
|
|
|
2012-06-15 17:56:15 +00:00
|
|
|
/**
|
|
|
|
* delete non existing files from the cache
|
|
|
|
*/
|
2012-11-02 18:53:02 +00:00
|
|
|
public static function cleanFolder($path, $root=false) {
|
2012-09-07 13:22:01 +00:00
|
|
|
if($root===false) {
|
2012-06-15 17:56:15 +00:00
|
|
|
$view=OC_Filesystem::getView();
|
|
|
|
}else{
|
|
|
|
$view=new OC_FilesystemView($root);
|
|
|
|
}
|
|
|
|
|
2012-11-02 18:53:02 +00:00
|
|
|
$cachedContent=OC_FileCache_Cached::getFolderContent($path, $root);
|
2012-09-07 13:22:01 +00:00
|
|
|
foreach($cachedContent as $fileData) {
|
2012-06-15 17:56:15 +00:00
|
|
|
$path=$fileData['path'];
|
|
|
|
$file=$view->getRelativePath($path);
|
2012-09-07 13:22:01 +00:00
|
|
|
if(!$view->file_exists($file)) {
|
|
|
|
if($root===false) {//filesystem hooks are only valid for the default root
|
2012-09-10 09:35:15 +00:00
|
|
|
OC_Hook::emit('OC_Filesystem', 'post_delete', array('path'=>$file));
|
2012-06-15 17:56:15 +00:00
|
|
|
}else{
|
2012-09-10 09:35:15 +00:00
|
|
|
self::delete($file, $root);
|
2012-06-15 17:56:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* update the cache according to changes in the folder
|
|
|
|
* @param string path
|
|
|
|
* @param string root (optional)
|
|
|
|
*/
|
2012-11-02 18:53:02 +00:00
|
|
|
public static function updateFolder($path, $root=false) {
|
2012-09-07 13:22:01 +00:00
|
|
|
if($root===false) {
|
2012-06-15 17:56:15 +00:00
|
|
|
$view=OC_Filesystem::getView();
|
|
|
|
}else{
|
|
|
|
$view=new OC_FilesystemView($root);
|
|
|
|
}
|
|
|
|
$dh=$view->opendir($path.'/');
|
2012-09-07 13:22:01 +00:00
|
|
|
if($dh) {//check for changed/new files
|
2012-06-15 17:56:15 +00:00
|
|
|
while (($filename = readdir($dh)) !== false) {
|
2012-09-30 14:11:01 +00:00
|
|
|
if($filename != '.' and $filename != '..' and $filename != '') {
|
2012-06-15 17:56:15 +00:00
|
|
|
$file=$path.'/'.$filename;
|
2012-09-30 14:11:01 +00:00
|
|
|
$isDir=$view->is_dir($file);
|
|
|
|
if(self::hasUpdated($file, $root, $isDir)) {
|
2012-11-04 09:46:32 +00:00
|
|
|
if($isDir) {
|
2012-09-30 14:11:01 +00:00
|
|
|
self::updateFolder($file, $root);
|
|
|
|
}elseif($root===false) {//filesystem hooks are only valid for the default root
|
2012-09-10 09:35:15 +00:00
|
|
|
OC_Hook::emit('OC_Filesystem', 'post_write', array('path'=>$file));
|
2012-06-15 17:56:15 +00:00
|
|
|
}else{
|
2012-09-10 09:35:15 +00:00
|
|
|
self::update($file, $root);
|
2012-06-15 17:56:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-10 09:35:15 +00:00
|
|
|
self::cleanFolder($path, $root);
|
2012-06-15 17:56:15 +00:00
|
|
|
|
|
|
|
//update the folder last, so we can calculate the size correctly
|
2012-09-07 13:22:01 +00:00
|
|
|
if($root===false) {//filesystem hooks are only valid for the default root
|
2012-09-10 09:35:15 +00:00
|
|
|
OC_Hook::emit('OC_Filesystem', 'post_write', array('path'=>$path));
|
2012-06-15 17:56:15 +00:00
|
|
|
}else{
|
2012-09-10 09:35:15 +00:00
|
|
|
self::update($path, $root);
|
2012-06-15 17:56:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* called when changes are made to files
|
|
|
|
* @param array $params
|
|
|
|
* @param string root (optional)
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function fileSystemWatcherWrite($params) {
|
2012-06-15 17:56:15 +00:00
|
|
|
$path=$params['path'];
|
|
|
|
self::update($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* called when files are deleted
|
|
|
|
* @param array $params
|
|
|
|
* @param string root (optional)
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function fileSystemWatcherDelete($params) {
|
2012-06-15 17:56:15 +00:00
|
|
|
$path=$params['path'];
|
|
|
|
self::delete($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* called when files are deleted
|
|
|
|
* @param array $params
|
|
|
|
* @param string root (optional)
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function fileSystemWatcherRename($params) {
|
2012-06-15 17:56:15 +00:00
|
|
|
$oldPath=$params['oldpath'];
|
|
|
|
$newPath=$params['newpath'];
|
2012-09-10 09:35:15 +00:00
|
|
|
self::rename($oldPath, $newPath);
|
2012-06-15 17:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-09-30 14:11:01 +00:00
|
|
|
* update the filecache according to changes to the filesystem
|
2012-06-15 17:56:15 +00:00
|
|
|
* @param string path
|
|
|
|
* @param string root (optional)
|
|
|
|
*/
|
2012-11-02 18:53:02 +00:00
|
|
|
public static function update($path, $root=false) {
|
2012-09-07 13:22:01 +00:00
|
|
|
if($root===false) {
|
2012-06-15 17:56:15 +00:00
|
|
|
$view=OC_Filesystem::getView();
|
|
|
|
}else{
|
|
|
|
$view=new OC_FilesystemView($root);
|
|
|
|
}
|
|
|
|
|
|
|
|
$mimetype=$view->getMimeType($path);
|
2012-08-29 06:38:33 +00:00
|
|
|
|
2012-06-15 17:56:15 +00:00
|
|
|
$size=0;
|
2012-11-02 18:53:02 +00:00
|
|
|
$cached=OC_FileCache_Cached::get($path, $root);
|
2012-06-15 17:56:15 +00:00
|
|
|
$cachedSize=isset($cached['size'])?$cached['size']:0;
|
2012-08-09 14:36:16 +00:00
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
if($view->is_dir($path.'/')) {
|
2012-09-10 09:35:15 +00:00
|
|
|
if(OC_FileCache::inCache($path, $root)) {
|
|
|
|
$cachedContent=OC_FileCache_Cached::getFolderContent($path, $root);
|
2012-09-07 13:22:01 +00:00
|
|
|
foreach($cachedContent as $file) {
|
2012-06-15 17:56:15 +00:00
|
|
|
$size+=$file['size'];
|
|
|
|
}
|
2012-06-17 00:15:11 +00:00
|
|
|
$mtime=$view->filemtime($path.'/');
|
|
|
|
$ctime=$view->filectime($path.'/');
|
|
|
|
$writable=$view->is_writable($path.'/');
|
2012-11-04 10:10:46 +00:00
|
|
|
OC_FileCache::put($path, array('size'=>$size,'mtime'=>$mtime,'ctime'=>$ctime,'mimetype'=>$mimetype, 'writable'=>$writable));
|
2012-06-15 17:56:15 +00:00
|
|
|
}else{
|
|
|
|
$count=0;
|
2012-09-10 09:35:15 +00:00
|
|
|
OC_FileCache::scan($path, null, $count, $root);
|
2012-06-15 17:56:15 +00:00
|
|
|
return; //increaseSize is already called inside scan
|
|
|
|
}
|
|
|
|
}else{
|
2012-09-10 09:35:15 +00:00
|
|
|
$size=OC_FileCache::scanFile($path, $root);
|
2012-06-15 17:56:15 +00:00
|
|
|
}
|
2012-11-04 09:46:32 +00:00
|
|
|
if($path !== '' and $path !== '/') {
|
2012-10-13 12:32:58 +00:00
|
|
|
OC_FileCache::increaseSize(dirname($path), $size-$cachedSize, $root);
|
|
|
|
}
|
2012-06-15 17:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* update the filesystem after a delete has been detected
|
|
|
|
* @param string path
|
|
|
|
* @param string root (optional)
|
|
|
|
*/
|
2012-11-02 18:53:02 +00:00
|
|
|
public static function delete($path, $root=false) {
|
2012-09-10 09:35:15 +00:00
|
|
|
$cached=OC_FileCache_Cached::get($path, $root);
|
2012-09-07 13:22:01 +00:00
|
|
|
if(!isset($cached['size'])) {
|
2012-06-15 17:56:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$size=$cached['size'];
|
2012-09-10 09:35:15 +00:00
|
|
|
OC_FileCache::increaseSize(dirname($path), -$size, $root);
|
|
|
|
OC_FileCache::delete($path, $root);
|
2012-06-15 17:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* update the filesystem after a rename has been detected
|
|
|
|
* @param string oldPath
|
|
|
|
* @param string newPath
|
|
|
|
* @param string root (optional)
|
|
|
|
*/
|
2012-11-04 10:10:46 +00:00
|
|
|
public static function rename($oldPath, $newPath, $root=false) {
|
2012-09-10 09:35:15 +00:00
|
|
|
if(!OC_FileCache::inCache($oldPath, $root)) {
|
2012-06-15 17:56:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-09-07 13:22:01 +00:00
|
|
|
if($root===false) {
|
2012-06-15 17:56:15 +00:00
|
|
|
$view=OC_Filesystem::getView();
|
|
|
|
}else{
|
|
|
|
$view=new OC_FilesystemView($root);
|
|
|
|
}
|
|
|
|
|
2012-09-10 09:35:15 +00:00
|
|
|
$cached=OC_FileCache_Cached::get($oldPath, $root);
|
2012-06-15 17:56:15 +00:00
|
|
|
$oldSize=$cached['size'];
|
2012-09-10 09:35:15 +00:00
|
|
|
OC_FileCache::increaseSize(dirname($oldPath), -$oldSize, $root);
|
|
|
|
OC_FileCache::increaseSize(dirname($newPath), $oldSize, $root);
|
|
|
|
OC_FileCache::move($oldPath, $newPath);
|
2012-06-15 17:56:15 +00:00
|
|
|
}
|
2012-06-14 14:09:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* delete files owned by user from the cache
|
|
|
|
* @param string $parameters$parameters["uid"])
|
|
|
|
*/
|
|
|
|
public static function deleteFromUser($parameters) {
|
|
|
|
OC_FileCache::clear($parameters["uid"]);
|
|
|
|
}
|
2012-09-30 14:11:01 +00:00
|
|
|
}
|