1
0
mirror of https://github.com/danog/file.git synced 2025-01-22 13:21:13 +01:00

Merge branch 'master' into amp_v2

This commit is contained in:
Aaron Piotrowski 2017-01-11 00:05:18 -06:00
commit 78ec5cc634
7 changed files with 95 additions and 14 deletions

View File

@ -1,3 +1,8 @@
### 0.1.3
- Add recursive mkdir()
- EioDriver and UvDriver now pass O_TRUNC in file\put()
### 0.1.2
- Make UvHandle throw FilesystemException instead of \RuntimeException

View File

@ -233,8 +233,8 @@ class BlockingDriver implements Driver {
/**
* {@inheritdoc}
*/
public function mkdir(string $path, int $mode = 0644): Promise {
return new Success((bool) @\mkdir($path, $mode));
public function mkdir(string $path, int $mode = 0644, bool $recursive = false): Promise {
return new Success((bool) @\mkdir($path, $mode, $recursive));
}
/**

View File

@ -148,9 +148,10 @@ interface Driver {
*
* @param string $path
* @param int $mode
* @param bool $recursive
* @return \Interop\Async\Promise
*/
public function mkdir(string $path, int $mode = 0644): Promise;
public function mkdir(string $path, int $mode = 0644, bool $recursive = false): Promise;
/**
* Delete a directory

View File

@ -378,11 +378,40 @@ class EioDriver implements Driver {
/**
* {@inheritdoc}
*/
public function mkdir(string $path, int $mode = 0644): Promise {
public function mkdir(string $path, int $mode = 0644, bool $recursive = false): Promise {
($this->incrementor)(1);
$deferred = new Deferred;
$priority = \EIO_PRI_DEFAULT;
\eio_mkdir($path, $mode, $priority, [$this, "onGenericResult"], $deferred);
if ($recursive) {
$path = str_replace("/", DIRECTORY_SEPARATOR, $path);
$arrayPath = array_filter(explode(DIRECTORY_SEPARATOR, $path));
$tmpPath = "";
$callback = function() use (
&$callback, &$arrayPath, &$tmpPath, $mode, $priority, $deferred
) {
$tmpPath .= DIRECTORY_SEPARATOR . array_shift($arrayPath);
if (empty($arrayPath)) {
\eio_mkdir($tmpPath, $mode, $priority, [$this, "onGenericResult"], $deferred);
} else {
$this->isdir($tmpPath)->when(function ($error, $result) use (
$callback, $tmpPath, $mode, $priority
) {
if ($result) {
$callback();
} else {
\eio_mkdir($tmpPath, $mode, $priority, $callback);
}
});
}
};
$callback();
} else {
\eio_mkdir($path, $mode, $priority, [$this, "onGenericResult"], $deferred);
}
return $deferred->promise();
}

View File

@ -336,13 +336,45 @@ class UvDriver implements Driver {
/**
* {@inheritdoc}
*/
public function mkdir(string $path, int $mode = 0644): Promise {
public function mkdir(string $path, int $mode = 0644, bool $recursive = false): Promise {
$this->driver->reference($this->busy);
$deferred = new Deferred;
\uv_fs_mkdir($this->loop, $path, $mode, function($fh) use ($deferred) {
$this->driver->unreference($this->busy);
$deferred->resolve((bool)$fh);
});
if ($recursive) {
$path = str_replace("/", DIRECTORY_SEPARATOR, $path);
$arrayPath = array_filter(explode(DIRECTORY_SEPARATOR, $path));
$tmpPath = "";
$callback = function() use (
&$callback, &$arrayPath, &$tmpPath, $mode, $deferred
) {
$tmpPath .= DIRECTORY_SEPARATOR . array_shift($arrayPath);
if (empty($arrayPath)) {
\uv_fs_mkdir($this->loop, $tmpPath, $mode, function($fh) use ($deferred) {
$this->driver->unreference($this->busy);
$deferred->resolve((bool) $fh);
});
} else {
$this->isdir($tmpPath)->when(function ($error, $result) use (
$callback, $tmpPath, $mode
) {
if ($result) {
$callback();
} else {
\uv_fs_mkdir($this->loop, $tmpPath, $mode, $callback);
}
});
}
};
$callback();
} else {
\uv_fs_mkdir($this->loop, $path, $mode, function($fh) use ($deferred) {
$this->driver->unreference($this->busy);
$deferred->resolve((bool) $fh);
});
}
return $deferred->promise();
}

View File

@ -231,10 +231,11 @@ function unlink(string $path): Promise {
*
* @param string $path
* @param int $mode
* @param bool $recursive
* @return \Interop\Async\Promise<null>
*/
function mkdir(string $path, int $mode = 0644): Promise {
return filesystem()->mkdir($path, $mode);
function mkdir(string $path, int $mode = 0644, bool $recursive = false): Promise {
return filesystem()->mkdir($path, $mode, $recursive);
}
/**

View File

@ -7,6 +7,7 @@ use Interop\Async\Loop;
abstract class DriverTest extends \PHPUnit_Framework_TestCase {
private static $fixtureId;
private static $umask;
private static function getFixturePath() {
if (empty(self::$fixtureId)) {
@ -33,6 +34,7 @@ abstract class DriverTest extends \PHPUnit_Framework_TestCase {
$fixtureDir = self::getFixturePath();
self::clearFixtureDir();
self::$umask = umask(0022);
if (!\mkdir($fixtureDir, $mode = 0777, $recursive = true)) {
throw new \RuntimeException(
@ -53,6 +55,7 @@ abstract class DriverTest extends \PHPUnit_Framework_TestCase {
public static function tearDownAfterClass() {
self::clearFixtureDir();
umask(self::$umask);
}
protected function setUp() {
@ -249,8 +252,11 @@ abstract class DriverTest extends \PHPUnit_Framework_TestCase {
$this->lRun(function () {
$fixtureDir = self::getFixturePath();
$toUnlink = "{$fixtureDir}/unlink";
$mask = umask(062);
yield file\put($toUnlink, "unlink me");
$this->assertTrue((bool) (yield file\stat($toUnlink)));
umask($mask);
$stat = (yield file\stat($toUnlink));
$this->assertTrue(($stat["mode"] & 0777) == 0604);
yield file\unlink($toUnlink);
$this->assertNull(yield file\stat($toUnlink));
});
@ -263,9 +269,16 @@ abstract class DriverTest extends \PHPUnit_Framework_TestCase {
$dir = "{$fixtureDir}/newdir";
yield file\mkdir($dir);
yield file\stat($dir);
$stat = yield file\stat($dir);
$this->assertTrue(($stat["mode"] & 0777) === 0644);
yield file\rmdir($dir);
$this->assertNull(yield file\stat($dir));
$dir = "{$fixtureDir}/newdir/with/recursive/creation";
yield file\mkdir($dir, 0764, true); // the umask is 022 by default
$stat = yield file\stat($dir);
$this->assertTrue(($stat["mode"] & 0777) == 0744);
});
}