1
0
mirror of https://github.com/danog/file.git synced 2024-11-30 04:19:39 +01:00

Function for parsing mode

This commit is contained in:
Aaron Piotrowski 2017-05-17 22:34:27 -05:00
parent 6ad1b8e55f
commit cadc86d85a

View File

@ -30,21 +30,8 @@ class UvDriver implements Driver {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function open(string $path, string $mode): Promise { public function open(string $path, string $mode): Promise {
switch ($mode) { $flags = $this->parseMode($mode);
case "r": $flags = \UV::O_RDONLY; break;
case "r+": $flags = \UV::O_RDWR; break;
case "w": $flags = \UV::O_WRONLY | \UV::O_CREAT; break;
case "w+": $flags = \UV::O_RDWR | \UV::O_CREAT; break;
case "a": $flags = \UV::O_WRONLY | \UV::O_CREAT | \UV::O_APPEND; break;
case "a+": $flags = \UV::O_RDWR | \UV::O_CREAT | \UV::O_APPEND; break;
case "x": $flags = \UV::O_WRONLY | \UV::O_CREAT | \UV::O_EXCL; break;
case "x+": $flags = \UV::O_RDWR | \UV::O_CREAT | \UV::O_EXCL; break;
case "c": $flags = \UV::O_WRONLY | \UV::O_CREAT; break;
case "c+": $flags = \UV::O_RDWR | \UV::O_CREAT; break;
default: return new Failure(new FilesystemException(
"Invalid open mode"
));
}
$chmod = ($flags & \UV::O_CREAT) ? 0644 : 0; $chmod = ($flags & \UV::O_CREAT) ? 0644 : 0;
$this->driver->reference($this->busy); $this->driver->reference($this->busy);
$deferred = new Deferred; $deferred = new Deferred;
@ -64,6 +51,26 @@ class UvDriver implements Driver {
return $deferred->promise(); return $deferred->promise();
} }
private function parseMode(string $mode): int {
$mode = \str_replace(['b', 't'], '', $mode);
switch ($mode) {
case "r": return \UV::O_RDONLY;
case "r+": return \UV::O_RDWR;
case "w": return \UV::O_WRONLY | \UV::O_CREAT;
case "w+": return \UV::O_RDWR | \UV::O_CREAT;
case "a": return \UV::O_WRONLY | \UV::O_CREAT | \UV::O_APPEND;
case "a+": return \UV::O_RDWR | \UV::O_CREAT | \UV::O_APPEND;
case "x": return \UV::O_WRONLY | \UV::O_CREAT | \UV::O_EXCL;
case "x+": return \UV::O_RDWR | \UV::O_CREAT | \UV::O_EXCL;
case "c": return \UV::O_WRONLY | \UV::O_CREAT;
case "c+": return \UV::O_RDWR | \UV::O_CREAT;
default:
throw new \Error('Invalid file mode');
}
}
private function onOpenHandle($fh, array $openArr) { private function onOpenHandle($fh, array $openArr) {
list($mode) = $openArr; list($mode) = $openArr;
if ($mode[0] === "w") { if ($mode[0] === "w") {