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
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) {
$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;
}
}

View file

@ -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;
}
}