Ensure the owner of launch agents dir is current user.

This commit is contained in:
Qiu Yuzhou
2017-08-26 00:46:33 +08:00
parent 948f7d4691
commit af73e0e3ca
3 changed files with 40 additions and 0 deletions

View File

@ -50,12 +50,36 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
var statusItem: NSStatusItem!
static let StatusItemIconWidth: CGFloat = NSVariableStatusItemLength
func ensureLaunchAgentsDirOwner () {
let dirPath = NSHomeDirectory() + "/Library/LaunchAgents"
let fileMgr = FileManager.default
if fileMgr.fileExists(atPath: dirPath) {
do {
let attrs = try fileMgr.attributesOfItem(atPath: dirPath)
if attrs[FileAttributeKey.ownerAccountName] as! String != NSUserName() {
//try fileMgr.setAttributes([FileAttributeKey.ownerAccountName: NSUserName()], ofItemAtPath: dirPath)
let bashFilePath = Bundle.main.path(forResource: "fix_dir_owner.sh", ofType: nil)!
let script = "do shell script \"bash \(bashFilePath) \(NSUserName()) \" with administrator privileges"
if let appleScript = NSAppleScript(source: script) {
var err: NSDictionary? = nil
appleScript.executeAndReturnError(&err)
}
}
}
catch {
NSLog("Error when ensure the owner of $HOME/Library/LaunchAgents, \(error.localizedDescription)")
}
}
}
func applicationDidFinishLaunching(_ aNotification: Notification) {
_ = LaunchAtLoginController()// Ensure set when launch
NSUserNotificationCenter.default.delegate = self
self.ensureLaunchAgentsDirOwner()
// Prepare ss-local
InstallSSLocal()
InstallKcptunClient()

View File

@ -0,0 +1,12 @@
#!/bin/sh
# fix_dir_owner.sh
# ShadowsocksX-NG
#
# Created by 邱宇舟 on 2017/8/25.
# Copyright © 2017年 qiuyuzhou. All rights reserved.
LAUNCH_AGENTS_DIR="$HOME/Library/LaunchAgents"
sudo chown $@ "$HOME/Library/LaunchAgents"