I try run the following code in Linux and Windows:
const fs = require("fs/promises")
const {
constants,
existsSync
} = require("fs")
async function copy() {
try {
await fs.writeFile("demo.txt", "Hello World")
await fs.copyFile("demo.txt", "copy.txt", constants.COPYFILE_FICLONE_FORCE)
} finally {
fs.rm("demo.txt")
if (existsSync("copy.txt")) fs.rm("copy.txt")
}
}
copy().catch(console.error)
Both failed, error message:
Linux:
[Error: ENOTSUP: operation not supported on socket, copyfile 'demo.txt' -> 'copy.txt'] {
errno: -95,
code: 'ENOTSUP',
syscall: 'copyfile',
path: 'demo.txt',
dest: 'copy.txt'
}
Windows:
Error: ENOSYS: function not implemented, copyfile 'demo.txt' -> 'copy.txt'] {
errno: -4054,
code: 'ENOSYS',
syscall: 'copyfile',
path: 'demo.txt',
dest: 'copy.txt'
}
The official documentation of nodejs says
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."
Most server operating systems should be Windows server or Linux.
I'm sure the developers of nodejs won't develop a feature that doesn't work, so I'd like to know on what platforms the "fs.constants.COPYFILE_FICLONE_FORCE" file copy method is available. Darwin, BSD, or some other OS?
Here is some reference information:
node version: v16.14.2
windows version: windows 10
windows file system: NTFS
linux version: 5.10.109-1-MANJARO
linux core version: 5.10
linux file system: ext4
Node.js was originally sponsored and heavily developed by Joyent. And Joyent was/is also a big supporter of Illumos (which is based on OpenSolaris which is based on Solaris which is a BSD distribution sold by Sun and later Oracle).
In its early days node was heavily tested on Illumos. And surprise.. Illumos's default filesystem is ZFS which has copy-on-write.
But ZFS is also available on other OSes (At one point Apple even worked on an official ZFS driver for OSX) so you can have copy-on-write if you want if you use something like ZFS. I'm not quite sure if node's copy-on-write support work on other copy-on-write filesystems like Btrfs but it should in theory.