File System

Содержание

In this article we will discuss about using Cosmos VFS (virtual file system). Cosmos VFS and the VFS manager classes, let you manage your file system.

Attention: Always format your drive with Cosmos and only Cosmos if you plan to use it with Cosmos. Using any other tool such as Parted, FDisk (or any other tool) might lead to weird things when using that drive with Cosmos’ VFS. Those tools are much more advanced and might format and read/write to the disk differently than Cosmos.

First, we should create and initialize an instance of the VFS, this will initialize the partition and files-system lists, as well as register the new VFS. This is essential for using the VFS.

We start with creating a global CosmosVFS, this line should appear outside of any function, and before the BeforeRun() function.

Next, we register our VFS at the VFS manager, this will initiate the VFS and make it usable, add this to your kernel’s BeforeRun() function:

After the initialization process is done, a message like this would appear on your screen: Initialize

This message is printed by the RegisterVFS() method and it provides info about the partition.

Note: From now on, we’ll be using some plugged functions from System.IO , so be sure to use that reference to your code. Alright, now, let’s get started over some useful functions:

Format drive

Note: You don’t have to format your drive if you’re debugging your Cosmos project with VMWare. The build will automatically add an already formatted FAT32 VMDK file for your convenience.

Note 2: You can only format a drive that already has been formatted with FAT32.

You can format your drive with the Format() function, just like this:

Attention: Don’t add anything after the drive id, or formatting won’t work. You just need to put the drive id, here we put 0 to format the main drive.

Get available free space

We use this function to get the size of the available free space in our file system, in bytes.

Free Space

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 using DOS drive naming system and this is why we use 0.

Attention: Typing 0:/ instead of 0: might lead to errors, you’ve been warned.

Get file system type

This will let us know what is the file system type that we are using. You should be seeing FAT32, if you see other types of FAT like FAT16 or FAT12, then the virtual disk has probably been formatted with one of those FAT types, but remember, the best supported one is FAT32.

System Type

Get files list

We start by getting a list of files, using:

Once we have it, we can get the names of our files:

Files List

Read all the files in a directory

This one is more tricky, We need to get a list of files and print all of their content to the screen.

Of course, we’ll start with geting that files list:

Now we can go through our list, and print the raw content of each file.

Read File

Create new file

Reading and writing is working on existing files, but it’s much more useful to write to our own files. Let’s jump right into it:

We can also check our files list and see our new file in it.

Create File

Write to file

Now we will write to an existing file. Writing to a file is almost the same as reading from a file. Always remember that we should put our code in a try catch block.

Read specific file

Now we will read a specific file from a given path.
As usual, we’ll do it in a try catch block.


Источник: cosmosos.github.io