Skip to content
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

Mac: Support projecting symbolic links #298

Merged
merged 14 commits into from
Oct 2, 2018
Merged
Show file tree
Hide file tree
Changes from 7 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
4 changes: 2 additions & 2 deletions GVFS/GVFS.Common/FileSystem/PhysicalFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ public virtual void DeleteDirectory(string path, bool recursive = false)
RecursiveDelete(path);
}

public virtual bool IsSymlink(string path)
public virtual bool IsSymLink(string path)
{
return (this.GetAttributes(path) & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint && NativeMethods.IsSymlink(path);
return (this.GetAttributes(path) & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint && NativeMethods.IsSymLink(path);
}

public virtual IEnumerable<DirectoryItemInfo> ItemsInDirectory(string path)
Expand Down
1 change: 1 addition & 0 deletions GVFS/GVFS.Common/Git/GVFSGitObjects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public enum RequestSource
FileStreamCallback,
GVFSVerb,
NamedPipeMessage,
SymLinkCreation,
}

protected GVFSContext Context { get; private set; }
Expand Down
2 changes: 1 addition & 1 deletion GVFS/GVFS.Common/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public static uint GetWindowsBuildNumber()
return versionInfo.BuildNumber;
}

public static bool IsSymlink(string path)
public static bool IsSymLink(string path)
{
using (SafeFileHandle output = CreateFile(
path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,14 +367,14 @@ private void PerformIOBeforePlaceholderDatabaseUpgradeTest()
this.fileSystem.DeleteDirectory(Path.Combine(this.Enlistment.RepoRoot, "GVFS\\GVFS.Tests\\Properties"));

string junctionTarget = Path.Combine(this.Enlistment.EnlistmentRoot, "DirJunction");
string symlinkTarget = Path.Combine(this.Enlistment.EnlistmentRoot, "DirSymlink");
string symLinkTarget = Path.Combine(this.Enlistment.EnlistmentRoot, "DirSymLink");
Directory.CreateDirectory(junctionTarget);
Directory.CreateDirectory(symlinkTarget);
Directory.CreateDirectory(symLinkTarget);

string junctionLink = Path.Combine(this.Enlistment.RepoRoot, "DirJunction");
string symlink = Path.Combine(this.Enlistment.RepoRoot, "DirLink");
string symLink = Path.Combine(this.Enlistment.RepoRoot, "DirLink");
ProcessHelper.Run("CMD.exe", "/C mklink /J " + junctionLink + " " + junctionTarget);
ProcessHelper.Run("CMD.exe", "/C mklink /D " + symlink + " " + symlinkTarget);
ProcessHelper.Run("CMD.exe", "/C mklink /D " + symLink + " " + symLinkTarget);

string target = Path.Combine(this.Enlistment.EnlistmentRoot, "GVFS", "GVFS", "GVFS.UnitTests");
string link = Path.Combine(this.Enlistment.RepoRoot, "UnitTests");
Expand Down
56 changes: 46 additions & 10 deletions GVFS/GVFS.FunctionalTests/FileSystemRunners/BashRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ public BashRunner()
}
}

private enum FileType
wilbaker marked this conversation as resolved.
Show resolved Hide resolved
{
Invalid,
File,
Directory,
SymLink,
}

protected override string FileName
{
get
Expand Down Expand Up @@ -86,15 +94,22 @@ public static void DeleteDirectoryWithUnlimitedRetries(string path)
}
}

public override bool FileExists(string path)
public bool IsSymbolicLink(string path)
wilbaker marked this conversation as resolved.
Show resolved Hide resolved
{
string bashPath = this.ConvertWinPathToBashPath(path);
return this.FileExistsOnDisk(path, FileType.SymLink);
}

string command = string.Format("-c \"[ -f {0} ] && echo {1} || echo {2}\"", bashPath, ShellRunner.SuccessOutput, ShellRunner.FailureOutput);
public void CreateSymbolicLink(string newLinkFilePath, string existingFilePath)
{
string existingFileBashPath = this.ConvertWinPathToBashPath(existingFilePath);
string newLinkBashPath = this.ConvertWinPathToBashPath(newLinkFilePath);

string output = this.RunProcess(command).Trim();
this.RunProcess(string.Format("-c \"ln -s -F {0} {1}\"", existingFileBashPath, newLinkBashPath));
}

return output.Equals(ShellRunner.SuccessOutput, StringComparison.InvariantCulture);
public override bool FileExists(string path)
{
return this.FileExistsOnDisk(path, FileType.File);
}

public override string MoveFile(string sourcePath, string targetPath)
Expand Down Expand Up @@ -187,11 +202,7 @@ public override void WriteAllTextShouldFail<ExceptionType>(string path, string c

public override bool DirectoryExists(string path)
{
string bashPath = this.ConvertWinPathToBashPath(path);

string output = this.RunProcess(string.Format("-c \"[ -d {0} ] && echo {1} || echo {2}\"", bashPath, ShellRunner.SuccessOutput, ShellRunner.FailureOutput)).Trim();

return output.Equals(ShellRunner.SuccessOutput, StringComparison.InvariantCulture);
return this.FileExistsOnDisk(path, FileType.Directory);
}

public override void MoveDirectory(string sourcePath, string targetPath)
Expand Down Expand Up @@ -267,6 +278,31 @@ public override void DeleteDirectory_ShouldBeBlockedByProcess(string path)
Assert.Fail("Unlike the other runners, bash.exe does not check folder handle before recusively deleting");
}

private bool FileExistsOnDisk(string path, FileType type)
{
string checkArgument = string.Empty;
switch (type)
{
case FileType.File:
checkArgument = "-f";
break;
case FileType.Directory:
checkArgument = "-d";
break;
case FileType.SymLink:
checkArgument = "-h";
break;
default:
Assert.Fail($"{nameof(FileExistsOnDisk)} does not support {nameof(FileType)} {type}");
break;
}

string bashPath = this.ConvertWinPathToBashPath(path);
string command = $"-c \"[ {checkArgument} {bashPath} ] && echo {ShellRunner.SuccessOutput} || echo {ShellRunner.FailureOutput}\"";
string output = this.RunProcess(command).Trim();
return output.Equals(ShellRunner.SuccessOutput, StringComparison.InvariantCulture);
}

private string ConvertWinPathToBashPath(string winPath)
{
string bashPath = string.Concat("/", winPath);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
using System.IO;
using GVFS.FunctionalTests.FileSystemRunners;
using GVFS.FunctionalTests.Should;
using GVFS.FunctionalTests.Tools;
using GVFS.Tests.Should;
using NUnit.Framework;

namespace GVFS.FunctionalTests.Tests.EnlistmentPerFixture
{
// MacOnly until issue #297 (add SymLink support for Windows) is complete
[Category(Categories.MacOnly)]
[TestFixture]
public class SymbolicLinkTests : TestsWithEnlistmentPerFixture
wilbaker marked this conversation as resolved.
Show resolved Hide resolved
{
private const string TestFolderName = "Test_EPF_SymbolicLinks";

// FunctionalTests/20180925_SymLinksPart1 files
private const string TestFileName = "TestFile.txt";
private const string TestFileContents = "This is a real file";
private const string TestFile2Name = "TestFile2.txt";
private const string TestFile2Contents = "This is the second real file";
private const string ChildFolderName = "ChildDir";
private const string ChildLinkName = "LinkToFileInFolder";
private const string GrandChildLinkName = "LinkToFileInParentFolder";

// FunctionalTests/20180925_SymLinksPart2 files
// Note: In this branch ChildLinkName has been changed to point to TestFile2Name
private const string GrandChildFileName = "TestFile3.txt";
private const string GrandChildFileContents = "This is the third file";
private const string GrandChildLinkNowAFileContents = "This was a link but is now a file";

private BashRunner bashRunner;
public SymbolicLinkTests()
{
this.bashRunner = new BashRunner();
}

[TestCase, Order(1)]
public void CheckoutBranchWithSymLinks()
{
GitHelpers.InvokeGitAgainstGVFSRepo(this.Enlistment.RepoRoot, "checkout FunctionalTests/20180925_SymLinksPart1");
GitHelpers.CheckGitCommandAgainstGVFSRepo(
this.Enlistment.RepoRoot,
"status",
"On branch FunctionalTests/20180925_SymLinksPart1",
"nothing to commit, working tree clean");

string testFilePath = this.Enlistment.GetVirtualPathTo(Path.Combine(TestFolderName, TestFileName));
testFilePath.ShouldBeAFile(this.bashRunner).WithContents(TestFileContents);
this.bashRunner.IsSymbolicLink(testFilePath).ShouldBeFalse($"{testFilePath} should not be a symlink");

string testFile2Path = this.Enlistment.GetVirtualPathTo(Path.Combine(TestFolderName, TestFile2Name));
testFile2Path.ShouldBeAFile(this.bashRunner).WithContents(TestFile2Contents);
this.bashRunner.IsSymbolicLink(testFile2Path).ShouldBeFalse($"{testFile2Path} should not be a symlink");

string childLinkPath = this.Enlistment.GetVirtualPathTo(Path.Combine(TestFolderName, ChildLinkName));
this.bashRunner.IsSymbolicLink(childLinkPath).ShouldBeTrue($"{childLinkPath} should be a symlink");
childLinkPath.ShouldBeAFile(this.bashRunner).WithContents(TestFileContents);

string grandChildLinkPath = this.Enlistment.GetVirtualPathTo(Path.Combine(TestFolderName, ChildFolderName, GrandChildLinkName));
this.bashRunner.IsSymbolicLink(grandChildLinkPath).ShouldBeTrue($"{grandChildLinkPath} should be a symlink");
grandChildLinkPath.ShouldBeAFile(this.bashRunner).WithContents(TestFile2Contents);
}

[TestCase, Order(2)]
public void CheckoutBranchWhereSymLinksChangeContentsAndTransitionToFile()
{
GitHelpers.InvokeGitAgainstGVFSRepo(this.Enlistment.RepoRoot, "checkout FunctionalTests/20180925_SymLinksPart2");
GitHelpers.CheckGitCommandAgainstGVFSRepo(
this.Enlistment.RepoRoot,
"status",
"On branch FunctionalTests/20180925_SymLinksPart2",
"nothing to commit, working tree clean");

// testFilePath and testFile2Path are unchanged from FunctionalTests/20180925_SymLinksPart2
string testFilePath = this.Enlistment.GetVirtualPathTo(Path.Combine(TestFolderName, TestFileName));
testFilePath.ShouldBeAFile(this.bashRunner).WithContents(TestFileContents);
this.bashRunner.IsSymbolicLink(testFilePath).ShouldBeFalse($"{testFilePath} should not be a symlink");

string testFile2Path = this.Enlistment.GetVirtualPathTo(Path.Combine(TestFolderName, TestFile2Name));
testFile2Path.ShouldBeAFile(this.bashRunner).WithContents(TestFile2Contents);
this.bashRunner.IsSymbolicLink(testFile2Path).ShouldBeFalse($"{testFile2Path} should not be a symlink");

// In this branch childLinkPath has been changed to point to testFile2Path
string childLinkPath = this.Enlistment.GetVirtualPathTo(Path.Combine(TestFolderName, ChildLinkName));
this.bashRunner.IsSymbolicLink(childLinkPath).ShouldBeTrue($"{childLinkPath} should be a symlink");
childLinkPath.ShouldBeAFile(this.bashRunner).WithContents(TestFile2Contents);

// grandChildLinkPath should now be a file
string grandChildLinkPath = this.Enlistment.GetVirtualPathTo(Path.Combine(TestFolderName, ChildFolderName, GrandChildLinkName));
this.bashRunner.IsSymbolicLink(grandChildLinkPath).ShouldBeFalse($"{grandChildLinkPath} should not be a symlink");
grandChildLinkPath.ShouldBeAFile(this.bashRunner).WithContents(GrandChildLinkNowAFileContents);

// There should also be a new file in the child folder
string newGrandChildFilePath = this.Enlistment.GetVirtualPathTo(Path.Combine(TestFolderName, ChildFolderName, GrandChildFileName));
newGrandChildFilePath.ShouldBeAFile(this.bashRunner).WithContents(GrandChildFileContents);
this.bashRunner.IsSymbolicLink(newGrandChildFilePath).ShouldBeFalse($"{newGrandChildFilePath} should not be a symlink");
}

[TestCase, Order(3)]
public void CheckoutBranchWhereFilesTransitionToSymLinks()
{
GitHelpers.InvokeGitAgainstGVFSRepo(this.Enlistment.RepoRoot, "checkout FunctionalTests/20180925_SymLinksPart3");
GitHelpers.CheckGitCommandAgainstGVFSRepo(
this.Enlistment.RepoRoot,
"status",
"On branch FunctionalTests/20180925_SymLinksPart3",
"nothing to commit, working tree clean");

// In this branch testFilePath has been changed to point to newGrandChildFilePath
string testFilePath = this.Enlistment.GetVirtualPathTo(Path.Combine(TestFolderName, TestFileName));
testFilePath.ShouldBeAFile(this.bashRunner).WithContents(GrandChildFileContents);
this.bashRunner.IsSymbolicLink(testFilePath).ShouldBeTrue($"{testFilePath} should be a symlink");

// The rest of the files are unchanged from FunctionalTests/20180925_SymLinksPart2
string testFile2Path = this.Enlistment.GetVirtualPathTo(Path.Combine(TestFolderName, TestFile2Name));
testFile2Path.ShouldBeAFile(this.bashRunner).WithContents(TestFile2Contents);
this.bashRunner.IsSymbolicLink(testFile2Path).ShouldBeFalse($"{testFile2Path} should not be a symlink");

string childLinkPath = this.Enlistment.GetVirtualPathTo(Path.Combine(TestFolderName, ChildLinkName));
this.bashRunner.IsSymbolicLink(childLinkPath).ShouldBeTrue($"{childLinkPath} should be a symlink");
childLinkPath.ShouldBeAFile(this.bashRunner).WithContents(TestFile2Contents);

string grandChildLinkPath = this.Enlistment.GetVirtualPathTo(Path.Combine(TestFolderName, ChildFolderName, GrandChildLinkName));
this.bashRunner.IsSymbolicLink(grandChildLinkPath).ShouldBeFalse($"{grandChildLinkPath} should not be a symlink");
grandChildLinkPath.ShouldBeAFile(this.bashRunner).WithContents(GrandChildLinkNowAFileContents);

string newGrandChildFilePath = this.Enlistment.GetVirtualPathTo(Path.Combine(TestFolderName, ChildFolderName, GrandChildFileName));
newGrandChildFilePath.ShouldBeAFile(this.bashRunner).WithContents(GrandChildFileContents);
this.bashRunner.IsSymbolicLink(newGrandChildFilePath).ShouldBeFalse($"{newGrandChildFilePath} should not be a symlink");
}

[TestCase, Order(4)]
public void GitStatusReportsSymLinkChanges()
{
GitHelpers.CheckGitCommandAgainstGVFSRepo(
this.Enlistment.RepoRoot,
"status",
"On branch FunctionalTests/20180925_SymLinksPart3",
"nothing to commit, working tree clean");

string testFilePath = this.Enlistment.GetVirtualPathTo(Path.Combine(TestFolderName, TestFileName));
testFilePath.ShouldBeAFile(this.bashRunner).WithContents(GrandChildFileContents);
this.bashRunner.IsSymbolicLink(testFilePath).ShouldBeTrue($"{testFilePath} should be a symlink");

string testFile2Path = this.Enlistment.GetVirtualPathTo(Path.Combine(TestFolderName, TestFile2Name));
testFile2Path.ShouldBeAFile(this.bashRunner).WithContents(TestFile2Contents);
this.bashRunner.IsSymbolicLink(testFile2Path).ShouldBeFalse($"{testFile2Path} should not be a symlink");

// Update testFilePath's symlink to point to testFile2Path
this.bashRunner.CreateSymbolicLink(testFilePath, testFile2Path);

testFilePath.ShouldBeAFile(this.bashRunner).WithContents(TestFile2Contents);
this.bashRunner.IsSymbolicLink(testFilePath).ShouldBeTrue($"{testFilePath} should be a symlink");

GitHelpers.CheckGitCommandAgainstGVFSRepo(
this.Enlistment.RepoRoot,
"status",
"On branch FunctionalTests/20180925_SymLinksPart3",
$"modified: {TestFolderName}/{TestFileName}");
}
}
}
Loading