From 51fac7ada4a18c48cd5ddd7d44aa2e54958b134a Mon Sep 17 00:00:00 2001 From: fanoI Date: Sun, 10 Dec 2017 20:35:29 +0100 Subject: [PATCH] Plugged FileInfo - Added tests for FileInfo - Added tests for Binary Reader/Writer - Unified StreamReader and StreamWriter in a unique test --- .../System/EncodingTest.cs | 8 ++ Tests/Cosmos.Kernel.Tests.Fat/Kernel.cs | 7 +- .../System.IO/BinaryReaderTest.cs | 65 ---------- .../System.IO/BinaryWriterBinaryReaderTest.cs | 65 ++++++++++ .../System.IO/BinaryWriterTest.cs | 48 ------- .../System.IO/DirectoryInfoTest.cs | 28 +++- .../System.IO/FileInfoTest.cs | 121 ++++++++++++++++++ .../System.IO/FileTest.cs | 30 +++-- .../System.IO/StreamReaderTest.cs | 35 ----- ...est.cs => StreamWriterStreamReaderTest.cs} | 2 +- .../System/IO/CosmosFileSystem.cs | 2 +- .../System/IO/DirectoryInfoImpl.cs | 56 ++++---- .../System/IO/FileImpl.cs | 32 ----- .../System/IO/FileInfoImpl.cs | 29 +++++ .../System/IO/FileLoadExceptionImpl.cs | 23 ++++ .../System/IO/FileNotFoundExceptionImpl.cs | 17 +++ .../System/IO/FileStreamImpl.cs | 19 ++- 17 files changed, 362 insertions(+), 225 deletions(-) delete mode 100644 Tests/Cosmos.Kernel.Tests.Fat/System.IO/BinaryReaderTest.cs create mode 100644 Tests/Cosmos.Kernel.Tests.Fat/System.IO/BinaryWriterBinaryReaderTest.cs delete mode 100644 Tests/Cosmos.Kernel.Tests.Fat/System.IO/BinaryWriterTest.cs create mode 100644 Tests/Cosmos.Kernel.Tests.Fat/System.IO/FileInfoTest.cs delete mode 100644 Tests/Cosmos.Kernel.Tests.Fat/System.IO/StreamReaderTest.cs rename Tests/Cosmos.Kernel.Tests.Fat/System.IO/{StreamWriterTest.cs => StreamWriterStreamReaderTest.cs} (98%) create mode 100644 source/Cosmos.System2_Plugs/System/IO/FileInfoImpl.cs create mode 100644 source/Cosmos.System2_Plugs/System/IO/FileLoadExceptionImpl.cs create mode 100644 source/Cosmos.System2_Plugs/System/IO/FileNotFoundExceptionImpl.cs diff --git a/Tests/Cosmos.Compiler.Tests.Bcl/System/EncodingTest.cs b/Tests/Cosmos.Compiler.Tests.Bcl/System/EncodingTest.cs index 008adf914..5d1c1ae0a 100644 --- a/Tests/Cosmos.Compiler.Tests.Bcl/System/EncodingTest.cs +++ b/Tests/Cosmos.Compiler.Tests.Bcl/System/EncodingTest.cs @@ -102,6 +102,14 @@ namespace Cosmos.Compiler.Tests.Bcl.System text = Encoder.GetString(UTF8GothicText); expectedText = "𐍈"; Assert.IsTrue((text == expectedText), "UTF8 Decoding of Gothic text failed strings different"); + + /* But this not work is searching '437' in some native Windows tables, we need plugs for this sadly! */ + //Encoder = Encoding.GetEncoding(437); + //text = "àèìòù"; + //result = Encoder.GetBytes(text); + //expectedResult = new byte[] { 0x85, 0x8A, 0x8D, 0x95, 0x97 }; + //Assert.IsTrue(EqualityHelper.ByteArrayAreEquals(result, expectedResult), "CP437 Encoding of accents text failed byte arrays different"); + } } } diff --git a/Tests/Cosmos.Kernel.Tests.Fat/Kernel.cs b/Tests/Cosmos.Kernel.Tests.Fat/Kernel.cs index fd2696c75..c870866d0 100644 --- a/Tests/Cosmos.Kernel.Tests.Fat/Kernel.cs +++ b/Tests/Cosmos.Kernel.Tests.Fat/Kernel.cs @@ -40,10 +40,9 @@ namespace Cosmos.Kernel.Tests.Fat FileTest.Execute(mDebugger); FileStreamTest.Execute(mDebugger); DirectoryInfoTest.Execute(mDebugger); - StreamWriterTest.Execute(mDebugger); - //StreamReaderTest.Execute(mDebugger); - //BinaryWriterTest.Execute(mDebugger); - //BinaryReaderTest.Execute(mDebugger); + StreamWriterStreamReaderTest.Execute(mDebugger); + BinaryWriterBinaryReaderTest.Execute(mDebugger); + FileInfoTest.Execute(mDebugger); TestController.Completed(); } diff --git a/Tests/Cosmos.Kernel.Tests.Fat/System.IO/BinaryReaderTest.cs b/Tests/Cosmos.Kernel.Tests.Fat/System.IO/BinaryReaderTest.cs deleted file mode 100644 index 8f95d7bbb..000000000 --- a/Tests/Cosmos.Kernel.Tests.Fat/System.IO/BinaryReaderTest.cs +++ /dev/null @@ -1,65 +0,0 @@ -using System.IO; -using Cosmos.TestRunner; -using Cosmos.Debug.Kernel; -using static Cosmos.Kernel.Tests.Fat.System.IO.HelperMethods; - -namespace Cosmos.Kernel.Tests.Fat.System.IO -{ - class BinaryReaderTest - { - // TODO change this to only a test (BinaryIOTest?) together with BinaryWriter - static private byte[] xBytes = new byte[16] - { - 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, - 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF - }; - - /// - /// Tests System.IO.BinaryWriter plugs. - /// - public static void Execute(Debugger mDebugger) - { - /*Error Stack Trace: - * - *Error: Exception: System.Exception: Error compiling method 'SystemVoidSystemIOMemoryStreamDisposeSystemBoolean': System.NullReferenceException: Object reference not set to an instance of an object. - *at Cosmos.IL2CPU.X86.IL.Leave.Execute(MethodInfo aMethod, ILOpCode aOpCode) in Cosmos\source\Cosmos.IL2CPU\IL\Leave.cs:line 17 - *at Cosmos.IL2CPU.AppAssembler.EmitInstructions(MethodInfo aMethod, List`1 aCurrentGroup, Boolean & amp; emitINT3) in Cosmos\source\Cosmos.IL2CPU\AppAssembler.cs:line 667 - *at Cosmos.IL2CPU.AppAssembler.ProcessMethod(MethodInfo aMethod, List`1 aOpCodes) in Cosmos\source\Cosmos.IL2CPU\AppAssembler.cs:line 533-- - >; System.NullReferenceException: Object reference not set to an instance of an object. - *at Cosmos.IL2CPU.X86.IL.Leave.Execute(MethodInfo aMethod, ILOpCode aOpCode) in Cosmos\source\Cosmos.IL2CPU\IL\Leave.cs:line 17 - *at Cosmos.IL2CPU.AppAssembler.EmitInstructions(MethodInfo aMethod, List`1 aCurrentGroup, Boolean & amp; emitINT3) in Cosmos\source\Cosmos.IL2CPU\AppAssembler.cs:line 667 - *at Cosmos.IL2CPU.AppAssembler.ProcessMethod(MethodInfo aMethod, List`1 aOpCodes) in Cosmos\source\Cosmos.IL2CPU\AppAssembler.cs:line 533 - *--- End of inner exception stack trace --- - *at Cosmos.IL2CPU.AppAssembler.ProcessMethod(MethodInfo aMethod, List`1 aOpCodes) in Cosmos\source\Cosmos.IL2CPU\AppAssembler.cs:line 540 - *at Cosmos.IL2CPU.ILScanner.Assemble() in Cosmos\source\Cosmos.IL2CPU\ILScanner.cs:line 946 - *at Cosmos.IL2CPU.ILScanner.Execute(MethodBase aStartMethod) in Cosmos\source\Cosmos.IL2CPU\ILScanner.cs:line 247 - *at Cosmos.IL2CPU.CompilerEngine.Execute() in Cosmos\source\Cosmos.IL2CPU\CompilerEngine.cs:line 252 - * - */ - - mDebugger.Send("START TEST: BinaryReader"); - mDebugger.Send("Creating FileStream: FileMode.Open"); - - using (var xFS = new FileStream(@"0:\binary.bin", FileMode.Open)) - { - mDebugger.Send("Creating BinaryReader"); - - using (var xBR = new BinaryReader(xFS)) - { - if (xFS != null) - { - mDebugger.Send("Start reading"); - - byte[] xBuffer = xBR.ReadBytes(xBytes.Length); - Assert.IsTrue(ByteArrayAreEquals(xBytes, xBuffer), "Bytes changed during BinaryWriter and BinaryReader opeartions on FileStream"); - } - else - { - Assert.IsTrue(false, @"Failed to create StreamReader for file 0:\binary.bin"); - } - } - } - - mDebugger.Send("END TEST"); - } - } -} diff --git a/Tests/Cosmos.Kernel.Tests.Fat/System.IO/BinaryWriterBinaryReaderTest.cs b/Tests/Cosmos.Kernel.Tests.Fat/System.IO/BinaryWriterBinaryReaderTest.cs new file mode 100644 index 000000000..e672a2633 --- /dev/null +++ b/Tests/Cosmos.Kernel.Tests.Fat/System.IO/BinaryWriterBinaryReaderTest.cs @@ -0,0 +1,65 @@ +using System.IO; +using Cosmos.TestRunner; +using Cosmos.Debug.Kernel; +using static Cosmos.Kernel.Tests.Fat.System.IO.HelperMethods; + +namespace Cosmos.Kernel.Tests.Fat.System.IO +{ + class BinaryWriterBinaryReaderTest + { + static private byte[] xBytes = new byte[16] + { + 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, + 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF + }; + + /// + /// Tests System.IO.BinaryWriter plugs. + /// + public static void Execute(Debugger mDebugger) + { + mDebugger.Send("START TEST: BinaryWriter"); + mDebugger.Send("Creating FileStream: FileMode.Create"); + + using (var xFS = new FileStream(@"0:\binary.bin", FileMode.Create)) + { + if (xFS == null) + { + Assert.IsTrue(false, "Failed to create StreamWriter for file 0:\binary.bin"); + return; + } + + mDebugger.Send("Creating BinaryWriter"); + using (var xBW = new BinaryWriter(xFS)) + { + mDebugger.Send("Start writing"); + + xBW.Write(xBytes); + Assert.IsTrue(xFS.Length == xBytes.Length, "The length of the stream and the length of the bytes are different"); + + mDebugger.Send("Binary data written"); + } + + /* Put the FileStream on position 0 again */ + mDebugger.Send("Resetting FileStream"); + //xFS.Seek(0, SeekOrigin.Begin); + xFS.Position = 0; + + using (var xBR = new BinaryReader(xFS)) + { + if (xFS != null) + { + mDebugger.Send("Start reading"); + + byte[] xBuffer = xBR.ReadBytes(xBytes.Length); + Assert.IsTrue(ByteArrayAreEquals(xBytes, xBuffer), "Bytes changed during BinaryWriter and BinaryReader opeartions on FileStream"); + + mDebugger.Send("Binary data read"); + } + } + } + + mDebugger.Send("END TEST"); + } + } +} diff --git a/Tests/Cosmos.Kernel.Tests.Fat/System.IO/BinaryWriterTest.cs b/Tests/Cosmos.Kernel.Tests.Fat/System.IO/BinaryWriterTest.cs deleted file mode 100644 index f737ba789..000000000 --- a/Tests/Cosmos.Kernel.Tests.Fat/System.IO/BinaryWriterTest.cs +++ /dev/null @@ -1,48 +0,0 @@ -using System.IO; -using Cosmos.TestRunner; -using Cosmos.Debug.Kernel; - -namespace Cosmos.Kernel.Tests.Fat.System.IO -{ - class BinaryWriterTest - { - static private byte[] xBytes = new byte[16] - { - 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, - 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF - }; - - /// - /// Tests System.IO.BinaryWriter plugs. - /// - public static void Execute(Debugger mDebugger) - { - //TODO: Implement FileStream with FileMode.Create, currently throws a file not found exception - - mDebugger.Send("START TEST: BinaryWriter"); - mDebugger.Send("Creating FileStream: FileMode.Create"); - - using (var xFS = new FileStream(@"0:\binary.bin", FileMode.Create)) - { - mDebugger.Send("Creating BinaryWriter"); - - using (var xBW = new BinaryWriter(xFS)) - { - if (xFS != null) - { - mDebugger.Send("Start writing"); - - xBW.Write(xBytes); - Assert.IsTrue(xFS.Length == xBytes.Length, "The length of the stream and the length of the bytes are different"); - } - else - { - Assert.IsTrue(false, @"Failed to create StreamWriter for file 0:\binary.bin"); - } - } - } - - mDebugger.Send("END TEST"); - } - } -} diff --git a/Tests/Cosmos.Kernel.Tests.Fat/System.IO/DirectoryInfoTest.cs b/Tests/Cosmos.Kernel.Tests.Fat/System.IO/DirectoryInfoTest.cs index b32b10eec..d5ac39c9f 100644 --- a/Tests/Cosmos.Kernel.Tests.Fat/System.IO/DirectoryInfoTest.cs +++ b/Tests/Cosmos.Kernel.Tests.Fat/System.IO/DirectoryInfoTest.cs @@ -77,18 +77,32 @@ namespace Cosmos.Kernel.Tests.Fat.System.IO // XXX EnumerateDirectories() and EnumerateFiles() can not be neither tested as IL2CPU crashes! //var xEnumDir = xDi.EnumerateDirectories(); - // Not working xFiles[0].FullName is null I'll return on it when I'll test FileInfo -#if false mDebugger.Send("START TEST: GetFiles on xDi"); - //var expectedFilePath = Path.Combine(xDi.FullName, "test"); + var expectedFilePath = @"0:\DiTest\test"; File.Create(expectedFilePath); + var xFiles = xDi.GetFiles(); - Assert.IsTrue(xFiles != null, "GetFiles() failed it returns null array"); - Assert.IsTrue(xFiles.Length != 0, "GetFiles() failed it returns empty array"); - Assert.IsTrue(xFiles[0].FullName == expectedFilePath, "GetFiles() does not return the expected directories"); + + Assert.IsTrue(xFiles[0].FullName == expectedFilePath, "GetFiles() does not return the expected files"); mDebugger.Send("END TEST"); -#endif + + mDebugger.Send("START TEST: GetFileSystemInfos on xDi"); + var xEntries = xDi.GetFileSystemInfos(); + + /* This the correct way to implement dir :-) */ + // There 2 entries on xDi a subdir and a file, let's check that xEntries has lenght 2 first */ + Assert.IsTrue(xEntries.Length == 2, "There are not 2 entries inside xDi"); + // OK let's see if they are the expected ones... + foreach (var xEntry in xEntries) + { + if (xEntry is DirectoryInfo) + Assert.IsTrue(xEntry.FullName == xSubDi.FullName, "Directory found but with wrong name"); + if (xEntry is FileInfo) + Assert.IsTrue(xEntry.FullName == expectedFilePath, "File found but with wrong name"); + } + mDebugger.Send("END TEST"); + mDebugger.Send(""); // TODO we need to implement Move at VFS level to have this working #if false diff --git a/Tests/Cosmos.Kernel.Tests.Fat/System.IO/FileInfoTest.cs b/Tests/Cosmos.Kernel.Tests.Fat/System.IO/FileInfoTest.cs new file mode 100644 index 000000000..9d8c836da --- /dev/null +++ b/Tests/Cosmos.Kernel.Tests.Fat/System.IO/FileInfoTest.cs @@ -0,0 +1,121 @@ +using System; +using System.IO; +using Cosmos.TestRunner; +using Cosmos.Debug.Kernel; +using Cosmos.Common.Extensions; +using static Cosmos.Kernel.Tests.Fat.System.IO.HelperMethods; + +namespace Cosmos.Kernel.Tests.Fat.System.IO +{ + class FileInfoTest + { + /// + /// Tests System.IO.FileInfo plugs. + /// + public static void Execute(Debugger mDebugger) + { + var xPath = @"0:\FiTest"; + var xFi = new FileInfo(xPath); + + mDebugger.Send("xFi allocated"); + + mDebugger.Send("START TEST: Create"); + xFi.Create(); + mDebugger.Send("xFi File Created"); + + Assert.IsTrue(xFi.Exists == true, "FileInfo.Create failed: file does not exists"); + mDebugger.Send("END TEST"); + mDebugger.Send(""); + + /* OK the File has been created, let's test some Properties */ + mDebugger.Send("START TEST: Get Name"); + Assert.IsTrue(xFi.Name == "FiTest", "FileInfo.Name failed: File has wrong name"); + mDebugger.Send("END TEST"); + mDebugger.Send(""); + + mDebugger.Send("START TEST: Get FullName"); + Assert.IsTrue(xFi.FullName == xPath, "FileInfo.FullName failed: file has wrong path"); + mDebugger.Send("END TEST"); + mDebugger.Send(""); + + mDebugger.Send("START TEST: Get Directory"); + var xDiParent = xFi.Directory; + var ExpectedParentFullName = @"0:\"; + + Assert.IsTrue(xDiParent.FullName == ExpectedParentFullName, "FileInfo.Directory is wrong"); + mDebugger.Send("END TEST"); + mDebugger.Send(""); + + mDebugger.Send("START TEST: Get DirectoryName"); + var xDiParentStr = xFi.DirectoryName; + Assert.IsTrue(xDiParentStr == ExpectedParentFullName, "FileInfo.DirectoryName is wrong"); + mDebugger.Send("END TEST"); + mDebugger.Send(""); + + mDebugger.Send("START TEST: Get Name"); + var Name = xFi.Name; + var ExpectedFileName = "FiTest"; + Assert.IsTrue(Name == ExpectedFileName, "FileInfo.Name is wrong"); + mDebugger.Send("END TEST"); + mDebugger.Send(""); + + mDebugger.Send("START TEST: Get Attributes"); + + // This requires a plug in EnumImpl to work... but it will requires Reflection probably + //bool isReallyADir = xDi.Attributes.HasFlag(FileAttributes.Directory); + bool isReallyAFile = ((xFi.Attributes & FileAttributes.Normal) == FileAttributes.Normal); + Assert.IsTrue(isReallyAFile == true, "FileInfo.Attributes is wrong: file NOT a file."); + mDebugger.Send("END TEST"); + mDebugger.Send(""); + + string writtenText = "This a test line!"; + + mDebugger.Send("START TEST: Write text and then read it"); + /* OK let's try to write something on your xFi */ + using (StreamWriter sw = xFi.CreateText()) + { + sw.Write(writtenText); + } + + string readText; + /* ... and now let's read it... */ + using (StreamReader sr = xFi.OpenText()) + { + readText = sr.ReadToEnd(); + } + + Assert.IsTrue(writtenText == readText, "FileInfo write and read text failed"); + + mDebugger.Send("END TEST"); + mDebugger.Send(""); + + mDebugger.Send("START TEST: Length property"); + Assert.IsTrue(xFi.Length == writtenText.Length, "FileInfo has not changed size after writing"); + mDebugger.Send("END TEST"); + mDebugger.Send(""); + + mDebugger.Send("START TEST: CopyTo"); + var xFi2 = xFi.CopyTo(@"0:\FiTest2.txt"); + + using (StreamReader sr = xFi2.OpenText()) + { + readText = sr.ReadToEnd(); + } + + Assert.IsTrue(xFi2.Exists == true, "Copied file does not exists"); + + Assert.IsTrue(writtenText == readText, "Copied file has not same content of the original"); + + mDebugger.Send("END TEST"); + mDebugger.Send(""); + + mDebugger.Send("START TEST: Extension"); + //Console.WriteLine($"Extension is {xFi2.Extension}"); + + Assert.IsTrue(xFi2.Extension == ".txt", "File extension is not correct"); + + mDebugger.Send("END TEST"); + mDebugger.Send(""); + } + } +} diff --git a/Tests/Cosmos.Kernel.Tests.Fat/System.IO/FileTest.cs b/Tests/Cosmos.Kernel.Tests.Fat/System.IO/FileTest.cs index 5ae8f7e56..246d9c49b 100644 --- a/Tests/Cosmos.Kernel.Tests.Fat/System.IO/FileTest.cs +++ b/Tests/Cosmos.Kernel.Tests.Fat/System.IO/FileTest.cs @@ -124,10 +124,11 @@ namespace Cosmos.Kernel.Tests.Fat.System.IO } byte[] dataWritten = new byte[] { 0x01, 0x02, 0x03 }; File.WriteAllBytes(@"0:\test.dat", dataWritten); - mDebugger.Send("Text written"); + mDebugger.Send("Binary Text written"); byte[] dataRead = File.ReadAllBytes(@"0:\test.dat"); + mDebugger.Send("Bynary Text read"); - Assert.IsTrue(ByteArrayAreEquals(dataWritten, dataRead), "Failed to write binary data to a file."); + Assert.IsTrue(ByteArrayAreEquals(dataWritten, dataRead), "Failed to write and read binary data to a file."); mDebugger.Send("END TEST"); mDebugger.Send(""); @@ -195,14 +196,25 @@ namespace Cosmos.Kernel.Tests.Fat.System.IO mDebugger.Send("START TEST: Copy a file:"); File.Copy(@"0:\Kudzu.txt", @"0:\Kudzu2.txt"); - if (File.Exists(@"0:\Kudzu2.txt")) - { - mDebugger.Send(" The new file has been created, reading..."); - string KudzuTxtContent = File.ReadAllText(@"0:\Kudzu.txt"); - string Kudzu2TxtContent = File.ReadAllText(@"0:\Kudzu2.txt"); - Assert.IsTrue(KudzuTxtContent == Kudzu2TxtContent, "File has not been copied correctly"); - } + string KudzuTxtContent = File.ReadAllText(@"0:\Kudzu.txt"); + + Assert.IsTrue(File.Exists(@"0:\Kudzu2.txt"), "Copy failed destination file not created"); + + mDebugger.Send(" The new file has been created, reading..."); + + string Kudzu2TxtContent = File.ReadAllText(@"0:\Kudzu2.txt"); + + Assert.IsTrue(KudzuTxtContent == Kudzu2TxtContent, "File has not been copied correctly"); + + /* Now Try to Copy '0:\Kudzu.txt' onto an existing file with the overload of Copy that does permit this */ + mDebugger.Send("START TEST: Copy a file (overwrite existing) :"); + File.Copy(@"0:\Kudzu.txt", @"0:\test.dat", true); + + mDebugger.Send("The existing file has been overwritten, reading..."); + string TestDatContent = File.ReadAllText(@"0:\test.dat"); + + Assert.IsTrue(KudzuTxtContent == TestDatContent, "File has not been copied correctly"); mDebugger.Send("END TEST"); } diff --git a/Tests/Cosmos.Kernel.Tests.Fat/System.IO/StreamReaderTest.cs b/Tests/Cosmos.Kernel.Tests.Fat/System.IO/StreamReaderTest.cs deleted file mode 100644 index edd611bf3..000000000 --- a/Tests/Cosmos.Kernel.Tests.Fat/System.IO/StreamReaderTest.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System.IO; -using Cosmos.TestRunner; -using Cosmos.Debug.Kernel; - -namespace Cosmos.Kernel.Tests.Fat.System.IO -{ - class StreamReaderTest - { - /// - /// Tests System.IO.StreamReader plugs. - /// - public static void Execute(Debugger mDebugger) - { - mDebugger.Send("START TEST: StreamReader:"); - mDebugger.Send("Create StreamReader"); - - using (var xSR = new StreamReader(@"0:\test.txt")) - { - if (xSR != null) - { - mDebugger.Send("Start reading"); - - var content = xSR.ReadToEnd(); - Assert.IsTrue(content == "A line of text for testing\nSecond line", "Content: " + content); - } - else - { - Assert.IsTrue(false, @"Failed to create StreamReader for file 0:\test.txt"); - } - } - - mDebugger.Send("END TEST"); - } - } -} diff --git a/Tests/Cosmos.Kernel.Tests.Fat/System.IO/StreamWriterTest.cs b/Tests/Cosmos.Kernel.Tests.Fat/System.IO/StreamWriterStreamReaderTest.cs similarity index 98% rename from Tests/Cosmos.Kernel.Tests.Fat/System.IO/StreamWriterTest.cs rename to Tests/Cosmos.Kernel.Tests.Fat/System.IO/StreamWriterStreamReaderTest.cs index 676d1923b..ce89a47e1 100644 --- a/Tests/Cosmos.Kernel.Tests.Fat/System.IO/StreamWriterTest.cs +++ b/Tests/Cosmos.Kernel.Tests.Fat/System.IO/StreamWriterStreamReaderTest.cs @@ -6,7 +6,7 @@ using System.Text; namespace Cosmos.Kernel.Tests.Fat.System.IO { - class StreamWriterTest + class StreamWriterStreamReaderTest { /// /// Tests System.IO.StreamWriter plugs. diff --git a/source/Cosmos.System2_Plugs/System/IO/CosmosFileSystem.cs b/source/Cosmos.System2_Plugs/System/IO/CosmosFileSystem.cs index 219cc6a9b..ee4933027 100644 --- a/source/Cosmos.System2_Plugs/System/IO/CosmosFileSystem.cs +++ b/source/Cosmos.System2_Plugs/System/IO/CosmosFileSystem.cs @@ -234,7 +234,7 @@ namespace Cosmos.System_Plugs.System.IO public static void CopyFile(object aThis, string sourceFullPath, string destFullPath, bool overwrite) { - Global.mFileSystemDebugger.SendInternal($"CopyFile {sourceFullPath} into {destFullPath}"); + Global.mFileSystemDebugger.SendInternal($"CopyFile {sourceFullPath} into {destFullPath} with overwrite {overwrite}"); // The destination path may just be a directory into which the file should be copied. // If it is, append the filename from the source onto the destination directory diff --git a/source/Cosmos.System2_Plugs/System/IO/DirectoryInfoImpl.cs b/source/Cosmos.System2_Plugs/System/IO/DirectoryInfoImpl.cs index c9a23be05..02845bb28 100644 --- a/source/Cosmos.System2_Plugs/System/IO/DirectoryInfoImpl.cs +++ b/source/Cosmos.System2_Plugs/System/IO/DirectoryInfoImpl.cs @@ -1,16 +1,10 @@ #define COSMOSDEBUG -using System; using System.IO; -using System.Collections.Generic; -using Cosmos.Debug.Kernel; -using Cosmos.IL2CPU.API; using Cosmos.IL2CPU.API.Attribs; using Cosmos.System; -using Cosmos.System.FileSystem; using Cosmos.System.FileSystem.Listing; using Cosmos.System.FileSystem.VFS; -using System.Diagnostics.Contracts; -using System.Linq; +using System.Collections.Generic; namespace Cosmos.System_Plugs.System.IO { @@ -21,7 +15,6 @@ namespace Cosmos.System_Plugs.System.IO [Plug(Target = typeof(DirectoryInfo))] public static class DirectoryInfoImpl { - public static bool get_Exists(DirectoryInfo aThis) { string aPath = aThis.FullName; @@ -34,21 +27,19 @@ namespace Cosmos.System_Plugs.System.IO { Global.mFileSystemDebugger.SendInternal($"DirectoryInfo.GetDirectories() on path {aThis.FullName}"); var xEntries = VFSManager.GetDirectoryListing(aThis.FullName); - /* - * GetDirectoryListing() returns Directories and Files so could allocate a little more than needed but - * but I think this is better than to use a List and then convert it to an array - */ - var result = new DirectoryInfo[xEntries.Count]; + + //var result = new DirectoryInfo[xEntries.Count]; + var result = new List(); for (int i = 0; i < xEntries.Count; i++) { if (xEntries[i].mEntryType == DirectoryEntryTypeEnum.Directory) { - result[i] = new DirectoryInfo(xEntries[i].mFullPath); + result.Add(new DirectoryInfo(xEntries[i].mFullPath)); } } - return result; + return result.ToArray(); } /* The real implementation uses IEnumerable and do a conversion ToArray that crashes IL2CPU */ @@ -56,22 +47,43 @@ namespace Cosmos.System_Plugs.System.IO { Global.mFileSystemDebugger.SendInternal($"DirectoryInfo.GetFiles() on path {aThis.FullName}"); var xEntries = VFSManager.GetDirectoryListing(aThis.FullName); - /* - * GetDirectoryListing() returns Directories and Files so could allocate a little more than needed but - * but I think this is better than to use a List and then convert it to an array - */ - var result = new FileInfo[xEntries.Count]; + + var result = new List(); for (int i = 0; i < xEntries.Count; i++) { Global.mFileSystemDebugger.SendInternal($"Found entry of type {(int)xEntries[i].mEntryType} and name {xEntries[i].mFullPath}"); if (xEntries[i].mEntryType == DirectoryEntryTypeEnum.File) { - result[i] = new FileInfo(xEntries[i].mFullPath); + //result[i] = new FileInfo(xEntries[i].mFullPath); + result.Add(new FileInfo(xEntries[i].mFullPath)); } } - return result; + return result.ToArray(); + } + + public static FileSystemInfo[] GetFileSystemInfos(DirectoryInfo aThis) + { + Global.mFileSystemDebugger.SendInternal($"DirectoryInfo.GetFiles() on path {aThis.FullName}"); + var xEntries = VFSManager.GetDirectoryListing(aThis.FullName); + + var result = new List(); + + for (int i = 0; i < xEntries.Count; i++) + { + Global.mFileSystemDebugger.SendInternal($"Found entry of type {(int)xEntries[i].mEntryType} and name {xEntries[i].mFullPath}"); + if (xEntries[i].mEntryType == DirectoryEntryTypeEnum.Directory) + { + result.Add(new DirectoryInfo(xEntries[i].mFullPath)); + } + if (xEntries[i].mEntryType == DirectoryEntryTypeEnum.File) + { + result.Add(new FileInfo(xEntries[i].mFullPath)); + } + } + + return result.ToArray(); } } } diff --git a/source/Cosmos.System2_Plugs/System/IO/FileImpl.cs b/source/Cosmos.System2_Plugs/System/IO/FileImpl.cs index d27de0c99..0a70f4d3e 100644 --- a/source/Cosmos.System2_Plugs/System/IO/FileImpl.cs +++ b/source/Cosmos.System2_Plugs/System/IO/FileImpl.cs @@ -1,15 +1,8 @@ //#define COSMOSDEBUG using System; -using System.Collections.Generic; using System.IO; using Cosmos.System; -using Cosmos.Common.Extensions; -using Cosmos.Debug.Kernel; -using Cosmos.IL2CPU.API; using Cosmos.IL2CPU.API.Attribs; -using Cosmos.System.FileSystem; -using Cosmos.System.FileSystem.VFS; -using System.Text; namespace Cosmos.System_Plugs.System.IO { @@ -44,30 +37,5 @@ namespace Cosmos.System_Plugs.System.IO xSW.WriteLine(current); } } - - public static byte[] ReadAllBytes(string aFile) - { - Global.mFileSystemDebugger.SendInternal("In FileImpl.ReadAllText"); - using (var xFS = new FileStream(aFile, FileMode.Open)) - { - var xBuff = new byte[(int)xFS.Length]; - var xResult = xFS.Read(xBuff, 0, xBuff.Length); - if (xResult != xBuff.Length) - { - throw new Exception("Couldn't read complete file!"); - } - Global.mFileSystemDebugger.SendInternal("Bytes read"); - - return xBuff; - } - } - - public static void WriteAllBytes(string aFile, byte[] aBytes) - { - using (var xFS = new FileStream(aFile, FileMode.Create)) - { - xFS.Write(aBytes, 0, aBytes.Length); - } - } } } diff --git a/source/Cosmos.System2_Plugs/System/IO/FileInfoImpl.cs b/source/Cosmos.System2_Plugs/System/IO/FileInfoImpl.cs new file mode 100644 index 000000000..d5be2dc13 --- /dev/null +++ b/source/Cosmos.System2_Plugs/System/IO/FileInfoImpl.cs @@ -0,0 +1,29 @@ +#define COSMOSDEBUG +using System.IO; +using Cosmos.IL2CPU.API.Attribs; +using Cosmos.System; +using Cosmos.System.FileSystem.Listing; +using Cosmos.System.FileSystem.VFS; + +namespace Cosmos.System2_Plugs.System.IO +{ + [Plug(Target = typeof(FileInfo))] + public static class FileInfoImpl + { + public static bool get_Exists(FileInfo aThis) + { + string aPath = aThis.FullName; + Global.mFileSystemDebugger.SendInternal($"FileInfo.Exists : aPath = {aPath}"); + return VFSManager.FileExists(aPath); + } + + /* Optimize this: CosmosVFS should expose an attribute without the need to open the file for reading... */ + public static long get_Length(FileInfo aThis) + { + using (var xFs = aThis.OpenRead()) + { + return xFs.Length; + } + } + } +} diff --git a/source/Cosmos.System2_Plugs/System/IO/FileLoadExceptionImpl.cs b/source/Cosmos.System2_Plugs/System/IO/FileLoadExceptionImpl.cs new file mode 100644 index 000000000..76491351f --- /dev/null +++ b/source/Cosmos.System2_Plugs/System/IO/FileLoadExceptionImpl.cs @@ -0,0 +1,23 @@ +using System.IO; +using Cosmos.IL2CPU.API.Attribs; +using Cosmos.System; +using Cosmos.System.FileSystem.Listing; +using Cosmos.System.FileSystem.VFS; +using System; + +namespace Cosmos.System2_Plugs.System.IO +{ + [Plug(Target = typeof(FileLoadException))] + public static class FileLoadExceptionImpl + { + public static string FormatFileLoadExceptionMessage(string fileName, int hResult) + { + throw new NotImplementedException("FormatFileLoadExceptionMessage"); + } + + public static string ToString(FileLoadException aThis) + { + throw new NotImplementedException("FileLoadException.ToString()"); + } + } +} diff --git a/source/Cosmos.System2_Plugs/System/IO/FileNotFoundExceptionImpl.cs b/source/Cosmos.System2_Plugs/System/IO/FileNotFoundExceptionImpl.cs new file mode 100644 index 000000000..9ab48b97d --- /dev/null +++ b/source/Cosmos.System2_Plugs/System/IO/FileNotFoundExceptionImpl.cs @@ -0,0 +1,17 @@ +using System.IO; +using Cosmos.IL2CPU.API.Attribs; +using Cosmos.System; +using Cosmos.System.FileSystem.Listing; +using Cosmos.System.FileSystem.VFS; +using System; +namespace Cosmos.System2_Plugs.System.IO +{ + [Plug(Target = typeof(FileNotFoundException))] + public static class FileNotFoundExceptionImpl + { + public static string ToString(FileNotFoundException aThis) + { + throw new NotImplementedException("FileNotFoundException.ToString()"); + } + } +} diff --git a/source/Cosmos.System2_Plugs/System/IO/FileStreamImpl.cs b/source/Cosmos.System2_Plugs/System/IO/FileStreamImpl.cs index 7f6193270..df1aee624 100644 --- a/source/Cosmos.System2_Plugs/System/IO/FileStreamImpl.cs +++ b/source/Cosmos.System2_Plugs/System/IO/FileStreamImpl.cs @@ -183,7 +183,14 @@ namespace Cosmos.System_Plugs.System.IO Global.mFileSystemDebugger.SendInternal("CreateNew Mode with aPath not existing new file created"); // TODO it seems that GetFileStream effectively Creates the file if it does not exist - aStream = File.Create(aPath); + //aStream = File.Create(aPath); + xEntry = VFSManager.CreateFile(aPath); + if (xEntry == null) + { + return null; + } + //aStream = File.Create(aPath); + aStream = VFSManager.GetFileStream(aPath); break; case FileMode.Open: @@ -226,5 +233,15 @@ namespace Cosmos.System_Plugs.System.IO return aStream; } + + public static bool get_CanWrite(FileStream aThis, [FieldAccess(Name = InnerStreamFieldId)] ref Stream innerStream) + { + return innerStream.CanWrite; + } + + public static bool get_CanRead(FileStream aThis, [FieldAccess(Name = InnerStreamFieldId)] ref Stream innerStream) + { + return innerStream.CanRead; + } } }