-
Notifications
You must be signed in to change notification settings - Fork 28.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fs.copyFile does not actually do a full copy #30575
Comments
Tested the windows code. Because it's calling into the OS to do the copy it does copy the entire file including meta data. Example:
|
@greggman Did you find a work around? |
Is this issue still occuring in the latest of LTS version of Node.js? CC @nodejs/platform-macos |
Yes, it's still an issue. Here's a test test.js import fs from 'fs';
import { spawnSync } from 'child_process';
import assert from 'assert';
function assertStringsEqual(a, b) {
assert(a === b, `expected: '${a}' === '${b}'`);
}
function run(cmd, args) {
const r = spawnSync(cmd, args, { encoding: 'utf8'});
assert(!r.status, `${r.status}: ${r.stdout}${r.stderr}`);
return r;
}
// create test.txt
fs.writeFileSync('test.txt', 'hello');
// give test.txt an xattribute
let r = run('xattr', [
'-w', 'foo', 'bar', 'test.txt',
]);
// copy test.txt to dup.txt
r = run('cp', [
'test.txt', 'dup.txt',
]);
// list the attributes on both test.txt and dup.txt
let r1 = run('xattr', ['-l', 'test.txt']);
let r2 = run('xattr', ['-l', 'dup.txt']);
// compare - they're the same.
const s1 = r1.stdout;
const s2 = r2.stdout.replaceAll('dup', 'test');
assertStringsEqual(s1, s2);
// use fs.copyFile to copy test.txt to fscopy.txt
fs.copyFileSync('test.txt', 'fscopy.txt');
// list the attributes of fscopy.txt
const r3 = run('xattr', ['-l', 'fscopy.txt']);
// compare - they are not the same but should be
const s3 = r3.stdout.replaceAll('fscopy', 'test');
console.log('s1:', JSON.stringify(s1));
console.log('s2:', JSON.stringify(s2));
console.log('s3:', JSON.stringify(s3));
assertStringsEqual(s1, s3); pacakge.json {
"name": "test-fs-copyfile",
"version": "1.0.0",
"main": "test.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": ""
} run with results
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
copyFile on MacOS does not correctly copy files. A simple example
Here's a .jpg downloaded with Chrome
copy with it
cp
now copy it with fs.copyFileSync
notice all the metadata is missing.
I believe like windows calls an OS level copy function in MacOS
copyItem
needs to be called.The text was updated successfully, but these errors were encountered: