mirror of
https://github.com/danog/file.git
synced 2024-11-30 04:19:39 +01:00
Throw FilesytemException on closed file
We may wish to change this later, but at least it is consistent between drivers now.
This commit is contained in:
parent
477683f58f
commit
4f9630298f
@ -31,7 +31,7 @@ class BlockingHandle implements Handle {
|
|||||||
*/
|
*/
|
||||||
public function read(int $length = self::DEFAULT_READ_LENGTH): Promise {
|
public function read(int $length = self::DEFAULT_READ_LENGTH): Promise {
|
||||||
if ($this->fh === null) {
|
if ($this->fh === null) {
|
||||||
throw new \Error("The file has been closed");
|
throw new FilesystemException("The file has been closed");
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = \fread($this->fh, $length);
|
$data = \fread($this->fh, $length);
|
||||||
@ -49,7 +49,7 @@ class BlockingHandle implements Handle {
|
|||||||
*/
|
*/
|
||||||
public function write(string $data): Promise {
|
public function write(string $data): Promise {
|
||||||
if ($this->fh === null) {
|
if ($this->fh === null) {
|
||||||
throw new \Error("The file has been closed");
|
throw new FilesystemException("The file has been closed");
|
||||||
}
|
}
|
||||||
|
|
||||||
$len = \fwrite($this->fh, $data);
|
$len = \fwrite($this->fh, $data);
|
||||||
@ -76,13 +76,13 @@ class BlockingHandle implements Handle {
|
|||||||
*/
|
*/
|
||||||
public function close(): Promise {
|
public function close(): Promise {
|
||||||
if ($this->fh === null) {
|
if ($this->fh === null) {
|
||||||
throw new \Error("The file has already been closed");
|
throw new FilesystemException("The file has already been closed");
|
||||||
}
|
}
|
||||||
|
|
||||||
$fh = $this->fh;
|
$fh = $this->fh;
|
||||||
$this->fh = null;
|
$this->fh = null;
|
||||||
|
|
||||||
if (\fclose($fh)) {
|
if (@\fclose($fh)) {
|
||||||
return new Success;
|
return new Success;
|
||||||
} else {
|
} else {
|
||||||
return new Failure(new FilesystemException(
|
return new Failure(new FilesystemException(
|
||||||
@ -96,7 +96,7 @@ class BlockingHandle implements Handle {
|
|||||||
*/
|
*/
|
||||||
public function seek(int $position, int $whence = \SEEK_SET): Promise {
|
public function seek(int $position, int $whence = \SEEK_SET): Promise {
|
||||||
if ($this->fh === null) {
|
if ($this->fh === null) {
|
||||||
throw new \Error("The file has been closed");
|
throw new FilesystemException("The file has been closed");
|
||||||
}
|
}
|
||||||
|
|
||||||
switch ($whence) {
|
switch ($whence) {
|
||||||
@ -119,7 +119,7 @@ class BlockingHandle implements Handle {
|
|||||||
*/
|
*/
|
||||||
public function tell(): int {
|
public function tell(): int {
|
||||||
if ($this->fh === null) {
|
if ($this->fh === null) {
|
||||||
throw new \Error("The file has been closed");
|
throw new FilesystemException("The file has been closed");
|
||||||
}
|
}
|
||||||
|
|
||||||
return \ftell($this->fh);
|
return \ftell($this->fh);
|
||||||
@ -130,7 +130,7 @@ class BlockingHandle implements Handle {
|
|||||||
*/
|
*/
|
||||||
public function eof(): bool {
|
public function eof(): bool {
|
||||||
if ($this->fh === null) {
|
if ($this->fh === null) {
|
||||||
throw new \Error("The file has been closed");
|
throw new FilesystemException("The file has been closed");
|
||||||
}
|
}
|
||||||
|
|
||||||
return \feof($this->fh);
|
return \feof($this->fh);
|
||||||
|
@ -80,7 +80,7 @@ class ParallelHandle implements Handle {
|
|||||||
|
|
||||||
public function read(int $length = self::DEFAULT_READ_LENGTH): Promise {
|
public function read(int $length = self::DEFAULT_READ_LENGTH): Promise {
|
||||||
if ($this->id === null) {
|
if ($this->id === null) {
|
||||||
throw new \Error("The file has been closed");
|
throw new FilesystemException("The file has been closed");
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Coroutine($this->doRead($length));
|
return new Coroutine($this->doRead($length));
|
||||||
@ -105,7 +105,7 @@ class ParallelHandle implements Handle {
|
|||||||
*/
|
*/
|
||||||
public function write(string $data): Promise {
|
public function write(string $data): Promise {
|
||||||
if ($this->id === null) {
|
if ($this->id === null) {
|
||||||
throw new \Error("The file has been closed");
|
throw new FilesystemException("The file has been closed");
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Coroutine($this->doWrite($data));
|
return new Coroutine($this->doWrite($data));
|
||||||
@ -143,7 +143,7 @@ class ParallelHandle implements Handle {
|
|||||||
*/
|
*/
|
||||||
public function seek(int $offset, int $whence = SEEK_SET): Promise {
|
public function seek(int $offset, int $whence = SEEK_SET): Promise {
|
||||||
if ($this->id === null) {
|
if ($this->id === null) {
|
||||||
throw new \Error("The file has been closed");
|
throw new FilesystemException("The file has been closed");
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Coroutine($this->doSeek($offset, $whence));
|
return new Coroutine($this->doSeek($offset, $whence));
|
||||||
|
@ -144,10 +144,14 @@ abstract class HandleTest extends TestCase {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \Amp\File\FilesystemException
|
||||||
|
*/
|
||||||
public function testClose() {
|
public function testClose() {
|
||||||
$this->execute(function () {
|
$this->execute(function () {
|
||||||
$handle = yield File\open(__FILE__, "r");
|
$handle = yield File\open(__FILE__, "r");
|
||||||
yield $handle->close();
|
yield $handle->close();
|
||||||
|
yield $handle->read();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user