UNPKG

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