NODE.JS为什么要用途:COPYFILE_FICLONE和COPYFILE_FICLONE_FORCE是什么?

4sup72z8  于 2023-06-22  发布在  Node.js
关注(0)|答案(2)|浏览(90)

伙计们,我正试图通过阅读Node.js文档来学习Node.js。
我首先开始学习fs模块
在学习的过程中,我看到了这样的解释:
mode是一个可选整数,用于指定复制操作的行为。可以创建由两个或更多个值的按位OR组成的掩码(例如,fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE)。
https://nodejs.org/api/fs.html#fscopyfilesrc-dest-mode-callback
我不明白COPYFILE_FICLONECOPYFILE_FICLONE_FORCE是干什么用的,为什么我们要使用这两种模式
我研究了“写时复制”是如何工作的”,发现了这些网站:

我还是不明白
也许我认为你们可以帮助我,我能理解为什么

//*  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'
}
n6lpvg4x

n6lpvg4x1#

根据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.

因此,您使用的是WINDOWS,并且某些标志/功能不可用。

jfgube3f

jfgube3f2#

你在链接中发现了什么你不明白的地方?
写时复制或CoW是在计算机系统中有效地复制数据资源的技术。如果数据单元被复制但未被修改,则“副本”可以作为对原始数据的引用而存在。只有当复制的数据被修改时,才会创建副本,并且实际写入新字节。
https://www.computerhope.com/jargon/c/copy-on-write.htm
因此,CoW允许几乎立即复制新对象,与旧对象共享内存,直到新对象被更改。例如,将现有文件A复制到新文件B,这样将生成一个新的B,即其当前内容与A相同。然而,当B被修改时,B实际上将被复制到一个单独的内存区域,两者将不再共享内存
Copy-on-Write requires filesystem support,因为如果文件系统不允许,你不能标记一个块属于多个文件。例如,在Linux上,默认的ext4根本不支持CoW,您必须使用受支持的FS,如ZFS,Btrfs或XFS。在Windows only ReFS supports CoW上,如果您使用NTFS,显然您会收到不支持的错误

[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'] {

不幸的是,nodejs尚未更新以支持Windows上的CoW,因此即使您使用Dev Drive或ReFS,在修复之前,您仍然无法执行COPYFILE_FICLONE_FORCE

相关问题