From b08a8d15ddd556b8e306621b21b5522f82875845 Mon Sep 17 00:00:00 2001 From: Barry Tulchinsky Date: Sun, 5 Jan 2014 12:22:57 -0500 Subject: [PATCH] fixed null value and refactored setxattr --- src/fs.js | 81 +++++++++++++++++-------------------- tests/spec/fs.xattr.spec.js | 67 ++++++++++++++++-------------- 2 files changed, 73 insertions(+), 75 deletions(-) diff --git a/src/fs.js b/src/fs.js index ba6e357..440a3ab 100644 --- a/src/fs.js +++ b/src/fs.js @@ -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); } } diff --git a/tests/spec/fs.xattr.spec.js b/tests/spec/fs.xattr.spec.js index 579a1be..1dfc793 100644 --- a/tests/spec/fs.xattr.spec.js +++ b/tests/spec/fs.xattr.spec.js @@ -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); + }); + }); }); }); \ No newline at end of file