[Linux] Don't keep the file descriptor for FileImageSource.

We don't really need it after calling `mmap()`, and not keeping it around
means it's easy to make sure it gets closed properly in all cases.

rdar://117590155
This commit is contained in:
Alastair Houghton
2023-10-31 10:02:37 +00:00
parent 8678ead796
commit 018868e23f

View File

@@ -24,7 +24,6 @@ enum FileImageSourceError: Error {
}
class FileImageSource: ImageSource {
private var _fd: Int32
private var _mapping: UnsafeRawBufferPointer
public var isMappedImage: Bool { return false }
@@ -38,15 +37,16 @@ class FileImageSource: ImageSource {
public init(path: String) throws {
_path = path
_fd = _swift_open(path, O_RDONLY, 0)
if _fd < 0 {
let fd = _swift_open(path, O_RDONLY, 0)
if fd < 0 {
throw FileImageSourceError.posixError(_swift_get_errno())
}
let size = lseek(_fd, 0, SEEK_END)
defer { close(fd) }
let size = lseek(fd, 0, SEEK_END)
if size < 0 {
throw FileImageSourceError.posixError(_swift_get_errno())
}
let base = mmap(nil, Int(size), PROT_READ, MAP_FILE|MAP_PRIVATE, _fd, 0)
let base = mmap(nil, Int(size), PROT_READ, MAP_FILE|MAP_PRIVATE, fd, 0)
if base == nil || base! == UnsafeRawPointer(bitPattern: -1)! {
throw FileImageSourceError.posixError(_swift_get_errno())
}
@@ -56,7 +56,6 @@ class FileImageSource: ImageSource {
deinit {
munmap(UnsafeMutableRawPointer(mutating: _mapping.baseAddress),
_mapping.count)
close(_fd)
}
public func fetch<T>(from addr: Address,