mov eax , cr0
or eax , 0x01
mov cr0 , eax



back to months list

Project : The "Microkernel" Operating System

Journal Entry Date : 2024.05.27

Today I just fixed some minor bugs and I implemented read_directory() function.

read_directory() function

This function is used for getting the lists of file from the directory. Initially I thought of the function just putting the file names into the given list.

// Function puts the names of the files in the directory this file_list structure.
// The return of the function is the number of files in the directory.
int vfs::read_directory(file_info *directory , ObjectLinkedList<char*> &file_list) {
    ...
}

I realized that just giving the name of the file would be very inefficient, because you have to search the file again with the vfs function to actually use the file. So, I decided to just give the list of the whole file_info structure, not file names.

But then I realized that we already have that kinds of stuff in the file_info structure! We have file_list structure in file_info that's dedicated to something like this. So, I (again..) decided to make function just put the file informations to the file_list of directory's file_info structure.

// Yes, the only output is the file count. 
// You instead get the real output from the file_list structure.
int vfs::read_directory(file_info *directory) {
    ...
}

This function will create the cache structure(file_info structure) for the uncached files in the directory and store it to the file_list field. There will be no changes to the already existing entries of file_list; only the caches of uncached ones will be newly created.

The interface function will be changed to create the file_info structures and put them to the designated list.

struct file_system_driver {
    ...
    /// @brief Read the directory, save the file_info structure to the linked list
    /// ***Note : This function must create a new file_info structure and store to the linked list. 
    ///           Allocate the new file_info object (for each file) using pmem allocator and 
    ///           store it to the linked list. 
    /// @param file the file_info structure of the directory to read
    /// @param file_list Where the file_info structures are stored
    /// @return Number of files
    virtual int read_directory(file_info *file , ObjectLinkedList<file_info> &file_list) = 0;
    ...
};

Note that above function is not an identical function to vfs::read_directory().

Implementation of read_directory()

The gists of the algorithm of read_directory() function are the followings :

  1. Read the entire list of file_info structure using the file system driver function. (fs_driver->read_directory(...))
  2. Circulate the list and check whether the file_info already exists in the cache list of the directory.
  3. Add (to the cache) the file_info structures that does not exist in the cache.
  4. Discard the structure when it already exists on the cache
  5. Return the number of files

Because what we need to get the file list is the only file_list field in the directory, it is more efficient than constantly searching for the directory and reading the actual disk to get the lists of the file. (Although the disk i/o operations are called when you use read_directory() function, you really do not have to keep call the function because the cache list is automatically renewed for every file operation.)

Implementation of vfs::read_directory()

Now we just need to implement the read_directory function in the FAT12/16 driver.

int fat16::fat16_driver::read_directory(file_info *file , ObjectLinkedList &file_list) {
    physical_file_location *file_loc = fsdev::get_physical_loc_info(file);
    fat::general_fat_info_t ginfo;
    fat16::fat16_vbr_t vbr;

    fat::get_vbr(file_loc->block_device , &vbr , sizeof(fat16::fat16_vbr_t));
    fat16::get_ginfo(ginfo , &vbr);

    return fat::get_file_list(file_loc , file_list , ginfo);
}

It's just a basic function that calls a subroutine that creates the list of file_info structures.

I'll not include the implementation of fat::get_file_list() function, as it is nearly identical to other fat functions. It's basically just reading the whole sector that consists of the directory, parsing all the SFN entries from it and synthesizing those entries into the file_info structure.

Next... things to do?

I'm quite happy how this overall vfs system turned out, but this is not over yet. We have some functions(rename(), remove() and move()) not yet implemented! I really need to quickly implemenet those functions and focus on other stuff such as task management!!

Time is too short for me..