some code cleanup

This commit is contained in:
Robin Appelman 2012-12-29 22:38:47 +01:00
commit 71e917bbae
10 changed files with 32 additions and 114 deletions

View file

@ -34,10 +34,30 @@ abstract class Command {
abstract public function run($arguments);
/**
* @param array $lines
* @return mixed
* @param $lines
* @return bool
*/
abstract protected function parseOutput($lines);
protected function parseOutput($lines) {
if (count($lines) === 0) {
return true;
} else {
list($error,) = explode(' ', $lines[0]);
switch ($error) {
case 'NT_STATUS_OBJECT_PATH_NOT_FOUND':
case 'NT_STATUS_OBJECT_NAME_NOT_FOUND':
case 'NT_STATUS_NO_SUCH_FILE':
throw new \SMB\NotFoundException();
case 'NT_STATUS_OBJECT_NAME_COLLISION':
throw new \SMB\AlreadyExistsException();
case 'NT_STATUS_ACCESS_DENIED':
throw new \SMB\AccessDeniedException();
case 'NT_STATUS_DIRECTORY_NOT_EMPTY':
throw new \SMB\NotEmptyException();
default:
throw new \Exception();
}
}
}
/**
* @param string $string

View file

@ -13,24 +13,4 @@ class Del extends Simple {
parent::__construct($connection);
$this->command = 'del';
}
/**
* @param $lines
* @return bool
*/
protected function parseOutput($lines) {
if (count($lines) === 0) {
return true;
} else {
list($error,) = explode(' ', $lines[0]);
switch ($error) {
case 'NT_STATUS_OBJECT_PATH_NOT_FOUND':
case 'NT_STATUS_OBJECT_NAME_NOT_FOUND':
case 'NT_STATUS_NO_SUCH_FILE':
throw new \SMB\NotFoundException();
default:
throw new \Exception();
}
}
}
}

View file

@ -15,23 +15,4 @@ class Get extends Command {
$output = $this->execute('get ' . $path1 . ' ' . $path2);
return $this->parseOutput($output);
}
/**
* @param $lines
* @return bool
*/
protected function parseOutput($lines) {
if (count($lines) === 0) {
return true;
} else {
list($error,) = explode(' ', $lines[0]);
switch ($error) {
case 'NT_STATUS_OBJECT_PATH_NOT_FOUND':
case 'NT_STATUS_OBJECT_NAME_NOT_FOUND':
throw new \SMB\NotFoundException();
default:
throw new \Exception();
}
}
}
}

View file

@ -13,25 +13,4 @@ class Mkdir extends Simple {
parent::__construct($connection);
$this->command = 'mkdir';
}
/**
* @param $lines
* @return bool
*/
protected function parseOutput($lines) {
if (count($lines) ===0) {
return true;
} else {
list($error,) = explode(' ', $lines[0]);
switch ($error) {
case 'NT_STATUS_OBJECT_PATH_NOT_FOUND':
case 'NT_STATUS_OBJECT_NAME_NOT_FOUND':
throw new \SMB\NotFoundException();
case 'NT_STATUS_OBJECT_NAME_COLLISION':
throw new \SMB\AlreadyExistsException();
default:
throw new \Exception();
}
}
}
}

View file

@ -26,14 +26,8 @@ class Put extends Command {
} else {
if (strpos($lines[0], 'does not exist')) {
throw new \SMB\NotFoundException();
}
list($error,) = explode(' ', $lines[0]);
switch ($error) {
case 'NT_STATUS_OBJECT_PATH_NOT_FOUND':
case 'NT_STATUS_OBJECT_NAME_NOT_FOUND':
throw new \SMB\NotFoundException();
default:
throw new \Exception();
} else {
parent::parseOutput($lines);
}
}
}

View file

@ -13,23 +13,4 @@ class Rename extends Double {
parent::__construct($connection);
$this->command = 'rename';
}
/**
* @param $lines
* @return bool
*/
protected function parseOutput($lines) {
if (count($lines) === 0) {
return true;
} else {
list($error,) = explode(' ', $lines[0]);
switch ($error) {
case 'NT_STATUS_OBJECT_PATH_NOT_FOUND':
case 'NT_STATUS_OBJECT_NAME_NOT_FOUND':
throw new \SMB\NotFoundException();
default:
throw new \Exception();
}
}
}
}

View file

@ -13,25 +13,4 @@ class Rmdir extends Simple {
parent::__construct($connection);
$this->command = 'rmdir';
}
/**
* @param $lines
* @return bool
*/
protected function parseOutput($lines) {
if (count($lines) === 0) {
return true;
} else {
list($error,) = explode(' ', $lines[0]);
switch ($error) {
case 'NT_STATUS_OBJECT_PATH_NOT_FOUND':
case 'NT_STATUS_OBJECT_NAME_NOT_FOUND':
throw new \SMB\NotFoundException();
case 'NT_STATUS_DIRECTORY_NOT_EMPTY':
throw new \SMB\NotEmptyException();
default:
throw new \Exception();
}
}
}
}

View file

@ -10,6 +10,7 @@ namespace SMB;
class Connection {
const CLIENT = 'smbclient';
const LOCALE = 'en_US.UTF-8';
/**
* @var string $host

View file

@ -19,3 +19,6 @@ class NotEmptyException extends \Exception {
class ConnectionError extends \Exception {
}
class AccessDeniedException extends \Exception {
}

View file

@ -43,10 +43,11 @@ class Share {
$descriptorSpec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
// 1 => array("file", "/tmp/smbout", 'a'),
2 => array("file", "/tmp/smberror", "a")
);
putenv('LC_ALL=' . Connection::LOCALE);
setlocale(LC_ALL, Connection::LOCALE);
$command = Connection::CLIENT . ' -N -U ' . $this->connection->getAuthString() .
' //' . $this->connection->getHost() . '/' . $this->name;
$this->process = proc_open($command, $descriptorSpec, $this->pipes, null, array(
@ -55,7 +56,6 @@ class Share {
if (!is_resource($this->process)) {
throw new ConnectionError();
}
// stream_set_blocking($this->pipes[1], 0);
}
public function __destruct() {