Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::filesystem::VirtualFileSystem Class Reference

Combines all the filesystems across the partitions on each disk into a single filesystems and exposes a single API to interact with them. More...

#include <vfs.h>

Public Member Functions

 VirtualFileSystem ()
 Construct a new Virtual File System object and set it as the current file system.
 
 ~VirtualFileSystem ()
 Destroy the Virtual File System object and unmount all filesystems.
 
void mount_filesystem (FileSystem *filesystem)
 Add a filesystem to the virtual file system.
 
void mount_filesystem (FileSystem *filesystem, const string &mount_point)
 Add a filesystem to the virtual file system at a given mount point.
 
void unmount_filesystem (FileSystem *filesystem)
 Remove a filesystem from the virtual file system & delete it.
 
void unmount_filesystem (const string &mount_point)
 Remove a filesystem from the virtual file system.
 
void unmount_all ()
 Remove all mounted filesystems.
 
Directoryroot_directory ()
 Get the root directory of the virtual file system.
 
FileSystemroot_filesystem ()
 Get the filesystem mounted at the root.
 
FileSystemget_filesystem (const string &mount_point)
 Get a specific filesystem mounted at a given mount point.
 
FileSystemfind_filesystem (string path)
 Find the filesystem that is responsible for a given path.
 
string get_relative_path (FileSystem *filesystem, string path)
 Get the relative path on a filesystem for a given VFS path (ie remove the mount point)
 
Directoryopen_directory (const string &path)
 Try to open a directory on the virtual file system and read it's contents.
 
Directorycreate_directory (string path)
 Attempts to open the parent directory and creates the sub directory at the end of the path.
 
void delete_directory (string path)
 Attempts to open the parent directory and deletes the sub directory at the end of the path.
 
Filecreate_file (const string &path)
 Attempts to open the parent directory and create the file at the end of the path.
 
Fileopen_file (const string &path, size_t offset=0)
 Try to open a file on the virtual file system with a given offset.
 
void delete_file (const string &path)
 Opens a directory on the vfs and deletes the file at the end of the path.
 

Static Public Member Functions

static VirtualFileSystemcurrent_file_system ()
 Get the active virtual file system.
 
static Directoryopen_directory (Directory *parent, const string &name)
 Opens a subdirectory from a parent directory.
 
static Directorycreate_directory (Directory *parent, const string &name)
 Creates a subdirectory in the specified directory and.
 
static void delete_directory (Directory *parent, const string &name)
 Delete a directory on the virtual file system and it's sub contents.
 
static void delete_directory (Directory *parent, Directory *directory)
 Delete a directory on the virtual file system and it's sub contents.
 
static Filecreate_file (Directory *parent, const string &name)
 Create a file in a directory.
 
static Fileopen_file (Directory *parent, const string &name, size_t offset=0)
 Opens a file in a directory with the given offset.
 
static void delete_file (Directory *parent, const string &name)
 Delete a file in the given directory.
 

Detailed Description

Combines all the filesystems across the partitions on each disk into a single filesystems and exposes a single API to interact with them.

Definition at line 25 of file vfs.h.

Constructor & Destructor Documentation

◆ VirtualFileSystem()

VirtualFileSystem::VirtualFileSystem ( )

Construct a new Virtual File System object and set it as the current file system.

Note
Only call once

Definition at line 21 of file vfs.cpp.

21 {
22
23 // Set the current file system to this instance
24 s_current_file_system = this;
25
26}

◆ ~VirtualFileSystem()

VirtualFileSystem::~VirtualFileSystem ( )

Destroy the Virtual File System object and unmount all filesystems.

Definition at line 31 of file vfs.cpp.

31 {
32
33 // Remove all mounted filesystems
35
36 // Set the current file system to null
37 s_current_file_system = nullptr;
38
39}
void unmount_all()
Remove all mounted filesystems.
Definition vfs.cpp:129

References unmount_all().

Member Function Documentation

◆ create_directory() [1/2]

Directory * VirtualFileSystem::create_directory ( Directory parent,
const string name 
)
static

Creates a subdirectory in the specified directory and.

Parameters
parentWhere to create the directory
nameThe name of the new directory
Returns
The created directory

Definition at line 322 of file vfs.cpp.

322 {
323
324 // Create the directory
325 Directory* directory = parent->create_subdirectory(name);
326 directory->read_from_disk();
327
328 return directory;
329}
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
Handles a group of files (directory)
Definition filesystem.h:57
virtual void read_from_disk()
read the directory from the disk

References MaxOS::filesystem::Directory::read_from_disk().

◆ create_directory() [2/2]

Directory * VirtualFileSystem::create_directory ( string  path)

Attempts to open the parent directory and creates the sub directory at the end of the path.

Parameters
pathThe path to the directory
Returns
The directory object or null if it could not be opened

Definition at line 293 of file vfs.cpp.

293 {
294
295 // Ensure a valid path is given
296 if (!Path::valid(path))
297 return nullptr;
298
299 path = path.strip('/');
300
301 // Try to find the filesystem that is responsible for the path
303 if (!fs)
304 return nullptr;
305
306 // Open the parent directory
307 Directory* parent_directory = open_directory(path);
308 if (!parent_directory)
309 return nullptr;
310
312 return create_directory(parent_directory, directory_name);
313}
Handles the disk operations and file system information.
Definition filesystem.h:96
static bool valid(const string &path)
Check if a path is valid.
Definition path.cpp:21
static string file_name(const string &path)
Get the file name component of a path if it exists or an empty string.
Definition path.cpp:54
Directory * open_directory(const string &path)
Try to open a directory on the virtual file system and read it's contents.
Definition vfs.cpp:245
FileSystem * find_filesystem(string path)
Find the filesystem that is responsible for a given path.
Definition vfs.cpp:191
Directory * create_directory(string path)
Attempts to open the parent directory and creates the sub directory at the end of the path.
Definition vfs.cpp:293

References create_directory(), MaxOS::filesystem::Path::file_name(), find_filesystem(), open_directory(), and MaxOS::filesystem::Path::valid().

Referenced by create_directory(), and MaxOS::filesystem::VFSResourceRegistry::create_resource().

◆ create_file() [1/2]

File * VirtualFileSystem::create_file ( const string path)

Attempts to open the parent directory and create the file at the end of the path.

Parameters
pathThe path to the file (including the extension)
Returns
The file object or null if it could not be created

Definition at line 425 of file vfs.cpp.

425 {
426
427 // Ensure a valid path is given
428 if (!Path::valid(path))
429 return nullptr;
430
431 // Open the directory
432 Directory* directory = open_directory(path);
433 if (!directory)
434 return nullptr;
435
436 // Create the file
437 string file_name = Path::file_name(path);
438 return create_file(directory, file_name);
439}
File * create_file(const string &path)
Attempts to open the parent directory and create the file at the end of the path.
Definition vfs.cpp:425

References create_file(), MaxOS::filesystem::Path::file_name(), open_directory(), and MaxOS::filesystem::Path::valid().

Referenced by create_file(), and MaxOS::filesystem::VFSResourceRegistry::create_resource().

◆ create_file() [2/2]

File * VirtualFileSystem::create_file ( Directory parent,
const string name 
)
static

Create a file in a directory.

Parameters
parentThe directory where the file should be created
nameThe name of the file to create
Returns
The created file or null pointer if it could not be created

Definition at line 448 of file vfs.cpp.

448 {
449 return parent->create_file(name);
450}

◆ current_file_system()

VirtualFileSystem * VirtualFileSystem::current_file_system ( )
static

Get the active virtual file system.

Returns
The current virtual file system or null if none is set

Definition at line 46 of file vfs.cpp.

46 {
47 return s_current_file_system;
48}

Referenced by MaxOS::filesystem::partition::MSDOSPartition::mount_partitions().

◆ delete_directory() [1/3]

void VirtualFileSystem::delete_directory ( Directory parent,
const string name 
)
static

Delete a directory on the virtual file system and it's sub contents.

Parameters
parentThe directory that contains the reference to the directory being deleted
nameThe name of the directory to delete

Definition at line 361 of file vfs.cpp.

361 {
362
363 // Find the directory and delete it
364 for (const auto& directory: parent->subdirectories())
365 if (directory->name() == name)
366 delete_directory(parent, directory);
367
368}
void delete_directory(string path)
Attempts to open the parent directory and deletes the sub directory at the end of the path.
Definition vfs.cpp:337

References delete_directory().

◆ delete_directory() [2/3]

void VirtualFileSystem::delete_directory ( Directory parent,
Directory directory 
)
static

Delete a directory on the virtual file system and it's sub contents.

Parameters
parentThe directory that contains the reference to the directory being deleted
directoryThe the directory to delete

Definition at line 376 of file vfs.cpp.

376 {
377
378 // Nothing to delete
379 if (!directory)
380 return;
381
382 // Store a reference to each subdirectory and its parent
385 stack.push_back(parent, directory);
386
387 while (!stack.empty()) {
388
389 // Save the current
390 auto current = stack.pop_back();
391 auto current_directory = current.second;
392 current_directory->read_from_disk();
393 to_delete.push_back({current.first, current_directory});
394
395 // Empty the directory
396 for (const auto &file: current_directory->files())
397 delete_file(current_directory, file->name());
398
399 // Process the subdirectories
400 for (const auto &subdir: current_directory->subdirectories())
401 if (subdir->name() != "." && subdir->name() != "..")
402 stack.push_back(current_directory, subdir);
403
404 }
405
406 // Delete the directory from the bottom of the tree
407 for (uint32_t i = to_delete.size() - 1; i >= 0; --i) {
408
409 // Get the parent and child
410 const auto &current = to_delete[i];
411 Directory* owner = current.first;
413
414 owner->remove_subdirectory(subdirectory->name());
415 }
416}
void delete_file(const string &path)
Opens a directory on the vfs and deletes the file at the end of the path.
Definition vfs.cpp:499
uint64_t stack[]
The stack setup for the core in loader.s.

References delete_file(), and stack.

◆ delete_directory() [3/3]

void VirtualFileSystem::delete_directory ( string  path)

Attempts to open the parent directory and deletes the sub directory at the end of the path.

Parameters
pathThe path to the directory

Definition at line 337 of file vfs.cpp.

337 {
338
339 // Ensure a valid path is given
340 if (!Path::valid(path))
341 return;
342
343 path = path.strip('/');
344
345 // Open the directory
346 Directory* parent_directory = open_directory(path);
347 if (!parent_directory)
348 return;
349
350 // Delete the directory
352 delete_directory(parent_directory, directory_name);
353}

References delete_directory(), MaxOS::filesystem::Path::file_name(), open_directory(), and MaxOS::filesystem::Path::valid().

Referenced by delete_directory(), and delete_directory().

◆ delete_file() [1/2]

void VirtualFileSystem::delete_file ( const string path)

Opens a directory on the vfs and deletes the file at the end of the path.

Parameters
pathThe path to the file (including the extension)

Definition at line 499 of file vfs.cpp.

499 {
500
501 // Ensure a valid path is given
502 if (!Path::valid(path))
503 return;
504
505 // Open the directory
506 Directory* directory = open_directory(path);
507 if (!directory)
508 return;
509
510 // Delete the file
511 string file_name = Path::file_name(path);
512 delete_file(directory, file_name);
513}

References delete_file(), MaxOS::filesystem::Path::file_name(), open_directory(), and MaxOS::filesystem::Path::valid().

Referenced by delete_directory(), and delete_file().

◆ delete_file() [2/2]

void VirtualFileSystem::delete_file ( Directory parent,
const string name 
)
static

Delete a file in the given directory.

Parameters
parentThe directory containing the file
nameThe name of the file

Definition at line 521 of file vfs.cpp.

521 {
522
523 // Delete the file
524 parent->remove_file(name);
525}

◆ find_filesystem()

FileSystem * VirtualFileSystem::find_filesystem ( string  path)

Find the filesystem that is responsible for a given path.

Parameters
pathThe path to search for
Returns
The filesystem that contains the path or null if none is found

Definition at line 191 of file vfs.cpp.

191 {
192
193 // Longest matching path will be where the filesystem is mounted
194 string longest_match = "";
195 FileSystem* longest_match_fs = nullptr;
196
197 // Search through the filesystems
198 for (const auto& filesystem : filesystems) {
199 // Get the filesystem and mount point
200 FileSystem* fs = filesystem.first;
201 string mount_point = filesystem.second;
202
203 // Check if the path starts with the mount point
204 if (path.starts_with(mount_point) && mount_point.length() > longest_match.length()) {
207 }
208 }
209
210 return longest_match_fs;
211}

Referenced by create_directory(), and open_directory().

◆ get_filesystem()

FileSystem * VirtualFileSystem::get_filesystem ( const string mount_point)

Get a specific filesystem mounted at a given mount point.

Parameters
mount_pointThe mount point to search for
Returns
The filesystem mounted at the given mount point or null if none is found

Definition at line 174 of file vfs.cpp.

174 {
175
176 // Check if the filesystem is mounted
177 for(const auto& filesystem : filesystems)
178 if (filesystem.second == mount_point)
179 return filesystem.first;
180
181 // Filesystem not found
182 return nullptr;
183}

◆ get_relative_path()

string VirtualFileSystem::get_relative_path ( FileSystem filesystem,
string  path 
)

Get the relative path on a filesystem for a given VFS path (ie remove the mount point)

Parameters
filesystemThe filesystem to get the path for
pathThe path to get the relative path for
Returns
The relative path on the filesystem or an empty string if none is found

Definition at line 220 of file vfs.cpp.

220 {
221
222 // Find the mount point for the filesystem
223 auto fs = filesystems.find(filesystem);
224 if (fs == filesystems.end())
225 return "";
226
227 // Get the mount point
228 string mount_point = fs->second;
229
230 // Make sure that the path points to the filesystem
231 if (!path.starts_with(mount_point))
232 return "";
233
234 // Get the relative path
235 string relative_path = path.substring(mount_point.length(), path.length() - mount_point.length());
236 return relative_path;
237}

Referenced by open_directory().

◆ mount_filesystem() [1/2]

void VirtualFileSystem::mount_filesystem ( FileSystem filesystem)

Add a filesystem to the virtual file system.

Parameters
filesystemThe filesystem to add

Definition at line 55 of file vfs.cpp.

55 {
56
57 // Check if the filesystem is already mounted
58 if (filesystems.find(filesystem) != filesystems.end()) {
59 Logger::WARNING() << "Filesystem already mounted\n";
60 return;
61 }
62
63 // Get the mount point for the filesystem
64 string mount_point = "/filesystem_" + filesystems.size();
65
66 // If this is the first filesystem to be mounted, set the root filesystem
67 if (filesystems.size() == 0)
68 mount_point = "/";
69
70 // Add the filesystem to the map
71 filesystems.insert(filesystem, mount_point);
72}
static Logger WARNING()
Gets active logger set to WARNING level.
Definition logger.cpp:205

References MaxOS::Logger::WARNING().

◆ mount_filesystem() [2/2]

void VirtualFileSystem::mount_filesystem ( FileSystem filesystem,
const string mount_point 
)

Add a filesystem to the virtual file system at a given mount point.

Parameters
filesystemThe filesystem to add
mount_pointThe mount point for the filesystem

Definition at line 80 of file vfs.cpp.

80 {
81
82 // Check if the filesystem is already mounted
83 if (filesystems.find(filesystem) != filesystems.end()) {
84 Logger::WARNING() << "Filesystem already mounted at " << mount_point << "\n";
85 return;
86 }
87
88 // Add the filesystem to the map
89 filesystems.insert(filesystem, mount_point);
90}

References MaxOS::Logger::WARNING().

◆ open_directory() [1/2]

Directory * VirtualFileSystem::open_directory ( const string path)

Try to open a directory on the virtual file system and read it's contents.

Parameters
pathThe path to the directory
Returns
The directory object or null if it could not be opened

Definition at line 245 of file vfs.cpp.

245 {
246
247 // Ensure a valid path is given
248 if (!Path::valid(path))
249 return nullptr;
250
251 // Try to find the filesystem that is responsible for the path
253 if (!fs)
254 return nullptr;
255
256 // Get where to open the directory
259
260 // Open the directory
261 Directory* directory = fs->get_directory(directory_path);
262 if (!directory)
263 return nullptr;
264 directory->read_from_disk();
265
266 return directory;
267}
static string file_path(const string &path)
Finds the path to the directory the file is in.
Definition path.cpp:101
string get_relative_path(FileSystem *filesystem, string path)
Get the relative path on a filesystem for a given VFS path (ie remove the mount point)
Definition vfs.cpp:220

References MaxOS::filesystem::Path::file_path(), find_filesystem(), get_relative_path(), MaxOS::filesystem::Directory::read_from_disk(), and MaxOS::filesystem::Path::valid().

Referenced by create_directory(), create_file(), delete_directory(), delete_file(), MaxOS::filesystem::VFSResourceRegistry::get_resource(), and open_file().

◆ open_directory() [2/2]

Directory * VirtualFileSystem::open_directory ( Directory parent,
const string name 
)
static

Opens a subdirectory from a parent directory.

Parameters
parentThe parent directory
nameThe name of the subdirectory
Returns
The opened subdirectory or null if it could not be opened

Definition at line 276 of file vfs.cpp.

276 {
277
278 // Open the file
279 Directory* opened_directory = parent->open_subdirectory(name);
280 if (!opened_directory)
281 return nullptr;
282
283 opened_directory->read_from_disk();
284 return opened_directory;
285}

◆ open_file() [1/2]

File * VirtualFileSystem::open_file ( const string path,
size_t  offset = 0 
)

Try to open a file on the virtual file system with a given offset.

Parameters
pathThe path to the file (including the extension)
offsetThe offset to seek to (default = 0)
Returns
The file or null pointer if not found

Definition at line 459 of file vfs.cpp.

459 {
460
461 // Ensure a valid path is given
462 if (!Path::valid(path))
463 return nullptr;
464
465 // Open the directory
466 Directory* directory = open_directory(path);
467 if (!directory)
468 return nullptr;
469
470 return open_file(directory, Path::file_name(path), offset);
471}
File * open_file(const string &path, size_t offset=0)
Try to open a file on the virtual file system with a given offset.
Definition vfs.cpp:459

References MaxOS::filesystem::Path::file_name(), open_directory(), open_file(), and MaxOS::filesystem::Path::valid().

Referenced by MaxOS::filesystem::VFSResourceRegistry::get_resource(), and open_file().

◆ open_file() [2/2]

File * VirtualFileSystem::open_file ( Directory parent,
const string name,
size_t  offset = 0 
)
static

Opens a file in a directory with the given offset.

Parameters
parentThe directory containing the file
nameThe name of the file to open
offsetHow far in the file to open (default = 0)
Returns
The file or null pointer if not found

Definition at line 481 of file vfs.cpp.

481 {
482
483 // Open the file
484 File* opened_file = parent->open_file(name);
485 if (!opened_file)
486 return nullptr;
487
488 // Seek to the offset
489 opened_file->seek(SeekType::SET, offset);
490
491 return opened_file;
492}
Handles file operations and information.
Definition filesystem.h:31

◆ root_directory()

Directory * VirtualFileSystem::root_directory ( )

Get the root directory of the virtual file system.

Returns
The root directory of the virtual file system

Definition at line 142 of file vfs.cpp.

142 {
143
144 // Get the root filesystem
146 if (!fs)
147 return nullptr;
148
149 // Get the root directory
150 return fs->root_directory();
151}
FileSystem * root_filesystem()
Get the filesystem mounted at the root.
Definition vfs.cpp:158

References root_filesystem().

◆ root_filesystem()

FileSystem * VirtualFileSystem::root_filesystem ( )

Get the filesystem mounted at the root.

Returns
The file system mounted "/" or null if none is mounted

Definition at line 158 of file vfs.cpp.

158 {
159
160 // Ensure there is at least one filesystem mounted
161 if (filesystems.size() == 0)
162 return nullptr;
163
164 // It is always the first filesystem mounted
165 return filesystems.begin()->first;
166}

Referenced by root_directory().

◆ unmount_all()

void VirtualFileSystem::unmount_all ( )

Remove all mounted filesystems.

Definition at line 129 of file vfs.cpp.

129 {
130
131 // Loop through the filesystems and unmount them
132 for(const auto& filesystem : filesystems)
133 unmount_filesystem(filesystem.first);
134
135}
void unmount_filesystem(FileSystem *filesystem)
Remove a filesystem from the virtual file system & delete it.
Definition vfs.cpp:97

References unmount_filesystem().

Referenced by ~VirtualFileSystem().

◆ unmount_filesystem() [1/2]

void VirtualFileSystem::unmount_filesystem ( const string mount_point)

Remove a filesystem from the virtual file system.

Parameters
mount_pointWhere the filesystem is mounted

Definition at line 115 of file vfs.cpp.

115 {
116
117 // Remove the filesystem from the map
118 for(const auto& filesystem : filesystems)
119 if (filesystem.second == mount_point)
120 return unmount_filesystem(filesystem.first);
121
122 // Filesystem not found
123 Logger::WARNING() << "Filesystem not found at " << mount_point << "\n";
124}

References unmount_filesystem(), and MaxOS::Logger::WARNING().

◆ unmount_filesystem() [2/2]

void VirtualFileSystem::unmount_filesystem ( FileSystem filesystem)

Remove a filesystem from the virtual file system & delete it.

Parameters
filesystemThe filesystem to remove

Definition at line 97 of file vfs.cpp.

97 {
98
99 // Check if the filesystem is mounted
100 if (filesystems.find(filesystem) == filesystems.end())
101 return;
102
103 // Remove the filesystem from the map
104 filesystems.erase(filesystem);
105
106 // Delete the filesystem
107 delete filesystem;
108}

Referenced by unmount_all(), and unmount_filesystem().


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