From c5198ccb8083d2002a651104f5febbfc87ac6baf Mon Sep 17 00:00:00 2001 From: Matthijs ter Woord Date: Sat, 4 Jul 2015 18:43:32 +0200 Subject: [PATCH] File.Exists works (at least for root files) --- Users/Sentinel/SentinelKernel/Kernel.cs | 41 +++++---- .../FileSystem/FAT/FatFileSystem.cs | 5 ++ .../SentinelSystem/FileSystem/FileSystem.cs | 2 + .../FileSystem/VFS/VFSManager.cs | 29 +++--- .../SentinelSystem/SentinelSystem.csproj | 1 - Users/Sentinel/SentinelSystem/SentinelVFS.cs | 88 +++++++++++-------- .../SentinelSystem/System/IO/PathImpl.cs | 7 +- 7 files changed, 107 insertions(+), 66 deletions(-) diff --git a/Users/Sentinel/SentinelKernel/Kernel.cs b/Users/Sentinel/SentinelKernel/Kernel.cs index 9ad5faec5..7e8f84b46 100644 --- a/Users/Sentinel/SentinelKernel/Kernel.cs +++ b/Users/Sentinel/SentinelKernel/Kernel.cs @@ -26,32 +26,41 @@ namespace SentinelKernel var xRoot = Path.GetPathRoot(@"0:\test"); bool xTest = Directory.Exists("0:\\test"); Console.WriteLine("After test"); - if (xTest) - { - Console.WriteLine("Folder exists!"); - xTest = Directory.Exists("0:\\test\\DirInTest"); - if (xTest) - { - Console.WriteLine("Subfolder exists as well!"); - } - else - { - Console.WriteLine("Subfolder doesn't exist!"); - } - } - else + if (!xTest) { Console.WriteLine("Folder does not exist!"); + return; } + Console.WriteLine("Folder exists!"); + xTest = Directory.Exists("0:\\test\\DirInTest"); + if (!xTest) + { + Console.WriteLine("Subfolder doesn't exist!"); + return; + } + Console.WriteLine("Subfolder exists as well!"); + + xTest = File.Exists(@"0:\Kudzu.txt"); + if (!xTest) + { + Console.WriteLine(@"\Kudzu.txt not found!"); + return; + } + + Console.WriteLine("Kudzu.txt found!"); + } catch (Exception e) { Console.WriteLine("Exception occurred:"); Console.WriteLine(e.Message); } - - Stop(); + finally + { + while (true) + ; + } } } } diff --git a/Users/Sentinel/SentinelSystem/FileSystem/FAT/FatFileSystem.cs b/Users/Sentinel/SentinelSystem/FileSystem/FAT/FatFileSystem.cs index a237c4f33..65f1c5817 100644 --- a/Users/Sentinel/SentinelSystem/FileSystem/FAT/FatFileSystem.cs +++ b/Users/Sentinel/SentinelSystem/FileSystem/FAT/FatFileSystem.cs @@ -394,5 +394,10 @@ namespace SentinelKernel.System.FileSystem.FAT return GetDirectoryContents((FatDirectory)baseDirectory); } } + + public override Directory GetRootDirectory(string name) + { + return new FatDirectory(this, name, RootCluster); + } } } diff --git a/Users/Sentinel/SentinelSystem/FileSystem/FileSystem.cs b/Users/Sentinel/SentinelSystem/FileSystem/FileSystem.cs index 6a63bf5c5..833d1a6aa 100644 --- a/Users/Sentinel/SentinelSystem/FileSystem/FileSystem.cs +++ b/Users/Sentinel/SentinelSystem/FileSystem/FileSystem.cs @@ -41,5 +41,7 @@ namespace SentinelKernel.System.FileSystem } public abstract List GetDirectoryListing(Directory baseDirectory); + + public abstract Directory GetRootDirectory(string name); } } diff --git a/Users/Sentinel/SentinelSystem/FileSystem/VFS/VFSManager.cs b/Users/Sentinel/SentinelSystem/FileSystem/VFS/VFSManager.cs index 66086361e..ff3766125 100644 --- a/Users/Sentinel/SentinelSystem/FileSystem/VFS/VFSManager.cs +++ b/Users/Sentinel/SentinelSystem/FileSystem/VFS/VFSManager.cs @@ -141,29 +141,34 @@ namespace SentinelKernel.System.FileSystem.VFS throw new ArgumentNullException("aPath"); } - return null; - - /* string xFileName = Path.GetFileName(aPath); - string xDirectory = Path.GetDirectoryName(aPath) + Path.DirectorySeparatorChar; - - foreach (var xEntry in GetDirectoryListing(xDirectory)) + string xDirectory = Path.GetDirectoryName(aPath); + var xLastChar = xDirectory[xDirectory.Length - 1]; + if (xLastChar != Path.DirectorySeparatorChar) { - if ((xEntry is FileSystem.Listing.File) && (xEntry.Name == xFileName)) + xDirectory = xDirectory + Path.DirectorySeparatorChar; + } + + + 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)) { - return (xEntry as FileSystem.Listing.File); + return xFile; } } return null; - */ } public static List GetFiles(string aPath) { if (string.IsNullOrEmpty(aPath)) { - throw new ArgumentNullException("sPath"); + throw new ArgumentNullException("aPath"); } return null; @@ -274,8 +279,10 @@ namespace SentinelKernel.System.FileSystem.VFS { return (VFSManager.GetFile(aPath) != null); } - catch (Exception) + catch (Exception E) { + Console.Write("Exception occurred: "); + Console.WriteLine(E.Message); return false; } } diff --git a/Users/Sentinel/SentinelSystem/SentinelSystem.csproj b/Users/Sentinel/SentinelSystem/SentinelSystem.csproj index df7d45ba1..480378ba4 100644 --- a/Users/Sentinel/SentinelSystem/SentinelSystem.csproj +++ b/Users/Sentinel/SentinelSystem/SentinelSystem.csproj @@ -59,7 +59,6 @@ - diff --git a/Users/Sentinel/SentinelSystem/SentinelVFS.cs b/Users/Sentinel/SentinelSystem/SentinelVFS.cs index 5e2fad0b6..a2cd8d8d8 100644 --- a/Users/Sentinel/SentinelSystem/SentinelVFS.cs +++ b/Users/Sentinel/SentinelSystem/SentinelVFS.cs @@ -8,6 +8,7 @@ using System.IO; using SentinelKernel.System.FileSystem.FAT; using SentinelKernel.System.FileSystem.Listing; using Console = global::System.Console; +using Directory = SentinelKernel.System.FileSystem.Listing.Directory; namespace SentinelKernel.System.FileSystem.VFS { @@ -199,12 +200,12 @@ namespace SentinelKernel.System.FileSystem.VFS for (int i = 0; i < mFileSystems.Count; i++) { string xTest = mFileSystems[i].Key; - if (xTest == xPath) + if (String.Equals(xTest, xPath)) { return mFileSystems[i].Value; } } - return null; + throw new Exception("Unable to determine filesystem for path: " + aPath); } public override void Initialize() @@ -221,55 +222,63 @@ namespace SentinelKernel.System.FileSystem.VFS public override Listing.Directory GetDirectory(string aPath) { - string[] xPathParts = VFSManager.SplitPath(aPath); var xFS = GetFileSystemFromPath(aPath); - if (xFS != null) + return DoGetDirectory(aPath, xFS); + } + + private Directory DoGetDirectory(string aPath, FileSystem aFS) + { + if (aFS == null) { - if (xPathParts.Length == 1) + throw new ArgumentNullException("aFS"); + } + string[] xPathParts = VFSManager.SplitPath(aPath); + + if (xPathParts.Length == 1) + { + return GetVolume(aFS, aPath); + } + + Listing.Directory xBaseDirectory = null; + + // start at index 1, because 0 is the volume + for (int i = 1; i < xPathParts.Length; i++) + { + var xPathPart = xPathParts[i]; + var xPartFound = false; + var xListing = aFS.GetDirectoryListing(xBaseDirectory); + + for (int j = 0; j < xListing.Count; j++) { - return GetVolume(xPathParts[0]); - } - - Listing.Directory xBaseDirectory = null; - - // start at index 1, because 0 is the volume - for (int i = 1; i < xPathParts.Length; i++) - { - var xPathPart = xPathParts[i]; - var xPartFound = false; - var xListing = xFS.GetDirectoryListing(xBaseDirectory); - - for (int j = 0; j < xListing.Count; j++) + var xListingItem = xListing[j]; + if (String.Equals(xListingItem.Name, xPathPart, StringComparison.OrdinalIgnoreCase)) { - var xListingItem = xListing[j]; - if (String.Equals(xListingItem.Name, xPathPart, StringComparison.OrdinalIgnoreCase)) + if (xListingItem is Listing.Directory) { - if (xListingItem is Listing.Directory) - { - xBaseDirectory = (Listing.Directory)xListingItem; - xPartFound = true; - } - else - { - throw new Exception("Path part '" + xPathPart + "' found, but not a directory!"); - } + xBaseDirectory = (Listing.Directory)xListingItem; + xPartFound = true; + } + else + { + throw new Exception("Path part '" + xPathPart + "' found, but not a directory!"); } } - - if (!xPartFound) - { - throw new Exception("Path part '" + xPathPart + "' not found!"); - } } - return xBaseDirectory; + + if (!xPartFound) + { + throw new Exception("Path part '" + xPathPart + "' not found!"); + } } - return null; + return xBaseDirectory; } public override List GetDirectoryListing(string aPath) { - throw new NotImplementedException(); + var xFS = GetFileSystemFromPath(aPath); + var xDirectory = DoGetDirectory(aPath, xFS); + return xFS.GetDirectoryListing(xDirectory); } public override List GetDirectoryListing(Listing.Directory aParentDirectory) @@ -287,6 +296,11 @@ namespace SentinelKernel.System.FileSystem.VFS throw new NotImplementedException(); } + public Listing.Directory GetVolume(FileSystem filesystem, string name) + { + return filesystem.GetRootDirectory(name); + } + /* public override SentinelKernel.System.FileSystem.Listing.Directory GetVolume(string aVolume) { diff --git a/Users/Sentinel/SentinelSystem/System/IO/PathImpl.cs b/Users/Sentinel/SentinelSystem/System/IO/PathImpl.cs index 89e0c7d49..2db8b757e 100644 --- a/Users/Sentinel/SentinelSystem/System/IO/PathImpl.cs +++ b/Users/Sentinel/SentinelSystem/System/IO/PathImpl.cs @@ -245,7 +245,12 @@ namespace SentinelKernel.System.Plugs.System.IO return null; } aPath = NormalizePath(aPath, false); - return aPath.Substring(0, GetRootLength(aPath)); + var xResult = aPath.Substring(0, GetRootLength(aPath)); + if (xResult[xResult.Length - 1] != Path.DirectorySeparatorChar) + { + xResult = xResult + Path.DirectorySeparatorChar; + } + return xResult; } public static string GetRandomFileName()