Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save billywhizz/339869 to your computer and use it in GitHub Desktop.
Save billywhizz/339869 to your computer and use it in GitHub Desktop.
From 56758296f6763aeffdd321ba7127706ea946ac2a Mon Sep 17 00:00:00 2001
From: Andrew Johnston <apjohnsto@gmail.com>
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 | 40 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 56 insertions(+), 0 deletions(-)
diff --git a/lib/fs.js b/lib/fs.js
index f9fc339..797f104 100644
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -162,6 +162,22 @@ exports.truncateSync = function (fd, len) {
return fs.truncate(fd, len);
};
+exports.fdatasync = function (fd, callback) {
+ fs.fdatasync(fd, callback || noop);
+};
+
+exports.fdatasyncSync = function (fd) {
+ return fs.fdatasync(fd);
+};
+
+exports.fsync = function (fd, callback) {
+ fs.fsync(fd, callback || noop);
+};
+
+exports.fsyncSync = function (fd) {
+ return fs.fsync(fd);
+};
+
exports.rmdir = function (path, callback) {
fs.rmdir(path, callback || noop);
};
diff --git a/src/node_file.cc b/src/node_file.cc
index 5ca5416..b8edb9d 100644
--- a/src/node_file.cc
+++ b/src/node_file.cc
@@ -60,6 +60,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:
@@ -309,6 +311,42 @@ static Handle<Value> Truncate(const Arguments& args) {
}
}
+static Handle<Value> 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 {
+ int ret = fdatasync(fd);
+ if (ret != 0) return ThrowException(errno_exception(errno));
+ return Undefined();
+ }
+}
+
+static Handle<Value> 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(errno_exception(errno));
+ return Undefined();
+ }
+}
+
static Handle<Value> Unlink(const Arguments& args) {
HandleScope scope;
@@ -636,6 +674,8 @@ void File::Initialize(Handle<Object> 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);
--
1.6.6.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment