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

Handles the file operations on the ext2 filesystem. More...

#include <ext2.h>

Inheritance diagram for MaxOS::filesystem::format::ext2::Ext2File:
MaxOS::filesystem::File

Public Member Functions

 Ext2File (Ext2Volume *volume, uint32_t inode, const string &name)
 Construct a new Ext2 File object.
 
void write (common::buffer_t *data, size_t amount) final
 write data to the file (at the current seek position, updated to be += amount)
 
void read (common::buffer_t *data, size_t amount) final
 read data from the file (at the current seek position, updated to be += amount)
 
void flush () final
 Flush the file to the disk.
 
- Public Member Functions inherited from MaxOS::filesystem::File
void seek (SeekType seek_type, size_t offset)
 Seek to a position in the file.
 
uint32_t position () const
 Get where the file is currently at (amount read/write/seeked)
 
size_t size () const
 Get the size of the file.
 
string name ()
 Get the name of the file.
 

Additional Inherited Members

- Protected Attributes inherited from MaxOS::filesystem::File
uint32_t m_offset = 0
 The current offset in the file.
 
string m_name
 The name of the file.
 
size_t m_size = 0
 The size of the file.
 

Detailed Description

Handles the file operations on the ext2 filesystem.

Definition at line 402 of file ext2.h.

Constructor & Destructor Documentation

◆ Ext2File()

Ext2File::Ext2File ( Ext2Volume volume,
uint32_t  inode,
const string name 
)

Construct a new Ext2 File object.

Parameters
volumeThe volume the file exists on
inodeThe inode number of the file (will be used to setup the internal InodeHandler)
nameThe name of the file

Definition at line 725 of file ext2.cpp.

726 : m_volume(volume),
727 m_inode(volume, inode) {
728
729 // Set up the base information
730 m_name = name;
731 m_size = m_inode.size();
732
733}
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
size_t m_size
The size of the file.
Definition filesystem.h:36
string m_name
The name of the file.
Definition filesystem.h:35
string name()
Get the name of the file.
size_t size() const
read the size upper and lower into a single size_t
Definition ext2.cpp:529

References MaxOS::filesystem::File::m_name, MaxOS::filesystem::File::m_size, MaxOS::filesystem::File::name(), and MaxOS::filesystem::format::ext2::InodeHandler::size().

Member Function Documentation

◆ flush()

void Ext2File::flush ( )
finalvirtual

Flush the file to the disk.

Reimplemented from MaxOS::filesystem::File.

Definition at line 841 of file ext2.cpp.

841 {
842 File::flush();
843}
virtual void flush()
Flush the file to the disk.

References MaxOS::filesystem::File::flush().

◆ read()

void Ext2File::read ( common::buffer_t data,
size_t  amount 
)
finalvirtual

read data from the file (at the current seek position, updated to be += amount)

Parameters
dataThe byte buffer to read into
amountThe amount of data to read

Reimplemented from MaxOS::filesystem::File.

Definition at line 795 of file ext2.cpp.

795 {
796
797 // Nothing to read
798 if(m_size == 0 || amount == 0)
799 return;
800
801 // Prepare for reading
802 m_volume->ext2_lock.lock();
803 const uint32_t block_size = m_volume->block_size;
804 buffer_t buffer(block_size);
805
806 // Force bounds
807 if(m_offset + amount > m_size)
809
810 // Convert bytes to blocks
811 uint32_t block_start = m_offset / block_size;
812 uint32_t block_offset = m_offset % block_size;
813
814 // Read each block
815 size_t current_block = block_start;
816 size_t read = 0;
817 while(read < amount) {
818
819 // Read the block
821 m_volume->read_block(block, &buffer);
822
823 // Where in this block to start reading
824 size_t buffer_start = (current_block - 1 == block_start) ? block_offset : 0;
825 size_t readable = (amount - read < block_size - buffer_start) ? (amount - read) : (block_size - buffer_start);
826
827 // Read the block
828 buffer.copy_to(data, readable, buffer_start, read);
829 read += readable;
830
831 }
832
833 // Clean up
834 m_offset += amount;
835 m_volume->ext2_lock.unlock();
836}
Wrapper class for a region of bytes in memor in an attempt to add some memory safety....
Definition buffer.h:25
void lock()
Lock the spinlock once it is available.
Definition spinlock.cpp:24
void unlock()
Unlock the spinlock.
Definition spinlock.cpp:32
uint32_t m_offset
The current offset in the file.
Definition filesystem.h:34
void read(common::buffer_t *data, size_t amount) final
read data from the file (at the current seek position, updated to be += amount)
Definition ext2.cpp:795
common::Spinlock ext2_lock
Lock for synchronised access to the volume (.
Definition ext2.h:350
size_t block_size
How large each block is (in bytes)
Definition ext2.h:342
void read_block(uint32_t block_num, common::buffer_t *buffer) const
Reads a single block from the disk into a buffer.
Definition ext2.cpp:129
common::Vector< uint32_t > block_cache
All the blocks used by this inode.
Definition ext2.h:387

References MaxOS::filesystem::format::ext2::InodeHandler::block_cache, MaxOS::filesystem::format::ext2::Ext2Volume::block_size, MaxOS::common::Buffer::copy_to(), MaxOS::filesystem::format::ext2::Ext2Volume::ext2_lock, MaxOS::common::Spinlock::lock(), MaxOS::filesystem::File::m_offset, MaxOS::filesystem::File::m_size, read(), MaxOS::filesystem::format::ext2::Ext2Volume::read_block(), and MaxOS::common::Spinlock::unlock().

Referenced by read().

◆ write()

void Ext2File::write ( common::buffer_t data,
size_t  amount 
)
finalvirtual

write data to the file (at the current seek position, updated to be += amount)

Parameters
dataThe byte buffer to write
amountThe amount of data to write

Reimplemented from MaxOS::filesystem::File.

Definition at line 741 of file ext2.cpp.

741 {
742
743 // Nothing to write
744 if(amount == 0)
745 return;
746
747 // Prepare for writing
748 m_volume->ext2_lock.lock();
749 const uint32_t block_size = m_volume->block_size;
750 buffer_t buffer(block_size);
751
752 // Expand the file
753 if(m_offset + amount > m_size)
754 m_size = m_inode.grow((m_offset + amount) - m_size, false);
755
756 // Save the updated metadata
757 m_inode.inode.last_modification_time = time_to_epoch(Clock::active_clock()->get_time());
758 m_inode.save();
759
760 // Convert bytes to blocks
761 uint32_t block_start = m_offset / block_size;
762 uint32_t block_offset = m_offset % block_size;
763
764 // Write each block
765 size_t current_block = block_start;
766 size_t written = 0;
767 while(written < amount) {
768
769 // Read the block
771 m_volume->read_block(block, &buffer);
772
773 // Where in this block to start writing
774 size_t buffer_start = (current_block - 1 == block_start) ? block_offset : 0;
775 size_t writable = (amount - written < block_size - buffer_start) ? (amount - written) : (block_size -
777
778 // Update the block
779 buffer.copy_from(data, writable, buffer_start, written);
780 m_volume->write_block(block, &buffer);
781 written += writable;
782 }
783
784 // Clean up
785 m_offset += amount;
786 m_volume->ext2_lock.unlock();
787}
static Clock * active_clock()
Gets the currently active clock.
Definition clock.cpp:208
void write_block(uint32_t block_num, common::buffer_t *buffer) const
write a single block from a buffer into onto the disk
Definition ext2.cpp:80
inode_t inode
The inode metadata for this file/directory.
Definition ext2.h:386
size_t grow(size_t amount, bool flush=true)
Increase the size of the inode's storage capacity by allocating new blocks.
Definition ext2.cpp:675
void save()
Writes the inode meta data to disk.
Definition ext2.cpp:697

References MaxOS::drivers::clock::Clock::active_clock(), MaxOS::filesystem::format::ext2::InodeHandler::block_cache, MaxOS::filesystem::format::ext2::Ext2Volume::block_size, MaxOS::common::Buffer::copy_from(), MaxOS::filesystem::format::ext2::Ext2Volume::ext2_lock, MaxOS::filesystem::format::ext2::InodeHandler::grow(), MaxOS::filesystem::format::ext2::InodeHandler::inode, MaxOS::common::Spinlock::lock(), MaxOS::filesystem::File::m_offset, MaxOS::filesystem::File::m_size, MaxOS::filesystem::format::ext2::Ext2Volume::read_block(), MaxOS::filesystem::format::ext2::InodeHandler::save(), MaxOS::common::Spinlock::unlock(), and MaxOS::filesystem::format::ext2::Ext2Volume::write_block().


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