diff options
author | cjihrig <cjihrig@gmail.com> | 2017-09-06 19:54:29 +0300 |
---|---|---|
committer | Myles Borins <mylesborins@google.com> | 2017-09-12 04:18:38 +0300 |
commit | 71f90c6f80dcb05a0f81a5a90b62e97209f399b0 (patch) | |
tree | c27240ef515a3d48b33968ab67ce4efdca19b8a6 /doc/api/fs.md | |
parent | 3a68b0bb98dc9a9e32d056a212816da83f7203da (diff) |
fs: add fs.copyFile{Sync}
Fixes: https://github.com/nodejs/node/issues/14906
PR-URL: https://github.com/nodejs/node/pull/15034
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Diffstat (limited to 'doc/api/fs.md')
-rw-r--r-- | doc/api/fs.md | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/doc/api/fs.md b/doc/api/fs.md index 2613419df17..ce05469881b 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -750,6 +750,88 @@ Returns an object containing commonly used constants for file system operations. The specific constants currently defined are described in [FS Constants][]. +## fs.copyFile(src, dest[, flags], callback) +<!-- YAML +added: REPLACEME +--> + +* `src` {string|Buffer|URL} source filename to copy +* `dest` {string|Buffer|URL} destination filename of the copy operation +* `flags` {number} modifiers for copy operation. **Default:** `0` +* `callback` {Function} + +Asynchronously copies `src` to `dest`. By default, `dest` is overwritten if it +already exists. No arguments other than a possible exception are given to the +callback function. Node.js makes no guarantees about the atomicity of the copy +operation. If an error occurs after the destination file has been opened for +writing, Node.js will attempt to remove the destination. + +`flags` is an optional integer that specifies the behavior +of the copy operation. The only supported flag is `fs.constants.COPYFILE_EXCL`, +which causes the copy operation to fail if `dest` already exists. + +Example: + +```js +const fs = require('fs'); + +// destination.txt will be created or overwritten by default. +fs.copyFile('source.txt', 'destination.txt', (err) => { + if (err) throw err; + console.log('source.txt was copied to destination.txt'); +}); +``` + +If the third argument is a number, then it specifies `flags`, as shown in the +following example. + +```js +const fs = require('fs'); +const { COPYFILE_EXCL } = fs.constants; + +// By using COPYFILE_EXCL, the operation will fail if destination.txt exists. +fs.copyFile('source.txt', 'destination.txt', COPYFILE_EXCL, callback); +``` + +## fs.copyFileSync(src, dest[, flags]) +<!-- YAML +added: REPLACEME +--> + +* `src` {string|Buffer|URL} source filename to copy +* `dest` {string|Buffer|URL} destination filename of the copy operation +* `flags` {number} modifiers for copy operation. **Default:** `0` + +Synchronously copies `src` to `dest`. By default, `dest` is overwritten if it +already exists. Returns `undefined`. Node.js makes no guarantees about the +atomicity of the copy operation. If an error occurs after the destination file +has been opened for writing, Node.js will attempt to remove the destination. + +`flags` is an optional integer that specifies the behavior +of the copy operation. The only supported flag is `fs.constants.COPYFILE_EXCL`, +which causes the copy operation to fail if `dest` already exists. + +Example: + +```js +const fs = require('fs'); + +// destination.txt will be created or overwritten by default. +fs.copyFileSync('source.txt', 'destination.txt'); +console.log('source.txt was copied to destination.txt'); +``` + +If the third argument is a number, then it specifies `flags`, as shown in the +following example. + +```js +const fs = require('fs'); +const { COPYFILE_EXCL } = fs.constants; + +// By using COPYFILE_EXCL, the operation will fail if destination.txt exists. +fs.copyFileSync('source.txt', 'destination.txt', COPYFILE_EXCL); +``` + ## fs.createReadStream(path[, options]) <!-- YAML added: v0.1.31 |