From 90a1f4dd839068c296b3c9725b5fa3421f46b72a Mon Sep 17 00:00:00 2001 From: Magesh K <47920326+mahee96@users.noreply.github.com> Date: Fri, 13 Dec 2024 20:13:07 +0530 Subject: [PATCH] [op-download]: Fixed a bug where downloaded temp file was deleted before it was used --- .../Operations/DownloadAppOperation.swift | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/AltStore/Operations/DownloadAppOperation.swift b/AltStore/Operations/DownloadAppOperation.swift index fbf42678..45687c5d 100644 --- a/AltStore/Operations/DownloadAppOperation.swift +++ b/AltStore/Operations/DownloadAppOperation.swift @@ -155,6 +155,10 @@ private extension DownloadAppOperation } } + func printWithTid(_ msg: String){ + print("DownloadAppOperation: Thread: \(Thread.current.name ?? Thread.current.description) - " + msg) + } + func download(@Managed _ app: AppProtocol) { guard let sourceURL = self.sourceURL else { @@ -223,11 +227,13 @@ private extension DownloadAppOperation { // Patreon app fileURL = try await downloadPatreonApp(from: sourceURL) + self.printWithTid("downloadPatreonApp: completed at \(fileURL.path)") } else { // Regular app fileURL = try await downloadFile(from: sourceURL) + self.printWithTid("downloadFile: completed at \(fileURL.path)") } defer { @@ -238,7 +244,9 @@ private extension DownloadAppOperation } var isDirectory: ObjCBool = false - guard FileManager.default.fileExists(atPath: fileURL.path, isDirectory: &isDirectory) else { throw OperationError.appNotFound(name: self.appName) } + guard FileManager.default.fileExists(atPath: fileURL.path, isDirectory: &isDirectory) else { + throw OperationError.appNotFound(name: self.appName) + } try FileManager.default.createDirectory(at: self.temporaryDirectory, withIntermediateDirectories: true, attributes: nil) @@ -265,6 +273,17 @@ private extension DownloadAppOperation } guard let application = ALTApplication(fileURL: appBundleURL) else { throw OperationError.invalidApp } + + // perform cleanup of the temp files + if(FileManager.default.fileExists(atPath: fileURL.path)){ + self.printWithTid("Removing downloaded temp file at: \(fileURL.path)") + do{ + try FileManager.default.removeItem(at: fileURL) + } catch{ + self.printWithTid("Removing downloaded temp error: \(error)") + } + } + completionHandler(.success(application)) } catch @@ -287,17 +306,20 @@ private extension DownloadAppOperation } let (fileURL, _) = try Result((fileURL, response), error).get() - try? FileManager.default.removeItem(at: fileURL) continuation.resume(returning: fileURL) + +// self.printWithTid("downloadtask completed: fileURL: \(fileURL) URL: \(downloadURL)") } catch { +// self.printWithTid("downloadtask Error: \(error) URL:\(downloadURL)") continuation.resume(throwing: error) } } self.progress.addChild(downloadTask.progress, withPendingUnitCount: 3) downloadTask.resume() + self.printWithTid("download started: \(downloadURL)") } }