From a391864356e3a9eaf514b6bc74a68928b48e2814 Mon Sep 17 00:00:00 2001 From: Charles Betros Date: Sat, 14 Nov 2015 21:41:30 -0600 Subject: [PATCH] More filesystem work. --- Tests/Cosmos.Kernel.Tests.Fat/Kernel.cs | 66 +++- .../DefaultEngineConfiguration.cs | 4 +- .../System/IO/DirectoryImpl.cs | 76 ++++- .../System/IO/DirectoryInfoImpl.cs | 310 +++++++++++++++--- .../Cosmos.System.Plugs/System/IO/PathImpl.cs | 7 - source/Cosmos.System/FileSystem/CosmosVFS.cs | 29 +- .../FileSystem/VFS/VFSManager.cs | 1 + 7 files changed, 392 insertions(+), 101 deletions(-) diff --git a/Tests/Cosmos.Kernel.Tests.Fat/Kernel.cs b/Tests/Cosmos.Kernel.Tests.Fat/Kernel.cs index 39bb946de..1b00ae8be 100644 --- a/Tests/Cosmos.Kernel.Tests.Fat/Kernel.cs +++ b/Tests/Cosmos.Kernel.Tests.Fat/Kernel.cs @@ -33,6 +33,42 @@ namespace Cosmos.Kernel.Tests.Fat bool xTest; string xContents; + + mDebugger.Send("Get parent:"); + var xParent = Directory.GetParent(@"0:\test"); + Assert.IsTrue(xParent.Name == @"0:\", "Failed to get directory parent."); + + mDebugger.Send("Create directory:"); + var xDirectory = Directory.CreateDirectory(@"0:\test2"); + bool xExists = Directory.Exists(@"0:\test2"); + Assert.IsTrue(xExists, "Failed to create a new directory."); + + //mDebugger.Send("Get files:"); + //var xFiles = Directory.GetFiles(@"0:\"); + //mDebugger.Send("Found " + xFiles.Length + " files."); + //if (xFiles.Length > 0) + //{ + // mDebugger.Send("-- File list --"); + // for (int i = 0; i < xFiles.Length; i++) + // { + // mDebugger.Send("File: " + xFiles[i]); + // } + //} + //Assert.IsTrue(xFiles.Length > 0, "Failed to get files from the directory."); + + //mDebugger.Send("Get directories:"); + //var xDirectories = Directory.GetDirectories(@"0:\"); + //mDebugger.Send("Found " + xDirectories.Length + " directories."); + //if (xDirectories.Length > 0) + //{ + // mDebugger.Send("-- Directory list --"); + // for (int i = 0; i < xDirectories.Length; i++) + // { + // mDebugger.Send("Directory: " + xDirectories[i]); + // } + //} + //Assert.IsTrue(xDirectories.Length > 0, "Failed to get directories from the directory."); + //Assert.IsTrue(Path.GetDirectoryName(@"0:\test") == @"0:\", @"Path.GetDirectoryName(@'0:\test') == @'0:\'"); //Assert.IsTrue(Path.GetFileName(@"0:\test") == @"test", @"Path.GetFileName(@'0:\test') == @'test'"); @@ -70,26 +106,26 @@ namespace Cosmos.Kernel.Tests.Fat //mDebugger.Send(xContents); //Assert.IsTrue(xContents == "Hello", "Contents of Kudzu.txt was read incorrectly!"); - using (var xFS = new FileStream(@"0:\Kudzu.txt", FileMode.Create)) - { - mDebugger.Send("Start writing"); - var xStr = "Test FAT Write."; - var xBuff = xStr.GetUtf8Bytes(0, (uint)xStr.Length); - xFS.Write(xBuff, 0, xBuff.Length); - mDebugger.Send("---- Data written"); - xFS.Position = 0; - xFS.Read(xBuff, 0, xBuff.Length); - mDebugger.Send(xBuff.GetUtf8String(0, (uint)xBuff.Length)); - } + //using (var xFS = new FileStream(@"0:\Kudzu.txt", FileMode.Create)) + //{ + // mDebugger.Send("Start writing"); + // var xStr = "Test FAT Write."; + // var xBuff = xStr.GetUtf8Bytes(0, (uint)xStr.Length); + // xFS.Write(xBuff, 0, xBuff.Length); + // mDebugger.Send("---- Data written"); + // xFS.Position = 0; + // xFS.Read(xBuff, 0, xBuff.Length); + // mDebugger.Send(xBuff.GetUtf8String(0, (uint)xBuff.Length)); + //} //mDebugger.Send("Write to file now"); //File.WriteAllText(@"0:\Kudzu.txt", "Test FAT write."); //mDebugger.Send("Text written"); - xContents = File.ReadAllText(@"0:\Kudzu.txt"); - mDebugger.Send("Contents retrieved after writing"); - mDebugger.Send(xContents); - Assert.IsTrue(xContents == "Test FAT write.", "Contents of Kudzu.txt was written incorrectly!"); + //xContents = File.ReadAllText(@"0:\Kudzu.txt"); + //mDebugger.Send("Contents retrieved after writing"); + //mDebugger.Send(xContents); + //Assert.IsTrue(xContents == "Test FAT write.", "Contents of Kudzu.txt was written incorrectly!"); TestController.Completed(); } diff --git a/Tests/Cosmos.TestRunner.Core/DefaultEngineConfiguration.cs b/Tests/Cosmos.TestRunner.Core/DefaultEngineConfiguration.cs index e75d28f55..60b5cdd32 100644 --- a/Tests/Cosmos.TestRunner.Core/DefaultEngineConfiguration.cs +++ b/Tests/Cosmos.TestRunner.Core/DefaultEngineConfiguration.cs @@ -41,8 +41,8 @@ namespace Cosmos.TestRunner.Core //engine.AddKernel(typeof(Cosmos.Compiler.Tests.MultidimensionalArrays.Kernel).Assembly.Location); // Experimental stuff: - //engine.AddKernel(typeof(Cosmos.Kernel.Tests.Fat.Kernel).Assembly.Location); - engine.AddKernel(typeof(Cosmos.Kernel.Tests.FileSystemPlugs.Kernel).Assembly.Location); + engine.AddKernel(typeof(Cosmos.Kernel.Tests.Fat.Kernel).Assembly.Location); + //engine.AddKernel(typeof(Cosmos.Kernel.Tests.FileSystemPlugs.Kernel).Assembly.Location); // end of known bugs diff --git a/source/Cosmos.System.Plugs/System/IO/DirectoryImpl.cs b/source/Cosmos.System.Plugs/System/IO/DirectoryImpl.cs index 824b12e7a..6fe37baec 100644 --- a/source/Cosmos.System.Plugs/System/IO/DirectoryImpl.cs +++ b/source/Cosmos.System.Plugs/System/IO/DirectoryImpl.cs @@ -1,13 +1,14 @@ using System; using System.IO; +using System.Collections.Generic; using Cosmos.IL2CPU.Plugs; +using Cosmos.System.FileSystem; +using Cosmos.System.FileSystem.Listing; using Cosmos.System.FileSystem.VFS; namespace Cosmos.System.Plugs.System.IO { - using Cosmos.System.FileSystem; - [Plug(Target = typeof(Directory))] public static class DirectoryImpl { @@ -16,28 +17,46 @@ namespace Cosmos.System.Plugs.System.IO public static string GetCurrentDirectory() { FatHelpers.Debug("-- Directory.GetCurrentDirectory --"); - return mCurrentDirectory; } public static void SetCurrentDirectory(string aPath) { FatHelpers.Debug("-- Directory.SetCurrentDirectory --"); - mCurrentDirectory = aPath; } public static bool Exists(string aPath) { FatHelpers.Debug("-- Directory.Exists --"); - return VFSManager.DirectoryExists(aPath); } + public static DirectoryInfo CreateDirectory(string aPath) + { + FatHelpers.Debug("-- Directory.CreateDirectory --"); + if (aPath == null) + { + throw new ArgumentNullException("aPath"); + } + + if (aPath.Length == 0) + { + throw new ArgumentException("Path must not be empty.", "aPath"); + } + + var xEntry = VFSManager.CreateDirectory(aPath); + if (xEntry == null) + { + return null; + } + + return new DirectoryInfo(aPath); + } + public static DirectoryInfo GetParent(string aPath) { FatHelpers.Debug("-- Directory.GetParent --"); - if (aPath == null) { throw new ArgumentNullException("aPath"); @@ -55,7 +74,50 @@ namespace Cosmos.System.Plugs.System.IO return null; } - return new DirectoryInfo(xName); + return new DirectoryInfo(xFullPath); } + + public static string[] GetDirectories(string aPath) + { + FatHelpers.Debug("-- Directory.GetDirectories --"); + if (aPath == null) + { + throw new ArgumentNullException(aPath); + } + + var xDirectories = new List(); + var xEntries = VFSManager.GetDirectoryListing(aPath); + for (int i = 0; i < xEntries.Count; i++) + { + if (xEntries[i].EntryType == DirectoryEntryTypeEnum.Directory) + { + xDirectories.Add(xEntries[i].Name); + } + } + + return xDirectories.ToArray(); + } + + public static string[] GetFiles(string aPath) + { + FatHelpers.Debug("-- Directory.GetFiles --"); + if (aPath == null) + { + throw new ArgumentNullException(aPath); + } + + var xFiles = new List(); + var xEntries = VFSManager.GetDirectoryListing(aPath); + for (int i = 0; i < xEntries.Count; i++) + { + if (xEntries[i].EntryType == DirectoryEntryTypeEnum.File) + { + xFiles.Add(xEntries[i].Name); + } + } + + return xFiles.ToArray(); + } + } } \ No newline at end of file diff --git a/source/Cosmos.System.Plugs/System/IO/DirectoryInfoImpl.cs b/source/Cosmos.System.Plugs/System/IO/DirectoryInfoImpl.cs index 21ecdb673..c7d7cc476 100644 --- a/source/Cosmos.System.Plugs/System/IO/DirectoryInfoImpl.cs +++ b/source/Cosmos.System.Plugs/System/IO/DirectoryInfoImpl.cs @@ -6,82 +6,302 @@ using Cosmos.System.FileSystem.VFS; namespace Cosmos.System.Plugs.System.IO { - + using Cosmos.System.FileSystem; + + using global::System; + using global::System.Collections.Generic; [Plug(Target = typeof(DirectoryInfo))] [PlugField(FieldId = "$$Storage$$", FieldType = typeof(DirectoryEntry))] [PlugField(FieldId = "$$FullPath$$", FieldType = typeof(string))] public static class DirectoryInfoImpl { - [PlugMethod(/*Signature = "System_Void__System_IO_DirectoryInfo__ctor_System_String_"*/)] - public static void Ctor(DirectoryInfo aThis, string aPath) + public static void Ctor(DirectoryInfo aThis, string aPath, + [FieldAccess(Name = "$$Storage$$")] ref DirectoryEntry aStorage, + [FieldAccess(Name = "$$FullPath$$")] ref string aFullPath) { - //if (aPath == null) - // throw new ArgumentNullException("aPath is null in DirectoryInfo ctor"); + if (aPath == null) + { + throw new ArgumentNullException("aPath is null in DirectoryInfo ctor"); + } - ////Search for directory - ////if (!VFSManager.DirectoryExists(aPath)) - //// throw new DirectoryNotFoundException("Unable to find directory " + aPath); + if (!VFSManager.DirectoryExists(aPath)) + { + throw new DirectoryNotFoundException("Unable to find directory " + aPath); + } - ////If it exists, then get the directory as a FilesystemEntry - //aStorage = VFSManager.GetDirectoryEntry(aPath); - //aFullPath = aPath; + aStorage = VFSManager.GetDirectory(aPath); + aFullPath = aPath; + } + + public static string get_Name(DirectoryInfo aThis, [FieldAccess(Name = "$$Storage$$")] ref DirectoryEntry aStorage) + { + FatHelpers.Debug("-- DirectoryInfo.get_Name --"); + throw new NotImplementedException(); + } + + public static DirectoryInfo get_Parent(DirectoryInfo aThis) + { + FatHelpers.Debug("-- DirectoryInfo.get_Parent --"); + throw new NotImplementedException(); + } + + public static DirectoryInfo get_Root(DirectoryInfo aThis) + { + FatHelpers.Debug("-- DirectoryInfo.get_Root --"); + throw new NotImplementedException(); } public static bool get_Exists(DirectoryInfo aThis, [FieldAccess(Name = "$$Storage$$")] ref DirectoryEntry aStorage) { + FatHelpers.Debug("-- DirectoryInfo.get_Exists --"); return VFSManager.DirectoryExists(aStorage); } - //public static string FullName - //{ - // get - // { - // return ".FullName isn't implemented yet"; - // } - //} - - //public static string get_FullName(DirectoryInfo aThis, [FieldAccess(Name = "$$FullPath$$")] String aFullPath) - //{ - // //TODO: return FULL name - // return aFullPath; - //} - - public static string get_Name(DirectoryInfo aThis, [FieldAccess(Name = "$$Storage$$")] ref DirectoryEntry aStorage) + public static FileInfo[] GetFiles(DirectoryInfo aThis, string searchPattern, [FieldAccess(Name = "$$Storage$$")] ref DirectoryEntry aStorage) { - return aStorage.Name; + if (searchPattern == null) + { + throw new ArgumentNullException("searchPattern"); + } + + throw new NotImplementedException(); + } + + public static FileInfo[] GetFiles(DirectoryInfo aThis, string searchPattern, SearchOption searchOption, [FieldAccess(Name = "$$Storage$$")] ref DirectoryEntry aStorage) + { + if (searchPattern == null) + { + throw new ArgumentNullException("searchPattern"); + } + if ((searchOption != SearchOption.TopDirectoryOnly) && (searchOption != SearchOption.AllDirectories)) + { + throw new ArgumentOutOfRangeException("searchOption", "Argument is out of range."); + } + + throw new NotImplementedException(); } public static FileInfo[] GetFiles(DirectoryInfo aThis, [FieldAccess(Name = "$$Storage$$")] ref DirectoryEntry aStorage) { return null; + } - /* - List xFiles = new List(); - var xEntries = VFSManager.GetFiles(aStorage); - - foreach (FileSystem.Listing.Base xEntry in xEntries) + public static DirectoryInfo CreateSubdirectory(DirectoryInfo aThis, string path, [FieldAccess(Name = "$$Storage$$")] ref DirectoryEntry aStorage) + { + if (path == null) { - xFiles.Add(new FileInfo(xEntry.Name)); + throw new ArgumentNullException("path"); } - return xFiles.ToArray(); - - //Alternative implementation - //var xEntries = VFSManager.GetFiles(aStorage); - //FileInfo[] files = new FileInfo[xEntries.Length]; - //for (int i = 0; i < xEntries.Length; i++) - //{ - // files[i] = new FileInfo(xEntries[i].Name); - //} - - //return files; - */ + throw new NotImplementedException(); } public static string ToString([FieldAccess(Name = "$$Path$$")] ref string aPath) { return "DirectoryInfo.ToString() not yet implemented"; } + + //public static void Create() + //{ + + //} + + //public static DirectoryInfo[] GetDirectories() + //{ + // throw new NotImplementedException(); + //} + + //public static FileSystemInfo[] GetFileSystemInfos(String searchPattern) + //{ + // if (searchPattern == null) + // { + // throw new ArgumentNullException("searchPattern"); + // } + + // throw new NotImplementedException(); + //} + + //public static FileSystemInfo[] GetFileSystemInfos(String searchPattern, SearchOption searchOption) + //{ + // if (searchPattern == null) + // { + // throw new ArgumentNullException("searchPattern"); + // } + // if ((searchOption != SearchOption.TopDirectoryOnly) && (searchOption != SearchOption.AllDirectories)) + // { + // throw new ArgumentOutOfRangeException("searchOption", "Argument is out of range."); + // } + + // throw new NotImplementedException(); + //} + + //public static FileSystemInfo[] GetFileSystemInfos() + //{ + // throw new NotImplementedException(); + //} + + //public static DirectoryInfo[] GetDirectories(String searchPattern) + //{ + // if (searchPattern == null) + // { + // throw new ArgumentNullException("searchPattern"); + // } + + // throw new NotImplementedException(); + //} + + //public static DirectoryInfo[] GetDirectories(String searchPattern, SearchOption searchOption) + //{ + // if (searchPattern == null) + // { + // throw new ArgumentNullException("searchPattern"); + // } + // if ((searchOption != SearchOption.TopDirectoryOnly) && (searchOption != SearchOption.AllDirectories)) + // { + // throw new ArgumentOutOfRangeException("searchOption", "Argument is out of range."); + // } + + // throw new NotImplementedException(); + //} + + //public static IEnumerable EnumerateDirectories() + //{ + // throw new NotImplementedException(); + //} + + //public static IEnumerable EnumerateDirectories(String searchPattern) + //{ + // if (searchPattern == null) + // { + // throw new ArgumentNullException("searchPattern"); + // } + + // throw new NotImplementedException(); + //} + + //public static IEnumerable EnumerateDirectories(String searchPattern, SearchOption searchOption) + //{ + // if (searchPattern == null) + // { + // throw new ArgumentNullException("searchPattern"); + // } + // if ((searchOption != SearchOption.TopDirectoryOnly) && (searchOption != SearchOption.AllDirectories)) + // { + // throw new ArgumentOutOfRangeException("searchOption", "Argument is out of range."); + // } + + // throw new NotImplementedException(); + //} + + //public static IEnumerable EnumerateFiles() + //{ + // throw new NotImplementedException(); + //} + + //public static IEnumerable EnumerateFiles(String searchPattern) + //{ + // if (searchPattern == null) + // { + // throw new ArgumentNullException("searchPattern"); + // } + + // throw new NotImplementedException(); + //} + + //public static IEnumerable EnumerateFiles(String searchPattern, SearchOption searchOption) + //{ + // if (searchPattern == null) + // { + // throw new ArgumentNullException("searchPattern"); + // } + // if ((searchOption != SearchOption.TopDirectoryOnly) && (searchOption != SearchOption.AllDirectories)) + // { + // throw new ArgumentOutOfRangeException("searchOption", "Argument is out of range."); + // } + + // throw new NotImplementedException(); + //} + + //public static IEnumerable EnumerateFileSystemInfos() + //{ + // throw new NotImplementedException(); + //} + + //public static IEnumerable EnumerateFileSystemInfos(String searchPattern) + //{ + // if (searchPattern == null) + // { + // throw new ArgumentNullException("searchPattern"); + // } + + // throw new NotImplementedException(); + //} + + //public static IEnumerable EnumerateFileSystemInfos(string aSearchPattern, SearchOption aSearchOption) + //{ + // if (aSearchPattern == null) + // { + // throw new ArgumentNullException("searchPattern"); + // } + // if ((aSearchOption != SearchOption.TopDirectoryOnly) && (aSearchOption != SearchOption.AllDirectories)) + // { + // throw new ArgumentOutOfRangeException("searchOption", "Argument is out of range."); + // } + + // throw new NotImplementedException(); + //} + + //public static void MoveTo(String destDirName) + //{ + // if (destDirName == null) + // { + // throw new ArgumentNullException("destDirName"); + // } + // if (destDirName.Length == 0) + // { + // throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "destDirName"); + // } + + // String fullDestDirName = Path.GetFullPathInternal(destDirName); + // if (!fullDestDirName.EndsWith(Path.DirectorySeparatorChar)) + // fullDestDirName = fullDestDirName + Path.DirectorySeparatorChar; + + // String fullSourcePath; + // if (FullPath.EndsWith(Path.DirectorySeparatorChar)) + // fullSourcePath = FullPath; + // else + // fullSourcePath = FullPath + Path.DirectorySeparatorChar; + + // if (String.Compare(fullSourcePath, fullDestDirName, StringComparison.OrdinalIgnoreCase) == 0) + // throw new IOException(Environment.GetResourceString("IO.IO_SourceDestMustBeDifferent")); + + // String sourceRoot = Path.GetPathRoot(fullSourcePath); + // String destinationRoot = Path.GetPathRoot(fullDestDirName); + + // if (String.Compare(sourceRoot, destinationRoot, StringComparison.OrdinalIgnoreCase) != 0) + // throw new IOException(Environment.GetResourceString("IO.IO_SourceDestMustHaveSameRoot")); + + // // TODO: Do the move + + + // FullPath = fullDestDirName; + // OriginalPath = destDirName; + // DisplayPath = GetDisplayName(OriginalPath, FullPath); + //} + + //public static void Delete() + //{ + // throw new NotImplementedException(); + // //Directory.Delete(FullPath, OriginalPath, false, true); + //} + + //public static void Delete(bool recursive) + //{ + // throw new NotImplementedException(); + // //Directory.Delete(FullPath, OriginalPath, recursive, true); + //} + } } + diff --git a/source/Cosmos.System.Plugs/System/IO/PathImpl.cs b/source/Cosmos.System.Plugs/System/IO/PathImpl.cs index e67677c72..ddd5be977 100644 --- a/source/Cosmos.System.Plugs/System/IO/PathImpl.cs +++ b/source/Cosmos.System.Plugs/System/IO/PathImpl.cs @@ -250,13 +250,6 @@ namespace Cosmos.System.Plugs.System.IO { FatHelpers.Debug("-- Path.GetFullPath --"); - return GetFullPathInternal(aPath); - } - - public static string GetFullPathInternal(string aPath) - { - FatHelpers.Debug("-- Path.GetFullPathInternal --"); - if (aPath == null) { throw new ArgumentNullException("aPath"); diff --git a/source/Cosmos.System/FileSystem/CosmosVFS.cs b/source/Cosmos.System/FileSystem/CosmosVFS.cs index e77adc1f4..87ccd35ea 100644 --- a/source/Cosmos.System/FileSystem/CosmosVFS.cs +++ b/source/Cosmos.System/FileSystem/CosmosVFS.cs @@ -9,32 +9,10 @@ using Cosmos.System.FileSystem.VFS; namespace Cosmos.System.FileSystem { - [Serializable] - public struct KVP - { - private readonly TKey key; - private readonly TValue value; - - public KVP(TKey key, TValue value) - { - this.key = key; - this.value = value; - } - - public TKey Key - { - get { return key; } - } - - public TValue Value - { - get { return value; } - } - } - public class CosmosVFS : VFSBase { private List mPartitions; + private List mFileSystems; public override void Initialize() @@ -164,6 +142,7 @@ namespace Cosmos.System.FileSystem string xMessage = string.Concat("Initialized ", mFileSystems.Count, "filesystem(s)..."); global::System.Console.WriteLine(xMessage); mFileSystems[i].DisplayFileSystemInfo(); + Directory.SetCurrentDirectory(xRootPath); } else { @@ -237,6 +216,6 @@ namespace Cosmos.System.FileSystem { return aFS.GetRootDirectory(); } - } -} + } +} \ No newline at end of file diff --git a/source/Cosmos.System/FileSystem/VFS/VFSManager.cs b/source/Cosmos.System/FileSystem/VFS/VFSManager.cs index eaf120161..b9c4ca4bc 100644 --- a/source/Cosmos.System/FileSystem/VFS/VFSManager.cs +++ b/source/Cosmos.System/FileSystem/VFS/VFSManager.cs @@ -387,6 +387,7 @@ namespace Cosmos.System.FileSystem.VFS { xPath = xEntry + xEntry.Name; xEntry = aEntry.Parent; + FatHelpers.Debug("-- VFSManager.GetFullPath : xPath = " + xPath + " --"); } return Path.GetFullPath(xPath);