1 | 'use strict';
|
2 |
|
3 | var path = require('path');
|
4 |
|
5 | var counter = 0;
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | var permissions = {
|
13 | USER_READ: 256,
|
14 | USER_WRITE: 128,
|
15 | USER_EXEC: 64,
|
16 | GROUP_READ: 32,
|
17 | GROUP_WRITE: 16,
|
18 | GROUP_EXEC: 8,
|
19 | OTHER_READ: 4,
|
20 | OTHER_WRITE: 2,
|
21 | OTHER_EXEC: 1
|
22 | };
|
23 |
|
24 | function getUid() {
|
25 | return process.getuid && process.getuid();
|
26 | }
|
27 |
|
28 | function getGid() {
|
29 | return process.getgid && process.getgid();
|
30 | }
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | function Item() {
|
39 |
|
40 | var now = Date.now();
|
41 |
|
42 | |
43 |
|
44 |
|
45 |
|
46 | this._atime = new Date(now);
|
47 |
|
48 | |
49 |
|
50 |
|
51 |
|
52 | this._ctime = new Date(now);
|
53 |
|
54 | |
55 |
|
56 |
|
57 |
|
58 | this._mtime = new Date(now);
|
59 |
|
60 | |
61 |
|
62 |
|
63 | this._mode = 438;
|
64 |
|
65 | |
66 |
|
67 |
|
68 |
|
69 | this._uid = getUid();
|
70 |
|
71 | |
72 |
|
73 |
|
74 |
|
75 | this._gid = getGid();
|
76 |
|
77 | |
78 |
|
79 |
|
80 |
|
81 | this._id = ++counter;
|
82 |
|
83 | |
84 |
|
85 |
|
86 | this.links = 0;
|
87 |
|
88 | }
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | Item.prototype.canRead = function() {
|
96 | var uid = getUid();
|
97 | var gid = getGid();
|
98 | var can = false;
|
99 | if (uid === 0) {
|
100 | can = true;
|
101 | } else if (uid === this._uid) {
|
102 | can = (permissions.USER_READ & this._mode) === permissions.USER_READ;
|
103 | } else if (gid === this._gid) {
|
104 | can = (permissions.GROUP_READ & this._mode) === permissions.GROUP_READ;
|
105 | } else {
|
106 | can = (permissions.OTHER_READ & this._mode) === permissions.OTHER_READ;
|
107 | }
|
108 | return can;
|
109 | };
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | Item.prototype.canWrite = function() {
|
117 | var uid = getUid();
|
118 | var gid = getGid();
|
119 | var can = false;
|
120 | if (uid === 0) {
|
121 | can = true;
|
122 | } else if (uid === this._uid) {
|
123 | can = (permissions.USER_WRITE & this._mode) === permissions.USER_WRITE;
|
124 | } else if (gid === this._gid) {
|
125 | can = (permissions.GROUP_WRITE & this._mode) === permissions.GROUP_WRITE;
|
126 | } else {
|
127 | can = (permissions.OTHER_WRITE & this._mode) === permissions.OTHER_WRITE;
|
128 | }
|
129 | return can;
|
130 | };
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | Item.prototype.canExecute = function() {
|
138 | var uid = getUid();
|
139 | var gid = getGid();
|
140 | var can = false;
|
141 | if (uid === 0) {
|
142 | can = true;
|
143 | } else if (uid === this._uid) {
|
144 | can = (permissions.USER_EXEC & this._mode) === permissions.USER_EXEC;
|
145 | } else if (gid === this._gid) {
|
146 | can = (permissions.GROUP_EXEC & this._mode) === permissions.GROUP_EXEC;
|
147 | } else {
|
148 | can = (permissions.OTHER_EXEC & this._mode) === permissions.OTHER_EXEC;
|
149 | }
|
150 | return can;
|
151 | };
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | Item.prototype.getATime = function() {
|
159 | return this._atime;
|
160 | };
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 | Item.prototype.setATime = function(atime) {
|
168 | this._atime = atime;
|
169 | };
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | Item.prototype.getCTime = function() {
|
177 | return this._ctime;
|
178 | };
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | Item.prototype.setCTime = function(ctime) {
|
186 | this._ctime = ctime;
|
187 | };
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 | Item.prototype.getMTime = function() {
|
195 | return this._mtime;
|
196 | };
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 | Item.prototype.setMTime = function(mtime) {
|
204 | this._mtime = mtime;
|
205 | };
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 | Item.prototype.getMode = function() {
|
213 | return this._mode;
|
214 | };
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 | Item.prototype.setMode = function(mode) {
|
222 | this.setCTime(new Date());
|
223 | this._mode = mode;
|
224 | };
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 | Item.prototype.getUid = function() {
|
232 | return this._uid;
|
233 | };
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 | Item.prototype.setUid = function(uid) {
|
241 | this.setCTime(new Date());
|
242 | this._uid = uid;
|
243 | };
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 | Item.prototype.getGid = function() {
|
251 | return this._gid;
|
252 | };
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 | Item.prototype.setGid = function(gid) {
|
260 | this.setCTime(new Date());
|
261 | this._gid = gid;
|
262 | };
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 | Item.prototype.getStats = function() {
|
270 | return {
|
271 | dev: 8675309,
|
272 | nlink: this.links,
|
273 | uid: this.getUid(),
|
274 | gid: this.getGid(),
|
275 | rdev: 0,
|
276 | blksize: 4096,
|
277 | ino: this._id,
|
278 | atime: this.getATime(),
|
279 | mtime: this.getMTime(),
|
280 | ctime: this.getCTime()
|
281 | };
|
282 | };
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 | Item.prototype.toString = function() {
|
290 | return '[' + this.constructor.name + ']';
|
291 | };
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 | exports = module.exports = Item;
|