-
Notifications
You must be signed in to change notification settings - Fork 551
FAT File System
File Allocation Table (FAT) is a computer file system architecture and a family of industry-standard file systems utilizing it. The FAT file system is a legacy file system which is simple and robust. It offers good performance even in light-weight implementations, but cannot deliver the same performance, reliability and scalability as some modern file systems. It is, however, supported for compatibility reasons by nearly all currently developed operating systems for personal computers and many mobile devices and embedded systems, and thus is a well-suited format for data exchange between computers and devices of almost any type and age from 1981 up to the present.
Referenced from File Allocation Table from Wikipedia
FAT support is currently under construction, and there are known issues. It's not recommended to use the implementation in production code. Usually, a Virtual File System (VFS) to manage the file system under Cosmos.
Remember, prior to when you can use the Cosmos VFS you need to include the following namespaces:
using Cosmos.System.FileSystem.VFS;
using Cosmos.System.FileSystem;
First, we need to create and initialize an instance of the VFS; this will initialize the partition and filesystem lists, as well as register said VFS.
We start with creating a global CosmosVFS
, this line should appear outside of any function, and before the BeforeRun()
function.
CosmosVFS fs = new Sys.FileSystem.CosmosVFS();
Next, we register our VFS with the VFSManager
. This will initialize the VFS and make it usable. Add this line to your kernel's BeforeRun()
:
Sys.FileSystem.VFS.VFSManager.RegisterVFS(fs);
After the initialization process, a message like this would appear on your screen:
This message is printed by the RegisterVFS
method and it provides debugging information about the file system.
After the initialization of a VFS, we can use more interesting functions:
We use this function to get the size of the available free space in our file system, in bytes.
long availableSpace = Sys.FileSystem.VFS.VFSManager.GetAvailableFreeSpace("0:\\");
Console.WriteLine("Available Free Space: " + availableSpace);
You have probably noticed the "0:\\" argument passed to this function; this is the ID of the drive that we want to get available free space of. Cosmos uses DOS-like drive naming, and this is why we use "0".
This will let us know what file system type we are using.
string fsType = Sys.FileSystem.VFS.VFSManager.GetFileSystemType("0:\\");
Console.WriteLine("File System Type: " + fsType );
We start by getting the directory listing:
var directoryList = Sys.FileSystem.VFS.VFSManager.GetDirectoryListing("0:\\");
Once we have it, we can get the names of our files:
foreach (var directoryEntry in directoryList)
{
Console.WriteLine(directoryEntry.mName);
}
In order to read all of the files in a directory, we need to get a DirectoryEntry
, find files in the list and, print the content to the screen.
We start with getting the directory listing:
var directoryList = Sys.FileSystem.VFS.VFSManager.GetDirectoryListing("0:\\");
Now we can go through our list, and, for example, print the textual content of the iterated files.
try
{
foreach (var directoryEntry in directoryList)
{
var fileStream = directoryEntry.GetFileStream();
var entryType = directoryEntry.mEntryType;
if(entryType == Sys.FileSystem.Listing.DirectoryEntryTypeEnum.File)
{
byte[] content = new byte[fileStream.Length];
fileStream.Read(content, 0, (int)fileStream.Length);
Console.WriteLine("File name: " + directoryEntry.mName);
Console.WriteLine("File size: " + directoryEntry.mSize);
Console.WriteLine("Content: ");
foreach (char ch in content)
{
Console.Write(ch.ToString());
}
Console.WriteLine();
}
}
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
In order to read a specific file, we can use the GetFile
method of the VFSManager
class:
try
{
var helloFile = Sys.FileSystem.VFS.VFSManager.GetFile(@"0:\hello_from_elia.txt");
var helloFileStream = helloFile.GetFileStream();
if (helloFileStream.CanRead)
{
byte[] textToRead = new byte[helloFileStream.Length];
helloFileStream.Read(textToRead, 0, (int)helloFileStream.Length);
Console.WriteLine(Encoding.Default.GetString(textToRead));
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Writing to a file is almost the same as reading from a file.
try
{
var helloFile = Sys.FileSystem.VFS.VFSManager.GetFile(@"0:\hello_from_elia.txt");
var helloFileStream = helloFile.GetFileStream();
if (helloFileStream.CanWrite)
{
byte[] textToWrite = Encoding.ASCII.GetBytes("Learning how to use VFS!");
helloFileStream.Write(textToWrite, 0, textToWrite.Length);
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
We use the GetFile()
method, which returns a file from the given path.
Next, we open a stream to the file and check if we can write to it.
The last thing we do is writing to the stream, we use Write()
method, which write a byte array to the stream.
In order to create files, you can use the VFSManager.CreateFile
method.
try
{
Sys.FileSystem.VFS.VFSManager.CreateFile(@"0:\hello_from_elia.txt");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
This will create a new, empty file. Now we can check our files list and see our new file in it:
- The virtual file system won't work unless you change the .vmdk file of your VM to this if you are on VMware and this if you are on Hyper-V (currently nothing has been done for VirtualBox so far).
-
Path format: Cosmos uses a DOS-like path format, where each file system has its own drive letter. However, in Cosmos, drive letters are numeric and zero-based, i.e, the first filesystem will be
0:\
. - No USB support: Cosmos does not support USB.
⚠ Warning
Do not use this on real hardware; improper usage of the VFS or bugs/unintended behavior can irrevocably corrupt existing files on your hard drive(s). The VFS implementation of Cosmos is still under construction and should be regarded as unstable. For testing, it's recommended to use a virtual machine.