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

Commit 474db22

Browse files
author
Saquib Khan
committed
Adding autoClose=true option to fs.createWrireStream #20880
1 parent a58b174 commit 474db22

File tree

3 files changed

+92
-4
lines changed

3 files changed

+92
-4
lines changed

doc/api/fs.markdown

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,13 +822,20 @@ Returns a new WriteStream object (See `Writable Stream`).
822822
{ flags: 'w',
823823
encoding: null,
824824
fd: null,
825-
mode: 0666 }
825+
mode: 0666,
826+
autoClose: true }
826827

827828
`options` may also include a `start` option to allow writing data at
828829
some position past the beginning of the file. Modifying a file rather
829830
than replacing it may require a `flags` mode of `r+` rather than the
830831
default mode `w`.
831832

833+
If `autoClose` is false, then the file descriptor won't be closed, even if
834+
there's an error. It is your responsiblity to close it and make sure
835+
there's no file descriptor leak. If `autoClose` is set to true (default
836+
behavior) on `error` or `end` the file descriptor will be closed
837+
automatically.
838+
832839
Like `ReadStream` above, if `fd` is specified, `WriteStream` will ignore the
833840
`path` argument and will use the specified file descriptor. This means that no
834841
`open` event will be emitted.

lib/fs.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,6 +1742,9 @@ function WriteStream(path, options) {
17421742
this.mode = options.hasOwnProperty('mode') ? options.mode : 438; /*=0666*/
17431743

17441744
this.start = options.hasOwnProperty('start') ? options.start : undefined;
1745+
this.autoClose = options.hasOwnProperty('autoClose') ?
1746+
options.autoClose : true;
1747+
17451748
this.pos = undefined;
17461749
this.bytesWritten = 0;
17471750

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

17621765
// dispose on finish.
1763-
this.once('finish', this.close);
1766+
this.once('finish', function() {
1767+
if (this.autoClose) {
1768+
this.close();
1769+
}
1770+
});
17641771
}
17651772

17661773
fs.FileWriteStream = fs.WriteStream; // support the legacy name
@@ -1769,7 +1776,9 @@ fs.FileWriteStream = fs.WriteStream; // support the legacy name
17691776
WriteStream.prototype.open = function() {
17701777
fs.open(this.path, this.flags, this.mode, function(er, fd) {
17711778
if (er) {
1772-
this.destroy();
1779+
if (this.autoClose) {
1780+
this.destroy();
1781+
}
17731782
this.emit('error', er);
17741783
return;
17751784
}
@@ -1792,7 +1801,9 @@ WriteStream.prototype._write = function(data, encoding, cb) {
17921801
var self = this;
17931802
fs.write(this.fd, data, 0, data.length, this.pos, function(er, bytes) {
17941803
if (er) {
1795-
self.destroy();
1804+
if (this.autoClose) {
1805+
self.destroy();
1806+
}
17961807
return cb(er);
17971808
}
17981809
self.bytesWritten += bytes;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
var common = require('../common');
22+
var assert = require('assert');
23+
var path = require('path');
24+
var fs = require('fs');
25+
26+
var file = path.join(common.tmpDir, 'write-autoclose-opt1.txt');
27+
var stream = fs.createWriteStream(file, {flags: 'w+', autoClose: false});
28+
stream.write('Test1');
29+
stream.end();
30+
stream.on('finish', function() {
31+
process.nextTick(function() {
32+
assert(!stream.closed);
33+
assert(stream.fd);
34+
next();
35+
});
36+
});
37+
38+
function next() {
39+
// This will tell us if the fd is usable again or not
40+
stream = fs.createWriteStream(null, {fd: stream.fd, start:0 });
41+
stream.write('Test2');
42+
stream.end();
43+
stream.on('finish', function() {
44+
assert(stream.closed);
45+
assert(!stream.fd);
46+
process.nextTick(next2);
47+
});
48+
}
49+
50+
function next2() {
51+
//This will test if after reusing the fd data is written properly
52+
fs.readFile(file, function(err, data) {
53+
assert(!err);
54+
assert.equal(data, 'Test2');
55+
process.nextTick(next3);
56+
});
57+
}
58+
59+
function next3() {
60+
//This is to test success scenario where autoClose is true
61+
var stream = fs.createWriteStream(file, {autoClose: true});
62+
stream.write('Test3');
63+
stream.end();
64+
stream.on('finish', function() {
65+
process.nextTick(function() {
66+
assert(stream.closed);
67+
assert(!stream.fd);
68+
});
69+
});
70+
}

0 commit comments

Comments
 (0)