From 7926ec725d1a0a4bdc197bb6d6ba6f45620b7aad Mon Sep 17 00:00:00 2001 From: coderaiser Date: Thu, 3 May 2018 12:59:50 +0300 Subject: [PATCH] feature(fs-copy-file) add mask support introduce in node v10 --- README.md | 7 ++++++- lib/fs-copy-file.js | 12 ++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e0d4350..5a7b0dc 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/fs-copy-file.js b/lib/fs-copy-file.js index ca7efe7..9babb77 100644 --- a/lib/fs-copy-file.js +++ b/lib/fs-copy-file.js @@ -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) { @@ -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) => {