UNPKG

5.19 kBJavaScriptView Raw
1'use strict';
2
3var path = require('path');
4
5var counter = 0;
6
7
8/**
9 * Permissions.
10 * @enum {number}
11 */
12var permissions = {
13 USER_READ: 256, // 0400
14 USER_WRITE: 128, // 0200
15 USER_EXEC: 64, // 0100
16 GROUP_READ: 32, // 0040
17 GROUP_WRITE: 16, // 0020
18 GROUP_EXEC: 8, // 0010
19 OTHER_READ: 4, // 0004
20 OTHER_WRITE: 2, // 0002
21 OTHER_EXEC: 1 // 0001
22};
23
24function getUid() {
25 return process.getuid && process.getuid();
26}
27
28function getGid() {
29 return process.getgid && process.getgid();
30}
31
32
33
34/**
35 * A filesystem item.
36 * @constructor
37 */
38function Item() {
39
40 var now = Date.now();
41
42 /**
43 * Access time.
44 * @type {Date}
45 */
46 this._atime = new Date(now);
47
48 /**
49 * Change time.
50 * @type {Date}
51 */
52 this._ctime = new Date(now);
53
54 /**
55 * Modification time.
56 * @type {Date}
57 */
58 this._mtime = new Date(now);
59
60 /**
61 * Permissions.
62 */
63 this._mode = 438; // 0666
64
65 /**
66 * User id.
67 * @type {number}
68 */
69 this._uid = getUid();
70
71 /**
72 * Group id.
73 * @type {number}
74 */
75 this._gid = getGid();
76
77 /**
78 * Item number.
79 * @type {number}
80 */
81 this._id = ++counter;
82
83 /**
84 * Number of links to this item.
85 */
86 this.links = 0;
87
88}
89
90
91/**
92 * Determine if the current user has read permission.
93 * @return {boolean} The current user can read.
94 */
95Item.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 * Determine if the current user has write permission.
114 * @return {boolean} The current user can write.
115 */
116Item.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 * Determine if the current user has execute permission.
135 * @return {boolean} The current user can execute.
136 */
137Item.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 * Get access time.
156 * @return {Date} Access time.
157 */
158Item.prototype.getATime = function() {
159 return this._atime;
160};
161
162
163/**
164 * Set access time.
165 * @param {Date} atime Access time.
166 */
167Item.prototype.setATime = function(atime) {
168 this._atime = atime;
169};
170
171
172/**
173 * Get change time.
174 * @return {Date} Change time.
175 */
176Item.prototype.getCTime = function() {
177 return this._ctime;
178};
179
180
181/**
182 * Set change time.
183 * @param {Date} ctime Change time.
184 */
185Item.prototype.setCTime = function(ctime) {
186 this._ctime = ctime;
187};
188
189
190/**
191 * Get modification time.
192 * @return {Date} Modification time.
193 */
194Item.prototype.getMTime = function() {
195 return this._mtime;
196};
197
198
199/**
200 * Set modification time.
201 * @param {Date} mtime Modification time.
202 */
203Item.prototype.setMTime = function(mtime) {
204 this._mtime = mtime;
205};
206
207
208/**
209 * Get mode (permission only, e.g 0666).
210 * @return {number} Mode.
211 */
212Item.prototype.getMode = function() {
213 return this._mode;
214};
215
216
217/**
218 * Set mode (permission only, e.g 0666).
219 * @param {Date} mode Mode.
220 */
221Item.prototype.setMode = function(mode) {
222 this.setCTime(new Date());
223 this._mode = mode;
224};
225
226
227/**
228 * Get user id.
229 * @return {number} User id.
230 */
231Item.prototype.getUid = function() {
232 return this._uid;
233};
234
235
236/**
237 * Set user id.
238 * @param {number} uid User id.
239 */
240Item.prototype.setUid = function(uid) {
241 this.setCTime(new Date());
242 this._uid = uid;
243};
244
245
246/**
247 * Get group id.
248 * @return {number} Group id.
249 */
250Item.prototype.getGid = function() {
251 return this._gid;
252};
253
254
255/**
256 * Set group id.
257 * @param {number} gid Group id.
258 */
259Item.prototype.setGid = function(gid) {
260 this.setCTime(new Date());
261 this._gid = gid;
262};
263
264
265/**
266 * Get item stats.
267 * @return {Object} Stats properties.
268 */
269Item.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 * Get the item's string representation.
287 * @return {string} String representation.
288 */
289Item.prototype.toString = function() {
290 return '[' + this.constructor.name + ']';
291};
292
293
294/**
295 * Export the constructor.
296 * @type {function()}
297 */
298exports = module.exports = Item;