Move Exceptions to their own namespace

This commit is contained in:
Robin Appelman 2014-08-06 23:15:12 +02:00
commit 5c57d2d094
22 changed files with 233 additions and 123 deletions

View file

@ -7,6 +7,10 @@
namespace Icewind\SMB;
use Icewind\SMB\Exception\AuthenticationException;
use Icewind\SMB\Exception\ConnectionException;
use Icewind\SMB\Exception\InvalidHostException;
class Connection extends RawConnection {
const DELIMITER = 'smb:';
@ -22,12 +26,12 @@ class Connection extends RawConnection {
/**
* get all unprocessed output from smbclient until the next prompt
*
* @throws ConnectionError
* @throws ConnectionException
* @return string
*/
public function read() {
if (!$this->isValid()) {
throw new ConnectionError();
throw new ConnectionException();
}
$line = $this->readLine(); //first line is prompt
$this->checkConnectionError($line);

View file

@ -7,43 +7,6 @@
namespace Icewind\SMB;
class Exception extends \Exception {
}
class ConnectException extends Exception {
}
class ConnectionError extends ConnectException {
}
class AuthenticationException extends ConnectException {
}
class InvalidHostException extends ConnectException {
}
class AccessDeniedException extends ConnectException {
}
class InvalidRequestException extends Exception {
}
class NotFoundException extends InvalidRequestException {
}
class AlreadyExistsException extends InvalidRequestException {
}
class NotEmptyException extends InvalidRequestException {
}
class InvalidTypeException extends InvalidRequestException {
}
class ForbiddenException extends InvalidRequestException {
}
class ErrorCodes {
/**
* connection errors

View file

@ -0,0 +1,10 @@
<?php
/**
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Licensed under the MIT license:
* http://opensource.org/licenses/MIT
*/
namespace Icewind\SMB\Exception;
class AccessDeniedException extends ConnectException {}

View file

@ -0,0 +1,10 @@
<?php
/**
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Licensed under the MIT license:
* http://opensource.org/licenses/MIT
*/
namespace Icewind\SMB\Exception;
class AlreadyExistsException extends InvalidRequestException {}

View file

@ -0,0 +1,10 @@
<?php
/**
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Licensed under the MIT license:
* http://opensource.org/licenses/MIT
*/
namespace Icewind\SMB\Exception;
class AuthenticationException extends ConnectException{}

View file

@ -0,0 +1,10 @@
<?php
/**
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Licensed under the MIT license:
* http://opensource.org/licenses/MIT
*/
namespace Icewind\SMB\Exception;
class ConnectException extends Exception {}

View file

@ -0,0 +1,10 @@
<?php
/**
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Licensed under the MIT license:
* http://opensource.org/licenses/MIT
*/
namespace Icewind\SMB\Exception;
class ConnectionException extends ConnectException {}

View file

@ -0,0 +1,10 @@
<?php
/**
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Licensed under the MIT license:
* http://opensource.org/licenses/MIT
*/
namespace Icewind\SMB\Exception;
class Exception extends \Exception {}

View file

@ -0,0 +1,10 @@
<?php
/**
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Licensed under the MIT license:
* http://opensource.org/licenses/MIT
*/
namespace Icewind\SMB\Exception;
class ForbiddenException extends InvalidRequestException {}

View file

@ -0,0 +1,10 @@
<?php
/**
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Licensed under the MIT license:
* http://opensource.org/licenses/MIT
*/
namespace Icewind\SMB\Exception;
class InvalidHostException extends ConnectException {}

View file

@ -0,0 +1,10 @@
<?php
/**
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Licensed under the MIT license:
* http://opensource.org/licenses/MIT
*/
namespace Icewind\SMB\Exception;
class InvalidRequestException extends Exception {}

View file

@ -0,0 +1,10 @@
<?php
/**
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Licensed under the MIT license:
* http://opensource.org/licenses/MIT
*/
namespace Icewind\SMB\Exception;
class InvalidTypeException extends InvalidRequestException {}

View file

@ -0,0 +1,10 @@
<?php
/**
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Licensed under the MIT license:
* http://opensource.org/licenses/MIT
*/
namespace Icewind\SMB\Exception;
class NotEmptyException extends InvalidRequestException {}

View file

@ -0,0 +1,10 @@
<?php
/**
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Licensed under the MIT license:
* http://opensource.org/licenses/MIT
*/
namespace Icewind\SMB\Exception;
class NotFoundException extends InvalidRequestException {}

View file

@ -34,8 +34,8 @@ class NativeServer extends Server {
/**
* @return \Icewind\SMB\IShare[]
* @throws \Icewind\SMB\AuthenticationException
* @throws \Icewind\SMB\InvalidHostException
* @throws \Icewind\SMB\Exception\AuthenticationException
* @throws \Icewind\SMB\Exception\InvalidHostException
*/
public function listShares() {
$this->connect();

View file

@ -7,8 +7,6 @@
namespace Icewind\SMB;
require_once 'ErrorCodes.php';
class NativeShare implements IShare {
/**
* @var Server $server
@ -36,9 +34,9 @@ class NativeShare implements IShare {
}
/**
* @throws \Icewind\SMB\ConnectionError
* @throws \Icewind\SMB\AuthenticationException
* @throws \Icewind\SMB\InvalidHostException
* @throws \Icewind\SMB\Exception\ConnectionException
* @throws \Icewind\SMB\Exception\AuthenticationException
* @throws \Icewind\SMB\Exception\InvalidHostException
*/
protected function connect() {
if ($this->state and $this->state instanceof NativeShare) {
@ -63,7 +61,7 @@ class NativeShare implements IShare {
}
private function buildUrl($path) {
$url = 'smb://' . $this->server->getHost() . '/' . $this->name;
$url = sprintf('smb://%s/%s', $this->server->getHost(), $this->name);
if ($path) {
$path = trim($path, '/');
$url .= '/' . $path;
@ -77,8 +75,8 @@ class NativeShare implements IShare {
* @param string $path
* @return \Icewind\SMB\IFileInfo[]
*
* @throws \Icewind\SMB\NotFoundException
* @throws \Icewind\SMB\InvalidTypeException
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\InvalidTypeException
*/
public function dir($path) {
$this->connect();
@ -115,8 +113,8 @@ class NativeShare implements IShare {
* @param string $path
* @return bool
*
* @throws \Icewind\SMB\NotFoundException
* @throws \Icewind\SMB\AlreadyExistsException
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\AlreadyExistsException
*/
public function mkdir($path) {
$this->connect();
@ -129,8 +127,8 @@ class NativeShare implements IShare {
* @param string $path
* @return bool
*
* @throws \Icewind\SMB\NotFoundException
* @throws \Icewind\SMB\InvalidTypeException
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\InvalidTypeException
*/
public function rmdir($path) {
$this->connect();
@ -143,8 +141,8 @@ class NativeShare implements IShare {
* @param string $path
* @return bool
*
* @throws \Icewind\SMB\NotFoundException
* @throws \Icewind\SMB\InvalidTypeException
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\InvalidTypeException
*/
public function del($path) {
return $this->state->unlink($this->buildUrl($path));
@ -157,8 +155,8 @@ class NativeShare implements IShare {
* @param string $to
* @return bool
*
* @throws \Icewind\SMB\NotFoundException
* @throws \Icewind\SMB\AlreadyExistsException
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\AlreadyExistsException
*/
public function rename($from, $to) {
$this->connect();
@ -172,8 +170,8 @@ class NativeShare implements IShare {
* @param string $target remove file
* @return bool
*
* @throws \Icewind\SMB\NotFoundException
* @throws \Icewind\SMB\InvalidTypeException
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\InvalidTypeException
*/
public function put($source, $target) {
$this->connect();
@ -195,8 +193,8 @@ class NativeShare implements IShare {
* @param string $target local file
* @return bool
*
* @throws \Icewind\SMB\NotFoundException
* @throws \Icewind\SMB\InvalidTypeException
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\InvalidTypeException
*/
public function get($source, $target) {
$this->connect();
@ -216,8 +214,8 @@ class NativeShare implements IShare {
* @param string $source
* @return resource a read only stream with the contents of the remote file
*
* @throws \Icewind\SMB\NotFoundException
* @throws \Icewind\SMB\InvalidTypeException
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\InvalidTypeException
*/
public function read($source) {
$this->connect();
@ -231,8 +229,8 @@ class NativeShare implements IShare {
* @param string $source
* @return resource a read only stream with the contents of the remote file
*
* @throws \Icewind\SMB\NotFoundException
* @throws \Icewind\SMB\InvalidTypeException
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\InvalidTypeException
*/
public function write($source) {
$this->connect();

View file

@ -7,7 +7,11 @@
namespace Icewind\SMB;
require_once 'ErrorCodes.php';
use Icewind\SMB\Exception\AlreadyExistsException;
use Icewind\SMB\Exception\Exception;
use Icewind\SMB\Exception\ForbiddenException;
use Icewind\SMB\Exception\InvalidTypeException;
use Icewind\SMB\Exception\NotFoundException;
/**
* Low level wrapper for libsmbclient-php for error handling

View file

@ -7,6 +7,8 @@
namespace Icewind\SMB;
use Icewind\SMB\Exception\ConnectionException;
class RawConnection {
/**
* @var resource[] $pipes
@ -37,7 +39,7 @@ class RawConnection {
));
$this->process = proc_open($command, $descriptorSpec, $this->pipes, '/', $env);
if (!$this->isValid()) {
throw new ConnectionError();
throw new ConnectionException();
}
}

View file

@ -7,6 +7,9 @@
namespace Icewind\SMB;
use Icewind\SMB\Exception\AuthenticationException;
use Icewind\SMB\Exception\InvalidHostException;
class Server {
const CLIENT = 'smbclient';
const LOCALE = 'en_US.UTF-8';
@ -77,8 +80,8 @@ class Server {
/**
* @return \Icewind\SMB\IShare[]
*
* @throws \Icewind\SMB\AuthenticationException
* @throws \Icewind\SMB\InvalidHostException
* @throws \Icewind\SMB\Exception\AuthenticationException
* @throws \Icewind\SMB\Exception\InvalidHostException
*/
public function listShares() {
$command = Server::CLIENT . ' --authentication-file=/proc/self/fd/3' .

View file

@ -7,6 +7,13 @@
namespace Icewind\SMB;
use Icewind\SMB\Exception\AccessDeniedException;
use Icewind\SMB\Exception\AlreadyExistsException;
use Icewind\SMB\Exception\ConnectionException;
use Icewind\SMB\Exception\Exception;
use Icewind\SMB\Exception\InvalidTypeException;
use Icewind\SMB\Exception\NotEmptyException;
use Icewind\SMB\Exception\NotFoundException;
use Icewind\Streams\CallbackWrapper;
class Share implements IShare {
@ -37,20 +44,23 @@ class Share implements IShare {
}
/**
* @throws \Icewind\SMB\ConnectionError
* @throws \Icewind\SMB\AuthenticationException
* @throws \Icewind\SMB\InvalidHostException
* @throws \Icewind\SMB\Exception\ConnectionError
* @throws \Icewind\SMB\Exception\AuthenticationException
* @throws \Icewind\SMB\Exception\InvalidHostException
*/
protected function connect() {
if ($this->connection and $this->connection->isValid()) {
return;
}
$command = Server::CLIENT . ' --authentication-file=/proc/self/fd/3' .
' //' . $this->server->getHost() . '/' . $this->name;
$command = sprintf('%s --authentication-file=/proc/self/fd/3 //%s/%s',
Server::CLIENT,
$this->server->getHost(),
$this->name
);
$this->connection = new Connection($command);
$this->connection->writeAuthentication($this->server->getUser(), $this->server->getPassword());
if (!$this->connection->isValid()) {
throw new ConnectionError();
throw new ConnectionException();
}
}
@ -83,8 +93,8 @@ class Share implements IShare {
* @param $path
* @return \Icewind\SMB\IFileInfo[]
*
* @throws \Icewind\SMB\NotFoundException
* @throws \Icewind\SMB\InvalidTypeException
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\InvalidTypeException
*/
public function dir($path) {
$escapedPath = $this->escapePath($path);
@ -146,8 +156,8 @@ class Share implements IShare {
* @param string $path
* @return bool
*
* @throws \Icewind\SMB\NotFoundException
* @throws \Icewind\SMB\AlreadyExistsException
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\AlreadyExistsException
*/
public function mkdir($path) {
return $this->simpleCommand('mkdir', $path);
@ -159,8 +169,8 @@ class Share implements IShare {
* @param string $path
* @return bool
*
* @throws \Icewind\SMB\NotFoundException
* @throws \Icewind\SMB\InvalidTypeException
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\InvalidTypeException
*/
public function rmdir($path) {
return $this->simpleCommand('rmdir', $path);
@ -172,8 +182,8 @@ class Share implements IShare {
* @param string $path
* @return bool
*
* @throws \Icewind\SMB\NotFoundException
* @throws \Icewind\SMB\InvalidTypeException
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\InvalidTypeException
*/
public function del($path) {
//del return a file not found error when trying to delete a folder
@ -200,8 +210,8 @@ class Share implements IShare {
* @param string $to
* @return bool
*
* @throws \Icewind\SMB\NotFoundException
* @throws \Icewind\SMB\AlreadyExistsException
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\AlreadyExistsException
*/
public function rename($from, $to) {
$path1 = $this->escapePath($from);
@ -218,8 +228,8 @@ class Share implements IShare {
* @param string $target remove file
* @return bool
*
* @throws \Icewind\SMB\NotFoundException
* @throws \Icewind\SMB\InvalidTypeException
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\InvalidTypeException
*/
public function put($source, $target) {
$path1 = $this->escapeLocalPath($source); //first path is local, needs different escaping
@ -235,8 +245,8 @@ class Share implements IShare {
* @param string $target local file
* @return bool
*
* @throws \Icewind\SMB\NotFoundException
* @throws \Icewind\SMB\InvalidTypeException
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\InvalidTypeException
*/
public function get($source, $target) {
$path1 = $this->escapePath($source);
@ -251,8 +261,8 @@ class Share implements IShare {
* @param string $source
* @return resource a read only stream with the contents of the remote file
*
* @throws \Icewind\SMB\NotFoundException
* @throws \Icewind\SMB\InvalidTypeException
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\InvalidTypeException
*/
public function read($source) {
$source = $this->escapePath($source);
@ -260,9 +270,12 @@ class Share implements IShare {
$source = str_replace('\'', '\'"\'"\'', $source);
// since returned stream is closed by the caller we need to create a new instance
// since we can't re-use the same file descriptor over multiple calls
$command = Server::CLIENT . ' --authentication-file=/proc/self/fd/3' .
' //' . $this->server->getHost() . '/' . $this->name
. ' -c \'get ' . $source . ' /proc/self/fd/5\'';
$command = sprintf('%s --authentication-file=/proc/self/fd/3 //%s/%s -c \'get %s /proc/self/fd/5\'',
Server::CLIENT,
$this->server->getHost(),
$this->name,
$source
);
$connection = new Connection($command);
$connection->writeAuthentication($this->server->getUser(), $this->server->getPassword());
$fh = $connection->getFileOutputStream();
@ -276,8 +289,8 @@ class Share implements IShare {
* @param string $target
* @return resource a write only stream to upload a remote file
*
* @throws \Icewind\SMB\NotFoundException
* @throws \Icewind\SMB\InvalidTypeException
* @throws \Icewind\SMB\Exception\NotFoundException
* @throws \Icewind\SMB\Exception\InvalidTypeException
*/
public function write($target) {
$target = $this->escapePath($target);
@ -285,9 +298,12 @@ class Share implements IShare {
$target = str_replace('\'', '\'"\'"\'', $target);
// since returned stream is closed by the caller we need to create a new instance
// since we can't re-use the same file descriptor over multiple calls
$command = Server::CLIENT . ' --authentication-file=/proc/self/fd/3' .
' //' . $this->server->getHost() . '/' . $this->name
. ' -c \'put /proc/self/fd/4 ' . $target . '\'';
$command = sprintf('%s --authentication-file=/proc/self/fd/3 //%s/%s -c \'put /proc/self/fd/4 %s\'',
Server::CLIENT,
$this->server->getHost(),
$this->name,
$target
);
$connection = new RawConnection($command);
$connection->writeAuthentication($this->server->getUser(), $this->server->getPassword());
$fh = $connection->getFileInputStream();
@ -368,11 +384,11 @@ class Share implements IShare {
* @param $lines
*
* @throws NotFoundException
* @throws AlreadyExistsException
* @throws AccessDeniedException
* @throws NotEmptyException
* @throws InvalidTypeException
* @throws \Exception
* @throws \Icewind\SMB\Exception\AlreadyExistsException
* @throws \Icewind\SMB\Exception\AccessDeniedException
* @throws \Icewind\SMB\Exception\NotEmptyException
* @throws \Icewind\SMB\Exception\InvalidTypeException
* @throws \Icewind\SMB\Exception\Exception
* @return bool
*/
protected function parseOutput($lines) {