From 043cbb2f194465206802c00f26f8e34aaa47ca5d Mon Sep 17 00:00:00 2001 From: Charles Betros Date: Thu, 25 Feb 2016 11:56:30 -0600 Subject: [PATCH] Fixed path plugs --- .../Cosmos.Core.Plugs.csproj | 1 - .../System/IO/PathHelperImpl.cs | 107 +++++++++++++++- source/Cosmos.IL2CPU/ILOpCodes/OpNone.cs | 6 +- source/Cosmos.IL2CPU/ILOpCodes/OpVar.cs | 3 +- .../Cosmos.System.Plugs/System/IO/PathImpl.cs | 120 +++++++++--------- .../FileSystem/VFS/VFSManager.cs | 60 ++++++++- 6 files changed, 221 insertions(+), 76 deletions(-) diff --git a/source/Cosmos.Core.Plugs/Cosmos.Core.Plugs.csproj b/source/Cosmos.Core.Plugs/Cosmos.Core.Plugs.csproj index 89aba91a8f..21bbe85528 100644 --- a/source/Cosmos.Core.Plugs/Cosmos.Core.Plugs.csproj +++ b/source/Cosmos.Core.Plugs/Cosmos.Core.Plugs.csproj @@ -134,7 +134,6 @@ - diff --git a/source/Cosmos.Core.Plugs/System/IO/PathHelperImpl.cs b/source/Cosmos.Core.Plugs/System/IO/PathHelperImpl.cs index 51aba21488..49361ca9f9 100644 --- a/source/Cosmos.Core.Plugs/System/IO/PathHelperImpl.cs +++ b/source/Cosmos.Core.Plugs/System/IO/PathHelperImpl.cs @@ -1,26 +1,121 @@ -using Cosmos.IL2CPU.Plugs; +using System; +using System.Diagnostics.CodeAnalysis; +using System.IO; + +using Cosmos.IL2CPU.Plugs; namespace Cosmos.Core.Plugs.System.IO { [Plug(TargetName = "System.IO.PathHelper")] + [SuppressMessage("ReSharper", "InconsistentNaming")] public static class PathHelperImpl { + public static unsafe void Ctor(ref object aThis, char* aCharArrayPtr, int aLength, + [FieldAccess(Name = "System.Boolean System.IO.PathHelper.doNotTryExpandShortFileName")] ref bool mDoNotTryExpandShortFileName, + [FieldAccess(Name = "System.Char* System.IO.PathHelper.m_arrayPtr")] ref char* mArrayPtr, + [FieldAccess(Name = "System.Int32 System.IO.PathHelper.m_capacity")] ref int mCapacity, + [FieldAccess(Name = "System.Int32 System.IO.PathHelper.m_length")] ref int mLength, + [FieldAccess(Name = "System.Int32 System.IO.PathHelper.m_maxPath")] ref int mMaxPath, + [FieldAccess(Name = "System.Boolean System.IO.PathHelper.useStackAlloc")] ref bool mUseStackAlloc + ) + { + mLength = 0; + mCapacity = aLength; + mArrayPtr = aCharArrayPtr; + mUseStackAlloc = true; + } + + public static unsafe void Ctor(ref object aThis, int aCapacity, int aMaxPath, + [FieldAccess(Name = "System.Boolean System.IO.PathHelper.doNotTryExpandShortFileName")] ref bool mDoNotTryExpandShortFileName, + [FieldAccess(Name = "System.Char* System.IO.PathHelper.m_arrayPtr")] ref char* mArrayPtr, + [FieldAccess(Name = "System.Int32 System.IO.PathHelper.m_capacity")] ref int mCapacity, + [FieldAccess(Name = "System.Int32 System.IO.PathHelper.m_length")] ref int mLength, + [FieldAccess(Name = "System.Int32 System.IO.PathHelper.m_maxPath")] ref int mMaxPath, + [FieldAccess(Name = "System.Boolean System.IO.PathHelper.useStackAlloc")] ref bool mUseStackAlloc) + { + mLength = 0; + mCapacity = aCapacity; + mUseStackAlloc = true; + + } + + public static int get_Capacity(ref object aThis, + [FieldAccess(Name = "System.Int32 System.IO.PathHelper.m_capacity")] ref int mCapacity) + { + return mCapacity; + } + + public static unsafe char get_Item(ref object aThis, int aIndex, + [FieldAccess(Name = "System.Char* System.IO.PathHelper.m_arrayPtr")] ref char* mArrayPtr) + { + return mArrayPtr[aIndex]; + } + + public static unsafe void set_Item(ref object aThis, int aIndex, char aValue, + [FieldAccess(Name = "System.Char* System.IO.PathHelper.m_arrayPtr")] ref char* mArrayPtr) + { + mArrayPtr[aIndex] = aValue; + } + + public static int get_Length(ref object aThis, + [FieldAccess(Name = "System.Int32 System.IO.PathHelper.m_length")] ref int mLength) + { + return mLength; + } + + public static void set_Length(ref object aThis, int aValue, + [FieldAccess(Name = "System.Int32 System.IO.PathHelper.m_length")] ref int mLength) + { + mLength = aValue; + } + + public static unsafe void Append(ref object aThis, char aValue, + [FieldAccess(Name = "System.Int32 System.IO.PathHelper.m_capacity")] ref int mCapacity, + [FieldAccess(Name = "System.Int32 System.IO.PathHelper.m_length")] ref int mLength, + [FieldAccess(Name = "System.Char* System.IO.PathHelper.m_arrayPtr")] ref char* mArrayPtr) + { + if (mLength + 1 > mCapacity) + { + throw new PathTooLongException(); + } + + mArrayPtr[mLength] = aValue; + mLength++; + } + public static unsafe int GetFullPathName(ref object aThis, - [FieldAccess(Name = "System.Char* System.IO.PathHelper.m_arrayPtr")] ref char* aArrayPtr) + [FieldAccess(Name = "System.Int32 System.IO.PathHelper.m_length")] ref int mLength, + [FieldAccess(Name = "System.Char* System.IO.PathHelper.m_arrayPtr")] ref char* mArrayPtr) { int xLength = 0; - while (*aArrayPtr != '\0') + while (*mArrayPtr != '\0') { xLength++; - aArrayPtr++; + mArrayPtr++; } + mLength = xLength; return xLength; } - public static bool TryExpandShortFileName(ref object aThis) + public static unsafe string ToString(ref object aThis, + [FieldAccess(Name = "System.Int32 System.IO.PathHelper.m_length")] ref int mLength, + [FieldAccess(Name = "System.Char* System.IO.PathHelper.m_arrayPtr")] ref char* mArrayPtr) { + return new string(mArrayPtr, 0, mLength); + } + + public static unsafe bool TryExpandShortFileName(ref object aThis, + [FieldAccess(Name = "System.Int32 System.IO.PathHelper.m_length")] ref int mLength, + [FieldAccess(Name = "System.Char* System.IO.PathHelper.m_arrayPtr")] ref char* mArrayPtr) + { + int xLength = 0; + while (*mArrayPtr != '\0') + { + xLength++; + mArrayPtr++; + } + mLength = xLength; return true; } } - } diff --git a/source/Cosmos.IL2CPU/ILOpCodes/OpNone.cs b/source/Cosmos.IL2CPU/ILOpCodes/OpNone.cs index d3e634e7a3..e7d7c66570 100644 --- a/source/Cosmos.IL2CPU/ILOpCodes/OpNone.cs +++ b/source/Cosmos.IL2CPU/ILOpCodes/OpNone.cs @@ -535,14 +535,14 @@ protected override void DoInterpretStackTypes(ref bool aSituationChanged) return; } - if ((StackPopTypes[0] == typeof (IntPtr) && StackPopTypes[1] == typeof (UInt32*)) + if ((StackPopTypes[0] == typeof (IntPtr) && StackPopTypes[1] == typeof (uint*)) || (StackPopTypes[0] == typeof (uint*) && StackPopTypes[1] == typeof (IntPtr))) { StackPushTypes[0] = typeof (uint*); aSituationChanged = true; return; } - if ((StackPopTypes[0] == typeof(UIntPtr) && StackPopTypes[1] == typeof(UInt32*)) + if ((StackPopTypes[0] == typeof(UIntPtr) && StackPopTypes[1] == typeof(uint*)) || (StackPopTypes[0] == typeof(uint*) && StackPopTypes[1] == typeof(UIntPtr))) { StackPushTypes[0] = typeof(uint*); @@ -663,7 +663,7 @@ protected override void DoInterpretStackTypes(ref bool aSituationChanged) } if (StackPopTypes[0] == typeof(IntPtr) && StackPopTypes[1] == typeof(IntPtr)) { - StackPushTypes[0] = typeof(uint); + StackPushTypes[0] = typeof(IntPtr); aSituationChanged = true; return; } diff --git a/source/Cosmos.IL2CPU/ILOpCodes/OpVar.cs b/source/Cosmos.IL2CPU/ILOpCodes/OpVar.cs index ef44b54a76..b2cbc58927 100644 --- a/source/Cosmos.IL2CPU/ILOpCodes/OpVar.cs +++ b/source/Cosmos.IL2CPU/ILOpCodes/OpVar.cs @@ -95,6 +95,7 @@ protected override void DoInitStackAnalysis(MethodBase aMethod) } xArgIndexCorrection = -1; } + string x = aMethod.Name; var xParams = aMethod.GetParameters(); StackPushTypes[0] = xParams[Value + xArgIndexCorrection].ParameterType; if (StackPushTypes[0].IsEnum) @@ -102,8 +103,6 @@ protected override void DoInitStackAnalysis(MethodBase aMethod) StackPushTypes[0] = StackPushTypes[0].GetEnumUnderlyingType(); } return; - default: - break; } } } diff --git a/source/Cosmos.System.Plugs/System/IO/PathImpl.cs b/source/Cosmos.System.Plugs/System/IO/PathImpl.cs index 685df4da24..c7494ba659 100644 --- a/source/Cosmos.System.Plugs/System/IO/PathImpl.cs +++ b/source/Cosmos.System.Plugs/System/IO/PathImpl.cs @@ -423,20 +423,20 @@ public static string RemoveLongPathPrefix(string aPath) // return false; //} - //public static bool IsDirectorySeparator(char aC) - //{ - // if (aC.ToString() == Path.DirectorySeparatorChar.ToString()) - // { - // return true; - // } + public static bool IsDirectorySeparator(char aC) + { + if (aC.ToString() == Path.DirectorySeparatorChar.ToString()) + { + return true; + } - // if (aC.ToString() == Path.AltDirectorySeparatorChar.ToString()) - // { - // return true; - // } + if (aC.ToString() == Path.AltDirectorySeparatorChar.ToString()) + { + return true; + } - // return false; - //} + return false; + } //public static bool IsPathRooted(string aPath) //{ @@ -455,54 +455,54 @@ public static string RemoveLongPathPrefix(string aPath) // return false; //} - //private static bool IsRelative(string aPath) - //{ - // if (aPath == null) - // { - // throw new ArgumentNullException("aPath"); - // } - - // if (aPath.Length < 3) - // { - // return true; - // } - - // if (aPath[1] != Path.VolumeSeparatorChar) - // { - // return true; - // } - - // if (aPath[2] != Path.DirectorySeparatorChar) - // { - // return true; - // } - - // return false; - //} - - //public static string NormalizePath(string aPath, bool aFullCheck) - //{ - // if (aPath == null) - // { - // Global.mFileSystemDebugger.SendInternal($"Path.NormalizePath : aPath is null"); - // throw new ArgumentNullException("aPath"); - // } - - // string result = aPath; - // if (IsRelative(result)) - // { - // result = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + result; - // Global.mFileSystemDebugger.SendInternal($"Path.NormalizePath : aPath is relative, aPath = {aPath}, result = {result}"); - // } - - // if (IsDirectorySeparator(result[result.Length - 1])) - // { - // Global.mFileSystemDebugger.SendInternal($"Path.NormalizePath : Found directory seprator"); - // result = result.Remove(result.Length - 1); - // } + private static bool IsRelative(string aPath) + { + if (aPath == null) + { + throw new ArgumentNullException("aPath"); + } + + if (aPath.Length < 3) + { + return true; + } + + if (aPath[1] != Path.VolumeSeparatorChar) + { + return true; + } + + if (aPath[2] != Path.DirectorySeparatorChar) + { + return true; + } + + return false; + } - // Global.mFileSystemDebugger.SendInternal($"Path.NormalizePath : aPath = {aPath}, returning {result}"); - // return result; - //} + public static string NormalizePath(string aPath, bool aFullCheck) + { + if (aPath == null) + { + Global.mFileSystemDebugger.SendInternal($"Path.NormalizePath : aPath is null"); + throw new ArgumentNullException("aPath"); + } + + string result = aPath; + if (IsRelative(result)) + { + result = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + result; + Global.mFileSystemDebugger.SendInternal($"Path.NormalizePath : aPath is relative, aPath = {aPath}, result = {result}"); + } + + if (IsDirectorySeparator(result[result.Length - 1])) + { + Global.mFileSystemDebugger.SendInternal($"Path.NormalizePath : Found directory seprator"); + result = result.Remove(result.Length - 1); + } + + Global.mFileSystemDebugger.SendInternal($"Path.NormalizePath : aPath = {aPath}, returning {result}"); + return result; + } } } diff --git a/source/Cosmos.System/FileSystem/VFS/VFSManager.cs b/source/Cosmos.System/FileSystem/VFS/VFSManager.cs index 701b4e3de2..f9be96e52b 100644 --- a/source/Cosmos.System/FileSystem/VFS/VFSManager.cs +++ b/source/Cosmos.System/FileSystem/VFS/VFSManager.cs @@ -294,13 +294,44 @@ public static char GetDirectorySeparatorChar() public static char[] GetInvalidFileNameChars() { - char[] xReturn = { '"', '<', '>', '|', '\0', '\a', '\b', '\t', '\n', '\v', '\f', '\r', ':', '*', '?', '\\', '/' }; + char[] xReturn = new char[17]; + xReturn[0] = '"'; + xReturn[1] = '<'; + xReturn[2] = '>'; + xReturn[3] = '|'; + xReturn[4] = '\0'; + xReturn[5] = '\a'; + xReturn[6] = '\b'; + xReturn[7] = '\t'; + xReturn[8] = '\n'; + xReturn[9] = '\v'; + xReturn[10] = '\f'; + xReturn[11] = '\r'; + xReturn[12] = ':'; + xReturn[13] = '*'; + xReturn[14] = '?'; + xReturn[15] = '\\'; + xReturn[16] = '/'; return xReturn; } public static char[] GetInvalidPathCharsWithAdditionalChecks() { - char[] xReturn = { '"', '<', '>', '|', '\0', '\a', '\b', '\t', '\n', '\v', '\f', '\r', '*', '?' }; + char[] xReturn = new char[14]; + xReturn[0] = '"'; + xReturn[1] = '<'; + xReturn[2] = '>'; + xReturn[3] = '|'; + xReturn[4] = '\0'; + xReturn[5] = '\a'; + xReturn[6] = '\b'; + xReturn[7] = '\t'; + xReturn[8] = '\n'; + xReturn[9] = '\v'; + xReturn[10] = '\f'; + xReturn[11] = '\r'; + xReturn[12] = '*'; + xReturn[13] = '?'; return xReturn; } @@ -311,13 +342,34 @@ public static char GetPathSeparator() public static char[] GetRealInvalidPathChars() { - char[] xReturn = { '"', '<', '>', '|', '\0', '\a', '\b', '\t', '\n', '\v', '\f', '\r' }; + char[] xReturn = new char[12]; + xReturn[0] = '"'; + xReturn[1] = '<'; + xReturn[2] = '>'; + xReturn[3] = '|'; + xReturn[4] = '\0'; + xReturn[5] = '\a'; + xReturn[6] = '\b'; + xReturn[7] = '\t'; + xReturn[8] = '\n'; + xReturn[9] = '\v'; + xReturn[10] = '\f'; + xReturn[11] = '\r'; return xReturn; } public static char[] GetTrimEndChars() { - return new[] { (char)0x9, (char)0xA, (char)0xB, (char)0xC, (char)0xD, (char)0x20, (char)0x85, (char)0xA0 }; + char[] xReturn = new char[8]; + xReturn[0] = (char)0x9; + xReturn[1] = (char)0xA; + xReturn[2] = (char)0xB; + xReturn[3] = (char)0xC; + xReturn[4] = (char)0xD; + xReturn[5] = (char)0x20; + xReturn[6] = (char)0x85; + xReturn[7] = (char)0xA0; + return xReturn; } public static char GetVolumeSeparatorChar()