Skip to content

fs.copyFile with COPYFILE_FICLONE flag does not overwrite destination if it exists #27273

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
rudolf opened this issue Apr 17, 2019 · 2 comments
Closed

Comments

@rudolf
Copy link
Contributor

rudolf commented Apr 17, 2019

  • Version: 10.15.2, 11.14.0
  • Platform: Darwin Kernel Version 18.2.0 x86_64
  • Subsystem: fs

According to https://github.com/nodejs/node/blob/master/doc/api/fs.md#fscopyfilesrc-dest-flags-callback the default behaviour of fs.copyFile is to overwrite the destination if it already exists. The documentation further gives the example of using fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE to create a copy-on-write reflink which fails if the destination exists.

const fs = require('fs');

// first copy succeeds
fs.copyFile('source.txt', 'destination.txt', fs.constants.COPYFILE_FICLONE, (err) => {
  if (err) throw err;
  console.log('source.txt was copied to destination.txt');
});

// destination now exists, causing copy operation to fail
fs.copyFile('source.txt', 'destination.txt', fs.constants.COPYFILE_FICLONE, (err) => {
  if (err) throw err;
  console.log('source.txt was copied to destination.txt');
});

{ [Error: EEXIST: file already exists, copyfile 'source.txt' -> 'destination.txt']
errno: -17,
code: 'EEXIST',
syscall: 'copyfile',
path: 'source.txt',
dest: 'destination.txt' }

@richardlau
Copy link
Member

The latest version of libuv (#27241) is switching from the macOS native copyfile(3) implementation to the more Unix-like generic implementation (libuv/libuv#2233). Would it be possible to test if the behavior still exists with the changed implementation?

cc @nodejs/libuv

@rudolf
Copy link
Contributor Author

rudolf commented Apr 17, 2019

I can confirm that #27241 solves the issue.

MylesBorins pushed a commit to MylesBorins/node that referenced this issue May 20, 2019
Notable changes:

- uv_gettimeofday() has been added.
- Streaming readdir() via the uv_fs_{open,read,close}dir() methods.
- A macOS copyfile() permissions bug has been fixed.
- A bug in uv_interface_addresses() on machines with multiple
  interfaces has been fixed.

Fixes: nodejs#27273
PR-URL: nodejs#27241
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
MylesBorins pushed a commit that referenced this issue May 21, 2019
Notable changes:

- uv_gettimeofday() has been added.
- Streaming readdir() via the uv_fs_{open,read,close}dir() methods.
- A macOS copyfile() permissions bug has been fixed.
- A bug in uv_interface_addresses() on machines with multiple
  interfaces has been fixed.

Fixes: #27273
Backport-PR-URL: #27776
PR-URL: #27241
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants