1 | 'use strict'
|
2 | module.exports = writeFile
|
3 | module.exports.sync = writeFileSync
|
4 | module.exports._getTmpname = getTmpname
|
5 |
|
6 | var fs = require('graceful-fs')
|
7 | var chain = require('slide').chain
|
8 | var MurmurHash3 = require('imurmurhash')
|
9 | var extend = Object.assign || require('util')._extend
|
10 |
|
11 | var invocations = 0
|
12 | function getTmpname (filename) {
|
13 | return filename + '.' +
|
14 | MurmurHash3(__filename)
|
15 | .hash(String(process.pid))
|
16 | .hash(String(++invocations))
|
17 | .result()
|
18 | }
|
19 |
|
20 | function writeFile (filename, data, options, callback) {
|
21 | if (options instanceof Function) {
|
22 | callback = options
|
23 | options = null
|
24 | }
|
25 | if (!options) options = {}
|
26 | fs.realpath(filename, function (_, realname) {
|
27 | _writeFile(realname || filename, data, options, callback)
|
28 | })
|
29 | }
|
30 | function _writeFile (filename, data, options, callback) {
|
31 | var tmpfile = getTmpname(filename)
|
32 |
|
33 | if (options.mode && options.chown) {
|
34 | return thenWriteFile()
|
35 | } else {
|
36 |
|
37 |
|
38 | return fs.stat(filename, function (err, stats) {
|
39 | if (err || !stats) return thenWriteFile()
|
40 |
|
41 | options = extend({}, options)
|
42 | if (!options.mode) {
|
43 | options.mode = stats.mode
|
44 | }
|
45 | if (!options.chown && process.getuid) {
|
46 | options.chown = { uid: stats.uid, gid: stats.gid }
|
47 | }
|
48 | return thenWriteFile()
|
49 | })
|
50 | }
|
51 |
|
52 | function thenWriteFile () {
|
53 | chain([
|
54 | [writeFileAsync, tmpfile, data, options.mode, options.encoding || 'utf8'],
|
55 | options.chown && [fs, fs.chown, tmpfile, options.chown.uid, options.chown.gid],
|
56 | options.mode && [fs, fs.chmod, tmpfile, options.mode],
|
57 | [fs, fs.rename, tmpfile, filename]
|
58 | ], function (err) {
|
59 | err ? fs.unlink(tmpfile, function () { callback(err) })
|
60 | : callback()
|
61 | })
|
62 | }
|
63 |
|
64 |
|
65 |
|
66 | function writeFileAsync (file, data, mode, encoding, cb) {
|
67 | fs.open(file, 'w', options.mode, function (err, fd) {
|
68 | if (err) return cb(err)
|
69 | if (Buffer.isBuffer(data)) {
|
70 | return fs.write(fd, data, 0, data.length, 0, syncAndClose)
|
71 | } else if (data != null) {
|
72 | return fs.write(fd, String(data), 0, String(encoding), syncAndClose)
|
73 | } else {
|
74 | return syncAndClose()
|
75 | }
|
76 | function syncAndClose (err) {
|
77 | if (err) return cb(err)
|
78 | fs.fsync(fd, function (err) {
|
79 | if (err) return cb(err)
|
80 | fs.close(fd, cb)
|
81 | })
|
82 | }
|
83 | })
|
84 | }
|
85 | }
|
86 |
|
87 | function writeFileSync (filename, data, options) {
|
88 | if (!options) options = {}
|
89 | try {
|
90 | filename = fs.realpathSync(filename)
|
91 | } catch (ex) {
|
92 |
|
93 | }
|
94 | var tmpfile = getTmpname(filename)
|
95 |
|
96 | try {
|
97 | if (!options.mode || !options.chown) {
|
98 |
|
99 |
|
100 | try {
|
101 | var stats = fs.statSync(filename)
|
102 | options = extend({}, options)
|
103 | if (!options.mode) {
|
104 | options.mode = stats.mode
|
105 | }
|
106 | if (!options.chown && process.getuid) {
|
107 | options.chown = { uid: stats.uid, gid: stats.gid }
|
108 | }
|
109 | } catch (ex) {
|
110 |
|
111 | }
|
112 | }
|
113 |
|
114 | var fd = fs.openSync(tmpfile, 'w', options.mode)
|
115 | if (Buffer.isBuffer(data)) {
|
116 | fs.writeSync(fd, data, 0, data.length, 0)
|
117 | } else if (data != null) {
|
118 | fs.writeSync(fd, String(data), 0, String(options.encoding || 'utf8'))
|
119 | }
|
120 | fs.fsyncSync(fd)
|
121 | fs.closeSync(fd)
|
122 | if (options.chown) fs.chownSync(tmpfile, options.chown.uid, options.chown.gid)
|
123 | if (options.mode) fs.chmodSync(tmpfile, options.mode)
|
124 | fs.renameSync(tmpfile, filename)
|
125 | } catch (err) {
|
126 | try { fs.unlinkSync(tmpfile) } catch (e) {}
|
127 | throw err
|
128 | }
|
129 | }
|