Skip to content

Commit 305c10c

Browse files
committed
Improve logging and other small tweaks to watcher mode
1 parent cedde99 commit 305c10c

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

ExtendFS Extension (ext4)/Ext4Volume.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ final class Ext4Volume: FSVolume, FSVolume.Operations, FSVolume.PathConfOperatio
413413
config.activates = false
414414
config.addsToRecentItems = false
415415
config.hides = true
416-
try? await NSWorkspace.shared.open([URL(string: "extendfs-internal-diskwatch:/dev/\(resource.bsdName)")!], withApplicationAt: appURL, configuration: config)
416+
let _ = try? await NSWorkspace.shared.open([URL(string: "extendfs-internal-diskwatch:/dev/\(resource.bsdName)")!], withApplicationAt: appURL, configuration: config)
417417
}
418418

419419
return await cache.root

ExtendFS.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@
314314
CLANG_WARN_UNREACHABLE_CODE = YES;
315315
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
316316
COPY_PHASE_STRIP = NO;
317-
CURRENT_PROJECT_VERSION = 8;
317+
CURRENT_PROJECT_VERSION = 11;
318318
DEAD_CODE_STRIPPING = YES;
319319
DEBUG_INFORMATION_FORMAT = dwarf;
320320
DEVELOPMENT_TEAM = YN43D87323;
@@ -382,7 +382,7 @@
382382
CLANG_WARN_UNREACHABLE_CODE = YES;
383383
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
384384
COPY_PHASE_STRIP = NO;
385-
CURRENT_PROJECT_VERSION = 8;
385+
CURRENT_PROJECT_VERSION = 11;
386386
DEAD_CODE_STRIPPING = YES;
387387
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
388388
DEVELOPMENT_TEAM = YN43D87323;

ExtendFS/DiskWatcher.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import Foundation
55
import AppKit
66
import DiskArbitration
7+
import os.log
8+
9+
fileprivate let logger = Logger(subsystem: "com.kpchew.ExtendFS", category: "WatcherModeDelegate")
710

811
/// An object that watches a disk and keeps the process alive until it is unmounted.
912
///
@@ -21,6 +24,7 @@ class DiskWatcher {
2124
self.session = session
2225

2326
guard let disk = DADiskCreateFromBSDName(kCFAllocatorDefault, session, blockDevice) else {
27+
logger.log("Couldn't create disk from BSD name \(blockDevice)")
2428
return nil
2529
}
2630
let cfDesc = DADiskCopyDescription(disk) as! [String: Any]
@@ -32,6 +36,7 @@ class DiskWatcher {
3236
]
3337

3438
DARegisterDiskUnmountApprovalCallback(session, filter as CFDictionary, { (recvDisk, context) in
39+
logger.log("Disk is trying to unmount, exiting")
3540
Task { @MainActor in
3641
exit(0)
3742
}
@@ -58,6 +63,7 @@ class DiskWatcher {
5863

5964
if let description = DADiskCopyDescription(disk) as? [CFString: Any], let bsd = description[kDADiskDescriptionMediaBSDNameKey] as? String {
6065
// mark as unmounted so it can be tried to be mounted again later
66+
logger.log("Creating marker for unmounted disk")
6167
let empty = Data()
6268
try? empty.write(to: URL.temporaryDirectory.appending(component: "unmounted-\(bsd)"))
6369
}

ExtendFS/ExtendFSApp.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ struct ExtendFSApp: App {
2323

2424
init() {
2525
// handle unmounted disks that were previously unmounted during a quit event, probably indicating an App Store update relaunch
26-
27-
if let contents = try? FileManager.default.contentsOfDirectory(at: URL.temporaryDirectory, includingPropertiesForKeys: nil) {
26+
if let contents = try? FileManager.default.contentsOfDirectory(at: URL.temporaryDirectory, includingPropertiesForKeys: [.creationDateKey]) {
2827
let bsdList = contents
2928
.filter({ $0.lastPathComponent.starts(with: "unmounted-") })
3029
guard bsdList.count > 0 else {
@@ -46,6 +45,11 @@ struct ExtendFSApp: App {
4645
logger.error("Couldn't create disk from BSD name \(bsd, privacy: .public)")
4746
}
4847

48+
if let creationDate = try? name.resourceValues(forKeys: [.creationDateKey]).creationDate, abs(creationDate.timeIntervalSinceNow) > 60 * 10 {
49+
logger.log("Disk was marked as unmounted more than 10 minutes ago, which is strange. Will open GUI as a fallback")
50+
couldCleanAllItems = false
51+
}
52+
4953
do {
5054
try FileManager.default.removeItem(at: name)
5155
} catch {
@@ -66,6 +70,8 @@ struct ExtendFSApp: App {
6670
.onOpenURL { url in
6771
guard !delegate.wasStartedForGUI, url.scheme == "extendfs-internal-diskwatch" else { return }
6872

73+
logger.log("Got request to monitor disk \(url.lastPathComponent, privacy: .public)")
74+
6975
dismissWindow(id: "main")
7076
let devNode = url.path(percentEncoded: false)
7177
NSApp.hide(nil)
@@ -87,9 +93,17 @@ struct ExtendFSApp: App {
8793
}
8894
do {
8995
try Task.checkCancellation()
96+
logger.log("Normal app launch")
9097
delegate.wasStartedForGUI = true
9198
NSApp.setActivationPolicy(.regular)
9299
NSApp.activate()
100+
if !NSApp.isActive {
101+
// weird workaround since LSUIElement is set to avoid dock flickering, but that causes the window to not be activated by default
102+
// either this will activate the GUI, or a background process which is setup to then activate the GUI
103+
let appURL = Bundle.main.bundleURL
104+
let config = NSWorkspace.OpenConfiguration()
105+
NSWorkspace.shared.openApplication(at: appURL, configuration: config)
106+
}
93107
} catch {}
94108
}
95109
}

ExtendFS/WatcherModeDelegate.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import AppKit
55
import DiskArbitration
66
import Foundation
7+
import os.log
8+
9+
fileprivate let logger = Logger(subsystem: "com.kpchew.ExtendFS", category: "WatcherModeDelegate")
710

811
/// A delegate that creates a pseudo-headless mode for the app to run in when it is in disk monitoring mode.
912
///
@@ -16,7 +19,7 @@ class WatcherModeDelegate: NSObject, NSApplicationDelegate {
1619
var diskWatcher: DiskWatcher?
1720

1821
func getGUIApp() -> NSRunningApplication? {
19-
let runningApps = NSRunningApplication.runningApplications(withBundleIdentifier: "com.kpchew.ExtendFS")
22+
let runningApps = NSRunningApplication.runningApplications(withBundleIdentifier: Bundle.main.bundleIdentifier!)
2023
for app in runningApps {
2124
if app.activationPolicy != .prohibited {
2225
return app
@@ -31,24 +34,32 @@ class WatcherModeDelegate: NSObject, NSApplicationDelegate {
3134

3235
if hasFirstActivated {
3336
// user opened app manually but background listener was activated, open a new GUI app
37+
logger.log("Received activation request in background mode")
3438
NSApp.deactivate()
3539
NSApp.hide(nil)
3640
NSApplication.shared.setActivationPolicy(.prohibited)
3741
if let app = getGUIApp() {
42+
logger.log("GUI app found, activating it")
3843
app.activate()
3944
} else {
45+
logger.log("No GUI app found, launching one")
4046
let appURL = Bundle.main.bundleURL
4147
let config = NSWorkspace.OpenConfiguration()
4248
config.createsNewApplicationInstance = true
4349
config.activates = true
44-
NSWorkspace.shared.openApplication(at: appURL, configuration: config)
50+
Task {
51+
let app = try? await NSWorkspace.shared.openApplication(at: appURL, configuration: config)
52+
app?.activate()
53+
}
54+
4555
}
4656
}
4757
}
4858

4959
func applicationWillTerminate(_ notification: Notification) {
5060
guard !wasStartedForGUI else { return }
5161
// something told the app to quit, try to cleanly unmount because this is probably an update
62+
logger.log("Received request to terminate, trying to unmounting disk")
5263
diskWatcher?.unmount()
5364
}
5465

0 commit comments

Comments
 (0)