Skip to content

Commit

Permalink
Merge pull request #20 from jkuchar/fix-19-empty-file
Browse files Browse the repository at this point in the history
Empty files fix
  • Loading branch information
jkuchar committed Mar 17, 2016
2 parents 25e62df + 7519c63 commit cea157a
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 18 deletions.
51 changes: 33 additions & 18 deletions src/Driver/ExecDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BigFileTools\Driver;
use Brick\Math\BigInteger;
use Brick\Math\Exception\ArithmeticException;

class ExecDriver implements ISizeDriver
{
Expand Down Expand Up @@ -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")
);
}
}
8 changes: 8 additions & 0 deletions tests/BigFileTools/Driver/BaseDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
1 change: 1 addition & 0 deletions tests/setup-linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions tests/setup-osx.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions tests/setup.cmd
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit cea157a

Please sign in to comment.