fixed null value and refactored setxattr

This commit is contained in:
Barry Tulchinsky 2014-01-05 12:22:57 -05:00
parent c4dd0f560c
commit b08a8d15dd
2 changed files with 73 additions and 75 deletions

View File

@ -222,6 +222,41 @@ define(function(require) {
}
}
/*
* set extended attribute (refactor)
*/
function set_extended_attribute (context, path, name, value, flag, callback) {
function set_xattr (error, node) {
var xattr = (node ? node.xattrs[name] : null);
if (error) {
callback(error);
}
else if (flag === XATTR_CREATE && node.xattrs.hasOwnProperty(name)) {
callback(new EExists('attribute already exists'));
}
else if (flag === XATTR_REPLACE && !node.xattrs.hasOwnProperty(name)) {
callback(new ENoAttr('attribute does not exist'));
}
else {
node.xattrs[name] = value;
context.put(node.id, node, callback);
}
}
if (typeof path == 'string') {
find_node(context, path, set_xattr);
}
else if (typeof path.id == 'string') {
context.get(path.id, set_xattr);
}
else {
callback(new EInvalid('path or file descriptor of wrong type'));
}
}
/*
* make_root_directory
*/
@ -1124,77 +1159,35 @@ define(function(require) {
function setxattr_file (context, path, name, value, flag, callback) {
path = normalize(path);
function set_xattr (error, node) {
var xattr = (node ? node.xattrs[name] : null);
if (error) {
callback (error);
}
else if (flag == XATTR_CREATE && node.xattrs.hasOwnProperty(name)) {
callback(new EExists('attribute already exists'));
}
else if (flag === XATTR_REPLACE && !node.xattrs.hasOwnProperty(name)) {
callback(new ENoAttr('attribute does not exist'));
}
else {
node.xattrs[name] = value;
context.put(node.id, node, callback);
}
}
if (typeof name != 'string') {
callback(new EInvalid('attribute name must be a string'));
}
else if (!name) {
callback(new EInvalid('attribute name cannot be an empty string'));
}
else if (value == null) {
callback(new EInvalid('value must be defined'));
}
else if (flag != null &&
flag !== XATTR_CREATE && flag !== XATTR_REPLACE) {
callback(new EInvalid('invalid flag, must be null, XATTR_CREATE or XATTR_REPLACE'));
}
else {
find_node (context, path, set_xattr);
set_extended_attribute(context, path, name, value, flag, callback);
}
}
function fsetxattr_file (context, ofd, name, value, flag, callback) {
function set_xattr (error, node) {
var xattr = (node ? node.xattrs[name] : null);
if (error) {
callback(error);
}
else if (flag === XATTR_CREATE && node.xattrs.hasOwnProperty(name)) {
callback(new EExists('attribute already exists'));
}
else if (flag === XATTR_REPLACE && !node.xattrs.hasOwnProperty(name)) {
callback(new ENoAttr('attribute does not exist'));
}
else {
node.xattrs[name] = value;
context.put(node.id, node, callback);
}
}
if (typeof name != 'string') {
callback(new EInvalid('attribute name must be a string'));
}
else if (!name) {
callback(new EInvalid('attribute name cannot be an empty string'));
}
else if (value == null) {
callback(new EInvalid('value must be defined'));
}
else if (flag != null &&
flag !== XATTR_CREATE && flag !== XATTR_REPLACE) {
callback(new EInvalid('invalid flag, must be null, XATTR_CREATE or XATTR_REPLACE'));
}
else {
context.get(ofd.id, set_xattr);
set_extended_attribute(context, ofd, name, value, flag, callback);
}
}

View File

@ -15,16 +15,15 @@ define(["IDBFS"], function(IDBFS) {
});
it('should be a function', function () {
// that = this;
that = this;
// that.fs.writeFile('/testfile/this/is/a/dir/tmp', function (error) {
// if (error) throw error;
that.fs.writeFile('/testfile/this/is/a/dir/tmp', function (error) {
if (error) throw error;
// that.fs.setxattr('/testfile/this/is/a/dir/tmp', 'test', 'value', function (error) {
that.fs.setxattr('/testfile/this/is/a/dir/tmp', 'test', 'value', function (error) {
});
});
// });
// });
expect(typeof this.fs.setxattr).toEqual('function');
expect(typeof this.fs.getxattr).toEqual('function');
@ -78,30 +77,6 @@ define(["IDBFS"], function(IDBFS) {
});
});
it('should error when setting with a null value', function () {
var complete = false;
var _error;
var that = this;
that.fs.writeFile('/testfile', '', function (error) {
if (error) throw error;
that.fs.setxattr('/testfile', 'test', null, function (error) {
_error = error;
complete = true;
});
});
waitsFor(function () {
return complete;
}, 'test to complete', DEFAULT_TIMEOUT);
runs(function () {
expect(_error).toBeDefined();
expect(_error.name).toEqual('EInvalid');
});
});
it('should error when setting with an invalid flag', function () {
var complete = false;
var _error;
@ -617,5 +592,35 @@ define(["IDBFS"], function(IDBFS) {
expect(_error.name).toEqual('ENoAttr');
});
});
it('should allow setting with a null value', function () {
var complete = false;
var _error;
var _value;
var that = this;
that.fs.writeFile('/testfile', '', function (error) {
if (error) throw error;
that.fs.setxattr('/testfile', 'test', null, function (error) {
if (error) throw error;
that.fs.getxattr('/testfile', 'test', function (error, value) {
_error = error;
_value = value;
complete = true;
});
});
});
waitsFor(function () {
return complete;
}, 'test to complete', DEFAULT_TIMEOUT);
runs(function () {
expect(_error).toEqual(null);
expect(_value).toEqual(null);
});
});
});
});