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