Merge pull request #148 from humphd/issue147

Expose errors on Filer.Errors, refactor how they get used internally, add docs
This commit is contained in:
Alan K 2014-03-10 15:57:44 -04:00
commit b825772fdd
6 changed files with 117 additions and 106 deletions

View File

@ -205,6 +205,34 @@ For more info see the docs in the [path module](http://nodejs.org/api/path.html)
* `path.sep` * `path.sep`
* `path.delimiter` * `path.delimiter`
####Filer.Errors<a name="Errors"></a>
The error objects used internally by Filer are also exposed via the `Filer.Errors` object. As much as possible
these match their node.js counterparts. See [src/errors.js](https://github.com/js-platform/filer/blob/develop/src/errors.js)
for the complete list. Errors can be used, or compared, like so:
Examples:
```javascript
// Example 1: create an EExists error
var err = new Filer.Errors.EExists();
// Example 2: compare an error to see if it is EInvalid
function callback(err) {
if(err instanceof Filer.Errors.EInvalid){
...
}
// Or compare the error's code
if(err.code === 'EINVALID') {
...
}
}
// Example 3: display the error message
console.log(err.message);
```
###FileSystem Instance Methods<a name="FileSystemMethods"></a> ###FileSystem Instance Methods<a name="FileSystemMethods"></a>
Once a `FileSystem` is created, it has the following methods. NOTE: code examples below assume Once a `FileSystem` is created, it has the following methods. NOTE: code examples below assume

View File

@ -21,7 +21,7 @@ module.exports = function(grunt) {
all: [ all: [
'gruntfile.js', 'gruntfile.js',
'src/constants.js', 'src/constants.js',
'src/error.js', 'src/errors.js',
'src/fs.js', 'src/fs.js',
'src/index.js', 'src/index.js',
'src/shared.js', 'src/shared.js',

178
src/fs.js
View File

@ -16,21 +16,6 @@ define(function(require) {
var hash = require('src/shared').hash; var hash = require('src/shared').hash;
var nop = require('src/shared').nop; var nop = require('src/shared').nop;
var EExists = require('src/error').EExists;
var EIsDirectory = require('src/error').EIsDirectory;
var ENoEntry = require('src/error').ENoEntry;
var EBusy = require('src/error').EBusy;
var ENotEmpty = require('src/error').ENotEmpty;
var ENotDirectory = require('src/error').ENotDirectory;
var EBadFileDescriptor = require('src/error').EBadFileDescriptor;
var ENotImplemented = require('src/error').ENotImplemented;
var ENotMounted = require('src/error').ENotMounted;
var EInvalid = require('src/error').EInvalid;
var EIO = require('src/error').EIO;
var ELoop = require('src/error').ELoop;
var EFileSystemError = require('src/error').EFileSystemError;
var ENoAttr = require('src/error').ENoAttr;
var FILE_SYSTEM_NAME = require('src/constants').FILE_SYSTEM_NAME; var FILE_SYSTEM_NAME = require('src/constants').FILE_SYSTEM_NAME;
var FS_FORMAT = require('src/constants').FS_FORMAT; var FS_FORMAT = require('src/constants').FS_FORMAT;
var MODE_FILE = require('src/constants').MODE_FILE; var MODE_FILE = require('src/constants').MODE_FILE;
@ -60,6 +45,7 @@ define(function(require) {
var Shell = require('src/shell'); var Shell = require('src/shell');
var Intercom = require('intercom'); var Intercom = require('intercom');
var FSWatcher = require('src/fswatcher'); var FSWatcher = require('src/fswatcher');
var Errors = require('src/errors');
/* /*
* DirectoryEntry * DirectoryEntry
@ -217,7 +203,7 @@ define(function(require) {
function find_node(context, path, callback) { function find_node(context, path, callback) {
path = normalize(path); path = normalize(path);
if(!path) { if(!path) {
return callback(new ENoEntry('path is an empty string')); return callback(new Errors.ENoEntry('path is an empty string'));
} }
var name = basename(path); var name = basename(path);
var parentPath = dirname(path); var parentPath = dirname(path);
@ -227,7 +213,7 @@ define(function(require) {
if(error) { if(error) {
callback(error); callback(error);
} else if(!superNode || superNode.mode !== MODE_META || !superNode.rnode) { } else if(!superNode || superNode.mode !== MODE_META || !superNode.rnode) {
callback(new EFileSystemError('missing super node')); callback(new Errors.EFileSystemError());
} else { } else {
context.get(superNode.rnode, check_root_directory_node); context.get(superNode.rnode, check_root_directory_node);
} }
@ -237,7 +223,7 @@ define(function(require) {
if(error) { if(error) {
callback(error); callback(error);
} else if(!rootDirectoryNode) { } else if(!rootDirectoryNode) {
callback(new ENoEntry('path does not exist')); callback(new Errors.ENoEntry());
} else { } else {
callback(null, rootDirectoryNode); callback(null, rootDirectoryNode);
} }
@ -249,7 +235,7 @@ define(function(require) {
if(error) { if(error) {
callback(error); callback(error);
} else if(parentDirectoryNode.mode !== MODE_DIRECTORY || !parentDirectoryNode.data) { } else if(parentDirectoryNode.mode !== MODE_DIRECTORY || !parentDirectoryNode.data) {
callback(new ENotDirectory('a component of the path prefix is not a directory')); callback(new Errors.ENotDirectory('a component of the path prefix is not a directory'));
} else { } else {
context.get(parentDirectoryNode.data, get_node_from_parent_directory_data); context.get(parentDirectoryNode.data, get_node_from_parent_directory_data);
} }
@ -262,7 +248,7 @@ define(function(require) {
callback(error); callback(error);
} else { } else {
if(!_(parentDirectoryData).has(name)) { if(!_(parentDirectoryData).has(name)) {
callback(new ENoEntry('path does not exist')); callback(new Errors.ENoEntry());
} else { } else {
var nodeId = parentDirectoryData[name].id; var nodeId = parentDirectoryData[name].id;
context.get(nodeId, is_symbolic_link); context.get(nodeId, is_symbolic_link);
@ -277,7 +263,7 @@ define(function(require) {
if(node.mode == MODE_SYMBOLIC_LINK) { if(node.mode == MODE_SYMBOLIC_LINK) {
followedCount++; followedCount++;
if(followedCount > SYMLOOP_MAX){ if(followedCount > SYMLOOP_MAX){
callback(new ELoop('too many symbolic links were encountered')); callback(new Errors.ELoop());
} else { } else {
follow_symbolic_link(node.data); follow_symbolic_link(node.data);
} }
@ -328,10 +314,10 @@ define(function(require) {
callback(error); callback(error);
} }
else if (flag === XATTR_CREATE && node.xattrs.hasOwnProperty(name)) { else if (flag === XATTR_CREATE && node.xattrs.hasOwnProperty(name)) {
callback(new EExists('attribute already exists')); callback(new Errors.EExists('attribute already exists'));
} }
else if (flag === XATTR_REPLACE && !node.xattrs.hasOwnProperty(name)) { else if (flag === XATTR_REPLACE && !node.xattrs.hasOwnProperty(name)) {
callback(new ENoAttr('attribute does not exist')); callback(new Errors.ENoAttr());
} }
else { else {
node.xattrs[name] = value; node.xattrs[name] = value;
@ -348,7 +334,7 @@ define(function(require) {
context.get(path_or_fd.id, set_xattr); context.get(path_or_fd.id, set_xattr);
} }
else { else {
callback(new EInvalid('path or file descriptor of wrong type')); callback(new Errors.EInvalid('path or file descriptor of wrong type'));
} }
} }
@ -364,8 +350,8 @@ define(function(require) {
function write_super_node(error, existingNode) { function write_super_node(error, existingNode) {
if(!error && existingNode) { if(!error && existingNode) {
callback(new EExists()); callback(new Errors.EExists());
} else if(error && !error instanceof ENoEntry) { } else if(error && !error instanceof Errors.ENoEntry) {
callback(error); callback(error);
} else { } else {
superNode = new SuperNode(); superNode = new SuperNode();
@ -411,8 +397,8 @@ define(function(require) {
function check_if_directory_exists(error, result) { function check_if_directory_exists(error, result) {
if(!error && result) { if(!error && result) {
callback(new EExists()); callback(new Errors.EExists());
} else if(error && !error instanceof ENoEntry) { } else if(error && !error instanceof Errors.ENoEntry) {
callback(error); callback(error);
} else { } else {
find_node(context, parentPath, read_parent_directory_data); find_node(context, parentPath, read_parent_directory_data);
@ -496,9 +482,9 @@ define(function(require) {
if(error) { if(error) {
callback(error); callback(error);
} else if(ROOT_DIRECTORY_NAME == name) { } else if(ROOT_DIRECTORY_NAME == name) {
callback(new EBusy()); callback(new Errors.EBusy());
} else if(!_(result).has(name)) { } else if(!_(result).has(name)) {
callback(new ENoEntry()); callback(new Errors.ENoEntry());
} else { } else {
parentDirectoryData = result; parentDirectoryData = result;
directoryNode = parentDirectoryData[name].id; directoryNode = parentDirectoryData[name].id;
@ -510,7 +496,7 @@ define(function(require) {
if(error) { if(error) {
callback(error); callback(error);
} else if(result.mode != MODE_DIRECTORY) { } else if(result.mode != MODE_DIRECTORY) {
callback(new ENotDirectory()); callback(new Errors.ENotDirectory());
} else { } else {
directoryNode = result; directoryNode = result;
context.get(directoryNode.data, check_if_directory_is_empty); context.get(directoryNode.data, check_if_directory_is_empty);
@ -523,7 +509,7 @@ define(function(require) {
} else { } else {
directoryData = result; directoryData = result;
if(_(directoryData).size() > 0) { if(_(directoryData).size() > 0) {
callback(new ENotEmpty()); callback(new Errors.ENotEmpty());
} else { } else {
remove_directory_entry_from_parent_directory_node(); remove_directory_entry_from_parent_directory_node();
} }
@ -578,7 +564,7 @@ define(function(require) {
if(ROOT_DIRECTORY_NAME == name) { if(ROOT_DIRECTORY_NAME == name) {
if(_(flags).contains(O_WRITE)) { if(_(flags).contains(O_WRITE)) {
callback(new EIsDirectory('the named file is a directory and O_WRITE is set')); callback(new Errors.EIsDirectory('the named file is a directory and O_WRITE is set'));
} else { } else {
find_node(context, path, set_file_node); find_node(context, path, set_file_node);
} }
@ -602,18 +588,18 @@ define(function(require) {
directoryData = result; directoryData = result;
if(_(directoryData).has(name)) { if(_(directoryData).has(name)) {
if(_(flags).contains(O_EXCLUSIVE)) { if(_(flags).contains(O_EXCLUSIVE)) {
callback(new ENoEntry('O_CREATE and O_EXCLUSIVE are set, and the named file exists')); callback(new Errors.ENoEntry('O_CREATE and O_EXCLUSIVE are set, and the named file exists'));
} else { } else {
directoryEntry = directoryData[name]; directoryEntry = directoryData[name];
if(directoryEntry.type == MODE_DIRECTORY && _(flags).contains(O_WRITE)) { if(directoryEntry.type == MODE_DIRECTORY && _(flags).contains(O_WRITE)) {
callback(new EIsDirectory('the named file is a directory and O_WRITE is set')); callback(new Errors.EIsDirectory('the named file is a directory and O_WRITE is set'));
} else { } else {
context.get(directoryEntry.id, check_if_symbolic_link); context.get(directoryEntry.id, check_if_symbolic_link);
} }
} }
} else { } else {
if(!_(flags).contains(O_CREATE)) { if(!_(flags).contains(O_CREATE)) {
callback(new ENoEntry('O_CREATE is not set and the named file does not exist')); callback(new Errors.ENoEntry('O_CREATE is not set and the named file does not exist'));
} else { } else {
write_file_node(); write_file_node();
} }
@ -629,7 +615,7 @@ define(function(require) {
if(node.mode == MODE_SYMBOLIC_LINK) { if(node.mode == MODE_SYMBOLIC_LINK) {
followedCount++; followedCount++;
if(followedCount > SYMLOOP_MAX){ if(followedCount > SYMLOOP_MAX){
callback(new ELoop('too many symbolic links were encountered')); callback(new Errors.ELoop('too many symbolic links were encountered'));
} else { } else {
follow_symbolic_link(node.data); follow_symbolic_link(node.data);
} }
@ -645,7 +631,7 @@ define(function(require) {
name = basename(data); name = basename(data);
if(ROOT_DIRECTORY_NAME == name) { if(ROOT_DIRECTORY_NAME == name) {
if(_(flags).contains(O_WRITE)) { if(_(flags).contains(O_WRITE)) {
callback(new EIsDirectory('the named file is a directory and O_WRITE is set')); callback(new Errors.EIsDirectory('the named file is a directory and O_WRITE is set'));
} else { } else {
find_node(context, path, set_file_node); find_node(context, path, set_file_node);
} }
@ -913,7 +899,7 @@ define(function(require) {
} else { } else {
directoryData = result; directoryData = result;
if(!_(directoryData).has(name)) { if(!_(directoryData).has(name)) {
callback(new ENoEntry('a component of the path does not name an existing file')); callback(new Errors.ENoEntry('a component of the path does not name an existing file'));
} else { } else {
context.get(directoryData[name].id, check_file); context.get(directoryData[name].id, check_file);
} }
@ -976,7 +962,7 @@ define(function(require) {
} else { } else {
newDirectoryData = result; newDirectoryData = result;
if(_(newDirectoryData).has(newname)) { if(_(newDirectoryData).has(newname)) {
callback(new EExists('newpath resolves to an existing file')); callback(new Errors.EExists('newpath resolves to an existing file'));
} else { } else {
newDirectoryData[newname] = oldDirectoryData[oldname]; newDirectoryData[newname] = oldDirectoryData[oldname];
context.put(newDirectoryNode.data, newDirectoryData, read_directory_entry); context.put(newDirectoryNode.data, newDirectoryData, read_directory_entry);
@ -999,7 +985,7 @@ define(function(require) {
} else { } else {
oldDirectoryData = result; oldDirectoryData = result;
if(!_(oldDirectoryData).has(oldname)) { if(!_(oldDirectoryData).has(oldname)) {
callback(new ENoEntry('a component of either path prefix does not exist')); callback(new Errors.ENoEntry('a component of either path prefix does not exist'));
} else { } else {
find_node(context, newParentPath, read_new_directory_data); find_node(context, newParentPath, read_new_directory_data);
} }
@ -1069,7 +1055,7 @@ define(function(require) {
} else { } else {
directoryData = result; directoryData = result;
if(!_(directoryData).has(name)) { if(!_(directoryData).has(name)) {
callback(new ENoEntry('a component of the path does not name an existing file')); callback(new Errors.ENoEntry('a component of the path does not name an existing file'));
} else { } else {
context.get(directoryData[name].id, update_file_node); context.get(directoryData[name].id, update_file_node);
} }
@ -1127,7 +1113,7 @@ define(function(require) {
var fileNode; var fileNode;
if(ROOT_DIRECTORY_NAME == name) { if(ROOT_DIRECTORY_NAME == name) {
callback(new EExists('the destination path already exists')); callback(new Errors.EExists('the destination path already exists'));
} else { } else {
find_node(context, parentPath, read_directory_data); find_node(context, parentPath, read_directory_data);
} }
@ -1147,7 +1133,7 @@ define(function(require) {
} else { } else {
directoryData = result; directoryData = result;
if(_(directoryData).has(name)) { if(_(directoryData).has(name)) {
callback(new EExists('the destination path already exists')); callback(new Errors.EExists('the destination path already exists'));
} else { } else {
write_file_node(); write_file_node();
} }
@ -1206,7 +1192,7 @@ define(function(require) {
} else { } else {
directoryData = result; directoryData = result;
if(!_(directoryData).has(name)) { if(!_(directoryData).has(name)) {
callback(new ENoEntry('a component of the path does not name an existing file')); callback(new Errors.ENoEntry('a component of the path does not name an existing file'));
} else { } else {
context.get(directoryData[name].id, check_if_symbolic); context.get(directoryData[name].id, check_if_symbolic);
} }
@ -1218,7 +1204,7 @@ define(function(require) {
callback(error); callback(error);
} else { } else {
if(result.mode != MODE_SYMBOLIC_LINK) { if(result.mode != MODE_SYMBOLIC_LINK) {
callback(new EInvalid("path not a symbolic link")); callback(new Errors.EInvalid("path not a symbolic link"));
} else { } else {
callback(null, result.data); callback(null, result.data);
} }
@ -1235,7 +1221,7 @@ define(function(require) {
if (error) { if (error) {
callback(error); callback(error);
} else if(node.mode == MODE_DIRECTORY ) { } else if(node.mode == MODE_DIRECTORY ) {
callback(new EIsDirectory('the named file is a directory')); callback(new Errors.EIsDirectory('the named file is a directory'));
} else{ } else{
fileNode = node; fileNode = node;
context.get(fileNode.data, truncate_file_data); context.get(fileNode.data, truncate_file_data);
@ -1274,7 +1260,7 @@ define(function(require) {
} }
if(length < 0) { if(length < 0) {
callback(new EInvalid('length cannot be negative')); callback(new Errors.EInvalid('length cannot be negative'));
} else { } else {
find_node(context, path, read_file_data); find_node(context, path, read_file_data);
} }
@ -1287,7 +1273,7 @@ define(function(require) {
if (error) { if (error) {
callback(error); callback(error);
} else if(node.mode == MODE_DIRECTORY ) { } else if(node.mode == MODE_DIRECTORY ) {
callback(new EIsDirectory('the named file is a directory')); callback(new Errors.EIsDirectory('the named file is a directory'));
} else{ } else{
fileNode = node; fileNode = node;
context.get(fileNode.data, truncate_file_data); context.get(fileNode.data, truncate_file_data);
@ -1325,7 +1311,7 @@ define(function(require) {
} }
if(length < 0) { if(length < 0) {
callback(new EInvalid('length cannot be negative')); callback(new Errors.EInvalid('length cannot be negative'));
} else { } else {
context.get(ofd.id, read_file_data); context.get(ofd.id, read_file_data);
} }
@ -1343,10 +1329,10 @@ define(function(require) {
} }
if (typeof atime != 'number' || typeof mtime != 'number') { if (typeof atime != 'number' || typeof mtime != 'number') {
callback(new EInvalid('atime and mtime must be number')); callback(new Errors.EInvalid('atime and mtime must be number'));
} }
else if (atime < 0 || mtime < 0) { else if (atime < 0 || mtime < 0) {
callback(new EInvalid('atime and mtime must be positive integers')); callback(new Errors.EInvalid('atime and mtime must be positive integers'));
} }
else { else {
find_node(context, path, update_times); find_node(context, path, update_times);
@ -1364,10 +1350,10 @@ define(function(require) {
} }
if (typeof atime != 'number' || typeof mtime != 'number') { if (typeof atime != 'number' || typeof mtime != 'number') {
callback(new EInvalid('atime and mtime must be a number')); callback(new Errors.EInvalid('atime and mtime must be a number'));
} }
else if (atime < 0 || mtime < 0) { else if (atime < 0 || mtime < 0) {
callback(new EInvalid('atime and mtime must be positive integers')); callback(new Errors.EInvalid('atime and mtime must be positive integers'));
} }
else { else {
context.get(ofd.id, update_times); context.get(ofd.id, update_times);
@ -1378,14 +1364,14 @@ define(function(require) {
path = normalize(path); path = normalize(path);
if (typeof name != 'string') { if (typeof name != 'string') {
callback(new EInvalid('attribute name must be a string')); callback(new Errors.EInvalid('attribute name must be a string'));
} }
else if (!name) { else if (!name) {
callback(new EInvalid('attribute name cannot be an empty string')); callback(new Errors.EInvalid('attribute name cannot be an empty string'));
} }
else if (flag !== null && else if (flag !== null &&
flag !== XATTR_CREATE && flag !== XATTR_REPLACE) { flag !== XATTR_CREATE && flag !== XATTR_REPLACE) {
callback(new EInvalid('invalid flag, must be null, XATTR_CREATE or XATTR_REPLACE')); callback(new Errors.EInvalid('invalid flag, must be null, XATTR_CREATE or XATTR_REPLACE'));
} }
else { else {
set_extended_attribute(context, path, name, value, flag, callback); set_extended_attribute(context, path, name, value, flag, callback);
@ -1395,14 +1381,14 @@ define(function(require) {
function fsetxattr_file (context, ofd, name, value, flag, callback) { function fsetxattr_file (context, ofd, name, value, flag, callback) {
if (typeof name != 'string') { if (typeof name != 'string') {
callback(new EInvalid('attribute name must be a string')); callback(new Errors.EInvalid('attribute name must be a string'));
} }
else if (!name) { else if (!name) {
callback(new EInvalid('attribute name cannot be an empty string')); callback(new Errors.EInvalid('attribute name cannot be an empty string'));
} }
else if (flag !== null && else if (flag !== null &&
flag !== XATTR_CREATE && flag !== XATTR_REPLACE) { flag !== XATTR_CREATE && flag !== XATTR_REPLACE) {
callback(new EInvalid('invalid flag, must be null, XATTR_CREATE or XATTR_REPLACE')); callback(new Errors.EInvalid('invalid flag, must be null, XATTR_CREATE or XATTR_REPLACE'));
} }
else { else {
set_extended_attribute(context, ofd, name, value, flag, callback); set_extended_attribute(context, ofd, name, value, flag, callback);
@ -1419,7 +1405,7 @@ define(function(require) {
callback (error); callback (error);
} }
else if (!node.xattrs.hasOwnProperty(name)) { else if (!node.xattrs.hasOwnProperty(name)) {
callback(new ENoAttr('attribute does not exist')); callback(new Errors.ENoAttr('attribute does not exist'));
} }
else { else {
callback(null, node.xattrs[name]); callback(null, node.xattrs[name]);
@ -1427,10 +1413,10 @@ define(function(require) {
} }
if (typeof name != 'string') { if (typeof name != 'string') {
callback(new EInvalid('attribute name must be a string')); callback(new Errors.EInvalid('attribute name must be a string'));
} }
else if (!name) { else if (!name) {
callback(new EInvalid('attribute name cannot be an empty string')); callback(new Errors.EInvalid('attribute name cannot be an empty string'));
} }
else { else {
find_node(context, path, get_xattr); find_node(context, path, get_xattr);
@ -1446,7 +1432,7 @@ define(function(require) {
callback(error); callback(error);
} }
else if (!node.xattrs.hasOwnProperty(name)) { else if (!node.xattrs.hasOwnProperty(name)) {
callback(new ENoAttr('attribute does not exist')); callback(new Errors.ENoAttr('attribute does not exist'));
} }
else { else {
callback(null, node.xattrs[name]); callback(null, node.xattrs[name]);
@ -1454,10 +1440,10 @@ define(function(require) {
} }
if (typeof name != 'string') { if (typeof name != 'string') {
callback(new EInvalid('attribute name must be a string')); callback(new Errors.EInvalid('attribute name must be a string'));
} }
else if (!name) { else if (!name) {
callback(new EInvalid('attribute name cannot be an empty string')); callback(new Errors.EInvalid('attribute name cannot be an empty string'));
} }
else { else {
context.get(ofd.id, get_xattr); context.get(ofd.id, get_xattr);
@ -1482,7 +1468,7 @@ define(function(require) {
callback(error); callback(error);
} }
else if (!xattr.hasOwnProperty(name)) { else if (!xattr.hasOwnProperty(name)) {
callback(new ENoAttr('attribute does not exist')); callback(new Errors.ENoAttr('attribute does not exist'));
} }
else { else {
delete node.xattrs[name]; delete node.xattrs[name];
@ -1491,10 +1477,10 @@ define(function(require) {
} }
if (typeof name != 'string') { if (typeof name != 'string') {
callback(new EInvalid('attribute name must be a string')); callback(new Errors.EInvalid('attribute name must be a string'));
} }
else if (!name) { else if (!name) {
callback(new EInvalid('attribute name cannot be an empty string')); callback(new Errors.EInvalid('attribute name cannot be an empty string'));
} }
else { else {
find_node(context, path, remove_xattr); find_node(context, path, remove_xattr);
@ -1516,7 +1502,7 @@ define(function(require) {
callback(error); callback(error);
} }
else if (!node.xattrs.hasOwnProperty(name)) { else if (!node.xattrs.hasOwnProperty(name)) {
callback(new ENoAttr('attribute does not exist')); callback(new Errors.ENoAttr('attribute does not exist'));
} }
else { else {
delete node.xattrs[name]; delete node.xattrs[name];
@ -1525,10 +1511,10 @@ define(function(require) {
} }
if (typeof name != 'string') { if (typeof name != 'string') {
callback(new EInvalid('attribute name must be a string')); callback(new Errors.EInvalid('attribute name must be a string'));
} }
else if (!name) { else if (!name) {
callback(new EInvalid('attribute name cannot be an empty string')); callback(new Errors.EInvalid('attribute name cannot be an empty string'));
} }
else { else {
context.get(ofd.id, remove_xattr); context.get(ofd.id, remove_xattr);
@ -1777,7 +1763,7 @@ define(function(require) {
flags = validate_flags(flags); flags = validate_flags(flags);
if(!flags) { if(!flags) {
callback(new EInvalid('flags is not valid')); callback(new Errors.EInvalid('flags is not valid'));
} }
open_file(context, path, flags, check_result); open_file(context, path, flags, check_result);
@ -1785,7 +1771,7 @@ define(function(require) {
function _close(fs, fd, callback) { function _close(fs, fd, callback) {
if(!_(fs.openFiles).has(fd)) { if(!_(fs.openFiles).has(fd)) {
callback(new EBadFileDescriptor('invalid file descriptor')); callback(new Errors.EBadFileDescriptor('invalid file descriptor'));
} else { } else {
fs.releaseDescriptor(fd); fs.releaseDescriptor(fd);
callback(null); callback(null);
@ -1898,9 +1884,9 @@ define(function(require) {
var ofd = fs.openFiles[fd]; var ofd = fs.openFiles[fd];
if(!ofd) { if(!ofd) {
callback(new EBadFileDescriptor('invalid file descriptor')); callback(new Errors.EBadFileDescriptor('invalid file descriptor'));
} else if(!_(ofd.flags).contains(O_READ)) { } else if(!_(ofd.flags).contains(O_READ)) {
callback(new EBadFileDescriptor('descriptor does not permit reading')); callback(new Errors.EBadFileDescriptor('descriptor does not permit reading'));
} else { } else {
read_data(context, ofd, buffer, offset, length, position, check_result); read_data(context, ofd, buffer, offset, length, position, check_result);
} }
@ -1913,7 +1899,7 @@ define(function(require) {
var flags = validate_flags(options.flag || 'r'); var flags = validate_flags(options.flag || 'r');
if(!flags) { if(!flags) {
callback(new EInvalid('flags is not valid')); callback(new Errors.EInvalid('flags is not valid'));
} }
open_file(context, path, flags, function(err, fileNode) { open_file(context, path, flags, function(err, fileNode) {
@ -1965,11 +1951,11 @@ define(function(require) {
var ofd = fs.openFiles[fd]; var ofd = fs.openFiles[fd];
if(!ofd) { if(!ofd) {
callback(new EBadFileDescriptor('invalid file descriptor')); callback(new Errors.EBadFileDescriptor('invalid file descriptor'));
} else if(!_(ofd.flags).contains(O_WRITE)) { } else if(!_(ofd.flags).contains(O_WRITE)) {
callback(new EBadFileDescriptor('descriptor does not permit writing')); callback(new Errors.EBadFileDescriptor('descriptor does not permit writing'));
} else if(buffer.length - offset < length) { } else if(buffer.length - offset < length) {
callback(new EIO('intput buffer is too small')); callback(new Errors.EIO('intput buffer is too small'));
} else { } else {
write_data(context, ofd, buffer, offset, length, position, check_result); write_data(context, ofd, buffer, offset, length, position, check_result);
} }
@ -1982,7 +1968,7 @@ define(function(require) {
var flags = validate_flags(options.flag || 'w'); var flags = validate_flags(options.flag || 'w');
if(!flags) { if(!flags) {
callback(new EInvalid('flags is not valid')); callback(new Errors.EInvalid('flags is not valid'));
} }
data = data || ''; data = data || '';
@ -2017,7 +2003,7 @@ define(function(require) {
var flags = validate_flags(options.flag || 'a'); var flags = validate_flags(options.flag || 'a');
if(!flags) { if(!flags) {
callback(new EInvalid('flags is not valid')); callback(new Errors.EInvalid('flags is not valid'));
} }
data = data || ''; data = data || '';
@ -2081,7 +2067,7 @@ define(function(require) {
var ofd = fs.openFiles[fd]; var ofd = fs.openFiles[fd];
if (!ofd) { if (!ofd) {
callback(new EBadFileDescriptor('invalid file descriptor')); callback(new Errors.EBadFileDescriptor('invalid file descriptor'));
} }
else { else {
fgetxattr_file(context, ofd, name, get_result); fgetxattr_file(context, ofd, name, get_result);
@ -2116,10 +2102,10 @@ define(function(require) {
var ofd = fs.openFiles[fd]; var ofd = fs.openFiles[fd];
if (!ofd) { if (!ofd) {
callback(new EBadFileDescriptor('invalid file descriptor')); callback(new Errors.EBadFileDescriptor('invalid file descriptor'));
} }
else if (!_(ofd.flags).contains(O_WRITE)) { else if (!_(ofd.flags).contains(O_WRITE)) {
callback(new EBadFileDescriptor('descriptor does not permit writing')); callback(new Errors.EBadFileDescriptor('descriptor does not permit writing'));
} }
else { else {
fsetxattr_file(context, ofd, name, value, flag, check_result); fsetxattr_file(context, ofd, name, value, flag, check_result);
@ -2155,10 +2141,10 @@ define(function(require) {
var ofd = fs.openFiles[fd]; var ofd = fs.openFiles[fd];
if (!ofd) { if (!ofd) {
callback(new EBadFileDescriptor('invalid file descriptor')); callback(new Errors.EBadFileDescriptor('invalid file descriptor'));
} }
else if (!_(ofd.flags).contains(O_WRITE)) { else if (!_(ofd.flags).contains(O_WRITE)) {
callback(new EBadFileDescriptor('descriptor does not permit writing')); callback(new Errors.EBadFileDescriptor('descriptor does not permit writing'));
} }
else { else {
fremovexattr_file(context, ofd, name, remove_xattr); fremovexattr_file(context, ofd, name, remove_xattr);
@ -2179,7 +2165,7 @@ define(function(require) {
callback(error); callback(error);
} else { } else {
if(stats.size + offset < 0) { if(stats.size + offset < 0) {
callback(new EInvalid('resulting file offset would be negative')); callback(new Errors.EInvalid('resulting file offset would be negative'));
} else { } else {
ofd.position = stats.size + offset; ofd.position = stats.size + offset;
callback(null, ofd.position); callback(null, ofd.position);
@ -2190,19 +2176,19 @@ define(function(require) {
var ofd = fs.openFiles[fd]; var ofd = fs.openFiles[fd];
if(!ofd) { if(!ofd) {
callback(new EBadFileDescriptor('invalid file descriptor')); callback(new Errors.EBadFileDescriptor('invalid file descriptor'));
} }
if('SET' === whence) { if('SET' === whence) {
if(offset < 0) { if(offset < 0) {
callback(new EInvalid('resulting file offset would be negative')); callback(new Errors.EInvalid('resulting file offset would be negative'));
} else { } else {
ofd.position = offset; ofd.position = offset;
callback(null, ofd.position); callback(null, ofd.position);
} }
} else if('CUR' === whence) { } else if('CUR' === whence) {
if(ofd.position + offset < 0) { if(ofd.position + offset < 0) {
callback(new EInvalid('resulting file offset would be negative')); callback(new Errors.EInvalid('resulting file offset would be negative'));
} else { } else {
ofd.position += offset; ofd.position += offset;
callback(null, ofd.position); callback(null, ofd.position);
@ -2210,7 +2196,7 @@ define(function(require) {
} else if('END' === whence) { } else if('END' === whence) {
fstat_file(context, ofd, update_descriptor_position); fstat_file(context, ofd, update_descriptor_position);
} else { } else {
callback(new EInvalid('whence argument is not a proper value')); callback(new Errors.EInvalid('whence argument is not a proper value'));
} }
} }
@ -2263,9 +2249,9 @@ define(function(require) {
var ofd = fs.openFiles[fd]; var ofd = fs.openFiles[fd];
if(!ofd) { if(!ofd) {
callback(new EBadFileDescriptor('invalid file descriptor')); callback(new Errors.EBadFileDescriptor('invalid file descriptor'));
} else if(!_(ofd.flags).contains(O_WRITE)) { } else if(!_(ofd.flags).contains(O_WRITE)) {
callback(new EBadFileDescriptor('descriptor does not permit writing')); callback(new Errors.EBadFileDescriptor('descriptor does not permit writing'));
} else { } else {
futimes_file(context, ofd, atime, mtime, check_result); futimes_file(context, ofd, atime, mtime, check_result);
} }
@ -2368,9 +2354,9 @@ define(function(require) {
var ofd = fs.openFiles[fd]; var ofd = fs.openFiles[fd];
if(!ofd) { if(!ofd) {
callback(new EBadFileDescriptor('invalid file descriptor')); callback(new Errors.EBadFileDescriptor('invalid file descriptor'));
} else if(!_(ofd.flags).contains(O_WRITE)) { } else if(!_(ofd.flags).contains(O_WRITE)) {
callback(new EBadFileDescriptor('descriptor does not permit writing')); callback(new Errors.EBadFileDescriptor('descriptor does not permit writing'));
} else { } else {
ftruncate_file(context, ofd, length, check_result); ftruncate_file(context, ofd, length, check_result);
} }

View File

@ -1,10 +1,7 @@
define(function(require) { define(function(require) {
var fs = require('src/fs');
return { return {
FileSystem: require('src/fs'), FileSystem: require('src/fs'),
Path: require('src/path') Path: require('src/path'),
Errors: require('src/errors')
}; };
}); });

View File

@ -2,7 +2,7 @@
define(function(require) { define(function(require) {
var Path = require('src/path'); var Path = require('src/path');
var FilerError = require('src/error'); var Errors = require('src/errors');
var Environment = require('src/environment'); var Environment = require('src/environment');
var async = require('async'); var async = require('async');
@ -40,14 +40,14 @@ define(function(require) {
// Make sure the path actually exists, and is a dir // Make sure the path actually exists, and is a dir
fs.stat(path, function(err, stats) { fs.stat(path, function(err, stats) {
if(err) { if(err) {
callback(new FilerError.ENotDirectory()); callback(new Errors.ENotDirectory());
return; return;
} }
if(stats.type === 'DIRECTORY') { if(stats.type === 'DIRECTORY') {
cwd = path; cwd = path;
callback(); callback();
} else { } else {
callback(new FilerError.ENotDirectory()); callback(new Errors.ENotDirectory());
} }
}); });
}; };
@ -313,7 +313,7 @@ define(function(require) {
// If not, see if we're allowed to delete recursively // If not, see if we're allowed to delete recursively
if(!options.recursive) { if(!options.recursive) {
callback(new FilerError.ENotEmpty()); callback(new Errors.ENotEmpty());
return; return;
} }