Skip to content

Commit

Permalink
Imrprove rename error (#2731)
Browse files Browse the repository at this point in the history
Motivation:

The name of the syscall used in errors for all rename calls is "rename".
However, this isn't always correct, in some cases "renamex_np" is used,
and in others "renameat2" is used.

Modifications:

- Allow the name of the syscall to be provided to the rename error
- Use the correct syscall name

Result:

Better errors
  • Loading branch information
glbrntt authored May 29, 2024
1 parent 4612941 commit 9f63b12
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions Sources/NIOFileSystem/FileSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,7 @@ extension FileSystem {

case let .failure(errno):
let error = FileSystemError.rename(
"rename",
errno: errno,
oldName: sourcePath,
newName: destinationPath,
Expand Down
3 changes: 2 additions & 1 deletion Sources/NIOFileSystem/FileSystemError+Syscall.swift
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ extension FileSystemError {

@_spi(Testing)
public static func rename(
_ name: String,
errno: Errno,
oldName: FilePath,
newName: FilePath,
Expand Down Expand Up @@ -739,7 +740,7 @@ extension FileSystemError {
return FileSystemError(
code: code,
message: message,
systemCall: "rename",
systemCall: name,
errno: errno,
location: location
)
Expand Down
4 changes: 4 additions & 0 deletions Sources/NIOFileSystem/Internal/SystemFileHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,9 @@ extension SystemFileHandle.SendableView {
case .rename:
if materialize {
let renameResult: Result<Void, Errno>
let renameFunction: String
#if canImport(Darwin)
renameFunction = "renamex_np"
renameResult = Syscall.rename(
from: createdPath,
to: desiredPath,
Expand All @@ -814,6 +816,7 @@ extension SystemFileHandle.SendableView {
// The created and desired paths are absolute, so the relative descriptors are
// ignored. However, they must still be provided to 'rename' in order to pass
// flags.
renameFunction = "renameat2"
renameResult = Syscall.rename(
from: createdPath,
relativeTo: .currentWorkingDirectory,
Expand All @@ -831,6 +834,7 @@ extension SystemFileHandle.SendableView {

result = renameResult.mapError { errno in
.rename(
renameFunction,
errno: errno,
oldName: createdPath,
newName: desiredPath,
Expand Down
4 changes: 2 additions & 2 deletions Tests/NIOFileSystemTests/FileSystemErrorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ final class FileSystemErrorTests: XCTestCase {
}

assertCauseIsSyscall("rename", here) {
.rename(errno: .badFileDescriptor, oldName: "old", newName: "new", location: here)
.rename("rename", errno: .badFileDescriptor, oldName: "old", newName: "new", location: here)
}

assertCauseIsSyscall("remove", here) {
Expand Down Expand Up @@ -465,7 +465,7 @@ final class FileSystemErrorTests: XCTestCase {
.ioError: .io,
]
) { errno in
.rename(errno: errno, oldName: "old", newName: "new", location: .fixed)
.rename("rename", errno: errno, oldName: "old", newName: "new", location: .fixed)
}
}

Expand Down

0 comments on commit 9f63b12

Please sign in to comment.