Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NotifyHandler.php « Wrapped « src « smb « icewind « 3rdparty « files_external « apps - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 18451f4daa69c0dd021df2992a77095550e65543 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
 * @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl>
 * This file is licensed under the Licensed under the MIT license:
 * http://opensource.org/licenses/MIT
 *
 */

namespace Icewind\SMB\Wrapped;

use Icewind\SMB\Change;
use Icewind\SMB\Exception\Exception;
use Icewind\SMB\Exception\RevisionMismatchException;
use Icewind\SMB\INotifyHandler;

class NotifyHandler implements INotifyHandler {
	/** @var Connection */
	private $connection;

	/** @var string */
	private $path;

	/** @var bool */
	private $listening = true;

	// see error.h
	const EXCEPTION_MAP = [
		ErrorCodes::RevisionMismatch => RevisionMismatchException::class,
	];

	/**
	 * @param Connection $connection
	 * @param string $path
	 */
	public function __construct(Connection $connection, string $path) {
		$this->connection = $connection;
		$this->path = $path;
	}

	/**
	 * Get all changes detected since the start of the notify process or the last call to getChanges
	 *
	 * @return Change[]
	 */
	public function getChanges(): array {
		if (!$this->listening) {
			return [];
		}
		stream_set_blocking($this->connection->getOutputStream(), false);
		$lines = [];
		while (($line = $this->connection->readLine())) {
			$this->checkForError($line);
			$lines[] = $line;
		}
		stream_set_blocking($this->connection->getOutputStream(), true);
		return array_values(array_filter(array_map([$this, 'parseChangeLine'], $lines)));
	}

	/**
	 * Listen actively to all incoming changes
	 *
	 * Note that this is a blocking process and will cause the process to block forever if not explicitly terminated
	 *
	 * @param callable(Change):?bool $callback
	 */
	public function listen(callable $callback): void {
		if ($this->listening) {
			$this->connection->read(function (string $line) use ($callback): bool {
				$this->checkForError($line);
				$change = $this->parseChangeLine($line);
				if ($change) {
					$result = $callback($change);
					return $result === false ? false : true;
				} else {
					return true;
				}
			});
		}
	}

	private function parseChangeLine(string $line): ?Change {
		$code = (int)substr($line, 0, 4);
		if ($code === 0) {
			return null;
		}
		$subPath = str_replace('\\', '/', substr($line, 5));
		if ($this->path === '') {
			return new Change($code, $subPath);
		} else {
			return new Change($code, $this->path . '/' . $subPath);
		}
	}

	private function checkForError(string $line): void {
		if (substr($line, 0, 16) === 'notify returned ') {
			$error = substr($line, 16);
			throw Exception::fromMap(array_merge(self::EXCEPTION_MAP, Parser::EXCEPTION_MAP), $error, 'Notify is not supported with the used smb version');
		}
	}

	public function stop(): void {
		$this->listening = false;
		$this->connection->close();
	}

	public function __destruct() {
		$this->stop();
	}
}