From 3e6ca7c673bd21ad101e7d59aa817e61c1252c92 Mon Sep 17 00:00:00 2001 From: nythepegasus Date: Wed, 8 May 2024 23:48:57 -0400 Subject: [PATCH] Limits quitting other AltStore/SideStore processes to database migrations --- AltStoreCore/Model/DatabaseManager.swift | 33 +++++++++++++----------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/AltStoreCore/Model/DatabaseManager.swift b/AltStoreCore/Model/DatabaseManager.swift index b91a7e8e..cdda104b 100644 --- a/AltStoreCore/Model/DatabaseManager.swift +++ b/AltStoreCore/Model/DatabaseManager.swift @@ -13,11 +13,11 @@ import Roxas extension CFNotificationName { - fileprivate static let willAccessDatabase = CFNotificationName("com.rileytestut.AltStore.WillAccessDatabase" as CFString) + fileprivate static let willMigrateDatabase = CFNotificationName("com.rileytestut.AltStore.WillMigrateDatabase" as CFString) } -private let ReceivedWillAccessDatabaseNotification: @convention(c) (CFNotificationCenter?, UnsafeMutableRawPointer?, CFNotificationName?, UnsafeRawPointer?, CFDictionary?) -> Void = { (center, observer, name, object, userInfo) in - DatabaseManager.shared.receivedWillAccessDatabaseNotification() +private let ReceivedWillMigrateDatabaseNotification: @convention(c) (CFNotificationCenter?, UnsafeMutableRawPointer?, CFNotificationName?, UnsafeRawPointer?, CFDictionary?) -> Void = { (center, observer, name, object, userInfo) in + DatabaseManager.shared.receivedWillMigrateDatabaseNotification() } fileprivate class PersistentContainer: RSTPersistentContainer @@ -52,15 +52,15 @@ public class DatabaseManager private let coordinator = NSFileCoordinator() private let coordinatorQueue = OperationQueue() - private var ignoreWillAccessDatabaseNotification = false - + private var ignoreWillMigrateDatabaseNotification = false + private init() { self.persistentContainer = PersistentContainer(name: "AltStore", bundle: Bundle(for: DatabaseManager.self)) self.persistentContainer.preferredMergePolicy = MergePolicy() let observer = Unmanaged.passUnretained(self).toOpaque() - CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), observer, ReceivedWillAccessDatabaseNotification, CFNotificationName.willAccessDatabase.rawValue, nil, .deliverImmediately) + CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), observer, ReceivedWillMigrateDatabaseNotification, CFNotificationName.willMigrateDatabase.rawValue, nil, .deliverImmediately) } } @@ -87,10 +87,13 @@ public extension DatabaseManager guard !self.isStarted else { return finish(nil) } - // Quit any other running AltStore processes to prevent concurrent database access during and after migration. - self.ignoreWillAccessDatabaseNotification = true - CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), .willAccessDatabase, nil, nil, true) - + if self.persistentContainer.isMigrationRequired { + + // Quit any other running AltStore processes to prevent concurrent database access during and after migration. + self.ignoreWillMigrateDatabaseNotification = true + CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), .willMigrateDatabase, nil, nil, true) + } + self.migrateDatabaseToAppGroupIfNeeded { (result) in switch result { @@ -417,13 +420,13 @@ private extension DatabaseManager } } - func receivedWillAccessDatabaseNotification() + func receivedWillMigrateDatabaseNotification() { - defer { self.ignoreWillAccessDatabaseNotification = false } - + defer { self.ignoreWillMigrateDatabaseNotification = false } + // Ignore notifications sent by the current process. - guard !self.ignoreWillAccessDatabaseNotification else { return } - + guard !self.ignoreWillMigrateDatabaseNotification else { return } + exit(104) } }