Skip to content

Commit 56090c7

Browse files
committed
Open then fstat, not stat then open
1 parent 92a2fa6 commit 56090c7

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

Sources/FoundationEssentials/FileManager/FileOperations.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -874,18 +874,18 @@ enum _FileOperations {
874874
#else
875875
#if !canImport(Darwin)
876876
private static func _copyRegularFile(_ srcPtr: UnsafePointer<CChar>, _ dstPtr: UnsafePointer<CChar>, delegate: some LinkOrCopyDelegate) throws {
877-
var fileInfo = stat()
878-
guard stat(srcPtr, &fileInfo) >= 0 else {
879-
try delegate.throwIfNecessary(errno, String(cString: srcPtr), String(cString: dstPtr))
880-
return
881-
}
882-
883877
let srcfd = open(srcPtr, O_RDONLY)
884878
guard srcfd >= 0 else {
885879
try delegate.throwIfNecessary(errno, String(cString: srcPtr), String(cString: dstPtr))
886880
return
887881
}
888882
defer { close(srcfd) }
883+
884+
var fileInfo = stat()
885+
guard fstat(srcfd, &fileInfo) >= 0 else {
886+
try delegate.throwIfNecessary(errno, String(cString: srcPtr), String(cString: dstPtr))
887+
return
888+
}
889889

890890
let dstfd = open(dstPtr, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC, 0o666)
891891
guard dstfd >= 0 else {

0 commit comments

Comments
 (0)