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

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

#include <fat32.h>

Inheritance diagram for MaxOS::filesystem::format::Fat32File:
MaxOS::filesystem::File

Public Member Functions

 Fat32File (Fat32Volume *volume, Fat32Directory *parent, dir_entry_t *info, const string &name)
 Construct a new Fat32 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.
 
uint32_t first_cluster () const
 Get the first cluster of the directory.
 
- 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 FAT32 filesystem.

Definition at line 207 of file fat32.h.

Constructor & Destructor Documentation

◆ Fat32File()

Fat32File::Fat32File ( Fat32Volume volume,
Fat32Directory parent,
dir_entry_t info,
const string name 
)

Construct a new Fat32 File object.

Parameters
volumeThe helper volume object
parentThe directory that contains this file
infoThe directory entry information that describes this file
nameThe name of the file

Definition at line 239 of file fat32.cpp.

240 : m_volume(volume),
241 m_parent_directory(parent),
242 m_entry(info),
243 m_first_cluster((info->first_cluster_high << 16) | info->first_cluster_low) {
244
245 m_name = name;
246 m_size = info->size;
247 m_offset = 0;
248}
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
uint32_t m_offset
The current offset in the file.
Definition filesystem.h:34
string name()
Get the name of the file.

References MaxOS::filesystem::File::m_name, MaxOS::filesystem::File::m_offset, MaxOS::filesystem::File::m_size, and MaxOS::filesystem::File::name().

Member Function Documentation

◆ first_cluster()

uint32_t MaxOS::filesystem::format::Fat32File::first_cluster ( ) const
inline

Get the first cluster of the directory.

Returns
The first cluster of the directory

Definition at line 228 of file fat32.h.

228{ return m_first_cluster; }

◆ flush()

void Fat32File::flush ( )
finalvirtual

Flush the file to the disk.

Reimplemented from MaxOS::filesystem::File.

Definition at line 413 of file fat32.cpp.

413 {
414 File::flush();
415}
virtual void flush()
Flush the file to the disk.

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

◆ read()

void Fat32File::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 361 of file fat32.cpp.

361 {
362 size_t buffer_space = m_volume->bpb.bytes_per_sector * m_volume->bpb.sectors_per_cluster;
363 buffer_t buffer(buffer_space);
364 buffer.clear();
365
368
369 // Read the file
370 for(uint32_t cluster = m_first_cluster;
371 cluster != (uint32_t) ClusterState::END_OF_CHAIN; cluster = m_volume->next_cluster(cluster)) {
372
373 // Skip clusters before the offset
376 continue;
377 }
378
379 // Read each sector in the cluster
380 lba_t lba = m_volume->data_lba + (cluster - 2) * m_volume->bpb.sectors_per_cluster;
381 for(size_t sector = 0; sector < m_volume->bpb.sectors_per_cluster; sector++)
382 m_volume->disk->read(lba + sector, &buffer, m_volume->bpb.bytes_per_sector);
383 buffer.set_offset(0);
384
385 // If the offset is in the middle of the cluster
386 size_t buffer_offset = 0;
389
390 // Calculate how many bytes are being copied (read from cluster at offset? or read part of cluster?)
396
397 // Read the data
398 buffer.copy_from(data, bytes_to_copy, buffer_offset, bytes_read);
401
402 // Dont read more than needed
403 if(bytes_read >= amount)
404 break;
405 }
406
408}
Wrapper class for a region of bytes in memor in an attempt to add some memory safety....
Definition buffer.h:25
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
uint32_t lba_t
Logical Block Addressing type.
Definition filesystem.h:24

References MaxOS::filesystem::format::Fat32Volume::bpb, MaxOS::common::Buffer::clear(), MaxOS::common::Buffer::copy_from(), MaxOS::filesystem::format::Fat32Volume::data_lba, MaxOS::filesystem::format::Fat32Volume::disk, MaxOS::filesystem::File::m_offset, MaxOS::filesystem::format::Fat32Volume::next_cluster(), MaxOS::drivers::disk::Disk::read(), and MaxOS::common::Buffer::set_offset().

◆ write()

void Fat32File::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
Todo:
When in userspace: save timestamps

Reimplemented from MaxOS::filesystem::File.

Definition at line 260 of file fat32.cpp.

260 {
261
262 size_t buffer_space = m_volume->bpb.bytes_per_sector * m_volume->bpb.sectors_per_cluster;
263 buffer_t buffer(buffer_space);
264 buffer.clear();
265
268 uint32_t last = m_first_cluster;
269
270 // Read the file
271 for(uint32_t cluster = last;
272 cluster != (uint32_t) ClusterState::END_OF_CHAIN; cluster = m_volume->next_cluster(cluster)) {
273 last = cluster;
274
275 // No cluster to read from (blank file)
276 if(cluster == 0)
277 break;
278
279 // Skip clusters before the offset
282 continue;
283 }
284
285 // Read each sector in the cluster (prevent overwriting the data)
286 lba_t lba = m_volume->data_lba + (cluster - 2) * m_volume->bpb.sectors_per_cluster;
287 for(size_t sector = 0; sector < m_volume->bpb.sectors_per_cluster; sector++)
288 m_volume->disk->read(lba + sector, &buffer, m_volume->bpb.bytes_per_sector);
289 buffer.set_offset(0);
290
291 // If the offset is in the middle of the cluster
292 size_t buffer_offset = 0;
295
296 // Calculate how many bytes are being copied (read from cluster at offset?
297 // or read part of cluster?)
303
304 // Update the data
305 buffer.copy_from(data, bytes_to_copy, buffer_offset, bytes_written);
308 buffer.set_offset(0);
309
310 // Write the data back to the disk
311 for(size_t sector = 0; sector < m_volume->bpb.sectors_per_cluster; sector++)
312 m_volume->disk->write(lba + sector, &buffer, m_volume->bpb.bytes_per_sector);
313 }
314
315 // Extend the file
316 while(bytes_written < amount) {
317 // Allocate a new cluster
319 if(new_cluster == 0)
320 break;
321
322 if(last == 0)
323 m_first_cluster = new_cluster;
324
325 // Update the data
327 buffer.copy_from(data, bytes_to_copy, 0, bytes_written);
330 buffer.set_offset(0);
331
332 // Write the data back to the disk
333 lba_t lba = m_volume->data_lba + (new_cluster - 2) * m_volume->bpb.sectors_per_cluster;
334 for(size_t sector = 0; sector < m_volume->bpb.sectors_per_cluster; sector++)
335 m_volume->disk->write(lba + sector, &buffer, m_volume->bpb.bytes_per_sector);
336
337 // Go to the next cluster
339 }
340
341 // Update file size
343 if(m_offset > m_size)
345
346 // Update entry info
347 m_entry->size = m_size;
348 m_entry->first_cluster_high = (m_first_cluster >> 16) & 0xFFFF;
349 m_entry->first_cluster_low = m_first_cluster & 0xFFFF;
350
351 m_parent_directory->save_entry_to_disk(m_entry);
352
353}
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 save_entry_to_disk(dir_entry_t *entry)
Writes an updated directory entry to the disk.
Definition fat32.cpp:589
uint32_t allocate_cluster(uint32_t cluster)
Allocate a cluster in the FAT table.
Definition fat32.cpp:141

References MaxOS::filesystem::format::Fat32Volume::allocate_cluster(), MaxOS::filesystem::format::Fat32Volume::bpb, MaxOS::common::Buffer::clear(), MaxOS::common::Buffer::copy_from(), MaxOS::filesystem::format::Fat32Volume::data_lba, MaxOS::filesystem::format::Fat32Volume::disk, MaxOS::filesystem::File::m_offset, MaxOS::filesystem::File::m_size, MaxOS::filesystem::format::Fat32Volume::next_cluster(), MaxOS::drivers::disk::Disk::read(), MaxOS::filesystem::format::Fat32Directory::save_entry_to_disk(), MaxOS::common::Buffer::set_offset(), and MaxOS::drivers::disk::Disk::write().


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