Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

FileSystem Implementation (#247) #2158

Merged
merged 3 commits into from
Dec 6, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/debug/DebugCommandHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ define(function (require, exports, module) {

function _handleSwitchLanguage() {
var stringsPath = FileUtils.getNativeBracketsDirectoryPath() + "/nls";
NativeFileSystem.requestNativeFileSystem(stringsPath, function (dirEntry) {
dirEntry.createReader().readEntries(function (entries) {
NativeFileSystem.requestNativeFileSystem(stringsPath, function (fs) {
fs.root.createReader().readEntries(function (entries) {

var $activeLanguage,
$submit,
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/default/HTMLCodeHints/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,8 @@ define(function (require, exports, module) {
self.cachedHints = {};
self.cachedHints.unfiltered = [];

NativeFileSystem.requestNativeFileSystem(targetDir, function (dirEntry) {
dirEntry.createReader().readEntries(function (entries) {
NativeFileSystem.requestNativeFileSystem(targetDir, function (fs) {
fs.root.createReader().readEntries(function (entries) {

entries.forEach(function (entry) {
if (ProjectManager.shouldShow(entry)) {
Expand Down
75 changes: 54 additions & 21 deletions src/file/NativeFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,13 @@ define(function (require, exports, module) {
/** requestNativeFileSystem
*
* @param {!string} path
* @param {!function(DirectoryEntry)} successCallback
* @param {!function(FileSystem)} successCallback
* @param {!function(number)} errorCallback (TODO #2057: should pass a FileError)
*/
requestNativeFileSystem: function (path, successCallback, errorCallback) {
brackets.fs.stat(path, function (err, data) {
if (!err) {
// FIXME (issue #247): return a NativeFileSystem object
var root = new NativeFileSystem.DirectoryEntry(path);
successCallback(root);
successCallback(new NativeFileSystem.FileSystem(path));
} else if (errorCallback) {
errorCallback(NativeFileSystem._nativeToFileError(err));
}
Expand Down Expand Up @@ -210,11 +208,12 @@ define(function (require, exports, module) {

/** class: Entry
*
* @param {string} name
* @param {string} isFile
* @param {string} fullPath
* @param {boolean} isDirectory
* @param {FileSystem} fs
* @constructor
*/
NativeFileSystem.Entry = function (fullPath, isDirectory) {
NativeFileSystem.Entry = function (fullPath, isDirectory, fs) {
this.isDirectory = isDirectory;
this.isFile = !isDirectory;

Expand All @@ -237,9 +236,7 @@ define(function (require, exports, module) {
}
}

// TODO (issue #241)
// http://www.w3.org/TR/2011/WD-file-system-api-20110419/#widl-Entry-filesystem
this.filesystem = null;
this.filesystem = fs;
};

NativeFileSystem.Entry.prototype.moveTo = function (parent, newName, successCallback, errorCallback) {
Expand Down Expand Up @@ -295,11 +292,12 @@ define(function (require, exports, module) {
* This interface represents a file on a file system.
*
* @param {string} name
* @param {FileSystem} fs
* @constructor
* @extends {Entry}
*/
NativeFileSystem.FileEntry = function (name) {
NativeFileSystem.Entry.call(this, name, false);
NativeFileSystem.FileEntry = function (name, fs) {
NativeFileSystem.Entry.call(this, name, false, fs);
};
NativeFileSystem.FileEntry.prototype = new NativeFileSystem.Entry();

Expand Down Expand Up @@ -528,10 +526,11 @@ define(function (require, exports, module) {
*
* @constructor
* @param {string} name
* @param {FileSystem} fs
* @extends {Entry}
*/
NativeFileSystem.DirectoryEntry = function (name) {
NativeFileSystem.Entry.call(this, name, true);
NativeFileSystem.DirectoryEntry = function (name, fs) {
NativeFileSystem.Entry.call(this, name, true, fs);

// TODO (issue #241): void removeRecursively (VoidCallback successCallback, optional ErrorCallback errorCallback);
};
Expand All @@ -548,7 +547,8 @@ define(function (require, exports, module) {
* @param {!function(FileError|number)} errorCallback (TODO #2057: should consistently pass a FileError)
*/
NativeFileSystem.DirectoryEntry.prototype.getDirectory = function (path, options, successCallback, errorCallback) {
var directoryFullPath = path;
var directoryFullPath = path,
filesystem = this.filesystem;

function isRelativePath(path) {
// If the path contains a colons it must be a full path on Windows (colons are
Expand All @@ -568,7 +568,7 @@ define(function (require, exports, module) {

var createDirectoryEntry = function () {
if (successCallback) {
successCallback(new NativeFileSystem.DirectoryEntry(directoryFullPath));
successCallback(new NativeFileSystem.DirectoryEntry(directoryFullPath, filesystem));
}
};

Expand Down Expand Up @@ -656,7 +656,8 @@ define(function (require, exports, module) {
* @param {!function(FileError|number)} errorCallback (TODO #2057: should consistently pass a FileError)
*/
NativeFileSystem.DirectoryEntry.prototype.getFile = function (path, options, successCallback, errorCallback) {
var fileFullPath = path;
var fileFullPath = path,
filesystem = this.filesystem;

function isRelativePath(path) {
// If the path contains a colons it must be a full path on Windows (colons are
Expand All @@ -676,7 +677,7 @@ define(function (require, exports, module) {

var createFileEntry = function () {
if (successCallback) {
successCallback(new NativeFileSystem.FileEntry(fileFullPath));
successCallback(new NativeFileSystem.FileEntry(fileFullPath, filesystem));
}
};

Expand Down Expand Up @@ -752,7 +753,9 @@ define(function (require, exports, module) {
* @returns {Array.<Entry>}
*/
NativeFileSystem.DirectoryReader.prototype.readEntries = function (successCallback, errorCallback) {
var rootPath = this._directory.fullPath;
var rootPath = this._directory.fullPath,
filesystem = this.filesystem;

brackets.fs.readdir(rootPath, function (err, filelist) {
if (!err) {
var entries = [];
Expand All @@ -767,9 +770,9 @@ define(function (require, exports, module) {
brackets.fs.stat(itemFullPath, function (statErr, statData) {
if (!statErr) {
if (statData.isDirectory()) {
entries[index] = new NativeFileSystem.DirectoryEntry(itemFullPath);
entries[index] = new NativeFileSystem.DirectoryEntry(itemFullPath, filesystem);
} else if (statData.isFile()) {
entries[index] = new NativeFileSystem.FileEntry(itemFullPath);
entries[index] = new NativeFileSystem.FileEntry(itemFullPath, filesystem);
} else {
entries[index] = null; // neither a file nor a dir, so don't include it
}
Expand Down Expand Up @@ -976,6 +979,36 @@ define(function (require, exports, module) {
// TODO (issue #241): implement, readonly
this.lastModifiedDate = null;
};

/**
* Implementation of w3 FileSystem interface
* http://www.w3.org/TR/file-system-api/#the-filesystem-interface
*
* FileSystem represents a file system
*/
NativeFileSystem.FileSystem = function (path) {

/**
* This is the name of the file system and must be unique across the list
* of exposed file systems.
* @const
* @type {string}
*/
Object.defineProperty(this, "name", {
value: path,
writable: false
});

/**
* The root directory of the file system.
* @const
* @type {DirectoryEntry}
*/
Object.defineProperty(this, "root", {
value: new NativeFileSystem.DirectoryEntry(path, this),
writable: false
});
};

/** class: FileError
*
Expand Down
3 changes: 2 additions & 1 deletion src/project/ProjectManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,8 @@ define(function (require, exports, module) {
if (!brackets.inBrowser) {
// Point at a real folder structure on local disk
NativeFileSystem.requestNativeFileSystem(rootPath,
function (rootEntry) {
function (fs) {
var rootEntry = fs.root;
var projectRootChanged = (!_projectRoot || !rootEntry)
|| _projectRoot.fullPath !== rootEntry.fullPath;

Expand Down
4 changes: 2 additions & 2 deletions src/utils/ExtensionLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ define(function (require, exports, module) {
var result = new $.Deferred();

NativeFileSystem.requestNativeFileSystem(directory,
function (rootEntry) {
rootEntry.createReader().readEntries(
function (fs) {
fs.root.createReader().readEntries(
function (entries) {
var i,
extensions = [];
Expand Down
30 changes: 13 additions & 17 deletions test/spec/NativeFileSystem-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ define(function (require, exports, module) {
deferred = new $.Deferred();

function requestNativeFileSystemSuccessCB(nfs) {
var reader = nfs.createReader();
var reader = nfs.root.createReader();

var successCallback = function (e) { entries = e; deferred.resolve(); };
var errorCallback = function () { deferred.reject(); };
Expand Down Expand Up @@ -86,7 +86,7 @@ define(function (require, exports, module) {
deferred = new $.Deferred();

function requestNativeFileSystemSuccessCB(nfs) {
var reader = nfs.createReader();
var reader = nfs.root.createReader();

var successCallback = function (e) { entries = e; deferred.resolve(); };
var errorCallback = function () { deferred.reject(); };
Expand Down Expand Up @@ -190,14 +190,14 @@ define(function (require, exports, module) {
accessedFolder = true;

function recreatePlaceholder(deferred) {
nfs.getFile("placeholder",
nfs.root.getFile("placeholder",
{ create: true, exclusive: true },
function () { placeholderRecreated = true; deferred.resolve(); },
function () { placeholderRecreated = false; deferred.reject(); });
}

function readDirectory() {
var reader = nfs.createReader();
var reader = nfs.root.createReader();
var successCallback = function (e) {
entries = e;
recreatePlaceholder(deferred);
Expand Down Expand Up @@ -254,7 +254,7 @@ define(function (require, exports, module) {
this.after(function () { brackets.fs.stat = oldStat; });

function requestNativeFileSystemSuccessCB(nfs) {
var reader = nfs.createReader();
var reader = nfs.root.createReader();

var successCallback = function (e) { readComplete = true; };
var errorCallback = function (error) { readComplete = true; gotError = true; theError = error; };
Expand Down Expand Up @@ -472,8 +472,7 @@ define(function (require, exports, module) {
writeComplete = true;
};

// FIXME (issue #247): NativeFileSystem.root is missing
this.nfs.getFile("new-zero-length-file.txt", { create: true, exclusive: true }, successCallback, errorCallback);
this.nfs.root.getFile("new-zero-length-file.txt", { create: true, exclusive: true }, successCallback, errorCallback);
});

waitsFor(function () { return writeComplete; }, 1000);
Expand Down Expand Up @@ -526,8 +525,7 @@ define(function (require, exports, module) {
writeComplete = true;
};

// FIXME (issue #247): NativeFileSystem.root is missing
this.nfs.getFile("does-not-exist.txt", { create: false }, successCallback, errorCallback);
this.nfs.root.getFile("does-not-exist.txt", { create: false }, successCallback, errorCallback);
});

waitsFor(function () { return writeComplete; }, 1000);
Expand Down Expand Up @@ -555,8 +553,7 @@ define(function (require, exports, module) {
writeComplete = true;
};

// FIXME (issue #247): NativeFileSystem.root is missing
this.nfs.getFile("file1", { create: true, exclusive: true }, successCallback, errorCallback);
this.nfs.root.getFile("file1", { create: true, exclusive: true }, successCallback, errorCallback);
});

// wait for success or error to return
Expand Down Expand Up @@ -587,8 +584,7 @@ define(function (require, exports, module) {
writeComplete = true;
};

// FIXME (issue #247): NativeFileSystem.root is missing
this.nfs.getFile("dir1", { create: false }, successCallback, errorCallback);
this.nfs.root.getFile("dir1", { create: false }, successCallback, errorCallback);
});

// wait for success or error to return
Expand Down Expand Up @@ -628,7 +624,7 @@ define(function (require, exports, module) {
writeComplete = true;
};

this.nfs.getFile("file1", { create: false }, successCallback, errorCallback);
this.nfs.root.getFile("file1", { create: false }, successCallback, errorCallback);
});

waitsFor(function () { return writeComplete && fileEntry; }, 1000);
Expand Down Expand Up @@ -685,7 +681,7 @@ define(function (require, exports, module) {
writeComplete = true;
};

this.nfs.getFile("file1", { create: false }, successCallback, errorCallback);
this.nfs.root.getFile("file1", { create: false }, successCallback, errorCallback);
});

waitsFor(function () { return writeComplete && fileEntry; }, 1000);
Expand Down Expand Up @@ -726,7 +722,7 @@ define(function (require, exports, module) {

// createWriter() should return an error for files it can't read
runs(function () {
this.nfs.getFile(
this.nfs.root.getFile(
"cant_read_here.txt",
{ create: false },
function (entry) {
Expand Down Expand Up @@ -768,7 +764,7 @@ define(function (require, exports, module) {
writeComplete = true;
};

this.nfs.getFile("cant_write_here.txt", { create: false }, successCallback, errorCallback);
this.nfs.root.getFile("cant_write_here.txt", { create: false }, successCallback, errorCallback);
});

// fileWriter.onerror handler should be invoked for read only files
Expand Down