NativeFileInfo::stat()   B
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 12
cts 12
cp 1
rs 8.5706
c 0
b 0
f 0
cc 7
nc 5
nop 0
crap 7
1
<?php
2
/**
3
 * Copyright (c) 2014 Robin Appelman <[email protected]>
4
 * This file is licensed under the Licensed under the MIT license:
5
 * http://opensource.org/licenses/MIT
6
 */
7
8
namespace Icewind\SMB\Native;
9
10
use Icewind\SMB\ACL;
11
use Icewind\SMB\Exception\Exception;
12
use Icewind\SMB\IFileInfo;
13
14
class NativeFileInfo implements IFileInfo {
15
	/** @var string */
16
	protected $path;
17
	/** @var string */
18
	protected $name;
19
	/** @var NativeShare */
20
	protected $share;
21
	/** @var array{"mode": int, "size": int, "write_time": int}|null */
22
	protected $attributeCache = null;
23
24
	public function __construct(NativeShare $share, string $path, string $name) {
25
		$this->share = $share;
26
		$this->path = $path;
27
		$this->name = $name;
28
	}
29
30
	public function getPath(): string {
31
		return $this->path;
32
	}
33
34
	public function getName(): string {
35
		return $this->name;
36
	}
37
38
	/**
39 210
	 * @return array{"mode": int, "size": int, "write_time": int}
0 ignored issues
show
Documentation introduced by
The doc-type array{"mode": could not be parsed: Unknown type name "array{"mode":" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
40 210
	 */
41 210
	protected function stat(): array {
42 210
		if (is_null($this->attributeCache)) {
43 210
			$rawAttributes = explode(',', $this->share->getAttribute($this->path, 'system.dos_attr.*'));
44
			$attributes = [];
45
			foreach ($rawAttributes as $rawAttribute) {
46
				list($name, $value) = explode(':', $rawAttribute);
47
				$name = strtolower($name);
48 184
				if ($name == 'mode') {
49 184
					$attributes[$name] = (int)hexdec(substr($value, 2));
50
				} else {
51
					$attributes[$name] = (int)$value;
52
				}
53
			}
54
			if (!isset($attributes['mode'])) {
55 98
				throw new Exception("Invalid attribute response");
56 98
			}
57
			if (!isset($attributes['size'])) {
58
				throw new Exception("Invalid attribute response");
59
			}
60
			if (!isset($attributes['write_time'])) {
61
				throw new Exception("Invalid attribute response");
62 210
			}
63 210
			$this->attributeCache = $attributes;
64 210
		}
65 190
		return $this->attributeCache;
66 190
	}
67 190
68 190
	public function getSize(): int {
69 190
		$stat = $this->stat();
70 190
		return $stat['size'];
71
	}
72 190
73
	public function getMTime(): int {
74
		$stat = $this->stat();
75
		return $stat['write_time'];
76 190
	}
77
78
	/**
79
	 * On "mode":
80
	 *
81
	 * different smbclient versions seem to return different mode values for 'system.dos_attr.mode'
82 82
	 *
83 82
	 * older versions return the dos permissions mask as defined in `IFileInfo::MODE_*` while
84 62
	 * newer versions return the equivalent unix permission mask.
85
	 *
86
	 * Since the unix mask doesn't contain the proper hidden/archive/system flags we have to assume them
87
	 * as false (except for `hidden` where we use the unix dotfile convention)
88
	 */
89
90 2
	protected function getMode(): int {
91 2
		$mode = $this->stat()['mode'];
92 2
93
		// Let us ignore the ATTR_NOT_CONTENT_INDEXED for now
94
		$mode &= ~0x00002000;
95
96
		return $mode;
97
	}
98
99 View Code Duplication
	public function isDirectory(): bool {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
		$mode = $this->getMode();
101
		if ($mode > 0x1000) {
102
			return (bool)($mode & 0x4000); // 0x4000: unix directory flag
103
		} else {
104
			return (bool)($mode & IFileInfo::MODE_DIRECTORY);
105
		}
106
	}
107
108 View Code Duplication
	public function isReadOnly(): bool {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
		$mode = $this->getMode();
110 184
		if ($mode > 0x1000) {
111 184
			return !(bool)($mode & 0x80); // 0x80: owner write permissions
112
		} else {
113
			return (bool)($mode & IFileInfo::MODE_READONLY);
114 184
		}
115
	}
116 184
117
	public function isHidden(): bool {
118
		$mode = $this->getMode();
119
		if ($mode > 0x1000) {
120
			return strlen($this->name) > 0 && $this->name[0] === '.';
121
		} else {
122 184
			return (bool)($mode & IFileInfo::MODE_HIDDEN);
123 184
		}
124 184
	}
125
126 View Code Duplication
	public function isSystem(): bool {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
127 184
		$mode = $this->getMode();
128
		if ($mode > 0x1000) {
129
			return false;
130
		} else {
131
			return (bool)($mode & IFileInfo::MODE_SYSTEM);
132
		}
133
	}
134 2
135 2 View Code Duplication
	public function isArchived(): bool {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136 2
		$mode = $this->getMode();
137
		if ($mode > 0x1000) {
138
			return false;
139 2
		} else {
140
			return (bool)($mode & IFileInfo::MODE_ARCHIVE);
141
		}
142
	}
143
144
	/**
145
	 * @return ACL[]
146 12
	 */
147 12
	public function getAcls(): array {
148 12
		$acls = [];
149
		$attribute = $this->share->getAttribute($this->path, 'system.nt_sec_desc.acl.*+');
150
151 12
		foreach (explode(',', $attribute) as $acl) {
152
			list($user, $permissions) = explode(':', $acl, 2);
153
			$user = trim($user, '\\');
154
			list($type, $flags, $mask) = explode('/', $permissions);
155
			$mask = hexdec($mask);
156
157
			$acls[$user] = new ACL((int)$type, (int)$flags, (int)$mask);
158
		}
159
160
		return $acls;
161
	}
162
}
163