UNPKG

1.62 kBMarkdownView Raw
1write-file-atomic
2-----------------
3
4This is an extension for node's `fs.writeFile` that makes its operation
5atomic and allows you set ownership (uid/gid of the file).
6
7### var writeFileAtomic = require('write-file-atomic')<br>writeFileAtomic(filename, data, [options], callback)
8
9* filename **String**
10* data **String** | **Buffer**
11* options **Object**
12 * chown **Object**
13 * uid **Number**
14 * gid **Number**
15 * encoding **String** | **Null** default = 'utf8'
16 * mode **Number** default = 438 (aka 0666 in Octal)
17callback **Function**
18
19Atomically and asynchronously writes data to a file, replacing the file if it already
20exists. data can be a string or a buffer.
21
22The file is initially named `filename + "." + murmurhex(__filename, process.pid, ++invocations)`.
23If writeFile completes successfully then, if passed the **chown** option it will change
24the ownership of the file. Finally it renames the file back to the filename you specified. If
25it encounters errors at any of these steps it will attempt to unlink the temporary file and then
26pass the error back to the caller.
27
28If provided, the **chown** option requires both **uid** and **gid** properties or else
29you'll get an error.
30
31The **encoding** option is ignored if **data** is a buffer. It defaults to 'utf8'.
32
33Example:
34
35```javascript
36writeFileAtomic('message.txt', 'Hello Node', {chown:{uid:100,gid:50}}, function (err) {
37 if (err) throw err;
38 console.log('It\'s saved!');
39});
40```
41
42### var writeFileAtomicSync = require('write-file-atomic').sync<br>writeFileAtomicSync(filename, data, [options])
43
44The synchronous version of **writeFileAtomic**.