Better state and error handling

This commit is contained in:
Robin Appelman 2014-08-03 01:36:23 +02:00
commit 3a6d77bc78
4 changed files with 508 additions and 123 deletions

View file

@ -40,6 +40,10 @@ class NotEmptyException extends InvalidRequestException {
class InvalidTypeException extends InvalidRequestException {
}
class ForbiddenException extends InvalidRequestException {
}
class ErrorCodes {
/**
* connection errors

View file

@ -9,10 +9,20 @@ namespace Icewind\SMB;
class NativeServer extends Server {
/**
* @var resource
* @var \Icewind\SMB\NativeState
*/
protected $state;
/**
* @param string $host
* @param string $user
* @param string $password
*/
public function __construct($host, $user, $password) {
parent::__construct($host, $user, $password);
$this->state = new NativeState();
}
protected function connect() {
if ($this->state and is_resource($this->state)) {
return;
@ -22,13 +32,7 @@ class NativeServer extends Server {
if (strpos($user, '/')) {
list($workgroup, $user) = explode($user, '/');
}
NativeShare::registerErrorHandler();
$this->state = smbclient_state_new();
$result = smbclient_state_init($this->state, $workgroup, $user, $this->getPassword());
NativeShare::restoreErrorHandler();
if (!$result) {
throw new ConnectionError();
}
$this->state->init($workgroup, $user, $this->getPassword());
}
/**
@ -39,15 +43,13 @@ class NativeServer extends Server {
public function listShares() {
$this->connect();
$shares = array();
NativeShare::registerErrorHandler();
$dh = smbclient_opendir($this->state, 'smb://' . $this->getHost());
while ($share = smbclient_readdir($this->state, $dh)) {
$dh = $this->state->opendir('smb://' . $this->getHost());
while ($share = $this->state->readdir($dh)) {
if ($share['type'] === 'file share') {
$shares[] = $this->getShare($share['name']);
}
}
NativeShare::restoreErrorHandler();
smbclient_closedir($this->state, $dh);
$this->state->closedir($dh);
return $shares;
}

View file

@ -21,26 +21,18 @@ class NativeShare implements IShare {
private $name;
/**
* @var resource $state
* @var \Icewind\SMB\NativeState $state
*/
private $state;
/**
* @param Server $server
* @param string $name
* @throws ConnectionError
*/
public function __construct($server, $name) {
$this->server = $server;
$this->name = $name;
}
public static function registerErrorHandler() {
set_error_handler(array('Icewind\SMB\NativeShare', 'errorHandler'));
}
public static function restoreErrorHandler() {
restore_error_handler();
$this->state = new NativeState();
}
/**
@ -58,10 +50,7 @@ class NativeShare implements IShare {
if (strpos($user, '/')) {
list($workgroup, $user) = explode($user, '/');
}
self::registerErrorHandler();
$this->state = smbclient_state_new();
smbclient_state_init($this->state, $workgroup, $user, $this->server->getPassword());
self::restoreErrorHandler();
$this->state->init($workgroup, $user, $this->server->getPassword());
}
/**
@ -82,35 +71,6 @@ class NativeShare implements IShare {
return $url;
}
public static function errorHandler($errno, $errorString) {
if (strpos($errorString, 'Path does not exist') or strpos($errorString, 'path doesn\'t exist')) {
throw new NotFoundException($errorString);
} else if (strpos($errorString, 'already exists')) {
throw new AlreadyExistsException($errorString);
} else if (strpos($errorString, 'Can\'t write to a directory') or
strpos($errorString, 'use rmdir instead') or
strpos($errorString, 'unknown error (20)') // 20: ENOTDIR
) {
throw new InvalidTypeException($errorString);
} else if (strpos($errorString, 'Workgroup not found') or
strpos($errorString, 'Workgroup or server not found')
) {
throw new InvalidHostException($errorString);
} else if (strpos($errorString, 'Permission denied')) {
throw new AccessDeniedException($errorString);
} else if (strpos($errorString, 'unknown error (110)') or
strpos($errorString, 'unknown error (111)') or
strpos($errorString, 'unknown error (112)') or
strpos($errorString, 'unknown error (113)')
) {
// errors for connection timeout, connection refused, host is down and
// no route to host, respectively
throw new ConnectionError($errorString);
} else {
throw new \Exception($errorString);
}
}
/**
* List the content of a remote folder
*
@ -124,25 +84,21 @@ class NativeShare implements IShare {
$this->connect();
$files = array();
self::registerErrorHandler();
$dh = smbclient_opendir($this->state, $this->buildUrl($path));
while ($file = smbclient_readdir($this->state, $dh)) {
$dh = $this->state->opendir($this->buildUrl($path));
while ($file = $this->state->readdir($dh)) {
$name = $file['name'];
if ($name !== '.' and $name !== '..') {
$files [] = new NativeFileInfo($this, $path . '/' . $name, $name);
}
}
smbclient_closedir($this->state, $dh);
self::restoreErrorHandler();
$this->state->closedir($dh);
return $files;
}
public function stat($path) {
$this->connect();
self::registerErrorHandler();
$stat = smbclient_stat($this->state, $this->buildUrl($path));
self::restoreErrorHandler();
return $stat;
return $this->state->stat($this->buildUrl($path));
}
/**
@ -156,10 +112,7 @@ class NativeShare implements IShare {
*/
public function mkdir($path) {
$this->connect();
self::registerErrorHandler();
$result = smbclient_mkdir($this->state, $this->buildUrl($path));
self::restoreErrorHandler();
return $result;
return $this->state->mkdir($this->buildUrl($path));
}
/**
@ -173,10 +126,7 @@ class NativeShare implements IShare {
*/
public function rmdir($path) {
$this->connect();
self::registerErrorHandler();
$result = smbclient_rmdir($this->state, $this->buildUrl($path));
self::restoreErrorHandler();
return $result;
return $this->state->rmdir($this->buildUrl($path));
}
/**
@ -189,11 +139,7 @@ class NativeShare implements IShare {
* @throws \Icewind\SMB\InvalidTypeException
*/
public function del($path) {
$this->connect();
self::registerErrorHandler();
$result = smbclient_unlink($this->state, $this->buildUrl($path));
self::restoreErrorHandler();
return $result;
return $this->state->unlink($this->buildUrl($path));
}
/**
@ -208,35 +154,7 @@ class NativeShare implements IShare {
*/
public function rename($from, $to) {
$this->connect();
self::registerErrorHandler();
$result = smbclient_rename($this->state, $this->buildUrl($from), $this->state, $this->buildUrl($to));
self::restoreErrorHandler();
return $result;
}
/**
* @param string $path
* @param string $mode
* @return resource
*/
protected function fopen($path, $mode) {
$this->connect();
self::registerErrorHandler();
$result = smbclient_open($this->state, $this->buildUrl($path), $mode);
self::restoreErrorHandler();
return $result;
}
/**
* @param string $path
* @return resource
*/
protected function create($path) {
$this->connect();
self::registerErrorHandler();
$result = smbclient_creat($this->state, $this->buildUrl($path));
self::restoreErrorHandler();
return $result;
return $this->state->rename($this->buildUrl($from), $this->buildUrl($to));
}
/**
@ -250,14 +168,14 @@ class NativeShare implements IShare {
* @throws \Icewind\SMB\InvalidTypeException
*/
public function put($source, $target) {
$this->connect();
$sourceHandle = fopen($source, 'rb');
$targetHandle = $this->create($target);
$targetHandle = $this->state->create($this->buildUrl($target));
self::registerErrorHandler();
while ($data = fread($sourceHandle, 4096)) {
smbclient_write($this->state, $targetHandle, $data);
$this->state->write($targetHandle, $data);
}
smbclient_close($this->state, $targetHandle);
$this->state->close($targetHandle);
restore_error_handler();
return true;
}
@ -273,15 +191,15 @@ class NativeShare implements IShare {
* @throws \Icewind\SMB\InvalidTypeException
*/
public function get($source, $target) {
$sourceHandle = $this->fopen($source, 'r');
$this->connect();
$sourceHandle = $this->state->open($this->buildUrl($source), 'r');
$targetHandle = fopen($target, 'wb');
self::registerErrorHandler();
while ($data = smbclient_read($this->state, $sourceHandle, 4096)) {
while ($data = $this->state->read($sourceHandle, 4096)) {
fwrite($targetHandle, $data);
}
smbclient_close($this->state, $sourceHandle);
restore_error_handler();
$this->state->close($sourceHandle);
return true;
}
/**
@ -294,8 +212,9 @@ class NativeShare implements IShare {
* @throws \Icewind\SMB\InvalidTypeException
*/
public function read($source) {
$handle = $this->fopen($source, 'r');
return NativeStream::wrap($this->state, $handle, 'r');
$this->connect();
$handle = $this->state->open($this->buildUrl($source), 'r');
return NativeStream::wrap($this->state->getState(), $handle, 'r');
}
/**
@ -308,8 +227,9 @@ class NativeShare implements IShare {
* @throws \Icewind\SMB\InvalidTypeException
*/
public function write($source) {
$handle = $this->create($source);
return NativeStream::wrap($this->state, $handle, 'w');
$this->connect();
$handle = $this->state->create($this->buildUrl($source));
return NativeStream::wrap($this->state->getState(), $handle, 'w');
}
/**
@ -321,13 +241,16 @@ class NativeShare implements IShare {
*/
public function getAttribute($path, $attribute) {
$this->connect();
self::registerErrorHandler();
$result = smbclient_getxattr($this->state, $this->buildUrl($path), $attribute);
self::restoreErrorHandler();
$result = $this->state->getxattr($this->buildUrl($path), $attribute);
// parse hex string
if ($attribute === 'system.dos_attr.mode') {
$result = hexdec(substr($result, 2));
}
return $result;
}
public function __destruct() {
unset($this->state);
}
}

456
src/NativeState.php Normal file
View file

@ -0,0 +1,456 @@
<?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;
/**
* Low level wrapper for libsmbclient-php for error handling
*/
class NativeState {
/**
* @var resource
*/
protected $state;
protected $handlerSet = false;
protected $connected = false;
/**
* @return resource
*/
public function getState() {
return $this->state;
}
protected function setErrorHandler() {
if ($this->handlerSet) {
return;
}
$this->handlerSet = true;
$state = $this;
set_error_handler(function ($errorNumber, $errorString) use ($state) {
/**
* @var \Icewind\SMB\NativeState $state
*/
$state->handleError($errorString);
});
}
protected function restoreErrorHandler() {
if (!$this->handlerSet) {
return;
}
$this->handlerSet = false;
restore_error_handler();
}
protected function handleError($errorString = '') {
$error = smbclient_state_errno($this->state);
switch ($error) {
// see error.h
case 0;
return;
case 1:
throw new ForbiddenException();
case 2:
throw new NotFoundException();
case 17:
throw new AlreadyExistsException();
case 20:
throw new InvalidTypeException();
case 21:
throw new InvalidTypeException();
default:
if ($errorString) {
throw new Exception($errorString);
} else {
throw new Exception('Unknown error (' . $error . ')');
}
}
}
/**
* @param string $workGroup
* @param string $user
* @param string $password
* @return bool
*/
public function init($workGroup, $user, $password) {
if ($this->connected) {
return true;
}
$this->state = smbclient_state_new();
$this->setErrorHandler();
$result = smbclient_state_init($this->state, $workGroup, $user, $password);
$this->restoreErrorHandler();
if ($result === false) {
$this->handleError();
}
$this->connected = true;
return $result;
}
/**
* @param string $uri
* @return resource
*/
public function opendir($uri) {
$this->setErrorHandler();
$result = smbclient_opendir($this->state, $uri);
$this->restoreErrorHandler();
if ($result === false) {
$this->handleError();
}
return $result;
}
/**
* @param resource $dir
* @return array
*/
public function readdir($dir) {
$this->setErrorHandler();
$result = smbclient_readdir($this->state, $dir);
$this->restoreErrorHandler();
if ($result === false) {
$this->handleError();
}
return $result;
}
/**
* @param $dir
* @return bool
*/
public function closedir($dir) {
$this->setErrorHandler();
$result = smbclient_closedir($this->state, $dir);
$this->restoreErrorHandler();
if ($result === false) {
$this->handleError();
}
return $result;
}
/**
* @param string $old
* @param string $new
* @return bool
*/
public function rename($old, $new) {
$this->setErrorHandler();
$result = smbclient_rename($this->state, $old, $this->state, $new);
$this->restoreErrorHandler();
if ($result === false) {
$this->handleError();
}
return $result;
}
/**
* @param string $uri
* @return bool
*/
public function unlink($uri) {
$this->setErrorHandler();
$result = smbclient_unlink($this->state, $uri);
$this->restoreErrorHandler();
if ($result === false) {
$this->handleError();
}
return $result;
}
/**
* @param string $uri
* @param int $mask
* @return bool
*/
public function mkdir($uri, $mask = 0777) {
$this->setErrorHandler();
$result = smbclient_mkdir($this->state, $uri, $mask);
$this->restoreErrorHandler();
if ($result === false) {
$this->handleError();
}
return $result;
}
/**
* @param string $uri
* @return bool
*/
public function rmdir($uri) {
$this->setErrorHandler();
$result = smbclient_rmdir($this->state, $uri);
$this->restoreErrorHandler();
if ($result === false) {
$this->handleError();
}
return $result;
}
/**
* @param string $uri
* @return array
*/
public function stat($uri) {
$this->setErrorHandler();
$result = smbclient_stat($this->state, $uri);
$this->restoreErrorHandler();
if ($result === false) {
$this->handleError();
}
return $result;
}
/**
* @param resource $file
* @return array
*/
public function fstat($file) {
$this->setErrorHandler();
$result = smbclient_fstat($this->state, $file);
$this->restoreErrorHandler();
if ($result === false) {
$this->handleError();
}
return $result;
}
/**
* @param string $uri
* @param string $mode
* @param int $mask
* @return resource
*/
public function open($uri, $mode, $mask = 0666) {
$this->setErrorHandler();
$result = smbclient_open($this->state, $uri, $mode, $mask);
$this->restoreErrorHandler();
if ($result === false) {
$this->handleError();
}
return $result;
}
/**
* @param string $uri
* @param int $mask
* @return resource
*/
public function create($uri, $mask = 0666) {
$this->setErrorHandler();
$result = smbclient_creat($this->state, $uri, $mask);
$this->restoreErrorHandler();
if ($result === false) {
$this->handleError();
}
return $result;
}
/**
* @param resource $file
* @param int $bytes
* @return string
*/
public function read($file, $bytes) {
$this->setErrorHandler();
$result = smbclient_read($this->state, $file, $bytes);
$this->restoreErrorHandler();
if ($result === false) {
$this->handleError();
}
return $result;
}
/**
* @param resource $file
* @param string $data
* @param int $length
* @return int
*/
public function write($file, $data, $length = null) {
$this->setErrorHandler();
$result = smbclient_write($this->state, $file, $data, $length);
$this->restoreErrorHandler();
if ($result === false) {
$this->handleError();
}
return $result;
}
/**
* @param resource $file
* @param int $offset
* @param int $whence SEEK_SET | SEEK_CUR | SEEK_END
* @return int
*/
public function lseek($file, $offset, $whence = SEEK_SET) {
$this->setErrorHandler();
$result = smbclient_lseek($this->state, $file, $offset, $whence);
$this->restoreErrorHandler();
if ($result === false) {
$this->handleError();
}
return $result;
}
/**
* @param resource $file
* @param int $size
* @return bool
*/
public function ftruncate($file, $size) {
$this->setErrorHandler();
$result = smbclient_ftruncate($this->state, $file, $size);
$this->restoreErrorHandler();
if ($result === false) {
$this->handleError();
}
return $result;
}
public function close($file) {
$this->setErrorHandler();
$result = smbclient_close($this->state, $file);
$this->restoreErrorHandler();
if ($result === false) {
$this->handleError();
}
return $result;
}
/**
* @param string $uri
* @param string $mode
* @return bool
*/
public function chmod($uri, $mode) {
$this->setErrorHandler();
$result = smbclient_chmod($this->state, $uri, $mode);
$this->restoreErrorHandler();
if ($result === false) {
$this->handleError();
}
return $result;
}
/**
* @param string $uri
* @param int $mtime
* @param int $atime
* @return bool
*/
public function utimes($uri, $mtime = null, $atime = null) {
if ($mtime = null) {
$mtime = time();
}
if ($atime = null) {
$atime = $mtime;
}
$this->setErrorHandler();
$result = smbclient_utimes($this->state, $uri, $mtime, $atime);
$this->restoreErrorHandler();
if ($result === false) {
$this->handleError();
}
return $result;
}
/**
* @param string $uri
* @return array
*/
public function listxattr($uri) {
$this->setErrorHandler();
$result = smbclient_listxattr($this->state, $uri);
$this->restoreErrorHandler();
if ($result === false) {
$this->handleError();
}
return $result;
}
/**
* @param string $uri
* @param string $key
* @return string
*/
public function getxattr($uri, $key) {
$this->setErrorHandler();
$result = smbclient_getxattr($this->state, $uri, $key);
$this->restoreErrorHandler();
if ($result === false) {
$this->handleError();
}
return $result;
}
/**
* @param string $uri
* @param string $key
* @param string $value
* @param int $flags
* @return mixed
*/
public function setxattr($uri, $key, $value, $flags = 0) {
$this->setErrorHandler();
$result = smbclient_setxattr($this->state, $uri, $key, $value, $flags);
$this->restoreErrorHandler();
if ($result === false) {
$this->handleError();
}
return $result;
}
/**
* @param string $uri
* @param string $key
* @return bool
*/
public function removexattr($uri, $key) {
$this->setErrorHandler();
$result = smbclient_removexattr($this->state, $uri, $key);
$this->restoreErrorHandler();
if ($result === false) {
$this->handleError();
}
return $result;
}
public function __destruct() {
if ($this->connected) {
smbclient_state_free($this->state);
}
$this->restoreErrorHandler();
}
}