Max OS 0.3
Loading...
Searching...
No Matches
vfs.cpp
Go to the documentation of this file.
1
9#include <filesystem/vfs.h>
10#include <common/logger.h>
11
12using namespace MaxOS;
13using namespace MaxOS::filesystem;
14using namespace MaxOS::common;
15
22
23 // Set the current file system to this instance
24 s_current_file_system = this;
25
26}
27
32
33 // Remove all mounted filesystems
35
36 // Set the current file system to null
37 s_current_file_system = nullptr;
38
39}
40
47 return s_current_file_system;
48}
49
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}
73
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}
91
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}
109
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}
125
130
131 // Loop through the filesystems and unmount them
132 for(const auto& filesystem : filesystems)
133 unmount_filesystem(filesystem.first);
134
135}
136
143
144 // Get the root filesystem
146 if (!fs)
147 return nullptr;
148
149 // Get the root directory
150 return fs->root_directory();
151}
152
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}
167
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}
184
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}
212
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}
238
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}
268
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}
286
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}
314
323
324 // Create the directory
325 Directory* directory = parent->create_subdirectory(name);
326 directory->read_from_disk();
327
328 return directory;
329}
330
331
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}
354
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}
369
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}
417
418
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}
440
449 return parent->create_file(name);
450}
451
459File* VirtualFileSystem::open_file(const string &path, size_t offset) {
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}
472
481File* VirtualFileSystem::open_file(Directory* parent, string const &name, size_t offset) {
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}
493
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}
514
522
523 // Delete the file
524 parent->remove_file(name);
525}
static Logger WARNING()
Gets active logger set to WARNING level.
Definition logger.cpp:205
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
Handles the disk operations and file system information.
Definition filesystem.h:96
Handles file operations and information.
Definition filesystem.h:31
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
static string file_path(const string &path)
Finds the path to the directory the file is in.
Definition path.cpp:101
Combines all the filesystems across the partitions on each disk into a single filesystems and exposes...
Definition vfs.h:25
~VirtualFileSystem()
Destroy the Virtual File System object and unmount all filesystems.
Definition vfs.cpp:31
void unmount_all()
Remove all mounted filesystems.
Definition vfs.cpp:129
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 * root_filesystem()
Get the filesystem mounted at the root.
Definition vfs.cpp:158
FileSystem * get_filesystem(const string &mount_point)
Get a specific filesystem mounted at a given mount point.
Definition vfs.cpp:174
void unmount_filesystem(FileSystem *filesystem)
Remove a filesystem from the virtual file system & delete it.
Definition vfs.cpp:97
void mount_filesystem(FileSystem *filesystem)
Add a filesystem to the virtual file system.
Definition vfs.cpp:55
FileSystem * find_filesystem(string path)
Find the filesystem that is responsible for a given path.
Definition vfs.cpp:191
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
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
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
Directory * root_directory()
Get the root directory of the virtual file system.
Definition vfs.cpp:142
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
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
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
static VirtualFileSystem * current_file_system()
Get the active virtual file system.
Definition vfs.cpp:46
VirtualFileSystem()
Construct a new Virtual File System object and set it as the current file system.
Definition vfs.cpp:21
uint64_t stack[]
The stack setup for the core in loader.s.
Defines a Logger class for logging messages with different severity levels to multiple output streams...
Defines a Virtual File System (VFS) class for managing multiple filesystems and providing a unified i...