<commit_before><?php
/**
 * Copyright (c) 2012 <NAME> <<EMAIL>>
 * 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)
	 * @param boolean folder
	 * @return bool
	 */
	public static function hasUpdated($path,$root=false,$folder=false){
		if($root===false){
			$view=OC_Filesystem::getView();
		}else{
			$view=new OC_FilesystemView($root);
		}
		if(!$view->file_exists($path)){
			return false;
		}
		$cachedData=OC_FileCache_Cached::get($path,$root);
		if(isset($cachedData['mtime'])){
			$cachedMTime=$cachedData['mtime'];
			if($folder){
				return $view->hasUpdated($path.'/',$cachedMTime);
			}else{
				return $view->hasUpdated($path,$cachedMTime);
			}
		}else{//file not in cache, so it has to be updated
			if(($path=='/' or $path=='') and $root===false){//dont auto update the home folder, it will be scanned
				return false;
			}
			return true;
		}
	}
	
	/**
	 * delete non existing files from the cache
	 */
	public static function cleanFolder($path,$root=false){
		if($root===false){
			$view=OC_Filesystem::getView();
		}else{
			$view=new OC_FilesystemView($root);
		}

		$cachedContent=OC_FileCache_Cached::getFolderContent($path,$root);
		foreach($cachedContent as $fileData){
			$path=$fileData['path'];
			$file=$view->getRelativePath($path);
			if(!$view->file_exists($file)){
				if($root===false){//filesystem hooks are only valid for the default root
					OC_Hook::emit('OC_Filesystem','post_delete',array('path'=>$file));
				}else{
					self::delete($file,$root);
				}
			}
		}
	}

	/**
	 * update the cache according to changes in the folder
	 * @param string path
	 * @param string root (optional)
	 */
	public static function updateFolder($path,$root=false){
		if($root===false){
			$view=OC_Filesystem::getView();
		}else{
			$view=new OC_FilesystemView($root);
		}
		$dh=$view->opendir($path.'/');
		if($dh){//check for changed/new files
			while (($filename = readdir($dh)) !== false) {
				if($filename != '.' and $filename != '..'){
					$file=$path.'/'.$filename;
					if(self::hasUpdated($file,$root)){
						if($root===false){//filesystem hooks are only valid for the default root
							OC_Hook::emit('OC_Filesystem','post_write',array('path'=>$file));
						}else{
							self::update($file,$root);
						}
					}
				}
			}
		}

		self::cleanFolder($path,$root);

		//update the folder last, so we can calculate the size correctly
		if($root===false){//filesystem hooks are only valid for the default root
			OC_Hook::emit('OC_Filesystem','post_write',array('path'=>$path));
		}else{
			self::update($path,$root);
		}
	}

	/**
	 * called when changes are made to files
	 * @param array $params
	 * @param string root (optional)
	 */
	public static function fileSystemWatcherWrite($params){
		$path=$params['path'];
		self::update($path);
	}

	/**
	 * called when files are deleted
	 * @param array $params
	 * @param string root (optional)
	 */
	public static function fileSystemWatcherDelete($params){
		$path=$params['path'];
		self::delete($path);
	}

	/**
	 * called when files are deleted
	 * @param array $params
	 * @param string root (optional)
	 */
	public static function fileSystemWatcherRename($params){
		$oldPath=$params['oldpath'];
		$newPath=$params['newpath'];
		self::rename($oldPath,$newPath);
	}

	/**
	 * update the filecache according to changes to the fileysystem
	 * @param string path
	 * @param string root (optional)
	 */
	public static function update($path,$root=false){
		if($root===false){
			$view=OC_Filesystem::getView();
		}else{
			$view=new OC_FilesystemView($root);
		}

		$mimetype=$view->getMimeType($path);
		
		$size=0;
		$cached=OC_FileCache_Cached::get($path,$root);
		$cachedSize=isset($cached['size'])?$cached['size']:0;
		
		if($mimetype=='httpd/unix-directory'){
			if(OC_FileCache::inCache($path,$root)){
				$cachedContent=OC_FileCache_Cached::getFolderContent($path,$root);
				foreach($cachedContent as $file){
					$size+=$file['size'];
				}
				$mtime=$view->filemtime($path.'/');
				$ctime=$view->filectime($path.'/');
				$writable=$view->is_writable($path.'/');
				OC_FileCache::put($path,array('size'=>$size,'mtime'=>$mtime,'ctime'=>$ctime,'mimetype'=>$mimetype,'writable'=>$writable));
			}else{
				$count=0;
				OC_FileCache::scan($path,null,$count,$root);
				return; //increaseSize is already called inside scan
			}
		}else{
			$size=OC_FileCache::scanFile($path,$root);
		}
		OC_FileCache::increaseSize(dirname($path),$size-$cachedSize,$root);
	}

	/**
	 * update the filesystem after a delete has been detected
	 * @param string path<commit_msg>fix scanning of archives in some cases<commit_after><?php
/**
 * Copyright (c) 2012 <NAME> <<EMAIL>>
 * 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)
	 * @param boolean folder
	 * @return bool
	 */
	public static function hasUpdated($path,$root=false,$folder=false){
		if($root===false){
			$view=OC_Filesystem::getView();
		}else{
			$view=new OC_FilesystemView($root);
		}
		if(!$view->file_exists($path)){
			return false;
		}
		$cachedData=OC_FileCache_Cached::get($path,$root);
		if(isset($cachedData['mtime'])){
			$cachedMTime=$cachedData['mtime'];
			if($folder){
				return $view->hasUpdated($path.'/',$cachedMTime);
			}else{
				return $view->hasUpdated($path,$cachedMTime);
			}
		}else{//file not in cache, so it has to be updated
			if(($path=='/' or $path=='') and $root===false){//dont auto update the home folder, it will be scanned
				return false;
			}
			return true;
		}
	}
	
	/**
	 * delete non existing files from the cache
	 */
	public static function cleanFolder($path,$root=false){
		if($root===false){
			$view=OC_Filesystem::getView();
		}else{
			$view=new OC_FilesystemView($root);
		}

		$cachedContent=OC_FileCache_Cached::getFolderContent($path,$root);
		foreach($cachedContent as $fileData){
			$path=$fileData['path'];
			$file=$view->getRelativePath($path);
			if(!$view->file_exists($file)){
				if($root===false){//filesystem hooks are only valid for the default root
					OC_Hook::emit('OC_Filesystem','post_delete',array('path'=>$file));
				}else{
					self::delete($file,$root);
				}
			}
		}
	}

	/**
	 * update the cache according to changes in the folder
	 * @param string path
	 * @param string root (optional)
	 */
	public static function updateFolder($path,$root=false){
		if($root===false){
			$view=OC_Filesystem::getView();
		}else{
			$view=new OC_FilesystemView($root);
		}
		$dh=$view->opendir($path.'/');
		if($dh){//check for changed/new files
			while (($filename = readdir($dh)) !== false) {
				if($filename != '.' and $filename != '..'){
					$file=$path.'/'.$filename;
					if(self::hasUpdated($file,$root)){
						if($root===false){//filesystem hooks are only valid for the default root
							OC_Hook::emit('OC_Filesystem','post_write',array('path'=>$file));
						}else{
							self::update($file,$root);
						}
					}
				}
			}
		}

		self::cleanFolder($path,$root);

		//update the folder last, so we can calculate the size correctly
		if($root===false){//filesystem hooks are only valid for the default root
			OC_Hook::emit('OC_Filesystem','post_write',array('path'=>$path));
		}else{
			self::update($path,$root);
		}
	}

	/**
	 * called when changes are made to files
	 * @param array $params
	 * @param string root (optional)
	 */
	public static function fileSystemWatcherWrite($params){
		$path=$params['path'];
		self::update($path);
	}

	/**
	 * called when files are deleted
	 * @param array $params
	 * @param string root (optional)
	 */
	public static function fileSystemWatcherDelete($params){
		$path=$params['path'];
		self::delete($path);
	}

	/**
	 * called when files are deleted
	 * @param array $params
	 * @param string root (optional)
	 */
	public static function fileSystemWatcherRename($params){
		$oldPath=$params['oldpath'];
		$newPath=$params['newpath'];
		self::rename($oldPath,$newPath);
	}

	/**
	 * update the filecache according to changes to the fileysystem
	 * @param string path
	 * @param string root (optional)
	 */
	public static function update($path,$root=false){
		if($root===false){
			$view=OC_Filesystem::getView();
		}else{
			$view=new OC_FilesystemView($root);
		}

		$mimetype=$view->getMimeType($path);
		
		$size=0;
		$cached=OC_FileCache_Cached::get($path,$root);
		$cachedSize=isset($cached['size'])?$cached['size']:0;

		if($view->is_dir($path.'/')){
			if(OC_FileCache::inCache($path,$root)){
				$cachedContent=OC_FileCache_Cached::getFolderContent($path,$root);
				foreach($cachedContent as $file){
					$size+=$file['size'];
				}
				$mtime=$view->filemtime($path.'/');
				$ctime=$view->filectime($path.'/');
				$writable=$view->is_writable($path.'/');
				OC_FileCache::put($path,array('size'=>$size,'mtime'=>$mtime,'ctime'=>$ctime,'mimetype'=>$mimetype,'writable'=>$writable));
			}else{
				$count=0;
				OC_FileCache::scan($path,null,$count,$root);
				return; //increaseSize is already called inside scan
			}
		}else{
			$size=OC_FileCache::scanFile($path,$root);
		}
		OC_FileCache::increaseSize(dirname($path),$size-$cachedSize,$root);
	}

	/**
	 * update the filesystem after a delete has been detected
	 * @param string path
{'fraction_non_alphanumeric': 0.17920445177246497, 'fraction_numerical': 0.0016488046166529267, 'mean_word_length': 4.3678097345132745, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5490729', 'n_tokens_mistral': 3672, 'n_tokens_neox': 3132, 'n_words': 679}