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); }
private List <Base> GetDirectoryContents(FatDirectory directory, Stream directoryContents) { if (directory == null) { throw new ArgumentNullException("directory"); } FatHelpers.Debug("--GetDirectoryContents"); if (directoryContents == null) { FatHelpers.Debug("No Stream specified!"); } else { FatHelpers.Debug("Stream specified."); } var xResult = new List <Base>(); //TODO: Change xLongName to StringBuilder string xLongName = ""; string xName = ""; var xBuff = new byte[32]; FatHelpers.Debug("Stream.Position"); FatHelpers.DebugNumber((uint)directoryContents.Position); FatHelpers.Debug("Stream.Length"); FatHelpers.DebugNumber((uint)directoryContents.Length); if (directoryContents.Position < directoryContents.Length) { FatHelpers.Debug("Data to read!"); } else { FatHelpers.Debug("No data to read!"); } while (directoryContents.Position < directoryContents.Length) { if (directoryContents.Read(xBuff, 0, 32) != 32) { FatHelpers.Debug("Not enough data read!"); throw new Exception("Unable to read 32 bytes from stream!"); } FatHelpers.Debug("No data read"); FatHelpers.DevDebug("-------------------------------------------------"); byte xAttrib = xBuff[11]; byte xStatus = xBuff[0]; FatHelpers.DevDebug("Attrib = " + xAttrib.ToString() + ", Status = " + xStatus); if (xAttrib == DirectoryEntryAttributeConsts.LongName) { byte xType = xBuff[12]; byte xOrd = xBuff[0]; FatHelpers.DevDebug("Reading LFN with Seqnr " + xOrd.ToString() + ", Type = " + xType); if (xOrd == 0xE5) { FatHelpers.DevDebug("Skipping deleted entry"); continue; } if (xType == 0) { if ((xOrd & 0x40) > 0) { xLongName = ""; } //TODO: Check LDIR_Ord for ordering and throw exception // if entries are found out of order. // Also save buffer and only copy name if a end Ord marker is found. string xLongPart = xBuff.GetUtf16String(0 + 1, 5); // We have to check the length because 0xFFFF is a valid Unicode codepoint. // So we only want to stop if the 0xFFFF is AFTER a 0x0000. We can determin // this by also looking at the length. Since we short circuit the or, the length // is rarely evaluated. if (xBuff.ToUInt16(0 + 14) != 0xFFFF || xLongPart.Length == 5) { xLongPart = xLongPart + xBuff.GetUtf16String(0 + 14, 6); if (xBuff.ToUInt16(0 + 28) != 0xFFFF || xLongPart.Length == 11) { xLongPart = xLongPart + xBuff.GetUtf16String(0 + 28, 2); } } xLongName = xLongPart + xLongName; xLongPart = null; //TODO: LDIR_Chksum } } else { xName = xLongName; if (xStatus == 0x00) { // 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) { // 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 = xBuff.GetAsciiString(0, 11); xName = xEntry.Substring(0, 8).TrimEnd(); string xExt = xEntry.Substring(8, 3).TrimEnd(); if (xExt.Length > 0) { xName = xName + "." + xExt; } } } } UInt32 xFirstCluster = (UInt32)(xBuff.ToUInt16(0 + 20) << 16 | xBuff.ToUInt16(0 + 26)); var xTest = xAttrib & (DirectoryEntryAttributeConsts.Directory | DirectoryEntryAttributeConsts.VolumeID); 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 = xBuff.ToUInt32(0 + 28); if (xSize == 0 && xName.Length == 0) { continue; } xResult.Add(new FatFile(this, xName, xSize, xFirstCluster, directory)); FatHelpers.Debug("Returning file '" + xName + "'"); } else if (xTest == DirectoryEntryAttributeConsts.Directory) { UInt32 xSize = xBuff.ToUInt32(0 + 28); var xFatDirectory = new FatDirectory(this, xName, xFirstCluster, directory, xSize); FatHelpers.Debug("Returning directory '" + xFatDirectory.Name + "', FirstCluster = " + xFirstCluster); xResult.Add(xFatDirectory); } else if (xTest == DirectoryEntryAttributeConsts.VolumeID) { FatHelpers.DevDebug("Directory entry is VolumeID"); // } else { FatHelpers.DevDebug("Not sure what to do!"); } } return(xResult); }
public void SetFileLength(Base file, long value) { if (file == null) { throw new ArgumentNullException(nameof(file)); } FatHelpers.Debug("File.Name:"); FatHelpers.Debug(file.Name); var xIsFile = file is FatFile; var xIsDirectory = !xIsFile; using (var xDirectoryContents = new FatDirectoryStream((FatDirectory)file.BaseDirectory)) { FatHelpers.Debug("--SetFileLength"); FatHelpers.DebugNumber((uint)xDirectoryContents.Length); //TODO: Change xLongName to StringBuilder string xLongName = ""; string xName = ""; var xBuff = new byte[32]; while (xDirectoryContents.Position < xDirectoryContents.Length) { if (xDirectoryContents.Read(xBuff, 0, 32) != 32) { throw new Exception("Unable to read 32 bytes from stream!"); } FatHelpers.DevDebug("-------------------------------------------------"); byte xAttrib = xBuff[11]; byte xStatus = xBuff[0]; FatHelpers.DevDebug("Attrib = " + xAttrib.ToString() + ", Status = " + xStatus); if (xAttrib == DirectoryEntryAttributeConsts.LongName) { byte xType = xBuff[12]; byte xOrd = xBuff[0]; FatHelpers.DevDebug("Reading LFN with Seqnr " + xOrd.ToString() + ", Type = " + xType); if (xOrd == 0xE5) { FatHelpers.DevDebug("Skipping deleted entry"); continue; } if (xType == 0) { if ((xOrd & 0x40) > 0) { xLongName = ""; } //TODO: Check LDIR_Ord for ordering and throw exception // if entries are found out of order. // Also save buffer and only copy name if a end Ord marker is found. string xLongPart = xBuff.GetUtf16String(0 + 1, 5); // We have to check the length because 0xFFFF is a valid Unicode codepoint. // So we only want to stop if the 0xFFFF is AFTER a 0x0000. We can determin // this by also looking at the length. Since we short circuit the or, the length // is rarely evaluated. if (xBuff.ToUInt16(0 + 14) != 0xFFFF || xLongPart.Length == 5) { xLongPart = xLongPart + xBuff.GetUtf16String(0 + 14, 6); if (xBuff.ToUInt16(0 + 28) != 0xFFFF || xLongPart.Length == 11) { xLongPart = xLongPart + xBuff.GetUtf16String(0 + 28, 2); } } xLongName = xLongPart + xLongName; xLongPart = null; //TODO: LDIR_Chksum } } else { xName = xLongName; if (xStatus == 0x00) { // 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) { // 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 = xBuff.GetAsciiString(0, 11); xName = xEntry.Substring(0, 8).TrimEnd(); string xExt = xEntry.Substring(8, 3).TrimEnd(); if (xExt.Length > 0) { xName = xName + "." + xExt; } } } } UInt32 xFirstCluster = (UInt32)(xBuff.ToUInt16(0 + 20) << 16 | xBuff.ToUInt16(0 + 26)); var xTest = xAttrib & (DirectoryEntryAttributeConsts.Directory | DirectoryEntryAttributeConsts.VolumeID); 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) { if (xIsFile && String.Equals(file.Name, xName, StringComparison.OrdinalIgnoreCase)) { // found, now update size and write back xBuff.SetUInt32(28, (uint)value); xDirectoryContents.Position -= 32; xDirectoryContents.Write(xBuff, 0, 32); FatHelpers.Debug("--- File size changed!"); return; } } else if (xTest == DirectoryEntryAttributeConsts.Directory) { if (xIsDirectory && String.Equals(file.Name, xName, StringComparison.OrdinalIgnoreCase)) { xBuff.SetUInt32(28, (uint)value); xDirectoryContents.Position -= 32; xDirectoryContents.Write(xBuff, 0, 32); FatHelpers.Debug("--- Directory size changed!"); return; } } else if (xTest == DirectoryEntryAttributeConsts.VolumeID) { FatHelpers.DevDebug("Directory entry is VolumeID"); // } else { FatHelpers.DevDebug("Not sure what to do!"); } } } throw new Exception("Unable to find the file entry!"); }
protected void Write(byte[] aBuffer, long aOffset, long aCount) { if (aCount < 0) { throw new ArgumentOutOfRangeException(nameof(aCount)); } if (aOffset < 0) { throw new ArgumentOutOfRangeException(nameof(aOffset)); } if (aBuffer == null || aBuffer.Length - aOffset < aCount) { throw new ArgumentException("Invalid offset length!"); } ulong xCount = (ulong)aCount; var xCluster = mFileSystem.NewClusterArray(); UInt32 xClusterSize = mFileSystem.BytesPerCluster; long xTotalLength = (long)(mPosition + xCount); if (xTotalLength > Length) { SetLength(xTotalLength); } while (xCount > 0) { long xWriteSize; UInt64 xClusterIdx = mPosition / xClusterSize; UInt64 xPosInCluster = mPosition % xClusterSize; if (xPosInCluster + xCount > xClusterSize) { xWriteSize = (long)(xClusterSize - xPosInCluster - 1); } else { xWriteSize = (long)xCount; } mFileSystem.ReadCluster(xClusterIdx, xCluster); FatHelpers.Debug("Writing to cluster idx"); FatHelpers.DebugNumber((uint)xClusterIdx); FatHelpers.Debug("Writing to pos in cluster"); FatHelpers.DebugNumber((uint)xPosInCluster); FatHelpers.Debug("Offset"); FatHelpers.DebugNumber((uint)aOffset); FatHelpers.Debug("xWriteSize"); FatHelpers.DebugNumber((uint)xWriteSize); FatHelpers.Debug("First byte"); FatHelpers.DebugNumber((uint)aBuffer[0]); Array.Copy(aBuffer, aOffset, xCluster, (long)xPosInCluster, xWriteSize); mFileSystem.WriteCluster(mFatTable[(int)xClusterIdx], xCluster); aOffset += xWriteSize; xCount -= (ulong)xWriteSize; } mPosition += (ulong)aOffset; }