From 6c9d48073626ce3c31283e7ee0421a8e928fd271 Mon Sep 17 00:00:00 2001 From: Lars Jung Date: Wed, 20 May 2015 18:41:48 +0200 Subject: [PATCH] Start filesize refactoring. --- src/_h5ai/private/php/core/class-filesize.php | 98 +++++++++++++++++++ src/_h5ai/private/php/core/class-util.php | 46 ++------- 2 files changed, 106 insertions(+), 38 deletions(-) create mode 100644 src/_h5ai/private/php/core/class-filesize.php diff --git a/src/_h5ai/private/php/core/class-filesize.php b/src/_h5ai/private/php/core/class-filesize.php new file mode 100644 index 00000000..7afde4cb --- /dev/null +++ b/src/_h5ai/private/php/core/class-filesize.php @@ -0,0 +1,98 @@ + 1) { + if (fseek($handle, $step, SEEK_CUR) === 0) { + $size += $step; + } else { + fseek($handle, -$step, SEEK_CUR); + $step = intval($step / 2, 10); + } + } + + while (fgetc($handle) !== false) { + $size += 1; + } + + fclose($handle); + + return $size; + } + + public function filesize($path) { + + return @filesize($path); + } + + private function read_dir($path) { + + $paths = []; + if (is_dir($path)) { + foreach (scandir($path) as $name) { + if ($name !== '.' && $name !== '..') { + $paths[] = $path . '/' . $name; + } + } + } + return $paths; + } + + private function exec($cmdv) { + + $cmd = implode(' ', array_map('escapeshellarg', $cmdv)); + $lines = []; + $rc = null; + exec($cmd, $lines, $rc); + return $lines; + } + + public function du_paths($paths) { + + $cmdv = array_merge(['du', '-sk'], $paths); + $lines = $this->exec($cmdv); + + $sizes = []; + foreach ($lines as $line) { + $parts = preg_split('/[\s]+/', $line, 2); + $size = intval($parts[0], 10) * 1024; + $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]); + return $sizes[$path]; + } + + public function add($path) { + + $size = 0; + foreach ($this->read_dir($path) as $p) { + $size += $this->filesize($p); + } + return $size; + } +} diff --git a/src/_h5ai/private/php/core/class-util.php b/src/_h5ai/private/php/core/class-util.php index 35c0940c..fbc6a9e1 100644 --- a/src/_h5ai/private/php/core/class-util.php +++ b/src/_h5ai/private/php/core/class-util.php @@ -99,51 +99,21 @@ class Util { if (array_key_exists($path, Util::$size_cache)) { return Util::$size_cache[$path]; } + $fs = new Filesize(); $size = null; if (is_file($path)) { - if (PHP_INT_SIZE < 8) { - $_handle = fopen($path, 'r'); - - $_pos = 0; - $_size = 1073741824; - fseek($_handle, 0, SEEK_SET); - while ($_size > 1) { - fseek($_handle, $_size, SEEK_CUR); - - if (fgetc($_handle) === false) { - fseek($_handle, -$_size, SEEK_CUR); - $_size = (int)($_size / 2); - } else { - fseek($_handle, -1, SEEK_CUR); - $_pos += $_size; - } - } - - while (fgetc($_handle) !== false) { - $_pos++; - } - fclose($_handle); - - $size = $_pos; + $size = $fs->fseek($path); } else { - $size = @filesize($path); + $size = $fs->filesize($path); } - - } else if (is_dir($path)) { - - if ($context->query_option('foldersize.enabled', false)) { - if ($context->get_setup()->get('HAS_CMD_DU') && $context->query_option('foldersize.type', null) === 'shell-du') { - $cmdv = ['du', '-sk', $path]; - $size = intval(preg_replace('#\s.*$#', '', Util::exec_cmdv($cmdv)), 10) * 1024; - } else { - $size = 0; - foreach ($context->read_dir($path) as $name) { - $size += Util::filesize($context, $path . '/' . $name); - } - } + } 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); } }