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

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 explintation:

"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 is it for COPYFILE_FICLONE and COPYFILE_FICLONE_FORCE why we are using these two mode

i researched about "how "copy on write" works" i found these websites: https://www.geeksforgeeks.org/copy-on-write/ https://www.computerhope.com/jargon/c/copy-on-write.htm 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;
}

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

       // 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 error by using COPYFILE_FICLONE_FORCE result :

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'
}




Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jone Polvora