Skip to content

Commit cea157a

Browse files
committed
Merge pull request #20 from jkuchar/fix-19-empty-file
Empty files fix
2 parents 25e62df + 7519c63 commit cea157a

File tree

6 files changed

+47
-18
lines changed

6 files changed

+47
-18
lines changed

src/Driver/ExecDriver.php

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace BigFileTools\Driver;
44
use Brick\Math\BigInteger;
5+
use Brick\Math\Exception\ArithmeticException;
56

67
class ExecDriver implements ISizeDriver
78
{
@@ -41,36 +42,50 @@ public function getFileSize($path)
4142
}
4243
}
4344

45+
/**
46+
* Convert string into integer
47+
* Must be precise number, otherwise you will see and exception.
48+
*
49+
* @param $valueAsString
50+
* @return BigInteger
51+
* @throws Exception
52+
*/
53+
private function convertToInteger($valueAsString) {
54+
if(!is_string($valueAsString)) {
55+
throw new Exception("Cannot convert to integer. Expected string, but got " . gettype($valueAsString). ".");
56+
}
57+
$trimmedInput = trim($valueAsString);
58+
59+
try {
60+
return BigInteger::of($trimmedInput);
61+
62+
} catch (ArithmeticException $e) {
63+
throw new Exception("Returned value cannot be converted to an integer.",0, $e);
64+
}
65+
66+
}
67+
4468
private function getFileSizeWindows($path)
4569
{
4670
$escapedPath = escapeshellarg($path);
47-
$size = trim(exec("for %F in ($escapedPath) do @echo %~zF"));
48-
if ($size AND ctype_digit($size)) {
49-
return BigInteger::of($size);
50-
} else {
51-
throw new Exception("Exec returned invalid value");
52-
}
71+
return $this->convertToInteger(
72+
exec("for %F in ($escapedPath) do @echo %~zF")
73+
);
5374
}
5475

5576
private function getFileSizeLinux($path)
5677
{
5778
$escapedPath = escapeshellarg($path);
58-
$size = trim(exec("stat -Lc%s $escapedPath"));
59-
if ($size AND ctype_digit($size)) {
60-
return BigInteger::of($size);
61-
} else {
62-
throw new Exception("Exec returned invalid value");
63-
}
79+
return $this->convertToInteger(
80+
exec("stat -Lc%s $escapedPath")
81+
);
6482
}
6583

6684
private function getFileSizeMac($path)
6785
{
6886
$escapedPath = escapeshellarg($path);
69-
$size = trim(exec("stat -f%z $escapedPath"));
70-
if ($size AND ctype_digit($size)) {
71-
return BigInteger::of($size);
72-
} else {
73-
throw new Exception("Exec returned invalid value");
74-
}
87+
return $this->convertToInteger(
88+
exec("stat -f%z $escapedPath")
89+
);
7590
}
7691
}

tests/BigFileTools/Driver/BaseDriverTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ protected function setUp()
3434
*/
3535
abstract protected function getDriver();
3636

37+
public function testFileEmpty() {
38+
Assert::equal(
39+
TESTS_EMPTY_FILE_SIZE,
40+
(string) $this->driver->getFileSize(TESTS_EMPTY_FILE_PATH),
41+
"Driver " . get_class($this->getDriver()) . "Failed for file empty file."
42+
);
43+
}
44+
3745
public function testFileSmall_Under2_31bites() {
3846
Assert::equal(
3947
TESTS_SMALL_FILE_SIZE,

tests/bootstrap.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
Tester\Environment::setup();
66

7+
define("TESTS_EMPTY_FILE_PATH", __DIR__ . "/temp/emptyfile.tmp"); // 0B
8+
define("TESTS_EMPTY_FILE_SIZE", "0");
9+
710
define("TESTS_SMALL_FILE_PATH", __DIR__ . "/temp/smallfile.tmp"); // 1M (less then 2^31)
811
define("TESTS_SMALL_FILE_SIZE", "1048576");
912

tests/setup-linux.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
bash tests/cleanup.sh
88

9+
touch tests/temp/emptyfile.tmp
910
truncate -s 1M tests/temp/smallfile.tmp
1011
truncate -s 2050M tests/temp/mediumfile.tmp
1112
truncate -s 4100M tests/temp/bigfile.tmp

tests/setup-osx.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
bash tests/cleanup.sh
88

9+
touch tests/temp/emptyfile.tmp
910
gtruncate -s 1M tests/temp/smallfile.tmp
1011
gtruncate -s 2050M tests/temp/mediumfile.tmp
1112
gtruncate -s 4100M tests/temp/bigfile.tmp

tests/setup.cmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
rem http://stackoverflow.com/a/986041/631369
22
call tests\cleanup.cmd
3+
fsutil file createnew tests\temp\emptyfile.tmp 0
34
fsutil file createnew tests\temp\smallfile.tmp 1048576
45
fsutil file createnew tests\temp\mediumfile.tmp 2149580800
56
fsutil file createnew tests\temp\bigfile.tmp 4299161600

0 commit comments

Comments
 (0)