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
|
var fs = require('graceful-fs')
var util = require('util')
var crypto = require('crypto')
function md5hex () {
var hash = crypto.createHash('md5')
for (var ii=0; ii<arguments.length; ++ii) {
hash.update(''+arguments[ii])
}
return hash.digest('hex')
}
var invocations = 0;
function getTmpname (filename) {
return filename + "." + md5hex(__filename, process.pid, ++invocations)
}
module.exports = WriteStream
util.inherits(WriteStream, fs.WriteStream)
function WriteStream (path, options) {
if (!options)
options = {}
if (!(this instanceof WriteStream))
return new WriteStream(path, options)
this.__atomicTarget = path
this.__atomicChown = options.chown
this.__atomicDidStuff = false
this.__atomicTmp = getTmpname(path)
fs.WriteStream.call(this, this.__atomicTmp, options)
}
function cleanup (er) {
fs.unlink(this.__atomicTmp, function () {
fs.WriteStream.prototype.emit.call(this, 'error', er)
}.bind(this))
}
function cleanupSync (er) {
try {
fs.unlinkSync(this.__atomicTmp)
} finally {
return fs.WriteStream.prototype.emit.call(this, 'error', er)
}
}
// When we *would* emit 'close' or 'finish', instead do our stuff
WriteStream.prototype.emit = function (ev) {
if (ev === 'error')
return cleanupSync(this)
if (ev !== 'close' && ev !== 'finish')
return fs.WriteStream.prototype.emit.apply(this, arguments)
if (ev === 'finish') {
atomicDoStuff.call(this, function (er) {
if (er)
cleanup.call(this, er)
else
fs.WriteStream.prototype.emit.call(this, 'finish')
}.bind(this))
}
// close will be emitted later, once we do the rename
}
function atomicDoStuff(cb) {
if (this.__atomicDidStuff)
throw new Error('Already did atomic move-into-place')
this.__atomicDidStuff = true
if (this.__atomicChown) {
var uid = this.__atomicChown.uid
var gid = this.__atomicChown.gid
return fs.chown(this.__atomicTmp, uid, gid, function (er) {
if (er) return cb(er)
moveIntoPlace.call(this, cb)
}.bind(this))
} else {
moveIntoPlace.call(this, cb)
}
}
function moveIntoPlace (cb) {
fs.rename(this.__atomicTmp, this.__atomicTarget, function (er) {
cb(er)
fs.WriteStream.prototype.emit.call(this, 'close')
}.bind(this))
}
|