1 | write-file-atomic
|
2 | -----------------
|
3 |
|
4 | This is an extension for node's `fs.writeFile` that makes its operation
|
5 | atomic 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)
|
17 | callback **Function**
|
18 |
|
19 | Atomically and asynchronously writes data to a file, replacing the file if it already
|
20 | exists. data can be a string or a buffer.
|
21 |
|
22 | The file is initially named `filename + "." + murmurhex(__filename, process.pid, ++invocations)`.
|
23 | If writeFile completes successfully then, if passed the **chown** option it will change
|
24 | the ownership of the file. Finally it renames the file back to the filename you specified. If
|
25 | it encounters errors at any of these steps it will attempt to unlink the temporary file and then
|
26 | pass the error back to the caller.
|
27 |
|
28 | If provided, the **chown** option requires both **uid** and **gid** properties or else
|
29 | you'll get an error.
|
30 |
|
31 | The **encoding** option is ignored if **data** is a buffer. It defaults to 'utf8'.
|
32 |
|
33 | Example:
|
34 |
|
35 | ```javascript
|
36 | writeFileAtomic('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 |
|
44 | The synchronous version of **writeFileAtomic**.
|