mirror of
https://github.com/danog/file.git
synced 2024-11-30 04:19:39 +01:00
Use StreamExceptions in Handle, make FilesystemException not extend StreamException
This commit is contained in:
parent
a13bb6f721
commit
d2c1409bf4
@ -3,6 +3,7 @@
|
|||||||
namespace Amp\File;
|
namespace Amp\File;
|
||||||
|
|
||||||
use Amp\ByteStream\ClosedException;
|
use Amp\ByteStream\ClosedException;
|
||||||
|
use Amp\ByteStream\StreamException;
|
||||||
use Amp\Failure;
|
use Amp\Failure;
|
||||||
use Amp\Promise;
|
use Amp\Promise;
|
||||||
use Amp\Success;
|
use Amp\Success;
|
||||||
@ -44,7 +45,7 @@ class BlockingHandle implements Handle {
|
|||||||
return new Success(\strlen($data) ? $data : null);
|
return new Success(\strlen($data) ? $data : null);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Failure(new FilesystemException(
|
return new Failure(new StreamException(
|
||||||
"Failed reading from file handle"
|
"Failed reading from file handle"
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
@ -63,7 +64,7 @@ class BlockingHandle implements Handle {
|
|||||||
return new Success($len);
|
return new Success($len);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Failure(new FilesystemException(
|
return new Failure(new StreamException(
|
||||||
"Failed writing to file handle"
|
"Failed writing to file handle"
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
@ -87,7 +88,7 @@ class BlockingHandle implements Handle {
|
|||||||
*/
|
*/
|
||||||
public function close(): Promise {
|
public function close(): Promise {
|
||||||
if ($this->fh === null) {
|
if ($this->fh === null) {
|
||||||
throw new FilesystemException("The file has already been closed");
|
throw new ClosedException("The file has already been closed");
|
||||||
}
|
}
|
||||||
|
|
||||||
$fh = $this->fh;
|
$fh = $this->fh;
|
||||||
@ -97,7 +98,7 @@ class BlockingHandle implements Handle {
|
|||||||
return new Success;
|
return new Success;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Failure(new FilesystemException(
|
return new Failure(new StreamException(
|
||||||
"Failed closing file handle"
|
"Failed closing file handle"
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
@ -107,7 +108,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 FilesystemException("The file has been closed");
|
throw new ClosedException("The file has been closed");
|
||||||
}
|
}
|
||||||
|
|
||||||
switch ($whence) {
|
switch ($whence) {
|
||||||
@ -115,7 +116,7 @@ class BlockingHandle implements Handle {
|
|||||||
case \SEEK_CUR:
|
case \SEEK_CUR:
|
||||||
case \SEEK_END:
|
case \SEEK_END:
|
||||||
if (@\fseek($this->fh, $position, $whence) === -1) {
|
if (@\fseek($this->fh, $position, $whence) === -1) {
|
||||||
return new Failure(new FilesystemException("Could not seek in file"));
|
return new Failure(new StreamException("Could not seek in file"));
|
||||||
}
|
}
|
||||||
return new Success($this->tell());
|
return new Success($this->tell());
|
||||||
default:
|
default:
|
||||||
@ -130,7 +131,7 @@ class BlockingHandle implements Handle {
|
|||||||
*/
|
*/
|
||||||
public function tell(): int {
|
public function tell(): int {
|
||||||
if ($this->fh === null) {
|
if ($this->fh === null) {
|
||||||
throw new FilesystemException("The file has been closed");
|
throw new ClosedException("The file has been closed");
|
||||||
}
|
}
|
||||||
|
|
||||||
return \ftell($this->fh);
|
return \ftell($this->fh);
|
||||||
@ -141,7 +142,7 @@ class BlockingHandle implements Handle {
|
|||||||
*/
|
*/
|
||||||
public function eof(): bool {
|
public function eof(): bool {
|
||||||
if ($this->fh === null) {
|
if ($this->fh === null) {
|
||||||
throw new FilesystemException("The file has been closed");
|
throw new ClosedException("The file has been closed");
|
||||||
}
|
}
|
||||||
|
|
||||||
return \feof($this->fh);
|
return \feof($this->fh);
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
namespace Amp\File;
|
namespace Amp\File;
|
||||||
|
|
||||||
use Amp\ByteStream\ClosedException;
|
use Amp\ByteStream\ClosedException;
|
||||||
|
use Amp\ByteStream\StreamException;
|
||||||
use Amp\Deferred;
|
use Amp\Deferred;
|
||||||
use Amp\Promise;
|
use Amp\Promise;
|
||||||
use Amp\Success;
|
use Amp\Success;
|
||||||
@ -62,9 +63,12 @@ class EioHandle implements Handle {
|
|||||||
$this->isActive = false;
|
$this->isActive = false;
|
||||||
|
|
||||||
if ($result === -1) {
|
if ($result === -1) {
|
||||||
$deferred->fail(new ClosedException(
|
$error = \eio_get_last_error($req);
|
||||||
sprintf('Reading from file failed: %s.', \eio_get_last_error($req))
|
if ($error === "Bad file descriptor") {
|
||||||
));
|
$deferred->fail(new ClosedException("Reading from the file failed due to a closed handle"));
|
||||||
|
} else {
|
||||||
|
$deferred->fail(new StreamException("Reading from the file failed:" . $error));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$this->position += \strlen($result);
|
$this->position += \strlen($result);
|
||||||
$deferred->resolve(\strlen($result) ? $result : null);
|
$deferred->resolve(\strlen($result) ? $result : null);
|
||||||
@ -145,9 +149,12 @@ class EioHandle implements Handle {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($result === -1) {
|
if ($result === -1) {
|
||||||
$deferred->fail(new ClosedException(
|
$error = \eio_get_last_error($req);
|
||||||
sprintf('Writing to the file failed: %s', \eio_get_last_error($req))
|
if ($error === "Bad file descriptor") {
|
||||||
));
|
$deferred->fail(new ClosedException("Writing to the file failed due to a closed handle"));
|
||||||
|
} else {
|
||||||
|
$deferred->fail(new StreamException("Writing to the file failed: " . $error));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$this->position += $result;
|
$this->position += $result;
|
||||||
if ($this->position > $this->size) {
|
if ($this->position > $this->size) {
|
||||||
@ -172,7 +179,8 @@ class EioHandle implements Handle {
|
|||||||
|
|
||||||
private function onClose($deferred, $result, $req) {
|
private function onClose($deferred, $result, $req) {
|
||||||
if ($result === -1) {
|
if ($result === -1) {
|
||||||
$deferred->fail(new FilesystemException(\eio_get_last_error($req)));
|
$error = \eio_get_last_error($req);
|
||||||
|
$deferred->fail(new StreamException($error));
|
||||||
} else {
|
} else {
|
||||||
$deferred->resolve();
|
$deferred->resolve();
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,7 @@
|
|||||||
|
|
||||||
namespace Amp\File;
|
namespace Amp\File;
|
||||||
|
|
||||||
use Amp\ByteStream\StreamException;
|
class FilesystemException extends \Exception {
|
||||||
|
|
||||||
class FilesystemException extends StreamException {
|
|
||||||
public function __construct(string $message, \Throwable $previous = null) {
|
public function __construct(string $message, \Throwable $previous = null) {
|
||||||
parent::__construct($message, 0, $previous);
|
parent::__construct($message, 0, $previous);
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
namespace Amp\File;
|
namespace Amp\File;
|
||||||
|
|
||||||
use Amp\ByteStream\ClosedException;
|
use Amp\ByteStream\ClosedException;
|
||||||
|
use Amp\ByteStream\StreamException;
|
||||||
use Amp\Coroutine;
|
use Amp\Coroutine;
|
||||||
use Amp\Parallel\Worker\TaskException;
|
use Amp\Parallel\Worker\TaskException;
|
||||||
use Amp\Parallel\Worker\Worker;
|
use Amp\Parallel\Worker\Worker;
|
||||||
@ -110,9 +111,9 @@ class ParallelHandle implements Handle {
|
|||||||
$this->position += \strlen($data);
|
$this->position += \strlen($data);
|
||||||
return $data;
|
return $data;
|
||||||
} catch (TaskException $exception) {
|
} catch (TaskException $exception) {
|
||||||
throw new FilesystemException("Reading from the file failed", $exception);
|
throw new StreamException("Reading from the file failed", 0, $exception);
|
||||||
} catch (WorkerException $exception) {
|
} catch (WorkerException $exception) {
|
||||||
throw new FilesystemException("Sending the task to the worker failed", $exception);
|
throw new StreamException("Sending the task to the worker failed", 0, $exception);
|
||||||
} finally {
|
} finally {
|
||||||
$this->busy = false;
|
$this->busy = false;
|
||||||
}
|
}
|
||||||
@ -159,9 +160,9 @@ class ParallelHandle implements Handle {
|
|||||||
try {
|
try {
|
||||||
$length = yield $this->worker->enqueue(new Internal\FileTask('fwrite', [$data], $this->id));
|
$length = yield $this->worker->enqueue(new Internal\FileTask('fwrite', [$data], $this->id));
|
||||||
} catch (TaskException $exception) {
|
} catch (TaskException $exception) {
|
||||||
throw new FilesystemException("Writing to the file failed", $exception);
|
throw new StreamException("Writing to the file failed", 0, $exception);
|
||||||
} catch (WorkerException $exception) {
|
} catch (WorkerException $exception) {
|
||||||
throw new FilesystemException("Sending the task to the worker failed", $exception);
|
throw new StreamException("Sending the task to the worker failed", 0, $exception);
|
||||||
} finally {
|
} finally {
|
||||||
if (--$this->pendingWrites === 0) {
|
if (--$this->pendingWrites === 0) {
|
||||||
$this->busy = false;
|
$this->busy = false;
|
||||||
@ -205,9 +206,9 @@ class ParallelHandle implements Handle {
|
|||||||
|
|
||||||
return $this->position;
|
return $this->position;
|
||||||
} catch (TaskException $exception) {
|
} catch (TaskException $exception) {
|
||||||
throw new FilesystemException('Seeking in the file failed.', $exception);
|
throw new StreamException('Seeking in the file failed.', 0, $exception);
|
||||||
} catch (WorkerException $exception) {
|
} catch (WorkerException $exception) {
|
||||||
throw new FilesystemException("Sending the task to the worker failed", $exception);
|
throw new StreamException("Sending the task to the worker failed", 0, $exception);
|
||||||
} finally {
|
} finally {
|
||||||
$this->busy = false;
|
$this->busy = false;
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
namespace Amp\File;
|
namespace Amp\File;
|
||||||
|
|
||||||
use Amp\ByteStream\ClosedException;
|
use Amp\ByteStream\ClosedException;
|
||||||
|
use Amp\ByteStream\StreamException;
|
||||||
use Amp\Deferred;
|
use Amp\Deferred;
|
||||||
use Amp\File\Internal\UvPoll;
|
use Amp\File\Internal\UvPoll;
|
||||||
use Amp\Loop;
|
use Amp\Loop;
|
||||||
@ -58,7 +59,12 @@ class UvHandle implements Handle {
|
|||||||
$this->isActive = false;
|
$this->isActive = false;
|
||||||
|
|
||||||
if ($result < 0) {
|
if ($result < 0) {
|
||||||
$deferred->fail(new ClosedException(\uv_strerror($result)));
|
$error = \uv_strerror($result);
|
||||||
|
if ($error === "bad file descriptor") {
|
||||||
|
$deferred->fail(new ClosedException("Reading from the file failed due to a closed handle"));
|
||||||
|
} else {
|
||||||
|
$deferred->fail(new StreamException("Reading from the file failed: " . $error));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$length = strlen($buffer);
|
$length = strlen($buffer);
|
||||||
$this->position = $this->position + $length;
|
$this->position = $this->position + $length;
|
||||||
@ -132,7 +138,12 @@ class UvHandle implements Handle {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($result < 0) {
|
if ($result < 0) {
|
||||||
$deferred->fail(new ClosedException(\uv_strerror($result)));
|
$error = \uv_strerror($result);
|
||||||
|
if ($error === "bad file descriptor") {
|
||||||
|
$deferred->fail(new ClosedException("Writing to the file failed due to a closed handle"));
|
||||||
|
} else {
|
||||||
|
$deferred->fail(new StreamException("Writing to the file failed: " . $error));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
StatCache::clear($this->path);
|
StatCache::clear($this->path);
|
||||||
$newPosition = $this->position + $length;
|
$newPosition = $this->position + $length;
|
||||||
|
Loading…
Reference in New Issue
Block a user