2020-05-16 15:28:22 -07:00
|
|
|
//
|
|
|
|
|
// RemoveAppBackupOperation.swift
|
|
|
|
|
// AltStore
|
|
|
|
|
//
|
|
|
|
|
// Created by Riley Testut on 5/13/20.
|
|
|
|
|
// Copyright © 2020 Riley Testut. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
2023-03-02 00:40:11 -05:00
|
|
|
import os.log
|
2020-05-16 15:28:22 -07:00
|
|
|
|
|
|
|
|
@objc(RemoveAppBackupOperation)
|
2023-03-01 00:48:36 -05:00
|
|
|
final class RemoveAppBackupOperation: ResultOperation<Void> {
|
2020-05-16 15:28:22 -07:00
|
|
|
let context: InstallAppOperationContext
|
2023-03-01 00:48:36 -05:00
|
|
|
|
2020-05-16 15:28:22 -07:00
|
|
|
private let coordinator = NSFileCoordinator()
|
|
|
|
|
private let coordinatorQueue = OperationQueue()
|
2023-03-01 00:48:36 -05:00
|
|
|
|
|
|
|
|
init(context: InstallAppOperationContext) {
|
2020-05-16 15:28:22 -07:00
|
|
|
self.context = context
|
2023-03-01 00:48:36 -05:00
|
|
|
|
2020-05-16 15:28:22 -07:00
|
|
|
super.init()
|
2023-03-01 00:48:36 -05:00
|
|
|
|
|
|
|
|
coordinatorQueue.name = "AltStore - RemoveAppBackupOperation Queue"
|
2020-05-16 15:28:22 -07:00
|
|
|
}
|
2023-03-01 00:48:36 -05:00
|
|
|
|
|
|
|
|
override func main() {
|
2020-05-16 15:28:22 -07:00
|
|
|
super.main()
|
2023-03-01 00:48:36 -05:00
|
|
|
|
|
|
|
|
if let error = context.error {
|
|
|
|
|
finish(.failure(error))
|
2020-05-16 15:28:22 -07:00
|
|
|
return
|
|
|
|
|
}
|
2023-03-01 00:48:36 -05:00
|
|
|
|
|
|
|
|
guard let installedApp = context.installedApp else { return finish(.failure(OperationError.invalidParameters)) }
|
2020-05-16 15:28:22 -07:00
|
|
|
installedApp.managedObjectContext?.perform {
|
|
|
|
|
guard let backupDirectoryURL = FileManager.default.backupDirectoryURL(for: installedApp) else { return self.finish(.failure(OperationError.missingAppGroup)) }
|
2023-03-01 00:48:36 -05:00
|
|
|
|
2020-05-16 15:28:22 -07:00
|
|
|
let intent = NSFileAccessIntent.writingIntent(with: backupDirectoryURL, options: [.forDeleting])
|
2023-03-01 00:48:36 -05:00
|
|
|
self.coordinator.coordinate(with: [intent], queue: self.coordinatorQueue) { error in
|
|
|
|
|
do {
|
|
|
|
|
if let error = error {
|
2020-05-16 15:28:22 -07:00
|
|
|
throw error
|
|
|
|
|
}
|
2023-03-01 00:48:36 -05:00
|
|
|
|
2020-05-16 15:28:22 -07:00
|
|
|
try FileManager.default.removeItem(at: intent.url)
|
2023-03-01 00:48:36 -05:00
|
|
|
|
2020-05-16 15:28:22 -07:00
|
|
|
self.finish(.success(()))
|
2023-03-01 00:48:36 -05:00
|
|
|
} catch let error as CocoaError where error.code == CocoaError.Code.fileNoSuchFile {
|
2020-05-16 15:28:22 -07:00
|
|
|
#if DEBUG
|
2023-03-01 00:48:36 -05:00
|
|
|
|
|
|
|
|
// When debugging, it's expected that app groups don't match, so ignore.
|
|
|
|
|
self.finish(.success(()))
|
|
|
|
|
|
2020-05-16 15:28:22 -07:00
|
|
|
#else
|
2023-03-01 00:48:36 -05:00
|
|
|
|
2023-03-02 00:40:11 -05:00
|
|
|
os_log("Failed to remove app backup directory: %@", type: .error , error.localizedDescription)
|
2023-03-01 00:48:36 -05:00
|
|
|
self.finish(.failure(error))
|
|
|
|
|
|
2020-05-16 15:28:22 -07:00
|
|
|
#endif
|
2023-03-01 00:48:36 -05:00
|
|
|
} catch {
|
2023-03-02 00:40:11 -05:00
|
|
|
os_log("Failed to remove app backup directory: %@", type: .error , error.localizedDescription)
|
2020-05-16 15:28:22 -07:00
|
|
|
self.finish(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|