-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5024 from mnaamani/colossus-move-instead-of-rename
storage-node: moveFile
- Loading branch information
Showing
4 changed files
with
27 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import fs from 'fs' | ||
const fsPromises = fs.promises | ||
|
||
/** | ||
* Asynchronously copies `src` to `dest`. If `dest` already exists, operation is aborted | ||
* to avoid corrupting destination file or removal by nodejs when an error occurs after | ||
* it has been opened for writing. | ||
* Use this function instead of `rename` to move files between volumes, otherwise | ||
* operation fails with: EXDEV: cross-device link not permitted. | ||
* @param src A path to the source file. | ||
* @param dest A path to the destination file. | ||
*/ | ||
export async function moveFile(src: fs.PathLike, dest: fs.PathLike): Promise<void> { | ||
// `COPYFILE_EXCL` flag causes the copy operation to fail if `dest` already exists. | ||
const { COPYFILE_EXCL } = fs.constants | ||
// Try to copy source file to destination | ||
await fsPromises.copyFile(src, dest, COPYFILE_EXCL) | ||
|
||
// Only if copy operation succeeds we delete the source file | ||
await fsPromises.unlink(src) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters