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