Skip to content

Commit faba87e

Browse files
committed
FileManager: Use st_blksize as a buffer size when comparing files.
1 parent c8c2a7b commit faba87e

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

Foundation/FileManager.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,7 @@ open class FileManager : NSObject {
638638
NSUnimplemented()
639639
}
640640

641-
private func _compareFiles(withFileSystemRepresentation file1Rep: UnsafePointer<Int8>, andFileSystemRepresentation file2Rep: UnsafePointer<Int8>, size: Int64) -> Bool {
642-
let bufSize = min(size, 1024 * 1024)
641+
private func _compareFiles(withFileSystemRepresentation file1Rep: UnsafePointer<Int8>, andFileSystemRepresentation file2Rep: UnsafePointer<Int8>, size: Int64, bufSize: Int) -> Bool {
643642
let fd1 = open(file1Rep, O_RDONLY)
644643
guard fd1 >= 0 else {
645644
return false
@@ -652,16 +651,16 @@ open class FileManager : NSObject {
652651
}
653652
defer { close(fd2) }
654653

655-
let buffer1 = UnsafeMutablePointer<UInt8>.allocate(capacity: Int(bufSize))
656-
let buffer2 = UnsafeMutablePointer<UInt8>.allocate(capacity: Int(bufSize))
654+
let buffer1 = UnsafeMutablePointer<UInt8>.allocate(capacity: bufSize)
655+
let buffer2 = UnsafeMutablePointer<UInt8>.allocate(capacity: bufSize)
657656
defer {
658657
buffer1.deallocate()
659658
buffer2.deallocate()
660659
}
661660

662661
var bytesLeft = size
663662
while bytesLeft > 0 {
664-
let bytesToRead = Int(min(bufSize, bytesLeft))
663+
let bytesToRead = Int(min(Int64(bufSize), bytesLeft))
665664
guard read(fd1, buffer1, bytesToRead) == bytesToRead else {
666665
return false
667666
}
@@ -784,7 +783,7 @@ open class FileManager : NSObject {
784783
guard file1.st_size == file2.st_size else {
785784
return false
786785
}
787-
return _compareFiles(withFileSystemRepresentation: path1, andFileSystemRepresentation: path2, size: Int64(file1.st_size))
786+
return _compareFiles(withFileSystemRepresentation: path1, andFileSystemRepresentation: path2, size: Int64(file1.st_size), bufSize: Int(file1.st_blksize))
788787
}
789788
else if file1Type == S_IFLNK {
790789
return _compareSymlinks(withFileSystemRepresentation: fsRep1, andFileSystemRepresentation: fsRep2, size: Int64(file1.st_size))

0 commit comments

Comments
 (0)