Add ability to overwrite regular files and symbolic links in NIO FileSystem#3470
Closed
stepan-ulyanin wants to merge 58 commits intoapple:mainfrom
Closed
Add ability to overwrite regular files and symbolic links in NIO FileSystem#3470stepan-ulyanin wants to merge 58 commits intoapple:mainfrom
stepan-ulyanin wants to merge 58 commits intoapple:mainfrom
Conversation
…py with overwrite
…opyFileOverwritingExistentDestination` test
…th a file and file with a symlink
…pan-ulyanin/swift-nio into su/add-overwriting-file-copy
glbrntt
requested changes
Feb 3, 2026
Contributor
There was a problem hiding this comment.
Thanks for opening this! This is quite a large set of changes, can I suggest that you split this into (at least) two PRs. Maybe (1) the syscall changes, and (2) the file system changes?
One notable omission from a quick skim is the detailed error descriptions for the syscalls (take look at FileSystemError+Syscall.swift).
It might actually be easier to do this on _NIOFileSystem first and then bring over the changes to NIOFS as a 3rd PR but I'll leave that decision to you.
Contributor
Author
|
Thanks @glbrntt, I think that is a good idea, let open smaller PR in the coming weeks |
Contributor
Author
glbrntt
pushed a commit
that referenced
this pull request
Feb 9, 2026
…3505) Adds three new system call wrappers for the `symlinkat`, `renameatx_np`, and `unlinkat` system calls. ### Motivation: Related to #3403 and #3470. This PR adds syscall wrappers needed to atomically overwrite existing files or symlinks at the destination during copy operations. On Linux, atomic overwrites require a "copy to temp file, then rename" strategy. We use the `*at` family of syscalls (which operate relative to directory file descriptors) to avoid TOCTOU race conditions. ### Modifications: 1. Adds three system call wrappers for the `symlinkat`, `renameatx_np`, and `unlinkat` system calls. 2. Adds related tests 3. Updates the `FileSystemError` for `symlink` and `unlink` to take in the system call name to allow for the `*at` names to be passed.
Contributor
Author
glbrntt
added a commit
that referenced
this pull request
Feb 18, 2026
…3508) Adds capability to NIOFS to copy regular files and symlink, allowing to overwrite the destination. ### Motivation: Per #3403 and #3470 we want to add `replaceExisting: bool` to `FileSystem.copyItem`. ### Modifications: 1. Adds `replaceExisting: bool` parameter to `FileSystemProtocol.copyItem`. 2. Adds `replaceExisting: bool` parameter to `FileSystem.copyItem` and implementation for regular files and symbolic links. 3. Adds tests. --------- Co-authored-by: George Barnett <gbarnett@apple.com>
Contributor
Author
glbrntt
pushed a commit
that referenced
this pull request
Mar 2, 2026
…to the `_NIOFileSystem` module (#3524) Ports the added system calls from `NIOFS` (from #3505) to `_NIOFileSystem`: > Adds three new system call wrappers for the `symlinkat`, `renameatx_np`, and `unlinkat` system calls. ### Motivation: `_NIOFileSystem` module is around for backward compatibility - hence, we are porting the new feature to it. ### Modifications: Exactly same as in #3505 but applied to the `_NIOFileSystem`: > Related to #3403 and #3470. This PR adds syscall wrappers needed to atomically overwrite existing files or symlinks at the destination during copy operations. On Linux, atomic overwrites require a "copy to temp file, then rename" strategy. We use the `*at` family of syscalls (which operate relative to directory file descriptors) to avoid TOCTOU race conditions.
Contributor
Author
|
Ok, we have added all of these changes in separate PRs, closing this one. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a new parameter to the NIOFS FileSystem's
copyItemthat allows to atomically overwrite regular files and symbolic links.Note that I opted in to not implement the overwrite for directories in this PR as it seems that there is no atomic way to overwrite directory trees on Posix systems - do we want to do it non-atomically?
Motivation:
Per #3403 it is not uncommon to want to copy an item and not care if the copied item overwrites an existing one.
Modifications:
overwriting: Boolparameter to NIOFS FileSystem'scopyItem.system_renameatx_np/Syscall.rename(from:relativeTo:to:relativeTo:options:)- Darwin directory-relative renamesystem_symlinkat/Syscall.symlinkat- create symlink relative to directory FDsystem_unlinkat/Syscall.unlinkat- unlink file relative to directory FDCOPYFILE_UNLINKflag is added tocopyfile(2)renameat2(2)which overwrites the existing file.renameatx_np(2)which overwrites the existing symlink.renameat2(2)which overwrites the existing symlink._NIOFileSystemFileSystem'scopyItemfor backward compatibility.Tests
Syscall tests:
test_symlinkattest_unlinkattest_renameatx_npIntegration tests:
testCopyFileOverwritingExistingFiletestCopyFileOverwritingNonExistingFiletestCopyFileOverwritingCleansUpTempFiletestCopyFileOverwritingPreservesPermissionstestCopySymlinkOverwritingExistingSymlinktestCopySymlinkOverwritingNonExistingSymlinktestCopySymlinkOverwritingCleansUpTempLinktestCopyFileOverwritingExistingSymlinktestCopySymlinkOverwritingExistingFileResult:
Users will be able to copy a regular file or a symbolic link, overwriting an existing regular file or existing symbolic link.