Skip to content
Open
Show file tree
Hide file tree
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
73 changes: 73 additions & 0 deletions GVFS/GVFS.Common/DevDriveFilterConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using GVFS.Common.Git;
using GVFS.Common.Tracing;
using System;

namespace GVFS.Common
{
public static class DevDriveFilterConfig
{
public static readonly TimeSpan ReadTimeout = TimeSpan.FromSeconds(5);

public static bool? ReadClientValue(
LibGit2Repo.TryReadConfigBoolWithGit readConfig,
ITracer tracer)
{
if (!TryReadValue(readConfig, "enlistment", tracer, out bool? value, out string _))
{
return false;
}

return value;
}

public static bool IsEnabled(
LibGit2Repo.TryReadConfigBoolWithGit readConfig,
bool? clientValue,
string source,
ITracer tracer,
out string disabledReason)
{
disabledReason = null;
if (!TryReadValue(readConfig, source, tracer, out bool? value, out disabledReason))
{
return false;
}

if (value == false || clientValue == false)
{
string disabledSource = value == false ? source : "enlistment";
disabledReason = $"Automatic filter allowance changes are disabled by {GVFSConstants.GitConfig.AutoAllowDevDriveFilter} in the {disabledSource} configuration.";
return false;
}

if ((value ?? GVFSConstants.GitConfig.AutoAllowDevDriveFilterDefault)
|| clientValue == true)
{
return true;
}

disabledReason = $"Automatic filter allowance changes are not enabled. Set {GVFSConstants.GitConfig.AutoAllowDevDriveFilter} to true to opt in.";
return false;
}

private static bool TryReadValue(
LibGit2Repo.TryReadConfigBoolWithGit readConfig,
string source,
ITracer tracer,
out bool? value,
out string error)
{
value = null;
error = "Git is unavailable.";
if (readConfig == null
|| !readConfig(GVFSConstants.GitConfig.AutoAllowDevDriveFilter, out value, out error))
{
error = $"Automatic filter allowance changes are disabled because the {source} configuration read failed. {error}";
tracer.RelatedWarning(error);
return false;
}

return true;
}
}
}
8 changes: 8 additions & 0 deletions GVFS/GVFS.Common/GVFSConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ public static class GitConfig

public const string PrefetchOffload = GVFSPrefix + "prefetch-offload";
public const bool PrefetchOffloadDefault = false;

/* Permits GVFS.Service to allow PrjFlt on the enlistment's Dev Drive after attachment
* is rejected. Default off: an explicit true value opts in to the policy update.
* The service reads its Git installation's system config (including includes) for each
* request. Either that value or the client's resolved value can veto a write.
* Disabling the flag does not remove a persisted allowance. */
public const string AutoAllowDevDriveFilter = GVFSPrefix + "auto-allow-devdrive-filter";
public const bool AutoAllowDevDriveFilterDefault = false;
}

public static class LocalGVFSConfig
Expand Down
43 changes: 43 additions & 0 deletions GVFS/GVFS.Common/Git/GitProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,49 @@ public bool TryGetBoolFromConfig(string settingName, out bool? value, out string
return LibGit2Repo.TryParseConfigBoolOutput(settingName, result.ExitCode, result.Output, result.Errors, out value, out error);
}

/// <summary>
/// Reads a boolean with Git within one deadline for all attempts and retry delays.
/// Returns null for an unset key and reports read errors without choosing a default.
/// </summary>
public bool TryGetBoolFromConfig(
string settingName,
GitConfigBoolScope scope,
TimeSpan timeout,
ITracer tracer,
out bool? value,
out string error)
{
tracer = tracer ?? NullTracer.Instance;
bool useRepository = scope == GitConfigBoolScope.Merged && this.workingDirectoryRoot != null;
if (useRepository && !Directory.Exists(this.workingDirectoryRoot))
{
value = null;
error = $"Cannot read '{settingName}': the enlistment directory is unavailable.";
tracer.RelatedWarning(error);
return false;
}

return GitProcessConfigReader.TryReadBool(
settingName,
scope,
(arguments, cancellation) => GitProcessConfigReader.ReadRaw(
settingName,
() => this.GetGitProcess(
arguments,
Environment.SystemDirectory,
useRepository ? this.gitDirPath : null,
useReadObjectHook: false,
gitObjectsDirectory: null,
usePreCommandHook: !useRepository),
string.Empty,
cancellation,
tracer),
timeout,
tracer,
out value,
out error);
}

public ConfigResult GetFromLocalConfig(string settingName)
{
// See GetFromConfig above for why pre-command hook is disabled here.
Expand Down
83 changes: 81 additions & 2 deletions GVFS/GVFS.Common/Git/GitProcessConfigReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,72 @@

namespace GVFS.Common.Git
{
public enum GitConfigBoolScope
{
Merged,
System,
}

internal static class GitProcessConfigReader
{
private const int MaxOutput = 65536;
private const int MaxErrors = 4096;

internal static bool TryReadBool(
string key,
GitConfigBoolScope scope,
Func<string, CancellationToken, GitProcess.Result> readConfig,
TimeSpan timeout,
ITracer tracer,
out bool? value,
out string error)
{
value = null;
error = null;
tracer = tracer ?? NullTracer.Instance;
if (scope != GitConfigBoolScope.System && scope != GitConfigBoolScope.Merged)
{
throw new ArgumentOutOfRangeException(nameof(scope));
}

string arguments = scope == GitConfigBoolScope.System
? "config --system --includes --type=bool " + key
: LibGit2Repo.GetConfigBoolArguments(key);

using (CancellationTokenSource deadline = new CancellationTokenSource(timeout))
{
try
{
GitProcess.Result result = readConfig(arguments, deadline.Token);
if (result.OutputTruncated || result.ErrorsTruncated)
{
error = $"Error while reading '{key}' from config: output was truncated.";
return false;
}

if (result.ExitCode == 1 && !string.IsNullOrWhiteSpace(result.Output))
{
error = $"Error while reading '{key}' from config: unexpected output for an unset value.";
return false;
}

return LibGit2Repo.TryParseConfigBoolOutput(key, result.ExitCode, result.Output, result.Errors, out value, out error);
}
catch (OperationCanceledException) when (deadline.IsCancellationRequested)
{
error = $"The Git configuration read for '{key}' timed out.";
tracer.RelatedWarning(error);
return false;
}
catch (Exception e) when (e is GitConfigProcessException || e is Win32Exception || e is IOException || e is UnauthorizedAccessException)
{
error = $"The Git configuration read for '{key}' failed: {e.Message}";
tracer.RelatedWarning($"{error} {e}");
return false;
}
}
}

// If the caller runs Git and returns a result, use Read for retries.
internal static GitProcess.Result Read(
string configName,
Expand Down Expand Up @@ -84,7 +145,8 @@ internal static GitProcess.Result ReadRaw(
ReadResultAsync,
() => process.HasExited,
() => process.Kill(entireProcessTree: true),
cancellation);
cancellation,
() => process.WaitForExit(5000));

async Task<GitProcess.Result> ReadResultAsync()
{
Expand Down Expand Up @@ -115,7 +177,8 @@ internal static GitProcess.Result ReadRaw(
Func<Task<GitProcess.Result>> read,
Func<bool> hasExited,
Action killTree,
CancellationToken cancellation)
CancellationToken cancellation,
Func<bool> waitForExit = null)
{
TaskCompletionSource<Exception> cancellationKill = new TaskCompletionSource<Exception>(TaskCreationOptions.RunContinuationsAsynchronously);
CancellationTokenRegistration registration = cancellation.Register(() =>
Expand Down Expand Up @@ -160,6 +223,22 @@ internal static GitProcess.Result ReadRaw(
{
killTree();
}

}
catch (Exception cleanupFailure) when (failure != null || readFailure != null)
{
failure = failure == null ? cleanupFailure : new AggregateException(failure, cleanupFailure);
}
}

if (waitForExit != null)
{
try
{
if (!hasExited() && !waitForExit())
{
throw new Win32Exception("The Git configuration process did not exit within the cleanup deadline.");
}
}
catch (Exception cleanupFailure) when (failure != null || readFailure != null)
{
Expand Down
12 changes: 12 additions & 0 deletions GVFS/GVFS.Common/NamedPipes/NamedPipeMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,18 @@ public class EnableAndAttachProjFSRequest

public string EnlistmentRoot { get; set; }

/// <summary>
/// The value of gvfs.auto-allow-devdrive-filter as resolved by the client against the
/// enlistment (system, global, and local git config). Null when the value is unset
/// or the client predates this field. A read failure is sent as false.
/// </summary>
/// <remarks>
/// The service also reads the system config of its selected Git installation.
/// An explicit true value opts in. A false value at either side vetoes the write.
/// See GVFSConstants.GitConfig.AutoAllowDevDriveFilter for the configuration contract.
/// </remarks>
public bool? AutoAllowDevDriveFilter { get; set; }

public static EnableAndAttachProjFSRequest FromMessage(Message message)
{
return GVFSJsonOptions.Deserialize<EnableAndAttachProjFSRequest>(message.Body);
Expand Down
Loading
Loading