From 65fdc07baddd0dfd832daff127af99428abeaf1b Mon Sep 17 00:00:00 2001 From: Matthijs ter Woord Date: Sun, 5 Jul 2015 11:14:16 +0200 Subject: [PATCH] File.Exists works for files in subdirectories as well. --- Users/Sentinel/SentinelKernel/Kernel.cs | 11 +- .../FileSystem/FAT/FatFileSystem.cs | 113 ++++++++++-------- .../SentinelSystem/FileSystem/FatHelpers.cs | 2 +- .../FileSystem/VFS/VFSManager.cs | 1 - 4 files changed, 73 insertions(+), 54 deletions(-) diff --git a/Users/Sentinel/SentinelKernel/Kernel.cs b/Users/Sentinel/SentinelKernel/Kernel.cs index 7e8f84b46..beab38e10 100644 --- a/Users/Sentinel/SentinelKernel/Kernel.cs +++ b/Users/Sentinel/SentinelKernel/Kernel.cs @@ -41,7 +41,7 @@ namespace SentinelKernel } Console.WriteLine("Subfolder exists as well!"); - xTest = File.Exists(@"0:\Kudzu.txt"); + xTest = File.Exists(@"0:\KudzU.txt"); if (!xTest) { Console.WriteLine(@"\Kudzu.txt not found!"); @@ -50,6 +50,15 @@ namespace SentinelKernel Console.WriteLine("Kudzu.txt found!"); + xTest = File.Exists(@"0:\Test\DirInTest\Readme.txt"); + if (!xTest) + { + Console.WriteLine(@"\Test\DirInTest\Readme.txt not found!"); + return; + } + + Console.WriteLine(@"Test\DirInTest\Readme.txt found!"); + } catch (Exception e) { diff --git a/Users/Sentinel/SentinelSystem/FileSystem/FAT/FatFileSystem.cs b/Users/Sentinel/SentinelSystem/FileSystem/FAT/FatFileSystem.cs index 65f1c5817..c8cc165ec 100644 --- a/Users/Sentinel/SentinelSystem/FileSystem/FAT/FatFileSystem.cs +++ b/Users/Sentinel/SentinelSystem/FileSystem/FAT/FatFileSystem.cs @@ -241,17 +241,20 @@ namespace SentinelKernel.System.FileSystem.FAT { var xResult = new List(); //TODO: Change xLongName to StringBuilder + string xLongName = ""; + string xName = ""; for (UInt32 i = 0; i < xData.Length; i = i + 32) { FatHelpers.Debug("-------------------------------------------------"); - string xLongName = ""; byte xAttrib = xData[i + 11]; - FatHelpers.Debug("Attrib = " + xAttrib.ToString()); + byte xStatus = xData[i]; + + FatHelpers.Debug("Attrib = " + xAttrib.ToString() + ", Status = " + xStatus); if (xAttrib == DirectoryEntryAttributeConsts.LongName) { byte xType = xData[i + 12]; byte xOrd = xData[i]; - FatHelpers.Debug("Reading LFN with Seqnr " + xOrd.ToString()); + FatHelpers.Debug("Reading LFN with Seqnr " + xOrd.ToString() + ", Type = " + xType); if (xOrd == 0xE5) { FatHelpers.Debug("Skipping deleted entry"); @@ -280,65 +283,74 @@ namespace SentinelKernel.System.FileSystem.FAT } } xLongName = xLongPart + xLongName; + xLongPart = null; //TODO: LDIR_Chksum } } - string xName = xLongName; - byte xStatus = xData[i]; - if (xStatus == 0x00) + else { - // Empty slot, and no more entries after this - break; - } - else if (xStatus == 0x05) - { - // Japanese characters - We dont handle these - } - else if (xStatus == 0xE5) - { - // Empty slot, skip it - } - else if (xStatus >= 0x20) - { - if (xLongName.Length > 0) + xName = xLongName; + if (xStatus == 0x00) { - // Leading and trailing spaces are to be ignored according to spec. - // Many programs (including Windows) pad trailing spaces although it - // it is not required for long names. - // As per spec, ignore trailing periods - xName = xLongName.Trim(); - - //If there are trailing periods - int nameIndex = xName.Length - 1; - if (xName[nameIndex] == '.') - { - //Search backwards till we find the first non-period character - for (; nameIndex > 0; nameIndex--) - { - if (xName[nameIndex] != '.') - { - break; - } - } - //Substring to remove the periods - xName = xName.Substring(0, nameIndex + 1); - } + // Empty slot, and no more entries after this + break; } - else + else if (xStatus == 0x05) { - string xEntry = xData.GetAsciiString(i, 11); - xName = xEntry.Substring(0, 8).TrimEnd(); - string xExt = xEntry.Substring(8, 3).TrimEnd(); - if (xExt.Length > 0) + // Japanese characters - We dont handle these + } + else if (xStatus == 0xE5) + { + // Empty slot, skip it + } + else if (xStatus >= 0x20) + { + if (xLongName.Length > 0) { - xName = xName + "." + xExt; + // Leading and trailing spaces are to be ignored according to spec. + // Many programs (including Windows) pad trailing spaces although it + // it is not required for long names. + // As per spec, ignore trailing periods + xName = xLongName.Trim(); + + //If there are trailing periods + int nameIndex = xName.Length - 1; + if (xName[nameIndex] == '.') + { + //Search backwards till we find the first non-period character + for (; nameIndex > 0; nameIndex--) + { + if (xName[nameIndex] != '.') + { + break; + } + } + //Substring to remove the periods + xName = xName.Substring(0, nameIndex + 1); + } + xLongName = ""; + } + else + { + string xEntry = xData.GetAsciiString(i, 11); + xName = xEntry.Substring(0, 8).TrimEnd(); + string xExt = xEntry.Substring(8, 3).TrimEnd(); + if (xExt.Length > 0) + { + xName = xName + "." + xExt; + } } } } UInt32 xFirstCluster = (UInt32)(xData.ToUInt16(i + 20) << 16 | xData.ToUInt16(i + 26)); var xTest = xAttrib & (DirectoryEntryAttributeConsts.Directory | DirectoryEntryAttributeConsts.VolumeID); - if (xTest == 0) + if (xAttrib == DirectoryEntryAttributeConsts.LongName) + { + // skip adding, as it's a LongFileName entry, meaning the next normal entry is the item with the name. + FatHelpers.Debug("Entry was an Long FileName entry. Current LongName = '" + xLongName + "'"); + } + else if (xTest == 0) { UInt32 xSize = xData.ToUInt32(i + 28); if (xSize == 0 && xName.Length == 0) @@ -348,11 +360,11 @@ namespace SentinelKernel.System.FileSystem.FAT xResult.Add(new FatFile(this, xName, xSize, xFirstCluster)); FatHelpers.Debug("Returning file '" + xName + "'"); } - else if (xTest == DirectoryEntryAttributeConsts.Directory || xAttrib == DirectoryEntryAttributeConsts.LongName) + else if (xTest == DirectoryEntryAttributeConsts.Directory) { UInt32 xSize = xData.ToUInt32(i + 28); var xFatDirectory = new FatDirectory(this, xName, xFirstCluster); - FatHelpers.Debug("Returning directory '" + xFatDirectory.Name + "'"); + FatHelpers.Debug("Returning directory '" + xFatDirectory.Name + "', FirstCluster = " + xFirstCluster); xResult.Add(xFatDirectory); } else if (xTest == DirectoryEntryAttributeConsts.VolumeID) @@ -364,7 +376,6 @@ namespace SentinelKernel.System.FileSystem.FAT { FatHelpers.Debug("Not sure what to do!"); } - xLongName = ""; } return xResult; diff --git a/Users/Sentinel/SentinelSystem/FileSystem/FatHelpers.cs b/Users/Sentinel/SentinelSystem/FileSystem/FatHelpers.cs index 5e197996b..043221718 100644 --- a/Users/Sentinel/SentinelSystem/FileSystem/FatHelpers.cs +++ b/Users/Sentinel/SentinelSystem/FileSystem/FatHelpers.cs @@ -8,7 +8,7 @@ namespace SentinelKernel.System.FileSystem private static Debugger mDebugger = new Debugger("FAT", "Debug"); public static void Debug(string message) { - mDebugger.Send("FAT Debug: " + message); + //mDebugger.Send("FAT Debug: " + message); } } } diff --git a/Users/Sentinel/SentinelSystem/FileSystem/VFS/VFSManager.cs b/Users/Sentinel/SentinelSystem/FileSystem/VFS/VFSManager.cs index ff3766125..fea00ecd4 100644 --- a/Users/Sentinel/SentinelSystem/FileSystem/VFS/VFSManager.cs +++ b/Users/Sentinel/SentinelSystem/FileSystem/VFS/VFSManager.cs @@ -149,7 +149,6 @@ namespace SentinelKernel.System.FileSystem.VFS xDirectory = xDirectory + Path.DirectorySeparatorChar; } - var xList = GetDirectoryListing(xDirectory); for (int i = 0; i < xList.Count; i++) {