Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for files with utf8 names #24

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/Driver/ComDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ public function __construct()
public function getFileSize($path)
{
// Use the Windows COM interface
$fsobj = new \COM('Scripting.FileSystemObject');
if (dirname($path) == '.')
$this->path = ((substr(getcwd(), -1) == DIRECTORY_SEPARATOR) ? getcwd() . basename($path) : getcwd() . DIRECTORY_SEPARATOR . basename($path));
$fsobj = new \COM('Scripting.FileSystemObject', null, CP_UTF8);
if (dirname($path) == '.') {
$path = ((substr(getcwd(), -1) == DIRECTORY_SEPARATOR) ? getcwd() . basename($path) : getcwd() . DIRECTORY_SEPARATOR . basename($path));
}
$f = $fsobj->GetFile($path);
return BigInteger::of($f->Size);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Driver/CurlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public function getFileSize($path)
if ($data !== false && preg_match('/Content-Length: (\d+)/', $data, $matches)) {
return BigInteger::of($matches[1]);
}
throw new Exception("Curl haven't returned file size.");
throw new Exception("Curl haven't returned file size. File probably does not exist.");
}
}
2 changes: 1 addition & 1 deletion src/Driver/ExecDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private function convertToInteger($valueAsString) {
return BigInteger::of($trimmedInput);

} catch (ArithmeticException $e) {
throw new Exception("Returned value cannot be converted to an integer.",0, $e);
throw new Exception("Returned value cannot be converted to an integer.", 0, $e);
}

}
Expand Down
8 changes: 8 additions & 0 deletions tests/BigFileTools/Driver/BaseDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public function testFileEmptyWithSpace() {
);
}

public function testFileEmptyWithUtf8InName() {
Assert::equal(
TESTS_EMPTY_FILE_SIZE,
(string) $this->driver->getFileSize(TESTS_EMPTY_FILE_WITH_UTF8_PATH),
"Driver " . get_class($this->getDriver()) . "Failed for file empty file with UTF-8 characters in name."
);
}

public function testFileSmall_Under2_31bites() {
Assert::equal(
TESTS_SMALL_FILE_SIZE,
Expand Down
10 changes: 10 additions & 0 deletions tests/BigFileTools/Driver/CurlDriverTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace BigFileTools\Driver;

use Tester\Environment;

$container = require __DIR__ . "/../../bootstrap.php";

class CurlDriverTest extends BaseDriverTest
Expand All @@ -15,6 +17,14 @@ class CurlDriverTest extends BaseDriverTest
{
return new CurlDriver();
}

public function testFileEmptyWithUtf8InName()
{
if (isWindows()) {
Environment::skip("CURL does not support UTF-8 on Windows.");
}
parent::testFileEmptyWithUtf8InName();
}
}

(new CurlDriverTest())->run();
11 changes: 11 additions & 0 deletions tests/BigFileTools/Driver/ExecDriverTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace BigFileTools\Driver;

use Tester\Environment;

$container = require __DIR__ . "/../../bootstrap.php";

class ExecDriverTest extends BaseDriverTest
Expand All @@ -15,6 +17,15 @@ class ExecDriverTest extends BaseDriverTest
{
return new ExecDriver();
}

public function testFileEmptyWithUtf8InName()
{
if (isWindows()) {
// @link http://stackoverflow.com/questions/13332321/php-exec-in-unicode-mode
Environment::skip("PHP does not support UTF-8 in commandline on Windows.");
}
parent::testFileEmptyWithUtf8InName();
}
}

(new ExecDriverTest())->run();
11 changes: 11 additions & 0 deletions tests/BigFileTools/Driver/NativeReadDriverTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace BigFileTools\Driver;

use Tester\Environment;

$container = require __DIR__ . "/../../bootstrap.php";

class NativeReadDriverTest extends BaseDriverTest
Expand All @@ -15,6 +17,15 @@ class NativeReadDriverTest extends BaseDriverTest
{
return new NativeReadDriver();
}

public function testFileEmptyWithUtf8InName()
{
if (isWindows()) {
// @link http://stackoverflow.com/questions/6467501/php-how-to-create-unicode-filenames
Environment::skip("PHP does not support UTF-8 in filenames on Windows.");
}
parent::testFileEmptyWithUtf8InName();
}
}

(new NativeReadDriverTest())->run();
10 changes: 10 additions & 0 deletions tests/BigFileTools/Driver/NativeSeekDriverTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace BigFileTools\Driver;

use BigFileTools\Utils;
use Tester\Assert;
use Tester\Environment;

$container = require __DIR__ . "/../../bootstrap.php";

Expand All @@ -30,6 +31,15 @@ class NativeSeekDriverTest extends BaseDriverTest
parent::testFileBig_LargerThen2_32bites();
}
}

public function testFileEmptyWithUtf8InName()
{
if (isWindows()) {
// @link http://stackoverflow.com/questions/6467501/php-how-to-create-unicode-filenames
Environment::skip("PHP does not support UTF-8 in filenames on Windows.");
}
parent::testFileEmptyWithUtf8InName();
}
}

(new NativeSeekDriverTest())->run();
4 changes: 4 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

Tester\Environment::setup();

function isWindows() {
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
}

define("TESTS_EMPTY_FILE_PATH", __DIR__ . "/temp/emptyfile.tmp"); // 0B
define("TESTS_EMPTY_FILE_WITH_SPACE_PATH", __DIR__ . "/temp/empty - file.tmp"); // 0B with with space in name
define("TESTS_EMPTY_FILE_WITH_UTF8_PATH", __DIR__ . "/temp/emptyfileěšů指事字.tmp"); // 0B with utf-8
Expand Down
1 change: 1 addition & 0 deletions tests/php-windows.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extension_dir = "./ext"
extension=php_com_dotnet.dll

; for testing CURL driver
extension=php_mbstring.dll
extension=php_curl.dll

date.timezone = "Europe/Prague"
Expand Down
1 change: 1 addition & 0 deletions tests/setup-linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ bash tests/cleanup.sh

touch tests/temp/emptyfile.tmp
touch "tests/temp/empty - file.tmp"
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 @@ -8,6 +8,7 @@ bash tests/cleanup.sh

touch tests/temp/emptyfile.tmp
touch "tests/temp/empty - file.tmp"
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
7 changes: 6 additions & 1 deletion tests/setup.cmd
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
rem http://stackoverflow.com/a/986041/631369
call tests\cleanup.cmd

rem UTF-8 support: http://stackoverflow.com/a/18088002/631369
chcp 65001

rem http://stackoverflow.com/a/986041/631369
fsutil file createnew tests\temp\emptyfile.tmp 0
fsutil file createnew "tests\temp\empty - file.tmp" 0
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