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

NativeFileInfo.php « Native « src « smb « icewind « 3rdparty « files_external « apps - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 539bb728426715d29c773f893ce1c89d3dae8964 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?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\Native;

use Icewind\SMB\ACL;
use Icewind\SMB\Exception\Exception;
use Icewind\SMB\IFileInfo;

class NativeFileInfo implements IFileInfo {
	/** @var string */
	protected $path;
	/** @var string */
	protected $name;
	/** @var NativeShare */
	protected $share;
	/** @var array{"mode": int, "size": int, "write_time": int}|null */
	protected $attributeCache = null;

	public function __construct(NativeShare $share, string $path, string $name) {
		$this->share = $share;
		$this->path = $path;
		$this->name = $name;
	}

	public function getPath(): string {
		return $this->path;
	}

	public function getName(): string {
		return $this->name;
	}

	/**
	 * @return array{"mode": int, "size": int, "write_time": int}
	 */
	protected function stat(): array {
		if (is_null($this->attributeCache)) {
			$rawAttributes = explode(',', $this->share->getAttribute($this->path, 'system.dos_attr.*'));
			$attributes = [];
			foreach ($rawAttributes as $rawAttribute) {
				list($name, $value) = explode(':', $rawAttribute);
				$name = strtolower($name);
				if ($name == 'mode') {
					$attributes[$name] = (int)hexdec(substr($value, 2));
				} else {
					$attributes[$name] = (int)$value;
				}
			}
			if (!isset($attributes['mode'])) {
				throw new Exception("Invalid attribute response");
			}
			if (!isset($attributes['size'])) {
				throw new Exception("Invalid attribute response");
			}
			if (!isset($attributes['write_time'])) {
				throw new Exception("Invalid attribute response");
			}
			$this->attributeCache = $attributes;
		}
		return $this->attributeCache;
	}

	public function getSize(): int {
		$stat = $this->stat();
		return $stat['size'];
	}

	public function getMTime(): int {
		$stat = $this->stat();
		return $stat['write_time'];
	}

	/**
	 * On "mode":
	 *
	 * different smbclient versions seem to return different mode values for 'system.dos_attr.mode'
	 *
	 * older versions return the dos permissions mask as defined in `IFileInfo::MODE_*` while
	 * newer versions return the equivalent unix permission mask.
	 *
	 * Since the unix mask doesn't contain the proper hidden/archive/system flags we have to assume them
	 * as false (except for `hidden` where we use the unix dotfile convention)
	 */

	protected function getMode(): int {
		$mode = $this->stat()['mode'];

		// Let us ignore the ATTR_NOT_CONTENT_INDEXED for now
		$mode &= ~0x00002000;

		return $mode;
	}

	public function isDirectory(): bool {
		$mode = $this->getMode();
		if ($mode > 0x1000) {
			return (bool)($mode & 0x4000); // 0x4000: unix directory flag
		} else {
			return (bool)($mode & IFileInfo::MODE_DIRECTORY);
		}
	}

	public function isReadOnly(): bool {
		$mode = $this->getMode();
		if ($mode > 0x1000) {
			return !(bool)($mode & 0x80); // 0x80: owner write permissions
		} else {
			return (bool)($mode & IFileInfo::MODE_READONLY);
		}
	}

	public function isHidden(): bool {
		$mode = $this->getMode();
		if ($mode > 0x1000) {
			return strlen($this->name) > 0 && $this->name[0] === '.';
		} else {
			return (bool)($mode & IFileInfo::MODE_HIDDEN);
		}
	}

	public function isSystem(): bool {
		$mode = $this->getMode();
		if ($mode > 0x1000) {
			return false;
		} else {
			return (bool)($mode & IFileInfo::MODE_SYSTEM);
		}
	}

	public function isArchived(): bool {
		$mode = $this->getMode();
		if ($mode > 0x1000) {
			return false;
		} else {
			return (bool)($mode & IFileInfo::MODE_ARCHIVE);
		}
	}

	/**
	 * @return ACL[]
	 */
	public function getAcls(): array {
		$acls = [];
		$attribute = $this->share->getAttribute($this->path, 'system.nt_sec_desc.acl.*+');

		foreach (explode(',', $attribute) as $acl) {
			list($user, $permissions) = explode(':', $acl, 2);
			$user = trim($user, '\\');
			list($type, $flags, $mask) = explode('/', $permissions);
			$mask = hexdec($mask);

			$acls[$user] = new ACL((int)$type, (int)$flags, (int)$mask);
		}

		return $acls;
	}
}