Max OS 0.3
Loading...
Searching...
No Matches
filesystem.cpp
Go to the documentation of this file.
1
10#include <common/logger.h>
11
12using namespace MaxOS;
13using namespace MaxOS::filesystem;
14using namespace MaxOS::common;
15
16File::File() = default;
17
18File::~File() = default;
19
26void File::write(buffer_t* data, size_t amount) {
27}
28
35void File::read(common::buffer_t* data, size_t amount) {
36}
37
42}
43
50void File::seek(SeekType seek_type, size_t offset) {
51
52 // Seek based on the type
53 switch (seek_type) {
54 case SeekType::SET:
55 m_offset = offset;
56 break;
57
58 case SeekType::CURRENT:
59 m_offset += offset;
60 break;
61
62 case SeekType::END:
63 m_offset = size() - offset;
64 break;
65 }
66}
67
74 return m_offset;
75}
76
82string File::name() {
83 return m_name;
84}
85
91size_t File::size() const {
92 return m_size;
93}
94
95Directory::Directory() = default;
96
101
102 // Free the files
103 for (auto &file: m_files)
104 delete file;
105
106 // Free the subdirectories
107 for (auto &subdirectory: m_subdirectories)
108 delete subdirectory;
109
110}
111
118
127
134File* Directory::open_file(const string &name) {
135
136 // Try to find the file
137 for (auto &file: m_files)
138 if (file->name() == name)
139 return file;
140
141 // File not found
142 return nullptr;
143}
144
151File* Directory::create_file(const string &name) {
152 return nullptr;
153}
154
160void Directory::remove_file(const string &name) {
161}
162
171
179
180 // Try to find the directory
181 for (auto &subdirectory: m_subdirectories)
182 if (subdirectory->name() == name) {
183 subdirectory->read_from_disk();
184 return subdirectory;
185 }
186
187 // Directory not found
188 return nullptr;
189}
190
198 return nullptr;
199}
200
205void Directory::remove_subdirectory(const string &name) {
206}
207
208
215 return m_name;
216}
217
224
225 // Sum the size of all the files
226 size_t size = 0;
227 for (auto &file: m_files)
228 size += file->size();
229
230 return size;
231
232}
233
240void Directory::rename_file(File* file, string const &new_name) {
241
242 rename_file(file->name(), new_name);
243
244}
245
252void Directory::rename_file(string const &old_name, string const &new_name) {
253
254 ASSERT(false, "not implemented");
255
256}
257
264void Directory::rename_subdirectory(Directory* directory, string const &new_name) {
265
266 rename_subdirectory(directory->name(), new_name);
267
268}
269
276void Directory::rename_subdirectory(string const &old_name, string const &new_name) {
277 ASSERT(false, "not implemented");
278}
279
280FileSystem::FileSystem() = default;
281
286
287 // Free the root directory
288 delete m_root_directory;
289
290}
291
300
308
309 // Check if the path is the root
310 if (path == "/")
311 return root_directory();
312
313 // Recursively open the directory
314 Directory* directory = root_directory();
315 string directory_path = path;
316 while (directory_path.length() > 0) {
317
318 // Get the name of the directory
320
321 // Open the directory
323 if (!subdirectory)
324 return nullptr;
325
326 // Set the new directory
327 directory = subdirectory;
328
329 // Get the path to the next directory
330 directory_path = directory_path.substring(directory_name.length() + 1, directory_path.length() - directory_name.length() - 1);
331 }
332
333 return directory;
334}
335
342bool FileSystem::exists(const string &path) {
343
344 // Get the directory at the path
347
348 // Check if the directory exists
349 if (!directory)
350 return false;
351
352 // Check if the file exists
353 const string file_name = Path::file_name(path);
354 return directory->open_file(file_name) != nullptr;
355}
Wrapper class for a region of bytes in memor in an attempt to add some memory safety....
Definition buffer.h:25
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
Handles a group of files (directory)
Definition filesystem.h:57
common::Vector< Directory * > m_subdirectories
The subdirectories in this directory.
Definition filesystem.h:60
void rename_file(File *file, const string &new_name)
Rename a file in the directory.
string m_name
The name of this directory.
Definition filesystem.h:62
Directory * open_subdirectory(const string &name)
Open a directory in the directory.
virtual Directory * create_subdirectory(const string &name)
Create a directory in the directory.
File * open_file(const string &name)
Open a file in the directory.
virtual void remove_subdirectory(const string &name)
Try to remove a directory in the directory.
size_t size()
Get the size of the directory.
void rename_subdirectory(Directory *directory, const string &new_name)
Rename a subdirectory in the directory.
common::Vector< File * > m_files
The files in this directory.
Definition filesystem.h:59
common::Vector< Directory * > subdirectories()
Get the subdirectories in the directory.
virtual void remove_file(const string &name)
Delete a file in the directory.
common::Vector< File * > files()
Get the files in the directory.
virtual void read_from_disk()
read the directory from the disk
string name()
Get the name of the directory.
virtual File * create_file(const string &name)
Create a file in the directory.
virtual ~Directory()
Destructor for Directory, frees all files and subdirectories.
Directory * get_directory(const string &path)
Get the directory at a given path.
bool exists(const string &path)
Check if a path exists on the filesystem.
Directory * m_root_directory
The fist directory in the file system (not be confused with the system root)
Definition filesystem.h:98
virtual ~FileSystem()
Destructor for FileSystem, frees the root directory (Which in turn frees all files and subdirectories...
Directory * root_directory()
Get the directory at "/".
Handles file operations and information.
Definition filesystem.h:31
size_t m_size
The size of the file.
Definition filesystem.h:36
virtual void read(common::buffer_t *data, size_t size)
read data from the file
size_t size() const
Get the size of the file.
string m_name
The name of the file.
Definition filesystem.h:35
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)
virtual void write(common::buffer_t *data, size_t size)
write data to the file
uint32_t m_offset
The current offset in the file.
Definition filesystem.h:34
string name()
Get the name of the file.
virtual void flush()
Flush the file to the disk.
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 top_directory(const string &path)
Get the top directory of a path.
Definition path.cpp:125
static string file_path(const string &path)
Finds the path to the directory the file is in.
Definition path.cpp:101
Defines a generic API for a file system, including classes for File, Directory, and FileSystem.
syscore::filesystem::SeekType SeekType
Seek type for file operations.
Definition filesystem.h:25
Defines a Logger class for logging messages with different severity levels to multiple output streams...
#define ASSERT(condition, format,...)
If the specified condition is not met then the kernel will crash with the specified message.
Definition logger.h:100