From 628836a201da4ad026e58c38aa389d6632619139 Mon Sep 17 00:00:00 2001 From: Lars Jung Date: Wed, 22 Jun 2016 16:54:32 +0200 Subject: [PATCH] PHP filesize. --- src/_h5ai/private/php/core/class-filesize.php | 85 ++++++++++--------- src/_h5ai/private/php/core/class-util.php | 27 +----- 2 files changed, 49 insertions(+), 63 deletions(-) diff --git a/src/_h5ai/private/php/core/class-filesize.php b/src/_h5ai/private/php/core/class-filesize.php index e9860a45..62e98b13 100644 --- a/src/_h5ai/private/php/core/class-filesize.php +++ b/src/_h5ai/private/php/core/class-filesize.php @@ -1,40 +1,26 @@ size($path, $withFoldersize, $withDu); } - public function fseek($path) { - $size = 0; - $step = 1073741824; - - $handle = fopen($path, 'r'); - fseek($handle, 0, SEEK_SET); - - while ($step > 1) { - fseek($handle, $step, SEEK_CUR); - if (fgetc($handle) !== false) { - $size += $step + 1; - } else { - fseek($handle, -$step, SEEK_CUR); - $step = intval($step / 2, 10); - } + public static function getCachedSize($path, $withFoldersize, $withDu) { + if (array_key_exists($path, Filesize::$cache)) { + return Filesize::$cache[$path]; } - while (fgetc($handle) !== false) { - $size += 1; - } - - fclose($handle); + $size = Filesize::getSize($path, $withFoldersize, $withDu); + Filesize::$cache[$path] = $size; return $size; } - public function filesize($path) { - return @filesize($path); - } + + private function __construct() {} private function read_dir($path) { $paths = []; @@ -48,6 +34,22 @@ class Filesize { return $paths; } + private function php_filesize($path, $recursive = false) { + // if (PHP_INT_SIZE < 8) { + // } + $size = @filesize($path); + + if (!is_dir($path) || !$recursive) { + return $size; + } + + foreach ($this->read_dir($path) as $p) { + $size += $this->php_filesize($p, true); + } + return $size; + } + + private function exec($cmdv) { $cmd = implode(' ', array_map('escapeshellarg', $cmdv)); $lines = []; @@ -56,34 +58,39 @@ class Filesize { return $lines; } - public function du_paths($paths) { - $cmdv = array_merge(['du', '-sk'], $paths); + private function exec_du_all($paths) { + $cmdv = array_merge(['du', '-sb'], $paths); $lines = $this->exec($cmdv); $sizes = []; foreach ($lines as $line) { $parts = preg_split('/[\s]+/', $line, 2); - $size = intval($parts[0], 10) * 1024; + $size = intval($parts[0], 10); $path = $parts[1]; $sizes[$path] = $size; } return $sizes; } - public function du_dir($path) { - return $this->du_paths($this->read_dir($path)); - } - - public function du_path($path) { - $sizes = $this->du_paths([$path]); + private function exec_du($path) { + $sizes = $this->exec_du_all([$path]); return $sizes[$path]; } - public function add($path) { - $size = 0; - foreach ($this->read_dir($path) as $p) { - $size += $this->filesize($p); + + private function size($path, $withFoldersize = false, $withDu = false) { + if (is_file($path)) { + return $this->php_filesize($path); } - return $size; + + if (is_dir($path) && $withFoldersize) { + if ($withDu) { + return $this->exec_du($path); + } + + return $this->php_filesize($path, true); + } + + return null; } } diff --git a/src/_h5ai/private/php/core/class-util.php b/src/_h5ai/private/php/core/class-util.php index a1c2d36e..c1416354 100644 --- a/src/_h5ai/private/php/core/class-util.php +++ b/src/_h5ai/private/php/core/class-util.php @@ -81,31 +81,10 @@ class Util { return false; } - private static $size_cache = []; - public static function filesize($context, $path) { - if (array_key_exists($path, Util::$size_cache)) { - return Util::$size_cache[$path]; - } - $fs = new Filesize(); + $withFoldersize = $context->query_option('foldersize.enabled', false); + $withDu = $context->get_setup()->get('HAS_CMD_DU') && $context->query_option('foldersize.type', null) === 'shell-du'; + return Filesize::getCachedSize($path, $withFoldersize, $withDu); - $size = null; - - if (is_file($path)) { - if (PHP_INT_SIZE < 8) { - $size = $fs->fseek($path); - } else { - $size = $fs->filesize($path); - } - } else if (is_dir($path) && $context->query_option('foldersize.enabled', false)) { - if ($context->get_setup()->get('HAS_CMD_DU') && $context->query_option('foldersize.type', null) === 'shell-du') { - $size = $fs->du_path($path); - } else { - $size = $fs->add($path); - } - } - - Util::$size_cache[$path] = $size; - return $size; } }