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
/
File.js
232 lines (203 loc) · 7.82 KB
/
File.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
/*
* 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.
*
* This class should *not* be instantiated directly. Use FileSystem.getFileForPath,
* FileSystem.resolve, or Directory.getContents to create an instance of this class.
*
* See the FileSystem class for more details.
*
* @constructor
* @param {!string} fullPath The full path for this File.
* @param {!FileSystem} fileSystem The file system associated with this File.
*/
function File(fullPath, fileSystem) {
this._isFile = true;
FileSystemEntry.call(this, fullPath, fileSystem);
}
File.prototype = Object.create(FileSystemEntry.prototype);
File.prototype.constructor = File;
File.prototype.parentClass = FileSystemEntry.prototype;
/**
* Cached contents of this file. This value is nullable but should NOT be undefined.
* @private
* @type {?string}
*/
File.prototype._contents = null;
/**
* Encoding detected by brackets-shell
* @private
* @type {?string}
*/
File.prototype._encoding = null;
/**
* BOM detected by brackets-shell
* @private
* @type {?bool}
*/
File.prototype._preserveBOM = false;
/**
* Consistency hash for this file. Reads and writes update this value, and
* writes confirm the hash before overwriting existing files. The type of
* this object is dependent on the FileSystemImpl; the only constraint is
* that === can be used as an equality relation on hashes.
* @private
* @type {?object}
*/
File.prototype._hash = null;
/**
* Clear any cached data for this file. Note that this explicitly does NOT
* clear the file's hash.
* @private
*/
File.prototype._clearCachedData = function () {
FileSystemEntry.prototype._clearCachedData.apply(this);
this._contents = null;
};
/**
* Read a file.
*
* @param {Object=} options Currently unused.
* @param {function (?string, string=, FileSystemStats=)} callback Callback that is passed the
* FileSystemError string or the file's contents and its stats.
*/
File.prototype.read = function (options, callback) {
if (typeof (options) === "function") {
callback = options;
options = {};
options.encoding = this._encoding;
}
options.encoding = this._encoding || "utf8";
// We don't need to check isWatched() here because contents are only saved
// for watched files. Note that we need to explicitly test this._contents
// for a default value; otherwise it could be the empty string, which is
// falsey.
if (this._contents !== null && this._stat) {
callback(null, this._contents, this._encoding, this._stat);
return;
}
var watched = this._isWatched();
if (watched) {
options.stat = this._stat;
}
this._impl.readFile(this._path, options, function (err, data, encoding, preserveBOM, stat) {
if (err) {
this._clearCachedData();
callback(err);
return;
}
// Always store the hash
this._hash = stat._hash;
this._encoding = encoding;
this._preserveBOM = preserveBOM;
// Only cache data for watched files
if (watched) {
this._stat = stat;
this._contents = data;
}
callback(err, data, encoding, stat);
}.bind(this));
};
/**
* Write a file.
*
* @param {string} data Data to write.
* @param {object=} options Currently unused.
* @param {function (?string, FileSystemStats=)=} callback Callback that is passed the
* FileSystemError string or the file's new stats.
*/
File.prototype.write = function (data, options, callback) {
if (typeof options === "function") {
callback = options;
options = {};
} else {
if (options === undefined) {
options = {};
}
callback = callback || function () {};
}
// Request a consistency check if the write is not blind
if (!options.blind) {
options.expectedHash = this._hash;
options.expectedContents = this._contents;
}
if (!options.encoding) {
options.encoding = this._encoding || "utf8";
}
options.preserveBOM = this._preserveBOM;
// Block external change events until after the write has finished
this._fileSystem._beginChange();
this._impl.writeFile(this._path, data, options, function (err, stat, created) {
if (err) {
this._clearCachedData();
try {
callback(err);
return;
} finally {
// Always unblock external change events
this._fileSystem._endChange();
}
}
// Always store the hash
this._hash = stat._hash;
// Only cache data for watched files
if (this._isWatched()) {
this._stat = stat;
this._contents = data;
}
if (created) {
var parent = this._fileSystem.getDirectoryForPath(this.parentPath);
this._fileSystem._handleDirectoryChange(parent, function (added, removed) {
try {
// Notify the caller
callback(null, stat);
} finally {
if (parent._isWatched()) {
// If the write succeeded and the parent directory is watched,
// fire a synthetic change event
this._fileSystem._fireChangeEvent(parent, added, removed);
}
// Always unblock external change events
this._fileSystem._endChange();
}
}.bind(this));
} else {
try {
// Notify the caller
callback(null, stat);
} finally {
// existing file modified
this._fileSystem._fireChangeEvent(this);
// Always unblock external change events
this._fileSystem._endChange();
}
}
}.bind(this));
};
// Export this class
module.exports = File;
});