We are running into some issues when doing:
```swift
let writeOptions: OpenOptions.Write =
byteOffset > 0 ? .modifyFile(createIfNecessary: true) : .newFile(replaceExisting: true)
try await FileSystem.shared.withFileHandle(
forWritingAt: FilePath(outputFilePath),
options: writeOptions
) { fileHandle in
```
which results in crashes on some Android devices where `renameat2` is
not available:
```
('renameat2' system call failed with '(38) Function not implemented'.)
```
This PR adds functionality similar to what is already done for `EINVAL`
on Linux, such that both lead to the same recovery: stat the
destination, then plain rename() if absent.
And for non-exclusive renames where it also fails to function not
implemented, it falls back to plain `rename`.
Ports the added system calls from `NIOFS` (from
https://github.com/apple/swift-nio/pull/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 https://github.com/apple/swift-nio/pull/3505 but
applied to the `_NIOFileSystem`:
> Related to https://github.com/apple/swift-nio/issues/3403 and
https://github.com/apple/swift-nio/pull/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.
Motivation:
In #3363 we converted `_NIOFileSystem` to `NIOFileSystem` and removed
the (unreleased) overloads for FilePath/NIOFilePath. This change adds
back `_NIOFileSystem` such that it matches the API it had at 2.86.0.
Modifications:
- Add back `_NIOFileSystem` and `_NIOFileSystemFoundationCompat` such
that their API is at 2.86.0
Result:
- `NIOFileSystem` uses `NIOFilePath`
- `_NIOFileSystem` uses `FilePath`