Skip to content

Commit

Permalink
FAT file system work.
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesbetros committed Apr 14, 2016
1 parent ef92828 commit 3fcc286
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 53 deletions.
6 changes: 5 additions & 1 deletion source/Cosmos.IL2CPU/Plugs/System/EnvironmentImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Cosmos.IL2CPU.Plugs.System
{
[Plug(Target=typeof(Environment))]
[Plug(Target = typeof(Environment))]
public static class EnvironmentImpl
{
[PlugMethod(Signature = "System_Environment_OSName__System_Environment_get_OSInfo__")]
Expand Down Expand Up @@ -78,6 +78,10 @@ public static string GetResourceFromDefault(string aResource)
{
return "Value does not fall within the expected range.";
}
if (aResource == "Arg_ArgumentOutOfRangeException")
{
return "Specified argument was out of the range of valid values.";
}
if (aResource == "Arg_DirectoryNotFoundException")
{
return "Attempted to access a path that is not on the disk.";
Expand Down
2 changes: 1 addition & 1 deletion source/Cosmos.System.Plugs/System/IO/DirectoryImpl.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//#define COSMOSDEBUG
//define COSMOSDEBUG

using System;
using System.IO;
Expand Down
2 changes: 1 addition & 1 deletion source/Cosmos.System.Plugs/System/IO/DirectoryInfoImpl.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//#define COSMOSDEBUG
//define COSMOSDEBUG

using System;
using System.Collections.Generic;
Expand Down
2 changes: 1 addition & 1 deletion source/Cosmos.System.Plugs/System/IO/FileImpl.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//#define COSMOSDEBUG
//define COSMOSDEBUG

using global::System;
using global::System.IO;
Expand Down
2 changes: 1 addition & 1 deletion source/Cosmos.System.Plugs/System/IO/FileStreamImpl.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//#define COSMOSDEBUG
//define COSMOSDEBUG

using global::System;
using global::System.IO;
Expand Down
2 changes: 1 addition & 1 deletion source/Cosmos.System.Plugs/System/IO/PathImpl.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//#define COSMOSDEBUG
//define COSMOSDEBUG

using System;
using System.IO;
Expand Down
11 changes: 10 additions & 1 deletion source/Cosmos.System/FileSystem/CosmosVFS.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//#define COSMOSDEBUG
#define COSMOSDEBUG

using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -281,8 +281,17 @@ protected virtual void InitializePartitions()
{
for (int i = 0; i < mPartitions.Count; i++)
{
Global.mFileSystemDebugger.SendInternal("Partition #:");
Global.mFileSystemDebugger.SendInternal(i + 1);
global::System.Console.WriteLine("Partition #: " + (i + 1));
Global.mFileSystemDebugger.SendInternal("Block Size:");
Global.mFileSystemDebugger.SendInternal(mPartitions[i].BlockSize);
global::System.Console.WriteLine("Block Size: " + mPartitions[i].BlockSize + " bytes");
Global.mFileSystemDebugger.SendInternal("Block Count:");
Global.mFileSystemDebugger.SendInternal(mPartitions[i].BlockCount);
global::System.Console.WriteLine("Block Count: " + mPartitions[i].BlockCount);
Global.mFileSystemDebugger.SendInternal("Size:");
Global.mFileSystemDebugger.SendInternal(mPartitions[i].BlockCount * mPartitions[i].BlockSize / 1024 / 1024);
global::System.Console.WriteLine("Size: " + mPartitions[i].BlockCount * mPartitions[i].BlockSize / 1024 / 1024 + " MB");
}
}
Expand Down
35 changes: 33 additions & 2 deletions source/Cosmos.System/FileSystem/FAT/FatFileSystem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//#define COSMOSDEBUG
#define COSMOSDEBUG

using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -586,21 +586,52 @@ public static bool IsDeviceFat(Partition aDevice)

public override void DisplayFileSystemInfo()
{
Global.mFileSystemDebugger.SendInternal(nameof(DisplayFileSystemInfo));
global::System.Console.WriteLine("-------File System--------");
Global.mFileSystemDebugger.SendInternal("Bytes per Cluster:");
Global.mFileSystemDebugger.SendInternal(BytesPerCluster);
global::System.Console.WriteLine("Bytes per Cluster: " + BytesPerCluster);
Global.mFileSystemDebugger.SendInternal("Bytes per Sector:");
Global.mFileSystemDebugger.SendInternal(BytesPerSector);
global::System.Console.WriteLine("Bytes per Sector: " + BytesPerSector);
Global.mFileSystemDebugger.SendInternal("Cluster Count:");
Global.mFileSystemDebugger.SendInternal(ClusterCount);
global::System.Console.WriteLine("Cluster Count: " + ClusterCount);
Global.mFileSystemDebugger.SendInternal("Data Sector:");
Global.mFileSystemDebugger.SendInternal(DataSector);
global::System.Console.WriteLine("Data Sector: " + DataSector);
Global.mFileSystemDebugger.SendInternal("Data Sector Count:");
Global.mFileSystemDebugger.SendInternal(DataSectorCount);
global::System.Console.WriteLine("Data Sector Count: " + DataSectorCount);
Global.mFileSystemDebugger.SendInternal("FAT Sector Count:");
Global.mFileSystemDebugger.SendInternal(FatSectorCount);
global::System.Console.WriteLine("FAT Sector Count: " + FatSectorCount);
global::System.Console.WriteLine("FAT Type: " + mFatType);
Global.mFileSystemDebugger.SendInternal("FAT Type:");
Global.mFileSystemDebugger.SendInternal((uint)mFatType);
global::System.Console.WriteLine("FAT Type: " + (uint)mFatType);
Global.mFileSystemDebugger.SendInternal("Number of FATS:");
Global.mFileSystemDebugger.SendInternal(NumberOfFATs);
global::System.Console.WriteLine("Number of FATS: " + NumberOfFATs);
Global.mFileSystemDebugger.SendInternal("Reserved Sector Count:");
Global.mFileSystemDebugger.SendInternal(ReservedSectorCount);
global::System.Console.WriteLine("Reserved Sector Count: " + ReservedSectorCount);
Global.mFileSystemDebugger.SendInternal("Root Cluster:");
Global.mFileSystemDebugger.SendInternal(RootCluster);
global::System.Console.WriteLine("Root Cluster: " + RootCluster);
Global.mFileSystemDebugger.SendInternal("Root Entry Count:");
Global.mFileSystemDebugger.SendInternal(RootEntryCount);
global::System.Console.WriteLine("Root Entry Count: " + RootEntryCount);
Global.mFileSystemDebugger.SendInternal("Root Sector:");
Global.mFileSystemDebugger.SendInternal(RootSector);
global::System.Console.WriteLine("Root Sector: " + RootSector);
Global.mFileSystemDebugger.SendInternal("Root Sector Count:");
Global.mFileSystemDebugger.SendInternal(RootSectorCount);
global::System.Console.WriteLine("Root Sector Count: " + RootSectorCount);
Global.mFileSystemDebugger.SendInternal("Sectors per Cluster:");
Global.mFileSystemDebugger.SendInternal(SectorsPerCluster);
global::System.Console.WriteLine("Sectors per Cluster: " + SectorsPerCluster);
Global.mFileSystemDebugger.SendInternal("Total Sector Count:");
Global.mFileSystemDebugger.SendInternal(TotalSectorCount);
global::System.Console.WriteLine("Total Sector Count: " + TotalSectorCount);
}

Expand Down
39 changes: 17 additions & 22 deletions source/Cosmos.System/FileSystem/FAT/FatStream.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//#define COSMOSDEBUG
#define COSMOSDEBUG

using System;
using System.IO;
Expand Down Expand Up @@ -83,7 +83,9 @@ public sealed override long Length
{
throw new NullReferenceException("The stream does not currently have an open entry.");
}
Global.mFileSystemDebugger.SendInternal($"FatStream.get_Length : Length = {(long)mSize}");
Global.mFileSystemDebugger.SendInternal("FatStream.get_Length:");
Global.mFileSystemDebugger.SendInternal("Length =");
Global.mFileSystemDebugger.SendInternal(mSize.ToString());
return (long)mSize;
}
}
Expand Down Expand Up @@ -113,8 +115,6 @@ public override long Position

public override int Read(byte[] aBuffer, int aOffset, int aCount)
{
Global.mFileSystemDebugger.SendInternal("FatStream.Read:");

return Read(aBuffer, aOffset, aCount);
}

Expand Down Expand Up @@ -212,21 +212,6 @@ public override void SetLength(long value)

public override void Write(byte[] aBuffer, int aOffset, int aCount)
{
Global.mFileSystemDebugger.SendInternal("FatStream.Write:");

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!");
}

Write(aBuffer, aOffset, aCount);
}

Expand All @@ -248,17 +233,27 @@ protected void Write(byte[] aBuffer, long aOffset, long aCount)
}

Global.mFileSystemDebugger.SendInternal("aBuffer.Length =");
Global.mFileSystemDebugger.SendInternal((uint)aBuffer.Length);
Global.mFileSystemDebugger.SendInternal(aBuffer.Length);
Global.mFileSystemDebugger.SendInternal("aOffset =");
Global.mFileSystemDebugger.SendInternal((uint)aOffset);
Global.mFileSystemDebugger.SendInternal(aOffset);
Global.mFileSystemDebugger.SendInternal("aCount =");
Global.mFileSystemDebugger.SendInternal((uint)aCount);
Global.mFileSystemDebugger.SendInternal(aCount);
Global.mFileSystemDebugger.SendInternal("mPosition =");
Global.mFileSystemDebugger.SendInternal(mPosition);

ulong xCount = (ulong)aCount;
Global.mFileSystemDebugger.SendInternal("xCount =");
Global.mFileSystemDebugger.SendInternal(xCount);

var xCluster = mFS.NewClusterArray();
uint xClusterSize = mFS.BytesPerCluster;

long xTotalLength = (long)(mPosition + xCount);
Global.mFileSystemDebugger.SendInternal("xTotalLength =");
Global.mFileSystemDebugger.SendInternal(xTotalLength);
Global.mFileSystemDebugger.SendInternal("Length =");
Global.mFileSystemDebugger.SendInternal(Length);

if (xTotalLength > Length)
{
SetLength(xTotalLength);
Expand Down
84 changes: 63 additions & 21 deletions source/Cosmos.System/FileSystem/FAT/Listing/FatDiretoryEntry.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//#define COSMOSDEBUG
#define COSMOSDEBUG

using Cosmos.Common.Extensions;
using Cosmos.System.FileSystem.Listing;
Expand Down Expand Up @@ -33,6 +33,8 @@ public FatDirectoryEntry(

if (aFirstCluster < 2)
{
Global.mFileSystemDebugger.SendInternal("aFirstCluster =");
Global.mFileSystemDebugger.SendInternal(aFirstCluster.ToString());
Global.mFileSystemDebugger.SendInternal("aFirstCluster is out of range.");
throw new ArgumentOutOfRangeException(nameof(aFirstCluster));
}
Expand All @@ -53,7 +55,10 @@ public FatDirectoryEntry(

if (aFirstCluster < 2)
{
Global.mFileSystemDebugger.SendInternal("aFirstCluster =");
Global.mFileSystemDebugger.SendInternal(aFirstCluster.ToString());
Global.mFileSystemDebugger.SendInternal("aFirstCluster is out of range.");

throw new ArgumentOutOfRangeException(nameof(aFirstCluster));
}

Expand Down Expand Up @@ -100,7 +105,7 @@ public override void SetName(string aName)
Global.mFileSystemDebugger.SendInternal("mName =");
Global.mFileSystemDebugger.SendInternal(mName);
Global.mFileSystemDebugger.SendInternal("mSize =");
Global.mFileSystemDebugger.SendInternal(mSize.ToString());
Global.mFileSystemDebugger.SendInternal(mSize);
Global.mFileSystemDebugger.SendInternal("aName =");
Global.mFileSystemDebugger.SendInternal(aName);

Expand All @@ -114,8 +119,14 @@ public override void SetSize(long aSize)
throw new ArgumentOutOfRangeException(nameof(aSize));
}

Global.mFileSystemDebugger.SendInternal($"FatDirectoryEntry.SetSize : mName = {mName}, mSize = {mSize}, aSize = {aSize}");
SetDirectoryEntryMetadataValue(FatDirectoryEntryMetadata.Size, (uint)aSize);
Global.mFileSystemDebugger.SendInternal("FatDirectoryEntry.SetSize:");
Global.mFileSystemDebugger.SendInternal("mName =");
Global.mFileSystemDebugger.SendInternal(mName);
Global.mFileSystemDebugger.SendInternal("mSize =");
Global.mFileSystemDebugger.SendInternal(mSize);
Global.mFileSystemDebugger.SendInternal("aSize =");
Global.mFileSystemDebugger.SendInternal((ulong)aSize);
SetDirectoryEntryMetadataValue(FatDirectoryEntryMetadata.Size, (ulong)aSize);
}

private void AllocateDirectoryEntry()
Expand All @@ -124,11 +135,11 @@ private void AllocateDirectoryEntry()
Global.mFileSystemDebugger.SendInternal("FatDirectoryEntry.AllocateDirectoryEntry:");
Global.mFileSystemDebugger.SendInternal("mName = ");
Global.mFileSystemDebugger.SendInternal(mName);
char[] xName = new char[]
{
(char)0x20, (char)0x20, (char)0x20, (char)0x20, (char)0x20, (char)0x20, (char)0x20, (char)0x20,
(char)0x20, (char)0x20, (char)0x20
};
char[] xName =
{
(char)0x20, (char)0x20, (char)0x20, (char)0x20, (char)0x20, (char)0x20, (char)0x20,
(char)0x20, (char)0x20, (char)0x20, (char)0x20
};

int j = 0;
for (int i = 0; i < mName.Length; i++)
Expand Down Expand Up @@ -167,8 +178,8 @@ public FatDirectoryEntry AddDirectoryEntry(string aName, DirectoryEntryTypeEnum
Global.mFileSystemDebugger.SendInternal("FatDirectoryEntry.AddDirectoryEntry:");
Global.mFileSystemDebugger.SendInternal("aName =");
Global.mFileSystemDebugger.SendInternal(aName);
Global.mFileSystemDebugger.SendInternal("aType = ");
Global.mFileSystemDebugger.SendInternal(((int)aType).ToString());
Global.mFileSystemDebugger.SendInternal("aType =");
Global.mFileSystemDebugger.SendInternal((uint)aType);

if ((aType == DirectoryEntryTypeEnum.Directory) || (aType == DirectoryEntryTypeEnum.File))
{
Expand All @@ -194,16 +205,16 @@ public List<FatDirectoryEntry> ReadDirectoryContents()
string xName = "";
for (uint i = 0; i < xData.Length; i = i + 32)
{
Global.mFileSystemDebugger.SendInternal($"-------------------------------------------------");
//Global.mFileSystemDebugger.SendInternal($"-------------------------------------------------");
byte xAttrib = xData[i + 11];
byte xStatus = xData[i];

Global.mFileSystemDebugger.SendInternal($"Attrib = {xAttrib}, Status = {xStatus}");
//Global.mFileSystemDebugger.SendInternal($"Attrib = {xAttrib}, Status = {xStatus}");
if (xAttrib == FatDirectoryEntryAttributeConsts.LongName)
{
byte xType = xData[i + 12];
byte xOrd = xData[i];
Global.mFileSystemDebugger.SendInternal($"Reading LFN with Seqnr = {xOrd}, Type = {xType}");
//Global.mFileSystemDebugger.SendInternal($"Reading LFN with Seqnr = {xOrd}, Type = {xType}");
if (xOrd == 0xE5)
{
Global.mFileSystemDebugger.SendInternal($"<DELETED> : Attrib = {xAttrib}, Status = {xStatus}");
Expand Down Expand Up @@ -300,7 +311,7 @@ public List<FatDirectoryEntry> ReadDirectoryContents()
if (xAttrib == FatDirectoryEntryAttributeConsts.LongName)
{
// skip adding, as it's a LongFileName entry, meaning the next normal entry is the item with the name.
Global.mFileSystemDebugger.SendInternal($"Entry was a Long FileName entry. Current LongName = '{xLongName}'");
//Global.mFileSystemDebugger.SendInternal($"Entry was a Long FileName entry. Current LongName = '{xLongName}'");
}
else if (xTest == 0)
{
Expand Down Expand Up @@ -338,7 +349,9 @@ public List<FatDirectoryEntry> ReadDirectoryContents()
private uint GetNextUnallocatedEntry()
{
var xData = GetDirectoryEntryData();
Global.mFileSystemDebugger.SendInternal($"FatDirectoryEntry.GetNextUnallocatedEntry : xData.Length = {xData.Length}");
Global.mFileSystemDebugger.SendInternal("FatDirectoryEntry.GetNextUnallocatedEntry:");
Global.mFileSystemDebugger.SendInternal("xData.Length =");
Global.mFileSystemDebugger.SendInternal(xData.Length);
for (uint i = 0; i < xData.Length; i += 32)
{
uint x1 = xData.ToUInt32(i);
Expand All @@ -347,7 +360,8 @@ private uint GetNextUnallocatedEntry()
uint x4 = xData.ToUInt32(i + 24);
if ((x1 == 0) && (x2 == 0) && (x3 == 0) && (x4 == 0))
{
Global.mFileSystemDebugger.SendInternal($"FatDirectoryEntry.GetNextUnallocatedEntry : returning {i}");
Global.mFileSystemDebugger.SendInternal("returning");
Global.mFileSystemDebugger.SendInternal(i);
return i;
}
}
Expand All @@ -373,14 +387,12 @@ private void SetDirectoryEntryData(byte[] aData)
{
if (aData == null)
{
Global.mFileSystemDebugger.SendInternal($"FatDirectoryEntry.SetDirectoryEntryData : aData is null.");
throw new ArgumentNullException("aData");
throw new ArgumentNullException(nameof(aData));
}

if (aData.Length == 0)
{
Global.mFileSystemDebugger.SendInternal($"FatDirectoryEntry.SetDirectoryEntryData : aData length is 0.");
return;
throw new ArgumentException("aData does not contain any data.", nameof(aData));
}

Global.mFileSystemDebugger.SendInternal($"FatDirectoryEntry.SetDirectoryEntryData : Name = {mName}");
Expand Down Expand Up @@ -427,6 +439,36 @@ internal void SetDirectoryEntryMetadataValue(FatDirectoryEntryMetadata aEntryMet
}
}

internal void SetDirectoryEntryMetadataValue(FatDirectoryEntryMetadata aEntryMetadata, ulong aValue)
{
if (mParent != null)
{
var xData = ((FatDirectoryEntry)mParent).GetDirectoryEntryData();
if (xData.Length > 0)
{
var xValue = new byte[aEntryMetadata.DataLength];
xValue.SetUInt64(0, aValue);

uint offset = mEntryHeaderDataOffset + aEntryMetadata.DataOffset;

Array.Copy(xValue, 0, xData, offset, aEntryMetadata.DataLength);

Global.mFileSystemDebugger.SendInternal($"FatDirectoryEntry.SetDirectoryEntryMetadataValue : DataLength = {aEntryMetadata.DataLength}");
Global.mFileSystemDebugger.SendInternal($"FatDirectoryEntry.SetDirectoryEntryMetadataValue : DataOffset = {aEntryMetadata.DataOffset}");
Global.mFileSystemDebugger.SendInternal($"FatDirectoryEntry.SetDirectoryEntryMetadataValue : EntryHeaderDataOffset = {mEntryHeaderDataOffset}");
Global.mFileSystemDebugger.SendInternal($"FatDirectoryEntry.SetDirectoryEntryMetadataValue : TotalOffset = {offset}");
Global.mFileSystemDebugger.SendInternal($"FatDirectoryEntry.SetDirectoryEntryMetadataValue : aValue = {aValue}");

((FatDirectoryEntry)mParent).SetDirectoryEntryData(xData);
}
}
else
{
throw new Exception("Root directory metadata can not be changed using the file stream.");
}
}


internal void SetDirectoryEntryMetadataValue(FatDirectoryEntryMetadata aEntryMetadata, string aValue)
{
var xData = ((FatDirectoryEntry)mParent).GetDirectoryEntryData();
Expand Down
Loading

0 comments on commit 3fcc286

Please sign in to comment.