1 | var path = require('path');
|
2 | var util = require('util');
|
3 |
|
4 | var Item = require('./item').Item;
|
5 |
|
6 | var constants = process.binding('constants');
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | var Directory = exports.Directory = function Directory(name) {
|
16 | Item.call(this, name);
|
17 |
|
18 | |
19 |
|
20 |
|
21 |
|
22 | this._items = {};
|
23 |
|
24 | };
|
25 | util.inherits(Directory, Item);
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | Directory.prototype.addItem = function(item) {
|
34 | var name = item.getName();
|
35 | if (this._items.hasOwnProperty(name)) {
|
36 | throw new Error('Item with the same name already exists: ' + name);
|
37 | }
|
38 | this._items[name] = item;
|
39 |
|
40 | item.setParent(this);
|
41 | return item;
|
42 | };
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | Directory.prototype.getItem = function(name) {
|
51 | var item = null;
|
52 | if (this._items.hasOwnProperty(name)) {
|
53 | item = this._items[name];
|
54 | }
|
55 | return item;
|
56 | };
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | Directory.prototype.removeItem = function(name) {
|
65 | if (!this._items.hasOwnProperty(name)) {
|
66 | throw new Error('Item does not exist in directory: ' + name);
|
67 | }
|
68 | var item = this._items[name];
|
69 | delete this._items[name];
|
70 | item.setParent(null);
|
71 | return item;
|
72 | };
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | Directory.prototype.renameItem = function(oldName, newName) {
|
82 | if (!this._items.hasOwnProperty(oldName)) {
|
83 | throw new Error('Item does not exist in directory: ' + oldName);
|
84 | }
|
85 | if (this._items.hasOwnProperty(newName)) {
|
86 | throw new Error('Item with same name already exists: ' + newName);
|
87 | }
|
88 | var item = this._items[oldName];
|
89 | delete this._items[oldName];
|
90 | this._items[newName] = item;
|
91 | item.setName(newName);
|
92 | return item;
|
93 | };
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | Directory.prototype.list = function() {
|
101 | return Object.keys(this._items).sort();
|
102 | };
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | Directory.prototype.getStats = function() {
|
110 | return {
|
111 | mode: this.getMode() | constants.S_IFDIR,
|
112 | atime: this.getATime(),
|
113 | mtime: this.getMTime(),
|
114 | ctime: this.getCTime(),
|
115 | uid: this.getUid(),
|
116 | gid: this.getGid(),
|
117 | size: 1
|
118 | };
|
119 | };
|