Skip to content

Filepath with space not recognized correctly by xdg-open #17682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/Avalonia.Base/Platform/Storage/FileIO/BclLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Avalonia.Compatibility;
using Avalonia.Metadata;

namespace Avalonia.Platform.Storage.FileIO;

Expand Down Expand Up @@ -44,7 +43,8 @@ private static bool Exec(string urlOrFile)
{
// If no associated application/json MimeType is found xdg-open opens return error
// but it tries to open it anyway using the console editor (nano, vim, other..)
ShellExec($"xdg-open {urlOrFile}", waitForExit: false);
var args = EscapeForShell(urlOrFile);
ShellExecRaw($"xdg-open \\\"{args}\\\"", waitForExit: false);
return true;
}
else if (OperatingSystemEx.IsWindows() || OperatingSystemEx.IsMacOS())
Expand All @@ -63,17 +63,18 @@ private static bool Exec(string urlOrFile)
return false;
}
}

private static void ShellExec(string cmd, bool waitForExit = true)

private static string EscapeForShell(string input) => Regex
.Replace(input, "(?=[`~!#&*()|;'<>])", "\\")
.Replace("\"", "\\\\\\\"");

private static void ShellExecRaw(string cmd, bool waitForExit = true)
{
var escapedArgs = Regex.Replace(cmd, "(?=[`~!#&*()|;'<>])", "\\")
.Replace("\"", "\\\\\\\"");

using (var process = Process.Start(
new ProcessStartInfo
{
FileName = "/bin/sh",
Arguments = $"-c \"{escapedArgs}\"",
Arguments = $"-c \"{cmd}\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
Expand Down
Loading