UNPKG

3.07 kBJavaScriptView Raw
1var path = require('path');
2
3var counter = 0;
4
5
6
7/**
8 * A filesystem item.
9 * @constructor
10 */
11function Item() {
12
13 var now = Date.now();
14
15 /**
16 * Access time.
17 * @type {Date}
18 */
19 this._atime = new Date(now);
20
21 /**
22 * Change time.
23 * @type {Date}
24 */
25 this._ctime = new Date(now);
26
27 /**
28 * Modification time.
29 * @type {Date}
30 */
31 this._mtime = new Date(now);
32
33 /**
34 * Permissions.
35 */
36 this._mode = 0666;
37
38 /**
39 * User id.
40 * @type {number}
41 */
42 this._uid = process.getuid ? process.getuid() : undefined;
43
44 /**
45 * Group id.
46 * @type {number}
47 */
48 this._gid = process.getgid ? process.getgid() : undefined;
49
50 /**
51 * Item number.
52 * @type {number}
53 */
54 this._id = ++counter;
55
56 /**
57 * Number of links to this item.
58 */
59 this.links = 0;
60
61}
62
63
64/**
65 * Get access time.
66 * @return {Date} Access time.
67 */
68Item.prototype.getATime = function() {
69 return this._atime;
70};
71
72
73/**
74 * Set access time.
75 * @param {Date} atime Access time.
76 */
77Item.prototype.setATime = function(atime) {
78 this._atime = atime;
79};
80
81
82/**
83 * Get change time.
84 * @return {Date} Change time.
85 */
86Item.prototype.getCTime = function() {
87 return this._ctime;
88};
89
90
91/**
92 * Set change time.
93 * @param {Date} ctime Change time.
94 */
95Item.prototype.setCTime = function(ctime) {
96 this._ctime = ctime;
97};
98
99
100/**
101 * Get modification time.
102 * @return {Date} Modification time.
103 */
104Item.prototype.getMTime = function() {
105 return this._mtime;
106};
107
108
109/**
110 * Set modification time.
111 * @param {Date} mtime Modification time.
112 */
113Item.prototype.setMTime = function(mtime) {
114 this._mtime = mtime;
115};
116
117
118/**
119 * Get mode (permission only, e.g 0666).
120 * @return {number} Mode.
121 */
122Item.prototype.getMode = function() {
123 return this._mode;
124};
125
126
127/**
128 * Set mode (permission only, e.g 0666).
129 * @param {Date} mode Mode.
130 */
131Item.prototype.setMode = function(mode) {
132 this.setCTime(new Date());
133 this._mode = mode;
134};
135
136
137/**
138 * Get user id.
139 * @return {number} User id.
140 */
141Item.prototype.getUid = function() {
142 return this._uid;
143};
144
145
146/**
147 * Set user id.
148 * @param {number} uid User id.
149 */
150Item.prototype.setUid = function(uid) {
151 this.setCTime(new Date());
152 this._uid = uid;
153};
154
155
156/**
157 * Get group id.
158 * @return {number} Group id.
159 */
160Item.prototype.getGid = function() {
161 return this._gid;
162};
163
164
165/**
166 * Set group id.
167 * @param {number} gid Group id.
168 */
169Item.prototype.setGid = function(gid) {
170 this.setCTime(new Date());
171 this._gid = gid;
172};
173
174
175/**
176 * Get item stats.
177 * @return {Object} Stats properties.
178 */
179Item.prototype.getStats = function() {
180 return {
181 dev: 8675309,
182 nlink: this.links,
183 uid: this.getUid(),
184 gid: this.getGid(),
185 rdev: 0,
186 blksize: 4096,
187 ino: this._id,
188 atime: this.getATime(),
189 mtime: this.getMTime(),
190 ctime: this.getCTime(),
191 };
192};
193
194
195/**
196 * Get the item's string representation.
197 * @return {string} String representation.
198 */
199Item.prototype.toString = function() {
200 return '[' + this.constructor.name + ']';
201};
202
203
204/**
205 * Export the constructor.
206 * @type {function()}
207 */
208exports = module.exports = Item;