Skip to content

Commit

Permalink
fix: sync rename overwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed Dec 24, 2023
1 parent e77c1bd commit 0397b5d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
4 changes: 4 additions & 0 deletions rename-overwrite/CHANGELOG.md
@@ -1,5 +1,9 @@
# rename-overwrite

## 5.0.0

- Sync rename overwrite locks up the process to wait and retry rename again.

## 4.0.4

### Patch Changes
Expand Down
24 changes: 21 additions & 3 deletions rename-overwrite/index.js
Expand Up @@ -77,10 +77,28 @@ module.exports.sync = function renameOverwriteSync (oldPath, newPath, retry = 0)
retry++
if (retry > 3) throw err
switch (err.code) {
case 'EPERM': // weird Windows stuff
// Windows Antivirus issues
case 'EPERM':
case 'EACCESS': {
rimraf.sync(newPath)
renameOverwriteSync(oldPath, newPath, retry)
return
const start = Date.now()
let backoff = 0
let lastError = err
while (Date.now() - start < 60000 && (lastError.code === 'EPERM' || lastError.code === 'EACCESS')) {
const waitUntil = Date.now() + backoff
while (waitUntil > Date.now()) {}
try {
fs.renameSync(oldPath, newPath)
return
} catch (err) {
lastError = err
}
if (backoff < 100) {
backoff += 10
}
}
throw lastError
}
case 'ENOTEMPTY':
case 'EEXIST':
case 'ENOTDIR':
Expand Down
2 changes: 1 addition & 1 deletion rename-overwrite/package.json
@@ -1,6 +1,6 @@
{
"name": "rename-overwrite",
"version": "4.0.4",
"version": "5.0.0",
"description": "Like `fs.rename` but overwrites existing file or directory",
"main": "index.js",
"files": [
Expand Down

0 comments on commit 0397b5d

Please sign in to comment.