From ae5ba81138659a5c714e2e6765fc7cbd0f4417d3 Mon Sep 17 00:00:00 2001 From: Riley Testut Date: Wed, 11 Dec 2019 12:26:48 -0800 Subject: [PATCH] Fixes increasing app size when refreshing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We now delete temporary directory + resigned .ipa before installation is complete to ensure AltStore doesn’t quit before we have the chance to. --- AltStore/Managing Apps/AppManager.swift | 5 +-- AltStore/Operations/InstallAppOperation.swift | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/AltStore/Managing Apps/AppManager.swift b/AltStore/Managing Apps/AppManager.swift index b6df3c65..45d85dc3 100644 --- a/AltStore/Managing Apps/AppManager.swift +++ b/AltStore/Managing Apps/AppManager.swift @@ -405,10 +405,7 @@ private extension AppManager do { try installedApp.managedObjectContext?.save() } catch { print("Error saving installed app.", error) } } - } - - do { try FileManager.default.removeItem(at: context.temporaryDirectory) } - catch { print("Failed to remove temporary directory.", error) } + } print("Finished operation!", context.bundleIdentifier) diff --git a/AltStore/Operations/InstallAppOperation.swift b/AltStore/Operations/InstallAppOperation.swift index d47d395b..7704e6b6 100644 --- a/AltStore/Operations/InstallAppOperation.swift +++ b/AltStore/Operations/InstallAppOperation.swift @@ -18,6 +18,8 @@ class InstallAppOperation: ResultOperation { let context: AppOperationContext + private var didCleanUp = false + init(context: AppOperationContext) { self.context = context @@ -65,6 +67,9 @@ class InstallAppOperation: ResultOperation installedApp.expirationDate = profile.expirationDate } + // Temporary directory and resigned .ipa no longer needed, so delete them now to ensure AltStore doesn't quit before we get the chance to. + self.cleanUp() + self.context.group.beginInstallationHandler?(installedApp) let request = BeginInstallationRequest() @@ -92,6 +97,16 @@ class InstallAppOperation: ResultOperation } } + override func finish(_ result: Result) + { + self.cleanUp() + + super.finish(result) + } +} + +private extension InstallAppOperation +{ func receive(from connection: NWConnection, server: Server, completionHandler: @escaping (Result) -> Void) { server.receiveResponse(from: connection) { (result) in @@ -127,4 +142,25 @@ class InstallAppOperation: ResultOperation } } } + + func cleanUp() + { + guard !self.didCleanUp else { return } + self.didCleanUp = true + + do + { + try FileManager.default.removeItem(at: self.context.temporaryDirectory) + + if let app = self.context.app + { + let fileURL = InstalledApp.refreshedIPAURL(for: app) + try FileManager.default.removeItem(at: fileURL) + } + } + catch + { + print("Failed to remove temporary directory.", error) + } + } }