From d5b5c55fe19e81e21f13cc5f953ebeedb9d2b49c Mon Sep 17 00:00:00 2001 From: Barry Tulchinsky Date: Fri, 13 Dec 2013 00:46:31 -0500 Subject: [PATCH 1/9] implementation of utimes and futimes --- src/fs.js | 114 ++++++++++++++++++++++++++++++++++- tests/spec/fs.utimes.spec.js | 21 +++++++ tests/test-manifest.js | 1 + 3 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 tests/spec/fs.utimes.spec.js diff --git a/src/fs.js b/src/fs.js index a80b5df..a0b265a 100644 --- a/src/fs.js +++ b/src/fs.js @@ -1033,6 +1033,56 @@ define(function(require) { } } + //NOTE: utimes does follow symoblic links (safe to use find_node) + function utimes_file(context, path, atime, mtime, callback) { + path = normalize(path); + + function update_times (error, node) { + if (error) { + callback(error); + } + else { + node.atime = atime; + node.mtime = mtime; + } + } + + //check if atime and mtime are integers and >= 0 + if (typeof atime != 'number' || typeof mtime == 'number') { + callback(new EInvalid('Invalid DateTime values')); + } + else if (atime < 0 || mtime < 0) { + callback(new EInvalid('DateTime values cannot be negative')) + } + else { + find_node(context, path, update_times) + } + } + + function futimes_file(context, ofd, atime, mtime, callback) { + + function update_times (error, node) { + if (error) { + callback(error); + } + else { + node.atime = atime; + node.mtime = mtime; + } + } + + //check if atime and mtime are integers and >= 0 + if (typeof atime != 'number' || typeof mtime == 'number') { + callback(new EInvalid('Invalid DateTime values')); + } + else if (atime < 0 || mtime < 0) { + callback(new EInvalid('DateTime values cannot be negative')) + } + else { + context.get(ofd.id, path, update_times) + } + } + function validate_flags(flags) { if(!_(O_FLAGS).has(flags)) { return null; @@ -1519,9 +1569,47 @@ define(function(require) { read_directory(context, path, check_result); } - function _utimes(path, atime, mtime, callback) { - // TODO - // if(!nullCheck(path, callback)) return; + function _utimes(context, path, atime, mtime, callback) { + if(!nullCheck(path, callback)) return; + + //set atime or mtime to the current time if they are null + atime = (atime) ? atime : Date.now(); + mtime = (mtime) ? mtime : Date.now(); + + function check_result(error) { + if (error) { + callback(error); + } + else { + callback(null); + } + } + utimes_file(context, path, atime, mtime, check_result) + } + + function _futimes(context, fd, atime, mtime, callback) { + function check_result(error) { + if (error) { + callback(error); + } + else { + callback(null); + } + } + + //set atime or mtime to the current time if they are null + atime = (atime) ? atime : Date.now(); + mtime = (mtime) ? mtime : Date.now(); + + var ofd = fs.openFiles[fd]; + + if(!ofd) { + callback(new EBadFileDescriptor('invalid file descriptor')); + } else if(!_(ofd.flags).contains(O_WRITE)) { + callback(new EBadFileDescriptor('descriptor does not permit writing')); + } else { + futimes_file(context, ofd, atime, mtime, check_result); + } } function _rename(context, oldpath, newpath, callback) { @@ -1859,6 +1947,26 @@ define(function(require) { ); if(error) callback(error); }; + FileSystem.prototype.utimes = function(path, atime, mtime, callback) { + callback = maybeCallback(callback); + var fs = this; + var error = fs.queueOrRun( + function () { + var context = fs.provider.getReadWriteContext(); + _utimes(context, path, atime, mtime, callback); + } + ); + }; + FileSystem.prototype.futimes = function(fd, atime, mtime, callback) { + callback = maybeCallback(callback); + var fs = this; + var error = fs.queueOrRun( + function () { + var context = fs.provider.getReadWriteContext(); + _futimes(context, fd, atime, mtime, callback); + } + ); + }; return { FileSystem: FileSystem, diff --git a/tests/spec/fs.utimes.spec.js b/tests/spec/fs.utimes.spec.js new file mode 100644 index 0000000..11b500f --- /dev/null +++ b/tests/spec/fs.utimes.spec.js @@ -0,0 +1,21 @@ +define(["IDBFS"], function(IDBFS) { + + describe('fs.utimes', function() { + beforeEach(function() { + this.db_name = mk_db_name(); + this.fs = new IDBFS.FileSystem({ + name: this.db_name, + flags: 'FORMAT' + }); + }); + + afterEach(function() { + indexedDB.deleteDatabase(this.db_name); + delete this.fs; + }); + + it('should be a function', function() { + expect(typeof this.fs.utimes).toEqual('function'); + }); + }); +}); \ No newline at end of file diff --git a/tests/test-manifest.js b/tests/test-manifest.js index 72c44a9..124f36e 100644 --- a/tests/test-manifest.js +++ b/tests/test-manifest.js @@ -27,6 +27,7 @@ define([ "spec/fs.symlink.spec", "spec/fs.readlink.spec", "spec/fs.truncate.spec", + "spec/fs.utimes.spec", "spec/path-resolution.spec", // IDBFS.FileSystem.providers.* From e84d6392109fd9e7f2f0698be2f05afaaf485104 Mon Sep 17 00:00:00 2001 From: Barry Tulchinsky Date: Sun, 15 Dec 2013 02:54:51 -0500 Subject: [PATCH 2/9] fixed bugs where atime and mtime weren't updated on the node and where mtime was valid it would error. Also added more tests --- src/fs.js | 21 ++++-- tests/spec/fs.utimes.spec.js | 135 ++++++++++++++++++++++++++++++++++- 2 files changed, 149 insertions(+), 7 deletions(-) diff --git a/src/fs.js b/src/fs.js index a0b265a..76a587d 100644 --- a/src/fs.js +++ b/src/fs.js @@ -1038,24 +1038,34 @@ define(function(require) { path = normalize(path); function update_times (error, node) { + //Note: Going by node.js' implementation, utimes works on directories if (error) { callback(error); } else { + console.log(node); node.atime = atime; node.mtime = mtime; + console.log('updated times atime=' + node.atime + 'and mtime=' + node.mtime); + + _stat (context, 'test', path, function (error, stat) { + if (error) console.log('error'); + else console.log(stat); + }); + // callback(null); + context.put(node.id, node, callback); } } //check if atime and mtime are integers and >= 0 - if (typeof atime != 'number' || typeof mtime == 'number') { + if (typeof atime != 'number' || typeof mtime != 'number') { callback(new EInvalid('Invalid DateTime values')); } else if (atime < 0 || mtime < 0) { - callback(new EInvalid('DateTime values cannot be negative')) + callback(new EInvalid('DateTime values cannot be negative')); } else { - find_node(context, path, update_times) + find_node(context, path, update_times); } } @@ -1068,6 +1078,7 @@ define(function(require) { else { node.atime = atime; node.mtime = mtime; + context.put(node.id, node, callback); } } @@ -1076,10 +1087,10 @@ define(function(require) { callback(new EInvalid('Invalid DateTime values')); } else if (atime < 0 || mtime < 0) { - callback(new EInvalid('DateTime values cannot be negative')) + callback(new EInvalid('DateTime values cannot be negative')); } else { - context.get(ofd.id, path, update_times) + context.get(ofd.id, path, update_times); } } diff --git a/tests/spec/fs.utimes.spec.js b/tests/spec/fs.utimes.spec.js index 11b500f..1156a8c 100644 --- a/tests/spec/fs.utimes.spec.js +++ b/tests/spec/fs.utimes.spec.js @@ -14,8 +14,139 @@ define(["IDBFS"], function(IDBFS) { delete this.fs; }); - it('should be a function', function() { - expect(typeof this.fs.utimes).toEqual('function'); + // it('should be a function', function() { + // expect(typeof this.fs.utimes).toEqual('function'); + // }); + + // it('should error when atime is negative', function () { + // var complete = false; + // var _error; + // var that = this; + + // that.fs.writeFile('/testfile', '', function(error) { + // if (error) throw error; + + // that.fs.utimes('/testfile', -1, Date.now(), function (error) { + // _error = error; + // complete = true; + // }); + // }); + + // waitsFor(function () { + // return complete; + // }, 'test to complete', DEFAULT_TIMEOUT); + + // runs(function () { + // expect(_error).toBeDefined(); + // }); + // }); + + // it('should error when mtime is negative', function () { + // var complete = false; + // var _error; + // var that = this; + + // that.fs.writeFile('/testfile', '', function(error) { + // if (error) throw error; + + // that.fs.utimes('/testfile', Date.now(), -1, function (error) { + // _error = error; + // complete = true; + // }); + // }); + + // waitsFor(function () { + // return complete; + // }, 'test to complete', DEFAULT_TIMEOUT); + + // runs(function () { + // expect(_error).toBeDefined(); + // }); + // }); + + // it('should error when atime is as invalid Datetime', function () { + // var complete = false; + // var _error; + // var that = this; + + // that.fs.writeFile('/testfile', '', function (error) { + // if (error) throw error; + + // that.fs.utimes('/testfile', 'invalid datetime', Date.now(), function (error) { + // _error = error; + // complete = true; + // }); + // }); + + // waitsFor(function () { + // return complete; + // }, 'test to complete', DEFAULT_TIMEOUT); + + // runs(function () { + // expect(_error).toBeDefined(); + // }); + // }); + + // it('should error when mtime is as invalid Datetime', function () { + // var complete = false; + // var _error; + // var that = this; + + // that.fs.writeFile('/testfile', '', function (error) { + // if (error) throw error; + + // that.fs.utimes('/testfile', Date.now(), 'invalid datetime', function (error) { + // _error = error; + // complete = true; + // }); + // }); + + // waitsFor(function () { + // return complete; + // }, 'test to complete', DEFAULT_TIMEOUT); + + // runs(function () { + // expect(_error).toBeDefined(); + // }); + // }); + + it('should change atime and mtime of a file path)', function () { + var complete = false; + var _error; + var that = this; + + var stat; + + var atime = Date.parse('1 Oct 2000 15:33:22'); + var mtime = Date.parse('30 Sep 2000 06:43:54'); + + that.fs.writeFile('/testfile', '', function (error) { + if (error) throw error; + + console.log('created file'); + that.fs.utimes('/testfile', atime, mtime, function (error) { + _error = error; + + console.log('getting stats'); + that.fs.stat('/testfile', function (error, rstat) { + if (error) throw error; + + console.log('got stats'); + stat = rstat; + complete = true; + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(stat.atime).toEqual(atime); + expect(stat.mtime).toEqual(mtime); + }); }); }); }); \ No newline at end of file From a0456e290958e487e194309032acac7ed3ac78cb Mon Sep 17 00:00:00 2001 From: Barry Tulchinsky Date: Sun, 15 Dec 2013 02:57:11 -0500 Subject: [PATCH 3/9] removed console.log statements used for debugging --- src/fs.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/fs.js b/src/fs.js index 76a587d..276a458 100644 --- a/src/fs.js +++ b/src/fs.js @@ -1043,16 +1043,8 @@ define(function(require) { callback(error); } else { - console.log(node); node.atime = atime; node.mtime = mtime; - console.log('updated times atime=' + node.atime + 'and mtime=' + node.mtime); - - _stat (context, 'test', path, function (error, stat) { - if (error) console.log('error'); - else console.log(stat); - }); - // callback(null); context.put(node.id, node, callback); } } From b5b2367959d89fc3f95537e2475aee2f495817a9 Mon Sep 17 00:00:00 2001 From: Barry Tulchinsky Date: Sun, 15 Dec 2013 15:27:48 -0500 Subject: [PATCH 4/9] removed console.log statements used for debugging from spec tests --- tests/spec/fs.utimes.spec.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/spec/fs.utimes.spec.js b/tests/spec/fs.utimes.spec.js index 1156a8c..038f26f 100644 --- a/tests/spec/fs.utimes.spec.js +++ b/tests/spec/fs.utimes.spec.js @@ -123,15 +123,12 @@ define(["IDBFS"], function(IDBFS) { that.fs.writeFile('/testfile', '', function (error) { if (error) throw error; - console.log('created file'); that.fs.utimes('/testfile', atime, mtime, function (error) { _error = error; - console.log('getting stats'); that.fs.stat('/testfile', function (error, rstat) { if (error) throw error; - console.log('got stats'); stat = rstat; complete = true; }); From 23ace603c9bbfbf92d02cfa6406026a7d3d82831 Mon Sep 17 00:00:00 2001 From: Barry Tulchinsky Date: Sun, 15 Dec 2013 21:22:36 -0500 Subject: [PATCH 5/9] added/fixed tests and futimes bug --- AUTHORS | 1 + README.md | 8 + src/fs.js | 8 +- tests/spec/fs.utimes.spec.js | 282 ++++++++++++++++++++++------------- 4 files changed, 194 insertions(+), 105 deletions(-) diff --git a/AUTHORS b/AUTHORS index e218b72..eed1c65 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,3 +1,4 @@ Alan K (blog.modeswitch.org) David Humphrey (@humphd) Abir Viqar +Barry Tulchinsky (@btulchinsky) diff --git a/README.md b/README.md index c954638..bc2b2fd 100644 --- a/README.md +++ b/README.md @@ -336,3 +336,11 @@ Asynchronous truncate(2). Callback gets no additional arguments. #### fs.ftruncate(fd, length, callback) Asynchronous ftruncate(2). Callback gets no additional arguments. + +#### fs.utimes(path, atime, mtime, callback) + +Asynchronous utimes(3). Callback gets no additional arguments. + +#### fs.futimes(fd, atime, mtime, callback) + +Asynchronous futimes(3). Callback gets no additional arguments. diff --git a/src/fs.js b/src/fs.js index 276a458..f78b7bd 100644 --- a/src/fs.js +++ b/src/fs.js @@ -1075,14 +1075,14 @@ define(function(require) { } //check if atime and mtime are integers and >= 0 - if (typeof atime != 'number' || typeof mtime == 'number') { + if (typeof atime != 'number' || typeof mtime != 'number') { callback(new EInvalid('Invalid DateTime values')); } else if (atime < 0 || mtime < 0) { callback(new EInvalid('DateTime values cannot be negative')); } else { - context.get(ofd.id, path, update_times); + context.get(ofd.id, update_times); } } @@ -1590,7 +1590,7 @@ define(function(require) { utimes_file(context, path, atime, mtime, check_result) } - function _futimes(context, fd, atime, mtime, callback) { + function _futimes(fs, context, fd, atime, mtime, callback) { function check_result(error) { if (error) { callback(error); @@ -1966,7 +1966,7 @@ define(function(require) { var error = fs.queueOrRun( function () { var context = fs.provider.getReadWriteContext(); - _futimes(context, fd, atime, mtime, callback); + _futimes(fs, context, fd, atime, mtime, callback); } ); }; diff --git a/tests/spec/fs.utimes.spec.js b/tests/spec/fs.utimes.spec.js index 038f26f..8fb0b52 100644 --- a/tests/spec/fs.utimes.spec.js +++ b/tests/spec/fs.utimes.spec.js @@ -14,108 +14,108 @@ define(["IDBFS"], function(IDBFS) { delete this.fs; }); - // it('should be a function', function() { - // expect(typeof this.fs.utimes).toEqual('function'); - // }); + it('should be a function', function() { + expect(typeof this.fs.utimes).toEqual('function'); + }); - // it('should error when atime is negative', function () { - // var complete = false; - // var _error; - // var that = this; - - // that.fs.writeFile('/testfile', '', function(error) { - // if (error) throw error; - - // that.fs.utimes('/testfile', -1, Date.now(), function (error) { - // _error = error; - // complete = true; - // }); - // }); - - // waitsFor(function () { - // return complete; - // }, 'test to complete', DEFAULT_TIMEOUT); - - // runs(function () { - // expect(_error).toBeDefined(); - // }); - // }); - - // it('should error when mtime is negative', function () { - // var complete = false; - // var _error; - // var that = this; - - // that.fs.writeFile('/testfile', '', function(error) { - // if (error) throw error; - - // that.fs.utimes('/testfile', Date.now(), -1, function (error) { - // _error = error; - // complete = true; - // }); - // }); - - // waitsFor(function () { - // return complete; - // }, 'test to complete', DEFAULT_TIMEOUT); - - // runs(function () { - // expect(_error).toBeDefined(); - // }); - // }); - - // it('should error when atime is as invalid Datetime', function () { - // var complete = false; - // var _error; - // var that = this; - - // that.fs.writeFile('/testfile', '', function (error) { - // if (error) throw error; - - // that.fs.utimes('/testfile', 'invalid datetime', Date.now(), function (error) { - // _error = error; - // complete = true; - // }); - // }); - - // waitsFor(function () { - // return complete; - // }, 'test to complete', DEFAULT_TIMEOUT); - - // runs(function () { - // expect(_error).toBeDefined(); - // }); - // }); - - // it('should error when mtime is as invalid Datetime', function () { - // var complete = false; - // var _error; - // var that = this; - - // that.fs.writeFile('/testfile', '', function (error) { - // if (error) throw error; - - // that.fs.utimes('/testfile', Date.now(), 'invalid datetime', function (error) { - // _error = error; - // complete = true; - // }); - // }); - - // waitsFor(function () { - // return complete; - // }, 'test to complete', DEFAULT_TIMEOUT); - - // runs(function () { - // expect(_error).toBeDefined(); - // }); - // }); - - it('should change atime and mtime of a file path)', function () { + it('should error when atime is negative', function () { var complete = false; var _error; var that = this; - var stat; + that.fs.writeFile('/testfile', '', function(error) { + if (error) throw error; + + that.fs.utimes('/testfile', -1, Date.now(), function (error) { + _error = error; + complete = true; + }); + }); + + waitsFor(function () { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function () { + expect(_error).toBeDefined(); + }); + }); + + it('should error when mtime is negative', function () { + var complete = false; + var _error; + var that = this; + + that.fs.writeFile('/testfile', '', function(error) { + if (error) throw error; + + that.fs.utimes('/testfile', Date.now(), -1, function (error) { + _error = error; + complete = true; + }); + }); + + waitsFor(function () { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function () { + expect(_error).toBeDefined(); + }); + }); + + it('should error when atime is as invalid Datetime', function () { + var complete = false; + var _error; + var that = this; + + that.fs.writeFile('/testfile', '', function (error) { + if (error) throw error; + + that.fs.utimes('/testfile', 'invalid datetime', Date.now(), function (error) { + _error = error; + complete = true; + }); + }); + + waitsFor(function () { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function () { + expect(_error).toBeDefined(); + }); + }); + + it('should error when mtime is as invalid Datetime', function () { + var complete = false; + var _error; + var that = this; + + that.fs.writeFile('/testfile', '', function (error) { + if (error) throw error; + + that.fs.utimes('/testfile', Date.now(), 'invalid datetime', function (error) { + _error = error; + complete = true; + }); + }); + + waitsFor(function () { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function () { + expect(_error).toBeDefined(); + }); + }); + + it('should change atime and mtime of a file path', function () { + var complete = false; + var _error; + var that = this; + + var _stat; var atime = Date.parse('1 Oct 2000 15:33:22'); var mtime = Date.parse('30 Sep 2000 06:43:54'); @@ -126,10 +126,10 @@ define(["IDBFS"], function(IDBFS) { that.fs.utimes('/testfile', atime, mtime, function (error) { _error = error; - that.fs.stat('/testfile', function (error, rstat) { + that.fs.stat('/testfile', function (error, stat) { if (error) throw error; - stat = rstat; + _stat = stat; complete = true; }); }); @@ -141,8 +141,88 @@ define(["IDBFS"], function(IDBFS) { runs(function() { expect(_error).toEqual(null); - expect(stat.atime).toEqual(atime); - expect(stat.mtime).toEqual(mtime); + expect(_stat.atime).toEqual(atime); + expect(_stat.mtime).toEqual(mtime); + }); + }); + + it ('should change atime and mtime for a valid file descriptor', function (error) { + var complete = false; + var _error; + var that = this; + + var ofd; + var _stat; + + var atime = Date.parse('1 Oct 2000 15:33:22'); + var mtime = Date.parse('30 Sep 2000 06:43:54'); + + that.fs.open('/testfile', 'w', function (error, result) { + if (error) throw error; + + ofd = result; + + that.fs.futimes(ofd, atime, mtime, function (error) { + _error = error; + + that.fs.fstat(ofd, function (error, stat) { + if (error) throw error; + + _stat = stat; + complete = true; + }); + }); + }); + + waitsFor(function () { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function () { + expect(_error).toEqual(null); + expect(_stat.atime).toEqual(atime); + expect(_stat.mtime).toEqual(mtime); + }); + }); + + it ('should update atime and mtime of directory path', function (error) { + var complete = false + var _error; + + //Note: required as the filesystem somehow gets removed from the Jasmine object + var fs = this.fs; + + var _stat; + + var atime = Date.parse('1 Oct 2000 15:33:22'); + var mtime = Date.parse('30 Sep 2000 06:43:54'); + + fs.mkdir('/testdir', function (error) { + if (error) throw error; + + fs.utimes('/testdir', atime, mtime, function (error) { + _error = error; + + fs.stat('/testdir', function (error, stat) { + if (error) { + throw error; + } + + _stat = stat; + complete = true; + }); + }); + }); + + waitsFor(function () { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function () { + expect(_error).toEqual(null); + expect(_stat.atime).toEqual(atime); + expect(_stat.mtime).toEqual(mtime); + delete fs; }); }); }); From 359b0705367d54724b1a8b9966d6e878e3921aa9 Mon Sep 17 00:00:00 2001 From: Barry Tulchinsky Date: Tue, 17 Dec 2013 11:28:22 -0500 Subject: [PATCH 6/9] updated/added tests and caching for current time --- src/fs.js | 13 +++-- tests/spec/fs.utimes.spec.js | 100 +++++++++++++++++++++++++++++++++-- 2 files changed, 101 insertions(+), 12 deletions(-) diff --git a/src/fs.js b/src/fs.js index f78b7bd..c9a7169 100644 --- a/src/fs.js +++ b/src/fs.js @@ -1033,7 +1033,6 @@ define(function(require) { } } - //NOTE: utimes does follow symoblic links (safe to use find_node) function utimes_file(context, path, atime, mtime, callback) { path = normalize(path); @@ -1575,9 +1574,9 @@ define(function(require) { function _utimes(context, path, atime, mtime, callback) { if(!nullCheck(path, callback)) return; - //set atime or mtime to the current time if they are null - atime = (atime) ? atime : Date.now(); - mtime = (mtime) ? mtime : Date.now(); + var currentTime = Date.now(); + atime = (atime) ? atime : currentTime; + mtime = (mtime) ? mtime : currentTime; function check_result(error) { if (error) { @@ -1600,9 +1599,9 @@ define(function(require) { } } - //set atime or mtime to the current time if they are null - atime = (atime) ? atime : Date.now(); - mtime = (mtime) ? mtime : Date.now(); + var currentTime = Date.now() + atime = (atime) ? atime : currentTime; + mtime = (mtime) ? mtime : currentTime; var ofd = fs.openFiles[fd]; diff --git a/tests/spec/fs.utimes.spec.js b/tests/spec/fs.utimes.spec.js index 8fb0b52..80be8a9 100644 --- a/tests/spec/fs.utimes.spec.js +++ b/tests/spec/fs.utimes.spec.js @@ -38,6 +38,7 @@ define(["IDBFS"], function(IDBFS) { runs(function () { expect(_error).toBeDefined(); + expect(_error.name).toEqual('EInvalid'); }); }); @@ -61,10 +62,11 @@ define(["IDBFS"], function(IDBFS) { runs(function () { expect(_error).toBeDefined(); + expect(_error.name).toEqual('EInvalid'); }); }); - it('should error when atime is as invalid Datetime', function () { + it('should error when atime is as invalid number', function () { var complete = false; var _error; var that = this; @@ -84,10 +86,34 @@ define(["IDBFS"], function(IDBFS) { runs(function () { expect(_error).toBeDefined(); + expect(_error.name).toEqual('EInvalid'); }); }); - it('should error when mtime is as invalid Datetime', function () { + it ('should error when path does not exist', function () { + var complete = false; + var _error; + var that = this; + + var atime = Date.parse('1 Oct 2000 15:33:22'); + var mtime = Date.parse('30 Sep 2000 06:43:54'); + + that.fs.utimes('/pathdoesnotexist', atime, mtime, function (error) { + _error = error; + complete = true; + }); + + waitsFor(function () { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function () { + expect(_error).toBeDefined(); + expect(_error.name).toEqual('ENoEntry'); + }); + }); + + it('should error when mtime is an invalid number', function () { var complete = false; var _error; var that = this; @@ -107,6 +133,30 @@ define(["IDBFS"], function(IDBFS) { runs(function () { expect(_error).toBeDefined(); + expect(_error.name).toEqual('EInvalid'); + }); + }); + + it ('should error when file descriptor is invalid', function () { + var complete = false; + var _error; + var that = this; + + var atime = Date.parse('1 Oct 2000 15:33:22'); + var mtime = Date.parse('30 Sep 2000 06:43:54'); + + that.fs.futimes(1, atime, mtime, function (error) { + _error = error; + complete = true; + }); + + waitsFor(function () { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function () { + expect(_error).toBeDefined(); + expect(_error.name).toEqual('EBadFileDescriptor'); }); }); @@ -204,9 +254,7 @@ define(["IDBFS"], function(IDBFS) { _error = error; fs.stat('/testdir', function (error, stat) { - if (error) { - throw error; - } + if (error) throw error; _stat = stat; complete = true; @@ -225,5 +273,47 @@ define(["IDBFS"], function(IDBFS) { delete fs; }); }); + + it ('should update atime and mtime if they are null', function () { + var complete = false; + var _error; + var that = this; + + var atimeEst; + var mtimeEst; + var now; + + that.fs.writeFile('/myfile', '', function (error) { + if (error) throw error; + + that.fs.utimes('/myfile', null, null, function (error) { + _error = error; + + now = Date.now(); + + that.fs.stat('/myfile', function (error, stat) { + if (error) throw error; + + atime = stat.atime; + mtime = stat.mtime; + atimeEst = now - stat.atime; + mtimeEst = now - stat.mtime; + complete = true; + }); + }); + }); + + waitsFor(function (){ + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function () { + expect(_error).toEqual(null); + // Note: testing estimation as time may differ by a couple of milliseconds + // This number should be increased if tests are on slow systems + expect(atimeEst).toBeLessThan(10); + expect(mtimeEst).toBeLessThan(10); + }); + }); }); }); \ No newline at end of file From 08c1ccfd25be1a6a5d0aa9083792784046ad0029 Mon Sep 17 00:00:00 2001 From: Barry Tulchinsky Date: Tue, 17 Dec 2013 11:36:47 -0500 Subject: [PATCH 7/9] removed unnecessary variables in test --- tests/spec/fs.utimes.spec.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/spec/fs.utimes.spec.js b/tests/spec/fs.utimes.spec.js index 80be8a9..ed3cd9b 100644 --- a/tests/spec/fs.utimes.spec.js +++ b/tests/spec/fs.utimes.spec.js @@ -294,8 +294,6 @@ define(["IDBFS"], function(IDBFS) { that.fs.stat('/myfile', function (error, stat) { if (error) throw error; - atime = stat.atime; - mtime = stat.mtime; atimeEst = now - stat.atime; mtimeEst = now - stat.mtime; complete = true; From 53e6293494be22721b1d34c027b99980778761d3 Mon Sep 17 00:00:00 2001 From: Barry Tulchinsky Date: Tue, 17 Dec 2013 14:15:10 -0500 Subject: [PATCH 8/9] updated error messages --- src/fs.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/fs.js b/src/fs.js index c9a7169..a6060b7 100644 --- a/src/fs.js +++ b/src/fs.js @@ -1050,10 +1050,10 @@ define(function(require) { //check if atime and mtime are integers and >= 0 if (typeof atime != 'number' || typeof mtime != 'number') { - callback(new EInvalid('Invalid DateTime values')); + callback(new EInvalid('atime and mtime must be number')); } else if (atime < 0 || mtime < 0) { - callback(new EInvalid('DateTime values cannot be negative')); + callback(new EInvalid('atime and mtime must be positive integers')); } else { find_node(context, path, update_times); @@ -1075,10 +1075,10 @@ define(function(require) { //check if atime and mtime are integers and >= 0 if (typeof atime != 'number' || typeof mtime != 'number') { - callback(new EInvalid('Invalid DateTime values')); + callback(new EInvalid('atime and mtime must be a number')); } else if (atime < 0 || mtime < 0) { - callback(new EInvalid('DateTime values cannot be negative')); + callback(new EInvalid('atime and mtime must be positive integers')); } else { context.get(ofd.id, update_times); From 7dc0ef6ccb70830584f8182497f28d3b1819d3ab Mon Sep 17 00:00:00 2001 From: Alan Kligman Date: Fri, 20 Dec 2013 00:45:11 -0500 Subject: [PATCH 9/9] remove comments; clarify test message --- src/fs.js | 7 ++----- tests/spec/fs.utimes.spec.js | 18 +++++++++--------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/fs.js b/src/fs.js index a6060b7..dfb4ac6 100644 --- a/src/fs.js +++ b/src/fs.js @@ -1037,7 +1037,6 @@ define(function(require) { path = normalize(path); function update_times (error, node) { - //Note: Going by node.js' implementation, utimes works on directories if (error) { callback(error); } @@ -1048,7 +1047,6 @@ define(function(require) { } } - //check if atime and mtime are integers and >= 0 if (typeof atime != 'number' || typeof mtime != 'number') { callback(new EInvalid('atime and mtime must be number')); } @@ -1073,7 +1071,6 @@ define(function(require) { } } - //check if atime and mtime are integers and >= 0 if (typeof atime != 'number' || typeof mtime != 'number') { callback(new EInvalid('atime and mtime must be a number')); } @@ -1585,7 +1582,7 @@ define(function(require) { else { callback(null); } - } + } utimes_file(context, path, atime, mtime, check_result) } @@ -1611,7 +1608,7 @@ define(function(require) { callback(new EBadFileDescriptor('descriptor does not permit writing')); } else { futimes_file(context, ofd, atime, mtime, check_result); - } + } } function _rename(context, oldpath, newpath, callback) { diff --git a/tests/spec/fs.utimes.spec.js b/tests/spec/fs.utimes.spec.js index ed3cd9b..6e085e0 100644 --- a/tests/spec/fs.utimes.spec.js +++ b/tests/spec/fs.utimes.spec.js @@ -32,7 +32,7 @@ define(["IDBFS"], function(IDBFS) { }); }); - waitsFor(function () { + waitsFor(function () { return complete; }, 'test to complete', DEFAULT_TIMEOUT); @@ -56,7 +56,7 @@ define(["IDBFS"], function(IDBFS) { }); }); - waitsFor(function () { + waitsFor(function () { return complete; }, 'test to complete', DEFAULT_TIMEOUT); @@ -80,7 +80,7 @@ define(["IDBFS"], function(IDBFS) { }); }); - waitsFor(function () { + waitsFor(function () { return complete; }, 'test to complete', DEFAULT_TIMEOUT); @@ -97,7 +97,7 @@ define(["IDBFS"], function(IDBFS) { var atime = Date.parse('1 Oct 2000 15:33:22'); var mtime = Date.parse('30 Sep 2000 06:43:54'); - + that.fs.utimes('/pathdoesnotexist', atime, mtime, function (error) { _error = error; complete = true; @@ -127,7 +127,7 @@ define(["IDBFS"], function(IDBFS) { }); }); - waitsFor(function () { + waitsFor(function () { return complete; }, 'test to complete', DEFAULT_TIMEOUT); @@ -214,13 +214,13 @@ define(["IDBFS"], function(IDBFS) { that.fs.futimes(ofd, atime, mtime, function (error) { _error = error; - + that.fs.fstat(ofd, function (error, stat) { if (error) throw error; _stat = stat; complete = true; - }); + }); }); }); @@ -274,7 +274,7 @@ define(["IDBFS"], function(IDBFS) { }); }); - it ('should update atime and mtime if they are null', function () { + it ('should update atime and mtime using current time if arguments are null', function () { var complete = false; var _error; var that = this; @@ -293,7 +293,7 @@ define(["IDBFS"], function(IDBFS) { that.fs.stat('/myfile', function (error, stat) { if (error) throw error; - + atimeEst = now - stat.atime; mtimeEst = now - stat.mtime; complete = true;