Remove SetupHelper class

This commit is contained in:
Lauri Kenttä
2023-09-02 16:13:06 +03:00
parent 8301a16ca8
commit 1fa53f3f06
3 changed files with 86 additions and 105 deletions

View File

@@ -9,7 +9,7 @@ GNUEFI_LIB = /usr/$(CC_PREFIX)/lib
FILES_C = src/main.c src/util.c src/types.c src/config.c
FILES_H = $(wildcard src/*.h)
FILES_CS = src/Setup.cs src/SetupHelper.cs src/Esp.cs src/Efi.cs
FILES_CS = src/Setup.cs src/Esp.cs src/Efi.cs
GIT_DESCRIBE = $(firstword $(shell git describe --tags) unknown)
CFLAGS += '-DGIT_DESCRIBE=L"$(GIT_DESCRIBE)"'
ZIPDIR = HackBGRT-$(GIT_DESCRIBE:v%=%)

View File

@@ -6,11 +6,13 @@ using System.Linq;
using System.Reflection;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Security.Principal;
/**
* HackBGRT Setup.
*/
public class Setup: SetupHelper {
public class Setup {
/** @var Version of the setup program. */
const string Version =
#if GIT_DESCRIBE
@@ -73,6 +75,82 @@ public class Setup: SetupHelper {
/** @var Run in batch mode? */
protected bool Batch;
/**
* Start another process.
*
* @param app Path to the application.
* @param args The argument string.
* @return The started process.
*/
public static Process StartProcess(string app, string args) {
try {
var info = new ProcessStartInfo(app, args);
info.UseShellExecute = false;
return Process.Start(info);
} catch {
return null;
}
}
/**
* Execute another process, return the output.
*
* @param app Path to the application.
* @param args The argument string.
* @param nullOnFail If set, return null if the program exits with a code other than 0, even if there was some output.
* @return The output, or null if the execution failed.
*/
public static string Execute(string app, string args, bool nullOnFail) {
try {
var info = new ProcessStartInfo(app, args);
info.UseShellExecute = false;
info.RedirectStandardOutput = true;
var p = Process.Start(info);
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
return (nullOnFail && p.ExitCode != 0) ? null : output;
} catch {
return null;
}
}
/**
* Check for required privileges (access to ESP and EFI vars).
*
* @return True, if the file was successfully copied.
*/
public static bool HasPrivileges() {
try {
var id = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(id);
var admin = WindowsBuiltInRole.Administrator;
// FIXME: Check access to ESP as well.
if (!principal.IsInRole(admin)) {
return false;
}
Efi.EnablePrivilege();
return true;
} catch {
}
return false;
}
/**
* Run another process as admin, return the exit code.
*
* @param app Path to the application.
* @param args The argument string.
*/
public static int RunElevated(string app, string args) {
ProcessStartInfo startInfo = new ProcessStartInfo(app);
startInfo.Arguments = args;
startInfo.Verb = "runas";
startInfo.UseShellExecute = true;
Process p = Process.Start(startInfo);
p.WaitForExit();
return p.ExitCode;
}
/**
* Find or mount or manually choose the EFI System Partition.
*/
@@ -159,7 +237,9 @@ public class Setup: SetupHelper {
* @return true if the replacement was successful.
*/
public bool ReplaceWith(BootLoaderInfo other) {
if (!other.Exists || !Copy(other.Path, Path)) {
try {
File.Copy(other.Path, Path, true);
} catch {
return false;
}
Exists = other.Exists;
@@ -174,7 +254,9 @@ public class Setup: SetupHelper {
*/
protected void InstallFile(string name, string newName = null) {
newName = Path.Combine(InstallPath, newName == null ? name : newName);
if (!Copy(name, newName)) {
try {
File.Copy(name, newName, true);
} catch {
throw new SetupException($"Failed to install file {name} to {newName}.");
}
Console.WriteLine($"Installed {name} to {newName}.");

View File

@@ -1,101 +0,0 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Security.Principal;
/**
* Helper functions.
*/
public class SetupHelper {
/**
* Start another process.
*
* @param app Path to the application.
* @param args The argument string.
* @return The started process.
*/
public static Process StartProcess(string app, string args) {
try {
var info = new ProcessStartInfo(app, args);
info.UseShellExecute = false;
return Process.Start(info);
} catch {
return null;
}
}
/**
* Execute another process, return the output.
*
* @param app Path to the application.
* @param args The argument string.
* @param nullOnFail If set, return null if the program exits with a code other than 0, even if there was some output.
* @return The output, or null if the execution failed.
*/
public static string Execute(string app, string args, bool nullOnFail) {
try {
var info = new ProcessStartInfo(app, args);
info.UseShellExecute = false;
info.RedirectStandardOutput = true;
var p = Process.Start(info);
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
return (nullOnFail && p.ExitCode != 0) ? null : output;
} catch {
return null;
}
}
/**
* Copy a file, overwrite by default.
*
* @param src The source path.
* @param dest The destination path.
* @return True, if the file was successfully copied.
*/
public static bool Copy(string src, string dest) {
try {
File.Copy(src, dest, true);
return true;
} catch {
return false;
}
}
/**
* Check for required privileges (access to ESP and EFI vars).
*
* @return True, if the file was successfully copied.
*/
public static bool HasPrivileges() {
try {
var id = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(id);
var admin = WindowsBuiltInRole.Administrator;
// FIXME: Check access to ESP as well.
if (!principal.IsInRole(admin)) {
return false;
}
Efi.EnablePrivilege();
return true;
} catch {
}
return false;
}
/**
* Run another process as admin, return the exit code.
*
* @param app Path to the application.
* @param args The argument string.
*/
public static int RunElevated(string app, string args) {
ProcessStartInfo startInfo = new ProcessStartInfo(app);
startInfo.Arguments = args;
startInfo.Verb = "runas";
startInfo.UseShellExecute = true;
Process p = Process.Start(startInfo);
p.WaitForExit();
return p.ExitCode;
}
}