Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 87 - Added fs.fsync() as a no-op, and added tests. Modified docume... #176

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ fs.open('/myfile.txt', function(err, fd) {

#### fs.fsync(fd, callback)<a name="fsync"></a>

NOTE: Not yet implemented, see https://github.com/js-platform/filer/issues/87
NOTE: Currently implemented as a no-op. Will error if a file descriptor is invalid or if the descriptor is not opened in a write context, but otherwise returns an empty callback.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is unnecessary.


#### fs.write(fd, buffer, offset, length, position, callback)<a name="write"></a>

Expand Down
12 changes: 12 additions & 0 deletions src/filesystem/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,17 @@ define(function(require) {
unlink_node(context, path, standard_check_result_cb(callback));
}

function fsync(fs, context, fd, callback) {
var ofd = fs.openFiles[fd];
if(!ofd) {
callback(new Errors.EBADF());
} else if(!_(ofd.flags).contains(O_WRITE)) {
callback(new Errors.EBADF('descriptor does not permit writing'));
} else {
callback();
}
}

function read(fs, context, fd, buffer, offset, length, position, callback) {
// Follow how node.js does this
function wrapped_cb(err, bytesRead) {
Expand Down Expand Up @@ -1898,6 +1909,7 @@ define(function(require) {
stat: stat,
fstat: fstat,
link: link,
fsync: fsync,
read: read,
readFile: readFile,
write: write,
Expand Down
1 change: 1 addition & 0 deletions src/filesystem/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ define(function(require) {
'fstat',
'link',
'unlink',
'fsync',
'read',
'readFile',
'write',
Expand Down
48 changes: 48 additions & 0 deletions tests/spec/fs.fsync.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
define(["Filer", "util"], function(Filer, util) {

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 invalid', function() {
var fs = util.fs();
fs.fsync(1, function(error){
expect(error).to.exist;
});
});

it('should error if the fd is not open in a write context', function(done) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fsync() is valid for read-only files; It causes metadata to be written.

var fs = util.fs();
fs.writeFile('/myfile', 'the 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.exist;
expect(error.code).to.equal('EBADF');
done();
});
});
});
});

it('should not error if the fd is open in a write context', function(done) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge this test with the previous one. Only care if the descriptor is valid.

var fs = util.fs();
fs.writeFile('/myfile', 'the contents', function(error) {
if(error) throw error;
fs.open('/myfile', 'w', function(error, fd) {
if(error) throw error;
fs.fsync(fd, function(error) {
expect(error).to.not.exist;
done();
});
});
});
});
});
});
1 change: 1 addition & 0 deletions tests/test-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ define([
"spec/fs.readdir.spec",
"spec/fs.rmdir.spec",
"spec/fs.open.spec",
"spec/fs.fsync.spec",
"spec/fs.write.spec",
"spec/fs.writeFile-readFile.spec",
"spec/fs.appendFile.spec",
Expand Down