From 045c0acc92a291d5bc785103e129516f99032f94 Mon Sep 17 00:00:00 2001 From: Thomas Mueller Date: Thu, 3 Jan 2013 00:35:57 +0100 Subject: [PATCH 1/6] workaround for 32-bit systems to handle filesizes bigger than 2GB --- lib/filestorage.php | 2 +- lib/filestorage/local.php | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/filestorage.php b/lib/filestorage.php index dd65f4421b..2e03c4cb6d 100644 --- a/lib/filestorage.php +++ b/lib/filestorage.php @@ -21,7 +21,7 @@ */ /** - * Provde a common interface to all different storage options + * Provide a common interface to all different storage options */ abstract class OC_Filestorage{ abstract public function __construct($parameters); diff --git a/lib/filestorage/local.php b/lib/filestorage/local.php index 6fe45acf8c..0bf6ad2d3d 100644 --- a/lib/filestorage/local.php +++ b/lib/filestorage/local.php @@ -29,7 +29,24 @@ class OC_Filestorage_Local extends OC_Filestorage_Common{ return is_file($this->datadir.$path); } public function stat($path) { - return stat($this->datadir.$path); + $fullPath = $this->datadir.$path; + $statResult = stat($fullPath); + + // special case for 32-bit systems + if (PHP_INT_SIZE===4) { + if (!(strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')) + $size = (float)exec('stat -c %s '. escapeshellarg ($fullPath)); + else{ + $fsobj = new COM("Scripting.FileSystemObject"); + $f = $fsobj->GetFile($fullPath); + $size = $f->Size; + } + + $statResult['size'] = $size; + $statResult[7] = $size; + } + + return $statResult; } public function filetype($path) { $filetype=filetype($this->datadir.$path); From dfc8d757b5dd7e5d7bf54a32b9f11f0798905d4a Mon Sep 17 00:00:00 2001 From: Thomas Mueller Date: Fri, 4 Jan 2013 23:00:51 +0100 Subject: [PATCH 2/6] OC_Helper::is_function_enabled() added to find out if a function is available And some documentation adjusted on the way --- lib/helper.php | 61 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/lib/helper.php b/lib/helper.php index be4e4e5267..1aba2a3810 100644 --- a/lib/helper.php +++ b/lib/helper.php @@ -31,8 +31,9 @@ class OC_Helper { /** * @brief Creates an url using a defined route * @param $route - * @param $parameters - * @param $args array with param=>value, will be appended to the returned url + * @param array $parameters + * @return + * @internal param array $args with param=>value, will be appended to the returned url * @returns the url * * Returns a url to the given app and file. @@ -128,6 +129,7 @@ class OC_Helper { /** * @brief Creates an absolute url for remote use * @param string $service id + * @param bool $add_slash * @return string the url * * Returns a absolute url to the given service. @@ -139,6 +141,7 @@ class OC_Helper { /** * @brief Creates an absolute url for public use * @param string $service id + * @param bool $add_slash * @return string the url * * Returns a absolute url to the given service. @@ -450,12 +453,14 @@ class OC_Helper { } /** - * detect if a given program is found in the search PATH - * - * @param string $program name - * @param string $optional search path, defaults to $PATH - * @return bool true if executable program found in path - */ + * detect if a given program is found in the search PATH + * + * @param $name + * @param bool $path + * @internal param string $program name + * @internal param string $optional search path, defaults to $PATH + * @return bool true if executable program found in path + */ public static function canExecute($name, $path = false) { // path defaults to PATH from environment if not set if ($path === false) { @@ -676,16 +681,16 @@ class OC_Helper { } /** - * @brief replaces a copy of string delimited by the start and (optionally) length parameters with the string given in replacement. - * - * @param string $input The input string. .Opposite to the PHP build-in function does not accept an array. - * @param string $replacement The replacement string. - * @param int $start If start is positive, the replacing will begin at the start'th offset into string. If start is negative, the replacing will begin at the start'th character from the end of string. - * @param int $length Length of the part to be replaced - * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 - * @return string - * - */ + * @brief replaces a copy of string delimited by the start and (optionally) length parameters with the string given in replacement. + * + * @param $string + * @param string $replacement The replacement string. + * @param int $start If start is positive, the replacing will begin at the start'th offset into string. If start is negative, the replacing will begin at the start'th character from the end of string. + * @param int $length Length of the part to be replaced + * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 + * @internal param string $input The input string. .Opposite to the PHP build-in function does not accept an array. + * @return string + */ public static function mb_substr_replace($string, $replacement, $start, $length = null, $encoding = 'UTF-8') { $start = intval($start); $length = intval($length); @@ -758,4 +763,24 @@ class OC_Helper { } return $str; } + + /** + * Checks if a function is available + * @param string $function_name + * @return bool + */ + public static function is_function_enabled($function_name) { + if (!function_exists($function_name)) { + return false; + } + $disabled = explode(', ', ini_get('disable_functions')); + if (in_array($function_name, $disabled)) { + return false; + } + $disabled = explode(', ', ini_get('suhosin.executor.func.blacklist')); + if (in_array($function_name, $disabled)) { + return false; + } + return true; + } } From cedec593ee32d7e8b988c9df7c08020df52dbdb3 Mon Sep 17 00:00:00 2001 From: Thomas Mueller Date: Fri, 4 Jan 2013 23:03:26 +0100 Subject: [PATCH 3/6] Fallback to use COM or exec('stat ..') is only executed if stat or filesize return negative values. Special case of BSD has been added as stat syntax differs In addition unknown OS is logged --- lib/filestorage/local.php | 47 +++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/lib/filestorage/local.php b/lib/filestorage/local.php index 0bf6ad2d3d..435fcb692a 100644 --- a/lib/filestorage/local.php +++ b/lib/filestorage/local.php @@ -29,22 +29,15 @@ class OC_Filestorage_Local extends OC_Filestorage_Common{ return is_file($this->datadir.$path); } public function stat($path) { - $fullPath = $this->datadir.$path; + $fullPath = $this->datadir . $path; $statResult = stat($fullPath); - // special case for 32-bit systems - if (PHP_INT_SIZE===4) { - if (!(strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')) - $size = (float)exec('stat -c %s '. escapeshellarg ($fullPath)); - else{ - $fsobj = new COM("Scripting.FileSystemObject"); - $f = $fsobj->GetFile($fullPath); - $size = $f->Size; - } - + if ($statResult['size'] < 0) { + $size = self::getFileSizeFromOS($fullPath); $statResult['size'] = $size; $statResult[7] = $size; } + return $statResult; return $statResult; } @@ -59,7 +52,13 @@ class OC_Filestorage_Local extends OC_Filestorage_Common{ if($this->is_dir($path)) { return 0; }else{ - return filesize($this->datadir.$path); + $fullPath = $this->datadir . $path; + $fileSize = filesize($fullPath); + if ($fileSize < 0) { + return self::getFileSizeFromOS($fullPath); + } + + return $fileSize; } } public function isReadable($path) { @@ -173,6 +172,30 @@ class OC_Filestorage_Local extends OC_Filestorage_Common{ return $return; } + private static function getFileSizeFromOS($fullPath) { + $name = strtolower(php_uname('s')); + // Windows OS: we use COM to access the filesystem + if (strpos($name, 'win') !== false) { + if (class_exists('COM')) { + $fsobj = new COM("Scripting.FileSystemObject"); + $f = $fsobj->GetFile($fullPath); + return $f->Size; + } + } else if (strpos($name, 'bsd') !== false) { + if (\OC_Helper::is_function_enabled('exec')) { + return (float)exec('stat -f %z ' . escapeshellarg($fullPath)); + } + } else if (strpos($name, 'linux') !== false) { + if (\OC_Helper::is_function_enabled('exec')) { + return (float)exec('stat -c %s ' . escapeshellarg($fullPath)); + } + } else { + OC_Log::write('core', 'Unknown OS: '.$name, OC_Log::ERROR); + } + + return 0; + } + public function hash($path, $type, $raw=false) { return hash_file($type, $this->datadir.$path, $raw); } From ac50c2f97322cb8ac2631d092327e91758aa75de Mon Sep 17 00:00:00 2001 From: Thomas Mueller Date: Fri, 4 Jan 2013 23:04:23 +0100 Subject: [PATCH 4/6] documentation adjusted on the way --- lib/filestorage/local.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/filestorage/local.php b/lib/filestorage/local.php index 435fcb692a..86cc910d2e 100644 --- a/lib/filestorage/local.php +++ b/lib/filestorage/local.php @@ -230,6 +230,7 @@ class OC_Filestorage_Local extends OC_Filestorage_Common{ /** * check if a file or folder has been updated since $time + * @param string $path * @param int $time * @return bool */ From 2cb66327525472d1b5f287b20d81554c84c2ddbe Mon Sep 17 00:00:00 2001 From: Thomas Mueller Date: Fri, 4 Jan 2013 23:44:01 +0100 Subject: [PATCH 5/6] enhanced log message --- lib/filestorage/local.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/filestorage/local.php b/lib/filestorage/local.php index 86cc910d2e..ca68d6b870 100644 --- a/lib/filestorage/local.php +++ b/lib/filestorage/local.php @@ -190,7 +190,7 @@ class OC_Filestorage_Local extends OC_Filestorage_Common{ return (float)exec('stat -c %s ' . escapeshellarg($fullPath)); } } else { - OC_Log::write('core', 'Unknown OS: '.$name, OC_Log::ERROR); + OC_Log::write('core', 'Unable to determine file size of "'.$fullPath.'". Unknown OS: '.$name, OC_Log::ERROR); } return 0; From da28ae0069b720161deb648028b612563e5f6497 Mon Sep 17 00:00:00 2001 From: Thomas Mueller Date: Sat, 5 Jan 2013 00:09:37 +0100 Subject: [PATCH 6/6] remove duplicate return --- lib/filestorage/local.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/filestorage/local.php b/lib/filestorage/local.php index ca68d6b870..910b3fa039 100644 --- a/lib/filestorage/local.php +++ b/lib/filestorage/local.php @@ -38,8 +38,6 @@ class OC_Filestorage_Local extends OC_Filestorage_Common{ $statResult[7] = $size; } return $statResult; - - return $statResult; } public function filetype($path) { $filetype=filetype($this->datadir.$path);