Skip to content

Commit 9508266

Browse files
committed
Use Console.IsOutputRedirected instead of P/Invoke for redirect detection
The previous IsConsoleOutputRedirectedToFile() used Win32 GetFileType to check if stdout was redirected to a disk file. This missed the pipe case (GetFileType returns Pipe, not Disk), so the console spinner would still run when stdout was captured via Process.Start with RedirectStandardOutput (e.g. in functional tests and CI), polluting captured output with \r and spinner characters. Replace with .NET's Console.IsOutputRedirected which returns true for any non-console handle (files, pipes, NUL). Remove the now-unused P/Invoke declarations (GetStdHandle, GetFileType) and the platform abstraction (IsConsoleOutputRedirectedToFile) from GVFSPlatform, WindowsPlatform, GVFSHooksPlatform, and MockPlatform. Assisted-by: Claude Opus 4.6 Signed-off-by: Tyrie Vella <tyrielv@gmail.com>
1 parent 73e8e29 commit 9508266

8 files changed

Lines changed: 4 additions & 49 deletions

File tree

GVFS/GVFS.Common/GVFSPlatform.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ public static void Register(GVFSPlatform platform)
103103

104104
public abstract Dictionary<string, string> GetPhysicalDiskInfo(string path, bool sizeStatsOnly);
105105

106-
public abstract bool IsConsoleOutputRedirectedToFile();
107-
108106
public abstract bool TryKillProcessTree(int processId, out int exitCode, out string error);
109107

110108
public abstract bool TryGetGVFSEnlistmentRoot(string directory, out string enlistmentRoot, out string errorMessage);

GVFS/GVFS.Hooks/HooksPlatform/GVFSHooksPlatform.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ public static string GetNamedPipeName(string enlistmentRoot)
2222
return WindowsPlatform.GetNamedPipeNameImplementation(enlistmentRoot);
2323
}
2424

25-
public static bool IsConsoleOutputRedirectedToFile()
26-
{
27-
return WindowsPlatform.IsConsoleOutputRedirectedToFileImplementation();
28-
}
29-
3025
public static bool TryGetGVFSEnlistmentRoot(string directory, out string enlistmentRoot, out string errorMessage)
3126
{
3227
return WindowsPlatform.TryGetGVFSEnlistmentRootImplementation(directory, out enlistmentRoot, out errorMessage);

GVFS/GVFS.Hooks/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ private static void AcquireGVFSLockForProcess(bool unattended, string[] args, in
317317
fullCommand,
318318
pid,
319319
GVFSHooksPlatform.IsElevated(),
320-
isConsoleOutputRedirectedToFile: GVFSHooksPlatform.IsConsoleOutputRedirectedToFile(),
320+
isConsoleOutputRedirectedToFile: Console.IsOutputRedirected,
321321
checkAvailabilityOnly: checkGvfsLockAvailabilityOnly,
322322
gvfsEnlistmentRoot: null,
323323
gitCommandSessionId: gitCommandSessionId,
@@ -337,7 +337,7 @@ private static void ReleaseGVFSLock(bool unattended, string[] args, int pid, Nam
337337
fullCommand,
338338
pid,
339339
GVFSHooksPlatform.IsElevated(),
340-
GVFSHooksPlatform.IsConsoleOutputRedirectedToFile(),
340+
Console.IsOutputRedirected,
341341
response =>
342342
{
343343
if (response == null || response.ResponseData == null)

GVFS/GVFS.Platform.Windows/WindowsPlatform.Shared.cs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System;
44
using System.Diagnostics;
55
using System.IO;
6-
using System.Runtime.InteropServices;
76
using System.Security.Principal;
87

98
namespace GVFS.Platform.Windows
@@ -15,22 +14,6 @@ public partial class WindowsPlatform
1514

1615
private const int StillActive = 259; /* from Win32 STILL_ACTIVE */
1716

18-
private enum StdHandle
19-
{
20-
Stdin = -10,
21-
Stdout = -11,
22-
Stderr = -12
23-
}
24-
25-
private enum FileType : uint
26-
{
27-
Unknown = 0x0000,
28-
Disk = 0x0001,
29-
Char = 0x0002,
30-
Pipe = 0x0003,
31-
Remote = 0x8000,
32-
}
33-
3417
public static bool IsElevatedImplementation()
3518
{
3619
using (WindowsIdentity id = WindowsIdentity.GetCurrent())
@@ -153,11 +136,6 @@ public static string GetSecureDataRootForGVFSComponentImplementation(string comp
153136
return Path.Combine(GetSecureDataRootForGVFSImplementation(), componentName);
154137
}
155138

156-
public static bool IsConsoleOutputRedirectedToFileImplementation()
157-
{
158-
return FileType.Disk == GetFileType(GetStdHandle(StdHandle.Stdout));
159-
}
160-
161139
public static bool TryGetGVFSEnlistmentRootImplementation(string directory, out string enlistmentRoot, out string errorMessage)
162140
{
163141
enlistmentRoot = null;
@@ -177,11 +155,5 @@ public static bool TryGetGVFSEnlistmentRootImplementation(string directory, out
177155

178156
return true;
179157
}
180-
181-
[DllImport("kernel32.dll")]
182-
private static extern IntPtr GetStdHandle(StdHandle std);
183-
184-
[DllImport("kernel32.dll")]
185-
private static extern FileType GetFileType(IntPtr hdl);
186158
}
187159
}

GVFS/GVFS.Platform.Windows/WindowsPlatform.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,6 @@ public override string GetSystemInstallerLogPath()
331331

332332
public override Dictionary<string, string> GetPhysicalDiskInfo(string path, bool sizeStatsOnly) => WindowsPhysicalDiskInfo.GetPhysicalDiskInfo(path, sizeStatsOnly);
333333

334-
public override bool IsConsoleOutputRedirectedToFile()
335-
{
336-
return WindowsPlatform.IsConsoleOutputRedirectedToFileImplementation();
337-
}
338-
339334
public override bool IsGitStatusCacheSupported()
340335
{
341336
return File.Exists(Path.Combine(GVFSPlatform.Instance.GetSecureDataRootForGVFSComponent(GVFSConstants.Service.ServiceName), GVFSConstants.GitStatusCache.EnableGitStatusCacheTokenFile));

GVFS/GVFS.UnitTests/Mock/Common/MockPlatform.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,6 @@ public override string GetSystemInstallerLogPath()
131131
return "MockPath";
132132
}
133133

134-
public override bool IsConsoleOutputRedirectedToFile()
135-
{
136-
throw new NotSupportedException();
137-
}
138-
139134
public override bool IsElevated()
140135
{
141136
throw new NotSupportedException();

GVFS/GVFS/CommandLine/GVFSVerb.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ protected bool ShowStatusWhileRunning(
193193
action,
194194
message,
195195
this.Output,
196-
showSpinner: !this.Unattended && this.Output == Console.Out && !GVFSPlatform.Instance.IsConsoleOutputRedirectedToFile(),
196+
showSpinner: !this.Unattended && this.Output == Console.Out && !Console.IsOutputRedirected,
197197
gvfsLogEnlistmentRoot: gvfsLogEnlistmentRoot,
198198
initialDelayMs: 0);
199199
}

GVFS/GVFS/CommandLine/UnmountVerb.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ private void AcquireLock(string pipeName, string enlistmentRoot)
261261
"gvfs unmount",
262262
currentProcess.Id,
263263
GVFSPlatform.Instance.IsElevated(),
264-
isConsoleOutputRedirectedToFile: GVFSPlatform.Instance.IsConsoleOutputRedirectedToFile(),
264+
isConsoleOutputRedirectedToFile: Console.IsOutputRedirected,
265265
checkAvailabilityOnly: false,
266266
gvfsEnlistmentRoot: enlistmentRoot,
267267
gitCommandSessionId: string.Empty,

0 commit comments

Comments
 (0)