forked from twocanoes/xcreds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateFileHelper.swift
More file actions
104 lines (78 loc) · 2.73 KB
/
StateFileHelper.swift
File metadata and controls
104 lines (78 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
// RunFileHelper.swift
// XCreds
//
// Created by Timothy Perfitt on 11/27/24.
//
import Foundation
class StateFileHelper {
enum StateFileHelperError:Error {
case FileCreationError
}
enum StateFileType {
case returnType
case delayType
case fileVaultLogin
}
func paths(_ fileType:StateFileType) -> (folderPath:String, filePath:String){
var folderPath=""
var filePath=""
switch fileType {
case .returnType:
folderPath = "/usr/local/var/"
filePath = "xcreds_return"
case .delayType:
folderPath = "/usr/local/var/"
filePath = "xcreds_delay"
case .fileVaultLogin:
folderPath = "/Library/Application Support/XCreds/statefile/"
filePath = "xcreds_filevaultlogin"
}
return (folderPath, filePath)
}
func createFile(_ fileType:StateFileType) throws {
TCSLogWithMark()
let (folderPath, filePath) = paths(fileType)
var attributes = [FileAttributeKey : Any]()
attributes[.posixPermissions] = 0o770
attributes[.ownerAccountID] = 92
attributes[.groupOwnerAccountID] = 0
if FileManager.default.fileExists(atPath: folderPath)==false {
try FileManager.default.createDirectory(atPath: folderPath, withIntermediateDirectories: true, attributes:attributes)
}
attributes[.posixPermissions] = 0o660
if FileManager.default.createFile(atPath: folderPath+filePath, contents: nil, attributes: attributes)==false {
throw StateFileHelperError.FileCreationError
}
}
func fileExists(_ fileType:StateFileType) -> Bool {
TCSLogWithMark()
let (folderPath, filePath) = paths(fileType)
let fullPath = folderPath + filePath
return FileManager.default.fileExists(atPath: fullPath)
}
func removeFile(_ fileType:StateFileType) throws {
TCSLogWithMark()
let (folderPath, filePath) = paths(fileType)
let fullPath = folderPath + filePath
if FileManager.default.fileExists(atPath: fullPath){
return try FileManager.default.removeItem(atPath: fullPath)
}
}
func killOrReboot(){
if UserDefaults.standard.bool(forKey:PrefKeys.shouldUseKillWhenLoginWindowSwitching.rawValue)==true{
TCSLogWithMark("killing loginwindow")
do {
try createFile(.delayType)
}
catch {
TCSLog("could not create delay file")
}
let _ = cliTask("/usr/bin/killall loginwindow")
}
else {
TCSLogWithMark("Reboot")
let _ = cliTask("/sbin/reboot")
}
}
}