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); abstract public function run($arguments);
/** /**
* @param array $lines * @param $lines
* @return mixed * @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 * @param string $string

View file

@ -13,24 +13,4 @@ class Del extends Simple {
parent::__construct($connection); parent::__construct($connection);
$this->command = 'del'; $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); $output = $this->execute('get ' . $path1 . ' ' . $path2);
return $this->parseOutput($output); 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); parent::__construct($connection);
$this->command = 'mkdir'; $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 { } else {
if (strpos($lines[0], 'does not exist')) { if (strpos($lines[0], 'does not exist')) {
throw new \SMB\NotFoundException(); throw new \SMB\NotFoundException();
} } else {
list($error,) = explode(' ', $lines[0]); parent::parseOutput($lines);
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,23 +13,4 @@ class Rename extends Double {
parent::__construct($connection); parent::__construct($connection);
$this->command = 'rename'; $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); parent::__construct($connection);
$this->command = 'rmdir'; $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 { class Connection {
const CLIENT = 'smbclient'; const CLIENT = 'smbclient';
const LOCALE = 'en_US.UTF-8';
/** /**
* @var string $host * @var string $host

View file

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

View file

@ -43,10 +43,11 @@ class Share {
$descriptorSpec = array( $descriptorSpec = array(
0 => array("pipe", "r"), 0 => array("pipe", "r"),
1 => array("pipe", "w"), 1 => array("pipe", "w"),
// 1 => array("file", "/tmp/smbout", 'a'),
2 => array("file", "/tmp/smberror", "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() . $command = Connection::CLIENT . ' -N -U ' . $this->connection->getAuthString() .
' //' . $this->connection->getHost() . '/' . $this->name; ' //' . $this->connection->getHost() . '/' . $this->name;
$this->process = proc_open($command, $descriptorSpec, $this->pipes, null, array( $this->process = proc_open($command, $descriptorSpec, $this->pipes, null, array(
@ -55,7 +56,6 @@ class Share {
if (!is_resource($this->process)) { if (!is_resource($this->process)) {
throw new ConnectionError(); throw new ConnectionError();
} }
// stream_set_blocking($this->pipes[1], 0);
} }
public function __destruct() { public function __destruct() {
@ -152,7 +152,7 @@ class Share {
* @return array * @return array
*/ */
public function read() { public function read() {
fgets($this->pipes[1]);//first line is prompt fgets($this->pipes[1]); //first line is prompt
$output = array(); $output = array();
$line = fgets($this->pipes[1]); $line = fgets($this->pipes[1]);
while (substr($line, 0, 4) !== 'smb:') { //next prompt functions as delimiter while (substr($line, 0, 4) !== 'smb:') { //next prompt functions as delimiter