NODE.JS why we should use : COPYFILE_FICLONE and COPYFILE_FICLONE_FORCE what is it for?

919 views Asked by At

Guys I am trying to learn Node.js by reading Node.js documentation.

I began to learn fs module firstly

and while learning I saw this explanation:

mode 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).

at https://nodejs.org/api/fs.html#fscopyfilesrc-dest-mode-callback

I didn't understand what COPYFILE_FICLONE and COPYFILE_FICLONE_FORCE are for, and why we are using these two modes

I researched about "how copy-on-write" works" and found these websites:

and I still didn't understand

may be I thought you guys can help to me and I can understand why is

 //*  Module   *//
let fs = require('fs');

//* Variables *//
source = 'source.txt';
destination = 'hesyy.txt';

//* call back function for error *//
function callback(err) {
    if (!err){
        console.log("source.txt copied to destination");
    } else throw err;
}

// the copy operation will fail if dest already exists.
const {COPYFILE_EXCL} = fs.constants;
// 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.
const {COPYFILE_FICLONE} = fs.constants;
 // 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.
const {COPYFILE_FICLONE_FORCE} = fs.constants;

// fs.copyFile(source,destination,callback);
// fs.copyFile(source,destination,COPYFILE_EXCL,callback);
// fs.copyFile(source,destination,COPYFILE_FICLONE,callback);
fs.copyFile(source,destination,COPYFILE_FICLONE_FORCE,err => {
    if (!err) {
        console.log("Copied");
    }else{
        console.log("err yo:",err);
    }
});

running : node copyFile.js and I got an error by using COPYFILE_FICLONE_FORCE:

err yo: [Error: ENOSYS: function not implemented, copyfile 'C:\Users\CENSORED\Desktop\nodejss\fs\fs.copyFile\source.txt' -> 'C:\Users\CENSORED\Desktop\nodejss\fs\fs.copyFile\hessyy.txt'] {
  errno: -4054,
  code: 'ENOSYS',
  syscall: 'copyfile',
  path: 'C:\\Users\\CENSORED\\Desktop\\nodejss\\fs\\fs.copyFile\\source.txt',
  dest: 'C:\\Users\\CENSORED\\Desktop\\nodejss\\fs\\fs.copyFile\\hessyy.txt'
}
2

There are 2 answers

1
Jone Polvora On

According to docs:

mode 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.

So you're using WINDOWS and some flags/functions are not available.

0
phuclv On

What in the link you found that you don't understand?

Copy-on-write or CoW is a technique to efficiently copy data resources in a computer system. If a unit of data is copied but not modified, the "copy" can exist as a reference to the original data. Only when the copied data is modified is a copy created, and new bytes are actually written.

https://www.computerhope.com/jargon/c/copy-on-write.htm

So CoW allows a new object to be copied almost instantly, sharing memory with the old one until the new object is changed. For example copying the existing file A to the new file B that way will make a new B, saying that its current content is the same with A. However when B is modified then B will be actually copied to a separate memory region and the two will not share memory anymore

Copy-on-Write requires filesystem support because you can't mark that a block belongs to multiple files if the FS doesn't allow that. For example on Linux the default ext4 doesn't support CoW at all and you'll have to use a supported FS such as ZFS, Btrfs or XFS. On Windows only ReFS supports CoW, if you use NTFS obviously you'll get the not supported error

[Error: ENOSYS: function not implemented, copyfile 'C:\Users\CENSORED\Desktop\nodejss\fs\fs.copyFile\source.txt' -> 'C:\Users\CENSORED\Desktop\nodejss\fs\fs.copyFile\hessyy.txt'] {

Unfortunately nodejs hasn't been updated to support CoW on Windows yet so even when you use Dev Drive or ReFS you still won't be able to do COPYFILE_FICLONE_FORCE until that's fixed