using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using Cosmos.Debug.Kernel; using Directory = Cosmos.System.FileSystem.Listing.Directory; using File = Cosmos.System.FileSystem.Listing.File; namespace Cosmos.System.FileSystem.VFS { public static class VFSManager { private static VFSBase mVFS; public static void RegisterVFS(VFSBase aVFS) { Cosmos.System.Global.Dbg.Send("VFSManager.RegisterVFS"); if (mVFS != null) { throw new Exception("Virtual File System Manager already initialized!"); } aVFS.Initialize(); mVFS = aVFS; } #region Helpers public static char GetAltDirectorySeparatorChar() { return '/'; } public static char GetDirectorySeparatorChar() { return '\\'; } public static char[] GetInvalidFileNameChars() { return new[] { '"', '<', '>', '|', '\0', '\a', '\b', '\t', '\n', '\v', '\f', '\r', ':', '*', '?', '\\', '/' }; } public static char[] GetInvalidPathCharsWithAdditionalChecks() { return new[] { '"', '<', '>', '|', '\0', '\a', '\b', '\t', '\n', '\v', '\f', '\r', '*', '?' }; } public static char GetPathSeparator() { return ';'; } public static char[] GetRealInvalidPathChars() { return new[] { '"', '<', '>', '|', '\0', '\a', '\b', '\t', '\n', '\v', '\f', '\r' }; } public static char GetVolumeSeparatorChar() { return ':'; } public static int GetMaxPath() { return 260; } //public static bool IsAbsolutePath(this string aPath) //{ // return ((aPath[0] == VFSBase.DirectorySeparatorChar) || (aPath[0] == VFSBase.AltDirectorySeparatorChar)); //} //public static bool IsRelativePath(this string aPath) //{ // return (aPath[0] != VFSBase.DirectorySeparatorChar || aPath[0] != VFSBase.AltDirectorySeparatorChar); //} public static string[] SplitPath(string aPath) { //TODO: This should call Path.GetDirectoryName() and then loop calling Directory.GetParent(), but those aren't implemented yet. return aPath.Split(GetDirectorySeparators(), StringSplitOptions.RemoveEmptyEntries); } private static char[] GetDirectorySeparators() { return new[] { GetDirectorySeparatorChar(), GetAltDirectorySeparatorChar() }; } #endregion public static Listing.File TryGetFile(string aPath) { if (aPath == null) { throw new Exception("Path can not be null."); } FatHelpers.Debug("In VFSManager.TryGetFile"); FatHelpers.Debug(aPath); string xFileName = Path.GetFileName(aPath); string xDirectory = Path.GetDirectoryName(aPath); var xLastChar = xDirectory[xDirectory.Length - 1]; if (xLastChar != Path.DirectorySeparatorChar) { xDirectory = xDirectory + Path.DirectorySeparatorChar; } FatHelpers.Debug("Now Try to get directory listing"); var xList = GetDirectoryListing(xDirectory); for (int i = 0; i < xList.Count; i++) { var xEntry = xList[i]; var xFile = xEntry as Listing.File; if (xFile != null && String.Equals(xEntry.Name, xFileName, StringComparison.OrdinalIgnoreCase)) { FatHelpers.Debug("--- Returning file"); FatHelpers.Debug("Name"); FatHelpers.Debug(xFile.Name); return xFile; } } return null; } public static Listing.Directory TryGetDirectory(string aPath) { if (string.IsNullOrEmpty(aPath)) { throw new Exception("Path can not be null."); } FatHelpers.Debug("In VFSManager.TryGetFile"); string xFileName = Path.GetFileName(aPath); string xDirectory = Path.GetDirectoryName(aPath); FatHelpers.Debug("Filename: "); FatHelpers.Debug(xFileName); FatHelpers.Debug("Directory:"); FatHelpers.Debug(xDirectory); var xLastChar = xDirectory[xDirectory.Length - 1]; if (xLastChar != Path.DirectorySeparatorChar) { xDirectory = xDirectory + Path.DirectorySeparatorChar; } FatHelpers.Debug("Now Try to get directory listing"); var xList = GetDirectoryListing(xDirectory); FatHelpers.DebugNumber((uint) xList.Count); for (int i = 0; i < xList.Count; i++) { var xEntry = xList[i]; var xFile = xEntry as Listing.Directory; if (xFile != null && String.Equals(xEntry.Name, xFileName, StringComparison.OrdinalIgnoreCase)) { return xFile; } else { FatHelpers.Debug("--Skipping item"); if (xFile == null) { FatHelpers.Debug(" File"); } else { FatHelpers.Debug(" Directory"); } FatHelpers.Debug(" Name"); FatHelpers.Debug(xEntry.Name); } } FatHelpers.Debug("Directory not found"); FatHelpers.Debug(xFileName); return null; } public static List