Skip to content

[android] Fix symlink comparison in Android. #2433

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
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
27 changes: 15 additions & 12 deletions Foundation/FileManager+POSIX.swift
Original file line number Diff line number Diff line change
Expand Up @@ -853,24 +853,27 @@ extension FileManager {
}
}

private func _compareSymlinks(withFileSystemRepresentation file1Rep: UnsafePointer<Int8>, andFileSystemRepresentation file2Rep: UnsafePointer<Int8>, size: Int64) -> Bool {
let bufSize = Int(size)
private func _compareSymlinks(withFileSystemRepresentation file1Rep: UnsafePointer<Int8>, andFileSystemRepresentation file2Rep: UnsafePointer<Int8>, size fileSize: Int64) -> Bool {
let bufSize = Int(fileSize)
let buffer1 = UnsafeMutablePointer<CChar>.allocate(capacity: bufSize)
defer { buffer1.deallocate() }
let buffer2 = UnsafeMutablePointer<CChar>.allocate(capacity: bufSize)
defer { buffer2.deallocate() }

let size1 = readlink(file1Rep, buffer1, bufSize)
guard size1 >= 0 else { return false }

let size2 = readlink(file2Rep, buffer2, bufSize)
guard size2 >= 0 else { return false }

let compare: Bool
if size1 < 0 || size2 < 0 || size1 != size || size1 != size2 {
compare = false
} else {
compare = memcmp(buffer1, buffer2, size1) == 0
}
#if !os(Android)
// In Android the reported size doesn't match the contents.
// Other platforms seems to follow that rule.
guard fileSize == size1 else { return false }
#endif

buffer1.deallocate()
buffer2.deallocate()
return compare
guard size1 == size2 else { return false }
return memcmp(buffer1, buffer2, size1) == 0
}

internal func _lstatFile(atPath path: String, withFileSystemRepresentation fsRep: UnsafePointer<Int8>? = nil) throws -> stat {
Expand Down Expand Up @@ -938,7 +941,7 @@ extension FileManager {

/* -contentsEqualAtPath:andPath: does not take into account data stored in the resource fork or filesystem extended attributes.
*/
internal func _contentsEqual(atPath path1: String, andPath path2: String) -> Bool {
internal func _contentsEqual(atPath path1: String, andPath path2: String) -> Bool {
do {
let fsRep1 = try __fileSystemRepresentation(withPath: path1)
defer { fsRep1.deallocate() }
Expand Down