This commit is contained in:
Robin Appelman 2023-08-10 15:02:01 +02:00
commit 9d57f5aad9
4 changed files with 317 additions and 19 deletions

View file

@ -8,6 +8,7 @@
namespace Icewind\SMB\Native;
use Icewind\SMB\Exception\AlreadyExistsException;
use Icewind\SMB\Exception\ConnectionException;
use Icewind\SMB\Exception\ConnectionRefusedException;
use Icewind\SMB\Exception\ConnectionResetException;
use Icewind\SMB\Exception\Exception;
@ -32,9 +33,6 @@ class NativeState {
/** @var resource|null */
protected $state = null;
/** @var bool */
protected $handlerSet = false;
/** @var bool */
protected $connected = false;
@ -67,7 +65,9 @@ class NativeState {
];
protected function handleError(?string $path): void {
/** @var int $error */
if (!$this->state) {
return;
}
$error = smbclient_state_errno($this->state);
if ($error === 0) {
return;
@ -120,7 +120,6 @@ class NativeState {
// __deconstruct() of KerberosAuth should not caled too soon
$this->auth = $auth;
/** @var bool $result */
$result = @smbclient_state_init($this->state, $auth->getWorkgroup(), $auth->getUsername(), $auth->getPassword());
$this->testResult($result, '');
@ -133,6 +132,9 @@ class NativeState {
* @return resource
*/
public function opendir(string $uri) {
if (!$this->state) {
throw new ConnectionException("Not connected");
}
/** @var resource $result */
$result = @smbclient_opendir($this->state, $uri);
@ -146,6 +148,9 @@ class NativeState {
* @return array{"type": string, "comment": string, "name": string}|false
*/
public function readdir($dir, string $path) {
if (!$this->state) {
throw new ConnectionException("Not connected");
}
/** @var array{"type": string, "comment": string, "name": string}|false $result */
$result = @smbclient_readdir($this->state, $dir);
@ -159,8 +164,10 @@ class NativeState {
* @return bool
*/
public function closedir($dir, string $path): bool {
/** @var bool $result */
$result = smbclient_closedir($this->state, $dir);
if (!$this->state) {
throw new ConnectionException("Not connected");
}
$result = @smbclient_closedir($this->state, $dir);
$this->testResult($result, $path);
return $result;
@ -172,7 +179,9 @@ class NativeState {
* @return bool
*/
public function rename(string $old, string $new): bool {
/** @var bool $result */
if (!$this->state) {
throw new ConnectionException("Not connected");
}
$result = @smbclient_rename($this->state, $old, $this->state, $new);
$this->testResult($result, $new);
@ -184,7 +193,9 @@ class NativeState {
* @return bool
*/
public function unlink(string $uri): bool {
/** @var bool $result */
if (!$this->state) {
throw new ConnectionException("Not connected");
}
$result = @smbclient_unlink($this->state, $uri);
$this->testResult($result, $uri);
@ -197,7 +208,9 @@ class NativeState {
* @return bool
*/
public function mkdir(string $uri, int $mask = 0777): bool {
/** @var bool $result */
if (!$this->state) {
throw new ConnectionException("Not connected");
}
$result = @smbclient_mkdir($this->state, $uri, $mask);
$this->testResult($result, $uri);
@ -209,7 +222,9 @@ class NativeState {
* @return bool
*/
public function rmdir(string $uri): bool {
/** @var bool $result */
if (!$this->state) {
throw new ConnectionException("Not connected");
}
$result = @smbclient_rmdir($this->state, $uri);
$this->testResult($result, $uri);
@ -221,6 +236,9 @@ class NativeState {
* @return array{"mtime": int, "size": int, "mode": int}
*/
public function stat(string $uri): array {
if (!$this->state) {
throw new ConnectionException("Not connected");
}
/** @var array{"mtime": int, "size": int, "mode": int} $result */
$result = @smbclient_stat($this->state, $uri);
@ -234,6 +252,9 @@ class NativeState {
* @return array{"mtime": int, "size": int, "mode": int}
*/
public function fstat($file, string $path): array {
if (!$this->state) {
throw new ConnectionException("Not connected");
}
/** @var array{"mtime": int, "size": int, "mode": int} $result */
$result = @smbclient_fstat($this->state, $file);
@ -248,6 +269,9 @@ class NativeState {
* @return resource
*/
public function open(string $uri, string $mode, int $mask = 0666) {
if (!$this->state) {
throw new ConnectionException("Not connected");
}
/** @var resource $result */
$result = @smbclient_open($this->state, $uri, $mode, $mask);
@ -261,6 +285,9 @@ class NativeState {
* @return resource
*/
public function create(string $uri, int $mask = 0666) {
if (!$this->state) {
throw new ConnectionException("Not connected");
}
/** @var resource $result */
$result = @smbclient_creat($this->state, $uri, $mask);
@ -275,6 +302,9 @@ class NativeState {
* @return string
*/
public function read($file, int $bytes, string $path): string {
if (!$this->state) {
throw new ConnectionException("Not connected");
}
/** @var string $result */
$result = @smbclient_read($this->state, $file, $bytes);
@ -290,10 +320,19 @@ class NativeState {
* @return int
*/
public function write($file, string $data, string $path, ?int $length = null): int {
/** @var int $result */
$result = @smbclient_write($this->state, $file, $data, $length);
if (!$this->state) {
throw new ConnectionException("Not connected");
}
if ($length) {
$result = @smbclient_write($this->state, $file, $data, $length);
} else {
$result = @smbclient_write($this->state, $file, $data);
}
$this->testResult($result, $path);
if ($result === false) {
return 0;
}
return $result;
}
@ -302,10 +341,18 @@ class NativeState {
* @param int $offset
* @param int $whence SEEK_SET | SEEK_CUR | SEEK_END
* @param string|null $path
* @return int|false new file offset as measured from the start of the file on success.
*
* @return false|int new file offset as measured from the start of the file on success.
*/
public function lseek($file, int $offset, int $whence = SEEK_SET, string $path = null) {
/** @var int|false $result */
if (!$this->state) {
throw new ConnectionException("Not connected");
}
// psalm doesn't think int|false == int|false for some reason, so we do a needless annotation to help it out
/**
* @psalm-suppress UnnecessaryVarAnnotation
* @var int|false $result
*/
$result = @smbclient_lseek($this->state, $file, $offset, $whence);
$this->testResult($result, $path);
@ -319,7 +366,9 @@ class NativeState {
* @return bool
*/
public function ftruncate($file, int $size, string $path): bool {
/** @var bool $result */
if (!$this->state) {
throw new ConnectionException("Not connected");
}
$result = @smbclient_ftruncate($this->state, $file, $size);
$this->testResult($result, $path);
@ -332,7 +381,9 @@ class NativeState {
* @return bool
*/
public function close($file, string $path): bool {
/** @var bool $result */
if (!$this->state) {
return false;
}
$result = @smbclient_close($this->state, $file);
$this->testResult($result, $path);
@ -345,6 +396,9 @@ class NativeState {
* @return string
*/
public function getxattr(string $uri, string $key) {
if (!$this->state) {
throw new ConnectionException("Not connected");
}
/** @var string $result */
$result = @smbclient_getxattr($this->state, $uri, $key);
@ -360,6 +414,9 @@ class NativeState {
* @return bool
*/
public function setxattr(string $uri, string $key, string $value, int $flags = 0) {
if (!$this->state) {
throw new ConnectionException("Not connected");
}
/** @var bool $result */
$result = @smbclient_setxattr($this->state, $uri, $key, $value, $flags);
@ -368,7 +425,7 @@ class NativeState {
}
public function __destruct() {
if ($this->connected) {
if ($this->connected && $this->state) {
if (smbclient_state_free($this->state) === false) {
throw new Exception("Failed to free smb state");
}