Skip to content
Merged
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
23 changes: 20 additions & 3 deletions GVFS/GVFS.FunctionalTests/FileSystemRunners/BashRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;

namespace GVFS.FunctionalTests.FileSystemRunners
Expand All @@ -28,11 +29,16 @@ public class BashRunner : ShellRunner
"Function not implemented"
};

private static string[] permissionDeniedMessage = new string[]
private static string[] windowsPermissionDeniedMessage = new string[]
{
"Permission denied"
};

private static string[] macPermissionDeniedMessage = new string[]
{
"Resource temporarily unavailable"
};

private readonly string pathToBash;

public BashRunner()
Expand Down Expand Up @@ -108,7 +114,7 @@ public override void MoveFileShouldFail(string sourcePath, string targetPath)
{
// BashRunner does nothing special when a failure is expected, so just confirm source file is still present
this.MoveFile(sourcePath, targetPath);
this.FileExists(sourcePath).ShouldEqual(true);
this.FileExists(sourcePath).ShouldBeTrue($"{sourcePath} does not exist when it should");
}

public override void MoveFile_FileShouldNotBeFound(string sourcePath, string targetPath)
Expand Down Expand Up @@ -246,7 +252,8 @@ public override void DeleteFile_FileShouldNotBeFound(string path)

public override void DeleteFile_AccessShouldBeDenied(string path)
{
this.DeleteFile(path).ShouldContain(permissionDeniedMessage);
Comment thread
wilbaker marked this conversation as resolved.
this.DeleteFile(path).ShouldContain(this.GetPermissionDeniedError());
this.FileExists(path).ShouldBeTrue($"{path} does not exist when it should");
}

public override void ReadAllText_FileShouldNotBeFound(string path)
Expand All @@ -272,5 +279,15 @@ private string ConvertWinPathToBashPath(string winPath)
bashPath = bashPath.Replace('\\', '/');
return bashPath;
}

private string[] GetPermissionDeniedError()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return windowsPermissionDeniedMessage;
}

return macPermissionDeniedMessage;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ public override void DeleteFile_FileShouldNotBeFound(string path)
public override void DeleteFile_AccessShouldBeDenied(string path)
{
this.DeleteFile(path).ShouldContain(permissionDeniedMessage);
this.FileExists(path).ShouldBeTrue($"{path} does not exist when it should");
}

public override void ReadAllText_FileShouldNotBeFound(string path)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NUnit.Framework;
using GVFS.Tests.Should;
using NUnit.Framework;
using System;
using System.Diagnostics;
using System.IO;
Expand Down Expand Up @@ -61,6 +62,7 @@ public override void DeleteFile_FileShouldNotBeFound(string path)
public override void DeleteFile_AccessShouldBeDenied(string path)
{
this.ShouldFail<Exception>(() => { this.DeleteFile(path); });
this.FileExists(path).ShouldBeTrue($"{path} does not exist when it should");
}

public override string ReadAllText(string path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,6 @@ public void MoveDotGitFullFolderTreeToDotGitFullFolder(FileSystemRunner fileSyst
}

[TestCaseSource(typeof(FileSystemRunner), FileSystemRunner.TestRunners)]
[Category(Categories.MacTODO.M4)]
public void DeleteIndexFileFails(FileSystemRunner fileSystem)
{
string indexFilePath = this.Enlistment.GetVirtualPathTo(Path.Combine(".git", "index"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ private enum MoveFileFlags : uint
}

[TestCase]
[Category(Categories.MacTODO.M2)]
public void GitCheckoutFailsOutsideLock()
{
const string BackupPrefix = "BACKUP_";
Expand Down Expand Up @@ -65,32 +64,32 @@ public void GitCheckoutFailsOutsideLock()
}

[TestCase]
[Category(Categories.MacTODO.M4)]
public void LockPreventsRenameFromOutsideRootOnTopOfIndex()
{
this.OverwritingIndexShouldFail(Path.Combine(this.Enlistment.EnlistmentRoot, "LockPreventsRenameFromOutsideRootOnTopOfIndex.txt"));
}

[TestCase]
[Category(Categories.MacTODO.M4)]
public void LockPreventsRenameFromInsideWorkingTreeOnTopOfIndex()
{
this.OverwritingIndexShouldFail(this.Enlistment.GetVirtualPathTo("LockPreventsRenameFromInsideWorkingTreeOnTopOfIndex.txt"));
}

[TestCase]
[Category(Categories.MacTODO.M4)]
public void LockPreventsRenameOfIndexLockOnTopOfIndex()
{
this.OverwritingIndexShouldFail(this.Enlistment.GetVirtualPathTo(".git", "index.lock"));
}

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool MoveFileEx(
[DllImport("kernel32.dll", EntryPoint = "MoveFileEx", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool WindowsMoveFileEx(
string existingFileName,
string newFileName,
uint flags);

[DllImport("libc", EntryPoint = "rename", SetLastError = true)]
private static extern int MacRename(string oldPath, string newPath);

private void OverwritingIndexShouldFail(string testFilePath)
{
string indexPath = this.Enlistment.GetVirtualPathTo(".git", "index");
Expand All @@ -102,15 +101,28 @@ private void OverwritingIndexShouldFail(string testFilePath)
this.fileSystem.WriteAllText(testFilePath, testFileContents);

this.Enlistment.WaitForBackgroundOperations().ShouldEqual(true, "Background operations failed to complete.");
MoveFileEx(
testFilePath,
indexPath,
(uint)(MoveFileFlags.MoveFileReplaceExisting | MoveFileFlags.MoveFileCopyAllowed)).ShouldBeFalse("GVFS should prevent renaming on top of index when GVFSLock is not held");

this.RenameAndOverwrite(testFilePath, indexPath).ShouldBeFalse("GVFS should prevent renaming on top of index when GVFSLock is not held");
byte[] newIndexContents = File.ReadAllBytes(indexPath);

indexContents.SequenceEqual(newIndexContents).ShouldBeTrue("Index contenst should not have changed");

this.fileSystem.DeleteFile(testFilePath);
}

private bool RenameAndOverwrite(string oldPath, string newPath)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
Comment thread
wilbaker marked this conversation as resolved.
{
return WindowsMoveFileEx(
oldPath,
newPath,
(uint)(MoveFileFlags.MoveFileReplaceExisting | MoveFileFlags.MoveFileCopyAllowed));
}
else
{
return MacRename(oldPath, newPath) == 0;
}
}
}
}
18 changes: 18 additions & 0 deletions GVFS/GVFS.Platform.Mac/MacFileSystemVirtualizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class MacFileSystemVirtualizer : FileSystemVirtualizer
{
public static readonly byte[] PlaceholderVersionId = ToVersionIdByteArray(new byte[] { PlaceholderVersion });

private const string ClassName = nameof(MacFileSystemVirtualizer);

private VirtualizationInstance virtualizationInstance;

public MacFileSystemVirtualizer(GVFSContext context, GVFSGitObjects gitObjects)
Expand All @@ -32,6 +34,8 @@ public MacFileSystemVirtualizer(
this.virtualizationInstance = virtualizationInstance ?? new VirtualizationInstance();
}

protected override string EtwArea => ClassName;

public static FSResult ResultToFSResult(Result result)
{
switch (result)
Expand Down Expand Up @@ -330,6 +334,20 @@ private Result OnPreDelete(string relativePath, bool isDirectory)
bool pathInsideDotGit = Virtualization.FileSystemCallbacks.IsPathInsideDotGit(relativePath);
if (pathInsideDotGit)
{
if (relativePath.Equals(GVFSConstants.DotGit.Index, StringComparison.OrdinalIgnoreCase))
{
string lockedGitCommand = this.Context.Repository.GVFSLock.GetLockedGitCommand();
if (string.IsNullOrEmpty(lockedGitCommand))
{
EventMetadata metadata = new EventMetadata();
metadata.Add("Area", this.EtwArea);
metadata.Add(TracingConstants.MessageKey.WarningMessage, "Blocked index delete outside the lock");
this.Context.Tracer.RelatedEvent(EventLevel.Warning, $"{nameof(OnPreDelete)}_BlockedIndexDelete", metadata);

return Result.EAccessDenied;
}
}

this.OnDotGitFileOrFolderDeleted(relativePath);
}
else
Expand Down
8 changes: 1 addition & 7 deletions GVFS/GVFS.Platform.Windows/WindowsFileSystemVirtualizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,7 @@ public WindowsFileSystemVirtualizer(
this.activeCommands = new ConcurrentDictionary<int, CancellationTokenSource>();
}

protected override string EtwArea
{
get
{
return ClassName;
}
}
protected override string EtwArea => ClassName;

/// <remarks>
/// Public for unit testing
Expand Down