From 95996070650c5d487c1abaa227b73ab1b771278e Mon Sep 17 00:00:00 2001 From: Andrew Johnston Date: Mon, 22 Mar 2010 07:25:24 +0000 Subject: [PATCH] Added posix fsync and fdatasync to fs module --- lib/fs.js | 16 ++++++++++++++++ src/node_file.cc | 44 ++++++++++++++++++++++++++++++++++++++++++++ test/simple/test-fs-fsync.js | 37 +++++++++++++++++++++++++++++++++++++ wscript | 16 ++++++++++++++++ 4 files changed, 113 insertions(+) create mode 100644 test/simple/test-fs-fsync.js diff --git a/lib/fs.js b/lib/fs.js index fcb89d2..833300d 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -187,6 +187,22 @@ fs.rmdirSync = function (path) { return binding.rmdir(path); }; +fs.fdatasync = function (fd, callback) { + binding.fdatasync(fd, callback || noop); +}; + +fs.fdatasyncSync = function (fd) { + return binding.fdatasync(fd); +}; + +fs.fsync = function (fd, callback) { + binding.fsync(fd, callback || noop); +}; + +fs.fsyncSync = function (fd) { + return binding.fsync(fd); +}; + fs.mkdir = function (path, mode, callback) { binding.mkdir(path, mode, callback || noop); }; diff --git a/src/node_file.cc b/src/node_file.cc index a576b5c..1aee4b9 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -54,6 +54,8 @@ static int After(eio_req *req) { case EIO_RMDIR: case EIO_MKDIR: case EIO_FTRUNCATE: + case EIO_FSYNC: + case EIO_FDATASYNC: case EIO_LINK: case EIO_SYMLINK: case EIO_CHMOD: @@ -330,6 +332,46 @@ static Handle Truncate(const Arguments& args) { } } +static Handle Fdatasync(const Arguments& args) { + HandleScope scope; + + if (args.Length() < 1 || !args[0]->IsInt32()) { + return THROW_BAD_ARGS; + } + + int fd = args[0]->Int32Value(); + + if (args[1]->IsFunction()) { + ASYNC_CALL(fdatasync, args[1], fd) + } else { +#if HAVE_FDATASYNC + int ret = fdatasync(fd); +#else + int ret = fsync(fd); +#endif + if (ret != 0) return ThrowException(ErrnoException(errno)); + return Undefined(); + } +} + +static Handle Fsync(const Arguments& args) { + HandleScope scope; + + if (args.Length() < 1 || !args[0]->IsInt32()) { + return THROW_BAD_ARGS; + } + + int fd = args[0]->Int32Value(); + + if (args[1]->IsFunction()) { + ASYNC_CALL(fsync, args[1], fd) + } else { + int ret = fsync(fd); + if (ret != 0) return ThrowException(ErrnoException(errno)); + return Undefined(); + } +} + static Handle Unlink(const Arguments& args) { HandleScope scope; @@ -756,6 +798,8 @@ void File::Initialize(Handle target) { NODE_SET_METHOD(target, "close", Close); NODE_SET_METHOD(target, "open", Open); NODE_SET_METHOD(target, "read", Read); + NODE_SET_METHOD(target, "fdatasync", Fdatasync); + NODE_SET_METHOD(target, "fsync", Fsync); NODE_SET_METHOD(target, "rename", Rename); NODE_SET_METHOD(target, "truncate", Truncate); NODE_SET_METHOD(target, "rmdir", RMDir); diff --git a/test/simple/test-fs-fsync.js b/test/simple/test-fs-fsync.js new file mode 100644 index 0000000..3014743 --- /dev/null +++ b/test/simple/test-fs-fsync.js @@ -0,0 +1,37 @@ +require("../common"); + +var path = require('path'); +var fs = require('fs'); +var successes = 0; + +var file = path.join(fixturesDir, "a.js"); + +error("open " + file); + +fs.open(file, "a", 0777, function (err, fd) { + error("fd " + fd); + if (err) throw err; + + fs.fdatasyncSync(fd); + error("fdatasync SYNC: ok"); + successes++; + + fs.fsyncSync(fd); + error("fsync SYNC: ok"); + successes++; + + fs.fdatasync(fd, function (err) { + if (err) throw err; + error("fdatasync ASYNC: ok"); + successes++; + fs.fsync(fd, function(err) { + if (err) throw err; + error("fsync ASYNC: ok"); + successes++; + }); + }); +}); + +process.addListener("exit", function () { + assert.equal(4, successes); +}); diff --git a/wscript b/wscript index 0083b88..63d072c 100644 --- a/wscript +++ b/wscript @@ -133,6 +133,22 @@ def configure(conf): conf.env.append_value('CCFLAGS', '-D_FILE_OFFSET_BITS=64') conf.env.append_value('CXXFLAGS', '-D_FILE_OFFSET_BITS=64') + ## needed for node_file.cc fdatasync + ## Strangely on OSX 10.6 the g++ doesn't see fdatasync but gcc does? + code = """ + #include + int main(void) + { + int fd = 0; + fdatasync (fd); + return 0; + } + """ + if conf.check_cxx(msg="Checking for fdatasync(2) with c++", fragment=code): + conf.env.append_value('CXXFLAGS', '-DHAVE_FDATASYNC=1') + else: + conf.env.append_value('CXXFLAGS', '-DHAVE_FDATASYNC=0') + # platform platform_def = '-DPLATFORM=' + sys.platform conf.env.append_value('CCFLAGS', platform_def) -- 2.7.4