Skip to content

Commit

Permalink
Fix #78: Microsoft Store Apps fail to elevate if command line has quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
gerardog committed Jul 30, 2021
1 parent 04d7220 commit 1d81d28
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
26 changes: 13 additions & 13 deletions src/gsudo/Helpers/ArgumentsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ Running ./gsudo {command} should elevate the powershell command.
else if (currentShell.In(Shell.PowerShell, Shell.PowerShellCore))
{
var newArgs = new List<string>();
DoFixIfIsMicrosoftStoreApp(currentShellExeName, newArgs);


newArgs.Add($"\"{currentShellExeName}\"");
newArgs.Add("-NoLogo");

Expand All @@ -61,8 +60,8 @@ Running ./gsudo {command} should elevate the powershell command.
newArgs.Add("-Command");
newArgs.AddMany(args);
}

return newArgs.ToArray();

return DoFixIfIsMicrosoftStoreApp(currentShellExeName, newArgs.ToArray());
}

if (currentShell == Shell.Yori)
Expand Down Expand Up @@ -99,17 +98,14 @@ Running ./gsudo {command} should elevate the powershell command.
}
else
{
var newArgs = new List<string>();
DoFixIfIsMicrosoftStoreApp(exename, newArgs);

newArgs.Add($"\"{exename}\""); // replace command name with full+absolute+quoted path to resolve many issues, like "Batch files not started by create process if no extension is specified."
newArgs.AddRange(args.Skip(1));
args[0] = $"\"{exename}\"";
var newArgs = DoFixIfIsMicrosoftStoreApp(exename, args);
return newArgs.ToArray();
}
}
}

private static void DoFixIfIsMicrosoftStoreApp(string targetExe, List<string> newArgs)
private static string[] DoFixIfIsMicrosoftStoreApp(string targetExe, string[] args)
{
// -- Workaround for https://github.com/gerardog/gsudo/issues/65

Expand All @@ -122,10 +118,14 @@ private static void DoFixIfIsMicrosoftStoreApp(string targetExe, List<string> ne

if (targetExe.IndexOf("\\WindowsApps\\", StringComparison.OrdinalIgnoreCase) >= 0) // Terrible but cheap Microsoft Store App detection.
{
Logger.Instance.Log("Workaround for target app installed via MSStore.", LogLevel.Debug);
newArgs.Add(Environment.GetEnvironmentVariable("COMSPEC"));
newArgs.Add("/c");
Logger.Instance.Log("Applying workaround for target app installed via MSStore.", LogLevel.Debug);
return new string[] {
Environment.GetEnvironmentVariable("COMSPEC"),
"/s /c" ,
$"\"{string.Join(" ", args)}\""};
}
else
return args;
// -- End of workaround.
}

Expand Down
5 changes: 0 additions & 5 deletions src/gsudo/Logger.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Diagnostics;
using System.IO;

namespace gsudo
{
Expand All @@ -16,7 +14,6 @@ public enum LogLevel

class Logger
{
public static int ProcessId = System.Diagnostics.Process.GetCurrentProcess().Id;
public static readonly Logger Instance = new Logger();

private Logger() { }
Expand All @@ -31,8 +28,6 @@ public void Log(string message, LogLevel level)
Console.Error.WriteLine($"{level.ToString()}: {message}");
Console.ResetColor();
}

//File.AppendAllText("C:\\test\\gsudolog.txt", $"{ProcessId}\t{level.ToString()}: {message}\r\n");
}
catch { }
}
Expand Down

0 comments on commit 1d81d28

Please sign in to comment.