Skip to content

Commit

Permalink
feature(fs-copy-file) add mask support introduce in node v10
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed May 3, 2018
1 parent d05cb29 commit 7926ec7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ fs-copy-file [![License][LicenseIMGURL]][LicenseURL] [![NPM version][NPMIMGURL]]
Node.js v8.5.0 [fs.copyFile](https://nodejs.org/dist/latest-v8.x/docs/api/fs.html#fs_fs_copyfile_src_dest_flags_callback) [ponyfill](https://ponyfill.com).

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.
`flags` is an optional integer that specifies the behavior of the copy operation. The only supported flag is `COPYFILE_EXCL`, which causes the copy operation to fail if dest already exists.

`flags` is an optional integer that specifies the behavior of the copy operation. It is possible to create a mask consisting of the bitwise OR of two or more values (e.g. `fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE`).

- `fs.constants.COPYFILE_EXCL` - The copy operation will fail if dest already exists.
- `fs.constants.COPYFILE_FICLONE` - The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then a fallback copy mechanism is used.
- `fs.constants.COPYFILE_FICLONE_FORCE` - The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then the operation will fail.

## Install

Expand Down
12 changes: 10 additions & 2 deletions lib/fs-copy-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@ const fs = require('fs');
const _copyFile = require('@cloudcmd/copy-file');

const COPYFILE_EXCL = 1;
const COPYFILE_FICLONE = 2;
const COPYFILE_FICLONE_FORCE = 4;

module.exports = fs.copyFile ? fs.copyFile : copyFile;

module.exports.constants = {
COPYFILE_EXCL
COPYFILE_EXCL,
COPYFILE_FICLONE,
COPYFILE_FICLONE_FORCE,
};

const isExcl = (flags) => {
return flags & COPYFILE_EXCL;
};

function copyFile(src, dest, flags, callback) {
Expand All @@ -19,7 +27,7 @@ function copyFile(src, dest, flags, callback) {

check(src, dest, flags, callback);

if (flags !== COPYFILE_EXCL)
if (isExcl(flags) !== COPYFILE_EXCL)
return _copyFile(src, dest, callback);

fs.stat(dest, (error) => {
Expand Down

0 comments on commit 7926ec7

Please sign in to comment.