This repository was 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.5k
Expand file tree
/
Copy pathAppshellFileSystem.js
More file actions
606 lines (549 loc) · 22.7 KB
/
AppshellFileSystem.js
File metadata and controls
606 lines (549 loc) · 22.7 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/*
* Copyright (c) 2013 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.
*
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, appshell, $, window */
define(function (require, exports, module) {
"use strict";
var FileUtils = require("file/FileUtils"),
FileSystemStats = require("filesystem/FileSystemStats"),
FileSystemError = require("filesystem/FileSystemError"),
NodeDomain = require("utils/NodeDomain");
var FILE_WATCHER_BATCH_TIMEOUT = 200; // 200ms - granularity of file watcher changes
/**
* Callback to notify FileSystem of watcher changes
* @type {?function(string, FileSystemStats=)}
*/
var _changeCallback;
/**
* Callback to notify FileSystem if watchers stop working entirely
* @type {?function()}
*/
var _offlineCallback;
/** Timeout used to batch up file watcher changes (setTimeout() return value) */
var _changeTimeout;
/**
* Pending file watcher changes - map from fullPath to flag indicating whether we need to pass stats
* to _changeCallback() for this path.
* @type {!Object.<string, boolean>}
*/
var _pendingChanges = {};
var _bracketsPath = FileUtils.getNativeBracketsDirectoryPath(),
_modulePath = FileUtils.getNativeModuleDirectoryPath(module),
_nodePath = "node/FileWatcherDomain",
_domainPath = [_bracketsPath, _modulePath, _nodePath].join("/"),
_nodeDomain = new NodeDomain("fileWatcher", _domainPath);
var _isRunningOnWindowsXP = navigator.userAgent.indexOf("Windows NT 5.") >= 0;
// If the connection closes, notify the FileSystem that watchers have gone offline.
$(_nodeDomain.connection).on("close", function (event, promise) {
if (_offlineCallback) {
_offlineCallback();
}
});
/**
* Enqueue a file change event for eventual reporting back to the FileSystem.
*
* @param {string} changedPath The path that was changed
* @param {boolean} needsStats Whether or not the eventual change event should include stats
* @private
*/
function _enqueueChange(changedPath, needsStats) {
_pendingChanges[changedPath] = _pendingChanges[changedPath] || needsStats;
if (!_changeTimeout) {
_changeTimeout = window.setTimeout(function () {
if (_changeCallback) {
Object.keys(_pendingChanges).forEach(function (path) {
var needsStats = _pendingChanges[path];
if (needsStats) {
exports.stat(path, function (err, stats) {
if (err) {
// warning has been removed due to spamming the console - see #7332
// console.warn("Unable to stat changed path: ", path, err);
return;
}
_changeCallback(path, stats);
});
} else {
_changeCallback(path);
}
});
}
_changeTimeout = null;
_pendingChanges = {};
}, FILE_WATCHER_BATCH_TIMEOUT);
}
}
/**
* Event handler for the Node fileWatcher domain's change event.
*
* @param {jQuery.Event} The underlying change event
* @param {string} path The path that is reported to have changed
* @param {string} event The type of the event: either "change" or "rename"
* @param {string=} filename The name of the file that changed.
* @private
*/
function _fileWatcherChange(evt, path, event, filename) {
var change;
if (event === "change") {
// Only register change events if filename is passed
if (filename) {
// an existing file was modified; stats are needed
change = path + filename;
_enqueueChange(change, true);
}
} else if (event === "rename") {
// a new file was created; no stats are needed
change = path;
_enqueueChange(change, false);
}
}
// Setup the change handler. This only needs to happen once.
$(_nodeDomain).on("change", _fileWatcherChange);
/**
* Convert appshell error codes to FileSystemError values.
*
* @param {?number} err An appshell error code
* @return {?string} A FileSystemError string, or null if there was no error code.
* @private
*/
function _mapError(err) {
if (!err) {
return null;
}
switch (err) {
case appshell.fs.ERR_INVALID_PARAMS:
return FileSystemError.INVALID_PARAMS;
case appshell.fs.ERR_NOT_FOUND:
return FileSystemError.NOT_FOUND;
case appshell.fs.ERR_CANT_READ:
return FileSystemError.NOT_READABLE;
case appshell.fs.ERR_CANT_WRITE:
return FileSystemError.NOT_WRITABLE;
case appshell.fs.ERR_UNSUPPORTED_ENCODING:
return FileSystemError.NOT_READABLE;
case appshell.fs.ERR_OUT_OF_SPACE:
return FileSystemError.OUT_OF_SPACE;
case appshell.fs.ERR_FILE_EXISTS:
return FileSystemError.ALREADY_EXISTS;
}
return FileSystemError.UNKNOWN;
}
/**
* Convert a callback to one that transforms its first parameter from an
* appshell error code to a FileSystemError string.
*
* @param {function(?number)} cb A callback that expects an appshell error code
* @return {function(?string)} A callback that expects a FileSystemError string
* @private
*/
function _wrap(cb) {
return function (err) {
var args = Array.prototype.slice.call(arguments);
args[0] = _mapError(args[0]);
cb.apply(null, args);
};
}
/**
* Display an open-files dialog to the user and call back asynchronously with
* either a FileSystmError string or an array of path strings, which indicate
* the entry or entries selected.
*
* @param {boolean} allowMultipleSelection
* @param {boolean} chooseDirectories
* @param {string} title
* @param {string} initialPath
* @param {Array.<string>=} fileTypes
* @param {function(?string, Array.<string>=)} callback
*/
function showOpenDialog(allowMultipleSelection, chooseDirectories, title, initialPath, fileTypes, callback) {
appshell.fs.showOpenDialog(allowMultipleSelection, chooseDirectories, title, initialPath, fileTypes, _wrap(callback));
}
/**
* Display a save-file dialog and call back asynchronously with either a
* FileSystemError string or the path to which the user has chosen to save
* the file. If the dialog is cancelled, the path string will be empty.
*
* @param {string} title
* @param {string} initialPath
* @param {string} proposedNewFilename
* @param {function(?string, string=)} callback
*/
function showSaveDialog(title, initialPath, proposedNewFilename, callback) {
appshell.fs.showSaveDialog(title, initialPath, proposedNewFilename, _wrap(callback));
}
/**
* Stat the file or directory at the given path, calling back
* asynchronously with either a FileSystemError string or the entry's
* associated FileSystemStats object.
*
* @param {string} path
* @param {function(?string, FileSystemStats=)} callback
*/
function stat(path, callback) {
appshell.fs.stat(path, function (err, stats) {
if (err) {
callback(_mapError(err));
} else {
var options = {
isFile: stats.isFile(),
mtime: stats.mtime,
size: stats.size,
realPath: stats.realPath,
hash: stats.mtime.getTime()
};
var fsStats = new FileSystemStats(options);
callback(null, fsStats);
}
});
}
/**
* Determine whether a file or directory exists at the given path by calling
* back asynchronously with either a FileSystemError string or a boolean,
* which is true if the file exists and false otherwise. The error will never
* be FileSystemError.NOT_FOUND; in that case, there will be no error and the
* boolean parameter will be false.
*
* @param {string} path
* @param {function(?string, boolean)} callback
*/
function exists(path, callback) {
stat(path, function (err) {
if (err) {
if (err === FileSystemError.NOT_FOUND) {
callback(null, false);
} else {
callback(err);
}
return;
}
callback(null, true);
});
}
/**
* Read the contents of the directory at the given path, calling back
* asynchronously either with a FileSystemError string or an array of
* FileSystemEntry objects along with another consistent array, each index
* of which either contains a FileSystemStats object for the corresponding
* FileSystemEntry object in the second parameter or a FileSystemError
* string describing a stat error.
*
* @param {string} path
* @param {function(?string, Array.<FileSystemEntry>=, Array.<string|FileSystemStats>=)} callback
*/
function readdir(path, callback) {
appshell.fs.readdir(path, function (err, contents) {
if (err) {
callback(_mapError(err));
return;
}
var count = contents.length;
if (!count) {
callback(null, [], []);
return;
}
var stats = [];
contents.forEach(function (val, idx) {
stat(path + "/" + val, function (err, stat) {
stats[idx] = err || stat;
count--;
if (count <= 0) {
callback(null, contents, stats);
}
});
});
});
}
/**
* Create a directory at the given path, and call back asynchronously with
* either a FileSystemError string or a stats object for the newly created
* directory. The octal mode parameter is optional; if unspecified, the mode
* of the created directory is implementation dependent.
*
* @param {string} path
* @param {number=} mode The base-eight mode of the newly created directory.
* @param {function(?string, FileSystemStats=)=} callback
*/
function mkdir(path, mode, callback) {
if (typeof mode === "function") {
callback = mode;
mode = parseInt("0755", 8);
}
appshell.fs.makedir(path, mode, function (err) {
if (err) {
callback(_mapError(err));
} else {
stat(path, function (err, stat) {
callback(err, stat);
});
}
});
}
/**
* Rename the file or directory at oldPath to newPath, and call back
* asynchronously with a possibly null FileSystemError string.
*
* @param {string} oldPath
* @param {string} newPath
* @param {function(?string)=} callback
*/
function rename(oldPath, newPath, callback) {
appshell.fs.rename(oldPath, newPath, _wrap(callback));
}
/**
* Read the contents of the file at the given path, calling back
* asynchronously with either a FileSystemError string, or with the data and
* the FileSystemStats object associated with the read file. The options
* parameter can be used to specify an encoding (default "utf8"), and also
* a cached stats object that the implementation is free to use in order
* to avoid an additional stat call.
*
* Note: if either the read or the stat call fails then neither the read data
* nor stat will be passed back, and the call should be considered to have failed.
* If both calls fail, the error from the read call is passed back.
*
* @param {string} path
* @param {{encoding: string=, stat: FileSystemStats=}} options
* @param {function(?string, string=, FileSystemStats=)} callback
*/
function readFile(path, options, callback) {
var encoding = options.encoding || "utf8";
// Execute the read and stat calls in parallel. Callback early if the
// read call completes first with an error; otherwise wait for both
// to finish.
var done = false, data, stat, err;
if (options.stat) {
done = true;
stat = options.stat;
} else {
exports.stat(path, function (_err, _stat) {
if (done) {
callback(_err, _err ? null : data, _stat);
} else {
done = true;
stat = _stat;
err = _err;
}
});
}
appshell.fs.readFile(path, encoding, function (_err, _data) {
if (_err) {
callback(_mapError(_err));
return;
}
if (done) {
callback(err, err ? null : _data, stat);
} else {
done = true;
data = _data;
}
});
}
/**
* Write data to the file at the given path, calling back asynchronously with
* either a FileSystemError string or the FileSystemStats object associated
* with the written file and a boolean that indicates whether the file was
* created by the write (true) or not (false). If no file exists at the
* given path, a new file will be created. The options parameter can be used
* to specify an encoding (default "utf8"), an octal mode (default
* unspecified and implementation dependent), and a consistency hash, which
* is used to the current state of the file before overwriting it. If a
* consistency hash is provided but does not match the hash of the file on
* disk, a FileSystemError.CONTENTS_MODIFIED error is passed to the callback.
*
* @param {string} path
* @param {string} data
* @param {{encoding : string=, mode : number=, expectedHash : object=, expectedContents : string=}} options
* @param {function(?string, FileSystemStats=, boolean)} callback
*/
function writeFile(path, data, options, callback) {
var encoding = options.encoding || "utf8";
function _finishWrite(created) {
appshell.fs.writeFile(path, data, encoding, function (err) {
if (err) {
callback(_mapError(err));
} else {
stat(path, function (err, stat) {
callback(err, stat, created);
});
}
});
}
stat(path, function (err, stats) {
if (err) {
switch (err) {
case FileSystemError.NOT_FOUND:
_finishWrite(true);
break;
default:
callback(err);
}
return;
}
if (options.hasOwnProperty("expectedHash") && options.expectedHash !== stats._hash) {
console.error("Blind write attempted: ", path, stats._hash, options.expectedHash);
if (options.hasOwnProperty("expectedContents")) {
appshell.fs.readFile(path, encoding, function (_err, _data) {
if (_err || _data !== options.expectedContents) {
callback(FileSystemError.CONTENTS_MODIFIED);
return;
}
_finishWrite(false);
});
return;
} else {
callback(FileSystemError.CONTENTS_MODIFIED);
return;
}
}
_finishWrite(false);
});
}
/**
* Unlink (i.e., permanently delete) the file or directory at the given path,
* calling back asynchronously with a possibly null FileSystemError string.
* Directories will be unlinked even when non-empty.
*
* @param {string} path
* @param {function(string)=} callback
*/
function unlink(path, callback) {
appshell.fs.unlink(path, function (err) {
callback(_mapError(err));
});
}
/**
* Move the file or directory at the given path to a system dependent trash
* location, calling back asynchronously with a possibly null FileSystemError
* string. Directories will be moved even when non-empty.
*
* @param {string} path
* @param {function(string)=} callback
*/
function moveToTrash(path, callback) {
appshell.fs.moveToTrash(path, function (err) {
callback(_mapError(err));
});
}
/**
* Initialize file watching for this filesystem, using the supplied
* changeCallback to provide change notifications. The first parameter of
* changeCallback specifies the changed path (either a file or a directory);
* if this parameter is null, it indicates that the implementation cannot
* specify a particular changed path, and so the callers should consider all
* paths to have changed and to update their state accordingly. The second
* parameter to changeCallback is an optional FileSystemStats object that
* may be provided in case the changed path already exists and stats are
* readily available. The offlineCallback will be called in case watchers
* are no longer expected to function properly. All watched paths are
* cleared when the offlineCallback is called.
*
* @param {function(?string, FileSystemStats=)} changeCallback
* @param {function()=} offlineCallback
*/
function initWatchers(changeCallback, offlineCallback) {
_changeCallback = changeCallback;
_offlineCallback = offlineCallback;
if (_isRunningOnWindowsXP && _offlineCallback) {
_offlineCallback();
}
}
/**
* Start providing change notifications for the file or directory at the
* given path, calling back asynchronously with a possibly null FileSystemError
* string when the initialization is complete. Notifications are provided
* using the changeCallback function provided by the initWatchers method.
* Note that change notifications are only provided recursively for directories
* when the recursiveWatch property of this module is true.
*
* @param {string} path
* @param {function(?string)=} callback
*/
function watchPath(path, callback) {
if (_isRunningOnWindowsXP) {
callback(FileSystemError.NOT_SUPPORTED);
return;
}
appshell.fs.isNetworkDrive(path, function (err, isNetworkDrive) {
if (err || isNetworkDrive) {
callback(FileSystemError.UNKNOWN);
return;
}
_nodeDomain.exec("watchPath", path)
.then(callback, callback);
});
}
/**
* Stop providing change notifications for the file or directory at the
* given path, calling back asynchronously with a possibly null FileSystemError
* string when the operation is complete.
*
* @param {string} path
* @param {function(?string)=} callback
*/
function unwatchPath(path, callback) {
_nodeDomain.exec("unwatchPath", path)
.then(callback, callback);
}
/**
* Stop providing change notifications for all previously watched files and
* directories, optionally calling back asynchronously with a possibly null
* FileSystemError string when the operation is complete.
*
* @param {function(?string)=} callback
*/
function unwatchAll(callback) {
_nodeDomain.exec("unwatchAll")
.then(callback, callback);
}
// Export public API
exports.showOpenDialog = showOpenDialog;
exports.showSaveDialog = showSaveDialog;
exports.exists = exists;
exports.readdir = readdir;
exports.mkdir = mkdir;
exports.rename = rename;
exports.stat = stat;
exports.readFile = readFile;
exports.writeFile = writeFile;
exports.unlink = unlink;
exports.moveToTrash = moveToTrash;
exports.initWatchers = initWatchers;
exports.watchPath = watchPath;
exports.unwatchPath = unwatchPath;
exports.unwatchAll = unwatchAll;
/**
* Indicates whether or not recursive watching notifications are supported
* by the watchPath call. Currently, only Darwin supports recursive watching.
*
* @type {boolean}
*/
exports.recursiveWatch = (appshell.platform === "mac" || appshell.platform === "win");
/**
* Indicates whether or not the filesystem should expect and normalize UNC
* paths. If set, then //server/directory/ is a normalized path; otherwise the
* filesystem will normalize it to /server/directory. Currently, UNC path
* normalization only occurs on Windows.
*
* @type {boolean}
*/
exports.normalizeUNCPaths = appshell.platform === "win";
});