Skip to content

[SR-9979] FileHandle class used to implement FileManager._compareFiles #2029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Foundation/FileHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,23 @@ open class FileHandle : NSObject, NSSecureCoding {
}
#endif
}

internal func _readBytes(into buffer: UnsafeMutablePointer<UInt8>, length: Int) throws -> Int {
#if os(Windows)
var BytesRead: DWORD = 0
let BytesToRead: DWORD = DWORD(length)
if ReadFile(_handle, buffer, BytesToRead, &BytesRead, nil) == FALSE {
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
}
return Int(BytesRead)
#else
let amtRead = _read(_fd, buffer, length)
if amtRead < 0 {
throw _NSErrorWithErrno(errno, reading: true)
}
return amtRead
#endif
}

internal func _writeBytes(buf: UnsafeRawPointer, length: Int) throws {
#if os(Windows)
Expand Down Expand Up @@ -426,6 +443,19 @@ open class FileHandle : NSObject, NSSecureCoding {
}
#endif

internal convenience init?(fileSystemRepresentation: UnsafePointer<Int8>, flags: Int32, createMode: Int) {
#if os(Windows)
var fd: Int = -1
if let path = String(cString: fileSystemRepresentation).cString(using: .utf16) {
fd = _CFOpenFileWithMode(path, flags, mode_t(createMode))
}
#else
let fd = _CFOpenFileWithMode(fileSystemRepresentation, flags, mode_t(createMode))
#endif
guard fd > 0 else { return nil }
self.init(fileDescriptor: fd, closeOnDealloc: true)
}

deinit {
// .close() tries to wait after operations in flight on the handle queue, if one exists, and then close. It does so by sending .sync { … } work to it.
// if we try to do that here, we may end up in a situation where:
Expand Down
25 changes: 8 additions & 17 deletions Foundation/FileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1720,32 +1720,23 @@ open class FileManager : NSObject {
#if os(Windows)
NSUnimplemented()
#else
let fd1 = open(file1Rep, O_RDONLY)
guard fd1 >= 0 else {
return false
}
defer { close(fd1) }

let fd2 = open(file2Rep, O_RDONLY)
guard fd2 >= 0 else {
return false
}
defer { close(fd2) }

let buffer1 = UnsafeMutablePointer<UInt8>.allocate(capacity: bufSize)
let buffer2 = UnsafeMutablePointer<UInt8>.allocate(capacity: bufSize)
guard let file1 = FileHandle(fileSystemRepresentation: file1Rep, flags: O_RDONLY, createMode: 0) else { return false }
guard let file2 = FileHandle(fileSystemRepresentation: file2Rep, flags: O_RDONLY, createMode: 0) else { return false }

var buffer1 = UnsafeMutablePointer<UInt8>.allocate(capacity: bufSize)
var buffer2 = UnsafeMutablePointer<UInt8>.allocate(capacity: bufSize)
defer {
buffer1.deallocate()
buffer2.deallocate()
}

var bytesLeft = size
while bytesLeft > 0 {
let bytesToRead = Int(min(Int64(bufSize), bytesLeft))
guard read(fd1, buffer1, bytesToRead) == bytesToRead else {

guard let file1BytesRead = try? file1._readBytes(into: buffer1, length: bytesToRead), file1BytesRead == bytesToRead else {
return false
}
guard read(fd2, buffer2, bytesToRead) == bytesToRead else {
guard let file2BytesRead = try? file2._readBytes(into: buffer2, length: bytesToRead), file2BytesRead == bytesToRead else {
return false
}
guard memcmp(buffer1, buffer2, bytesToRead) == 0 else {
Expand Down