ensure we only initialize the state once

This commit is contained in:
Robin Appelman 2018-01-18 15:44:45 +01:00
commit 1f650a5fbf

View file

@ -34,7 +34,6 @@ class NativeShare extends AbstractShare {
parent::__construct();
$this->server = $server;
$this->name = $name;
$this->state = new NativeState();
}
/**
@ -42,12 +41,14 @@ class NativeShare extends AbstractShare {
* @throws \Icewind\SMB\Exception\AuthenticationException
* @throws \Icewind\SMB\Exception\InvalidHostException
*/
protected function connect() {
if ($this->state and $this->state instanceof NativeShare) {
return;
protected function getState() {
if ($this->state and $this->state instanceof NativeState) {
return $this->state;
}
$this->state = new NativeState();
$this->state->init($this->server->getWorkgroup(), $this->server->getUser(), $this->server->getPassword());
return $this->state;
}
/**
@ -60,7 +61,6 @@ class NativeShare extends AbstractShare {
}
private function buildUrl($path) {
$this->connect();
$this->verifyPath($path);
$url = sprintf('smb://%s/%s', $this->server->getHost(), $this->name);
if ($path) {
@ -83,15 +83,15 @@ class NativeShare extends AbstractShare {
public function dir($path) {
$files = array();
$dh = $this->state->opendir($this->buildUrl($path));
while ($file = $this->state->readdir($dh)) {
$dh = $this->getState()->opendir($this->buildUrl($path));
while ($file = $this->getState()->readdir($dh)) {
$name = $file['name'];
if ($name !== '.' and $name !== '..') {
$files [] = new NativeFileInfo($this, $path . '/' . $name, $name);
}
}
$this->state->closedir($dh);
$this->getState()->closedir($dh);
return $files;
}
@ -104,7 +104,7 @@ class NativeShare extends AbstractShare {
}
public function getStat($path) {
return $this->state->stat($this->buildUrl($path));
return $this->getState()->stat($this->buildUrl($path));
}
/**
@ -117,7 +117,7 @@ class NativeShare extends AbstractShare {
* @throws \Icewind\SMB\Exception\AlreadyExistsException
*/
public function mkdir($path) {
return $this->state->mkdir($this->buildUrl($path));
return $this->getState()->mkdir($this->buildUrl($path));
}
/**
@ -130,7 +130,7 @@ class NativeShare extends AbstractShare {
* @throws \Icewind\SMB\Exception\InvalidTypeException
*/
public function rmdir($path) {
return $this->state->rmdir($this->buildUrl($path));
return $this->getState()->rmdir($this->buildUrl($path));
}
/**
@ -143,7 +143,7 @@ class NativeShare extends AbstractShare {
* @throws \Icewind\SMB\Exception\InvalidTypeException
*/
public function del($path) {
return $this->state->unlink($this->buildUrl($path));
return $this->getState()->unlink($this->buildUrl($path));
}
/**
@ -157,7 +157,7 @@ class NativeShare extends AbstractShare {
* @throws \Icewind\SMB\Exception\AlreadyExistsException
*/
public function rename($from, $to) {
return $this->state->rename($this->buildUrl($from), $this->buildUrl($to));
return $this->getState()->rename($this->buildUrl($from), $this->buildUrl($to));
}
/**
@ -172,12 +172,12 @@ class NativeShare extends AbstractShare {
*/
public function put($source, $target) {
$sourceHandle = fopen($source, 'rb');
$targetHandle = $this->state->create($this->buildUrl($target));
$targetHandle = $this->getState()->create($this->buildUrl($target));
while ($data = fread($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
$this->state->write($targetHandle, $data);
$this->getState()->write($targetHandle, $data);
}
$this->state->close($targetHandle);
$this->getState()->close($targetHandle);
return true;
}
@ -208,16 +208,16 @@ class NativeShare extends AbstractShare {
throw new InvalidResourceException('Failed opening local file "' . $target . '" for writing: ' . $reason);
}
$sourceHandle = $this->state->open($this->buildUrl($source), 'r');
$sourceHandle = $this->getState()->open($this->buildUrl($source), 'r');
if (!$sourceHandle) {
fclose($targetHandle);
throw new InvalidResourceException('Failed opening remote file "' . $source . '" for reading');
}
while ($data = $this->state->read($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
while ($data = $this->getState()->read($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
fwrite($targetHandle, $data);
}
$this->state->close($sourceHandle);
$this->getState()->close($sourceHandle);
return true;
}
@ -232,8 +232,8 @@ class NativeShare extends AbstractShare {
*/
public function read($source) {
$url = $this->buildUrl($source);
$handle = $this->state->open($url, 'r');
return NativeReadStream::wrap($this->state, $handle, 'r', $url);
$handle = $this->getState()->open($url, 'r');
return NativeReadStream::wrap($this->getState(), $handle, 'r', $url);
}
/**
@ -247,8 +247,8 @@ class NativeShare extends AbstractShare {
*/
public function write($source) {
$url = $this->buildUrl($source);
$handle = $this->state->create($url);
return NativeWriteStream::wrap($this->state, $handle, 'w', $url);
$handle = $this->getState()->create($url);
return NativeWriteStream::wrap($this->getState(), $handle, 'w', $url);
}
/**
@ -259,7 +259,7 @@ class NativeShare extends AbstractShare {
* @return string the attribute value
*/
public function getAttribute($path, $attribute) {
return $this->state->getxattr($this->buildUrl($path), $attribute);
return $this->getState()->getxattr($this->buildUrl($path), $attribute);
}
/**
@ -276,7 +276,7 @@ class NativeShare extends AbstractShare {
$value = '0x' . dechex($value);
}
return $this->state->setxattr($this->buildUrl($path), $attribute, $value);
return $this->getState()->setxattr($this->buildUrl($path), $attribute, $value);
}
/**