From ac8560afd328f2d78cd33f98edeeb5d4710a32b8 Mon Sep 17 00:00:00 2001 From: Riley Testut Date: Mon, 25 Oct 2021 23:13:25 -0700 Subject: [PATCH] Adds basic error handling when downloading OTA firmware --- .../Operations/Patch App/PatchAppOperation.swift | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/AltStore/Operations/Patch App/PatchAppOperation.swift b/AltStore/Operations/Patch App/PatchAppOperation.swift index f4272ad3..728108a4 100644 --- a/AltStore/Operations/Patch App/PatchAppOperation.swift +++ b/AltStore/Operations/Patch App/PatchAppOperation.swift @@ -161,13 +161,20 @@ private extension PatchAppOperation try FileManager.default.createDirectory(at: self.patchDirectory, withIntermediateDirectories: true, attributes: nil) let archiveURL = self.patchDirectory.appendingPathComponent("ota.archive") - archiveURL.withUnsafeFileSystemRepresentation { archivePath in - let fz = fragmentzip_open((update.url.absoluteString as NSString).utf8String!) + try archiveURL.withUnsafeFileSystemRepresentation { archivePath in + guard let fz = fragmentzip_open((update.url.absoluteString as NSString).utf8String!) else { + throw URLError(.cannotConnectToHost, userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("The connection failed because a connection cannot be made to the host.", comment: ""), + NSURLErrorKey: update.url]) + } defer { fragmentzip_close(fz) } self.progress.becomeCurrent(withPendingUnitCount: 100) - fragmentzip_download_file(fz, update.archivePath, archivePath!, ALTFragmentZipCallback) - self.progress.resignCurrent() + defer { self.progress.resignCurrent() } + + guard fragmentzip_download_file(fz, update.archivePath, archivePath!, ALTFragmentZipCallback) == 0 else { + throw URLError(.networkConnectionLost, userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("The connection failed because the network connection was lost.", comment: ""), + NSURLErrorKey: update.url]) + } } print("Downloaded OTA archive.")