public void SymLinksAreReparsePoints() { var path = GetTestFilePath(); var linkPath = GetTestFilePath(); File.Create(path).Dispose(); Assert.True(MountHelper.CreateSymbolicLink(linkPath, path)); Assert.Equal(FileAttributes.ReparsePoint, FileAttributes.ReparsePoint & Get(linkPath)); }
public void SymLinksAreReparsePoints() { string path = CreateItem(); string linkPath = GetTestFilePath(); Assert.True(MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: IsDirectory)); Assert.NotEqual(FileAttributes.ReparsePoint, FileAttributes.ReparsePoint & GetAttributes(path)); Assert.Equal(FileAttributes.ReparsePoint, FileAttributes.ReparsePoint & GetAttributes(linkPath)); }
public void SymLinksAreReparsePoints() { var path = GetTestFilePath(); var linkPath = GetTestFilePath(); Directory.CreateDirectory(path); Assert.True(MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: true)); Assert.NotEqual(FileAttributes.ReparsePoint, FileAttributes.ReparsePoint & Get(path)); Assert.Equal(FileAttributes.ReparsePoint, FileAttributes.ReparsePoint & Get(linkPath)); }
public void SymlinkToNewDirectory() { string targetPath = GetTestFilePath(); Directory.CreateDirectory(targetPath); string linkPath = GetTestFilePath(); Assert.True(MountHelper.CreateSymbolicLink(linkPath, targetPath)); Assert.NotEqual(RuntimeInformation.IsOSPlatform(OSPlatform.Windows), Directory.Exists(linkPath)); }
public void SymlinkToNewDirectory() { string path = GetTestFilePath(); Directory.CreateDirectory(path); string linkPath = GetTestFilePath(); Assert.True(MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: true)); Assert.True(Directory.Exists(path)); Assert.True(Directory.Exists(linkPath)); }
public void SymlinkToNewDirectoryInfo() { string path = GetTestFilePath(); new DirectoryInfo(path).Create(); string linkPath = GetTestFilePath(); Assert.True(MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: true)); Assert.True(new DirectoryInfo(path).Exists); Assert.True(new DirectoryInfo(linkPath).Exists); }
public void FileSystemWatcher_File_Create_SymLink() { using (var testDirectory = new TempDirectory(GetTestFilePath())) using (var dir = new TempDirectory(Path.Combine(testDirectory.Path, "dir"))) using (var temp = new TempFile(GetTestFilePath())) using (var watcher = new FileSystemWatcher(dir.Path, "*")) { // Make the symlink in our path (to the temp file) and make sure an event is raised string symLinkPath = Path.Combine(dir.Path, GetRandomLinkName()); Action action = () => Assert.True(MountHelper.CreateSymbolicLink(symLinkPath, temp.Path, false)); Action cleanup = () => File.Delete(symLinkPath); ExpectEvent(watcher, WatcherChangeTypes.Created, action, cleanup, symLinkPath); } }
public void FileSystemWatcher_Directory_Create_SymLink() { string dir = CreateTestDirectory(TestDirectory, "dir"); string temp = CreateTestDirectory(); using (var watcher = new FileSystemWatcher(Path.GetFullPath(dir), "*")) { // Make the symlink in our path (to the temp folder) and make sure an event is raised string symLinkPath = Path.Combine(dir, GetRandomLinkName()); Action action = () => Assert.True(MountHelper.CreateSymbolicLink(symLinkPath, temp, true)); Action cleanup = () => Directory.Delete(symLinkPath); ExpectEvent(watcher, WatcherChangeTypes.Created, action, cleanup, symLinkPath); } }
public void FileSystemWatcher_File_Changed_SymLink() { using (var testDirectory = new TempDirectory(GetTestFilePath())) using (var dir = new TempDirectory(Path.Combine(testDirectory.Path, "dir"))) using (var file = new TempFile(GetTestFilePath())) using (var watcher = new FileSystemWatcher(dir.Path, "*")) { watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size; Assert.True(MountHelper.CreateSymbolicLink(Path.Combine(dir.Path, GetRandomLinkName()), file.Path, false)); Action action = () => File.AppendAllText(file.Path, "longtext"); Action cleanup = () => File.AppendAllText(file.Path, "short"); ExpectEvent(watcher, 0, action, cleanup, expectedPath: file.Path); } }
public void FileSystemWatcher_File_Changed_SymLink() { string dir = CreateTestDirectory(TestDirectory, "dir"); string file = CreateTestFile(); using (var watcher = new FileSystemWatcher(dir, "*")) { watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size; Assert.True(MountHelper.CreateSymbolicLink(Path.Combine(dir, GetRandomLinkName()), file, false)); Action action = () => File.AppendAllText(file, "longtext"); Action cleanup = () => File.AppendAllText(file, "short"); ExpectEvent(watcher, 0, action, cleanup, expectedPath: file); } }
public void SymLinksMayExistIndependentlyOfTarget() { var path = GetTestFilePath(); var linkPath = GetTestFilePath(); Directory.CreateDirectory(path); Assert.True(MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: true)); // Both the symlink and the target exist Assert.True(Directory.Exists(path), "path should exist"); Assert.True(Directory.Exists(linkPath), "linkPath should exist"); Assert.False(File.Exists(linkPath)); // Delete the target. The symlink should still exist. On Unix, the symlink will now be // considered a file (since it's broken and we don't know what it'll eventually point to). Directory.Delete(path); Assert.False(Directory.Exists(path), "path should now not exist"); if (OperatingSystem.IsWindows()) { Assert.True(Directory.Exists(linkPath), "linkPath should still exist as a directory"); Assert.False(File.Exists(linkPath), "linkPath should not be a file"); } else { Assert.False(Directory.Exists(linkPath), "linkPath should no longer be a directory"); Assert.True(File.Exists(linkPath), "linkPath should now be a file"); } // Now delete the symlink. // On Unix, deleting the symlink should fail, because it's not a directory, it's a file. // On Windows, it should succeed. try { Directory.Delete(linkPath); Assert.True(OperatingSystem.IsWindows(), "Should only succeed on Windows"); } catch (IOException) { Assert.False(OperatingSystem.IsWindows(), "Should only fail on Unix"); File.Delete(linkPath); } Assert.False(Directory.Exists(linkPath), "linkPath should no longer exist as a directory"); Assert.False(File.Exists(linkPath), "linkPath should no longer exist as a file"); }
public void SymLinkLength() { var path = GetTestFilePath(); var linkPath = GetTestFilePath(); using (var tempFile = new TempFile(path, 2000)) { Assert.True(MountHelper.CreateSymbolicLink(linkPath, path)); var info = new FileInfo(path); Assert.Equal(2000, info.Length); // On Windows, symlinks have length 0. // On Unix, we follow to the target and report on the target's size. var linkInfo = new FileInfo(linkPath); Assert.Equal(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 0 : info.Length, linkInfo.Length); } }
public void FileSystemWatcher_File_Delete_SymLink() { FileSystemWatcherTest.Execute(() => { string dir = CreateTestDirectory(TestDirectory, "dir"); string temp = CreateTestFile(); using (var watcher = new FileSystemWatcher(dir, "*")) { // Make the symlink in our path (to the temp file) and make sure an event is raised string symLinkPath = Path.Combine(dir, GetRandomLinkName()); Action action = () => File.Delete(symLinkPath); Action cleanup = () => Assert.True(MountHelper.CreateSymbolicLink(symLinkPath, temp, false)); cleanup(); ExpectEvent(watcher, WatcherChangeTypes.Deleted, action, cleanup, symLinkPath); } }, maxAttempts: DefaultAttemptsForExpectedEvent, backoffFunc: (iteration) => RetryDelayMilliseconds, retryWhen: e => e is XunitException); }
public void SymLinksReflectSymLinkAttributes() { string path = CreateItem(); string linkPath = GetTestFilePath(); Assert.True(MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: IsDirectory)); SetAttributes(path, FileAttributes.ReadOnly); try { Assert.Equal(FileAttributes.ReadOnly, FileAttributes.ReadOnly & GetAttributes(path)); Assert.NotEqual(FileAttributes.ReadOnly, FileAttributes.ReadOnly & GetAttributes(linkPath)); } finally { SetAttributes(path, GetAttributes(path) & ~FileAttributes.ReadOnly); } }
public void DeletingSymLinkDoesntDeleteTarget() { var path = GetTestFilePath(); var linkPath = GetTestFilePath(); Directory.CreateDirectory(path); Assert.True(MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: true)); // Both the symlink and the target exist Assert.True(Directory.Exists(path), "path should exist"); Assert.True(Directory.Exists(linkPath), "linkPath should exist"); // Delete the symlink Directory.Delete(linkPath); // Target should still exist Assert.True(Directory.Exists(path), "path should still exist"); Assert.False(Directory.Exists(linkPath), "linkPath should no longer exist"); }
public void SymLinksReflectSymLinkAttributes() { var path = GetTestFilePath(); var linkPath = GetTestFilePath(); Directory.CreateDirectory(path); Assert.True(MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: true)); Set(path, FileAttributes.ReadOnly); try { Assert.Equal(FileAttributes.ReadOnly, FileAttributes.ReadOnly & Get(path)); Assert.NotEqual(FileAttributes.ReadOnly, FileAttributes.ReadOnly & Get(linkPath)); } finally { Set(path, Get(path) & ~FileAttributes.ReadOnly); } }
public void FileSystemWatcher_Directory_Changed_SymLink() { string dir = Path.Combine(TestDirectory, "dir"); string tempDir = CreateTestDirectory(dir, "tempDir"); string file = CreateTestFile(tempDir, "test"); using (var watcher = new FileSystemWatcher(dir, "*")) { // Setup the watcher watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size; watcher.IncludeSubdirectories = true; Assert.True(MountHelper.CreateSymbolicLink(Path.Combine(dir, GetRandomLinkName()), tempDir, true)); Action action = () => File.AppendAllText(file, "longtext"); Action cleanup = () => File.AppendAllText(file, "short"); ExpectEvent(watcher, 0, action, cleanup, dir); } }
public void SymLinksMayExistIndependentlyOfTarget() { var path = GetTestFilePath(); var linkPath = GetTestFilePath(); File.Create(path).Dispose(); Assert.True(MountHelper.CreateSymbolicLink(linkPath, path)); File.Delete(path); // We've delete the target file, so it shouldn't exist. var info = new FileInfo(path); Assert.False(info.Exists); // On Windows we report about the existence of the symlink file itself, so // does still exist. On Unix, we report about the target, where it doesn't. var linkInfo = new FileInfo(linkPath); Assert.Equal(RuntimeInformation.IsOSPlatform(OSPlatform.Windows), linkInfo.Exists); }
public void SymLinksMayExistIndependentlyOfTarget() { var path = GetTestFilePath(); var linkPath = GetTestFilePath(); var pathFI = new DirectoryInfo(path); var linkPathFI = new DirectoryInfo(linkPath); pathFI.Create(); Assert.True(MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: true)); // Both the symlink and the target exist pathFI.Refresh(); linkPathFI.Refresh(); Assert.True(pathFI.Exists, "path should exist"); Assert.True(linkPathFI.Exists, "linkPath should exist"); // Delete the target. The symlink should still exist, but on Unix it'll now // be considered a file and won't exist as a directory. pathFI.Delete(); pathFI.Refresh(); Assert.False(pathFI.Exists, "path should now not exist"); linkPathFI.Refresh(); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { Assert.True(linkPathFI.Exists, "linkPath directory should still exist"); Assert.False(File.Exists(linkPath), "linkPath file should not exist"); Directory.Delete(linkPath); } else { Assert.False(linkPathFI.Exists, "linkPath directory should no longer exist"); Assert.True(File.Exists(linkPath), "linkPath file should now exist"); File.Delete(linkPath); } linkPathFI.Refresh(); Assert.False(linkPathFI.Exists, "linkPath should no longer exist"); }
public void SymLinksMayExistIndependentlyOfTarget() { var path = GetTestFilePath(); var linkPath = GetRandomLinkPath(); File.Create(path).Dispose(); Assert.True(MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: false)); // Both the symlink and the target exist Assert.True(File.Exists(path), "path should exist"); Assert.True(File.Exists(linkPath), "linkPath should exist"); // Delete the target. The symlink should still exist File.Delete(path); Assert.False(File.Exists(path), "path should now not exist"); Assert.True(File.Exists(linkPath), "linkPath should still exist"); // Now delete the symlink. File.Delete(linkPath); Assert.False(File.Exists(linkPath), "linkPath should no longer exist"); }
public void SymLinksReflectTargetAttributes() { var path = GetTestFilePath(); var linkPath = GetTestFilePath(); File.Create(path).Dispose(); Assert.True(MountHelper.CreateSymbolicLink(linkPath, path)); Set(path, FileAttributes.ReadOnly); Assert.Equal(FileAttributes.ReadOnly, Get(path)); // Can't assume that ReparsePoint is the only attribute because Windows will add Archive automatically // Instead, make sure that ReparsePoint is present. Assert.Equal(FileAttributes.ReparsePoint, FileAttributes.ReparsePoint & Get(linkPath)); // Then for ReadOnly, there is a difference between Windows and Unix. Given the prevalence of symlinks // on Unix, matching the existing Windows behavior doesn't make as much sense, so we still follow // to the target object. As such, on Windows ReadOnly should not be set, but it should be set elsewhere. Assert.Equal( RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? (FileAttributes)0 : FileAttributes.ReadOnly, FileAttributes.ReadOnly & Get(linkPath)); }
public void SetToPathContainingSymLink() { var path = GetTestFilePath(); var linkPath = GetTestFilePath(); Directory.CreateDirectory(path); Assert.True(MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: true)); // Both the symlink and the target exist Assert.True(Directory.Exists(path), "path should exist"); Assert.True(Directory.Exists(linkPath), "linkPath should exist"); // Set Current Directory to symlink string currentDir = Directory.GetCurrentDirectory(); try { Directory.SetCurrentDirectory(linkPath); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { Assert.Equal(linkPath, Directory.GetCurrentDirectory()); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { Assert.Equal("/private" + path, Directory.GetCurrentDirectory()); } else { Assert.Equal(path, Directory.GetCurrentDirectory()); } } finally { Directory.SetCurrentDirectory(currentDir); } Assert.Equal(currentDir, Directory.GetCurrentDirectory()); }
public void SymLinkLength() { string path = GetTestFilePath(); string linkPath = GetTestFilePath(); const int FileSize = 2000; using (var tempFile = new TempFile(path, FileSize)) { Assert.True(MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: false)); var info = new FileInfo(path); Assert.Equal(FileSize, info.Length); var linkInfo = new FileInfo(linkPath); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { // On Windows, symlinks have length 0. Assert.Equal(0, linkInfo.Length); } else { // On Unix, a symlink contains the path to the target, and thus has that length. // But the length could actually be longer if it's not just ASCII characters. // We just verify it's at least that big, but also verify that we're not accidentally // getting the target file size. Assert.InRange(linkInfo.Length, path.Length, FileSize - 1); } // On both, FileStream should however open the target such that its length is the target length using (FileStream linkFs = File.OpenRead(linkPath)) { Assert.Equal(FileSize, linkFs.Length); } } }
public void EnumerateWithSymLinkToFile() { using (var containingFolder = new TemporaryDirectory()) { string linkPath; // Test a symlink to a file that does and then doesn't exist using (var targetFile = new TemporaryFile()) { linkPath = Path.Combine(containingFolder.Path, Path.GetRandomFileName()); Assert.True(MountHelper.CreateSymbolicLink(linkPath, targetFile.Path, isDirectory: false)); Assert.True(File.Exists(linkPath)); Assert.Equal(1, GetEntries(containingFolder.Path).Count()); } // The symlink still exists even though the target file is gone. Assert.Equal(1, GetEntries(containingFolder.Path).Count()); // The symlink is gone File.Delete(linkPath); Assert.Equal(0, GetEntries(containingFolder.Path).Count()); } }