2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * @param {!WebInspector.IsolatedFileSystemManager} manager
34 * @param {string} path
35 * @param {string} name
36 * @param {string} rootURL
38 WebInspector
.IsolatedFileSystem = function(manager
, path
, name
, rootURL
)
40 this._manager
= manager
;
43 this._rootURL
= rootURL
;
47 * @param {!FileError} error
50 WebInspector
.IsolatedFileSystem
.errorMessage = function(error
)
52 return WebInspector
.UIString("File system error: %s", error
.message
);
56 * @param {string} fileSystemPath
59 WebInspector
.IsolatedFileSystem
.normalizePath = function(fileSystemPath
)
61 if (WebInspector
.isWin())
62 return fileSystemPath
.replace(/\\/g, "/");
63 return fileSystemPath;
66 WebInspector.IsolatedFileSystem.prototype = {
78 normalizedPath: function()
80 if (this._normalizedPath)
81 return this._normalizedPath;
82 this._normalizedPath = WebInspector.IsolatedFileSystem.normalizePath(this._path);
83 return this._normalizedPath;
103 * @param {function(?DOMFileSystem)} callback
105 _requestFileSystem: function(callback)
107 this._manager.requestDOMFileSystem(this._path, callback);
111 * @param {string} path
112 * @param {function(string)} fileCallback
113 * @param {function()=} finishedCallback
115 requestFilesRecursive: function(path, fileCallback, finishedCallback)
118 var pendingRequests = 0;
119 this._requestFileSystem(fileSystemLoaded.bind(this));
121 * @param {?DOMFileSystem} fs
122 * @this {WebInspector.IsolatedFileSystem}
124 function fileSystemLoaded(fs)
126 domFileSystem = /** @type {!DOMFileSystem} */ (fs);
127 console.assert(domFileSystem);
129 this._requestEntries(domFileSystem, path, innerCallback.bind(this));
133 * @param {!Array.<!FileEntry>} entries
134 * @this {WebInspector.IsolatedFileSystem}
136 function innerCallback(entries)
138 for (var i = 0; i < entries.length; ++i) {
139 var entry = entries[i];
140 if (!entry.isDirectory) {
141 if (this._manager.excludedFolderManager().isFileExcluded(this._path, entry.fullPath))
143 fileCallback(entry.fullPath.substr(1));
146 if (this._manager.excludedFolderManager().isFileExcluded(this._path, entry.fullPath + "/"))
149 this._requestEntries(domFileSystem, entry.fullPath, innerCallback.bind(this));
152 if (finishedCallback && (--pendingRequests === 0))
158 * @param {string} path
159 * @param {?string} name
160 * @param {function(?string)} callback
162 createFile: function(path, name, callback)
164 this._requestFileSystem(fileSystemLoaded.bind(this));
165 var newFileIndex = 1;
171 * @param {?DOMFileSystem} fs
172 * @this {WebInspector.IsolatedFileSystem}
174 function fileSystemLoaded(fs)
176 var domFileSystem = /** @type {!DOMFileSystem} */ (fs);
177 console.assert(domFileSystem);
178 domFileSystem.root.getDirectory(path, null, dirEntryLoaded.bind(this), errorHandler.bind(this));
182 * @param {!DirectoryEntry} dirEntry
183 * @this {WebInspector.IsolatedFileSystem}
185 function dirEntryLoaded(dirEntry)
187 var nameCandidate = name;
188 if (newFileIndex > 1)
189 nameCandidate += newFileIndex;
191 dirEntry.getFile(nameCandidate, { create: true, exclusive: true }, fileCreated, fileCreationError.bind(this));
193 function fileCreated(entry)
195 callback(entry.fullPath.substr(1));
199 * @this {WebInspector.IsolatedFileSystem}
201 function fileCreationError(error)
203 if (error.code === FileError.INVALID_MODIFICATION_ERR) {
204 dirEntryLoaded.call(this, dirEntry);
208 var errorMessage = WebInspector.IsolatedFileSystem.errorMessage(error);
209 console.error(errorMessage + " when testing
if file exists
'" + (this._path + "/" + path + "/" + nameCandidate) + "'");
215 * @this {WebInspector.IsolatedFileSystem}
217 function errorHandler(error)
219 var errorMessage = WebInspector.IsolatedFileSystem.errorMessage(error);
220 var filePath = this._path + "/" + path;
222 filePath += "/" + nameCandidate;
223 console.error(errorMessage + " when getting content
for file
'" + (filePath) + "'");
229 * @param {string} path
231 deleteFile: function(path)
233 this._requestFileSystem(fileSystemLoaded.bind(this));
236 * @param {?DOMFileSystem} fs
237 * @this {WebInspector.IsolatedFileSystem}
239 function fileSystemLoaded(fs)
241 var domFileSystem = /** @type {!DOMFileSystem} */ (fs);
242 console.assert(domFileSystem);
243 domFileSystem.root.getFile(path, null, fileEntryLoaded.bind(this), errorHandler.bind(this));
247 * @param {!FileEntry} fileEntry
248 * @this {WebInspector.IsolatedFileSystem}
250 function fileEntryLoaded(fileEntry)
252 fileEntry.remove(fileEntryRemoved, errorHandler.bind(this));
255 function fileEntryRemoved()
260 * @param {!FileError} error
261 * @this {WebInspector.IsolatedFileSystem}
263 function errorHandler(error)
265 var errorMessage = WebInspector.IsolatedFileSystem.errorMessage(error);
266 console.error(errorMessage + " when deleting file
'" + (this._path + "/" + path) + "'");
271 * @param {string} path
272 * @param {function(?Date, ?number)} callback
274 requestMetadata: function(path, callback)
276 this._requestFileSystem(fileSystemLoaded);
279 * @param {?DOMFileSystem} fs
281 function fileSystemLoaded(fs)
283 var domFileSystem = /** @type {!DOMFileSystem} */ (fs);
284 console.assert(domFileSystem);
285 domFileSystem.root.getFile(path, null, fileEntryLoaded, errorHandler);
289 * @param {!FileEntry} entry
291 function fileEntryLoaded(entry)
293 entry.getMetadata(successHandler, errorHandler);
297 * @param {!Metadata} metadata
299 function successHandler(metadata)
301 callback(metadata.modificationTime, metadata.size);
305 * @param {!FileError} error
307 function errorHandler(error)
309 callback(null, null);
314 * @param {string} path
315 * @param {function(?string)} callback
317 requestFileContent: function(path, callback)
319 this._requestFileSystem(fileSystemLoaded.bind(this));
322 * @param {?DOMFileSystem} fs
323 * @this {WebInspector.IsolatedFileSystem}
325 function fileSystemLoaded(fs)
327 var domFileSystem = /** @type {!DOMFileSystem} */ (fs);
328 console.assert(domFileSystem);
329 domFileSystem.root.getFile(path, null, fileEntryLoaded.bind(this), errorHandler.bind(this));
333 * @param {!FileEntry} entry
334 * @this {WebInspector.IsolatedFileSystem}
336 function fileEntryLoaded(entry)
338 entry.file(fileLoaded, errorHandler.bind(this));
342 * @param {!Blob} file
344 function fileLoaded(file)
346 var reader = new FileReader();
347 reader.onloadend = readerLoadEnd;
348 reader.readAsText(file);
352 * @this {!FileReader}
354 function readerLoadEnd()
356 /** @type {?string} */
359 string = /** @type {string} */ (this.result);
361 console.error("Can
't read file: " + path + ": " + e);
367 * @this {WebInspector.IsolatedFileSystem}
369 function errorHandler(error)
371 if (error.code === FileError.NOT_FOUND_ERR) {
376 var errorMessage = WebInspector.IsolatedFileSystem.errorMessage(error);
377 console.error(errorMessage + " when getting content for file '" + (this._path + "/" + path) + "'");
383 * @param {string} path
384 * @param {string} content
385 * @param {function()} callback
387 setFileContent: function(path, content, callback)
389 this._requestFileSystem(fileSystemLoaded.bind(this));
390 WebInspector.userMetrics.FileSavedInWorkspace.record();
393 * @param {?DOMFileSystem} fs
394 * @this {WebInspector.IsolatedFileSystem}
396 function fileSystemLoaded(fs)
398 var domFileSystem = /** @type {!DOMFileSystem} */ (fs);
399 console.assert(domFileSystem);
400 domFileSystem.root.getFile(path, { create: true }, fileEntryLoaded.bind(this), errorHandler.bind(this));
404 * @param {!FileEntry} entry
405 * @this {WebInspector.IsolatedFileSystem}
407 function fileEntryLoaded(entry)
409 entry.createWriter(fileWriterCreated.bind(this), errorHandler.bind(this));
413 * @param {!FileWriter} fileWriter
414 * @this {WebInspector.IsolatedFileSystem}
416 function fileWriterCreated(fileWriter)
418 fileWriter.onerror = errorHandler.bind(this);
419 fileWriter.onwriteend = fileWritten;
420 var blob = new Blob([content], { type: "text/plain" });
421 fileWriter.write(blob);
423 function fileWritten()
425 fileWriter.onwriteend = writerEnd;
426 fileWriter.truncate(blob.size);
436 * @this {WebInspector.IsolatedFileSystem}
438 function errorHandler(error)
440 var errorMessage = WebInspector.IsolatedFileSystem.errorMessage(error);
441 console.error(errorMessage + " when setting content for file '" + (this._path + "/" + path) + "'");
447 * @param {string} path
448 * @param {string} newName
449 * @param {function(boolean, string=)} callback
451 renameFile: function(path, newName, callback)
453 newName = newName ? newName.trim() : newName;
454 if (!newName || newName.indexOf("/") !== -1) {
460 this._requestFileSystem(fileSystemLoaded.bind(this));
463 * @param {?DOMFileSystem} fs
464 * @this {WebInspector.IsolatedFileSystem}
466 function fileSystemLoaded(fs)
468 var domFileSystem = /** @type {!DOMFileSystem} */ (fs);
469 console.assert(domFileSystem);
470 domFileSystem.root.getFile(path, null, fileEntryLoaded.bind(this), errorHandler.bind(this));
474 * @param {!FileEntry} entry
475 * @this {WebInspector.IsolatedFileSystem}
477 function fileEntryLoaded(entry)
479 if (entry.name === newName) {
485 fileEntry.getParent(dirEntryLoaded.bind(this), errorHandler.bind(this));
489 * @param {!Entry} entry
490 * @this {WebInspector.IsolatedFileSystem}
492 function dirEntryLoaded(entry)
495 dirEntry.getFile(newName, null, newFileEntryLoaded, newFileEntryLoadErrorHandler.bind(this));
499 * @param {!FileEntry} entry
501 function newFileEntryLoaded(entry)
507 * @this {WebInspector.IsolatedFileSystem}
509 function newFileEntryLoadErrorHandler(error)
511 if (error.code !== FileError.NOT_FOUND_ERR) {
515 fileEntry.moveTo(dirEntry, newName, fileRenamed, errorHandler.bind(this));
519 * @param {!FileEntry} entry
521 function fileRenamed(entry)
523 callback(true, entry.name);
527 * @this {WebInspector.IsolatedFileSystem}
529 function errorHandler(error)
531 var errorMessage = WebInspector.IsolatedFileSystem.errorMessage(error);
532 console.error(errorMessage + " when renaming file '" + (this._path + "/" + path) + "' to '" + newName + "'");
538 * @param {!DirectoryEntry} dirEntry
539 * @param {function(!Array.<!FileEntry>)} callback
541 _readDirectory: function(dirEntry, callback)
543 var dirReader = dirEntry.createReader();
546 function innerCallback(results)
549 callback(entries.sort());
551 entries = entries.concat(toArray(results));
552 dirReader.readEntries(innerCallback, errorHandler);
556 function toArray(list)
558 return Array.prototype.slice.call(list || [], 0);
561 dirReader.readEntries(innerCallback, errorHandler);
563 function errorHandler(error)
565 var errorMessage = WebInspector.IsolatedFileSystem.errorMessage(error);
566 console.error(errorMessage + " when reading directory '" + dirEntry.fullPath + "'");
572 * @param {!DOMFileSystem} domFileSystem
573 * @param {string} path
574 * @param {function(!Array.<!FileEntry>)} callback
576 _requestEntries: function(domFileSystem, path, callback)
578 domFileSystem.root.getDirectory(path, null, innerCallback.bind(this), errorHandler);
581 * @param {!DirectoryEntry} dirEntry
582 * @this {WebInspector.IsolatedFileSystem}
584 function innerCallback(dirEntry)
586 this._readDirectory(dirEntry, callback);
589 function errorHandler(error)
591 var errorMessage = WebInspector.IsolatedFileSystem.errorMessage(error);
592 console.error(errorMessage + " when requesting entry '" + path + "'");