Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
fs: add autoClose option to fs.WriteStream
Browse files Browse the repository at this point in the history
Add support to fs.createWriteStream and fs.WriteStream for an autoClose
option that behaves similarly to the autoClose option supported by
fs.createReadStream and fs.ReadStream.

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

Fixes #20880
  • Loading branch information
Saquib Khan committed May 12, 2015
1 parent a58b174 commit 17e6fdd
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 4 deletions.
9 changes: 8 additions & 1 deletion doc/api/fs.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -822,13 +822,20 @@ Returns a new WriteStream object (See `Writable Stream`).
{ flags: 'w',
encoding: null,
fd: null,
mode: 0666 }
mode: 0666,
autoClose: true }

`options` may also include a `start` option to allow writing data at
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
default mode `w`.

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. If `autoClose` is set to true (default
behavior) on `error` or `end` the file descriptor will be closed
automatically.

Like `ReadStream` above, if `fd` is specified, `WriteStream` will ignore the
`path` argument and will use the specified file descriptor. This means that no
`open` event will be emitted.
Expand Down
17 changes: 14 additions & 3 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1742,6 +1742,9 @@ function WriteStream(path, options) {
this.mode = options.hasOwnProperty('mode') ? options.mode : 438; /*=0666*/

this.start = options.hasOwnProperty('start') ? options.start : undefined;
this.autoClose = options.hasOwnProperty('autoClose') ?
options.autoClose : true;

this.pos = undefined;
this.bytesWritten = 0;

Expand All @@ -1760,7 +1763,11 @@ function WriteStream(path, options) {
this.open();

// 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
Expand All @@ -1769,7 +1776,9 @@ fs.FileWriteStream = fs.WriteStream; // support the legacy name
WriteStream.prototype.open = function() {
fs.open(this.path, this.flags, this.mode, function(er, fd) {
if (er) {
this.destroy();
if (this.autoClose) {
this.destroy();
}
this.emit('error', er);
return;
}
Expand All @@ -1792,7 +1801,9 @@ WriteStream.prototype._write = function(data, encoding, cb) {
var self = this;
fs.write(this.fd, data, 0, data.length, this.pos, function(er, bytes) {
if (er) {
self.destroy();
if (this.autoClose) {
self.destroy();
}
return cb(er);
}
self.bytesWritten += bytes;
Expand Down
70 changes: 70 additions & 0 deletions test/simple/test-fs-write-stream-autoclose-option.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
var common = require('../common');
var assert = require('assert');
var path = require('path');
var fs = require('fs');

var file = path.join(common.tmpDir, 'write-autoclose-opt1.txt');
var stream = fs.createWriteStream(file, {flags: 'w+', autoClose: false});
stream.write('Test1');
stream.end();
stream.on('finish', function() {
process.nextTick(function() {
assert(!stream.closed);
assert(stream.fd);
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', function() {
assert(stream.closed);
assert(!stream.fd);
process.nextTick(next2);
});
}

function next2() {
//This will test if after reusing the fd data is written properly
fs.readFile(file, function(err, data) {
assert(!err);
assert.equal(data, 'Test2');
process.nextTick(next3);
});
}

function next3() {
//This is to test success scenario where autoClose is true
var stream = fs.createWriteStream(file, {autoClose: true});
stream.write('Test3');
stream.end();
stream.on('finish', function() {
process.nextTick(function() {
assert(stream.closed);
assert(!stream.fd);
});
});
}

0 comments on commit 17e6fdd

Please sign in to comment.