Skip to content

Commit

Permalink
Fix read and write of run-once record to it writes to `/Library/Prefe…
Browse files Browse the repository at this point in the history
…rences` and not to `/var/root/Library/Preferences`

Added logic to migrateLegacyPreferences if they exist (issue in initial 4.0 to 4.0.2 using UserDefaults which results in preferences being written to `/var/root/Library/Preferences` which is problematic for settings that all users might need to access.
  • Loading branch information
Bart Reardon committed Jul 1, 2023
1 parent e6d5139 commit 721d871
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
28 changes: 23 additions & 5 deletions Outset/Functions/FileUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//
// Created by Bart Reardon on 3/12/2022.
//
// swiftlint:disable large_tuple line_length
// swiftlint:disable large_tuple line_length force_cast file_length cyclomatic_complexity function_body_length

import Foundation
import CommonCrypto
Expand Down Expand Up @@ -112,9 +112,10 @@ func ensureWorkingFolders() {
}

func migrateLegacyPreferences() {
let newoldRootUserDefaults = "/var/root/Library/Preferences/io.macadmins.Outset.plist"
// shared folder should not contain any executable content, iterate and update as required
if checkFileExists(path: shareDirectory) {
writeLog("\(shareDirectory) exists. Migrating prefrences to user defaults", logLevel: .debug)
if checkFileExists(path: shareDirectory) || checkFileExists(path: newoldRootUserDefaults) {
writeLog("Legacy preferences exist. Migrating to user defaults", logLevel: .debug)

let legacyOutsetPreferencesFile = "\(shareDirectory)com.chilcote.outset.plist"
let legacyRootRunOncePlistFile = "com.github.outset.once.\(getConsoleUserInfo().userID).plist"
Expand All @@ -126,6 +127,7 @@ func migrateLegacyPreferences() {
shareFiles.append(legacyOutsetPreferencesFile)
shareFiles.append(legacyRootRunOncePlistFile)
shareFiles.append(legacyUserRunOncePlistFile)
shareFiles.append(newoldRootUserDefaults)

for filename in shareFiles where checkFileExists(path: filename) {

Expand All @@ -134,7 +136,22 @@ func migrateLegacyPreferences() {
let data = try Data(contentsOf: url)
switch filename {

case newoldRootUserDefaults:
if isRoot() {
writeLog("\(newoldRootUserDefaults) migration", logLevel: .debug)
let legacyDefaultKeys = CFPreferencesCopyKeyList(Bundle.main.bundleIdentifier! as CFString, kCFPreferencesCurrentUser, kCFPreferencesAnyHost)
for key in legacyDefaultKeys as! [CFString] {
let keyValue = CFPreferencesCopyValue(key, Bundle.main.bundleIdentifier! as CFString, kCFPreferencesCurrentUser, kCFPreferencesAnyHost)
CFPreferencesSetValue(key as CFString,
keyValue as CFPropertyList,
Bundle.main.bundleIdentifier! as CFString,
kCFPreferencesAnyUser,
kCFPreferencesAnyHost)
}
deletePath(newoldRootUserDefaults)
}
case legacyOutsetPreferencesFile:
writeLog("\(legacyOutsetPreferencesFile) migration", logLevel: .debug)
do {
let legacyPreferences = try PropertyListDecoder().decode(OutsetPreferences.self, from: data)
writePreferences(prefs: legacyPreferences)
Expand All @@ -145,6 +162,7 @@ func migrateLegacyPreferences() {
}

case legacyRootRunOncePlistFile, legacyUserRunOncePlistFile:
writeLog("\(legacyRootRunOncePlistFile) and \(legacyUserRunOncePlistFile) migration", logLevel: .debug)
do {
let legacyRunOncePlistData = try PropertyListDecoder().decode([String: Date].self, from: data)
writeRunOnce(runOnceData: legacyRunOncePlistData)
Expand All @@ -167,7 +185,7 @@ func migrateLegacyPreferences() {

}

if folderContents(path: shareDirectory).isEmpty {
if checkFileExists(path: shareDirectory) && folderContents(path: shareDirectory).isEmpty {
deletePath(shareDirectory)
}
}
Expand Down Expand Up @@ -393,4 +411,4 @@ extension URL {
}
}

// swiftlint:enable large_tuple line_length
// swiftlint:enable large_tuple line_length force_cast file_length cyclomatic_complexity function_body_length
1 change: 1 addition & 0 deletions Outset/Functions/Processing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func processItems(_ path: String, deleteItems: Bool=false, once: Bool=false, ove
let checksumsAvailable = !checksumList.isEmpty

// See if there's any old stuff to migrate
// Perform this each processing run to pick up individual user preferences as well
migrateLegacyPreferences()

// Get a list of all the files to process
Expand Down
12 changes: 10 additions & 2 deletions Outset/Functions/SystemUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,10 @@ func loadRunOnce() -> [String: Date] {

if isRoot() {
runOnceKey += "-"+getConsoleUserInfo().username
return CFPreferencesCopyValue(runOnceKey as CFString, Bundle.main.bundleIdentifier! as CFString, kCFPreferencesAnyUser, kCFPreferencesAnyHost) as? [String: Date] ?? [:]
} else {
return defaults.object(forKey: runOnceKey) as? [String: Date] ?? [:]
}
return defaults.object(forKey: runOnceKey) as? [String: Date] ?? [:]
}

func writeRunOnce(runOnceData: [String: Date]) {
Expand All @@ -204,8 +206,14 @@ func writeRunOnce(runOnceData: [String: Date]) {

if isRoot() {
runOnceKey += "-"+getConsoleUserInfo().username
CFPreferencesSetValue(runOnceKey as CFString,
runOnceData as CFPropertyList,
Bundle.main.bundleIdentifier! as CFString,
kCFPreferencesAnyUser,
kCFPreferencesAnyHost)
} else {
defaults.set(runOnceData, forKey: runOnceKey)
}
defaults.set(runOnceData, forKey: runOnceKey)
}

func showPrefrencePath(_ action: String) {
Expand Down

0 comments on commit 721d871

Please sign in to comment.