summaryrefslogtreecommitdiffstats
path: root/apps/files_external/3rdparty/icewind/smb/src/NativeStream.php
blob: 07bd2f1e7978fee2816f25086df58a47e24bb6e9 (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
110
111
112
113
114
<?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;

use Icewind\SMB\Exception\InvalidRequestException;
use Icewind\Streams\File;

class NativeStream implements File {
	/**
	 * @var resource
	 */
	public $context;

	/**
	 * @var \Icewind\SMB\NativeState
	 */
	private $state;

	/**
	 * @var resource
	 */
	private $handle;

	/**
	 * @var bool
	 */
	private $eof = false;

	/**
	 * Wrap a stream from libsmbclient-php into a regular php stream
	 *
	 * @param \Icewind\SMB\NativeState $state
	 * @param resource $smbStream
	 * @param string $mode
	 * @return resource
	 */
	public static function wrap($state, $smbStream, $mode) {
		stream_wrapper_register('nativesmb', '\Icewind\SMB\NativeStream');
		$context = stream_context_create(array(
			'nativesmb' => array(
				'state' => $state,
				'handle' => $smbStream
			)
		));
		$fh = fopen('nativesmb://', $mode, false, $context);
		stream_wrapper_unregister('nativesmb');
		return $fh;
	}

	public function stream_close() {
		return $this->state->close($this->handle);
	}

	public function stream_eof() {
		return $this->eof;
	}

	public function stream_flush() {
	}


	public function stream_open($path, $mode, $options, &$opened_path) {
		$context = stream_context_get_options($this->context);
		$this->state = $context['nativesmb']['state'];
		$this->handle = $context['nativesmb']['handle'];
		return true;
	}

	public function stream_read($count) {
		$result = $this->state->read($this->handle, $count);
		if (strlen($result) < $count) {
			$this->eof = true;
		}
		return $result;
	}

	public function stream_seek($offset, $whence = SEEK_SET) {
		$this->eof = false;
		try {
			return $this->state->lseek($this->handle, $offset, $whence) !== false;
		} catch (InvalidRequestException $e) {
			return false;
		}
	}

	public function stream_stat() {
		return $this->state->fstat($this->handle);
	}

	public function stream_tell() {
		return $this->state->lseek($this->handle, 0, SEEK_CUR);
	}

	public function stream_write($data) {
		return $this->state->write($this->handle, $data);
	}

	public function stream_truncate($size) {
		return $this->state->ftruncate($this->handle, $size);
	}

	public function stream_set_option($option, $arg1, $arg2) {
		return false;
	}

	public function stream_lock($operation) {
		return false;
	}
}