Skip to content
Prev Previous commit
Next Next commit
special case directories to work on Unix
  • Loading branch information
SteveL-MSFT committed Jul 26, 2017
commit 1a3134c4f199adb02fb3f0a6b264e82e4beaa642
Original file line number Diff line number Diff line change
Expand Up @@ -1323,12 +1323,38 @@ protected override void InvokeDefaultAction(string path)

string resource = StringUtil.Format(FileSystemProviderStrings.InvokeItemResourceFileTemplate, path);

#if UNIX
void StartProcessWithOpen(string filename)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
// The file is possibly not an executable, so we try invoking the default program that handles this file.
const string quoteFormat = "\"{0}\"";
process.StartInfo.FileName = Platform.IsLinux ? "xdg-open" : /* OS X */ "open";
if (NativeCommandParameterBinder.NeedQuotes(filename))
{
path = string.Format(CultureInfo.InvariantCulture, quoteFormat, filename);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check NeedQuotes? Maybe do that for every filename?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NeedQuotes check is needed only if you are specifying arguments for ProcessInfo, for example, the argument string may have space in it.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we could remove the 'if' and each name quoted - "filename.txt", "file name.txt".

}
process.StartInfo.Arguments = filename;
// xdg-open on Ubuntu outputs debug info to stderr, suppress it
process.StartInfo.RedirectStandardError = true;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if xdg-open or open fails to open a file? In that case, user won't see anything after running Invoke-Item and will suspect that Invoke-Item doesn't work.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's probably true. I guess we'll just have to have the debug spew coming out.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What error an user see if run the xdg-open or open from command line?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the same stderr if run directly

process.Start();
}
#endif

if (ShouldProcess(resource, action))
{
System.Diagnostics.Process invokeProcess = new System.Diagnostics.Process();

try
{
// special case directories on UNIX so that we can open them in the window-manager
// corefx doesn't return error if you try to start a directory
#if UNIX
if (Directory.Exists(path))
{
StartProcessWithOpen(path);
}
#endif
// Try Process.Start first.
// - In FullCLR, this is all we need to do.
// - In CoreCLR, this works for executables on Win/Unix platforms
Expand All @@ -1339,15 +1365,7 @@ protected override void InvokeDefaultAction(string path)
catch (Win32Exception ex) when (ex.NativeErrorCode == 13)
{
// Error code 13 -- Permission denied.
// The file is possibly not an executable, so we try invoking the default program that handles this file.
const string quoteFormat = "\"{0}\"";
invokeProcess.StartInfo.FileName = Platform.IsLinux ? "xdg-open" : /* OS X */ "open";
if (NativeCommandParameterBinder.NeedQuotes(path))
{
path = string.Format(CultureInfo.InvariantCulture, quoteFormat, path);
}
invokeProcess.StartInfo.Arguments = path;
invokeProcess.Start();
StartProcessWithOpen(path);
}
#elif CORECLR
catch (Win32Exception ex) when (ex.NativeErrorCode == 193 || ex.NativeErrorCode == 5)
Expand Down