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\IFileInfo; |
11
|
|
|
|
12
|
|
|
class NativeFileInfo implements IFileInfo { |
13
|
|
|
const MODE_FILE = 0100000; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $path; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $name; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var NativeShare |
27
|
|
|
*/ |
28
|
|
|
protected $share; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array|null |
32
|
|
|
*/ |
33
|
|
|
protected $statCache = null; |
34
|
|
|
|
35
|
|
|
/** @var callable|null */ |
36
|
|
|
protected $statCallback = null; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
protected $modeCache; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param NativeShare $share |
45
|
|
|
* @param string $path |
46
|
|
|
* @param string $name |
47
|
|
|
* @param array|callable $stat |
48
|
|
|
*/ |
49
|
104 |
|
public function __construct($share, $path, $name, $stat) { |
50
|
104 |
|
$this->share = $share; |
51
|
104 |
|
$this->path = $path; |
52
|
104 |
|
$this->name = $name; |
53
|
|
|
|
54
|
104 |
|
if (is_array($stat)) { |
55
|
23 |
|
$this->statCache = $stat; |
56
|
102 |
|
} elseif (is_callable($stat)) { |
57
|
102 |
|
$this->statCallback = $stat; |
58
|
|
|
} else { |
59
|
|
|
throw new \InvalidArgumentException('$stat needs to be an array or callback'); |
60
|
|
|
} |
61
|
104 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
101 |
|
public function getPath() { |
67
|
101 |
|
return $this->path; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
49 |
|
public function getName() { |
74
|
49 |
|
return $this->name; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
102 |
|
protected function stat() { |
81
|
102 |
|
if (is_null($this->statCache)) { |
82
|
102 |
|
$this->statCache = call_user_func($this->statCallback); |
83
|
|
|
} |
84
|
102 |
|
return $this->statCache; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return int |
89
|
|
|
*/ |
90
|
29 |
|
public function getSize() { |
91
|
29 |
|
$stat = $this->stat(); |
92
|
29 |
|
return $stat['size']; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return int |
97
|
|
|
*/ |
98
|
1 |
|
public function getMTime() { |
99
|
1 |
|
$stat = $this->stat(); |
100
|
1 |
|
return $stat['mtime']; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
101 |
|
public function isDirectory() { |
107
|
101 |
|
$stat = $this->stat(); |
108
|
101 |
|
return !($stat['mode'] & self::MODE_FILE); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return int |
113
|
|
|
*/ |
114
|
14 |
|
protected function getMode() { |
115
|
14 |
|
if (!$this->modeCache) { |
116
|
14 |
|
$attribute = $this->share->getAttribute($this->path, 'system.dos_attr.mode'); |
117
|
|
|
// parse hex string |
118
|
14 |
|
$this->modeCache = (int)hexdec(substr($attribute, 2)); |
119
|
|
|
} |
120
|
14 |
|
return $this->modeCache; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
9 |
|
public function isReadOnly() { |
127
|
9 |
|
$mode = $this->getMode(); |
128
|
9 |
|
return (bool)($mode & IFileInfo::MODE_READONLY); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
13 |
|
public function isHidden() { |
135
|
13 |
|
$mode = $this->getMode(); |
136
|
13 |
|
return (bool)($mode & IFileInfo::MODE_HIDDEN); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return bool |
141
|
|
|
*/ |
142
|
8 |
|
public function isSystem() { |
143
|
8 |
|
$mode = $this->getMode(); |
144
|
8 |
|
return (bool)($mode & IFileInfo::MODE_SYSTEM); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
8 |
|
public function isArchived() { |
151
|
8 |
|
$mode = $this->getMode(); |
152
|
8 |
|
return (bool)($mode & IFileInfo::MODE_ARCHIVE); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|