Files
SideStore/SideStoreApp/Sources/SideStoreAppKit/Operations/RemoveAppBackupOperation.swift

69 lines
2.3 KiB
Swift
Raw Normal View History

//
// 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
@objc(RemoveAppBackupOperation)
2023-03-01 00:48:36 -05:00
final class RemoveAppBackupOperation: ResultOperation<Void> {
let context: InstallAppOperationContext
2023-03-01 00:48:36 -05:00
private let coordinator = NSFileCoordinator()
private let coordinatorQueue = OperationQueue()
2023-03-01 00:48:36 -05:00
init(context: InstallAppOperationContext) {
self.context = context
2023-03-01 00:48:36 -05:00
super.init()
2023-03-01 00:48:36 -05:00
coordinatorQueue.name = "AltStore - RemoveAppBackupOperation Queue"
}
2023-03-01 00:48:36 -05:00
override func main() {
super.main()
2023-03-01 00:48:36 -05:00
if let error = context.error {
finish(.failure(error))
return
}
2023-03-01 00:48:36 -05:00
guard let installedApp = context.installedApp else { return finish(.failure(OperationError.invalidParameters)) }
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
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 {
throw error
}
2023-03-01 00:48:36 -05:00
try FileManager.default.removeItem(at: intent.url)
2023-03-01 00:48:36 -05:00
self.finish(.success(()))
2023-03-01 00:48:36 -05:00
} catch let error as CocoaError where error.code == CocoaError.Code.fileNoSuchFile {
#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(()))
#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))
#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)
self.finish(.failure(error))
}
}
}
}
}