From d5b5c55fe19e81e21f13cc5f953ebeedb9d2b49c Mon Sep 17 00:00:00 2001 From: Barry Tulchinsky Date: Fri, 13 Dec 2013 00:46:31 -0500 Subject: [PATCH] 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.*