diff --git a/src/Driver/ExecDriver.php b/src/Driver/ExecDriver.php index 3349858..64df7ce 100644 --- a/src/Driver/ExecDriver.php +++ b/src/Driver/ExecDriver.php @@ -2,6 +2,7 @@ namespace BigFileTools\Driver; use Brick\Math\BigInteger; +use Brick\Math\Exception\ArithmeticException; class ExecDriver implements ISizeDriver { @@ -41,36 +42,50 @@ public function getFileSize($path) } } + /** + * Convert string into integer + * Must be precise number, otherwise you will see and exception. + * + * @param $valueAsString + * @return BigInteger + * @throws Exception + */ + private function convertToInteger($valueAsString) { + if(!is_string($valueAsString)) { + throw new Exception("Cannot convert to integer. Expected string, but got " . gettype($valueAsString). "."); + } + $trimmedInput = trim($valueAsString); + + try { + return BigInteger::of($trimmedInput); + + } catch (ArithmeticException $e) { + throw new Exception("Returned value cannot be converted to an integer.",0, $e); + } + + } + private function getFileSizeWindows($path) { $escapedPath = escapeshellarg($path); - $size = trim(exec("for %F in ($escapedPath) do @echo %~zF")); - if ($size AND ctype_digit($size)) { - return BigInteger::of($size); - } else { - throw new Exception("Exec returned invalid value"); - } + return $this->convertToInteger( + exec("for %F in ($escapedPath) do @echo %~zF") + ); } private function getFileSizeLinux($path) { $escapedPath = escapeshellarg($path); - $size = trim(exec("stat -Lc%s $escapedPath")); - if ($size AND ctype_digit($size)) { - return BigInteger::of($size); - } else { - throw new Exception("Exec returned invalid value"); - } + return $this->convertToInteger( + exec("stat -Lc%s $escapedPath") + ); } private function getFileSizeMac($path) { $escapedPath = escapeshellarg($path); - $size = trim(exec("stat -f%z $escapedPath")); - if ($size AND ctype_digit($size)) { - return BigInteger::of($size); - } else { - throw new Exception("Exec returned invalid value"); - } + return $this->convertToInteger( + exec("stat -f%z $escapedPath") + ); } } \ No newline at end of file diff --git a/tests/BigFileTools/Driver/BaseDriverTest.php b/tests/BigFileTools/Driver/BaseDriverTest.php index d7f61e8..c423f12 100644 --- a/tests/BigFileTools/Driver/BaseDriverTest.php +++ b/tests/BigFileTools/Driver/BaseDriverTest.php @@ -34,6 +34,14 @@ protected function setUp() */ abstract protected function getDriver(); + public function testFileEmpty() { + Assert::equal( + TESTS_EMPTY_FILE_SIZE, + (string) $this->driver->getFileSize(TESTS_EMPTY_FILE_PATH), + "Driver " . get_class($this->getDriver()) . "Failed for file empty file." + ); + } + public function testFileSmall_Under2_31bites() { Assert::equal( TESTS_SMALL_FILE_SIZE, diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 8a2b8cb..7fae6f2 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -4,6 +4,9 @@ Tester\Environment::setup(); +define("TESTS_EMPTY_FILE_PATH", __DIR__ . "/temp/emptyfile.tmp"); // 0B +define("TESTS_EMPTY_FILE_SIZE", "0"); + define("TESTS_SMALL_FILE_PATH", __DIR__ . "/temp/smallfile.tmp"); // 1M (less then 2^31) define("TESTS_SMALL_FILE_SIZE", "1048576"); diff --git a/tests/setup-linux.sh b/tests/setup-linux.sh index e13b8fd..283af42 100644 --- a/tests/setup-linux.sh +++ b/tests/setup-linux.sh @@ -6,6 +6,7 @@ bash tests/cleanup.sh +touch tests/temp/emptyfile.tmp truncate -s 1M tests/temp/smallfile.tmp truncate -s 2050M tests/temp/mediumfile.tmp truncate -s 4100M tests/temp/bigfile.tmp diff --git a/tests/setup-osx.sh b/tests/setup-osx.sh index da4def7..acac62c 100644 --- a/tests/setup-osx.sh +++ b/tests/setup-osx.sh @@ -6,6 +6,7 @@ bash tests/cleanup.sh +touch tests/temp/emptyfile.tmp gtruncate -s 1M tests/temp/smallfile.tmp gtruncate -s 2050M tests/temp/mediumfile.tmp gtruncate -s 4100M tests/temp/bigfile.tmp diff --git a/tests/setup.cmd b/tests/setup.cmd index 0be5556..3920450 100644 --- a/tests/setup.cmd +++ b/tests/setup.cmd @@ -1,5 +1,6 @@ rem http://stackoverflow.com/a/986041/631369 call tests\cleanup.cmd +fsutil file createnew tests\temp\emptyfile.tmp 0 fsutil file createnew tests\temp\smallfile.tmp 1048576 fsutil file createnew tests\temp\mediumfile.tmp 2149580800 fsutil file createnew tests\temp\bigfile.tmp 4299161600