Browse Source

fs: add autoClose option to fs.createWriteStream

Add support to fs.createWriteStream and fs.createWriteStream for an autoClose
option that behaves similarly to the autoClose option supported by
fs.createReadStream and fs.ReadStream.

When an instance of fs.createWriteStream created with autoClose === false finishes,
it is not destroyed. Its underlying fd is not closed and it is the
responsibility of the user to close it.

PR-URL: https://github.com/nodejs/node/pull/3679
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
v5.x
Saquib 9 years ago
committed by Evan Lucas
parent
commit
28793958af
  1. 9
      doc/api/fs.markdown
  2. 15
      lib/fs.js
  3. 52
      test/parallel/test-fs-write-stream-autoclose-option.js

9
doc/api/fs.markdown

@ -342,13 +342,20 @@ Returns a new [`WriteStream`][] object. (See [Writable Stream][]).
{ flags: 'w', { flags: 'w',
defaultEncoding: 'utf8', defaultEncoding: 'utf8',
fd: null, fd: null,
mode: 0o666 } mode: 0o666,
autoClose: true }
`options` may also include a `start` option to allow writing data at `options` may also include a `start` option to allow writing data at
some position past the beginning of the file. Modifying a file rather some position past the beginning of the file. Modifying a file rather
than replacing it may require a `flags` mode of `r+` rather than the than replacing it may require a `flags` mode of `r+` rather than the
default mode `w`. The `defaultEncoding` can be any one of those accepted by [`Buffer`][]. default mode `w`. The `defaultEncoding` can be any one of those accepted by [`Buffer`][].
If `autoClose` is set to true (default behavior) on `error` or `end`
the file descriptor will be closed automatically. If `autoClose` is false,
then the file descriptor won't be closed, even if there's an error.
It is your responsiblity to close it and make sure
there's no file descriptor leak.
Like [`ReadStream`][], if `fd` is specified, `WriteStream` will ignore the Like [`ReadStream`][], if `fd` is specified, `WriteStream` will ignore the
`path` argument and will use the specified file descriptor. This means that no `path` argument and will use the specified file descriptor. This means that no
`'open'` event will be emitted. Note that `fd` should be blocking; non-blocking `'open'` event will be emitted. Note that `fd` should be blocking; non-blocking

15
lib/fs.js

@ -1886,6 +1886,7 @@ function WriteStream(path, options) {
this.mode = options.mode === undefined ? 0o666 : options.mode; this.mode = options.mode === undefined ? 0o666 : options.mode;
this.start = options.start; this.start = options.start;
this.autoClose = options.autoClose === undefined ? true : !!options.autoClose;
this.pos = undefined; this.pos = undefined;
this.bytesWritten = 0; this.bytesWritten = 0;
@ -1907,7 +1908,11 @@ function WriteStream(path, options) {
this.open(); this.open();
// dispose on finish. // dispose on finish.
this.once('finish', this.close); this.once('finish', function() {
if (this.autoClose) {
this.close();
}
});
} }
fs.FileWriteStream = fs.WriteStream; // support the legacy name fs.FileWriteStream = fs.WriteStream; // support the legacy name
@ -1916,7 +1921,9 @@ fs.FileWriteStream = fs.WriteStream; // support the legacy name
WriteStream.prototype.open = function() { WriteStream.prototype.open = function() {
fs.open(this.path, this.flags, this.mode, function(er, fd) { fs.open(this.path, this.flags, this.mode, function(er, fd) {
if (er) { if (er) {
this.destroy(); if (this.autoClose) {
this.destroy();
}
this.emit('error', er); this.emit('error', er);
return; return;
} }
@ -1939,7 +1946,9 @@ WriteStream.prototype._write = function(data, encoding, cb) {
var self = this; var self = this;
fs.write(this.fd, data, 0, data.length, this.pos, function(er, bytes) { fs.write(this.fd, data, 0, data.length, this.pos, function(er, bytes) {
if (er) { if (er) {
self.destroy(); if (self.autoClose) {
self.destroy();
}
return cb(er); return cb(er);
} }
self.bytesWritten += bytes; self.bytesWritten += bytes;

52
test/parallel/test-fs-write-stream-autoclose-option.js

@ -0,0 +1,52 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const path = require('path');
const fs = require('fs');
const file = path.join(common.tmpDir, 'write-autoclose-opt1.txt');
common.refreshTmpDir();
let stream = fs.createWriteStream(file, {flags: 'w+', autoClose: false});
stream.write('Test1');
stream.end();
stream.on('finish', common.mustCall(function() {
process.nextTick(common.mustCall(function() {
assert.strictEqual(stream.closed, undefined);
assert(stream.fd !== null);
next();
}));
}));
function next() {
// This will tell us if the fd is usable again or not
stream = fs.createWriteStream(null, {fd: stream.fd, start: 0});
stream.write('Test2');
stream.end();
stream.on('finish', common.mustCall(function() {
assert.strictEqual(stream.closed, true);
assert.strictEqual(stream.fd, null);
process.nextTick(common.mustCall(next2));
}));
}
function next2() {
// This will test if after reusing the fd data is written properly
fs.readFile(file, function(err, data) {
assert(!err);
assert.strictEqual(data.toString(), 'Test2');
process.nextTick(common.mustCall(next3));
});
}
function next3() {
// This is to test success scenario where autoClose is true
const stream = fs.createWriteStream(file, {autoClose: true});
stream.write('Test3');
stream.end();
stream.on('finish', common.mustCall(function() {
process.nextTick(common.mustCall(function() {
assert.strictEqual(stream.closed, true);
assert.strictEqual(stream.fd, null);
}));
}));
}
Loading…
Cancel
Save