Skip to content

Commit 9f63b12

Browse files
authored
Imrprove rename error (#2731)
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
1 parent 4612941 commit 9f63b12

File tree

4 files changed

+9
-3
lines changed

4 files changed

+9
-3
lines changed

Sources/NIOFileSystem/FileSystem.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,7 @@ extension FileSystem {
12161216

12171217
case let .failure(errno):
12181218
let error = FileSystemError.rename(
1219+
"rename",
12191220
errno: errno,
12201221
oldName: sourcePath,
12211222
newName: destinationPath,

Sources/NIOFileSystem/FileSystemError+Syscall.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,7 @@ extension FileSystemError {
689689

690690
@_spi(Testing)
691691
public static func rename(
692+
_ name: String,
692693
errno: Errno,
693694
oldName: FilePath,
694695
newName: FilePath,
@@ -739,7 +740,7 @@ extension FileSystemError {
739740
return FileSystemError(
740741
code: code,
741742
message: message,
742-
systemCall: "rename",
743+
systemCall: name,
743744
errno: errno,
744745
location: location
745746
)

Sources/NIOFileSystem/Internal/SystemFileHandle.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,9 @@ extension SystemFileHandle.SendableView {
804804
case .rename:
805805
if materialize {
806806
let renameResult: Result<Void, Errno>
807+
let renameFunction: String
807808
#if canImport(Darwin)
809+
renameFunction = "renamex_np"
808810
renameResult = Syscall.rename(
809811
from: createdPath,
810812
to: desiredPath,
@@ -814,6 +816,7 @@ extension SystemFileHandle.SendableView {
814816
// The created and desired paths are absolute, so the relative descriptors are
815817
// ignored. However, they must still be provided to 'rename' in order to pass
816818
// flags.
819+
renameFunction = "renameat2"
817820
renameResult = Syscall.rename(
818821
from: createdPath,
819822
relativeTo: .currentWorkingDirectory,
@@ -831,6 +834,7 @@ extension SystemFileHandle.SendableView {
831834

832835
result = renameResult.mapError { errno in
833836
.rename(
837+
renameFunction,
834838
errno: errno,
835839
oldName: createdPath,
836840
newName: desiredPath,

Tests/NIOFileSystemTests/FileSystemErrorTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ final class FileSystemErrorTests: XCTestCase {
230230
}
231231

232232
assertCauseIsSyscall("rename", here) {
233-
.rename(errno: .badFileDescriptor, oldName: "old", newName: "new", location: here)
233+
.rename("rename", errno: .badFileDescriptor, oldName: "old", newName: "new", location: here)
234234
}
235235

236236
assertCauseIsSyscall("remove", here) {
@@ -465,7 +465,7 @@ final class FileSystemErrorTests: XCTestCase {
465465
.ioError: .io,
466466
]
467467
) { errno in
468-
.rename(errno: errno, oldName: "old", newName: "new", location: .fixed)
468+
.rename("rename", errno: errno, oldName: "old", newName: "new", location: .fixed)
469469
}
470470
}
471471

0 commit comments

Comments
 (0)