PHP filesize.

This commit is contained in:
Lars Jung 2016-06-22 16:54:32 +02:00
parent 00f9ee09d3
commit 628836a201
2 changed files with 49 additions and 63 deletions

View file

@ -1,40 +1,26 @@
<?php <?php
class Filesize { class Filesize {
private $cache = []; private static $cache = [];
public function __construct() { public static function getSize($path, $withFoldersize, $withDu) {
$fs = new Filesize();
return $fs->size($path, $withFoldersize, $withDu);
} }
public function fseek($path) { public static function getCachedSize($path, $withFoldersize, $withDu) {
$size = 0; if (array_key_exists($path, Filesize::$cache)) {
$step = 1073741824; return Filesize::$cache[$path];
$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);
}
} }
while (fgetc($handle) !== false) { $size = Filesize::getSize($path, $withFoldersize, $withDu);
$size += 1;
}
fclose($handle);
Filesize::$cache[$path] = $size;
return $size; return $size;
} }
public function filesize($path) {
return @filesize($path); private function __construct() {}
}
private function read_dir($path) { private function read_dir($path) {
$paths = []; $paths = [];
@ -48,6 +34,22 @@ class Filesize {
return $paths; 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) { private function exec($cmdv) {
$cmd = implode(' ', array_map('escapeshellarg', $cmdv)); $cmd = implode(' ', array_map('escapeshellarg', $cmdv));
$lines = []; $lines = [];
@ -56,34 +58,39 @@ class Filesize {
return $lines; return $lines;
} }
public function du_paths($paths) { private function exec_du_all($paths) {
$cmdv = array_merge(['du', '-sk'], $paths); $cmdv = array_merge(['du', '-sb'], $paths);
$lines = $this->exec($cmdv); $lines = $this->exec($cmdv);
$sizes = []; $sizes = [];
foreach ($lines as $line) { foreach ($lines as $line) {
$parts = preg_split('/[\s]+/', $line, 2); $parts = preg_split('/[\s]+/', $line, 2);
$size = intval($parts[0], 10) * 1024; $size = intval($parts[0], 10);
$path = $parts[1]; $path = $parts[1];
$sizes[$path] = $size; $sizes[$path] = $size;
} }
return $sizes; return $sizes;
} }
public function du_dir($path) { private function exec_du($path) {
return $this->du_paths($this->read_dir($path)); $sizes = $this->exec_du_all([$path]);
}
public function du_path($path) {
$sizes = $this->du_paths([$path]);
return $sizes[$path]; return $sizes[$path];
} }
public function add($path) {
$size = 0; private function size($path, $withFoldersize = false, $withDu = false) {
foreach ($this->read_dir($path) as $p) { if (is_file($path)) {
$size += $this->filesize($p); 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;
} }
} }

View file

@ -81,31 +81,10 @@ class Util {
return false; return false;
} }
private static $size_cache = [];
public static function filesize($context, $path) { public static function filesize($context, $path) {
if (array_key_exists($path, Util::$size_cache)) { $withFoldersize = $context->query_option('foldersize.enabled', false);
return Util::$size_cache[$path]; $withDu = $context->get_setup()->get('HAS_CMD_DU') && $context->query_option('foldersize.type', null) === 'shell-du';
} return Filesize::getCachedSize($path, $withFoldersize, $withDu);
$fs = new Filesize();
$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;
} }
} }