Merge pull request #1906 from owncloud/storage-stat-fail

Common storage: don't throw warning when a stat fails
This commit is contained in:
Bernhard Posselt 2013-02-26 00:44:42 -08:00
commit b987c76d16
1 changed files with 116 additions and 83 deletions

View File

@ -22,81 +22,102 @@ namespace OC\Files\Storage;
abstract class Common implements \OC\Files\Storage\Storage { abstract class Common implements \OC\Files\Storage\Storage {
public function __construct($parameters) {} public function __construct($parameters) {
}
public function is_dir($path) { public function is_dir($path) {
return $this->filetype($path)=='dir'; return $this->filetype($path) == 'dir';
} }
public function is_file($path) { public function is_file($path) {
return $this->filetype($path)=='file'; return $this->filetype($path) == 'file';
} }
public function filesize($path) { public function filesize($path) {
if($this->is_dir($path)) { if ($this->is_dir($path)) {
return 0;//by definition return 0; //by definition
}else{ } else {
$stat = $this->stat($path); $stat = $this->stat($path);
return $stat['size']; if (isset($stat['size'])) {
return $stat['size'];
} else {
return 0;
}
} }
} }
public function isCreatable($path) { public function isCreatable($path) {
if ($this->is_dir($path) && $this->isUpdatable($path)) { if ($this->is_dir($path) && $this->isUpdatable($path)) {
return true; return true;
} }
return false; return false;
} }
public function isDeletable($path) { public function isDeletable($path) {
return $this->isUpdatable($path); return $this->isUpdatable($path);
} }
public function isSharable($path) { public function isSharable($path) {
return $this->isReadable($path); return $this->isReadable($path);
} }
public function getPermissions($path){
public function getPermissions($path) {
$permissions = 0; $permissions = 0;
if($this->isCreatable($path)) { if ($this->isCreatable($path)) {
$permissions |= \OCP\PERMISSION_CREATE; $permissions |= \OCP\PERMISSION_CREATE;
} }
if($this->isReadable($path)) { if ($this->isReadable($path)) {
$permissions |= \OCP\PERMISSION_READ; $permissions |= \OCP\PERMISSION_READ;
} }
if($this->isUpdatable($path)) { if ($this->isUpdatable($path)) {
$permissions |= \OCP\PERMISSION_UPDATE; $permissions |= \OCP\PERMISSION_UPDATE;
} }
if($this->isDeletable($path)) { if ($this->isDeletable($path)) {
$permissions |= \OCP\PERMISSION_DELETE; $permissions |= \OCP\PERMISSION_DELETE;
} }
if($this->isSharable($path)) { if ($this->isSharable($path)) {
$permissions |= \OCP\PERMISSION_SHARE; $permissions |= \OCP\PERMISSION_SHARE;
} }
return $permissions; return $permissions;
} }
public function filemtime($path) { public function filemtime($path) {
$stat = $this->stat($path); $stat = $this->stat($path);
return $stat['mtime']; if (isset($stat['mtime'])) {
return $stat['mtime'];
} else {
return 0;
}
} }
public function file_get_contents($path) { public function file_get_contents($path) {
$handle = $this->fopen($path, "r"); $handle = $this->fopen($path, "r");
if(!$handle) { if (!$handle) {
return false; return false;
} }
$size=$this->filesize($path); $size = $this->filesize($path);
if($size==0) { if ($size == 0) {
return ''; return '';
} }
return fread($handle, $size); return fread($handle, $size);
} }
public function file_put_contents($path, $data) { public function file_put_contents($path, $data) {
$handle = $this->fopen($path, "w"); $handle = $this->fopen($path, "w");
return fwrite($handle, $data); return fwrite($handle, $data);
} }
public function rename($path1, $path2) { public function rename($path1, $path2) {
if($this->copy($path1, $path2)) { if ($this->copy($path1, $path2)) {
return $this->unlink($path1); return $this->unlink($path1);
}else{ } else {
return false; return false;
} }
} }
public function copy($path1, $path2) { public function copy($path1, $path2) {
$source=$this->fopen($path1, 'r'); $source = $this->fopen($path1, 'r');
$target=$this->fopen($path2, 'w'); $target = $this->fopen($path2, 'w');
list($count, $result) = \OC_Helper::streamCopy($source, $target); list($count, $result) = \OC_Helper::streamCopy($source, $target);
return $result; return $result;
} }
@ -110,29 +131,30 @@ abstract class Common implements \OC\Files\Storage\Storage {
* @note By default the directory specified by $directory will be * @note By default the directory specified by $directory will be
* deleted together with its contents. To avoid this set $empty to true * deleted together with its contents. To avoid this set $empty to true
*/ */
public function deleteAll( $directory, $empty = false ) { public function deleteAll($directory, $empty = false) {
$directory = trim($directory, '/'); $directory = trim($directory, '/');
if ( !$this->file_exists( \OCP\USER::getUser() . '/' . $directory ) if (!$this->file_exists(\OCP\USER::getUser() . '/' . $directory)
|| !$this->is_dir( \OCP\USER::getUser() . '/' . $directory ) ) { || !$this->is_dir(\OCP\USER::getUser() . '/' . $directory)
) {
return false; return false;
} elseif( !$this->isReadable( \OCP\USER::getUser() . '/' . $directory ) ) { } elseif (!$this->isReadable(\OCP\USER::getUser() . '/' . $directory)) {
return false; return false;
} else { } else {
$directoryHandle = $this->opendir( \OCP\USER::getUser() . '/' . $directory ); $directoryHandle = $this->opendir(\OCP\USER::getUser() . '/' . $directory);
while ( $contents = readdir( $directoryHandle ) ) { while ($contents = readdir($directoryHandle)) {
if ( $contents != '.' && $contents != '..') { if ($contents != '.' && $contents != '..') {
$path = $directory . "/" . $contents; $path = $directory . "/" . $contents;
if ( $this->is_dir( $path ) ) { if ($this->is_dir($path)) {
$this->deleteAll( $path ); $this->deleteAll($path);
} else { } else {
$this->unlink( \OCP\USER::getUser() .'/' . $path ); // TODO: make unlink use same system path as is_dir $this->unlink(\OCP\USER::getUser() . '/' . $path); // TODO: make unlink use same system path as is_dir
} }
} }
} }
//$this->closedir( $directoryHandle ); // TODO: implement closedir in OC_FSV //$this->closedir( $directoryHandle ); // TODO: implement closedir in OC_FSV
if ( $empty == false ) { if ($empty == false) {
if ( !$this->rmdir( $directory ) ) { if (!$this->rmdir($directory)) {
return false; return false;
} }
} }
@ -140,88 +162,95 @@ abstract class Common implements \OC\Files\Storage\Storage {
} }
} }
public function getMimeType($path) { public function getMimeType($path) {
if(!$this->file_exists($path)) { if (!$this->file_exists($path)) {
return false; return false;
} }
if($this->is_dir($path)) { if ($this->is_dir($path)) {
return 'httpd/unix-directory'; return 'httpd/unix-directory';
} }
$source=$this->fopen($path, 'r'); $source = $this->fopen($path, 'r');
if(!$source) { if (!$source) {
return false; return false;
} }
$head=fread($source, 8192);//8kb should suffice to determine a mimetype $head = fread($source, 8192); //8kb should suffice to determine a mimetype
if($pos=strrpos($path, '.')) { if ($pos = strrpos($path, '.')) {
$extension=substr($path, $pos); $extension = substr($path, $pos);
}else{ } else {
$extension=''; $extension = '';
} }
$tmpFile=\OC_Helper::tmpFile($extension); $tmpFile = \OC_Helper::tmpFile($extension);
file_put_contents($tmpFile, $head); file_put_contents($tmpFile, $head);
$mime=\OC_Helper::getMimeType($tmpFile); $mime = \OC_Helper::getMimeType($tmpFile);
unlink($tmpFile); unlink($tmpFile);
return $mime; return $mime;
} }
public function hash($type, $path, $raw = false) { public function hash($type, $path, $raw = false) {
$tmpFile=$this->getLocalFile($path); $tmpFile = $this->getLocalFile($path);
$hash=hash($type, $tmpFile, $raw); $hash = hash($type, $tmpFile, $raw);
unlink($tmpFile); unlink($tmpFile);
return $hash; return $hash;
} }
public function search($query) { public function search($query) {
return $this->searchInDir($query); return $this->searchInDir($query);
} }
public function getLocalFile($path) { public function getLocalFile($path) {
return $this->toTmpFile($path); return $this->toTmpFile($path);
} }
private function toTmpFile($path) {//no longer in the storage api, still useful here
$source=$this->fopen($path, 'r'); private function toTmpFile($path) { //no longer in the storage api, still useful here
if(!$source) { $source = $this->fopen($path, 'r');
if (!$source) {
return false; return false;
} }
if($pos=strrpos($path, '.')) { if ($pos = strrpos($path, '.')) {
$extension=substr($path, $pos); $extension = substr($path, $pos);
}else{ } else {
$extension=''; $extension = '';
} }
$tmpFile=\OC_Helper::tmpFile($extension); $tmpFile = \OC_Helper::tmpFile($extension);
$target=fopen($tmpFile, 'w'); $target = fopen($tmpFile, 'w');
\OC_Helper::streamCopy($source, $target); \OC_Helper::streamCopy($source, $target);
return $tmpFile; return $tmpFile;
} }
public function getLocalFolder($path) { public function getLocalFolder($path) {
$baseDir=\OC_Helper::tmpFolder(); $baseDir = \OC_Helper::tmpFolder();
$this->addLocalFolder($path, $baseDir); $this->addLocalFolder($path, $baseDir);
return $baseDir; return $baseDir;
} }
private function addLocalFolder($path, $target) { private function addLocalFolder($path, $target) {
if($dh=$this->opendir($path)) { if ($dh = $this->opendir($path)) {
while($file=readdir($dh)) { while ($file = readdir($dh)) {
if($file!=='.' and $file!=='..') { if ($file !== '.' and $file !== '..') {
if($this->is_dir($path.'/'.$file)) { if ($this->is_dir($path . '/' . $file)) {
mkdir($target.'/'.$file); mkdir($target . '/' . $file);
$this->addLocalFolder($path.'/'.$file, $target.'/'.$file); $this->addLocalFolder($path . '/' . $file, $target . '/' . $file);
}else{ } else {
$tmp=$this->toTmpFile($path.'/'.$file); $tmp = $this->toTmpFile($path . '/' . $file);
rename($tmp, $target.'/'.$file); rename($tmp, $target . '/' . $file);
} }
} }
} }
} }
} }
protected function searchInDir($query, $dir='') { protected function searchInDir($query, $dir = '') {
$files=array(); $files = array();
$dh=$this->opendir($dir); $dh = $this->opendir($dir);
if($dh) { if ($dh) {
while($item=readdir($dh)) { while ($item = readdir($dh)) {
if ($item == '.' || $item == '..') continue; if ($item == '.' || $item == '..') continue;
if(strstr(strtolower($item), strtolower($query))!==false) { if (strstr(strtolower($item), strtolower($query)) !== false) {
$files[]=$dir.'/'.$item; $files[] = $dir . '/' . $item;
} }
if($this->is_dir($dir.'/'.$item)) { if ($this->is_dir($dir . '/' . $item)) {
$files=array_merge($files, $this->searchInDir($query, $dir.'/'.$item)); $files = array_merge($files, $this->searchInDir($query, $dir . '/' . $item));
} }
} }
} }
@ -230,32 +259,34 @@ abstract class Common implements \OC\Files\Storage\Storage {
/** /**
* check if a file or folder has been updated since $time * check if a file or folder has been updated since $time
*
* @param string $path * @param string $path
* @param int $time * @param int $time
* @return bool * @return bool
*/ */
public function hasUpdated($path, $time) { public function hasUpdated($path, $time) {
return $this->filemtime($path)>$time; return $this->filemtime($path) > $time;
} }
public function getCache($path=''){ public function getCache($path = '') {
return new \OC\Files\Cache\Cache($this); return new \OC\Files\Cache\Cache($this);
} }
public function getScanner($path=''){ public function getScanner($path = '') {
return new \OC\Files\Cache\Scanner($this); return new \OC\Files\Cache\Scanner($this);
} }
public function getPermissionsCache($path=''){ public function getPermissionsCache($path = '') {
return new \OC\Files\Cache\Permissions($this); return new \OC\Files\Cache\Permissions($this);
} }
public function getWatcher($path=''){ public function getWatcher($path = '') {
return new \OC\Files\Cache\Watcher($this); return new \OC\Files\Cache\Watcher($this);
} }
/** /**
* get the owner of a path * get the owner of a path
*
* @param string $path The path to get the owner * @param string $path The path to get the owner
* @return string uid or false * @return string uid or false
*/ */
@ -269,12 +300,12 @@ abstract class Common implements \OC\Files\Storage\Storage {
* @param string $path * @param string $path
* @return string * @return string
*/ */
public function getETag($path){ public function getETag($path) {
$ETagFunction = \OC_Connector_Sabre_Node::$ETagFunction; $ETagFunction = \OC_Connector_Sabre_Node::$ETagFunction;
if($ETagFunction) { if ($ETagFunction) {
$hash = call_user_func($ETagFunction, $path); $hash = call_user_func($ETagFunction, $path);
return $hash; return $hash;
}else{ } else {
return uniqid(); return uniqid();
} }
} }
@ -282,6 +313,7 @@ abstract class Common implements \OC\Files\Storage\Storage {
/** /**
* clean a path, i.e. remove all redundant '.' and '..' * clean a path, i.e. remove all redundant '.' and '..'
* making sure that it can't point to higher than '/' * making sure that it can't point to higher than '/'
*
* @param $path The path to clean * @param $path The path to clean
* @return string cleaned path * @return string cleaned path
*/ */
@ -304,10 +336,11 @@ abstract class Common implements \OC\Files\Storage\Storage {
/** /**
* get the free space in the storage * get the free space in the storage
*
* @param $path * @param $path
* return int * return int
*/ */
public function free_space($path){ public function free_space($path) {
return \OC\Files\FREE_SPACE_UNKNOWN; return \OC\Files\FREE_SPACE_UNKNOWN;
} }
} }