public void DeletingSymLinkDoesntDeleteTarget() { var path = GetTestFilePath(); var linkPath = GetTestFilePath(); 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 symlink File.Delete(linkPath); // Target should still exist Assert.True(File.Exists(path), "path should still exist"); Assert.False(File.Exists(linkPath), "linkPath should no longer exist"); }
public void SymLinksReflectSymLinkAttributes() { var path = GetTestFilePath(); var linkPath = GetTestFilePath(); File.Create(path).Dispose(); Assert.True(MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: false)); 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 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 static void WorkingDirectories() { foreach (var dir in DefaultWorkingDirectories) { var mntDir = MountHelper.SetDirsPath(dir); Directory.CreateDirectory(dir); Directory.CreateDirectory(mntDir); if (MountHelper.IsAlreadyMounted(dir)) { continue; } ConsoleLogger.Log($"mount {mntDir} -> {dir}"); SetBind(mntDir, dir); } foreach (var kvp in DefaultWorkingDirectoriesWithOptions) { if (MountHelper.IsAlreadyMounted(kvp.Key) == false) { Bash.Execute($"mount {kvp.Value} {kvp.Key}", false); } } }
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 static void WorkingDirectories() { if (MountHelper.IsAlreadyMounted("/mnt/cdrom/Kernel/active-firmware", "/lib64/firmware") == false) { MountSimple("/mnt/cdrom/Kernel/active-firmware", "/lib64/firmware"); } var kernelRelease = Bash.Execute("uname -r").Trim(); var linkedRelease = Bash.Execute("file /mnt/cdrom/Kernel/active-modules").Trim(); if (MountHelper.IsAlreadyMounted("/mnt/cdrom/Kernel/active-modules") == false && linkedRelease.Contains(kernelRelease)) { var moduleDir = $"/lib64/modules/{kernelRelease}/"; Directory.CreateDirectory(moduleDir); MountSimple("/mnt/cdrom/Kernel/active-modules", moduleDir); } Bash.Execute("systemctl restart systemd-modules-load.service", false); foreach (var dir in DefaultWorkingDirectories) { var mntDir = MountHelper.ConvertDirectoryTargetPathToDirs(dir); Directory.CreateDirectory(dir); Directory.CreateDirectory(mntDir); if (MountHelper.IsAlreadyMounted(dir)) { continue; } ConsoleLogger.Log($"[mount] {mntDir} -> {dir}"); MountWithBind(mntDir, dir); } foreach (var kvp in DefaultWorkingDirectoriesWithOptions) { if (MountHelper.IsAlreadyMounted(kvp.Key) == false) { MountSimple(kvp.Value, kvp.Key); } } }
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()); } }
[PlatformSpecific(TestPlatforms.Windows)] // testing mounting volumes and reparse points public static void runTest() { try { Stopwatch watch; const string MountPrefixName = "LaksMount"; string mountedDirName; string dirNameWithoutRoot; string dirNameReferedFromMountedDrive; string dirName; string[] expectedFiles; string[] files; string[] expectedDirs; string[] dirs; List <string> list; watch = new Stopwatch(); watch.Start(); try { //Scenario 1: Vanilla - Different drive is mounted on the current drive Console.WriteLine("Scenario 1 - Vanilla: Different drive is mounted on the current drive: {0}", watch.Elapsed); string otherDriveInMachine = IOServices.GetNtfsDriveOtherThanCurrent(); if (FileSystemDebugInfo.IsCurrentDriveNTFS() && otherDriveInMachine != null) { mountedDirName = Path.GetFullPath(ManageFileSystem.GetNonExistingDir(Path.DirectorySeparatorChar.ToString(), MountPrefixName)); try { Console.WriteLine("Creating directory " + mountedDirName); Directory.CreateDirectory(mountedDirName); MountHelper.Mount(otherDriveInMachine.Substring(0, 2), mountedDirName); dirName = ManageFileSystem.GetNonExistingDir(otherDriveInMachine, ManageFileSystem.DirPrefixName); using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100)) { dirNameWithoutRoot = dirName.Substring(3); dirNameReferedFromMountedDrive = Path.Combine(mountedDirName, dirNameWithoutRoot); //Files expectedFiles = fileManager.GetAllFiles(); list = new List <string>(); //We will only test the filenames since they are unique foreach (string file in expectedFiles) { list.Add(Path.GetFileName(file)); } files = Directory.GetFiles(dirNameReferedFromMountedDrive, "*.*", SearchOption.AllDirectories); Eval(files.Length == list.Count, "Err_3947g! wrong count"); for (int i = 0; i < expectedFiles.Length; i++) { if (Eval(list.Contains(Path.GetFileName(files[i])), "Err_582bmw! No file found: {0}", files[i])) { list.Remove(Path.GetFileName(files[i])); } } if (!Eval(list.Count == 0, "Err_891vut! wrong count: {0}", list.Count)) { Console.WriteLine(); foreach (string fileName in list) { Console.WriteLine(fileName); } } //Directories expectedDirs = fileManager.GetAllDirectories(); list = new List <string>(); foreach (string dir in expectedDirs) { list.Add(dir.Substring(dirName.Length)); } dirs = Directory.GetDirectories(dirNameReferedFromMountedDrive, "*.*", SearchOption.AllDirectories); Eval(dirs.Length == list.Count, "Err_813weq! wrong count"); for (int i = 0; i < dirs.Length; i++) { string exDir = dirs[i].Substring(dirNameReferedFromMountedDrive.Length); if (Eval(list.Contains(exDir), "Err_287kkm! No file found: {0}", exDir)) { list.Remove(exDir); } } if (!Eval(list.Count == 0, "Err_921mhs! wrong count: {0}", list.Count)) { Console.WriteLine(); foreach (string value in list) { Console.WriteLine(value); } } } } finally { MountHelper.Unmount(mountedDirName); DeleteDir(mountedDirName, true); } } else { Console.WriteLine("Skipping since drive is not NTFS and there is no other drive on the machine"); } } catch (Exception ex) { s_pass = false; Console.WriteLine("Err_768lme! Exception caught in scenario: {0}", ex); } //Scenario 2: Current drive is mounted on a different drive Console.WriteLine(Environment.NewLine + "Scenario 2 - Current drive is mounted on a different drive: {0}", watch.Elapsed); try { string otherDriveInMachine = IOServices.GetNtfsDriveOtherThanCurrent(); if (otherDriveInMachine != null) { mountedDirName = Path.GetFullPath(ManageFileSystem.GetNonExistingDir(otherDriveInMachine.Substring(0, 3), MountPrefixName)); try { Directory.CreateDirectory(mountedDirName); MountHelper.Mount(Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName); dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName); using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100)) { dirNameWithoutRoot = dirName.Substring(3); dirNameReferedFromMountedDrive = Path.Combine(mountedDirName, dirNameWithoutRoot); //Files expectedFiles = fileManager.GetAllFiles(); list = new List <string>(); //We will only test the filenames since they are unique foreach (string file in expectedFiles) { list.Add(Path.GetFileName(file)); } files = Directory.GetFiles(dirNameReferedFromMountedDrive, "*.*", SearchOption.AllDirectories); Eval(files.Length == list.Count, "Err_689myg! wrong count"); for (int i = 0; i < expectedFiles.Length; i++) { if (Eval(list.Contains(Path.GetFileName(files[i])), "Err_894vhm! No file found: {0}", files[i])) { list.Remove(Path.GetFileName(files[i])); } } if (!Eval(list.Count == 0, "Err_952qkj! wrong count: {0}", list.Count)) { Console.WriteLine(); foreach (string fileName in list) { Console.WriteLine(fileName); } } //Directories expectedDirs = fileManager.GetAllDirectories(); list = new List <string>(); foreach (string dir in expectedDirs) { list.Add(dir.Substring(dirName.Length)); } dirs = Directory.GetDirectories(dirNameReferedFromMountedDrive, "*.*", SearchOption.AllDirectories); Eval(dirs.Length == list.Count, "Err_154vrz! wrong count"); for (int i = 0; i < dirs.Length; i++) { string exDir = dirs[i].Substring(dirNameReferedFromMountedDrive.Length); if (Eval(list.Contains(exDir), "Err_301sao! No file found: {0}", exDir)) { list.Remove(exDir); } } if (!Eval(list.Count == 0, "Err_630gjj! wrong count: {0}", list.Count)) { Console.WriteLine(); foreach (string value in list) { Console.WriteLine(value); } } } } finally { MountHelper.Unmount(mountedDirName); DeleteDir(mountedDirName, true); } } else { Console.WriteLine("Skipping since drive is not NTFS and there is no other drive on the machine"); } } catch (Exception ex) { s_pass = false; Console.WriteLine("Err_231vwf! Exception caught in scenario: {0}", ex); } //scenario 3.1: Current drive is mounted on current drive Console.WriteLine(Environment.NewLine + "Scenario 3.1 - Current drive is mounted on current drive: {0}", watch.Elapsed); try { if (FileSystemDebugInfo.IsCurrentDriveNTFS()) { mountedDirName = Path.GetFullPath(ManageFileSystem.GetNonExistingDir(Path.DirectorySeparatorChar.ToString(), MountPrefixName)); try { Directory.CreateDirectory(mountedDirName); MountHelper.Mount(Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName); dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName); using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100)) { dirNameWithoutRoot = dirName.Substring(3); dirNameReferedFromMountedDrive = Path.Combine(mountedDirName, dirNameWithoutRoot); //Files expectedFiles = fileManager.GetAllFiles(); list = new List <string>(); //We will only test the filenames since they are unique foreach (string file in expectedFiles) { list.Add(Path.GetFileName(file)); } files = Directory.GetFiles(dirNameReferedFromMountedDrive, "*.*", SearchOption.AllDirectories); Eval(files.Length == list.Count, "Err_213fuo! wrong count"); for (int i = 0; i < expectedFiles.Length; i++) { if (Eval(list.Contains(Path.GetFileName(files[i])), "Err_499oxz! No file found: {0}", files[i])) { list.Remove(Path.GetFileName(files[i])); } } if (!Eval(list.Count == 0, "Err_301gtz! wrong count: {0}", list.Count)) { Console.WriteLine(); foreach (string fileName in list) { Console.WriteLine(fileName); } } //Directories expectedDirs = fileManager.GetAllDirectories(); list = new List <string>(); foreach (string dir in expectedDirs) { list.Add(dir.Substring(dirName.Length)); } dirs = Directory.GetDirectories(dirNameReferedFromMountedDrive, "*.*", SearchOption.AllDirectories); Eval(dirs.Length == list.Count, "Err_771dxv! wrong count"); for (int i = 0; i < dirs.Length; i++) { string exDir = dirs[i].Substring(dirNameReferedFromMountedDrive.Length); if (Eval(list.Contains(exDir), "Err_315jey! No file found: {0}", exDir)) { list.Remove(exDir); } } if (!Eval(list.Count == 0, "Err_424opm! wrong count: {0}", list.Count)) { Console.WriteLine(); foreach (string value in list) { Console.WriteLine(value); } } } } finally { MountHelper.Unmount(mountedDirName); DeleteDir(mountedDirName, true); } } else { Console.WriteLine("Drive is not NTFS. Skipping scenario"); } } catch (Exception ex) { s_pass = false; Console.WriteLine("Err_493ojg! Exception caught in scenario: {0}", ex); } //scenario 3.2: Current drive is mounted on current directory Console.WriteLine(Environment.NewLine + "Scenario 3.2 - Current drive is mounted on current directory: {0}", watch.Elapsed); try { if (FileSystemDebugInfo.IsCurrentDriveNTFS()) { mountedDirName = Path.GetFullPath(ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), MountPrefixName)); try { Directory.CreateDirectory(mountedDirName); MountHelper.Mount(Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName); dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName); using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100)) { dirNameWithoutRoot = dirName.Substring(3); dirNameReferedFromMountedDrive = Path.Combine(mountedDirName, dirNameWithoutRoot); //Files expectedFiles = fileManager.GetAllFiles(); list = new List <string>(); //We will only test the filenames since they are unique foreach (string file in expectedFiles) { list.Add(Path.GetFileName(file)); } files = Directory.GetFiles(dirNameReferedFromMountedDrive, "*.*", SearchOption.AllDirectories); Eval(files.Length == list.Count, "Err_253yit! wrong count"); for (int i = 0; i < expectedFiles.Length; i++) { if (Eval(list.Contains(Path.GetFileName(files[i])), "Err_798mjs! No file found: {0}", files[i])) { list.Remove(Path.GetFileName(files[i])); } } if (!Eval(list.Count == 0, "Err_141lgl! wrong count: {0}", list.Count)) { Console.WriteLine(); foreach (string fileName in list) { Console.WriteLine(fileName); } } //Directories expectedDirs = fileManager.GetAllDirectories(); list = new List <string>(); foreach (string dir in expectedDirs) { list.Add(dir.Substring(dirName.Length)); } dirs = Directory.GetDirectories(dirNameReferedFromMountedDrive, "*.*", SearchOption.AllDirectories); Eval(dirs.Length == list.Count, "Err_512oxq! wrong count"); for (int i = 0; i < dirs.Length; i++) { string exDir = dirs[i].Substring(dirNameReferedFromMountedDrive.Length); if (Eval(list.Contains(exDir), "Err_907zbr! No file found: {0}", exDir)) { list.Remove(exDir); } } if (!Eval(list.Count == 0, "Err_574raf! wrong count: {0}", list.Count)) { Console.WriteLine(); foreach (string value in list) { Console.WriteLine(value); } } } } finally { MountHelper.Unmount(mountedDirName); DeleteDir(mountedDirName, true); } } else { Console.WriteLine("Drive is not NTFS. Skipping scenario"); } } catch (Exception ex) { s_pass = false; Console.WriteLine("Err_432qcp! Exception caught in scenario: {0}", ex); } Console.WriteLine("Completed {0}", watch.Elapsed); } catch (Exception ex) { s_pass = false; Console.WriteLine("Err_234rsgf! Uncaught exception in RunTest: {0}", ex); } Assert.True(s_pass); }
[PlatformSpecific(PlatformID.Windows)] // testing volumes / mounts / drive letters public static void RunTest() { try { const String MountPrefixName = "LaksMount"; String mountedDirName; String dirName; String dirNameWithoutRoot; String dirNameReferedFromMountedDrive; //Adding debug info since this test hangs sometime in RTS runs String debugFileName = "Co7604Delete_MountVolume_Debug.txt"; DeleteFile(debugFileName); String scenarioDescription; scenarioDescription = "Scenario 1: Vanilla - Different drive is mounted on the current drive"; try { File.AppendAllText(debugFileName, String.Format("{0}{1}", scenarioDescription, Environment.NewLine)); string otherDriveInMachine = IOServices.GetNtfsDriveOtherThanCurrent(); //out labs use UIP tools in one drive and dont expect this drive to be used by others. We avoid this problem by not testing if the other drive is not NTFS if (FileSystemDebugInfo.IsCurrentDriveNTFS() && otherDriveInMachine != null) { Console.WriteLine(scenarioDescription); mountedDirName = Path.GetFullPath(ManageFileSystem.GetNonExistingDir(Path.DirectorySeparatorChar.ToString(), MountPrefixName)); try { Directory.CreateDirectory(mountedDirName); File.AppendAllText(debugFileName, String.Format("Mounting on {0}{1}{2}", otherDriveInMachine.Substring(0, 2), mountedDirName, Environment.NewLine)); MountHelper.Mount(otherDriveInMachine.Substring(0, 2), mountedDirName); dirName = ManageFileSystem.GetNonExistingDir(otherDriveInMachine, ManageFileSystem.DirPrefixName); File.AppendAllText(debugFileName, String.Format("Creating a sub tree at: {0}{1}", dirName, Environment.NewLine)); using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100)) { Eval(Directory.Exists(dirName), "Err_3974g! Directory {0} doesn't exist: {1}", dirName, Directory.Exists(dirName)); //Lets refer to these via mounted drive and check dirNameWithoutRoot = dirName.Substring(3); dirNameReferedFromMountedDrive = Path.Combine(mountedDirName, dirNameWithoutRoot); Directory.Delete(dirNameReferedFromMountedDrive, true); Task.Delay(300).Wait(); Eval(!Directory.Exists(dirName), "Err_20387g! Directory {0} still exist: {1}", dirName, Directory.Exists(dirName)); } } finally { MountHelper.Unmount(mountedDirName); DeleteDir(mountedDirName, true); } File.AppendAllText(debugFileName, String.Format("Completed scenario {0}", Environment.NewLine)); } else { File.AppendAllText(debugFileName, String.Format("Scenario 1 - Vanilla - NOT RUN: Different drive is mounted on the current drive {0}", Environment.NewLine)); } } catch (Exception ex) { s_pass = false; Console.WriteLine("Err_768lme! Exception caught in scenario: {0}", ex); } scenarioDescription = "Scenario 2: Current drive is mounted on a different drive"; Console.WriteLine(scenarioDescription); File.AppendAllText(debugFileName, String.Format("{0}{1}", scenarioDescription, Environment.NewLine)); try { string otherDriveInMachine = IOServices.GetNtfsDriveOtherThanCurrent(); if (otherDriveInMachine != null) { mountedDirName = Path.GetFullPath(ManageFileSystem.GetNonExistingDir(otherDriveInMachine.Substring(0, 3), MountPrefixName)); try { Directory.CreateDirectory(mountedDirName); File.AppendAllText(debugFileName, String.Format("Mounting on {0}{1}{2}", Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName, Environment.NewLine)); MountHelper.Mount(Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName); dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName); File.AppendAllText(debugFileName, String.Format("Creating a sub tree at: {0}{1}", dirName, Environment.NewLine)); using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100)) { Eval(Directory.Exists(dirName), "Err_239ufz! Directory {0} doesn't exist: {1}", dirName, Directory.Exists(dirName)); //Lets refer to these via mounted drive and check dirNameWithoutRoot = dirName.Substring(3); dirNameReferedFromMountedDrive = Path.Combine(mountedDirName, dirNameWithoutRoot); Directory.Delete(dirNameReferedFromMountedDrive, true); Task.Delay(300).Wait(); Eval(!Directory.Exists(dirName), "Err_794aiu! Directory {0} still exist: {1}", dirName, Directory.Exists(dirName)); } } finally { MountHelper.Unmount(mountedDirName); DeleteDir(mountedDirName, true); } File.AppendAllText(debugFileName, String.Format("Completed scenario {0}", Environment.NewLine)); } } catch (Exception ex) { s_pass = false; Console.WriteLine("Err_231vwf! Exception caught in scenario: {0}", ex); } //scenario 3.1: Current drive is mounted on current drive scenarioDescription = "Scenario 3.1 - Current drive is mounted on current drive"; try { if (FileSystemDebugInfo.IsCurrentDriveNTFS()) { File.AppendAllText(debugFileName, String.Format("{0}{1}", scenarioDescription, Environment.NewLine)); mountedDirName = Path.GetFullPath(ManageFileSystem.GetNonExistingDir(Path.DirectorySeparatorChar.ToString(), MountPrefixName)); try { Directory.CreateDirectory(mountedDirName); File.AppendAllText(debugFileName, String.Format("Mounting on {0}{1}{2}", Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName, Environment.NewLine)); MountHelper.Mount(Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName); dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName); File.AppendAllText(debugFileName, String.Format("Creating a sub tree at: {0}{1}", dirName, Environment.NewLine)); using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100)) { Eval(Directory.Exists(dirName), "Err_324eez! Directory {0} doesn't exist: {1}", dirName, Directory.Exists(dirName)); //Lets refer to these via mounted drive and check dirNameWithoutRoot = dirName.Substring(3); dirNameReferedFromMountedDrive = Path.Combine(mountedDirName, dirNameWithoutRoot); Directory.Delete(dirNameReferedFromMountedDrive, true); Task.Delay(300).Wait(); Eval(!Directory.Exists(dirName), "Err_195whv! Directory {0} still exist: {1}", dirName, Directory.Exists(dirName)); } } finally { MountHelper.Unmount(mountedDirName); DeleteDir(mountedDirName, true); } File.AppendAllText(debugFileName, String.Format("Completed scenario {0}", Environment.NewLine)); } } catch (Exception ex) { s_pass = false; Console.WriteLine("Err_493ojg! Exception caught in scenario: {0}", ex); } //scenario 3.2: Current drive is mounted on current directory scenarioDescription = "Scenario 3.2 - Current drive is mounted on current directory"; try { if (FileSystemDebugInfo.IsCurrentDriveNTFS()) { File.AppendAllText(debugFileName, String.Format("{0}{1}", scenarioDescription, Environment.NewLine)); mountedDirName = Path.GetFullPath(ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), MountPrefixName)); try { Directory.CreateDirectory(mountedDirName); File.AppendAllText(debugFileName, String.Format("Mounting on {0}{1}{2}", Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName, Environment.NewLine)); MountHelper.Mount(Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName); dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName); File.AppendAllText(debugFileName, String.Format("Creating a sub tree at: {0}{1}", dirName, Environment.NewLine)); using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100)) { Eval(Directory.Exists(dirName), "Err_951ipb! Directory {0} doesn't exist: {1}", dirName, Directory.Exists(dirName)); //Lets refer to these via mounted drive and check dirNameWithoutRoot = dirName.Substring(3); dirNameReferedFromMountedDrive = Path.Combine(mountedDirName, dirNameWithoutRoot); Directory.Delete(dirNameReferedFromMountedDrive, true); Task.Delay(300).Wait(); Eval(!Directory.Exists(dirName), "Err_493yin! Directory {0} still exist: {1}", dirName, Directory.Exists(dirName)); } } finally { MountHelper.Unmount(mountedDirName); DeleteDir(mountedDirName, true); } File.AppendAllText(debugFileName, String.Format("Completed scenario {0}", Environment.NewLine)); } } catch (Exception ex) { s_pass = false; Console.WriteLine("Err_432qcp! Exception caught in scenario: {0}", ex); } //@WATCH - potentially dangerous code - can delete the whole drive!! //scenario 3.3: we call delete on the mounted volume - this should only delete the mounted drive? //Current drive is mounted on current directory scenarioDescription = "Scenario 3.3 - we call delete on the mounted volume"; try { if (FileSystemDebugInfo.IsCurrentDriveNTFS()) { File.AppendAllText(debugFileName, String.Format("{0}{1}", scenarioDescription, Environment.NewLine)); mountedDirName = Path.GetFullPath(ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), MountPrefixName)); try { Directory.CreateDirectory(mountedDirName); File.AppendAllText(debugFileName, String.Format("Mounting on {0}{1}{2}", Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName, Environment.NewLine)); MountHelper.Mount(Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName); Directory.Delete(mountedDirName, true); Task.Delay(300).Wait(); } finally { if (!Eval(!Directory.Exists(mountedDirName), "Err_001yph! Directory {0} still exist: {1}", mountedDirName, Directory.Exists(mountedDirName))) { MountHelper.Unmount(mountedDirName); DeleteDir(mountedDirName, true); } } File.AppendAllText(debugFileName, String.Format("Completed scenario {0}", Environment.NewLine)); } } catch (Exception ex) { s_pass = false; Console.WriteLine("Err_386rpj! Exception caught in scenario: {0}", ex); } //@WATCH - potentially dangerous code - can delete the whole drive!! //scenario 3.4: we call delete on parent directory of the mounted volume, the parent directoriy will have some other directories and files //Current drive is mounted on current directory scenarioDescription = "Scenario 3.4 - we call delete on parent directory of the mounted volume, the parent directoriy will have some other directories and files"; try { if (FileSystemDebugInfo.IsCurrentDriveNTFS()) { File.AppendAllText(debugFileName, String.Format("{0}{1}", scenarioDescription, Environment.NewLine)); mountedDirName = null; try { dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName); File.AppendAllText(debugFileName, String.Format("Creating a sub tree at: {0}{1}", dirName, Environment.NewLine)); using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 2, 20)) { Eval(Directory.Exists(dirName), "Err_469yvh! Directory {0} doesn't exist: {1}", dirName, Directory.Exists(dirName)); String[] dirs = fileManager.GetDirectories(1); mountedDirName = Path.GetFullPath(dirs[0]); if (Eval(Directory.GetDirectories(mountedDirName).Length == 0, "Err_974tsg! the sub directory has directories: {0}", mountedDirName)) { foreach (String file in Directory.GetFiles(mountedDirName)) { File.Delete(file); } if (Eval(Directory.GetFiles(mountedDirName).Length == 0, "Err_13ref! the mounted directory has files: {0}", mountedDirName)) { File.AppendAllText(debugFileName, String.Format("Mounting on {0}{1}{2}", Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName, Environment.NewLine)); MountHelper.Mount(Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName); //now lets call delete on the parent directory Directory.Delete(dirName, true); Task.Delay(300).Wait(); Eval(!Directory.Exists(dirName), "Err_006jsf! Directory {0} still exist: {1}", dirName, Directory.Exists(dirName)); Console.WriteLine("Completed Scenario 3.4"); } } } } finally { if (!Eval(!Directory.Exists(mountedDirName), "Err_625ckx! Directory {0} still exist: {1}", mountedDirName, Directory.Exists(mountedDirName))) { MountHelper.Unmount(mountedDirName); DeleteDir(mountedDirName, true); } } File.AppendAllText(debugFileName, String.Format("Completed scenario {0}", Environment.NewLine)); } } catch (Exception ex) { s_pass = false; Console.WriteLine("Err_578tni! Exception caught in scenario: {0}", ex); } //@WATCH - potentially dangerous code - can delete the whole drive!! //scenario 3.5: we call delete on parent directory of the mounted volume, the parent directoriy will have some other directories and files //we call a different directory than the first //Current drive is mounted on current directory scenarioDescription = "Scenario 3.5 - we call delete on parent directory of the mounted volume, the parent directoriy will have some other directories and files"; try { if (FileSystemDebugInfo.IsCurrentDriveNTFS()) { File.AppendAllText(debugFileName, String.Format("{0}{1}", scenarioDescription, Environment.NewLine)); mountedDirName = null; try { dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName); File.AppendAllText(debugFileName, String.Format("Creating a sub tree at: {0}{1}", dirName, Environment.NewLine)); using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 2, 30)) { Eval(Directory.Exists(dirName), "Err_715tdq! Directory {0} doesn't exist: {1}", dirName, Directory.Exists(dirName)); String[] dirs = fileManager.GetDirectories(1); mountedDirName = Path.GetFullPath(dirs[0]); if (dirs.Length > 1) { mountedDirName = Path.GetFullPath(dirs[1]); } if (Eval(Directory.GetDirectories(mountedDirName).Length == 0, "Err_492qwl! the sub directory has directories: {0}", mountedDirName)) { foreach (String file in Directory.GetFiles(mountedDirName)) { File.Delete(file); } if (Eval(Directory.GetFiles(mountedDirName).Length == 0, "Err_904kij! the mounted directory has files: {0}", mountedDirName)) { File.AppendAllText(debugFileName, String.Format("Mounting on {0}{1}{2}", Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName, Environment.NewLine)); MountHelper.Mount(Directory.GetCurrentDirectory().Substring(0, 2), mountedDirName); //now lets call delete on the parent directory Directory.Delete(dirName, true); Task.Delay(300).Wait(); Eval(!Directory.Exists(dirName), "Err_900edl! Directory {0} still exist: {1}", dirName, Directory.Exists(dirName)); Console.WriteLine("Completed Scenario 3.5: {0}", mountedDirName); } } } } finally { if (!Eval(!Directory.Exists(mountedDirName), "Err_462xtc! Directory {0} still exist: {1}", mountedDirName, Directory.Exists(mountedDirName))) { MountHelper.Unmount(mountedDirName); DeleteDir(mountedDirName, true); } } File.AppendAllText(debugFileName, String.Format("Completed scenario {0}", Environment.NewLine)); } } catch (Exception ex) { s_pass = false; Console.WriteLine("Err_471jli! Exception caught in scenario: {0}", ex); } } catch (Exception ex) { s_pass = false; Console.WriteLine("Err_234rsgf! Uncaught exception in RunTest: {0}", ex); } finally { Assert.True(s_pass); } }
public static void File(string file) { var mntFile = MountHelper.SetFilesPath(file); SetBind(mntFile, file); }
public static void AllDirectories() { var list = new List <MountModel>(); var directories = Directory.EnumerateDirectories(Parameter.RepoDirs, "DIR*", SearchOption.TopDirectoryOnly).ToArray(); foreach (var directory in directories) { var mo = new MountModel { SystemPath = MountHelper.GetDirsPath(Path.GetFileName(directory)), RepoDirsPath = directory, Context = MountContext.External, Entity = MountEntity.Directory }; list.Add(mo); } var files = Directory.EnumerateFiles(Parameter.RepoDirs, "FILE*", SearchOption.TopDirectoryOnly).ToArray(); foreach (var file in files) { var mo = new MountModel { SystemPath = MountHelper.GetFilesPath(Path.GetFileName(file)), RepoDirsPath = file, Context = MountContext.External, Entity = MountEntity.File }; list.Add(mo); } ConsoleLogger.Log("directories and files enumerated"); var directoryMounts = list.Where(m => m.Entity == MountEntity.Directory).ToList(); foreach (var directoryMount in directoryMounts) { try { var dir = directoryMount.SystemPath.Replace("\\", ""); var mntDir = directoryMount.RepoDirsPath; if (MountHelper.IsAlreadyMounted(dir) == false) { Directory.CreateDirectory(dir); Directory.CreateDirectory(mntDir); SetBind(mntDir, dir); ConsoleLogger.Log($"mount {mntDir} -> {dir}"); } } catch (Exception ex) { ConsoleLogger.Warn(ex.Message); } } ConsoleLogger.Log("directories mounted"); var fileMounts = list.Where(m => m.Entity == MountEntity.File).ToList(); foreach (var fileMount in fileMounts) { var file = fileMount.SystemPath.Replace("\\", ""); var mntFile = fileMount.RepoDirsPath; if (System.IO.File.Exists(mntFile)) { var path = Path.GetDirectoryName(file); var mntPath = Path.GetDirectoryName(mntFile); if (MountHelper.IsAlreadyMounted(file) == false) { Bash.Execute($"mkdir -p {path}", false); Bash.Execute($"mkdir -p {mntPath}", false); if (!System.IO.File.Exists(file)) { Bash.Execute($"cp {mntFile} {file}", false); } SetBind(mntFile, file); ConsoleLogger.Log($"mount {mntFile} -> {file}"); } } } ConsoleLogger.Log("files mounted"); }
public void Setup(string appName) { ConsoleLogger.Log("========================================="); ConsoleLogger.Log($"Installing {appName}"); var apps = Detect(); var appInfo = apps.FirstOrDefault(_ => _.Name == appName); if (appInfo != null) { ConsoleLogger.Log($"{appName} info found"); var name = appInfo.Values.FirstOrDefault(_ => _.Key == "name").Value; var repoPath = appInfo.Repository; var timestamp = DateTime.Now.ToString("yyyyMMdd"); var squashName = $"DIR_framework_{name.ToLower().Replace("/", "-")}-aosApps-{timestamp}-std-x86_64.squashfs.xz"; ConsoleLogger.Log($"name => {name}"); ConsoleLogger.Log($"repoPath => {repoPath}"); ConsoleLogger.Log($"timestamp => {timestamp}"); ConsoleLogger.Log($"squashName => {squashName}"); if (File.Exists($"{repoPath}/{squashName}")) { File.Delete($"{repoPath}/{squashName}"); } ConsoleLogger.Log($">> mksquashfs {repoPath}/{name} {repoPath}/{squashName} -comp xz -Xbcj x86 -Xdict-size 75%"); _bash.Execute($"mksquashfs {repoPath}/{name} {repoPath}/{squashName} -comp xz -Xbcj x86 -Xdict-size 75%", false); ConsoleLogger.Log("compressed fs for application created"); var activeVersionPath = $"{repoPath}/active-version"; ConsoleLogger.Log($"activeVersionPath => {activeVersionPath}"); _bash.Execute($"ln -s {squashName} {activeVersionPath}", false); ConsoleLogger.Log("link created"); var frameworkDir = $"/framework/{name.ToLower().Replace("/", "-")}"; ConsoleLogger.Log($"frameworkDir => {frameworkDir}"); Directory.CreateDirectory("/framework"); Directory.CreateDirectory(frameworkDir); ConsoleLogger.Log("framework directories created"); if (MountHelper.IsAlreadyMounted(frameworkDir) == false) { ConsoleLogger.Log($">> mount {activeVersionPath} {frameworkDir}"); _bash.Execute($"mount {activeVersionPath} {frameworkDir}", false); ConsoleLogger.Log("application fs mounted"); } var prepareUnitName = _appsUnits.CreatePrepareUnit(name, frameworkDir); var mountUnitName = _appsUnits.CreateMountUnit(name, activeVersionPath, frameworkDir); ConsoleLogger.Log($"prepareUnitName => {prepareUnitName}"); ConsoleLogger.Log($"mountUnitName => {mountUnitName}"); ConsoleLogger.Log("units created"); var launcherUnitName = new List <string>(); var exes = appInfo.Values.Where(_ => _.Key == "app_exe").Select(_ => _.Value).ToList(); var frameworkDirContent = Directory.EnumerateFiles(frameworkDir, "*", SearchOption.AllDirectories).ToList(); foreach (var exe in exes) { ConsoleLogger.Log($"exe => {exe}"); var exePath = frameworkDirContent.FirstOrDefault(_ => _.EndsWith(exe)); ConsoleLogger.Log($"exePath? => {exePath}"); if (File.Exists(exePath)) { var lun = _appsUnits.CreateLauncherUnit(name, exe, exePath); ConsoleLogger.Log($"launcherUnitName => {lun}"); launcherUnitName.Add(lun); } } ConsoleLogger.Log("launcher units created"); var mounts = appInfo.Values.Where(_ => _.Key == "app_path").Select(_ => _.Value).ToList(); foreach (var mount in mounts) { ConsoleLogger.Log($"workingDir => {mount}"); _mount.Dir(mount); } ConsoleLogger.Log("working directories created and mounted"); var appsConfiguration = new AppsConfiguration(); var tryGet = appsConfiguration.Get().Apps.FirstOrDefault(_ => _.Name == name); if (tryGet != null) { return; } var model = new ApplicationModel { Name = name, RepositoryName = repoPath, Exes = exes, WorkingDirectories = mounts, UnitPrepare = prepareUnitName, UnitMount = mountUnitName, UnitLauncher = launcherUnitName }; appsConfiguration.AddApp(model); } }