This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Directory.js
264 lines (232 loc) · 10.2 KB
/
Directory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
* Copyright (c) 2013 - present Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
define(function (require, exports, module) {
"use strict";
var FileSystemEntry = require("filesystem/FileSystemEntry");
/*
* Model for a file system Directory.
*
* This class should *not* be instantiated directly. Use FileSystem.getDirectoryForPath,
* FileSystem.resolve, or Directory.getContents to create an instance of this class.
*
* Note: Directory.fullPath always has a trailing slash.
*
* See the FileSystem class for more details.
*
* @constructor
* @param {!string} fullPath The full path for this Directory.
* @param {!FileSystem} fileSystem The file system associated with this Directory.
*/
function Directory(fullPath, fileSystem) {
this._isDirectory = true;
FileSystemEntry.call(this, fullPath, fileSystem);
}
Directory.prototype = Object.create(FileSystemEntry.prototype);
Directory.prototype.constructor = Directory;
Directory.prototype.parentClass = FileSystemEntry.prototype;
/**
* The contents of this directory. This "private" property is used by FileSystem.
* @type {Array<FileSystemEntry>}
*/
Directory.prototype._contents = null;
/**
* The stats for the contents of this directory, such that this._contentsStats[i]
* corresponds to this._contents[i].
* @type {Array.<FileSystemStats>}
*/
Directory.prototype._contentsStats = null;
/**
* The stats errors for the contents of this directory.
* @type {object.<string: string>} fullPaths are mapped to FileSystemError strings
*/
Directory.prototype._contentsStatsErrors = null;
/**
* Clear any cached data for this directory. By default, we clear the contents
* of immediate children as well, because in some cases file watchers fail
* provide precise change notifications. (Sometimes, like after a "git
* checkout", they just report that some directory has changed when in fact
* many of the file within the directory have changed.
*
* @private
* @param {boolean=} preserveImmediateChildren
*/
Directory.prototype._clearCachedData = function (preserveImmediateChildren) {
FileSystemEntry.prototype._clearCachedData.apply(this);
if (!preserveImmediateChildren) {
if (this._contents) {
this._contents.forEach(function (child) {
child._clearCachedData(true);
});
} else {
// No cached _contents, but child entries may still exist.
// Scan the full index to catch all of them.
var dirPath = this.fullPath;
this._fileSystem._index.visitAll(function (entry) {
if (entry.parentPath === dirPath) {
entry._clearCachedData(true);
}
});
}
}
this._contents = undefined;
this._contentsStats = undefined;
this._contentsStatsErrors = undefined;
};
/**
* Apply each callback in a list to the provided arguments. Callbacks
* can throw without preventing other callbacks from being applied.
*
* @private
* @param {Array.<function>} callbacks The callbacks to apply
* @param {Array} args The arguments to which each callback is applied
*/
function _applyAllCallbacks(callbacks, args) {
if (callbacks.length > 0) {
var callback = callbacks.pop();
try {
callback.apply(undefined, args);
} finally {
_applyAllCallbacks(callbacks, args);
}
}
}
/**
* Read the contents of a Directory. If this Directory is under a watch root,
* the listing will exclude any items filtered out by the watch root's filter
* function.
*
* @param {Directory} directory Directory whose contents you want to get
* @param {function (?string, Array.<FileSystemEntry>=, Array.<FileSystemStats>=, Object.<string, string>=)} callback
* Callback that is passed an error code or the stat-able contents
* of the directory along with the stats for these entries and a
* fullPath-to-FileSystemError string map of unstat-able entries
* and their stat errors. If there are no stat errors then the last
* parameter shall remain undefined.
*/
Directory.prototype.getContents = function (callback) {
if (this._contentsCallbacks) {
// There is already a pending call for this directory's contents.
// Push the new callback onto the stack and return.
this._contentsCallbacks.push(callback);
return;
}
// Return cached contents if the directory is watched
if (this._contents) {
callback(null, this._contents, this._contentsStats, this._contentsStatsErrors);
return;
}
this._contentsCallbacks = [callback];
this._impl.readdir(this.fullPath, function (err, names, stats) {
var contents = [],
contentsStats = [],
contentsStatsErrors;
if (err) {
this._clearCachedData();
} else {
// Use the "relaxed" parameter to _isWatched because it's OK to
// cache data even while watchers are still starting up
var watched = this._isWatched(true);
names.forEach(function (name, index) {
var entryPath = this.fullPath + name;
var entryStats = stats[index];
if (this._fileSystem._indexFilter(entryPath, name, entryStats)) {
var entry;
// Note: not all entries necessarily have associated stats.
if (typeof entryStats === "string") {
// entryStats is an error string
if (contentsStatsErrors === undefined) {
contentsStatsErrors = {};
}
contentsStatsErrors[entryPath] = entryStats;
} else {
// entryStats is a FileSystemStats object
if (entryStats.isFile) {
entry = this._fileSystem.getFileForPath(entryPath);
} else {
entry = this._fileSystem.getDirectoryForPath(entryPath);
}
if (watched) {
entry._stat = entryStats;
}
contents.push(entry);
contentsStats.push(entryStats);
}
}
}, this);
if (watched) {
this._contents = contents;
this._contentsStats = contentsStats;
this._contentsStatsErrors = contentsStatsErrors;
}
}
// Reset the callback list before we begin calling back so that
// synchronous reentrant calls are handled correctly.
var currentCallbacks = this._contentsCallbacks;
this._contentsCallbacks = null;
// Invoke all saved callbacks
var callbackArgs = [err, contents, contentsStats, contentsStatsErrors];
_applyAllCallbacks(currentCallbacks, callbackArgs);
}.bind(this));
};
/**
* Create a directory
*
* @param {function (?string, FileSystemStats=)=} callback Callback resolved with a
* FileSystemError string or the stat object for the created directory.
*/
Directory.prototype.create = function (callback) {
callback = callback || function () {};
// Block external change events until after the write has finished
this._fileSystem._beginChange();
this._impl.mkdir(this._path, function (err, stat) {
if (err) {
this._clearCachedData();
try {
callback(err);
return;
} finally {
// Unblock external change events
this._fileSystem._endChange();
}
}
var parent = this._fileSystem.getDirectoryForPath(this.parentPath);
// Update internal filesystem state
if (this._isWatched()) {
this._stat = stat;
}
this._fileSystem._handleDirectoryChange(parent, function (added, removed) {
try {
callback(null, stat);
} finally {
if (parent._isWatched()) {
this._fileSystem._fireChangeEvent(parent, added, removed);
}
// Unblock external change events
this._fileSystem._endChange();
}
}.bind(this));
}.bind(this));
};
// Export this class
module.exports = Directory;
});