Browse Source

Avoid closing a WriteStream before it has been opened.

v0.7.4-release
Tobie Langel 15 years ago
committed by Ryan Dahl
parent
commit
f5e4047064
  1. 17
      lib/fs.js
  2. 19
      test/simple/test-fs-write-stream.js

17
lib/fs.js

@ -949,20 +949,23 @@ WriteStream.prototype.destroy = function (cb) {
var self = this; var self = this;
this.writeable = false; this.writeable = false;
function close() {
fs.close(self.fd, function(err) { fs.close(self.fd, function(err) {
if (err) { if (err) {
if (cb) { if (cb) { cb(err); }
cb(err);
}
self.emit('error', err); self.emit('error', err);
return; return;
} }
if (cb) { if (cb) { cb(null); }
cb(null);
}
self.emit('close'); self.emit('close');
}); });
}
if (this.fd) {
close();
} else {
this.addListener('open', close);
}
}; };

19
test/simple/test-fs-write-stream.js

@ -0,0 +1,19 @@
common = require("../common");
assert = common.assert
var path = require('path'),
fs = require('fs');
var file = path.join(common.fixturesDir, "write.txt");
(function() {
var stream = fs.createWriteStream(file),
_fs_close = fs.close;
fs.close = function(fd) {
assert.ok(fd, "fs.close must not be called without an undefined fd.")
fs.close = _fs_close;
}
stream.destroy();
})();
Loading…
Cancel
Save