This is what I would do in a perfect world:
fs.open('somepath', 'r+', function(err, fd) { fs.write(fd, 'somedata', function(err, written, string) { fs.rewind(fd, 0)
This is my current implementation:
return async.waterfall([ function(next) { //opening a file descriptor to write some data return fs.open('somepath', 'w+', next) }, function(fd, next) { //writing the data return fs.write(fd, 'somedata', function(err, written, string) { return next(null, fd) }) }, function(fd, next) { //closing the file descriptor return fs.close(fd, next) }, function(next) { //open again to reset cursor position return fs.open('somepath', 'r', next) } ], function(err, fd) { //fd cursor is now at beginning of the file })
I tried resetting the position without closing fd with:
fs.read(fd, new Buffer(0), 0, 0, 0, fn)
but this causes Error: Offset is out of bounds .
Is there a way to reset the cursor without doing this terrible hack?
/ e: an offset error outside comes from this exception . It is easily fixed by setting the buffer size to 1, but it does not rewind the cursor. Perhaps because we ask the function not to read anything.