-
Notifications
You must be signed in to change notification settings - Fork 86
/
filesystem.js
297 lines (270 loc) · 7.38 KB
/
filesystem.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
'use strict';
var os = require('os');
var path = require('path');
var Directory = require('./directory');
var File = require('./file');
var FSError = require('./error');
var SymbolicLink = require('./symlink');
var isWindows = process.platform === 'win32';
function getPathParts(filepath) {
var parts = path._makeLong(path.resolve(filepath)).split(path.sep);
parts.shift();
if (isWindows) {
// parts currently looks like ['', '?', 'c:', ...]
parts.shift();
var q = parts.shift(); // should be '?'
var base = '\\\\' + q + '\\' + parts.shift().toLowerCase();
parts.unshift(base);
}
if (parts[parts.length - 1] === '') {
parts.pop();
}
return parts;
}
/**
* Create a new file system.
* @constructor
*/
function FileSystem() {
var root = new Directory();
// populate with default directories
var defaults = [os.tmpdir && os.tmpdir() || os.tmpDir(), process.cwd()];
defaults.forEach(function(dir) {
var parts = getPathParts(dir);
var directory = root;
var i, ii, name, candidate;
for (i = 0, ii = parts.length; i < ii; ++i) {
name = parts[i];
candidate = directory.getItem(name);
if (!candidate) {
directory = directory.addItem(name, new Directory());
} else if (candidate instanceof Directory) {
directory = candidate;
} else {
throw new Error('Failed to create directory: ' + dir);
}
}
});
/**
* Root directory.
* @type {Directory}
*/
this._root = root;
}
/**
* Get a file system item.
* @param {string} filepath Path to item.
* @return {Item} The item (or null if not found).
*/
FileSystem.prototype.getItem = function(filepath) {
var parts = getPathParts(filepath);
var currentParts = getPathParts(process.cwd());
var item = this._root;
var itemPath = '/';
var name;
for (var i = 0, ii = parts.length; i < ii; ++i) {
name = parts[i];
while (item instanceof SymbolicLink) {
// Symbolic link being traversed as a directory --- If link targets
// another symbolic link, resolve target's path relative to the original
// link's target, otherwise relative to the current item.
itemPath = path.resolve(path.dirname(itemPath), item.getPath());
item = this.getItem(itemPath);
}
if (item) {
if (item instanceof Directory && name !== currentParts[i]) {
// make sure traversal is allowed
if (!item.canExecute()) {
throw new FSError('EACCES', filepath);
}
}
item = item.getItem(name);
}
if (!item) {
break;
}
itemPath = path.resolve(itemPath, name);
}
return item;
};
/**
* Populate a directory with an item.
* @param {Directory} directory The directory to populate.
* @param {string} name The name of the item.
* @param {string|Buffer|function|Object} obj Instructions for creating the
* item.
*/
function populate(directory, name, obj) {
var item;
if (typeof obj === 'string' || Buffer.isBuffer(obj)) {
// contents for a file
item = new File();
item.setContent(obj);
} else if (typeof obj === 'function') {
// item factory
item = obj();
} else {
// directory with more to populate
item = new Directory();
for (var key in obj) {
populate(item, key, obj[key]);
}
}
/**
* Special exception for redundant adding of empty directories.
*/
if (item instanceof Directory &&
item.list().length === 0 &&
directory.getItem(name) instanceof Directory) {
// pass
} else {
directory.addItem(name, item);
}
}
/**
* Configure a mock file system.
* @param {Object} paths Config object.
* @return {FileSystem} Mock file system.
*/
FileSystem.create = function(paths) {
var system = new FileSystem();
for (var filepath in paths) {
var parts = getPathParts(filepath);
var directory = system._root;
var i, ii, name, candidate;
for (i = 0, ii = parts.length - 1; i < ii; ++i) {
name = parts[i];
candidate = directory.getItem(name);
if (!candidate) {
directory = directory.addItem(name, new Directory());
} else if (candidate instanceof Directory) {
directory = candidate;
} else {
throw new Error('Failed to create directory: ' + filepath);
}
}
populate(directory, parts[i], paths[filepath]);
}
return system;
};
/**
* Generate a factory for new files.
* @param {Object} config File config.
* @return {function():File} Factory that creates a new file.
*/
FileSystem.file = function(config) {
config = config || {};
return function() {
var file = new File();
if (config.hasOwnProperty('content')) {
file.setContent(config.content);
}
if (config.hasOwnProperty('mode')) {
file.setMode(config.mode);
} else {
file.setMode(438); // 0666
}
if (config.hasOwnProperty('uid')) {
file.setUid(config.uid);
}
if (config.hasOwnProperty('gid')) {
file.setGid(config.gid);
}
if (config.hasOwnProperty('atime')) {
file.setATime(config.atime);
}
if (config.hasOwnProperty('ctime')) {
file.setCTime(config.ctime);
}
if (config.hasOwnProperty('mtime')) {
file.setMTime(config.mtime);
}
if (config.hasOwnProperty('birthtime')) {
file.setBirthtime(config.birthtime);
}
return file;
};
};
/**
* Generate a factory for new symbolic links.
* @param {Object} config File config.
* @return {function():File} Factory that creates a new symbolic link.
*/
FileSystem.symlink = function(config) {
config = config || {};
return function() {
var link = new SymbolicLink();
if (config.hasOwnProperty('mode')) {
link.setMode(config.mode);
} else {
link.setMode(438); // 0666
}
if (config.hasOwnProperty('uid')) {
link.setUid(config.uid);
}
if (config.hasOwnProperty('gid')) {
link.setGid(config.gid);
}
if (config.hasOwnProperty('path')) {
link.setPath(config.path);
} else {
throw new Error('Missing "path" property');
}
if (config.hasOwnProperty('atime')) {
link.setATime(config.atime);
}
if (config.hasOwnProperty('ctime')) {
link.setCTime(config.ctime);
}
if (config.hasOwnProperty('mtime')) {
link.setMTime(config.mtime);
}
if (config.hasOwnProperty('birthtime')) {
link.setBirthtime(config.birthtime);
}
return link;
};
};
/**
* Generate a factory for new directories.
* @param {Object} config File config.
* @return {function():Directory} Factory that creates a new directory.
*/
FileSystem.directory = function(config) {
config = config || {};
return function() {
var dir = new Directory();
if (config.hasOwnProperty('mode')) {
dir.setMode(config.mode);
}
if (config.hasOwnProperty('uid')) {
dir.setUid(config.uid);
}
if (config.hasOwnProperty('gid')) {
dir.setGid(config.gid);
}
if (config.hasOwnProperty('items')) {
for (var name in config.items) {
populate(dir, name, config.items[name]);
}
}
if (config.hasOwnProperty('atime')) {
dir.setATime(config.atime);
}
if (config.hasOwnProperty('ctime')) {
dir.setCTime(config.ctime);
}
if (config.hasOwnProperty('mtime')) {
dir.setMTime(config.mtime);
}
if (config.hasOwnProperty('birthtime')) {
dir.setBirthtime(config.birthtime);
}
return dir;
};
};
/**
* Module exports.
* @type {function}
*/
module.exports = FileSystem;