forked from haraka/Haraka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsync_writestream.js
141 lines (120 loc) · 3.8 KB
/
fsync_writestream.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
"use strict";
var fs = require('fs');
var util = require('util');
module.exports = FsyncWriteStream;
function FsyncWriteStream (path, options) {
if (!(this instanceof FsyncWriteStream)) return new FsyncWriteStream(path, options);
fs.WriteStream.call(this, path, options);
}
util.inherits(FsyncWriteStream, fs.WriteStream);
var versions = process.version.split('.'),
version = Number(versions[0].substring(1)),
subversion = Number(versions[1]);
if (version > 0 || subversion >= 10) {
// for v0.10+ compat
FsyncWriteStream.prototype.close = function(cb) {
var self = this;
if (cb)
this.once('close', cb);
if (this.closed || 'number' !== typeof this.fd) {
if ('number' !== typeof this.fd) {
this.once('open', close);
return;
}
return process.nextTick(this.emit.bind(this, 'close'));
}
this.closed = true;
close();
function close(fd) {
fs.fsync(fd || self.fd, function(er) {
if (er)
self.emit('error', er);
else {
fs.close(fd || self.fd, function(er) {
if (er)
self.emit('error', er);
else
self.emit('close');
});
self.fd = null;
}
})
}
};
}
else {
FsyncWriteStream.prototype.flush = function() {
if (this.busy) return;
var self = this;
var args = this._queue.shift();
if (!args) {
if (this.drainable) { this.emit('drain'); }
return;
}
this.busy = true;
var method = args.shift(),
cb = args.pop();
args.push(function(err) {
self.busy = false;
if (err) {
self.writable = false;
if (cb) {
cb(err);
}
self.emit('error', err);
return;
}
if (method == fs.write) {
self.bytesWritten += arguments[1];
if (cb) {
// write callback
cb(null, arguments[1]);
}
} else if (method === self._open) {
// save reference for file pointer
self.fd = arguments[1];
self.emit('open', self.fd);
} else if (method === fs.close) {
// stop flushing after close
if (cb) {
cb(null);
}
self.emit('close');
return;
} else if (method === fs.fsync) {
// XXX: New code compared to WriteStream.flush
// stop flushing after fsync
if (cb) {
cb(null);
}
return;
}
self.flush();
});
// Inject the file pointer
if (method !== self._open) {
args.unshift(this.fd);
}
method.apply(this, args);
};
FsyncWriteStream.prototype.end = function(data, encoding, cb) {
if (typeof(data) === 'function') {
cb = data;
} else if (typeof(encoding) === 'function') {
cb = encoding;
this.write(data);
} else if (arguments.length > 0) {
this.write(data, encoding);
}
this.writable = false;
// XXX: New code compared to WriteStream.flush
var self = this;
var fsync_cb = function (err) {
if (err) return cb(err);
self._queue.push([fs.close, cb]);
self.flush();
}
this._queue.push([fs.fsync, fsync_cb]);
this.flush();
};
}