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

Add autoClose=true option to fs.createWrireStream #20880 #25275

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link

Choose a reason for hiding this comment

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

Can we change the end of this sentence to ..., even if an error occurs.

there's an error. It is your responsiblity to close it and make sure
Copy link

Choose a reason for hiding this comment

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

Let's change this sentence to In this case, it is your responsibility to close the descriptor.

Copy link
Author

Choose a reason for hiding this comment

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

I can change this line but i copied this text from createReadStream autoclose paragraph. Shall i still change it? As of now both createReadStream and createWriteStream para are consistent.

Copy link

Choose a reason for hiding this comment

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

OK, keep it in that case.

Copy link
Author

Choose a reason for hiding this comment

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

okie. Are we good to merge this PR now? This will be my first contribution to Node :-)

Choose a reason for hiding this comment

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

@saquibkhan Thank you very much for your first contribution!

There's no rush to merge a PR. Taking the extra time to make sure changes are as good as they can be and well understood always pays off. We don't want to rush merging changes and introduce new problems that will take more time to fix.

In this case however I'm confident we'll be able to merge it very soon, so there's no need to worry :)

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();
});
});

Choose a reason for hiding this comment

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

Minor style issue: unnecessary blank line.

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);
});
});
}