mirror of
https://codeberg.org/icewind/SMB.git
synced 2026-06-03 17:24:07 +02:00
add basic smb notify support
at the moment this only works when using the smbclient based backend
This commit is contained in:
parent
c50d9aa6a5
commit
cf23de2702
4 changed files with 56 additions and 2 deletions
|
|
@ -28,6 +28,7 @@ class Connection extends RawConnection {
|
|||
/**
|
||||
* get all unprocessed output from smbclient until the next prompt
|
||||
*
|
||||
* @param callable $callback (optional) callback to call for every line read
|
||||
* @return string
|
||||
* @throws AuthenticationException
|
||||
* @throws ConnectException
|
||||
|
|
@ -35,7 +36,7 @@ class Connection extends RawConnection {
|
|||
* @throws InvalidHostException
|
||||
* @throws NoLoginServerException
|
||||
*/
|
||||
public function read() {
|
||||
public function read(callable $callback = null) {
|
||||
if (!$this->isValid()) {
|
||||
throw new ConnectionException('Connection not valid');
|
||||
}
|
||||
|
|
@ -57,7 +58,13 @@ class Connection extends RawConnection {
|
|||
}
|
||||
}
|
||||
$length = mb_strlen(self::DELIMITER);
|
||||
while (mb_substr($line, 0, $length) !== self::DELIMITER) { //next prompt functions as delimiter
|
||||
while (mb_substr($line, 0, $length) !== self::DELIMITER && $line) { //next prompt functions as delimiter
|
||||
if (is_callable($callback)) {
|
||||
$result = $callback($line);
|
||||
if ($result === false) { // allow the callback to close the connection for infinite running commands
|
||||
$this->close(true);
|
||||
}
|
||||
}
|
||||
$output[] .= $line;
|
||||
$line = $this->readLine();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,17 @@
|
|||
namespace Icewind\SMB;
|
||||
|
||||
interface IShare {
|
||||
// https://msdn.microsoft.com/en-us/library/dn392331.aspx
|
||||
const NOTIFY_ADDED = 1;
|
||||
const NOTIFY_REMOVED = 2;
|
||||
const NOTIFY_MODIFIED = 3;
|
||||
const NOTIFY_RENAMED_OLD = 4;
|
||||
const NOTIFY_RENAMED_NEW = 5;
|
||||
const NOTIFY_ADDED_STREAM = 6;
|
||||
const NOTIFY_REMOVED_STREAM = 7;
|
||||
const NOTIFY_MODIFIED_STREAM = 8;
|
||||
const NOTIFY_REMOVED_BY_DELETE = 9;
|
||||
|
||||
/**
|
||||
* Get the name of the share
|
||||
*
|
||||
|
|
@ -131,4 +142,11 @@ interface IShare {
|
|||
* @return mixed
|
||||
*/
|
||||
public function setMode($path, $mode);
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param callable $callback callable which will be called for each received change
|
||||
* @return mixed
|
||||
*/
|
||||
public function notify($path, callable $callback);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -301,6 +301,15 @@ class NativeShare extends AbstractShare {
|
|||
return $this->setAttribute($path, 'system.dos_attr.mode', $mode);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param callable $callback callable which will be called for each received change
|
||||
* @return mixed
|
||||
*/
|
||||
public function notify($path, callable $callback) {
|
||||
throw new \Exception('not implemented');
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
unset($this->state);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -344,6 +344,26 @@ class Share extends AbstractShare {
|
|||
return $this->parseOutput($output, $path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param callable $callback callable which will be called for each received change
|
||||
* @return mixed
|
||||
*/
|
||||
public function notify($path, callable $callback) {
|
||||
$this->connect();
|
||||
$command = 'notify ' . $this->escapePath($path);
|
||||
$this->connection->write($command . PHP_EOL);
|
||||
$this->connection->read(function ($line) use ($callback, $path) {
|
||||
$code = (int)substr($line, 0, 4);
|
||||
$subPath = substr($line, 5);
|
||||
if ($path === '') {
|
||||
$callback($code, $subPath);
|
||||
} else {
|
||||
$callback($code, $path . '/' . $subPath);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $command
|
||||
* @return array
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue