Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::filesystem::format::Fat32Directory Class Referencefinal

Handles the directory operations on the FAT32 filesystem. More...

#include <fat32.h>

Inheritance diagram for MaxOS::filesystem::format::Fat32Directory:
MaxOS::filesystem::Directory

Public Member Functions

 Fat32Directory (Fat32Volume *volume, lba_t cluster, const string &name)
 Construct a new Fat32 Directory object.
 
void read_from_disk () final
 read the directory from the disk
 
Filecreate_file (const string &name) final
 Create a new file in the directory.
 
void remove_file (const string &name) final
 Delete a file from the subdirectory.
 
Directorycreate_subdirectory (const string &name) final
 Create a new directory in the directory.
 
void remove_subdirectory (const string &name) final
 Remove a directory entry from the directory.
 
lba_t first_cluster () const
 Get the first cluster of the directory.
 
- Public Member Functions inherited from MaxOS::filesystem::Directory
virtual ~Directory ()
 Destructor for Directory, frees all files and subdirectories.
 
common::Vector< File * > files ()
 Get the files in the directory.
 
common::Vector< Directory * > subdirectories ()
 Get the subdirectories in the directory.
 
Fileopen_file (const string &name)
 Open a file in the directory.
 
Directoryopen_subdirectory (const string &name)
 Open a directory in the directory.
 
void rename_file (File *file, const string &new_name)
 Rename a file in the directory.
 
virtual void rename_file (const string &old_name, const string &new_name)
 Rename a file in the directory.
 
void rename_subdirectory (Directory *directory, const string &new_name)
 Rename a subdirectory in the directory.
 
virtual void rename_subdirectory (const string &old_name, const string &new_name)
 Rename a subdirectory in the directory.
 
string name ()
 Get the name of the directory.
 
size_t size ()
 Get the size of the directory.
 

Protected Member Functions

void save_entry_to_disk (dir_entry_t *entry)
 Writes an updated directory entry to the disk.
 
void update_entry_on_disk (size_t index)
 Save the directory entry at the given index to the disk.
 

Friends

class Fat32File
 

Additional Inherited Members

- Protected Attributes inherited from MaxOS::filesystem::Directory
common::Vector< File * > m_files
 The files in this directory.
 
common::Vector< Directory * > m_subdirectories
 The subdirectories in this directory.
 
string m_name
 The name of this directory.
 

Detailed Description

Handles the directory operations on the FAT32 filesystem.

Definition at line 235 of file fat32.h.

Constructor & Destructor Documentation

◆ Fat32Directory()

Fat32Directory::Fat32Directory ( Fat32Volume volume,
lba_t  cluster,
const string name 
)

Construct a new Fat32 Directory object.

Parameters
volumeThe FAT32 volume
clusterThe cluster of the directory
nameThe name of the directory

Definition at line 424 of file fat32.cpp.

425 : m_volume(volume),
426 m_first_cluster(cluster) {
427 m_name = name;
428}
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
string m_name
The name of this directory.
Definition filesystem.h:62
string name()
Get the name of the directory.

References MaxOS::filesystem::Directory::m_name, and MaxOS::filesystem::Directory::name().

Member Function Documentation

◆ create_file()

File * Fat32Directory::create_file ( const string name)
finalvirtual

Create a new file in the directory.

Parameters
nameThe name of the file to create
Returns
The new file object or null if it could not be created

Reimplemented from MaxOS::filesystem::Directory.

Definition at line 890 of file fat32.cpp.

890 {
891
892 // Check if the file already exists
893 for(auto& file : m_files)
894 if(file->name() == name)
896
897 // Check if the name is too long
898 if(name.length() > MAX_NAME_LENGTH)
899 return nullptr;
900
901 // Create the file
902 auto file = new Fat32File(m_volume, this, create_entry(name, false), name);
903 m_files.push_back(file);
904 return file;
905}
common::Vector< File * > m_files
The files in this directory.
Definition filesystem.h:59
Handles the file operations on the FAT32 filesystem.
Definition fat32.h:207

References MaxOS::filesystem::Directory::m_files, MaxOS::filesystem::format::MAX_NAME_LENGTH, and MaxOS::filesystem::Directory::name().

◆ create_subdirectory()

Directory * Fat32Directory::create_subdirectory ( const string name)
finalvirtual

Create a new directory in the directory.

Parameters
nameThe name of the directory to create
Returns
The new directory object or null if it could not be created

Reimplemented from MaxOS::filesystem::Directory.

Definition at line 933 of file fat32.cpp.

933 {
934
935 // Check if the directory already exists
936 for(auto& subdirectory : m_subdirectories)
937 if(subdirectory->name() == name)
939
940 // Check if the name is too long
941 if(name.length() > MAX_NAME_LENGTH)
942 return nullptr;
943
944 // Create the directory
945 auto entry = create_entry(name, true);
946 uint32_t cluster = ((entry->first_cluster_high << 16) | entry->first_cluster_low);
947
948 // Store the directory
949 auto directory = new Fat32Directory(m_volume, cluster, name);
950 m_subdirectories.push_back(directory);
951 return directory;
952}
common::Vector< Directory * > m_subdirectories
The subdirectories in this directory.
Definition filesystem.h:60
Handles the directory operations on the FAT32 filesystem.
Definition fat32.h:235

References MaxOS::filesystem::Directory::m_subdirectories, MaxOS::filesystem::format::MAX_NAME_LENGTH, and MaxOS::filesystem::Directory::name().

◆ first_cluster()

lba_t MaxOS::filesystem::format::Fat32Directory::first_cluster ( ) const
inline

Get the first cluster of the directory.

Returns
The first cluster of the directory

Definition at line 279 of file fat32.h.

279{ return m_first_cluster; }

Referenced by remove_file(), and remove_subdirectory().

◆ read_from_disk()

void Fat32Directory::read_from_disk ( )
finalvirtual

read the directory from the disk

Reimplemented from MaxOS::filesystem::Directory.

Definition at line 829 of file fat32.cpp.

829 {
830
831 for(auto& file : m_files)
832 delete file;
833 m_files.clear();
834
835 for(auto& directory : m_subdirectories)
836 delete directory;
837 m_subdirectories.clear();
838
839 // Load the entries from the disk into memory
840 read_all_entries();
841
842 // Parse the entries
843 string long_name = "";
844 for(auto& entry : m_entries) {
845
846 // Skip free entries and volume labels
847 if(entry.name[0] == (uint8_t) DirectoryEntryType::FREE
848 || entry.attributes == (uint8_t) DirectoryEntryAttributes::FREE
849 || entry.attributes == (uint8_t) DirectoryEntryAttributes::VOLUME_ID)
850 continue;
851
852 // Extract the long name
853 if(entry.attributes == (uint8_t) DirectoryEntryAttributes::LONG_NAME) {
854 long_name = parse_long_filename((long_file_name_entry_t*) &entry, long_name);
855 continue;
856 }
857
858 bool is_directory = entry.attributes == (uint8_t) DirectoryEntryAttributes::DIRECTORY;
859
860 // Get the name of the entry
861 string name = long_name;
862 if(long_name == "") {
863 name = string(entry.name, 8);
864
865 // Add the extension
866 if(!is_directory)
867 name = name.strip() + "." + string(entry.extension, 3);
868 }
869
870 long_name = "";
871
872 // Get the starting cluster
873 uint32_t start_cluster = (entry.first_cluster_high << 16) | entry.first_cluster_low;
874
875 // Store the file or directory
876 if(is_directory)
877 m_subdirectories.push_back(new Fat32Directory(m_volume, start_cluster, name.strip()));
878 else
879 m_files.push_back(new Fat32File(m_volume, this, &entry, name));
880
881 }
882}
struct PACKED MaxOS::filesystem::format::LongFileNameEntry long_file_name_entry_t
Alias for LongFileNameEntry struct.
class MaxOS::String string
Typedef for String.

References MaxOS::filesystem::Directory::m_files, MaxOS::filesystem::Directory::m_subdirectories, and MaxOS::filesystem::Directory::name().

◆ remove_file()

void Fat32Directory::remove_file ( const string name)
finalvirtual

Delete a file from the subdirectory.

Parameters
nameThe name of the file to delete

Reimplemented from MaxOS::filesystem::Directory.

Definition at line 912 of file fat32.cpp.

912 {
913 // Find the file if it exists
914 for(auto& file : m_files)
915 if(file->name() == name) {
916
917 // Remove the file from the directory
918 m_files.erase(file);
919 remove_entry(((Fat32File*) file)->first_cluster());
920
921 // Delete the file reference
922 delete file;
923 return;
924 }
925}
lba_t first_cluster() const
Get the first cluster of the directory.
Definition fat32.h:279

References first_cluster(), MaxOS::filesystem::Directory::m_files, and MaxOS::filesystem::Directory::name().

◆ remove_subdirectory()

void Fat32Directory::remove_subdirectory ( const string name)
finalvirtual

Remove a directory entry from the directory.

Parameters
nameThe name of the entry to remove

Reimplemented from MaxOS::filesystem::Directory.

Definition at line 959 of file fat32.cpp.

959 {
960 // Find the directory if it exists
961 for(auto& subdirectory : m_subdirectories) {
962 if(subdirectory->name() != name)
963 continue;
964
965 // Remove all the files in the directory
966 for(auto& file : subdirectory->files())
967 subdirectory->remove_file(file->name());
968
969 // Remove all the subdirectories in the directory
972
973 // Remove the entry
975 remove_entry(((Fat32Directory*) subdirectory)->first_cluster());
976
977 // Delete the directory
978 delete subdirectory;
979 return;
980 }
981}
common::Vector< Directory * > subdirectories()
Get the subdirectories in the directory.
common::Vector< File * > files()
Get the files in the directory.
void remove_file(const string &name) final
Delete a file from the subdirectory.
Definition fat32.cpp:912
void remove_subdirectory(const string &name) final
Remove a directory entry from the directory.
Definition fat32.cpp:959

References first_cluster(), MaxOS::filesystem::Directory::m_subdirectories, and MaxOS::filesystem::Directory::name().

◆ save_entry_to_disk()

void Fat32Directory::save_entry_to_disk ( dir_entry_t entry)
protected

Writes an updated directory entry to the disk.

Parameters
entryThe entry to write

Definition at line 589 of file fat32.cpp.

589 {
590
591 int index = 0;
592 for(auto& m_entry : m_entries) {
593 if(&m_entry == entry)
594 break;
595 index++;
596 }
597
599}
void update_entry_on_disk(size_t index)
Save the directory entry at the given index to the disk.
Definition fat32.cpp:605

References update_entry_on_disk().

Referenced by MaxOS::filesystem::format::Fat32File::write().

◆ update_entry_on_disk()

void Fat32Directory::update_entry_on_disk ( size_t  index)
protected

Save the directory entry at the given index to the disk.

Parameters
indexThe index of the entry to update

Definition at line 605 of file fat32.cpp.

605 {
606
607 // Get the entry
608 auto entry = m_entries[index];
609
610 // Determine sector offset and in-sector byte offset
611 uint32_t bytes_per_sector = m_volume->bpb.bytes_per_sector;
612 uint32_t entry_offset = index * sizeof(dir_entry_t);
613 uint32_t sector_offset = entry_offset / bytes_per_sector;
614 uint32_t in_sector_offset = entry_offset % bytes_per_sector;
615
616 // Find which cluster has the entry
617 uint32_t cluster = m_first_cluster;
619 offset_remaining >= bytes_per_sector; offset_remaining -= bytes_per_sector)
620 cluster = m_volume->next_cluster(cluster);
621
622 // Read the full sector into a buffer
623 lba_t base_lba = m_volume->data_lba + (cluster - 2) * m_volume->bpb.sectors_per_cluster;
624 buffer_t sector_buffer(bytes_per_sector, false);
626
627 // Update the entry in the buffer
628 sector_buffer.copy_from(&entry, sizeof(dir_entry_t), in_sector_offset);
630}
Wrapper class for a region of bytes in memor in an attempt to add some memory safety....
Definition buffer.h:25
void write(uint32_t sector, common::buffer_t *data)
write data to the disk from a buffer (max capacity 512 bytes)
Definition disk.cpp:50
void read(uint32_t sector, common::buffer_t *data_buffer)
read data from the disk into a buffer (max capacity 512 bytes)
Definition disk.cpp:26
uint32_t next_cluster(uint32_t cluster) const
Take the cluster and gets the next cluster in the chain.
Definition fat32.cpp:65
drivers::disk::Disk * disk
The disk that this volume is on.
Definition fat32.h:187
bpb32_t bpb
The BIOS Parameter Block for the FAT32 volume.
Definition fat32.h:176
lba_t data_lba
The starting LBA of the data region.
Definition fat32.h:184
struct PACKED MaxOS::filesystem::format::DirectoryEntry dir_entry_t
Alias for DirectoryEntry struct.
uint32_t lba_t
Logical Block Addressing type.
Definition filesystem.h:24

References MaxOS::filesystem::format::Fat32Volume::bpb, MaxOS::filesystem::format::Fat32Volume::data_lba, MaxOS::filesystem::format::Fat32Volume::disk, MaxOS::filesystem::format::Fat32Volume::next_cluster(), MaxOS::drivers::disk::Disk::read(), and MaxOS::drivers::disk::Disk::write().

Referenced by save_entry_to_disk().

Friends And Related Symbol Documentation

◆ Fat32File

friend class Fat32File
friend

Definition at line 236 of file fat32.h.


The documentation for this class was generated from the following files: