Today I just implemented more functions in Virtual File System.
remove() function is very simple. Remove the file from disk physically, and remove the file in cache. The hard part here is implementing the physical removal of file, but thankfully my past self already implemented it in my past operating system project.
void fat16::fat16_driver::remove(const general_file_name file_name) {
dword starting_cluster_location = (sfn_entry.starting_cluster_high << 16)|sfn_entry.starting_cluster_low;
dword cluster_count = sfn_entry.file_size/(vbr.sectors_per_cluster*vbr.bytes_per_sector)
+(sfn_entry.file_size%(vbr.sectors_per_cluster*vbr.bytes_per_sector) != 0);
char sfn_name[12] = {0 , };
fat::create_sfn_name(sfn_name , file_name.file_name , 1);
if(fat::mark_entry_removed(rootdir_file_loc->block_device , rootdir_file_loc->block_location , sfn_name , ginfo) == false) return false;
dword cluster_ptr = starting_cluster_location;
for(dword i = 0; i < cluster_count; i++) {
dword next_cluster = fat::find_next_cluster(rootdir_file_loc->block_device , cluster_ptr , ginfo);
fat::write_cluster_info(rootdir_file_loc->block_device , cluster_ptr , 0x00 , ginfo);
cluster_ptr = next_cluster;
}
}
It's quite short thanks to some vfs magic.
Now for vfs part, we just need to remove the file_info object from the tree structure of parent directory(file_list)...
bool vfs::remove(const general_file_name file_name) {
...
char base_file_name[strlen(file_path.file_name)+2];
// Get the file name with directory paths all parsed out
vfs_mgr->get_file_base_name(file_path.file_name , base_file_name);
// Call the file system driver
if(parent_loc->fs_driver->remove({base_file_name , parent_dir}) == false) return false;
// Remove corresponding file_info object from the parent directory
vfs_mgr->remove_object(base_file_name , parent_dir);
}
We're done!