Skip to content

Commit

Permalink
Turns out, explicit NSError does not crash
Browse files Browse the repository at this point in the history
  • Loading branch information
halo committed Jul 7, 2017
1 parent 13df4e7 commit 08cd5a0
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 37 deletions.
8 changes: 4 additions & 4 deletions LinkHelper/Classes/ConfigDirectory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ struct ConfigDirectory {
do {
try manager.removeItem(atPath: Paths.configDirectory)
Log.debug("Deleted config directory \(Paths.configDirectory)")
} catch {
Log.info("Could not delete config directory \(Paths.configDirectory) \(error.localizedDescription)")
} catch let error as NSError {
Log.info("Could not delete config directory \(Paths.configDirectory) \(error)")
}
}

private static func ensureDirectory() {
do {
try manager.createDirectory(atPath: Paths.configDirectory, withIntermediateDirectories: false)
Log.debug("Created config directory \(Paths.configDirectory)")
} catch {
} catch let error as NSError {
Log.info("Could not create config directory \(Paths.configDirectory) does it already exist? \(error.localizedDescription)")
}
}
Expand All @@ -50,7 +50,7 @@ struct ConfigDirectory {
do {
try manager.setAttributes(directoryPermissions, ofItemAtPath: Paths.configDirectory)
Log.debug("Set permissions of config directory at \(Paths.configDirectory) to \(directoryPermissions)")
} catch {
} catch let error as NSError {
Log.info("Could not set permissions for config directory \(error.localizedDescription)")
}
}
Expand Down
4 changes: 2 additions & 2 deletions LinkHelper/Classes/HelperProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ import Foundation
protocol HelperProtocol {
func version(reply: (String) -> Void)

func install(pristineExecutableURL: URL, reply: (Bool) -> Void)
func install(pristineDaemonExecutablePath: String, reply: (Bool) -> Void)
func uninstall(reply: (Bool) -> Void)

func createConfigDirectory(reply: (Bool) -> Void)
func removeConfigDirectory(reply: (Bool) -> Void)

func installDaemon(pristineExecutableURL: URL, reply: (Bool) -> Void)
func installDaemon(pristineDaemonExecutablePath: String, reply: (Bool) -> Void)
func activateDaemon(reply: (Bool) -> Void)
func deactivateDaemon(reply: (Bool) -> Void)
func uninstallDaemon(reply: (Bool) -> Void)
Expand Down
15 changes: 8 additions & 7 deletions LinkHelper/Classes/InstallDaemon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ struct InstallDaemon {
"KeepAlive": true
]

static func perform(pristineExecutableURL: URL) {
static func perform(pristineExecutablePath: String) {
createPlist()
ensureDirectory()
ensureDirectoryPermissions()
copyPristineExecutable(pristineExecutableURL: pristineExecutableURL)
copyPristineExecutable(pristineExecutablePath: pristineExecutablePath)
ensureExecutablePermissions()
}

Expand All @@ -52,7 +52,7 @@ struct InstallDaemon {
do {
try manager.createDirectory(atPath: Paths.daemonDirectory, withIntermediateDirectories: false)
Log.debug("Created daemon directory \(Paths.daemonDirectory)")
} catch {
} catch let error as NSError {
Log.info("Could not create daemon directory \(Paths.daemonDirectory) does it already exist? \(error.localizedDescription)")
}
}
Expand All @@ -61,17 +61,18 @@ struct InstallDaemon {
do {
try manager.setAttributes(directoryPermissions, ofItemAtPath: Paths.daemonDirectory)
Log.debug("Ensured permissions of daemon directory at \(Paths.daemonDirectory) to \(directoryPermissions)")
} catch {
} catch let error as NSError {
Log.info("Could not set permissions for daemon directory \(error.localizedDescription)")
}
}

private static func copyPristineExecutable(pristineExecutableURL: URL) {
private static func copyPristineExecutable(pristineExecutablePath: String) {
let pristineExecutableURL = URL(fileURLWithPath: pristineExecutablePath)
Log.debug("Copying daemon executable from \(pristineExecutableURL) to \(Paths.daemonExecutable)")
do {
try manager.copyItem(at: pristineExecutableURL, to: Paths.daemonExecutableURL)
Log.debug("Copied daemon executable from \(pristineExecutableURL) to \(Paths.daemonExecutable)")
} catch {
} catch let error as NSError {
Log.info("Could not copy daemon executable from \(pristineExecutableURL) to \(Paths.daemonExecutable) does it already exist? \(error.localizedDescription)")
}
}
Expand All @@ -80,7 +81,7 @@ struct InstallDaemon {
do {
try manager.setAttributes(executablePermissions, ofItemAtPath: Paths.daemonExecutable)
Log.debug("Set permissions of daemon executable at \(Paths.daemonExecutable) to \(executablePermissions)")
} catch {
} catch let error as NSError {
Log.info("Could not set permissions for daemon executable \(error.localizedDescription)")
}
}
Expand Down
11 changes: 6 additions & 5 deletions LinkHelper/Classes/LinkHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ extension LinkHelper: HelperProtocol {
reply(LinkHelper.version.formatted)
}

func install(pristineExecutableURL: URL, reply: (Bool) -> Void) {
func install(pristineDaemonExecutablePath: String, reply: (Bool) -> Void) {
ConfigDirectory.create()
UninstallDaemon.perform()
InstallDaemon.perform(pristineExecutableURL: pristineExecutableURL)
InstallDaemon.perform(pristineExecutablePath: pristineDaemonExecutablePath)
reply(BootDaemon.bootstrap())
}

Expand All @@ -81,10 +81,10 @@ extension LinkHelper: HelperProtocol {
reply(true)
}

func installDaemon(pristineExecutableURL: URL, reply: (Bool) -> Void) {
func installDaemon(pristineDaemonExecutablePath: String, reply: (Bool) -> Void) {
Log.debug("Going to prophylactically uninstall and then install daemon...")
UninstallDaemon.perform()
InstallDaemon.perform(pristineExecutableURL: pristineExecutableURL)
InstallDaemon.perform(pristineExecutablePath: pristineDaemonExecutablePath)
reply(BootDaemon.bootstrap())
}

Expand All @@ -97,8 +97,9 @@ extension LinkHelper: HelperProtocol {
}

func uninstallDaemon(reply: (Bool) -> Void) {
let success = BootDaemon.bootout()
UninstallDaemon.perform()
reply(BootDaemon.bootout())
reply(success)
}

func uninstallHelper(reply: (Bool) -> Void) {
Expand Down
4 changes: 2 additions & 2 deletions LinkHelper/Classes/UninstallDaemon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ struct UninstallDaemon {
private static func removePlist() {
do {
try FileManager.default.removeItem(atPath: Paths.daemonPlistFile)
} catch {
} catch let error as NSError {
Log.error("Could not delete Daemon plist at \(Paths.daemonPlistFile) is it there? \(error.localizedDescription)")
}
}

private static func removeDirectory() {
do {
try FileManager.default.removeItem(atPath: Paths.daemonDirectory)
} catch {
} catch let error as NSError {
Log.info("Could not delete Daemon directory at \(Paths.daemonDirectory) is it there? \(error.localizedDescription)")
}
}
Expand Down
4 changes: 2 additions & 2 deletions LinkHelper/Classes/UninstallHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ struct UninstallHelper {
private static func removePlist() {
do {
try FileManager.default.removeItem(atPath: Paths.helperPlistFile)
} catch {
} catch let error as NSError {
Log.error("Could not delete Helper plist at \(Paths.helperPlistFile) is it there? \(error.localizedDescription)")
}
}

private static func removeExecutable() {
do {
try FileManager.default.removeItem(atPath: Paths.helperExecutable)
} catch {
} catch let error as NSError {
Log.info("Could not delete Helper executable at \(Paths.helperExecutable) is it there? \(error.localizedDescription)")
}
}
Expand Down
12 changes: 6 additions & 6 deletions LinkLiar.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
C2340D9E1F0EB1120065F034 /* UninstallDaemon.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = UninstallDaemon.swift; path = Classes/UninstallDaemon.swift; sourceTree = "<group>"; };
C2340DA11F0EB11B0065F034 /* ConfigDirectory.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ConfigDirectory.swift; path = Classes/ConfigDirectory.swift; sourceTree = "<group>"; };
C2340D9E1F0EB1120065F034 /* UninstallDaemon.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; name = UninstallDaemon.swift; path = Classes/UninstallDaemon.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
C2340DA11F0EB11B0065F034 /* ConfigDirectory.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; name = ConfigDirectory.swift; path = Classes/ConfigDirectory.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
C23926491EEDB84E008506E4 /* index.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; name = index.html; path = LinkLiarHelp/index.html; sourceTree = "<group>"; };
C239264B1EEDBBB4008506E4 /* Interface.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Interface.swift; path = LinkTools/Interface.swift; sourceTree = "<group>"; };
C239264E1EEDCD73008506E4 /* MACAddress.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MACAddress.swift; path = LinkTools/MACAddress.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -203,18 +203,18 @@
C23926C31F06D04E008506E4 /* oui.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = oui.json; sourceTree = "<group>"; };
C24A486E1F090EDF0095ED0C /* OuiPrefixes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = OuiPrefixes.swift; path = Classes/OuiPrefixes.swift; sourceTree = "<group>"; };
C24A48701F0937390095ED0C /* RandomMACs.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = RandomMACs.swift; path = Classes/RandomMACs.swift; sourceTree = "<group>"; };
C291CBE61EE85D0D00154452 /* Log.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Log.swift; path = LinkTools/Log.swift; sourceTree = "<group>"; };
C291CBF51EE8827D00154452 /* JSONReader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = JSONReader.swift; path = LinkTools/JSONReader.swift; sourceTree = "<group>"; };
C291CBE61EE85D0D00154452 /* Log.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; name = Log.swift; path = LinkTools/Log.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
C291CBF51EE8827D00154452 /* JSONReader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; name = JSONReader.swift; path = LinkTools/JSONReader.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
C29B7D941EE69D1800609A21 /* io.github.halo.linkhelper */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = io.github.halo.linkhelper; sourceTree = BUILT_PRODUCTS_DIR; };
C29B7D961EE69D1800609A21 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
C29B7D9E1EE71AC000609A21 /* LinkHelper-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "LinkHelper-Info.plist"; sourceTree = "<group>"; };
C29B7D9F1EE71AC000609A21 /* LinkHelper-Launchd.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "LinkHelper-Launchd.plist"; sourceTree = "<group>"; };
C2BE6B991F0D011D0086A843 /* NSMenuItem+placeholder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSMenuItem+placeholder.swift"; sourceTree = "<group>"; };
C2BE6B9B1F0D523F0086A843 /* ConfigurationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConfigurationTests.swift; sourceTree = "<group>"; };
C2BE6B9D1F0D791B0086A843 /* LaunchCtl.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LaunchCtl.swift; sourceTree = "<group>"; };
C2BE6B9F1F0E47770086A843 /* UninstallHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = UninstallHelper.swift; path = Classes/UninstallHelper.swift; sourceTree = "<group>"; };
C2BE6B9F1F0E47770086A843 /* UninstallHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; name = UninstallHelper.swift; path = Classes/UninstallHelper.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
C2BE6BA31F0E8D4C0086A843 /* BootDaemon.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = BootDaemon.swift; path = Classes/BootDaemon.swift; sourceTree = "<group>"; };
C2BE6BA51F0EA4B60086A843 /* InstallDaemon.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = InstallDaemon.swift; path = Classes/InstallDaemon.swift; sourceTree = "<group>"; };
C2BE6BA51F0EA4B60086A843 /* InstallDaemon.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; name = InstallDaemon.swift; path = Classes/InstallDaemon.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
C2BEBFD91EE48B5E0072BE59 /* LinkLiar.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = LinkLiar.app; sourceTree = BUILT_PRODUCTS_DIR; };
C2BEBFE01EE48B5E0072BE59 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
C2BEBFE51EE48B5E0072BE59 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down
9 changes: 5 additions & 4 deletions LinkLiar/Classes/Intercom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Intercom: NSObject {

static func install(reply: @escaping (Bool) -> Void) {
usingHelper(block: { helper in
helper.install(pristineExecutableURL: Paths.daemonPristineExecutableURL, reply: { success in
helper.install(pristineDaemonExecutablePath: Paths.daemonPristineExecutablePath, reply: { success in
Log.debug("Helper worked on installation")
reply(success)
})
Expand Down Expand Up @@ -84,7 +84,7 @@ class Intercom: NSObject {
static func installDaemon(reply: @escaping (Bool) -> Void) {
Log.debug("Asking Helper to install daemon")
usingHelper(block: { helper in
helper.installDaemon(pristineExecutableURL: Paths.daemonPristineExecutableURL, reply: { success in
helper.installDaemon(pristineDaemonExecutablePath: Paths.daemonPristineExecutablePath, reply: { success in
Log.debug("Helper worked on the establishment of the daemon")
reply(success)
})
Expand Down Expand Up @@ -140,15 +140,16 @@ class Intercom: NSObject {
self.xpcConnection?.interruptionHandler = nil
OperationQueue.main.addOperation(){
self.xpcConnection = nil
Log.debug("XPC Connection interrupted\n")
Log.debug("XPC Connection interrupted - the Helper probably crashed.")
Log.debug("You mght find a crash report at /Library/Logs/DiagnosticReports")
}
}

self.xpcConnection!.invalidationHandler = {
self.xpcConnection?.invalidationHandler = nil
OperationQueue.main.addOperation(){
self.xpcConnection = nil
Log.debug("XPC Connection Invalidated\n")
Log.debug("XPC Connection Invalidated")
}
}

Expand Down
4 changes: 2 additions & 2 deletions LinkTools/JSONReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class JSONReader {
Log.debug("Content: \(result)")
Log.debug("Successfuly read it")
return result
} catch {
} catch let error as NSError {
Log.debug(error.localizedDescription)
return nil
}
Expand All @@ -63,7 +63,7 @@ class JSONReader {
} else {
Log.debug("JSON is not a Key-Value object")
}
} catch {
} catch let error as NSError {
Log.error(error.localizedDescription)
}
return [String: Any]()
Expand Down
2 changes: 1 addition & 1 deletion LinkTools/Log.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public struct Log {
#if DEBUG
do {
try data.write(to: Paths.debugLogFileURL)
} catch {}
} catch let error as NSError {}
// There is no logfile, which means the end-user does not want file logging
#endif
*/
Expand Down
4 changes: 2 additions & 2 deletions LinkTools/Paths.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ class Paths {
static let helperPlistFile = daemonsPlistDirectory.appendPath(Identifiers.helper.rawValue + ".plist")
static let helperPlistFileURL = URL(fileURLWithPath: helperPlistFile)

static var daemonPristineExecutable: String {
static var daemonPristineExecutablePath: String {
guard let url = Bundle.main.url(forResource: "linkdaemon", withExtension: nil) else {
Log.error("Missing linkdaemon executable in LinkLiar.app Bundle at \(Bundle.main.resourcePath ?? "?")")
return "/dev/null"
}
return url.path
}
static var daemonPristineExecutableURL = URL(fileURLWithPath: daemonPristineExecutable)
static var daemonPristineExecutableURL = URL(fileURLWithPath: daemonPristineExecutablePath)

static let daemonDirectory = "/Library/Application Support/LinkDaemon"
static let daemonDirectoryURL = URL(fileURLWithPath: daemonDirectory)
Expand Down

0 comments on commit 08cd5a0

Please sign in to comment.