Skip to content
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

Overwriting rename is not atomic #835

Closed
wmertens opened this issue Sep 20, 2020 · 2 comments
Closed

Overwriting rename is not atomic #835

wmertens opened this issue Sep 20, 2020 · 2 comments

Comments

@wmertens
Copy link

wmertens commented Sep 20, 2020

  • Operating System: Linux 5
  • Node.js version: 12
  • fs-extra version: latest

As you can see in

return remove(dest, err => {
if (err) return cb(err)
return rename(src, dest, overwrite, cb)
})
, when overwriting, the target is first removed. This is not an atomic operation and can leave the target removed but the source in place.

However, the rename call already removes the target, and this is an atomic operation.

So IMHO, move should only check if the target exists when not overwriting, it should not delete the file itself. (and if the source and target have the same inode, it should not count as already existing, #801)

@RyanZim
Copy link
Collaborator

RyanZim commented Sep 21, 2020

Ah, good point; PR welcome to fix this.

@RyanZim
Copy link
Collaborator

RyanZim commented Apr 1, 2021

Set out to fix this today; I didn't read the docs close enough:

In the case that newPath already exists, it will be overwritten. If there is a directory at newPath, an error will be raised instead.

We support moving dirs, and at this stage in the code, we know nothing about the state of the destination. We're gonna have to be non-atomic for dirs anyhow; so I'm going to elect to leave this as-is. If you're working with files only, and you need perfect atomic behavior, use fs.rename directly.

@RyanZim RyanZim closed this as completed Apr 1, 2021
@RyanZim RyanZim removed the bug label Apr 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants