diff --git a/src/filesystem/implementation.js b/src/filesystem/implementation.js index fee85b8..f55019a 100644 --- a/src/filesystem/implementation.js +++ b/src/filesystem/implementation.js @@ -1761,6 +1761,16 @@ function read(fs, context, fd, buffer, offset, length, position, callback) { } } +function fsync(fs, context, fd, callback) { + if(validateInteger(fd, callback) !== fd) return; + var ofd = fs.openFiles[fd]; + if(!ofd) { + callback(new Errors.EBADF()); + } else { + callback(); + } +} + function readFile(fs, context, path, options, callback) { callback = arguments[arguments.length - 1]; options = validate_file_options(options, null, 'r'); @@ -2413,6 +2423,7 @@ module.exports = { stat: stat, fstat: fstat, link: link, + fsync: fsync, read: read, readFile: readFile, write: write, diff --git a/src/filesystem/interface.js b/src/filesystem/interface.js index 0cf012c..bd21b9a 100644 --- a/src/filesystem/interface.js +++ b/src/filesystem/interface.js @@ -282,6 +282,7 @@ function FileSystem(options, callback) { { name: 'rmdir', promises: true }, { name: 'stat', promises: true }, { name: 'fstat' }, + { name: 'fsync' }, { name: 'link', promises: true }, { name: 'unlink', promises: true }, { name: 'read' }, diff --git a/tests/index.js b/tests/index.js index a8b86dc..8a7e136 100644 --- a/tests/index.js +++ b/tests/index.js @@ -24,6 +24,7 @@ require('./spec/fs.writeFile-readFile.spec'); require('./spec/fs.appendFile.spec'); require('./spec/fs.read.spec'); require('./spec/fs.close.spec'); +require('./spec/fs.fsync.spec'); require('./spec/fs.link.spec'); require('./spec/fs.unlink.spec'); require('./spec/fs.rename.spec'); diff --git a/tests/spec/fs.fsync.spec.js b/tests/spec/fs.fsync.spec.js new file mode 100644 index 0000000..fcedaab --- /dev/null +++ b/tests/spec/fs.fsync.spec.js @@ -0,0 +1,47 @@ +var util = require('../lib/test-utils.js'); +var expect = require('chai').expect; + +describe('fs.fsync', function() { + beforeEach(util.setup); + afterEach(util.cleanup); + + it('should be a function', function() { + var fs = util.fs(); + expect(fs.fsync).to.be.a('function'); + }); + + it('should return error when fd is not a number', function(done) { + var fs = util.fs(); + fs.fsync('1', function(error) { + expect(error).to.exist; + expect(error.code).to.equal('EINVAL'); + done(); + }); + }); + + it('should return error when fd is invalid', function(done) { + var fs = util.fs(); + fs.fsync(1, function(error) { + expect(error).to.exist; + expect(error.code).to.equal('EBADF'); + done(); + }); + }); + + it('should not error if the fd is valid', function(done) { + var fs = util.fs(); + + fs.writeFile('/myfile', 'contents', function(error) { + if(error) throw error; + + fs.open('/myfile', 'r', function(error, fd) { + if(error) throw error; + + fs.fsync(fd, function(error) { + expect(error).to.not.exist; + fs.close(done); + }); + }); + }); + }); +});