Move code to more appropriate files.

Partial #533

Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
This commit is contained in:
Ross Goldberg 2024-10-29 00:16:35 -04:00
parent e639341d11
commit 3d264675bf
No known key found for this signature in database
6 changed files with 50 additions and 31 deletions

View file

@ -32,12 +32,12 @@ extension MAS {
throw MASError.macOSUserMustBeRoot
}
guard let username = getSudoUsername() else {
guard let username = ProcessInfo.processInfo.sudoUsername else {
throw MASError.runtimeError("Could not determine the original username")
}
guard
let uid = getSudoUID(),
let uid = ProcessInfo.processInfo.sudoUID,
seteuid(uid) == 0
else {
throw MASError.runtimeError("Failed to switch effective user from 'root' to '\(username)'")

View file

@ -43,24 +43,6 @@ class SoftwareMapAppLibrary: AppLibrary {
}
}
func getSudoUsername() -> String? {
ProcessInfo.processInfo.environment["SUDO_USER"]
}
func getSudoUID() -> uid_t? {
guard let uid = ProcessInfo.processInfo.environment["SUDO_UID"] else {
return nil
}
return uid_t(uid)
}
func getSudoGID() -> gid_t? {
guard let gid = ProcessInfo.processInfo.environment["SUDO_GID"] else {
return nil
}
return gid_t(gid)
}
private func getOwnerAndGroupOfItem(atPath path: String) throws -> (uid_t, gid_t) {
do {
let attributes = try FileManager.default.attributesOfItem(atPath: path)
@ -75,11 +57,11 @@ private func getOwnerAndGroupOfItem(atPath path: String) throws -> (uid_t, gid_t
}
private func chown(paths: [String]) throws -> [String: (uid_t, gid_t)] {
guard let sudoUID = getSudoUID() else {
guard let sudoUID = ProcessInfo.processInfo.sudoUID else {
throw MASError.runtimeError("Failed to get original uid")
}
guard let sudoGID = getSudoGID() else {
guard let sudoGID = ProcessInfo.processInfo.sudoGID else {
throw MASError.runtimeError("Failed to get original gid")
}

View file

@ -7,7 +7,6 @@
//
import ArgumentParser
import Foundation
import PromiseKit
@main
@ -55,11 +54,3 @@ struct MAS: ParsableCommand {
Self.initialize()
}
}
typealias AppID = UInt64
extension NSNumber {
var appIDValue: AppID {
uint64Value
}
}

View file

@ -0,0 +1,17 @@
//
// AppID.swift
// mas
//
// Created by Ross Goldberg on 2024-10-29.
// Copyright © 2024 mas-cli. All rights reserved.
//
import Foundation
typealias AppID = UInt64
extension NSNumber {
var appIDValue: AppID {
uint64Value
}
}

View file

@ -0,0 +1,29 @@
//
// ProcessInfo.swift
// mas
//
// Created by Ross Goldberg on 2024-10-29.
// Copyright © 2024 mas-cli. All rights reserved.
//
import Foundation
extension ProcessInfo {
var sudoUsername: String? {
environment["SUDO_USER"]
}
var sudoUID: uid_t? {
guard let uid = environment["SUDO_UID"] else {
return nil
}
return uid_t(uid)
}
var sudoGID: gid_t? {
guard let gid = environment["SUDO_GID"] else {
return nil
}
return gid_t(gid)
}
}