Maybe you want to add a file system to your operating system. We all do. But as that is not possible with Cosmos, we will be creating a virtual file system. A virtual file system, or VFS, is essentially a file system wrapper, hence virtual. As there is not file system to wrap, the VFS is alone. Lets get started.

Step 1 - New Project
We will need to create a new project to host our virtual file system. It doesn't matter the Cosmos version, as we have tutorials for both. We favor to use JoshDOS (Milestone 4 only) for our new project template, as it comes prepackaged with Cosmos.Hardware and a simple command prompt. As we create these tutorials assuming that you have sufficient C# and Visual Studio knowledge, we will let you create a new project without us holding your hand.

Step 2 - File object
We now need to add a file object. This will allow our file system to have files. Alliteratively, you could have several arrays or lists holding the names and content, but this way is more clean. Create a new class and add the following code:

        public file()
        {
        }

        public override string ToString()
        {
            return _name + " - " + _content;
        }

        public int fileSize()
        {
            int final = 0;

            for (int i = 0; i < _content.Length; i++)
            {
                final += 7;
            }

            final = final / 8;

            return final;
        }

        public string FileNameAndSize()
        {
            string final = "";

            final = _name + " - " + fileSize().ToString() + " bytes";

            return final;
        }

        private string _name;
        private string _content;

        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }

        public string Content
        {
            get
            {
                return _content;
            }
            set
            {
                _content = value;
            }
        }
Lets see what it does. The ToString() override void returns the file name and content. FileNameAndSize() will return us the name of the file and the size. The fileSize() int simply return the size of the file in bytes. There are properties for the Content and the Name of the file.

Step 3 - Lots of files
Now that we have a file structure, we can make use of it. Create either a list or array, (both approaches are described later) of the file object. Now we have a list:

List<file> vfs = new List<file>();
or an array:

file[10000] vfs = new file[];

We prefer the list method because the List object has an Add() method, which the array does not have. It is untested whether ArrayList will work, but we assume it will.

Step 4 - File operations
Technically, we now have a virtual file system, but it is not being used. We need to add some file operations as seen in the VFS Lab. Add the following code to your command structure:

                if (command == "new file")
                {
                    Console.Write("Name your file: ");
                    string fileName = Console.ReadLine();
                    Console.Write("Put some crap in your file: ");
                    string crap = Console.ReadLine();

                    file newFile = new file();
                    newFile.Content = crap;
                    newFile.Name = fileName;

                    fs.Add(newFile);

                    newFile = null;
                }
                else if (command == "help")
                {
                    Console.WriteLine("new file:  Create new file");
                    Console.WriteLine("list:  List the files");
                    Console.WriteLine("cls:  Clear the screen");
                    Console.WriteLine("help:  Show this");
                    Console.WriteLine("open [filename]:  open a file");
                }
                else if (command == "list")
                {
                    Console.WriteLine("There are: " + fs.Count + " files");

                    for (int i = 0; i < fs.Count; i++)
                    {
                        Console.WriteLine("\t" + fs[i].FileNameAndSize());
                    }
                }
                else if (command.StartsWith("open"))
                {
                    string filename = command.Substring(5);
                    bool fileFound = false;

                    for (int i = 0; i < fs.Count; i++)
                    {
                        if (fs[i].Name == filename)
                        {
                            Console.WriteLine(fs[i].Content);
                            fileFound = true;
                            break;
                        }
                    }

                    if (fileFound == false)
                    {
                        Console.WriteLine("No such file");
                    }
                }
The above code snippet assumes that your command is a string called command obtained by Console.ReadLine() or however else you get your command.

Whew, thank you for sticking with us through that long tutorial of making a virtual file system for your OS, and you're welcome.

Some ideas:
  • Maybe add directory support for your file system the same way you added file support.
  • You might add file properties such as date created, time created, a size property, or a file type

Last edited Feb 7, 2011 at 9:12 PM by joshbeitler, version 2

Comments

No comments yet.