1 | 'use strict';
|
2 |
|
3 | var path = require('path');
|
4 |
|
5 | var File = require('./file');
|
6 | var FileDescriptor = require('./descriptor');
|
7 | var Directory = require('./directory');
|
8 | var SymbolicLink = require('./symlink');
|
9 | var FSError = require('./error');
|
10 |
|
11 | var constants = process.binding('constants');
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | function maybeCallback(callback, thisArg, func) {
|
23 | if (callback) {
|
24 | var err = null;
|
25 | var val;
|
26 | try {
|
27 | val = func.call(thisArg);
|
28 | } catch (e) {
|
29 | err = e;
|
30 | }
|
31 |
|
32 | callback = callback.oncomplete || callback;
|
33 | process.nextTick(function() {
|
34 | if (val === undefined) {
|
35 | callback(err);
|
36 | } else {
|
37 | callback(err, val);
|
38 | }
|
39 | });
|
40 | } else {
|
41 | return func.call(thisArg);
|
42 | }
|
43 | }
|
44 |
|
45 | function notImplemented() {
|
46 | throw new Error('Method not implemented');
|
47 | }
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | function Stats(config) {
|
57 | for (var key in config) {
|
58 | this[key] = config[key];
|
59 | }
|
60 | }
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | Stats.prototype._checkModeProperty = function(property) {
|
69 | return ((this.mode & constants.S_IFMT) === property);
|
70 | };
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | Stats.prototype.isDirectory = function() {
|
77 | return this._checkModeProperty(constants.S_IFDIR);
|
78 | };
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | Stats.prototype.isFile = function() {
|
85 | return this._checkModeProperty(constants.S_IFREG);
|
86 | };
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | Stats.prototype.isBlockDevice = function() {
|
93 | return this._checkModeProperty(constants.S_IFBLK);
|
94 | };
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | Stats.prototype.isCharacterDevice = function() {
|
101 | return this._checkModeProperty(constants.S_IFCHR);
|
102 | };
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | Stats.prototype.isSymbolicLink = function() {
|
109 | return this._checkModeProperty(constants.S_IFLNK);
|
110 | };
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | Stats.prototype.isFIFO = function() {
|
117 | return this._checkModeProperty(constants.S_IFIFO);
|
118 | };
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | Stats.prototype.isSocket = function() {
|
125 | return this._checkModeProperty(constants.S_IFSOCK);
|
126 | };
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | function Binding(system) {
|
136 |
|
137 | |
138 |
|
139 |
|
140 |
|
141 | this._system = system;
|
142 |
|
143 | |
144 |
|
145 |
|
146 |
|
147 | this.Stats = Stats;
|
148 |
|
149 | |
150 |
|
151 |
|
152 |
|
153 | this._openFiles = {};
|
154 |
|
155 | |
156 |
|
157 |
|
158 |
|
159 | this._counter = 0;
|
160 |
|
161 | }
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | Binding.prototype.getSystem = function() {
|
169 | return this._system;
|
170 | };
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | Binding.prototype.setSystem = function(system) {
|
178 | this._system = system;
|
179 | };
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | Binding.prototype._getDescriptorById = function(fd) {
|
188 | if (!this._openFiles.hasOwnProperty(fd)) {
|
189 | throw new FSError('EBADF');
|
190 | }
|
191 | return this._openFiles[fd];
|
192 | };
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 | Binding.prototype._trackDescriptor = function(descriptor) {
|
201 | var fd = ++this._counter;
|
202 | this._openFiles[fd] = descriptor;
|
203 | return fd;
|
204 | };
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 | Binding.prototype._untrackDescriptorById = function(fd) {
|
212 | if (!this._openFiles.hasOwnProperty(fd)) {
|
213 | throw new FSError('EBADF');
|
214 | }
|
215 | delete this._openFiles[fd];
|
216 | };
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 | Binding.prototype.stat = function(filepath, callback) {
|
226 | return maybeCallback(callback, this, function() {
|
227 | var item = this._system.getItem(filepath);
|
228 | if (item instanceof SymbolicLink) {
|
229 | item = this._system.getItem(
|
230 | path.resolve(path.dirname(filepath), item.getPath()));
|
231 | }
|
232 | if (!item) {
|
233 | throw new FSError('ENOENT', filepath);
|
234 | }
|
235 | return new Stats(item.getStats());
|
236 | });
|
237 | };
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 | Binding.prototype.fstat = function(fd, callback) {
|
247 | return maybeCallback(callback, this, function() {
|
248 | var descriptor = this._getDescriptorById(fd);
|
249 | var item = descriptor.getItem();
|
250 | return new Stats(item.getStats());
|
251 | });
|
252 | };
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 | Binding.prototype.close = function(fd, callback) {
|
261 | maybeCallback(callback, this, function() {
|
262 | this._untrackDescriptorById(fd);
|
263 | });
|
264 | };
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 | Binding.prototype.open = function(pathname, flags, mode, callback) {
|
276 | return maybeCallback(callback, this, function() {
|
277 | var descriptor = new FileDescriptor(flags);
|
278 | var item = this._system.getItem(pathname);
|
279 | if (item instanceof SymbolicLink) {
|
280 | item = this._system.getItem(
|
281 | path.resolve(path.dirname(pathname), item.getPath()));
|
282 | }
|
283 | if (descriptor.isExclusive() && item) {
|
284 | throw new FSError('EEXIST', pathname);
|
285 | }
|
286 | if (descriptor.isCreate() && !item) {
|
287 | var parent = this._system.getItem(path.dirname(pathname));
|
288 | if (!parent) {
|
289 | throw new FSError('ENOENT', pathname);
|
290 | }
|
291 | if (!(parent instanceof Directory)) {
|
292 | throw new FSError('ENOTDIR', pathname);
|
293 | }
|
294 | item = new File();
|
295 | if (mode) {
|
296 | item.setMode(mode);
|
297 | }
|
298 | parent.addItem(path.basename(pathname), item);
|
299 | }
|
300 | if (descriptor.isRead()) {
|
301 | if (!item) {
|
302 | throw new FSError('ENOENT', pathname);
|
303 | }
|
304 | if (!item.canRead()) {
|
305 | throw new FSError('EACCES', pathname);
|
306 | }
|
307 | }
|
308 | if (descriptor.isWrite() && !item.canWrite()) {
|
309 | throw new FSError('EACCES', pathname);
|
310 | }
|
311 | if (descriptor.isTruncate()) {
|
312 | item.setContent('');
|
313 | }
|
314 | if (descriptor.isTruncate() || descriptor.isAppend()) {
|
315 | descriptor.setPosition(item.getContent().length);
|
316 | }
|
317 | descriptor.setItem(item);
|
318 | return this._trackDescriptor(descriptor);
|
319 | });
|
320 | };
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 | Binding.prototype.read = function(fd, buffer, offset, length, position,
|
336 | callback) {
|
337 | return maybeCallback(callback, this, function() {
|
338 | var descriptor = this._getDescriptorById(fd);
|
339 | if (!descriptor.isRead()) {
|
340 | throw new FSError('EBADF');
|
341 | }
|
342 | var file = descriptor.getItem();
|
343 | if (!(file instanceof File)) {
|
344 |
|
345 | throw new FSError('EBADF');
|
346 | }
|
347 | if (typeof position !== 'number' || position < 0) {
|
348 | position = descriptor.getPosition();
|
349 | }
|
350 | var content = file.getContent();
|
351 | var start = Math.min(position, content.length);
|
352 | var end = Math.min(position + length, content.length);
|
353 | var read = (start < end) ? content.copy(buffer, offset, start, end) : 0;
|
354 | descriptor.setPosition(position + read);
|
355 | return read;
|
356 | });
|
357 | };
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 | Binding.prototype.writeBuffer = function(fd, buffer, offset, length, position,
|
373 | callback) {
|
374 | return maybeCallback(callback, this, function() {
|
375 | var descriptor = this._getDescriptorById(fd);
|
376 | if (!descriptor.isWrite()) {
|
377 | throw new FSError('EBADF');
|
378 | }
|
379 | var file = descriptor.getItem();
|
380 | if (!(file instanceof File)) {
|
381 |
|
382 | throw new FSError('EBADF');
|
383 | }
|
384 | if (typeof position !== 'number' || position < 0) {
|
385 | position = descriptor.getPosition();
|
386 | }
|
387 | var content = file.getContent();
|
388 | var newLength = position + length;
|
389 | if (content.length < newLength) {
|
390 | var newContent = new Buffer(newLength);
|
391 | content.copy(newContent);
|
392 | content = newContent;
|
393 | }
|
394 | var sourceEnd = Math.min(offset + length, buffer.length);
|
395 | var written = buffer.copy(content, position, offset, sourceEnd);
|
396 | file.setContent(content);
|
397 | descriptor.setPosition(newLength);
|
398 | return written;
|
399 | });
|
400 | };
|
401 |
|
402 |
|
403 |
|
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 |
|
410 |
|
411 |
|
412 |
|
413 |
|
414 |
|
415 | Binding.prototype.write = Binding.prototype.writeBuffer;
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 | Binding.prototype.writeString = function(fd, string, position, encoding,
|
430 | callback) {
|
431 | var buffer = new Buffer(string, encoding);
|
432 | var wrapper;
|
433 | if (callback) {
|
434 | callback = callback.oncomplete || callback;
|
435 | wrapper = function(err, written, returned) {
|
436 | callback(err, written, returned && string);
|
437 | };
|
438 | }
|
439 | return this.writeBuffer(fd, buffer, 0, string.length, position, wrapper);
|
440 | };
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 | Binding.prototype.rename = function(oldPath, newPath, callback) {
|
451 | return maybeCallback(callback, this, function() {
|
452 | var oldItem = this._system.getItem(oldPath);
|
453 | if (!oldItem) {
|
454 | throw new FSError('ENOENT', oldPath);
|
455 | }
|
456 | var oldParent = this._system.getItem(path.dirname(oldPath));
|
457 | var oldName = path.basename(oldPath);
|
458 | var newItem = this._system.getItem(newPath);
|
459 | var newParent = this._system.getItem(path.dirname(newPath));
|
460 | var newName = path.basename(newPath);
|
461 | if (newItem) {
|
462 |
|
463 | if (oldItem instanceof File) {
|
464 | if (newItem instanceof Directory) {
|
465 | throw new FSError('EISDIR', newPath);
|
466 | }
|
467 | } else if (oldItem instanceof Directory) {
|
468 | if (!(newItem instanceof Directory)) {
|
469 | throw new FSError('ENOTDIR', newPath);
|
470 | }
|
471 | if (newItem.list().length > 0) {
|
472 | throw new FSError('ENOTEMPTY', newPath);
|
473 | }
|
474 | }
|
475 | newParent.removeItem(newName);
|
476 | } else {
|
477 | if (!newParent) {
|
478 | throw new FSError('ENOENT', newPath);
|
479 | }
|
480 | if (!(newParent instanceof Directory)) {
|
481 | throw new FSError('ENOTDIR', newPath);
|
482 | }
|
483 | }
|
484 | oldParent.removeItem(oldName);
|
485 | newParent.addItem(newName, oldItem);
|
486 | });
|
487 | };
|
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 |
|
497 | Binding.prototype.readdir = function(dirpath, callback) {
|
498 | return maybeCallback(callback, this, function() {
|
499 | var dir = this._system.getItem(dirpath);
|
500 | if (!dir) {
|
501 | throw new FSError('ENOENT', dirpath);
|
502 | }
|
503 | if (!(dir instanceof Directory)) {
|
504 | throw new FSError('ENOTDIR', dirpath);
|
505 | }
|
506 | return dir.list();
|
507 | });
|
508 | };
|
509 |
|
510 |
|
511 |
|
512 |
|
513 |
|
514 |
|
515 |
|
516 |
|
517 | Binding.prototype.mkdir = function(pathname, mode, callback) {
|
518 | maybeCallback(callback, this, function() {
|
519 | var item = this._system.getItem(pathname);
|
520 | if (item) {
|
521 | throw new FSError('EEXIST', pathname);
|
522 | }
|
523 | var parent = this._system.getItem(path.dirname(pathname));
|
524 | if (!parent) {
|
525 | throw new FSError('ENOENT', pathname);
|
526 | }
|
527 | var dir = new Directory();
|
528 | if (mode) {
|
529 | dir.setMode(mode);
|
530 | }
|
531 | parent.addItem(path.basename(pathname), dir);
|
532 | });
|
533 | };
|
534 |
|
535 |
|
536 |
|
537 |
|
538 |
|
539 |
|
540 |
|
541 | Binding.prototype.rmdir = function(pathname, callback) {
|
542 | maybeCallback(callback, this, function() {
|
543 | var item = this._system.getItem(pathname);
|
544 | if (!item) {
|
545 | throw new FSError('ENOENT', pathname);
|
546 | }
|
547 | if (!(item instanceof Directory)) {
|
548 | throw new FSError('ENOTDIR', pathname);
|
549 | }
|
550 | if (item.list().length > 0) {
|
551 | throw new FSError('ENOTEMPTY', pathname);
|
552 | }
|
553 | var parent = this._system.getItem(path.dirname(pathname));
|
554 | parent.removeItem(path.basename(pathname));
|
555 | });
|
556 | };
|
557 |
|
558 |
|
559 |
|
560 |
|
561 |
|
562 |
|
563 |
|
564 |
|
565 | Binding.prototype.ftruncate = function(fd, len, callback) {
|
566 | maybeCallback(callback, this, function() {
|
567 | var descriptor = this._getDescriptorById(fd);
|
568 | if (!descriptor.isWrite()) {
|
569 | throw new FSError('EINVAL');
|
570 | }
|
571 | var file = descriptor.getItem();
|
572 | if (!(file instanceof File)) {
|
573 | throw new FSError('EINVAL');
|
574 | }
|
575 | var content = file.getContent();
|
576 | var newContent = new Buffer(len);
|
577 | content.copy(newContent);
|
578 | file.setContent(newContent);
|
579 | });
|
580 | };
|
581 |
|
582 |
|
583 |
|
584 |
|
585 |
|
586 |
|
587 |
|
588 |
|
589 | Binding.prototype.truncate = Binding.prototype.ftruncate;
|
590 |
|
591 |
|
592 |
|
593 |
|
594 |
|
595 |
|
596 |
|
597 |
|
598 |
|
599 | Binding.prototype.chown = function(pathname, uid, gid, callback) {
|
600 | maybeCallback(callback, this, function() {
|
601 | var item = this._system.getItem(pathname);
|
602 | if (!item) {
|
603 | throw new FSError('ENOENT', pathname);
|
604 | }
|
605 | item.setUid(uid);
|
606 | item.setGid(gid);
|
607 | });
|
608 | };
|
609 |
|
610 |
|
611 |
|
612 |
|
613 |
|
614 |
|
615 |
|
616 |
|
617 |
|
618 | Binding.prototype.fchown = function(fd, uid, gid, callback) {
|
619 | maybeCallback(callback, this, function() {
|
620 | var descriptor = this._getDescriptorById(fd);
|
621 | var item = descriptor.getItem();
|
622 | item.setUid(uid);
|
623 | item.setGid(gid);
|
624 | });
|
625 | };
|
626 |
|
627 |
|
628 |
|
629 |
|
630 |
|
631 |
|
632 |
|
633 |
|
634 | Binding.prototype.chmod = function(pathname, mode, callback) {
|
635 | maybeCallback(callback, this, function() {
|
636 | var item = this._system.getItem(pathname);
|
637 | if (!item) {
|
638 | throw new FSError('ENOENT', pathname);
|
639 | }
|
640 | item.setMode(mode);
|
641 | });
|
642 | };
|
643 |
|
644 |
|
645 |
|
646 |
|
647 |
|
648 |
|
649 |
|
650 |
|
651 | Binding.prototype.fchmod = function(fd, mode, callback) {
|
652 | maybeCallback(callback, this, function() {
|
653 | var descriptor = this._getDescriptorById(fd);
|
654 | var item = descriptor.getItem();
|
655 | item.setMode(mode);
|
656 | });
|
657 | };
|
658 |
|
659 |
|
660 |
|
661 |
|
662 |
|
663 |
|
664 |
|
665 | Binding.prototype.unlink = function(pathname, callback) {
|
666 | maybeCallback(callback, this, function() {
|
667 | var item = this._system.getItem(pathname);
|
668 | if (!item) {
|
669 | throw new FSError('ENOENT', pathname);
|
670 | }
|
671 | if (item instanceof Directory) {
|
672 | throw new FSError('EPERM', pathname);
|
673 | }
|
674 | var parent = this._system.getItem(path.dirname(pathname));
|
675 | parent.removeItem(path.basename(pathname));
|
676 | });
|
677 | };
|
678 |
|
679 |
|
680 |
|
681 |
|
682 |
|
683 |
|
684 |
|
685 |
|
686 |
|
687 | Binding.prototype.utimes = function(pathname, atime, mtime, callback) {
|
688 | maybeCallback(callback, this, function() {
|
689 | var item = this._system.getItem(pathname);
|
690 | if (!item) {
|
691 | throw new FSError('ENOENT', pathname);
|
692 | }
|
693 | item.setATime(new Date(atime * 1000));
|
694 | item.setMTime(new Date(mtime * 1000));
|
695 | });
|
696 | };
|
697 |
|
698 |
|
699 |
|
700 |
|
701 |
|
702 |
|
703 |
|
704 |
|
705 |
|
706 | Binding.prototype.futimes = function(fd, atime, mtime, callback) {
|
707 | maybeCallback(callback, this, function() {
|
708 | var descriptor = this._getDescriptorById(fd);
|
709 | var item = descriptor.getItem();
|
710 | item.setATime(new Date(atime * 1000));
|
711 | item.setMTime(new Date(mtime * 1000));
|
712 | });
|
713 | };
|
714 |
|
715 |
|
716 |
|
717 |
|
718 |
|
719 |
|
720 |
|
721 | Binding.prototype.fsync = function(fd, callback) {
|
722 | maybeCallback(callback, this, function() {
|
723 | this._getDescriptorById(fd);
|
724 | });
|
725 | };
|
726 |
|
727 |
|
728 |
|
729 |
|
730 |
|
731 |
|
732 |
|
733 | Binding.prototype.fdatasync = function(fd, callback) {
|
734 | maybeCallback(callback, this, function() {
|
735 | this._getDescriptorById(fd);
|
736 | });
|
737 | };
|
738 |
|
739 |
|
740 |
|
741 |
|
742 |
|
743 |
|
744 |
|
745 |
|
746 | Binding.prototype.link = function(srcPath, destPath, callback) {
|
747 | maybeCallback(callback, this, function() {
|
748 | var item = this._system.getItem(srcPath);
|
749 | if (!item) {
|
750 | throw new FSError('ENOENT', srcPath);
|
751 | }
|
752 | if (item instanceof Directory) {
|
753 | throw new FSError('EPERM', srcPath);
|
754 | }
|
755 | if (this._system.getItem(destPath)) {
|
756 | throw new FSError('EEXIST', destPath);
|
757 | }
|
758 | var parent = this._system.getItem(path.dirname(destPath));
|
759 | if (!parent) {
|
760 | throw new FSError('ENOENT', destPath);
|
761 | }
|
762 | if (!(parent instanceof Directory)) {
|
763 | throw new FSError('ENOTDIR', destPath);
|
764 | }
|
765 | parent.addItem(path.basename(destPath), item);
|
766 | });
|
767 | };
|
768 |
|
769 |
|
770 |
|
771 |
|
772 |
|
773 |
|
774 |
|
775 |
|
776 |
|
777 | Binding.prototype.symlink = function(srcPath, destPath, type, callback) {
|
778 | maybeCallback(callback, this, function() {
|
779 | if (this._system.getItem(destPath)) {
|
780 | throw new FSError('EEXIST', destPath);
|
781 | }
|
782 | var parent = this._system.getItem(path.dirname(destPath));
|
783 | if (!parent) {
|
784 | throw new FSError('ENOENT', destPath);
|
785 | }
|
786 | if (!(parent instanceof Directory)) {
|
787 | throw new FSError('ENOTDIR', destPath);
|
788 | }
|
789 | var link = new SymbolicLink();
|
790 | link.setPath(srcPath);
|
791 | parent.addItem(path.basename(destPath), link);
|
792 | });
|
793 | };
|
794 |
|
795 |
|
796 |
|
797 |
|
798 |
|
799 |
|
800 |
|
801 |
|
802 | Binding.prototype.readlink = function(pathname, callback) {
|
803 | return maybeCallback(callback, this, function() {
|
804 | var link = this._system.getItem(pathname);
|
805 | if (!(link instanceof SymbolicLink)) {
|
806 | throw new FSError('EINVAL', pathname);
|
807 | }
|
808 | return link.getPath();
|
809 | });
|
810 | };
|
811 |
|
812 |
|
813 |
|
814 |
|
815 |
|
816 |
|
817 |
|
818 |
|
819 | Binding.prototype.lstat = function(filepath, callback) {
|
820 | return maybeCallback(callback, this, function() {
|
821 | var item = this._system.getItem(filepath);
|
822 | if (!item) {
|
823 | throw new FSError('ENOENT', filepath);
|
824 | }
|
825 | return new Stats(item.getStats());
|
826 | });
|
827 | };
|
828 |
|
829 |
|
830 |
|
831 |
|
832 |
|
833 |
|
834 | Binding.prototype.StatWatcher = notImplemented;
|
835 |
|
836 |
|
837 |
|
838 |
|
839 |
|
840 |
|
841 | exports = module.exports = Binding;
|