1 | 'use strict';
|
2 |
|
3 | const util = require('util');
|
4 |
|
5 | const Item = require('./item');
|
6 |
|
7 | const constants = require('constants');
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | function Directory() {
|
14 | Item.call(this);
|
15 |
|
16 | |
17 |
|
18 |
|
19 |
|
20 | this._items = {};
|
21 |
|
22 | |
23 |
|
24 |
|
25 | this._mode = 511;
|
26 | }
|
27 | util.inherits(Directory, Item);
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | Directory.prototype.addItem = function(name, item) {
|
36 | if (this._items.hasOwnProperty(name)) {
|
37 | throw new Error('Item with the same name already exists: ' + name);
|
38 | }
|
39 | this._items[name] = item;
|
40 | ++item.links;
|
41 | if (item instanceof Directory) {
|
42 |
|
43 | ++item.links;
|
44 |
|
45 | ++this.links;
|
46 | }
|
47 | this.setMTime(new Date());
|
48 | return item;
|
49 | };
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | Directory.prototype.getItem = function(name) {
|
57 | let item = null;
|
58 | if (this._items.hasOwnProperty(name)) {
|
59 | item = this._items[name];
|
60 | }
|
61 | return item;
|
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 | const 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 | Directory.prototype.list = function() {
|
91 | return Object.keys(this._items).sort();
|
92 | };
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | Directory.prototype.getStats = function(bigint) {
|
99 | const stats = Item.prototype.getStats.call(this, bigint);
|
100 | const convert = bigint ? v => BigInt(v) : v => v;
|
101 |
|
102 | stats[1] = convert(this.getMode() | constants.S_IFDIR);
|
103 | stats[8] = convert(1);
|
104 | stats[9] = convert(1);
|
105 |
|
106 | return stats;
|
107 | };
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | exports = module.exports = Directory;
|